blob: 88599264b004b61ebc08684de34cb989016c0d8d [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.
Bram Moolenaar3e8cb582010-01-12 19:52:03 +010029 * To make things complicated, up to six composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * 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 Moolenaar182c5be2010-06-25 05:37:59 +0200103#if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
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 Moolenaarfeba08b2009-06-16 13:12:07 +0000130static int dbcs_ptr2len_len __ARGS((char_u *p, int size));
131static int utf_ptr2cells_len __ARGS((char_u *p, int size));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132static int dbcs_char2cells __ARGS((int c));
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000133static int dbcs_ptr2cells_len __ARGS((char_u *p, int size));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134static int dbcs_ptr2char __ARGS((char_u *p));
Bram Moolenaar35ee4522011-07-15 21:16:59 +0200135static int utf_safe_read_char_adv __ARGS((char_u **s, size_t *n));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar24397332009-12-02 14:02:39 +0000137/*
138 * Lookup table to quickly get the length in bytes of a UTF-8 character from
139 * the first byte of a UTF-8 string.
140 * Bytes which are illegal when used as the first byte have a 1.
141 * The NUL byte has length 1.
142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static char utf8len_tab[256] =
144{
145 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,
146 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,
147 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,
148 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,
Bram Moolenaar24397332009-12-02 14:02:39 +0000149 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,
150 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,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 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,
152 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,
153};
154
155/*
Bram Moolenaar24397332009-12-02 14:02:39 +0000156 * Like utf8len_tab above, but using a zero for illegal lead bytes.
157 */
158static char utf8len_tab_zero[256] =
159{
160 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,
161 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,
162 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,
163 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,
164 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
165 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
166 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,
167 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,0,0,
168};
169
170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 * XIM often causes trouble. Define XIM_DEBUG to get a log of XIM callbacks
172 * in the "xim.log" file.
173 */
174/* #define XIM_DEBUG */
175#ifdef XIM_DEBUG
176 static void
177xim_log(char *s, ...)
178{
179 va_list arglist;
180 static FILE *fd = NULL;
181
182 if (fd == (FILE *)-1)
183 return;
184 if (fd == NULL)
185 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000186 fd = mch_fopen("xim.log", "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (fd == NULL)
188 {
189 EMSG("Cannot open xim.log");
190 fd = (FILE *)-1;
191 return;
192 }
193 }
194
195 va_start(arglist, s);
196 vfprintf(fd, s, arglist);
197 va_end(arglist);
198}
199#endif
200
201#endif
202
203#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
204/*
205 * Canonical encoding names and their properties.
206 * "iso-8859-n" is handled by enc_canonize() directly.
207 */
208static struct
209{ char *name; int prop; int codepage;}
210enc_canon_table[] =
211{
212#define IDX_LATIN_1 0
213 {"latin1", ENC_8BIT + ENC_LATIN1, 1252},
214#define IDX_ISO_2 1
215 {"iso-8859-2", ENC_8BIT, 0},
216#define IDX_ISO_3 2
217 {"iso-8859-3", ENC_8BIT, 0},
218#define IDX_ISO_4 3
219 {"iso-8859-4", ENC_8BIT, 0},
220#define IDX_ISO_5 4
221 {"iso-8859-5", ENC_8BIT, 0},
222#define IDX_ISO_6 5
223 {"iso-8859-6", ENC_8BIT, 0},
224#define IDX_ISO_7 6
225 {"iso-8859-7", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000226#define IDX_ISO_8 7
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {"iso-8859-8", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000228#define IDX_ISO_9 8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {"iso-8859-9", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000230#define IDX_ISO_10 9
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {"iso-8859-10", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000232#define IDX_ISO_11 10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {"iso-8859-11", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000234#define IDX_ISO_13 11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {"iso-8859-13", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000236#define IDX_ISO_14 12
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {"iso-8859-14", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000238#define IDX_ISO_15 13
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000239 {"iso-8859-15", ENC_8BIT + ENC_LATIN9, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000240#define IDX_KOI8_R 14
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {"koi8-r", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000242#define IDX_KOI8_U 15
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {"koi8-u", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000244#define IDX_UTF8 16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {"utf-8", ENC_UNICODE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000246#define IDX_UCS2 17
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {"ucs-2", ENC_UNICODE + ENC_ENDIAN_B + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000248#define IDX_UCS2LE 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {"ucs-2le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000250#define IDX_UTF16 19
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 {"utf-16", ENC_UNICODE + ENC_ENDIAN_B + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000252#define IDX_UTF16LE 20
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {"utf-16le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000254#define IDX_UCS4 21
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {"ucs-4", ENC_UNICODE + ENC_ENDIAN_B + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000256#define IDX_UCS4LE 22
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {"ucs-4le", ENC_UNICODE + ENC_ENDIAN_L + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000258
259 /* For debugging DBCS encoding on Unix. */
260#define IDX_DEBUG 23
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {"debug", ENC_DBCS, DBCS_DEBUG},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000262#define IDX_EUC_JP 24
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {"euc-jp", ENC_DBCS, DBCS_JPNU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000264#define IDX_SJIS 25
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {"sjis", ENC_DBCS, DBCS_JPN},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000266#define IDX_EUC_KR 26
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 {"euc-kr", ENC_DBCS, DBCS_KORU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000268#define IDX_EUC_CN 27
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {"euc-cn", ENC_DBCS, DBCS_CHSU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000270#define IDX_EUC_TW 28
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {"euc-tw", ENC_DBCS, DBCS_CHTU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000272#define IDX_BIG5 29
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 {"big5", ENC_DBCS, DBCS_CHT},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000274
275 /* MS-DOS and MS-Windows codepages are included here, so that they can be
276 * used on Unix too. Most of them are similar to ISO-8859 encodings, but
277 * not exactly the same. */
278#define IDX_CP437 30
279 {"cp437", ENC_8BIT, 437}, /* like iso-8859-1 */
280#define IDX_CP737 31
281 {"cp737", ENC_8BIT, 737}, /* like iso-8859-7 */
282#define IDX_CP775 32
283 {"cp775", ENC_8BIT, 775}, /* Baltic */
284#define IDX_CP850 33
285 {"cp850", ENC_8BIT, 850}, /* like iso-8859-4 */
286#define IDX_CP852 34
287 {"cp852", ENC_8BIT, 852}, /* like iso-8859-1 */
288#define IDX_CP855 35
289 {"cp855", ENC_8BIT, 855}, /* like iso-8859-2 */
290#define IDX_CP857 36
291 {"cp857", ENC_8BIT, 857}, /* like iso-8859-5 */
292#define IDX_CP860 37
293 {"cp860", ENC_8BIT, 860}, /* like iso-8859-9 */
294#define IDX_CP861 38
295 {"cp861", ENC_8BIT, 861}, /* like iso-8859-1 */
296#define IDX_CP862 39
297 {"cp862", ENC_8BIT, 862}, /* like iso-8859-1 */
298#define IDX_CP863 40
299 {"cp863", ENC_8BIT, 863}, /* like iso-8859-8 */
300#define IDX_CP865 41
301 {"cp865", ENC_8BIT, 865}, /* like iso-8859-1 */
302#define IDX_CP866 42
303 {"cp866", ENC_8BIT, 866}, /* like iso-8859-5 */
304#define IDX_CP869 43
305 {"cp869", ENC_8BIT, 869}, /* like iso-8859-7 */
306#define IDX_CP874 44
307 {"cp874", ENC_8BIT, 874}, /* Thai */
308#define IDX_CP932 45
309 {"cp932", ENC_DBCS, DBCS_JPN},
310#define IDX_CP936 46
311 {"cp936", ENC_DBCS, DBCS_CHS},
312#define IDX_CP949 47
313 {"cp949", ENC_DBCS, DBCS_KOR},
314#define IDX_CP950 48
315 {"cp950", ENC_DBCS, DBCS_CHT},
316#define IDX_CP1250 49
317 {"cp1250", ENC_8BIT, 1250}, /* Czech, Polish, etc. */
318#define IDX_CP1251 50
319 {"cp1251", ENC_8BIT, 1251}, /* Cyrillic */
320 /* cp1252 is considered to be equal to latin1 */
321#define IDX_CP1253 51
322 {"cp1253", ENC_8BIT, 1253}, /* Greek */
323#define IDX_CP1254 52
324 {"cp1254", ENC_8BIT, 1254}, /* Turkish */
325#define IDX_CP1255 53
326 {"cp1255", ENC_8BIT, 1255}, /* Hebrew */
327#define IDX_CP1256 54
328 {"cp1256", ENC_8BIT, 1256}, /* Arabic */
329#define IDX_CP1257 55
330 {"cp1257", ENC_8BIT, 1257}, /* Baltic */
331#define IDX_CP1258 56
332 {"cp1258", ENC_8BIT, 1258}, /* Vietnamese */
333
334#define IDX_MACROMAN 57
335 {"macroman", ENC_8BIT + ENC_MACROMAN, 0}, /* Mac OS */
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000336#define IDX_DECMCS 58
337 {"dec-mcs", ENC_8BIT, 0}, /* DEC MCS */
338#define IDX_HPROMAN8 59
339 {"hp-roman8", ENC_8BIT, 0}, /* HP Roman8 */
340#define IDX_COUNT 60
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341};
342
343/*
344 * Aliases for encoding names.
345 */
346static struct
347{ char *name; int canon;}
348enc_alias_table[] =
349{
350 {"ansi", IDX_LATIN_1},
351 {"iso-8859-1", IDX_LATIN_1},
352 {"latin2", IDX_ISO_2},
353 {"latin3", IDX_ISO_3},
354 {"latin4", IDX_ISO_4},
355 {"cyrillic", IDX_ISO_5},
356 {"arabic", IDX_ISO_6},
357 {"greek", IDX_ISO_7},
358#ifdef WIN3264
359 {"hebrew", IDX_CP1255},
360#else
361 {"hebrew", IDX_ISO_8},
362#endif
363 {"latin5", IDX_ISO_9},
364 {"turkish", IDX_ISO_9}, /* ? */
365 {"latin6", IDX_ISO_10},
366 {"nordic", IDX_ISO_10}, /* ? */
367 {"thai", IDX_ISO_11}, /* ? */
368 {"latin7", IDX_ISO_13},
369 {"latin8", IDX_ISO_14},
370 {"latin9", IDX_ISO_15},
371 {"utf8", IDX_UTF8},
372 {"unicode", IDX_UCS2},
373 {"ucs2", IDX_UCS2},
374 {"ucs2be", IDX_UCS2},
375 {"ucs-2be", IDX_UCS2},
376 {"ucs2le", IDX_UCS2LE},
377 {"utf16", IDX_UTF16},
378 {"utf16be", IDX_UTF16},
379 {"utf-16be", IDX_UTF16},
380 {"utf16le", IDX_UTF16LE},
381 {"ucs4", IDX_UCS4},
382 {"ucs4be", IDX_UCS4},
383 {"ucs-4be", IDX_UCS4},
384 {"ucs4le", IDX_UCS4LE},
Bram Moolenaar5360af92008-02-20 10:29:39 +0000385 {"utf32", IDX_UCS4},
386 {"utf-32", IDX_UCS4},
387 {"utf32be", IDX_UCS4},
388 {"utf-32be", IDX_UCS4},
389 {"utf32le", IDX_UCS4LE},
390 {"utf-32le", IDX_UCS4LE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {"932", IDX_CP932},
392 {"949", IDX_CP949},
393 {"936", IDX_CP936},
Bram Moolenaar0928caa2006-08-16 16:03:34 +0000394 {"gbk", IDX_CP936},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {"950", IDX_CP950},
396 {"eucjp", IDX_EUC_JP},
397 {"unix-jis", IDX_EUC_JP},
398 {"ujis", IDX_EUC_JP},
399 {"shift-jis", IDX_SJIS},
400 {"euckr", IDX_EUC_KR},
401 {"5601", IDX_EUC_KR}, /* Sun: KS C 5601 */
402 {"euccn", IDX_EUC_CN},
403 {"gb2312", IDX_EUC_CN},
404 {"euctw", IDX_EUC_TW},
405#if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
406 {"japan", IDX_CP932},
407 {"korea", IDX_CP949},
408 {"prc", IDX_CP936},
409 {"chinese", IDX_CP936},
410 {"taiwan", IDX_CP950},
411 {"big5", IDX_CP950},
412#else
413 {"japan", IDX_EUC_JP},
414 {"korea", IDX_EUC_KR},
415 {"prc", IDX_EUC_CN},
416 {"chinese", IDX_EUC_CN},
417 {"taiwan", IDX_EUC_TW},
418 {"cp950", IDX_BIG5},
419 {"950", IDX_BIG5},
420#endif
421 {"mac", IDX_MACROMAN},
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000422 {"mac-roman", IDX_MACROMAN},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {NULL, 0}
424};
425
426#ifndef CP_UTF8
427# define CP_UTF8 65001 /* magic number from winnls.h */
428#endif
429
430/*
431 * Find encoding "name" in the list of canonical encoding names.
432 * Returns -1 if not found.
433 */
434 static int
435enc_canon_search(name)
436 char_u *name;
437{
438 int i;
439
440 for (i = 0; i < IDX_COUNT; ++i)
441 if (STRCMP(name, enc_canon_table[i].name) == 0)
442 return i;
443 return -1;
444}
445
446#endif
447
448#if defined(FEAT_MBYTE) || defined(PROTO)
449
450/*
451 * Find canonical encoding "name" in the list and return its properties.
452 * Returns 0 if not found.
453 */
454 int
455enc_canon_props(name)
456 char_u *name;
457{
458 int i;
459
460 i = enc_canon_search(name);
461 if (i >= 0)
462 return enc_canon_table[i].prop;
463#ifdef WIN3264
464 if (name[0] == 'c' && name[1] == 'p' && VIM_ISDIGIT(name[2]))
465 {
466 CPINFO cpinfo;
467
468 /* Get info on this codepage to find out what it is. */
469 if (GetCPInfo(atoi(name + 2), &cpinfo) != 0)
470 {
471 if (cpinfo.MaxCharSize == 1) /* some single-byte encoding */
472 return ENC_8BIT;
473 if (cpinfo.MaxCharSize == 2
474 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
475 /* must be a DBCS encoding */
476 return ENC_DBCS;
477 }
478 return 0;
479 }
480#endif
481 if (STRNCMP(name, "2byte-", 6) == 0)
482 return ENC_DBCS;
483 if (STRNCMP(name, "8bit-", 5) == 0 || STRNCMP(name, "iso-8859-", 9) == 0)
484 return ENC_8BIT;
485 return 0;
486}
487
488/*
489 * Set up for using multi-byte characters.
490 * Called in three cases:
491 * - by main() to initialize (p_enc == NULL)
492 * - by set_init_1() after 'encoding' was set to its default.
493 * - by do_set() when 'encoding' has been set.
494 * p_enc must have been passed through enc_canonize() already.
495 * Sets the "enc_unicode", "enc_utf8", "enc_dbcs" and "has_mbyte" flags.
496 * Fills mb_bytelen_tab[] and returns NULL when there are no problems.
497 * When there is something wrong: Returns an error message and doesn't change
498 * anything.
499 */
500 char_u *
501mb_init()
502{
503 int i;
504 int idx;
505 int n;
506 int enc_dbcs_new = 0;
507#if defined(USE_ICONV) && !defined(WIN3264) && !defined(WIN32UNIX) \
508 && !defined(MACOS)
509# define LEN_FROM_CONV
510 vimconv_T vimconv;
511 char_u *p;
512#endif
513
514 if (p_enc == NULL)
515 {
516 /* Just starting up: set the whole table to one's. */
517 for (i = 0; i < 256; ++i)
518 mb_bytelen_tab[i] = 1;
519 input_conv.vc_type = CONV_NONE;
520 input_conv.vc_factor = 1;
521 output_conv.vc_type = CONV_NONE;
522 return NULL;
523 }
524
525#ifdef WIN3264
526 if (p_enc[0] == 'c' && p_enc[1] == 'p' && VIM_ISDIGIT(p_enc[2]))
527 {
528 CPINFO cpinfo;
529
530 /* Get info on this codepage to find out what it is. */
531 if (GetCPInfo(atoi(p_enc + 2), &cpinfo) != 0)
532 {
533 if (cpinfo.MaxCharSize == 1)
534 {
535 /* some single-byte encoding */
536 enc_unicode = 0;
537 enc_utf8 = FALSE;
538 }
539 else if (cpinfo.MaxCharSize == 2
540 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
541 {
542 /* must be a DBCS encoding, check below */
543 enc_dbcs_new = atoi(p_enc + 2);
544 }
545 else
546 goto codepage_invalid;
547 }
548 else if (GetLastError() == ERROR_INVALID_PARAMETER)
549 {
550codepage_invalid:
551 return (char_u *)N_("E543: Not a valid codepage");
552 }
553 }
554#endif
555 else if (STRNCMP(p_enc, "8bit-", 5) == 0
556 || STRNCMP(p_enc, "iso-8859-", 9) == 0)
557 {
558 /* Accept any "8bit-" or "iso-8859-" name. */
559 enc_unicode = 0;
560 enc_utf8 = FALSE;
561 }
562 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
563 {
564#ifdef WIN3264
565 /* Windows: accept only valid codepage numbers, check below. */
566 if (p_enc[6] != 'c' || p_enc[7] != 'p'
567 || (enc_dbcs_new = atoi(p_enc + 8)) == 0)
568 return e_invarg;
569#else
570 /* Unix: accept any "2byte-" name, assume current locale. */
571 enc_dbcs_new = DBCS_2BYTE;
572#endif
573 }
574 else if ((idx = enc_canon_search(p_enc)) >= 0)
575 {
576 i = enc_canon_table[idx].prop;
577 if (i & ENC_UNICODE)
578 {
579 /* Unicode */
580 enc_utf8 = TRUE;
581 if (i & (ENC_2BYTE | ENC_2WORD))
582 enc_unicode = 2;
583 else if (i & ENC_4BYTE)
584 enc_unicode = 4;
585 else
586 enc_unicode = 0;
587 }
588 else if (i & ENC_DBCS)
589 {
590 /* 2byte, handle below */
591 enc_dbcs_new = enc_canon_table[idx].codepage;
592 }
593 else
594 {
595 /* Must be 8-bit. */
596 enc_unicode = 0;
597 enc_utf8 = FALSE;
598 }
599 }
600 else /* Don't know what encoding this is, reject it. */
601 return e_invarg;
602
603 if (enc_dbcs_new != 0)
604 {
605#ifdef WIN3264
606 /* Check if the DBCS code page is OK. */
607 if (!IsValidCodePage(enc_dbcs_new))
608 goto codepage_invalid;
609#endif
610 enc_unicode = 0;
611 enc_utf8 = FALSE;
612 }
613 enc_dbcs = enc_dbcs_new;
614 has_mbyte = (enc_dbcs != 0 || enc_utf8);
615
616#ifdef WIN3264
617 enc_codepage = encname2codepage(p_enc);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000618 enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#endif
620
Bram Moolenaar78622822005-08-23 21:00:13 +0000621 /* Detect an encoding that uses latin1 characters. */
622 enc_latin1like = (enc_utf8 || STRCMP(p_enc, "latin1") == 0
623 || STRCMP(p_enc, "iso-8859-15") == 0);
624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 /*
626 * Set the function pointers.
627 */
628 if (enc_utf8)
629 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000630 mb_ptr2len = utfc_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000631 mb_ptr2len_len = utfc_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 mb_char2len = utf_char2len;
633 mb_char2bytes = utf_char2bytes;
634 mb_ptr2cells = utf_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000635 mb_ptr2cells_len = utf_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 mb_char2cells = utf_char2cells;
637 mb_off2cells = utf_off2cells;
638 mb_ptr2char = utf_ptr2char;
639 mb_head_off = utf_head_off;
640 }
641 else if (enc_dbcs != 0)
642 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000643 mb_ptr2len = dbcs_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000644 mb_ptr2len_len = dbcs_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 mb_char2len = dbcs_char2len;
646 mb_char2bytes = dbcs_char2bytes;
647 mb_ptr2cells = dbcs_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000648 mb_ptr2cells_len = dbcs_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 mb_char2cells = dbcs_char2cells;
650 mb_off2cells = dbcs_off2cells;
651 mb_ptr2char = dbcs_ptr2char;
652 mb_head_off = dbcs_head_off;
653 }
654 else
655 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000656 mb_ptr2len = latin_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000657 mb_ptr2len_len = latin_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 mb_char2len = latin_char2len;
659 mb_char2bytes = latin_char2bytes;
660 mb_ptr2cells = latin_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000661 mb_ptr2cells_len = latin_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 mb_char2cells = latin_char2cells;
663 mb_off2cells = latin_off2cells;
664 mb_ptr2char = latin_ptr2char;
665 mb_head_off = latin_head_off;
666 }
667
668 /*
669 * Fill the mb_bytelen_tab[] for MB_BYTE2LEN().
670 */
671#ifdef LEN_FROM_CONV
672 /* When 'encoding' is different from the current locale mblen() won't
673 * work. Use conversion to "utf-8" instead. */
674 vimconv.vc_type = CONV_NONE;
675 if (enc_dbcs)
676 {
677 p = enc_locale();
678 if (p == NULL || STRCMP(p, p_enc) != 0)
679 {
680 convert_setup(&vimconv, p_enc, (char_u *)"utf-8");
681 vimconv.vc_fail = TRUE;
682 }
683 vim_free(p);
684 }
685#endif
686
687 for (i = 0; i < 256; ++i)
688 {
689 /* Our own function to reliably check the length of UTF-8 characters,
690 * independent of mblen(). */
691 if (enc_utf8)
692 n = utf8len_tab[i];
693 else if (enc_dbcs == 0)
694 n = 1;
695 else
696 {
697#if defined(WIN3264) || defined(WIN32UNIX)
698 /* enc_dbcs is set by setting 'fileencoding'. It becomes a Windows
699 * CodePage identifier, which we can pass directly in to Windows
700 * API */
701 n = IsDBCSLeadByteEx(enc_dbcs, (BYTE)i) ? 2 : 1;
702#else
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000703# if defined(MACOS) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 /*
705 * if mblen() is not available, character which MSB is turned on
706 * are treated as leading byte character. (note : This assumption
707 * is not always true.)
708 */
709 n = (i & 0x80) ? 2 : 1;
710# else
711 char buf[MB_MAXBYTES];
712# ifdef X_LOCALE
713# ifndef mblen
714# define mblen _Xmblen
715# endif
716# endif
717 if (i == NUL) /* just in case mblen() can't handle "" */
718 n = 1;
719 else
720 {
721 buf[0] = i;
722 buf[1] = 0;
723#ifdef LEN_FROM_CONV
724 if (vimconv.vc_type != CONV_NONE)
725 {
726 /*
727 * string_convert() should fail when converting the first
728 * byte of a double-byte character.
729 */
730 p = string_convert(&vimconv, (char_u *)buf, NULL);
731 if (p != NULL)
732 {
733 vim_free(p);
734 n = 1;
735 }
736 else
737 n = 2;
738 }
739 else
740#endif
741 {
742 /*
743 * mblen() should return -1 for invalid (means the leading
744 * multibyte) character. However there are some platforms
745 * where mblen() returns 0 for invalid character.
746 * Therefore, following condition includes 0.
747 */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000748 ignored = mblen(NULL, 0); /* First reset the state. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 if (mblen(buf, (size_t)1) <= 0)
750 n = 2;
751 else
752 n = 1;
753 }
754 }
755# endif
756#endif
757 }
758
759 mb_bytelen_tab[i] = n;
760 }
761
762#ifdef LEN_FROM_CONV
763 convert_setup(&vimconv, NULL, NULL);
764#endif
765
766 /* The cell width depends on the type of multi-byte characters. */
767 (void)init_chartab();
768
769 /* When enc_utf8 is set or reset, (de)allocate ScreenLinesUC[] */
770 screenalloc(FALSE);
771
772 /* When using Unicode, set default for 'fileencodings'. */
773 if (enc_utf8 && !option_was_set((char_u *)"fencs"))
774 set_string_option_direct((char_u *)"fencs", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000775 (char_u *)"ucs-bom,utf-8,default,latin1", OPT_FREE, 0);
776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
778 /* GNU gettext 0.10.37 supports this feature: set the codeset used for
779 * translated messages independently from the current locale. */
780 (void)bind_textdomain_codeset(VIMPACKAGE,
781 enc_utf8 ? "utf-8" : (char *)p_enc);
782#endif
783
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000784#ifdef WIN32
785 /* When changing 'encoding' while starting up, then convert the command
786 * line arguments from the active codepage to 'encoding'. */
787 if (starting != 0)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000788 fix_arg_enc();
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000789#endif
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#ifdef FEAT_AUTOCMD
792 /* Fire an autocommand to let people do custom font setup. This must be
793 * after Vim has been setup for the new encoding. */
794 apply_autocmds(EVENT_ENCODINGCHANGED, NULL, (char_u *)"", FALSE, curbuf);
795#endif
796
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000797#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000798 /* Need to reload spell dictionaries */
799 spell_reload();
800#endif
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 return NULL;
803}
804
805/*
806 * Return the size of the BOM for the current buffer:
807 * 0 - no BOM
808 * 2 - UCS-2 or UTF-16 BOM
809 * 4 - UCS-4 BOM
810 * 3 - UTF-8 BOM
811 */
812 int
813bomb_size()
814{
815 int n = 0;
816
817 if (curbuf->b_p_bomb && !curbuf->b_p_bin)
818 {
819 if (*curbuf->b_p_fenc == NUL)
820 {
821 if (enc_utf8)
822 {
823 if (enc_unicode != 0)
824 n = enc_unicode;
825 else
826 n = 3;
827 }
828 }
829 else if (STRCMP(curbuf->b_p_fenc, "utf-8") == 0)
830 n = 3;
831 else if (STRNCMP(curbuf->b_p_fenc, "ucs-2", 5) == 0
832 || STRNCMP(curbuf->b_p_fenc, "utf-16", 6) == 0)
833 n = 2;
834 else if (STRNCMP(curbuf->b_p_fenc, "ucs-4", 5) == 0)
835 n = 4;
836 }
837 return n;
838}
839
840/*
841 * Get class of pointer:
842 * 0 for blank or NUL
843 * 1 for punctuation
844 * 2 for an (ASCII) word character
845 * >2 for other word characters
846 */
847 int
848mb_get_class(p)
849 char_u *p;
850{
851 if (MB_BYTE2LEN(p[0]) == 1)
852 {
853 if (p[0] == NUL || vim_iswhite(p[0]))
854 return 0;
855 if (vim_iswordc(p[0]))
856 return 2;
857 return 1;
858 }
859 if (enc_dbcs != 0 && p[0] != NUL && p[1] != NUL)
860 return dbcs_class(p[0], p[1]);
861 if (enc_utf8)
862 return utf_class(utf_ptr2char(p));
863 return 0;
864}
865
866/*
867 * Get class of a double-byte character. This always returns 3 or bigger.
868 * TODO: Should return 1 for punctuation.
869 */
870 int
871dbcs_class(lead, trail)
872 unsigned lead;
873 unsigned trail;
874{
875 switch (enc_dbcs)
876 {
877 /* please add classfy routine for your language in here */
878
879 case DBCS_JPNU: /* ? */
880 case DBCS_JPN:
881 {
882 /* JIS code classification */
883 unsigned char lb = lead;
884 unsigned char tb = trail;
885
886 /* convert process code to JIS */
887# if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
888 /* process code is SJIS */
889 if (lb <= 0x9f)
890 lb = (lb - 0x81) * 2 + 0x21;
891 else
892 lb = (lb - 0xc1) * 2 + 0x21;
893 if (tb <= 0x7e)
894 tb -= 0x1f;
895 else if (tb <= 0x9e)
896 tb -= 0x20;
897 else
898 {
899 tb -= 0x7e;
900 lb += 1;
901 }
902# else
903 /*
904 * XXX: Code page identification can not use with all
905 * system! So, some other encoding information
906 * will be needed.
907 * In japanese: SJIS,EUC,UNICODE,(JIS)
908 * Note that JIS-code system don't use as
909 * process code in most system because it uses
910 * escape sequences(JIS is context depend encoding).
911 */
912 /* assume process code is JAPANESE-EUC */
913 lb &= 0x7f;
914 tb &= 0x7f;
915# endif
916 /* exceptions */
917 switch (lb << 8 | tb)
918 {
919 case 0x2121: /* ZENKAKU space */
920 return 0;
921 case 0x2122: /* KU-TEN (Japanese comma) */
922 case 0x2123: /* TOU-TEN (Japanese period) */
923 case 0x2124: /* ZENKAKU comma */
924 case 0x2125: /* ZENKAKU period */
925 return 1;
926 case 0x213c: /* prolongedsound handled as KATAKANA */
927 return 13;
928 }
929 /* sieved by KU code */
930 switch (lb)
931 {
932 case 0x21:
933 case 0x22:
934 /* special symbols */
935 return 10;
936 case 0x23:
937 /* alpha-numeric */
938 return 11;
939 case 0x24:
940 /* hiragana */
941 return 12;
942 case 0x25:
943 /* katakana */
944 return 13;
945 case 0x26:
946 /* greek */
947 return 14;
948 case 0x27:
949 /* russian */
950 return 15;
951 case 0x28:
952 /* lines */
953 return 16;
954 default:
955 /* kanji */
956 return 17;
957 }
958 }
959
960 case DBCS_KORU: /* ? */
961 case DBCS_KOR:
962 {
963 /* KS code classification */
964 unsigned char c1 = lead;
965 unsigned char c2 = trail;
966
967 /*
968 * 20 : Hangul
969 * 21 : Hanja
970 * 22 : Symbols
971 * 23 : Alpha-numeric/Roman Letter (Full width)
972 * 24 : Hangul Letter(Alphabet)
973 * 25 : Roman Numeral/Greek Letter
974 * 26 : Box Drawings
975 * 27 : Unit Symbols
976 * 28 : Circled/Parenthesized Letter
977 * 29 : Hirigana/Katakana
978 * 30 : Cyrillic Letter
979 */
980
981 if (c1 >= 0xB0 && c1 <= 0xC8)
982 /* Hangul */
983 return 20;
984#if defined(WIN3264) || defined(WIN32UNIX)
985 else if (c1 <= 0xA0 || c2 <= 0xA0)
986 /* Extended Hangul Region : MS UHC(Unified Hangul Code) */
987 /* c1: 0x81-0xA0 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xFE
988 * c1: 0xA1-0xC6 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xA0
989 */
990 return 20;
991#endif
992
993 else if (c1 >= 0xCA && c1 <= 0xFD)
994 /* Hanja */
995 return 21;
996 else switch (c1)
997 {
998 case 0xA1:
999 case 0xA2:
1000 /* Symbols */
1001 return 22;
1002 case 0xA3:
1003 /* Alpha-numeric */
1004 return 23;
1005 case 0xA4:
1006 /* Hangul Letter(Alphabet) */
1007 return 24;
1008 case 0xA5:
1009 /* Roman Numeral/Greek Letter */
1010 return 25;
1011 case 0xA6:
1012 /* Box Drawings */
1013 return 26;
1014 case 0xA7:
1015 /* Unit Symbols */
1016 return 27;
1017 case 0xA8:
1018 case 0xA9:
1019 if (c2 <= 0xAF)
1020 return 25; /* Roman Letter */
1021 else if (c2 >= 0xF6)
1022 return 22; /* Symbols */
1023 else
1024 /* Circled/Parenthesized Letter */
1025 return 28;
1026 case 0xAA:
1027 case 0xAB:
1028 /* Hirigana/Katakana */
1029 return 29;
1030 case 0xAC:
1031 /* Cyrillic Letter */
1032 return 30;
1033 }
1034 }
1035 default:
1036 break;
1037 }
1038 return 3;
1039}
1040
1041/*
1042 * mb_char2len() function pointer.
1043 * Return length in bytes of character "c".
1044 * Returns 1 for a single-byte character.
1045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 int
1047latin_char2len(c)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001048 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
1050 return 1;
1051}
1052
1053 static int
1054dbcs_char2len(c)
1055 int c;
1056{
1057 if (c >= 0x100)
1058 return 2;
1059 return 1;
1060}
1061
1062/*
1063 * mb_char2bytes() function pointer.
1064 * Convert a character to its bytes.
1065 * Returns the length in bytes.
1066 */
1067 int
1068latin_char2bytes(c, buf)
1069 int c;
1070 char_u *buf;
1071{
1072 buf[0] = c;
1073 return 1;
1074}
1075
1076 static int
1077dbcs_char2bytes(c, buf)
1078 int c;
1079 char_u *buf;
1080{
1081 if (c >= 0x100)
1082 {
1083 buf[0] = (unsigned)c >> 8;
1084 buf[1] = c;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001085 /* Never use a NUL byte, it causes lots of trouble. It's an invalid
1086 * character anyway. */
1087 if (buf[1] == NUL)
1088 buf[1] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 return 2;
1090 }
1091 buf[0] = c;
1092 return 1;
1093}
1094
1095/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001096 * mb_ptr2len() function pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 * Get byte length of character at "*p" but stop at a NUL.
1098 * For UTF-8 this includes following composing characters.
1099 * Returns 0 when *p is NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
1101 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001102latin_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 char_u *p;
1104{
1105 return MB_BYTE2LEN(*p);
1106}
1107
1108 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001109dbcs_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 char_u *p;
1111{
1112 int len;
1113
1114 /* Check if second byte is not missing. */
1115 len = MB_BYTE2LEN(*p);
1116 if (len == 2 && p[1] == NUL)
1117 len = 1;
1118 return len;
1119}
1120
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001121/*
1122 * mb_ptr2len_len() function pointer.
1123 * Like mb_ptr2len(), but limit to read "size" bytes.
1124 * Returns 0 for an empty string.
1125 * Returns 1 for an illegal char or an incomplete byte sequence.
1126 */
1127 int
1128latin_ptr2len_len(p, size)
1129 char_u *p;
1130 int size;
1131{
1132 if (size < 1 || *p == NUL)
1133 return 0;
1134 return 1;
1135}
1136
1137 static int
1138dbcs_ptr2len_len(p, size)
1139 char_u *p;
1140 int size;
1141{
1142 int len;
1143
1144 if (size < 1 || *p == NUL)
1145 return 0;
1146 if (size == 1)
1147 return 1;
1148 /* Check that second byte is not missing. */
1149 len = MB_BYTE2LEN(*p);
1150 if (len == 2 && p[1] == NUL)
1151 len = 1;
1152 return len;
1153}
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155struct interval
1156{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01001157 long first;
1158 long last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159};
1160static int intable __ARGS((struct interval *table, size_t size, int c));
1161
1162/*
1163 * Return TRUE if "c" is in "table[size / sizeof(struct interval)]".
1164 */
1165 static int
1166intable(table, size, c)
1167 struct interval *table;
1168 size_t size;
1169 int c;
1170{
1171 int mid, bot, top;
1172
1173 /* first quick check for Latin1 etc. characters */
1174 if (c < table[0].first)
1175 return FALSE;
1176
1177 /* binary search in table */
1178 bot = 0;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001179 top = (int)(size / sizeof(struct interval) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 while (top >= bot)
1181 {
1182 mid = (bot + top) / 2;
1183 if (table[mid].last < c)
1184 bot = mid + 1;
1185 else if (table[mid].first > c)
1186 top = mid - 1;
1187 else
1188 return TRUE;
1189 }
1190 return FALSE;
1191}
1192
1193/*
1194 * For UTF-8 character "c" return 2 for a double-width character, 1 for others.
1195 * Returns 4 or 6 for an unprintable character.
1196 * Is only correct for characters >= 0x80.
1197 * When p_ambw is "double", return 2 for a character with East Asian Width
1198 * class 'A'(mbiguous).
1199 */
1200 int
1201utf_char2cells(c)
1202 int c;
1203{
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001204 /* Sorted list of non-overlapping intervals of East Asian double width
1205 * characters, generated with ../runtime/tools/unicode.vim. */
1206 static struct interval doublewidth[] =
1207 {
1208 {0x1100, 0x115f},
1209 {0x11a3, 0x11a7},
1210 {0x11fa, 0x11ff},
1211 {0x2329, 0x232a},
1212 {0x2e80, 0x2e99},
1213 {0x2e9b, 0x2ef3},
1214 {0x2f00, 0x2fd5},
1215 {0x2ff0, 0x2ffb},
1216 {0x3000, 0x3029},
1217 {0x3030, 0x303e},
1218 {0x3041, 0x3096},
1219 {0x309b, 0x30ff},
1220 {0x3105, 0x312d},
1221 {0x3131, 0x318e},
1222 {0x3190, 0x31b7},
1223 {0x31c0, 0x31e3},
1224 {0x31f0, 0x321e},
1225 {0x3220, 0x3247},
1226 {0x3250, 0x32fe},
1227 {0x3300, 0x4dbf},
1228 {0x4e00, 0xa48c},
1229 {0xa490, 0xa4c6},
1230 {0xa960, 0xa97c},
1231 {0xac00, 0xd7a3},
1232 {0xd7b0, 0xd7c6},
1233 {0xd7cb, 0xd7fb},
1234 {0xf900, 0xfaff},
1235 {0xfe10, 0xfe19},
1236 {0xfe30, 0xfe52},
1237 {0xfe54, 0xfe66},
1238 {0xfe68, 0xfe6b},
1239 {0xff01, 0xff60},
1240 {0xffe0, 0xffe6},
1241 {0x1f200, 0x1f200},
1242 {0x1f210, 0x1f231},
1243 {0x1f240, 0x1f248},
1244 {0x20000, 0x2fffd},
1245 {0x30000, 0x3fffd}
1246 };
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01001247 /* Sorted list of non-overlapping intervals of East Asian Ambiguous
1248 * characters, generated with ../runtime/tools/unicode.vim. */
1249 static struct interval ambiguous[] =
1250 {
1251 {0x00a1, 0x00a1},
1252 {0x00a4, 0x00a4},
1253 {0x00a7, 0x00a8},
1254 {0x00aa, 0x00aa},
1255 {0x00ad, 0x00ae},
1256 {0x00b0, 0x00b4},
1257 {0x00b6, 0x00ba},
1258 {0x00bc, 0x00bf},
1259 {0x00c6, 0x00c6},
1260 {0x00d0, 0x00d0},
1261 {0x00d7, 0x00d8},
1262 {0x00de, 0x00e1},
1263 {0x00e6, 0x00e6},
1264 {0x00e8, 0x00ea},
1265 {0x00ec, 0x00ed},
1266 {0x00f0, 0x00f0},
1267 {0x00f2, 0x00f3},
1268 {0x00f7, 0x00fa},
1269 {0x00fc, 0x00fc},
1270 {0x00fe, 0x00fe},
1271 {0x0101, 0x0101},
1272 {0x0111, 0x0111},
1273 {0x0113, 0x0113},
1274 {0x011b, 0x011b},
1275 {0x0126, 0x0127},
1276 {0x012b, 0x012b},
1277 {0x0131, 0x0133},
1278 {0x0138, 0x0138},
1279 {0x013f, 0x0142},
1280 {0x0144, 0x0144},
1281 {0x0148, 0x014b},
1282 {0x014d, 0x014d},
1283 {0x0152, 0x0153},
1284 {0x0166, 0x0167},
1285 {0x016b, 0x016b},
1286 {0x01ce, 0x01ce},
1287 {0x01d0, 0x01d0},
1288 {0x01d2, 0x01d2},
1289 {0x01d4, 0x01d4},
1290 {0x01d6, 0x01d6},
1291 {0x01d8, 0x01d8},
1292 {0x01da, 0x01da},
1293 {0x01dc, 0x01dc},
1294 {0x0251, 0x0251},
1295 {0x0261, 0x0261},
1296 {0x02c4, 0x02c4},
1297 {0x02c7, 0x02c7},
1298 {0x02c9, 0x02cb},
1299 {0x02cd, 0x02cd},
1300 {0x02d0, 0x02d0},
1301 {0x02d8, 0x02db},
1302 {0x02dd, 0x02dd},
1303 {0x02df, 0x02df},
1304 {0x0391, 0x03a1},
1305 {0x03a3, 0x03a9},
1306 {0x03b1, 0x03c1},
1307 {0x03c3, 0x03c9},
1308 {0x0401, 0x0401},
1309 {0x0410, 0x044f},
1310 {0x0451, 0x0451},
1311 {0x2010, 0x2010},
1312 {0x2013, 0x2016},
1313 {0x2018, 0x2019},
1314 {0x201c, 0x201d},
1315 {0x2020, 0x2022},
1316 {0x2024, 0x2027},
1317 {0x2030, 0x2030},
1318 {0x2032, 0x2033},
1319 {0x2035, 0x2035},
1320 {0x203b, 0x203b},
1321 {0x203e, 0x203e},
1322 {0x2074, 0x2074},
1323 {0x207f, 0x207f},
1324 {0x2081, 0x2084},
1325 {0x20ac, 0x20ac},
1326 {0x2103, 0x2103},
1327 {0x2105, 0x2105},
1328 {0x2109, 0x2109},
1329 {0x2113, 0x2113},
1330 {0x2116, 0x2116},
1331 {0x2121, 0x2122},
1332 {0x2126, 0x2126},
1333 {0x212b, 0x212b},
1334 {0x2153, 0x2154},
1335 {0x215b, 0x215e},
1336 {0x2160, 0x216b},
1337 {0x2170, 0x2179},
1338 {0x2189, 0x2189},
1339 {0x2190, 0x2199},
1340 {0x21b8, 0x21b9},
1341 {0x21d2, 0x21d2},
1342 {0x21d4, 0x21d4},
1343 {0x21e7, 0x21e7},
1344 {0x2200, 0x2200},
1345 {0x2202, 0x2203},
1346 {0x2207, 0x2208},
1347 {0x220b, 0x220b},
1348 {0x220f, 0x220f},
1349 {0x2211, 0x2211},
1350 {0x2215, 0x2215},
1351 {0x221a, 0x221a},
1352 {0x221d, 0x2220},
1353 {0x2223, 0x2223},
1354 {0x2225, 0x2225},
1355 {0x2227, 0x222c},
1356 {0x222e, 0x222e},
1357 {0x2234, 0x2237},
1358 {0x223c, 0x223d},
1359 {0x2248, 0x2248},
1360 {0x224c, 0x224c},
1361 {0x2252, 0x2252},
1362 {0x2260, 0x2261},
1363 {0x2264, 0x2267},
1364 {0x226a, 0x226b},
1365 {0x226e, 0x226f},
1366 {0x2282, 0x2283},
1367 {0x2286, 0x2287},
1368 {0x2295, 0x2295},
1369 {0x2299, 0x2299},
1370 {0x22a5, 0x22a5},
1371 {0x22bf, 0x22bf},
1372 {0x2312, 0x2312},
1373 {0x2460, 0x24e9},
1374 {0x24eb, 0x254b},
1375 {0x2550, 0x2573},
1376 {0x2580, 0x258f},
1377 {0x2592, 0x2595},
1378 {0x25a0, 0x25a1},
1379 {0x25a3, 0x25a9},
1380 {0x25b2, 0x25b3},
1381 {0x25b6, 0x25b7},
1382 {0x25bc, 0x25bd},
1383 {0x25c0, 0x25c1},
1384 {0x25c6, 0x25c8},
1385 {0x25cb, 0x25cb},
1386 {0x25ce, 0x25d1},
1387 {0x25e2, 0x25e5},
1388 {0x25ef, 0x25ef},
1389 {0x2605, 0x2606},
1390 {0x2609, 0x2609},
1391 {0x260e, 0x260f},
1392 {0x2614, 0x2615},
1393 {0x261c, 0x261c},
1394 {0x261e, 0x261e},
1395 {0x2640, 0x2640},
1396 {0x2642, 0x2642},
1397 {0x2660, 0x2661},
1398 {0x2663, 0x2665},
1399 {0x2667, 0x266a},
1400 {0x266c, 0x266d},
1401 {0x266f, 0x266f},
1402 {0x269e, 0x269f},
1403 {0x26be, 0x26bf},
1404 {0x26c4, 0x26cd},
1405 {0x26cf, 0x26e1},
1406 {0x26e3, 0x26e3},
1407 {0x26e8, 0x26ff},
1408 {0x273d, 0x273d},
1409 {0x2757, 0x2757},
1410 {0x2776, 0x277f},
1411 {0x2b55, 0x2b59},
1412 {0x3248, 0x324f},
1413 {0xe000, 0xf8ff},
1414 {0xfffd, 0xfffd},
1415 {0x1f100, 0x1f10a},
1416 {0x1f110, 0x1f12d},
1417 {0x1f131, 0x1f131},
1418 {0x1f13d, 0x1f13d},
1419 {0x1f13f, 0x1f13f},
1420 {0x1f142, 0x1f142},
1421 {0x1f146, 0x1f146},
1422 {0x1f14a, 0x1f14e},
1423 {0x1f157, 0x1f157},
1424 {0x1f15f, 0x1f15f},
1425 {0x1f179, 0x1f179},
1426 {0x1f17b, 0x1f17c},
1427 {0x1f17f, 0x1f17f},
1428 {0x1f18a, 0x1f18d},
1429 {0x1f190, 0x1f190},
1430 {0xf0000, 0xffffd},
1431 {0x100000, 0x10fffd}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 };
1433
1434 if (c >= 0x100)
1435 {
1436#ifdef USE_WCHAR_FUNCTIONS
1437 /*
1438 * Assume the library function wcwidth() works better than our own
1439 * stuff. It should return 1 for ambiguous width chars!
1440 */
1441 int n = wcwidth(c);
1442
1443 if (n < 0)
1444 return 6; /* unprintable, displays <xxxx> */
1445 if (n > 1)
1446 return n;
1447#else
1448 if (!utf_printable(c))
1449 return 6; /* unprintable, displays <xxxx> */
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001450 if (intable(doublewidth, sizeof(doublewidth), c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return 2;
1452#endif
1453 }
1454
1455 /* Characters below 0x100 are influenced by 'isprint' option */
1456 else if (c >= 0x80 && !vim_isprintc(c))
1457 return 4; /* unprintable, displays <xx> */
1458
1459 if (c >= 0x80 && *p_ambw == 'd' && intable(ambiguous, sizeof(ambiguous), c))
1460 return 2;
1461
1462 return 1;
1463}
1464
1465/*
1466 * mb_ptr2cells() function pointer.
1467 * Return the number of display cells character at "*p" occupies.
1468 * This doesn't take care of unprintable characters, use ptr2cells() for that.
1469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 int
1471latin_ptr2cells(p)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001472 char_u *p UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
1474 return 1;
1475}
1476
1477 int
1478utf_ptr2cells(p)
1479 char_u *p;
1480{
1481 int c;
1482
1483 /* Need to convert to a wide character. */
1484 if (*p >= 0x80)
1485 {
1486 c = utf_ptr2char(p);
1487 /* An illegal byte is displayed as <xx>. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001488 if (utf_ptr2len(p) == 1 || c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 return 4;
1490 /* If the char is ASCII it must be an overlong sequence. */
1491 if (c < 0x80)
1492 return char2cells(c);
1493 return utf_char2cells(c);
1494 }
1495 return 1;
1496}
1497
1498 int
1499dbcs_ptr2cells(p)
1500 char_u *p;
1501{
1502 /* Number of cells is equal to number of bytes, except for euc-jp when
1503 * the first byte is 0x8e. */
1504 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
1505 return 1;
1506 return MB_BYTE2LEN(*p);
1507}
1508
1509/*
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001510 * mb_ptr2cells_len() function pointer.
1511 * Like mb_ptr2cells(), but limit string length to "size".
1512 * For an empty string or truncated character returns 1.
1513 */
1514 int
1515latin_ptr2cells_len(p, size)
1516 char_u *p UNUSED;
1517 int size UNUSED;
1518{
1519 return 1;
1520}
1521
1522 static int
1523utf_ptr2cells_len(p, size)
1524 char_u *p;
1525 int size;
1526{
1527 int c;
1528
1529 /* Need to convert to a wide character. */
1530 if (size > 0 && *p >= 0x80)
1531 {
1532 if (utf_ptr2len_len(p, size) < utf8len_tab[*p])
Bram Moolenaar24397332009-12-02 14:02:39 +00001533 return 1; /* truncated */
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001534 c = utf_ptr2char(p);
1535 /* An illegal byte is displayed as <xx>. */
1536 if (utf_ptr2len(p) == 1 || c == NUL)
1537 return 4;
1538 /* If the char is ASCII it must be an overlong sequence. */
1539 if (c < 0x80)
1540 return char2cells(c);
1541 return utf_char2cells(c);
1542 }
1543 return 1;
1544}
1545
1546 static int
1547dbcs_ptr2cells_len(p, size)
1548 char_u *p;
1549 int size;
1550{
1551 /* Number of cells is equal to number of bytes, except for euc-jp when
1552 * the first byte is 0x8e. */
1553 if (size <= 1 || (enc_dbcs == DBCS_JPNU && *p == 0x8e))
1554 return 1;
1555 return MB_BYTE2LEN(*p);
1556}
1557
1558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 * mb_char2cells() function pointer.
1560 * Return the number of display cells character "c" occupies.
1561 * Only takes care of multi-byte chars, not "^C" and such.
1562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 int
1564latin_char2cells(c)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001565 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
1567 return 1;
1568}
1569
1570 static int
1571dbcs_char2cells(c)
1572 int c;
1573{
1574 /* Number of cells is equal to number of bytes, except for euc-jp when
1575 * the first byte is 0x8e. */
1576 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
1577 return 1;
1578 /* use the first byte */
1579 return MB_BYTE2LEN((unsigned)c >> 8);
1580}
1581
1582/*
Bram Moolenaar72597a52010-07-18 15:31:08 +02001583 * Return the number of cells occupied by string "p".
1584 * Stop at a NUL character. When "len" >= 0 stop at character "p[len]".
1585 */
1586 int
1587mb_string2cells(p, len)
1588 char_u *p;
1589 int len;
1590{
1591 int i;
1592 int clen = 0;
1593
1594 for (i = 0; (len < 0 || i < len) && p[i] != NUL; i += (*mb_ptr2len)(p + i))
1595 clen += (*mb_ptr2cells)(p + i);
1596 return clen;
1597}
1598
1599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 * mb_off2cells() function pointer.
1601 * Return number of display cells for char at ScreenLines[off].
Bram Moolenaar367329b2007-08-30 11:53:22 +00001602 * We make sure that the offset used is less than "max_off".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001605latin_off2cells(off, max_off)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001606 unsigned off UNUSED;
1607 unsigned max_off UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
1609 return 1;
1610}
1611
1612 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001613dbcs_off2cells(off, max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 unsigned off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001615 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001617 /* never check beyond end of the line */
1618 if (off >= max_off)
1619 return 1;
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* Number of cells is equal to number of bytes, except for euc-jp when
1622 * the first byte is 0x8e. */
1623 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1624 return 1;
1625 return MB_BYTE2LEN(ScreenLines[off]);
1626}
1627
1628 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001629utf_off2cells(off, max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 unsigned off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001631 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001633 return (off + 1 < max_off && ScreenLines[off + 1] == 0) ? 2 : 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634}
1635
1636/*
1637 * mb_ptr2char() function pointer.
1638 * Convert a byte sequence into a character.
1639 */
1640 int
1641latin_ptr2char(p)
1642 char_u *p;
1643{
1644 return *p;
1645}
1646
1647 static int
1648dbcs_ptr2char(p)
1649 char_u *p;
1650{
1651 if (MB_BYTE2LEN(*p) > 1 && p[1] != NUL)
1652 return (p[0] << 8) + p[1];
1653 return *p;
1654}
1655
1656/*
1657 * Convert a UTF-8 byte sequence to a wide character.
1658 * If the sequence is illegal or truncated by a NUL the first byte is
1659 * returned.
1660 * Does not include composing characters, of course.
1661 */
1662 int
1663utf_ptr2char(p)
1664 char_u *p;
1665{
1666 int len;
1667
1668 if (p[0] < 0x80) /* be quick for ASCII */
1669 return p[0];
1670
Bram Moolenaar24397332009-12-02 14:02:39 +00001671 len = utf8len_tab_zero[p[0]];
Bram Moolenaar89bf0922008-06-29 14:16:06 +00001672 if (len > 1 && (p[1] & 0xc0) == 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 if (len == 2)
1675 return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
1676 if ((p[2] & 0xc0) == 0x80)
1677 {
1678 if (len == 3)
1679 return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
1680 + (p[2] & 0x3f);
1681 if ((p[3] & 0xc0) == 0x80)
1682 {
1683 if (len == 4)
1684 return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
1685 + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
1686 if ((p[4] & 0xc0) == 0x80)
1687 {
1688 if (len == 5)
1689 return ((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
1690 + ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
1691 + (p[4] & 0x3f);
1692 if ((p[5] & 0xc0) == 0x80 && len == 6)
1693 return ((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
1694 + ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
1695 + ((p[4] & 0x3f) << 6) + (p[5] & 0x3f);
1696 }
1697 }
1698 }
1699 }
1700 /* Illegal value, just return the first byte */
1701 return p[0];
1702}
1703
1704/*
Bram Moolenaar35ee4522011-07-15 21:16:59 +02001705 * Convert a UTF-8 byte sequence to a wide character.
1706 * String is assumed to be terminated by NUL or after "n" bytes, whichever
1707 * comes first.
1708 * The function is safe in the sense that it never accesses memory beyond the
1709 * first "n" bytes of "s".
1710 *
1711 * On success, returns decoded codepoint, advances "s" to the beginning of
1712 * next character and decreases "n" accordingly.
1713 *
1714 * If end of string was reached, returns 0 and, if "n" > 0, advances "s" past
1715 * NUL byte.
1716 *
1717 * If byte sequence is illegal or incomplete, returns -1 and does not advance
1718 * "s".
1719 */
1720 static int
1721utf_safe_read_char_adv(s, n)
1722 char_u **s;
1723 size_t *n;
1724{
1725 int c, k;
1726
1727 if (*n == 0) /* end of buffer */
1728 return 0;
1729
1730 k = utf8len_tab_zero[**s];
1731
1732 if (k == 1)
1733 {
1734 /* ASCII character or NUL */
1735 (*n)--;
1736 return *(*s)++;
1737 }
1738
1739 if ((size_t)k <= *n)
1740 {
1741 /* We have a multibyte sequence and it isn't truncated by buffer
1742 * limits so utf_ptr2char() is safe to use. Or the first byte is
1743 * illegal (k=0), and it's also safe to use utf_ptr2char(). */
1744 c = utf_ptr2char(*s);
1745
1746 /* On failure, utf_ptr2char() returns the first byte, so here we
1747 * check equality with the first byte. The only non-ASCII character
1748 * which equals the first byte of its own UTF-8 representation is
1749 * U+00C3 (UTF-8: 0xC3 0x83), so need to check that special case too.
1750 * It's safe even if n=1, else we would have k=2 > n. */
1751 if (c != (int)(**s) || (c == 0xC3 && (*s)[1] == 0x83))
1752 {
1753 /* byte sequence was successfully decoded */
1754 *s += k;
1755 *n -= k;
1756 return c;
1757 }
1758 }
1759
1760 /* byte sequence is incomplete or illegal */
1761 return -1;
1762}
1763
1764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 * Get character at **pp and advance *pp to the next character.
1766 * Note: composing characters are skipped!
1767 */
1768 int
1769mb_ptr2char_adv(pp)
1770 char_u **pp;
1771{
1772 int c;
1773
1774 c = (*mb_ptr2char)(*pp);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001775 *pp += (*mb_ptr2len)(*pp);
1776 return c;
1777}
1778
1779/*
1780 * Get character at **pp and advance *pp to the next character.
1781 * Note: composing characters are returned as separate characters.
1782 */
1783 int
1784mb_cptr2char_adv(pp)
1785 char_u **pp;
1786{
1787 int c;
1788
1789 c = (*mb_ptr2char)(*pp);
1790 if (enc_utf8)
1791 *pp += utf_ptr2len(*pp);
1792 else
1793 *pp += (*mb_ptr2len)(*pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return c;
1795}
1796
1797#if defined(FEAT_ARABIC) || defined(PROTO)
1798/*
1799 * Check whether we are dealing with Arabic combining characters.
1800 * Note: these are NOT really composing characters!
1801 */
1802 int
1803arabic_combine(one, two)
1804 int one; /* first character */
1805 int two; /* character just after "one" */
1806{
1807 if (one == a_LAM)
1808 return arabic_maycombine(two);
1809 return FALSE;
1810}
1811
1812/*
1813 * Check whether we are dealing with a character that could be regarded as an
1814 * Arabic combining character, need to check the character before this.
1815 */
1816 int
1817arabic_maycombine(two)
1818 int two;
1819{
1820 if (p_arshape && !p_tbidi)
1821 return (two == a_ALEF_MADDA
1822 || two == a_ALEF_HAMZA_ABOVE
1823 || two == a_ALEF_HAMZA_BELOW
1824 || two == a_ALEF);
1825 return FALSE;
1826}
1827
1828/*
1829 * Check if the character pointed to by "p2" is a composing character when it
1830 * comes after "p1". For Arabic sometimes "ab" is replaced with "c", which
1831 * behaves like a composing character.
1832 */
1833 int
1834utf_composinglike(p1, p2)
1835 char_u *p1;
1836 char_u *p2;
1837{
1838 int c2;
1839
1840 c2 = utf_ptr2char(p2);
1841 if (utf_iscomposing(c2))
1842 return TRUE;
1843 if (!arabic_maycombine(c2))
1844 return FALSE;
1845 return arabic_combine(utf_ptr2char(p1), c2);
1846}
1847#endif
1848
1849/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001850 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 * composing characters.
1852 */
1853 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001854utfc_ptr2char(p, pcc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001856 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int len;
1859 int c;
1860 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001861 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001864 len = utf_ptr2len(p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 /* Only accept a composing char when the first char isn't illegal. */
1867 if ((len > 1 || *p < 0x80)
1868 && p[len] >= 0x80
1869 && UTF_COMPOSINGLIKE(p, p + len))
1870 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001871 cc = utf_ptr2char(p + len);
1872 for (;;)
1873 {
1874 pcc[i++] = cc;
1875 if (i == MAX_MCO)
1876 break;
1877 len += utf_ptr2len(p + len);
1878 if (p[len] < 0x80 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1879 break;
1880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001882
1883 if (i < MAX_MCO) /* last composing char must be 0 */
1884 pcc[i] = 0;
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 return c;
1887}
1888
1889/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001890 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 * composing characters. Use no more than p[maxlen].
1892 */
1893 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001894utfc_ptr2char_len(p, pcc, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001896 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int maxlen;
1898{
1899 int len;
1900 int c;
1901 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001902 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001905 len = utf_ptr2len_len(p, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 /* Only accept a composing char when the first char isn't illegal. */
1907 if ((len > 1 || *p < 0x80)
1908 && len < maxlen
1909 && p[len] >= 0x80
1910 && UTF_COMPOSINGLIKE(p, p + len))
1911 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001912 cc = utf_ptr2char(p + len);
1913 for (;;)
1914 {
1915 pcc[i++] = cc;
1916 if (i == MAX_MCO)
1917 break;
1918 len += utf_ptr2len_len(p + len, maxlen - len);
1919 if (len >= maxlen
1920 || p[len] < 0x80
1921 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1922 break;
1923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001925
1926 if (i < MAX_MCO) /* last composing char must be 0 */
1927 pcc[i] = 0;
1928
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return c;
1930}
1931
1932/*
1933 * Convert the character at screen position "off" to a sequence of bytes.
1934 * Includes the composing characters.
1935 * "buf" must at least have the length MB_MAXBYTES.
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02001936 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 * Returns the produced number of bytes.
1938 */
1939 int
1940utfc_char2bytes(off, buf)
1941 int off;
1942 char_u *buf;
1943{
1944 int len;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001945 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 len = utf_char2bytes(ScreenLinesUC[off], buf);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001948 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001950 if (ScreenLinesC[i][off] == 0)
1951 break;
1952 len += utf_char2bytes(ScreenLinesC[i][off], buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 return len;
1955}
1956
1957/*
1958 * Get the length of a UTF-8 byte sequence, not including any following
1959 * composing characters.
1960 * Returns 0 for "".
1961 * Returns 1 for an illegal byte sequence.
1962 */
1963 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001964utf_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 char_u *p;
1966{
1967 int len;
1968 int i;
1969
1970 if (*p == NUL)
1971 return 0;
1972 len = utf8len_tab[*p];
1973 for (i = 1; i < len; ++i)
1974 if ((p[i] & 0xc0) != 0x80)
1975 return 1;
1976 return len;
1977}
1978
1979/*
1980 * Return length of UTF-8 character, obtained from the first byte.
1981 * "b" must be between 0 and 255!
Bram Moolenaar24397332009-12-02 14:02:39 +00001982 * Returns 1 for an invalid first byte value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 */
1984 int
1985utf_byte2len(b)
1986 int b;
1987{
1988 return utf8len_tab[b];
1989}
1990
1991/*
1992 * Get the length of UTF-8 byte sequence "p[size]". Does not include any
1993 * following composing characters.
1994 * Returns 1 for "".
Bram Moolenaarc048f672008-01-04 16:47:25 +00001995 * Returns 1 for an illegal byte sequence (also in incomplete byte seq.).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 * Returns number > "size" for an incomplete byte sequence.
Bram Moolenaar24397332009-12-02 14:02:39 +00001997 * Never returns zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 */
1999 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002000utf_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 char_u *p;
2002 int size;
2003{
2004 int len;
2005 int i;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002006 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
Bram Moolenaar24397332009-12-02 14:02:39 +00002008 len = utf8len_tab[*p];
2009 if (len == 1)
2010 return 1; /* NUL, ascii or illegal lead byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (len > size)
Bram Moolenaarc048f672008-01-04 16:47:25 +00002012 m = size; /* incomplete byte sequence. */
Bram Moolenaar24397332009-12-02 14:02:39 +00002013 else
2014 m = len;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002015 for (i = 1; i < m; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if ((p[i] & 0xc0) != 0x80)
2017 return 1;
2018 return len;
2019}
2020
2021/*
2022 * Return the number of bytes the UTF-8 encoding of the character at "p" takes.
2023 * This includes following composing characters.
2024 */
2025 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002026utfc_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 char_u *p;
2028{
2029 int len;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002030 int b0 = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031#ifdef FEAT_ARABIC
2032 int prevlen;
2033#endif
2034
Bram Moolenaarc669e662005-06-08 22:00:03 +00002035 if (b0 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 return 0;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002037 if (b0 < 0x80 && p[1] < 0x80) /* be quick for ASCII */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 return 1;
2039
2040 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002041 len = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 /* Check for illegal byte. */
Bram Moolenaarc669e662005-06-08 22:00:03 +00002044 if (len == 1 && b0 >= 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 return 1;
2046
2047 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002048 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 * skip all of them (otherwise the cursor would get stuck).
2050 */
2051#ifdef FEAT_ARABIC
2052 prevlen = 0;
2053#endif
2054 for (;;)
2055 {
2056 if (p[len] < 0x80 || !UTF_COMPOSINGLIKE(p + prevlen, p + len))
2057 return len;
2058
2059 /* Skip over composing char */
2060#ifdef FEAT_ARABIC
2061 prevlen = len;
2062#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002063 len += utf_ptr2len(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065}
2066
2067/*
2068 * Return the number of bytes the UTF-8 encoding of the character at "p[size]"
2069 * takes. This includes following composing characters.
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002070 * Returns 0 for an empty string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 * Returns 1 for an illegal char or an incomplete byte sequence.
2072 */
2073 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002074utfc_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 char_u *p;
2076 int size;
2077{
2078 int len;
2079#ifdef FEAT_ARABIC
2080 int prevlen;
2081#endif
2082
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002083 if (size < 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 return 0;
2085 if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */
2086 return 1;
2087
2088 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002089 len = utf_ptr2len_len(p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /* Check for illegal byte and incomplete byte sequence. */
2092 if ((len == 1 && p[0] >= 0x80) || len > size)
2093 return 1;
2094
2095 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002096 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 * skip all of them (otherwise the cursor would get stuck).
2098 */
2099#ifdef FEAT_ARABIC
2100 prevlen = 0;
2101#endif
2102 while (len < size)
2103 {
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002104 int len_next_char;
2105
2106 if (p[len] < 0x80)
2107 break;
2108
2109 /*
2110 * Next character length should not go beyond size to ensure that
2111 * UTF_COMPOSINGLIKE(...) does not read beyond size.
2112 */
2113 len_next_char = utf_ptr2len_len(p + len, size - len);
2114 if (len_next_char > size - len)
2115 break;
2116
2117 if (!UTF_COMPOSINGLIKE(p + prevlen, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 break;
2119
2120 /* Skip over composing char */
2121#ifdef FEAT_ARABIC
2122 prevlen = len;
2123#endif
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002124 len += len_next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 return len;
2127}
2128
2129/*
2130 * Return the number of bytes the UTF-8 encoding of character "c" takes.
2131 * This does not include composing characters.
2132 */
2133 int
2134utf_char2len(c)
2135 int c;
2136{
2137 if (c < 0x80)
2138 return 1;
2139 if (c < 0x800)
2140 return 2;
2141 if (c < 0x10000)
2142 return 3;
2143 if (c < 0x200000)
2144 return 4;
2145 if (c < 0x4000000)
2146 return 5;
2147 return 6;
2148}
2149
2150/*
2151 * Convert Unicode character "c" to UTF-8 string in "buf[]".
2152 * Returns the number of bytes.
2153 * This does not include composing characters.
2154 */
2155 int
2156utf_char2bytes(c, buf)
2157 int c;
2158 char_u *buf;
2159{
2160 if (c < 0x80) /* 7 bits */
2161 {
2162 buf[0] = c;
2163 return 1;
2164 }
2165 if (c < 0x800) /* 11 bits */
2166 {
2167 buf[0] = 0xc0 + ((unsigned)c >> 6);
2168 buf[1] = 0x80 + (c & 0x3f);
2169 return 2;
2170 }
2171 if (c < 0x10000) /* 16 bits */
2172 {
2173 buf[0] = 0xe0 + ((unsigned)c >> 12);
2174 buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2175 buf[2] = 0x80 + (c & 0x3f);
2176 return 3;
2177 }
2178 if (c < 0x200000) /* 21 bits */
2179 {
2180 buf[0] = 0xf0 + ((unsigned)c >> 18);
2181 buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2182 buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2183 buf[3] = 0x80 + (c & 0x3f);
2184 return 4;
2185 }
2186 if (c < 0x4000000) /* 26 bits */
2187 {
2188 buf[0] = 0xf8 + ((unsigned)c >> 24);
2189 buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2190 buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2191 buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2192 buf[4] = 0x80 + (c & 0x3f);
2193 return 5;
2194 }
2195 /* 31 bits */
2196 buf[0] = 0xfc + ((unsigned)c >> 30);
2197 buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f);
2198 buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2199 buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2200 buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2201 buf[5] = 0x80 + (c & 0x3f);
2202 return 6;
2203}
2204
2205/*
2206 * Return TRUE if "c" is a composing UTF-8 character. This means it will be
2207 * drawn on top of the preceding character.
2208 * Based on code from Markus Kuhn.
2209 */
2210 int
2211utf_iscomposing(c)
2212 int c;
2213{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002214 /* Sorted list of non-overlapping intervals.
2215 * Generated by ../runtime/tools/unicode.vim. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 static struct interval combining[] =
2217 {
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002218 {0x0300, 0x036f},
2219 {0x0483, 0x0489},
2220 {0x0591, 0x05bd},
2221 {0x05bf, 0x05bf},
2222 {0x05c1, 0x05c2},
2223 {0x05c4, 0x05c5},
2224 {0x05c7, 0x05c7},
2225 {0x0610, 0x061a},
2226 {0x064b, 0x065e},
2227 {0x0670, 0x0670},
2228 {0x06d6, 0x06dc},
2229 {0x06de, 0x06e4},
2230 {0x06e7, 0x06e8},
2231 {0x06ea, 0x06ed},
2232 {0x0711, 0x0711},
2233 {0x0730, 0x074a},
2234 {0x07a6, 0x07b0},
2235 {0x07eb, 0x07f3},
2236 {0x0816, 0x0819},
2237 {0x081b, 0x0823},
2238 {0x0825, 0x0827},
2239 {0x0829, 0x082d},
2240 {0x0900, 0x0903},
2241 {0x093c, 0x093c},
2242 {0x093e, 0x094e},
2243 {0x0951, 0x0955},
2244 {0x0962, 0x0963},
2245 {0x0981, 0x0983},
2246 {0x09bc, 0x09bc},
2247 {0x09be, 0x09c4},
2248 {0x09c7, 0x09c8},
2249 {0x09cb, 0x09cd},
2250 {0x09d7, 0x09d7},
2251 {0x09e2, 0x09e3},
2252 {0x0a01, 0x0a03},
2253 {0x0a3c, 0x0a3c},
2254 {0x0a3e, 0x0a42},
2255 {0x0a47, 0x0a48},
2256 {0x0a4b, 0x0a4d},
2257 {0x0a51, 0x0a51},
2258 {0x0a70, 0x0a71},
2259 {0x0a75, 0x0a75},
2260 {0x0a81, 0x0a83},
2261 {0x0abc, 0x0abc},
2262 {0x0abe, 0x0ac5},
2263 {0x0ac7, 0x0ac9},
2264 {0x0acb, 0x0acd},
2265 {0x0ae2, 0x0ae3},
2266 {0x0b01, 0x0b03},
2267 {0x0b3c, 0x0b3c},
2268 {0x0b3e, 0x0b44},
2269 {0x0b47, 0x0b48},
2270 {0x0b4b, 0x0b4d},
2271 {0x0b56, 0x0b57},
2272 {0x0b62, 0x0b63},
2273 {0x0b82, 0x0b82},
2274 {0x0bbe, 0x0bc2},
2275 {0x0bc6, 0x0bc8},
2276 {0x0bca, 0x0bcd},
2277 {0x0bd7, 0x0bd7},
2278 {0x0c01, 0x0c03},
2279 {0x0c3e, 0x0c44},
2280 {0x0c46, 0x0c48},
2281 {0x0c4a, 0x0c4d},
2282 {0x0c55, 0x0c56},
2283 {0x0c62, 0x0c63},
2284 {0x0c82, 0x0c83},
2285 {0x0cbc, 0x0cbc},
2286 {0x0cbe, 0x0cc4},
2287 {0x0cc6, 0x0cc8},
2288 {0x0cca, 0x0ccd},
2289 {0x0cd5, 0x0cd6},
2290 {0x0ce2, 0x0ce3},
2291 {0x0d02, 0x0d03},
2292 {0x0d3e, 0x0d44},
2293 {0x0d46, 0x0d48},
2294 {0x0d4a, 0x0d4d},
2295 {0x0d57, 0x0d57},
2296 {0x0d62, 0x0d63},
2297 {0x0d82, 0x0d83},
2298 {0x0dca, 0x0dca},
2299 {0x0dcf, 0x0dd4},
2300 {0x0dd6, 0x0dd6},
2301 {0x0dd8, 0x0ddf},
2302 {0x0df2, 0x0df3},
2303 {0x0e31, 0x0e31},
2304 {0x0e34, 0x0e3a},
2305 {0x0e47, 0x0e4e},
2306 {0x0eb1, 0x0eb1},
2307 {0x0eb4, 0x0eb9},
2308 {0x0ebb, 0x0ebc},
2309 {0x0ec8, 0x0ecd},
2310 {0x0f18, 0x0f19},
2311 {0x0f35, 0x0f35},
2312 {0x0f37, 0x0f37},
2313 {0x0f39, 0x0f39},
2314 {0x0f3e, 0x0f3f},
2315 {0x0f71, 0x0f84},
2316 {0x0f86, 0x0f87},
2317 {0x0f90, 0x0f97},
2318 {0x0f99, 0x0fbc},
2319 {0x0fc6, 0x0fc6},
2320 {0x102b, 0x103e},
2321 {0x1056, 0x1059},
2322 {0x105e, 0x1060},
2323 {0x1062, 0x1064},
2324 {0x1067, 0x106d},
2325 {0x1071, 0x1074},
2326 {0x1082, 0x108d},
2327 {0x108f, 0x108f},
2328 {0x109a, 0x109d},
2329 {0x135f, 0x135f},
2330 {0x1712, 0x1714},
2331 {0x1732, 0x1734},
2332 {0x1752, 0x1753},
2333 {0x1772, 0x1773},
2334 {0x17b6, 0x17d3},
2335 {0x17dd, 0x17dd},
2336 {0x180b, 0x180d},
2337 {0x18a9, 0x18a9},
2338 {0x1920, 0x192b},
2339 {0x1930, 0x193b},
2340 {0x19b0, 0x19c0},
2341 {0x19c8, 0x19c9},
2342 {0x1a17, 0x1a1b},
2343 {0x1a55, 0x1a5e},
2344 {0x1a60, 0x1a7c},
2345 {0x1a7f, 0x1a7f},
2346 {0x1b00, 0x1b04},
2347 {0x1b34, 0x1b44},
2348 {0x1b6b, 0x1b73},
2349 {0x1b80, 0x1b82},
2350 {0x1ba1, 0x1baa},
2351 {0x1c24, 0x1c37},
2352 {0x1cd0, 0x1cd2},
2353 {0x1cd4, 0x1ce8},
2354 {0x1ced, 0x1ced},
2355 {0x1cf2, 0x1cf2},
2356 {0x1dc0, 0x1de6},
2357 {0x1dfd, 0x1dff},
2358 {0x20d0, 0x20f0},
2359 {0x2cef, 0x2cf1},
2360 {0x2de0, 0x2dff},
2361 {0x302a, 0x302f},
2362 {0x3099, 0x309a},
2363 {0xa66f, 0xa672},
2364 {0xa67c, 0xa67d},
2365 {0xa6f0, 0xa6f1},
2366 {0xa802, 0xa802},
2367 {0xa806, 0xa806},
2368 {0xa80b, 0xa80b},
2369 {0xa823, 0xa827},
2370 {0xa880, 0xa881},
2371 {0xa8b4, 0xa8c4},
2372 {0xa8e0, 0xa8f1},
2373 {0xa926, 0xa92d},
2374 {0xa947, 0xa953},
2375 {0xa980, 0xa983},
2376 {0xa9b3, 0xa9c0},
2377 {0xaa29, 0xaa36},
2378 {0xaa43, 0xaa43},
2379 {0xaa4c, 0xaa4d},
2380 {0xaa7b, 0xaa7b},
2381 {0xaab0, 0xaab0},
2382 {0xaab2, 0xaab4},
2383 {0xaab7, 0xaab8},
2384 {0xaabe, 0xaabf},
2385 {0xaac1, 0xaac1},
2386 {0xabe3, 0xabea},
2387 {0xabec, 0xabed},
2388 {0xfb1e, 0xfb1e},
2389 {0xfe00, 0xfe0f},
2390 {0xfe20, 0xfe26},
2391 {0x101fd, 0x101fd},
2392 {0x10a01, 0x10a03},
2393 {0x10a05, 0x10a06},
2394 {0x10a0c, 0x10a0f},
2395 {0x10a38, 0x10a3a},
2396 {0x10a3f, 0x10a3f},
2397 {0x11080, 0x11082},
2398 {0x110b0, 0x110ba},
2399 {0x1d165, 0x1d169},
2400 {0x1d16d, 0x1d172},
2401 {0x1d17b, 0x1d182},
2402 {0x1d185, 0x1d18b},
2403 {0x1d1aa, 0x1d1ad},
2404 {0x1d242, 0x1d244},
2405 {0xe0100, 0xe01ef}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 };
2407
2408 return intable(combining, sizeof(combining), c);
2409}
2410
2411/*
2412 * Return TRUE for characters that can be displayed in a normal way.
2413 * Only for characters of 0x100 and above!
2414 */
2415 int
2416utf_printable(c)
2417 int c;
2418{
2419#ifdef USE_WCHAR_FUNCTIONS
2420 /*
2421 * Assume the iswprint() library function works better than our own stuff.
2422 */
2423 return iswprint(c);
2424#else
2425 /* Sorted list of non-overlapping intervals.
2426 * 0xd800-0xdfff is reserved for UTF-16, actually illegal. */
2427 static struct interval nonprint[] =
2428 {
2429 {0x070f, 0x070f}, {0x180b, 0x180e}, {0x200b, 0x200f}, {0x202a, 0x202e},
2430 {0x206a, 0x206f}, {0xd800, 0xdfff}, {0xfeff, 0xfeff}, {0xfff9, 0xfffb},
2431 {0xfffe, 0xffff}
2432 };
2433
2434 return !intable(nonprint, sizeof(nonprint), c);
2435#endif
2436}
2437
2438/*
2439 * Get class of a Unicode character.
2440 * 0: white space
2441 * 1: punctuation
2442 * 2 or bigger: some class of word character.
2443 */
2444 int
2445utf_class(c)
2446 int c;
2447{
2448 /* sorted list of non-overlapping intervals */
2449 static struct clinterval
2450 {
2451 unsigned short first;
2452 unsigned short last;
2453 unsigned short class;
2454 } classes[] =
2455 {
2456 {0x037e, 0x037e, 1}, /* Greek question mark */
2457 {0x0387, 0x0387, 1}, /* Greek ano teleia */
2458 {0x055a, 0x055f, 1}, /* Armenian punctuation */
2459 {0x0589, 0x0589, 1}, /* Armenian full stop */
2460 {0x05be, 0x05be, 1},
2461 {0x05c0, 0x05c0, 1},
2462 {0x05c3, 0x05c3, 1},
2463 {0x05f3, 0x05f4, 1},
2464 {0x060c, 0x060c, 1},
2465 {0x061b, 0x061b, 1},
2466 {0x061f, 0x061f, 1},
2467 {0x066a, 0x066d, 1},
2468 {0x06d4, 0x06d4, 1},
2469 {0x0700, 0x070d, 1}, /* Syriac punctuation */
2470 {0x0964, 0x0965, 1},
2471 {0x0970, 0x0970, 1},
2472 {0x0df4, 0x0df4, 1},
2473 {0x0e4f, 0x0e4f, 1},
2474 {0x0e5a, 0x0e5b, 1},
2475 {0x0f04, 0x0f12, 1},
2476 {0x0f3a, 0x0f3d, 1},
2477 {0x0f85, 0x0f85, 1},
2478 {0x104a, 0x104f, 1}, /* Myanmar punctuation */
2479 {0x10fb, 0x10fb, 1}, /* Georgian punctuation */
2480 {0x1361, 0x1368, 1}, /* Ethiopic punctuation */
2481 {0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
2482 {0x1680, 0x1680, 0},
2483 {0x169b, 0x169c, 1},
2484 {0x16eb, 0x16ed, 1},
2485 {0x1735, 0x1736, 1},
2486 {0x17d4, 0x17dc, 1}, /* Khmer punctuation */
2487 {0x1800, 0x180a, 1}, /* Mongolian punctuation */
2488 {0x2000, 0x200b, 0}, /* spaces */
2489 {0x200c, 0x2027, 1}, /* punctuation and symbols */
2490 {0x2028, 0x2029, 0},
2491 {0x202a, 0x202e, 1}, /* punctuation and symbols */
2492 {0x202f, 0x202f, 0},
2493 {0x2030, 0x205e, 1}, /* punctuation and symbols */
2494 {0x205f, 0x205f, 0},
2495 {0x2060, 0x27ff, 1}, /* punctuation and symbols */
2496 {0x2070, 0x207f, 0x2070}, /* superscript */
Bram Moolenaarbbb79722008-06-04 09:00:32 +00002497 {0x2080, 0x2094, 0x2080}, /* subscript */
2498 {0x20a0, 0x27ff, 1}, /* all kinds of symbols */
2499 {0x2800, 0x28ff, 0x2800}, /* braille */
2500 {0x2900, 0x2998, 1}, /* arrows, brackets, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {0x29d8, 0x29db, 1},
2502 {0x29fc, 0x29fd, 1},
2503 {0x3000, 0x3000, 0}, /* ideographic space */
2504 {0x3001, 0x3020, 1}, /* ideographic punctuation */
2505 {0x3030, 0x3030, 1},
2506 {0x303d, 0x303d, 1},
2507 {0x3040, 0x309f, 0x3040}, /* Hiragana */
2508 {0x30a0, 0x30ff, 0x30a0}, /* Katakana */
2509 {0x3300, 0x9fff, 0x4e00}, /* CJK Ideographs */
2510 {0xac00, 0xd7a3, 0xac00}, /* Hangul Syllables */
2511 {0xf900, 0xfaff, 0x4e00}, /* CJK Ideographs */
2512 {0xfd3e, 0xfd3f, 1},
2513 {0xfe30, 0xfe6b, 1}, /* punctuation forms */
2514 {0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
2515 {0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
2516 {0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
2517 {0xff5b, 0xff65, 1}, /* half/fullwidth ASCII */
2518 };
2519 int bot = 0;
2520 int top = sizeof(classes) / sizeof(struct clinterval) - 1;
2521 int mid;
2522
2523 /* First quick check for Latin1 characters, use 'iskeyword'. */
2524 if (c < 0x100)
2525 {
Bram Moolenaarf7bbbc52005-06-17 21:55:00 +00002526 if (c == ' ' || c == '\t' || c == NUL || c == 0xa0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 return 0; /* blank */
2528 if (vim_iswordc(c))
2529 return 2; /* word character */
2530 return 1; /* punctuation */
2531 }
2532
2533 /* binary search in table */
2534 while (top >= bot)
2535 {
2536 mid = (bot + top) / 2;
2537 if (classes[mid].last < c)
2538 bot = mid + 1;
2539 else if (classes[mid].first > c)
2540 top = mid - 1;
2541 else
2542 return (int)classes[mid].class;
2543 }
2544
2545 /* most other characters are "word" characters */
2546 return 2;
2547}
2548
2549/*
2550 * Code for Unicode case-dependent operations. Based on notes in
2551 * http://www.unicode.org/Public/UNIDATA/CaseFolding.txt
2552 * This code uses simple case folding, not full case folding.
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002553 * Last updated for Unicode 5.2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
2555
2556/*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002557 * The following tables are built by ../runtime/tools/unicode.vim.
2558 * They must be in numeric order, because we use binary search.
2559 * An entry such as {0x41,0x5a,1,32} means that Unicode characters in the
2560 * range from 0x41 to 0x5a inclusive, stepping by 1, are changed to
2561 * folded/upper/lower by adding 32.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563typedef struct
2564{
2565 int rangeStart;
2566 int rangeEnd;
2567 int step;
2568 int offset;
2569} convertStruct;
2570
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002571static convertStruct foldCase[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002573 {0x41,0x5a,1,32},
2574 {0xb5,0xb5,-1,775},
2575 {0xc0,0xd6,1,32},
2576 {0xd8,0xde,1,32},
2577 {0x100,0x12e,2,1},
2578 {0x132,0x136,2,1},
2579 {0x139,0x147,2,1},
2580 {0x14a,0x176,2,1},
2581 {0x178,0x178,-1,-121},
2582 {0x179,0x17d,2,1},
2583 {0x17f,0x17f,-1,-268},
2584 {0x181,0x181,-1,210},
2585 {0x182,0x184,2,1},
2586 {0x186,0x186,-1,206},
2587 {0x187,0x187,-1,1},
2588 {0x189,0x18a,1,205},
2589 {0x18b,0x18b,-1,1},
2590 {0x18e,0x18e,-1,79},
2591 {0x18f,0x18f,-1,202},
2592 {0x190,0x190,-1,203},
2593 {0x191,0x191,-1,1},
2594 {0x193,0x193,-1,205},
2595 {0x194,0x194,-1,207},
2596 {0x196,0x196,-1,211},
2597 {0x197,0x197,-1,209},
2598 {0x198,0x198,-1,1},
2599 {0x19c,0x19c,-1,211},
2600 {0x19d,0x19d,-1,213},
2601 {0x19f,0x19f,-1,214},
2602 {0x1a0,0x1a4,2,1},
2603 {0x1a6,0x1a6,-1,218},
2604 {0x1a7,0x1a7,-1,1},
2605 {0x1a9,0x1a9,-1,218},
2606 {0x1ac,0x1ac,-1,1},
2607 {0x1ae,0x1ae,-1,218},
2608 {0x1af,0x1af,-1,1},
2609 {0x1b1,0x1b2,1,217},
2610 {0x1b3,0x1b5,2,1},
2611 {0x1b7,0x1b7,-1,219},
2612 {0x1b8,0x1bc,4,1},
2613 {0x1c4,0x1c4,-1,2},
2614 {0x1c5,0x1c5,-1,1},
2615 {0x1c7,0x1c7,-1,2},
2616 {0x1c8,0x1c8,-1,1},
2617 {0x1ca,0x1ca,-1,2},
2618 {0x1cb,0x1db,2,1},
2619 {0x1de,0x1ee,2,1},
2620 {0x1f1,0x1f1,-1,2},
2621 {0x1f2,0x1f4,2,1},
2622 {0x1f6,0x1f6,-1,-97},
2623 {0x1f7,0x1f7,-1,-56},
2624 {0x1f8,0x21e,2,1},
2625 {0x220,0x220,-1,-130},
2626 {0x222,0x232,2,1},
2627 {0x23a,0x23a,-1,10795},
2628 {0x23b,0x23b,-1,1},
2629 {0x23d,0x23d,-1,-163},
2630 {0x23e,0x23e,-1,10792},
2631 {0x241,0x241,-1,1},
2632 {0x243,0x243,-1,-195},
2633 {0x244,0x244,-1,69},
2634 {0x245,0x245,-1,71},
2635 {0x246,0x24e,2,1},
2636 {0x345,0x345,-1,116},
2637 {0x370,0x372,2,1},
2638 {0x376,0x376,-1,1},
2639 {0x386,0x386,-1,38},
2640 {0x388,0x38a,1,37},
2641 {0x38c,0x38c,-1,64},
2642 {0x38e,0x38f,1,63},
2643 {0x391,0x3a1,1,32},
2644 {0x3a3,0x3ab,1,32},
2645 {0x3c2,0x3c2,-1,1},
2646 {0x3cf,0x3cf,-1,8},
2647 {0x3d0,0x3d0,-1,-30},
2648 {0x3d1,0x3d1,-1,-25},
2649 {0x3d5,0x3d5,-1,-15},
2650 {0x3d6,0x3d6,-1,-22},
2651 {0x3d8,0x3ee,2,1},
2652 {0x3f0,0x3f0,-1,-54},
2653 {0x3f1,0x3f1,-1,-48},
2654 {0x3f4,0x3f4,-1,-60},
2655 {0x3f5,0x3f5,-1,-64},
2656 {0x3f7,0x3f7,-1,1},
2657 {0x3f9,0x3f9,-1,-7},
2658 {0x3fa,0x3fa,-1,1},
2659 {0x3fd,0x3ff,1,-130},
2660 {0x400,0x40f,1,80},
2661 {0x410,0x42f,1,32},
2662 {0x460,0x480,2,1},
2663 {0x48a,0x4be,2,1},
2664 {0x4c0,0x4c0,-1,15},
2665 {0x4c1,0x4cd,2,1},
2666 {0x4d0,0x524,2,1},
2667 {0x531,0x556,1,48},
2668 {0x10a0,0x10c5,1,7264},
2669 {0x1e00,0x1e94,2,1},
2670 {0x1e9b,0x1e9b,-1,-58},
2671 {0x1e9e,0x1e9e,-1,-7615},
2672 {0x1ea0,0x1efe,2,1},
2673 {0x1f08,0x1f0f,1,-8},
2674 {0x1f18,0x1f1d,1,-8},
2675 {0x1f28,0x1f2f,1,-8},
2676 {0x1f38,0x1f3f,1,-8},
2677 {0x1f48,0x1f4d,1,-8},
2678 {0x1f59,0x1f5f,2,-8},
2679 {0x1f68,0x1f6f,1,-8},
2680 {0x1f88,0x1f8f,1,-8},
2681 {0x1f98,0x1f9f,1,-8},
2682 {0x1fa8,0x1faf,1,-8},
2683 {0x1fb8,0x1fb9,1,-8},
2684 {0x1fba,0x1fbb,1,-74},
2685 {0x1fbc,0x1fbc,-1,-9},
2686 {0x1fbe,0x1fbe,-1,-7173},
2687 {0x1fc8,0x1fcb,1,-86},
2688 {0x1fcc,0x1fcc,-1,-9},
2689 {0x1fd8,0x1fd9,1,-8},
2690 {0x1fda,0x1fdb,1,-100},
2691 {0x1fe8,0x1fe9,1,-8},
2692 {0x1fea,0x1feb,1,-112},
2693 {0x1fec,0x1fec,-1,-7},
2694 {0x1ff8,0x1ff9,1,-128},
2695 {0x1ffa,0x1ffb,1,-126},
2696 {0x1ffc,0x1ffc,-1,-9},
2697 {0x2126,0x2126,-1,-7517},
2698 {0x212a,0x212a,-1,-8383},
2699 {0x212b,0x212b,-1,-8262},
2700 {0x2132,0x2132,-1,28},
2701 {0x2160,0x216f,1,16},
2702 {0x2183,0x2183,-1,1},
2703 {0x24b6,0x24cf,1,26},
2704 {0x2c00,0x2c2e,1,48},
2705 {0x2c60,0x2c60,-1,1},
2706 {0x2c62,0x2c62,-1,-10743},
2707 {0x2c63,0x2c63,-1,-3814},
2708 {0x2c64,0x2c64,-1,-10727},
2709 {0x2c67,0x2c6b,2,1},
2710 {0x2c6d,0x2c6d,-1,-10780},
2711 {0x2c6e,0x2c6e,-1,-10749},
2712 {0x2c6f,0x2c6f,-1,-10783},
2713 {0x2c70,0x2c70,-1,-10782},
2714 {0x2c72,0x2c75,3,1},
2715 {0x2c7e,0x2c7f,1,-10815},
2716 {0x2c80,0x2ce2,2,1},
2717 {0x2ceb,0x2ced,2,1},
2718 {0xa640,0xa65e,2,1},
2719 {0xa662,0xa66c,2,1},
2720 {0xa680,0xa696,2,1},
2721 {0xa722,0xa72e,2,1},
2722 {0xa732,0xa76e,2,1},
2723 {0xa779,0xa77b,2,1},
2724 {0xa77d,0xa77d,-1,-35332},
2725 {0xa77e,0xa786,2,1},
2726 {0xa78b,0xa78b,-1,1},
2727 {0xff21,0xff3a,1,32},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {0x10400,0x10427,1,40}
2729};
2730
Bram Moolenaar35ee4522011-07-15 21:16:59 +02002731static int utf_convert __ARGS((int a, convertStruct table[], int tableSize));
2732static int utf_strnicmp __ARGS((char_u *s1, char_u *s2, size_t n1, size_t n2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734/*
2735 * Generic conversion function for case operations.
2736 * Return the converted equivalent of "a", which is a UCS-4 character. Use
2737 * the given conversion "table". Uses binary search on "table".
2738 */
2739 static int
2740utf_convert(a, table, tableSize)
2741 int a;
2742 convertStruct table[];
2743 int tableSize;
2744{
2745 int start, mid, end; /* indices into table */
2746
2747 start = 0;
2748 end = tableSize / sizeof(convertStruct);
2749 while (start < end)
2750 {
2751 /* need to search further */
2752 mid = (end + start) /2;
2753 if (table[mid].rangeEnd < a)
2754 start = mid + 1;
2755 else
2756 end = mid;
2757 }
2758 if (table[start].rangeStart <= a && a <= table[start].rangeEnd
2759 && (a - table[start].rangeStart) % table[start].step == 0)
2760 return (a + table[start].offset);
2761 else
2762 return a;
2763}
2764
2765/*
2766 * Return the folded-case equivalent of "a", which is a UCS-4 character. Uses
2767 * simple case folding.
2768 */
2769 int
2770utf_fold(a)
2771 int a;
2772{
2773 return utf_convert(a, foldCase, sizeof(foldCase));
2774}
2775
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002776static convertStruct toLower[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002778 {0x41,0x5a,1,32},
2779 {0xc0,0xd6,1,32},
2780 {0xd8,0xde,1,32},
2781 {0x100,0x12e,2,1},
2782 {0x130,0x130,-1,-199},
2783 {0x132,0x136,2,1},
2784 {0x139,0x147,2,1},
2785 {0x14a,0x176,2,1},
2786 {0x178,0x178,-1,-121},
2787 {0x179,0x17d,2,1},
2788 {0x181,0x181,-1,210},
2789 {0x182,0x184,2,1},
2790 {0x186,0x186,-1,206},
2791 {0x187,0x187,-1,1},
2792 {0x189,0x18a,1,205},
2793 {0x18b,0x18b,-1,1},
2794 {0x18e,0x18e,-1,79},
2795 {0x18f,0x18f,-1,202},
2796 {0x190,0x190,-1,203},
2797 {0x191,0x191,-1,1},
2798 {0x193,0x193,-1,205},
2799 {0x194,0x194,-1,207},
2800 {0x196,0x196,-1,211},
2801 {0x197,0x197,-1,209},
2802 {0x198,0x198,-1,1},
2803 {0x19c,0x19c,-1,211},
2804 {0x19d,0x19d,-1,213},
2805 {0x19f,0x19f,-1,214},
2806 {0x1a0,0x1a4,2,1},
2807 {0x1a6,0x1a6,-1,218},
2808 {0x1a7,0x1a7,-1,1},
2809 {0x1a9,0x1a9,-1,218},
2810 {0x1ac,0x1ac,-1,1},
2811 {0x1ae,0x1ae,-1,218},
2812 {0x1af,0x1af,-1,1},
2813 {0x1b1,0x1b2,1,217},
2814 {0x1b3,0x1b5,2,1},
2815 {0x1b7,0x1b7,-1,219},
2816 {0x1b8,0x1bc,4,1},
2817 {0x1c4,0x1c4,-1,2},
2818 {0x1c5,0x1c5,-1,1},
2819 {0x1c7,0x1c7,-1,2},
2820 {0x1c8,0x1c8,-1,1},
2821 {0x1ca,0x1ca,-1,2},
2822 {0x1cb,0x1db,2,1},
2823 {0x1de,0x1ee,2,1},
2824 {0x1f1,0x1f1,-1,2},
2825 {0x1f2,0x1f4,2,1},
2826 {0x1f6,0x1f6,-1,-97},
2827 {0x1f7,0x1f7,-1,-56},
2828 {0x1f8,0x21e,2,1},
2829 {0x220,0x220,-1,-130},
2830 {0x222,0x232,2,1},
2831 {0x23a,0x23a,-1,10795},
2832 {0x23b,0x23b,-1,1},
2833 {0x23d,0x23d,-1,-163},
2834 {0x23e,0x23e,-1,10792},
2835 {0x241,0x241,-1,1},
2836 {0x243,0x243,-1,-195},
2837 {0x244,0x244,-1,69},
2838 {0x245,0x245,-1,71},
2839 {0x246,0x24e,2,1},
2840 {0x370,0x372,2,1},
2841 {0x376,0x376,-1,1},
2842 {0x386,0x386,-1,38},
2843 {0x388,0x38a,1,37},
2844 {0x38c,0x38c,-1,64},
2845 {0x38e,0x38f,1,63},
2846 {0x391,0x3a1,1,32},
2847 {0x3a3,0x3ab,1,32},
2848 {0x3cf,0x3cf,-1,8},
2849 {0x3d8,0x3ee,2,1},
2850 {0x3f4,0x3f4,-1,-60},
2851 {0x3f7,0x3f7,-1,1},
2852 {0x3f9,0x3f9,-1,-7},
2853 {0x3fa,0x3fa,-1,1},
2854 {0x3fd,0x3ff,1,-130},
2855 {0x400,0x40f,1,80},
2856 {0x410,0x42f,1,32},
2857 {0x460,0x480,2,1},
2858 {0x48a,0x4be,2,1},
2859 {0x4c0,0x4c0,-1,15},
2860 {0x4c1,0x4cd,2,1},
2861 {0x4d0,0x524,2,1},
2862 {0x531,0x556,1,48},
2863 {0x10a0,0x10c5,1,7264},
2864 {0x1e00,0x1e94,2,1},
2865 {0x1e9e,0x1e9e,-1,-7615},
2866 {0x1ea0,0x1efe,2,1},
2867 {0x1f08,0x1f0f,1,-8},
2868 {0x1f18,0x1f1d,1,-8},
2869 {0x1f28,0x1f2f,1,-8},
2870 {0x1f38,0x1f3f,1,-8},
2871 {0x1f48,0x1f4d,1,-8},
2872 {0x1f59,0x1f5f,2,-8},
2873 {0x1f68,0x1f6f,1,-8},
2874 {0x1f88,0x1f8f,1,-8},
2875 {0x1f98,0x1f9f,1,-8},
2876 {0x1fa8,0x1faf,1,-8},
2877 {0x1fb8,0x1fb9,1,-8},
2878 {0x1fba,0x1fbb,1,-74},
2879 {0x1fbc,0x1fbc,-1,-9},
2880 {0x1fc8,0x1fcb,1,-86},
2881 {0x1fcc,0x1fcc,-1,-9},
2882 {0x1fd8,0x1fd9,1,-8},
2883 {0x1fda,0x1fdb,1,-100},
2884 {0x1fe8,0x1fe9,1,-8},
2885 {0x1fea,0x1feb,1,-112},
2886 {0x1fec,0x1fec,-1,-7},
2887 {0x1ff8,0x1ff9,1,-128},
2888 {0x1ffa,0x1ffb,1,-126},
2889 {0x1ffc,0x1ffc,-1,-9},
2890 {0x2126,0x2126,-1,-7517},
2891 {0x212a,0x212a,-1,-8383},
2892 {0x212b,0x212b,-1,-8262},
2893 {0x2132,0x2132,-1,28},
2894 {0x2160,0x216f,1,16},
2895 {0x2183,0x2183,-1,1},
2896 {0x24b6,0x24cf,1,26},
2897 {0x2c00,0x2c2e,1,48},
2898 {0x2c60,0x2c60,-1,1},
2899 {0x2c62,0x2c62,-1,-10743},
2900 {0x2c63,0x2c63,-1,-3814},
2901 {0x2c64,0x2c64,-1,-10727},
2902 {0x2c67,0x2c6b,2,1},
2903 {0x2c6d,0x2c6d,-1,-10780},
2904 {0x2c6e,0x2c6e,-1,-10749},
2905 {0x2c6f,0x2c6f,-1,-10783},
2906 {0x2c70,0x2c70,-1,-10782},
2907 {0x2c72,0x2c75,3,1},
2908 {0x2c7e,0x2c7f,1,-10815},
2909 {0x2c80,0x2ce2,2,1},
2910 {0x2ceb,0x2ced,2,1},
2911 {0xa640,0xa65e,2,1},
2912 {0xa662,0xa66c,2,1},
2913 {0xa680,0xa696,2,1},
2914 {0xa722,0xa72e,2,1},
2915 {0xa732,0xa76e,2,1},
2916 {0xa779,0xa77b,2,1},
2917 {0xa77d,0xa77d,-1,-35332},
2918 {0xa77e,0xa786,2,1},
2919 {0xa78b,0xa78b,-1,1},
2920 {0xff21,0xff3a,1,32},
2921 {0x10400,0x10427,1,40}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922};
2923
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002924static convertStruct toUpper[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002926 {0x61,0x7a,1,-32},
2927 {0xb5,0xb5,-1,743},
2928 {0xe0,0xf6,1,-32},
2929 {0xf8,0xfe,1,-32},
2930 {0xff,0xff,-1,121},
2931 {0x101,0x12f,2,-1},
2932 {0x131,0x131,-1,-232},
2933 {0x133,0x137,2,-1},
2934 {0x13a,0x148,2,-1},
2935 {0x14b,0x177,2,-1},
2936 {0x17a,0x17e,2,-1},
2937 {0x17f,0x17f,-1,-300},
2938 {0x180,0x180,-1,195},
2939 {0x183,0x185,2,-1},
2940 {0x188,0x18c,4,-1},
2941 {0x192,0x192,-1,-1},
2942 {0x195,0x195,-1,97},
2943 {0x199,0x199,-1,-1},
2944 {0x19a,0x19a,-1,163},
2945 {0x19e,0x19e,-1,130},
2946 {0x1a1,0x1a5,2,-1},
2947 {0x1a8,0x1ad,5,-1},
2948 {0x1b0,0x1b4,4,-1},
2949 {0x1b6,0x1b9,3,-1},
2950 {0x1bd,0x1bd,-1,-1},
2951 {0x1bf,0x1bf,-1,56},
2952 {0x1c5,0x1c5,-1,-1},
2953 {0x1c6,0x1c6,-1,-2},
2954 {0x1c8,0x1c8,-1,-1},
2955 {0x1c9,0x1c9,-1,-2},
2956 {0x1cb,0x1cb,-1,-1},
2957 {0x1cc,0x1cc,-1,-2},
2958 {0x1ce,0x1dc,2,-1},
2959 {0x1dd,0x1dd,-1,-79},
2960 {0x1df,0x1ef,2,-1},
2961 {0x1f2,0x1f2,-1,-1},
2962 {0x1f3,0x1f3,-1,-2},
2963 {0x1f5,0x1f9,4,-1},
2964 {0x1fb,0x21f,2,-1},
2965 {0x223,0x233,2,-1},
2966 {0x23c,0x23c,-1,-1},
2967 {0x23f,0x240,1,10815},
2968 {0x242,0x247,5,-1},
2969 {0x249,0x24f,2,-1},
2970 {0x250,0x250,-1,10783},
2971 {0x251,0x251,-1,10780},
2972 {0x252,0x252,-1,10782},
2973 {0x253,0x253,-1,-210},
2974 {0x254,0x254,-1,-206},
2975 {0x256,0x257,1,-205},
2976 {0x259,0x259,-1,-202},
2977 {0x25b,0x25b,-1,-203},
2978 {0x260,0x260,-1,-205},
2979 {0x263,0x263,-1,-207},
2980 {0x268,0x268,-1,-209},
2981 {0x269,0x269,-1,-211},
2982 {0x26b,0x26b,-1,10743},
2983 {0x26f,0x26f,-1,-211},
2984 {0x271,0x271,-1,10749},
2985 {0x272,0x272,-1,-213},
2986 {0x275,0x275,-1,-214},
2987 {0x27d,0x27d,-1,10727},
2988 {0x280,0x283,3,-218},
2989 {0x288,0x288,-1,-218},
2990 {0x289,0x289,-1,-69},
2991 {0x28a,0x28b,1,-217},
2992 {0x28c,0x28c,-1,-71},
2993 {0x292,0x292,-1,-219},
2994 {0x345,0x345,-1,84},
2995 {0x371,0x373,2,-1},
2996 {0x377,0x377,-1,-1},
2997 {0x37b,0x37d,1,130},
2998 {0x3ac,0x3ac,-1,-38},
2999 {0x3ad,0x3af,1,-37},
3000 {0x3b1,0x3c1,1,-32},
3001 {0x3c2,0x3c2,-1,-31},
3002 {0x3c3,0x3cb,1,-32},
3003 {0x3cc,0x3cc,-1,-64},
3004 {0x3cd,0x3ce,1,-63},
3005 {0x3d0,0x3d0,-1,-62},
3006 {0x3d1,0x3d1,-1,-57},
3007 {0x3d5,0x3d5,-1,-47},
3008 {0x3d6,0x3d6,-1,-54},
3009 {0x3d7,0x3d7,-1,-8},
3010 {0x3d9,0x3ef,2,-1},
3011 {0x3f0,0x3f0,-1,-86},
3012 {0x3f1,0x3f1,-1,-80},
3013 {0x3f2,0x3f2,-1,7},
3014 {0x3f5,0x3f5,-1,-96},
3015 {0x3f8,0x3fb,3,-1},
3016 {0x430,0x44f,1,-32},
3017 {0x450,0x45f,1,-80},
3018 {0x461,0x481,2,-1},
3019 {0x48b,0x4bf,2,-1},
3020 {0x4c2,0x4ce,2,-1},
3021 {0x4cf,0x4cf,-1,-15},
3022 {0x4d1,0x525,2,-1},
3023 {0x561,0x586,1,-48},
3024 {0x1d79,0x1d79,-1,35332},
3025 {0x1d7d,0x1d7d,-1,3814},
3026 {0x1e01,0x1e95,2,-1},
3027 {0x1e9b,0x1e9b,-1,-59},
3028 {0x1ea1,0x1eff,2,-1},
3029 {0x1f00,0x1f07,1,8},
3030 {0x1f10,0x1f15,1,8},
3031 {0x1f20,0x1f27,1,8},
3032 {0x1f30,0x1f37,1,8},
3033 {0x1f40,0x1f45,1,8},
3034 {0x1f51,0x1f57,2,8},
3035 {0x1f60,0x1f67,1,8},
3036 {0x1f70,0x1f71,1,74},
3037 {0x1f72,0x1f75,1,86},
3038 {0x1f76,0x1f77,1,100},
3039 {0x1f78,0x1f79,1,128},
3040 {0x1f7a,0x1f7b,1,112},
3041 {0x1f7c,0x1f7d,1,126},
3042 {0x1f80,0x1f87,1,8},
3043 {0x1f90,0x1f97,1,8},
3044 {0x1fa0,0x1fa7,1,8},
3045 {0x1fb0,0x1fb1,1,8},
3046 {0x1fb3,0x1fb3,-1,9},
3047 {0x1fbe,0x1fbe,-1,-7205},
3048 {0x1fc3,0x1fc3,-1,9},
3049 {0x1fd0,0x1fd1,1,8},
3050 {0x1fe0,0x1fe1,1,8},
3051 {0x1fe5,0x1fe5,-1,7},
3052 {0x1ff3,0x1ff3,-1,9},
3053 {0x214e,0x214e,-1,-28},
3054 {0x2170,0x217f,1,-16},
3055 {0x2184,0x2184,-1,-1},
3056 {0x24d0,0x24e9,1,-26},
3057 {0x2c30,0x2c5e,1,-48},
3058 {0x2c61,0x2c61,-1,-1},
3059 {0x2c65,0x2c65,-1,-10795},
3060 {0x2c66,0x2c66,-1,-10792},
3061 {0x2c68,0x2c6c,2,-1},
3062 {0x2c73,0x2c76,3,-1},
3063 {0x2c81,0x2ce3,2,-1},
3064 {0x2cec,0x2cee,2,-1},
3065 {0x2d00,0x2d25,1,-7264},
3066 {0xa641,0xa65f,2,-1},
3067 {0xa663,0xa66d,2,-1},
3068 {0xa681,0xa697,2,-1},
3069 {0xa723,0xa72f,2,-1},
3070 {0xa733,0xa76f,2,-1},
3071 {0xa77a,0xa77c,2,-1},
3072 {0xa77f,0xa787,2,-1},
3073 {0xa78c,0xa78c,-1,-1},
3074 {0xff41,0xff5a,1,-32},
3075 {0x10428,0x1044f,1,-40}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076};
3077
3078/*
3079 * Return the upper-case equivalent of "a", which is a UCS-4 character. Use
3080 * simple case folding.
3081 */
3082 int
3083utf_toupper(a)
3084 int a;
3085{
3086 /* If 'casemap' contains "keepascii" use ASCII style toupper(). */
3087 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3088 return TOUPPER_ASC(a);
3089
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003090#if defined(HAVE_TOWUPPER) && defined(__STDC_ISO_10646__)
3091 /* If towupper() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 if (!(cmp_flags & CMP_INTERNAL))
3093 return towupper(a);
3094#endif
3095
3096 /* For characters below 128 use locale sensitive toupper(). */
3097 if (a < 128)
3098 return TOUPPER_LOC(a);
3099
3100 /* For any other characters use the above mapping table. */
3101 return utf_convert(a, toUpper, sizeof(toUpper));
3102}
3103
3104 int
3105utf_islower(a)
3106 int a;
3107{
3108 return (utf_toupper(a) != a);
3109}
3110
3111/*
3112 * Return the lower-case equivalent of "a", which is a UCS-4 character. Use
3113 * simple case folding.
3114 */
3115 int
3116utf_tolower(a)
3117 int a;
3118{
3119 /* If 'casemap' contains "keepascii" use ASCII style tolower(). */
3120 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3121 return TOLOWER_ASC(a);
3122
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003123#if defined(HAVE_TOWLOWER) && defined(__STDC_ISO_10646__)
Bram Moolenaar8fcc0f72005-04-23 20:45:11 +00003124 /* If towlower() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (!(cmp_flags & CMP_INTERNAL))
3126 return towlower(a);
3127#endif
3128
3129 /* For characters below 128 use locale sensitive tolower(). */
3130 if (a < 128)
3131 return TOLOWER_LOC(a);
3132
3133 /* For any other characters use the above mapping table. */
3134 return utf_convert(a, toLower, sizeof(toLower));
3135}
3136
3137 int
3138utf_isupper(a)
3139 int a;
3140{
3141 return (utf_tolower(a) != a);
3142}
3143
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003144 static int
3145utf_strnicmp(s1, s2, n1, n2)
3146 char_u *s1, *s2;
3147 size_t n1, n2;
3148{
3149 int c1, c2, cdiff;
3150 char_u buffer[6];
3151
3152 for (;;)
3153 {
3154 c1 = utf_safe_read_char_adv(&s1, &n1);
3155 c2 = utf_safe_read_char_adv(&s2, &n2);
3156
3157 if (c1 <= 0 || c2 <= 0)
3158 break;
3159
3160 if (c1 == c2)
3161 continue;
3162
3163 cdiff = utf_fold(c1) - utf_fold(c2);
3164 if (cdiff != 0)
3165 return cdiff;
3166 }
3167
3168 /* some string ended or has an incomplete/illegal character sequence */
3169
3170 if (c1 == 0 || c2 == 0)
3171 {
3172 /* some string ended. shorter string is smaller */
3173 if (c1 == 0 && c2 == 0)
3174 return 0;
3175 return c1 == 0 ? -1 : 1;
3176 }
3177
3178 /* Continue with bytewise comparison to produce some result that
3179 * would make comparison operations involving this function transitive.
3180 *
3181 * If only one string had an error, comparison should be made with
3182 * folded version of the other string. In this case it is enough
3183 * to fold just one character to determine the result of comparison. */
3184
3185 if (c1 != -1 && c2 == -1)
3186 {
3187 n1 = utf_char2bytes(utf_fold(c1), buffer);
3188 s1 = buffer;
3189 }
3190 else if (c2 != -1 && c1 == -1)
3191 {
3192 n2 = utf_char2bytes(utf_fold(c2), buffer);
3193 s2 = buffer;
3194 }
3195
3196 while (n1 > 0 && n2 > 0 && *s1 != NUL && *s2 != NUL)
3197 {
3198 cdiff = (int)(*s1) - (int)(*s2);
3199 if (cdiff != 0)
3200 return cdiff;
3201
3202 s1++;
3203 s2++;
3204 n1--;
3205 n2--;
3206 }
3207
3208 if (n1 > 0 && *s1 == NUL)
3209 n1 = 0;
3210 if (n2 > 0 && *s2 == NUL)
3211 n2 = 0;
3212
3213 if (n1 == 0 && n2 == 0)
3214 return 0;
3215 return n1 == 0 ? -1 : 1;
3216}
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218/*
3219 * Version of strnicmp() that handles multi-byte characters.
3220 * Needed for Big5, Sjift-JIS and UTF-8 encoding. Other DBCS encodings can
3221 * probably use strnicmp(), because there are no ASCII characters in the
3222 * second byte.
3223 * Returns zero if s1 and s2 are equal (ignoring case), the difference between
3224 * two characters otherwise.
3225 */
3226 int
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003227mb_strnicmp(s1, s2, nn)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 char_u *s1, *s2;
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003229 size_t nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003231 int i, l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 int cdiff;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003233 int n = (int)nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003235 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003237 return utf_strnicmp(s1, s2, nn, nn);
3238 }
3239 else
3240 {
3241 for (i = 0; i < n; i += l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003243 if (s1[i] == NUL && s2[i] == NUL) /* both strings end */
3244 return 0;
3245
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003246 l = (*mb_ptr2len)(s1 + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (l <= 1)
3248 {
3249 /* Single byte: first check normally, then with ignore case. */
3250 if (s1[i] != s2[i])
3251 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003252 cdiff = MB_TOLOWER(s1[i]) - MB_TOLOWER(s2[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (cdiff != 0)
3254 return cdiff;
3255 }
3256 }
3257 else
3258 {
3259 /* For non-Unicode multi-byte don't ignore case. */
3260 if (l > n - i)
3261 l = n - i;
3262 cdiff = STRNCMP(s1 + i, s2 + i, l);
3263 if (cdiff != 0)
3264 return cdiff;
3265 }
3266 }
3267 }
3268 return 0;
3269}
3270
3271/*
3272 * "g8": show bytes of the UTF-8 char under the cursor. Doesn't matter what
3273 * 'encoding' has been set to.
3274 */
3275 void
3276show_utf8()
3277{
3278 int len;
Bram Moolenaar051b7822005-05-19 21:00:46 +00003279 int rlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 char_u *line;
3281 int clen;
3282 int i;
3283
3284 /* Get the byte length of the char under the cursor, including composing
3285 * characters. */
3286 line = ml_get_cursor();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003287 len = utfc_ptr2len(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (len == 0)
3289 {
3290 MSG("NUL");
3291 return;
3292 }
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 clen = 0;
3295 for (i = 0; i < len; ++i)
3296 {
3297 if (clen == 0)
3298 {
3299 /* start of (composing) character, get its length */
3300 if (i > 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +00003301 {
3302 STRCPY(IObuff + rlen, "+ ");
3303 rlen += 2;
3304 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003305 clen = utf_ptr2len(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaarb382ad12010-05-21 15:46:35 +02003307 sprintf((char *)IObuff + rlen, "%02x ",
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003308 (line[i] == NL) ? NUL : line[i]); /* NUL is stored as NL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 --clen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003310 rlen += (int)STRLEN(IObuff + rlen);
Bram Moolenaar051b7822005-05-19 21:00:46 +00003311 if (rlen > IOSIZE - 20)
3312 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314
3315 msg(IObuff);
3316}
3317
3318/*
3319 * mb_head_off() function pointer.
3320 * Return offset from "p" to the first byte of the character it points into.
Bram Moolenaar24397332009-12-02 14:02:39 +00003321 * If "p" points to the NUL at the end of the string return 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 * Returns 0 when already at the first byte of a character.
3323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int
3325latin_head_off(base, p)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003326 char_u *base UNUSED;
3327 char_u *p UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 return 0;
3330}
3331
3332 int
3333dbcs_head_off(base, p)
3334 char_u *base;
3335 char_u *p;
3336{
3337 char_u *q;
3338
3339 /* It can't be a trailing byte when not using DBCS, at the start of the
3340 * string or the previous byte can't start a double-byte. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003341 if (p <= base || MB_BYTE2LEN(p[-1]) == 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return 0;
3343
3344 /* This is slow: need to start at the base and go forward until the
3345 * byte we are looking for. Return 1 when we went past it, 0 otherwise. */
3346 q = base;
3347 while (q < p)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003348 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 return (q == p) ? 0 : 1;
3350}
3351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352/*
3353 * Special version of dbcs_head_off() that works for ScreenLines[], where
3354 * single-width DBCS_JPNU characters are stored separately.
3355 */
3356 int
3357dbcs_screen_head_off(base, p)
3358 char_u *base;
3359 char_u *p;
3360{
3361 char_u *q;
3362
3363 /* It can't be a trailing byte when not using DBCS, at the start of the
3364 * string or the previous byte can't start a double-byte.
3365 * For euc-jp an 0x8e byte in the previous cell always means we have a
3366 * lead byte in the current cell. */
3367 if (p <= base
3368 || (enc_dbcs == DBCS_JPNU && p[-1] == 0x8e)
Bram Moolenaar24397332009-12-02 14:02:39 +00003369 || MB_BYTE2LEN(p[-1]) == 1
3370 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 return 0;
3372
3373 /* This is slow: need to start at the base and go forward until the
3374 * byte we are looking for. Return 1 when we went past it, 0 otherwise.
3375 * For DBCS_JPNU look out for 0x8e, which means the second byte is not
3376 * stored as the next byte. */
3377 q = base;
3378 while (q < p)
3379 {
3380 if (enc_dbcs == DBCS_JPNU && *q == 0x8e)
3381 ++q;
3382 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003383 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 return (q == p) ? 0 : 1;
3386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 int
3389utf_head_off(base, p)
3390 char_u *base;
3391 char_u *p;
3392{
3393 char_u *q;
3394 char_u *s;
3395 int c;
Bram Moolenaar24397332009-12-02 14:02:39 +00003396 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397#ifdef FEAT_ARABIC
3398 char_u *j;
3399#endif
3400
3401 if (*p < 0x80) /* be quick for ASCII */
3402 return 0;
3403
3404 /* Skip backwards over trailing bytes: 10xx.xxxx
3405 * Skip backwards again if on a composing char. */
3406 for (q = p; ; --q)
3407 {
3408 /* Move s to the last byte of this char. */
3409 for (s = q; (s[1] & 0xc0) == 0x80; ++s)
3410 ;
3411 /* Move q to the first byte of this char. */
3412 while (q > base && (*q & 0xc0) == 0x80)
3413 --q;
3414 /* Check for illegal sequence. Do allow an illegal byte after where we
3415 * started. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003416 len = utf8len_tab[*q];
3417 if (len != (int)(s - q + 1) && len != (int)(p - q + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 return 0;
3419
3420 if (q <= base)
3421 break;
3422
3423 c = utf_ptr2char(q);
3424 if (utf_iscomposing(c))
3425 continue;
3426
3427#ifdef FEAT_ARABIC
3428 if (arabic_maycombine(c))
3429 {
3430 /* Advance to get a sneak-peak at the next char */
3431 j = q;
3432 --j;
3433 /* Move j to the first byte of this char. */
3434 while (j > base && (*j & 0xc0) == 0x80)
3435 --j;
3436 if (arabic_combine(utf_ptr2char(j), c))
3437 continue;
3438 }
3439#endif
3440 break;
3441 }
3442
3443 return (int)(p - q);
3444}
3445
3446/*
Bram Moolenaard8b02732005-01-14 21:48:43 +00003447 * Copy a character from "*fp" to "*tp" and advance the pointers.
3448 */
3449 void
3450mb_copy_char(fp, tp)
3451 char_u **fp;
3452 char_u **tp;
3453{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003454 int l = (*mb_ptr2len)(*fp);
Bram Moolenaard8b02732005-01-14 21:48:43 +00003455
3456 mch_memmove(*tp, *fp, (size_t)l);
3457 *tp += l;
3458 *fp += l;
3459}
3460
3461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 * Return the offset from "p" to the first byte of a character. When "p" is
3463 * at the start of a character 0 is returned, otherwise the offset to the next
3464 * character. Can start anywhere in a stream of bytes.
3465 */
3466 int
3467mb_off_next(base, p)
3468 char_u *base;
3469 char_u *p;
3470{
3471 int i;
3472 int j;
3473
3474 if (enc_utf8)
3475 {
3476 if (*p < 0x80) /* be quick for ASCII */
3477 return 0;
3478
3479 /* Find the next character that isn't 10xx.xxxx */
3480 for (i = 0; (p[i] & 0xc0) == 0x80; ++i)
3481 ;
3482 if (i > 0)
3483 {
3484 /* Check for illegal sequence. */
3485 for (j = 0; p - j > base; ++j)
3486 if ((p[-j] & 0xc0) != 0x80)
3487 break;
3488 if (utf8len_tab[p[-j]] != i + j)
3489 return 0;
3490 }
3491 return i;
3492 }
3493
3494 /* Only need to check if we're on a trail byte, it doesn't matter if we
3495 * want the offset to the next or current character. */
3496 return (*mb_head_off)(base, p);
3497}
3498
3499/*
3500 * Return the offset from "p" to the last byte of the character it points
3501 * into. Can start anywhere in a stream of bytes.
3502 */
3503 int
3504mb_tail_off(base, p)
3505 char_u *base;
3506 char_u *p;
3507{
3508 int i;
3509 int j;
3510
3511 if (*p == NUL)
3512 return 0;
3513
3514 if (enc_utf8)
3515 {
3516 /* Find the last character that is 10xx.xxxx */
3517 for (i = 0; (p[i + 1] & 0xc0) == 0x80; ++i)
3518 ;
3519 /* Check for illegal sequence. */
3520 for (j = 0; p - j > base; ++j)
3521 if ((p[-j] & 0xc0) != 0x80)
3522 break;
3523 if (utf8len_tab[p[-j]] != i + j + 1)
3524 return 0;
3525 return i;
3526 }
3527
3528 /* It can't be the first byte if a double-byte when not using DBCS, at the
3529 * end of the string or the byte can't start a double-byte. */
3530 if (enc_dbcs == 0 || p[1] == NUL || MB_BYTE2LEN(*p) == 1)
3531 return 0;
3532
3533 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3534 return 1 - dbcs_head_off(base, p);
3535}
3536
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003537/*
3538 * Find the next illegal byte sequence.
3539 */
3540 void
3541utf_find_illegal()
3542{
3543 pos_T pos = curwin->w_cursor;
3544 char_u *p;
3545 int len;
3546 vimconv_T vimconv;
3547 char_u *tofree = NULL;
3548
3549 vimconv.vc_type = CONV_NONE;
3550 if (enc_utf8 && (enc_canon_props(curbuf->b_p_fenc) & ENC_8BIT))
3551 {
3552 /* 'encoding' is "utf-8" but we are editing a 8-bit encoded file,
3553 * possibly a utf-8 file with illegal bytes. Setup for conversion
3554 * from utf-8 to 'fileencoding'. */
3555 convert_setup(&vimconv, p_enc, curbuf->b_p_fenc);
3556 }
3557
3558#ifdef FEAT_VIRTUALEDIT
3559 curwin->w_cursor.coladd = 0;
3560#endif
3561 for (;;)
3562 {
3563 p = ml_get_cursor();
3564 if (vimconv.vc_type != CONV_NONE)
3565 {
3566 vim_free(tofree);
3567 tofree = string_convert(&vimconv, p, NULL);
3568 if (tofree == NULL)
3569 break;
3570 p = tofree;
3571 }
3572
3573 while (*p != NUL)
3574 {
3575 /* Illegal means that there are not enough trail bytes (checked by
3576 * utf_ptr2len()) or too many of them (overlong sequence). */
3577 len = utf_ptr2len(p);
3578 if (*p >= 0x80 && (len == 1
3579 || utf_char2len(utf_ptr2char(p)) != len))
3580 {
3581 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003582 curwin->w_cursor.col += (colnr_T)(p - ml_get_cursor());
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003583 else
3584 {
3585 int l;
3586
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003587 len = (int)(p - tofree);
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003588 for (p = ml_get_cursor(); *p != NUL && len-- > 0; p += l)
3589 {
3590 l = utf_ptr2len(p);
3591 curwin->w_cursor.col += l;
3592 }
3593 }
3594 goto theend;
3595 }
3596 p += len;
3597 }
3598 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
3599 break;
3600 ++curwin->w_cursor.lnum;
3601 curwin->w_cursor.col = 0;
3602 }
3603
3604 /* didn't find it: don't move and beep */
3605 curwin->w_cursor = pos;
3606 beep_flush();
3607
3608theend:
3609 vim_free(tofree);
3610 convert_setup(&vimconv, NULL, NULL);
3611}
3612
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003613#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003614/*
3615 * Return TRUE if string "s" is a valid utf-8 string.
3616 * When "end" is NULL stop at the first NUL.
3617 * When "end" is positive stop there.
3618 */
3619 int
3620utf_valid_string(s, end)
3621 char_u *s;
3622 char_u *end;
3623{
3624 int l;
3625 char_u *p = s;
3626
3627 while (end == NULL ? *p != NUL : p < end)
3628 {
Bram Moolenaar24397332009-12-02 14:02:39 +00003629 l = utf8len_tab_zero[*p];
3630 if (l == 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003631 return FALSE; /* invalid lead byte */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003632 if (end != NULL && p + l > end)
3633 return FALSE; /* incomplete byte sequence */
3634 ++p;
3635 while (--l > 0)
3636 if ((*p++ & 0xc0) != 0x80)
3637 return FALSE; /* invalid trail byte */
3638 }
3639 return TRUE;
3640}
3641#endif
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643#if defined(FEAT_GUI) || defined(PROTO)
3644/*
3645 * Special version of mb_tail_off() for use in ScreenLines[].
3646 */
3647 int
3648dbcs_screen_tail_off(base, p)
3649 char_u *base;
3650 char_u *p;
3651{
3652 /* It can't be the first byte if a double-byte when not using DBCS, at the
3653 * end of the string or the byte can't start a double-byte.
3654 * For euc-jp an 0x8e byte always means we have a lead byte in the current
3655 * cell. */
3656 if (*p == NUL || p[1] == NUL
3657 || (enc_dbcs == DBCS_JPNU && *p == 0x8e)
3658 || MB_BYTE2LEN(*p) == 1)
3659 return 0;
3660
3661 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3662 return 1 - dbcs_screen_head_off(base, p);
3663}
3664#endif
3665
3666/*
3667 * If the cursor moves on an trail byte, set the cursor on the lead byte.
3668 * Thus it moves left if necessary.
3669 * Return TRUE when the cursor was adjusted.
3670 */
3671 void
3672mb_adjust_cursor()
3673{
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003674 mb_adjustpos(curbuf, &curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675}
3676
3677/*
3678 * Adjust position "*lp" to point to the first byte of a multi-byte character.
3679 * If it points to a tail byte it's moved backwards to the head byte.
3680 */
3681 void
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003682mb_adjustpos(buf, lp)
3683 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 pos_T *lp;
3685{
3686 char_u *p;
3687
3688 if (lp->col > 0
3689#ifdef FEAT_VIRTUALEDIT
3690 || lp->coladd > 1
3691#endif
3692 )
3693 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003694 p = ml_get_buf(buf, lp->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 lp->col -= (*mb_head_off)(p, p + lp->col);
3696#ifdef FEAT_VIRTUALEDIT
3697 /* Reset "coladd" when the cursor would be on the right half of a
3698 * double-wide character. */
3699 if (lp->coladd == 1
3700 && p[lp->col] != TAB
3701 && vim_isprintc((*mb_ptr2char)(p + lp->col))
3702 && ptr2cells(p + lp->col) > 1)
3703 lp->coladd = 0;
3704#endif
3705 }
3706}
3707
3708/*
3709 * Return a pointer to the character before "*p", if there is one.
3710 */
3711 char_u *
3712mb_prevptr(line, p)
3713 char_u *line; /* start of the string */
3714 char_u *p;
3715{
3716 if (p > line)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003717 mb_ptr_back(line, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 return p;
3719}
3720
3721/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003722 * Return the character length of "str". Each multi-byte character (with
3723 * following composing characters) counts as one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
3725 int
3726mb_charlen(str)
3727 char_u *str;
3728{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003729 char_u *p = str;
3730 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003732 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 return 0;
3734
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003735 for (count = 0; *p != NUL; count++)
3736 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 return count;
3739}
3740
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00003741#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003742/*
3743 * Like mb_charlen() but for a string with specified length.
3744 */
3745 int
3746mb_charlen_len(str, len)
3747 char_u *str;
3748 int len;
3749{
3750 char_u *p = str;
3751 int count;
3752
3753 for (count = 0; *p != NUL && p < str + len; count++)
3754 p += (*mb_ptr2len)(p);
3755
3756 return count;
3757}
3758#endif
3759
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760/*
3761 * Try to un-escape a multi-byte character.
3762 * Used for the "to" and "from" part of a mapping.
3763 * Return the un-escaped string if it is a multi-byte character, and advance
3764 * "pp" to just after the bytes that formed it.
3765 * Return NULL if no multi-byte char was found.
3766 */
3767 char_u *
3768mb_unescape(pp)
3769 char_u **pp;
3770{
3771 static char_u buf[MB_MAXBYTES + 1];
3772 int n, m = 0;
3773 char_u *str = *pp;
3774
3775 /* Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI
3776 * KS_EXTRA KE_CSI to CSI. */
3777 for (n = 0; str[n] != NUL && m <= MB_MAXBYTES; ++n)
3778 {
3779 if (str[n] == K_SPECIAL
3780 && str[n + 1] == KS_SPECIAL
3781 && str[n + 2] == KE_FILLER)
3782 {
3783 buf[m++] = K_SPECIAL;
3784 n += 2;
3785 }
Bram Moolenaar6203ff92008-01-06 16:18:56 +00003786 else if ((str[n] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787# ifdef FEAT_GUI
Bram Moolenaar6203ff92008-01-06 16:18:56 +00003788 || str[n] == CSI
3789# endif
3790 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 && str[n + 1] == KS_EXTRA
3792 && str[n + 2] == (int)KE_CSI)
3793 {
3794 buf[m++] = CSI;
3795 n += 2;
3796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 else if (str[n] == K_SPECIAL
3798# ifdef FEAT_GUI
3799 || str[n] == CSI
3800# endif
3801 )
3802 break; /* a special key can't be a multibyte char */
3803 else
3804 buf[m++] = str[n];
3805 buf[m] = NUL;
3806
3807 /* Return a multi-byte character if it's found. An illegal sequence
3808 * will result in a 1 here. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003809 if ((*mb_ptr2len)(buf) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
3811 *pp = str + n + 1;
3812 return buf;
3813 }
3814 }
3815 return NULL;
3816}
3817
3818/*
3819 * Return TRUE if the character at "row"/"col" on the screen is the left side
3820 * of a double-width character.
3821 * Caller must make sure "row" and "col" are not invalid!
3822 */
3823 int
3824mb_lefthalve(row, col)
3825 int row;
3826 int col;
3827{
3828#ifdef FEAT_HANGULIN
3829 if (composing_hangul)
3830 return TRUE;
3831#endif
Bram Moolenaar367329b2007-08-30 11:53:22 +00003832 return (*mb_off2cells)(LineOffset[row] + col,
3833 LineOffset[row] + screen_Columns) > 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834}
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836/*
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003837 * Correct a position on the screen, if it's the right half of a double-wide
3838 * char move it to the left half. Returns the corrected column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 */
3840 int
3841mb_fix_col(col, row)
3842 int col;
3843 int row;
3844{
3845 col = check_col(col);
3846 row = check_row(row);
3847 if (has_mbyte && ScreenLines != NULL && col > 0
3848 && ((enc_dbcs
3849 && ScreenLines[LineOffset[row] + col] != NUL
3850 && dbcs_screen_head_off(ScreenLines + LineOffset[row],
3851 ScreenLines + LineOffset[row] + col))
3852 || (enc_utf8 && ScreenLines[LineOffset[row] + col] == 0)))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003853 return col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return col;
3855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#endif
3857
3858#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
3859static int enc_alias_search __ARGS((char_u *name));
3860
3861/*
3862 * Skip the Vim specific head of a 'encoding' name.
3863 */
3864 char_u *
3865enc_skip(p)
3866 char_u *p;
3867{
3868 if (STRNCMP(p, "2byte-", 6) == 0)
3869 return p + 6;
3870 if (STRNCMP(p, "8bit-", 5) == 0)
3871 return p + 5;
3872 return p;
3873}
3874
3875/*
3876 * Find the canonical name for encoding "enc".
3877 * When the name isn't recognized, returns "enc" itself, but with all lower
3878 * case characters and '_' replaced with '-'.
3879 * Returns an allocated string. NULL for out-of-memory.
3880 */
3881 char_u *
3882enc_canonize(enc)
3883 char_u *enc;
3884{
3885 char_u *r;
3886 char_u *p, *s;
3887 int i;
3888
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003889# ifdef FEAT_MBYTE
3890 if (STRCMP(enc, "default") == 0)
3891 {
3892 /* Use the default encoding as it's found by set_init_1(). */
3893 r = get_encoding_default();
3894 if (r == NULL)
3895 r = (char_u *)"latin1";
3896 return vim_strsave(r);
3897 }
3898# endif
3899
Bram Moolenaar29466f22007-05-10 17:45:37 +00003900 /* copy "enc" to allocated memory, with room for two '-' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 r = alloc((unsigned)(STRLEN(enc) + 3));
3902 if (r != NULL)
3903 {
3904 /* Make it all lower case and replace '_' with '-'. */
3905 p = r;
3906 for (s = enc; *s != NUL; ++s)
3907 {
3908 if (*s == '_')
3909 *p++ = '-';
3910 else
3911 *p++ = TOLOWER_ASC(*s);
3912 }
3913 *p = NUL;
3914
3915 /* Skip "2byte-" and "8bit-". */
3916 p = enc_skip(r);
3917
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003918 /* Change "microsoft-cp" to "cp". Used in some spell files. */
3919 if (STRNCMP(p, "microsoft-cp", 12) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003920 STRMOVE(p, p + 10);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 /* "iso8859" -> "iso-8859" */
3923 if (STRNCMP(p, "iso8859", 7) == 0)
3924 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003925 STRMOVE(p + 4, p + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 p[3] = '-';
3927 }
3928
3929 /* "iso-8859n" -> "iso-8859-n" */
3930 if (STRNCMP(p, "iso-8859", 8) == 0 && p[8] != '-')
3931 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003932 STRMOVE(p + 9, p + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 p[8] = '-';
3934 }
3935
3936 /* "latin-N" -> "latinN" */
3937 if (STRNCMP(p, "latin-", 6) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003938 STRMOVE(p + 5, p + 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
3940 if (enc_canon_search(p) >= 0)
3941 {
3942 /* canonical name can be used unmodified */
3943 if (p != r)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003944 STRMOVE(r, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 else if ((i = enc_alias_search(p)) >= 0)
3947 {
3948 /* alias recognized, get canonical name */
3949 vim_free(r);
3950 r = vim_strsave((char_u *)enc_canon_table[i].name);
3951 }
3952 }
3953 return r;
3954}
3955
3956/*
3957 * Search for an encoding alias of "name".
3958 * Returns -1 when not found.
3959 */
3960 static int
3961enc_alias_search(name)
3962 char_u *name;
3963{
3964 int i;
3965
3966 for (i = 0; enc_alias_table[i].name != NULL; ++i)
3967 if (STRCMP(name, enc_alias_table[i].name) == 0)
3968 return enc_alias_table[i].canon;
3969 return -1;
3970}
3971#endif
3972
3973#if defined(FEAT_MBYTE) || defined(PROTO)
3974
3975#ifdef HAVE_LANGINFO_H
3976# include <langinfo.h>
3977#endif
3978
3979/*
3980 * Get the canonicalized encoding of the current locale.
3981 * Returns an allocated string when successful, NULL when not.
3982 */
3983 char_u *
3984enc_locale()
3985{
3986#ifndef WIN3264
3987 char *s;
3988 char *p;
3989 int i;
3990#endif
3991 char buf[50];
3992#ifdef WIN3264
3993 long acp = GetACP();
3994
3995 if (acp == 1200)
3996 STRCPY(buf, "ucs-2le");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003997 else if (acp == 1252) /* cp1252 is used as latin1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 STRCPY(buf, "latin1");
3999 else
4000 sprintf(buf, "cp%ld", acp);
4001#else
4002# ifdef HAVE_NL_LANGINFO_CODESET
4003 if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
4004# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004005# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if ((s = getenv("LC_ALL")) == NULL || *s == NUL)
4009 if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL)
4010 s = getenv("LANG");
4011
4012 if (s == NULL || *s == NUL)
4013 return FAIL;
4014
4015 /* The most generic locale format is:
4016 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
4017 * If there is a '.' remove the part before it.
4018 * if there is something after the codeset, remove it.
4019 * Make the name lowercase and replace '_' with '-'.
4020 * Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
4021 * "ko_KR.EUC" == "euc-kr"
4022 */
4023 if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL)
4024 {
4025 if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0
4026 && !isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_')
4027 {
4028 /* copy "XY.EUC" to "euc-XY" to buf[10] */
4029 STRCPY(buf + 10, "euc-");
4030 buf[14] = p[-2];
4031 buf[15] = p[-1];
4032 buf[16] = 0;
4033 s = buf + 10;
4034 }
4035 else
4036 s = p + 1;
4037 }
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004038 for (i = 0; s[i] != NUL && i < (int)sizeof(buf) - 1; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
4040 if (s[i] == '_' || s[i] == '-')
4041 buf[i] = '-';
4042 else if (isalnum((int)s[i]))
4043 buf[i] = TOLOWER_ASC(s[i]);
4044 else
4045 break;
4046 }
4047 buf[i] = NUL;
4048#endif
4049
4050 return enc_canonize((char_u *)buf);
4051}
4052
4053#if defined(WIN3264) || defined(PROTO)
4054/*
4055 * Convert an encoding name to an MS-Windows codepage.
4056 * Returns zero if no codepage can be figured out.
4057 */
4058 int
4059encname2codepage(name)
4060 char_u *name;
4061{
4062 int cp;
4063 char_u *p = name;
4064 int idx;
4065
4066 if (STRNCMP(p, "8bit-", 5) == 0)
4067 p += 5;
4068 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
4069 p += 6;
4070
4071 if (p[0] == 'c' && p[1] == 'p')
4072 cp = atoi(p + 2);
4073 else if ((idx = enc_canon_search(p)) >= 0)
4074 cp = enc_canon_table[idx].codepage;
4075 else
4076 return 0;
4077 if (IsValidCodePage(cp))
4078 return cp;
4079 return 0;
4080}
4081#endif
4082
4083# if defined(USE_ICONV) || defined(PROTO)
4084
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004085static char_u *iconv_string __ARGS((vimconv_T *vcp, char_u *str, int slen, int *unconvlenp, int *resultlenp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087/*
4088 * Call iconv_open() with a check if iconv() works properly (there are broken
4089 * versions).
4090 * Returns (void *)-1 if failed.
4091 * (should return iconv_t, but that causes problems with prototypes).
4092 */
4093 void *
4094my_iconv_open(to, from)
4095 char_u *to;
4096 char_u *from;
4097{
4098 iconv_t fd;
4099#define ICONV_TESTLEN 400
4100 char_u tobuf[ICONV_TESTLEN];
4101 char *p;
4102 size_t tolen;
4103 static int iconv_ok = -1;
4104
4105 if (iconv_ok == FALSE)
4106 return (void *)-1; /* detected a broken iconv() previously */
4107
4108#ifdef DYNAMIC_ICONV
4109 /* Check if the iconv.dll can be found. */
4110 if (!iconv_enabled(TRUE))
4111 return (void *)-1;
4112#endif
4113
4114 fd = iconv_open((char *)enc_skip(to), (char *)enc_skip(from));
4115
4116 if (fd != (iconv_t)-1 && iconv_ok == -1)
4117 {
4118 /*
4119 * Do a dummy iconv() call to check if it actually works. There is a
4120 * version of iconv() on Linux that is broken. We can't ignore it,
4121 * because it's wide-spread. The symptoms are that after outputting
4122 * the initial shift state the "to" pointer is NULL and conversion
4123 * stops for no apparent reason after about 8160 characters.
4124 */
4125 p = (char *)tobuf;
4126 tolen = ICONV_TESTLEN;
4127 (void)iconv(fd, NULL, NULL, &p, &tolen);
4128 if (p == NULL)
4129 {
4130 iconv_ok = FALSE;
4131 iconv_close(fd);
4132 fd = (iconv_t)-1;
4133 }
4134 else
4135 iconv_ok = TRUE;
4136 }
4137
4138 return (void *)fd;
4139}
4140
4141/*
4142 * Convert the string "str[slen]" with iconv().
4143 * If "unconvlenp" is not NULL handle the string ending in an incomplete
4144 * sequence and set "*unconvlenp" to the length of it.
4145 * Returns the converted string in allocated memory. NULL for an error.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004146 * If resultlenp is not NULL, sets it to the result length in bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
4148 static char_u *
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004149iconv_string(vcp, str, slen, unconvlenp, resultlenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 vimconv_T *vcp;
4151 char_u *str;
4152 int slen;
4153 int *unconvlenp;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004154 int *resultlenp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 const char *from;
4157 size_t fromlen;
4158 char *to;
4159 size_t tolen;
4160 size_t len = 0;
4161 size_t done = 0;
4162 char_u *result = NULL;
4163 char_u *p;
4164 int l;
4165
4166 from = (char *)str;
4167 fromlen = slen;
4168 for (;;)
4169 {
4170 if (len == 0 || ICONV_ERRNO == ICONV_E2BIG)
4171 {
4172 /* Allocate enough room for most conversions. When re-allocating
4173 * increase the buffer size. */
4174 len = len + fromlen * 2 + 40;
4175 p = alloc((unsigned)len);
4176 if (p != NULL && done > 0)
4177 mch_memmove(p, result, done);
4178 vim_free(result);
4179 result = p;
4180 if (result == NULL) /* out of memory */
4181 break;
4182 }
4183
4184 to = (char *)result + done;
4185 tolen = len - done - 2;
4186 /* Avoid a warning for systems with a wrong iconv() prototype by
4187 * casting the second argument to void *. */
4188 if (iconv(vcp->vc_fd, (void *)&from, &fromlen, &to, &tolen)
4189 != (size_t)-1)
4190 {
4191 /* Finished, append a NUL. */
4192 *to = NUL;
4193 break;
4194 }
4195
4196 /* Check both ICONV_EINVAL and EINVAL, because the dynamically loaded
4197 * iconv library may use one of them. */
4198 if (!vcp->vc_fail && unconvlenp != NULL
4199 && (ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4200 {
4201 /* Handle an incomplete sequence at the end. */
4202 *to = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004203 *unconvlenp = (int)fromlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 break;
4205 }
4206
4207 /* Check both ICONV_EILSEQ and EILSEQ, because the dynamically loaded
4208 * iconv library may use one of them. */
4209 else if (!vcp->vc_fail
4210 && (ICONV_ERRNO == ICONV_EILSEQ || ICONV_ERRNO == EILSEQ
4211 || ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4212 {
4213 /* Can't convert: insert a '?' and skip a character. This assumes
4214 * conversion from 'encoding' to something else. In other
4215 * situations we don't know what to skip anyway. */
4216 *to++ = '?';
4217 if ((*mb_ptr2cells)((char_u *)from) > 1)
4218 *to++ = '?';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004219 if (enc_utf8)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004220 l = utfc_ptr2len_len((char_u *)from, (int)fromlen);
Bram Moolenaar217ad922005-03-20 22:37:15 +00004221 else
4222 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004223 l = (*mb_ptr2len)((char_u *)from);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004224 if (l > (int)fromlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004225 l = (int)fromlen;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 from += l;
4228 fromlen -= l;
4229 }
4230 else if (ICONV_ERRNO != ICONV_E2BIG)
4231 {
4232 /* conversion failed */
4233 vim_free(result);
4234 result = NULL;
4235 break;
4236 }
4237 /* Not enough room or skipping illegal sequence. */
4238 done = to - (char *)result;
4239 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004240
Bram Moolenaar0d35e912011-04-11 14:29:17 +02004241 if (resultlenp != NULL && result != NULL)
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004242 *resultlenp = (int)(to - (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return result;
4244}
4245
4246# if defined(DYNAMIC_ICONV) || defined(PROTO)
4247/*
4248 * Dynamically load the "iconv.dll" on Win32.
4249 */
4250
4251#ifndef DYNAMIC_ICONV /* just generating prototypes */
4252# define HINSTANCE int
4253#endif
Bram Moolenaard6f676d2005-06-01 21:51:55 +00004254static HINSTANCE hIconvDLL = 0;
4255static HINSTANCE hMsvcrtDLL = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257# ifndef DYNAMIC_ICONV_DLL
4258# define DYNAMIC_ICONV_DLL "iconv.dll"
4259# define DYNAMIC_ICONV_DLL_ALT "libiconv.dll"
4260# endif
4261# ifndef DYNAMIC_MSVCRT_DLL
4262# define DYNAMIC_MSVCRT_DLL "msvcrt.dll"
4263# endif
4264
4265/*
4266 * Try opening the iconv.dll and return TRUE if iconv() can be used.
4267 */
4268 int
4269iconv_enabled(verbose)
4270 int verbose;
4271{
4272 if (hIconvDLL != 0 && hMsvcrtDLL != 0)
4273 return TRUE;
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004274 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (hIconvDLL == 0) /* sometimes it's called libiconv.dll */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004276 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL_ALT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (hIconvDLL != 0)
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004278 hMsvcrtDLL = vimLoadLib(DYNAMIC_MSVCRT_DLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (hIconvDLL == 0 || hMsvcrtDLL == 0)
4280 {
4281 /* Only give the message when 'verbose' is set, otherwise it might be
4282 * done whenever a conversion is attempted. */
4283 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004284 {
4285 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 EMSG2(_(e_loadlib),
4287 hIconvDLL == 0 ? DYNAMIC_ICONV_DLL : DYNAMIC_MSVCRT_DLL);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004288 verbose_leave();
4289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 iconv_end();
4291 return FALSE;
4292 }
4293
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004294 iconv = (void *)GetProcAddress(hIconvDLL, "libiconv");
4295 iconv_open = (void *)GetProcAddress(hIconvDLL, "libiconv_open");
4296 iconv_close = (void *)GetProcAddress(hIconvDLL, "libiconv_close");
4297 iconvctl = (void *)GetProcAddress(hIconvDLL, "libiconvctl");
4298 iconv_errno = (void *)GetProcAddress(hMsvcrtDLL, "_errno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 if (iconv == NULL || iconv_open == NULL || iconv_close == NULL
4300 || iconvctl == NULL || iconv_errno == NULL)
4301 {
4302 iconv_end();
4303 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004304 {
4305 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 EMSG2(_(e_loadfunc), "for libiconv");
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004307 verbose_leave();
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FALSE;
4310 }
4311 return TRUE;
4312}
4313
4314 void
4315iconv_end()
4316{
4317 /* Don't use iconv() when inputting or outputting characters. */
4318 if (input_conv.vc_type == CONV_ICONV)
4319 convert_setup(&input_conv, NULL, NULL);
4320 if (output_conv.vc_type == CONV_ICONV)
4321 convert_setup(&output_conv, NULL, NULL);
4322
4323 if (hIconvDLL != 0)
4324 FreeLibrary(hIconvDLL);
4325 if (hMsvcrtDLL != 0)
4326 FreeLibrary(hMsvcrtDLL);
4327 hIconvDLL = 0;
4328 hMsvcrtDLL = 0;
4329}
4330# endif /* DYNAMIC_ICONV */
4331# endif /* USE_ICONV */
4332
4333#endif /* FEAT_MBYTE */
4334
4335#if defined(FEAT_XIM) || defined(PROTO)
4336
Bram Moolenaar64404472010-06-26 06:24:45 +02004337# if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338static int xim_has_preediting INIT(= FALSE); /* IM current status */
4339
4340/*
4341 * Set preedit_start_col to the current cursor position.
4342 */
4343 static void
4344init_preedit_start_col(void)
4345{
4346 if (State & CMDLINE)
4347 preedit_start_col = cmdline_getvcol_cursor();
4348 else if (curwin != NULL)
4349 getvcol(curwin, &curwin->w_cursor, &preedit_start_col, NULL, NULL);
4350 /* Prevent that preediting marks the buffer as changed. */
4351 xim_changed_while_preediting = curbuf->b_changed;
4352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354static int im_is_active = FALSE; /* IM is enabled for current mode */
Bram Moolenaarc236c162008-07-13 17:41:49 +00004355static int preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356static int im_preedit_cursor = 0; /* cursor offset in characters */
4357static int im_preedit_trailing = 0; /* number of characters after cursor */
4358
4359static unsigned long im_commit_handler_id = 0;
4360static unsigned int im_activatekey_keyval = GDK_VoidSymbol;
4361static unsigned int im_activatekey_state = 0;
4362
4363 void
4364im_set_active(int active)
4365{
4366 int was_active;
4367
4368 was_active = !!im_is_active;
4369 im_is_active = (active && !p_imdisable);
4370
4371 if (im_is_active != was_active)
4372 xim_reset();
4373}
4374
4375 void
4376xim_set_focus(int focus)
4377{
4378 if (xic != NULL)
4379 {
4380 if (focus)
4381 gtk_im_context_focus_in(xic);
4382 else
4383 gtk_im_context_focus_out(xic);
4384 }
4385}
4386
4387 void
4388im_set_position(int row, int col)
4389{
4390 if (xic != NULL)
4391 {
4392 GdkRectangle area;
4393
4394 area.x = FILL_X(col);
4395 area.y = FILL_Y(row);
4396 area.width = gui.char_width * (mb_lefthalve(row, col) ? 2 : 1);
4397 area.height = gui.char_height;
4398
4399 gtk_im_context_set_cursor_location(xic, &area);
4400 }
4401}
4402
4403# if 0 || defined(PROTO) /* apparently only used in gui_x11.c */
4404 void
4405xim_set_preedit(void)
4406{
4407 im_set_position(gui.row, gui.col);
4408}
4409# endif
4410
4411 static void
4412im_add_to_input(char_u *str, int len)
4413{
4414 /* Convert from 'termencoding' (always "utf-8") to 'encoding' */
4415 if (input_conv.vc_type != CONV_NONE)
4416 {
4417 str = string_convert(&input_conv, str, &len);
4418 g_return_if_fail(str != NULL);
4419 }
4420
4421 add_to_input_buf_csi(str, len);
4422
4423 if (input_conv.vc_type != CONV_NONE)
4424 vim_free(str);
4425
4426 if (p_mh) /* blank out the pointer if necessary */
4427 gui_mch_mousehide(TRUE);
4428}
4429
4430 static void
4431im_delete_preedit(void)
4432{
4433 char_u bskey[] = {CSI, 'k', 'b'};
4434 char_u delkey[] = {CSI, 'k', 'D'};
4435
4436 if (State & NORMAL)
4437 {
4438 im_preedit_cursor = 0;
4439 return;
4440 }
4441 for (; im_preedit_cursor > 0; --im_preedit_cursor)
4442 add_to_input_buf(bskey, (int)sizeof(bskey));
4443
4444 for (; im_preedit_trailing > 0; --im_preedit_trailing)
4445 add_to_input_buf(delkey, (int)sizeof(delkey));
4446}
4447
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004448/*
4449 * Move the cursor left by "num_move_back" characters.
4450 * Note that ins_left() checks im_is_preediting() to avoid breaking undo for
4451 * these K_LEFT keys.
4452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 static void
4454im_correct_cursor(int num_move_back)
4455{
4456 char_u backkey[] = {CSI, 'k', 'l'};
4457
4458 if (State & NORMAL)
4459 return;
4460# ifdef FEAT_RIGHTLEFT
4461 if ((State & CMDLINE) == 0 && curwin != NULL && curwin->w_p_rl)
4462 backkey[2] = 'r';
4463# endif
4464 for (; num_move_back > 0; --num_move_back)
4465 add_to_input_buf(backkey, (int)sizeof(backkey));
4466}
4467
4468static int xim_expected_char = NUL;
4469static int xim_ignored_char = FALSE;
4470
4471/*
4472 * Update the mode and cursor while in an IM callback.
4473 */
4474 static void
4475im_show_info(void)
4476{
4477 int old_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 old_vgetc_busy = vgetc_busy;
4480 vgetc_busy = TRUE;
4481 showmode();
4482 vgetc_busy = old_vgetc_busy;
4483 setcursor();
4484 out_flush();
4485}
4486
4487/*
4488 * Callback invoked when the user finished preediting.
4489 * Put the final string into the input buffer.
4490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004492im_commit_cb(GtkIMContext *context UNUSED,
4493 const gchar *str,
4494 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495{
Bram Moolenaar72597a52010-07-18 15:31:08 +02004496 int slen = (int)STRLEN(str);
4497 int add_to_input = TRUE;
4498 int clen;
4499 int len = slen;
4500 int commit_with_preedit = TRUE;
4501 char_u *im_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503#ifdef XIM_DEBUG
4504 xim_log("im_commit_cb(): %s\n", str);
4505#endif
4506
4507 /* The imhangul module doesn't reset the preedit string before
4508 * committing. Call im_delete_preedit() to work around that. */
4509 im_delete_preedit();
4510
4511 /* Indicate that preediting has finished. */
4512 if (preedit_start_col == MAXCOL)
4513 {
4514 init_preedit_start_col();
4515 commit_with_preedit = FALSE;
4516 }
4517
4518 /* The thing which setting "preedit_start_col" to MAXCOL means that
4519 * "preedit_start_col" will be set forcely when calling
4520 * preedit_changed_cb() next time.
4521 * "preedit_start_col" should not reset with MAXCOL on this part. Vim
4522 * is simulating the preediting by using add_to_input_str(). when
4523 * preedit begin immediately before committed, the typebuf is not
4524 * flushed to screen, then it can't get correct "preedit_start_col".
4525 * Thus, it should calculate the cells by adding cells of the committed
4526 * string. */
4527 if (input_conv.vc_type != CONV_NONE)
4528 {
4529 im_str = string_convert(&input_conv, (char_u *)str, &len);
4530 g_return_if_fail(im_str != NULL);
4531 }
4532 else
4533 im_str = (char_u *)str;
Bram Moolenaar72597a52010-07-18 15:31:08 +02004534
4535 clen = mb_string2cells(im_str, len);
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (input_conv.vc_type != CONV_NONE)
4538 vim_free(im_str);
4539 preedit_start_col += clen;
4540
4541 /* Is this a single character that matches a keypad key that's just
4542 * been pressed? If so, we don't want it to be entered as such - let
4543 * us carry on processing the raw keycode so that it may be used in
4544 * mappings as <kSomething>. */
4545 if (xim_expected_char != NUL)
4546 {
4547 /* We're currently processing a keypad or other special key */
4548 if (slen == 1 && str[0] == xim_expected_char)
4549 {
4550 /* It's a match - don't do it here */
4551 xim_ignored_char = TRUE;
4552 add_to_input = FALSE;
4553 }
4554 else
4555 {
4556 /* Not a match */
4557 xim_ignored_char = FALSE;
4558 }
4559 }
4560
4561 if (add_to_input)
4562 im_add_to_input((char_u *)str, slen);
4563
4564 /* Inserting chars while "im_is_active" is set does not cause a change of
4565 * buffer. When the chars are committed the buffer must be marked as
4566 * changed. */
4567 if (!commit_with_preedit)
4568 preedit_start_col = MAXCOL;
4569
4570 /* This flag is used in changed() at next call. */
4571 xim_changed_while_preediting = TRUE;
4572
4573 if (gtk_main_level() > 0)
4574 gtk_main_quit();
4575}
4576
4577/*
4578 * Callback invoked after start to the preedit.
4579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004581im_preedit_start_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
4583#ifdef XIM_DEBUG
4584 xim_log("im_preedit_start_cb()\n");
4585#endif
4586
4587 im_is_active = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00004588 preedit_is_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarc236c162008-07-13 17:41:49 +00004590 im_show_info();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591}
4592
4593/*
4594 * Callback invoked after end to the preedit.
4595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004597im_preedit_end_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
4599#ifdef XIM_DEBUG
4600 xim_log("im_preedit_end_cb()\n");
4601#endif
4602 im_delete_preedit();
4603
4604 /* Indicate that preediting has finished */
4605 preedit_start_col = MAXCOL;
4606 xim_has_preediting = FALSE;
4607
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004608#if 0
4609 /* Removal of this line suggested by Takuhiro Nishioka. Fixes that IM was
Bram Moolenaarc236c162008-07-13 17:41:49 +00004610 * switched off unintentionally. We now use preedit_is_active (added by
4611 * SungHyun Nam). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 im_is_active = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004613#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00004614 preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 gui_update_cursor(TRUE, FALSE);
4616 im_show_info();
4617}
4618
4619/*
4620 * Callback invoked after changes to the preedit string. If the preedit
4621 * string was empty before, remember the preedit start column so we know
4622 * where to apply feedback attributes. Delete the previous preedit string
4623 * if there was one, save the new preedit cursor offset, and put the new
4624 * string into the input buffer.
4625 *
4626 * TODO: The pragmatic "put into input buffer" approach used here has
4627 * several fundamental problems:
4628 *
4629 * - The characters in the preedit string are subject to remapping.
4630 * That's broken, only the finally committed string should be remapped.
4631 *
4632 * - There is a race condition involved: The retrieved value for the
4633 * current cursor position will be wrong if any unprocessed characters
4634 * are still queued in the input buffer.
4635 *
4636 * - Due to the lack of synchronization between the file buffer in memory
4637 * and any typed characters, it's practically impossible to implement the
4638 * "retrieve_surrounding" and "delete_surrounding" signals reliably. IM
4639 * modules for languages such as Thai are likely to rely on this feature
4640 * for proper operation.
4641 *
4642 * Conclusions: I think support for preediting needs to be moved to the
4643 * core parts of Vim. Ideally, until it has been committed, the preediting
4644 * string should only be displayed and not affect the buffer content at all.
4645 * The question how to deal with the synchronization issue still remains.
4646 * Circumventing the input buffer is probably not desirable. Anyway, I think
4647 * implementing "retrieve_surrounding" is the only hard problem.
4648 *
4649 * One way to solve all of this in a clean manner would be to queue all key
4650 * press/release events "as is" in the input buffer, and apply the IM filtering
4651 * at the receiving end of the queue. This, however, would have a rather large
4652 * impact on the code base. If there is an easy way to force processing of all
4653 * remaining input from within the "retrieve_surrounding" signal handler, this
4654 * might not be necessary. Gotta ask on vim-dev for opinions.
4655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004657im_preedit_changed_cb(GtkIMContext *context, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
4659 char *preedit_string = NULL;
4660 int cursor_index = 0;
4661 int num_move_back = 0;
4662 char_u *str;
4663 char_u *p;
4664 int i;
4665
4666 gtk_im_context_get_preedit_string(context,
4667 &preedit_string, NULL,
4668 &cursor_index);
4669
4670#ifdef XIM_DEBUG
4671 xim_log("im_preedit_changed_cb(): %s\n", preedit_string);
4672#endif
4673
4674 g_return_if_fail(preedit_string != NULL); /* just in case */
4675
4676 /* If preedit_start_col is MAXCOL set it to the current cursor position. */
4677 if (preedit_start_col == MAXCOL && preedit_string[0] != '\0')
4678 {
4679 xim_has_preediting = TRUE;
4680
4681 /* Urgh, this breaks if the input buffer isn't empty now */
4682 init_preedit_start_col();
4683 }
4684 else if (cursor_index == 0 && preedit_string[0] == '\0')
4685 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004686 xim_has_preediting = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 /* If at the start position (after typing backspace)
4689 * preedit_start_col must be reset. */
4690 preedit_start_col = MAXCOL;
4691 }
4692
4693 im_delete_preedit();
4694
4695 /*
4696 * Compute the end of the preediting area: "preedit_end_col".
4697 * According to the documentation of gtk_im_context_get_preedit_string(),
4698 * the cursor_pos output argument returns the offset in bytes. This is
4699 * unfortunately not true -- real life shows the offset is in characters,
4700 * and the GTK+ source code agrees with me. Will file a bug later.
4701 */
4702 if (preedit_start_col != MAXCOL)
4703 preedit_end_col = preedit_start_col;
4704 str = (char_u *)preedit_string;
4705 for (p = str, i = 0; *p != NUL; p += utf_byte2len(*p), ++i)
4706 {
4707 int is_composing;
4708
4709 is_composing = ((*p & 0x80) != 0 && utf_iscomposing(utf_ptr2char(p)));
4710 /*
4711 * These offsets are used as counters when generating <BS> and <Del>
4712 * to delete the preedit string. So don't count composing characters
4713 * unless 'delcombine' is enabled.
4714 */
4715 if (!is_composing || p_deco)
4716 {
4717 if (i < cursor_index)
4718 ++im_preedit_cursor;
4719 else
4720 ++im_preedit_trailing;
4721 }
4722 if (!is_composing && i >= cursor_index)
4723 {
4724 /* This is essentially the same as im_preedit_trailing, except
4725 * composing characters are not counted even if p_deco is set. */
4726 ++num_move_back;
4727 }
4728 if (preedit_start_col != MAXCOL)
4729 preedit_end_col += utf_ptr2cells(p);
4730 }
4731
4732 if (p > str)
4733 {
4734 im_add_to_input(str, (int)(p - str));
4735 im_correct_cursor(num_move_back);
4736 }
4737
4738 g_free(preedit_string);
4739
4740 if (gtk_main_level() > 0)
4741 gtk_main_quit();
4742}
4743
4744/*
4745 * Translate the Pango attributes at iter to Vim highlighting attributes.
4746 * Ignore attributes not supported by Vim highlighting. This shouldn't have
4747 * too much impact -- right now we handle even more attributes than necessary
4748 * for the IM modules I tested with.
4749 */
4750 static int
4751translate_pango_attributes(PangoAttrIterator *iter)
4752{
4753 PangoAttribute *attr;
4754 int char_attr = HL_NORMAL;
4755
4756 attr = pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE);
4757 if (attr != NULL && ((PangoAttrInt *)attr)->value
4758 != (int)PANGO_UNDERLINE_NONE)
4759 char_attr |= HL_UNDERLINE;
4760
4761 attr = pango_attr_iterator_get(iter, PANGO_ATTR_WEIGHT);
4762 if (attr != NULL && ((PangoAttrInt *)attr)->value >= (int)PANGO_WEIGHT_BOLD)
4763 char_attr |= HL_BOLD;
4764
4765 attr = pango_attr_iterator_get(iter, PANGO_ATTR_STYLE);
4766 if (attr != NULL && ((PangoAttrInt *)attr)->value
4767 != (int)PANGO_STYLE_NORMAL)
4768 char_attr |= HL_ITALIC;
4769
4770 attr = pango_attr_iterator_get(iter, PANGO_ATTR_BACKGROUND);
4771 if (attr != NULL)
4772 {
4773 const PangoColor *color = &((PangoAttrColor *)attr)->color;
4774
4775 /* Assume inverse if black background is requested */
4776 if ((color->red | color->green | color->blue) == 0)
4777 char_attr |= HL_INVERSE;
4778 }
4779
4780 return char_attr;
4781}
4782
4783/*
4784 * Retrieve the highlighting attributes at column col in the preedit string.
4785 * Return -1 if not in preediting mode or if col is out of range.
4786 */
4787 int
4788im_get_feedback_attr(int col)
4789{
4790 char *preedit_string = NULL;
4791 PangoAttrList *attr_list = NULL;
4792 int char_attr = -1;
4793
4794 if (xic == NULL)
4795 return char_attr;
4796
4797 gtk_im_context_get_preedit_string(xic, &preedit_string, &attr_list, NULL);
4798
4799 if (preedit_string != NULL && attr_list != NULL)
4800 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004801 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803 /* Get the byte index as used by PangoAttrIterator */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004804 for (idx = 0; col > 0 && preedit_string[idx] != '\0'; --col)
4805 idx += utfc_ptr2len((char_u *)preedit_string + idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
Bram Moolenaar89d40322006-08-29 15:30:07 +00004807 if (preedit_string[idx] != '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
4809 PangoAttrIterator *iter;
4810 int start, end;
4811
4812 char_attr = HL_NORMAL;
4813 iter = pango_attr_list_get_iterator(attr_list);
4814
4815 /* Extract all relevant attributes from the list. */
4816 do
4817 {
4818 pango_attr_iterator_range(iter, &start, &end);
4819
Bram Moolenaar89d40322006-08-29 15:30:07 +00004820 if (idx >= start && idx < end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 char_attr |= translate_pango_attributes(iter);
4822 }
4823 while (pango_attr_iterator_next(iter));
4824
4825 pango_attr_iterator_destroy(iter);
4826 }
4827 }
4828
4829 if (attr_list != NULL)
4830 pango_attr_list_unref(attr_list);
4831 g_free(preedit_string);
4832
4833 return char_attr;
4834}
4835
4836 void
4837xim_init(void)
4838{
4839#ifdef XIM_DEBUG
4840 xim_log("xim_init()\n");
4841#endif
4842
4843 g_return_if_fail(gui.drawarea != NULL);
4844 g_return_if_fail(gui.drawarea->window != NULL);
4845
4846 xic = gtk_im_multicontext_new();
4847 g_object_ref(xic);
4848
4849 im_commit_handler_id = g_signal_connect(G_OBJECT(xic), "commit",
4850 G_CALLBACK(&im_commit_cb), NULL);
4851 g_signal_connect(G_OBJECT(xic), "preedit_changed",
4852 G_CALLBACK(&im_preedit_changed_cb), NULL);
4853 g_signal_connect(G_OBJECT(xic), "preedit_start",
4854 G_CALLBACK(&im_preedit_start_cb), NULL);
4855 g_signal_connect(G_OBJECT(xic), "preedit_end",
4856 G_CALLBACK(&im_preedit_end_cb), NULL);
4857
4858 gtk_im_context_set_client_window(xic, gui.drawarea->window);
4859}
4860
4861 void
4862im_shutdown(void)
4863{
4864#ifdef XIM_DEBUG
4865 xim_log("im_shutdown()\n");
4866#endif
4867
4868 if (xic != NULL)
4869 {
4870 gtk_im_context_focus_out(xic);
4871 g_object_unref(xic);
4872 xic = NULL;
4873 }
4874 im_is_active = FALSE;
4875 im_commit_handler_id = 0;
4876 preedit_start_col = MAXCOL;
4877 xim_has_preediting = FALSE;
4878}
4879
4880/*
4881 * Convert the string argument to keyval and state for GdkEventKey.
4882 * If str is valid return TRUE, otherwise FALSE.
4883 *
4884 * See 'imactivatekey' for documentation of the format.
4885 */
4886 static int
4887im_string_to_keyval(const char *str, unsigned int *keyval, unsigned int *state)
4888{
4889 const char *mods_end;
4890 unsigned tmp_keyval;
4891 unsigned tmp_state = 0;
4892
4893 mods_end = strrchr(str, '-');
4894 mods_end = (mods_end != NULL) ? mods_end + 1 : str;
4895
4896 /* Parse modifier keys */
4897 while (str < mods_end)
4898 switch (*str++)
4899 {
4900 case '-': break;
4901 case 'S': case 's': tmp_state |= (unsigned)GDK_SHIFT_MASK; break;
4902 case 'L': case 'l': tmp_state |= (unsigned)GDK_LOCK_MASK; break;
4903 case 'C': case 'c': tmp_state |= (unsigned)GDK_CONTROL_MASK;break;
4904 case '1': tmp_state |= (unsigned)GDK_MOD1_MASK; break;
4905 case '2': tmp_state |= (unsigned)GDK_MOD2_MASK; break;
4906 case '3': tmp_state |= (unsigned)GDK_MOD3_MASK; break;
4907 case '4': tmp_state |= (unsigned)GDK_MOD4_MASK; break;
4908 case '5': tmp_state |= (unsigned)GDK_MOD5_MASK; break;
4909 default:
4910 return FALSE;
4911 }
4912
4913 tmp_keyval = gdk_keyval_from_name(str);
4914
4915 if (tmp_keyval == 0 || tmp_keyval == GDK_VoidSymbol)
4916 return FALSE;
4917
4918 if (keyval != NULL)
4919 *keyval = tmp_keyval;
4920 if (state != NULL)
4921 *state = tmp_state;
4922
4923 return TRUE;
4924}
4925
4926/*
4927 * Return TRUE if p_imak is valid, otherwise FALSE. As a special case, an
4928 * empty string is also regarded as valid.
4929 *
4930 * Note: The numerical key value of p_imak is cached if it was valid; thus
4931 * boldly assuming im_xim_isvalid_imactivate() will always be called whenever
4932 * 'imak' changes. This is currently the case but not obvious -- should
4933 * probably rename the function for clarity.
4934 */
4935 int
4936im_xim_isvalid_imactivate(void)
4937{
4938 if (p_imak[0] == NUL)
4939 {
4940 im_activatekey_keyval = GDK_VoidSymbol;
4941 im_activatekey_state = 0;
4942 return TRUE;
4943 }
4944
4945 return im_string_to_keyval((const char *)p_imak,
4946 &im_activatekey_keyval,
4947 &im_activatekey_state);
4948}
4949
4950 static void
4951im_synthesize_keypress(unsigned int keyval, unsigned int state)
4952{
4953 GdkEventKey *event;
4954
4955# ifdef HAVE_GTK_MULTIHEAD
4956 event = (GdkEventKey *)gdk_event_new(GDK_KEY_PRESS);
4957 g_object_ref(gui.drawarea->window); /* unreffed by gdk_event_free() */
4958# else
4959 event = (GdkEventKey *)g_malloc0((gulong)sizeof(GdkEvent));
4960 event->type = GDK_KEY_PRESS;
4961# endif
4962 event->window = gui.drawarea->window;
4963 event->send_event = TRUE;
4964 event->time = GDK_CURRENT_TIME;
4965 event->state = state;
4966 event->keyval = keyval;
4967 event->hardware_keycode = /* needed for XIM */
4968 XKeysymToKeycode(GDK_WINDOW_XDISPLAY(event->window), (KeySym)keyval);
4969 event->length = 0;
4970 event->string = NULL;
4971
4972 gtk_im_context_filter_keypress(xic, event);
4973
4974 /* For consistency, also send the corresponding release event. */
4975 event->type = GDK_KEY_RELEASE;
4976 event->send_event = FALSE;
4977 gtk_im_context_filter_keypress(xic, event);
4978
4979# ifdef HAVE_GTK_MULTIHEAD
4980 gdk_event_free((GdkEvent *)event);
4981# else
4982 g_free(event);
4983# endif
4984}
4985
4986 void
4987xim_reset(void)
4988{
4989 if (xic != NULL)
4990 {
4991 /*
4992 * The third-party imhangul module (and maybe others too) ignores
4993 * gtk_im_context_reset() or at least doesn't reset the active state.
4994 * Thus sending imactivatekey would turn it off if it was on before,
4995 * which is clearly not what we want. Fortunately we can work around
4996 * that for imhangul by sending GDK_Escape, but I don't know if it
4997 * works with all IM modules that support an activation key :/
4998 *
4999 * An alternative approach would be to destroy the IM context and
5000 * recreate it. But that means loading/unloading the IM module on
Bram Moolenaarb382ad12010-05-21 15:46:35 +02005001 * every mode switch, which causes a quite noticeable delay even on
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 * my rather fast box...
5003 * *
5004 * Moreover, there are some XIM which cannot respond to
5005 * im_synthesize_keypress(). we hope that they reset by
5006 * xim_shutdown().
5007 */
5008 if (im_activatekey_keyval != GDK_VoidSymbol && im_is_active)
5009 im_synthesize_keypress(GDK_Escape, 0U);
5010
5011 gtk_im_context_reset(xic);
5012
5013 /*
5014 * HACK for Ami: This sequence of function calls makes Ami handle
Bram Moolenaar29466f22007-05-10 17:45:37 +00005015 * the IM reset graciously, without breaking loads of other stuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * It seems to force English mode as well, which is exactly what we
5017 * want because it makes the Ami status display work reliably.
5018 */
5019 gtk_im_context_set_use_preedit(xic, FALSE);
5020
5021 if (p_imdisable)
5022 im_shutdown();
5023 else
5024 {
5025 gtk_im_context_set_use_preedit(xic, TRUE);
5026 xim_set_focus(gui.in_focus);
5027
5028 if (im_activatekey_keyval != GDK_VoidSymbol)
5029 {
5030 if (im_is_active)
5031 {
5032 g_signal_handler_block(xic, im_commit_handler_id);
5033 im_synthesize_keypress(im_activatekey_keyval,
5034 im_activatekey_state);
5035 g_signal_handler_unblock(xic, im_commit_handler_id);
5036 }
5037 }
5038 else
5039 {
5040 im_shutdown();
5041 xim_init();
5042 xim_set_focus(gui.in_focus);
5043 }
5044 }
5045 }
5046
5047 preedit_start_col = MAXCOL;
5048 xim_has_preediting = FALSE;
5049}
5050
5051 int
5052xim_queue_key_press_event(GdkEventKey *event, int down)
5053{
5054 if (down)
5055 {
5056 /*
5057 * Workaround GTK2 XIM 'feature' that always converts keypad keys to
5058 * chars., even when not part of an IM sequence (ref. feature of
5059 * gdk/gdkkeyuni.c).
5060 * Flag any keypad keys that might represent a single char.
5061 * If this (on its own - i.e., not part of an IM sequence) is
5062 * committed while we're processing one of these keys, we can ignore
5063 * that commit and go ahead & process it ourselves. That way we can
5064 * still distinguish keypad keys for use in mappings.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005065 * Also add GDK_space to make <S-Space> work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 */
5067 switch (event->keyval)
5068 {
5069 case GDK_KP_Add: xim_expected_char = '+'; break;
5070 case GDK_KP_Subtract: xim_expected_char = '-'; break;
5071 case GDK_KP_Divide: xim_expected_char = '/'; break;
5072 case GDK_KP_Multiply: xim_expected_char = '*'; break;
5073 case GDK_KP_Decimal: xim_expected_char = '.'; break;
5074 case GDK_KP_Equal: xim_expected_char = '='; break;
5075 case GDK_KP_0: xim_expected_char = '0'; break;
5076 case GDK_KP_1: xim_expected_char = '1'; break;
5077 case GDK_KP_2: xim_expected_char = '2'; break;
5078 case GDK_KP_3: xim_expected_char = '3'; break;
5079 case GDK_KP_4: xim_expected_char = '4'; break;
5080 case GDK_KP_5: xim_expected_char = '5'; break;
5081 case GDK_KP_6: xim_expected_char = '6'; break;
5082 case GDK_KP_7: xim_expected_char = '7'; break;
5083 case GDK_KP_8: xim_expected_char = '8'; break;
5084 case GDK_KP_9: xim_expected_char = '9'; break;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005085 case GDK_space: xim_expected_char = ' '; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 default: xim_expected_char = NUL;
5087 }
5088 xim_ignored_char = FALSE;
5089 }
5090
5091 /*
5092 * When typing fFtT, XIM may be activated. Thus it must pass
5093 * gtk_im_context_filter_keypress() in Normal mode.
5094 * And while doing :sh too.
5095 */
5096 if (xic != NULL && !p_imdisable
5097 && (State & (INSERT | CMDLINE | NORMAL | EXTERNCMD)) != 0)
5098 {
5099 /*
5100 * Filter 'imactivatekey' and map it to CTRL-^. This way, Vim is
5101 * always aware of the current status of IM, and can even emulate
5102 * the activation key for modules that don't support one.
5103 */
5104 if (event->keyval == im_activatekey_keyval
5105 && (event->state & im_activatekey_state) == im_activatekey_state)
5106 {
5107 unsigned int state_mask;
5108
5109 /* Require the state of the 3 most used modifiers to match exactly.
5110 * Otherwise e.g. <S-C-space> would be unusable for other purposes
5111 * if the IM activate key is <S-space>. */
5112 state_mask = im_activatekey_state;
5113 state_mask |= ((int)GDK_SHIFT_MASK | (int)GDK_CONTROL_MASK
5114 | (int)GDK_MOD1_MASK);
5115
5116 if ((event->state & state_mask) != im_activatekey_state)
5117 return FALSE;
5118
5119 /* Don't send it a second time on GDK_KEY_RELEASE. */
5120 if (event->type != GDK_KEY_PRESS)
5121 return TRUE;
5122
Bram Moolenaar658b74a2006-03-18 21:33:02 +00005123 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 im_set_active(FALSE);
5126
5127 /* ":lmap" mappings exists, toggle use of mappings. */
5128 State ^= LANGMAP;
5129 if (State & LANGMAP)
5130 {
5131 curbuf->b_p_iminsert = B_IMODE_NONE;
5132 State &= ~LANGMAP;
5133 }
5134 else
5135 {
5136 curbuf->b_p_iminsert = B_IMODE_LMAP;
5137 State |= LANGMAP;
5138 }
5139 return TRUE;
5140 }
5141
5142 return gtk_im_context_filter_keypress(xic, event);
5143 }
5144
5145 /* Don't filter events through the IM context if IM isn't active
5146 * right now. Unlike with GTK+ 1.2 we cannot rely on the IM module
5147 * not doing anything before the activation key was sent. */
5148 if (im_activatekey_keyval == GDK_VoidSymbol || im_is_active)
5149 {
5150 int imresult = gtk_im_context_filter_keypress(xic, event);
5151
5152 /* Some XIM send following sequence:
5153 * 1. preedited string.
5154 * 2. committed string.
5155 * 3. line changed key.
5156 * 4. preedited string.
5157 * 5. remove preedited string.
5158 * if 3, Vim can't move back the above line for 5.
5159 * thus, this part should not parse the key. */
5160 if (!imresult && preedit_start_col != MAXCOL
5161 && event->keyval == GDK_Return)
5162 {
5163 im_synthesize_keypress(GDK_Return, 0U);
5164 return FALSE;
5165 }
5166
5167 /* If XIM tried to commit a keypad key as a single char.,
5168 * ignore it so we can use the keypad key 'raw', for mappings. */
5169 if (xim_expected_char != NUL && xim_ignored_char)
5170 /* We had a keypad key, and XIM tried to thieve it */
5171 return FALSE;
5172
5173 /* Normal processing */
5174 return imresult;
5175 }
5176 }
5177
5178 return FALSE;
5179}
5180
5181 int
5182im_get_status(void)
5183{
5184 return im_is_active;
5185}
5186
Bram Moolenaar64404472010-06-26 06:24:45 +02005187 int
5188preedit_get_status(void)
5189{
5190 return preedit_is_active;
5191}
5192
5193 int
5194im_is_preediting()
5195{
5196 return xim_has_preediting;
5197}
5198
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005199# else /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
5201static int xim_is_active = FALSE; /* XIM should be active in the current
5202 mode */
5203static int xim_has_focus = FALSE; /* XIM is really being used for Vim */
5204#ifdef FEAT_GUI_X11
5205static XIMStyle input_style;
5206static int status_area_enabled = TRUE;
5207#endif
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209/*
5210 * Switch using XIM on/off. This is used by the code that changes "State".
5211 */
5212 void
5213im_set_active(active)
5214 int active;
5215{
5216 if (xic == NULL)
5217 return;
5218
5219 /* If 'imdisable' is set, XIM is never active. */
5220 if (p_imdisable)
5221 active = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005222#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 else if (input_style & XIMPreeditPosition)
5224 /* There is a problem in switching XIM off when preediting is used,
5225 * and it is not clear how this can be solved. For now, keep XIM on
5226 * all the time, like it was done in Vim 5.8. */
5227 active = TRUE;
5228#endif
5229
5230 /* Remember the active state, it is needed when Vim gets keyboard focus. */
5231 xim_is_active = active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 xim_set_preedit();
5233}
5234
5235/*
5236 * Adjust using XIM for gaining or losing keyboard focus. Also called when
5237 * "xim_is_active" changes.
5238 */
5239 void
5240xim_set_focus(focus)
5241 int focus;
5242{
5243 if (xic == NULL)
5244 return;
5245
5246 /*
5247 * XIM only gets focus when the Vim window has keyboard focus and XIM has
5248 * been set active for the current mode.
5249 */
5250 if (focus && xim_is_active)
5251 {
5252 if (!xim_has_focus)
5253 {
5254 xim_has_focus = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 XSetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
5257 }
5258 else
5259 {
5260 if (xim_has_focus)
5261 {
5262 xim_has_focus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 XUnsetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265 }
5266}
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 void
5269im_set_position(row, col)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005270 int row UNUSED;
5271 int col UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 xim_set_preedit();
5274}
5275
5276/*
5277 * Set the XIM to the current cursor position.
5278 */
5279 void
5280xim_set_preedit()
5281{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005282 XVaNestedList attr_list;
5283 XRectangle spot_area;
5284 XPoint over_spot;
5285 int line_space;
5286
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02005287 if (xic == NULL)
5288 return;
5289
5290 xim_set_focus(TRUE);
5291
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005292 if (!xim_has_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005294 /* hide XIM cursor */
5295 over_spot.x = 0;
5296 over_spot.y = -100; /* arbitrary invisible position */
5297 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5298 XNSpotLocation,
5299 &over_spot,
5300 NULL);
5301 XSetICValues(xic, XNPreeditAttributes, attr_list, NULL);
5302 XFree(attr_list);
5303 return;
5304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005306 if (input_style & XIMPreeditPosition)
5307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 if (xim_fg_color == INVALCOLOR)
5309 {
5310 xim_fg_color = gui.def_norm_pixel;
5311 xim_bg_color = gui.def_back_pixel;
5312 }
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005313 over_spot.x = TEXT_X(gui.col);
5314 over_spot.y = TEXT_Y(gui.row);
5315 spot_area.x = 0;
5316 spot_area.y = 0;
5317 spot_area.height = gui.char_height * Rows;
5318 spot_area.width = gui.char_width * Columns;
5319 line_space = gui.char_height;
5320 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5321 XNSpotLocation, &over_spot,
5322 XNForeground, (Pixel) xim_fg_color,
5323 XNBackground, (Pixel) xim_bg_color,
5324 XNArea, &spot_area,
5325 XNLineSpace, line_space,
5326 NULL);
5327 if (XSetICValues(xic, XNPreeditAttributes, attr_list, NULL))
5328 EMSG(_("E284: Cannot set IC values"));
5329 XFree(attr_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331}
5332
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005333#if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334static char e_xim[] = N_("E285: Failed to create input context");
5335#endif
5336
5337#if defined(FEAT_GUI_X11) || defined(PROTO)
5338# if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && !defined(sun)
5339# define USE_X11R6_XIM
5340# endif
5341
5342static int xim_real_init __ARGS((Window x11_window, Display *x11_display));
5343
5344
5345#ifdef USE_X11R6_XIM
5346static void xim_instantiate_cb __ARGS((Display *display, XPointer client_data, XPointer call_data));
5347static void xim_destroy_cb __ARGS((XIM im, XPointer client_data, XPointer call_data));
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 static void
5350xim_instantiate_cb(display, client_data, call_data)
5351 Display *display;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005352 XPointer client_data UNUSED;
5353 XPointer call_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 Window x11_window;
5356 Display *x11_display;
5357
5358#ifdef XIM_DEBUG
5359 xim_log("xim_instantiate_cb()\n");
5360#endif
5361
5362 gui_get_x11_windis(&x11_window, &x11_display);
5363 if (display != x11_display)
5364 return;
5365
5366 xim_real_init(x11_window, x11_display);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005367 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (xic != NULL)
5369 XUnregisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5370 xim_instantiate_cb, NULL);
5371}
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 static void
5374xim_destroy_cb(im, client_data, call_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005375 XIM im UNUSED;
5376 XPointer client_data UNUSED;
5377 XPointer call_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 Window x11_window;
5380 Display *x11_display;
5381
5382#ifdef XIM_DEBUG
5383 xim_log("xim_destroy_cb()\n");
5384#endif
5385 gui_get_x11_windis(&x11_window, &x11_display);
5386
5387 xic = NULL;
5388 status_area_enabled = FALSE;
5389
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005390 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
5392 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5393 xim_instantiate_cb, NULL);
5394}
5395#endif
5396
5397 void
5398xim_init()
5399{
5400 Window x11_window;
5401 Display *x11_display;
5402
5403#ifdef XIM_DEBUG
5404 xim_log("xim_init()\n");
5405#endif
5406
5407 gui_get_x11_windis(&x11_window, &x11_display);
5408
5409 xic = NULL;
5410
5411 if (xim_real_init(x11_window, x11_display))
5412 return;
5413
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005414 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416#ifdef USE_X11R6_XIM
5417 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5418 xim_instantiate_cb, NULL);
5419#endif
5420}
5421
5422 static int
5423xim_real_init(x11_window, x11_display)
5424 Window x11_window;
5425 Display *x11_display;
5426{
5427 int i;
5428 char *p,
5429 *s,
5430 *ns,
5431 *end,
5432 tmp[1024];
5433#define IMLEN_MAX 40
5434 char buf[IMLEN_MAX + 7];
5435 XIM xim = NULL;
5436 XIMStyles *xim_styles;
5437 XIMStyle this_input_style = 0;
5438 Boolean found;
5439 XPoint over_spot;
5440 XVaNestedList preedit_list, status_list;
5441
5442 input_style = 0;
5443 status_area_enabled = FALSE;
5444
5445 if (xic != NULL)
5446 return FALSE;
5447
5448 if (gui.rsrc_input_method != NULL && *gui.rsrc_input_method != NUL)
5449 {
5450 strcpy(tmp, gui.rsrc_input_method);
5451 for (ns = s = tmp; ns != NULL && *s != NUL;)
5452 {
5453 s = (char *)skipwhite((char_u *)s);
5454 if (*s == NUL)
5455 break;
5456 if ((ns = end = strchr(s, ',')) == NULL)
5457 end = s + strlen(s);
5458 while (isspace(((char_u *)end)[-1]))
5459 end--;
5460 *end = NUL;
5461
5462 if (strlen(s) <= IMLEN_MAX)
5463 {
5464 strcpy(buf, "@im=");
5465 strcat(buf, s);
5466 if ((p = XSetLocaleModifiers(buf)) != NULL && *p != NUL
5467 && (xim = XOpenIM(x11_display, NULL, NULL, NULL))
5468 != NULL)
5469 break;
5470 }
5471
5472 s = ns + 1;
5473 }
5474 }
5475
5476 if (xim == NULL && (p = XSetLocaleModifiers("")) != NULL && *p != NUL)
5477 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5478
5479 /* This is supposed to be useful to obtain characters through
5480 * XmbLookupString() without really using a XIM. */
5481 if (xim == NULL && (p = XSetLocaleModifiers("@im=none")) != NULL
5482 && *p != NUL)
5483 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5484
5485 if (xim == NULL)
5486 {
5487 /* Only give this message when verbose is set, because too many people
5488 * got this message when they didn't want to use a XIM. */
5489 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005490 {
5491 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 EMSG(_("E286: Failed to open input method"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005493 verbose_leave();
5494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 return FALSE;
5496 }
5497
5498#ifdef USE_X11R6_XIM
5499 {
5500 XIMCallback destroy_cb;
5501
5502 destroy_cb.callback = xim_destroy_cb;
5503 destroy_cb.client_data = NULL;
5504 if (XSetIMValues(xim, XNDestroyCallback, &destroy_cb, NULL))
5505 EMSG(_("E287: Warning: Could not set destroy callback to IM"));
5506 }
5507#endif
5508
5509 if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL) || !xim_styles)
5510 {
5511 EMSG(_("E288: input method doesn't support any style"));
5512 XCloseIM(xim);
5513 return FALSE;
5514 }
5515
5516 found = False;
5517 strcpy(tmp, gui.rsrc_preedit_type_name);
5518 for (s = tmp; s && !found; )
5519 {
5520 while (*s && isspace((unsigned char)*s))
5521 s++;
5522 if (!*s)
5523 break;
5524 if ((ns = end = strchr(s, ',')) != 0)
5525 ns++;
5526 else
5527 end = s + strlen(s);
5528 while (isspace((unsigned char)*end))
5529 end--;
5530 *end = '\0';
5531
5532 if (!strcmp(s, "OverTheSpot"))
5533 this_input_style = (XIMPreeditPosition | XIMStatusArea);
5534 else if (!strcmp(s, "OffTheSpot"))
5535 this_input_style = (XIMPreeditArea | XIMStatusArea);
5536 else if (!strcmp(s, "Root"))
5537 this_input_style = (XIMPreeditNothing | XIMStatusNothing);
5538
5539 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5540 {
5541 if (this_input_style == xim_styles->supported_styles[i])
5542 {
5543 found = True;
5544 break;
5545 }
5546 }
5547 if (!found)
5548 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5549 {
5550 if ((xim_styles->supported_styles[i] & this_input_style)
5551 == (this_input_style & ~XIMStatusArea))
5552 {
5553 this_input_style &= ~XIMStatusArea;
5554 found = True;
5555 break;
5556 }
5557 }
5558
5559 s = ns;
5560 }
5561 XFree(xim_styles);
5562
5563 if (!found)
5564 {
5565 /* Only give this message when verbose is set, because too many people
5566 * got this message when they didn't want to use a XIM. */
5567 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005568 {
5569 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 EMSG(_("E289: input method doesn't support my preedit type"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005571 verbose_leave();
5572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 XCloseIM(xim);
5574 return FALSE;
5575 }
5576
5577 over_spot.x = TEXT_X(gui.col);
5578 over_spot.y = TEXT_Y(gui.row);
5579 input_style = this_input_style;
5580
5581 /* A crash was reported when trying to pass gui.norm_font as XNFontSet,
5582 * thus that has been removed. Hopefully the default works... */
5583#ifdef FEAT_XFONTSET
5584 if (gui.fontset != NOFONTSET)
5585 {
5586 preedit_list = XVaCreateNestedList(0,
5587 XNSpotLocation, &over_spot,
5588 XNForeground, (Pixel)gui.def_norm_pixel,
5589 XNBackground, (Pixel)gui.def_back_pixel,
5590 XNFontSet, (XFontSet)gui.fontset,
5591 NULL);
5592 status_list = XVaCreateNestedList(0,
5593 XNForeground, (Pixel)gui.def_norm_pixel,
5594 XNBackground, (Pixel)gui.def_back_pixel,
5595 XNFontSet, (XFontSet)gui.fontset,
5596 NULL);
5597 }
5598 else
5599#endif
5600 {
5601 preedit_list = XVaCreateNestedList(0,
5602 XNSpotLocation, &over_spot,
5603 XNForeground, (Pixel)gui.def_norm_pixel,
5604 XNBackground, (Pixel)gui.def_back_pixel,
5605 NULL);
5606 status_list = XVaCreateNestedList(0,
5607 XNForeground, (Pixel)gui.def_norm_pixel,
5608 XNBackground, (Pixel)gui.def_back_pixel,
5609 NULL);
5610 }
5611
5612 xic = XCreateIC(xim,
5613 XNInputStyle, input_style,
5614 XNClientWindow, x11_window,
5615 XNFocusWindow, gui.wid,
5616 XNPreeditAttributes, preedit_list,
5617 XNStatusAttributes, status_list,
5618 NULL);
5619 XFree(status_list);
5620 XFree(preedit_list);
5621 if (xic != NULL)
5622 {
5623 if (input_style & XIMStatusArea)
5624 {
5625 xim_set_status_area();
5626 status_area_enabled = TRUE;
5627 }
5628 else
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005629 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631 else
5632 {
5633 EMSG(_(e_xim));
5634 XCloseIM(xim);
5635 return FALSE;
5636 }
5637
5638 return TRUE;
5639}
5640
5641#endif /* FEAT_GUI_X11 */
5642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643/*
5644 * Get IM status. When IM is on, return TRUE. Else return FALSE.
5645 * FIXME: This doesn't work correctly: Having focus doesn't always mean XIM is
5646 * active, when not having focus XIM may still be active (e.g., when using a
5647 * tear-off menu item).
5648 */
5649 int
5650im_get_status()
5651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 return xim_has_focus;
5653}
5654
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005655# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaar64404472010-06-26 06:24:45 +02005657# if !defined(FEAT_GUI_GTK) || defined(PROTO)
5658/*
5659 * Set up the status area.
5660 *
5661 * This should use a separate Widget, but that seems not possible, because
5662 * preedit_area and status_area should be set to the same window as for the
5663 * text input. Unfortunately this means the status area pollutes the text
5664 * window...
5665 */
5666 void
5667xim_set_status_area()
Bram Moolenaarc236c162008-07-13 17:41:49 +00005668{
Bram Moolenaar64404472010-06-26 06:24:45 +02005669 XVaNestedList preedit_list = 0, status_list = 0, list = 0;
5670 XRectangle pre_area, status_area;
5671
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02005672 if (xic == NULL)
5673 return;
5674
Bram Moolenaar64404472010-06-26 06:24:45 +02005675 if (input_style & XIMStatusArea)
5676 {
5677 if (input_style & XIMPreeditArea)
5678 {
5679 XRectangle *needed_rect;
5680
5681 /* to get status_area width */
5682 status_list = XVaCreateNestedList(0, XNAreaNeeded,
5683 &needed_rect, NULL);
5684 XGetICValues(xic, XNStatusAttributes, status_list, NULL);
5685 XFree(status_list);
5686
5687 status_area.width = needed_rect->width;
5688 }
5689 else
5690 status_area.width = gui.char_width * Columns;
5691
5692 status_area.x = 0;
5693 status_area.y = gui.char_height * Rows + gui.border_offset;
5694 if (gui.which_scrollbars[SBAR_BOTTOM])
5695 status_area.y += gui.scrollbar_height;
5696#ifdef FEAT_MENU
5697 if (gui.menu_is_active)
5698 status_area.y += gui.menu_height;
5699#endif
5700 status_area.height = gui.char_height;
5701 status_list = XVaCreateNestedList(0, XNArea, &status_area, NULL);
5702 }
5703 else
5704 {
5705 status_area.x = 0;
5706 status_area.y = gui.char_height * Rows + gui.border_offset;
5707 if (gui.which_scrollbars[SBAR_BOTTOM])
5708 status_area.y += gui.scrollbar_height;
5709#ifdef FEAT_MENU
5710 if (gui.menu_is_active)
5711 status_area.y += gui.menu_height;
5712#endif
5713 status_area.width = 0;
5714 status_area.height = gui.char_height;
5715 }
5716
5717 if (input_style & XIMPreeditArea) /* off-the-spot */
5718 {
5719 pre_area.x = status_area.x + status_area.width;
5720 pre_area.y = gui.char_height * Rows + gui.border_offset;
5721 pre_area.width = gui.char_width * Columns - pre_area.x;
5722 if (gui.which_scrollbars[SBAR_BOTTOM])
5723 pre_area.y += gui.scrollbar_height;
5724#ifdef FEAT_MENU
5725 if (gui.menu_is_active)
5726 pre_area.y += gui.menu_height;
5727#endif
5728 pre_area.height = gui.char_height;
5729 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
5730 }
5731 else if (input_style & XIMPreeditPosition) /* over-the-spot */
5732 {
5733 pre_area.x = 0;
5734 pre_area.y = 0;
5735 pre_area.height = gui.char_height * Rows;
5736 pre_area.width = gui.char_width * Columns;
5737 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
5738 }
5739
5740 if (preedit_list && status_list)
5741 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
5742 XNStatusAttributes, status_list, NULL);
5743 else if (preedit_list)
5744 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
5745 NULL);
5746 else if (status_list)
5747 list = XVaCreateNestedList(0, XNStatusAttributes, status_list,
5748 NULL);
5749 else
5750 list = NULL;
5751
5752 if (list)
5753 {
5754 XSetICValues(xic, XNVaNestedList, list, NULL);
5755 XFree(list);
5756 }
5757 if (status_list)
5758 XFree(status_list);
5759 if (preedit_list)
5760 XFree(preedit_list);
5761}
5762
5763 int
5764xim_get_status_area_height()
5765{
5766 if (status_area_enabled)
5767 return gui.char_height;
5768 return 0;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005769}
5770# endif
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#endif /* FEAT_XIM */
5773
5774#if defined(FEAT_MBYTE) || defined(PROTO)
5775
5776/*
5777 * Setup "vcp" for conversion from "from" to "to".
5778 * The names must have been made canonical with enc_canonize().
5779 * vcp->vc_type must have been initialized to CONV_NONE.
5780 * Note: cannot be used for conversion from/to ucs-2 and ucs-4 (will use utf-8
5781 * instead).
5782 * Afterwards invoke with "from" and "to" equal to NULL to cleanup.
5783 * Return FAIL when conversion is not supported, OK otherwise.
5784 */
5785 int
5786convert_setup(vcp, from, to)
5787 vimconv_T *vcp;
5788 char_u *from;
5789 char_u *to;
5790{
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005791 return convert_setup_ext(vcp, from, TRUE, to, TRUE);
5792}
5793
5794/*
5795 * As convert_setup(), but only when from_unicode_is_utf8 is TRUE will all
5796 * "from" unicode charsets be considered utf-8. Same for "to".
5797 */
5798 int
5799convert_setup_ext(vcp, from, from_unicode_is_utf8, to, to_unicode_is_utf8)
5800 vimconv_T *vcp;
5801 char_u *from;
5802 int from_unicode_is_utf8;
5803 char_u *to;
5804 int to_unicode_is_utf8;
5805{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 int from_prop;
5807 int to_prop;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005808 int from_is_utf8;
5809 int to_is_utf8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 /* Reset to no conversion. */
5812# ifdef USE_ICONV
5813 if (vcp->vc_type == CONV_ICONV && vcp->vc_fd != (iconv_t)-1)
5814 iconv_close(vcp->vc_fd);
5815# endif
5816 vcp->vc_type = CONV_NONE;
5817 vcp->vc_factor = 1;
5818 vcp->vc_fail = FALSE;
5819
5820 /* No conversion when one of the names is empty or they are equal. */
5821 if (from == NULL || *from == NUL || to == NULL || *to == NUL
5822 || STRCMP(from, to) == 0)
5823 return OK;
5824
5825 from_prop = enc_canon_props(from);
5826 to_prop = enc_canon_props(to);
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005827 if (from_unicode_is_utf8)
5828 from_is_utf8 = from_prop & ENC_UNICODE;
5829 else
5830 from_is_utf8 = from_prop == ENC_UNICODE;
5831 if (to_unicode_is_utf8)
5832 to_is_utf8 = to_prop & ENC_UNICODE;
5833 else
5834 to_is_utf8 = to_prop == ENC_UNICODE;
5835
5836 if ((from_prop & ENC_LATIN1) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
5838 /* Internal latin1 -> utf-8 conversion. */
5839 vcp->vc_type = CONV_TO_UTF8;
5840 vcp->vc_factor = 2; /* up to twice as long */
5841 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005842 else if ((from_prop & ENC_LATIN9) && to_is_utf8)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005843 {
5844 /* Internal latin9 -> utf-8 conversion. */
5845 vcp->vc_type = CONV_9_TO_UTF8;
5846 vcp->vc_factor = 3; /* up to three as long (euro sign) */
5847 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005848 else if (from_is_utf8 && (to_prop & ENC_LATIN1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
5850 /* Internal utf-8 -> latin1 conversion. */
5851 vcp->vc_type = CONV_TO_LATIN1;
5852 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005853 else if (from_is_utf8 && (to_prop & ENC_LATIN9))
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005854 {
5855 /* Internal utf-8 -> latin9 conversion. */
5856 vcp->vc_type = CONV_TO_LATIN9;
5857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858#ifdef WIN3264
5859 /* Win32-specific codepage <-> codepage conversion without iconv. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005860 else if ((from_is_utf8 || encname2codepage(from) > 0)
5861 && (to_is_utf8 || encname2codepage(to) > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
5863 vcp->vc_type = CONV_CODEPAGE;
5864 vcp->vc_factor = 2; /* up to twice as long */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005865 vcp->vc_cpfrom = from_is_utf8 ? 0 : encname2codepage(from);
5866 vcp->vc_cpto = to_is_utf8 ? 0 : encname2codepage(to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
5868#endif
5869#ifdef MACOS_X
5870 else if ((from_prop & ENC_MACROMAN) && (to_prop & ENC_LATIN1))
5871 {
5872 vcp->vc_type = CONV_MAC_LATIN1;
5873 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005874 else if ((from_prop & ENC_MACROMAN) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 {
5876 vcp->vc_type = CONV_MAC_UTF8;
5877 vcp->vc_factor = 2; /* up to twice as long */
5878 }
5879 else if ((from_prop & ENC_LATIN1) && (to_prop & ENC_MACROMAN))
5880 {
5881 vcp->vc_type = CONV_LATIN1_MAC;
5882 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005883 else if (from_is_utf8 && (to_prop & ENC_MACROMAN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
5885 vcp->vc_type = CONV_UTF8_MAC;
5886 }
5887#endif
5888# ifdef USE_ICONV
5889 else
5890 {
5891 /* Use iconv() for conversion. */
5892 vcp->vc_fd = (iconv_t)my_iconv_open(
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005893 to_is_utf8 ? (char_u *)"utf-8" : to,
5894 from_is_utf8 ? (char_u *)"utf-8" : from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (vcp->vc_fd != (iconv_t)-1)
5896 {
5897 vcp->vc_type = CONV_ICONV;
5898 vcp->vc_factor = 4; /* could be longer too... */
5899 }
5900 }
5901# endif
5902 if (vcp->vc_type == CONV_NONE)
5903 return FAIL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005904
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 return OK;
5906}
5907
5908#if defined(FEAT_GUI) || defined(AMIGA) || defined(WIN3264) \
5909 || defined(MSDOS) || defined(PROTO)
5910/*
5911 * Do conversion on typed input characters in-place.
5912 * The input and output are not NUL terminated!
5913 * Returns the length after conversion.
5914 */
5915 int
5916convert_input(ptr, len, maxlen)
5917 char_u *ptr;
5918 int len;
5919 int maxlen;
5920{
5921 return convert_input_safe(ptr, len, maxlen, NULL, NULL);
5922}
5923#endif
5924
5925/*
5926 * Like convert_input(), but when there is an incomplete byte sequence at the
5927 * end return that as an allocated string in "restp" and set "*restlenp" to
5928 * the length. If "restp" is NULL it is not used.
5929 */
5930 int
5931convert_input_safe(ptr, len, maxlen, restp, restlenp)
5932 char_u *ptr;
5933 int len;
5934 int maxlen;
5935 char_u **restp;
5936 int *restlenp;
5937{
5938 char_u *d;
5939 int dlen = len;
5940 int unconvertlen = 0;
5941
5942 d = string_convert_ext(&input_conv, ptr, &dlen,
5943 restp == NULL ? NULL : &unconvertlen);
5944 if (d != NULL)
5945 {
5946 if (dlen <= maxlen)
5947 {
5948 if (unconvertlen > 0)
5949 {
5950 /* Move the unconverted characters to allocated memory. */
5951 *restp = alloc(unconvertlen);
5952 if (*restp != NULL)
5953 mch_memmove(*restp, ptr + len - unconvertlen, unconvertlen);
5954 *restlenp = unconvertlen;
5955 }
5956 mch_memmove(ptr, d, dlen);
5957 }
5958 else
5959 /* result is too long, keep the unconverted text (the caller must
5960 * have done something wrong!) */
5961 dlen = len;
5962 vim_free(d);
5963 }
5964 return dlen;
5965}
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967/*
5968 * Convert text "ptr[*lenp]" according to "vcp".
5969 * Returns the result in allocated memory and sets "*lenp".
5970 * When "lenp" is NULL, use NUL terminated strings.
5971 * Illegal chars are often changed to "?", unless vcp->vc_fail is set.
5972 * When something goes wrong, NULL is returned and "*lenp" is unchanged.
5973 */
5974 char_u *
5975string_convert(vcp, ptr, lenp)
5976 vimconv_T *vcp;
5977 char_u *ptr;
5978 int *lenp;
5979{
5980 return string_convert_ext(vcp, ptr, lenp, NULL);
5981}
5982
5983/*
5984 * Like string_convert(), but when "unconvlenp" is not NULL and there are is
5985 * an incomplete sequence at the end it is not converted and "*unconvlenp" is
5986 * set to the number of remaining bytes.
5987 */
5988 char_u *
5989string_convert_ext(vcp, ptr, lenp, unconvlenp)
5990 vimconv_T *vcp;
5991 char_u *ptr;
5992 int *lenp;
5993 int *unconvlenp;
5994{
5995 char_u *retval = NULL;
5996 char_u *d;
5997 int len;
5998 int i;
5999 int l;
6000 int c;
6001
6002 if (lenp == NULL)
6003 len = (int)STRLEN(ptr);
6004 else
6005 len = *lenp;
6006 if (len == 0)
6007 return vim_strsave((char_u *)"");
6008
6009 switch (vcp->vc_type)
6010 {
6011 case CONV_TO_UTF8: /* latin1 to utf-8 conversion */
6012 retval = alloc(len * 2 + 1);
6013 if (retval == NULL)
6014 break;
6015 d = retval;
6016 for (i = 0; i < len; ++i)
6017 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006018 c = ptr[i];
6019 if (c < 0x80)
6020 *d++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 else
6022 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006023 *d++ = 0xc0 + ((unsigned)c >> 6);
6024 *d++ = 0x80 + (c & 0x3f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 }
6026 }
6027 *d = NUL;
6028 if (lenp != NULL)
6029 *lenp = (int)(d - retval);
6030 break;
6031
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006032 case CONV_9_TO_UTF8: /* latin9 to utf-8 conversion */
6033 retval = alloc(len * 3 + 1);
6034 if (retval == NULL)
6035 break;
6036 d = retval;
6037 for (i = 0; i < len; ++i)
6038 {
6039 c = ptr[i];
6040 switch (c)
6041 {
6042 case 0xa4: c = 0x20ac; break; /* euro */
6043 case 0xa6: c = 0x0160; break; /* S hat */
6044 case 0xa8: c = 0x0161; break; /* S -hat */
6045 case 0xb4: c = 0x017d; break; /* Z hat */
6046 case 0xb8: c = 0x017e; break; /* Z -hat */
6047 case 0xbc: c = 0x0152; break; /* OE */
6048 case 0xbd: c = 0x0153; break; /* oe */
6049 case 0xbe: c = 0x0178; break; /* Y */
6050 }
6051 d += utf_char2bytes(c, d);
6052 }
6053 *d = NUL;
6054 if (lenp != NULL)
6055 *lenp = (int)(d - retval);
6056 break;
6057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 case CONV_TO_LATIN1: /* utf-8 to latin1 conversion */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006059 case CONV_TO_LATIN9: /* utf-8 to latin9 conversion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 retval = alloc(len + 1);
6061 if (retval == NULL)
6062 break;
6063 d = retval;
6064 for (i = 0; i < len; ++i)
6065 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006066 l = utf_ptr2len_len(ptr + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (l == 0)
6068 *d++ = NUL;
6069 else if (l == 1)
6070 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006071 int l_w = utf8len_tab_zero[ptr[i]];
6072
6073 if (l_w == 0)
6074 {
6075 /* Illegal utf-8 byte cannot be converted */
6076 vim_free(retval);
6077 return NULL;
6078 }
6079 if (unconvlenp != NULL && l_w > len - i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 {
6081 /* Incomplete sequence at the end. */
6082 *unconvlenp = len - i;
6083 break;
6084 }
6085 *d++ = ptr[i];
6086 }
6087 else
6088 {
6089 c = utf_ptr2char(ptr + i);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006090 if (vcp->vc_type == CONV_TO_LATIN9)
6091 switch (c)
6092 {
6093 case 0x20ac: c = 0xa4; break; /* euro */
6094 case 0x0160: c = 0xa6; break; /* S hat */
6095 case 0x0161: c = 0xa8; break; /* S -hat */
6096 case 0x017d: c = 0xb4; break; /* Z hat */
6097 case 0x017e: c = 0xb8; break; /* Z -hat */
6098 case 0x0152: c = 0xbc; break; /* OE */
6099 case 0x0153: c = 0xbd; break; /* oe */
6100 case 0x0178: c = 0xbe; break; /* Y */
6101 case 0xa4:
6102 case 0xa6:
6103 case 0xa8:
6104 case 0xb4:
6105 case 0xb8:
6106 case 0xbc:
6107 case 0xbd:
6108 case 0xbe: c = 0x100; break; /* not in latin9 */
6109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (!utf_iscomposing(c)) /* skip composing chars */
6111 {
6112 if (c < 0x100)
6113 *d++ = c;
6114 else if (vcp->vc_fail)
6115 {
6116 vim_free(retval);
6117 return NULL;
6118 }
6119 else
6120 {
6121 *d++ = 0xbf;
6122 if (utf_char2cells(c) > 1)
6123 *d++ = '?';
6124 }
6125 }
6126 i += l - 1;
6127 }
6128 }
6129 *d = NUL;
6130 if (lenp != NULL)
6131 *lenp = (int)(d - retval);
6132 break;
6133
Bram Moolenaar56718732006-03-15 22:53:57 +00006134# ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 case CONV_MAC_LATIN1:
6136 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006137 'm', 'l', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 break;
6139
6140 case CONV_LATIN1_MAC:
6141 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006142 'l', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 break;
6144
6145 case CONV_MAC_UTF8:
6146 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006147 'm', 'u', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 break;
6149
6150 case CONV_UTF8_MAC:
6151 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006152 'u', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 break;
6154# endif
6155
6156# ifdef USE_ICONV
6157 case CONV_ICONV: /* conversion with output_conv.vc_fd */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006158 retval = iconv_string(vcp, ptr, len, unconvlenp, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 break;
6160# endif
6161# ifdef WIN3264
6162 case CONV_CODEPAGE: /* codepage -> codepage */
6163 {
6164 int retlen;
6165 int tmp_len;
6166 short_u *tmp;
6167
6168 /* 1. codepage/UTF-8 -> ucs-2. */
6169 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006170 tmp_len = utf8_to_utf16(ptr, len, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 else
6172 tmp_len = MultiByteToWideChar(vcp->vc_cpfrom, 0,
6173 ptr, len, 0, 0);
6174 tmp = (short_u *)alloc(sizeof(short_u) * tmp_len);
6175 if (tmp == NULL)
6176 break;
6177 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006178 utf8_to_utf16(ptr, len, tmp, unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 else
6180 MultiByteToWideChar(vcp->vc_cpfrom, 0, ptr, len, tmp, tmp_len);
6181
6182 /* 2. ucs-2 -> codepage/UTF-8. */
6183 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006184 retlen = utf16_to_utf8(tmp, tmp_len, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 else
6186 retlen = WideCharToMultiByte(vcp->vc_cpto, 0,
6187 tmp, tmp_len, 0, 0, 0, 0);
6188 retval = alloc(retlen + 1);
6189 if (retval != NULL)
6190 {
6191 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006192 utf16_to_utf8(tmp, tmp_len, retval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 else
6194 WideCharToMultiByte(vcp->vc_cpto, 0,
6195 tmp, tmp_len, retval, retlen, 0, 0);
6196 retval[retlen] = NUL;
6197 if (lenp != NULL)
6198 *lenp = retlen;
6199 }
6200 vim_free(tmp);
6201 break;
6202 }
6203# endif
6204 }
6205
6206 return retval;
6207}
6208#endif