blob: 33fa0a2540783a7c1095c9bb5599dab1f3c0d23e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaarca058dc2014-01-14 13:26:21 +010086# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD)
87# include <X11/Xwindows.h>
88# define WINBYTE wBYTE
89# else
90# include <windows.h>
91# define WINBYTE BYTE
92# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000093# ifdef WIN32
94# undef WIN32 /* Some windows.h define WIN32, we don't want that here. */
95# endif
Bram Moolenaarca058dc2014-01-14 13:26:21 +010096#else
97# define WINBYTE BYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#endif
99
100#if (defined(WIN3264) || defined(WIN32UNIX)) && !defined(__MINGW32__)
101# include <winnls.h>
102#endif
103
104#ifdef FEAT_GUI_X11
105# include <X11/Intrinsic.h>
106#endif
107#ifdef X_LOCALE
108#include <X11/Xlocale.h>
109#endif
110
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200111#if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaar98921892016-02-23 17:14:37 +0100112# if GTK_CHECK_VERSION(3,0,0)
113# include <gdk/gdkkeysyms-compat.h>
114# else
115# include <gdk/gdkkeysyms.h>
116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# ifdef WIN3264
118# include <gdk/gdkwin32.h>
119# else
120# include <gdk/gdkx.h>
121# endif
122#endif
123
124#ifdef HAVE_WCHAR_H
125# include <wchar.h>
126#endif
127
128#if 0
129/* This has been disabled, because several people reported problems with the
130 * wcwidth() and iswprint() library functions, esp. for Hebrew. */
131# ifdef __STDC_ISO_10646__
132# define USE_WCHAR_FUNCTIONS
133# endif
134#endif
135
136#if defined(FEAT_MBYTE) || defined(PROTO)
137
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138static int enc_canon_search(char_u *name);
139static int dbcs_char2len(int c);
140static int dbcs_char2bytes(int c, char_u *buf);
141static int dbcs_ptr2len(char_u *p);
142static int dbcs_ptr2len_len(char_u *p, int size);
143static int utf_ptr2cells_len(char_u *p, int size);
144static int dbcs_char2cells(int c);
145static int dbcs_ptr2cells_len(char_u *p, int size);
146static int dbcs_ptr2char(char_u *p);
147static int utf_safe_read_char_adv(char_u **s, size_t *n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar24397332009-12-02 14:02:39 +0000149/*
150 * Lookup table to quickly get the length in bytes of a UTF-8 character from
151 * the first byte of a UTF-8 string.
152 * Bytes which are illegal when used as the first byte have a 1.
153 * The NUL byte has length 1.
154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static char utf8len_tab[256] =
156{
157 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,
158 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,
159 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,
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,
Bram Moolenaar24397332009-12-02 14:02:39 +0000161 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,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 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,
164 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,
165};
166
167/*
Bram Moolenaar24397332009-12-02 14:02:39 +0000168 * Like utf8len_tab above, but using a zero for illegal lead bytes.
169 */
170static char utf8len_tab_zero[256] =
171{
172 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,
173 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,
174 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,
175 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,
176 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,
177 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,
178 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,
179 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,
180};
181
182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 * XIM often causes trouble. Define XIM_DEBUG to get a log of XIM callbacks
184 * in the "xim.log" file.
185 */
186/* #define XIM_DEBUG */
187#ifdef XIM_DEBUG
188 static void
189xim_log(char *s, ...)
190{
191 va_list arglist;
192 static FILE *fd = NULL;
193
194 if (fd == (FILE *)-1)
195 return;
196 if (fd == NULL)
197 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000198 fd = mch_fopen("xim.log", "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 if (fd == NULL)
200 {
201 EMSG("Cannot open xim.log");
202 fd = (FILE *)-1;
203 return;
204 }
205 }
206
207 va_start(arglist, s);
208 vfprintf(fd, s, arglist);
209 va_end(arglist);
210}
211#endif
212
213#endif
214
215#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
216/*
217 * Canonical encoding names and their properties.
218 * "iso-8859-n" is handled by enc_canonize() directly.
219 */
220static struct
221{ char *name; int prop; int codepage;}
222enc_canon_table[] =
223{
224#define IDX_LATIN_1 0
225 {"latin1", ENC_8BIT + ENC_LATIN1, 1252},
226#define IDX_ISO_2 1
227 {"iso-8859-2", ENC_8BIT, 0},
228#define IDX_ISO_3 2
229 {"iso-8859-3", ENC_8BIT, 0},
230#define IDX_ISO_4 3
231 {"iso-8859-4", ENC_8BIT, 0},
232#define IDX_ISO_5 4
233 {"iso-8859-5", ENC_8BIT, 0},
234#define IDX_ISO_6 5
235 {"iso-8859-6", ENC_8BIT, 0},
236#define IDX_ISO_7 6
237 {"iso-8859-7", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000238#define IDX_ISO_8 7
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {"iso-8859-8", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000240#define IDX_ISO_9 8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {"iso-8859-9", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000242#define IDX_ISO_10 9
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {"iso-8859-10", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000244#define IDX_ISO_11 10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {"iso-8859-11", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000246#define IDX_ISO_13 11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {"iso-8859-13", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000248#define IDX_ISO_14 12
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {"iso-8859-14", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000250#define IDX_ISO_15 13
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000251 {"iso-8859-15", ENC_8BIT + ENC_LATIN9, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000252#define IDX_KOI8_R 14
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {"koi8-r", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000254#define IDX_KOI8_U 15
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {"koi8-u", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000256#define IDX_UTF8 16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {"utf-8", ENC_UNICODE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000258#define IDX_UCS2 17
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 {"ucs-2", ENC_UNICODE + ENC_ENDIAN_B + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000260#define IDX_UCS2LE 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {"ucs-2le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000262#define IDX_UTF16 19
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {"utf-16", ENC_UNICODE + ENC_ENDIAN_B + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000264#define IDX_UTF16LE 20
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {"utf-16le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000266#define IDX_UCS4 21
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 {"ucs-4", ENC_UNICODE + ENC_ENDIAN_B + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000268#define IDX_UCS4LE 22
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {"ucs-4le", ENC_UNICODE + ENC_ENDIAN_L + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000270
271 /* For debugging DBCS encoding on Unix. */
272#define IDX_DEBUG 23
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 {"debug", ENC_DBCS, DBCS_DEBUG},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000274#define IDX_EUC_JP 24
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 {"euc-jp", ENC_DBCS, DBCS_JPNU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000276#define IDX_SJIS 25
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {"sjis", ENC_DBCS, DBCS_JPN},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000278#define IDX_EUC_KR 26
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {"euc-kr", ENC_DBCS, DBCS_KORU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000280#define IDX_EUC_CN 27
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 {"euc-cn", ENC_DBCS, DBCS_CHSU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000282#define IDX_EUC_TW 28
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 {"euc-tw", ENC_DBCS, DBCS_CHTU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000284#define IDX_BIG5 29
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {"big5", ENC_DBCS, DBCS_CHT},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000286
287 /* MS-DOS and MS-Windows codepages are included here, so that they can be
288 * used on Unix too. Most of them are similar to ISO-8859 encodings, but
289 * not exactly the same. */
290#define IDX_CP437 30
291 {"cp437", ENC_8BIT, 437}, /* like iso-8859-1 */
292#define IDX_CP737 31
293 {"cp737", ENC_8BIT, 737}, /* like iso-8859-7 */
294#define IDX_CP775 32
295 {"cp775", ENC_8BIT, 775}, /* Baltic */
296#define IDX_CP850 33
297 {"cp850", ENC_8BIT, 850}, /* like iso-8859-4 */
298#define IDX_CP852 34
299 {"cp852", ENC_8BIT, 852}, /* like iso-8859-1 */
300#define IDX_CP855 35
301 {"cp855", ENC_8BIT, 855}, /* like iso-8859-2 */
302#define IDX_CP857 36
303 {"cp857", ENC_8BIT, 857}, /* like iso-8859-5 */
304#define IDX_CP860 37
305 {"cp860", ENC_8BIT, 860}, /* like iso-8859-9 */
306#define IDX_CP861 38
307 {"cp861", ENC_8BIT, 861}, /* like iso-8859-1 */
308#define IDX_CP862 39
309 {"cp862", ENC_8BIT, 862}, /* like iso-8859-1 */
310#define IDX_CP863 40
311 {"cp863", ENC_8BIT, 863}, /* like iso-8859-8 */
312#define IDX_CP865 41
313 {"cp865", ENC_8BIT, 865}, /* like iso-8859-1 */
314#define IDX_CP866 42
315 {"cp866", ENC_8BIT, 866}, /* like iso-8859-5 */
316#define IDX_CP869 43
317 {"cp869", ENC_8BIT, 869}, /* like iso-8859-7 */
318#define IDX_CP874 44
319 {"cp874", ENC_8BIT, 874}, /* Thai */
320#define IDX_CP932 45
321 {"cp932", ENC_DBCS, DBCS_JPN},
322#define IDX_CP936 46
323 {"cp936", ENC_DBCS, DBCS_CHS},
324#define IDX_CP949 47
325 {"cp949", ENC_DBCS, DBCS_KOR},
326#define IDX_CP950 48
327 {"cp950", ENC_DBCS, DBCS_CHT},
328#define IDX_CP1250 49
329 {"cp1250", ENC_8BIT, 1250}, /* Czech, Polish, etc. */
330#define IDX_CP1251 50
331 {"cp1251", ENC_8BIT, 1251}, /* Cyrillic */
332 /* cp1252 is considered to be equal to latin1 */
333#define IDX_CP1253 51
334 {"cp1253", ENC_8BIT, 1253}, /* Greek */
335#define IDX_CP1254 52
336 {"cp1254", ENC_8BIT, 1254}, /* Turkish */
337#define IDX_CP1255 53
338 {"cp1255", ENC_8BIT, 1255}, /* Hebrew */
339#define IDX_CP1256 54
340 {"cp1256", ENC_8BIT, 1256}, /* Arabic */
341#define IDX_CP1257 55
342 {"cp1257", ENC_8BIT, 1257}, /* Baltic */
343#define IDX_CP1258 56
344 {"cp1258", ENC_8BIT, 1258}, /* Vietnamese */
345
346#define IDX_MACROMAN 57
347 {"macroman", ENC_8BIT + ENC_MACROMAN, 0}, /* Mac OS */
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000348#define IDX_DECMCS 58
349 {"dec-mcs", ENC_8BIT, 0}, /* DEC MCS */
350#define IDX_HPROMAN8 59
351 {"hp-roman8", ENC_8BIT, 0}, /* HP Roman8 */
352#define IDX_COUNT 60
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353};
354
355/*
356 * Aliases for encoding names.
357 */
358static struct
359{ char *name; int canon;}
360enc_alias_table[] =
361{
362 {"ansi", IDX_LATIN_1},
363 {"iso-8859-1", IDX_LATIN_1},
364 {"latin2", IDX_ISO_2},
365 {"latin3", IDX_ISO_3},
366 {"latin4", IDX_ISO_4},
367 {"cyrillic", IDX_ISO_5},
368 {"arabic", IDX_ISO_6},
369 {"greek", IDX_ISO_7},
370#ifdef WIN3264
371 {"hebrew", IDX_CP1255},
372#else
373 {"hebrew", IDX_ISO_8},
374#endif
375 {"latin5", IDX_ISO_9},
376 {"turkish", IDX_ISO_9}, /* ? */
377 {"latin6", IDX_ISO_10},
378 {"nordic", IDX_ISO_10}, /* ? */
379 {"thai", IDX_ISO_11}, /* ? */
380 {"latin7", IDX_ISO_13},
381 {"latin8", IDX_ISO_14},
382 {"latin9", IDX_ISO_15},
383 {"utf8", IDX_UTF8},
384 {"unicode", IDX_UCS2},
385 {"ucs2", IDX_UCS2},
386 {"ucs2be", IDX_UCS2},
387 {"ucs-2be", IDX_UCS2},
388 {"ucs2le", IDX_UCS2LE},
389 {"utf16", IDX_UTF16},
390 {"utf16be", IDX_UTF16},
391 {"utf-16be", IDX_UTF16},
392 {"utf16le", IDX_UTF16LE},
393 {"ucs4", IDX_UCS4},
394 {"ucs4be", IDX_UCS4},
395 {"ucs-4be", IDX_UCS4},
396 {"ucs4le", IDX_UCS4LE},
Bram Moolenaar5360af92008-02-20 10:29:39 +0000397 {"utf32", IDX_UCS4},
398 {"utf-32", IDX_UCS4},
399 {"utf32be", IDX_UCS4},
400 {"utf-32be", IDX_UCS4},
401 {"utf32le", IDX_UCS4LE},
402 {"utf-32le", IDX_UCS4LE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {"932", IDX_CP932},
404 {"949", IDX_CP949},
405 {"936", IDX_CP936},
Bram Moolenaar0928caa2006-08-16 16:03:34 +0000406 {"gbk", IDX_CP936},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {"950", IDX_CP950},
408 {"eucjp", IDX_EUC_JP},
409 {"unix-jis", IDX_EUC_JP},
410 {"ujis", IDX_EUC_JP},
411 {"shift-jis", IDX_SJIS},
Bram Moolenaar57bc4632014-11-19 17:05:55 +0100412 {"pck", IDX_SJIS}, /* Sun: PCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {"euckr", IDX_EUC_KR},
414 {"5601", IDX_EUC_KR}, /* Sun: KS C 5601 */
415 {"euccn", IDX_EUC_CN},
416 {"gb2312", IDX_EUC_CN},
417 {"euctw", IDX_EUC_TW},
418#if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
419 {"japan", IDX_CP932},
420 {"korea", IDX_CP949},
421 {"prc", IDX_CP936},
422 {"chinese", IDX_CP936},
423 {"taiwan", IDX_CP950},
424 {"big5", IDX_CP950},
425#else
426 {"japan", IDX_EUC_JP},
427 {"korea", IDX_EUC_KR},
428 {"prc", IDX_EUC_CN},
429 {"chinese", IDX_EUC_CN},
430 {"taiwan", IDX_EUC_TW},
431 {"cp950", IDX_BIG5},
432 {"950", IDX_BIG5},
433#endif
434 {"mac", IDX_MACROMAN},
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000435 {"mac-roman", IDX_MACROMAN},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 {NULL, 0}
437};
438
439#ifndef CP_UTF8
440# define CP_UTF8 65001 /* magic number from winnls.h */
441#endif
442
443/*
444 * Find encoding "name" in the list of canonical encoding names.
445 * Returns -1 if not found.
446 */
447 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100448enc_canon_search(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449{
450 int i;
451
452 for (i = 0; i < IDX_COUNT; ++i)
453 if (STRCMP(name, enc_canon_table[i].name) == 0)
454 return i;
455 return -1;
456}
457
458#endif
459
460#if defined(FEAT_MBYTE) || defined(PROTO)
461
462/*
463 * Find canonical encoding "name" in the list and return its properties.
464 * Returns 0 if not found.
465 */
466 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100467enc_canon_props(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 int i;
470
471 i = enc_canon_search(name);
472 if (i >= 0)
473 return enc_canon_table[i].prop;
474#ifdef WIN3264
475 if (name[0] == 'c' && name[1] == 'p' && VIM_ISDIGIT(name[2]))
476 {
477 CPINFO cpinfo;
478
479 /* Get info on this codepage to find out what it is. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100480 if (GetCPInfo(atoi((char *)name + 2), &cpinfo) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
482 if (cpinfo.MaxCharSize == 1) /* some single-byte encoding */
483 return ENC_8BIT;
484 if (cpinfo.MaxCharSize == 2
485 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
486 /* must be a DBCS encoding */
487 return ENC_DBCS;
488 }
489 return 0;
490 }
491#endif
492 if (STRNCMP(name, "2byte-", 6) == 0)
493 return ENC_DBCS;
494 if (STRNCMP(name, "8bit-", 5) == 0 || STRNCMP(name, "iso-8859-", 9) == 0)
495 return ENC_8BIT;
496 return 0;
497}
498
499/*
500 * Set up for using multi-byte characters.
501 * Called in three cases:
502 * - by main() to initialize (p_enc == NULL)
503 * - by set_init_1() after 'encoding' was set to its default.
504 * - by do_set() when 'encoding' has been set.
505 * p_enc must have been passed through enc_canonize() already.
506 * Sets the "enc_unicode", "enc_utf8", "enc_dbcs" and "has_mbyte" flags.
507 * Fills mb_bytelen_tab[] and returns NULL when there are no problems.
508 * When there is something wrong: Returns an error message and doesn't change
509 * anything.
510 */
511 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100512mb_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513{
514 int i;
515 int idx;
516 int n;
517 int enc_dbcs_new = 0;
518#if defined(USE_ICONV) && !defined(WIN3264) && !defined(WIN32UNIX) \
519 && !defined(MACOS)
520# define LEN_FROM_CONV
521 vimconv_T vimconv;
522 char_u *p;
523#endif
524
525 if (p_enc == NULL)
526 {
527 /* Just starting up: set the whole table to one's. */
528 for (i = 0; i < 256; ++i)
529 mb_bytelen_tab[i] = 1;
530 input_conv.vc_type = CONV_NONE;
531 input_conv.vc_factor = 1;
532 output_conv.vc_type = CONV_NONE;
533 return NULL;
534 }
535
536#ifdef WIN3264
537 if (p_enc[0] == 'c' && p_enc[1] == 'p' && VIM_ISDIGIT(p_enc[2]))
538 {
539 CPINFO cpinfo;
540
541 /* Get info on this codepage to find out what it is. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100542 if (GetCPInfo(atoi((char *)p_enc + 2), &cpinfo) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
544 if (cpinfo.MaxCharSize == 1)
545 {
546 /* some single-byte encoding */
547 enc_unicode = 0;
548 enc_utf8 = FALSE;
549 }
550 else if (cpinfo.MaxCharSize == 2
551 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
552 {
553 /* must be a DBCS encoding, check below */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100554 enc_dbcs_new = atoi((char *)p_enc + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556 else
557 goto codepage_invalid;
558 }
559 else if (GetLastError() == ERROR_INVALID_PARAMETER)
560 {
561codepage_invalid:
562 return (char_u *)N_("E543: Not a valid codepage");
563 }
564 }
565#endif
566 else if (STRNCMP(p_enc, "8bit-", 5) == 0
567 || STRNCMP(p_enc, "iso-8859-", 9) == 0)
568 {
569 /* Accept any "8bit-" or "iso-8859-" name. */
570 enc_unicode = 0;
571 enc_utf8 = FALSE;
572 }
573 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
574 {
575#ifdef WIN3264
576 /* Windows: accept only valid codepage numbers, check below. */
577 if (p_enc[6] != 'c' || p_enc[7] != 'p'
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100578 || (enc_dbcs_new = atoi((char *)p_enc + 8)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 return e_invarg;
580#else
581 /* Unix: accept any "2byte-" name, assume current locale. */
582 enc_dbcs_new = DBCS_2BYTE;
583#endif
584 }
585 else if ((idx = enc_canon_search(p_enc)) >= 0)
586 {
587 i = enc_canon_table[idx].prop;
588 if (i & ENC_UNICODE)
589 {
590 /* Unicode */
591 enc_utf8 = TRUE;
592 if (i & (ENC_2BYTE | ENC_2WORD))
593 enc_unicode = 2;
594 else if (i & ENC_4BYTE)
595 enc_unicode = 4;
596 else
597 enc_unicode = 0;
598 }
599 else if (i & ENC_DBCS)
600 {
601 /* 2byte, handle below */
602 enc_dbcs_new = enc_canon_table[idx].codepage;
603 }
604 else
605 {
606 /* Must be 8-bit. */
607 enc_unicode = 0;
608 enc_utf8 = FALSE;
609 }
610 }
611 else /* Don't know what encoding this is, reject it. */
612 return e_invarg;
613
614 if (enc_dbcs_new != 0)
615 {
616#ifdef WIN3264
617 /* Check if the DBCS code page is OK. */
618 if (!IsValidCodePage(enc_dbcs_new))
619 goto codepage_invalid;
620#endif
621 enc_unicode = 0;
622 enc_utf8 = FALSE;
623 }
624 enc_dbcs = enc_dbcs_new;
625 has_mbyte = (enc_dbcs != 0 || enc_utf8);
626
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100627#if defined(WIN3264) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 enc_codepage = encname2codepage(p_enc);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000629 enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#endif
631
Bram Moolenaar78622822005-08-23 21:00:13 +0000632 /* Detect an encoding that uses latin1 characters. */
633 enc_latin1like = (enc_utf8 || STRCMP(p_enc, "latin1") == 0
634 || STRCMP(p_enc, "iso-8859-15") == 0);
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 /*
637 * Set the function pointers.
638 */
639 if (enc_utf8)
640 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000641 mb_ptr2len = utfc_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000642 mb_ptr2len_len = utfc_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 mb_char2len = utf_char2len;
644 mb_char2bytes = utf_char2bytes;
645 mb_ptr2cells = utf_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000646 mb_ptr2cells_len = utf_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 mb_char2cells = utf_char2cells;
648 mb_off2cells = utf_off2cells;
649 mb_ptr2char = utf_ptr2char;
650 mb_head_off = utf_head_off;
651 }
652 else if (enc_dbcs != 0)
653 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000654 mb_ptr2len = dbcs_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000655 mb_ptr2len_len = dbcs_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 mb_char2len = dbcs_char2len;
657 mb_char2bytes = dbcs_char2bytes;
658 mb_ptr2cells = dbcs_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000659 mb_ptr2cells_len = dbcs_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 mb_char2cells = dbcs_char2cells;
661 mb_off2cells = dbcs_off2cells;
662 mb_ptr2char = dbcs_ptr2char;
663 mb_head_off = dbcs_head_off;
664 }
665 else
666 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000667 mb_ptr2len = latin_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000668 mb_ptr2len_len = latin_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 mb_char2len = latin_char2len;
670 mb_char2bytes = latin_char2bytes;
671 mb_ptr2cells = latin_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000672 mb_ptr2cells_len = latin_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 mb_char2cells = latin_char2cells;
674 mb_off2cells = latin_off2cells;
675 mb_ptr2char = latin_ptr2char;
676 mb_head_off = latin_head_off;
677 }
678
679 /*
680 * Fill the mb_bytelen_tab[] for MB_BYTE2LEN().
681 */
682#ifdef LEN_FROM_CONV
683 /* When 'encoding' is different from the current locale mblen() won't
684 * work. Use conversion to "utf-8" instead. */
685 vimconv.vc_type = CONV_NONE;
686 if (enc_dbcs)
687 {
688 p = enc_locale();
689 if (p == NULL || STRCMP(p, p_enc) != 0)
690 {
691 convert_setup(&vimconv, p_enc, (char_u *)"utf-8");
692 vimconv.vc_fail = TRUE;
693 }
694 vim_free(p);
695 }
696#endif
697
698 for (i = 0; i < 256; ++i)
699 {
700 /* Our own function to reliably check the length of UTF-8 characters,
701 * independent of mblen(). */
702 if (enc_utf8)
703 n = utf8len_tab[i];
704 else if (enc_dbcs == 0)
705 n = 1;
706 else
707 {
708#if defined(WIN3264) || defined(WIN32UNIX)
709 /* enc_dbcs is set by setting 'fileencoding'. It becomes a Windows
710 * CodePage identifier, which we can pass directly in to Windows
711 * API */
Bram Moolenaarca058dc2014-01-14 13:26:21 +0100712 n = IsDBCSLeadByteEx(enc_dbcs, (WINBYTE)i) ? 2 : 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713#else
Bram Moolenaar4064e242014-03-12 14:54:34 +0100714# if defined(MACOS) || defined(__amigaos4__) || defined(__ANDROID__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 /*
716 * if mblen() is not available, character which MSB is turned on
717 * are treated as leading byte character. (note : This assumption
718 * is not always true.)
719 */
720 n = (i & 0x80) ? 2 : 1;
721# else
Bram Moolenaar9a920d82012-06-01 15:21:02 +0200722 char buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723# ifdef X_LOCALE
724# ifndef mblen
725# define mblen _Xmblen
726# endif
727# endif
728 if (i == NUL) /* just in case mblen() can't handle "" */
729 n = 1;
730 else
731 {
732 buf[0] = i;
733 buf[1] = 0;
734#ifdef LEN_FROM_CONV
735 if (vimconv.vc_type != CONV_NONE)
736 {
737 /*
738 * string_convert() should fail when converting the first
739 * byte of a double-byte character.
740 */
741 p = string_convert(&vimconv, (char_u *)buf, NULL);
742 if (p != NULL)
743 {
744 vim_free(p);
745 n = 1;
746 }
747 else
748 n = 2;
749 }
750 else
751#endif
752 {
753 /*
754 * mblen() should return -1 for invalid (means the leading
755 * multibyte) character. However there are some platforms
756 * where mblen() returns 0 for invalid character.
757 * Therefore, following condition includes 0.
758 */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000759 ignored = mblen(NULL, 0); /* First reset the state. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (mblen(buf, (size_t)1) <= 0)
761 n = 2;
762 else
763 n = 1;
764 }
765 }
766# endif
767#endif
768 }
769
770 mb_bytelen_tab[i] = n;
771 }
772
773#ifdef LEN_FROM_CONV
774 convert_setup(&vimconv, NULL, NULL);
775#endif
776
777 /* The cell width depends on the type of multi-byte characters. */
778 (void)init_chartab();
779
780 /* When enc_utf8 is set or reset, (de)allocate ScreenLinesUC[] */
781 screenalloc(FALSE);
782
783 /* When using Unicode, set default for 'fileencodings'. */
784 if (enc_utf8 && !option_was_set((char_u *)"fencs"))
785 set_string_option_direct((char_u *)"fencs", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000786 (char_u *)"ucs-bom,utf-8,default,latin1", OPT_FREE, 0);
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
789 /* GNU gettext 0.10.37 supports this feature: set the codeset used for
790 * translated messages independently from the current locale. */
791 (void)bind_textdomain_codeset(VIMPACKAGE,
792 enc_utf8 ? "utf-8" : (char *)p_enc);
793#endif
794
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000795#ifdef WIN32
796 /* When changing 'encoding' while starting up, then convert the command
797 * line arguments from the active codepage to 'encoding'. */
798 if (starting != 0)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000799 fix_arg_enc();
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000800#endif
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#ifdef FEAT_AUTOCMD
803 /* Fire an autocommand to let people do custom font setup. This must be
804 * after Vim has been setup for the new encoding. */
805 apply_autocmds(EVENT_ENCODINGCHANGED, NULL, (char_u *)"", FALSE, curbuf);
806#endif
807
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000808#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000809 /* Need to reload spell dictionaries */
810 spell_reload();
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 return NULL;
814}
815
816/*
817 * Return the size of the BOM for the current buffer:
818 * 0 - no BOM
819 * 2 - UCS-2 or UTF-16 BOM
820 * 4 - UCS-4 BOM
821 * 3 - UTF-8 BOM
822 */
823 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100824bomb_size(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
826 int n = 0;
827
828 if (curbuf->b_p_bomb && !curbuf->b_p_bin)
829 {
830 if (*curbuf->b_p_fenc == NUL)
831 {
832 if (enc_utf8)
833 {
834 if (enc_unicode != 0)
835 n = enc_unicode;
836 else
837 n = 3;
838 }
839 }
840 else if (STRCMP(curbuf->b_p_fenc, "utf-8") == 0)
841 n = 3;
842 else if (STRNCMP(curbuf->b_p_fenc, "ucs-2", 5) == 0
843 || STRNCMP(curbuf->b_p_fenc, "utf-16", 6) == 0)
844 n = 2;
845 else if (STRNCMP(curbuf->b_p_fenc, "ucs-4", 5) == 0)
846 n = 4;
847 }
848 return n;
849}
850
851/*
Bram Moolenaar836082d2011-08-10 13:21:46 +0200852 * Remove all BOM from "s" by moving remaining text.
853 */
854 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100855remove_bom(char_u *s)
Bram Moolenaar836082d2011-08-10 13:21:46 +0200856{
857 if (enc_utf8)
858 {
859 char_u *p = s;
860
861 while ((p = vim_strbyte(p, 0xef)) != NULL)
862 {
863 if (p[1] == 0xbb && p[2] == 0xbf)
864 STRMOVE(p, p + 3);
865 else
866 ++p;
867 }
868 }
869}
870
871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 * Get class of pointer:
873 * 0 for blank or NUL
874 * 1 for punctuation
875 * 2 for an (ASCII) word character
876 * >2 for other word characters
877 */
878 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100879mb_get_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
Bram Moolenaarf813a182013-01-30 13:59:37 +0100881 return mb_get_class_buf(p, curbuf);
882}
883
884 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100885mb_get_class_buf(char_u *p, buf_T *buf)
Bram Moolenaarf813a182013-01-30 13:59:37 +0100886{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (MB_BYTE2LEN(p[0]) == 1)
888 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100889 if (p[0] == NUL || VIM_ISWHITE(p[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 return 0;
Bram Moolenaarf813a182013-01-30 13:59:37 +0100891 if (vim_iswordc_buf(p[0], buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 return 2;
893 return 1;
894 }
895 if (enc_dbcs != 0 && p[0] != NUL && p[1] != NUL)
896 return dbcs_class(p[0], p[1]);
897 if (enc_utf8)
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100898 return utf_class_buf(utf_ptr2char(p), buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 return 0;
900}
901
902/*
903 * Get class of a double-byte character. This always returns 3 or bigger.
904 * TODO: Should return 1 for punctuation.
905 */
906 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100907dbcs_class(unsigned lead, unsigned trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 switch (enc_dbcs)
910 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200911 /* please add classify routine for your language in here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 case DBCS_JPNU: /* ? */
914 case DBCS_JPN:
915 {
916 /* JIS code classification */
917 unsigned char lb = lead;
918 unsigned char tb = trail;
919
920 /* convert process code to JIS */
921# if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
922 /* process code is SJIS */
923 if (lb <= 0x9f)
924 lb = (lb - 0x81) * 2 + 0x21;
925 else
926 lb = (lb - 0xc1) * 2 + 0x21;
927 if (tb <= 0x7e)
928 tb -= 0x1f;
929 else if (tb <= 0x9e)
930 tb -= 0x20;
931 else
932 {
933 tb -= 0x7e;
934 lb += 1;
935 }
936# else
937 /*
938 * XXX: Code page identification can not use with all
939 * system! So, some other encoding information
940 * will be needed.
941 * In japanese: SJIS,EUC,UNICODE,(JIS)
942 * Note that JIS-code system don't use as
943 * process code in most system because it uses
944 * escape sequences(JIS is context depend encoding).
945 */
946 /* assume process code is JAPANESE-EUC */
947 lb &= 0x7f;
948 tb &= 0x7f;
949# endif
950 /* exceptions */
951 switch (lb << 8 | tb)
952 {
953 case 0x2121: /* ZENKAKU space */
954 return 0;
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100955 case 0x2122: /* TOU-TEN (Japanese comma) */
956 case 0x2123: /* KU-TEN (Japanese period) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 case 0x2124: /* ZENKAKU comma */
958 case 0x2125: /* ZENKAKU period */
959 return 1;
960 case 0x213c: /* prolongedsound handled as KATAKANA */
961 return 13;
962 }
963 /* sieved by KU code */
964 switch (lb)
965 {
966 case 0x21:
967 case 0x22:
968 /* special symbols */
969 return 10;
970 case 0x23:
971 /* alpha-numeric */
972 return 11;
973 case 0x24:
974 /* hiragana */
975 return 12;
976 case 0x25:
977 /* katakana */
978 return 13;
979 case 0x26:
980 /* greek */
981 return 14;
982 case 0x27:
983 /* russian */
984 return 15;
985 case 0x28:
986 /* lines */
987 return 16;
988 default:
989 /* kanji */
990 return 17;
991 }
992 }
993
994 case DBCS_KORU: /* ? */
995 case DBCS_KOR:
996 {
997 /* KS code classification */
998 unsigned char c1 = lead;
999 unsigned char c2 = trail;
1000
1001 /*
1002 * 20 : Hangul
1003 * 21 : Hanja
1004 * 22 : Symbols
1005 * 23 : Alpha-numeric/Roman Letter (Full width)
1006 * 24 : Hangul Letter(Alphabet)
1007 * 25 : Roman Numeral/Greek Letter
1008 * 26 : Box Drawings
1009 * 27 : Unit Symbols
1010 * 28 : Circled/Parenthesized Letter
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001011 * 29 : Hiragana/Katakana
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * 30 : Cyrillic Letter
1013 */
1014
1015 if (c1 >= 0xB0 && c1 <= 0xC8)
1016 /* Hangul */
1017 return 20;
1018#if defined(WIN3264) || defined(WIN32UNIX)
1019 else if (c1 <= 0xA0 || c2 <= 0xA0)
1020 /* Extended Hangul Region : MS UHC(Unified Hangul Code) */
1021 /* c1: 0x81-0xA0 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xFE
1022 * c1: 0xA1-0xC6 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xA0
1023 */
1024 return 20;
1025#endif
1026
1027 else if (c1 >= 0xCA && c1 <= 0xFD)
1028 /* Hanja */
1029 return 21;
1030 else switch (c1)
1031 {
1032 case 0xA1:
1033 case 0xA2:
1034 /* Symbols */
1035 return 22;
1036 case 0xA3:
1037 /* Alpha-numeric */
1038 return 23;
1039 case 0xA4:
1040 /* Hangul Letter(Alphabet) */
1041 return 24;
1042 case 0xA5:
1043 /* Roman Numeral/Greek Letter */
1044 return 25;
1045 case 0xA6:
1046 /* Box Drawings */
1047 return 26;
1048 case 0xA7:
1049 /* Unit Symbols */
1050 return 27;
1051 case 0xA8:
1052 case 0xA9:
1053 if (c2 <= 0xAF)
1054 return 25; /* Roman Letter */
1055 else if (c2 >= 0xF6)
1056 return 22; /* Symbols */
1057 else
1058 /* Circled/Parenthesized Letter */
1059 return 28;
1060 case 0xAA:
1061 case 0xAB:
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001062 /* Hiragana/Katakana */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 return 29;
1064 case 0xAC:
1065 /* Cyrillic Letter */
1066 return 30;
1067 }
1068 }
1069 default:
1070 break;
1071 }
1072 return 3;
1073}
1074
1075/*
1076 * mb_char2len() function pointer.
1077 * Return length in bytes of character "c".
1078 * Returns 1 for a single-byte character.
1079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001081latin_char2len(int c UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 return 1;
1084}
1085
1086 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001087dbcs_char2len(
1088 int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 if (c >= 0x100)
1091 return 2;
1092 return 1;
1093}
1094
1095/*
1096 * mb_char2bytes() function pointer.
1097 * Convert a character to its bytes.
1098 * Returns the length in bytes.
1099 */
1100 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001101latin_char2bytes(int c, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 buf[0] = c;
1104 return 1;
1105}
1106
1107 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001108dbcs_char2bytes(int c, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 if (c >= 0x100)
1111 {
1112 buf[0] = (unsigned)c >> 8;
1113 buf[1] = c;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001114 /* Never use a NUL byte, it causes lots of trouble. It's an invalid
1115 * character anyway. */
1116 if (buf[1] == NUL)
1117 buf[1] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 return 2;
1119 }
1120 buf[0] = c;
1121 return 1;
1122}
1123
1124/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001125 * mb_ptr2len() function pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 * Get byte length of character at "*p" but stop at a NUL.
1127 * For UTF-8 this includes following composing characters.
1128 * Returns 0 when *p is NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
1130 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001131latin_ptr2len(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001133 return MB_BYTE2LEN(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134}
1135
1136 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001137dbcs_ptr2len(
1138 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 int len;
1141
1142 /* Check if second byte is not missing. */
1143 len = MB_BYTE2LEN(*p);
1144 if (len == 2 && p[1] == NUL)
1145 len = 1;
1146 return len;
1147}
1148
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001149/*
1150 * mb_ptr2len_len() function pointer.
1151 * Like mb_ptr2len(), but limit to read "size" bytes.
1152 * Returns 0 for an empty string.
1153 * Returns 1 for an illegal char or an incomplete byte sequence.
1154 */
1155 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001156latin_ptr2len_len(char_u *p, int size)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001157{
1158 if (size < 1 || *p == NUL)
1159 return 0;
1160 return 1;
1161}
1162
1163 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001164dbcs_ptr2len_len(char_u *p, int size)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001165{
1166 int len;
1167
1168 if (size < 1 || *p == NUL)
1169 return 0;
1170 if (size == 1)
1171 return 1;
1172 /* Check that second byte is not missing. */
1173 len = MB_BYTE2LEN(*p);
1174 if (len == 2 && p[1] == NUL)
1175 len = 1;
1176 return len;
1177}
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179struct interval
1180{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01001181 long first;
1182 long last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185/*
1186 * Return TRUE if "c" is in "table[size / sizeof(struct interval)]".
1187 */
1188 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001189intable(struct interval *table, size_t size, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
1191 int mid, bot, top;
1192
1193 /* first quick check for Latin1 etc. characters */
1194 if (c < table[0].first)
1195 return FALSE;
1196
1197 /* binary search in table */
1198 bot = 0;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001199 top = (int)(size / sizeof(struct interval) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 while (top >= bot)
1201 {
1202 mid = (bot + top) / 2;
1203 if (table[mid].last < c)
1204 bot = mid + 1;
1205 else if (table[mid].first > c)
1206 top = mid - 1;
1207 else
1208 return TRUE;
1209 }
1210 return FALSE;
1211}
1212
Bram Moolenaarcb070082016-04-02 22:14:51 +02001213/* Sorted list of non-overlapping intervals of East Asian Ambiguous
1214 * characters, generated with ../runtime/tools/unicode.vim. */
1215static struct interval ambiguous[] =
1216{
1217 {0x00a1, 0x00a1},
1218 {0x00a4, 0x00a4},
1219 {0x00a7, 0x00a8},
1220 {0x00aa, 0x00aa},
1221 {0x00ad, 0x00ae},
1222 {0x00b0, 0x00b4},
1223 {0x00b6, 0x00ba},
1224 {0x00bc, 0x00bf},
1225 {0x00c6, 0x00c6},
1226 {0x00d0, 0x00d0},
1227 {0x00d7, 0x00d8},
1228 {0x00de, 0x00e1},
1229 {0x00e6, 0x00e6},
1230 {0x00e8, 0x00ea},
1231 {0x00ec, 0x00ed},
1232 {0x00f0, 0x00f0},
1233 {0x00f2, 0x00f3},
1234 {0x00f7, 0x00fa},
1235 {0x00fc, 0x00fc},
1236 {0x00fe, 0x00fe},
1237 {0x0101, 0x0101},
1238 {0x0111, 0x0111},
1239 {0x0113, 0x0113},
1240 {0x011b, 0x011b},
1241 {0x0126, 0x0127},
1242 {0x012b, 0x012b},
1243 {0x0131, 0x0133},
1244 {0x0138, 0x0138},
1245 {0x013f, 0x0142},
1246 {0x0144, 0x0144},
1247 {0x0148, 0x014b},
1248 {0x014d, 0x014d},
1249 {0x0152, 0x0153},
1250 {0x0166, 0x0167},
1251 {0x016b, 0x016b},
1252 {0x01ce, 0x01ce},
1253 {0x01d0, 0x01d0},
1254 {0x01d2, 0x01d2},
1255 {0x01d4, 0x01d4},
1256 {0x01d6, 0x01d6},
1257 {0x01d8, 0x01d8},
1258 {0x01da, 0x01da},
1259 {0x01dc, 0x01dc},
1260 {0x0251, 0x0251},
1261 {0x0261, 0x0261},
1262 {0x02c4, 0x02c4},
1263 {0x02c7, 0x02c7},
1264 {0x02c9, 0x02cb},
1265 {0x02cd, 0x02cd},
1266 {0x02d0, 0x02d0},
1267 {0x02d8, 0x02db},
1268 {0x02dd, 0x02dd},
1269 {0x02df, 0x02df},
1270 {0x0300, 0x036f},
1271 {0x0391, 0x03a1},
1272 {0x03a3, 0x03a9},
1273 {0x03b1, 0x03c1},
1274 {0x03c3, 0x03c9},
1275 {0x0401, 0x0401},
1276 {0x0410, 0x044f},
1277 {0x0451, 0x0451},
1278 {0x2010, 0x2010},
1279 {0x2013, 0x2016},
1280 {0x2018, 0x2019},
1281 {0x201c, 0x201d},
1282 {0x2020, 0x2022},
1283 {0x2024, 0x2027},
1284 {0x2030, 0x2030},
1285 {0x2032, 0x2033},
1286 {0x2035, 0x2035},
1287 {0x203b, 0x203b},
1288 {0x203e, 0x203e},
1289 {0x2074, 0x2074},
1290 {0x207f, 0x207f},
1291 {0x2081, 0x2084},
1292 {0x20ac, 0x20ac},
1293 {0x2103, 0x2103},
1294 {0x2105, 0x2105},
1295 {0x2109, 0x2109},
1296 {0x2113, 0x2113},
1297 {0x2116, 0x2116},
1298 {0x2121, 0x2122},
1299 {0x2126, 0x2126},
1300 {0x212b, 0x212b},
1301 {0x2153, 0x2154},
1302 {0x215b, 0x215e},
1303 {0x2160, 0x216b},
1304 {0x2170, 0x2179},
1305 {0x2189, 0x2189},
1306 {0x2190, 0x2199},
1307 {0x21b8, 0x21b9},
1308 {0x21d2, 0x21d2},
1309 {0x21d4, 0x21d4},
1310 {0x21e7, 0x21e7},
1311 {0x2200, 0x2200},
1312 {0x2202, 0x2203},
1313 {0x2207, 0x2208},
1314 {0x220b, 0x220b},
1315 {0x220f, 0x220f},
1316 {0x2211, 0x2211},
1317 {0x2215, 0x2215},
1318 {0x221a, 0x221a},
1319 {0x221d, 0x2220},
1320 {0x2223, 0x2223},
1321 {0x2225, 0x2225},
1322 {0x2227, 0x222c},
1323 {0x222e, 0x222e},
1324 {0x2234, 0x2237},
1325 {0x223c, 0x223d},
1326 {0x2248, 0x2248},
1327 {0x224c, 0x224c},
1328 {0x2252, 0x2252},
1329 {0x2260, 0x2261},
1330 {0x2264, 0x2267},
1331 {0x226a, 0x226b},
1332 {0x226e, 0x226f},
1333 {0x2282, 0x2283},
1334 {0x2286, 0x2287},
1335 {0x2295, 0x2295},
1336 {0x2299, 0x2299},
1337 {0x22a5, 0x22a5},
1338 {0x22bf, 0x22bf},
1339 {0x2312, 0x2312},
1340 {0x2460, 0x24e9},
1341 {0x24eb, 0x254b},
1342 {0x2550, 0x2573},
1343 {0x2580, 0x258f},
1344 {0x2592, 0x2595},
1345 {0x25a0, 0x25a1},
1346 {0x25a3, 0x25a9},
1347 {0x25b2, 0x25b3},
1348 {0x25b6, 0x25b7},
1349 {0x25bc, 0x25bd},
1350 {0x25c0, 0x25c1},
1351 {0x25c6, 0x25c8},
1352 {0x25cb, 0x25cb},
1353 {0x25ce, 0x25d1},
1354 {0x25e2, 0x25e5},
1355 {0x25ef, 0x25ef},
1356 {0x2605, 0x2606},
1357 {0x2609, 0x2609},
1358 {0x260e, 0x260f},
Bram Moolenaarcb070082016-04-02 22:14:51 +02001359 {0x261c, 0x261c},
1360 {0x261e, 0x261e},
1361 {0x2640, 0x2640},
1362 {0x2642, 0x2642},
1363 {0x2660, 0x2661},
1364 {0x2663, 0x2665},
1365 {0x2667, 0x266a},
1366 {0x266c, 0x266d},
1367 {0x266f, 0x266f},
1368 {0x269e, 0x269f},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001369 {0x26bf, 0x26bf},
1370 {0x26c6, 0x26cd},
1371 {0x26cf, 0x26d3},
1372 {0x26d5, 0x26e1},
Bram Moolenaarcb070082016-04-02 22:14:51 +02001373 {0x26e3, 0x26e3},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001374 {0x26e8, 0x26e9},
1375 {0x26eb, 0x26f1},
1376 {0x26f4, 0x26f4},
1377 {0x26f6, 0x26f9},
1378 {0x26fb, 0x26fc},
1379 {0x26fe, 0x26ff},
Bram Moolenaarcb070082016-04-02 22:14:51 +02001380 {0x273d, 0x273d},
Bram Moolenaarcb070082016-04-02 22:14:51 +02001381 {0x2776, 0x277f},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001382 {0x2b56, 0x2b59},
Bram Moolenaarcb070082016-04-02 22:14:51 +02001383 {0x3248, 0x324f},
1384 {0xe000, 0xf8ff},
1385 {0xfe00, 0xfe0f},
1386 {0xfffd, 0xfffd},
1387 {0x1f100, 0x1f10a},
1388 {0x1f110, 0x1f12d},
1389 {0x1f130, 0x1f169},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001390 {0x1f170, 0x1f18d},
1391 {0x1f18f, 0x1f190},
1392 {0x1f19b, 0x1f1ac},
Bram Moolenaarcb070082016-04-02 22:14:51 +02001393 {0xe0100, 0xe01ef},
1394 {0xf0000, 0xffffd},
1395 {0x100000, 0x10fffd}
1396};
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * For UTF-8 character "c" return 2 for a double-width character, 1 for others.
1400 * Returns 4 or 6 for an unprintable character.
1401 * Is only correct for characters >= 0x80.
1402 * When p_ambw is "double", return 2 for a character with East Asian Width
1403 * class 'A'(mbiguous).
1404 */
1405 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001406utf_char2cells(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001408 /* Sorted list of non-overlapping intervals of East Asian double width
1409 * characters, generated with ../runtime/tools/unicode.vim. */
1410 static struct interval doublewidth[] =
1411 {
1412 {0x1100, 0x115f},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001413 {0x231a, 0x231b},
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001414 {0x2329, 0x232a},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001415 {0x23e9, 0x23ec},
1416 {0x23f0, 0x23f0},
1417 {0x23f3, 0x23f3},
1418 {0x25fd, 0x25fe},
1419 {0x2614, 0x2615},
1420 {0x2648, 0x2653},
1421 {0x267f, 0x267f},
1422 {0x2693, 0x2693},
1423 {0x26a1, 0x26a1},
1424 {0x26aa, 0x26ab},
1425 {0x26bd, 0x26be},
1426 {0x26c4, 0x26c5},
1427 {0x26ce, 0x26ce},
1428 {0x26d4, 0x26d4},
1429 {0x26ea, 0x26ea},
1430 {0x26f2, 0x26f3},
1431 {0x26f5, 0x26f5},
1432 {0x26fa, 0x26fa},
1433 {0x26fd, 0x26fd},
1434 {0x2705, 0x2705},
1435 {0x270a, 0x270b},
1436 {0x2728, 0x2728},
1437 {0x274c, 0x274c},
1438 {0x274e, 0x274e},
1439 {0x2753, 0x2755},
1440 {0x2757, 0x2757},
1441 {0x2795, 0x2797},
1442 {0x27b0, 0x27b0},
1443 {0x27bf, 0x27bf},
1444 {0x2b1b, 0x2b1c},
1445 {0x2b50, 0x2b50},
1446 {0x2b55, 0x2b55},
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001447 {0x2e80, 0x2e99},
1448 {0x2e9b, 0x2ef3},
1449 {0x2f00, 0x2fd5},
1450 {0x2ff0, 0x2ffb},
Bram Moolenaarea676722015-01-14 17:40:09 +01001451 {0x3000, 0x303e},
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001452 {0x3041, 0x3096},
Bram Moolenaarea676722015-01-14 17:40:09 +01001453 {0x3099, 0x30ff},
Bram Moolenaar383aa842017-06-22 15:27:37 +02001454 {0x3105, 0x312e},
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001455 {0x3131, 0x318e},
Bram Moolenaarea676722015-01-14 17:40:09 +01001456 {0x3190, 0x31ba},
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001457 {0x31c0, 0x31e3},
1458 {0x31f0, 0x321e},
1459 {0x3220, 0x3247},
1460 {0x3250, 0x32fe},
1461 {0x3300, 0x4dbf},
1462 {0x4e00, 0xa48c},
1463 {0xa490, 0xa4c6},
1464 {0xa960, 0xa97c},
1465 {0xac00, 0xd7a3},
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001466 {0xf900, 0xfaff},
1467 {0xfe10, 0xfe19},
1468 {0xfe30, 0xfe52},
1469 {0xfe54, 0xfe66},
1470 {0xfe68, 0xfe6b},
1471 {0xff01, 0xff60},
1472 {0xffe0, 0xffe6},
Bram Moolenaar383aa842017-06-22 15:27:37 +02001473 {0x16fe0, 0x16fe1},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001474 {0x17000, 0x187ec},
1475 {0x18800, 0x18af2},
Bram Moolenaar383aa842017-06-22 15:27:37 +02001476 {0x1b000, 0x1b11e},
1477 {0x1b170, 0x1b2fb},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001478 {0x1f004, 0x1f004},
1479 {0x1f0cf, 0x1f0cf},
1480 {0x1f18e, 0x1f18e},
1481 {0x1f191, 0x1f19a},
Bram Moolenaard63aff02016-03-21 22:15:30 +01001482 {0x1f200, 0x1f202},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001483 {0x1f210, 0x1f23b},
Bram Moolenaard63aff02016-03-21 22:15:30 +01001484 {0x1f240, 0x1f248},
1485 {0x1f250, 0x1f251},
Bram Moolenaar383aa842017-06-22 15:27:37 +02001486 {0x1f260, 0x1f265},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001487 {0x1f300, 0x1f320},
1488 {0x1f32d, 0x1f335},
1489 {0x1f337, 0x1f37c},
1490 {0x1f37e, 0x1f393},
1491 {0x1f3a0, 0x1f3ca},
1492 {0x1f3cf, 0x1f3d3},
1493 {0x1f3e0, 0x1f3f0},
1494 {0x1f3f4, 0x1f3f4},
1495 {0x1f3f8, 0x1f43e},
1496 {0x1f440, 0x1f440},
1497 {0x1f442, 0x1f4fc},
1498 {0x1f4ff, 0x1f53d},
1499 {0x1f54b, 0x1f54e},
1500 {0x1f550, 0x1f567},
1501 {0x1f57a, 0x1f57a},
1502 {0x1f595, 0x1f596},
1503 {0x1f5a4, 0x1f5a4},
1504 {0x1f5fb, 0x1f64f},
1505 {0x1f680, 0x1f6c5},
1506 {0x1f6cc, 0x1f6cc},
1507 {0x1f6d0, 0x1f6d2},
1508 {0x1f6eb, 0x1f6ec},
Bram Moolenaar383aa842017-06-22 15:27:37 +02001509 {0x1f6f4, 0x1f6f8},
1510 {0x1f910, 0x1f93e},
1511 {0x1f940, 0x1f94c},
1512 {0x1f950, 0x1f96b},
1513 {0x1f980, 0x1f997},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02001514 {0x1f9c0, 0x1f9c0},
Bram Moolenaar383aa842017-06-22 15:27:37 +02001515 {0x1f9d0, 0x1f9e6},
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001516 {0x20000, 0x2fffd},
1517 {0x30000, 0x3fffd}
1518 };
Bram Moolenaarea676722015-01-14 17:40:09 +01001519
Bram Moolenaarb86f10e2016-03-21 22:09:44 +01001520 /* Sorted list of non-overlapping intervals of Emoji characters that don't
1521 * have ambiguous or double width,
1522 * based on http://unicode.org/emoji/charts/emoji-list.html */
1523 static struct interval emoji_width[] =
1524 {
Bram Moolenaarb86f10e2016-03-21 22:09:44 +01001525 {0x1f1e6, 0x1f1ff},
Bram Moolenaar383aa842017-06-22 15:27:37 +02001526 {0x1f321, 0x1f321},
1527 {0x1f324, 0x1f32c},
1528 {0x1f336, 0x1f336},
1529 {0x1f37d, 0x1f37d},
1530 {0x1f396, 0x1f397},
1531 {0x1f399, 0x1f39b},
1532 {0x1f39e, 0x1f39f},
1533 {0x1f3cb, 0x1f3ce},
1534 {0x1f3d4, 0x1f3df},
1535 {0x1f3f3, 0x1f3f5},
1536 {0x1f3f7, 0x1f3f7},
1537 {0x1f43f, 0x1f43f},
1538 {0x1f441, 0x1f441},
1539 {0x1f4fd, 0x1f4fd},
1540 {0x1f549, 0x1f54a},
1541 {0x1f56f, 0x1f570},
1542 {0x1f573, 0x1f579},
1543 {0x1f587, 0x1f587},
1544 {0x1f58a, 0x1f58d},
1545 {0x1f590, 0x1f590},
1546 {0x1f5a5, 0x1f5a5},
1547 {0x1f5a8, 0x1f5a8},
1548 {0x1f5b1, 0x1f5b2},
1549 {0x1f5bc, 0x1f5bc},
1550 {0x1f5c2, 0x1f5c4},
1551 {0x1f5d1, 0x1f5d3},
1552 {0x1f5dc, 0x1f5de},
1553 {0x1f5e1, 0x1f5e1},
1554 {0x1f5e3, 0x1f5e3},
1555 {0x1f5e8, 0x1f5e8},
1556 {0x1f5ef, 0x1f5ef},
1557 {0x1f5f3, 0x1f5f3},
1558 {0x1f5fa, 0x1f5fa},
1559 {0x1f6cb, 0x1f6cf},
1560 {0x1f6e0, 0x1f6e5},
1561 {0x1f6e9, 0x1f6e9},
1562 {0x1f6f0, 0x1f6f0},
1563 {0x1f6f3, 0x1f6f3}
Bram Moolenaarb86f10e2016-03-21 22:09:44 +01001564 };
1565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (c >= 0x100)
1567 {
1568#ifdef USE_WCHAR_FUNCTIONS
1569 /*
1570 * Assume the library function wcwidth() works better than our own
1571 * stuff. It should return 1 for ambiguous width chars!
1572 */
1573 int n = wcwidth(c);
1574
1575 if (n < 0)
1576 return 6; /* unprintable, displays <xxxx> */
1577 if (n > 1)
1578 return n;
1579#else
1580 if (!utf_printable(c))
1581 return 6; /* unprintable, displays <xxxx> */
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001582 if (intable(doublewidth, sizeof(doublewidth), c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 return 2;
1584#endif
Bram Moolenaarb86f10e2016-03-21 22:09:44 +01001585 if (p_emoji && intable(emoji_width, sizeof(emoji_width), c))
Bram Moolenaar3848e002016-03-19 18:42:29 +01001586 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588
1589 /* Characters below 0x100 are influenced by 'isprint' option */
1590 else if (c >= 0x80 && !vim_isprintc(c))
1591 return 4; /* unprintable, displays <xx> */
1592
1593 if (c >= 0x80 && *p_ambw == 'd' && intable(ambiguous, sizeof(ambiguous), c))
1594 return 2;
1595
1596 return 1;
1597}
1598
1599/*
1600 * mb_ptr2cells() function pointer.
1601 * Return the number of display cells character at "*p" occupies.
1602 * This doesn't take care of unprintable characters, use ptr2cells() for that.
1603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001605latin_ptr2cells(char_u *p UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 return 1;
1608}
1609
1610 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001611utf_ptr2cells(
1612 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 int c;
1615
1616 /* Need to convert to a wide character. */
1617 if (*p >= 0x80)
1618 {
1619 c = utf_ptr2char(p);
1620 /* An illegal byte is displayed as <xx>. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001621 if (utf_ptr2len(p) == 1 || c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 return 4;
1623 /* If the char is ASCII it must be an overlong sequence. */
1624 if (c < 0x80)
1625 return char2cells(c);
1626 return utf_char2cells(c);
1627 }
1628 return 1;
1629}
1630
1631 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001632dbcs_ptr2cells(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 /* Number of cells is equal to number of bytes, except for euc-jp when
1635 * the first byte is 0x8e. */
1636 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
1637 return 1;
1638 return MB_BYTE2LEN(*p);
1639}
1640
1641/*
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001642 * mb_ptr2cells_len() function pointer.
1643 * Like mb_ptr2cells(), but limit string length to "size".
1644 * For an empty string or truncated character returns 1.
1645 */
1646 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001647latin_ptr2cells_len(char_u *p UNUSED, int size UNUSED)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001648{
1649 return 1;
1650}
1651
1652 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001653utf_ptr2cells_len(char_u *p, int size)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001654{
1655 int c;
1656
1657 /* Need to convert to a wide character. */
1658 if (size > 0 && *p >= 0x80)
1659 {
1660 if (utf_ptr2len_len(p, size) < utf8len_tab[*p])
Bram Moolenaar24397332009-12-02 14:02:39 +00001661 return 1; /* truncated */
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001662 c = utf_ptr2char(p);
1663 /* An illegal byte is displayed as <xx>. */
1664 if (utf_ptr2len(p) == 1 || c == NUL)
1665 return 4;
1666 /* If the char is ASCII it must be an overlong sequence. */
1667 if (c < 0x80)
1668 return char2cells(c);
1669 return utf_char2cells(c);
1670 }
1671 return 1;
1672}
1673
1674 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001675dbcs_ptr2cells_len(char_u *p, int size)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001676{
1677 /* Number of cells is equal to number of bytes, except for euc-jp when
1678 * the first byte is 0x8e. */
1679 if (size <= 1 || (enc_dbcs == DBCS_JPNU && *p == 0x8e))
1680 return 1;
1681 return MB_BYTE2LEN(*p);
1682}
1683
1684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 * mb_char2cells() function pointer.
1686 * Return the number of display cells character "c" occupies.
1687 * Only takes care of multi-byte chars, not "^C" and such.
1688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001690latin_char2cells(int c UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 return 1;
1693}
1694
1695 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001696dbcs_char2cells(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 /* Number of cells is equal to number of bytes, except for euc-jp when
1699 * the first byte is 0x8e. */
1700 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
1701 return 1;
1702 /* use the first byte */
1703 return MB_BYTE2LEN((unsigned)c >> 8);
1704}
1705
1706/*
Bram Moolenaar72597a52010-07-18 15:31:08 +02001707 * Return the number of cells occupied by string "p".
1708 * Stop at a NUL character. When "len" >= 0 stop at character "p[len]".
1709 */
1710 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001711mb_string2cells(char_u *p, int len)
Bram Moolenaar72597a52010-07-18 15:31:08 +02001712{
1713 int i;
1714 int clen = 0;
1715
1716 for (i = 0; (len < 0 || i < len) && p[i] != NUL; i += (*mb_ptr2len)(p + i))
1717 clen += (*mb_ptr2cells)(p + i);
1718 return clen;
1719}
1720
1721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 * mb_off2cells() function pointer.
1723 * Return number of display cells for char at ScreenLines[off].
Bram Moolenaar367329b2007-08-30 11:53:22 +00001724 * We make sure that the offset used is less than "max_off".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001727latin_off2cells(unsigned off UNUSED, unsigned max_off UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 return 1;
1730}
1731
1732 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001733dbcs_off2cells(unsigned off, unsigned max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001735 /* never check beyond end of the line */
1736 if (off >= max_off)
1737 return 1;
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /* Number of cells is equal to number of bytes, except for euc-jp when
1740 * the first byte is 0x8e. */
1741 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1742 return 1;
1743 return MB_BYTE2LEN(ScreenLines[off]);
1744}
1745
1746 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001747utf_off2cells(unsigned off, unsigned max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001749 return (off + 1 < max_off && ScreenLines[off + 1] == 0) ? 2 : 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750}
1751
1752/*
1753 * mb_ptr2char() function pointer.
1754 * Convert a byte sequence into a character.
1755 */
1756 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001757latin_ptr2char(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 return *p;
1760}
1761
1762 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001763dbcs_ptr2char(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 if (MB_BYTE2LEN(*p) > 1 && p[1] != NUL)
1766 return (p[0] << 8) + p[1];
1767 return *p;
1768}
1769
1770/*
1771 * Convert a UTF-8 byte sequence to a wide character.
1772 * If the sequence is illegal or truncated by a NUL the first byte is
1773 * returned.
1774 * Does not include composing characters, of course.
1775 */
1776 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001777utf_ptr2char(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 int len;
1780
1781 if (p[0] < 0x80) /* be quick for ASCII */
1782 return p[0];
1783
Bram Moolenaar24397332009-12-02 14:02:39 +00001784 len = utf8len_tab_zero[p[0]];
Bram Moolenaar89bf0922008-06-29 14:16:06 +00001785 if (len > 1 && (p[1] & 0xc0) == 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
1787 if (len == 2)
1788 return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
1789 if ((p[2] & 0xc0) == 0x80)
1790 {
1791 if (len == 3)
1792 return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
1793 + (p[2] & 0x3f);
1794 if ((p[3] & 0xc0) == 0x80)
1795 {
1796 if (len == 4)
1797 return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
1798 + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
1799 if ((p[4] & 0xc0) == 0x80)
1800 {
1801 if (len == 5)
1802 return ((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
1803 + ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
1804 + (p[4] & 0x3f);
1805 if ((p[5] & 0xc0) == 0x80 && len == 6)
1806 return ((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
1807 + ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
1808 + ((p[4] & 0x3f) << 6) + (p[5] & 0x3f);
1809 }
1810 }
1811 }
1812 }
1813 /* Illegal value, just return the first byte */
1814 return p[0];
1815}
1816
1817/*
Bram Moolenaar35ee4522011-07-15 21:16:59 +02001818 * Convert a UTF-8 byte sequence to a wide character.
1819 * String is assumed to be terminated by NUL or after "n" bytes, whichever
1820 * comes first.
1821 * The function is safe in the sense that it never accesses memory beyond the
1822 * first "n" bytes of "s".
1823 *
1824 * On success, returns decoded codepoint, advances "s" to the beginning of
1825 * next character and decreases "n" accordingly.
1826 *
1827 * If end of string was reached, returns 0 and, if "n" > 0, advances "s" past
1828 * NUL byte.
1829 *
1830 * If byte sequence is illegal or incomplete, returns -1 and does not advance
1831 * "s".
1832 */
1833 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001834utf_safe_read_char_adv(char_u **s, size_t *n)
Bram Moolenaar35ee4522011-07-15 21:16:59 +02001835{
1836 int c, k;
1837
1838 if (*n == 0) /* end of buffer */
1839 return 0;
1840
1841 k = utf8len_tab_zero[**s];
1842
1843 if (k == 1)
1844 {
1845 /* ASCII character or NUL */
1846 (*n)--;
1847 return *(*s)++;
1848 }
1849
1850 if ((size_t)k <= *n)
1851 {
1852 /* We have a multibyte sequence and it isn't truncated by buffer
1853 * limits so utf_ptr2char() is safe to use. Or the first byte is
1854 * illegal (k=0), and it's also safe to use utf_ptr2char(). */
1855 c = utf_ptr2char(*s);
1856
1857 /* On failure, utf_ptr2char() returns the first byte, so here we
1858 * check equality with the first byte. The only non-ASCII character
1859 * which equals the first byte of its own UTF-8 representation is
1860 * U+00C3 (UTF-8: 0xC3 0x83), so need to check that special case too.
1861 * It's safe even if n=1, else we would have k=2 > n. */
1862 if (c != (int)(**s) || (c == 0xC3 && (*s)[1] == 0x83))
1863 {
1864 /* byte sequence was successfully decoded */
1865 *s += k;
1866 *n -= k;
1867 return c;
1868 }
1869 }
1870
1871 /* byte sequence is incomplete or illegal */
1872 return -1;
1873}
1874
1875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 * Get character at **pp and advance *pp to the next character.
1877 * Note: composing characters are skipped!
1878 */
1879 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001880mb_ptr2char_adv(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
1882 int c;
1883
1884 c = (*mb_ptr2char)(*pp);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001885 *pp += (*mb_ptr2len)(*pp);
1886 return c;
1887}
1888
1889/*
1890 * Get character at **pp and advance *pp to the next character.
1891 * Note: composing characters are returned as separate characters.
1892 */
1893 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001894mb_cptr2char_adv(char_u **pp)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001895{
1896 int c;
1897
1898 c = (*mb_ptr2char)(*pp);
1899 if (enc_utf8)
1900 *pp += utf_ptr2len(*pp);
1901 else
1902 *pp += (*mb_ptr2len)(*pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 return c;
1904}
1905
1906#if defined(FEAT_ARABIC) || defined(PROTO)
1907/*
1908 * Check whether we are dealing with Arabic combining characters.
1909 * Note: these are NOT really composing characters!
1910 */
1911 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001912arabic_combine(
1913 int one, /* first character */
1914 int two) /* character just after "one" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
1916 if (one == a_LAM)
1917 return arabic_maycombine(two);
1918 return FALSE;
1919}
1920
1921/*
1922 * Check whether we are dealing with a character that could be regarded as an
1923 * Arabic combining character, need to check the character before this.
1924 */
1925 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001926arabic_maycombine(int two)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927{
1928 if (p_arshape && !p_tbidi)
1929 return (two == a_ALEF_MADDA
1930 || two == a_ALEF_HAMZA_ABOVE
1931 || two == a_ALEF_HAMZA_BELOW
1932 || two == a_ALEF);
1933 return FALSE;
1934}
1935
1936/*
1937 * Check if the character pointed to by "p2" is a composing character when it
1938 * comes after "p1". For Arabic sometimes "ab" is replaced with "c", which
1939 * behaves like a composing character.
1940 */
1941 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001942utf_composinglike(char_u *p1, char_u *p2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943{
1944 int c2;
1945
1946 c2 = utf_ptr2char(p2);
1947 if (utf_iscomposing(c2))
1948 return TRUE;
1949 if (!arabic_maycombine(c2))
1950 return FALSE;
1951 return arabic_combine(utf_ptr2char(p1), c2);
1952}
1953#endif
1954
1955/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001956 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 * composing characters.
1958 */
1959 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001960utfc_ptr2char(
1961 char_u *p,
1962 int *pcc) /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
1964 int len;
1965 int c;
1966 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001967 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001970 len = utf_ptr2len(p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 /* Only accept a composing char when the first char isn't illegal. */
1973 if ((len > 1 || *p < 0x80)
1974 && p[len] >= 0x80
1975 && UTF_COMPOSINGLIKE(p, p + len))
1976 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001977 cc = utf_ptr2char(p + len);
1978 for (;;)
1979 {
1980 pcc[i++] = cc;
1981 if (i == MAX_MCO)
1982 break;
1983 len += utf_ptr2len(p + len);
1984 if (p[len] < 0x80 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1985 break;
1986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001988
1989 if (i < MAX_MCO) /* last composing char must be 0 */
1990 pcc[i] = 0;
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 return c;
1993}
1994
1995/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001996 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 * composing characters. Use no more than p[maxlen].
1998 */
1999 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002000utfc_ptr2char_len(
2001 char_u *p,
2002 int *pcc, /* return: composing chars, last one is 0 */
2003 int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 int len;
2006 int c;
2007 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002008 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002011 len = utf_ptr2len_len(p, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 /* Only accept a composing char when the first char isn't illegal. */
2013 if ((len > 1 || *p < 0x80)
2014 && len < maxlen
2015 && p[len] >= 0x80
2016 && UTF_COMPOSINGLIKE(p, p + len))
2017 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002018 cc = utf_ptr2char(p + len);
2019 for (;;)
2020 {
2021 pcc[i++] = cc;
2022 if (i == MAX_MCO)
2023 break;
2024 len += utf_ptr2len_len(p + len, maxlen - len);
2025 if (len >= maxlen
2026 || p[len] < 0x80
2027 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
2028 break;
2029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002031
2032 if (i < MAX_MCO) /* last composing char must be 0 */
2033 pcc[i] = 0;
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 return c;
2036}
2037
2038/*
2039 * Convert the character at screen position "off" to a sequence of bytes.
2040 * Includes the composing characters.
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002041 * "buf" must at least have the length MB_MAXBYTES + 1.
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002042 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 * Returns the produced number of bytes.
2044 */
2045 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002046utfc_char2bytes(int off, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047{
2048 int len;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002049 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 len = utf_char2bytes(ScreenLinesUC[off], buf);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002052 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002054 if (ScreenLinesC[i][off] == 0)
2055 break;
2056 len += utf_char2bytes(ScreenLinesC[i][off], buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058 return len;
2059}
2060
2061/*
2062 * Get the length of a UTF-8 byte sequence, not including any following
2063 * composing characters.
2064 * Returns 0 for "".
2065 * Returns 1 for an illegal byte sequence.
2066 */
2067 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002068utf_ptr2len(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
2070 int len;
2071 int i;
2072
2073 if (*p == NUL)
2074 return 0;
2075 len = utf8len_tab[*p];
2076 for (i = 1; i < len; ++i)
2077 if ((p[i] & 0xc0) != 0x80)
2078 return 1;
2079 return len;
2080}
2081
2082/*
2083 * Return length of UTF-8 character, obtained from the first byte.
2084 * "b" must be between 0 and 255!
Bram Moolenaar24397332009-12-02 14:02:39 +00002085 * Returns 1 for an invalid first byte value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 */
2087 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002088utf_byte2len(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089{
2090 return utf8len_tab[b];
2091}
2092
2093/*
2094 * Get the length of UTF-8 byte sequence "p[size]". Does not include any
2095 * following composing characters.
2096 * Returns 1 for "".
Bram Moolenaarc048f672008-01-04 16:47:25 +00002097 * Returns 1 for an illegal byte sequence (also in incomplete byte seq.).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 * Returns number > "size" for an incomplete byte sequence.
Bram Moolenaar24397332009-12-02 14:02:39 +00002099 * Never returns zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 */
2101 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002102utf_ptr2len_len(char_u *p, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 int len;
2105 int i;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002106 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaar24397332009-12-02 14:02:39 +00002108 len = utf8len_tab[*p];
2109 if (len == 1)
2110 return 1; /* NUL, ascii or illegal lead byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (len > size)
Bram Moolenaarc048f672008-01-04 16:47:25 +00002112 m = size; /* incomplete byte sequence. */
Bram Moolenaar24397332009-12-02 14:02:39 +00002113 else
2114 m = len;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002115 for (i = 1; i < m; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if ((p[i] & 0xc0) != 0x80)
2117 return 1;
2118 return len;
2119}
2120
2121/*
2122 * Return the number of bytes the UTF-8 encoding of the character at "p" takes.
2123 * This includes following composing characters.
2124 */
2125 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002126utfc_ptr2len(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 int len;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002129 int b0 = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#ifdef FEAT_ARABIC
2131 int prevlen;
2132#endif
2133
Bram Moolenaarc669e662005-06-08 22:00:03 +00002134 if (b0 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 return 0;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002136 if (b0 < 0x80 && p[1] < 0x80) /* be quick for ASCII */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 return 1;
2138
2139 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002140 len = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /* Check for illegal byte. */
Bram Moolenaarc669e662005-06-08 22:00:03 +00002143 if (len == 1 && b0 >= 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 return 1;
2145
2146 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002147 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * skip all of them (otherwise the cursor would get stuck).
2149 */
2150#ifdef FEAT_ARABIC
2151 prevlen = 0;
2152#endif
2153 for (;;)
2154 {
2155 if (p[len] < 0x80 || !UTF_COMPOSINGLIKE(p + prevlen, p + len))
2156 return len;
2157
2158 /* Skip over composing char */
2159#ifdef FEAT_ARABIC
2160 prevlen = len;
2161#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002162 len += utf_ptr2len(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164}
2165
2166/*
2167 * Return the number of bytes the UTF-8 encoding of the character at "p[size]"
2168 * takes. This includes following composing characters.
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002169 * Returns 0 for an empty string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 * Returns 1 for an illegal char or an incomplete byte sequence.
2171 */
2172 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002173utfc_ptr2len_len(char_u *p, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 int len;
2176#ifdef FEAT_ARABIC
2177 int prevlen;
2178#endif
2179
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002180 if (size < 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 return 0;
2182 if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */
2183 return 1;
2184
2185 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002186 len = utf_ptr2len_len(p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 /* Check for illegal byte and incomplete byte sequence. */
2189 if ((len == 1 && p[0] >= 0x80) || len > size)
2190 return 1;
2191
2192 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002193 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 * skip all of them (otherwise the cursor would get stuck).
2195 */
2196#ifdef FEAT_ARABIC
2197 prevlen = 0;
2198#endif
2199 while (len < size)
2200 {
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002201 int len_next_char;
2202
2203 if (p[len] < 0x80)
2204 break;
2205
2206 /*
2207 * Next character length should not go beyond size to ensure that
2208 * UTF_COMPOSINGLIKE(...) does not read beyond size.
2209 */
2210 len_next_char = utf_ptr2len_len(p + len, size - len);
2211 if (len_next_char > size - len)
2212 break;
2213
2214 if (!UTF_COMPOSINGLIKE(p + prevlen, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 break;
2216
2217 /* Skip over composing char */
2218#ifdef FEAT_ARABIC
2219 prevlen = len;
2220#endif
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002221 len += len_next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223 return len;
2224}
2225
2226/*
2227 * Return the number of bytes the UTF-8 encoding of character "c" takes.
2228 * This does not include composing characters.
2229 */
2230 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002231utf_char2len(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 if (c < 0x80)
2234 return 1;
2235 if (c < 0x800)
2236 return 2;
2237 if (c < 0x10000)
2238 return 3;
2239 if (c < 0x200000)
2240 return 4;
2241 if (c < 0x4000000)
2242 return 5;
2243 return 6;
2244}
2245
2246/*
2247 * Convert Unicode character "c" to UTF-8 string in "buf[]".
2248 * Returns the number of bytes.
2249 * This does not include composing characters.
2250 */
2251 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002252utf_char2bytes(int c, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
2254 if (c < 0x80) /* 7 bits */
2255 {
2256 buf[0] = c;
2257 return 1;
2258 }
2259 if (c < 0x800) /* 11 bits */
2260 {
2261 buf[0] = 0xc0 + ((unsigned)c >> 6);
2262 buf[1] = 0x80 + (c & 0x3f);
2263 return 2;
2264 }
2265 if (c < 0x10000) /* 16 bits */
2266 {
2267 buf[0] = 0xe0 + ((unsigned)c >> 12);
2268 buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2269 buf[2] = 0x80 + (c & 0x3f);
2270 return 3;
2271 }
2272 if (c < 0x200000) /* 21 bits */
2273 {
2274 buf[0] = 0xf0 + ((unsigned)c >> 18);
2275 buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2276 buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2277 buf[3] = 0x80 + (c & 0x3f);
2278 return 4;
2279 }
2280 if (c < 0x4000000) /* 26 bits */
2281 {
2282 buf[0] = 0xf8 + ((unsigned)c >> 24);
2283 buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2284 buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2285 buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2286 buf[4] = 0x80 + (c & 0x3f);
2287 return 5;
2288 }
2289 /* 31 bits */
2290 buf[0] = 0xfc + ((unsigned)c >> 30);
2291 buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f);
2292 buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2293 buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2294 buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2295 buf[5] = 0x80 + (c & 0x3f);
2296 return 6;
2297}
2298
2299/*
2300 * Return TRUE if "c" is a composing UTF-8 character. This means it will be
2301 * drawn on top of the preceding character.
2302 * Based on code from Markus Kuhn.
2303 */
2304 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002305utf_iscomposing(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002307 /* Sorted list of non-overlapping intervals.
2308 * Generated by ../runtime/tools/unicode.vim. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 static struct interval combining[] =
2310 {
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002311 {0x0300, 0x036f},
2312 {0x0483, 0x0489},
2313 {0x0591, 0x05bd},
2314 {0x05bf, 0x05bf},
2315 {0x05c1, 0x05c2},
2316 {0x05c4, 0x05c5},
2317 {0x05c7, 0x05c7},
2318 {0x0610, 0x061a},
Bram Moolenaarea676722015-01-14 17:40:09 +01002319 {0x064b, 0x065f},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002320 {0x0670, 0x0670},
2321 {0x06d6, 0x06dc},
Bram Moolenaarea676722015-01-14 17:40:09 +01002322 {0x06df, 0x06e4},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002323 {0x06e7, 0x06e8},
2324 {0x06ea, 0x06ed},
2325 {0x0711, 0x0711},
2326 {0x0730, 0x074a},
2327 {0x07a6, 0x07b0},
2328 {0x07eb, 0x07f3},
2329 {0x0816, 0x0819},
2330 {0x081b, 0x0823},
2331 {0x0825, 0x0827},
2332 {0x0829, 0x082d},
Bram Moolenaarea676722015-01-14 17:40:09 +01002333 {0x0859, 0x085b},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002334 {0x08d4, 0x08e1},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002335 {0x08e3, 0x0903},
Bram Moolenaarea676722015-01-14 17:40:09 +01002336 {0x093a, 0x093c},
2337 {0x093e, 0x094f},
2338 {0x0951, 0x0957},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002339 {0x0962, 0x0963},
2340 {0x0981, 0x0983},
2341 {0x09bc, 0x09bc},
2342 {0x09be, 0x09c4},
2343 {0x09c7, 0x09c8},
2344 {0x09cb, 0x09cd},
2345 {0x09d7, 0x09d7},
2346 {0x09e2, 0x09e3},
2347 {0x0a01, 0x0a03},
2348 {0x0a3c, 0x0a3c},
2349 {0x0a3e, 0x0a42},
2350 {0x0a47, 0x0a48},
2351 {0x0a4b, 0x0a4d},
2352 {0x0a51, 0x0a51},
2353 {0x0a70, 0x0a71},
2354 {0x0a75, 0x0a75},
2355 {0x0a81, 0x0a83},
2356 {0x0abc, 0x0abc},
2357 {0x0abe, 0x0ac5},
2358 {0x0ac7, 0x0ac9},
2359 {0x0acb, 0x0acd},
2360 {0x0ae2, 0x0ae3},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002361 {0x0afa, 0x0aff},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002362 {0x0b01, 0x0b03},
2363 {0x0b3c, 0x0b3c},
2364 {0x0b3e, 0x0b44},
2365 {0x0b47, 0x0b48},
2366 {0x0b4b, 0x0b4d},
2367 {0x0b56, 0x0b57},
2368 {0x0b62, 0x0b63},
2369 {0x0b82, 0x0b82},
2370 {0x0bbe, 0x0bc2},
2371 {0x0bc6, 0x0bc8},
2372 {0x0bca, 0x0bcd},
2373 {0x0bd7, 0x0bd7},
Bram Moolenaarea676722015-01-14 17:40:09 +01002374 {0x0c00, 0x0c03},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002375 {0x0c3e, 0x0c44},
2376 {0x0c46, 0x0c48},
2377 {0x0c4a, 0x0c4d},
2378 {0x0c55, 0x0c56},
2379 {0x0c62, 0x0c63},
Bram Moolenaarea676722015-01-14 17:40:09 +01002380 {0x0c81, 0x0c83},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002381 {0x0cbc, 0x0cbc},
2382 {0x0cbe, 0x0cc4},
2383 {0x0cc6, 0x0cc8},
2384 {0x0cca, 0x0ccd},
2385 {0x0cd5, 0x0cd6},
2386 {0x0ce2, 0x0ce3},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002387 {0x0d00, 0x0d03},
2388 {0x0d3b, 0x0d3c},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002389 {0x0d3e, 0x0d44},
2390 {0x0d46, 0x0d48},
2391 {0x0d4a, 0x0d4d},
2392 {0x0d57, 0x0d57},
2393 {0x0d62, 0x0d63},
2394 {0x0d82, 0x0d83},
2395 {0x0dca, 0x0dca},
2396 {0x0dcf, 0x0dd4},
2397 {0x0dd6, 0x0dd6},
2398 {0x0dd8, 0x0ddf},
2399 {0x0df2, 0x0df3},
2400 {0x0e31, 0x0e31},
2401 {0x0e34, 0x0e3a},
2402 {0x0e47, 0x0e4e},
2403 {0x0eb1, 0x0eb1},
2404 {0x0eb4, 0x0eb9},
2405 {0x0ebb, 0x0ebc},
2406 {0x0ec8, 0x0ecd},
2407 {0x0f18, 0x0f19},
2408 {0x0f35, 0x0f35},
2409 {0x0f37, 0x0f37},
2410 {0x0f39, 0x0f39},
2411 {0x0f3e, 0x0f3f},
2412 {0x0f71, 0x0f84},
2413 {0x0f86, 0x0f87},
Bram Moolenaarea676722015-01-14 17:40:09 +01002414 {0x0f8d, 0x0f97},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002415 {0x0f99, 0x0fbc},
2416 {0x0fc6, 0x0fc6},
2417 {0x102b, 0x103e},
2418 {0x1056, 0x1059},
2419 {0x105e, 0x1060},
2420 {0x1062, 0x1064},
2421 {0x1067, 0x106d},
2422 {0x1071, 0x1074},
2423 {0x1082, 0x108d},
2424 {0x108f, 0x108f},
2425 {0x109a, 0x109d},
Bram Moolenaarea676722015-01-14 17:40:09 +01002426 {0x135d, 0x135f},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002427 {0x1712, 0x1714},
2428 {0x1732, 0x1734},
2429 {0x1752, 0x1753},
2430 {0x1772, 0x1773},
Bram Moolenaarea676722015-01-14 17:40:09 +01002431 {0x17b4, 0x17d3},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002432 {0x17dd, 0x17dd},
2433 {0x180b, 0x180d},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002434 {0x1885, 0x1886},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002435 {0x18a9, 0x18a9},
2436 {0x1920, 0x192b},
2437 {0x1930, 0x193b},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002438 {0x1a17, 0x1a1b},
2439 {0x1a55, 0x1a5e},
2440 {0x1a60, 0x1a7c},
2441 {0x1a7f, 0x1a7f},
Bram Moolenaarea676722015-01-14 17:40:09 +01002442 {0x1ab0, 0x1abe},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002443 {0x1b00, 0x1b04},
2444 {0x1b34, 0x1b44},
2445 {0x1b6b, 0x1b73},
2446 {0x1b80, 0x1b82},
Bram Moolenaarea676722015-01-14 17:40:09 +01002447 {0x1ba1, 0x1bad},
2448 {0x1be6, 0x1bf3},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002449 {0x1c24, 0x1c37},
2450 {0x1cd0, 0x1cd2},
2451 {0x1cd4, 0x1ce8},
2452 {0x1ced, 0x1ced},
Bram Moolenaarea676722015-01-14 17:40:09 +01002453 {0x1cf2, 0x1cf4},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002454 {0x1cf7, 0x1cf9},
2455 {0x1dc0, 0x1df9},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002456 {0x1dfb, 0x1dff},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002457 {0x20d0, 0x20f0},
2458 {0x2cef, 0x2cf1},
Bram Moolenaarea676722015-01-14 17:40:09 +01002459 {0x2d7f, 0x2d7f},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002460 {0x2de0, 0x2dff},
2461 {0x302a, 0x302f},
2462 {0x3099, 0x309a},
2463 {0xa66f, 0xa672},
Bram Moolenaarea676722015-01-14 17:40:09 +01002464 {0xa674, 0xa67d},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002465 {0xa69e, 0xa69f},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002466 {0xa6f0, 0xa6f1},
2467 {0xa802, 0xa802},
2468 {0xa806, 0xa806},
2469 {0xa80b, 0xa80b},
2470 {0xa823, 0xa827},
2471 {0xa880, 0xa881},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002472 {0xa8b4, 0xa8c5},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002473 {0xa8e0, 0xa8f1},
2474 {0xa926, 0xa92d},
2475 {0xa947, 0xa953},
2476 {0xa980, 0xa983},
2477 {0xa9b3, 0xa9c0},
Bram Moolenaarea676722015-01-14 17:40:09 +01002478 {0xa9e5, 0xa9e5},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002479 {0xaa29, 0xaa36},
2480 {0xaa43, 0xaa43},
2481 {0xaa4c, 0xaa4d},
Bram Moolenaarea676722015-01-14 17:40:09 +01002482 {0xaa7b, 0xaa7d},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002483 {0xaab0, 0xaab0},
2484 {0xaab2, 0xaab4},
2485 {0xaab7, 0xaab8},
2486 {0xaabe, 0xaabf},
2487 {0xaac1, 0xaac1},
Bram Moolenaarea676722015-01-14 17:40:09 +01002488 {0xaaeb, 0xaaef},
2489 {0xaaf5, 0xaaf6},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002490 {0xabe3, 0xabea},
2491 {0xabec, 0xabed},
2492 {0xfb1e, 0xfb1e},
2493 {0xfe00, 0xfe0f},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002494 {0xfe20, 0xfe2f},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002495 {0x101fd, 0x101fd},
Bram Moolenaarea676722015-01-14 17:40:09 +01002496 {0x102e0, 0x102e0},
2497 {0x10376, 0x1037a},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002498 {0x10a01, 0x10a03},
2499 {0x10a05, 0x10a06},
2500 {0x10a0c, 0x10a0f},
2501 {0x10a38, 0x10a3a},
2502 {0x10a3f, 0x10a3f},
Bram Moolenaarea676722015-01-14 17:40:09 +01002503 {0x10ae5, 0x10ae6},
2504 {0x11000, 0x11002},
2505 {0x11038, 0x11046},
2506 {0x1107f, 0x11082},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002507 {0x110b0, 0x110ba},
Bram Moolenaarea676722015-01-14 17:40:09 +01002508 {0x11100, 0x11102},
2509 {0x11127, 0x11134},
2510 {0x11173, 0x11173},
2511 {0x11180, 0x11182},
2512 {0x111b3, 0x111c0},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002513 {0x111ca, 0x111cc},
Bram Moolenaarea676722015-01-14 17:40:09 +01002514 {0x1122c, 0x11237},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002515 {0x1123e, 0x1123e},
Bram Moolenaarea676722015-01-14 17:40:09 +01002516 {0x112df, 0x112ea},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002517 {0x11300, 0x11303},
Bram Moolenaarea676722015-01-14 17:40:09 +01002518 {0x1133c, 0x1133c},
2519 {0x1133e, 0x11344},
2520 {0x11347, 0x11348},
2521 {0x1134b, 0x1134d},
2522 {0x11357, 0x11357},
2523 {0x11362, 0x11363},
2524 {0x11366, 0x1136c},
2525 {0x11370, 0x11374},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002526 {0x11435, 0x11446},
Bram Moolenaarea676722015-01-14 17:40:09 +01002527 {0x114b0, 0x114c3},
2528 {0x115af, 0x115b5},
2529 {0x115b8, 0x115c0},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002530 {0x115dc, 0x115dd},
Bram Moolenaarea676722015-01-14 17:40:09 +01002531 {0x11630, 0x11640},
2532 {0x116ab, 0x116b7},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002533 {0x1171d, 0x1172b},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002534 {0x11a01, 0x11a0a},
2535 {0x11a33, 0x11a39},
2536 {0x11a3b, 0x11a3e},
2537 {0x11a47, 0x11a47},
2538 {0x11a51, 0x11a5b},
2539 {0x11a8a, 0x11a99},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002540 {0x11c2f, 0x11c36},
2541 {0x11c38, 0x11c3f},
2542 {0x11c92, 0x11ca7},
2543 {0x11ca9, 0x11cb6},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002544 {0x11d31, 0x11d36},
2545 {0x11d3a, 0x11d3a},
2546 {0x11d3c, 0x11d3d},
2547 {0x11d3f, 0x11d45},
2548 {0x11d47, 0x11d47},
Bram Moolenaarea676722015-01-14 17:40:09 +01002549 {0x16af0, 0x16af4},
2550 {0x16b30, 0x16b36},
2551 {0x16f51, 0x16f7e},
2552 {0x16f8f, 0x16f92},
2553 {0x1bc9d, 0x1bc9e},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002554 {0x1d165, 0x1d169},
2555 {0x1d16d, 0x1d172},
2556 {0x1d17b, 0x1d182},
2557 {0x1d185, 0x1d18b},
2558 {0x1d1aa, 0x1d1ad},
2559 {0x1d242, 0x1d244},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02002560 {0x1da00, 0x1da36},
2561 {0x1da3b, 0x1da6c},
2562 {0x1da75, 0x1da75},
2563 {0x1da84, 0x1da84},
2564 {0x1da9b, 0x1da9f},
2565 {0x1daa1, 0x1daaf},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002566 {0x1e000, 0x1e006},
2567 {0x1e008, 0x1e018},
2568 {0x1e01b, 0x1e021},
2569 {0x1e023, 0x1e024},
2570 {0x1e026, 0x1e02a},
Bram Moolenaarea676722015-01-14 17:40:09 +01002571 {0x1e8d0, 0x1e8d6},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02002572 {0x1e944, 0x1e94a},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002573 {0xe0100, 0xe01ef}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 };
2575
2576 return intable(combining, sizeof(combining), c);
2577}
2578
2579/*
2580 * Return TRUE for characters that can be displayed in a normal way.
2581 * Only for characters of 0x100 and above!
2582 */
2583 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002584utf_printable(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585{
2586#ifdef USE_WCHAR_FUNCTIONS
2587 /*
2588 * Assume the iswprint() library function works better than our own stuff.
2589 */
2590 return iswprint(c);
2591#else
2592 /* Sorted list of non-overlapping intervals.
2593 * 0xd800-0xdfff is reserved for UTF-16, actually illegal. */
2594 static struct interval nonprint[] =
2595 {
2596 {0x070f, 0x070f}, {0x180b, 0x180e}, {0x200b, 0x200f}, {0x202a, 0x202e},
2597 {0x206a, 0x206f}, {0xd800, 0xdfff}, {0xfeff, 0xfeff}, {0xfff9, 0xfffb},
2598 {0xfffe, 0xffff}
2599 };
2600
2601 return !intable(nonprint, sizeof(nonprint), c);
2602#endif
2603}
2604
Bram Moolenaarcb070082016-04-02 22:14:51 +02002605/* Sorted list of non-overlapping intervals of all Emoji characters,
2606 * based on http://unicode.org/emoji/charts/emoji-list.html */
2607static struct interval emoji_all[] =
2608{
2609 {0x203c, 0x203c},
2610 {0x2049, 0x2049},
2611 {0x2122, 0x2122},
2612 {0x2139, 0x2139},
2613 {0x2194, 0x2199},
2614 {0x21a9, 0x21aa},
2615 {0x231a, 0x231b},
2616 {0x2328, 0x2328},
2617 {0x23cf, 0x23cf},
2618 {0x23e9, 0x23f3},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002619 {0x23f8, 0x23fa},
Bram Moolenaarcb070082016-04-02 22:14:51 +02002620 {0x24c2, 0x24c2},
2621 {0x25aa, 0x25ab},
2622 {0x25b6, 0x25b6},
2623 {0x25c0, 0x25c0},
2624 {0x25fb, 0x25fe},
2625 {0x2600, 0x2604},
2626 {0x260e, 0x260e},
2627 {0x2611, 0x2611},
2628 {0x2614, 0x2615},
2629 {0x2618, 0x2618},
2630 {0x261d, 0x261d},
2631 {0x2620, 0x2620},
2632 {0x2622, 0x2623},
2633 {0x2626, 0x2626},
2634 {0x262a, 0x262a},
2635 {0x262e, 0x262f},
2636 {0x2638, 0x263a},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002637 {0x2640, 0x2640},
2638 {0x2642, 0x2642},
Bram Moolenaarcb070082016-04-02 22:14:51 +02002639 {0x2648, 0x2653},
2640 {0x2660, 0x2660},
2641 {0x2663, 0x2663},
2642 {0x2665, 0x2666},
2643 {0x2668, 0x2668},
2644 {0x267b, 0x267b},
2645 {0x267f, 0x267f},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002646 {0x2692, 0x2697},
Bram Moolenaarcb070082016-04-02 22:14:51 +02002647 {0x2699, 0x2699},
2648 {0x269b, 0x269c},
2649 {0x26a0, 0x26a1},
2650 {0x26aa, 0x26ab},
2651 {0x26b0, 0x26b1},
2652 {0x26bd, 0x26be},
2653 {0x26c4, 0x26c5},
2654 {0x26c8, 0x26c8},
2655 {0x26ce, 0x26cf},
2656 {0x26d1, 0x26d1},
2657 {0x26d3, 0x26d4},
2658 {0x26e9, 0x26ea},
2659 {0x26f0, 0x26f5},
2660 {0x26f7, 0x26fa},
2661 {0x26fd, 0x26fd},
2662 {0x2702, 0x2702},
2663 {0x2705, 0x2705},
2664 {0x2708, 0x270d},
2665 {0x270f, 0x270f},
2666 {0x2712, 0x2712},
2667 {0x2714, 0x2714},
2668 {0x2716, 0x2716},
2669 {0x271d, 0x271d},
2670 {0x2721, 0x2721},
2671 {0x2728, 0x2728},
2672 {0x2733, 0x2734},
2673 {0x2744, 0x2744},
2674 {0x2747, 0x2747},
2675 {0x274c, 0x274c},
2676 {0x274e, 0x274e},
2677 {0x2753, 0x2755},
2678 {0x2757, 0x2757},
2679 {0x2763, 0x2764},
2680 {0x2795, 0x2797},
2681 {0x27a1, 0x27a1},
2682 {0x27b0, 0x27b0},
2683 {0x27bf, 0x27bf},
2684 {0x2934, 0x2935},
2685 {0x2b05, 0x2b07},
2686 {0x2b1b, 0x2b1c},
2687 {0x2b50, 0x2b50},
2688 {0x2b55, 0x2b55},
2689 {0x3030, 0x3030},
2690 {0x303d, 0x303d},
2691 {0x3297, 0x3297},
2692 {0x3299, 0x3299},
2693 {0x1f004, 0x1f004},
2694 {0x1f0cf, 0x1f0cf},
2695 {0x1f170, 0x1f171},
2696 {0x1f17e, 0x1f17f},
2697 {0x1f18e, 0x1f18e},
2698 {0x1f191, 0x1f19a},
2699 {0x1f1e6, 0x1f1ff},
2700 {0x1f201, 0x1f202},
2701 {0x1f21a, 0x1f21a},
2702 {0x1f22f, 0x1f22f},
2703 {0x1f232, 0x1f23a},
2704 {0x1f250, 0x1f251},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002705 {0x1f300, 0x1f321},
2706 {0x1f324, 0x1f393},
2707 {0x1f396, 0x1f397},
2708 {0x1f399, 0x1f39b},
2709 {0x1f39e, 0x1f3f0},
2710 {0x1f3f3, 0x1f3f5},
2711 {0x1f3f7, 0x1f4fd},
2712 {0x1f4ff, 0x1f53d},
2713 {0x1f549, 0x1f54e},
Bram Moolenaarcb070082016-04-02 22:14:51 +02002714 {0x1f550, 0x1f567},
Bram Moolenaar383aa842017-06-22 15:27:37 +02002715 {0x1f56f, 0x1f570},
2716 {0x1f573, 0x1f57a},
2717 {0x1f587, 0x1f587},
2718 {0x1f58a, 0x1f58d},
2719 {0x1f590, 0x1f590},
2720 {0x1f595, 0x1f596},
2721 {0x1f5a4, 0x1f5a5},
2722 {0x1f5a8, 0x1f5a8},
2723 {0x1f5b1, 0x1f5b2},
2724 {0x1f5bc, 0x1f5bc},
2725 {0x1f5c2, 0x1f5c4},
2726 {0x1f5d1, 0x1f5d3},
2727 {0x1f5dc, 0x1f5de},
2728 {0x1f5e1, 0x1f5e1},
2729 {0x1f5e3, 0x1f5e3},
2730 {0x1f5e8, 0x1f5e8},
2731 {0x1f5ef, 0x1f5ef},
2732 {0x1f5f3, 0x1f5f3},
2733 {0x1f5fa, 0x1f64f},
2734 {0x1f680, 0x1f6c5},
2735 {0x1f6cb, 0x1f6d2},
2736 {0x1f6e0, 0x1f6e5},
2737 {0x1f6e9, 0x1f6e9},
2738 {0x1f6eb, 0x1f6ec},
2739 {0x1f6f0, 0x1f6f0},
2740 {0x1f6f3, 0x1f6f8},
2741 {0x1f910, 0x1f93a},
2742 {0x1f93c, 0x1f93e},
2743 {0x1f940, 0x1f945},
2744 {0x1f947, 0x1f94c},
2745 {0x1f950, 0x1f96b},
2746 {0x1f980, 0x1f997},
2747 {0x1f9c0, 0x1f9c0},
2748 {0x1f9d0, 0x1f9e6}
Bram Moolenaarcb070082016-04-02 22:14:51 +02002749};
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751/*
2752 * Get class of a Unicode character.
2753 * 0: white space
2754 * 1: punctuation
2755 * 2 or bigger: some class of word character.
2756 */
2757 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002758utf_class(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
Bram Moolenaar4019cf92017-01-28 16:39:34 +01002760 return utf_class_buf(c, curbuf);
2761}
2762
2763 int
2764utf_class_buf(int c, buf_T *buf)
2765{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 /* sorted list of non-overlapping intervals */
2767 static struct clinterval
2768 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002769 unsigned int first;
2770 unsigned int last;
2771 unsigned int class;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 } classes[] =
2773 {
2774 {0x037e, 0x037e, 1}, /* Greek question mark */
2775 {0x0387, 0x0387, 1}, /* Greek ano teleia */
2776 {0x055a, 0x055f, 1}, /* Armenian punctuation */
2777 {0x0589, 0x0589, 1}, /* Armenian full stop */
2778 {0x05be, 0x05be, 1},
2779 {0x05c0, 0x05c0, 1},
2780 {0x05c3, 0x05c3, 1},
2781 {0x05f3, 0x05f4, 1},
2782 {0x060c, 0x060c, 1},
2783 {0x061b, 0x061b, 1},
2784 {0x061f, 0x061f, 1},
2785 {0x066a, 0x066d, 1},
2786 {0x06d4, 0x06d4, 1},
2787 {0x0700, 0x070d, 1}, /* Syriac punctuation */
2788 {0x0964, 0x0965, 1},
2789 {0x0970, 0x0970, 1},
2790 {0x0df4, 0x0df4, 1},
2791 {0x0e4f, 0x0e4f, 1},
2792 {0x0e5a, 0x0e5b, 1},
2793 {0x0f04, 0x0f12, 1},
2794 {0x0f3a, 0x0f3d, 1},
2795 {0x0f85, 0x0f85, 1},
2796 {0x104a, 0x104f, 1}, /* Myanmar punctuation */
2797 {0x10fb, 0x10fb, 1}, /* Georgian punctuation */
2798 {0x1361, 0x1368, 1}, /* Ethiopic punctuation */
2799 {0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
2800 {0x1680, 0x1680, 0},
2801 {0x169b, 0x169c, 1},
2802 {0x16eb, 0x16ed, 1},
2803 {0x1735, 0x1736, 1},
2804 {0x17d4, 0x17dc, 1}, /* Khmer punctuation */
2805 {0x1800, 0x180a, 1}, /* Mongolian punctuation */
2806 {0x2000, 0x200b, 0}, /* spaces */
2807 {0x200c, 0x2027, 1}, /* punctuation and symbols */
2808 {0x2028, 0x2029, 0},
2809 {0x202a, 0x202e, 1}, /* punctuation and symbols */
2810 {0x202f, 0x202f, 0},
2811 {0x2030, 0x205e, 1}, /* punctuation and symbols */
2812 {0x205f, 0x205f, 0},
2813 {0x2060, 0x27ff, 1}, /* punctuation and symbols */
2814 {0x2070, 0x207f, 0x2070}, /* superscript */
Bram Moolenaarbbb79722008-06-04 09:00:32 +00002815 {0x2080, 0x2094, 0x2080}, /* subscript */
2816 {0x20a0, 0x27ff, 1}, /* all kinds of symbols */
2817 {0x2800, 0x28ff, 0x2800}, /* braille */
2818 {0x2900, 0x2998, 1}, /* arrows, brackets, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {0x29d8, 0x29db, 1},
2820 {0x29fc, 0x29fd, 1},
Bram Moolenaar103650d2014-09-15 14:25:54 +02002821 {0x2e00, 0x2e7f, 1}, /* supplemental punctuation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {0x3000, 0x3000, 0}, /* ideographic space */
2823 {0x3001, 0x3020, 1}, /* ideographic punctuation */
2824 {0x3030, 0x3030, 1},
2825 {0x303d, 0x303d, 1},
2826 {0x3040, 0x309f, 0x3040}, /* Hiragana */
2827 {0x30a0, 0x30ff, 0x30a0}, /* Katakana */
2828 {0x3300, 0x9fff, 0x4e00}, /* CJK Ideographs */
2829 {0xac00, 0xd7a3, 0xac00}, /* Hangul Syllables */
2830 {0xf900, 0xfaff, 0x4e00}, /* CJK Ideographs */
2831 {0xfd3e, 0xfd3f, 1},
2832 {0xfe30, 0xfe6b, 1}, /* punctuation forms */
2833 {0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
2834 {0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
2835 {0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
2836 {0xff5b, 0xff65, 1}, /* half/fullwidth ASCII */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002837 {0x20000, 0x2a6df, 0x4e00}, /* CJK Ideographs */
2838 {0x2a700, 0x2b73f, 0x4e00}, /* CJK Ideographs */
2839 {0x2b740, 0x2b81f, 0x4e00}, /* CJK Ideographs */
2840 {0x2f800, 0x2fa1f, 0x4e00}, /* CJK Ideographs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 };
Bram Moolenaarb86f10e2016-03-21 22:09:44 +01002842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 int bot = 0;
2844 int top = sizeof(classes) / sizeof(struct clinterval) - 1;
2845 int mid;
2846
2847 /* First quick check for Latin1 characters, use 'iskeyword'. */
2848 if (c < 0x100)
2849 {
Bram Moolenaarf7bbbc52005-06-17 21:55:00 +00002850 if (c == ' ' || c == '\t' || c == NUL || c == 0xa0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 return 0; /* blank */
Bram Moolenaar4019cf92017-01-28 16:39:34 +01002852 if (vim_iswordc_buf(c, buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 return 2; /* word character */
2854 return 1; /* punctuation */
2855 }
2856
2857 /* binary search in table */
2858 while (top >= bot)
2859 {
2860 mid = (bot + top) / 2;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002861 if (classes[mid].last < (unsigned int)c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 bot = mid + 1;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002863 else if (classes[mid].first > (unsigned int)c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 top = mid - 1;
2865 else
2866 return (int)classes[mid].class;
2867 }
2868
Bram Moolenaar4077b332016-03-20 18:15:21 +01002869 /* emoji */
Bram Moolenaarb86f10e2016-03-21 22:09:44 +01002870 if (intable(emoji_all, sizeof(emoji_all), c))
Bram Moolenaar4077b332016-03-20 18:15:21 +01002871 return 3;
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 /* most other characters are "word" characters */
2874 return 2;
2875}
2876
Bram Moolenaarcb070082016-04-02 22:14:51 +02002877 int
2878utf_ambiguous_width(int c)
2879{
2880 return c >= 0x80 && (intable(ambiguous, sizeof(ambiguous), c)
2881 || intable(emoji_all, sizeof(emoji_all), c));
2882}
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884/*
2885 * Code for Unicode case-dependent operations. Based on notes in
2886 * http://www.unicode.org/Public/UNIDATA/CaseFolding.txt
2887 * This code uses simple case folding, not full case folding.
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002888 * Last updated for Unicode 5.2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 */
2890
2891/*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002892 * The following tables are built by ../runtime/tools/unicode.vim.
2893 * They must be in numeric order, because we use binary search.
2894 * An entry such as {0x41,0x5a,1,32} means that Unicode characters in the
2895 * range from 0x41 to 0x5a inclusive, stepping by 1, are changed to
2896 * folded/upper/lower by adding 32.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898typedef struct
2899{
2900 int rangeStart;
2901 int rangeEnd;
2902 int step;
2903 int offset;
2904} convertStruct;
2905
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002906static convertStruct foldCase[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002908 {0x41,0x5a,1,32},
2909 {0xb5,0xb5,-1,775},
2910 {0xc0,0xd6,1,32},
2911 {0xd8,0xde,1,32},
2912 {0x100,0x12e,2,1},
2913 {0x132,0x136,2,1},
2914 {0x139,0x147,2,1},
2915 {0x14a,0x176,2,1},
2916 {0x178,0x178,-1,-121},
2917 {0x179,0x17d,2,1},
2918 {0x17f,0x17f,-1,-268},
2919 {0x181,0x181,-1,210},
2920 {0x182,0x184,2,1},
2921 {0x186,0x186,-1,206},
2922 {0x187,0x187,-1,1},
2923 {0x189,0x18a,1,205},
2924 {0x18b,0x18b,-1,1},
2925 {0x18e,0x18e,-1,79},
2926 {0x18f,0x18f,-1,202},
2927 {0x190,0x190,-1,203},
2928 {0x191,0x191,-1,1},
2929 {0x193,0x193,-1,205},
2930 {0x194,0x194,-1,207},
2931 {0x196,0x196,-1,211},
2932 {0x197,0x197,-1,209},
2933 {0x198,0x198,-1,1},
2934 {0x19c,0x19c,-1,211},
2935 {0x19d,0x19d,-1,213},
2936 {0x19f,0x19f,-1,214},
2937 {0x1a0,0x1a4,2,1},
2938 {0x1a6,0x1a6,-1,218},
2939 {0x1a7,0x1a7,-1,1},
2940 {0x1a9,0x1a9,-1,218},
2941 {0x1ac,0x1ac,-1,1},
2942 {0x1ae,0x1ae,-1,218},
2943 {0x1af,0x1af,-1,1},
2944 {0x1b1,0x1b2,1,217},
2945 {0x1b3,0x1b5,2,1},
2946 {0x1b7,0x1b7,-1,219},
2947 {0x1b8,0x1bc,4,1},
2948 {0x1c4,0x1c4,-1,2},
2949 {0x1c5,0x1c5,-1,1},
2950 {0x1c7,0x1c7,-1,2},
2951 {0x1c8,0x1c8,-1,1},
2952 {0x1ca,0x1ca,-1,2},
2953 {0x1cb,0x1db,2,1},
2954 {0x1de,0x1ee,2,1},
2955 {0x1f1,0x1f1,-1,2},
2956 {0x1f2,0x1f4,2,1},
2957 {0x1f6,0x1f6,-1,-97},
2958 {0x1f7,0x1f7,-1,-56},
2959 {0x1f8,0x21e,2,1},
2960 {0x220,0x220,-1,-130},
2961 {0x222,0x232,2,1},
2962 {0x23a,0x23a,-1,10795},
2963 {0x23b,0x23b,-1,1},
2964 {0x23d,0x23d,-1,-163},
2965 {0x23e,0x23e,-1,10792},
2966 {0x241,0x241,-1,1},
2967 {0x243,0x243,-1,-195},
2968 {0x244,0x244,-1,69},
2969 {0x245,0x245,-1,71},
2970 {0x246,0x24e,2,1},
2971 {0x345,0x345,-1,116},
2972 {0x370,0x372,2,1},
2973 {0x376,0x376,-1,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01002974 {0x37f,0x37f,-1,116},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002975 {0x386,0x386,-1,38},
2976 {0x388,0x38a,1,37},
2977 {0x38c,0x38c,-1,64},
2978 {0x38e,0x38f,1,63},
2979 {0x391,0x3a1,1,32},
2980 {0x3a3,0x3ab,1,32},
2981 {0x3c2,0x3c2,-1,1},
2982 {0x3cf,0x3cf,-1,8},
2983 {0x3d0,0x3d0,-1,-30},
2984 {0x3d1,0x3d1,-1,-25},
2985 {0x3d5,0x3d5,-1,-15},
2986 {0x3d6,0x3d6,-1,-22},
2987 {0x3d8,0x3ee,2,1},
2988 {0x3f0,0x3f0,-1,-54},
2989 {0x3f1,0x3f1,-1,-48},
2990 {0x3f4,0x3f4,-1,-60},
2991 {0x3f5,0x3f5,-1,-64},
2992 {0x3f7,0x3f7,-1,1},
2993 {0x3f9,0x3f9,-1,-7},
2994 {0x3fa,0x3fa,-1,1},
2995 {0x3fd,0x3ff,1,-130},
2996 {0x400,0x40f,1,80},
2997 {0x410,0x42f,1,32},
2998 {0x460,0x480,2,1},
2999 {0x48a,0x4be,2,1},
3000 {0x4c0,0x4c0,-1,15},
3001 {0x4c1,0x4cd,2,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003002 {0x4d0,0x52e,2,1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003003 {0x531,0x556,1,48},
3004 {0x10a0,0x10c5,1,7264},
Bram Moolenaarea676722015-01-14 17:40:09 +01003005 {0x10c7,0x10cd,6,7264},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003006 {0x13f8,0x13fd,1,-8},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003007 {0x1c80,0x1c80,-1,-6222},
3008 {0x1c81,0x1c81,-1,-6221},
3009 {0x1c82,0x1c82,-1,-6212},
3010 {0x1c83,0x1c84,1,-6210},
3011 {0x1c85,0x1c85,-1,-6211},
3012 {0x1c86,0x1c86,-1,-6204},
3013 {0x1c87,0x1c87,-1,-6180},
3014 {0x1c88,0x1c88,-1,35267},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003015 {0x1e00,0x1e94,2,1},
3016 {0x1e9b,0x1e9b,-1,-58},
3017 {0x1e9e,0x1e9e,-1,-7615},
3018 {0x1ea0,0x1efe,2,1},
3019 {0x1f08,0x1f0f,1,-8},
3020 {0x1f18,0x1f1d,1,-8},
3021 {0x1f28,0x1f2f,1,-8},
3022 {0x1f38,0x1f3f,1,-8},
3023 {0x1f48,0x1f4d,1,-8},
3024 {0x1f59,0x1f5f,2,-8},
3025 {0x1f68,0x1f6f,1,-8},
3026 {0x1f88,0x1f8f,1,-8},
3027 {0x1f98,0x1f9f,1,-8},
3028 {0x1fa8,0x1faf,1,-8},
3029 {0x1fb8,0x1fb9,1,-8},
3030 {0x1fba,0x1fbb,1,-74},
3031 {0x1fbc,0x1fbc,-1,-9},
3032 {0x1fbe,0x1fbe,-1,-7173},
3033 {0x1fc8,0x1fcb,1,-86},
3034 {0x1fcc,0x1fcc,-1,-9},
3035 {0x1fd8,0x1fd9,1,-8},
3036 {0x1fda,0x1fdb,1,-100},
3037 {0x1fe8,0x1fe9,1,-8},
3038 {0x1fea,0x1feb,1,-112},
3039 {0x1fec,0x1fec,-1,-7},
3040 {0x1ff8,0x1ff9,1,-128},
3041 {0x1ffa,0x1ffb,1,-126},
3042 {0x1ffc,0x1ffc,-1,-9},
3043 {0x2126,0x2126,-1,-7517},
3044 {0x212a,0x212a,-1,-8383},
3045 {0x212b,0x212b,-1,-8262},
3046 {0x2132,0x2132,-1,28},
3047 {0x2160,0x216f,1,16},
3048 {0x2183,0x2183,-1,1},
3049 {0x24b6,0x24cf,1,26},
3050 {0x2c00,0x2c2e,1,48},
3051 {0x2c60,0x2c60,-1,1},
3052 {0x2c62,0x2c62,-1,-10743},
3053 {0x2c63,0x2c63,-1,-3814},
3054 {0x2c64,0x2c64,-1,-10727},
3055 {0x2c67,0x2c6b,2,1},
3056 {0x2c6d,0x2c6d,-1,-10780},
3057 {0x2c6e,0x2c6e,-1,-10749},
3058 {0x2c6f,0x2c6f,-1,-10783},
3059 {0x2c70,0x2c70,-1,-10782},
3060 {0x2c72,0x2c75,3,1},
3061 {0x2c7e,0x2c7f,1,-10815},
3062 {0x2c80,0x2ce2,2,1},
3063 {0x2ceb,0x2ced,2,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003064 {0x2cf2,0xa640,31054,1},
3065 {0xa642,0xa66c,2,1},
3066 {0xa680,0xa69a,2,1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003067 {0xa722,0xa72e,2,1},
3068 {0xa732,0xa76e,2,1},
3069 {0xa779,0xa77b,2,1},
3070 {0xa77d,0xa77d,-1,-35332},
3071 {0xa77e,0xa786,2,1},
3072 {0xa78b,0xa78b,-1,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003073 {0xa78d,0xa78d,-1,-42280},
3074 {0xa790,0xa792,2,1},
3075 {0xa796,0xa7a8,2,1},
3076 {0xa7aa,0xa7aa,-1,-42308},
3077 {0xa7ab,0xa7ab,-1,-42319},
3078 {0xa7ac,0xa7ac,-1,-42315},
3079 {0xa7ad,0xa7ad,-1,-42305},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003080 {0xa7ae,0xa7ae,-1,-42308},
Bram Moolenaarea676722015-01-14 17:40:09 +01003081 {0xa7b0,0xa7b0,-1,-42258},
3082 {0xa7b1,0xa7b1,-1,-42282},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003083 {0xa7b2,0xa7b2,-1,-42261},
3084 {0xa7b3,0xa7b3,-1,928},
3085 {0xa7b4,0xa7b6,2,1},
3086 {0xab70,0xabbf,1,-38864},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003087 {0xff21,0xff3a,1,32},
Bram Moolenaarea676722015-01-14 17:40:09 +01003088 {0x10400,0x10427,1,40},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003089 {0x104b0,0x104d3,1,40},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003090 {0x10c80,0x10cb2,1,64},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003091 {0x118a0,0x118bf,1,32},
3092 {0x1e900,0x1e921,1,34}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093};
3094
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003095static int utf_convert(int a, convertStruct table[], int tableSize);
3096static int utf_strnicmp(char_u *s1, char_u *s2, size_t n1, size_t n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098/*
3099 * Generic conversion function for case operations.
3100 * Return the converted equivalent of "a", which is a UCS-4 character. Use
3101 * the given conversion "table". Uses binary search on "table".
3102 */
3103 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003104utf_convert(
3105 int a,
3106 convertStruct table[],
3107 int tableSize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 int start, mid, end; /* indices into table */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003110 int entries = tableSize / sizeof(convertStruct);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
3112 start = 0;
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003113 end = entries;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 while (start < end)
3115 {
3116 /* need to search further */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003117 mid = (end + start) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if (table[mid].rangeEnd < a)
3119 start = mid + 1;
3120 else
3121 end = mid;
3122 }
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003123 if (start < entries
3124 && table[start].rangeStart <= a
3125 && a <= table[start].rangeEnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 && (a - table[start].rangeStart) % table[start].step == 0)
3127 return (a + table[start].offset);
3128 else
3129 return a;
3130}
3131
3132/*
3133 * Return the folded-case equivalent of "a", which is a UCS-4 character. Uses
3134 * simple case folding.
3135 */
3136 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003137utf_fold(int a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
Bram Moolenaarc4a927c2016-07-10 18:24:27 +02003139 if (a < 0x80)
3140 /* be fast for ASCII */
3141 return a >= 0x41 && a <= 0x5a ? a + 32 : a;
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003142 return utf_convert(a, foldCase, (int)sizeof(foldCase));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143}
3144
Bram Moolenaard6f676d2005-06-01 21:51:55 +00003145static convertStruct toLower[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003147 {0x41,0x5a,1,32},
3148 {0xc0,0xd6,1,32},
3149 {0xd8,0xde,1,32},
3150 {0x100,0x12e,2,1},
3151 {0x130,0x130,-1,-199},
3152 {0x132,0x136,2,1},
3153 {0x139,0x147,2,1},
3154 {0x14a,0x176,2,1},
3155 {0x178,0x178,-1,-121},
3156 {0x179,0x17d,2,1},
3157 {0x181,0x181,-1,210},
3158 {0x182,0x184,2,1},
3159 {0x186,0x186,-1,206},
3160 {0x187,0x187,-1,1},
3161 {0x189,0x18a,1,205},
3162 {0x18b,0x18b,-1,1},
3163 {0x18e,0x18e,-1,79},
3164 {0x18f,0x18f,-1,202},
3165 {0x190,0x190,-1,203},
3166 {0x191,0x191,-1,1},
3167 {0x193,0x193,-1,205},
3168 {0x194,0x194,-1,207},
3169 {0x196,0x196,-1,211},
3170 {0x197,0x197,-1,209},
3171 {0x198,0x198,-1,1},
3172 {0x19c,0x19c,-1,211},
3173 {0x19d,0x19d,-1,213},
3174 {0x19f,0x19f,-1,214},
3175 {0x1a0,0x1a4,2,1},
3176 {0x1a6,0x1a6,-1,218},
3177 {0x1a7,0x1a7,-1,1},
3178 {0x1a9,0x1a9,-1,218},
3179 {0x1ac,0x1ac,-1,1},
3180 {0x1ae,0x1ae,-1,218},
3181 {0x1af,0x1af,-1,1},
3182 {0x1b1,0x1b2,1,217},
3183 {0x1b3,0x1b5,2,1},
3184 {0x1b7,0x1b7,-1,219},
3185 {0x1b8,0x1bc,4,1},
3186 {0x1c4,0x1c4,-1,2},
3187 {0x1c5,0x1c5,-1,1},
3188 {0x1c7,0x1c7,-1,2},
3189 {0x1c8,0x1c8,-1,1},
3190 {0x1ca,0x1ca,-1,2},
3191 {0x1cb,0x1db,2,1},
3192 {0x1de,0x1ee,2,1},
3193 {0x1f1,0x1f1,-1,2},
3194 {0x1f2,0x1f4,2,1},
3195 {0x1f6,0x1f6,-1,-97},
3196 {0x1f7,0x1f7,-1,-56},
3197 {0x1f8,0x21e,2,1},
3198 {0x220,0x220,-1,-130},
3199 {0x222,0x232,2,1},
3200 {0x23a,0x23a,-1,10795},
3201 {0x23b,0x23b,-1,1},
3202 {0x23d,0x23d,-1,-163},
3203 {0x23e,0x23e,-1,10792},
3204 {0x241,0x241,-1,1},
3205 {0x243,0x243,-1,-195},
3206 {0x244,0x244,-1,69},
3207 {0x245,0x245,-1,71},
3208 {0x246,0x24e,2,1},
3209 {0x370,0x372,2,1},
3210 {0x376,0x376,-1,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003211 {0x37f,0x37f,-1,116},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003212 {0x386,0x386,-1,38},
3213 {0x388,0x38a,1,37},
3214 {0x38c,0x38c,-1,64},
3215 {0x38e,0x38f,1,63},
3216 {0x391,0x3a1,1,32},
3217 {0x3a3,0x3ab,1,32},
3218 {0x3cf,0x3cf,-1,8},
3219 {0x3d8,0x3ee,2,1},
3220 {0x3f4,0x3f4,-1,-60},
3221 {0x3f7,0x3f7,-1,1},
3222 {0x3f9,0x3f9,-1,-7},
3223 {0x3fa,0x3fa,-1,1},
3224 {0x3fd,0x3ff,1,-130},
3225 {0x400,0x40f,1,80},
3226 {0x410,0x42f,1,32},
3227 {0x460,0x480,2,1},
3228 {0x48a,0x4be,2,1},
3229 {0x4c0,0x4c0,-1,15},
3230 {0x4c1,0x4cd,2,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003231 {0x4d0,0x52e,2,1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003232 {0x531,0x556,1,48},
3233 {0x10a0,0x10c5,1,7264},
Bram Moolenaarea676722015-01-14 17:40:09 +01003234 {0x10c7,0x10cd,6,7264},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003235 {0x13a0,0x13ef,1,38864},
3236 {0x13f0,0x13f5,1,8},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003237 {0x1e00,0x1e94,2,1},
3238 {0x1e9e,0x1e9e,-1,-7615},
3239 {0x1ea0,0x1efe,2,1},
3240 {0x1f08,0x1f0f,1,-8},
3241 {0x1f18,0x1f1d,1,-8},
3242 {0x1f28,0x1f2f,1,-8},
3243 {0x1f38,0x1f3f,1,-8},
3244 {0x1f48,0x1f4d,1,-8},
3245 {0x1f59,0x1f5f,2,-8},
3246 {0x1f68,0x1f6f,1,-8},
3247 {0x1f88,0x1f8f,1,-8},
3248 {0x1f98,0x1f9f,1,-8},
3249 {0x1fa8,0x1faf,1,-8},
3250 {0x1fb8,0x1fb9,1,-8},
3251 {0x1fba,0x1fbb,1,-74},
3252 {0x1fbc,0x1fbc,-1,-9},
3253 {0x1fc8,0x1fcb,1,-86},
3254 {0x1fcc,0x1fcc,-1,-9},
3255 {0x1fd8,0x1fd9,1,-8},
3256 {0x1fda,0x1fdb,1,-100},
3257 {0x1fe8,0x1fe9,1,-8},
3258 {0x1fea,0x1feb,1,-112},
3259 {0x1fec,0x1fec,-1,-7},
3260 {0x1ff8,0x1ff9,1,-128},
3261 {0x1ffa,0x1ffb,1,-126},
3262 {0x1ffc,0x1ffc,-1,-9},
3263 {0x2126,0x2126,-1,-7517},
3264 {0x212a,0x212a,-1,-8383},
3265 {0x212b,0x212b,-1,-8262},
3266 {0x2132,0x2132,-1,28},
3267 {0x2160,0x216f,1,16},
3268 {0x2183,0x2183,-1,1},
3269 {0x24b6,0x24cf,1,26},
3270 {0x2c00,0x2c2e,1,48},
3271 {0x2c60,0x2c60,-1,1},
3272 {0x2c62,0x2c62,-1,-10743},
3273 {0x2c63,0x2c63,-1,-3814},
3274 {0x2c64,0x2c64,-1,-10727},
3275 {0x2c67,0x2c6b,2,1},
3276 {0x2c6d,0x2c6d,-1,-10780},
3277 {0x2c6e,0x2c6e,-1,-10749},
3278 {0x2c6f,0x2c6f,-1,-10783},
3279 {0x2c70,0x2c70,-1,-10782},
3280 {0x2c72,0x2c75,3,1},
3281 {0x2c7e,0x2c7f,1,-10815},
3282 {0x2c80,0x2ce2,2,1},
3283 {0x2ceb,0x2ced,2,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003284 {0x2cf2,0xa640,31054,1},
3285 {0xa642,0xa66c,2,1},
3286 {0xa680,0xa69a,2,1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003287 {0xa722,0xa72e,2,1},
3288 {0xa732,0xa76e,2,1},
3289 {0xa779,0xa77b,2,1},
3290 {0xa77d,0xa77d,-1,-35332},
3291 {0xa77e,0xa786,2,1},
3292 {0xa78b,0xa78b,-1,1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003293 {0xa78d,0xa78d,-1,-42280},
3294 {0xa790,0xa792,2,1},
3295 {0xa796,0xa7a8,2,1},
3296 {0xa7aa,0xa7aa,-1,-42308},
3297 {0xa7ab,0xa7ab,-1,-42319},
3298 {0xa7ac,0xa7ac,-1,-42315},
3299 {0xa7ad,0xa7ad,-1,-42305},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003300 {0xa7ae,0xa7ae,-1,-42308},
Bram Moolenaarea676722015-01-14 17:40:09 +01003301 {0xa7b0,0xa7b0,-1,-42258},
3302 {0xa7b1,0xa7b1,-1,-42282},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003303 {0xa7b2,0xa7b2,-1,-42261},
3304 {0xa7b3,0xa7b3,-1,928},
3305 {0xa7b4,0xa7b6,2,1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003306 {0xff21,0xff3a,1,32},
Bram Moolenaarea676722015-01-14 17:40:09 +01003307 {0x10400,0x10427,1,40},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003308 {0x104b0,0x104d3,1,40},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003309 {0x10c80,0x10cb2,1,64},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003310 {0x118a0,0x118bf,1,32},
3311 {0x1e900,0x1e921,1,34}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312};
3313
Bram Moolenaard6f676d2005-06-01 21:51:55 +00003314static convertStruct toUpper[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003316 {0x61,0x7a,1,-32},
3317 {0xb5,0xb5,-1,743},
Bram Moolenaarea676722015-01-14 17:40:09 +01003318 {0xe0,0xf6,1,-32},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003319 {0xf8,0xfe,1,-32},
3320 {0xff,0xff,-1,121},
3321 {0x101,0x12f,2,-1},
3322 {0x131,0x131,-1,-232},
3323 {0x133,0x137,2,-1},
3324 {0x13a,0x148,2,-1},
3325 {0x14b,0x177,2,-1},
3326 {0x17a,0x17e,2,-1},
3327 {0x17f,0x17f,-1,-300},
3328 {0x180,0x180,-1,195},
3329 {0x183,0x185,2,-1},
3330 {0x188,0x18c,4,-1},
3331 {0x192,0x192,-1,-1},
3332 {0x195,0x195,-1,97},
3333 {0x199,0x199,-1,-1},
3334 {0x19a,0x19a,-1,163},
3335 {0x19e,0x19e,-1,130},
3336 {0x1a1,0x1a5,2,-1},
3337 {0x1a8,0x1ad,5,-1},
3338 {0x1b0,0x1b4,4,-1},
3339 {0x1b6,0x1b9,3,-1},
3340 {0x1bd,0x1bd,-1,-1},
3341 {0x1bf,0x1bf,-1,56},
3342 {0x1c5,0x1c5,-1,-1},
3343 {0x1c6,0x1c6,-1,-2},
3344 {0x1c8,0x1c8,-1,-1},
3345 {0x1c9,0x1c9,-1,-2},
3346 {0x1cb,0x1cb,-1,-1},
3347 {0x1cc,0x1cc,-1,-2},
3348 {0x1ce,0x1dc,2,-1},
3349 {0x1dd,0x1dd,-1,-79},
3350 {0x1df,0x1ef,2,-1},
3351 {0x1f2,0x1f2,-1,-1},
3352 {0x1f3,0x1f3,-1,-2},
3353 {0x1f5,0x1f9,4,-1},
3354 {0x1fb,0x21f,2,-1},
3355 {0x223,0x233,2,-1},
3356 {0x23c,0x23c,-1,-1},
3357 {0x23f,0x240,1,10815},
3358 {0x242,0x247,5,-1},
3359 {0x249,0x24f,2,-1},
3360 {0x250,0x250,-1,10783},
3361 {0x251,0x251,-1,10780},
3362 {0x252,0x252,-1,10782},
3363 {0x253,0x253,-1,-210},
3364 {0x254,0x254,-1,-206},
3365 {0x256,0x257,1,-205},
3366 {0x259,0x259,-1,-202},
3367 {0x25b,0x25b,-1,-203},
Bram Moolenaarea676722015-01-14 17:40:09 +01003368 {0x25c,0x25c,-1,42319},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003369 {0x260,0x260,-1,-205},
Bram Moolenaarea676722015-01-14 17:40:09 +01003370 {0x261,0x261,-1,42315},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003371 {0x263,0x263,-1,-207},
Bram Moolenaarea676722015-01-14 17:40:09 +01003372 {0x265,0x265,-1,42280},
3373 {0x266,0x266,-1,42308},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003374 {0x268,0x268,-1,-209},
3375 {0x269,0x269,-1,-211},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003376 {0x26a,0x26a,-1,42308},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003377 {0x26b,0x26b,-1,10743},
Bram Moolenaarea676722015-01-14 17:40:09 +01003378 {0x26c,0x26c,-1,42305},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003379 {0x26f,0x26f,-1,-211},
3380 {0x271,0x271,-1,10749},
3381 {0x272,0x272,-1,-213},
3382 {0x275,0x275,-1,-214},
3383 {0x27d,0x27d,-1,10727},
3384 {0x280,0x283,3,-218},
Bram Moolenaarea676722015-01-14 17:40:09 +01003385 {0x287,0x287,-1,42282},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003386 {0x288,0x288,-1,-218},
3387 {0x289,0x289,-1,-69},
3388 {0x28a,0x28b,1,-217},
3389 {0x28c,0x28c,-1,-71},
3390 {0x292,0x292,-1,-219},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003391 {0x29d,0x29d,-1,42261},
Bram Moolenaarea676722015-01-14 17:40:09 +01003392 {0x29e,0x29e,-1,42258},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003393 {0x345,0x345,-1,84},
3394 {0x371,0x373,2,-1},
3395 {0x377,0x377,-1,-1},
3396 {0x37b,0x37d,1,130},
3397 {0x3ac,0x3ac,-1,-38},
3398 {0x3ad,0x3af,1,-37},
3399 {0x3b1,0x3c1,1,-32},
3400 {0x3c2,0x3c2,-1,-31},
3401 {0x3c3,0x3cb,1,-32},
3402 {0x3cc,0x3cc,-1,-64},
3403 {0x3cd,0x3ce,1,-63},
3404 {0x3d0,0x3d0,-1,-62},
3405 {0x3d1,0x3d1,-1,-57},
3406 {0x3d5,0x3d5,-1,-47},
3407 {0x3d6,0x3d6,-1,-54},
3408 {0x3d7,0x3d7,-1,-8},
3409 {0x3d9,0x3ef,2,-1},
3410 {0x3f0,0x3f0,-1,-86},
3411 {0x3f1,0x3f1,-1,-80},
3412 {0x3f2,0x3f2,-1,7},
Bram Moolenaarea676722015-01-14 17:40:09 +01003413 {0x3f3,0x3f3,-1,-116},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003414 {0x3f5,0x3f5,-1,-96},
3415 {0x3f8,0x3fb,3,-1},
3416 {0x430,0x44f,1,-32},
3417 {0x450,0x45f,1,-80},
3418 {0x461,0x481,2,-1},
3419 {0x48b,0x4bf,2,-1},
3420 {0x4c2,0x4ce,2,-1},
3421 {0x4cf,0x4cf,-1,-15},
Bram Moolenaarea676722015-01-14 17:40:09 +01003422 {0x4d1,0x52f,2,-1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003423 {0x561,0x586,1,-48},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003424 {0x13f8,0x13fd,1,-8},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003425 {0x1c80,0x1c80,-1,-6254},
3426 {0x1c81,0x1c81,-1,-6253},
3427 {0x1c82,0x1c82,-1,-6244},
3428 {0x1c83,0x1c84,1,-6242},
3429 {0x1c85,0x1c85,-1,-6243},
3430 {0x1c86,0x1c86,-1,-6236},
3431 {0x1c87,0x1c87,-1,-6181},
3432 {0x1c88,0x1c88,-1,35266},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003433 {0x1d79,0x1d79,-1,35332},
3434 {0x1d7d,0x1d7d,-1,3814},
3435 {0x1e01,0x1e95,2,-1},
3436 {0x1e9b,0x1e9b,-1,-59},
3437 {0x1ea1,0x1eff,2,-1},
3438 {0x1f00,0x1f07,1,8},
3439 {0x1f10,0x1f15,1,8},
3440 {0x1f20,0x1f27,1,8},
3441 {0x1f30,0x1f37,1,8},
3442 {0x1f40,0x1f45,1,8},
3443 {0x1f51,0x1f57,2,8},
3444 {0x1f60,0x1f67,1,8},
3445 {0x1f70,0x1f71,1,74},
3446 {0x1f72,0x1f75,1,86},
3447 {0x1f76,0x1f77,1,100},
3448 {0x1f78,0x1f79,1,128},
3449 {0x1f7a,0x1f7b,1,112},
3450 {0x1f7c,0x1f7d,1,126},
3451 {0x1f80,0x1f87,1,8},
3452 {0x1f90,0x1f97,1,8},
3453 {0x1fa0,0x1fa7,1,8},
3454 {0x1fb0,0x1fb1,1,8},
3455 {0x1fb3,0x1fb3,-1,9},
3456 {0x1fbe,0x1fbe,-1,-7205},
3457 {0x1fc3,0x1fc3,-1,9},
3458 {0x1fd0,0x1fd1,1,8},
3459 {0x1fe0,0x1fe1,1,8},
3460 {0x1fe5,0x1fe5,-1,7},
3461 {0x1ff3,0x1ff3,-1,9},
3462 {0x214e,0x214e,-1,-28},
3463 {0x2170,0x217f,1,-16},
3464 {0x2184,0x2184,-1,-1},
3465 {0x24d0,0x24e9,1,-26},
3466 {0x2c30,0x2c5e,1,-48},
3467 {0x2c61,0x2c61,-1,-1},
3468 {0x2c65,0x2c65,-1,-10795},
3469 {0x2c66,0x2c66,-1,-10792},
3470 {0x2c68,0x2c6c,2,-1},
3471 {0x2c73,0x2c76,3,-1},
3472 {0x2c81,0x2ce3,2,-1},
3473 {0x2cec,0x2cee,2,-1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003474 {0x2cf3,0x2cf3,-1,-1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003475 {0x2d00,0x2d25,1,-7264},
Bram Moolenaarea676722015-01-14 17:40:09 +01003476 {0x2d27,0x2d2d,6,-7264},
3477 {0xa641,0xa66d,2,-1},
3478 {0xa681,0xa69b,2,-1},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003479 {0xa723,0xa72f,2,-1},
3480 {0xa733,0xa76f,2,-1},
3481 {0xa77a,0xa77c,2,-1},
3482 {0xa77f,0xa787,2,-1},
Bram Moolenaarea676722015-01-14 17:40:09 +01003483 {0xa78c,0xa791,5,-1},
3484 {0xa793,0xa797,4,-1},
3485 {0xa799,0xa7a9,2,-1},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003486 {0xa7b5,0xa7b7,2,-1},
3487 {0xab53,0xab53,-1,-928},
3488 {0xab70,0xabbf,1,-38864},
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01003489 {0xff41,0xff5a,1,-32},
Bram Moolenaarea676722015-01-14 17:40:09 +01003490 {0x10428,0x1044f,1,-40},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003491 {0x104d8,0x104fb,1,-40},
Bram Moolenaar66312ac2015-06-21 14:22:00 +02003492 {0x10cc0,0x10cf2,1,-64},
Bram Moolenaar04e2b4b2016-06-26 17:53:07 +02003493 {0x118c0,0x118df,1,-32},
3494 {0x1e922,0x1e943,1,-34}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495};
Bram Moolenaard63aff02016-03-21 22:15:30 +01003496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497/*
3498 * Return the upper-case equivalent of "a", which is a UCS-4 character. Use
3499 * simple case folding.
3500 */
3501 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003502utf_toupper(int a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 /* If 'casemap' contains "keepascii" use ASCII style toupper(). */
3505 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3506 return TOUPPER_ASC(a);
3507
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003508#if defined(HAVE_TOWUPPER) && defined(__STDC_ISO_10646__)
3509 /* If towupper() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (!(cmp_flags & CMP_INTERNAL))
3511 return towupper(a);
3512#endif
3513
3514 /* For characters below 128 use locale sensitive toupper(). */
3515 if (a < 128)
3516 return TOUPPER_LOC(a);
3517
3518 /* For any other characters use the above mapping table. */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003519 return utf_convert(a, toUpper, (int)sizeof(toUpper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520}
3521
3522 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003523utf_islower(int a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
Bram Moolenaar88178de2012-06-01 17:46:59 +02003525 /* German sharp s is lower case but has no upper case equivalent. */
3526 return (utf_toupper(a) != a) || a == 0xdf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527}
3528
3529/*
3530 * Return the lower-case equivalent of "a", which is a UCS-4 character. Use
3531 * simple case folding.
3532 */
3533 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003534utf_tolower(int a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
3536 /* If 'casemap' contains "keepascii" use ASCII style tolower(). */
3537 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3538 return TOLOWER_ASC(a);
3539
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003540#if defined(HAVE_TOWLOWER) && defined(__STDC_ISO_10646__)
Bram Moolenaar8fcc0f72005-04-23 20:45:11 +00003541 /* If towlower() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (!(cmp_flags & CMP_INTERNAL))
3543 return towlower(a);
3544#endif
3545
3546 /* For characters below 128 use locale sensitive tolower(). */
3547 if (a < 128)
3548 return TOLOWER_LOC(a);
3549
3550 /* For any other characters use the above mapping table. */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003551 return utf_convert(a, toLower, (int)sizeof(toLower));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552}
3553
3554 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003555utf_isupper(int a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
3557 return (utf_tolower(a) != a);
3558}
3559
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003560 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003561utf_strnicmp(
3562 char_u *s1,
3563 char_u *s2,
3564 size_t n1,
3565 size_t n2)
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003566{
3567 int c1, c2, cdiff;
3568 char_u buffer[6];
3569
3570 for (;;)
3571 {
3572 c1 = utf_safe_read_char_adv(&s1, &n1);
3573 c2 = utf_safe_read_char_adv(&s2, &n2);
3574
3575 if (c1 <= 0 || c2 <= 0)
3576 break;
3577
3578 if (c1 == c2)
3579 continue;
3580
3581 cdiff = utf_fold(c1) - utf_fold(c2);
3582 if (cdiff != 0)
3583 return cdiff;
3584 }
3585
3586 /* some string ended or has an incomplete/illegal character sequence */
3587
3588 if (c1 == 0 || c2 == 0)
3589 {
3590 /* some string ended. shorter string is smaller */
3591 if (c1 == 0 && c2 == 0)
3592 return 0;
3593 return c1 == 0 ? -1 : 1;
3594 }
3595
3596 /* Continue with bytewise comparison to produce some result that
3597 * would make comparison operations involving this function transitive.
3598 *
3599 * If only one string had an error, comparison should be made with
3600 * folded version of the other string. In this case it is enough
3601 * to fold just one character to determine the result of comparison. */
3602
3603 if (c1 != -1 && c2 == -1)
3604 {
3605 n1 = utf_char2bytes(utf_fold(c1), buffer);
3606 s1 = buffer;
3607 }
3608 else if (c2 != -1 && c1 == -1)
3609 {
3610 n2 = utf_char2bytes(utf_fold(c2), buffer);
3611 s2 = buffer;
3612 }
3613
3614 while (n1 > 0 && n2 > 0 && *s1 != NUL && *s2 != NUL)
3615 {
3616 cdiff = (int)(*s1) - (int)(*s2);
3617 if (cdiff != 0)
3618 return cdiff;
3619
3620 s1++;
3621 s2++;
3622 n1--;
3623 n2--;
3624 }
3625
3626 if (n1 > 0 && *s1 == NUL)
3627 n1 = 0;
3628 if (n2 > 0 && *s2 == NUL)
3629 n2 = 0;
3630
3631 if (n1 == 0 && n2 == 0)
3632 return 0;
3633 return n1 == 0 ? -1 : 1;
3634}
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636/*
3637 * Version of strnicmp() that handles multi-byte characters.
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01003638 * Needed for Big5, Shift-JIS and UTF-8 encoding. Other DBCS encodings can
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 * probably use strnicmp(), because there are no ASCII characters in the
3640 * second byte.
3641 * Returns zero if s1 and s2 are equal (ignoring case), the difference between
3642 * two characters otherwise.
3643 */
3644 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003645mb_strnicmp(char_u *s1, char_u *s2, size_t nn)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003647 int i, l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 int cdiff;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003649 int n = (int)nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003651 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003653 return utf_strnicmp(s1, s2, nn, nn);
3654 }
3655 else
3656 {
3657 for (i = 0; i < n; i += l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003659 if (s1[i] == NUL && s2[i] == NUL) /* both strings end */
3660 return 0;
3661
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003662 l = (*mb_ptr2len)(s1 + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (l <= 1)
3664 {
3665 /* Single byte: first check normally, then with ignore case. */
3666 if (s1[i] != s2[i])
3667 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003668 cdiff = MB_TOLOWER(s1[i]) - MB_TOLOWER(s2[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (cdiff != 0)
3670 return cdiff;
3671 }
3672 }
3673 else
3674 {
3675 /* For non-Unicode multi-byte don't ignore case. */
3676 if (l > n - i)
3677 l = n - i;
3678 cdiff = STRNCMP(s1 + i, s2 + i, l);
3679 if (cdiff != 0)
3680 return cdiff;
3681 }
3682 }
3683 }
3684 return 0;
3685}
3686
3687/*
3688 * "g8": show bytes of the UTF-8 char under the cursor. Doesn't matter what
3689 * 'encoding' has been set to.
3690 */
3691 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003692show_utf8(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 int len;
Bram Moolenaar051b7822005-05-19 21:00:46 +00003695 int rlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 char_u *line;
3697 int clen;
3698 int i;
3699
3700 /* Get the byte length of the char under the cursor, including composing
3701 * characters. */
3702 line = ml_get_cursor();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003703 len = utfc_ptr2len(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (len == 0)
3705 {
3706 MSG("NUL");
3707 return;
3708 }
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 clen = 0;
3711 for (i = 0; i < len; ++i)
3712 {
3713 if (clen == 0)
3714 {
3715 /* start of (composing) character, get its length */
3716 if (i > 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +00003717 {
3718 STRCPY(IObuff + rlen, "+ ");
3719 rlen += 2;
3720 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003721 clen = utf_ptr2len(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
Bram Moolenaarb382ad12010-05-21 15:46:35 +02003723 sprintf((char *)IObuff + rlen, "%02x ",
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003724 (line[i] == NL) ? NUL : line[i]); /* NUL is stored as NL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 --clen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003726 rlen += (int)STRLEN(IObuff + rlen);
Bram Moolenaar051b7822005-05-19 21:00:46 +00003727 if (rlen > IOSIZE - 20)
3728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730
3731 msg(IObuff);
3732}
3733
3734/*
3735 * mb_head_off() function pointer.
3736 * Return offset from "p" to the first byte of the character it points into.
Bram Moolenaar24397332009-12-02 14:02:39 +00003737 * If "p" points to the NUL at the end of the string return 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * Returns 0 when already at the first byte of a character.
3739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003741latin_head_off(char_u *base UNUSED, char_u *p UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 return 0;
3744}
3745
3746 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003747dbcs_head_off(char_u *base, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 char_u *q;
3750
3751 /* It can't be a trailing byte when not using DBCS, at the start of the
3752 * string or the previous byte can't start a double-byte. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003753 if (p <= base || MB_BYTE2LEN(p[-1]) == 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 return 0;
3755
3756 /* This is slow: need to start at the base and go forward until the
3757 * byte we are looking for. Return 1 when we went past it, 0 otherwise. */
3758 q = base;
3759 while (q < p)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003760 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 return (q == p) ? 0 : 1;
3762}
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764/*
3765 * Special version of dbcs_head_off() that works for ScreenLines[], where
3766 * single-width DBCS_JPNU characters are stored separately.
3767 */
3768 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003769dbcs_screen_head_off(char_u *base, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 char_u *q;
3772
3773 /* It can't be a trailing byte when not using DBCS, at the start of the
3774 * string or the previous byte can't start a double-byte.
3775 * For euc-jp an 0x8e byte in the previous cell always means we have a
3776 * lead byte in the current cell. */
3777 if (p <= base
3778 || (enc_dbcs == DBCS_JPNU && p[-1] == 0x8e)
Bram Moolenaar24397332009-12-02 14:02:39 +00003779 || MB_BYTE2LEN(p[-1]) == 1
3780 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 return 0;
3782
3783 /* This is slow: need to start at the base and go forward until the
3784 * byte we are looking for. Return 1 when we went past it, 0 otherwise.
3785 * For DBCS_JPNU look out for 0x8e, which means the second byte is not
3786 * stored as the next byte. */
3787 q = base;
3788 while (q < p)
3789 {
3790 if (enc_dbcs == DBCS_JPNU && *q == 0x8e)
3791 ++q;
3792 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003793 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 return (q == p) ? 0 : 1;
3796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003799utf_head_off(char_u *base, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 char_u *q;
3802 char_u *s;
3803 int c;
Bram Moolenaar24397332009-12-02 14:02:39 +00003804 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805#ifdef FEAT_ARABIC
3806 char_u *j;
3807#endif
3808
3809 if (*p < 0x80) /* be quick for ASCII */
3810 return 0;
3811
3812 /* Skip backwards over trailing bytes: 10xx.xxxx
3813 * Skip backwards again if on a composing char. */
3814 for (q = p; ; --q)
3815 {
3816 /* Move s to the last byte of this char. */
3817 for (s = q; (s[1] & 0xc0) == 0x80; ++s)
3818 ;
3819 /* Move q to the first byte of this char. */
3820 while (q > base && (*q & 0xc0) == 0x80)
3821 --q;
3822 /* Check for illegal sequence. Do allow an illegal byte after where we
3823 * started. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003824 len = utf8len_tab[*q];
3825 if (len != (int)(s - q + 1) && len != (int)(p - q + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return 0;
3827
3828 if (q <= base)
3829 break;
3830
3831 c = utf_ptr2char(q);
3832 if (utf_iscomposing(c))
3833 continue;
3834
3835#ifdef FEAT_ARABIC
3836 if (arabic_maycombine(c))
3837 {
3838 /* Advance to get a sneak-peak at the next char */
3839 j = q;
3840 --j;
3841 /* Move j to the first byte of this char. */
3842 while (j > base && (*j & 0xc0) == 0x80)
3843 --j;
3844 if (arabic_combine(utf_ptr2char(j), c))
3845 continue;
3846 }
3847#endif
3848 break;
3849 }
3850
3851 return (int)(p - q);
3852}
3853
3854/*
Bram Moolenaard8b02732005-01-14 21:48:43 +00003855 * Copy a character from "*fp" to "*tp" and advance the pointers.
3856 */
3857 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003858mb_copy_char(char_u **fp, char_u **tp)
Bram Moolenaard8b02732005-01-14 21:48:43 +00003859{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003860 int l = (*mb_ptr2len)(*fp);
Bram Moolenaard8b02732005-01-14 21:48:43 +00003861
3862 mch_memmove(*tp, *fp, (size_t)l);
3863 *tp += l;
3864 *fp += l;
3865}
3866
3867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 * Return the offset from "p" to the first byte of a character. When "p" is
3869 * at the start of a character 0 is returned, otherwise the offset to the next
3870 * character. Can start anywhere in a stream of bytes.
3871 */
3872 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003873mb_off_next(char_u *base, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
3875 int i;
3876 int j;
3877
3878 if (enc_utf8)
3879 {
3880 if (*p < 0x80) /* be quick for ASCII */
3881 return 0;
3882
3883 /* Find the next character that isn't 10xx.xxxx */
3884 for (i = 0; (p[i] & 0xc0) == 0x80; ++i)
3885 ;
3886 if (i > 0)
3887 {
3888 /* Check for illegal sequence. */
3889 for (j = 0; p - j > base; ++j)
3890 if ((p[-j] & 0xc0) != 0x80)
3891 break;
3892 if (utf8len_tab[p[-j]] != i + j)
3893 return 0;
3894 }
3895 return i;
3896 }
3897
3898 /* Only need to check if we're on a trail byte, it doesn't matter if we
3899 * want the offset to the next or current character. */
3900 return (*mb_head_off)(base, p);
3901}
3902
3903/*
3904 * Return the offset from "p" to the last byte of the character it points
3905 * into. Can start anywhere in a stream of bytes.
3906 */
3907 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003908mb_tail_off(char_u *base, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 int i;
3911 int j;
3912
3913 if (*p == NUL)
3914 return 0;
3915
3916 if (enc_utf8)
3917 {
3918 /* Find the last character that is 10xx.xxxx */
3919 for (i = 0; (p[i + 1] & 0xc0) == 0x80; ++i)
3920 ;
3921 /* Check for illegal sequence. */
3922 for (j = 0; p - j > base; ++j)
3923 if ((p[-j] & 0xc0) != 0x80)
3924 break;
3925 if (utf8len_tab[p[-j]] != i + j + 1)
3926 return 0;
3927 return i;
3928 }
3929
3930 /* It can't be the first byte if a double-byte when not using DBCS, at the
3931 * end of the string or the byte can't start a double-byte. */
3932 if (enc_dbcs == 0 || p[1] == NUL || MB_BYTE2LEN(*p) == 1)
3933 return 0;
3934
3935 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3936 return 1 - dbcs_head_off(base, p);
3937}
3938
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003939/*
3940 * Find the next illegal byte sequence.
3941 */
3942 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003943utf_find_illegal(void)
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003944{
3945 pos_T pos = curwin->w_cursor;
3946 char_u *p;
3947 int len;
3948 vimconv_T vimconv;
3949 char_u *tofree = NULL;
3950
3951 vimconv.vc_type = CONV_NONE;
3952 if (enc_utf8 && (enc_canon_props(curbuf->b_p_fenc) & ENC_8BIT))
3953 {
3954 /* 'encoding' is "utf-8" but we are editing a 8-bit encoded file,
3955 * possibly a utf-8 file with illegal bytes. Setup for conversion
3956 * from utf-8 to 'fileencoding'. */
3957 convert_setup(&vimconv, p_enc, curbuf->b_p_fenc);
3958 }
3959
3960#ifdef FEAT_VIRTUALEDIT
3961 curwin->w_cursor.coladd = 0;
3962#endif
3963 for (;;)
3964 {
3965 p = ml_get_cursor();
3966 if (vimconv.vc_type != CONV_NONE)
3967 {
3968 vim_free(tofree);
3969 tofree = string_convert(&vimconv, p, NULL);
3970 if (tofree == NULL)
3971 break;
3972 p = tofree;
3973 }
3974
3975 while (*p != NUL)
3976 {
3977 /* Illegal means that there are not enough trail bytes (checked by
3978 * utf_ptr2len()) or too many of them (overlong sequence). */
3979 len = utf_ptr2len(p);
3980 if (*p >= 0x80 && (len == 1
3981 || utf_char2len(utf_ptr2char(p)) != len))
3982 {
3983 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003984 curwin->w_cursor.col += (colnr_T)(p - ml_get_cursor());
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003985 else
3986 {
3987 int l;
3988
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003989 len = (int)(p - tofree);
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003990 for (p = ml_get_cursor(); *p != NUL && len-- > 0; p += l)
3991 {
3992 l = utf_ptr2len(p);
3993 curwin->w_cursor.col += l;
3994 }
3995 }
3996 goto theend;
3997 }
3998 p += len;
3999 }
4000 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4001 break;
4002 ++curwin->w_cursor.lnum;
4003 curwin->w_cursor.col = 0;
4004 }
4005
4006 /* didn't find it: don't move and beep */
4007 curwin->w_cursor = pos;
4008 beep_flush();
4009
4010theend:
4011 vim_free(tofree);
4012 convert_setup(&vimconv, NULL, NULL);
4013}
4014
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004015#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004016/*
4017 * Return TRUE if string "s" is a valid utf-8 string.
4018 * When "end" is NULL stop at the first NUL.
4019 * When "end" is positive stop there.
4020 */
4021 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004022utf_valid_string(char_u *s, char_u *end)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004023{
4024 int l;
4025 char_u *p = s;
4026
4027 while (end == NULL ? *p != NUL : p < end)
4028 {
Bram Moolenaar24397332009-12-02 14:02:39 +00004029 l = utf8len_tab_zero[*p];
4030 if (l == 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004031 return FALSE; /* invalid lead byte */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004032 if (end != NULL && p + l > end)
4033 return FALSE; /* incomplete byte sequence */
4034 ++p;
4035 while (--l > 0)
4036 if ((*p++ & 0xc0) != 0x80)
4037 return FALSE; /* invalid trail byte */
4038 }
4039 return TRUE;
4040}
4041#endif
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043#if defined(FEAT_GUI) || defined(PROTO)
4044/*
4045 * Special version of mb_tail_off() for use in ScreenLines[].
4046 */
4047 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004048dbcs_screen_tail_off(char_u *base, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
4050 /* It can't be the first byte if a double-byte when not using DBCS, at the
4051 * end of the string or the byte can't start a double-byte.
4052 * For euc-jp an 0x8e byte always means we have a lead byte in the current
4053 * cell. */
4054 if (*p == NUL || p[1] == NUL
4055 || (enc_dbcs == DBCS_JPNU && *p == 0x8e)
4056 || MB_BYTE2LEN(*p) == 1)
4057 return 0;
4058
4059 /* Return 1 when on the lead byte, 0 when on the tail byte. */
4060 return 1 - dbcs_screen_head_off(base, p);
4061}
4062#endif
4063
4064/*
4065 * If the cursor moves on an trail byte, set the cursor on the lead byte.
4066 * Thus it moves left if necessary.
4067 * Return TRUE when the cursor was adjusted.
4068 */
4069 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004070mb_adjust_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004072 mb_adjustpos(curbuf, &curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073}
4074
4075/*
4076 * Adjust position "*lp" to point to the first byte of a multi-byte character.
4077 * If it points to a tail byte it's moved backwards to the head byte.
4078 */
4079 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004080mb_adjustpos(buf_T *buf, pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081{
4082 char_u *p;
4083
4084 if (lp->col > 0
4085#ifdef FEAT_VIRTUALEDIT
4086 || lp->coladd > 1
4087#endif
4088 )
4089 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004090 p = ml_get_buf(buf, lp->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 lp->col -= (*mb_head_off)(p, p + lp->col);
4092#ifdef FEAT_VIRTUALEDIT
4093 /* Reset "coladd" when the cursor would be on the right half of a
4094 * double-wide character. */
4095 if (lp->coladd == 1
4096 && p[lp->col] != TAB
4097 && vim_isprintc((*mb_ptr2char)(p + lp->col))
4098 && ptr2cells(p + lp->col) > 1)
4099 lp->coladd = 0;
4100#endif
4101 }
4102}
4103
4104/*
4105 * Return a pointer to the character before "*p", if there is one.
4106 */
4107 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004108mb_prevptr(
4109 char_u *line, /* start of the string */
4110 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111{
4112 if (p > line)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004113 MB_PTR_BACK(line, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return p;
4115}
4116
4117/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004118 * Return the character length of "str". Each multi-byte character (with
4119 * following composing characters) counts as one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 */
4121 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004122mb_charlen(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004124 char_u *p = str;
4125 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004127 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 return 0;
4129
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004130 for (count = 0; *p != NUL; count++)
4131 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 return count;
4134}
4135
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00004136#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004137/*
4138 * Like mb_charlen() but for a string with specified length.
4139 */
4140 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004141mb_charlen_len(char_u *str, int len)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004142{
4143 char_u *p = str;
4144 int count;
4145
4146 for (count = 0; *p != NUL && p < str + len; count++)
4147 p += (*mb_ptr2len)(p);
4148
4149 return count;
4150}
4151#endif
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153/*
4154 * Try to un-escape a multi-byte character.
4155 * Used for the "to" and "from" part of a mapping.
4156 * Return the un-escaped string if it is a multi-byte character, and advance
4157 * "pp" to just after the bytes that formed it.
4158 * Return NULL if no multi-byte char was found.
4159 */
4160 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004161mb_unescape(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02004163 static char_u buf[6];
4164 int n;
4165 int m = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 char_u *str = *pp;
4167
4168 /* Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02004169 * KS_EXTRA KE_CSI to CSI.
4170 * Maximum length of a utf-8 character is 4 bytes. */
4171 for (n = 0; str[n] != NUL && m < 4; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 if (str[n] == K_SPECIAL
4174 && str[n + 1] == KS_SPECIAL
4175 && str[n + 2] == KE_FILLER)
4176 {
4177 buf[m++] = K_SPECIAL;
4178 n += 2;
4179 }
Bram Moolenaar6203ff92008-01-06 16:18:56 +00004180 else if ((str[n] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181# ifdef FEAT_GUI
Bram Moolenaar6203ff92008-01-06 16:18:56 +00004182 || str[n] == CSI
4183# endif
4184 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 && str[n + 1] == KS_EXTRA
4186 && str[n + 2] == (int)KE_CSI)
4187 {
4188 buf[m++] = CSI;
4189 n += 2;
4190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 else if (str[n] == K_SPECIAL
4192# ifdef FEAT_GUI
4193 || str[n] == CSI
4194# endif
4195 )
4196 break; /* a special key can't be a multibyte char */
4197 else
4198 buf[m++] = str[n];
4199 buf[m] = NUL;
4200
4201 /* Return a multi-byte character if it's found. An illegal sequence
4202 * will result in a 1 here. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004203 if ((*mb_ptr2len)(buf) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205 *pp = str + n + 1;
4206 return buf;
4207 }
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02004208
4209 /* Bail out quickly for ASCII. */
4210 if (buf[0] < 128)
4211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213 return NULL;
4214}
4215
4216/*
4217 * Return TRUE if the character at "row"/"col" on the screen is the left side
4218 * of a double-width character.
4219 * Caller must make sure "row" and "col" are not invalid!
4220 */
4221 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004222mb_lefthalve(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
4224#ifdef FEAT_HANGULIN
4225 if (composing_hangul)
4226 return TRUE;
4227#endif
Bram Moolenaar367329b2007-08-30 11:53:22 +00004228 return (*mb_off2cells)(LineOffset[row] + col,
4229 LineOffset[row] + screen_Columns) > 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232/*
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004233 * Correct a position on the screen, if it's the right half of a double-wide
4234 * char move it to the left half. Returns the corrected column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 */
4236 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004237mb_fix_col(int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239 col = check_col(col);
4240 row = check_row(row);
4241 if (has_mbyte && ScreenLines != NULL && col > 0
4242 && ((enc_dbcs
4243 && ScreenLines[LineOffset[row] + col] != NUL
4244 && dbcs_screen_head_off(ScreenLines + LineOffset[row],
4245 ScreenLines + LineOffset[row] + col))
4246 || (enc_utf8 && ScreenLines[LineOffset[row] + col] == 0)))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004247 return col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return col;
4249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250#endif
4251
4252#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004253static int enc_alias_search(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255/*
4256 * Skip the Vim specific head of a 'encoding' name.
4257 */
4258 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004259enc_skip(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260{
4261 if (STRNCMP(p, "2byte-", 6) == 0)
4262 return p + 6;
4263 if (STRNCMP(p, "8bit-", 5) == 0)
4264 return p + 5;
4265 return p;
4266}
4267
4268/*
4269 * Find the canonical name for encoding "enc".
4270 * When the name isn't recognized, returns "enc" itself, but with all lower
4271 * case characters and '_' replaced with '-'.
4272 * Returns an allocated string. NULL for out-of-memory.
4273 */
4274 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004275enc_canonize(char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
4277 char_u *r;
4278 char_u *p, *s;
4279 int i;
4280
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004281# ifdef FEAT_MBYTE
4282 if (STRCMP(enc, "default") == 0)
4283 {
4284 /* Use the default encoding as it's found by set_init_1(). */
4285 r = get_encoding_default();
4286 if (r == NULL)
4287 r = (char_u *)"latin1";
4288 return vim_strsave(r);
4289 }
4290# endif
4291
Bram Moolenaar29466f22007-05-10 17:45:37 +00004292 /* copy "enc" to allocated memory, with room for two '-' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 r = alloc((unsigned)(STRLEN(enc) + 3));
4294 if (r != NULL)
4295 {
4296 /* Make it all lower case and replace '_' with '-'. */
4297 p = r;
4298 for (s = enc; *s != NUL; ++s)
4299 {
4300 if (*s == '_')
4301 *p++ = '-';
4302 else
4303 *p++ = TOLOWER_ASC(*s);
4304 }
4305 *p = NUL;
4306
4307 /* Skip "2byte-" and "8bit-". */
4308 p = enc_skip(r);
4309
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004310 /* Change "microsoft-cp" to "cp". Used in some spell files. */
4311 if (STRNCMP(p, "microsoft-cp", 12) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004312 STRMOVE(p, p + 10);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 /* "iso8859" -> "iso-8859" */
4315 if (STRNCMP(p, "iso8859", 7) == 0)
4316 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004317 STRMOVE(p + 4, p + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 p[3] = '-';
4319 }
4320
4321 /* "iso-8859n" -> "iso-8859-n" */
4322 if (STRNCMP(p, "iso-8859", 8) == 0 && p[8] != '-')
4323 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004324 STRMOVE(p + 9, p + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 p[8] = '-';
4326 }
4327
4328 /* "latin-N" -> "latinN" */
4329 if (STRNCMP(p, "latin-", 6) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004330 STRMOVE(p + 5, p + 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 if (enc_canon_search(p) >= 0)
4333 {
4334 /* canonical name can be used unmodified */
4335 if (p != r)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004336 STRMOVE(r, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 else if ((i = enc_alias_search(p)) >= 0)
4339 {
4340 /* alias recognized, get canonical name */
4341 vim_free(r);
4342 r = vim_strsave((char_u *)enc_canon_table[i].name);
4343 }
4344 }
4345 return r;
4346}
4347
4348/*
4349 * Search for an encoding alias of "name".
4350 * Returns -1 when not found.
4351 */
4352 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004353enc_alias_search(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354{
4355 int i;
4356
4357 for (i = 0; enc_alias_table[i].name != NULL; ++i)
4358 if (STRCMP(name, enc_alias_table[i].name) == 0)
4359 return enc_alias_table[i].canon;
4360 return -1;
4361}
4362#endif
4363
4364#if defined(FEAT_MBYTE) || defined(PROTO)
4365
4366#ifdef HAVE_LANGINFO_H
4367# include <langinfo.h>
4368#endif
4369
4370/*
4371 * Get the canonicalized encoding of the current locale.
4372 * Returns an allocated string when successful, NULL when not.
4373 */
4374 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004375enc_locale(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377#ifndef WIN3264
4378 char *s;
4379 char *p;
4380 int i;
4381#endif
4382 char buf[50];
4383#ifdef WIN3264
4384 long acp = GetACP();
4385
4386 if (acp == 1200)
4387 STRCPY(buf, "ucs-2le");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004388 else if (acp == 1252) /* cp1252 is used as latin1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 STRCPY(buf, "latin1");
4390 else
4391 sprintf(buf, "cp%ld", acp);
4392#else
4393# ifdef HAVE_NL_LANGINFO_CODESET
4394 if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
4395# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004396# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 if ((s = getenv("LC_ALL")) == NULL || *s == NUL)
4400 if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL)
4401 s = getenv("LANG");
4402
4403 if (s == NULL || *s == NUL)
4404 return FAIL;
4405
4406 /* The most generic locale format is:
4407 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
4408 * If there is a '.' remove the part before it.
4409 * if there is something after the codeset, remove it.
4410 * Make the name lowercase and replace '_' with '-'.
4411 * Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
4412 * "ko_KR.EUC" == "euc-kr"
4413 */
4414 if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL)
4415 {
4416 if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0
4417 && !isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_')
4418 {
4419 /* copy "XY.EUC" to "euc-XY" to buf[10] */
4420 STRCPY(buf + 10, "euc-");
4421 buf[14] = p[-2];
4422 buf[15] = p[-1];
4423 buf[16] = 0;
4424 s = buf + 10;
4425 }
4426 else
4427 s = p + 1;
4428 }
Bram Moolenaar5498a412016-07-11 23:19:05 +02004429 for (i = 0; i < (int)sizeof(buf) - 1 && s[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 if (s[i] == '_' || s[i] == '-')
4432 buf[i] = '-';
4433 else if (isalnum((int)s[i]))
4434 buf[i] = TOLOWER_ASC(s[i]);
4435 else
4436 break;
4437 }
4438 buf[i] = NUL;
4439#endif
4440
4441 return enc_canonize((char_u *)buf);
4442}
4443
Bram Moolenaar693e40c2013-02-26 14:56:42 +01004444#if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445/*
4446 * Convert an encoding name to an MS-Windows codepage.
4447 * Returns zero if no codepage can be figured out.
4448 */
4449 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004450encname2codepage(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int cp;
4453 char_u *p = name;
4454 int idx;
4455
4456 if (STRNCMP(p, "8bit-", 5) == 0)
4457 p += 5;
4458 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
4459 p += 6;
4460
4461 if (p[0] == 'c' && p[1] == 'p')
Bram Moolenaar2c6f3dc2013-07-05 20:09:16 +02004462 cp = atoi((char *)p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 else if ((idx = enc_canon_search(p)) >= 0)
4464 cp = enc_canon_table[idx].codepage;
4465 else
4466 return 0;
4467 if (IsValidCodePage(cp))
4468 return cp;
4469 return 0;
4470}
4471#endif
4472
4473# if defined(USE_ICONV) || defined(PROTO)
4474
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004475static char_u *iconv_string(vimconv_T *vcp, char_u *str, int slen, int *unconvlenp, int *resultlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477/*
4478 * Call iconv_open() with a check if iconv() works properly (there are broken
4479 * versions).
4480 * Returns (void *)-1 if failed.
4481 * (should return iconv_t, but that causes problems with prototypes).
4482 */
4483 void *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004484my_iconv_open(char_u *to, char_u *from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485{
4486 iconv_t fd;
4487#define ICONV_TESTLEN 400
4488 char_u tobuf[ICONV_TESTLEN];
4489 char *p;
4490 size_t tolen;
4491 static int iconv_ok = -1;
4492
4493 if (iconv_ok == FALSE)
4494 return (void *)-1; /* detected a broken iconv() previously */
4495
4496#ifdef DYNAMIC_ICONV
4497 /* Check if the iconv.dll can be found. */
4498 if (!iconv_enabled(TRUE))
4499 return (void *)-1;
4500#endif
4501
4502 fd = iconv_open((char *)enc_skip(to), (char *)enc_skip(from));
4503
4504 if (fd != (iconv_t)-1 && iconv_ok == -1)
4505 {
4506 /*
4507 * Do a dummy iconv() call to check if it actually works. There is a
4508 * version of iconv() on Linux that is broken. We can't ignore it,
4509 * because it's wide-spread. The symptoms are that after outputting
4510 * the initial shift state the "to" pointer is NULL and conversion
4511 * stops for no apparent reason after about 8160 characters.
4512 */
4513 p = (char *)tobuf;
4514 tolen = ICONV_TESTLEN;
4515 (void)iconv(fd, NULL, NULL, &p, &tolen);
4516 if (p == NULL)
4517 {
4518 iconv_ok = FALSE;
4519 iconv_close(fd);
4520 fd = (iconv_t)-1;
4521 }
4522 else
4523 iconv_ok = TRUE;
4524 }
4525
4526 return (void *)fd;
4527}
4528
4529/*
4530 * Convert the string "str[slen]" with iconv().
4531 * If "unconvlenp" is not NULL handle the string ending in an incomplete
4532 * sequence and set "*unconvlenp" to the length of it.
4533 * Returns the converted string in allocated memory. NULL for an error.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004534 * If resultlenp is not NULL, sets it to the result length in bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 */
4536 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004537iconv_string(
4538 vimconv_T *vcp,
4539 char_u *str,
4540 int slen,
4541 int *unconvlenp,
4542 int *resultlenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543{
4544 const char *from;
4545 size_t fromlen;
4546 char *to;
4547 size_t tolen;
4548 size_t len = 0;
4549 size_t done = 0;
4550 char_u *result = NULL;
4551 char_u *p;
4552 int l;
4553
4554 from = (char *)str;
4555 fromlen = slen;
4556 for (;;)
4557 {
4558 if (len == 0 || ICONV_ERRNO == ICONV_E2BIG)
4559 {
4560 /* Allocate enough room for most conversions. When re-allocating
4561 * increase the buffer size. */
4562 len = len + fromlen * 2 + 40;
4563 p = alloc((unsigned)len);
4564 if (p != NULL && done > 0)
4565 mch_memmove(p, result, done);
4566 vim_free(result);
4567 result = p;
4568 if (result == NULL) /* out of memory */
4569 break;
4570 }
4571
4572 to = (char *)result + done;
4573 tolen = len - done - 2;
4574 /* Avoid a warning for systems with a wrong iconv() prototype by
4575 * casting the second argument to void *. */
4576 if (iconv(vcp->vc_fd, (void *)&from, &fromlen, &to, &tolen)
4577 != (size_t)-1)
4578 {
4579 /* Finished, append a NUL. */
4580 *to = NUL;
4581 break;
4582 }
4583
4584 /* Check both ICONV_EINVAL and EINVAL, because the dynamically loaded
4585 * iconv library may use one of them. */
4586 if (!vcp->vc_fail && unconvlenp != NULL
4587 && (ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4588 {
4589 /* Handle an incomplete sequence at the end. */
4590 *to = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004591 *unconvlenp = (int)fromlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 break;
4593 }
4594
4595 /* Check both ICONV_EILSEQ and EILSEQ, because the dynamically loaded
4596 * iconv library may use one of them. */
4597 else if (!vcp->vc_fail
4598 && (ICONV_ERRNO == ICONV_EILSEQ || ICONV_ERRNO == EILSEQ
4599 || ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4600 {
4601 /* Can't convert: insert a '?' and skip a character. This assumes
4602 * conversion from 'encoding' to something else. In other
4603 * situations we don't know what to skip anyway. */
4604 *to++ = '?';
4605 if ((*mb_ptr2cells)((char_u *)from) > 1)
4606 *to++ = '?';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004607 if (enc_utf8)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004608 l = utfc_ptr2len_len((char_u *)from, (int)fromlen);
Bram Moolenaar217ad922005-03-20 22:37:15 +00004609 else
4610 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004611 l = (*mb_ptr2len)((char_u *)from);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004612 if (l > (int)fromlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004613 l = (int)fromlen;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 from += l;
4616 fromlen -= l;
4617 }
4618 else if (ICONV_ERRNO != ICONV_E2BIG)
4619 {
4620 /* conversion failed */
4621 vim_free(result);
4622 result = NULL;
4623 break;
4624 }
4625 /* Not enough room or skipping illegal sequence. */
4626 done = to - (char *)result;
4627 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004628
Bram Moolenaar0d35e912011-04-11 14:29:17 +02004629 if (resultlenp != NULL && result != NULL)
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004630 *resultlenp = (int)(to - (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 return result;
4632}
4633
4634# if defined(DYNAMIC_ICONV) || defined(PROTO)
4635/*
4636 * Dynamically load the "iconv.dll" on Win32.
4637 */
4638
Bram Moolenaar938ee832016-01-24 15:36:03 +01004639# ifndef DYNAMIC_ICONV /* must be generating prototypes */
4640# define HINSTANCE int
4641# endif
Bram Moolenaard6f676d2005-06-01 21:51:55 +00004642static HINSTANCE hIconvDLL = 0;
4643static HINSTANCE hMsvcrtDLL = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
Bram Moolenaar938ee832016-01-24 15:36:03 +01004645# ifndef DYNAMIC_ICONV_DLL
4646# define DYNAMIC_ICONV_DLL "iconv.dll"
4647# define DYNAMIC_ICONV_DLL_ALT1 "libiconv.dll"
4648# define DYNAMIC_ICONV_DLL_ALT2 "libiconv2.dll"
4649# define DYNAMIC_ICONV_DLL_ALT3 "libiconv-2.dll"
4650# endif
4651# ifndef DYNAMIC_MSVCRT_DLL
4652# define DYNAMIC_MSVCRT_DLL "msvcrt.dll"
4653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655/*
4656 * Try opening the iconv.dll and return TRUE if iconv() can be used.
4657 */
4658 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004659iconv_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 if (hIconvDLL != 0 && hMsvcrtDLL != 0)
4662 return TRUE;
Bram Moolenaar9d6ca1c2015-10-13 13:49:09 +02004663
Bram Moolenaar938ee832016-01-24 15:36:03 +01004664 /* The iconv DLL file goes under different names, try them all.
4665 * Do the "2" version first, it's newer. */
4666#ifdef DYNAMIC_ICONV_DLL_ALT2
Bram Moolenaar9d6ca1c2015-10-13 13:49:09 +02004667 if (hIconvDLL == 0)
4668 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL_ALT2);
Bram Moolenaar938ee832016-01-24 15:36:03 +01004669#endif
4670#ifdef DYNAMIC_ICONV_DLL_ALT3
Bram Moolenaar9d6ca1c2015-10-13 13:49:09 +02004671 if (hIconvDLL == 0)
4672 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL_ALT3);
Bram Moolenaar938ee832016-01-24 15:36:03 +01004673#endif
4674 if (hIconvDLL == 0)
4675 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL);
4676#ifdef DYNAMIC_ICONV_DLL_ALT1
4677 if (hIconvDLL == 0)
4678 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL_ALT1);
4679#endif
Bram Moolenaar9d6ca1c2015-10-13 13:49:09 +02004680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (hIconvDLL != 0)
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004682 hMsvcrtDLL = vimLoadLib(DYNAMIC_MSVCRT_DLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (hIconvDLL == 0 || hMsvcrtDLL == 0)
4684 {
4685 /* Only give the message when 'verbose' is set, otherwise it might be
4686 * done whenever a conversion is attempted. */
4687 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004688 {
4689 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 EMSG2(_(e_loadlib),
4691 hIconvDLL == 0 ? DYNAMIC_ICONV_DLL : DYNAMIC_MSVCRT_DLL);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004692 verbose_leave();
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 iconv_end();
4695 return FALSE;
4696 }
4697
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004698 iconv = (void *)GetProcAddress(hIconvDLL, "libiconv");
4699 iconv_open = (void *)GetProcAddress(hIconvDLL, "libiconv_open");
4700 iconv_close = (void *)GetProcAddress(hIconvDLL, "libiconv_close");
4701 iconvctl = (void *)GetProcAddress(hIconvDLL, "libiconvctl");
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004702 iconv_errno = get_dll_import_func(hIconvDLL, "_errno");
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004703 if (iconv_errno == NULL)
4704 iconv_errno = (void *)GetProcAddress(hMsvcrtDLL, "_errno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (iconv == NULL || iconv_open == NULL || iconv_close == NULL
4706 || iconvctl == NULL || iconv_errno == NULL)
4707 {
4708 iconv_end();
4709 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004710 {
4711 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 EMSG2(_(e_loadfunc), "for libiconv");
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004713 verbose_leave();
4714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 return FALSE;
4716 }
4717 return TRUE;
4718}
4719
4720 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004721iconv_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 /* Don't use iconv() when inputting or outputting characters. */
4724 if (input_conv.vc_type == CONV_ICONV)
4725 convert_setup(&input_conv, NULL, NULL);
4726 if (output_conv.vc_type == CONV_ICONV)
4727 convert_setup(&output_conv, NULL, NULL);
4728
4729 if (hIconvDLL != 0)
4730 FreeLibrary(hIconvDLL);
4731 if (hMsvcrtDLL != 0)
4732 FreeLibrary(hMsvcrtDLL);
4733 hIconvDLL = 0;
4734 hMsvcrtDLL = 0;
4735}
4736# endif /* DYNAMIC_ICONV */
4737# endif /* USE_ICONV */
4738
4739#endif /* FEAT_MBYTE */
4740
4741#if defined(FEAT_XIM) || defined(PROTO)
4742
Bram Moolenaar64404472010-06-26 06:24:45 +02004743# if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744static int xim_has_preediting INIT(= FALSE); /* IM current status */
4745
4746/*
4747 * Set preedit_start_col to the current cursor position.
4748 */
4749 static void
4750init_preedit_start_col(void)
4751{
4752 if (State & CMDLINE)
4753 preedit_start_col = cmdline_getvcol_cursor();
Bram Moolenaar9ec021a2015-12-12 16:23:29 +01004754 else if (curwin != NULL && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 getvcol(curwin, &curwin->w_cursor, &preedit_start_col, NULL, NULL);
4756 /* Prevent that preediting marks the buffer as changed. */
4757 xim_changed_while_preediting = curbuf->b_changed;
4758}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759
4760static int im_is_active = FALSE; /* IM is enabled for current mode */
Bram Moolenaarc236c162008-07-13 17:41:49 +00004761static int preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762static int im_preedit_cursor = 0; /* cursor offset in characters */
4763static int im_preedit_trailing = 0; /* number of characters after cursor */
4764
4765static unsigned long im_commit_handler_id = 0;
4766static unsigned int im_activatekey_keyval = GDK_VoidSymbol;
4767static unsigned int im_activatekey_state = 0;
4768
4769 void
4770im_set_active(int active)
4771{
4772 int was_active;
4773
Bram Moolenaarabab85a2013-06-26 19:18:05 +02004774 was_active = !!im_get_status();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 im_is_active = (active && !p_imdisable);
4776
4777 if (im_is_active != was_active)
4778 xim_reset();
4779}
4780
4781 void
4782xim_set_focus(int focus)
4783{
4784 if (xic != NULL)
4785 {
4786 if (focus)
4787 gtk_im_context_focus_in(xic);
4788 else
4789 gtk_im_context_focus_out(xic);
4790 }
4791}
4792
4793 void
4794im_set_position(int row, int col)
4795{
4796 if (xic != NULL)
4797 {
4798 GdkRectangle area;
4799
4800 area.x = FILL_X(col);
4801 area.y = FILL_Y(row);
4802 area.width = gui.char_width * (mb_lefthalve(row, col) ? 2 : 1);
4803 area.height = gui.char_height;
4804
4805 gtk_im_context_set_cursor_location(xic, &area);
4806 }
4807}
4808
4809# if 0 || defined(PROTO) /* apparently only used in gui_x11.c */
4810 void
4811xim_set_preedit(void)
4812{
4813 im_set_position(gui.row, gui.col);
4814}
4815# endif
4816
4817 static void
4818im_add_to_input(char_u *str, int len)
4819{
4820 /* Convert from 'termencoding' (always "utf-8") to 'encoding' */
4821 if (input_conv.vc_type != CONV_NONE)
4822 {
4823 str = string_convert(&input_conv, str, &len);
4824 g_return_if_fail(str != NULL);
4825 }
4826
4827 add_to_input_buf_csi(str, len);
4828
4829 if (input_conv.vc_type != CONV_NONE)
4830 vim_free(str);
4831
4832 if (p_mh) /* blank out the pointer if necessary */
4833 gui_mch_mousehide(TRUE);
4834}
4835
4836 static void
4837im_delete_preedit(void)
4838{
4839 char_u bskey[] = {CSI, 'k', 'b'};
4840 char_u delkey[] = {CSI, 'k', 'D'};
4841
4842 if (State & NORMAL)
4843 {
4844 im_preedit_cursor = 0;
4845 return;
4846 }
4847 for (; im_preedit_cursor > 0; --im_preedit_cursor)
4848 add_to_input_buf(bskey, (int)sizeof(bskey));
4849
4850 for (; im_preedit_trailing > 0; --im_preedit_trailing)
4851 add_to_input_buf(delkey, (int)sizeof(delkey));
4852}
4853
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004854/*
4855 * Move the cursor left by "num_move_back" characters.
4856 * Note that ins_left() checks im_is_preediting() to avoid breaking undo for
4857 * these K_LEFT keys.
4858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 static void
4860im_correct_cursor(int num_move_back)
4861{
4862 char_u backkey[] = {CSI, 'k', 'l'};
4863
4864 if (State & NORMAL)
4865 return;
4866# ifdef FEAT_RIGHTLEFT
4867 if ((State & CMDLINE) == 0 && curwin != NULL && curwin->w_p_rl)
4868 backkey[2] = 'r';
4869# endif
4870 for (; num_move_back > 0; --num_move_back)
4871 add_to_input_buf(backkey, (int)sizeof(backkey));
4872}
4873
4874static int xim_expected_char = NUL;
4875static int xim_ignored_char = FALSE;
4876
4877/*
4878 * Update the mode and cursor while in an IM callback.
4879 */
4880 static void
4881im_show_info(void)
4882{
4883 int old_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 old_vgetc_busy = vgetc_busy;
4886 vgetc_busy = TRUE;
4887 showmode();
4888 vgetc_busy = old_vgetc_busy;
Bram Moolenaar917ba892012-03-07 19:38:55 +01004889 if ((State & NORMAL) || (State & INSERT))
4890 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 out_flush();
4892}
4893
4894/*
4895 * Callback invoked when the user finished preediting.
4896 * Put the final string into the input buffer.
4897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004899im_commit_cb(GtkIMContext *context UNUSED,
4900 const gchar *str,
4901 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
Bram Moolenaar72597a52010-07-18 15:31:08 +02004903 int slen = (int)STRLEN(str);
4904 int add_to_input = TRUE;
4905 int clen;
4906 int len = slen;
4907 int commit_with_preedit = TRUE;
4908 char_u *im_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910#ifdef XIM_DEBUG
4911 xim_log("im_commit_cb(): %s\n", str);
4912#endif
4913
4914 /* The imhangul module doesn't reset the preedit string before
4915 * committing. Call im_delete_preedit() to work around that. */
4916 im_delete_preedit();
4917
4918 /* Indicate that preediting has finished. */
4919 if (preedit_start_col == MAXCOL)
4920 {
4921 init_preedit_start_col();
4922 commit_with_preedit = FALSE;
4923 }
4924
4925 /* The thing which setting "preedit_start_col" to MAXCOL means that
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004926 * "preedit_start_col" will be set forcedly when calling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 * preedit_changed_cb() next time.
4928 * "preedit_start_col" should not reset with MAXCOL on this part. Vim
4929 * is simulating the preediting by using add_to_input_str(). when
4930 * preedit begin immediately before committed, the typebuf is not
4931 * flushed to screen, then it can't get correct "preedit_start_col".
4932 * Thus, it should calculate the cells by adding cells of the committed
4933 * string. */
4934 if (input_conv.vc_type != CONV_NONE)
4935 {
4936 im_str = string_convert(&input_conv, (char_u *)str, &len);
4937 g_return_if_fail(im_str != NULL);
4938 }
4939 else
4940 im_str = (char_u *)str;
Bram Moolenaar72597a52010-07-18 15:31:08 +02004941
4942 clen = mb_string2cells(im_str, len);
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (input_conv.vc_type != CONV_NONE)
4945 vim_free(im_str);
4946 preedit_start_col += clen;
4947
4948 /* Is this a single character that matches a keypad key that's just
4949 * been pressed? If so, we don't want it to be entered as such - let
4950 * us carry on processing the raw keycode so that it may be used in
4951 * mappings as <kSomething>. */
4952 if (xim_expected_char != NUL)
4953 {
4954 /* We're currently processing a keypad or other special key */
4955 if (slen == 1 && str[0] == xim_expected_char)
4956 {
4957 /* It's a match - don't do it here */
4958 xim_ignored_char = TRUE;
4959 add_to_input = FALSE;
4960 }
4961 else
4962 {
4963 /* Not a match */
4964 xim_ignored_char = FALSE;
4965 }
4966 }
4967
4968 if (add_to_input)
4969 im_add_to_input((char_u *)str, slen);
4970
4971 /* Inserting chars while "im_is_active" is set does not cause a change of
4972 * buffer. When the chars are committed the buffer must be marked as
4973 * changed. */
4974 if (!commit_with_preedit)
4975 preedit_start_col = MAXCOL;
4976
4977 /* This flag is used in changed() at next call. */
4978 xim_changed_while_preediting = TRUE;
4979
4980 if (gtk_main_level() > 0)
4981 gtk_main_quit();
4982}
4983
4984/*
4985 * Callback invoked after start to the preedit.
4986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004988im_preedit_start_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990#ifdef XIM_DEBUG
4991 xim_log("im_preedit_start_cb()\n");
4992#endif
4993
4994 im_is_active = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00004995 preedit_is_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarc236c162008-07-13 17:41:49 +00004997 im_show_info();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998}
4999
5000/*
5001 * Callback invoked after end to the preedit.
5002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005004im_preedit_end_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006#ifdef XIM_DEBUG
5007 xim_log("im_preedit_end_cb()\n");
5008#endif
5009 im_delete_preedit();
5010
5011 /* Indicate that preediting has finished */
5012 preedit_start_col = MAXCOL;
5013 xim_has_preediting = FALSE;
5014
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005015#if 0
5016 /* Removal of this line suggested by Takuhiro Nishioka. Fixes that IM was
Bram Moolenaarc236c162008-07-13 17:41:49 +00005017 * switched off unintentionally. We now use preedit_is_active (added by
5018 * SungHyun Nam). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 im_is_active = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005020#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00005021 preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 gui_update_cursor(TRUE, FALSE);
5023 im_show_info();
5024}
5025
5026/*
5027 * Callback invoked after changes to the preedit string. If the preedit
5028 * string was empty before, remember the preedit start column so we know
5029 * where to apply feedback attributes. Delete the previous preedit string
5030 * if there was one, save the new preedit cursor offset, and put the new
5031 * string into the input buffer.
5032 *
5033 * TODO: The pragmatic "put into input buffer" approach used here has
5034 * several fundamental problems:
5035 *
5036 * - The characters in the preedit string are subject to remapping.
5037 * That's broken, only the finally committed string should be remapped.
5038 *
5039 * - There is a race condition involved: The retrieved value for the
5040 * current cursor position will be wrong if any unprocessed characters
5041 * are still queued in the input buffer.
5042 *
5043 * - Due to the lack of synchronization between the file buffer in memory
5044 * and any typed characters, it's practically impossible to implement the
5045 * "retrieve_surrounding" and "delete_surrounding" signals reliably. IM
5046 * modules for languages such as Thai are likely to rely on this feature
5047 * for proper operation.
5048 *
5049 * Conclusions: I think support for preediting needs to be moved to the
5050 * core parts of Vim. Ideally, until it has been committed, the preediting
5051 * string should only be displayed and not affect the buffer content at all.
5052 * The question how to deal with the synchronization issue still remains.
5053 * Circumventing the input buffer is probably not desirable. Anyway, I think
5054 * implementing "retrieve_surrounding" is the only hard problem.
5055 *
5056 * One way to solve all of this in a clean manner would be to queue all key
5057 * press/release events "as is" in the input buffer, and apply the IM filtering
5058 * at the receiving end of the queue. This, however, would have a rather large
5059 * impact on the code base. If there is an easy way to force processing of all
5060 * remaining input from within the "retrieve_surrounding" signal handler, this
5061 * might not be necessary. Gotta ask on vim-dev for opinions.
5062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005064im_preedit_changed_cb(GtkIMContext *context, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 char *preedit_string = NULL;
5067 int cursor_index = 0;
5068 int num_move_back = 0;
5069 char_u *str;
5070 char_u *p;
5071 int i;
5072
5073 gtk_im_context_get_preedit_string(context,
5074 &preedit_string, NULL,
5075 &cursor_index);
5076
5077#ifdef XIM_DEBUG
5078 xim_log("im_preedit_changed_cb(): %s\n", preedit_string);
5079#endif
5080
5081 g_return_if_fail(preedit_string != NULL); /* just in case */
5082
5083 /* If preedit_start_col is MAXCOL set it to the current cursor position. */
5084 if (preedit_start_col == MAXCOL && preedit_string[0] != '\0')
5085 {
5086 xim_has_preediting = TRUE;
5087
5088 /* Urgh, this breaks if the input buffer isn't empty now */
5089 init_preedit_start_col();
5090 }
5091 else if (cursor_index == 0 && preedit_string[0] == '\0')
5092 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005093 xim_has_preediting = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 /* If at the start position (after typing backspace)
5096 * preedit_start_col must be reset. */
5097 preedit_start_col = MAXCOL;
5098 }
5099
5100 im_delete_preedit();
5101
5102 /*
5103 * Compute the end of the preediting area: "preedit_end_col".
5104 * According to the documentation of gtk_im_context_get_preedit_string(),
5105 * the cursor_pos output argument returns the offset in bytes. This is
5106 * unfortunately not true -- real life shows the offset is in characters,
5107 * and the GTK+ source code agrees with me. Will file a bug later.
5108 */
5109 if (preedit_start_col != MAXCOL)
5110 preedit_end_col = preedit_start_col;
5111 str = (char_u *)preedit_string;
5112 for (p = str, i = 0; *p != NUL; p += utf_byte2len(*p), ++i)
5113 {
5114 int is_composing;
5115
5116 is_composing = ((*p & 0x80) != 0 && utf_iscomposing(utf_ptr2char(p)));
5117 /*
5118 * These offsets are used as counters when generating <BS> and <Del>
5119 * to delete the preedit string. So don't count composing characters
5120 * unless 'delcombine' is enabled.
5121 */
5122 if (!is_composing || p_deco)
5123 {
5124 if (i < cursor_index)
5125 ++im_preedit_cursor;
5126 else
5127 ++im_preedit_trailing;
5128 }
5129 if (!is_composing && i >= cursor_index)
5130 {
5131 /* This is essentially the same as im_preedit_trailing, except
5132 * composing characters are not counted even if p_deco is set. */
5133 ++num_move_back;
5134 }
5135 if (preedit_start_col != MAXCOL)
5136 preedit_end_col += utf_ptr2cells(p);
5137 }
5138
5139 if (p > str)
5140 {
5141 im_add_to_input(str, (int)(p - str));
5142 im_correct_cursor(num_move_back);
5143 }
5144
5145 g_free(preedit_string);
5146
5147 if (gtk_main_level() > 0)
5148 gtk_main_quit();
5149}
5150
5151/*
5152 * Translate the Pango attributes at iter to Vim highlighting attributes.
5153 * Ignore attributes not supported by Vim highlighting. This shouldn't have
5154 * too much impact -- right now we handle even more attributes than necessary
5155 * for the IM modules I tested with.
5156 */
5157 static int
5158translate_pango_attributes(PangoAttrIterator *iter)
5159{
5160 PangoAttribute *attr;
5161 int char_attr = HL_NORMAL;
5162
5163 attr = pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE);
5164 if (attr != NULL && ((PangoAttrInt *)attr)->value
5165 != (int)PANGO_UNDERLINE_NONE)
5166 char_attr |= HL_UNDERLINE;
5167
5168 attr = pango_attr_iterator_get(iter, PANGO_ATTR_WEIGHT);
5169 if (attr != NULL && ((PangoAttrInt *)attr)->value >= (int)PANGO_WEIGHT_BOLD)
5170 char_attr |= HL_BOLD;
5171
5172 attr = pango_attr_iterator_get(iter, PANGO_ATTR_STYLE);
5173 if (attr != NULL && ((PangoAttrInt *)attr)->value
5174 != (int)PANGO_STYLE_NORMAL)
5175 char_attr |= HL_ITALIC;
5176
5177 attr = pango_attr_iterator_get(iter, PANGO_ATTR_BACKGROUND);
5178 if (attr != NULL)
5179 {
5180 const PangoColor *color = &((PangoAttrColor *)attr)->color;
5181
5182 /* Assume inverse if black background is requested */
5183 if ((color->red | color->green | color->blue) == 0)
5184 char_attr |= HL_INVERSE;
5185 }
5186
5187 return char_attr;
5188}
5189
5190/*
5191 * Retrieve the highlighting attributes at column col in the preedit string.
5192 * Return -1 if not in preediting mode or if col is out of range.
5193 */
5194 int
5195im_get_feedback_attr(int col)
5196{
5197 char *preedit_string = NULL;
5198 PangoAttrList *attr_list = NULL;
5199 int char_attr = -1;
5200
5201 if (xic == NULL)
5202 return char_attr;
5203
5204 gtk_im_context_get_preedit_string(xic, &preedit_string, &attr_list, NULL);
5205
5206 if (preedit_string != NULL && attr_list != NULL)
5207 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005208 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
5210 /* Get the byte index as used by PangoAttrIterator */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005211 for (idx = 0; col > 0 && preedit_string[idx] != '\0'; --col)
5212 idx += utfc_ptr2len((char_u *)preedit_string + idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
Bram Moolenaar89d40322006-08-29 15:30:07 +00005214 if (preedit_string[idx] != '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 PangoAttrIterator *iter;
5217 int start, end;
5218
5219 char_attr = HL_NORMAL;
5220 iter = pango_attr_list_get_iterator(attr_list);
5221
5222 /* Extract all relevant attributes from the list. */
5223 do
5224 {
5225 pango_attr_iterator_range(iter, &start, &end);
5226
Bram Moolenaar89d40322006-08-29 15:30:07 +00005227 if (idx >= start && idx < end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 char_attr |= translate_pango_attributes(iter);
5229 }
5230 while (pango_attr_iterator_next(iter));
5231
5232 pango_attr_iterator_destroy(iter);
5233 }
5234 }
5235
5236 if (attr_list != NULL)
5237 pango_attr_list_unref(attr_list);
5238 g_free(preedit_string);
5239
5240 return char_attr;
5241}
5242
5243 void
5244xim_init(void)
5245{
5246#ifdef XIM_DEBUG
5247 xim_log("xim_init()\n");
5248#endif
5249
5250 g_return_if_fail(gui.drawarea != NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005251#if GTK_CHECK_VERSION(3,0,0)
5252 g_return_if_fail(gtk_widget_get_window(gui.drawarea) != NULL);
5253#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 g_return_if_fail(gui.drawarea->window != NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257 xic = gtk_im_multicontext_new();
5258 g_object_ref(xic);
5259
5260 im_commit_handler_id = g_signal_connect(G_OBJECT(xic), "commit",
5261 G_CALLBACK(&im_commit_cb), NULL);
5262 g_signal_connect(G_OBJECT(xic), "preedit_changed",
5263 G_CALLBACK(&im_preedit_changed_cb), NULL);
5264 g_signal_connect(G_OBJECT(xic), "preedit_start",
5265 G_CALLBACK(&im_preedit_start_cb), NULL);
5266 g_signal_connect(G_OBJECT(xic), "preedit_end",
5267 G_CALLBACK(&im_preedit_end_cb), NULL);
5268
Bram Moolenaar98921892016-02-23 17:14:37 +01005269#if GTK_CHECK_VERSION(3,0,0)
5270 gtk_im_context_set_client_window(xic, gtk_widget_get_window(gui.drawarea));
5271#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 gtk_im_context_set_client_window(xic, gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01005273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274}
5275
5276 void
5277im_shutdown(void)
5278{
5279#ifdef XIM_DEBUG
5280 xim_log("im_shutdown()\n");
5281#endif
5282
5283 if (xic != NULL)
5284 {
5285 gtk_im_context_focus_out(xic);
5286 g_object_unref(xic);
5287 xic = NULL;
5288 }
5289 im_is_active = FALSE;
5290 im_commit_handler_id = 0;
5291 preedit_start_col = MAXCOL;
5292 xim_has_preediting = FALSE;
5293}
5294
5295/*
5296 * Convert the string argument to keyval and state for GdkEventKey.
5297 * If str is valid return TRUE, otherwise FALSE.
5298 *
5299 * See 'imactivatekey' for documentation of the format.
5300 */
5301 static int
5302im_string_to_keyval(const char *str, unsigned int *keyval, unsigned int *state)
5303{
5304 const char *mods_end;
5305 unsigned tmp_keyval;
5306 unsigned tmp_state = 0;
5307
5308 mods_end = strrchr(str, '-');
5309 mods_end = (mods_end != NULL) ? mods_end + 1 : str;
5310
5311 /* Parse modifier keys */
5312 while (str < mods_end)
5313 switch (*str++)
5314 {
5315 case '-': break;
5316 case 'S': case 's': tmp_state |= (unsigned)GDK_SHIFT_MASK; break;
5317 case 'L': case 'l': tmp_state |= (unsigned)GDK_LOCK_MASK; break;
5318 case 'C': case 'c': tmp_state |= (unsigned)GDK_CONTROL_MASK;break;
5319 case '1': tmp_state |= (unsigned)GDK_MOD1_MASK; break;
5320 case '2': tmp_state |= (unsigned)GDK_MOD2_MASK; break;
5321 case '3': tmp_state |= (unsigned)GDK_MOD3_MASK; break;
5322 case '4': tmp_state |= (unsigned)GDK_MOD4_MASK; break;
5323 case '5': tmp_state |= (unsigned)GDK_MOD5_MASK; break;
5324 default:
5325 return FALSE;
5326 }
5327
5328 tmp_keyval = gdk_keyval_from_name(str);
5329
5330 if (tmp_keyval == 0 || tmp_keyval == GDK_VoidSymbol)
5331 return FALSE;
5332
5333 if (keyval != NULL)
5334 *keyval = tmp_keyval;
5335 if (state != NULL)
5336 *state = tmp_state;
5337
5338 return TRUE;
5339}
5340
5341/*
5342 * Return TRUE if p_imak is valid, otherwise FALSE. As a special case, an
5343 * empty string is also regarded as valid.
5344 *
5345 * Note: The numerical key value of p_imak is cached if it was valid; thus
5346 * boldly assuming im_xim_isvalid_imactivate() will always be called whenever
5347 * 'imak' changes. This is currently the case but not obvious -- should
5348 * probably rename the function for clarity.
5349 */
5350 int
5351im_xim_isvalid_imactivate(void)
5352{
5353 if (p_imak[0] == NUL)
5354 {
5355 im_activatekey_keyval = GDK_VoidSymbol;
5356 im_activatekey_state = 0;
5357 return TRUE;
5358 }
5359
5360 return im_string_to_keyval((const char *)p_imak,
5361 &im_activatekey_keyval,
5362 &im_activatekey_state);
5363}
5364
5365 static void
5366im_synthesize_keypress(unsigned int keyval, unsigned int state)
5367{
5368 GdkEventKey *event;
5369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 event = (GdkEventKey *)gdk_event_new(GDK_KEY_PRESS);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02005371# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01005372 g_object_ref(gtk_widget_get_window(gui.drawarea));
5373 /* unreffed by gdk_event_free() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374# else
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02005375 g_object_ref(gui.drawarea->window); /* unreffed by gdk_event_free() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01005377# if GTK_CHECK_VERSION(3,0,0)
5378 event->window = gtk_widget_get_window(gui.drawarea);
5379# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 event->window = gui.drawarea->window;
Bram Moolenaar98921892016-02-23 17:14:37 +01005381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 event->send_event = TRUE;
5383 event->time = GDK_CURRENT_TIME;
5384 event->state = state;
5385 event->keyval = keyval;
5386 event->hardware_keycode = /* needed for XIM */
5387 XKeysymToKeycode(GDK_WINDOW_XDISPLAY(event->window), (KeySym)keyval);
5388 event->length = 0;
5389 event->string = NULL;
5390
5391 gtk_im_context_filter_keypress(xic, event);
5392
5393 /* For consistency, also send the corresponding release event. */
5394 event->type = GDK_KEY_RELEASE;
5395 event->send_event = FALSE;
5396 gtk_im_context_filter_keypress(xic, event);
5397
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 gdk_event_free((GdkEvent *)event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399}
5400
5401 void
5402xim_reset(void)
5403{
5404 if (xic != NULL)
5405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 gtk_im_context_reset(xic);
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 if (p_imdisable)
5409 im_shutdown();
5410 else
5411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 xim_set_focus(gui.in_focus);
5413
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005414# ifdef FEAT_EVAL
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005415 if (p_imaf[0] != NUL)
5416 {
5417 char_u *argv[1];
5418
5419 if (im_is_active)
5420 argv[0] = (char_u *)"1";
5421 else
5422 argv[0] = (char_u *)"0";
5423 (void)call_func_retnr(p_imaf, 1, argv, FALSE);
5424 }
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005425 else
5426# endif
5427 if (im_activatekey_keyval != GDK_VoidSymbol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
5429 if (im_is_active)
5430 {
5431 g_signal_handler_block(xic, im_commit_handler_id);
5432 im_synthesize_keypress(im_activatekey_keyval,
5433 im_activatekey_state);
5434 g_signal_handler_unblock(xic, im_commit_handler_id);
5435 }
5436 }
5437 else
5438 {
5439 im_shutdown();
5440 xim_init();
5441 xim_set_focus(gui.in_focus);
5442 }
5443 }
5444 }
5445
5446 preedit_start_col = MAXCOL;
5447 xim_has_preediting = FALSE;
5448}
5449
5450 int
5451xim_queue_key_press_event(GdkEventKey *event, int down)
5452{
5453 if (down)
5454 {
5455 /*
5456 * Workaround GTK2 XIM 'feature' that always converts keypad keys to
5457 * chars., even when not part of an IM sequence (ref. feature of
5458 * gdk/gdkkeyuni.c).
5459 * Flag any keypad keys that might represent a single char.
5460 * If this (on its own - i.e., not part of an IM sequence) is
5461 * committed while we're processing one of these keys, we can ignore
5462 * that commit and go ahead & process it ourselves. That way we can
5463 * still distinguish keypad keys for use in mappings.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005464 * Also add GDK_space to make <S-Space> work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 */
5466 switch (event->keyval)
5467 {
5468 case GDK_KP_Add: xim_expected_char = '+'; break;
5469 case GDK_KP_Subtract: xim_expected_char = '-'; break;
5470 case GDK_KP_Divide: xim_expected_char = '/'; break;
5471 case GDK_KP_Multiply: xim_expected_char = '*'; break;
5472 case GDK_KP_Decimal: xim_expected_char = '.'; break;
5473 case GDK_KP_Equal: xim_expected_char = '='; break;
5474 case GDK_KP_0: xim_expected_char = '0'; break;
5475 case GDK_KP_1: xim_expected_char = '1'; break;
5476 case GDK_KP_2: xim_expected_char = '2'; break;
5477 case GDK_KP_3: xim_expected_char = '3'; break;
5478 case GDK_KP_4: xim_expected_char = '4'; break;
5479 case GDK_KP_5: xim_expected_char = '5'; break;
5480 case GDK_KP_6: xim_expected_char = '6'; break;
5481 case GDK_KP_7: xim_expected_char = '7'; break;
5482 case GDK_KP_8: xim_expected_char = '8'; break;
5483 case GDK_KP_9: xim_expected_char = '9'; break;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005484 case GDK_space: xim_expected_char = ' '; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 default: xim_expected_char = NUL;
5486 }
5487 xim_ignored_char = FALSE;
5488 }
5489
5490 /*
5491 * When typing fFtT, XIM may be activated. Thus it must pass
5492 * gtk_im_context_filter_keypress() in Normal mode.
5493 * And while doing :sh too.
5494 */
5495 if (xic != NULL && !p_imdisable
5496 && (State & (INSERT | CMDLINE | NORMAL | EXTERNCMD)) != 0)
5497 {
5498 /*
5499 * Filter 'imactivatekey' and map it to CTRL-^. This way, Vim is
5500 * always aware of the current status of IM, and can even emulate
5501 * the activation key for modules that don't support one.
5502 */
5503 if (event->keyval == im_activatekey_keyval
5504 && (event->state & im_activatekey_state) == im_activatekey_state)
5505 {
5506 unsigned int state_mask;
5507
5508 /* Require the state of the 3 most used modifiers to match exactly.
5509 * Otherwise e.g. <S-C-space> would be unusable for other purposes
5510 * if the IM activate key is <S-space>. */
5511 state_mask = im_activatekey_state;
5512 state_mask |= ((int)GDK_SHIFT_MASK | (int)GDK_CONTROL_MASK
5513 | (int)GDK_MOD1_MASK);
5514
5515 if ((event->state & state_mask) != im_activatekey_state)
5516 return FALSE;
5517
5518 /* Don't send it a second time on GDK_KEY_RELEASE. */
5519 if (event->type != GDK_KEY_PRESS)
5520 return TRUE;
5521
Bram Moolenaar658b74a2006-03-18 21:33:02 +00005522 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
5524 im_set_active(FALSE);
5525
5526 /* ":lmap" mappings exists, toggle use of mappings. */
5527 State ^= LANGMAP;
5528 if (State & LANGMAP)
5529 {
5530 curbuf->b_p_iminsert = B_IMODE_NONE;
5531 State &= ~LANGMAP;
5532 }
5533 else
5534 {
5535 curbuf->b_p_iminsert = B_IMODE_LMAP;
5536 State |= LANGMAP;
5537 }
5538 return TRUE;
5539 }
5540
5541 return gtk_im_context_filter_keypress(xic, event);
5542 }
5543
5544 /* Don't filter events through the IM context if IM isn't active
5545 * right now. Unlike with GTK+ 1.2 we cannot rely on the IM module
5546 * not doing anything before the activation key was sent. */
5547 if (im_activatekey_keyval == GDK_VoidSymbol || im_is_active)
5548 {
5549 int imresult = gtk_im_context_filter_keypress(xic, event);
5550
5551 /* Some XIM send following sequence:
5552 * 1. preedited string.
5553 * 2. committed string.
5554 * 3. line changed key.
5555 * 4. preedited string.
5556 * 5. remove preedited string.
5557 * if 3, Vim can't move back the above line for 5.
5558 * thus, this part should not parse the key. */
5559 if (!imresult && preedit_start_col != MAXCOL
5560 && event->keyval == GDK_Return)
5561 {
5562 im_synthesize_keypress(GDK_Return, 0U);
5563 return FALSE;
5564 }
5565
5566 /* If XIM tried to commit a keypad key as a single char.,
5567 * ignore it so we can use the keypad key 'raw', for mappings. */
5568 if (xim_expected_char != NUL && xim_ignored_char)
5569 /* We had a keypad key, and XIM tried to thieve it */
5570 return FALSE;
5571
Bram Moolenaar673214b2011-07-27 18:25:44 +02005572 /* This is supposed to fix a problem with iBus, that space
5573 * characters don't work in input mode. */
5574 xim_expected_char = NUL;
5575
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 /* Normal processing */
5577 return imresult;
5578 }
5579 }
5580
5581 return FALSE;
5582}
5583
5584 int
5585im_get_status(void)
5586{
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005587# ifdef FEAT_EVAL
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005588 if (p_imsf[0] != NUL)
5589 {
5590 int is_active;
5591
5592 /* FIXME: Don't execute user function in unsafe situation. */
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005593 if (exiting
5594# ifdef FEAT_AUTOCMD
5595 || is_autocmd_blocked()
5596# endif
5597 )
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005598 return FALSE;
5599 /* FIXME: :py print 'xxx' is shown duplicate result.
5600 * Use silent to avoid it. */
5601 ++msg_silent;
5602 is_active = call_func_retnr(p_imsf, 0, NULL, FALSE);
5603 --msg_silent;
5604 return (is_active > 0);
5605 }
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 return im_is_active;
5608}
5609
Bram Moolenaar64404472010-06-26 06:24:45 +02005610 int
5611preedit_get_status(void)
5612{
5613 return preedit_is_active;
5614}
5615
5616 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005617im_is_preediting(void)
Bram Moolenaar64404472010-06-26 06:24:45 +02005618{
5619 return xim_has_preediting;
5620}
5621
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005622# else /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624static int xim_is_active = FALSE; /* XIM should be active in the current
5625 mode */
5626static int xim_has_focus = FALSE; /* XIM is really being used for Vim */
5627#ifdef FEAT_GUI_X11
5628static XIMStyle input_style;
5629static int status_area_enabled = TRUE;
5630#endif
5631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632/*
5633 * Switch using XIM on/off. This is used by the code that changes "State".
5634 */
5635 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005636im_set_active(int active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 if (xic == NULL)
5639 return;
5640
5641 /* If 'imdisable' is set, XIM is never active. */
5642 if (p_imdisable)
5643 active = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005644#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 else if (input_style & XIMPreeditPosition)
5646 /* There is a problem in switching XIM off when preediting is used,
5647 * and it is not clear how this can be solved. For now, keep XIM on
5648 * all the time, like it was done in Vim 5.8. */
5649 active = TRUE;
5650#endif
5651
5652 /* Remember the active state, it is needed when Vim gets keyboard focus. */
5653 xim_is_active = active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 xim_set_preedit();
5655}
5656
5657/*
5658 * Adjust using XIM for gaining or losing keyboard focus. Also called when
5659 * "xim_is_active" changes.
5660 */
5661 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005662xim_set_focus(int focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 if (xic == NULL)
5665 return;
5666
5667 /*
5668 * XIM only gets focus when the Vim window has keyboard focus and XIM has
5669 * been set active for the current mode.
5670 */
5671 if (focus && xim_is_active)
5672 {
5673 if (!xim_has_focus)
5674 {
5675 xim_has_focus = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 XSetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 }
5679 else
5680 {
5681 if (xim_has_focus)
5682 {
5683 xim_has_focus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 XUnsetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 }
5687}
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005690im_set_position(int row UNUSED, int col UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 xim_set_preedit();
5693}
5694
5695/*
5696 * Set the XIM to the current cursor position.
5697 */
5698 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005699xim_set_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005701 XVaNestedList attr_list;
5702 XRectangle spot_area;
5703 XPoint over_spot;
5704 int line_space;
5705
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02005706 if (xic == NULL)
5707 return;
5708
5709 xim_set_focus(TRUE);
5710
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005711 if (!xim_has_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005713 /* hide XIM cursor */
5714 over_spot.x = 0;
5715 over_spot.y = -100; /* arbitrary invisible position */
5716 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5717 XNSpotLocation,
5718 &over_spot,
5719 NULL);
5720 XSetICValues(xic, XNPreeditAttributes, attr_list, NULL);
5721 XFree(attr_list);
5722 return;
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005725 if (input_style & XIMPreeditPosition)
5726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if (xim_fg_color == INVALCOLOR)
5728 {
5729 xim_fg_color = gui.def_norm_pixel;
5730 xim_bg_color = gui.def_back_pixel;
5731 }
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005732 over_spot.x = TEXT_X(gui.col);
5733 over_spot.y = TEXT_Y(gui.row);
5734 spot_area.x = 0;
5735 spot_area.y = 0;
5736 spot_area.height = gui.char_height * Rows;
5737 spot_area.width = gui.char_width * Columns;
5738 line_space = gui.char_height;
5739 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5740 XNSpotLocation, &over_spot,
5741 XNForeground, (Pixel) xim_fg_color,
5742 XNBackground, (Pixel) xim_bg_color,
5743 XNArea, &spot_area,
5744 XNLineSpace, line_space,
5745 NULL);
5746 if (XSetICValues(xic, XNPreeditAttributes, attr_list, NULL))
5747 EMSG(_("E284: Cannot set IC values"));
5748 XFree(attr_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750}
5751
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005752#if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753static char e_xim[] = N_("E285: Failed to create input context");
5754#endif
5755
5756#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaara899e6e2016-12-03 16:40:51 +01005757# if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && !defined(SUN_SYSTEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758# define USE_X11R6_XIM
5759# endif
5760
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005761static int xim_real_init(Window x11_window, Display *x11_display);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763
5764#ifdef USE_X11R6_XIM
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005765static void xim_destroy_cb(XIM im, XPointer client_data, XPointer call_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005768xim_instantiate_cb(
5769 Display *display,
5770 XPointer client_data UNUSED,
5771 XPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
5773 Window x11_window;
5774 Display *x11_display;
5775
5776#ifdef XIM_DEBUG
5777 xim_log("xim_instantiate_cb()\n");
5778#endif
5779
5780 gui_get_x11_windis(&x11_window, &x11_display);
5781 if (display != x11_display)
5782 return;
5783
5784 xim_real_init(x11_window, x11_display);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005785 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 if (xic != NULL)
5787 XUnregisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5788 xim_instantiate_cb, NULL);
5789}
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005792xim_destroy_cb(
5793 XIM im UNUSED,
5794 XPointer client_data UNUSED,
5795 XPointer call_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 Window x11_window;
5798 Display *x11_display;
5799
5800#ifdef XIM_DEBUG
5801 xim_log("xim_destroy_cb()\n");
5802#endif
5803 gui_get_x11_windis(&x11_window, &x11_display);
5804
5805 xic = NULL;
5806 status_area_enabled = FALSE;
5807
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005808 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
5810 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5811 xim_instantiate_cb, NULL);
5812}
5813#endif
5814
5815 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005816xim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 Window x11_window;
5819 Display *x11_display;
5820
5821#ifdef XIM_DEBUG
5822 xim_log("xim_init()\n");
5823#endif
5824
5825 gui_get_x11_windis(&x11_window, &x11_display);
5826
5827 xic = NULL;
5828
5829 if (xim_real_init(x11_window, x11_display))
5830 return;
5831
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005832 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834#ifdef USE_X11R6_XIM
5835 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5836 xim_instantiate_cb, NULL);
5837#endif
5838}
5839
5840 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005841xim_real_init(Window x11_window, Display *x11_display)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 int i;
5844 char *p,
5845 *s,
5846 *ns,
5847 *end,
5848 tmp[1024];
5849#define IMLEN_MAX 40
5850 char buf[IMLEN_MAX + 7];
5851 XIM xim = NULL;
5852 XIMStyles *xim_styles;
5853 XIMStyle this_input_style = 0;
5854 Boolean found;
5855 XPoint over_spot;
5856 XVaNestedList preedit_list, status_list;
5857
5858 input_style = 0;
5859 status_area_enabled = FALSE;
5860
5861 if (xic != NULL)
5862 return FALSE;
5863
5864 if (gui.rsrc_input_method != NULL && *gui.rsrc_input_method != NUL)
5865 {
5866 strcpy(tmp, gui.rsrc_input_method);
5867 for (ns = s = tmp; ns != NULL && *s != NUL;)
5868 {
5869 s = (char *)skipwhite((char_u *)s);
5870 if (*s == NUL)
5871 break;
5872 if ((ns = end = strchr(s, ',')) == NULL)
5873 end = s + strlen(s);
5874 while (isspace(((char_u *)end)[-1]))
5875 end--;
5876 *end = NUL;
5877
5878 if (strlen(s) <= IMLEN_MAX)
5879 {
5880 strcpy(buf, "@im=");
5881 strcat(buf, s);
5882 if ((p = XSetLocaleModifiers(buf)) != NULL && *p != NUL
5883 && (xim = XOpenIM(x11_display, NULL, NULL, NULL))
5884 != NULL)
5885 break;
5886 }
5887
5888 s = ns + 1;
5889 }
5890 }
5891
5892 if (xim == NULL && (p = XSetLocaleModifiers("")) != NULL && *p != NUL)
5893 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5894
5895 /* This is supposed to be useful to obtain characters through
5896 * XmbLookupString() without really using a XIM. */
5897 if (xim == NULL && (p = XSetLocaleModifiers("@im=none")) != NULL
5898 && *p != NUL)
5899 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5900
5901 if (xim == NULL)
5902 {
5903 /* Only give this message when verbose is set, because too many people
5904 * got this message when they didn't want to use a XIM. */
5905 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005906 {
5907 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 EMSG(_("E286: Failed to open input method"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005909 verbose_leave();
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 return FALSE;
5912 }
5913
5914#ifdef USE_X11R6_XIM
5915 {
5916 XIMCallback destroy_cb;
5917
5918 destroy_cb.callback = xim_destroy_cb;
5919 destroy_cb.client_data = NULL;
5920 if (XSetIMValues(xim, XNDestroyCallback, &destroy_cb, NULL))
5921 EMSG(_("E287: Warning: Could not set destroy callback to IM"));
5922 }
5923#endif
5924
5925 if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL) || !xim_styles)
5926 {
5927 EMSG(_("E288: input method doesn't support any style"));
5928 XCloseIM(xim);
5929 return FALSE;
5930 }
5931
5932 found = False;
5933 strcpy(tmp, gui.rsrc_preedit_type_name);
5934 for (s = tmp; s && !found; )
5935 {
5936 while (*s && isspace((unsigned char)*s))
5937 s++;
5938 if (!*s)
5939 break;
5940 if ((ns = end = strchr(s, ',')) != 0)
5941 ns++;
5942 else
5943 end = s + strlen(s);
5944 while (isspace((unsigned char)*end))
5945 end--;
5946 *end = '\0';
5947
5948 if (!strcmp(s, "OverTheSpot"))
5949 this_input_style = (XIMPreeditPosition | XIMStatusArea);
5950 else if (!strcmp(s, "OffTheSpot"))
5951 this_input_style = (XIMPreeditArea | XIMStatusArea);
5952 else if (!strcmp(s, "Root"))
5953 this_input_style = (XIMPreeditNothing | XIMStatusNothing);
5954
5955 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5956 {
5957 if (this_input_style == xim_styles->supported_styles[i])
5958 {
5959 found = True;
5960 break;
5961 }
5962 }
5963 if (!found)
5964 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5965 {
5966 if ((xim_styles->supported_styles[i] & this_input_style)
5967 == (this_input_style & ~XIMStatusArea))
5968 {
5969 this_input_style &= ~XIMStatusArea;
5970 found = True;
5971 break;
5972 }
5973 }
5974
5975 s = ns;
5976 }
5977 XFree(xim_styles);
5978
5979 if (!found)
5980 {
5981 /* Only give this message when verbose is set, because too many people
5982 * got this message when they didn't want to use a XIM. */
5983 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005984 {
5985 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 EMSG(_("E289: input method doesn't support my preedit type"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005987 verbose_leave();
5988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 XCloseIM(xim);
5990 return FALSE;
5991 }
5992
5993 over_spot.x = TEXT_X(gui.col);
5994 over_spot.y = TEXT_Y(gui.row);
5995 input_style = this_input_style;
5996
5997 /* A crash was reported when trying to pass gui.norm_font as XNFontSet,
5998 * thus that has been removed. Hopefully the default works... */
5999#ifdef FEAT_XFONTSET
6000 if (gui.fontset != NOFONTSET)
6001 {
6002 preedit_list = XVaCreateNestedList(0,
6003 XNSpotLocation, &over_spot,
6004 XNForeground, (Pixel)gui.def_norm_pixel,
6005 XNBackground, (Pixel)gui.def_back_pixel,
6006 XNFontSet, (XFontSet)gui.fontset,
6007 NULL);
6008 status_list = XVaCreateNestedList(0,
6009 XNForeground, (Pixel)gui.def_norm_pixel,
6010 XNBackground, (Pixel)gui.def_back_pixel,
6011 XNFontSet, (XFontSet)gui.fontset,
6012 NULL);
6013 }
6014 else
6015#endif
6016 {
6017 preedit_list = XVaCreateNestedList(0,
6018 XNSpotLocation, &over_spot,
6019 XNForeground, (Pixel)gui.def_norm_pixel,
6020 XNBackground, (Pixel)gui.def_back_pixel,
6021 NULL);
6022 status_list = XVaCreateNestedList(0,
6023 XNForeground, (Pixel)gui.def_norm_pixel,
6024 XNBackground, (Pixel)gui.def_back_pixel,
6025 NULL);
6026 }
6027
6028 xic = XCreateIC(xim,
6029 XNInputStyle, input_style,
6030 XNClientWindow, x11_window,
6031 XNFocusWindow, gui.wid,
6032 XNPreeditAttributes, preedit_list,
6033 XNStatusAttributes, status_list,
6034 NULL);
6035 XFree(status_list);
6036 XFree(preedit_list);
6037 if (xic != NULL)
6038 {
6039 if (input_style & XIMStatusArea)
6040 {
6041 xim_set_status_area();
6042 status_area_enabled = TRUE;
6043 }
6044 else
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00006045 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 }
6047 else
6048 {
6049 EMSG(_(e_xim));
6050 XCloseIM(xim);
6051 return FALSE;
6052 }
6053
6054 return TRUE;
6055}
6056
6057#endif /* FEAT_GUI_X11 */
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059/*
6060 * Get IM status. When IM is on, return TRUE. Else return FALSE.
6061 * FIXME: This doesn't work correctly: Having focus doesn't always mean XIM is
6062 * active, when not having focus XIM may still be active (e.g., when using a
6063 * tear-off menu item).
6064 */
6065 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006066im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 return xim_has_focus;
6069}
6070
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02006071# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
Bram Moolenaar64404472010-06-26 06:24:45 +02006073# if !defined(FEAT_GUI_GTK) || defined(PROTO)
6074/*
6075 * Set up the status area.
6076 *
6077 * This should use a separate Widget, but that seems not possible, because
6078 * preedit_area and status_area should be set to the same window as for the
6079 * text input. Unfortunately this means the status area pollutes the text
6080 * window...
6081 */
6082 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006083xim_set_status_area(void)
Bram Moolenaarc236c162008-07-13 17:41:49 +00006084{
Bram Moolenaar64404472010-06-26 06:24:45 +02006085 XVaNestedList preedit_list = 0, status_list = 0, list = 0;
6086 XRectangle pre_area, status_area;
6087
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02006088 if (xic == NULL)
6089 return;
6090
Bram Moolenaar64404472010-06-26 06:24:45 +02006091 if (input_style & XIMStatusArea)
6092 {
6093 if (input_style & XIMPreeditArea)
6094 {
6095 XRectangle *needed_rect;
6096
6097 /* to get status_area width */
6098 status_list = XVaCreateNestedList(0, XNAreaNeeded,
6099 &needed_rect, NULL);
6100 XGetICValues(xic, XNStatusAttributes, status_list, NULL);
6101 XFree(status_list);
6102
6103 status_area.width = needed_rect->width;
6104 }
6105 else
6106 status_area.width = gui.char_width * Columns;
6107
6108 status_area.x = 0;
6109 status_area.y = gui.char_height * Rows + gui.border_offset;
6110 if (gui.which_scrollbars[SBAR_BOTTOM])
6111 status_area.y += gui.scrollbar_height;
6112#ifdef FEAT_MENU
6113 if (gui.menu_is_active)
6114 status_area.y += gui.menu_height;
6115#endif
6116 status_area.height = gui.char_height;
6117 status_list = XVaCreateNestedList(0, XNArea, &status_area, NULL);
6118 }
6119 else
6120 {
6121 status_area.x = 0;
6122 status_area.y = gui.char_height * Rows + gui.border_offset;
6123 if (gui.which_scrollbars[SBAR_BOTTOM])
6124 status_area.y += gui.scrollbar_height;
6125#ifdef FEAT_MENU
6126 if (gui.menu_is_active)
6127 status_area.y += gui.menu_height;
6128#endif
6129 status_area.width = 0;
6130 status_area.height = gui.char_height;
6131 }
6132
6133 if (input_style & XIMPreeditArea) /* off-the-spot */
6134 {
6135 pre_area.x = status_area.x + status_area.width;
6136 pre_area.y = gui.char_height * Rows + gui.border_offset;
6137 pre_area.width = gui.char_width * Columns - pre_area.x;
6138 if (gui.which_scrollbars[SBAR_BOTTOM])
6139 pre_area.y += gui.scrollbar_height;
6140#ifdef FEAT_MENU
6141 if (gui.menu_is_active)
6142 pre_area.y += gui.menu_height;
6143#endif
6144 pre_area.height = gui.char_height;
6145 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
6146 }
6147 else if (input_style & XIMPreeditPosition) /* over-the-spot */
6148 {
6149 pre_area.x = 0;
6150 pre_area.y = 0;
6151 pre_area.height = gui.char_height * Rows;
6152 pre_area.width = gui.char_width * Columns;
6153 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
6154 }
6155
6156 if (preedit_list && status_list)
6157 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
6158 XNStatusAttributes, status_list, NULL);
6159 else if (preedit_list)
6160 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
6161 NULL);
6162 else if (status_list)
6163 list = XVaCreateNestedList(0, XNStatusAttributes, status_list,
6164 NULL);
6165 else
6166 list = NULL;
6167
6168 if (list)
6169 {
6170 XSetICValues(xic, XNVaNestedList, list, NULL);
6171 XFree(list);
6172 }
6173 if (status_list)
6174 XFree(status_list);
6175 if (preedit_list)
6176 XFree(preedit_list);
6177}
6178
6179 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006180xim_get_status_area_height(void)
Bram Moolenaar64404472010-06-26 06:24:45 +02006181{
6182 if (status_area_enabled)
6183 return gui.char_height;
6184 return 0;
Bram Moolenaarc236c162008-07-13 17:41:49 +00006185}
6186# endif
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188#endif /* FEAT_XIM */
6189
6190#if defined(FEAT_MBYTE) || defined(PROTO)
6191
6192/*
6193 * Setup "vcp" for conversion from "from" to "to".
6194 * The names must have been made canonical with enc_canonize().
6195 * vcp->vc_type must have been initialized to CONV_NONE.
6196 * Note: cannot be used for conversion from/to ucs-2 and ucs-4 (will use utf-8
6197 * instead).
6198 * Afterwards invoke with "from" and "to" equal to NULL to cleanup.
6199 * Return FAIL when conversion is not supported, OK otherwise.
6200 */
6201 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006202convert_setup(vimconv_T *vcp, char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006204 return convert_setup_ext(vcp, from, TRUE, to, TRUE);
6205}
6206
6207/*
6208 * As convert_setup(), but only when from_unicode_is_utf8 is TRUE will all
6209 * "from" unicode charsets be considered utf-8. Same for "to".
6210 */
6211 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006212convert_setup_ext(
6213 vimconv_T *vcp,
6214 char_u *from,
6215 int from_unicode_is_utf8,
6216 char_u *to,
6217 int to_unicode_is_utf8)
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006218{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 int from_prop;
6220 int to_prop;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006221 int from_is_utf8;
6222 int to_is_utf8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
6224 /* Reset to no conversion. */
6225# ifdef USE_ICONV
6226 if (vcp->vc_type == CONV_ICONV && vcp->vc_fd != (iconv_t)-1)
6227 iconv_close(vcp->vc_fd);
6228# endif
6229 vcp->vc_type = CONV_NONE;
6230 vcp->vc_factor = 1;
6231 vcp->vc_fail = FALSE;
6232
6233 /* No conversion when one of the names is empty or they are equal. */
6234 if (from == NULL || *from == NUL || to == NULL || *to == NUL
6235 || STRCMP(from, to) == 0)
6236 return OK;
6237
6238 from_prop = enc_canon_props(from);
6239 to_prop = enc_canon_props(to);
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006240 if (from_unicode_is_utf8)
6241 from_is_utf8 = from_prop & ENC_UNICODE;
6242 else
6243 from_is_utf8 = from_prop == ENC_UNICODE;
6244 if (to_unicode_is_utf8)
6245 to_is_utf8 = to_prop & ENC_UNICODE;
6246 else
6247 to_is_utf8 = to_prop == ENC_UNICODE;
6248
6249 if ((from_prop & ENC_LATIN1) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 /* Internal latin1 -> utf-8 conversion. */
6252 vcp->vc_type = CONV_TO_UTF8;
6253 vcp->vc_factor = 2; /* up to twice as long */
6254 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006255 else if ((from_prop & ENC_LATIN9) && to_is_utf8)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006256 {
6257 /* Internal latin9 -> utf-8 conversion. */
6258 vcp->vc_type = CONV_9_TO_UTF8;
6259 vcp->vc_factor = 3; /* up to three as long (euro sign) */
6260 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006261 else if (from_is_utf8 && (to_prop & ENC_LATIN1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 /* Internal utf-8 -> latin1 conversion. */
6264 vcp->vc_type = CONV_TO_LATIN1;
6265 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006266 else if (from_is_utf8 && (to_prop & ENC_LATIN9))
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006267 {
6268 /* Internal utf-8 -> latin9 conversion. */
6269 vcp->vc_type = CONV_TO_LATIN9;
6270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271#ifdef WIN3264
6272 /* Win32-specific codepage <-> codepage conversion without iconv. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006273 else if ((from_is_utf8 || encname2codepage(from) > 0)
6274 && (to_is_utf8 || encname2codepage(to) > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 {
6276 vcp->vc_type = CONV_CODEPAGE;
6277 vcp->vc_factor = 2; /* up to twice as long */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006278 vcp->vc_cpfrom = from_is_utf8 ? 0 : encname2codepage(from);
6279 vcp->vc_cpto = to_is_utf8 ? 0 : encname2codepage(to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281#endif
6282#ifdef MACOS_X
6283 else if ((from_prop & ENC_MACROMAN) && (to_prop & ENC_LATIN1))
6284 {
6285 vcp->vc_type = CONV_MAC_LATIN1;
6286 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006287 else if ((from_prop & ENC_MACROMAN) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
6289 vcp->vc_type = CONV_MAC_UTF8;
6290 vcp->vc_factor = 2; /* up to twice as long */
6291 }
6292 else if ((from_prop & ENC_LATIN1) && (to_prop & ENC_MACROMAN))
6293 {
6294 vcp->vc_type = CONV_LATIN1_MAC;
6295 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006296 else if (from_is_utf8 && (to_prop & ENC_MACROMAN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 {
6298 vcp->vc_type = CONV_UTF8_MAC;
6299 }
6300#endif
6301# ifdef USE_ICONV
6302 else
6303 {
6304 /* Use iconv() for conversion. */
6305 vcp->vc_fd = (iconv_t)my_iconv_open(
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006306 to_is_utf8 ? (char_u *)"utf-8" : to,
6307 from_is_utf8 ? (char_u *)"utf-8" : from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (vcp->vc_fd != (iconv_t)-1)
6309 {
6310 vcp->vc_type = CONV_ICONV;
6311 vcp->vc_factor = 4; /* could be longer too... */
6312 }
6313 }
6314# endif
6315 if (vcp->vc_type == CONV_NONE)
6316 return FAIL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 return OK;
6319}
6320
6321#if defined(FEAT_GUI) || defined(AMIGA) || defined(WIN3264) \
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006322 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323/*
6324 * Do conversion on typed input characters in-place.
6325 * The input and output are not NUL terminated!
6326 * Returns the length after conversion.
6327 */
6328 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006329convert_input(char_u *ptr, int len, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 return convert_input_safe(ptr, len, maxlen, NULL, NULL);
6332}
6333#endif
6334
6335/*
6336 * Like convert_input(), but when there is an incomplete byte sequence at the
6337 * end return that as an allocated string in "restp" and set "*restlenp" to
6338 * the length. If "restp" is NULL it is not used.
6339 */
6340 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006341convert_input_safe(
6342 char_u *ptr,
6343 int len,
6344 int maxlen,
6345 char_u **restp,
6346 int *restlenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 char_u *d;
6349 int dlen = len;
6350 int unconvertlen = 0;
6351
6352 d = string_convert_ext(&input_conv, ptr, &dlen,
6353 restp == NULL ? NULL : &unconvertlen);
6354 if (d != NULL)
6355 {
6356 if (dlen <= maxlen)
6357 {
6358 if (unconvertlen > 0)
6359 {
6360 /* Move the unconverted characters to allocated memory. */
6361 *restp = alloc(unconvertlen);
6362 if (*restp != NULL)
6363 mch_memmove(*restp, ptr + len - unconvertlen, unconvertlen);
6364 *restlenp = unconvertlen;
6365 }
6366 mch_memmove(ptr, d, dlen);
6367 }
6368 else
6369 /* result is too long, keep the unconverted text (the caller must
6370 * have done something wrong!) */
6371 dlen = len;
6372 vim_free(d);
6373 }
6374 return dlen;
6375}
6376
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377/*
6378 * Convert text "ptr[*lenp]" according to "vcp".
6379 * Returns the result in allocated memory and sets "*lenp".
6380 * When "lenp" is NULL, use NUL terminated strings.
6381 * Illegal chars are often changed to "?", unless vcp->vc_fail is set.
6382 * When something goes wrong, NULL is returned and "*lenp" is unchanged.
6383 */
6384 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006385string_convert(
6386 vimconv_T *vcp,
6387 char_u *ptr,
6388 int *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
6390 return string_convert_ext(vcp, ptr, lenp, NULL);
6391}
6392
6393/*
6394 * Like string_convert(), but when "unconvlenp" is not NULL and there are is
6395 * an incomplete sequence at the end it is not converted and "*unconvlenp" is
6396 * set to the number of remaining bytes.
6397 */
6398 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01006399string_convert_ext(
6400 vimconv_T *vcp,
6401 char_u *ptr,
6402 int *lenp,
6403 int *unconvlenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 char_u *retval = NULL;
6406 char_u *d;
6407 int len;
6408 int i;
6409 int l;
6410 int c;
6411
6412 if (lenp == NULL)
6413 len = (int)STRLEN(ptr);
6414 else
6415 len = *lenp;
6416 if (len == 0)
6417 return vim_strsave((char_u *)"");
6418
6419 switch (vcp->vc_type)
6420 {
6421 case CONV_TO_UTF8: /* latin1 to utf-8 conversion */
6422 retval = alloc(len * 2 + 1);
6423 if (retval == NULL)
6424 break;
6425 d = retval;
6426 for (i = 0; i < len; ++i)
6427 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006428 c = ptr[i];
6429 if (c < 0x80)
6430 *d++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 else
6432 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006433 *d++ = 0xc0 + ((unsigned)c >> 6);
6434 *d++ = 0x80 + (c & 0x3f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 }
6436 }
6437 *d = NUL;
6438 if (lenp != NULL)
6439 *lenp = (int)(d - retval);
6440 break;
6441
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006442 case CONV_9_TO_UTF8: /* latin9 to utf-8 conversion */
6443 retval = alloc(len * 3 + 1);
6444 if (retval == NULL)
6445 break;
6446 d = retval;
6447 for (i = 0; i < len; ++i)
6448 {
6449 c = ptr[i];
6450 switch (c)
6451 {
6452 case 0xa4: c = 0x20ac; break; /* euro */
6453 case 0xa6: c = 0x0160; break; /* S hat */
6454 case 0xa8: c = 0x0161; break; /* S -hat */
6455 case 0xb4: c = 0x017d; break; /* Z hat */
6456 case 0xb8: c = 0x017e; break; /* Z -hat */
6457 case 0xbc: c = 0x0152; break; /* OE */
6458 case 0xbd: c = 0x0153; break; /* oe */
6459 case 0xbe: c = 0x0178; break; /* Y */
6460 }
6461 d += utf_char2bytes(c, d);
6462 }
6463 *d = NUL;
6464 if (lenp != NULL)
6465 *lenp = (int)(d - retval);
6466 break;
6467
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 case CONV_TO_LATIN1: /* utf-8 to latin1 conversion */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006469 case CONV_TO_LATIN9: /* utf-8 to latin9 conversion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 retval = alloc(len + 1);
6471 if (retval == NULL)
6472 break;
6473 d = retval;
6474 for (i = 0; i < len; ++i)
6475 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006476 l = utf_ptr2len_len(ptr + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (l == 0)
6478 *d++ = NUL;
6479 else if (l == 1)
6480 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006481 int l_w = utf8len_tab_zero[ptr[i]];
6482
6483 if (l_w == 0)
6484 {
6485 /* Illegal utf-8 byte cannot be converted */
6486 vim_free(retval);
6487 return NULL;
6488 }
6489 if (unconvlenp != NULL && l_w > len - i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 {
6491 /* Incomplete sequence at the end. */
6492 *unconvlenp = len - i;
6493 break;
6494 }
6495 *d++ = ptr[i];
6496 }
6497 else
6498 {
6499 c = utf_ptr2char(ptr + i);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006500 if (vcp->vc_type == CONV_TO_LATIN9)
6501 switch (c)
6502 {
6503 case 0x20ac: c = 0xa4; break; /* euro */
6504 case 0x0160: c = 0xa6; break; /* S hat */
6505 case 0x0161: c = 0xa8; break; /* S -hat */
6506 case 0x017d: c = 0xb4; break; /* Z hat */
6507 case 0x017e: c = 0xb8; break; /* Z -hat */
6508 case 0x0152: c = 0xbc; break; /* OE */
6509 case 0x0153: c = 0xbd; break; /* oe */
6510 case 0x0178: c = 0xbe; break; /* Y */
6511 case 0xa4:
6512 case 0xa6:
6513 case 0xa8:
6514 case 0xb4:
6515 case 0xb8:
6516 case 0xbc:
6517 case 0xbd:
6518 case 0xbe: c = 0x100; break; /* not in latin9 */
6519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (!utf_iscomposing(c)) /* skip composing chars */
6521 {
6522 if (c < 0x100)
6523 *d++ = c;
6524 else if (vcp->vc_fail)
6525 {
6526 vim_free(retval);
6527 return NULL;
6528 }
6529 else
6530 {
6531 *d++ = 0xbf;
6532 if (utf_char2cells(c) > 1)
6533 *d++ = '?';
6534 }
6535 }
6536 i += l - 1;
6537 }
6538 }
6539 *d = NUL;
6540 if (lenp != NULL)
6541 *lenp = (int)(d - retval);
6542 break;
6543
Bram Moolenaar56718732006-03-15 22:53:57 +00006544# ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 case CONV_MAC_LATIN1:
6546 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006547 'm', 'l', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 break;
6549
6550 case CONV_LATIN1_MAC:
6551 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006552 'l', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 break;
6554
6555 case CONV_MAC_UTF8:
6556 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006557 'm', 'u', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 break;
6559
6560 case CONV_UTF8_MAC:
6561 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006562 'u', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 break;
6564# endif
6565
6566# ifdef USE_ICONV
6567 case CONV_ICONV: /* conversion with output_conv.vc_fd */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006568 retval = iconv_string(vcp, ptr, len, unconvlenp, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 break;
6570# endif
6571# ifdef WIN3264
6572 case CONV_CODEPAGE: /* codepage -> codepage */
6573 {
6574 int retlen;
6575 int tmp_len;
6576 short_u *tmp;
6577
6578 /* 1. codepage/UTF-8 -> ucs-2. */
6579 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006580 tmp_len = utf8_to_utf16(ptr, len, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 else
Bram Moolenaarffeedec2013-02-13 16:49:58 +01006582 {
6583 tmp_len = MultiByteToWideChar(vcp->vc_cpfrom,
6584 unconvlenp ? MB_ERR_INVALID_CHARS : 0,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006585 (char *)ptr, len, 0, 0);
Bram Moolenaarffeedec2013-02-13 16:49:58 +01006586 if (tmp_len == 0
6587 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION)
6588 {
6589 if (lenp != NULL)
6590 *lenp = 0;
6591 if (unconvlenp != NULL)
6592 *unconvlenp = len;
6593 retval = alloc(1);
6594 if (retval)
6595 retval[0] = NUL;
6596 return retval;
6597 }
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 tmp = (short_u *)alloc(sizeof(short_u) * tmp_len);
6600 if (tmp == NULL)
6601 break;
6602 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006603 utf8_to_utf16(ptr, len, tmp, unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006605 MultiByteToWideChar(vcp->vc_cpfrom, 0,
6606 (char *)ptr, len, tmp, tmp_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608 /* 2. ucs-2 -> codepage/UTF-8. */
6609 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006610 retlen = utf16_to_utf8(tmp, tmp_len, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 else
6612 retlen = WideCharToMultiByte(vcp->vc_cpto, 0,
6613 tmp, tmp_len, 0, 0, 0, 0);
6614 retval = alloc(retlen + 1);
6615 if (retval != NULL)
6616 {
6617 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006618 utf16_to_utf8(tmp, tmp_len, retval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 else
6620 WideCharToMultiByte(vcp->vc_cpto, 0,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006621 tmp, tmp_len,
6622 (char *)retval, retlen, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 retval[retlen] = NUL;
6624 if (lenp != NULL)
6625 *lenp = retlen;
6626 }
6627 vim_free(tmp);
6628 break;
6629 }
6630# endif
6631 }
6632
6633 return retval;
6634}
6635#endif