blob: c48da8e3d71a1118cec7b066799addc52357412b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Multibyte extensions partly by Sung-Hoon Baek
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * mbyte.c: Code specifically for handling multi-byte characters.
12 *
13 * The encoding used in the core is set with 'encoding'. When 'encoding' is
14 * changed, the following four variables are set (for speed).
15 * Currently these types of character encodings are supported:
16 *
17 * "enc_dbcs" When non-zero it tells the type of double byte character
18 * encoding (Chinese, Korean, Japanese, etc.).
19 * The cell width on the display is equal to the number of
20 * bytes. (exception: DBCS_JPNU with first byte 0x8e)
21 * Recognizing the first or second byte is difficult, it
22 * requires checking a byte sequence from the start.
23 * "enc_utf8" When TRUE use Unicode characters in UTF-8 encoding.
24 * The cell width on the display needs to be determined from
25 * the character value.
26 * Recognizing bytes is easy: 0xxx.xxxx is a single-byte
27 * char, 10xx.xxxx is a trailing byte, 11xx.xxxx is a leading
28 * byte of a multi-byte character.
29 * To make things complicated, up to two composing characters
30 * are allowed. These are drawn on top of the first char.
31 * For most editing the sequence of bytes with composing
32 * characters included is considered to be one character.
33 * "enc_unicode" When 2 use 16-bit Unicode characters (or UTF-16).
34 * When 4 use 32-but Unicode characters.
35 * Internally characters are stored in UTF-8 encoding to
36 * avoid NUL bytes. Conversion happens when doing I/O.
37 * "enc_utf8" will also be TRUE.
38 *
39 * "has_mbyte" is set when "enc_dbcs" or "enc_utf8" is non-zero.
40 *
41 * If none of these is TRUE, 8-bit bytes are used for a character. The
42 * encoding isn't currently specified (TODO).
43 *
44 * 'encoding' specifies the encoding used in the core. This is in registers,
45 * text manipulation, buffers, etc. Conversion has to be done when characters
46 * in another encoding are received or send:
47 *
48 * clipboard
49 * ^
50 * | (2)
51 * V
52 * +---------------+
53 * (1) | | (3)
54 * keyboard ----->| core |-----> display
55 * | |
56 * +---------------+
57 * ^
58 * | (4)
59 * V
60 * file
61 *
62 * (1) Typed characters arrive in the current locale. Conversion is to be
63 * done when 'encoding' is different from 'termencoding'.
64 * (2) Text will be made available with the encoding specified with
65 * 'encoding'. If this is not sufficient, system-specific conversion
66 * might be required.
67 * (3) For the GUI the correct font must be selected, no conversion done.
68 * Otherwise, conversion is to be done when 'encoding' differs from
69 * 'termencoding'. (Different in the GTK+ 2 port -- 'termencoding'
70 * is always used for both input and output and must always be set to
71 * "utf-8". gui_mch_init() does this automatically.)
72 * (4) The encoding of the file is specified with 'fileencoding'. Conversion
73 * is to be done when it's different from 'encoding'.
74 *
75 * The viminfo file is a special case: Only text is converted, not file names.
76 * Vim scripts may contain an ":encoding" command. This has an effect for
77 * some commands, like ":menutrans"
78 */
79
80#include "vim.h"
81
82#ifdef WIN32UNIX
83# ifndef WIN32_LEAN_AND_MEAN
84# define WIN32_LEAN_AND_MEAN
85# endif
86# include <windows.h>
87# ifdef WIN32
88# undef WIN32 /* Some windows.h define WIN32, we don't want that here. */
89# endif
90#endif
91
92#if (defined(WIN3264) || defined(WIN32UNIX)) && !defined(__MINGW32__)
93# include <winnls.h>
94#endif
95
96#ifdef FEAT_GUI_X11
97# include <X11/Intrinsic.h>
98#endif
99#ifdef X_LOCALE
100#include <X11/Xlocale.h>
101#endif
102
Bram Moolenaar754b5602006-02-09 23:53:20 +0000103#if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104# include <gdk/gdkkeysyms.h>
105# ifdef WIN3264
106# include <gdk/gdkwin32.h>
107# else
108# include <gdk/gdkx.h>
109# endif
110#endif
111
112#ifdef HAVE_WCHAR_H
113# include <wchar.h>
114#endif
115
116#if 0
117/* This has been disabled, because several people reported problems with the
118 * wcwidth() and iswprint() library functions, esp. for Hebrew. */
119# ifdef __STDC_ISO_10646__
120# define USE_WCHAR_FUNCTIONS
121# endif
122#endif
123
124#if defined(FEAT_MBYTE) || defined(PROTO)
125
126static int enc_canon_search __ARGS((char_u *name));
127static int dbcs_char2len __ARGS((int c));
128static int dbcs_char2bytes __ARGS((int c, char_u *buf));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000129static int dbcs_ptr2len __ARGS((char_u *p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static int dbcs_char2cells __ARGS((int c));
131static int dbcs_ptr2char __ARGS((char_u *p));
132
133/* Lookup table to quickly get the length in bytes of a UTF-8 character from
134 * the first byte of a UTF-8 string. Bytes which are illegal when used as the
135 * first byte have a one, because these will be used separately. */
136static char utf8len_tab[256] =
137{
138 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
139 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
140 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
141 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
142 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*bogus*/
143 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*bogus*/
144 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
145 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1,
146};
147
148/*
149 * XIM often causes trouble. Define XIM_DEBUG to get a log of XIM callbacks
150 * in the "xim.log" file.
151 */
152/* #define XIM_DEBUG */
153#ifdef XIM_DEBUG
154 static void
155xim_log(char *s, ...)
156{
157 va_list arglist;
158 static FILE *fd = NULL;
159
160 if (fd == (FILE *)-1)
161 return;
162 if (fd == NULL)
163 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000164 fd = mch_fopen("xim.log", "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 if (fd == NULL)
166 {
167 EMSG("Cannot open xim.log");
168 fd = (FILE *)-1;
169 return;
170 }
171 }
172
173 va_start(arglist, s);
174 vfprintf(fd, s, arglist);
175 va_end(arglist);
176}
177#endif
178
179#endif
180
181#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
182/*
183 * Canonical encoding names and their properties.
184 * "iso-8859-n" is handled by enc_canonize() directly.
185 */
186static struct
187{ char *name; int prop; int codepage;}
188enc_canon_table[] =
189{
190#define IDX_LATIN_1 0
191 {"latin1", ENC_8BIT + ENC_LATIN1, 1252},
192#define IDX_ISO_2 1
193 {"iso-8859-2", ENC_8BIT, 0},
194#define IDX_ISO_3 2
195 {"iso-8859-3", ENC_8BIT, 0},
196#define IDX_ISO_4 3
197 {"iso-8859-4", ENC_8BIT, 0},
198#define IDX_ISO_5 4
199 {"iso-8859-5", ENC_8BIT, 0},
200#define IDX_ISO_6 5
201 {"iso-8859-6", ENC_8BIT, 0},
202#define IDX_ISO_7 6
203 {"iso-8859-7", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000204#define IDX_ISO_8 7
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {"iso-8859-8", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000206#define IDX_ISO_9 8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {"iso-8859-9", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000208#define IDX_ISO_10 9
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {"iso-8859-10", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000210#define IDX_ISO_11 10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {"iso-8859-11", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000212#define IDX_ISO_13 11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 {"iso-8859-13", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000214#define IDX_ISO_14 12
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 {"iso-8859-14", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000216#define IDX_ISO_15 13
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000217 {"iso-8859-15", ENC_8BIT + ENC_LATIN9, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000218#define IDX_KOI8_R 14
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 {"koi8-r", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000220#define IDX_KOI8_U 15
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 {"koi8-u", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000222#define IDX_UTF8 16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 {"utf-8", ENC_UNICODE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000224#define IDX_UCS2 17
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {"ucs-2", ENC_UNICODE + ENC_ENDIAN_B + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000226#define IDX_UCS2LE 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {"ucs-2le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000228#define IDX_UTF16 19
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {"utf-16", ENC_UNICODE + ENC_ENDIAN_B + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000230#define IDX_UTF16LE 20
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {"utf-16le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000232#define IDX_UCS4 21
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {"ucs-4", ENC_UNICODE + ENC_ENDIAN_B + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000234#define IDX_UCS4LE 22
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {"ucs-4le", ENC_UNICODE + ENC_ENDIAN_L + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000236
237 /* For debugging DBCS encoding on Unix. */
238#define IDX_DEBUG 23
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {"debug", ENC_DBCS, DBCS_DEBUG},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000240#define IDX_EUC_JP 24
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {"euc-jp", ENC_DBCS, DBCS_JPNU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000242#define IDX_SJIS 25
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {"sjis", ENC_DBCS, DBCS_JPN},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000244#define IDX_EUC_KR 26
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {"euc-kr", ENC_DBCS, DBCS_KORU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000246#define IDX_EUC_CN 27
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {"euc-cn", ENC_DBCS, DBCS_CHSU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000248#define IDX_EUC_TW 28
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {"euc-tw", ENC_DBCS, DBCS_CHTU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000250#define IDX_BIG5 29
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 {"big5", ENC_DBCS, DBCS_CHT},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000252
253 /* MS-DOS and MS-Windows codepages are included here, so that they can be
254 * used on Unix too. Most of them are similar to ISO-8859 encodings, but
255 * not exactly the same. */
256#define IDX_CP437 30
257 {"cp437", ENC_8BIT, 437}, /* like iso-8859-1 */
258#define IDX_CP737 31
259 {"cp737", ENC_8BIT, 737}, /* like iso-8859-7 */
260#define IDX_CP775 32
261 {"cp775", ENC_8BIT, 775}, /* Baltic */
262#define IDX_CP850 33
263 {"cp850", ENC_8BIT, 850}, /* like iso-8859-4 */
264#define IDX_CP852 34
265 {"cp852", ENC_8BIT, 852}, /* like iso-8859-1 */
266#define IDX_CP855 35
267 {"cp855", ENC_8BIT, 855}, /* like iso-8859-2 */
268#define IDX_CP857 36
269 {"cp857", ENC_8BIT, 857}, /* like iso-8859-5 */
270#define IDX_CP860 37
271 {"cp860", ENC_8BIT, 860}, /* like iso-8859-9 */
272#define IDX_CP861 38
273 {"cp861", ENC_8BIT, 861}, /* like iso-8859-1 */
274#define IDX_CP862 39
275 {"cp862", ENC_8BIT, 862}, /* like iso-8859-1 */
276#define IDX_CP863 40
277 {"cp863", ENC_8BIT, 863}, /* like iso-8859-8 */
278#define IDX_CP865 41
279 {"cp865", ENC_8BIT, 865}, /* like iso-8859-1 */
280#define IDX_CP866 42
281 {"cp866", ENC_8BIT, 866}, /* like iso-8859-5 */
282#define IDX_CP869 43
283 {"cp869", ENC_8BIT, 869}, /* like iso-8859-7 */
284#define IDX_CP874 44
285 {"cp874", ENC_8BIT, 874}, /* Thai */
286#define IDX_CP932 45
287 {"cp932", ENC_DBCS, DBCS_JPN},
288#define IDX_CP936 46
289 {"cp936", ENC_DBCS, DBCS_CHS},
290#define IDX_CP949 47
291 {"cp949", ENC_DBCS, DBCS_KOR},
292#define IDX_CP950 48
293 {"cp950", ENC_DBCS, DBCS_CHT},
294#define IDX_CP1250 49
295 {"cp1250", ENC_8BIT, 1250}, /* Czech, Polish, etc. */
296#define IDX_CP1251 50
297 {"cp1251", ENC_8BIT, 1251}, /* Cyrillic */
298 /* cp1252 is considered to be equal to latin1 */
299#define IDX_CP1253 51
300 {"cp1253", ENC_8BIT, 1253}, /* Greek */
301#define IDX_CP1254 52
302 {"cp1254", ENC_8BIT, 1254}, /* Turkish */
303#define IDX_CP1255 53
304 {"cp1255", ENC_8BIT, 1255}, /* Hebrew */
305#define IDX_CP1256 54
306 {"cp1256", ENC_8BIT, 1256}, /* Arabic */
307#define IDX_CP1257 55
308 {"cp1257", ENC_8BIT, 1257}, /* Baltic */
309#define IDX_CP1258 56
310 {"cp1258", ENC_8BIT, 1258}, /* Vietnamese */
311
312#define IDX_MACROMAN 57
313 {"macroman", ENC_8BIT + ENC_MACROMAN, 0}, /* Mac OS */
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000314#define IDX_DECMCS 58
315 {"dec-mcs", ENC_8BIT, 0}, /* DEC MCS */
316#define IDX_HPROMAN8 59
317 {"hp-roman8", ENC_8BIT, 0}, /* HP Roman8 */
318#define IDX_COUNT 60
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319};
320
321/*
322 * Aliases for encoding names.
323 */
324static struct
325{ char *name; int canon;}
326enc_alias_table[] =
327{
328 {"ansi", IDX_LATIN_1},
329 {"iso-8859-1", IDX_LATIN_1},
330 {"latin2", IDX_ISO_2},
331 {"latin3", IDX_ISO_3},
332 {"latin4", IDX_ISO_4},
333 {"cyrillic", IDX_ISO_5},
334 {"arabic", IDX_ISO_6},
335 {"greek", IDX_ISO_7},
336#ifdef WIN3264
337 {"hebrew", IDX_CP1255},
338#else
339 {"hebrew", IDX_ISO_8},
340#endif
341 {"latin5", IDX_ISO_9},
342 {"turkish", IDX_ISO_9}, /* ? */
343 {"latin6", IDX_ISO_10},
344 {"nordic", IDX_ISO_10}, /* ? */
345 {"thai", IDX_ISO_11}, /* ? */
346 {"latin7", IDX_ISO_13},
347 {"latin8", IDX_ISO_14},
348 {"latin9", IDX_ISO_15},
349 {"utf8", IDX_UTF8},
350 {"unicode", IDX_UCS2},
351 {"ucs2", IDX_UCS2},
352 {"ucs2be", IDX_UCS2},
353 {"ucs-2be", IDX_UCS2},
354 {"ucs2le", IDX_UCS2LE},
355 {"utf16", IDX_UTF16},
356 {"utf16be", IDX_UTF16},
357 {"utf-16be", IDX_UTF16},
358 {"utf16le", IDX_UTF16LE},
359 {"ucs4", IDX_UCS4},
360 {"ucs4be", IDX_UCS4},
361 {"ucs-4be", IDX_UCS4},
362 {"ucs4le", IDX_UCS4LE},
363 {"932", IDX_CP932},
364 {"949", IDX_CP949},
365 {"936", IDX_CP936},
366 {"950", IDX_CP950},
367 {"eucjp", IDX_EUC_JP},
368 {"unix-jis", IDX_EUC_JP},
369 {"ujis", IDX_EUC_JP},
370 {"shift-jis", IDX_SJIS},
371 {"euckr", IDX_EUC_KR},
372 {"5601", IDX_EUC_KR}, /* Sun: KS C 5601 */
373 {"euccn", IDX_EUC_CN},
374 {"gb2312", IDX_EUC_CN},
375 {"euctw", IDX_EUC_TW},
376#if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
377 {"japan", IDX_CP932},
378 {"korea", IDX_CP949},
379 {"prc", IDX_CP936},
380 {"chinese", IDX_CP936},
381 {"taiwan", IDX_CP950},
382 {"big5", IDX_CP950},
383#else
384 {"japan", IDX_EUC_JP},
385 {"korea", IDX_EUC_KR},
386 {"prc", IDX_EUC_CN},
387 {"chinese", IDX_EUC_CN},
388 {"taiwan", IDX_EUC_TW},
389 {"cp950", IDX_BIG5},
390 {"950", IDX_BIG5},
391#endif
392 {"mac", IDX_MACROMAN},
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000393 {"mac-roman", IDX_MACROMAN},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {NULL, 0}
395};
396
397#ifndef CP_UTF8
398# define CP_UTF8 65001 /* magic number from winnls.h */
399#endif
400
401/*
402 * Find encoding "name" in the list of canonical encoding names.
403 * Returns -1 if not found.
404 */
405 static int
406enc_canon_search(name)
407 char_u *name;
408{
409 int i;
410
411 for (i = 0; i < IDX_COUNT; ++i)
412 if (STRCMP(name, enc_canon_table[i].name) == 0)
413 return i;
414 return -1;
415}
416
417#endif
418
419#if defined(FEAT_MBYTE) || defined(PROTO)
420
421/*
422 * Find canonical encoding "name" in the list and return its properties.
423 * Returns 0 if not found.
424 */
425 int
426enc_canon_props(name)
427 char_u *name;
428{
429 int i;
430
431 i = enc_canon_search(name);
432 if (i >= 0)
433 return enc_canon_table[i].prop;
434#ifdef WIN3264
435 if (name[0] == 'c' && name[1] == 'p' && VIM_ISDIGIT(name[2]))
436 {
437 CPINFO cpinfo;
438
439 /* Get info on this codepage to find out what it is. */
440 if (GetCPInfo(atoi(name + 2), &cpinfo) != 0)
441 {
442 if (cpinfo.MaxCharSize == 1) /* some single-byte encoding */
443 return ENC_8BIT;
444 if (cpinfo.MaxCharSize == 2
445 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
446 /* must be a DBCS encoding */
447 return ENC_DBCS;
448 }
449 return 0;
450 }
451#endif
452 if (STRNCMP(name, "2byte-", 6) == 0)
453 return ENC_DBCS;
454 if (STRNCMP(name, "8bit-", 5) == 0 || STRNCMP(name, "iso-8859-", 9) == 0)
455 return ENC_8BIT;
456 return 0;
457}
458
459/*
460 * Set up for using multi-byte characters.
461 * Called in three cases:
462 * - by main() to initialize (p_enc == NULL)
463 * - by set_init_1() after 'encoding' was set to its default.
464 * - by do_set() when 'encoding' has been set.
465 * p_enc must have been passed through enc_canonize() already.
466 * Sets the "enc_unicode", "enc_utf8", "enc_dbcs" and "has_mbyte" flags.
467 * Fills mb_bytelen_tab[] and returns NULL when there are no problems.
468 * When there is something wrong: Returns an error message and doesn't change
469 * anything.
470 */
471 char_u *
472mb_init()
473{
474 int i;
475 int idx;
476 int n;
477 int enc_dbcs_new = 0;
478#if defined(USE_ICONV) && !defined(WIN3264) && !defined(WIN32UNIX) \
479 && !defined(MACOS)
480# define LEN_FROM_CONV
481 vimconv_T vimconv;
482 char_u *p;
483#endif
484
485 if (p_enc == NULL)
486 {
487 /* Just starting up: set the whole table to one's. */
488 for (i = 0; i < 256; ++i)
489 mb_bytelen_tab[i] = 1;
490 input_conv.vc_type = CONV_NONE;
491 input_conv.vc_factor = 1;
492 output_conv.vc_type = CONV_NONE;
493 return NULL;
494 }
495
496#ifdef WIN3264
497 if (p_enc[0] == 'c' && p_enc[1] == 'p' && VIM_ISDIGIT(p_enc[2]))
498 {
499 CPINFO cpinfo;
500
501 /* Get info on this codepage to find out what it is. */
502 if (GetCPInfo(atoi(p_enc + 2), &cpinfo) != 0)
503 {
504 if (cpinfo.MaxCharSize == 1)
505 {
506 /* some single-byte encoding */
507 enc_unicode = 0;
508 enc_utf8 = FALSE;
509 }
510 else if (cpinfo.MaxCharSize == 2
511 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
512 {
513 /* must be a DBCS encoding, check below */
514 enc_dbcs_new = atoi(p_enc + 2);
515 }
516 else
517 goto codepage_invalid;
518 }
519 else if (GetLastError() == ERROR_INVALID_PARAMETER)
520 {
521codepage_invalid:
522 return (char_u *)N_("E543: Not a valid codepage");
523 }
524 }
525#endif
526 else if (STRNCMP(p_enc, "8bit-", 5) == 0
527 || STRNCMP(p_enc, "iso-8859-", 9) == 0)
528 {
529 /* Accept any "8bit-" or "iso-8859-" name. */
530 enc_unicode = 0;
531 enc_utf8 = FALSE;
532 }
533 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
534 {
535#ifdef WIN3264
536 /* Windows: accept only valid codepage numbers, check below. */
537 if (p_enc[6] != 'c' || p_enc[7] != 'p'
538 || (enc_dbcs_new = atoi(p_enc + 8)) == 0)
539 return e_invarg;
540#else
541 /* Unix: accept any "2byte-" name, assume current locale. */
542 enc_dbcs_new = DBCS_2BYTE;
543#endif
544 }
545 else if ((idx = enc_canon_search(p_enc)) >= 0)
546 {
547 i = enc_canon_table[idx].prop;
548 if (i & ENC_UNICODE)
549 {
550 /* Unicode */
551 enc_utf8 = TRUE;
552 if (i & (ENC_2BYTE | ENC_2WORD))
553 enc_unicode = 2;
554 else if (i & ENC_4BYTE)
555 enc_unicode = 4;
556 else
557 enc_unicode = 0;
558 }
559 else if (i & ENC_DBCS)
560 {
561 /* 2byte, handle below */
562 enc_dbcs_new = enc_canon_table[idx].codepage;
563 }
564 else
565 {
566 /* Must be 8-bit. */
567 enc_unicode = 0;
568 enc_utf8 = FALSE;
569 }
570 }
571 else /* Don't know what encoding this is, reject it. */
572 return e_invarg;
573
574 if (enc_dbcs_new != 0)
575 {
576#ifdef WIN3264
577 /* Check if the DBCS code page is OK. */
578 if (!IsValidCodePage(enc_dbcs_new))
579 goto codepage_invalid;
580#endif
581 enc_unicode = 0;
582 enc_utf8 = FALSE;
583 }
584 enc_dbcs = enc_dbcs_new;
585 has_mbyte = (enc_dbcs != 0 || enc_utf8);
586
587#ifdef WIN3264
588 enc_codepage = encname2codepage(p_enc);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000589 enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#endif
591
Bram Moolenaar78622822005-08-23 21:00:13 +0000592 /* Detect an encoding that uses latin1 characters. */
593 enc_latin1like = (enc_utf8 || STRCMP(p_enc, "latin1") == 0
594 || STRCMP(p_enc, "iso-8859-15") == 0);
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 /*
597 * Set the function pointers.
598 */
599 if (enc_utf8)
600 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000601 mb_ptr2len = utfc_ptr2len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 mb_char2len = utf_char2len;
603 mb_char2bytes = utf_char2bytes;
604 mb_ptr2cells = utf_ptr2cells;
605 mb_char2cells = utf_char2cells;
606 mb_off2cells = utf_off2cells;
607 mb_ptr2char = utf_ptr2char;
608 mb_head_off = utf_head_off;
609 }
610 else if (enc_dbcs != 0)
611 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000612 mb_ptr2len = dbcs_ptr2len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 mb_char2len = dbcs_char2len;
614 mb_char2bytes = dbcs_char2bytes;
615 mb_ptr2cells = dbcs_ptr2cells;
616 mb_char2cells = dbcs_char2cells;
617 mb_off2cells = dbcs_off2cells;
618 mb_ptr2char = dbcs_ptr2char;
619 mb_head_off = dbcs_head_off;
620 }
621 else
622 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000623 mb_ptr2len = latin_ptr2len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 mb_char2len = latin_char2len;
625 mb_char2bytes = latin_char2bytes;
626 mb_ptr2cells = latin_ptr2cells;
627 mb_char2cells = latin_char2cells;
628 mb_off2cells = latin_off2cells;
629 mb_ptr2char = latin_ptr2char;
630 mb_head_off = latin_head_off;
631 }
632
633 /*
634 * Fill the mb_bytelen_tab[] for MB_BYTE2LEN().
635 */
636#ifdef LEN_FROM_CONV
637 /* When 'encoding' is different from the current locale mblen() won't
638 * work. Use conversion to "utf-8" instead. */
639 vimconv.vc_type = CONV_NONE;
640 if (enc_dbcs)
641 {
642 p = enc_locale();
643 if (p == NULL || STRCMP(p, p_enc) != 0)
644 {
645 convert_setup(&vimconv, p_enc, (char_u *)"utf-8");
646 vimconv.vc_fail = TRUE;
647 }
648 vim_free(p);
649 }
650#endif
651
652 for (i = 0; i < 256; ++i)
653 {
654 /* Our own function to reliably check the length of UTF-8 characters,
655 * independent of mblen(). */
656 if (enc_utf8)
657 n = utf8len_tab[i];
658 else if (enc_dbcs == 0)
659 n = 1;
660 else
661 {
662#if defined(WIN3264) || defined(WIN32UNIX)
663 /* enc_dbcs is set by setting 'fileencoding'. It becomes a Windows
664 * CodePage identifier, which we can pass directly in to Windows
665 * API */
666 n = IsDBCSLeadByteEx(enc_dbcs, (BYTE)i) ? 2 : 1;
667#else
668# ifdef MACOS
669 /*
670 * if mblen() is not available, character which MSB is turned on
671 * are treated as leading byte character. (note : This assumption
672 * is not always true.)
673 */
674 n = (i & 0x80) ? 2 : 1;
675# else
676 char buf[MB_MAXBYTES];
677# ifdef X_LOCALE
678# ifndef mblen
679# define mblen _Xmblen
680# endif
681# endif
682 if (i == NUL) /* just in case mblen() can't handle "" */
683 n = 1;
684 else
685 {
686 buf[0] = i;
687 buf[1] = 0;
688#ifdef LEN_FROM_CONV
689 if (vimconv.vc_type != CONV_NONE)
690 {
691 /*
692 * string_convert() should fail when converting the first
693 * byte of a double-byte character.
694 */
695 p = string_convert(&vimconv, (char_u *)buf, NULL);
696 if (p != NULL)
697 {
698 vim_free(p);
699 n = 1;
700 }
701 else
702 n = 2;
703 }
704 else
705#endif
706 {
707 /*
708 * mblen() should return -1 for invalid (means the leading
709 * multibyte) character. However there are some platforms
710 * where mblen() returns 0 for invalid character.
711 * Therefore, following condition includes 0.
712 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000713 (void)mblen(NULL, 0); /* First reset the state. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (mblen(buf, (size_t)1) <= 0)
715 n = 2;
716 else
717 n = 1;
718 }
719 }
720# endif
721#endif
722 }
723
724 mb_bytelen_tab[i] = n;
725 }
726
727#ifdef LEN_FROM_CONV
728 convert_setup(&vimconv, NULL, NULL);
729#endif
730
731 /* The cell width depends on the type of multi-byte characters. */
732 (void)init_chartab();
733
734 /* When enc_utf8 is set or reset, (de)allocate ScreenLinesUC[] */
735 screenalloc(FALSE);
736
737 /* When using Unicode, set default for 'fileencodings'. */
738 if (enc_utf8 && !option_was_set((char_u *)"fencs"))
739 set_string_option_direct((char_u *)"fencs", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000740 (char_u *)"ucs-bom,utf-8,default,latin1", OPT_FREE, 0);
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
743 /* GNU gettext 0.10.37 supports this feature: set the codeset used for
744 * translated messages independently from the current locale. */
745 (void)bind_textdomain_codeset(VIMPACKAGE,
746 enc_utf8 ? "utf-8" : (char *)p_enc);
747#endif
748
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000749#ifdef WIN32
750 /* When changing 'encoding' while starting up, then convert the command
751 * line arguments from the active codepage to 'encoding'. */
752 if (starting != 0)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000753 fix_arg_enc();
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000754#endif
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#ifdef FEAT_AUTOCMD
757 /* Fire an autocommand to let people do custom font setup. This must be
758 * after Vim has been setup for the new encoding. */
759 apply_autocmds(EVENT_ENCODINGCHANGED, NULL, (char_u *)"", FALSE, curbuf);
760#endif
761
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000762#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000763 /* Need to reload spell dictionaries */
764 spell_reload();
765#endif
766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 return NULL;
768}
769
770/*
771 * Return the size of the BOM for the current buffer:
772 * 0 - no BOM
773 * 2 - UCS-2 or UTF-16 BOM
774 * 4 - UCS-4 BOM
775 * 3 - UTF-8 BOM
776 */
777 int
778bomb_size()
779{
780 int n = 0;
781
782 if (curbuf->b_p_bomb && !curbuf->b_p_bin)
783 {
784 if (*curbuf->b_p_fenc == NUL)
785 {
786 if (enc_utf8)
787 {
788 if (enc_unicode != 0)
789 n = enc_unicode;
790 else
791 n = 3;
792 }
793 }
794 else if (STRCMP(curbuf->b_p_fenc, "utf-8") == 0)
795 n = 3;
796 else if (STRNCMP(curbuf->b_p_fenc, "ucs-2", 5) == 0
797 || STRNCMP(curbuf->b_p_fenc, "utf-16", 6) == 0)
798 n = 2;
799 else if (STRNCMP(curbuf->b_p_fenc, "ucs-4", 5) == 0)
800 n = 4;
801 }
802 return n;
803}
804
805/*
806 * Get class of pointer:
807 * 0 for blank or NUL
808 * 1 for punctuation
809 * 2 for an (ASCII) word character
810 * >2 for other word characters
811 */
812 int
813mb_get_class(p)
814 char_u *p;
815{
816 if (MB_BYTE2LEN(p[0]) == 1)
817 {
818 if (p[0] == NUL || vim_iswhite(p[0]))
819 return 0;
820 if (vim_iswordc(p[0]))
821 return 2;
822 return 1;
823 }
824 if (enc_dbcs != 0 && p[0] != NUL && p[1] != NUL)
825 return dbcs_class(p[0], p[1]);
826 if (enc_utf8)
827 return utf_class(utf_ptr2char(p));
828 return 0;
829}
830
831/*
832 * Get class of a double-byte character. This always returns 3 or bigger.
833 * TODO: Should return 1 for punctuation.
834 */
835 int
836dbcs_class(lead, trail)
837 unsigned lead;
838 unsigned trail;
839{
840 switch (enc_dbcs)
841 {
842 /* please add classfy routine for your language in here */
843
844 case DBCS_JPNU: /* ? */
845 case DBCS_JPN:
846 {
847 /* JIS code classification */
848 unsigned char lb = lead;
849 unsigned char tb = trail;
850
851 /* convert process code to JIS */
852# if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
853 /* process code is SJIS */
854 if (lb <= 0x9f)
855 lb = (lb - 0x81) * 2 + 0x21;
856 else
857 lb = (lb - 0xc1) * 2 + 0x21;
858 if (tb <= 0x7e)
859 tb -= 0x1f;
860 else if (tb <= 0x9e)
861 tb -= 0x20;
862 else
863 {
864 tb -= 0x7e;
865 lb += 1;
866 }
867# else
868 /*
869 * XXX: Code page identification can not use with all
870 * system! So, some other encoding information
871 * will be needed.
872 * In japanese: SJIS,EUC,UNICODE,(JIS)
873 * Note that JIS-code system don't use as
874 * process code in most system because it uses
875 * escape sequences(JIS is context depend encoding).
876 */
877 /* assume process code is JAPANESE-EUC */
878 lb &= 0x7f;
879 tb &= 0x7f;
880# endif
881 /* exceptions */
882 switch (lb << 8 | tb)
883 {
884 case 0x2121: /* ZENKAKU space */
885 return 0;
886 case 0x2122: /* KU-TEN (Japanese comma) */
887 case 0x2123: /* TOU-TEN (Japanese period) */
888 case 0x2124: /* ZENKAKU comma */
889 case 0x2125: /* ZENKAKU period */
890 return 1;
891 case 0x213c: /* prolongedsound handled as KATAKANA */
892 return 13;
893 }
894 /* sieved by KU code */
895 switch (lb)
896 {
897 case 0x21:
898 case 0x22:
899 /* special symbols */
900 return 10;
901 case 0x23:
902 /* alpha-numeric */
903 return 11;
904 case 0x24:
905 /* hiragana */
906 return 12;
907 case 0x25:
908 /* katakana */
909 return 13;
910 case 0x26:
911 /* greek */
912 return 14;
913 case 0x27:
914 /* russian */
915 return 15;
916 case 0x28:
917 /* lines */
918 return 16;
919 default:
920 /* kanji */
921 return 17;
922 }
923 }
924
925 case DBCS_KORU: /* ? */
926 case DBCS_KOR:
927 {
928 /* KS code classification */
929 unsigned char c1 = lead;
930 unsigned char c2 = trail;
931
932 /*
933 * 20 : Hangul
934 * 21 : Hanja
935 * 22 : Symbols
936 * 23 : Alpha-numeric/Roman Letter (Full width)
937 * 24 : Hangul Letter(Alphabet)
938 * 25 : Roman Numeral/Greek Letter
939 * 26 : Box Drawings
940 * 27 : Unit Symbols
941 * 28 : Circled/Parenthesized Letter
942 * 29 : Hirigana/Katakana
943 * 30 : Cyrillic Letter
944 */
945
946 if (c1 >= 0xB0 && c1 <= 0xC8)
947 /* Hangul */
948 return 20;
949#if defined(WIN3264) || defined(WIN32UNIX)
950 else if (c1 <= 0xA0 || c2 <= 0xA0)
951 /* Extended Hangul Region : MS UHC(Unified Hangul Code) */
952 /* c1: 0x81-0xA0 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xFE
953 * c1: 0xA1-0xC6 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xA0
954 */
955 return 20;
956#endif
957
958 else if (c1 >= 0xCA && c1 <= 0xFD)
959 /* Hanja */
960 return 21;
961 else switch (c1)
962 {
963 case 0xA1:
964 case 0xA2:
965 /* Symbols */
966 return 22;
967 case 0xA3:
968 /* Alpha-numeric */
969 return 23;
970 case 0xA4:
971 /* Hangul Letter(Alphabet) */
972 return 24;
973 case 0xA5:
974 /* Roman Numeral/Greek Letter */
975 return 25;
976 case 0xA6:
977 /* Box Drawings */
978 return 26;
979 case 0xA7:
980 /* Unit Symbols */
981 return 27;
982 case 0xA8:
983 case 0xA9:
984 if (c2 <= 0xAF)
985 return 25; /* Roman Letter */
986 else if (c2 >= 0xF6)
987 return 22; /* Symbols */
988 else
989 /* Circled/Parenthesized Letter */
990 return 28;
991 case 0xAA:
992 case 0xAB:
993 /* Hirigana/Katakana */
994 return 29;
995 case 0xAC:
996 /* Cyrillic Letter */
997 return 30;
998 }
999 }
1000 default:
1001 break;
1002 }
1003 return 3;
1004}
1005
1006/*
1007 * mb_char2len() function pointer.
1008 * Return length in bytes of character "c".
1009 * Returns 1 for a single-byte character.
1010 */
1011/* ARGSUSED */
1012 int
1013latin_char2len(c)
1014 int c;
1015{
1016 return 1;
1017}
1018
1019 static int
1020dbcs_char2len(c)
1021 int c;
1022{
1023 if (c >= 0x100)
1024 return 2;
1025 return 1;
1026}
1027
1028/*
1029 * mb_char2bytes() function pointer.
1030 * Convert a character to its bytes.
1031 * Returns the length in bytes.
1032 */
1033 int
1034latin_char2bytes(c, buf)
1035 int c;
1036 char_u *buf;
1037{
1038 buf[0] = c;
1039 return 1;
1040}
1041
1042 static int
1043dbcs_char2bytes(c, buf)
1044 int c;
1045 char_u *buf;
1046{
1047 if (c >= 0x100)
1048 {
1049 buf[0] = (unsigned)c >> 8;
1050 buf[1] = c;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001051 /* Never use a NUL byte, it causes lots of trouble. It's an invalid
1052 * character anyway. */
1053 if (buf[1] == NUL)
1054 buf[1] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 return 2;
1056 }
1057 buf[0] = c;
1058 return 1;
1059}
1060
1061/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001062 * mb_ptr2len() function pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 * Get byte length of character at "*p" but stop at a NUL.
1064 * For UTF-8 this includes following composing characters.
1065 * Returns 0 when *p is NUL.
1066 *
1067 */
1068 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001069latin_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 char_u *p;
1071{
1072 return MB_BYTE2LEN(*p);
1073}
1074
1075 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001076dbcs_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 char_u *p;
1078{
1079 int len;
1080
1081 /* Check if second byte is not missing. */
1082 len = MB_BYTE2LEN(*p);
1083 if (len == 2 && p[1] == NUL)
1084 len = 1;
1085 return len;
1086}
1087
1088struct interval
1089{
1090 unsigned short first;
1091 unsigned short last;
1092};
1093static int intable __ARGS((struct interval *table, size_t size, int c));
1094
1095/*
1096 * Return TRUE if "c" is in "table[size / sizeof(struct interval)]".
1097 */
1098 static int
1099intable(table, size, c)
1100 struct interval *table;
1101 size_t size;
1102 int c;
1103{
1104 int mid, bot, top;
1105
1106 /* first quick check for Latin1 etc. characters */
1107 if (c < table[0].first)
1108 return FALSE;
1109
1110 /* binary search in table */
1111 bot = 0;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001112 top = (int)(size / sizeof(struct interval) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 while (top >= bot)
1114 {
1115 mid = (bot + top) / 2;
1116 if (table[mid].last < c)
1117 bot = mid + 1;
1118 else if (table[mid].first > c)
1119 top = mid - 1;
1120 else
1121 return TRUE;
1122 }
1123 return FALSE;
1124}
1125
1126/*
1127 * For UTF-8 character "c" return 2 for a double-width character, 1 for others.
1128 * Returns 4 or 6 for an unprintable character.
1129 * Is only correct for characters >= 0x80.
1130 * When p_ambw is "double", return 2 for a character with East Asian Width
1131 * class 'A'(mbiguous).
1132 */
1133 int
1134utf_char2cells(c)
1135 int c;
1136{
1137 /* sorted list of non-overlapping intervals of East Asian Ambiguous
1138 * characters, generated with:
1139 * "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */
1140 static struct interval ambiguous[] = {
1141 {0x00A1, 0x00A1}, {0x00A4, 0x00A4}, {0x00A7, 0x00A8},
1142 {0x00AA, 0x00AA}, {0x00AE, 0x00AE}, {0x00B0, 0x00B4},
1143 {0x00B6, 0x00BA}, {0x00BC, 0x00BF}, {0x00C6, 0x00C6},
1144 {0x00D0, 0x00D0}, {0x00D7, 0x00D8}, {0x00DE, 0x00E1},
1145 {0x00E6, 0x00E6}, {0x00E8, 0x00EA}, {0x00EC, 0x00ED},
1146 {0x00F0, 0x00F0}, {0x00F2, 0x00F3}, {0x00F7, 0x00FA},
1147 {0x00FC, 0x00FC}, {0x00FE, 0x00FE}, {0x0101, 0x0101},
1148 {0x0111, 0x0111}, {0x0113, 0x0113}, {0x011B, 0x011B},
1149 {0x0126, 0x0127}, {0x012B, 0x012B}, {0x0131, 0x0133},
1150 {0x0138, 0x0138}, {0x013F, 0x0142}, {0x0144, 0x0144},
1151 {0x0148, 0x014B}, {0x014D, 0x014D}, {0x0152, 0x0153},
1152 {0x0166, 0x0167}, {0x016B, 0x016B}, {0x01CE, 0x01CE},
1153 {0x01D0, 0x01D0}, {0x01D2, 0x01D2}, {0x01D4, 0x01D4},
1154 {0x01D6, 0x01D6}, {0x01D8, 0x01D8}, {0x01DA, 0x01DA},
1155 {0x01DC, 0x01DC}, {0x0251, 0x0251}, {0x0261, 0x0261},
1156 {0x02C4, 0x02C4}, {0x02C7, 0x02C7}, {0x02C9, 0x02CB},
1157 {0x02CD, 0x02CD}, {0x02D0, 0x02D0}, {0x02D8, 0x02DB},
1158 {0x02DD, 0x02DD}, {0x02DF, 0x02DF}, {0x0391, 0x03A1},
1159 {0x03A3, 0x03A9}, {0x03B1, 0x03C1}, {0x03C3, 0x03C9},
1160 {0x0401, 0x0401}, {0x0410, 0x044F}, {0x0451, 0x0451},
1161 {0x2010, 0x2010}, {0x2013, 0x2016}, {0x2018, 0x2019},
1162 {0x201C, 0x201D}, {0x2020, 0x2022}, {0x2024, 0x2027},
1163 {0x2030, 0x2030}, {0x2032, 0x2033}, {0x2035, 0x2035},
1164 {0x203B, 0x203B}, {0x203E, 0x203E}, {0x2074, 0x2074},
1165 {0x207F, 0x207F}, {0x2081, 0x2084}, {0x20AC, 0x20AC},
1166 {0x2103, 0x2103}, {0x2105, 0x2105}, {0x2109, 0x2109},
1167 {0x2113, 0x2113}, {0x2116, 0x2116}, {0x2121, 0x2122},
1168 {0x2126, 0x2126}, {0x212B, 0x212B}, {0x2153, 0x2154},
1169 {0x215B, 0x215E}, {0x2160, 0x216B}, {0x2170, 0x2179},
1170 {0x2190, 0x2199}, {0x21B8, 0x21B9}, {0x21D2, 0x21D2},
1171 {0x21D4, 0x21D4}, {0x21E7, 0x21E7}, {0x2200, 0x2200},
1172 {0x2202, 0x2203}, {0x2207, 0x2208}, {0x220B, 0x220B},
1173 {0x220F, 0x220F}, {0x2211, 0x2211}, {0x2215, 0x2215},
1174 {0x221A, 0x221A}, {0x221D, 0x2220}, {0x2223, 0x2223},
1175 {0x2225, 0x2225}, {0x2227, 0x222C}, {0x222E, 0x222E},
1176 {0x2234, 0x2237}, {0x223C, 0x223D}, {0x2248, 0x2248},
1177 {0x224C, 0x224C}, {0x2252, 0x2252}, {0x2260, 0x2261},
1178 {0x2264, 0x2267}, {0x226A, 0x226B}, {0x226E, 0x226F},
1179 {0x2282, 0x2283}, {0x2286, 0x2287}, {0x2295, 0x2295},
1180 {0x2299, 0x2299}, {0x22A5, 0x22A5}, {0x22BF, 0x22BF},
1181 {0x2312, 0x2312}, {0x2460, 0x24E9}, {0x24EB, 0x254B},
1182 {0x2550, 0x2573}, {0x2580, 0x258F}, {0x2592, 0x2595},
1183 {0x25A0, 0x25A1}, {0x25A3, 0x25A9}, {0x25B2, 0x25B3},
1184 {0x25B6, 0x25B7}, {0x25BC, 0x25BD}, {0x25C0, 0x25C1},
1185 {0x25C6, 0x25C8}, {0x25CB, 0x25CB}, {0x25CE, 0x25D1},
1186 {0x25E2, 0x25E5}, {0x25EF, 0x25EF}, {0x2605, 0x2606},
1187 {0x2609, 0x2609}, {0x260E, 0x260F}, {0x2614, 0x2615},
1188 {0x261C, 0x261C}, {0x261E, 0x261E}, {0x2640, 0x2640},
1189 {0x2642, 0x2642}, {0x2660, 0x2661}, {0x2663, 0x2665},
1190 {0x2667, 0x266A}, {0x266C, 0x266D}, {0x266F, 0x266F},
1191 {0x273D, 0x273D}, {0x2776, 0x277F}, {0xE000, 0xF8FF},
1192 {0xFFFD, 0xFFFD}, /* {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD} */
1193 };
1194
1195 if (c >= 0x100)
1196 {
1197#ifdef USE_WCHAR_FUNCTIONS
1198 /*
1199 * Assume the library function wcwidth() works better than our own
1200 * stuff. It should return 1 for ambiguous width chars!
1201 */
1202 int n = wcwidth(c);
1203
1204 if (n < 0)
1205 return 6; /* unprintable, displays <xxxx> */
1206 if (n > 1)
1207 return n;
1208#else
1209 if (!utf_printable(c))
1210 return 6; /* unprintable, displays <xxxx> */
1211 if (c >= 0x1100
1212 && (c <= 0x115f /* Hangul Jamo */
1213 || c == 0x2329
1214 || c == 0x232a
1215 || (c >= 0x2e80 && c <= 0xa4cf
1216 && c != 0x303f) /* CJK ... Yi */
1217 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
1218 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility
1219 Ideographs */
1220 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
1221 || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */
1222 || (c >= 0xffe0 && c <= 0xffe6)
1223 || (c >= 0x20000 && c <= 0x2fffd)
1224 || (c >= 0x30000 && c <= 0x3fffd)))
1225 return 2;
1226#endif
1227 }
1228
1229 /* Characters below 0x100 are influenced by 'isprint' option */
1230 else if (c >= 0x80 && !vim_isprintc(c))
1231 return 4; /* unprintable, displays <xx> */
1232
1233 if (c >= 0x80 && *p_ambw == 'd' && intable(ambiguous, sizeof(ambiguous), c))
1234 return 2;
1235
1236 return 1;
1237}
1238
1239/*
1240 * mb_ptr2cells() function pointer.
1241 * Return the number of display cells character at "*p" occupies.
1242 * This doesn't take care of unprintable characters, use ptr2cells() for that.
1243 */
1244/*ARGSUSED*/
1245 int
1246latin_ptr2cells(p)
1247 char_u *p;
1248{
1249 return 1;
1250}
1251
1252 int
1253utf_ptr2cells(p)
1254 char_u *p;
1255{
1256 int c;
1257
1258 /* Need to convert to a wide character. */
1259 if (*p >= 0x80)
1260 {
1261 c = utf_ptr2char(p);
1262 /* An illegal byte is displayed as <xx>. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001263 if (utf_ptr2len(p) == 1 || c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 return 4;
1265 /* If the char is ASCII it must be an overlong sequence. */
1266 if (c < 0x80)
1267 return char2cells(c);
1268 return utf_char2cells(c);
1269 }
1270 return 1;
1271}
1272
1273 int
1274dbcs_ptr2cells(p)
1275 char_u *p;
1276{
1277 /* Number of cells is equal to number of bytes, except for euc-jp when
1278 * the first byte is 0x8e. */
1279 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
1280 return 1;
1281 return MB_BYTE2LEN(*p);
1282}
1283
1284/*
1285 * mb_char2cells() function pointer.
1286 * Return the number of display cells character "c" occupies.
1287 * Only takes care of multi-byte chars, not "^C" and such.
1288 */
1289/*ARGSUSED*/
1290 int
1291latin_char2cells(c)
1292 int c;
1293{
1294 return 1;
1295}
1296
1297 static int
1298dbcs_char2cells(c)
1299 int c;
1300{
1301 /* Number of cells is equal to number of bytes, except for euc-jp when
1302 * the first byte is 0x8e. */
1303 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
1304 return 1;
1305 /* use the first byte */
1306 return MB_BYTE2LEN((unsigned)c >> 8);
1307}
1308
1309/*
1310 * mb_off2cells() function pointer.
1311 * Return number of display cells for char at ScreenLines[off].
1312 * Caller must make sure "off" and "off + 1" are valid!
1313 */
1314/*ARGSUSED*/
1315 int
1316latin_off2cells(off)
1317 unsigned off;
1318{
1319 return 1;
1320}
1321
1322 int
1323dbcs_off2cells(off)
1324 unsigned off;
1325{
1326 /* Number of cells is equal to number of bytes, except for euc-jp when
1327 * the first byte is 0x8e. */
1328 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1329 return 1;
1330 return MB_BYTE2LEN(ScreenLines[off]);
1331}
1332
1333 int
1334utf_off2cells(off)
1335 unsigned off;
1336{
1337 return ScreenLines[off + 1] == 0 ? 2 : 1;
1338}
1339
1340/*
1341 * mb_ptr2char() function pointer.
1342 * Convert a byte sequence into a character.
1343 */
1344 int
1345latin_ptr2char(p)
1346 char_u *p;
1347{
1348 return *p;
1349}
1350
1351 static int
1352dbcs_ptr2char(p)
1353 char_u *p;
1354{
1355 if (MB_BYTE2LEN(*p) > 1 && p[1] != NUL)
1356 return (p[0] << 8) + p[1];
1357 return *p;
1358}
1359
1360/*
1361 * Convert a UTF-8 byte sequence to a wide character.
1362 * If the sequence is illegal or truncated by a NUL the first byte is
1363 * returned.
1364 * Does not include composing characters, of course.
1365 */
1366 int
1367utf_ptr2char(p)
1368 char_u *p;
1369{
1370 int len;
1371
1372 if (p[0] < 0x80) /* be quick for ASCII */
1373 return p[0];
1374
1375 len = utf8len_tab[p[0]];
1376 if ((p[1] & 0xc0) == 0x80)
1377 {
1378 if (len == 2)
1379 return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
1380 if ((p[2] & 0xc0) == 0x80)
1381 {
1382 if (len == 3)
1383 return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
1384 + (p[2] & 0x3f);
1385 if ((p[3] & 0xc0) == 0x80)
1386 {
1387 if (len == 4)
1388 return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
1389 + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
1390 if ((p[4] & 0xc0) == 0x80)
1391 {
1392 if (len == 5)
1393 return ((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
1394 + ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
1395 + (p[4] & 0x3f);
1396 if ((p[5] & 0xc0) == 0x80 && len == 6)
1397 return ((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
1398 + ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
1399 + ((p[4] & 0x3f) << 6) + (p[5] & 0x3f);
1400 }
1401 }
1402 }
1403 }
1404 /* Illegal value, just return the first byte */
1405 return p[0];
1406}
1407
1408/*
1409 * Get character at **pp and advance *pp to the next character.
1410 * Note: composing characters are skipped!
1411 */
1412 int
1413mb_ptr2char_adv(pp)
1414 char_u **pp;
1415{
1416 int c;
1417
1418 c = (*mb_ptr2char)(*pp);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001419 *pp += (*mb_ptr2len)(*pp);
1420 return c;
1421}
1422
1423/*
1424 * Get character at **pp and advance *pp to the next character.
1425 * Note: composing characters are returned as separate characters.
1426 */
1427 int
1428mb_cptr2char_adv(pp)
1429 char_u **pp;
1430{
1431 int c;
1432
1433 c = (*mb_ptr2char)(*pp);
1434 if (enc_utf8)
1435 *pp += utf_ptr2len(*pp);
1436 else
1437 *pp += (*mb_ptr2len)(*pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 return c;
1439}
1440
1441#if defined(FEAT_ARABIC) || defined(PROTO)
1442/*
1443 * Check whether we are dealing with Arabic combining characters.
1444 * Note: these are NOT really composing characters!
1445 */
1446 int
1447arabic_combine(one, two)
1448 int one; /* first character */
1449 int two; /* character just after "one" */
1450{
1451 if (one == a_LAM)
1452 return arabic_maycombine(two);
1453 return FALSE;
1454}
1455
1456/*
1457 * Check whether we are dealing with a character that could be regarded as an
1458 * Arabic combining character, need to check the character before this.
1459 */
1460 int
1461arabic_maycombine(two)
1462 int two;
1463{
1464 if (p_arshape && !p_tbidi)
1465 return (two == a_ALEF_MADDA
1466 || two == a_ALEF_HAMZA_ABOVE
1467 || two == a_ALEF_HAMZA_BELOW
1468 || two == a_ALEF);
1469 return FALSE;
1470}
1471
1472/*
1473 * Check if the character pointed to by "p2" is a composing character when it
1474 * comes after "p1". For Arabic sometimes "ab" is replaced with "c", which
1475 * behaves like a composing character.
1476 */
1477 int
1478utf_composinglike(p1, p2)
1479 char_u *p1;
1480 char_u *p2;
1481{
1482 int c2;
1483
1484 c2 = utf_ptr2char(p2);
1485 if (utf_iscomposing(c2))
1486 return TRUE;
1487 if (!arabic_maycombine(c2))
1488 return FALSE;
1489 return arabic_combine(utf_ptr2char(p1), c2);
1490}
1491#endif
1492
1493/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001494 * Convert a UTF-8 byte string to a wide chararacter. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * composing characters.
1496 */
1497 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001498utfc_ptr2char(p, pcc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001500 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 int len;
1503 int c;
1504 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001505 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506
1507 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001508 len = utf_ptr2len(p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 /* Only accept a composing char when the first char isn't illegal. */
1511 if ((len > 1 || *p < 0x80)
1512 && p[len] >= 0x80
1513 && UTF_COMPOSINGLIKE(p, p + len))
1514 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001515 cc = utf_ptr2char(p + len);
1516 for (;;)
1517 {
1518 pcc[i++] = cc;
1519 if (i == MAX_MCO)
1520 break;
1521 len += utf_ptr2len(p + len);
1522 if (p[len] < 0x80 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1523 break;
1524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001526
1527 if (i < MAX_MCO) /* last composing char must be 0 */
1528 pcc[i] = 0;
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 return c;
1531}
1532
1533/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001534 * Convert a UTF-8 byte string to a wide chararacter. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 * composing characters. Use no more than p[maxlen].
1536 */
1537 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001538utfc_ptr2char_len(p, pcc, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001540 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int maxlen;
1542{
1543 int len;
1544 int c;
1545 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001546 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001549 len = utf_ptr2len_len(p, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 /* Only accept a composing char when the first char isn't illegal. */
1551 if ((len > 1 || *p < 0x80)
1552 && len < maxlen
1553 && p[len] >= 0x80
1554 && UTF_COMPOSINGLIKE(p, p + len))
1555 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001556 cc = utf_ptr2char(p + len);
1557 for (;;)
1558 {
1559 pcc[i++] = cc;
1560 if (i == MAX_MCO)
1561 break;
1562 len += utf_ptr2len_len(p + len, maxlen - len);
1563 if (len >= maxlen
1564 || p[len] < 0x80
1565 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1566 break;
1567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001569
1570 if (i < MAX_MCO) /* last composing char must be 0 */
1571 pcc[i] = 0;
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 return c;
1574}
1575
1576/*
1577 * Convert the character at screen position "off" to a sequence of bytes.
1578 * Includes the composing characters.
1579 * "buf" must at least have the length MB_MAXBYTES.
1580 * Returns the produced number of bytes.
1581 */
1582 int
1583utfc_char2bytes(off, buf)
1584 int off;
1585 char_u *buf;
1586{
1587 int len;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001588 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 len = utf_char2bytes(ScreenLinesUC[off], buf);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001591 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001593 if (ScreenLinesC[i][off] == 0)
1594 break;
1595 len += utf_char2bytes(ScreenLinesC[i][off], buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 return len;
1598}
1599
1600/*
1601 * Get the length of a UTF-8 byte sequence, not including any following
1602 * composing characters.
1603 * Returns 0 for "".
1604 * Returns 1 for an illegal byte sequence.
1605 */
1606 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001607utf_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 char_u *p;
1609{
1610 int len;
1611 int i;
1612
1613 if (*p == NUL)
1614 return 0;
1615 len = utf8len_tab[*p];
1616 for (i = 1; i < len; ++i)
1617 if ((p[i] & 0xc0) != 0x80)
1618 return 1;
1619 return len;
1620}
1621
1622/*
1623 * Return length of UTF-8 character, obtained from the first byte.
1624 * "b" must be between 0 and 255!
1625 */
1626 int
1627utf_byte2len(b)
1628 int b;
1629{
1630 return utf8len_tab[b];
1631}
1632
1633/*
1634 * Get the length of UTF-8 byte sequence "p[size]". Does not include any
1635 * following composing characters.
1636 * Returns 1 for "".
1637 * Returns 1 for an illegal byte sequence.
1638 * Returns number > "size" for an incomplete byte sequence.
1639 */
1640 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001641utf_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 char_u *p;
1643 int size;
1644{
1645 int len;
1646 int i;
1647
1648 if (*p == NUL)
1649 return 1;
1650 len = utf8len_tab[*p];
1651 if (len > size)
1652 return len; /* incomplete byte sequence. */
1653 for (i = 1; i < len; ++i)
1654 if ((p[i] & 0xc0) != 0x80)
1655 return 1;
1656 return len;
1657}
1658
1659/*
1660 * Return the number of bytes the UTF-8 encoding of the character at "p" takes.
1661 * This includes following composing characters.
1662 */
1663 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001664utfc_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 char_u *p;
1666{
1667 int len;
Bram Moolenaarc669e662005-06-08 22:00:03 +00001668 int b0 = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669#ifdef FEAT_ARABIC
1670 int prevlen;
1671#endif
1672
Bram Moolenaarc669e662005-06-08 22:00:03 +00001673 if (b0 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 return 0;
Bram Moolenaarc669e662005-06-08 22:00:03 +00001675 if (b0 < 0x80 && p[1] < 0x80) /* be quick for ASCII */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 return 1;
1677
1678 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001679 len = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 /* Check for illegal byte. */
Bram Moolenaarc669e662005-06-08 22:00:03 +00001682 if (len == 1 && b0 >= 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 return 1;
1684
1685 /*
1686 * Check for composing characters. We can handle only the first two, but
1687 * skip all of them (otherwise the cursor would get stuck).
1688 */
1689#ifdef FEAT_ARABIC
1690 prevlen = 0;
1691#endif
1692 for (;;)
1693 {
1694 if (p[len] < 0x80 || !UTF_COMPOSINGLIKE(p + prevlen, p + len))
1695 return len;
1696
1697 /* Skip over composing char */
1698#ifdef FEAT_ARABIC
1699 prevlen = len;
1700#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001701 len += utf_ptr2len(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703}
1704
1705/*
1706 * Return the number of bytes the UTF-8 encoding of the character at "p[size]"
1707 * takes. This includes following composing characters.
1708 * Returns 1 for an illegal char or an incomplete byte sequence.
1709 */
1710 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001711utfc_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 char_u *p;
1713 int size;
1714{
1715 int len;
1716#ifdef FEAT_ARABIC
1717 int prevlen;
1718#endif
1719
1720 if (*p == NUL)
1721 return 0;
1722 if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */
1723 return 1;
1724
1725 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001726 len = utf_ptr2len_len(p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 /* Check for illegal byte and incomplete byte sequence. */
1729 if ((len == 1 && p[0] >= 0x80) || len > size)
1730 return 1;
1731
1732 /*
1733 * Check for composing characters. We can handle only the first two, but
1734 * skip all of them (otherwise the cursor would get stuck).
1735 */
1736#ifdef FEAT_ARABIC
1737 prevlen = 0;
1738#endif
1739 while (len < size)
1740 {
1741 if (p[len] < 0x80 || !UTF_COMPOSINGLIKE(p + prevlen, p + len))
1742 break;
1743
1744 /* Skip over composing char */
1745#ifdef FEAT_ARABIC
1746 prevlen = len;
1747#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001748 len += utf_ptr2len_len(p + len, size - len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 return len;
1751}
1752
1753/*
1754 * Return the number of bytes the UTF-8 encoding of character "c" takes.
1755 * This does not include composing characters.
1756 */
1757 int
1758utf_char2len(c)
1759 int c;
1760{
1761 if (c < 0x80)
1762 return 1;
1763 if (c < 0x800)
1764 return 2;
1765 if (c < 0x10000)
1766 return 3;
1767 if (c < 0x200000)
1768 return 4;
1769 if (c < 0x4000000)
1770 return 5;
1771 return 6;
1772}
1773
1774/*
1775 * Convert Unicode character "c" to UTF-8 string in "buf[]".
1776 * Returns the number of bytes.
1777 * This does not include composing characters.
1778 */
1779 int
1780utf_char2bytes(c, buf)
1781 int c;
1782 char_u *buf;
1783{
1784 if (c < 0x80) /* 7 bits */
1785 {
1786 buf[0] = c;
1787 return 1;
1788 }
1789 if (c < 0x800) /* 11 bits */
1790 {
1791 buf[0] = 0xc0 + ((unsigned)c >> 6);
1792 buf[1] = 0x80 + (c & 0x3f);
1793 return 2;
1794 }
1795 if (c < 0x10000) /* 16 bits */
1796 {
1797 buf[0] = 0xe0 + ((unsigned)c >> 12);
1798 buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f);
1799 buf[2] = 0x80 + (c & 0x3f);
1800 return 3;
1801 }
1802 if (c < 0x200000) /* 21 bits */
1803 {
1804 buf[0] = 0xf0 + ((unsigned)c >> 18);
1805 buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f);
1806 buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f);
1807 buf[3] = 0x80 + (c & 0x3f);
1808 return 4;
1809 }
1810 if (c < 0x4000000) /* 26 bits */
1811 {
1812 buf[0] = 0xf8 + ((unsigned)c >> 24);
1813 buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f);
1814 buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f);
1815 buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f);
1816 buf[4] = 0x80 + (c & 0x3f);
1817 return 5;
1818 }
1819 /* 31 bits */
1820 buf[0] = 0xfc + ((unsigned)c >> 30);
1821 buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f);
1822 buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f);
1823 buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f);
1824 buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f);
1825 buf[5] = 0x80 + (c & 0x3f);
1826 return 6;
1827}
1828
1829/*
1830 * Return TRUE if "c" is a composing UTF-8 character. This means it will be
1831 * drawn on top of the preceding character.
1832 * Based on code from Markus Kuhn.
1833 */
1834 int
1835utf_iscomposing(c)
1836 int c;
1837{
1838 /* sorted list of non-overlapping intervals */
1839 static struct interval combining[] =
1840 {
1841 {0x0300, 0x034f}, {0x0360, 0x036f}, {0x0483, 0x0486}, {0x0488, 0x0489},
1842 {0x0591, 0x05a1}, {0x05a3, 0x05b9}, {0x05bb, 0x05bd}, {0x05bf, 0x05bf},
1843 {0x05c1, 0x05c2}, {0x05c4, 0x05c4}, {0x0610, 0x0615}, {0x064b, 0x0658},
1844 {0x0670, 0x0670}, {0x06d6, 0x06dc}, {0x06de, 0x06e4}, {0x06e7, 0x06e8},
1845 {0x06ea, 0x06ed}, {0x0711, 0x0711}, {0x0730, 0x074a}, {0x07a6, 0x07b0},
1846 {0x0901, 0x0903}, {0x093c, 0x093c}, {0x093e, 0x094d}, {0x0951, 0x0954},
1847 {0x0962, 0x0963}, {0x0981, 0x0983}, {0x09bc, 0x09bc}, {0x09be, 0x09c4},
1848 {0x09c7, 0x09c8}, {0x09cb, 0x09cd}, {0x09d7, 0x09d7}, {0x09e2, 0x09e3},
1849 {0x0a01, 0x0a03}, {0x0a3c, 0x0a3c}, {0x0a3e, 0x0a42}, {0x0a47, 0x0a48},
1850 {0x0a4b, 0x0a4d}, {0x0a70, 0x0a71}, {0x0a81, 0x0a83}, {0x0abc, 0x0abc},
1851 {0x0abe, 0x0ac5}, {0x0ac7, 0x0ac9}, {0x0acb, 0x0acd}, {0x0ae2, 0x0ae3},
1852 {0x0b01, 0x0b03}, {0x0b3c, 0x0b3c}, {0x0b3e, 0x0b43}, {0x0b47, 0x0b48},
1853 {0x0b4b, 0x0b4d}, {0x0b56, 0x0b57}, {0x0b82, 0x0b82}, {0x0bbe, 0x0bc2},
1854 {0x0bc6, 0x0bc8}, {0x0bca, 0x0bcd}, {0x0bd7, 0x0bd7}, {0x0c01, 0x0c03},
1855 {0x0c3e, 0x0c44}, {0x0c46, 0x0c48}, {0x0c4a, 0x0c4d}, {0x0c55, 0x0c56},
1856 {0x0c82, 0x0c83}, {0x0cbc, 0x0cbc}, {0x0cbe, 0x0cc4}, {0x0cc6, 0x0cc8},
1857 {0x0cca, 0x0ccd}, {0x0cd5, 0x0cd6}, {0x0d02, 0x0d03}, {0x0d3e, 0x0d43},
1858 {0x0d46, 0x0d48}, {0x0d4a, 0x0d4d}, {0x0d57, 0x0d57}, {0x0d82, 0x0d83},
1859 {0x0dca, 0x0dca}, {0x0dcf, 0x0dd4}, {0x0dd6, 0x0dd6}, {0x0dd8, 0x0ddf},
1860 {0x0df2, 0x0df3}, {0x0e31, 0x0e31}, {0x0e34, 0x0e3a}, {0x0e47, 0x0e4e},
1861 {0x0eb1, 0x0eb1}, {0x0eb4, 0x0eb9}, {0x0ebb, 0x0ebc}, {0x0ec8, 0x0ecd},
1862 {0x0f18, 0x0f19}, {0x0f35, 0x0f35}, {0x0f37, 0x0f37}, {0x0f39, 0x0f39},
1863 {0x0f3e, 0x0f3f}, {0x0f71, 0x0f84}, {0x0f86, 0x0f87}, {0x0f90, 0x0f97},
1864 {0x0f99, 0x0fbc}, {0x0fc6, 0x0fc6}, {0x102c, 0x1032}, {0x1036, 0x1039},
1865 {0x1056, 0x1059}, {0x1712, 0x1714}, {0x1732, 0x1734}, {0x1752, 0x1753},
1866 {0x1772, 0x1773}, {0x17b6, 0x17d3}, {0x17dd, 0x17dd}, {0x180b, 0x180d},
1867 {0x18a9, 0x18a9}, {0x1920, 0x192b}, {0x1930, 0x193b}, {0x20d0, 0x20ea},
1868 {0x302a, 0x302f}, {0x3099, 0x309a}, {0xfb1e, 0xfb1e}, {0xfe00, 0xfe0f},
1869 {0xfe20, 0xfe23},
1870 };
1871
1872 return intable(combining, sizeof(combining), c);
1873}
1874
1875/*
1876 * Return TRUE for characters that can be displayed in a normal way.
1877 * Only for characters of 0x100 and above!
1878 */
1879 int
1880utf_printable(c)
1881 int c;
1882{
1883#ifdef USE_WCHAR_FUNCTIONS
1884 /*
1885 * Assume the iswprint() library function works better than our own stuff.
1886 */
1887 return iswprint(c);
1888#else
1889 /* Sorted list of non-overlapping intervals.
1890 * 0xd800-0xdfff is reserved for UTF-16, actually illegal. */
1891 static struct interval nonprint[] =
1892 {
1893 {0x070f, 0x070f}, {0x180b, 0x180e}, {0x200b, 0x200f}, {0x202a, 0x202e},
1894 {0x206a, 0x206f}, {0xd800, 0xdfff}, {0xfeff, 0xfeff}, {0xfff9, 0xfffb},
1895 {0xfffe, 0xffff}
1896 };
1897
1898 return !intable(nonprint, sizeof(nonprint), c);
1899#endif
1900}
1901
1902/*
1903 * Get class of a Unicode character.
1904 * 0: white space
1905 * 1: punctuation
1906 * 2 or bigger: some class of word character.
1907 */
1908 int
1909utf_class(c)
1910 int c;
1911{
1912 /* sorted list of non-overlapping intervals */
1913 static struct clinterval
1914 {
1915 unsigned short first;
1916 unsigned short last;
1917 unsigned short class;
1918 } classes[] =
1919 {
1920 {0x037e, 0x037e, 1}, /* Greek question mark */
1921 {0x0387, 0x0387, 1}, /* Greek ano teleia */
1922 {0x055a, 0x055f, 1}, /* Armenian punctuation */
1923 {0x0589, 0x0589, 1}, /* Armenian full stop */
1924 {0x05be, 0x05be, 1},
1925 {0x05c0, 0x05c0, 1},
1926 {0x05c3, 0x05c3, 1},
1927 {0x05f3, 0x05f4, 1},
1928 {0x060c, 0x060c, 1},
1929 {0x061b, 0x061b, 1},
1930 {0x061f, 0x061f, 1},
1931 {0x066a, 0x066d, 1},
1932 {0x06d4, 0x06d4, 1},
1933 {0x0700, 0x070d, 1}, /* Syriac punctuation */
1934 {0x0964, 0x0965, 1},
1935 {0x0970, 0x0970, 1},
1936 {0x0df4, 0x0df4, 1},
1937 {0x0e4f, 0x0e4f, 1},
1938 {0x0e5a, 0x0e5b, 1},
1939 {0x0f04, 0x0f12, 1},
1940 {0x0f3a, 0x0f3d, 1},
1941 {0x0f85, 0x0f85, 1},
1942 {0x104a, 0x104f, 1}, /* Myanmar punctuation */
1943 {0x10fb, 0x10fb, 1}, /* Georgian punctuation */
1944 {0x1361, 0x1368, 1}, /* Ethiopic punctuation */
1945 {0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
1946 {0x1680, 0x1680, 0},
1947 {0x169b, 0x169c, 1},
1948 {0x16eb, 0x16ed, 1},
1949 {0x1735, 0x1736, 1},
1950 {0x17d4, 0x17dc, 1}, /* Khmer punctuation */
1951 {0x1800, 0x180a, 1}, /* Mongolian punctuation */
1952 {0x2000, 0x200b, 0}, /* spaces */
1953 {0x200c, 0x2027, 1}, /* punctuation and symbols */
1954 {0x2028, 0x2029, 0},
1955 {0x202a, 0x202e, 1}, /* punctuation and symbols */
1956 {0x202f, 0x202f, 0},
1957 {0x2030, 0x205e, 1}, /* punctuation and symbols */
1958 {0x205f, 0x205f, 0},
1959 {0x2060, 0x27ff, 1}, /* punctuation and symbols */
1960 {0x2070, 0x207f, 0x2070}, /* superscript */
1961 {0x2080, 0x208f, 0x2080}, /* subscript */
1962 {0x2983, 0x2998, 1},
1963 {0x29d8, 0x29db, 1},
1964 {0x29fc, 0x29fd, 1},
1965 {0x3000, 0x3000, 0}, /* ideographic space */
1966 {0x3001, 0x3020, 1}, /* ideographic punctuation */
1967 {0x3030, 0x3030, 1},
1968 {0x303d, 0x303d, 1},
1969 {0x3040, 0x309f, 0x3040}, /* Hiragana */
1970 {0x30a0, 0x30ff, 0x30a0}, /* Katakana */
1971 {0x3300, 0x9fff, 0x4e00}, /* CJK Ideographs */
1972 {0xac00, 0xd7a3, 0xac00}, /* Hangul Syllables */
1973 {0xf900, 0xfaff, 0x4e00}, /* CJK Ideographs */
1974 {0xfd3e, 0xfd3f, 1},
1975 {0xfe30, 0xfe6b, 1}, /* punctuation forms */
1976 {0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
1977 {0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
1978 {0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
1979 {0xff5b, 0xff65, 1}, /* half/fullwidth ASCII */
1980 };
1981 int bot = 0;
1982 int top = sizeof(classes) / sizeof(struct clinterval) - 1;
1983 int mid;
1984
1985 /* First quick check for Latin1 characters, use 'iskeyword'. */
1986 if (c < 0x100)
1987 {
Bram Moolenaarf7bbbc52005-06-17 21:55:00 +00001988 if (c == ' ' || c == '\t' || c == NUL || c == 0xa0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 return 0; /* blank */
1990 if (vim_iswordc(c))
1991 return 2; /* word character */
1992 return 1; /* punctuation */
1993 }
1994
1995 /* binary search in table */
1996 while (top >= bot)
1997 {
1998 mid = (bot + top) / 2;
1999 if (classes[mid].last < c)
2000 bot = mid + 1;
2001 else if (classes[mid].first > c)
2002 top = mid - 1;
2003 else
2004 return (int)classes[mid].class;
2005 }
2006
2007 /* most other characters are "word" characters */
2008 return 2;
2009}
2010
2011/*
2012 * Code for Unicode case-dependent operations. Based on notes in
2013 * http://www.unicode.org/Public/UNIDATA/CaseFolding.txt
2014 * This code uses simple case folding, not full case folding.
2015 */
2016
2017/*
2018 * The following table is built by foldExtract.pl < CaseFolding.txt .
2019 * It must be in numeric order, because we use binary search on it.
2020 * An entry such as {0x41,0x5a,1,32} means that UCS-4 characters in the range
2021 * from 0x41 to 0x5a inclusive, stepping by 1, are folded by adding 32.
2022 */
2023
2024typedef struct
2025{
2026 int rangeStart;
2027 int rangeEnd;
2028 int step;
2029 int offset;
2030} convertStruct;
2031
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002032static convertStruct foldCase[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034 {0x41,0x5a,1,32}, {0xc0,0xd6,1,32}, {0xd8,0xde,1,32},
2035 {0x100,0x12e,2,1}, {0x130,0x130,-1,-199}, {0x132,0x136,2,1},
2036 {0x139,0x147,2,1}, {0x14a,0x176,2,1}, {0x178,0x178,-1,-121},
2037 {0x179,0x17d,2,1}, {0x181,0x181,-1,210}, {0x182,0x184,2,1},
2038 {0x186,0x186,-1,206}, {0x187,0x187,-1,1}, {0x189,0x18a,1,205},
2039 {0x18b,0x18b,-1,1}, {0x18e,0x18e,-1,79}, {0x18f,0x18f,-1,202},
2040 {0x190,0x190,-1,203}, {0x191,0x191,-1,1}, {0x193,0x193,-1,205},
2041 {0x194,0x194,-1,207}, {0x196,0x196,-1,211}, {0x197,0x197,-1,209},
2042 {0x198,0x198,-1,1}, {0x19c,0x19c,-1,211}, {0x19d,0x19d,-1,213},
2043 {0x19f,0x19f,-1,214}, {0x1a0,0x1a4,2,1}, {0x1a6,0x1a6,-1,218},
2044 {0x1a7,0x1a7,-1,1}, {0x1a9,0x1a9,-1,218}, {0x1ac,0x1ac,-1,1},
2045 {0x1ae,0x1ae,-1,218}, {0x1af,0x1af,-1,1}, {0x1b1,0x1b2,1,217},
2046 {0x1b3,0x1b5,2,1}, {0x1b7,0x1b7,-1,219}, {0x1b8,0x1bc,4,1},
2047 {0x1c4,0x1c4,-1,2}, {0x1c5,0x1c5,-1,1}, {0x1c7,0x1c7,-1,2},
2048 {0x1c8,0x1c8,-1,1}, {0x1ca,0x1ca,-1,2}, {0x1cb,0x1db,2,1},
2049 {0x1de,0x1ee,2,1}, {0x1f1,0x1f1,-1,2}, {0x1f2,0x1f4,2,1},
2050 {0x1f6,0x1f6,-1,-97}, {0x1f7,0x1f7,-1,-56}, {0x1f8,0x21e,2,1},
2051 {0x220,0x220,-1,-130}, {0x222,0x232,2,1}, {0x386,0x386,-1,38},
2052 {0x388,0x38a,1,37}, {0x38c,0x38c,-1,64}, {0x38e,0x38f,1,63},
2053 {0x391,0x3a1,1,32}, {0x3a3,0x3ab,1,32}, {0x3d8,0x3ee,2,1},
2054 {0x3f4,0x3f4,-1,-60}, {0x3f7,0x3f7,-1,1}, {0x3f9,0x3f9,-1,-7},
2055 {0x3fa,0x3fa,-1,1}, {0x400,0x40f,1,80}, {0x410,0x42f,1,32},
2056 {0x460,0x480,2,1}, {0x48a,0x4be,2,1}, {0x4c1,0x4cd,2,1},
2057 {0x4d0,0x4f4,2,1}, {0x4f8,0x500,8,1}, {0x502,0x50e,2,1},
2058 {0x531,0x556,1,48}, {0x1e00,0x1e94,2,1}, {0x1ea0,0x1ef8,2,1},
2059 {0x1f08,0x1f0f,1,-8}, {0x1f18,0x1f1d,1,-8}, {0x1f28,0x1f2f,1,-8},
2060 {0x1f38,0x1f3f,1,-8}, {0x1f48,0x1f4d,1,-8}, {0x1f59,0x1f5f,2,-8},
2061 {0x1f68,0x1f6f,1,-8}, {0x1f88,0x1f8f,1,-8}, {0x1f98,0x1f9f,1,-8},
2062 {0x1fa8,0x1faf,1,-8}, {0x1fb8,0x1fb9,1,-8}, {0x1fba,0x1fbb,1,-74},
2063 {0x1fbc,0x1fbc,-1,-9}, {0x1fc8,0x1fcb,1,-86}, {0x1fcc,0x1fcc,-1,-9},
2064 {0x1fd8,0x1fd9,1,-8}, {0x1fda,0x1fdb,1,-100}, {0x1fe8,0x1fe9,1,-8},
2065 {0x1fea,0x1feb,1,-112}, {0x1fec,0x1fec,-1,-7}, {0x1ff8,0x1ff9,1,-128},
2066 {0x1ffa,0x1ffb,1,-126}, {0x1ffc,0x1ffc,-1,-9}, {0x2126,0x2126,-1,-7517},
2067 {0x212a,0x212a,-1,-8383}, {0x212b,0x212b,-1,-8262},
2068 {0x2160,0x216f,1,16}, {0x24b6,0x24cf,1,26}, {0xff21,0xff3a,1,32},
2069 {0x10400,0x10427,1,40}
2070};
2071
2072static int utf_convert(int a, convertStruct table[], int tableSize);
2073
2074/*
2075 * Generic conversion function for case operations.
2076 * Return the converted equivalent of "a", which is a UCS-4 character. Use
2077 * the given conversion "table". Uses binary search on "table".
2078 */
2079 static int
2080utf_convert(a, table, tableSize)
2081 int a;
2082 convertStruct table[];
2083 int tableSize;
2084{
2085 int start, mid, end; /* indices into table */
2086
2087 start = 0;
2088 end = tableSize / sizeof(convertStruct);
2089 while (start < end)
2090 {
2091 /* need to search further */
2092 mid = (end + start) /2;
2093 if (table[mid].rangeEnd < a)
2094 start = mid + 1;
2095 else
2096 end = mid;
2097 }
2098 if (table[start].rangeStart <= a && a <= table[start].rangeEnd
2099 && (a - table[start].rangeStart) % table[start].step == 0)
2100 return (a + table[start].offset);
2101 else
2102 return a;
2103}
2104
2105/*
2106 * Return the folded-case equivalent of "a", which is a UCS-4 character. Uses
2107 * simple case folding.
2108 */
2109 int
2110utf_fold(a)
2111 int a;
2112{
2113 return utf_convert(a, foldCase, sizeof(foldCase));
2114}
2115
2116/*
2117 * The following tables are built by upperLowerExtract.pl < UnicodeData.txt .
2118 * They must be in numeric order, because we use binary search on them.
2119 * An entry such as {0x41,0x5a,1,32} means that UCS-4 characters in the range
2120 * from 0x41 to 0x5a inclusive, stepping by 1, are switched to lower (for
2121 * example) by adding 32.
2122 */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002123static convertStruct toLower[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 {0x41,0x5a,1,32}, {0xc0,0xd6,1,32}, {0xd8,0xde,1,32},
2126 {0x100,0x12e,2,1}, {0x130,0x130,-1,-199}, {0x132,0x136,2,1},
2127 {0x139,0x147,2,1}, {0x14a,0x176,2,1}, {0x178,0x178,-1,-121},
2128 {0x179,0x17d,2,1}, {0x181,0x181,-1,210}, {0x182,0x184,2,1},
2129 {0x186,0x186,-1,206}, {0x187,0x187,-1,1}, {0x189,0x18a,1,205},
2130 {0x18b,0x18b,-1,1}, {0x18e,0x18e,-1,79}, {0x18f,0x18f,-1,202},
2131 {0x190,0x190,-1,203}, {0x191,0x191,-1,1}, {0x193,0x193,-1,205},
2132 {0x194,0x194,-1,207}, {0x196,0x196,-1,211}, {0x197,0x197,-1,209},
2133 {0x198,0x198,-1,1}, {0x19c,0x19c,-1,211}, {0x19d,0x19d,-1,213},
2134 {0x19f,0x19f,-1,214}, {0x1a0,0x1a4,2,1}, {0x1a6,0x1a6,-1,218},
2135 {0x1a7,0x1a7,-1,1}, {0x1a9,0x1a9,-1,218}, {0x1ac,0x1ac,-1,1},
2136 {0x1ae,0x1ae,-1,218}, {0x1af,0x1af,-1,1}, {0x1b1,0x1b2,1,217},
2137 {0x1b3,0x1b5,2,1}, {0x1b7,0x1b7,-1,219}, {0x1b8,0x1bc,4,1},
2138 {0x1c4,0x1ca,3,2}, {0x1cd,0x1db,2,1}, {0x1de,0x1ee,2,1},
2139 {0x1f1,0x1f1,-1,2}, {0x1f4,0x1f4,-1,1}, {0x1f6,0x1f6,-1,-97},
2140 {0x1f7,0x1f7,-1,-56}, {0x1f8,0x21e,2,1}, {0x220,0x220,-1,-130},
2141 {0x222,0x232,2,1}, {0x386,0x386,-1,38}, {0x388,0x38a,1,37},
2142 {0x38c,0x38c,-1,64}, {0x38e,0x38f,1,63}, {0x391,0x3a1,1,32},
2143 {0x3a3,0x3ab,1,32}, {0x3d8,0x3ee,2,1}, {0x3f4,0x3f4,-1,-60},
2144 {0x3f7,0x3f7,-1,1}, {0x3f9,0x3f9,-1,-7}, {0x3fa,0x3fa,-1,1},
2145 {0x400,0x40f,1,80}, {0x410,0x42f,1,32}, {0x460,0x480,2,1},
2146 {0x48a,0x4be,2,1}, {0x4c1,0x4cd,2,1}, {0x4d0,0x4f4,2,1},
2147 {0x4f8,0x500,8,1}, {0x502,0x50e,2,1}, {0x531,0x556,1,48},
2148 {0x1e00,0x1e94,2,1}, {0x1ea0,0x1ef8,2,1}, {0x1f08,0x1f0f,1,-8},
2149 {0x1f18,0x1f1d,1,-8}, {0x1f28,0x1f2f,1,-8}, {0x1f38,0x1f3f,1,-8},
2150 {0x1f48,0x1f4d,1,-8}, {0x1f59,0x1f5f,2,-8}, {0x1f68,0x1f6f,1,-8},
2151 {0x1fb8,0x1fb9,1,-8}, {0x1fba,0x1fbb,1,-74}, {0x1fc8,0x1fcb,1,-86},
2152 {0x1fd8,0x1fd9,1,-8}, {0x1fda,0x1fdb,1,-100}, {0x1fe8,0x1fe9,1,-8},
2153 {0x1fea,0x1feb,1,-112}, {0x1fec,0x1fec,-1,-7}, {0x1ff8,0x1ff9,1,-128},
2154 {0x1ffa,0x1ffb,1,-126}, {0x2126,0x2126,-1,-7517}, {0x212a,0x212a,-1,-8383},
2155 {0x212b,0x212b,-1,-8262}, {0xff21,0xff3a,1,32}, {0x10400,0x10427,1,40}
2156};
2157
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002158static convertStruct toUpper[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159{
2160 {0x61,0x7a,1,-32}, {0xb5,0xb5,-1,743}, {0xe0,0xf6,1,-32},
2161 {0xf8,0xfe,1,-32}, {0xff,0xff,-1,121}, {0x101,0x12f,2,-1},
2162 {0x131,0x131,-1,-232}, {0x133,0x137,2,-1}, {0x13a,0x148,2,-1},
2163 {0x14b,0x177,2,-1}, {0x17a,0x17e,2,-1}, {0x17f,0x17f,-1,-300},
2164 {0x183,0x185,2,-1}, {0x188,0x18c,4,-1}, {0x192,0x192,-1,-1},
2165 {0x195,0x195,-1,97}, {0x199,0x199,-1,-1}, {0x19e,0x19e,-1,130},
2166 {0x1a1,0x1a5,2,-1}, {0x1a8,0x1ad,5,-1}, {0x1b0,0x1b4,4,-1},
2167 {0x1b6,0x1b9,3,-1}, {0x1bd,0x1bd,-1,-1}, {0x1bf,0x1bf,-1,56},
2168 {0x1c5,0x1c6,1,-1}, {0x1c8,0x1c9,1,-1}, {0x1cb,0x1cc,1,-1},
2169 {0x1ce,0x1dc,2,-1}, {0x1dd,0x1dd,-1,-79}, {0x1df,0x1ef,2,-1},
2170 {0x1f2,0x1f3,1,-1}, {0x1f5,0x1f9,4,-1}, {0x1fb,0x21f,2,-1},
2171 {0x223,0x233,2,-1}, {0x253,0x253,-1,-210}, {0x254,0x254,-1,-206},
2172 {0x256,0x257,1,-205}, {0x259,0x259,-1,-202}, {0x25b,0x25b,-1,-203},
2173 {0x260,0x260,-1,-205}, {0x263,0x263,-1,-207}, {0x268,0x268,-1,-209},
2174 {0x269,0x26f,6,-211}, {0x272,0x272,-1,-213}, {0x275,0x275,-1,-214},
2175 {0x280,0x283,3,-218}, {0x288,0x288,-1,-218}, {0x28a,0x28b,1,-217},
2176 {0x292,0x292,-1,-219}, {0x3ac,0x3ac,-1,-38}, {0x3ad,0x3af,1,-37},
2177 {0x3b1,0x3c1,1,-32}, {0x3c2,0x3c2,-1,-31}, {0x3c3,0x3cb,1,-32},
2178 {0x3cc,0x3cc,-1,-64}, {0x3cd,0x3ce,1,-63}, {0x3d0,0x3d0,-1,-62},
2179 {0x3d1,0x3d1,-1,-57}, {0x3d5,0x3d5,-1,-47}, {0x3d6,0x3d6,-1,-54},
2180 {0x3d9,0x3ef,2,-1}, {0x3f0,0x3f0,-1,-86}, {0x3f1,0x3f1,-1,-80},
2181 {0x3f2,0x3f2,-1,7}, {0x3f5,0x3f5,-1,-96}, {0x3f8,0x3fb,3,-1},
2182 {0x430,0x44f,1,-32}, {0x450,0x45f,1,-80}, {0x461,0x481,2,-1},
2183 {0x48b,0x4bf,2,-1}, {0x4c2,0x4ce,2,-1}, {0x4d1,0x4f5,2,-1},
2184 {0x4f9,0x501,8,-1}, {0x503,0x50f,2,-1}, {0x561,0x586,1,-48},
2185 {0x1e01,0x1e95,2,-1}, {0x1e9b,0x1e9b,-1,-59}, {0x1ea1,0x1ef9,2,-1},
2186 {0x1f00,0x1f07,1,8}, {0x1f10,0x1f15,1,8}, {0x1f20,0x1f27,1,8},
2187 {0x1f30,0x1f37,1,8}, {0x1f40,0x1f45,1,8}, {0x1f51,0x1f57,2,8},
2188 {0x1f60,0x1f67,1,8}, {0x1f70,0x1f71,1,74}, {0x1f72,0x1f75,1,86},
2189 {0x1f76,0x1f77,1,100}, {0x1f78,0x1f79,1,128}, {0x1f7a,0x1f7b,1,112},
2190 {0x1f7c,0x1f7d,1,126}, {0x1f80,0x1f87,1,8}, {0x1f90,0x1f97,1,8},
2191 {0x1fa0,0x1fa7,1,8}, {0x1fb0,0x1fb1,1,8}, {0x1fb3,0x1fb3,-1,9},
2192 {0x1fbe,0x1fbe,-1,-7205}, {0x1fc3,0x1fc3,-1,9}, {0x1fd0,0x1fd1,1,8},
2193 {0x1fe0,0x1fe1,1,8}, {0x1fe5,0x1fe5,-1,7}, {0x1ff3,0x1ff3,-1,9},
2194 {0xff41,0xff5a,1,-32}, {0x10428,0x1044f,1,-40}
2195};
2196
2197/*
2198 * Return the upper-case equivalent of "a", which is a UCS-4 character. Use
2199 * simple case folding.
2200 */
2201 int
2202utf_toupper(a)
2203 int a;
2204{
2205 /* If 'casemap' contains "keepascii" use ASCII style toupper(). */
2206 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
2207 return TOUPPER_ASC(a);
2208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002209#if defined(HAVE_TOWUPPER) && defined(__STDC_ISO_10646__)
2210 /* If towupper() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (!(cmp_flags & CMP_INTERNAL))
2212 return towupper(a);
2213#endif
2214
2215 /* For characters below 128 use locale sensitive toupper(). */
2216 if (a < 128)
2217 return TOUPPER_LOC(a);
2218
2219 /* For any other characters use the above mapping table. */
2220 return utf_convert(a, toUpper, sizeof(toUpper));
2221}
2222
2223 int
2224utf_islower(a)
2225 int a;
2226{
2227 return (utf_toupper(a) != a);
2228}
2229
2230/*
2231 * Return the lower-case equivalent of "a", which is a UCS-4 character. Use
2232 * simple case folding.
2233 */
2234 int
2235utf_tolower(a)
2236 int a;
2237{
2238 /* If 'casemap' contains "keepascii" use ASCII style tolower(). */
2239 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
2240 return TOLOWER_ASC(a);
2241
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002242#if defined(HAVE_TOWLOWER) && defined(__STDC_ISO_10646__)
Bram Moolenaar8fcc0f72005-04-23 20:45:11 +00002243 /* If towlower() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (!(cmp_flags & CMP_INTERNAL))
2245 return towlower(a);
2246#endif
2247
2248 /* For characters below 128 use locale sensitive tolower(). */
2249 if (a < 128)
2250 return TOLOWER_LOC(a);
2251
2252 /* For any other characters use the above mapping table. */
2253 return utf_convert(a, toLower, sizeof(toLower));
2254}
2255
2256 int
2257utf_isupper(a)
2258 int a;
2259{
2260 return (utf_tolower(a) != a);
2261}
2262
2263/*
2264 * Version of strnicmp() that handles multi-byte characters.
2265 * Needed for Big5, Sjift-JIS and UTF-8 encoding. Other DBCS encodings can
2266 * probably use strnicmp(), because there are no ASCII characters in the
2267 * second byte.
2268 * Returns zero if s1 and s2 are equal (ignoring case), the difference between
2269 * two characters otherwise.
2270 */
2271 int
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00002272mb_strnicmp(s1, s2, nn)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 char_u *s1, *s2;
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00002274 size_t nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
2276 int i, j, l;
2277 int cdiff;
Bram Moolenaar45eeb132005-06-06 21:59:07 +00002278 int incomplete = FALSE;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002279 int n = (int)nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280
2281 for (i = 0; i < n; i += l)
2282 {
2283 if (s1[i] == NUL && s2[i] == NUL) /* both strings end */
2284 return 0;
2285 if (enc_utf8)
2286 {
2287 l = utf_byte2len(s1[i]);
2288 if (l > n - i)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00002289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 l = n - i; /* incomplete character */
Bram Moolenaar45eeb132005-06-06 21:59:07 +00002291 incomplete = TRUE;
2292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 /* Check directly first, it's faster. */
2294 for (j = 0; j < l; ++j)
2295 if (s1[i + j] != s2[i + j])
2296 break;
2297 if (j < l)
2298 {
2299 /* If one of the two characters is incomplete return -1. */
Bram Moolenaar45eeb132005-06-06 21:59:07 +00002300 if (incomplete || i + utf_byte2len(s2[i]) > n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 return -1;
2302 cdiff = utf_fold(utf_ptr2char(s1 + i))
2303 - utf_fold(utf_ptr2char(s2 + i));
2304 if (cdiff != 0)
2305 return cdiff;
2306 }
2307 }
2308 else
2309 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002310 l = (*mb_ptr2len)(s1 + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (l <= 1)
2312 {
2313 /* Single byte: first check normally, then with ignore case. */
2314 if (s1[i] != s2[i])
2315 {
2316 cdiff = TOLOWER_LOC(s1[i]) - TOLOWER_LOC(s2[i]);
2317 if (cdiff != 0)
2318 return cdiff;
2319 }
2320 }
2321 else
2322 {
2323 /* For non-Unicode multi-byte don't ignore case. */
2324 if (l > n - i)
2325 l = n - i;
2326 cdiff = STRNCMP(s1 + i, s2 + i, l);
2327 if (cdiff != 0)
2328 return cdiff;
2329 }
2330 }
2331 }
2332 return 0;
2333}
2334
2335/*
2336 * "g8": show bytes of the UTF-8 char under the cursor. Doesn't matter what
2337 * 'encoding' has been set to.
2338 */
2339 void
2340show_utf8()
2341{
2342 int len;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002343 int rlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 char_u *line;
2345 int clen;
2346 int i;
2347
2348 /* Get the byte length of the char under the cursor, including composing
2349 * characters. */
2350 line = ml_get_cursor();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002351 len = utfc_ptr2len(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (len == 0)
2353 {
2354 MSG("NUL");
2355 return;
2356 }
2357
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 clen = 0;
2359 for (i = 0; i < len; ++i)
2360 {
2361 if (clen == 0)
2362 {
2363 /* start of (composing) character, get its length */
2364 if (i > 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +00002365 {
2366 STRCPY(IObuff + rlen, "+ ");
2367 rlen += 2;
2368 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002369 clen = utf_ptr2len(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
Bram Moolenaar051b7822005-05-19 21:00:46 +00002371 sprintf((char *)IObuff + rlen, "%02x ", line[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 --clen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002373 rlen += (int)STRLEN(IObuff + rlen);
Bram Moolenaar051b7822005-05-19 21:00:46 +00002374 if (rlen > IOSIZE - 20)
2375 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377
2378 msg(IObuff);
2379}
2380
2381/*
2382 * mb_head_off() function pointer.
2383 * Return offset from "p" to the first byte of the character it points into.
2384 * Returns 0 when already at the first byte of a character.
2385 */
2386/*ARGSUSED*/
2387 int
2388latin_head_off(base, p)
2389 char_u *base;
2390 char_u *p;
2391{
2392 return 0;
2393}
2394
2395 int
2396dbcs_head_off(base, p)
2397 char_u *base;
2398 char_u *p;
2399{
2400 char_u *q;
2401
2402 /* It can't be a trailing byte when not using DBCS, at the start of the
2403 * string or the previous byte can't start a double-byte. */
2404 if (p <= base || MB_BYTE2LEN(p[-1]) == 1)
2405 return 0;
2406
2407 /* This is slow: need to start at the base and go forward until the
2408 * byte we are looking for. Return 1 when we went past it, 0 otherwise. */
2409 q = base;
2410 while (q < p)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002411 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 return (q == p) ? 0 : 1;
2413}
2414
2415#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
2416 || defined(PROTO)
2417/*
2418 * Special version of dbcs_head_off() that works for ScreenLines[], where
2419 * single-width DBCS_JPNU characters are stored separately.
2420 */
2421 int
2422dbcs_screen_head_off(base, p)
2423 char_u *base;
2424 char_u *p;
2425{
2426 char_u *q;
2427
2428 /* It can't be a trailing byte when not using DBCS, at the start of the
2429 * string or the previous byte can't start a double-byte.
2430 * For euc-jp an 0x8e byte in the previous cell always means we have a
2431 * lead byte in the current cell. */
2432 if (p <= base
2433 || (enc_dbcs == DBCS_JPNU && p[-1] == 0x8e)
2434 || MB_BYTE2LEN(p[-1]) == 1)
2435 return 0;
2436
2437 /* This is slow: need to start at the base and go forward until the
2438 * byte we are looking for. Return 1 when we went past it, 0 otherwise.
2439 * For DBCS_JPNU look out for 0x8e, which means the second byte is not
2440 * stored as the next byte. */
2441 q = base;
2442 while (q < p)
2443 {
2444 if (enc_dbcs == DBCS_JPNU && *q == 0x8e)
2445 ++q;
2446 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449 return (q == p) ? 0 : 1;
2450}
2451#endif
2452
2453 int
2454utf_head_off(base, p)
2455 char_u *base;
2456 char_u *p;
2457{
2458 char_u *q;
2459 char_u *s;
2460 int c;
2461#ifdef FEAT_ARABIC
2462 char_u *j;
2463#endif
2464
2465 if (*p < 0x80) /* be quick for ASCII */
2466 return 0;
2467
2468 /* Skip backwards over trailing bytes: 10xx.xxxx
2469 * Skip backwards again if on a composing char. */
2470 for (q = p; ; --q)
2471 {
2472 /* Move s to the last byte of this char. */
2473 for (s = q; (s[1] & 0xc0) == 0x80; ++s)
2474 ;
2475 /* Move q to the first byte of this char. */
2476 while (q > base && (*q & 0xc0) == 0x80)
2477 --q;
2478 /* Check for illegal sequence. Do allow an illegal byte after where we
2479 * started. */
2480 if (utf8len_tab[*q] != (int)(s - q + 1)
2481 && utf8len_tab[*q] != (int)(p - q + 1))
2482 return 0;
2483
2484 if (q <= base)
2485 break;
2486
2487 c = utf_ptr2char(q);
2488 if (utf_iscomposing(c))
2489 continue;
2490
2491#ifdef FEAT_ARABIC
2492 if (arabic_maycombine(c))
2493 {
2494 /* Advance to get a sneak-peak at the next char */
2495 j = q;
2496 --j;
2497 /* Move j to the first byte of this char. */
2498 while (j > base && (*j & 0xc0) == 0x80)
2499 --j;
2500 if (arabic_combine(utf_ptr2char(j), c))
2501 continue;
2502 }
2503#endif
2504 break;
2505 }
2506
2507 return (int)(p - q);
2508}
2509
Bram Moolenaar677ee682005-01-27 14:41:15 +00002510#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511/*
Bram Moolenaard8b02732005-01-14 21:48:43 +00002512 * Copy a character from "*fp" to "*tp" and advance the pointers.
2513 */
2514 void
2515mb_copy_char(fp, tp)
2516 char_u **fp;
2517 char_u **tp;
2518{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002519 int l = (*mb_ptr2len)(*fp);
Bram Moolenaard8b02732005-01-14 21:48:43 +00002520
2521 mch_memmove(*tp, *fp, (size_t)l);
2522 *tp += l;
2523 *fp += l;
2524}
Bram Moolenaar677ee682005-01-27 14:41:15 +00002525#endif
Bram Moolenaard8b02732005-01-14 21:48:43 +00002526
2527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 * Return the offset from "p" to the first byte of a character. When "p" is
2529 * at the start of a character 0 is returned, otherwise the offset to the next
2530 * character. Can start anywhere in a stream of bytes.
2531 */
2532 int
2533mb_off_next(base, p)
2534 char_u *base;
2535 char_u *p;
2536{
2537 int i;
2538 int j;
2539
2540 if (enc_utf8)
2541 {
2542 if (*p < 0x80) /* be quick for ASCII */
2543 return 0;
2544
2545 /* Find the next character that isn't 10xx.xxxx */
2546 for (i = 0; (p[i] & 0xc0) == 0x80; ++i)
2547 ;
2548 if (i > 0)
2549 {
2550 /* Check for illegal sequence. */
2551 for (j = 0; p - j > base; ++j)
2552 if ((p[-j] & 0xc0) != 0x80)
2553 break;
2554 if (utf8len_tab[p[-j]] != i + j)
2555 return 0;
2556 }
2557 return i;
2558 }
2559
2560 /* Only need to check if we're on a trail byte, it doesn't matter if we
2561 * want the offset to the next or current character. */
2562 return (*mb_head_off)(base, p);
2563}
2564
2565/*
2566 * Return the offset from "p" to the last byte of the character it points
2567 * into. Can start anywhere in a stream of bytes.
2568 */
2569 int
2570mb_tail_off(base, p)
2571 char_u *base;
2572 char_u *p;
2573{
2574 int i;
2575 int j;
2576
2577 if (*p == NUL)
2578 return 0;
2579
2580 if (enc_utf8)
2581 {
2582 /* Find the last character that is 10xx.xxxx */
2583 for (i = 0; (p[i + 1] & 0xc0) == 0x80; ++i)
2584 ;
2585 /* Check for illegal sequence. */
2586 for (j = 0; p - j > base; ++j)
2587 if ((p[-j] & 0xc0) != 0x80)
2588 break;
2589 if (utf8len_tab[p[-j]] != i + j + 1)
2590 return 0;
2591 return i;
2592 }
2593
2594 /* It can't be the first byte if a double-byte when not using DBCS, at the
2595 * end of the string or the byte can't start a double-byte. */
2596 if (enc_dbcs == 0 || p[1] == NUL || MB_BYTE2LEN(*p) == 1)
2597 return 0;
2598
2599 /* Return 1 when on the lead byte, 0 when on the tail byte. */
2600 return 1 - dbcs_head_off(base, p);
2601}
2602
Bram Moolenaar68f1a482006-03-17 23:12:21 +00002603/*
2604 * Find the next illegal byte sequence.
2605 */
2606 void
2607utf_find_illegal()
2608{
2609 pos_T pos = curwin->w_cursor;
2610 char_u *p;
2611 int len;
2612 vimconv_T vimconv;
2613 char_u *tofree = NULL;
2614
2615 vimconv.vc_type = CONV_NONE;
2616 if (enc_utf8 && (enc_canon_props(curbuf->b_p_fenc) & ENC_8BIT))
2617 {
2618 /* 'encoding' is "utf-8" but we are editing a 8-bit encoded file,
2619 * possibly a utf-8 file with illegal bytes. Setup for conversion
2620 * from utf-8 to 'fileencoding'. */
2621 convert_setup(&vimconv, p_enc, curbuf->b_p_fenc);
2622 }
2623
2624#ifdef FEAT_VIRTUALEDIT
2625 curwin->w_cursor.coladd = 0;
2626#endif
2627 for (;;)
2628 {
2629 p = ml_get_cursor();
2630 if (vimconv.vc_type != CONV_NONE)
2631 {
2632 vim_free(tofree);
2633 tofree = string_convert(&vimconv, p, NULL);
2634 if (tofree == NULL)
2635 break;
2636 p = tofree;
2637 }
2638
2639 while (*p != NUL)
2640 {
2641 /* Illegal means that there are not enough trail bytes (checked by
2642 * utf_ptr2len()) or too many of them (overlong sequence). */
2643 len = utf_ptr2len(p);
2644 if (*p >= 0x80 && (len == 1
2645 || utf_char2len(utf_ptr2char(p)) != len))
2646 {
2647 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002648 curwin->w_cursor.col += (colnr_T)(p - ml_get_cursor());
Bram Moolenaar68f1a482006-03-17 23:12:21 +00002649 else
2650 {
2651 int l;
2652
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002653 len = (int)(p - tofree);
Bram Moolenaar68f1a482006-03-17 23:12:21 +00002654 for (p = ml_get_cursor(); *p != NUL && len-- > 0; p += l)
2655 {
2656 l = utf_ptr2len(p);
2657 curwin->w_cursor.col += l;
2658 }
2659 }
2660 goto theend;
2661 }
2662 p += len;
2663 }
2664 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2665 break;
2666 ++curwin->w_cursor.lnum;
2667 curwin->w_cursor.col = 0;
2668 }
2669
2670 /* didn't find it: don't move and beep */
2671 curwin->w_cursor = pos;
2672 beep_flush();
2673
2674theend:
2675 vim_free(tofree);
2676 convert_setup(&vimconv, NULL, NULL);
2677}
2678
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002679#if defined(HAVE_GTK2) || defined(PROTO)
2680/*
2681 * Return TRUE if string "s" is a valid utf-8 string.
2682 * When "end" is NULL stop at the first NUL.
2683 * When "end" is positive stop there.
2684 */
2685 int
2686utf_valid_string(s, end)
2687 char_u *s;
2688 char_u *end;
2689{
2690 int l;
2691 char_u *p = s;
2692
2693 while (end == NULL ? *p != NUL : p < end)
2694 {
2695 if ((*p & 0xc0) == 0x80)
2696 return FALSE; /* invalid lead byte */
2697 l = utf8len_tab[*p];
2698 if (end != NULL && p + l > end)
2699 return FALSE; /* incomplete byte sequence */
2700 ++p;
2701 while (--l > 0)
2702 if ((*p++ & 0xc0) != 0x80)
2703 return FALSE; /* invalid trail byte */
2704 }
2705 return TRUE;
2706}
2707#endif
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#if defined(FEAT_GUI) || defined(PROTO)
2710/*
2711 * Special version of mb_tail_off() for use in ScreenLines[].
2712 */
2713 int
2714dbcs_screen_tail_off(base, p)
2715 char_u *base;
2716 char_u *p;
2717{
2718 /* It can't be the first byte if a double-byte when not using DBCS, at the
2719 * end of the string or the byte can't start a double-byte.
2720 * For euc-jp an 0x8e byte always means we have a lead byte in the current
2721 * cell. */
2722 if (*p == NUL || p[1] == NUL
2723 || (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2724 || MB_BYTE2LEN(*p) == 1)
2725 return 0;
2726
2727 /* Return 1 when on the lead byte, 0 when on the tail byte. */
2728 return 1 - dbcs_screen_head_off(base, p);
2729}
2730#endif
2731
2732/*
2733 * If the cursor moves on an trail byte, set the cursor on the lead byte.
2734 * Thus it moves left if necessary.
2735 * Return TRUE when the cursor was adjusted.
2736 */
2737 void
2738mb_adjust_cursor()
2739{
2740 mb_adjustpos(&curwin->w_cursor);
2741}
2742
2743/*
2744 * Adjust position "*lp" to point to the first byte of a multi-byte character.
2745 * If it points to a tail byte it's moved backwards to the head byte.
2746 */
2747 void
2748mb_adjustpos(lp)
2749 pos_T *lp;
2750{
2751 char_u *p;
2752
2753 if (lp->col > 0
2754#ifdef FEAT_VIRTUALEDIT
2755 || lp->coladd > 1
2756#endif
2757 )
2758 {
2759 p = ml_get(lp->lnum);
2760 lp->col -= (*mb_head_off)(p, p + lp->col);
2761#ifdef FEAT_VIRTUALEDIT
2762 /* Reset "coladd" when the cursor would be on the right half of a
2763 * double-wide character. */
2764 if (lp->coladd == 1
2765 && p[lp->col] != TAB
2766 && vim_isprintc((*mb_ptr2char)(p + lp->col))
2767 && ptr2cells(p + lp->col) > 1)
2768 lp->coladd = 0;
2769#endif
2770 }
2771}
2772
2773/*
2774 * Return a pointer to the character before "*p", if there is one.
2775 */
2776 char_u *
2777mb_prevptr(line, p)
2778 char_u *line; /* start of the string */
2779 char_u *p;
2780{
2781 if (p > line)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002782 mb_ptr_back(line, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 return p;
2784}
2785
2786/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002787 * Return the character length of "str". Each multi-byte character (with
2788 * following composing characters) counts as one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 */
2790 int
2791mb_charlen(str)
2792 char_u *str;
2793{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002794 char_u *p = str;
2795 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002797 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 return 0;
2799
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002800 for (count = 0; *p != NUL; count++)
2801 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803 return count;
2804}
2805
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00002806#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002807/*
2808 * Like mb_charlen() but for a string with specified length.
2809 */
2810 int
2811mb_charlen_len(str, len)
2812 char_u *str;
2813 int len;
2814{
2815 char_u *p = str;
2816 int count;
2817
2818 for (count = 0; *p != NUL && p < str + len; count++)
2819 p += (*mb_ptr2len)(p);
2820
2821 return count;
2822}
2823#endif
2824
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825/*
2826 * Try to un-escape a multi-byte character.
2827 * Used for the "to" and "from" part of a mapping.
2828 * Return the un-escaped string if it is a multi-byte character, and advance
2829 * "pp" to just after the bytes that formed it.
2830 * Return NULL if no multi-byte char was found.
2831 */
2832 char_u *
2833mb_unescape(pp)
2834 char_u **pp;
2835{
2836 static char_u buf[MB_MAXBYTES + 1];
2837 int n, m = 0;
2838 char_u *str = *pp;
2839
2840 /* Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI
2841 * KS_EXTRA KE_CSI to CSI. */
2842 for (n = 0; str[n] != NUL && m <= MB_MAXBYTES; ++n)
2843 {
2844 if (str[n] == K_SPECIAL
2845 && str[n + 1] == KS_SPECIAL
2846 && str[n + 2] == KE_FILLER)
2847 {
2848 buf[m++] = K_SPECIAL;
2849 n += 2;
2850 }
2851# ifdef FEAT_GUI
2852 else if (str[n] == CSI
2853 && str[n + 1] == KS_EXTRA
2854 && str[n + 2] == (int)KE_CSI)
2855 {
2856 buf[m++] = CSI;
2857 n += 2;
2858 }
2859# endif
2860 else if (str[n] == K_SPECIAL
2861# ifdef FEAT_GUI
2862 || str[n] == CSI
2863# endif
2864 )
2865 break; /* a special key can't be a multibyte char */
2866 else
2867 buf[m++] = str[n];
2868 buf[m] = NUL;
2869
2870 /* Return a multi-byte character if it's found. An illegal sequence
2871 * will result in a 1 here. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002872 if ((*mb_ptr2len)(buf) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
2874 *pp = str + n + 1;
2875 return buf;
2876 }
2877 }
2878 return NULL;
2879}
2880
2881/*
2882 * Return TRUE if the character at "row"/"col" on the screen is the left side
2883 * of a double-width character.
2884 * Caller must make sure "row" and "col" are not invalid!
2885 */
2886 int
2887mb_lefthalve(row, col)
2888 int row;
2889 int col;
2890{
2891#ifdef FEAT_HANGULIN
2892 if (composing_hangul)
2893 return TRUE;
2894#endif
2895 if (enc_dbcs != 0)
2896 return dbcs_off2cells(LineOffset[row] + col) > 1;
2897 if (enc_utf8)
2898 return (col + 1 < Columns
2899 && ScreenLines[LineOffset[row] + col + 1] == 0);
2900 return FALSE;
2901}
2902
2903# if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
2904 || defined(PROTO)
2905/*
2906 * Correct a position on the screen, if it's the right halve of a double-wide
2907 * char move it to the left halve. Returns the corrected column.
2908 */
2909 int
2910mb_fix_col(col, row)
2911 int col;
2912 int row;
2913{
2914 col = check_col(col);
2915 row = check_row(row);
2916 if (has_mbyte && ScreenLines != NULL && col > 0
2917 && ((enc_dbcs
2918 && ScreenLines[LineOffset[row] + col] != NUL
2919 && dbcs_screen_head_off(ScreenLines + LineOffset[row],
2920 ScreenLines + LineOffset[row] + col))
2921 || (enc_utf8 && ScreenLines[LineOffset[row] + col] == 0)))
2922 --col;
2923 return col;
2924}
2925# endif
2926#endif
2927
2928#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
2929static int enc_alias_search __ARGS((char_u *name));
2930
2931/*
2932 * Skip the Vim specific head of a 'encoding' name.
2933 */
2934 char_u *
2935enc_skip(p)
2936 char_u *p;
2937{
2938 if (STRNCMP(p, "2byte-", 6) == 0)
2939 return p + 6;
2940 if (STRNCMP(p, "8bit-", 5) == 0)
2941 return p + 5;
2942 return p;
2943}
2944
2945/*
2946 * Find the canonical name for encoding "enc".
2947 * When the name isn't recognized, returns "enc" itself, but with all lower
2948 * case characters and '_' replaced with '-'.
2949 * Returns an allocated string. NULL for out-of-memory.
2950 */
2951 char_u *
2952enc_canonize(enc)
2953 char_u *enc;
2954{
2955 char_u *r;
2956 char_u *p, *s;
2957 int i;
2958
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002959# ifdef FEAT_MBYTE
2960 if (STRCMP(enc, "default") == 0)
2961 {
2962 /* Use the default encoding as it's found by set_init_1(). */
2963 r = get_encoding_default();
2964 if (r == NULL)
2965 r = (char_u *)"latin1";
2966 return vim_strsave(r);
2967 }
2968# endif
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 /* copy "enc" to allocted memory, with room for two '-' */
2971 r = alloc((unsigned)(STRLEN(enc) + 3));
2972 if (r != NULL)
2973 {
2974 /* Make it all lower case and replace '_' with '-'. */
2975 p = r;
2976 for (s = enc; *s != NUL; ++s)
2977 {
2978 if (*s == '_')
2979 *p++ = '-';
2980 else
2981 *p++ = TOLOWER_ASC(*s);
2982 }
2983 *p = NUL;
2984
2985 /* Skip "2byte-" and "8bit-". */
2986 p = enc_skip(r);
2987
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002988 /* Change "microsoft-cp" to "cp". Used in some spell files. */
2989 if (STRNCMP(p, "microsoft-cp", 12) == 0)
2990 mch_memmove(p, p + 10, STRLEN(p + 10) + 1);
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 /* "iso8859" -> "iso-8859" */
2993 if (STRNCMP(p, "iso8859", 7) == 0)
2994 {
2995 mch_memmove(p + 4, p + 3, STRLEN(p + 2));
2996 p[3] = '-';
2997 }
2998
2999 /* "iso-8859n" -> "iso-8859-n" */
3000 if (STRNCMP(p, "iso-8859", 8) == 0 && p[8] != '-')
3001 {
3002 mch_memmove(p + 9, p + 8, STRLEN(p + 7));
3003 p[8] = '-';
3004 }
3005
3006 /* "latin-N" -> "latinN" */
3007 if (STRNCMP(p, "latin-", 6) == 0)
3008 mch_memmove(p + 5, p + 6, STRLEN(p + 5));
3009
3010 if (enc_canon_search(p) >= 0)
3011 {
3012 /* canonical name can be used unmodified */
3013 if (p != r)
3014 mch_memmove(r, p, STRLEN(p) + 1);
3015 }
3016 else if ((i = enc_alias_search(p)) >= 0)
3017 {
3018 /* alias recognized, get canonical name */
3019 vim_free(r);
3020 r = vim_strsave((char_u *)enc_canon_table[i].name);
3021 }
3022 }
3023 return r;
3024}
3025
3026/*
3027 * Search for an encoding alias of "name".
3028 * Returns -1 when not found.
3029 */
3030 static int
3031enc_alias_search(name)
3032 char_u *name;
3033{
3034 int i;
3035
3036 for (i = 0; enc_alias_table[i].name != NULL; ++i)
3037 if (STRCMP(name, enc_alias_table[i].name) == 0)
3038 return enc_alias_table[i].canon;
3039 return -1;
3040}
3041#endif
3042
3043#if defined(FEAT_MBYTE) || defined(PROTO)
3044
3045#ifdef HAVE_LANGINFO_H
3046# include <langinfo.h>
3047#endif
3048
3049/*
3050 * Get the canonicalized encoding of the current locale.
3051 * Returns an allocated string when successful, NULL when not.
3052 */
3053 char_u *
3054enc_locale()
3055{
3056#ifndef WIN3264
3057 char *s;
3058 char *p;
3059 int i;
3060#endif
3061 char buf[50];
3062#ifdef WIN3264
3063 long acp = GetACP();
3064
3065 if (acp == 1200)
3066 STRCPY(buf, "ucs-2le");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003067 else if (acp == 1252) /* cp1252 is used as latin1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 STRCPY(buf, "latin1");
3069 else
3070 sprintf(buf, "cp%ld", acp);
3071#else
3072# ifdef HAVE_NL_LANGINFO_CODESET
3073 if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
3074# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003075# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 if ((s = getenv("LC_ALL")) == NULL || *s == NUL)
3079 if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL)
3080 s = getenv("LANG");
3081
3082 if (s == NULL || *s == NUL)
3083 return FAIL;
3084
3085 /* The most generic locale format is:
3086 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
3087 * If there is a '.' remove the part before it.
3088 * if there is something after the codeset, remove it.
3089 * Make the name lowercase and replace '_' with '-'.
3090 * Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
3091 * "ko_KR.EUC" == "euc-kr"
3092 */
3093 if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL)
3094 {
3095 if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0
3096 && !isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_')
3097 {
3098 /* copy "XY.EUC" to "euc-XY" to buf[10] */
3099 STRCPY(buf + 10, "euc-");
3100 buf[14] = p[-2];
3101 buf[15] = p[-1];
3102 buf[16] = 0;
3103 s = buf + 10;
3104 }
3105 else
3106 s = p + 1;
3107 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003108 for (i = 0; s[i] != NUL && s + i < buf + sizeof(buf) - 1; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 if (s[i] == '_' || s[i] == '-')
3111 buf[i] = '-';
3112 else if (isalnum((int)s[i]))
3113 buf[i] = TOLOWER_ASC(s[i]);
3114 else
3115 break;
3116 }
3117 buf[i] = NUL;
3118#endif
3119
3120 return enc_canonize((char_u *)buf);
3121}
3122
3123#if defined(WIN3264) || defined(PROTO)
3124/*
3125 * Convert an encoding name to an MS-Windows codepage.
3126 * Returns zero if no codepage can be figured out.
3127 */
3128 int
3129encname2codepage(name)
3130 char_u *name;
3131{
3132 int cp;
3133 char_u *p = name;
3134 int idx;
3135
3136 if (STRNCMP(p, "8bit-", 5) == 0)
3137 p += 5;
3138 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
3139 p += 6;
3140
3141 if (p[0] == 'c' && p[1] == 'p')
3142 cp = atoi(p + 2);
3143 else if ((idx = enc_canon_search(p)) >= 0)
3144 cp = enc_canon_table[idx].codepage;
3145 else
3146 return 0;
3147 if (IsValidCodePage(cp))
3148 return cp;
3149 return 0;
3150}
3151#endif
3152
3153# if defined(USE_ICONV) || defined(PROTO)
3154
3155static char_u *iconv_string __ARGS((vimconv_T *vcp, char_u *str, int slen, int *unconvlenp));
3156
3157/*
3158 * Call iconv_open() with a check if iconv() works properly (there are broken
3159 * versions).
3160 * Returns (void *)-1 if failed.
3161 * (should return iconv_t, but that causes problems with prototypes).
3162 */
3163 void *
3164my_iconv_open(to, from)
3165 char_u *to;
3166 char_u *from;
3167{
3168 iconv_t fd;
3169#define ICONV_TESTLEN 400
3170 char_u tobuf[ICONV_TESTLEN];
3171 char *p;
3172 size_t tolen;
3173 static int iconv_ok = -1;
3174
3175 if (iconv_ok == FALSE)
3176 return (void *)-1; /* detected a broken iconv() previously */
3177
3178#ifdef DYNAMIC_ICONV
3179 /* Check if the iconv.dll can be found. */
3180 if (!iconv_enabled(TRUE))
3181 return (void *)-1;
3182#endif
3183
3184 fd = iconv_open((char *)enc_skip(to), (char *)enc_skip(from));
3185
3186 if (fd != (iconv_t)-1 && iconv_ok == -1)
3187 {
3188 /*
3189 * Do a dummy iconv() call to check if it actually works. There is a
3190 * version of iconv() on Linux that is broken. We can't ignore it,
3191 * because it's wide-spread. The symptoms are that after outputting
3192 * the initial shift state the "to" pointer is NULL and conversion
3193 * stops for no apparent reason after about 8160 characters.
3194 */
3195 p = (char *)tobuf;
3196 tolen = ICONV_TESTLEN;
3197 (void)iconv(fd, NULL, NULL, &p, &tolen);
3198 if (p == NULL)
3199 {
3200 iconv_ok = FALSE;
3201 iconv_close(fd);
3202 fd = (iconv_t)-1;
3203 }
3204 else
3205 iconv_ok = TRUE;
3206 }
3207
3208 return (void *)fd;
3209}
3210
3211/*
3212 * Convert the string "str[slen]" with iconv().
3213 * If "unconvlenp" is not NULL handle the string ending in an incomplete
3214 * sequence and set "*unconvlenp" to the length of it.
3215 * Returns the converted string in allocated memory. NULL for an error.
3216 */
3217 static char_u *
3218iconv_string(vcp, str, slen, unconvlenp)
3219 vimconv_T *vcp;
3220 char_u *str;
3221 int slen;
3222 int *unconvlenp;
3223{
3224 const char *from;
3225 size_t fromlen;
3226 char *to;
3227 size_t tolen;
3228 size_t len = 0;
3229 size_t done = 0;
3230 char_u *result = NULL;
3231 char_u *p;
3232 int l;
3233
3234 from = (char *)str;
3235 fromlen = slen;
3236 for (;;)
3237 {
3238 if (len == 0 || ICONV_ERRNO == ICONV_E2BIG)
3239 {
3240 /* Allocate enough room for most conversions. When re-allocating
3241 * increase the buffer size. */
3242 len = len + fromlen * 2 + 40;
3243 p = alloc((unsigned)len);
3244 if (p != NULL && done > 0)
3245 mch_memmove(p, result, done);
3246 vim_free(result);
3247 result = p;
3248 if (result == NULL) /* out of memory */
3249 break;
3250 }
3251
3252 to = (char *)result + done;
3253 tolen = len - done - 2;
3254 /* Avoid a warning for systems with a wrong iconv() prototype by
3255 * casting the second argument to void *. */
3256 if (iconv(vcp->vc_fd, (void *)&from, &fromlen, &to, &tolen)
3257 != (size_t)-1)
3258 {
3259 /* Finished, append a NUL. */
3260 *to = NUL;
3261 break;
3262 }
3263
3264 /* Check both ICONV_EINVAL and EINVAL, because the dynamically loaded
3265 * iconv library may use one of them. */
3266 if (!vcp->vc_fail && unconvlenp != NULL
3267 && (ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
3268 {
3269 /* Handle an incomplete sequence at the end. */
3270 *to = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003271 *unconvlenp = (int)fromlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 break;
3273 }
3274
3275 /* Check both ICONV_EILSEQ and EILSEQ, because the dynamically loaded
3276 * iconv library may use one of them. */
3277 else if (!vcp->vc_fail
3278 && (ICONV_ERRNO == ICONV_EILSEQ || ICONV_ERRNO == EILSEQ
3279 || ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
3280 {
3281 /* Can't convert: insert a '?' and skip a character. This assumes
3282 * conversion from 'encoding' to something else. In other
3283 * situations we don't know what to skip anyway. */
3284 *to++ = '?';
3285 if ((*mb_ptr2cells)((char_u *)from) > 1)
3286 *to++ = '?';
Bram Moolenaar217ad922005-03-20 22:37:15 +00003287 if (enc_utf8)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003288 l = utfc_ptr2len_len((char_u *)from, (int)fromlen);
Bram Moolenaar217ad922005-03-20 22:37:15 +00003289 else
3290 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003291 l = (*mb_ptr2len)((char_u *)from);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003292 if (l > (int)fromlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003293 l = (int)fromlen;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 from += l;
3296 fromlen -= l;
3297 }
3298 else if (ICONV_ERRNO != ICONV_E2BIG)
3299 {
3300 /* conversion failed */
3301 vim_free(result);
3302 result = NULL;
3303 break;
3304 }
3305 /* Not enough room or skipping illegal sequence. */
3306 done = to - (char *)result;
3307 }
3308 return result;
3309}
3310
3311# if defined(DYNAMIC_ICONV) || defined(PROTO)
3312/*
3313 * Dynamically load the "iconv.dll" on Win32.
3314 */
3315
3316#ifndef DYNAMIC_ICONV /* just generating prototypes */
3317# define HINSTANCE int
3318#endif
Bram Moolenaard6f676d2005-06-01 21:51:55 +00003319static HINSTANCE hIconvDLL = 0;
3320static HINSTANCE hMsvcrtDLL = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322# ifndef DYNAMIC_ICONV_DLL
3323# define DYNAMIC_ICONV_DLL "iconv.dll"
3324# define DYNAMIC_ICONV_DLL_ALT "libiconv.dll"
3325# endif
3326# ifndef DYNAMIC_MSVCRT_DLL
3327# define DYNAMIC_MSVCRT_DLL "msvcrt.dll"
3328# endif
3329
3330/*
3331 * Try opening the iconv.dll and return TRUE if iconv() can be used.
3332 */
3333 int
3334iconv_enabled(verbose)
3335 int verbose;
3336{
3337 if (hIconvDLL != 0 && hMsvcrtDLL != 0)
3338 return TRUE;
3339 hIconvDLL = LoadLibrary(DYNAMIC_ICONV_DLL);
3340 if (hIconvDLL == 0) /* sometimes it's called libiconv.dll */
3341 hIconvDLL = LoadLibrary(DYNAMIC_ICONV_DLL_ALT);
3342 if (hIconvDLL != 0)
3343 hMsvcrtDLL = LoadLibrary(DYNAMIC_MSVCRT_DLL);
3344 if (hIconvDLL == 0 || hMsvcrtDLL == 0)
3345 {
3346 /* Only give the message when 'verbose' is set, otherwise it might be
3347 * done whenever a conversion is attempted. */
3348 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003349 {
3350 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 EMSG2(_(e_loadlib),
3352 hIconvDLL == 0 ? DYNAMIC_ICONV_DLL : DYNAMIC_MSVCRT_DLL);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003353 verbose_leave();
3354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 iconv_end();
3356 return FALSE;
3357 }
3358
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003359 iconv = (void *)GetProcAddress(hIconvDLL, "libiconv");
3360 iconv_open = (void *)GetProcAddress(hIconvDLL, "libiconv_open");
3361 iconv_close = (void *)GetProcAddress(hIconvDLL, "libiconv_close");
3362 iconvctl = (void *)GetProcAddress(hIconvDLL, "libiconvctl");
3363 iconv_errno = (void *)GetProcAddress(hMsvcrtDLL, "_errno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (iconv == NULL || iconv_open == NULL || iconv_close == NULL
3365 || iconvctl == NULL || iconv_errno == NULL)
3366 {
3367 iconv_end();
3368 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003369 {
3370 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 EMSG2(_(e_loadfunc), "for libiconv");
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003372 verbose_leave();
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 return FALSE;
3375 }
3376 return TRUE;
3377}
3378
3379 void
3380iconv_end()
3381{
3382 /* Don't use iconv() when inputting or outputting characters. */
3383 if (input_conv.vc_type == CONV_ICONV)
3384 convert_setup(&input_conv, NULL, NULL);
3385 if (output_conv.vc_type == CONV_ICONV)
3386 convert_setup(&output_conv, NULL, NULL);
3387
3388 if (hIconvDLL != 0)
3389 FreeLibrary(hIconvDLL);
3390 if (hMsvcrtDLL != 0)
3391 FreeLibrary(hMsvcrtDLL);
3392 hIconvDLL = 0;
3393 hMsvcrtDLL = 0;
3394}
3395# endif /* DYNAMIC_ICONV */
3396# endif /* USE_ICONV */
3397
3398#endif /* FEAT_MBYTE */
3399
3400#if defined(FEAT_XIM) || defined(PROTO)
3401
3402# ifdef FEAT_GUI_GTK
3403static int xim_has_preediting INIT(= FALSE); /* IM current status */
3404
3405/*
3406 * Set preedit_start_col to the current cursor position.
3407 */
3408 static void
3409init_preedit_start_col(void)
3410{
3411 if (State & CMDLINE)
3412 preedit_start_col = cmdline_getvcol_cursor();
3413 else if (curwin != NULL)
3414 getvcol(curwin, &curwin->w_cursor, &preedit_start_col, NULL, NULL);
3415 /* Prevent that preediting marks the buffer as changed. */
3416 xim_changed_while_preediting = curbuf->b_changed;
3417}
3418# endif
3419
3420# if defined(HAVE_GTK2) && !defined(PROTO)
3421
3422static int im_is_active = FALSE; /* IM is enabled for current mode */
3423static int im_preedit_cursor = 0; /* cursor offset in characters */
3424static int im_preedit_trailing = 0; /* number of characters after cursor */
3425
3426static unsigned long im_commit_handler_id = 0;
3427static unsigned int im_activatekey_keyval = GDK_VoidSymbol;
3428static unsigned int im_activatekey_state = 0;
3429
3430 void
3431im_set_active(int active)
3432{
3433 int was_active;
3434
3435 was_active = !!im_is_active;
3436 im_is_active = (active && !p_imdisable);
3437
3438 if (im_is_active != was_active)
3439 xim_reset();
3440}
3441
3442 void
3443xim_set_focus(int focus)
3444{
3445 if (xic != NULL)
3446 {
3447 if (focus)
3448 gtk_im_context_focus_in(xic);
3449 else
3450 gtk_im_context_focus_out(xic);
3451 }
3452}
3453
3454 void
3455im_set_position(int row, int col)
3456{
3457 if (xic != NULL)
3458 {
3459 GdkRectangle area;
3460
3461 area.x = FILL_X(col);
3462 area.y = FILL_Y(row);
3463 area.width = gui.char_width * (mb_lefthalve(row, col) ? 2 : 1);
3464 area.height = gui.char_height;
3465
3466 gtk_im_context_set_cursor_location(xic, &area);
3467 }
3468}
3469
3470# if 0 || defined(PROTO) /* apparently only used in gui_x11.c */
3471 void
3472xim_set_preedit(void)
3473{
3474 im_set_position(gui.row, gui.col);
3475}
3476# endif
3477
3478 static void
3479im_add_to_input(char_u *str, int len)
3480{
3481 /* Convert from 'termencoding' (always "utf-8") to 'encoding' */
3482 if (input_conv.vc_type != CONV_NONE)
3483 {
3484 str = string_convert(&input_conv, str, &len);
3485 g_return_if_fail(str != NULL);
3486 }
3487
3488 add_to_input_buf_csi(str, len);
3489
3490 if (input_conv.vc_type != CONV_NONE)
3491 vim_free(str);
3492
3493 if (p_mh) /* blank out the pointer if necessary */
3494 gui_mch_mousehide(TRUE);
3495}
3496
3497 static void
3498im_delete_preedit(void)
3499{
3500 char_u bskey[] = {CSI, 'k', 'b'};
3501 char_u delkey[] = {CSI, 'k', 'D'};
3502
3503 if (State & NORMAL)
3504 {
3505 im_preedit_cursor = 0;
3506 return;
3507 }
3508 for (; im_preedit_cursor > 0; --im_preedit_cursor)
3509 add_to_input_buf(bskey, (int)sizeof(bskey));
3510
3511 for (; im_preedit_trailing > 0; --im_preedit_trailing)
3512 add_to_input_buf(delkey, (int)sizeof(delkey));
3513}
3514
3515 static void
3516im_correct_cursor(int num_move_back)
3517{
3518 char_u backkey[] = {CSI, 'k', 'l'};
3519
3520 if (State & NORMAL)
3521 return;
3522# ifdef FEAT_RIGHTLEFT
3523 if ((State & CMDLINE) == 0 && curwin != NULL && curwin->w_p_rl)
3524 backkey[2] = 'r';
3525# endif
3526 for (; num_move_back > 0; --num_move_back)
3527 add_to_input_buf(backkey, (int)sizeof(backkey));
3528}
3529
3530static int xim_expected_char = NUL;
3531static int xim_ignored_char = FALSE;
3532
3533/*
3534 * Update the mode and cursor while in an IM callback.
3535 */
3536 static void
3537im_show_info(void)
3538{
3539 int old_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 old_vgetc_busy = vgetc_busy;
3542 vgetc_busy = TRUE;
3543 showmode();
3544 vgetc_busy = old_vgetc_busy;
3545 setcursor();
3546 out_flush();
3547}
3548
3549/*
3550 * Callback invoked when the user finished preediting.
3551 * Put the final string into the input buffer.
3552 */
3553/*ARGSUSED0*/
3554 static void
3555im_commit_cb(GtkIMContext *context, const gchar *str, gpointer data)
3556{
3557 int slen = (int)STRLEN(str);
3558 int add_to_input = TRUE;
3559 int clen;
3560 int len = slen;
3561 int commit_with_preedit = TRUE;
3562 char_u *im_str, *p;
3563
3564#ifdef XIM_DEBUG
3565 xim_log("im_commit_cb(): %s\n", str);
3566#endif
3567
3568 /* The imhangul module doesn't reset the preedit string before
3569 * committing. Call im_delete_preedit() to work around that. */
3570 im_delete_preedit();
3571
3572 /* Indicate that preediting has finished. */
3573 if (preedit_start_col == MAXCOL)
3574 {
3575 init_preedit_start_col();
3576 commit_with_preedit = FALSE;
3577 }
3578
3579 /* The thing which setting "preedit_start_col" to MAXCOL means that
3580 * "preedit_start_col" will be set forcely when calling
3581 * preedit_changed_cb() next time.
3582 * "preedit_start_col" should not reset with MAXCOL on this part. Vim
3583 * is simulating the preediting by using add_to_input_str(). when
3584 * preedit begin immediately before committed, the typebuf is not
3585 * flushed to screen, then it can't get correct "preedit_start_col".
3586 * Thus, it should calculate the cells by adding cells of the committed
3587 * string. */
3588 if (input_conv.vc_type != CONV_NONE)
3589 {
3590 im_str = string_convert(&input_conv, (char_u *)str, &len);
3591 g_return_if_fail(im_str != NULL);
3592 }
3593 else
3594 im_str = (char_u *)str;
3595 clen = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003596 for (p = im_str; p < im_str + len; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 clen += (*mb_ptr2cells)(p);
3598 if (input_conv.vc_type != CONV_NONE)
3599 vim_free(im_str);
3600 preedit_start_col += clen;
3601
3602 /* Is this a single character that matches a keypad key that's just
3603 * been pressed? If so, we don't want it to be entered as such - let
3604 * us carry on processing the raw keycode so that it may be used in
3605 * mappings as <kSomething>. */
3606 if (xim_expected_char != NUL)
3607 {
3608 /* We're currently processing a keypad or other special key */
3609 if (slen == 1 && str[0] == xim_expected_char)
3610 {
3611 /* It's a match - don't do it here */
3612 xim_ignored_char = TRUE;
3613 add_to_input = FALSE;
3614 }
3615 else
3616 {
3617 /* Not a match */
3618 xim_ignored_char = FALSE;
3619 }
3620 }
3621
3622 if (add_to_input)
3623 im_add_to_input((char_u *)str, slen);
3624
3625 /* Inserting chars while "im_is_active" is set does not cause a change of
3626 * buffer. When the chars are committed the buffer must be marked as
3627 * changed. */
3628 if (!commit_with_preedit)
3629 preedit_start_col = MAXCOL;
3630
3631 /* This flag is used in changed() at next call. */
3632 xim_changed_while_preediting = TRUE;
3633
3634 if (gtk_main_level() > 0)
3635 gtk_main_quit();
3636}
3637
3638/*
3639 * Callback invoked after start to the preedit.
3640 */
3641/*ARGSUSED*/
3642 static void
3643im_preedit_start_cb(GtkIMContext *context, gpointer data)
3644{
3645#ifdef XIM_DEBUG
3646 xim_log("im_preedit_start_cb()\n");
3647#endif
3648
3649 im_is_active = TRUE;
3650 gui_update_cursor(TRUE, FALSE);
3651}
3652
3653/*
3654 * Callback invoked after end to the preedit.
3655 */
3656/*ARGSUSED*/
3657 static void
3658im_preedit_end_cb(GtkIMContext *context, gpointer data)
3659{
3660#ifdef XIM_DEBUG
3661 xim_log("im_preedit_end_cb()\n");
3662#endif
3663 im_delete_preedit();
3664
3665 /* Indicate that preediting has finished */
3666 preedit_start_col = MAXCOL;
3667 xim_has_preediting = FALSE;
3668
3669 im_is_active = FALSE;
3670 gui_update_cursor(TRUE, FALSE);
3671 im_show_info();
3672}
3673
3674/*
3675 * Callback invoked after changes to the preedit string. If the preedit
3676 * string was empty before, remember the preedit start column so we know
3677 * where to apply feedback attributes. Delete the previous preedit string
3678 * if there was one, save the new preedit cursor offset, and put the new
3679 * string into the input buffer.
3680 *
3681 * TODO: The pragmatic "put into input buffer" approach used here has
3682 * several fundamental problems:
3683 *
3684 * - The characters in the preedit string are subject to remapping.
3685 * That's broken, only the finally committed string should be remapped.
3686 *
3687 * - There is a race condition involved: The retrieved value for the
3688 * current cursor position will be wrong if any unprocessed characters
3689 * are still queued in the input buffer.
3690 *
3691 * - Due to the lack of synchronization between the file buffer in memory
3692 * and any typed characters, it's practically impossible to implement the
3693 * "retrieve_surrounding" and "delete_surrounding" signals reliably. IM
3694 * modules for languages such as Thai are likely to rely on this feature
3695 * for proper operation.
3696 *
3697 * Conclusions: I think support for preediting needs to be moved to the
3698 * core parts of Vim. Ideally, until it has been committed, the preediting
3699 * string should only be displayed and not affect the buffer content at all.
3700 * The question how to deal with the synchronization issue still remains.
3701 * Circumventing the input buffer is probably not desirable. Anyway, I think
3702 * implementing "retrieve_surrounding" is the only hard problem.
3703 *
3704 * One way to solve all of this in a clean manner would be to queue all key
3705 * press/release events "as is" in the input buffer, and apply the IM filtering
3706 * at the receiving end of the queue. This, however, would have a rather large
3707 * impact on the code base. If there is an easy way to force processing of all
3708 * remaining input from within the "retrieve_surrounding" signal handler, this
3709 * might not be necessary. Gotta ask on vim-dev for opinions.
3710 */
3711/*ARGSUSED1*/
3712 static void
3713im_preedit_changed_cb(GtkIMContext *context, gpointer data)
3714{
3715 char *preedit_string = NULL;
3716 int cursor_index = 0;
3717 int num_move_back = 0;
3718 char_u *str;
3719 char_u *p;
3720 int i;
3721
3722 gtk_im_context_get_preedit_string(context,
3723 &preedit_string, NULL,
3724 &cursor_index);
3725
3726#ifdef XIM_DEBUG
3727 xim_log("im_preedit_changed_cb(): %s\n", preedit_string);
3728#endif
3729
3730 g_return_if_fail(preedit_string != NULL); /* just in case */
3731
3732 /* If preedit_start_col is MAXCOL set it to the current cursor position. */
3733 if (preedit_start_col == MAXCOL && preedit_string[0] != '\0')
3734 {
3735 xim_has_preediting = TRUE;
3736
3737 /* Urgh, this breaks if the input buffer isn't empty now */
3738 init_preedit_start_col();
3739 }
3740 else if (cursor_index == 0 && preedit_string[0] == '\0')
3741 {
3742 if (preedit_start_col == MAXCOL)
3743 xim_has_preediting = FALSE;
3744
3745 /* If at the start position (after typing backspace)
3746 * preedit_start_col must be reset. */
3747 preedit_start_col = MAXCOL;
3748 }
3749
3750 im_delete_preedit();
3751
3752 /*
3753 * Compute the end of the preediting area: "preedit_end_col".
3754 * According to the documentation of gtk_im_context_get_preedit_string(),
3755 * the cursor_pos output argument returns the offset in bytes. This is
3756 * unfortunately not true -- real life shows the offset is in characters,
3757 * and the GTK+ source code agrees with me. Will file a bug later.
3758 */
3759 if (preedit_start_col != MAXCOL)
3760 preedit_end_col = preedit_start_col;
3761 str = (char_u *)preedit_string;
3762 for (p = str, i = 0; *p != NUL; p += utf_byte2len(*p), ++i)
3763 {
3764 int is_composing;
3765
3766 is_composing = ((*p & 0x80) != 0 && utf_iscomposing(utf_ptr2char(p)));
3767 /*
3768 * These offsets are used as counters when generating <BS> and <Del>
3769 * to delete the preedit string. So don't count composing characters
3770 * unless 'delcombine' is enabled.
3771 */
3772 if (!is_composing || p_deco)
3773 {
3774 if (i < cursor_index)
3775 ++im_preedit_cursor;
3776 else
3777 ++im_preedit_trailing;
3778 }
3779 if (!is_composing && i >= cursor_index)
3780 {
3781 /* This is essentially the same as im_preedit_trailing, except
3782 * composing characters are not counted even if p_deco is set. */
3783 ++num_move_back;
3784 }
3785 if (preedit_start_col != MAXCOL)
3786 preedit_end_col += utf_ptr2cells(p);
3787 }
3788
3789 if (p > str)
3790 {
3791 im_add_to_input(str, (int)(p - str));
3792 im_correct_cursor(num_move_back);
3793 }
3794
3795 g_free(preedit_string);
3796
3797 if (gtk_main_level() > 0)
3798 gtk_main_quit();
3799}
3800
3801/*
3802 * Translate the Pango attributes at iter to Vim highlighting attributes.
3803 * Ignore attributes not supported by Vim highlighting. This shouldn't have
3804 * too much impact -- right now we handle even more attributes than necessary
3805 * for the IM modules I tested with.
3806 */
3807 static int
3808translate_pango_attributes(PangoAttrIterator *iter)
3809{
3810 PangoAttribute *attr;
3811 int char_attr = HL_NORMAL;
3812
3813 attr = pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE);
3814 if (attr != NULL && ((PangoAttrInt *)attr)->value
3815 != (int)PANGO_UNDERLINE_NONE)
3816 char_attr |= HL_UNDERLINE;
3817
3818 attr = pango_attr_iterator_get(iter, PANGO_ATTR_WEIGHT);
3819 if (attr != NULL && ((PangoAttrInt *)attr)->value >= (int)PANGO_WEIGHT_BOLD)
3820 char_attr |= HL_BOLD;
3821
3822 attr = pango_attr_iterator_get(iter, PANGO_ATTR_STYLE);
3823 if (attr != NULL && ((PangoAttrInt *)attr)->value
3824 != (int)PANGO_STYLE_NORMAL)
3825 char_attr |= HL_ITALIC;
3826
3827 attr = pango_attr_iterator_get(iter, PANGO_ATTR_BACKGROUND);
3828 if (attr != NULL)
3829 {
3830 const PangoColor *color = &((PangoAttrColor *)attr)->color;
3831
3832 /* Assume inverse if black background is requested */
3833 if ((color->red | color->green | color->blue) == 0)
3834 char_attr |= HL_INVERSE;
3835 }
3836
3837 return char_attr;
3838}
3839
3840/*
3841 * Retrieve the highlighting attributes at column col in the preedit string.
3842 * Return -1 if not in preediting mode or if col is out of range.
3843 */
3844 int
3845im_get_feedback_attr(int col)
3846{
3847 char *preedit_string = NULL;
3848 PangoAttrList *attr_list = NULL;
3849 int char_attr = -1;
3850
3851 if (xic == NULL)
3852 return char_attr;
3853
3854 gtk_im_context_get_preedit_string(xic, &preedit_string, &attr_list, NULL);
3855
3856 if (preedit_string != NULL && attr_list != NULL)
3857 {
3858 int index;
3859
3860 /* Get the byte index as used by PangoAttrIterator */
3861 for (index = 0; col > 0 && preedit_string[index] != '\0'; --col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003862 index += utfc_ptr2len((char_u *)preedit_string + index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 if (preedit_string[index] != '\0')
3865 {
3866 PangoAttrIterator *iter;
3867 int start, end;
3868
3869 char_attr = HL_NORMAL;
3870 iter = pango_attr_list_get_iterator(attr_list);
3871
3872 /* Extract all relevant attributes from the list. */
3873 do
3874 {
3875 pango_attr_iterator_range(iter, &start, &end);
3876
3877 if (index >= start && index < end)
3878 char_attr |= translate_pango_attributes(iter);
3879 }
3880 while (pango_attr_iterator_next(iter));
3881
3882 pango_attr_iterator_destroy(iter);
3883 }
3884 }
3885
3886 if (attr_list != NULL)
3887 pango_attr_list_unref(attr_list);
3888 g_free(preedit_string);
3889
3890 return char_attr;
3891}
3892
3893 void
3894xim_init(void)
3895{
3896#ifdef XIM_DEBUG
3897 xim_log("xim_init()\n");
3898#endif
3899
3900 g_return_if_fail(gui.drawarea != NULL);
3901 g_return_if_fail(gui.drawarea->window != NULL);
3902
3903 xic = gtk_im_multicontext_new();
3904 g_object_ref(xic);
3905
3906 im_commit_handler_id = g_signal_connect(G_OBJECT(xic), "commit",
3907 G_CALLBACK(&im_commit_cb), NULL);
3908 g_signal_connect(G_OBJECT(xic), "preedit_changed",
3909 G_CALLBACK(&im_preedit_changed_cb), NULL);
3910 g_signal_connect(G_OBJECT(xic), "preedit_start",
3911 G_CALLBACK(&im_preedit_start_cb), NULL);
3912 g_signal_connect(G_OBJECT(xic), "preedit_end",
3913 G_CALLBACK(&im_preedit_end_cb), NULL);
3914
3915 gtk_im_context_set_client_window(xic, gui.drawarea->window);
3916}
3917
3918 void
3919im_shutdown(void)
3920{
3921#ifdef XIM_DEBUG
3922 xim_log("im_shutdown()\n");
3923#endif
3924
3925 if (xic != NULL)
3926 {
3927 gtk_im_context_focus_out(xic);
3928 g_object_unref(xic);
3929 xic = NULL;
3930 }
3931 im_is_active = FALSE;
3932 im_commit_handler_id = 0;
3933 preedit_start_col = MAXCOL;
3934 xim_has_preediting = FALSE;
3935}
3936
3937/*
3938 * Convert the string argument to keyval and state for GdkEventKey.
3939 * If str is valid return TRUE, otherwise FALSE.
3940 *
3941 * See 'imactivatekey' for documentation of the format.
3942 */
3943 static int
3944im_string_to_keyval(const char *str, unsigned int *keyval, unsigned int *state)
3945{
3946 const char *mods_end;
3947 unsigned tmp_keyval;
3948 unsigned tmp_state = 0;
3949
3950 mods_end = strrchr(str, '-');
3951 mods_end = (mods_end != NULL) ? mods_end + 1 : str;
3952
3953 /* Parse modifier keys */
3954 while (str < mods_end)
3955 switch (*str++)
3956 {
3957 case '-': break;
3958 case 'S': case 's': tmp_state |= (unsigned)GDK_SHIFT_MASK; break;
3959 case 'L': case 'l': tmp_state |= (unsigned)GDK_LOCK_MASK; break;
3960 case 'C': case 'c': tmp_state |= (unsigned)GDK_CONTROL_MASK;break;
3961 case '1': tmp_state |= (unsigned)GDK_MOD1_MASK; break;
3962 case '2': tmp_state |= (unsigned)GDK_MOD2_MASK; break;
3963 case '3': tmp_state |= (unsigned)GDK_MOD3_MASK; break;
3964 case '4': tmp_state |= (unsigned)GDK_MOD4_MASK; break;
3965 case '5': tmp_state |= (unsigned)GDK_MOD5_MASK; break;
3966 default:
3967 return FALSE;
3968 }
3969
3970 tmp_keyval = gdk_keyval_from_name(str);
3971
3972 if (tmp_keyval == 0 || tmp_keyval == GDK_VoidSymbol)
3973 return FALSE;
3974
3975 if (keyval != NULL)
3976 *keyval = tmp_keyval;
3977 if (state != NULL)
3978 *state = tmp_state;
3979
3980 return TRUE;
3981}
3982
3983/*
3984 * Return TRUE if p_imak is valid, otherwise FALSE. As a special case, an
3985 * empty string is also regarded as valid.
3986 *
3987 * Note: The numerical key value of p_imak is cached if it was valid; thus
3988 * boldly assuming im_xim_isvalid_imactivate() will always be called whenever
3989 * 'imak' changes. This is currently the case but not obvious -- should
3990 * probably rename the function for clarity.
3991 */
3992 int
3993im_xim_isvalid_imactivate(void)
3994{
3995 if (p_imak[0] == NUL)
3996 {
3997 im_activatekey_keyval = GDK_VoidSymbol;
3998 im_activatekey_state = 0;
3999 return TRUE;
4000 }
4001
4002 return im_string_to_keyval((const char *)p_imak,
4003 &im_activatekey_keyval,
4004 &im_activatekey_state);
4005}
4006
4007 static void
4008im_synthesize_keypress(unsigned int keyval, unsigned int state)
4009{
4010 GdkEventKey *event;
4011
4012# ifdef HAVE_GTK_MULTIHEAD
4013 event = (GdkEventKey *)gdk_event_new(GDK_KEY_PRESS);
4014 g_object_ref(gui.drawarea->window); /* unreffed by gdk_event_free() */
4015# else
4016 event = (GdkEventKey *)g_malloc0((gulong)sizeof(GdkEvent));
4017 event->type = GDK_KEY_PRESS;
4018# endif
4019 event->window = gui.drawarea->window;
4020 event->send_event = TRUE;
4021 event->time = GDK_CURRENT_TIME;
4022 event->state = state;
4023 event->keyval = keyval;
4024 event->hardware_keycode = /* needed for XIM */
4025 XKeysymToKeycode(GDK_WINDOW_XDISPLAY(event->window), (KeySym)keyval);
4026 event->length = 0;
4027 event->string = NULL;
4028
4029 gtk_im_context_filter_keypress(xic, event);
4030
4031 /* For consistency, also send the corresponding release event. */
4032 event->type = GDK_KEY_RELEASE;
4033 event->send_event = FALSE;
4034 gtk_im_context_filter_keypress(xic, event);
4035
4036# ifdef HAVE_GTK_MULTIHEAD
4037 gdk_event_free((GdkEvent *)event);
4038# else
4039 g_free(event);
4040# endif
4041}
4042
4043 void
4044xim_reset(void)
4045{
4046 if (xic != NULL)
4047 {
4048 /*
4049 * The third-party imhangul module (and maybe others too) ignores
4050 * gtk_im_context_reset() or at least doesn't reset the active state.
4051 * Thus sending imactivatekey would turn it off if it was on before,
4052 * which is clearly not what we want. Fortunately we can work around
4053 * that for imhangul by sending GDK_Escape, but I don't know if it
4054 * works with all IM modules that support an activation key :/
4055 *
4056 * An alternative approach would be to destroy the IM context and
4057 * recreate it. But that means loading/unloading the IM module on
4058 * every mode switch, which causes a quite noticable delay even on
4059 * my rather fast box...
4060 * *
4061 * Moreover, there are some XIM which cannot respond to
4062 * im_synthesize_keypress(). we hope that they reset by
4063 * xim_shutdown().
4064 */
4065 if (im_activatekey_keyval != GDK_VoidSymbol && im_is_active)
4066 im_synthesize_keypress(GDK_Escape, 0U);
4067
4068 gtk_im_context_reset(xic);
4069
4070 /*
4071 * HACK for Ami: This sequence of function calls makes Ami handle
4072 * the IM reset gratiously, without breaking loads of other stuff.
4073 * It seems to force English mode as well, which is exactly what we
4074 * want because it makes the Ami status display work reliably.
4075 */
4076 gtk_im_context_set_use_preedit(xic, FALSE);
4077
4078 if (p_imdisable)
4079 im_shutdown();
4080 else
4081 {
4082 gtk_im_context_set_use_preedit(xic, TRUE);
4083 xim_set_focus(gui.in_focus);
4084
4085 if (im_activatekey_keyval != GDK_VoidSymbol)
4086 {
4087 if (im_is_active)
4088 {
4089 g_signal_handler_block(xic, im_commit_handler_id);
4090 im_synthesize_keypress(im_activatekey_keyval,
4091 im_activatekey_state);
4092 g_signal_handler_unblock(xic, im_commit_handler_id);
4093 }
4094 }
4095 else
4096 {
4097 im_shutdown();
4098 xim_init();
4099 xim_set_focus(gui.in_focus);
4100 }
4101 }
4102 }
4103
4104 preedit_start_col = MAXCOL;
4105 xim_has_preediting = FALSE;
4106}
4107
4108 int
4109xim_queue_key_press_event(GdkEventKey *event, int down)
4110{
4111 if (down)
4112 {
4113 /*
4114 * Workaround GTK2 XIM 'feature' that always converts keypad keys to
4115 * chars., even when not part of an IM sequence (ref. feature of
4116 * gdk/gdkkeyuni.c).
4117 * Flag any keypad keys that might represent a single char.
4118 * If this (on its own - i.e., not part of an IM sequence) is
4119 * committed while we're processing one of these keys, we can ignore
4120 * that commit and go ahead & process it ourselves. That way we can
4121 * still distinguish keypad keys for use in mappings.
4122 */
4123 switch (event->keyval)
4124 {
4125 case GDK_KP_Add: xim_expected_char = '+'; break;
4126 case GDK_KP_Subtract: xim_expected_char = '-'; break;
4127 case GDK_KP_Divide: xim_expected_char = '/'; break;
4128 case GDK_KP_Multiply: xim_expected_char = '*'; break;
4129 case GDK_KP_Decimal: xim_expected_char = '.'; break;
4130 case GDK_KP_Equal: xim_expected_char = '='; break;
4131 case GDK_KP_0: xim_expected_char = '0'; break;
4132 case GDK_KP_1: xim_expected_char = '1'; break;
4133 case GDK_KP_2: xim_expected_char = '2'; break;
4134 case GDK_KP_3: xim_expected_char = '3'; break;
4135 case GDK_KP_4: xim_expected_char = '4'; break;
4136 case GDK_KP_5: xim_expected_char = '5'; break;
4137 case GDK_KP_6: xim_expected_char = '6'; break;
4138 case GDK_KP_7: xim_expected_char = '7'; break;
4139 case GDK_KP_8: xim_expected_char = '8'; break;
4140 case GDK_KP_9: xim_expected_char = '9'; break;
4141 default: xim_expected_char = NUL;
4142 }
4143 xim_ignored_char = FALSE;
4144 }
4145
4146 /*
4147 * When typing fFtT, XIM may be activated. Thus it must pass
4148 * gtk_im_context_filter_keypress() in Normal mode.
4149 * And while doing :sh too.
4150 */
4151 if (xic != NULL && !p_imdisable
4152 && (State & (INSERT | CMDLINE | NORMAL | EXTERNCMD)) != 0)
4153 {
4154 /*
4155 * Filter 'imactivatekey' and map it to CTRL-^. This way, Vim is
4156 * always aware of the current status of IM, and can even emulate
4157 * the activation key for modules that don't support one.
4158 */
4159 if (event->keyval == im_activatekey_keyval
4160 && (event->state & im_activatekey_state) == im_activatekey_state)
4161 {
4162 unsigned int state_mask;
4163
4164 /* Require the state of the 3 most used modifiers to match exactly.
4165 * Otherwise e.g. <S-C-space> would be unusable for other purposes
4166 * if the IM activate key is <S-space>. */
4167 state_mask = im_activatekey_state;
4168 state_mask |= ((int)GDK_SHIFT_MASK | (int)GDK_CONTROL_MASK
4169 | (int)GDK_MOD1_MASK);
4170
4171 if ((event->state & state_mask) != im_activatekey_state)
4172 return FALSE;
4173
4174 /* Don't send it a second time on GDK_KEY_RELEASE. */
4175 if (event->type != GDK_KEY_PRESS)
4176 return TRUE;
4177
Bram Moolenaar658b74a2006-03-18 21:33:02 +00004178 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 im_set_active(FALSE);
4181
4182 /* ":lmap" mappings exists, toggle use of mappings. */
4183 State ^= LANGMAP;
4184 if (State & LANGMAP)
4185 {
4186 curbuf->b_p_iminsert = B_IMODE_NONE;
4187 State &= ~LANGMAP;
4188 }
4189 else
4190 {
4191 curbuf->b_p_iminsert = B_IMODE_LMAP;
4192 State |= LANGMAP;
4193 }
4194 return TRUE;
4195 }
4196
4197 return gtk_im_context_filter_keypress(xic, event);
4198 }
4199
4200 /* Don't filter events through the IM context if IM isn't active
4201 * right now. Unlike with GTK+ 1.2 we cannot rely on the IM module
4202 * not doing anything before the activation key was sent. */
4203 if (im_activatekey_keyval == GDK_VoidSymbol || im_is_active)
4204 {
4205 int imresult = gtk_im_context_filter_keypress(xic, event);
4206
4207 /* Some XIM send following sequence:
4208 * 1. preedited string.
4209 * 2. committed string.
4210 * 3. line changed key.
4211 * 4. preedited string.
4212 * 5. remove preedited string.
4213 * if 3, Vim can't move back the above line for 5.
4214 * thus, this part should not parse the key. */
4215 if (!imresult && preedit_start_col != MAXCOL
4216 && event->keyval == GDK_Return)
4217 {
4218 im_synthesize_keypress(GDK_Return, 0U);
4219 return FALSE;
4220 }
4221
4222 /* If XIM tried to commit a keypad key as a single char.,
4223 * ignore it so we can use the keypad key 'raw', for mappings. */
4224 if (xim_expected_char != NUL && xim_ignored_char)
4225 /* We had a keypad key, and XIM tried to thieve it */
4226 return FALSE;
4227
4228 /* Normal processing */
4229 return imresult;
4230 }
4231 }
4232
4233 return FALSE;
4234}
4235
4236 int
4237im_get_status(void)
4238{
4239 return im_is_active;
4240}
4241
4242# else /* !HAVE_GTK2 */
4243
4244static int xim_is_active = FALSE; /* XIM should be active in the current
4245 mode */
4246static int xim_has_focus = FALSE; /* XIM is really being used for Vim */
4247#ifdef FEAT_GUI_X11
4248static XIMStyle input_style;
4249static int status_area_enabled = TRUE;
4250#endif
4251
4252#ifdef FEAT_GUI_GTK
4253# ifdef WIN3264
4254# include <gdk/gdkwin32.h>
4255# else
4256# include <gdk/gdkx.h>
4257# endif
4258#else
4259# ifdef PROTO
4260/* Define a few things to be able to generate prototypes while not configured
4261 * for GTK. */
4262# define GSList int
4263# define gboolean int
4264 typedef int GdkEvent;
4265 typedef int GdkEventKey;
4266# define GdkIC int
4267# endif
4268#endif
4269
Bram Moolenaar9372a112005-12-06 19:59:18 +00004270#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271static int preedit_buf_len = 0;
4272static int xim_can_preediting INIT(= FALSE); /* XIM in showmode() */
4273static int xim_input_style;
4274#ifndef FEAT_GUI_GTK
4275# define gboolean int
4276#endif
4277static gboolean use_status_area = 0;
4278
4279static int im_xim_str2keycode __ARGS((unsigned int *code, unsigned int *state));
4280static void im_xim_send_event_imactivate __ARGS((void));
4281
4282/*
4283 * Convert string to keycode and state for XKeyEvent.
4284 * When string is valid return OK, when invalid return FAIL.
4285 *
4286 * See 'imactivatekey' documentation for the format.
4287 */
4288 static int
4289im_xim_str2keycode(code, state)
4290 unsigned int *code;
4291 unsigned int *state;
4292{
4293 int retval = OK;
4294 int len;
4295 unsigned keycode = 0, keystate = 0;
4296 Window window;
4297 Display *display;
4298 char_u *flag_end;
4299 char_u *str;
4300
4301 if (*p_imak != NUL)
4302 {
4303 len = STRLEN(p_imak);
4304 for (flag_end = p_imak + len - 1;
4305 flag_end > p_imak && *flag_end != '-'; --flag_end)
4306 ;
4307
4308 /* Parse modifier keys */
4309 for (str = p_imak; str < flag_end; ++str)
4310 {
4311 switch (*str)
4312 {
4313 case 's': case 'S':
4314 keystate |= ShiftMask;
4315 break;
4316 case 'l': case 'L':
4317 keystate |= LockMask;
4318 break;
4319 case 'c': case 'C':
4320 keystate |= ControlMask;
4321 break;
4322 case '1':
4323 keystate |= Mod1Mask;
4324 break;
4325 case '2':
4326 keystate |= Mod2Mask;
4327 break;
4328 case '3':
4329 keystate |= Mod3Mask;
4330 break;
4331 case '4':
4332 keystate |= Mod4Mask;
4333 break;
4334 case '5':
4335 keystate |= Mod5Mask;
4336 break;
4337 case '-':
4338 break;
4339 default:
4340 retval = FAIL;
4341 }
4342 }
4343 if (*str == '-')
4344 ++str;
4345
4346 /* Get keycode from string. */
4347 gui_get_x11_windis(&window, &display);
4348 if (display)
4349 keycode = XKeysymToKeycode(display, XStringToKeysym((char *)str));
4350 if (keycode == 0)
4351 retval = FAIL;
4352
4353 if (code != NULL)
4354 *code = keycode;
4355 if (state != NULL)
4356 *state = keystate;
4357 }
4358 return retval;
4359}
4360
4361 static void
4362im_xim_send_event_imactivate()
4363{
4364 /* Force turn on preedit state by symulate keypress event.
4365 * Keycode and state is specified by 'imactivatekey'.
4366 */
4367 XKeyEvent ev;
4368
4369 gui_get_x11_windis(&ev.window, &ev.display);
4370 ev.root = RootWindow(ev.display, DefaultScreen(ev.display));
4371 ev.subwindow = None;
4372 ev.time = CurrentTime;
4373 ev.x = 1;
4374 ev.y = 1;
4375 ev.x_root = 1;
4376 ev.y_root = 1;
4377 ev.same_screen = 1;
4378 ev.type = KeyPress;
4379 if (im_xim_str2keycode(&ev.keycode, &ev.state) == OK)
4380 XSendEvent(ev.display, ev.window, 1, KeyPressMask, (XEvent*)&ev);
4381}
4382
4383/*
4384 * Return TRUE if 'imactivatekey' has a valid value.
4385 */
4386 int
4387im_xim_isvalid_imactivate()
4388{
4389 return im_xim_str2keycode(NULL, NULL) == OK;
4390}
4391#endif /* FEAT_GUI_GTK */
4392
4393/*
4394 * Switch using XIM on/off. This is used by the code that changes "State".
4395 */
4396 void
4397im_set_active(active)
4398 int active;
4399{
4400 if (xic == NULL)
4401 return;
4402
4403 /* If 'imdisable' is set, XIM is never active. */
4404 if (p_imdisable)
4405 active = FALSE;
Bram Moolenaar9372a112005-12-06 19:59:18 +00004406#if !defined (FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else if (input_style & XIMPreeditPosition)
4408 /* There is a problem in switching XIM off when preediting is used,
4409 * and it is not clear how this can be solved. For now, keep XIM on
4410 * all the time, like it was done in Vim 5.8. */
4411 active = TRUE;
4412#endif
4413
4414 /* Remember the active state, it is needed when Vim gets keyboard focus. */
4415 xim_is_active = active;
4416
4417#ifdef FEAT_GUI_GTK
4418 /* When 'imactivatekey' has valid key-string, try to control XIM preedit
4419 * state. When 'imactivatekey' has no or invalid string, try old XIM
4420 * focus control.
4421 */
4422 if (*p_imak != NUL)
4423 {
4424 /* BASIC STRATEGY:
4425 * Destroy old Input Context (XIC), and create new one. New XIC
4426 * would have a state of preedit that is off. When argument:active
4427 * is false, that's all. Else argument:active is true, send a key
4428 * event specified by 'imactivatekey' to activate XIM preedit state.
4429 */
4430
4431 xim_is_active = TRUE; /* Disable old XIM focus control */
4432 /* If we can monitor preedit state with preedit callback functions,
4433 * try least creation of new XIC.
4434 */
4435 if (xim_input_style & (int)GDK_IM_PREEDIT_CALLBACKS)
4436 {
4437 if (xim_can_preediting && !active)
4438 {
4439 /* Force turn off preedit state. With some IM
4440 * implementations, we cannot turn off preedit state by
4441 * symulate keypress event. It is why using such a method
4442 * that destroy old IC (input context), and create new one.
4443 * When create new IC, its preedit state is usually off.
4444 */
4445 xim_reset();
4446 xim_set_focus(FALSE);
4447 gdk_ic_destroy(xic);
4448 xim_init();
4449 xim_can_preediting = FALSE;
4450 }
4451 else if (!xim_can_preediting && active)
4452 im_xim_send_event_imactivate();
4453 }
4454 else
4455 {
4456 /* First, force destroy old IC, and create new one. It
4457 * symulates "turning off preedit state".
4458 */
4459 xim_set_focus(FALSE);
4460 gdk_ic_destroy(xic);
4461 xim_init();
4462 xim_can_preediting = FALSE;
4463
4464 /* 2nd, when requested to activate IM, symulate this by sending
4465 * the event.
4466 */
4467 if (active)
4468 {
4469 im_xim_send_event_imactivate();
4470 xim_can_preediting = TRUE;
4471 }
4472 }
4473 }
4474 else
4475 {
4476# ifndef XIMPreeditUnKnown
4477 /* X11R5 doesn't have these, it looks safe enough to define here. */
4478 typedef unsigned long XIMPreeditState;
4479# define XIMPreeditUnKnown 0L
4480# define XIMPreeditEnable 1L
4481# define XIMPreeditDisable (1L<<1)
4482# define XNPreeditState "preeditState"
4483# endif
4484 XIMPreeditState preedit_state = XIMPreeditUnKnown;
4485 XVaNestedList preedit_attr;
4486 XIC pxic;
4487
4488 preedit_attr = XVaCreateNestedList(0,
4489 XNPreeditState, &preedit_state,
4490 NULL);
4491 pxic = ((GdkICPrivate *)xic)->xic;
4492
4493 if (!XGetICValues(pxic, XNPreeditAttributes, preedit_attr, NULL))
4494 {
4495 XFree(preedit_attr);
4496 preedit_attr = XVaCreateNestedList(0,
4497 XNPreeditState,
4498 active ? XIMPreeditEnable : XIMPreeditDisable,
4499 NULL);
4500 XSetICValues(pxic, XNPreeditAttributes, preedit_attr, NULL);
4501 xim_can_preediting = active;
4502 xim_is_active = active;
4503 }
4504 XFree(preedit_attr);
4505 }
4506 if (xim_input_style & XIMPreeditCallbacks)
4507 {
4508 preedit_buf_len = 0;
4509 init_preedit_start_col();
4510 }
4511#else
4512# if 0
4513 /* When had tested kinput2 + canna + Athena GUI version with
4514 * 'imactivatekey' is "s-space", im_xim_send_event_imactivate() did not
4515 * work correctly. It just inserted one space. I don't know why we
4516 * couldn't switch state of XIM preediting. This is reason why these
4517 * codes are commented out.
4518 */
4519 /* First, force destroy old IC, and create new one. It symulates
4520 * "turning off preedit state".
4521 */
4522 xim_set_focus(FALSE);
4523 XDestroyIC(xic);
4524 xic = NULL;
4525 xim_init();
4526
4527 /* 2nd, when requested to activate IM, symulate this by sending the
4528 * event.
4529 */
4530 if (active)
4531 im_xim_send_event_imactivate();
4532# endif
4533#endif
4534 xim_set_preedit();
4535}
4536
4537/*
4538 * Adjust using XIM for gaining or losing keyboard focus. Also called when
4539 * "xim_is_active" changes.
4540 */
4541 void
4542xim_set_focus(focus)
4543 int focus;
4544{
4545 if (xic == NULL)
4546 return;
4547
4548 /*
4549 * XIM only gets focus when the Vim window has keyboard focus and XIM has
4550 * been set active for the current mode.
4551 */
4552 if (focus && xim_is_active)
4553 {
4554 if (!xim_has_focus)
4555 {
4556 xim_has_focus = TRUE;
4557#ifdef FEAT_GUI_GTK
4558 gdk_im_begin(xic, gui.drawarea->window);
4559#else
4560 XSetICFocus(xic);
4561#endif
4562 }
4563 }
4564 else
4565 {
4566 if (xim_has_focus)
4567 {
4568 xim_has_focus = FALSE;
4569#ifdef FEAT_GUI_GTK
4570 gdk_im_end();
4571#else
4572 XUnsetICFocus(xic);
4573#endif
4574 }
4575 }
4576}
4577
4578/*ARGSUSED*/
4579 void
4580im_set_position(row, col)
4581 int row;
4582 int col;
4583{
4584 xim_set_preedit();
4585}
4586
4587/*
4588 * Set the XIM to the current cursor position.
4589 */
4590 void
4591xim_set_preedit()
4592{
4593 if (xic == NULL)
4594 return;
4595
4596 xim_set_focus(TRUE);
4597
4598#ifdef FEAT_GUI_GTK
4599 if (gdk_im_ready())
4600 {
4601 int attrmask;
4602 GdkICAttr *attr;
4603
4604 if (!xic_attr)
4605 return;
4606
4607 attr = xic_attr;
4608 attrmask = 0;
4609
4610# ifdef FEAT_XFONTSET
4611 if ((xim_input_style & (int)GDK_IM_PREEDIT_POSITION)
4612 && gui.fontset != NOFONTSET
4613 && gui.fontset->type == GDK_FONT_FONTSET)
4614 {
4615 if (!xim_has_focus)
4616 {
4617 if (attr->spot_location.y >= 0)
4618 {
4619 attr->spot_location.x = 0;
4620 attr->spot_location.y = -100;
4621 attrmask |= (int)GDK_IC_SPOT_LOCATION;
4622 }
4623 }
4624 else
4625 {
4626 gint width, height;
4627
4628 if (attr->spot_location.x != TEXT_X(gui.col)
4629 || attr->spot_location.y != TEXT_Y(gui.row))
4630 {
4631 attr->spot_location.x = TEXT_X(gui.col);
4632 attr->spot_location.y = TEXT_Y(gui.row);
4633 attrmask |= (int)GDK_IC_SPOT_LOCATION;
4634 }
4635
4636 gdk_window_get_size(gui.drawarea->window, &width, &height);
4637 width -= 2 * gui.border_offset;
4638 height -= 2 * gui.border_offset;
4639 if (xim_input_style & (int)GDK_IM_STATUS_AREA)
4640 height -= gui.char_height;
4641 if (attr->preedit_area.width != width
4642 || attr->preedit_area.height != height)
4643 {
4644 attr->preedit_area.x = gui.border_offset;
4645 attr->preedit_area.y = gui.border_offset;
4646 attr->preedit_area.width = width;
4647 attr->preedit_area.height = height;
4648 attrmask |= (int)GDK_IC_PREEDIT_AREA;
4649 }
4650
4651 if (attr->preedit_fontset != gui.current_font)
4652 {
4653 attr->preedit_fontset = gui.current_font;
4654 attrmask |= (int)GDK_IC_PREEDIT_FONTSET;
4655 }
4656 }
4657 }
4658# endif /* FEAT_XFONTSET */
4659
4660 if (xim_fg_color == INVALCOLOR)
4661 {
4662 xim_fg_color = gui.def_norm_pixel;
4663 xim_bg_color = gui.def_back_pixel;
4664 }
4665 if (attr->preedit_foreground.pixel != xim_fg_color)
4666 {
4667 attr->preedit_foreground.pixel = xim_fg_color;
4668 attrmask |= (int)GDK_IC_PREEDIT_FOREGROUND;
4669 }
4670 if (attr->preedit_background.pixel != xim_bg_color)
4671 {
4672 attr->preedit_background.pixel = xim_bg_color;
4673 attrmask |= (int)GDK_IC_PREEDIT_BACKGROUND;
4674 }
4675
4676 if (attrmask != 0)
4677 gdk_ic_set_attr(xic, attr, (GdkICAttributesType)attrmask);
4678 }
4679#else /* FEAT_GUI_GTK */
4680 {
4681 XVaNestedList attr_list;
4682 XRectangle spot_area;
4683 XPoint over_spot;
4684 int line_space;
4685
4686 if (!xim_has_focus)
4687 {
4688 /* hide XIM cursor */
4689 over_spot.x = 0;
4690 over_spot.y = -100; /* arbitrary invisible position */
4691 attr_list = (XVaNestedList) XVaCreateNestedList(0,
4692 XNSpotLocation,
4693 &over_spot,
4694 NULL);
4695 XSetICValues(xic, XNPreeditAttributes, attr_list, NULL);
4696 XFree(attr_list);
4697 return;
4698 }
4699
4700 if (input_style & XIMPreeditPosition)
4701 {
4702 if (xim_fg_color == INVALCOLOR)
4703 {
4704 xim_fg_color = gui.def_norm_pixel;
4705 xim_bg_color = gui.def_back_pixel;
4706 }
4707 over_spot.x = TEXT_X(gui.col);
4708 over_spot.y = TEXT_Y(gui.row);
4709 spot_area.x = 0;
4710 spot_area.y = 0;
4711 spot_area.height = gui.char_height * Rows;
4712 spot_area.width = gui.char_width * Columns;
4713 line_space = gui.char_height;
4714 attr_list = (XVaNestedList) XVaCreateNestedList(0,
4715 XNSpotLocation, &over_spot,
4716 XNForeground, (Pixel) xim_fg_color,
4717 XNBackground, (Pixel) xim_bg_color,
4718 XNArea, &spot_area,
4719 XNLineSpace, line_space,
4720 NULL);
4721 if (XSetICValues(xic, XNPreeditAttributes, attr_list, NULL))
4722 EMSG(_("E284: Cannot set IC values"));
4723 XFree(attr_list);
4724 }
4725 }
4726#endif /* FEAT_GUI_GTK */
4727}
4728
4729/*
4730 * Set up the status area.
4731 *
4732 * This should use a separate Widget, but that seems not possible, because
4733 * preedit_area and status_area should be set to the same window as for the
4734 * text input. Unfortunately this means the status area pollutes the text
4735 * window...
4736 */
4737 void
4738xim_set_status_area()
4739{
4740 if (xic == NULL)
4741 return;
4742
4743#ifdef FEAT_GUI_GTK
4744# if defined(FEAT_XFONTSET)
4745 if (use_status_area)
4746 {
4747 GdkICAttr *attr;
4748 int style;
4749 gint width, height;
4750 GtkWidget *widget;
4751 int attrmask;
4752
4753 if (!xic_attr)
4754 return;
4755
4756 attr = xic_attr;
4757 attrmask = 0;
4758 style = (int)gdk_ic_get_style(xic);
4759 if ((style & (int)GDK_IM_STATUS_MASK) == (int)GDK_IM_STATUS_AREA)
4760 {
4761 if (gui.fontset != NOFONTSET
4762 && gui.fontset->type == GDK_FONT_FONTSET)
4763 {
4764 widget = gui.mainwin;
4765 gdk_window_get_size(widget->window, &width, &height);
4766
4767 attrmask |= (int)GDK_IC_STATUS_AREA;
4768 attr->status_area.x = 0;
4769 attr->status_area.y = height - gui.char_height - 1;
4770 attr->status_area.width = width;
4771 attr->status_area.height = gui.char_height;
4772 }
4773 }
4774 if (attrmask != 0)
4775 gdk_ic_set_attr(xic, attr, (GdkICAttributesType)attrmask);
4776 }
4777# endif
4778#else
4779 {
4780 XVaNestedList preedit_list = 0, status_list = 0, list = 0;
4781 XRectangle pre_area, status_area;
4782
4783 if (input_style & XIMStatusArea)
4784 {
4785 if (input_style & XIMPreeditArea)
4786 {
4787 XRectangle *needed_rect;
4788
4789 /* to get status_area width */
4790 status_list = XVaCreateNestedList(0, XNAreaNeeded,
4791 &needed_rect, NULL);
4792 XGetICValues(xic, XNStatusAttributes, status_list, NULL);
4793 XFree(status_list);
4794
4795 status_area.width = needed_rect->width;
4796 }
4797 else
4798 status_area.width = gui.char_width * Columns;
4799
4800 status_area.x = 0;
4801 status_area.y = gui.char_height * Rows + gui.border_offset;
4802 if (gui.which_scrollbars[SBAR_BOTTOM])
4803 status_area.y += gui.scrollbar_height;
4804#ifdef FEAT_MENU
4805 if (gui.menu_is_active)
4806 status_area.y += gui.menu_height;
4807#endif
4808 status_area.height = gui.char_height;
4809 status_list = XVaCreateNestedList(0, XNArea, &status_area, NULL);
4810 }
4811 else
4812 {
4813 status_area.x = 0;
4814 status_area.y = gui.char_height * Rows + gui.border_offset;
4815 if (gui.which_scrollbars[SBAR_BOTTOM])
4816 status_area.y += gui.scrollbar_height;
4817#ifdef FEAT_MENU
4818 if (gui.menu_is_active)
4819 status_area.y += gui.menu_height;
4820#endif
4821 status_area.width = 0;
4822 status_area.height = gui.char_height;
4823 }
4824
4825 if (input_style & XIMPreeditArea) /* off-the-spot */
4826 {
4827 pre_area.x = status_area.x + status_area.width;
4828 pre_area.y = gui.char_height * Rows + gui.border_offset;
4829 pre_area.width = gui.char_width * Columns - pre_area.x;
4830 if (gui.which_scrollbars[SBAR_BOTTOM])
4831 pre_area.y += gui.scrollbar_height;
4832#ifdef FEAT_MENU
4833 if (gui.menu_is_active)
4834 pre_area.y += gui.menu_height;
4835#endif
4836 pre_area.height = gui.char_height;
4837 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
4838 }
4839 else if (input_style & XIMPreeditPosition) /* over-the-spot */
4840 {
4841 pre_area.x = 0;
4842 pre_area.y = 0;
4843 pre_area.height = gui.char_height * Rows;
4844 pre_area.width = gui.char_width * Columns;
4845 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
4846 }
4847
4848 if (preedit_list && status_list)
4849 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
4850 XNStatusAttributes, status_list, NULL);
4851 else if (preedit_list)
4852 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
4853 NULL);
4854 else if (status_list)
4855 list = XVaCreateNestedList(0, XNStatusAttributes, status_list,
4856 NULL);
4857 else
4858 list = NULL;
4859
4860 if (list)
4861 {
4862 XSetICValues(xic, XNVaNestedList, list, NULL);
4863 XFree(list);
4864 }
4865 if (status_list)
4866 XFree(status_list);
4867 if (preedit_list)
4868 XFree(preedit_list);
4869 }
4870#endif
4871}
4872
Bram Moolenaar9372a112005-12-06 19:59:18 +00004873#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874static char e_xim[] = N_("E285: Failed to create input context");
4875#endif
4876
4877#if defined(FEAT_GUI_X11) || defined(PROTO)
4878# if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && !defined(sun)
4879# define USE_X11R6_XIM
4880# endif
4881
4882static int xim_real_init __ARGS((Window x11_window, Display *x11_display));
4883
4884
4885#ifdef USE_X11R6_XIM
4886static void xim_instantiate_cb __ARGS((Display *display, XPointer client_data, XPointer call_data));
4887static void xim_destroy_cb __ARGS((XIM im, XPointer client_data, XPointer call_data));
4888
4889/*ARGSUSED*/
4890 static void
4891xim_instantiate_cb(display, client_data, call_data)
4892 Display *display;
4893 XPointer client_data;
4894 XPointer call_data;
4895{
4896 Window x11_window;
4897 Display *x11_display;
4898
4899#ifdef XIM_DEBUG
4900 xim_log("xim_instantiate_cb()\n");
4901#endif
4902
4903 gui_get_x11_windis(&x11_window, &x11_display);
4904 if (display != x11_display)
4905 return;
4906
4907 xim_real_init(x11_window, x11_display);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00004908 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (xic != NULL)
4910 XUnregisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
4911 xim_instantiate_cb, NULL);
4912}
4913
4914/*ARGSUSED*/
4915 static void
4916xim_destroy_cb(im, client_data, call_data)
4917 XIM im;
4918 XPointer client_data;
4919 XPointer call_data;
4920{
4921 Window x11_window;
4922 Display *x11_display;
4923
4924#ifdef XIM_DEBUG
4925 xim_log("xim_destroy_cb()\n");
4926#endif
4927 gui_get_x11_windis(&x11_window, &x11_display);
4928
4929 xic = NULL;
4930 status_area_enabled = FALSE;
4931
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00004932 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
4935 xim_instantiate_cb, NULL);
4936}
4937#endif
4938
4939 void
4940xim_init()
4941{
4942 Window x11_window;
4943 Display *x11_display;
4944
4945#ifdef XIM_DEBUG
4946 xim_log("xim_init()\n");
4947#endif
4948
4949 gui_get_x11_windis(&x11_window, &x11_display);
4950
4951 xic = NULL;
4952
4953 if (xim_real_init(x11_window, x11_display))
4954 return;
4955
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00004956 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958#ifdef USE_X11R6_XIM
4959 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
4960 xim_instantiate_cb, NULL);
4961#endif
4962}
4963
4964 static int
4965xim_real_init(x11_window, x11_display)
4966 Window x11_window;
4967 Display *x11_display;
4968{
4969 int i;
4970 char *p,
4971 *s,
4972 *ns,
4973 *end,
4974 tmp[1024];
4975#define IMLEN_MAX 40
4976 char buf[IMLEN_MAX + 7];
4977 XIM xim = NULL;
4978 XIMStyles *xim_styles;
4979 XIMStyle this_input_style = 0;
4980 Boolean found;
4981 XPoint over_spot;
4982 XVaNestedList preedit_list, status_list;
4983
4984 input_style = 0;
4985 status_area_enabled = FALSE;
4986
4987 if (xic != NULL)
4988 return FALSE;
4989
4990 if (gui.rsrc_input_method != NULL && *gui.rsrc_input_method != NUL)
4991 {
4992 strcpy(tmp, gui.rsrc_input_method);
4993 for (ns = s = tmp; ns != NULL && *s != NUL;)
4994 {
4995 s = (char *)skipwhite((char_u *)s);
4996 if (*s == NUL)
4997 break;
4998 if ((ns = end = strchr(s, ',')) == NULL)
4999 end = s + strlen(s);
5000 while (isspace(((char_u *)end)[-1]))
5001 end--;
5002 *end = NUL;
5003
5004 if (strlen(s) <= IMLEN_MAX)
5005 {
5006 strcpy(buf, "@im=");
5007 strcat(buf, s);
5008 if ((p = XSetLocaleModifiers(buf)) != NULL && *p != NUL
5009 && (xim = XOpenIM(x11_display, NULL, NULL, NULL))
5010 != NULL)
5011 break;
5012 }
5013
5014 s = ns + 1;
5015 }
5016 }
5017
5018 if (xim == NULL && (p = XSetLocaleModifiers("")) != NULL && *p != NUL)
5019 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5020
5021 /* This is supposed to be useful to obtain characters through
5022 * XmbLookupString() without really using a XIM. */
5023 if (xim == NULL && (p = XSetLocaleModifiers("@im=none")) != NULL
5024 && *p != NUL)
5025 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5026
5027 if (xim == NULL)
5028 {
5029 /* Only give this message when verbose is set, because too many people
5030 * got this message when they didn't want to use a XIM. */
5031 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005032 {
5033 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 EMSG(_("E286: Failed to open input method"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005035 verbose_leave();
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 return FALSE;
5038 }
5039
5040#ifdef USE_X11R6_XIM
5041 {
5042 XIMCallback destroy_cb;
5043
5044 destroy_cb.callback = xim_destroy_cb;
5045 destroy_cb.client_data = NULL;
5046 if (XSetIMValues(xim, XNDestroyCallback, &destroy_cb, NULL))
5047 EMSG(_("E287: Warning: Could not set destroy callback to IM"));
5048 }
5049#endif
5050
5051 if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL) || !xim_styles)
5052 {
5053 EMSG(_("E288: input method doesn't support any style"));
5054 XCloseIM(xim);
5055 return FALSE;
5056 }
5057
5058 found = False;
5059 strcpy(tmp, gui.rsrc_preedit_type_name);
5060 for (s = tmp; s && !found; )
5061 {
5062 while (*s && isspace((unsigned char)*s))
5063 s++;
5064 if (!*s)
5065 break;
5066 if ((ns = end = strchr(s, ',')) != 0)
5067 ns++;
5068 else
5069 end = s + strlen(s);
5070 while (isspace((unsigned char)*end))
5071 end--;
5072 *end = '\0';
5073
5074 if (!strcmp(s, "OverTheSpot"))
5075 this_input_style = (XIMPreeditPosition | XIMStatusArea);
5076 else if (!strcmp(s, "OffTheSpot"))
5077 this_input_style = (XIMPreeditArea | XIMStatusArea);
5078 else if (!strcmp(s, "Root"))
5079 this_input_style = (XIMPreeditNothing | XIMStatusNothing);
5080
5081 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5082 {
5083 if (this_input_style == xim_styles->supported_styles[i])
5084 {
5085 found = True;
5086 break;
5087 }
5088 }
5089 if (!found)
5090 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5091 {
5092 if ((xim_styles->supported_styles[i] & this_input_style)
5093 == (this_input_style & ~XIMStatusArea))
5094 {
5095 this_input_style &= ~XIMStatusArea;
5096 found = True;
5097 break;
5098 }
5099 }
5100
5101 s = ns;
5102 }
5103 XFree(xim_styles);
5104
5105 if (!found)
5106 {
5107 /* Only give this message when verbose is set, because too many people
5108 * got this message when they didn't want to use a XIM. */
5109 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005110 {
5111 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 EMSG(_("E289: input method doesn't support my preedit type"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005113 verbose_leave();
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 XCloseIM(xim);
5116 return FALSE;
5117 }
5118
5119 over_spot.x = TEXT_X(gui.col);
5120 over_spot.y = TEXT_Y(gui.row);
5121 input_style = this_input_style;
5122
5123 /* A crash was reported when trying to pass gui.norm_font as XNFontSet,
5124 * thus that has been removed. Hopefully the default works... */
5125#ifdef FEAT_XFONTSET
5126 if (gui.fontset != NOFONTSET)
5127 {
5128 preedit_list = XVaCreateNestedList(0,
5129 XNSpotLocation, &over_spot,
5130 XNForeground, (Pixel)gui.def_norm_pixel,
5131 XNBackground, (Pixel)gui.def_back_pixel,
5132 XNFontSet, (XFontSet)gui.fontset,
5133 NULL);
5134 status_list = XVaCreateNestedList(0,
5135 XNForeground, (Pixel)gui.def_norm_pixel,
5136 XNBackground, (Pixel)gui.def_back_pixel,
5137 XNFontSet, (XFontSet)gui.fontset,
5138 NULL);
5139 }
5140 else
5141#endif
5142 {
5143 preedit_list = XVaCreateNestedList(0,
5144 XNSpotLocation, &over_spot,
5145 XNForeground, (Pixel)gui.def_norm_pixel,
5146 XNBackground, (Pixel)gui.def_back_pixel,
5147 NULL);
5148 status_list = XVaCreateNestedList(0,
5149 XNForeground, (Pixel)gui.def_norm_pixel,
5150 XNBackground, (Pixel)gui.def_back_pixel,
5151 NULL);
5152 }
5153
5154 xic = XCreateIC(xim,
5155 XNInputStyle, input_style,
5156 XNClientWindow, x11_window,
5157 XNFocusWindow, gui.wid,
5158 XNPreeditAttributes, preedit_list,
5159 XNStatusAttributes, status_list,
5160 NULL);
5161 XFree(status_list);
5162 XFree(preedit_list);
5163 if (xic != NULL)
5164 {
5165 if (input_style & XIMStatusArea)
5166 {
5167 xim_set_status_area();
5168 status_area_enabled = TRUE;
5169 }
5170 else
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005171 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173 else
5174 {
5175 EMSG(_(e_xim));
5176 XCloseIM(xim);
5177 return FALSE;
5178 }
5179
5180 return TRUE;
5181}
5182
5183#endif /* FEAT_GUI_X11 */
5184
5185#if defined(FEAT_GUI_GTK) || defined(PROTO)
5186
5187# ifdef FEAT_XFONTSET
5188static char e_overthespot[] = N_("E290: over-the-spot style requires fontset");
5189# endif
5190
5191# ifdef PROTO
5192typedef int GdkIC;
5193# endif
5194
5195 void
5196xim_decide_input_style()
5197{
5198 /* GDK_IM_STATUS_CALLBACKS was disabled, enabled it to allow Japanese
5199 * OverTheSpot. */
5200 int supported_style = (int)GDK_IM_PREEDIT_NONE |
5201 (int)GDK_IM_PREEDIT_NOTHING |
5202 (int)GDK_IM_PREEDIT_POSITION |
5203 (int)GDK_IM_PREEDIT_CALLBACKS |
5204 (int)GDK_IM_STATUS_CALLBACKS |
5205 (int)GDK_IM_STATUS_AREA |
5206 (int)GDK_IM_STATUS_NONE |
5207 (int)GDK_IM_STATUS_NOTHING;
5208
5209#ifdef XIM_DEBUG
5210 xim_log("xim_decide_input_style()\n");
5211#endif
5212
5213 if (!gdk_im_ready())
5214 xim_input_style = 0;
5215 else
5216 {
5217 if (gtk_major_version > 1
5218 || (gtk_major_version == 1
5219 && (gtk_minor_version > 2
5220 || (gtk_minor_version == 2 && gtk_micro_version >= 3))))
5221 use_status_area = TRUE;
5222 else
5223 {
5224 EMSG(_("E291: Your GTK+ is older than 1.2.3. Status area disabled"));
5225 use_status_area = FALSE;
5226 }
5227#ifdef FEAT_XFONTSET
5228 if (gui.fontset == NOFONTSET || gui.fontset->type != GDK_FONT_FONTSET)
5229#endif
5230 supported_style &= ~((int)GDK_IM_PREEDIT_POSITION
5231 | (int)GDK_IM_STATUS_AREA);
5232 if (!use_status_area)
5233 supported_style &= ~(int)GDK_IM_STATUS_AREA;
5234 xim_input_style = (int)gdk_im_decide_style((GdkIMStyle)supported_style);
5235 }
5236}
5237
5238/*ARGSUSED*/
5239 static void
5240preedit_start_cbproc(XIC xic, XPointer client_data, XPointer call_data)
5241{
5242#ifdef XIM_DEBUG
5243 xim_log("xim_decide_input_style()\n");
5244#endif
5245
5246 draw_feedback = NULL;
5247 xim_can_preediting = TRUE;
5248 xim_has_preediting = TRUE;
5249 gui_update_cursor(TRUE, FALSE);
5250 if (showmode() > 0)
5251 {
5252 setcursor();
5253 out_flush();
5254 }
5255}
5256
5257 static void
5258xim_back_delete(int n)
5259{
5260 char_u str[3];
5261
5262 str[0] = CSI;
5263 str[1] = 'k';
5264 str[2] = 'b';
5265 while (n-- > 0)
5266 add_to_input_buf(str, 3);
5267}
5268
5269static GSList *key_press_event_queue = NULL;
5270static gboolean processing_queued_event = FALSE;
5271
5272/*ARGSUSED*/
5273 static void
5274preedit_draw_cbproc(XIC xic, XPointer client_data, XPointer call_data)
5275{
5276 XIMPreeditDrawCallbackStruct *draw_data;
5277 XIMText *text;
5278 char *src;
5279 GSList *event_queue;
5280
5281#ifdef XIM_DEBUG
5282 xim_log("preedit_draw_cbproc()\n");
5283#endif
5284
5285 draw_data = (XIMPreeditDrawCallbackStruct *) call_data;
5286 text = (XIMText *) draw_data->text;
5287
5288 if ((text == NULL && draw_data->chg_length == preedit_buf_len)
5289 || preedit_buf_len == 0)
5290 {
5291 init_preedit_start_col();
5292 vim_free(draw_feedback);
5293 draw_feedback = NULL;
5294 }
5295 if (draw_data->chg_length > 0)
5296 {
5297 int bs_cnt;
5298
5299 if (draw_data->chg_length > preedit_buf_len)
5300 bs_cnt = preedit_buf_len;
5301 else
5302 bs_cnt = draw_data->chg_length;
5303 xim_back_delete(bs_cnt);
5304 preedit_buf_len -= bs_cnt;
5305 }
5306 if (text != NULL)
5307 {
5308 int len;
5309#ifdef FEAT_MBYTE
5310 char_u *buf = NULL;
5311 unsigned int nfeedback = 0;
5312#endif
5313 char_u *ptr;
5314
5315 src = text->string.multi_byte;
5316 if (src != NULL && !text->encoding_is_wchar)
5317 {
5318 len = strlen(src);
5319 ptr = (char_u *)src;
5320 /* Avoid the enter for decision */
5321 if (*ptr == '\n')
5322 return;
5323
5324#ifdef FEAT_MBYTE
5325 if (input_conv.vc_type != CONV_NONE
5326 && (buf = string_convert(&input_conv,
5327 (char_u *)src, &len)) != NULL)
5328 {
5329 /* Converted from 'termencoding' to 'encoding'. */
5330 add_to_input_buf_csi(buf, len);
5331 ptr = buf;
5332 }
5333 else
5334#endif
5335 add_to_input_buf_csi((char_u *)src, len);
5336 /* Add count of character to preedit_buf_len */
5337 while (*ptr != NUL)
5338 {
5339#ifdef FEAT_MBYTE
5340 if (draw_data->text->feedback != NULL)
5341 {
5342 if (draw_feedback == NULL)
5343 draw_feedback = (char *)alloc(draw_data->chg_first
5344 + text->length);
5345 else
5346 draw_feedback = realloc(draw_feedback,
5347 draw_data->chg_first + text->length);
5348 if (draw_feedback != NULL)
5349 {
5350 draw_feedback[nfeedback + draw_data->chg_first]
5351 = draw_data->text->feedback[nfeedback];
5352 nfeedback++;
5353 }
5354 }
5355 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005356 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 else
5358#endif
5359 ptr++;
5360 preedit_buf_len++;
5361 }
5362#ifdef FEAT_MBYTE
5363 vim_free(buf);
5364#endif
5365 preedit_end_col = MAXCOL;
5366 }
5367 }
5368 if (text != NULL || draw_data->chg_length > 0)
5369 {
5370 event_queue = key_press_event_queue;
5371 processing_queued_event = TRUE;
5372 while (event_queue != NULL && processing_queued_event)
5373 {
5374 GdkEvent *ev = event_queue->data;
5375
5376 gboolean *ret;
5377 gtk_signal_emit_by_name((GtkObject*)gui.mainwin, "key_press_event",
5378 ev, &ret);
5379 gdk_event_free(ev);
5380 event_queue = event_queue->next;
5381 }
5382 processing_queued_event = FALSE;
5383 if (key_press_event_queue)
5384 {
5385 g_slist_free(key_press_event_queue);
5386 key_press_event_queue = NULL;
5387 }
5388 }
5389 if (gtk_main_level() > 0)
5390 gtk_main_quit();
5391}
5392
5393/*
5394 * Retrieve the highlighting attributes at column col in the preedit string.
5395 * Return -1 if not in preediting mode or if col is out of range.
5396 */
5397 int
5398im_get_feedback_attr(int col)
5399{
5400 if (draw_feedback != NULL && col < preedit_buf_len)
5401 {
5402 if (draw_feedback[col] & XIMReverse)
5403 return HL_INVERSE;
5404 else if (draw_feedback[col] & XIMUnderline)
5405 return HL_UNDERLINE;
5406 else
5407 return hl_attr(HLF_V);
5408 }
5409
5410 return -1;
5411}
5412
5413/*ARGSUSED*/
5414 static void
5415preedit_caret_cbproc(XIC xic, XPointer client_data, XPointer call_data)
5416{
5417#ifdef XIM_DEBUG
5418 xim_log("preedit_caret_cbproc()\n");
5419#endif
5420}
5421
5422/*ARGSUSED*/
5423 static void
5424preedit_done_cbproc(XIC xic, XPointer client_data, XPointer call_data)
5425{
5426#ifdef XIM_DEBUG
5427 xim_log("preedit_done_cbproc()\n");
5428#endif
5429
5430 vim_free(draw_feedback);
5431 draw_feedback = NULL;
5432 xim_can_preediting = FALSE;
5433 xim_has_preediting = FALSE;
5434 gui_update_cursor(TRUE, FALSE);
5435 if (showmode() > 0)
5436 {
5437 setcursor();
5438 out_flush();
5439 }
5440}
5441
5442 void
5443xim_reset(void)
5444{
5445 char *text;
5446
5447#ifdef XIM_DEBUG
5448 xim_log("xim_reset()\n");
5449#endif
5450
5451 if (xic != NULL)
5452 {
5453 text = XmbResetIC(((GdkICPrivate *)xic)->xic);
5454 if (text != NULL && !(xim_input_style & (int)GDK_IM_PREEDIT_CALLBACKS))
5455 add_to_input_buf_csi((char_u *)text, strlen(text));
5456 else
5457 preedit_buf_len = 0;
5458 if (text != NULL)
5459 XFree(text);
5460 }
5461}
5462
5463/*ARGSUSED*/
5464 int
5465xim_queue_key_press_event(GdkEventKey *event, int down)
5466{
5467#ifdef XIM_DEBUG
5468 xim_log("xim_queue_key_press_event()\n");
5469#endif
5470
5471 if (preedit_buf_len <= 0)
5472 return FALSE;
5473 if (processing_queued_event)
5474 processing_queued_event = FALSE;
5475
5476 key_press_event_queue = g_slist_append(key_press_event_queue,
5477 gdk_event_copy((GdkEvent *)event));
5478 return TRUE;
5479}
5480
5481/*ARGSUSED*/
5482 static void
5483preedit_callback_setup(GdkIC *ic)
5484{
5485 XIC xxic;
5486 XVaNestedList preedit_attr;
5487 XIMCallback preedit_start_cb;
5488 XIMCallback preedit_draw_cb;
5489 XIMCallback preedit_caret_cb;
5490 XIMCallback preedit_done_cb;
5491
5492 xxic = ((GdkICPrivate*)xic)->xic;
5493 preedit_start_cb.callback = (XIMProc)preedit_start_cbproc;
5494 preedit_draw_cb.callback = (XIMProc)preedit_draw_cbproc;
5495 preedit_caret_cb.callback = (XIMProc)preedit_caret_cbproc;
5496 preedit_done_cb.callback = (XIMProc)preedit_done_cbproc;
5497 preedit_attr
5498 = XVaCreateNestedList (0,
5499 XNPreeditStartCallback, &preedit_start_cb,
5500 XNPreeditDrawCallback, &preedit_draw_cb,
5501 XNPreeditCaretCallback, &preedit_caret_cb,
5502 XNPreeditDoneCallback, &preedit_done_cb,
5503 0);
5504 XSetICValues (xxic, XNPreeditAttributes, preedit_attr, 0);
5505 XFree(preedit_attr);
5506}
5507
5508/*ARGSUSED*/
5509 static void
5510reset_state_setup(GdkIC *ic)
5511{
5512#ifdef USE_X11R6_XIM
5513 /* don't change the input context when we call reset */
5514 XSetICValues(((GdkICPrivate*)ic)->xic, XNResetState, XIMPreserveState, 0);
5515#endif
5516}
5517
5518 void
5519xim_init(void)
5520{
5521#ifdef XIM_DEBUG
5522 xim_log("xim_init()\n");
5523#endif
5524
5525 xic = NULL;
5526 xic_attr = NULL;
5527
5528 if (!gdk_im_ready())
5529 {
5530 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005531 {
5532 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 EMSG(_("E292: Input Method Server is not running"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005534 verbose_leave();
5535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return;
5537 }
5538 if ((xic_attr = gdk_ic_attr_new()) != NULL)
5539 {
5540#ifdef FEAT_XFONTSET
5541 gint width, height;
5542#endif
5543 int mask;
5544 GdkColormap *colormap;
5545 GdkICAttr *attr = xic_attr;
5546 int attrmask = (int)GDK_IC_ALL_REQ;
5547 GtkWidget *widget = gui.drawarea;
5548
5549 attr->style = (GdkIMStyle)xim_input_style;
5550 attr->client_window = gui.mainwin->window;
5551
5552 if ((colormap = gtk_widget_get_colormap(widget)) !=
5553 gtk_widget_get_default_colormap())
5554 {
5555 attrmask |= (int)GDK_IC_PREEDIT_COLORMAP;
5556 attr->preedit_colormap = colormap;
5557 }
5558 attrmask |= (int)GDK_IC_PREEDIT_FOREGROUND;
5559 attrmask |= (int)GDK_IC_PREEDIT_BACKGROUND;
5560 attr->preedit_foreground = widget->style->fg[GTK_STATE_NORMAL];
5561 attr->preedit_background = widget->style->base[GTK_STATE_NORMAL];
5562
5563#ifdef FEAT_XFONTSET
5564 if ((xim_input_style & (int)GDK_IM_PREEDIT_MASK)
5565 == (int)GDK_IM_PREEDIT_POSITION)
5566 {
5567 if (gui.fontset == NOFONTSET
5568 || gui.fontset->type != GDK_FONT_FONTSET)
5569 {
5570 EMSG(_(e_overthespot));
5571 }
5572 else
5573 {
5574 gdk_window_get_size(widget->window, &width, &height);
5575
5576 attrmask |= (int)GDK_IC_PREEDIT_POSITION_REQ;
5577 attr->spot_location.x = TEXT_X(0);
5578 attr->spot_location.y = TEXT_Y(0);
5579 attr->preedit_area.x = gui.border_offset;
5580 attr->preedit_area.y = gui.border_offset;
5581 attr->preedit_area.width = width - 2*gui.border_offset;
5582 attr->preedit_area.height = height - 2*gui.border_offset;
5583 attr->preedit_fontset = gui.fontset;
5584 }
5585 }
5586
5587 if ((xim_input_style & (int)GDK_IM_STATUS_MASK)
5588 == (int)GDK_IM_STATUS_AREA)
5589 {
5590 if (gui.fontset == NOFONTSET
5591 || gui.fontset->type != GDK_FONT_FONTSET)
5592 {
5593 EMSG(_(e_overthespot));
5594 }
5595 else
5596 {
5597 gdk_window_get_size(gui.mainwin->window, &width, &height);
5598 attrmask |= (int)GDK_IC_STATUS_AREA_REQ;
5599 attr->status_area.x = 0;
5600 attr->status_area.y = height - gui.char_height - 1;
5601 attr->status_area.width = width;
5602 attr->status_area.height = gui.char_height;
5603 attr->status_fontset = gui.fontset;
5604 }
5605 }
5606 else if ((xim_input_style & (int)GDK_IM_STATUS_MASK)
5607 == (int)GDK_IM_STATUS_CALLBACKS)
5608 {
5609 /* FIXME */
5610 }
5611#endif
5612
5613 xic = gdk_ic_new(attr, (GdkICAttributesType)attrmask);
5614
5615 if (xic == NULL)
5616 EMSG(_(e_xim));
5617 else
5618 {
5619 mask = (int)gdk_window_get_events(widget->window);
5620 mask |= (int)gdk_ic_get_events(xic);
5621 gdk_window_set_events(widget->window, (GdkEventMask)mask);
5622 if (xim_input_style & (int)GDK_IM_PREEDIT_CALLBACKS)
5623 preedit_callback_setup(xic);
5624 reset_state_setup(xic);
5625 }
5626 }
5627}
5628
5629 void
5630im_shutdown(void)
5631{
5632#ifdef XIM_DEBUG
5633 xim_log("im_shutdown()\n");
5634#endif
5635
5636 if (xic != NULL)
5637 {
5638 gdk_im_end();
5639 gdk_ic_destroy(xic);
5640 xic = NULL;
5641 }
5642 xim_is_active = FALSE;
5643 xim_can_preediting = FALSE;
5644 preedit_start_col = MAXCOL;
5645 xim_has_preediting = FALSE;
5646}
5647
5648#endif /* FEAT_GUI_GTK */
5649
5650 int
5651xim_get_status_area_height()
5652{
5653#ifdef FEAT_GUI_GTK
5654 if (xim_input_style & (int)GDK_IM_STATUS_AREA)
5655 return gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656#else
5657 if (status_area_enabled)
5658 return gui.char_height;
5659#endif
5660 return 0;
5661}
5662
5663/*
5664 * Get IM status. When IM is on, return TRUE. Else return FALSE.
5665 * FIXME: This doesn't work correctly: Having focus doesn't always mean XIM is
5666 * active, when not having focus XIM may still be active (e.g., when using a
5667 * tear-off menu item).
5668 */
5669 int
5670im_get_status()
5671{
5672# ifdef FEAT_GUI_GTK
5673 if (xim_input_style & (int)GDK_IM_PREEDIT_CALLBACKS)
5674 return xim_can_preediting;
5675# endif
5676 return xim_has_focus;
5677}
5678
5679# endif /* !HAVE_GTK2 */
5680
5681# if defined(FEAT_GUI_GTK) || defined(PROTO)
5682 int
5683im_is_preediting()
5684{
5685 return xim_has_preediting;
5686}
5687# endif
5688#endif /* FEAT_XIM */
5689
5690#if defined(FEAT_MBYTE) || defined(PROTO)
5691
5692/*
5693 * Setup "vcp" for conversion from "from" to "to".
5694 * The names must have been made canonical with enc_canonize().
5695 * vcp->vc_type must have been initialized to CONV_NONE.
5696 * Note: cannot be used for conversion from/to ucs-2 and ucs-4 (will use utf-8
5697 * instead).
5698 * Afterwards invoke with "from" and "to" equal to NULL to cleanup.
5699 * Return FAIL when conversion is not supported, OK otherwise.
5700 */
5701 int
5702convert_setup(vcp, from, to)
5703 vimconv_T *vcp;
5704 char_u *from;
5705 char_u *to;
5706{
5707 int from_prop;
5708 int to_prop;
5709
5710 /* Reset to no conversion. */
5711# ifdef USE_ICONV
5712 if (vcp->vc_type == CONV_ICONV && vcp->vc_fd != (iconv_t)-1)
5713 iconv_close(vcp->vc_fd);
5714# endif
5715 vcp->vc_type = CONV_NONE;
5716 vcp->vc_factor = 1;
5717 vcp->vc_fail = FALSE;
5718
5719 /* No conversion when one of the names is empty or they are equal. */
5720 if (from == NULL || *from == NUL || to == NULL || *to == NUL
5721 || STRCMP(from, to) == 0)
5722 return OK;
5723
5724 from_prop = enc_canon_props(from);
5725 to_prop = enc_canon_props(to);
5726 if ((from_prop & ENC_LATIN1) && (to_prop & ENC_UNICODE))
5727 {
5728 /* Internal latin1 -> utf-8 conversion. */
5729 vcp->vc_type = CONV_TO_UTF8;
5730 vcp->vc_factor = 2; /* up to twice as long */
5731 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005732 else if ((from_prop & ENC_LATIN9) && (to_prop & ENC_UNICODE))
5733 {
5734 /* Internal latin9 -> utf-8 conversion. */
5735 vcp->vc_type = CONV_9_TO_UTF8;
5736 vcp->vc_factor = 3; /* up to three as long (euro sign) */
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 else if ((from_prop & ENC_UNICODE) && (to_prop & ENC_LATIN1))
5739 {
5740 /* Internal utf-8 -> latin1 conversion. */
5741 vcp->vc_type = CONV_TO_LATIN1;
5742 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005743 else if ((from_prop & ENC_UNICODE) && (to_prop & ENC_LATIN9))
5744 {
5745 /* Internal utf-8 -> latin9 conversion. */
5746 vcp->vc_type = CONV_TO_LATIN9;
5747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748#ifdef WIN3264
5749 /* Win32-specific codepage <-> codepage conversion without iconv. */
5750 else if (((from_prop & ENC_UNICODE) || encname2codepage(from) > 0)
5751 && ((to_prop & ENC_UNICODE) || encname2codepage(to) > 0))
5752 {
5753 vcp->vc_type = CONV_CODEPAGE;
5754 vcp->vc_factor = 2; /* up to twice as long */
5755 vcp->vc_cpfrom = (from_prop & ENC_UNICODE) ? 0 : encname2codepage(from);
5756 vcp->vc_cpto = (to_prop & ENC_UNICODE) ? 0 : encname2codepage(to);
5757 }
5758#endif
5759#ifdef MACOS_X
5760 else if ((from_prop & ENC_MACROMAN) && (to_prop & ENC_LATIN1))
5761 {
5762 vcp->vc_type = CONV_MAC_LATIN1;
5763 }
5764 else if ((from_prop & ENC_MACROMAN) && (to_prop & ENC_UNICODE))
5765 {
5766 vcp->vc_type = CONV_MAC_UTF8;
5767 vcp->vc_factor = 2; /* up to twice as long */
5768 }
5769 else if ((from_prop & ENC_LATIN1) && (to_prop & ENC_MACROMAN))
5770 {
5771 vcp->vc_type = CONV_LATIN1_MAC;
5772 }
5773 else if ((from_prop & ENC_UNICODE) && (to_prop & ENC_MACROMAN))
5774 {
5775 vcp->vc_type = CONV_UTF8_MAC;
5776 }
5777#endif
5778# ifdef USE_ICONV
5779 else
5780 {
5781 /* Use iconv() for conversion. */
5782 vcp->vc_fd = (iconv_t)my_iconv_open(
5783 (to_prop & ENC_UNICODE) ? (char_u *)"utf-8" : to,
5784 (from_prop & ENC_UNICODE) ? (char_u *)"utf-8" : from);
5785 if (vcp->vc_fd != (iconv_t)-1)
5786 {
5787 vcp->vc_type = CONV_ICONV;
5788 vcp->vc_factor = 4; /* could be longer too... */
5789 }
5790 }
5791# endif
5792 if (vcp->vc_type == CONV_NONE)
5793 return FAIL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 return OK;
5796}
5797
5798#if defined(FEAT_GUI) || defined(AMIGA) || defined(WIN3264) \
5799 || defined(MSDOS) || defined(PROTO)
5800/*
5801 * Do conversion on typed input characters in-place.
5802 * The input and output are not NUL terminated!
5803 * Returns the length after conversion.
5804 */
5805 int
5806convert_input(ptr, len, maxlen)
5807 char_u *ptr;
5808 int len;
5809 int maxlen;
5810{
5811 return convert_input_safe(ptr, len, maxlen, NULL, NULL);
5812}
5813#endif
5814
5815/*
5816 * Like convert_input(), but when there is an incomplete byte sequence at the
5817 * end return that as an allocated string in "restp" and set "*restlenp" to
5818 * the length. If "restp" is NULL it is not used.
5819 */
5820 int
5821convert_input_safe(ptr, len, maxlen, restp, restlenp)
5822 char_u *ptr;
5823 int len;
5824 int maxlen;
5825 char_u **restp;
5826 int *restlenp;
5827{
5828 char_u *d;
5829 int dlen = len;
5830 int unconvertlen = 0;
5831
5832 d = string_convert_ext(&input_conv, ptr, &dlen,
5833 restp == NULL ? NULL : &unconvertlen);
5834 if (d != NULL)
5835 {
5836 if (dlen <= maxlen)
5837 {
5838 if (unconvertlen > 0)
5839 {
5840 /* Move the unconverted characters to allocated memory. */
5841 *restp = alloc(unconvertlen);
5842 if (*restp != NULL)
5843 mch_memmove(*restp, ptr + len - unconvertlen, unconvertlen);
5844 *restlenp = unconvertlen;
5845 }
5846 mch_memmove(ptr, d, dlen);
5847 }
5848 else
5849 /* result is too long, keep the unconverted text (the caller must
5850 * have done something wrong!) */
5851 dlen = len;
5852 vim_free(d);
5853 }
5854 return dlen;
5855}
5856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857/*
5858 * Convert text "ptr[*lenp]" according to "vcp".
5859 * Returns the result in allocated memory and sets "*lenp".
5860 * When "lenp" is NULL, use NUL terminated strings.
5861 * Illegal chars are often changed to "?", unless vcp->vc_fail is set.
5862 * When something goes wrong, NULL is returned and "*lenp" is unchanged.
5863 */
5864 char_u *
5865string_convert(vcp, ptr, lenp)
5866 vimconv_T *vcp;
5867 char_u *ptr;
5868 int *lenp;
5869{
5870 return string_convert_ext(vcp, ptr, lenp, NULL);
5871}
5872
5873/*
5874 * Like string_convert(), but when "unconvlenp" is not NULL and there are is
5875 * an incomplete sequence at the end it is not converted and "*unconvlenp" is
5876 * set to the number of remaining bytes.
5877 */
5878 char_u *
5879string_convert_ext(vcp, ptr, lenp, unconvlenp)
5880 vimconv_T *vcp;
5881 char_u *ptr;
5882 int *lenp;
5883 int *unconvlenp;
5884{
5885 char_u *retval = NULL;
5886 char_u *d;
5887 int len;
5888 int i;
5889 int l;
5890 int c;
5891
5892 if (lenp == NULL)
5893 len = (int)STRLEN(ptr);
5894 else
5895 len = *lenp;
5896 if (len == 0)
5897 return vim_strsave((char_u *)"");
5898
5899 switch (vcp->vc_type)
5900 {
5901 case CONV_TO_UTF8: /* latin1 to utf-8 conversion */
5902 retval = alloc(len * 2 + 1);
5903 if (retval == NULL)
5904 break;
5905 d = retval;
5906 for (i = 0; i < len; ++i)
5907 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005908 c = ptr[i];
5909 if (c < 0x80)
5910 *d++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 else
5912 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005913 *d++ = 0xc0 + ((unsigned)c >> 6);
5914 *d++ = 0x80 + (c & 0x3f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
5916 }
5917 *d = NUL;
5918 if (lenp != NULL)
5919 *lenp = (int)(d - retval);
5920 break;
5921
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005922 case CONV_9_TO_UTF8: /* latin9 to utf-8 conversion */
5923 retval = alloc(len * 3 + 1);
5924 if (retval == NULL)
5925 break;
5926 d = retval;
5927 for (i = 0; i < len; ++i)
5928 {
5929 c = ptr[i];
5930 switch (c)
5931 {
5932 case 0xa4: c = 0x20ac; break; /* euro */
5933 case 0xa6: c = 0x0160; break; /* S hat */
5934 case 0xa8: c = 0x0161; break; /* S -hat */
5935 case 0xb4: c = 0x017d; break; /* Z hat */
5936 case 0xb8: c = 0x017e; break; /* Z -hat */
5937 case 0xbc: c = 0x0152; break; /* OE */
5938 case 0xbd: c = 0x0153; break; /* oe */
5939 case 0xbe: c = 0x0178; break; /* Y */
5940 }
5941 d += utf_char2bytes(c, d);
5942 }
5943 *d = NUL;
5944 if (lenp != NULL)
5945 *lenp = (int)(d - retval);
5946 break;
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 case CONV_TO_LATIN1: /* utf-8 to latin1 conversion */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005949 case CONV_TO_LATIN9: /* utf-8 to latin9 conversion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 retval = alloc(len + 1);
5951 if (retval == NULL)
5952 break;
5953 d = retval;
5954 for (i = 0; i < len; ++i)
5955 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005956 l = utf_ptr2len(ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (l == 0)
5958 *d++ = NUL;
5959 else if (l == 1)
5960 {
5961 if (unconvlenp != NULL && utf8len_tab[ptr[i]] > len - i)
5962 {
5963 /* Incomplete sequence at the end. */
5964 *unconvlenp = len - i;
5965 break;
5966 }
5967 *d++ = ptr[i];
5968 }
5969 else
5970 {
5971 c = utf_ptr2char(ptr + i);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005972 if (vcp->vc_type == CONV_TO_LATIN9)
5973 switch (c)
5974 {
5975 case 0x20ac: c = 0xa4; break; /* euro */
5976 case 0x0160: c = 0xa6; break; /* S hat */
5977 case 0x0161: c = 0xa8; break; /* S -hat */
5978 case 0x017d: c = 0xb4; break; /* Z hat */
5979 case 0x017e: c = 0xb8; break; /* Z -hat */
5980 case 0x0152: c = 0xbc; break; /* OE */
5981 case 0x0153: c = 0xbd; break; /* oe */
5982 case 0x0178: c = 0xbe; break; /* Y */
5983 case 0xa4:
5984 case 0xa6:
5985 case 0xa8:
5986 case 0xb4:
5987 case 0xb8:
5988 case 0xbc:
5989 case 0xbd:
5990 case 0xbe: c = 0x100; break; /* not in latin9 */
5991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 if (!utf_iscomposing(c)) /* skip composing chars */
5993 {
5994 if (c < 0x100)
5995 *d++ = c;
5996 else if (vcp->vc_fail)
5997 {
5998 vim_free(retval);
5999 return NULL;
6000 }
6001 else
6002 {
6003 *d++ = 0xbf;
6004 if (utf_char2cells(c) > 1)
6005 *d++ = '?';
6006 }
6007 }
6008 i += l - 1;
6009 }
6010 }
6011 *d = NUL;
6012 if (lenp != NULL)
6013 *lenp = (int)(d - retval);
6014 break;
6015
Bram Moolenaar56718732006-03-15 22:53:57 +00006016# ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 case CONV_MAC_LATIN1:
6018 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006019 'm', 'l', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 break;
6021
6022 case CONV_LATIN1_MAC:
6023 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006024 'l', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 break;
6026
6027 case CONV_MAC_UTF8:
6028 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006029 'm', 'u', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 break;
6031
6032 case CONV_UTF8_MAC:
6033 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006034 'u', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 break;
6036# endif
6037
6038# ifdef USE_ICONV
6039 case CONV_ICONV: /* conversion with output_conv.vc_fd */
6040 retval = iconv_string(vcp, ptr, len, unconvlenp);
6041 if (retval != NULL && lenp != NULL)
6042 *lenp = (int)STRLEN(retval);
6043 break;
6044# endif
6045# ifdef WIN3264
6046 case CONV_CODEPAGE: /* codepage -> codepage */
6047 {
6048 int retlen;
6049 int tmp_len;
6050 short_u *tmp;
6051
6052 /* 1. codepage/UTF-8 -> ucs-2. */
6053 if (vcp->vc_cpfrom == 0)
6054 tmp_len = utf8_to_ucs2(ptr, len, NULL, NULL);
6055 else
6056 tmp_len = MultiByteToWideChar(vcp->vc_cpfrom, 0,
6057 ptr, len, 0, 0);
6058 tmp = (short_u *)alloc(sizeof(short_u) * tmp_len);
6059 if (tmp == NULL)
6060 break;
6061 if (vcp->vc_cpfrom == 0)
6062 utf8_to_ucs2(ptr, len, tmp, unconvlenp);
6063 else
6064 MultiByteToWideChar(vcp->vc_cpfrom, 0, ptr, len, tmp, tmp_len);
6065
6066 /* 2. ucs-2 -> codepage/UTF-8. */
6067 if (vcp->vc_cpto == 0)
6068 retlen = ucs2_to_utf8(tmp, tmp_len, NULL);
6069 else
6070 retlen = WideCharToMultiByte(vcp->vc_cpto, 0,
6071 tmp, tmp_len, 0, 0, 0, 0);
6072 retval = alloc(retlen + 1);
6073 if (retval != NULL)
6074 {
6075 if (vcp->vc_cpto == 0)
6076 ucs2_to_utf8(tmp, tmp_len, retval);
6077 else
6078 WideCharToMultiByte(vcp->vc_cpto, 0,
6079 tmp, tmp_len, retval, retlen, 0, 0);
6080 retval[retlen] = NUL;
6081 if (lenp != NULL)
6082 *lenp = retlen;
6083 }
6084 vim_free(tmp);
6085 break;
6086 }
6087# endif
6088 }
6089
6090 return retval;
6091}
6092#endif