blob: 44cbd5f82ba1f4b085c46050cd28f77ee3020877 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Multibyte extensions partly by Sung-Hoon Baek
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * mbyte.c: Code specifically for handling multi-byte characters.
12 *
13 * The encoding used in the core is set with 'encoding'. When 'encoding' is
14 * changed, the following four variables are set (for speed).
15 * Currently these types of character encodings are supported:
16 *
17 * "enc_dbcs" When non-zero it tells the type of double byte character
18 * encoding (Chinese, Korean, Japanese, etc.).
19 * The cell width on the display is equal to the number of
20 * bytes. (exception: DBCS_JPNU with first byte 0x8e)
21 * Recognizing the first or second byte is difficult, it
22 * requires checking a byte sequence from the start.
23 * "enc_utf8" When TRUE use Unicode characters in UTF-8 encoding.
24 * The cell width on the display needs to be determined from
25 * the character value.
26 * Recognizing bytes is easy: 0xxx.xxxx is a single-byte
27 * char, 10xx.xxxx is a trailing byte, 11xx.xxxx is a leading
28 * byte of a multi-byte character.
Bram Moolenaar3e8cb582010-01-12 19:52:03 +010029 * To make things complicated, up to six composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * are allowed. These are drawn on top of the first char.
31 * For most editing the sequence of bytes with composing
32 * characters included is considered to be one character.
33 * "enc_unicode" When 2 use 16-bit Unicode characters (or UTF-16).
34 * When 4 use 32-but Unicode characters.
35 * Internally characters are stored in UTF-8 encoding to
36 * avoid NUL bytes. Conversion happens when doing I/O.
37 * "enc_utf8" will also be TRUE.
38 *
39 * "has_mbyte" is set when "enc_dbcs" or "enc_utf8" is non-zero.
40 *
41 * If none of these is TRUE, 8-bit bytes are used for a character. The
42 * encoding isn't currently specified (TODO).
43 *
44 * 'encoding' specifies the encoding used in the core. This is in registers,
45 * text manipulation, buffers, etc. Conversion has to be done when characters
46 * in another encoding are received or send:
47 *
48 * clipboard
49 * ^
50 * | (2)
51 * V
52 * +---------------+
53 * (1) | | (3)
54 * keyboard ----->| core |-----> display
55 * | |
56 * +---------------+
57 * ^
58 * | (4)
59 * V
60 * file
61 *
62 * (1) Typed characters arrive in the current locale. Conversion is to be
63 * done when 'encoding' is different from 'termencoding'.
64 * (2) Text will be made available with the encoding specified with
65 * 'encoding'. If this is not sufficient, system-specific conversion
66 * might be required.
67 * (3) For the GUI the correct font must be selected, no conversion done.
68 * Otherwise, conversion is to be done when 'encoding' differs from
69 * 'termencoding'. (Different in the GTK+ 2 port -- 'termencoding'
70 * is always used for both input and output and must always be set to
71 * "utf-8". gui_mch_init() does this automatically.)
72 * (4) The encoding of the file is specified with 'fileencoding'. Conversion
73 * is to be done when it's different from 'encoding'.
74 *
75 * The viminfo file is a special case: Only text is converted, not file names.
76 * Vim scripts may contain an ":encoding" command. This has an effect for
77 * some commands, like ":menutrans"
78 */
79
80#include "vim.h"
81
82#ifdef WIN32UNIX
83# ifndef WIN32_LEAN_AND_MEAN
84# define WIN32_LEAN_AND_MEAN
85# endif
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 Moolenaar071d4272004-06-13 20:20:40 +0000112# include <gdk/gdkkeysyms.h>
113# ifdef WIN3264
114# include <gdk/gdkwin32.h>
115# else
116# include <gdk/gdkx.h>
117# endif
118#endif
119
120#ifdef HAVE_WCHAR_H
121# include <wchar.h>
122#endif
123
124#if 0
125/* This has been disabled, because several people reported problems with the
126 * wcwidth() and iswprint() library functions, esp. for Hebrew. */
127# ifdef __STDC_ISO_10646__
128# define USE_WCHAR_FUNCTIONS
129# endif
130#endif
131
132#if defined(FEAT_MBYTE) || defined(PROTO)
133
134static int enc_canon_search __ARGS((char_u *name));
135static int dbcs_char2len __ARGS((int c));
136static int dbcs_char2bytes __ARGS((int c, char_u *buf));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000137static int dbcs_ptr2len __ARGS((char_u *p));
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000138static int dbcs_ptr2len_len __ARGS((char_u *p, int size));
139static int utf_ptr2cells_len __ARGS((char_u *p, int size));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static int dbcs_char2cells __ARGS((int c));
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000141static int dbcs_ptr2cells_len __ARGS((char_u *p, int size));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142static int dbcs_ptr2char __ARGS((char_u *p));
Bram Moolenaar35ee4522011-07-15 21:16:59 +0200143static int utf_safe_read_char_adv __ARGS((char_u **s, size_t *n));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar24397332009-12-02 14:02:39 +0000145/*
146 * Lookup table to quickly get the length in bytes of a UTF-8 character from
147 * the first byte of a UTF-8 string.
148 * Bytes which are illegal when used as the first byte have a 1.
149 * The NUL byte has length 1.
150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static char utf8len_tab[256] =
152{
153 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,
154 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,
155 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,
156 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 +0000157 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,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 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,
160 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,
161};
162
163/*
Bram Moolenaar24397332009-12-02 14:02:39 +0000164 * Like utf8len_tab above, but using a zero for illegal lead bytes.
165 */
166static char utf8len_tab_zero[256] =
167{
168 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,
169 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,
170 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,
171 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,
172 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,
173 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,
174 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,
175 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,
176};
177
178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 * XIM often causes trouble. Define XIM_DEBUG to get a log of XIM callbacks
180 * in the "xim.log" file.
181 */
182/* #define XIM_DEBUG */
183#ifdef XIM_DEBUG
184 static void
185xim_log(char *s, ...)
186{
187 va_list arglist;
188 static FILE *fd = NULL;
189
190 if (fd == (FILE *)-1)
191 return;
192 if (fd == NULL)
193 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000194 fd = mch_fopen("xim.log", "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (fd == NULL)
196 {
197 EMSG("Cannot open xim.log");
198 fd = (FILE *)-1;
199 return;
200 }
201 }
202
203 va_start(arglist, s);
204 vfprintf(fd, s, arglist);
205 va_end(arglist);
206}
207#endif
208
209#endif
210
211#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
212/*
213 * Canonical encoding names and their properties.
214 * "iso-8859-n" is handled by enc_canonize() directly.
215 */
216static struct
217{ char *name; int prop; int codepage;}
218enc_canon_table[] =
219{
220#define IDX_LATIN_1 0
221 {"latin1", ENC_8BIT + ENC_LATIN1, 1252},
222#define IDX_ISO_2 1
223 {"iso-8859-2", ENC_8BIT, 0},
224#define IDX_ISO_3 2
225 {"iso-8859-3", ENC_8BIT, 0},
226#define IDX_ISO_4 3
227 {"iso-8859-4", ENC_8BIT, 0},
228#define IDX_ISO_5 4
229 {"iso-8859-5", ENC_8BIT, 0},
230#define IDX_ISO_6 5
231 {"iso-8859-6", ENC_8BIT, 0},
232#define IDX_ISO_7 6
233 {"iso-8859-7", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000234#define IDX_ISO_8 7
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {"iso-8859-8", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000236#define IDX_ISO_9 8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {"iso-8859-9", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000238#define IDX_ISO_10 9
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {"iso-8859-10", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000240#define IDX_ISO_11 10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {"iso-8859-11", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000242#define IDX_ISO_13 11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {"iso-8859-13", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000244#define IDX_ISO_14 12
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {"iso-8859-14", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000246#define IDX_ISO_15 13
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000247 {"iso-8859-15", ENC_8BIT + ENC_LATIN9, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000248#define IDX_KOI8_R 14
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {"koi8-r", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000250#define IDX_KOI8_U 15
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 {"koi8-u", ENC_8BIT, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000252#define IDX_UTF8 16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {"utf-8", ENC_UNICODE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000254#define IDX_UCS2 17
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {"ucs-2", ENC_UNICODE + ENC_ENDIAN_B + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000256#define IDX_UCS2LE 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {"ucs-2le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000258#define IDX_UTF16 19
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 {"utf-16", ENC_UNICODE + ENC_ENDIAN_B + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000260#define IDX_UTF16LE 20
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {"utf-16le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2WORD, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000262#define IDX_UCS4 21
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {"ucs-4", ENC_UNICODE + ENC_ENDIAN_B + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000264#define IDX_UCS4LE 22
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {"ucs-4le", ENC_UNICODE + ENC_ENDIAN_L + ENC_4BYTE, 0},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000266
267 /* For debugging DBCS encoding on Unix. */
268#define IDX_DEBUG 23
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {"debug", ENC_DBCS, DBCS_DEBUG},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000270#define IDX_EUC_JP 24
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {"euc-jp", ENC_DBCS, DBCS_JPNU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000272#define IDX_SJIS 25
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 {"sjis", ENC_DBCS, DBCS_JPN},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000274#define IDX_EUC_KR 26
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 {"euc-kr", ENC_DBCS, DBCS_KORU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000276#define IDX_EUC_CN 27
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {"euc-cn", ENC_DBCS, DBCS_CHSU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000278#define IDX_EUC_TW 28
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {"euc-tw", ENC_DBCS, DBCS_CHTU},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000280#define IDX_BIG5 29
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 {"big5", ENC_DBCS, DBCS_CHT},
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000282
283 /* MS-DOS and MS-Windows codepages are included here, so that they can be
284 * used on Unix too. Most of them are similar to ISO-8859 encodings, but
285 * not exactly the same. */
286#define IDX_CP437 30
287 {"cp437", ENC_8BIT, 437}, /* like iso-8859-1 */
288#define IDX_CP737 31
289 {"cp737", ENC_8BIT, 737}, /* like iso-8859-7 */
290#define IDX_CP775 32
291 {"cp775", ENC_8BIT, 775}, /* Baltic */
292#define IDX_CP850 33
293 {"cp850", ENC_8BIT, 850}, /* like iso-8859-4 */
294#define IDX_CP852 34
295 {"cp852", ENC_8BIT, 852}, /* like iso-8859-1 */
296#define IDX_CP855 35
297 {"cp855", ENC_8BIT, 855}, /* like iso-8859-2 */
298#define IDX_CP857 36
299 {"cp857", ENC_8BIT, 857}, /* like iso-8859-5 */
300#define IDX_CP860 37
301 {"cp860", ENC_8BIT, 860}, /* like iso-8859-9 */
302#define IDX_CP861 38
303 {"cp861", ENC_8BIT, 861}, /* like iso-8859-1 */
304#define IDX_CP862 39
305 {"cp862", ENC_8BIT, 862}, /* like iso-8859-1 */
306#define IDX_CP863 40
307 {"cp863", ENC_8BIT, 863}, /* like iso-8859-8 */
308#define IDX_CP865 41
309 {"cp865", ENC_8BIT, 865}, /* like iso-8859-1 */
310#define IDX_CP866 42
311 {"cp866", ENC_8BIT, 866}, /* like iso-8859-5 */
312#define IDX_CP869 43
313 {"cp869", ENC_8BIT, 869}, /* like iso-8859-7 */
314#define IDX_CP874 44
315 {"cp874", ENC_8BIT, 874}, /* Thai */
316#define IDX_CP932 45
317 {"cp932", ENC_DBCS, DBCS_JPN},
318#define IDX_CP936 46
319 {"cp936", ENC_DBCS, DBCS_CHS},
320#define IDX_CP949 47
321 {"cp949", ENC_DBCS, DBCS_KOR},
322#define IDX_CP950 48
323 {"cp950", ENC_DBCS, DBCS_CHT},
324#define IDX_CP1250 49
325 {"cp1250", ENC_8BIT, 1250}, /* Czech, Polish, etc. */
326#define IDX_CP1251 50
327 {"cp1251", ENC_8BIT, 1251}, /* Cyrillic */
328 /* cp1252 is considered to be equal to latin1 */
329#define IDX_CP1253 51
330 {"cp1253", ENC_8BIT, 1253}, /* Greek */
331#define IDX_CP1254 52
332 {"cp1254", ENC_8BIT, 1254}, /* Turkish */
333#define IDX_CP1255 53
334 {"cp1255", ENC_8BIT, 1255}, /* Hebrew */
335#define IDX_CP1256 54
336 {"cp1256", ENC_8BIT, 1256}, /* Arabic */
337#define IDX_CP1257 55
338 {"cp1257", ENC_8BIT, 1257}, /* Baltic */
339#define IDX_CP1258 56
340 {"cp1258", ENC_8BIT, 1258}, /* Vietnamese */
341
342#define IDX_MACROMAN 57
343 {"macroman", ENC_8BIT + ENC_MACROMAN, 0}, /* Mac OS */
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000344#define IDX_DECMCS 58
345 {"dec-mcs", ENC_8BIT, 0}, /* DEC MCS */
346#define IDX_HPROMAN8 59
347 {"hp-roman8", ENC_8BIT, 0}, /* HP Roman8 */
348#define IDX_COUNT 60
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349};
350
351/*
352 * Aliases for encoding names.
353 */
354static struct
355{ char *name; int canon;}
356enc_alias_table[] =
357{
358 {"ansi", IDX_LATIN_1},
359 {"iso-8859-1", IDX_LATIN_1},
360 {"latin2", IDX_ISO_2},
361 {"latin3", IDX_ISO_3},
362 {"latin4", IDX_ISO_4},
363 {"cyrillic", IDX_ISO_5},
364 {"arabic", IDX_ISO_6},
365 {"greek", IDX_ISO_7},
366#ifdef WIN3264
367 {"hebrew", IDX_CP1255},
368#else
369 {"hebrew", IDX_ISO_8},
370#endif
371 {"latin5", IDX_ISO_9},
372 {"turkish", IDX_ISO_9}, /* ? */
373 {"latin6", IDX_ISO_10},
374 {"nordic", IDX_ISO_10}, /* ? */
375 {"thai", IDX_ISO_11}, /* ? */
376 {"latin7", IDX_ISO_13},
377 {"latin8", IDX_ISO_14},
378 {"latin9", IDX_ISO_15},
379 {"utf8", IDX_UTF8},
380 {"unicode", IDX_UCS2},
381 {"ucs2", IDX_UCS2},
382 {"ucs2be", IDX_UCS2},
383 {"ucs-2be", IDX_UCS2},
384 {"ucs2le", IDX_UCS2LE},
385 {"utf16", IDX_UTF16},
386 {"utf16be", IDX_UTF16},
387 {"utf-16be", IDX_UTF16},
388 {"utf16le", IDX_UTF16LE},
389 {"ucs4", IDX_UCS4},
390 {"ucs4be", IDX_UCS4},
391 {"ucs-4be", IDX_UCS4},
392 {"ucs4le", IDX_UCS4LE},
Bram Moolenaar5360af92008-02-20 10:29:39 +0000393 {"utf32", IDX_UCS4},
394 {"utf-32", IDX_UCS4},
395 {"utf32be", IDX_UCS4},
396 {"utf-32be", IDX_UCS4},
397 {"utf32le", IDX_UCS4LE},
398 {"utf-32le", IDX_UCS4LE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {"932", IDX_CP932},
400 {"949", IDX_CP949},
401 {"936", IDX_CP936},
Bram Moolenaar0928caa2006-08-16 16:03:34 +0000402 {"gbk", IDX_CP936},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {"950", IDX_CP950},
404 {"eucjp", IDX_EUC_JP},
405 {"unix-jis", IDX_EUC_JP},
406 {"ujis", IDX_EUC_JP},
407 {"shift-jis", IDX_SJIS},
408 {"euckr", IDX_EUC_KR},
409 {"5601", IDX_EUC_KR}, /* Sun: KS C 5601 */
410 {"euccn", IDX_EUC_CN},
411 {"gb2312", IDX_EUC_CN},
412 {"euctw", IDX_EUC_TW},
413#if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
414 {"japan", IDX_CP932},
415 {"korea", IDX_CP949},
416 {"prc", IDX_CP936},
417 {"chinese", IDX_CP936},
418 {"taiwan", IDX_CP950},
419 {"big5", IDX_CP950},
420#else
421 {"japan", IDX_EUC_JP},
422 {"korea", IDX_EUC_KR},
423 {"prc", IDX_EUC_CN},
424 {"chinese", IDX_EUC_CN},
425 {"taiwan", IDX_EUC_TW},
426 {"cp950", IDX_BIG5},
427 {"950", IDX_BIG5},
428#endif
429 {"mac", IDX_MACROMAN},
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000430 {"mac-roman", IDX_MACROMAN},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {NULL, 0}
432};
433
434#ifndef CP_UTF8
435# define CP_UTF8 65001 /* magic number from winnls.h */
436#endif
437
438/*
439 * Find encoding "name" in the list of canonical encoding names.
440 * Returns -1 if not found.
441 */
442 static int
443enc_canon_search(name)
444 char_u *name;
445{
446 int i;
447
448 for (i = 0; i < IDX_COUNT; ++i)
449 if (STRCMP(name, enc_canon_table[i].name) == 0)
450 return i;
451 return -1;
452}
453
454#endif
455
456#if defined(FEAT_MBYTE) || defined(PROTO)
457
458/*
459 * Find canonical encoding "name" in the list and return its properties.
460 * Returns 0 if not found.
461 */
462 int
463enc_canon_props(name)
464 char_u *name;
465{
466 int i;
467
468 i = enc_canon_search(name);
469 if (i >= 0)
470 return enc_canon_table[i].prop;
471#ifdef WIN3264
472 if (name[0] == 'c' && name[1] == 'p' && VIM_ISDIGIT(name[2]))
473 {
474 CPINFO cpinfo;
475
476 /* Get info on this codepage to find out what it is. */
477 if (GetCPInfo(atoi(name + 2), &cpinfo) != 0)
478 {
479 if (cpinfo.MaxCharSize == 1) /* some single-byte encoding */
480 return ENC_8BIT;
481 if (cpinfo.MaxCharSize == 2
482 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
483 /* must be a DBCS encoding */
484 return ENC_DBCS;
485 }
486 return 0;
487 }
488#endif
489 if (STRNCMP(name, "2byte-", 6) == 0)
490 return ENC_DBCS;
491 if (STRNCMP(name, "8bit-", 5) == 0 || STRNCMP(name, "iso-8859-", 9) == 0)
492 return ENC_8BIT;
493 return 0;
494}
495
496/*
497 * Set up for using multi-byte characters.
498 * Called in three cases:
499 * - by main() to initialize (p_enc == NULL)
500 * - by set_init_1() after 'encoding' was set to its default.
501 * - by do_set() when 'encoding' has been set.
502 * p_enc must have been passed through enc_canonize() already.
503 * Sets the "enc_unicode", "enc_utf8", "enc_dbcs" and "has_mbyte" flags.
504 * Fills mb_bytelen_tab[] and returns NULL when there are no problems.
505 * When there is something wrong: Returns an error message and doesn't change
506 * anything.
507 */
508 char_u *
509mb_init()
510{
511 int i;
512 int idx;
513 int n;
514 int enc_dbcs_new = 0;
515#if defined(USE_ICONV) && !defined(WIN3264) && !defined(WIN32UNIX) \
516 && !defined(MACOS)
517# define LEN_FROM_CONV
518 vimconv_T vimconv;
519 char_u *p;
520#endif
521
522 if (p_enc == NULL)
523 {
524 /* Just starting up: set the whole table to one's. */
525 for (i = 0; i < 256; ++i)
526 mb_bytelen_tab[i] = 1;
527 input_conv.vc_type = CONV_NONE;
528 input_conv.vc_factor = 1;
529 output_conv.vc_type = CONV_NONE;
530 return NULL;
531 }
532
533#ifdef WIN3264
534 if (p_enc[0] == 'c' && p_enc[1] == 'p' && VIM_ISDIGIT(p_enc[2]))
535 {
536 CPINFO cpinfo;
537
538 /* Get info on this codepage to find out what it is. */
539 if (GetCPInfo(atoi(p_enc + 2), &cpinfo) != 0)
540 {
541 if (cpinfo.MaxCharSize == 1)
542 {
543 /* some single-byte encoding */
544 enc_unicode = 0;
545 enc_utf8 = FALSE;
546 }
547 else if (cpinfo.MaxCharSize == 2
548 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
549 {
550 /* must be a DBCS encoding, check below */
551 enc_dbcs_new = atoi(p_enc + 2);
552 }
553 else
554 goto codepage_invalid;
555 }
556 else if (GetLastError() == ERROR_INVALID_PARAMETER)
557 {
558codepage_invalid:
559 return (char_u *)N_("E543: Not a valid codepage");
560 }
561 }
562#endif
563 else if (STRNCMP(p_enc, "8bit-", 5) == 0
564 || STRNCMP(p_enc, "iso-8859-", 9) == 0)
565 {
566 /* Accept any "8bit-" or "iso-8859-" name. */
567 enc_unicode = 0;
568 enc_utf8 = FALSE;
569 }
570 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
571 {
572#ifdef WIN3264
573 /* Windows: accept only valid codepage numbers, check below. */
574 if (p_enc[6] != 'c' || p_enc[7] != 'p'
575 || (enc_dbcs_new = atoi(p_enc + 8)) == 0)
576 return e_invarg;
577#else
578 /* Unix: accept any "2byte-" name, assume current locale. */
579 enc_dbcs_new = DBCS_2BYTE;
580#endif
581 }
582 else if ((idx = enc_canon_search(p_enc)) >= 0)
583 {
584 i = enc_canon_table[idx].prop;
585 if (i & ENC_UNICODE)
586 {
587 /* Unicode */
588 enc_utf8 = TRUE;
589 if (i & (ENC_2BYTE | ENC_2WORD))
590 enc_unicode = 2;
591 else if (i & ENC_4BYTE)
592 enc_unicode = 4;
593 else
594 enc_unicode = 0;
595 }
596 else if (i & ENC_DBCS)
597 {
598 /* 2byte, handle below */
599 enc_dbcs_new = enc_canon_table[idx].codepage;
600 }
601 else
602 {
603 /* Must be 8-bit. */
604 enc_unicode = 0;
605 enc_utf8 = FALSE;
606 }
607 }
608 else /* Don't know what encoding this is, reject it. */
609 return e_invarg;
610
611 if (enc_dbcs_new != 0)
612 {
613#ifdef WIN3264
614 /* Check if the DBCS code page is OK. */
615 if (!IsValidCodePage(enc_dbcs_new))
616 goto codepage_invalid;
617#endif
618 enc_unicode = 0;
619 enc_utf8 = FALSE;
620 }
621 enc_dbcs = enc_dbcs_new;
622 has_mbyte = (enc_dbcs != 0 || enc_utf8);
623
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100624#if defined(WIN3264) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 enc_codepage = encname2codepage(p_enc);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000626 enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#endif
628
Bram Moolenaar78622822005-08-23 21:00:13 +0000629 /* Detect an encoding that uses latin1 characters. */
630 enc_latin1like = (enc_utf8 || STRCMP(p_enc, "latin1") == 0
631 || STRCMP(p_enc, "iso-8859-15") == 0);
632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 /*
634 * Set the function pointers.
635 */
636 if (enc_utf8)
637 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000638 mb_ptr2len = utfc_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000639 mb_ptr2len_len = utfc_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 mb_char2len = utf_char2len;
641 mb_char2bytes = utf_char2bytes;
642 mb_ptr2cells = utf_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000643 mb_ptr2cells_len = utf_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 mb_char2cells = utf_char2cells;
645 mb_off2cells = utf_off2cells;
646 mb_ptr2char = utf_ptr2char;
647 mb_head_off = utf_head_off;
648 }
649 else if (enc_dbcs != 0)
650 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000651 mb_ptr2len = dbcs_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000652 mb_ptr2len_len = dbcs_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 mb_char2len = dbcs_char2len;
654 mb_char2bytes = dbcs_char2bytes;
655 mb_ptr2cells = dbcs_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000656 mb_ptr2cells_len = dbcs_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 mb_char2cells = dbcs_char2cells;
658 mb_off2cells = dbcs_off2cells;
659 mb_ptr2char = dbcs_ptr2char;
660 mb_head_off = dbcs_head_off;
661 }
662 else
663 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000664 mb_ptr2len = latin_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000665 mb_ptr2len_len = latin_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 mb_char2len = latin_char2len;
667 mb_char2bytes = latin_char2bytes;
668 mb_ptr2cells = latin_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000669 mb_ptr2cells_len = latin_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 mb_char2cells = latin_char2cells;
671 mb_off2cells = latin_off2cells;
672 mb_ptr2char = latin_ptr2char;
673 mb_head_off = latin_head_off;
674 }
675
676 /*
677 * Fill the mb_bytelen_tab[] for MB_BYTE2LEN().
678 */
679#ifdef LEN_FROM_CONV
680 /* When 'encoding' is different from the current locale mblen() won't
681 * work. Use conversion to "utf-8" instead. */
682 vimconv.vc_type = CONV_NONE;
683 if (enc_dbcs)
684 {
685 p = enc_locale();
686 if (p == NULL || STRCMP(p, p_enc) != 0)
687 {
688 convert_setup(&vimconv, p_enc, (char_u *)"utf-8");
689 vimconv.vc_fail = TRUE;
690 }
691 vim_free(p);
692 }
693#endif
694
695 for (i = 0; i < 256; ++i)
696 {
697 /* Our own function to reliably check the length of UTF-8 characters,
698 * independent of mblen(). */
699 if (enc_utf8)
700 n = utf8len_tab[i];
701 else if (enc_dbcs == 0)
702 n = 1;
703 else
704 {
705#if defined(WIN3264) || defined(WIN32UNIX)
706 /* enc_dbcs is set by setting 'fileencoding'. It becomes a Windows
707 * CodePage identifier, which we can pass directly in to Windows
708 * API */
Bram Moolenaarca058dc2014-01-14 13:26:21 +0100709 n = IsDBCSLeadByteEx(enc_dbcs, (WINBYTE)i) ? 2 : 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#else
Bram Moolenaar4064e242014-03-12 14:54:34 +0100711# if defined(MACOS) || defined(__amigaos4__) || defined(__ANDROID__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 /*
713 * if mblen() is not available, character which MSB is turned on
714 * are treated as leading byte character. (note : This assumption
715 * is not always true.)
716 */
717 n = (i & 0x80) ? 2 : 1;
718# else
Bram Moolenaar9a920d82012-06-01 15:21:02 +0200719 char buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720# ifdef X_LOCALE
721# ifndef mblen
722# define mblen _Xmblen
723# endif
724# endif
725 if (i == NUL) /* just in case mblen() can't handle "" */
726 n = 1;
727 else
728 {
729 buf[0] = i;
730 buf[1] = 0;
731#ifdef LEN_FROM_CONV
732 if (vimconv.vc_type != CONV_NONE)
733 {
734 /*
735 * string_convert() should fail when converting the first
736 * byte of a double-byte character.
737 */
738 p = string_convert(&vimconv, (char_u *)buf, NULL);
739 if (p != NULL)
740 {
741 vim_free(p);
742 n = 1;
743 }
744 else
745 n = 2;
746 }
747 else
748#endif
749 {
750 /*
751 * mblen() should return -1 for invalid (means the leading
752 * multibyte) character. However there are some platforms
753 * where mblen() returns 0 for invalid character.
754 * Therefore, following condition includes 0.
755 */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000756 ignored = mblen(NULL, 0); /* First reset the state. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (mblen(buf, (size_t)1) <= 0)
758 n = 2;
759 else
760 n = 1;
761 }
762 }
763# endif
764#endif
765 }
766
767 mb_bytelen_tab[i] = n;
768 }
769
770#ifdef LEN_FROM_CONV
771 convert_setup(&vimconv, NULL, NULL);
772#endif
773
774 /* The cell width depends on the type of multi-byte characters. */
775 (void)init_chartab();
776
777 /* When enc_utf8 is set or reset, (de)allocate ScreenLinesUC[] */
778 screenalloc(FALSE);
779
780 /* When using Unicode, set default for 'fileencodings'. */
781 if (enc_utf8 && !option_was_set((char_u *)"fencs"))
782 set_string_option_direct((char_u *)"fencs", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000783 (char_u *)"ucs-bom,utf-8,default,latin1", OPT_FREE, 0);
784
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785#if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
786 /* GNU gettext 0.10.37 supports this feature: set the codeset used for
787 * translated messages independently from the current locale. */
788 (void)bind_textdomain_codeset(VIMPACKAGE,
789 enc_utf8 ? "utf-8" : (char *)p_enc);
790#endif
791
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000792#ifdef WIN32
793 /* When changing 'encoding' while starting up, then convert the command
794 * line arguments from the active codepage to 'encoding'. */
795 if (starting != 0)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000796 fix_arg_enc();
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000797#endif
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799#ifdef FEAT_AUTOCMD
800 /* Fire an autocommand to let people do custom font setup. This must be
801 * after Vim has been setup for the new encoding. */
802 apply_autocmds(EVENT_ENCODINGCHANGED, NULL, (char_u *)"", FALSE, curbuf);
803#endif
804
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000805#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000806 /* Need to reload spell dictionaries */
807 spell_reload();
808#endif
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 return NULL;
811}
812
813/*
814 * Return the size of the BOM for the current buffer:
815 * 0 - no BOM
816 * 2 - UCS-2 or UTF-16 BOM
817 * 4 - UCS-4 BOM
818 * 3 - UTF-8 BOM
819 */
820 int
821bomb_size()
822{
823 int n = 0;
824
825 if (curbuf->b_p_bomb && !curbuf->b_p_bin)
826 {
827 if (*curbuf->b_p_fenc == NUL)
828 {
829 if (enc_utf8)
830 {
831 if (enc_unicode != 0)
832 n = enc_unicode;
833 else
834 n = 3;
835 }
836 }
837 else if (STRCMP(curbuf->b_p_fenc, "utf-8") == 0)
838 n = 3;
839 else if (STRNCMP(curbuf->b_p_fenc, "ucs-2", 5) == 0
840 || STRNCMP(curbuf->b_p_fenc, "utf-16", 6) == 0)
841 n = 2;
842 else if (STRNCMP(curbuf->b_p_fenc, "ucs-4", 5) == 0)
843 n = 4;
844 }
845 return n;
846}
847
848/*
Bram Moolenaar836082d2011-08-10 13:21:46 +0200849 * Remove all BOM from "s" by moving remaining text.
850 */
851 void
852remove_bom(s)
853 char_u *s;
854{
855 if (enc_utf8)
856 {
857 char_u *p = s;
858
859 while ((p = vim_strbyte(p, 0xef)) != NULL)
860 {
861 if (p[1] == 0xbb && p[2] == 0xbf)
862 STRMOVE(p, p + 3);
863 else
864 ++p;
865 }
866 }
867}
868
869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 * Get class of pointer:
871 * 0 for blank or NUL
872 * 1 for punctuation
873 * 2 for an (ASCII) word character
874 * >2 for other word characters
875 */
876 int
877mb_get_class(p)
878 char_u *p;
879{
Bram Moolenaarf813a182013-01-30 13:59:37 +0100880 return mb_get_class_buf(p, curbuf);
881}
882
883 int
884mb_get_class_buf(p, buf)
885 char_u *p;
886 buf_T *buf;
887{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (MB_BYTE2LEN(p[0]) == 1)
889 {
890 if (p[0] == NUL || vim_iswhite(p[0]))
891 return 0;
Bram Moolenaarf813a182013-01-30 13:59:37 +0100892 if (vim_iswordc_buf(p[0], buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 return 2;
894 return 1;
895 }
896 if (enc_dbcs != 0 && p[0] != NUL && p[1] != NUL)
897 return dbcs_class(p[0], p[1]);
898 if (enc_utf8)
899 return utf_class(utf_ptr2char(p));
900 return 0;
901}
902
903/*
904 * Get class of a double-byte character. This always returns 3 or bigger.
905 * TODO: Should return 1 for punctuation.
906 */
907 int
908dbcs_class(lead, trail)
909 unsigned lead;
910 unsigned trail;
911{
912 switch (enc_dbcs)
913 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200914 /* please add classify routine for your language in here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
916 case DBCS_JPNU: /* ? */
917 case DBCS_JPN:
918 {
919 /* JIS code classification */
920 unsigned char lb = lead;
921 unsigned char tb = trail;
922
923 /* convert process code to JIS */
924# if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
925 /* process code is SJIS */
926 if (lb <= 0x9f)
927 lb = (lb - 0x81) * 2 + 0x21;
928 else
929 lb = (lb - 0xc1) * 2 + 0x21;
930 if (tb <= 0x7e)
931 tb -= 0x1f;
932 else if (tb <= 0x9e)
933 tb -= 0x20;
934 else
935 {
936 tb -= 0x7e;
937 lb += 1;
938 }
939# else
940 /*
941 * XXX: Code page identification can not use with all
942 * system! So, some other encoding information
943 * will be needed.
944 * In japanese: SJIS,EUC,UNICODE,(JIS)
945 * Note that JIS-code system don't use as
946 * process code in most system because it uses
947 * escape sequences(JIS is context depend encoding).
948 */
949 /* assume process code is JAPANESE-EUC */
950 lb &= 0x7f;
951 tb &= 0x7f;
952# endif
953 /* exceptions */
954 switch (lb << 8 | tb)
955 {
956 case 0x2121: /* ZENKAKU space */
957 return 0;
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100958 case 0x2122: /* TOU-TEN (Japanese comma) */
959 case 0x2123: /* KU-TEN (Japanese period) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 case 0x2124: /* ZENKAKU comma */
961 case 0x2125: /* ZENKAKU period */
962 return 1;
963 case 0x213c: /* prolongedsound handled as KATAKANA */
964 return 13;
965 }
966 /* sieved by KU code */
967 switch (lb)
968 {
969 case 0x21:
970 case 0x22:
971 /* special symbols */
972 return 10;
973 case 0x23:
974 /* alpha-numeric */
975 return 11;
976 case 0x24:
977 /* hiragana */
978 return 12;
979 case 0x25:
980 /* katakana */
981 return 13;
982 case 0x26:
983 /* greek */
984 return 14;
985 case 0x27:
986 /* russian */
987 return 15;
988 case 0x28:
989 /* lines */
990 return 16;
991 default:
992 /* kanji */
993 return 17;
994 }
995 }
996
997 case DBCS_KORU: /* ? */
998 case DBCS_KOR:
999 {
1000 /* KS code classification */
1001 unsigned char c1 = lead;
1002 unsigned char c2 = trail;
1003
1004 /*
1005 * 20 : Hangul
1006 * 21 : Hanja
1007 * 22 : Symbols
1008 * 23 : Alpha-numeric/Roman Letter (Full width)
1009 * 24 : Hangul Letter(Alphabet)
1010 * 25 : Roman Numeral/Greek Letter
1011 * 26 : Box Drawings
1012 * 27 : Unit Symbols
1013 * 28 : Circled/Parenthesized Letter
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001014 * 29 : Hiragana/Katakana
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * 30 : Cyrillic Letter
1016 */
1017
1018 if (c1 >= 0xB0 && c1 <= 0xC8)
1019 /* Hangul */
1020 return 20;
1021#if defined(WIN3264) || defined(WIN32UNIX)
1022 else if (c1 <= 0xA0 || c2 <= 0xA0)
1023 /* Extended Hangul Region : MS UHC(Unified Hangul Code) */
1024 /* c1: 0x81-0xA0 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xFE
1025 * c1: 0xA1-0xC6 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xA0
1026 */
1027 return 20;
1028#endif
1029
1030 else if (c1 >= 0xCA && c1 <= 0xFD)
1031 /* Hanja */
1032 return 21;
1033 else switch (c1)
1034 {
1035 case 0xA1:
1036 case 0xA2:
1037 /* Symbols */
1038 return 22;
1039 case 0xA3:
1040 /* Alpha-numeric */
1041 return 23;
1042 case 0xA4:
1043 /* Hangul Letter(Alphabet) */
1044 return 24;
1045 case 0xA5:
1046 /* Roman Numeral/Greek Letter */
1047 return 25;
1048 case 0xA6:
1049 /* Box Drawings */
1050 return 26;
1051 case 0xA7:
1052 /* Unit Symbols */
1053 return 27;
1054 case 0xA8:
1055 case 0xA9:
1056 if (c2 <= 0xAF)
1057 return 25; /* Roman Letter */
1058 else if (c2 >= 0xF6)
1059 return 22; /* Symbols */
1060 else
1061 /* Circled/Parenthesized Letter */
1062 return 28;
1063 case 0xAA:
1064 case 0xAB:
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001065 /* Hiragana/Katakana */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 return 29;
1067 case 0xAC:
1068 /* Cyrillic Letter */
1069 return 30;
1070 }
1071 }
1072 default:
1073 break;
1074 }
1075 return 3;
1076}
1077
1078/*
1079 * mb_char2len() function pointer.
1080 * Return length in bytes of character "c".
1081 * Returns 1 for a single-byte character.
1082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 int
1084latin_char2len(c)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001085 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
1087 return 1;
1088}
1089
1090 static int
1091dbcs_char2len(c)
1092 int c;
1093{
1094 if (c >= 0x100)
1095 return 2;
1096 return 1;
1097}
1098
1099/*
1100 * mb_char2bytes() function pointer.
1101 * Convert a character to its bytes.
1102 * Returns the length in bytes.
1103 */
1104 int
1105latin_char2bytes(c, buf)
1106 int c;
1107 char_u *buf;
1108{
1109 buf[0] = c;
1110 return 1;
1111}
1112
1113 static int
1114dbcs_char2bytes(c, buf)
1115 int c;
1116 char_u *buf;
1117{
1118 if (c >= 0x100)
1119 {
1120 buf[0] = (unsigned)c >> 8;
1121 buf[1] = c;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001122 /* Never use a NUL byte, it causes lots of trouble. It's an invalid
1123 * character anyway. */
1124 if (buf[1] == NUL)
1125 buf[1] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 return 2;
1127 }
1128 buf[0] = c;
1129 return 1;
1130}
1131
1132/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001133 * mb_ptr2len() function pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * Get byte length of character at "*p" but stop at a NUL.
1135 * For UTF-8 this includes following composing characters.
1136 * Returns 0 when *p is NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 */
1138 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001139latin_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 char_u *p;
1141{
1142 return MB_BYTE2LEN(*p);
1143}
1144
1145 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001146dbcs_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 char_u *p;
1148{
1149 int len;
1150
1151 /* Check if second byte is not missing. */
1152 len = MB_BYTE2LEN(*p);
1153 if (len == 2 && p[1] == NUL)
1154 len = 1;
1155 return len;
1156}
1157
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001158/*
1159 * mb_ptr2len_len() function pointer.
1160 * Like mb_ptr2len(), but limit to read "size" bytes.
1161 * Returns 0 for an empty string.
1162 * Returns 1 for an illegal char or an incomplete byte sequence.
1163 */
1164 int
1165latin_ptr2len_len(p, size)
1166 char_u *p;
1167 int size;
1168{
1169 if (size < 1 || *p == NUL)
1170 return 0;
1171 return 1;
1172}
1173
1174 static int
1175dbcs_ptr2len_len(p, size)
1176 char_u *p;
1177 int size;
1178{
1179 int len;
1180
1181 if (size < 1 || *p == NUL)
1182 return 0;
1183 if (size == 1)
1184 return 1;
1185 /* Check that second byte is not missing. */
1186 len = MB_BYTE2LEN(*p);
1187 if (len == 2 && p[1] == NUL)
1188 len = 1;
1189 return len;
1190}
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192struct interval
1193{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01001194 long first;
1195 long last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196};
1197static int intable __ARGS((struct interval *table, size_t size, int c));
1198
1199/*
1200 * Return TRUE if "c" is in "table[size / sizeof(struct interval)]".
1201 */
1202 static int
1203intable(table, size, c)
1204 struct interval *table;
1205 size_t size;
1206 int c;
1207{
1208 int mid, bot, top;
1209
1210 /* first quick check for Latin1 etc. characters */
1211 if (c < table[0].first)
1212 return FALSE;
1213
1214 /* binary search in table */
1215 bot = 0;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001216 top = (int)(size / sizeof(struct interval) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 while (top >= bot)
1218 {
1219 mid = (bot + top) / 2;
1220 if (table[mid].last < c)
1221 bot = mid + 1;
1222 else if (table[mid].first > c)
1223 top = mid - 1;
1224 else
1225 return TRUE;
1226 }
1227 return FALSE;
1228}
1229
1230/*
1231 * For UTF-8 character "c" return 2 for a double-width character, 1 for others.
1232 * Returns 4 or 6 for an unprintable character.
1233 * Is only correct for characters >= 0x80.
1234 * When p_ambw is "double", return 2 for a character with East Asian Width
1235 * class 'A'(mbiguous).
1236 */
1237 int
1238utf_char2cells(c)
1239 int c;
1240{
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001241 /* Sorted list of non-overlapping intervals of East Asian double width
1242 * characters, generated with ../runtime/tools/unicode.vim. */
1243 static struct interval doublewidth[] =
1244 {
1245 {0x1100, 0x115f},
1246 {0x11a3, 0x11a7},
1247 {0x11fa, 0x11ff},
1248 {0x2329, 0x232a},
1249 {0x2e80, 0x2e99},
1250 {0x2e9b, 0x2ef3},
1251 {0x2f00, 0x2fd5},
1252 {0x2ff0, 0x2ffb},
1253 {0x3000, 0x3029},
1254 {0x3030, 0x303e},
1255 {0x3041, 0x3096},
1256 {0x309b, 0x30ff},
1257 {0x3105, 0x312d},
1258 {0x3131, 0x318e},
1259 {0x3190, 0x31b7},
1260 {0x31c0, 0x31e3},
1261 {0x31f0, 0x321e},
1262 {0x3220, 0x3247},
1263 {0x3250, 0x32fe},
1264 {0x3300, 0x4dbf},
1265 {0x4e00, 0xa48c},
1266 {0xa490, 0xa4c6},
1267 {0xa960, 0xa97c},
1268 {0xac00, 0xd7a3},
1269 {0xd7b0, 0xd7c6},
1270 {0xd7cb, 0xd7fb},
1271 {0xf900, 0xfaff},
1272 {0xfe10, 0xfe19},
1273 {0xfe30, 0xfe52},
1274 {0xfe54, 0xfe66},
1275 {0xfe68, 0xfe6b},
1276 {0xff01, 0xff60},
1277 {0xffe0, 0xffe6},
1278 {0x1f200, 0x1f200},
1279 {0x1f210, 0x1f231},
1280 {0x1f240, 0x1f248},
1281 {0x20000, 0x2fffd},
1282 {0x30000, 0x3fffd}
1283 };
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01001284 /* Sorted list of non-overlapping intervals of East Asian Ambiguous
1285 * characters, generated with ../runtime/tools/unicode.vim. */
1286 static struct interval ambiguous[] =
1287 {
1288 {0x00a1, 0x00a1},
1289 {0x00a4, 0x00a4},
1290 {0x00a7, 0x00a8},
1291 {0x00aa, 0x00aa},
1292 {0x00ad, 0x00ae},
1293 {0x00b0, 0x00b4},
1294 {0x00b6, 0x00ba},
1295 {0x00bc, 0x00bf},
1296 {0x00c6, 0x00c6},
1297 {0x00d0, 0x00d0},
1298 {0x00d7, 0x00d8},
1299 {0x00de, 0x00e1},
1300 {0x00e6, 0x00e6},
1301 {0x00e8, 0x00ea},
1302 {0x00ec, 0x00ed},
1303 {0x00f0, 0x00f0},
1304 {0x00f2, 0x00f3},
1305 {0x00f7, 0x00fa},
1306 {0x00fc, 0x00fc},
1307 {0x00fe, 0x00fe},
1308 {0x0101, 0x0101},
1309 {0x0111, 0x0111},
1310 {0x0113, 0x0113},
1311 {0x011b, 0x011b},
1312 {0x0126, 0x0127},
1313 {0x012b, 0x012b},
1314 {0x0131, 0x0133},
1315 {0x0138, 0x0138},
1316 {0x013f, 0x0142},
1317 {0x0144, 0x0144},
1318 {0x0148, 0x014b},
1319 {0x014d, 0x014d},
1320 {0x0152, 0x0153},
1321 {0x0166, 0x0167},
1322 {0x016b, 0x016b},
1323 {0x01ce, 0x01ce},
1324 {0x01d0, 0x01d0},
1325 {0x01d2, 0x01d2},
1326 {0x01d4, 0x01d4},
1327 {0x01d6, 0x01d6},
1328 {0x01d8, 0x01d8},
1329 {0x01da, 0x01da},
1330 {0x01dc, 0x01dc},
1331 {0x0251, 0x0251},
1332 {0x0261, 0x0261},
1333 {0x02c4, 0x02c4},
1334 {0x02c7, 0x02c7},
1335 {0x02c9, 0x02cb},
1336 {0x02cd, 0x02cd},
1337 {0x02d0, 0x02d0},
1338 {0x02d8, 0x02db},
1339 {0x02dd, 0x02dd},
1340 {0x02df, 0x02df},
1341 {0x0391, 0x03a1},
1342 {0x03a3, 0x03a9},
1343 {0x03b1, 0x03c1},
1344 {0x03c3, 0x03c9},
1345 {0x0401, 0x0401},
1346 {0x0410, 0x044f},
1347 {0x0451, 0x0451},
1348 {0x2010, 0x2010},
1349 {0x2013, 0x2016},
1350 {0x2018, 0x2019},
1351 {0x201c, 0x201d},
1352 {0x2020, 0x2022},
1353 {0x2024, 0x2027},
1354 {0x2030, 0x2030},
1355 {0x2032, 0x2033},
1356 {0x2035, 0x2035},
1357 {0x203b, 0x203b},
1358 {0x203e, 0x203e},
1359 {0x2074, 0x2074},
1360 {0x207f, 0x207f},
1361 {0x2081, 0x2084},
1362 {0x20ac, 0x20ac},
1363 {0x2103, 0x2103},
1364 {0x2105, 0x2105},
1365 {0x2109, 0x2109},
1366 {0x2113, 0x2113},
1367 {0x2116, 0x2116},
1368 {0x2121, 0x2122},
1369 {0x2126, 0x2126},
1370 {0x212b, 0x212b},
1371 {0x2153, 0x2154},
1372 {0x215b, 0x215e},
1373 {0x2160, 0x216b},
1374 {0x2170, 0x2179},
1375 {0x2189, 0x2189},
1376 {0x2190, 0x2199},
1377 {0x21b8, 0x21b9},
1378 {0x21d2, 0x21d2},
1379 {0x21d4, 0x21d4},
1380 {0x21e7, 0x21e7},
1381 {0x2200, 0x2200},
1382 {0x2202, 0x2203},
1383 {0x2207, 0x2208},
1384 {0x220b, 0x220b},
1385 {0x220f, 0x220f},
1386 {0x2211, 0x2211},
1387 {0x2215, 0x2215},
1388 {0x221a, 0x221a},
1389 {0x221d, 0x2220},
1390 {0x2223, 0x2223},
1391 {0x2225, 0x2225},
1392 {0x2227, 0x222c},
1393 {0x222e, 0x222e},
1394 {0x2234, 0x2237},
1395 {0x223c, 0x223d},
1396 {0x2248, 0x2248},
1397 {0x224c, 0x224c},
1398 {0x2252, 0x2252},
1399 {0x2260, 0x2261},
1400 {0x2264, 0x2267},
1401 {0x226a, 0x226b},
1402 {0x226e, 0x226f},
1403 {0x2282, 0x2283},
1404 {0x2286, 0x2287},
1405 {0x2295, 0x2295},
1406 {0x2299, 0x2299},
1407 {0x22a5, 0x22a5},
1408 {0x22bf, 0x22bf},
1409 {0x2312, 0x2312},
1410 {0x2460, 0x24e9},
1411 {0x24eb, 0x254b},
1412 {0x2550, 0x2573},
1413 {0x2580, 0x258f},
1414 {0x2592, 0x2595},
1415 {0x25a0, 0x25a1},
1416 {0x25a3, 0x25a9},
1417 {0x25b2, 0x25b3},
1418 {0x25b6, 0x25b7},
1419 {0x25bc, 0x25bd},
1420 {0x25c0, 0x25c1},
1421 {0x25c6, 0x25c8},
1422 {0x25cb, 0x25cb},
1423 {0x25ce, 0x25d1},
1424 {0x25e2, 0x25e5},
1425 {0x25ef, 0x25ef},
1426 {0x2605, 0x2606},
1427 {0x2609, 0x2609},
1428 {0x260e, 0x260f},
1429 {0x2614, 0x2615},
1430 {0x261c, 0x261c},
1431 {0x261e, 0x261e},
1432 {0x2640, 0x2640},
1433 {0x2642, 0x2642},
1434 {0x2660, 0x2661},
1435 {0x2663, 0x2665},
1436 {0x2667, 0x266a},
1437 {0x266c, 0x266d},
1438 {0x266f, 0x266f},
1439 {0x269e, 0x269f},
1440 {0x26be, 0x26bf},
1441 {0x26c4, 0x26cd},
1442 {0x26cf, 0x26e1},
1443 {0x26e3, 0x26e3},
1444 {0x26e8, 0x26ff},
1445 {0x273d, 0x273d},
1446 {0x2757, 0x2757},
1447 {0x2776, 0x277f},
1448 {0x2b55, 0x2b59},
1449 {0x3248, 0x324f},
1450 {0xe000, 0xf8ff},
1451 {0xfffd, 0xfffd},
1452 {0x1f100, 0x1f10a},
1453 {0x1f110, 0x1f12d},
1454 {0x1f131, 0x1f131},
1455 {0x1f13d, 0x1f13d},
1456 {0x1f13f, 0x1f13f},
1457 {0x1f142, 0x1f142},
1458 {0x1f146, 0x1f146},
1459 {0x1f14a, 0x1f14e},
1460 {0x1f157, 0x1f157},
1461 {0x1f15f, 0x1f15f},
1462 {0x1f179, 0x1f179},
1463 {0x1f17b, 0x1f17c},
1464 {0x1f17f, 0x1f17f},
1465 {0x1f18a, 0x1f18d},
1466 {0x1f190, 0x1f190},
1467 {0xf0000, 0xffffd},
1468 {0x100000, 0x10fffd}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 };
1470
1471 if (c >= 0x100)
1472 {
1473#ifdef USE_WCHAR_FUNCTIONS
1474 /*
1475 * Assume the library function wcwidth() works better than our own
1476 * stuff. It should return 1 for ambiguous width chars!
1477 */
1478 int n = wcwidth(c);
1479
1480 if (n < 0)
1481 return 6; /* unprintable, displays <xxxx> */
1482 if (n > 1)
1483 return n;
1484#else
1485 if (!utf_printable(c))
1486 return 6; /* unprintable, displays <xxxx> */
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001487 if (intable(doublewidth, sizeof(doublewidth), c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return 2;
1489#endif
1490 }
1491
1492 /* Characters below 0x100 are influenced by 'isprint' option */
1493 else if (c >= 0x80 && !vim_isprintc(c))
1494 return 4; /* unprintable, displays <xx> */
1495
1496 if (c >= 0x80 && *p_ambw == 'd' && intable(ambiguous, sizeof(ambiguous), c))
1497 return 2;
1498
1499 return 1;
1500}
1501
1502/*
1503 * mb_ptr2cells() function pointer.
1504 * Return the number of display cells character at "*p" occupies.
1505 * This doesn't take care of unprintable characters, use ptr2cells() for that.
1506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 int
1508latin_ptr2cells(p)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001509 char_u *p UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 return 1;
1512}
1513
1514 int
1515utf_ptr2cells(p)
1516 char_u *p;
1517{
1518 int c;
1519
1520 /* Need to convert to a wide character. */
1521 if (*p >= 0x80)
1522 {
1523 c = utf_ptr2char(p);
1524 /* An illegal byte is displayed as <xx>. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001525 if (utf_ptr2len(p) == 1 || c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 return 4;
1527 /* If the char is ASCII it must be an overlong sequence. */
1528 if (c < 0x80)
1529 return char2cells(c);
1530 return utf_char2cells(c);
1531 }
1532 return 1;
1533}
1534
1535 int
1536dbcs_ptr2cells(p)
1537 char_u *p;
1538{
1539 /* Number of cells is equal to number of bytes, except for euc-jp when
1540 * the first byte is 0x8e. */
1541 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
1542 return 1;
1543 return MB_BYTE2LEN(*p);
1544}
1545
1546/*
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001547 * mb_ptr2cells_len() function pointer.
1548 * Like mb_ptr2cells(), but limit string length to "size".
1549 * For an empty string or truncated character returns 1.
1550 */
1551 int
1552latin_ptr2cells_len(p, size)
1553 char_u *p UNUSED;
1554 int size UNUSED;
1555{
1556 return 1;
1557}
1558
1559 static int
1560utf_ptr2cells_len(p, size)
1561 char_u *p;
1562 int size;
1563{
1564 int c;
1565
1566 /* Need to convert to a wide character. */
1567 if (size > 0 && *p >= 0x80)
1568 {
1569 if (utf_ptr2len_len(p, size) < utf8len_tab[*p])
Bram Moolenaar24397332009-12-02 14:02:39 +00001570 return 1; /* truncated */
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001571 c = utf_ptr2char(p);
1572 /* An illegal byte is displayed as <xx>. */
1573 if (utf_ptr2len(p) == 1 || c == NUL)
1574 return 4;
1575 /* If the char is ASCII it must be an overlong sequence. */
1576 if (c < 0x80)
1577 return char2cells(c);
1578 return utf_char2cells(c);
1579 }
1580 return 1;
1581}
1582
1583 static int
1584dbcs_ptr2cells_len(p, size)
1585 char_u *p;
1586 int size;
1587{
1588 /* Number of cells is equal to number of bytes, except for euc-jp when
1589 * the first byte is 0x8e. */
1590 if (size <= 1 || (enc_dbcs == DBCS_JPNU && *p == 0x8e))
1591 return 1;
1592 return MB_BYTE2LEN(*p);
1593}
1594
1595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 * mb_char2cells() function pointer.
1597 * Return the number of display cells character "c" occupies.
1598 * Only takes care of multi-byte chars, not "^C" and such.
1599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 int
1601latin_char2cells(c)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001602 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 return 1;
1605}
1606
1607 static int
1608dbcs_char2cells(c)
1609 int c;
1610{
1611 /* Number of cells is equal to number of bytes, except for euc-jp when
1612 * the first byte is 0x8e. */
1613 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
1614 return 1;
1615 /* use the first byte */
1616 return MB_BYTE2LEN((unsigned)c >> 8);
1617}
1618
1619/*
Bram Moolenaar72597a52010-07-18 15:31:08 +02001620 * Return the number of cells occupied by string "p".
1621 * Stop at a NUL character. When "len" >= 0 stop at character "p[len]".
1622 */
1623 int
1624mb_string2cells(p, len)
1625 char_u *p;
1626 int len;
1627{
1628 int i;
1629 int clen = 0;
1630
1631 for (i = 0; (len < 0 || i < len) && p[i] != NUL; i += (*mb_ptr2len)(p + i))
1632 clen += (*mb_ptr2cells)(p + i);
1633 return clen;
1634}
1635
1636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * mb_off2cells() function pointer.
1638 * Return number of display cells for char at ScreenLines[off].
Bram Moolenaar367329b2007-08-30 11:53:22 +00001639 * We make sure that the offset used is less than "max_off".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001642latin_off2cells(off, max_off)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001643 unsigned off UNUSED;
1644 unsigned max_off UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 return 1;
1647}
1648
1649 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001650dbcs_off2cells(off, max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 unsigned off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001652 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001654 /* never check beyond end of the line */
1655 if (off >= max_off)
1656 return 1;
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 /* Number of cells is equal to number of bytes, except for euc-jp when
1659 * the first byte is 0x8e. */
1660 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1661 return 1;
1662 return MB_BYTE2LEN(ScreenLines[off]);
1663}
1664
1665 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001666utf_off2cells(off, max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 unsigned off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001668 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001670 return (off + 1 < max_off && ScreenLines[off + 1] == 0) ? 2 : 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671}
1672
1673/*
1674 * mb_ptr2char() function pointer.
1675 * Convert a byte sequence into a character.
1676 */
1677 int
1678latin_ptr2char(p)
1679 char_u *p;
1680{
1681 return *p;
1682}
1683
1684 static int
1685dbcs_ptr2char(p)
1686 char_u *p;
1687{
1688 if (MB_BYTE2LEN(*p) > 1 && p[1] != NUL)
1689 return (p[0] << 8) + p[1];
1690 return *p;
1691}
1692
1693/*
1694 * Convert a UTF-8 byte sequence to a wide character.
1695 * If the sequence is illegal or truncated by a NUL the first byte is
1696 * returned.
1697 * Does not include composing characters, of course.
1698 */
1699 int
1700utf_ptr2char(p)
1701 char_u *p;
1702{
1703 int len;
1704
1705 if (p[0] < 0x80) /* be quick for ASCII */
1706 return p[0];
1707
Bram Moolenaar24397332009-12-02 14:02:39 +00001708 len = utf8len_tab_zero[p[0]];
Bram Moolenaar89bf0922008-06-29 14:16:06 +00001709 if (len > 1 && (p[1] & 0xc0) == 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
1711 if (len == 2)
1712 return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
1713 if ((p[2] & 0xc0) == 0x80)
1714 {
1715 if (len == 3)
1716 return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
1717 + (p[2] & 0x3f);
1718 if ((p[3] & 0xc0) == 0x80)
1719 {
1720 if (len == 4)
1721 return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
1722 + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
1723 if ((p[4] & 0xc0) == 0x80)
1724 {
1725 if (len == 5)
1726 return ((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
1727 + ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
1728 + (p[4] & 0x3f);
1729 if ((p[5] & 0xc0) == 0x80 && len == 6)
1730 return ((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
1731 + ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
1732 + ((p[4] & 0x3f) << 6) + (p[5] & 0x3f);
1733 }
1734 }
1735 }
1736 }
1737 /* Illegal value, just return the first byte */
1738 return p[0];
1739}
1740
1741/*
Bram Moolenaar35ee4522011-07-15 21:16:59 +02001742 * Convert a UTF-8 byte sequence to a wide character.
1743 * String is assumed to be terminated by NUL or after "n" bytes, whichever
1744 * comes first.
1745 * The function is safe in the sense that it never accesses memory beyond the
1746 * first "n" bytes of "s".
1747 *
1748 * On success, returns decoded codepoint, advances "s" to the beginning of
1749 * next character and decreases "n" accordingly.
1750 *
1751 * If end of string was reached, returns 0 and, if "n" > 0, advances "s" past
1752 * NUL byte.
1753 *
1754 * If byte sequence is illegal or incomplete, returns -1 and does not advance
1755 * "s".
1756 */
1757 static int
1758utf_safe_read_char_adv(s, n)
1759 char_u **s;
1760 size_t *n;
1761{
1762 int c, k;
1763
1764 if (*n == 0) /* end of buffer */
1765 return 0;
1766
1767 k = utf8len_tab_zero[**s];
1768
1769 if (k == 1)
1770 {
1771 /* ASCII character or NUL */
1772 (*n)--;
1773 return *(*s)++;
1774 }
1775
1776 if ((size_t)k <= *n)
1777 {
1778 /* We have a multibyte sequence and it isn't truncated by buffer
1779 * limits so utf_ptr2char() is safe to use. Or the first byte is
1780 * illegal (k=0), and it's also safe to use utf_ptr2char(). */
1781 c = utf_ptr2char(*s);
1782
1783 /* On failure, utf_ptr2char() returns the first byte, so here we
1784 * check equality with the first byte. The only non-ASCII character
1785 * which equals the first byte of its own UTF-8 representation is
1786 * U+00C3 (UTF-8: 0xC3 0x83), so need to check that special case too.
1787 * It's safe even if n=1, else we would have k=2 > n. */
1788 if (c != (int)(**s) || (c == 0xC3 && (*s)[1] == 0x83))
1789 {
1790 /* byte sequence was successfully decoded */
1791 *s += k;
1792 *n -= k;
1793 return c;
1794 }
1795 }
1796
1797 /* byte sequence is incomplete or illegal */
1798 return -1;
1799}
1800
1801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 * Get character at **pp and advance *pp to the next character.
1803 * Note: composing characters are skipped!
1804 */
1805 int
1806mb_ptr2char_adv(pp)
1807 char_u **pp;
1808{
1809 int c;
1810
1811 c = (*mb_ptr2char)(*pp);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001812 *pp += (*mb_ptr2len)(*pp);
1813 return c;
1814}
1815
1816/*
1817 * Get character at **pp and advance *pp to the next character.
1818 * Note: composing characters are returned as separate characters.
1819 */
1820 int
1821mb_cptr2char_adv(pp)
1822 char_u **pp;
1823{
1824 int c;
1825
1826 c = (*mb_ptr2char)(*pp);
1827 if (enc_utf8)
1828 *pp += utf_ptr2len(*pp);
1829 else
1830 *pp += (*mb_ptr2len)(*pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 return c;
1832}
1833
1834#if defined(FEAT_ARABIC) || defined(PROTO)
1835/*
1836 * Check whether we are dealing with Arabic combining characters.
1837 * Note: these are NOT really composing characters!
1838 */
1839 int
1840arabic_combine(one, two)
1841 int one; /* first character */
1842 int two; /* character just after "one" */
1843{
1844 if (one == a_LAM)
1845 return arabic_maycombine(two);
1846 return FALSE;
1847}
1848
1849/*
1850 * Check whether we are dealing with a character that could be regarded as an
1851 * Arabic combining character, need to check the character before this.
1852 */
1853 int
1854arabic_maycombine(two)
1855 int two;
1856{
1857 if (p_arshape && !p_tbidi)
1858 return (two == a_ALEF_MADDA
1859 || two == a_ALEF_HAMZA_ABOVE
1860 || two == a_ALEF_HAMZA_BELOW
1861 || two == a_ALEF);
1862 return FALSE;
1863}
1864
1865/*
1866 * Check if the character pointed to by "p2" is a composing character when it
1867 * comes after "p1". For Arabic sometimes "ab" is replaced with "c", which
1868 * behaves like a composing character.
1869 */
1870 int
1871utf_composinglike(p1, p2)
1872 char_u *p1;
1873 char_u *p2;
1874{
1875 int c2;
1876
1877 c2 = utf_ptr2char(p2);
1878 if (utf_iscomposing(c2))
1879 return TRUE;
1880 if (!arabic_maycombine(c2))
1881 return FALSE;
1882 return arabic_combine(utf_ptr2char(p1), c2);
1883}
1884#endif
1885
1886/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001887 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 * composing characters.
1889 */
1890 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001891utfc_ptr2char(p, pcc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001893 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 int len;
1896 int c;
1897 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001898 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001901 len = utf_ptr2len(p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001902
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 /* Only accept a composing char when the first char isn't illegal. */
1904 if ((len > 1 || *p < 0x80)
1905 && p[len] >= 0x80
1906 && UTF_COMPOSINGLIKE(p, p + len))
1907 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001908 cc = utf_ptr2char(p + len);
1909 for (;;)
1910 {
1911 pcc[i++] = cc;
1912 if (i == MAX_MCO)
1913 break;
1914 len += utf_ptr2len(p + len);
1915 if (p[len] < 0x80 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1916 break;
1917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001919
1920 if (i < MAX_MCO) /* last composing char must be 0 */
1921 pcc[i] = 0;
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 return c;
1924}
1925
1926/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001927 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 * composing characters. Use no more than p[maxlen].
1929 */
1930 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001931utfc_ptr2char_len(p, pcc, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001933 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 int maxlen;
1935{
1936 int len;
1937 int c;
1938 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001939 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
1941 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001942 len = utf_ptr2len_len(p, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 /* Only accept a composing char when the first char isn't illegal. */
1944 if ((len > 1 || *p < 0x80)
1945 && len < maxlen
1946 && p[len] >= 0x80
1947 && UTF_COMPOSINGLIKE(p, p + len))
1948 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001949 cc = utf_ptr2char(p + len);
1950 for (;;)
1951 {
1952 pcc[i++] = cc;
1953 if (i == MAX_MCO)
1954 break;
1955 len += utf_ptr2len_len(p + len, maxlen - len);
1956 if (len >= maxlen
1957 || p[len] < 0x80
1958 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1959 break;
1960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001962
1963 if (i < MAX_MCO) /* last composing char must be 0 */
1964 pcc[i] = 0;
1965
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 return c;
1967}
1968
1969/*
1970 * Convert the character at screen position "off" to a sequence of bytes.
1971 * Includes the composing characters.
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001972 * "buf" must at least have the length MB_MAXBYTES + 1.
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02001973 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 * Returns the produced number of bytes.
1975 */
1976 int
1977utfc_char2bytes(off, buf)
1978 int off;
1979 char_u *buf;
1980{
1981 int len;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001982 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984 len = utf_char2bytes(ScreenLinesUC[off], buf);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001985 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001987 if (ScreenLinesC[i][off] == 0)
1988 break;
1989 len += utf_char2bytes(ScreenLinesC[i][off], buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 return len;
1992}
1993
1994/*
1995 * Get the length of a UTF-8 byte sequence, not including any following
1996 * composing characters.
1997 * Returns 0 for "".
1998 * Returns 1 for an illegal byte sequence.
1999 */
2000 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002001utf_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 char_u *p;
2003{
2004 int len;
2005 int i;
2006
2007 if (*p == NUL)
2008 return 0;
2009 len = utf8len_tab[*p];
2010 for (i = 1; i < len; ++i)
2011 if ((p[i] & 0xc0) != 0x80)
2012 return 1;
2013 return len;
2014}
2015
2016/*
2017 * Return length of UTF-8 character, obtained from the first byte.
2018 * "b" must be between 0 and 255!
Bram Moolenaar24397332009-12-02 14:02:39 +00002019 * Returns 1 for an invalid first byte value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 */
2021 int
2022utf_byte2len(b)
2023 int b;
2024{
2025 return utf8len_tab[b];
2026}
2027
2028/*
2029 * Get the length of UTF-8 byte sequence "p[size]". Does not include any
2030 * following composing characters.
2031 * Returns 1 for "".
Bram Moolenaarc048f672008-01-04 16:47:25 +00002032 * Returns 1 for an illegal byte sequence (also in incomplete byte seq.).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 * Returns number > "size" for an incomplete byte sequence.
Bram Moolenaar24397332009-12-02 14:02:39 +00002034 * Never returns zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 */
2036 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002037utf_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 char_u *p;
2039 int size;
2040{
2041 int len;
2042 int i;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002043 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
Bram Moolenaar24397332009-12-02 14:02:39 +00002045 len = utf8len_tab[*p];
2046 if (len == 1)
2047 return 1; /* NUL, ascii or illegal lead byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (len > size)
Bram Moolenaarc048f672008-01-04 16:47:25 +00002049 m = size; /* incomplete byte sequence. */
Bram Moolenaar24397332009-12-02 14:02:39 +00002050 else
2051 m = len;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002052 for (i = 1; i < m; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if ((p[i] & 0xc0) != 0x80)
2054 return 1;
2055 return len;
2056}
2057
2058/*
2059 * Return the number of bytes the UTF-8 encoding of the character at "p" takes.
2060 * This includes following composing characters.
2061 */
2062 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002063utfc_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 char_u *p;
2065{
2066 int len;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002067 int b0 = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068#ifdef FEAT_ARABIC
2069 int prevlen;
2070#endif
2071
Bram Moolenaarc669e662005-06-08 22:00:03 +00002072 if (b0 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 return 0;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002074 if (b0 < 0x80 && p[1] < 0x80) /* be quick for ASCII */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return 1;
2076
2077 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002078 len = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080 /* Check for illegal byte. */
Bram Moolenaarc669e662005-06-08 22:00:03 +00002081 if (len == 1 && b0 >= 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 return 1;
2083
2084 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002085 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 * skip all of them (otherwise the cursor would get stuck).
2087 */
2088#ifdef FEAT_ARABIC
2089 prevlen = 0;
2090#endif
2091 for (;;)
2092 {
2093 if (p[len] < 0x80 || !UTF_COMPOSINGLIKE(p + prevlen, p + len))
2094 return len;
2095
2096 /* Skip over composing char */
2097#ifdef FEAT_ARABIC
2098 prevlen = len;
2099#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002100 len += utf_ptr2len(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102}
2103
2104/*
2105 * Return the number of bytes the UTF-8 encoding of the character at "p[size]"
2106 * takes. This includes following composing characters.
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002107 * Returns 0 for an empty string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 * Returns 1 for an illegal char or an incomplete byte sequence.
2109 */
2110 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002111utfc_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 char_u *p;
2113 int size;
2114{
2115 int len;
2116#ifdef FEAT_ARABIC
2117 int prevlen;
2118#endif
2119
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002120 if (size < 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 return 0;
2122 if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */
2123 return 1;
2124
2125 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002126 len = utf_ptr2len_len(p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 /* Check for illegal byte and incomplete byte sequence. */
2129 if ((len == 1 && p[0] >= 0x80) || len > size)
2130 return 1;
2131
2132 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002133 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 * skip all of them (otherwise the cursor would get stuck).
2135 */
2136#ifdef FEAT_ARABIC
2137 prevlen = 0;
2138#endif
2139 while (len < size)
2140 {
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002141 int len_next_char;
2142
2143 if (p[len] < 0x80)
2144 break;
2145
2146 /*
2147 * Next character length should not go beyond size to ensure that
2148 * UTF_COMPOSINGLIKE(...) does not read beyond size.
2149 */
2150 len_next_char = utf_ptr2len_len(p + len, size - len);
2151 if (len_next_char > size - len)
2152 break;
2153
2154 if (!UTF_COMPOSINGLIKE(p + prevlen, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 break;
2156
2157 /* Skip over composing char */
2158#ifdef FEAT_ARABIC
2159 prevlen = len;
2160#endif
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002161 len += len_next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163 return len;
2164}
2165
2166/*
2167 * Return the number of bytes the UTF-8 encoding of character "c" takes.
2168 * This does not include composing characters.
2169 */
2170 int
2171utf_char2len(c)
2172 int c;
2173{
2174 if (c < 0x80)
2175 return 1;
2176 if (c < 0x800)
2177 return 2;
2178 if (c < 0x10000)
2179 return 3;
2180 if (c < 0x200000)
2181 return 4;
2182 if (c < 0x4000000)
2183 return 5;
2184 return 6;
2185}
2186
2187/*
2188 * Convert Unicode character "c" to UTF-8 string in "buf[]".
2189 * Returns the number of bytes.
2190 * This does not include composing characters.
2191 */
2192 int
2193utf_char2bytes(c, buf)
2194 int c;
2195 char_u *buf;
2196{
2197 if (c < 0x80) /* 7 bits */
2198 {
2199 buf[0] = c;
2200 return 1;
2201 }
2202 if (c < 0x800) /* 11 bits */
2203 {
2204 buf[0] = 0xc0 + ((unsigned)c >> 6);
2205 buf[1] = 0x80 + (c & 0x3f);
2206 return 2;
2207 }
2208 if (c < 0x10000) /* 16 bits */
2209 {
2210 buf[0] = 0xe0 + ((unsigned)c >> 12);
2211 buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2212 buf[2] = 0x80 + (c & 0x3f);
2213 return 3;
2214 }
2215 if (c < 0x200000) /* 21 bits */
2216 {
2217 buf[0] = 0xf0 + ((unsigned)c >> 18);
2218 buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2219 buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2220 buf[3] = 0x80 + (c & 0x3f);
2221 return 4;
2222 }
2223 if (c < 0x4000000) /* 26 bits */
2224 {
2225 buf[0] = 0xf8 + ((unsigned)c >> 24);
2226 buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2227 buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2228 buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2229 buf[4] = 0x80 + (c & 0x3f);
2230 return 5;
2231 }
2232 /* 31 bits */
2233 buf[0] = 0xfc + ((unsigned)c >> 30);
2234 buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f);
2235 buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2236 buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2237 buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2238 buf[5] = 0x80 + (c & 0x3f);
2239 return 6;
2240}
2241
2242/*
2243 * Return TRUE if "c" is a composing UTF-8 character. This means it will be
2244 * drawn on top of the preceding character.
2245 * Based on code from Markus Kuhn.
2246 */
2247 int
2248utf_iscomposing(c)
2249 int c;
2250{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002251 /* Sorted list of non-overlapping intervals.
2252 * Generated by ../runtime/tools/unicode.vim. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 static struct interval combining[] =
2254 {
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002255 {0x0300, 0x036f},
2256 {0x0483, 0x0489},
2257 {0x0591, 0x05bd},
2258 {0x05bf, 0x05bf},
2259 {0x05c1, 0x05c2},
2260 {0x05c4, 0x05c5},
2261 {0x05c7, 0x05c7},
2262 {0x0610, 0x061a},
2263 {0x064b, 0x065e},
2264 {0x0670, 0x0670},
2265 {0x06d6, 0x06dc},
2266 {0x06de, 0x06e4},
2267 {0x06e7, 0x06e8},
2268 {0x06ea, 0x06ed},
2269 {0x0711, 0x0711},
2270 {0x0730, 0x074a},
2271 {0x07a6, 0x07b0},
2272 {0x07eb, 0x07f3},
2273 {0x0816, 0x0819},
2274 {0x081b, 0x0823},
2275 {0x0825, 0x0827},
2276 {0x0829, 0x082d},
2277 {0x0900, 0x0903},
2278 {0x093c, 0x093c},
2279 {0x093e, 0x094e},
2280 {0x0951, 0x0955},
2281 {0x0962, 0x0963},
2282 {0x0981, 0x0983},
2283 {0x09bc, 0x09bc},
2284 {0x09be, 0x09c4},
2285 {0x09c7, 0x09c8},
2286 {0x09cb, 0x09cd},
2287 {0x09d7, 0x09d7},
2288 {0x09e2, 0x09e3},
2289 {0x0a01, 0x0a03},
2290 {0x0a3c, 0x0a3c},
2291 {0x0a3e, 0x0a42},
2292 {0x0a47, 0x0a48},
2293 {0x0a4b, 0x0a4d},
2294 {0x0a51, 0x0a51},
2295 {0x0a70, 0x0a71},
2296 {0x0a75, 0x0a75},
2297 {0x0a81, 0x0a83},
2298 {0x0abc, 0x0abc},
2299 {0x0abe, 0x0ac5},
2300 {0x0ac7, 0x0ac9},
2301 {0x0acb, 0x0acd},
2302 {0x0ae2, 0x0ae3},
2303 {0x0b01, 0x0b03},
2304 {0x0b3c, 0x0b3c},
2305 {0x0b3e, 0x0b44},
2306 {0x0b47, 0x0b48},
2307 {0x0b4b, 0x0b4d},
2308 {0x0b56, 0x0b57},
2309 {0x0b62, 0x0b63},
2310 {0x0b82, 0x0b82},
2311 {0x0bbe, 0x0bc2},
2312 {0x0bc6, 0x0bc8},
2313 {0x0bca, 0x0bcd},
2314 {0x0bd7, 0x0bd7},
2315 {0x0c01, 0x0c03},
2316 {0x0c3e, 0x0c44},
2317 {0x0c46, 0x0c48},
2318 {0x0c4a, 0x0c4d},
2319 {0x0c55, 0x0c56},
2320 {0x0c62, 0x0c63},
2321 {0x0c82, 0x0c83},
2322 {0x0cbc, 0x0cbc},
2323 {0x0cbe, 0x0cc4},
2324 {0x0cc6, 0x0cc8},
2325 {0x0cca, 0x0ccd},
2326 {0x0cd5, 0x0cd6},
2327 {0x0ce2, 0x0ce3},
2328 {0x0d02, 0x0d03},
2329 {0x0d3e, 0x0d44},
2330 {0x0d46, 0x0d48},
2331 {0x0d4a, 0x0d4d},
2332 {0x0d57, 0x0d57},
2333 {0x0d62, 0x0d63},
2334 {0x0d82, 0x0d83},
2335 {0x0dca, 0x0dca},
2336 {0x0dcf, 0x0dd4},
2337 {0x0dd6, 0x0dd6},
2338 {0x0dd8, 0x0ddf},
2339 {0x0df2, 0x0df3},
2340 {0x0e31, 0x0e31},
2341 {0x0e34, 0x0e3a},
2342 {0x0e47, 0x0e4e},
2343 {0x0eb1, 0x0eb1},
2344 {0x0eb4, 0x0eb9},
2345 {0x0ebb, 0x0ebc},
2346 {0x0ec8, 0x0ecd},
2347 {0x0f18, 0x0f19},
2348 {0x0f35, 0x0f35},
2349 {0x0f37, 0x0f37},
2350 {0x0f39, 0x0f39},
2351 {0x0f3e, 0x0f3f},
2352 {0x0f71, 0x0f84},
2353 {0x0f86, 0x0f87},
2354 {0x0f90, 0x0f97},
2355 {0x0f99, 0x0fbc},
2356 {0x0fc6, 0x0fc6},
2357 {0x102b, 0x103e},
2358 {0x1056, 0x1059},
2359 {0x105e, 0x1060},
2360 {0x1062, 0x1064},
2361 {0x1067, 0x106d},
2362 {0x1071, 0x1074},
2363 {0x1082, 0x108d},
2364 {0x108f, 0x108f},
2365 {0x109a, 0x109d},
2366 {0x135f, 0x135f},
2367 {0x1712, 0x1714},
2368 {0x1732, 0x1734},
2369 {0x1752, 0x1753},
2370 {0x1772, 0x1773},
2371 {0x17b6, 0x17d3},
2372 {0x17dd, 0x17dd},
2373 {0x180b, 0x180d},
2374 {0x18a9, 0x18a9},
2375 {0x1920, 0x192b},
2376 {0x1930, 0x193b},
2377 {0x19b0, 0x19c0},
2378 {0x19c8, 0x19c9},
2379 {0x1a17, 0x1a1b},
2380 {0x1a55, 0x1a5e},
2381 {0x1a60, 0x1a7c},
2382 {0x1a7f, 0x1a7f},
2383 {0x1b00, 0x1b04},
2384 {0x1b34, 0x1b44},
2385 {0x1b6b, 0x1b73},
2386 {0x1b80, 0x1b82},
2387 {0x1ba1, 0x1baa},
2388 {0x1c24, 0x1c37},
2389 {0x1cd0, 0x1cd2},
2390 {0x1cd4, 0x1ce8},
2391 {0x1ced, 0x1ced},
2392 {0x1cf2, 0x1cf2},
2393 {0x1dc0, 0x1de6},
2394 {0x1dfd, 0x1dff},
2395 {0x20d0, 0x20f0},
2396 {0x2cef, 0x2cf1},
2397 {0x2de0, 0x2dff},
2398 {0x302a, 0x302f},
2399 {0x3099, 0x309a},
2400 {0xa66f, 0xa672},
2401 {0xa67c, 0xa67d},
2402 {0xa6f0, 0xa6f1},
2403 {0xa802, 0xa802},
2404 {0xa806, 0xa806},
2405 {0xa80b, 0xa80b},
2406 {0xa823, 0xa827},
2407 {0xa880, 0xa881},
2408 {0xa8b4, 0xa8c4},
2409 {0xa8e0, 0xa8f1},
2410 {0xa926, 0xa92d},
2411 {0xa947, 0xa953},
2412 {0xa980, 0xa983},
2413 {0xa9b3, 0xa9c0},
2414 {0xaa29, 0xaa36},
2415 {0xaa43, 0xaa43},
2416 {0xaa4c, 0xaa4d},
2417 {0xaa7b, 0xaa7b},
2418 {0xaab0, 0xaab0},
2419 {0xaab2, 0xaab4},
2420 {0xaab7, 0xaab8},
2421 {0xaabe, 0xaabf},
2422 {0xaac1, 0xaac1},
2423 {0xabe3, 0xabea},
2424 {0xabec, 0xabed},
2425 {0xfb1e, 0xfb1e},
2426 {0xfe00, 0xfe0f},
2427 {0xfe20, 0xfe26},
2428 {0x101fd, 0x101fd},
2429 {0x10a01, 0x10a03},
2430 {0x10a05, 0x10a06},
2431 {0x10a0c, 0x10a0f},
2432 {0x10a38, 0x10a3a},
2433 {0x10a3f, 0x10a3f},
2434 {0x11080, 0x11082},
2435 {0x110b0, 0x110ba},
2436 {0x1d165, 0x1d169},
2437 {0x1d16d, 0x1d172},
2438 {0x1d17b, 0x1d182},
2439 {0x1d185, 0x1d18b},
2440 {0x1d1aa, 0x1d1ad},
2441 {0x1d242, 0x1d244},
2442 {0xe0100, 0xe01ef}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 };
2444
2445 return intable(combining, sizeof(combining), c);
2446}
2447
2448/*
2449 * Return TRUE for characters that can be displayed in a normal way.
2450 * Only for characters of 0x100 and above!
2451 */
2452 int
2453utf_printable(c)
2454 int c;
2455{
2456#ifdef USE_WCHAR_FUNCTIONS
2457 /*
2458 * Assume the iswprint() library function works better than our own stuff.
2459 */
2460 return iswprint(c);
2461#else
2462 /* Sorted list of non-overlapping intervals.
2463 * 0xd800-0xdfff is reserved for UTF-16, actually illegal. */
2464 static struct interval nonprint[] =
2465 {
2466 {0x070f, 0x070f}, {0x180b, 0x180e}, {0x200b, 0x200f}, {0x202a, 0x202e},
2467 {0x206a, 0x206f}, {0xd800, 0xdfff}, {0xfeff, 0xfeff}, {0xfff9, 0xfffb},
2468 {0xfffe, 0xffff}
2469 };
2470
2471 return !intable(nonprint, sizeof(nonprint), c);
2472#endif
2473}
2474
2475/*
2476 * Get class of a Unicode character.
2477 * 0: white space
2478 * 1: punctuation
2479 * 2 or bigger: some class of word character.
2480 */
2481 int
2482utf_class(c)
2483 int c;
2484{
2485 /* sorted list of non-overlapping intervals */
2486 static struct clinterval
2487 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002488 unsigned int first;
2489 unsigned int last;
2490 unsigned int class;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 } classes[] =
2492 {
2493 {0x037e, 0x037e, 1}, /* Greek question mark */
2494 {0x0387, 0x0387, 1}, /* Greek ano teleia */
2495 {0x055a, 0x055f, 1}, /* Armenian punctuation */
2496 {0x0589, 0x0589, 1}, /* Armenian full stop */
2497 {0x05be, 0x05be, 1},
2498 {0x05c0, 0x05c0, 1},
2499 {0x05c3, 0x05c3, 1},
2500 {0x05f3, 0x05f4, 1},
2501 {0x060c, 0x060c, 1},
2502 {0x061b, 0x061b, 1},
2503 {0x061f, 0x061f, 1},
2504 {0x066a, 0x066d, 1},
2505 {0x06d4, 0x06d4, 1},
2506 {0x0700, 0x070d, 1}, /* Syriac punctuation */
2507 {0x0964, 0x0965, 1},
2508 {0x0970, 0x0970, 1},
2509 {0x0df4, 0x0df4, 1},
2510 {0x0e4f, 0x0e4f, 1},
2511 {0x0e5a, 0x0e5b, 1},
2512 {0x0f04, 0x0f12, 1},
2513 {0x0f3a, 0x0f3d, 1},
2514 {0x0f85, 0x0f85, 1},
2515 {0x104a, 0x104f, 1}, /* Myanmar punctuation */
2516 {0x10fb, 0x10fb, 1}, /* Georgian punctuation */
2517 {0x1361, 0x1368, 1}, /* Ethiopic punctuation */
2518 {0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
2519 {0x1680, 0x1680, 0},
2520 {0x169b, 0x169c, 1},
2521 {0x16eb, 0x16ed, 1},
2522 {0x1735, 0x1736, 1},
2523 {0x17d4, 0x17dc, 1}, /* Khmer punctuation */
2524 {0x1800, 0x180a, 1}, /* Mongolian punctuation */
2525 {0x2000, 0x200b, 0}, /* spaces */
2526 {0x200c, 0x2027, 1}, /* punctuation and symbols */
2527 {0x2028, 0x2029, 0},
2528 {0x202a, 0x202e, 1}, /* punctuation and symbols */
2529 {0x202f, 0x202f, 0},
2530 {0x2030, 0x205e, 1}, /* punctuation and symbols */
2531 {0x205f, 0x205f, 0},
2532 {0x2060, 0x27ff, 1}, /* punctuation and symbols */
2533 {0x2070, 0x207f, 0x2070}, /* superscript */
Bram Moolenaarbbb79722008-06-04 09:00:32 +00002534 {0x2080, 0x2094, 0x2080}, /* subscript */
2535 {0x20a0, 0x27ff, 1}, /* all kinds of symbols */
2536 {0x2800, 0x28ff, 0x2800}, /* braille */
2537 {0x2900, 0x2998, 1}, /* arrows, brackets, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {0x29d8, 0x29db, 1},
2539 {0x29fc, 0x29fd, 1},
Bram Moolenaar103650d2014-09-15 14:25:54 +02002540 {0x2e00, 0x2e7f, 1}, /* supplemental punctuation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {0x3000, 0x3000, 0}, /* ideographic space */
2542 {0x3001, 0x3020, 1}, /* ideographic punctuation */
2543 {0x3030, 0x3030, 1},
2544 {0x303d, 0x303d, 1},
2545 {0x3040, 0x309f, 0x3040}, /* Hiragana */
2546 {0x30a0, 0x30ff, 0x30a0}, /* Katakana */
2547 {0x3300, 0x9fff, 0x4e00}, /* CJK Ideographs */
2548 {0xac00, 0xd7a3, 0xac00}, /* Hangul Syllables */
2549 {0xf900, 0xfaff, 0x4e00}, /* CJK Ideographs */
2550 {0xfd3e, 0xfd3f, 1},
2551 {0xfe30, 0xfe6b, 1}, /* punctuation forms */
2552 {0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
2553 {0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
2554 {0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
2555 {0xff5b, 0xff65, 1}, /* half/fullwidth ASCII */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002556 {0x20000, 0x2a6df, 0x4e00}, /* CJK Ideographs */
2557 {0x2a700, 0x2b73f, 0x4e00}, /* CJK Ideographs */
2558 {0x2b740, 0x2b81f, 0x4e00}, /* CJK Ideographs */
2559 {0x2f800, 0x2fa1f, 0x4e00}, /* CJK Ideographs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 };
2561 int bot = 0;
2562 int top = sizeof(classes) / sizeof(struct clinterval) - 1;
2563 int mid;
2564
2565 /* First quick check for Latin1 characters, use 'iskeyword'. */
2566 if (c < 0x100)
2567 {
Bram Moolenaarf7bbbc52005-06-17 21:55:00 +00002568 if (c == ' ' || c == '\t' || c == NUL || c == 0xa0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 return 0; /* blank */
2570 if (vim_iswordc(c))
2571 return 2; /* word character */
2572 return 1; /* punctuation */
2573 }
2574
2575 /* binary search in table */
2576 while (top >= bot)
2577 {
2578 mid = (bot + top) / 2;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002579 if (classes[mid].last < (unsigned int)c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 bot = mid + 1;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002581 else if (classes[mid].first > (unsigned int)c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 top = mid - 1;
2583 else
2584 return (int)classes[mid].class;
2585 }
2586
2587 /* most other characters are "word" characters */
2588 return 2;
2589}
2590
2591/*
2592 * Code for Unicode case-dependent operations. Based on notes in
2593 * http://www.unicode.org/Public/UNIDATA/CaseFolding.txt
2594 * This code uses simple case folding, not full case folding.
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002595 * Last updated for Unicode 5.2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
2597
2598/*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002599 * The following tables are built by ../runtime/tools/unicode.vim.
2600 * They must be in numeric order, because we use binary search.
2601 * An entry such as {0x41,0x5a,1,32} means that Unicode characters in the
2602 * range from 0x41 to 0x5a inclusive, stepping by 1, are changed to
2603 * folded/upper/lower by adding 32.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605typedef struct
2606{
2607 int rangeStart;
2608 int rangeEnd;
2609 int step;
2610 int offset;
2611} convertStruct;
2612
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002613static convertStruct foldCase[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002615 {0x41,0x5a,1,32},
2616 {0xb5,0xb5,-1,775},
2617 {0xc0,0xd6,1,32},
2618 {0xd8,0xde,1,32},
2619 {0x100,0x12e,2,1},
2620 {0x132,0x136,2,1},
2621 {0x139,0x147,2,1},
2622 {0x14a,0x176,2,1},
2623 {0x178,0x178,-1,-121},
2624 {0x179,0x17d,2,1},
2625 {0x17f,0x17f,-1,-268},
2626 {0x181,0x181,-1,210},
2627 {0x182,0x184,2,1},
2628 {0x186,0x186,-1,206},
2629 {0x187,0x187,-1,1},
2630 {0x189,0x18a,1,205},
2631 {0x18b,0x18b,-1,1},
2632 {0x18e,0x18e,-1,79},
2633 {0x18f,0x18f,-1,202},
2634 {0x190,0x190,-1,203},
2635 {0x191,0x191,-1,1},
2636 {0x193,0x193,-1,205},
2637 {0x194,0x194,-1,207},
2638 {0x196,0x196,-1,211},
2639 {0x197,0x197,-1,209},
2640 {0x198,0x198,-1,1},
2641 {0x19c,0x19c,-1,211},
2642 {0x19d,0x19d,-1,213},
2643 {0x19f,0x19f,-1,214},
2644 {0x1a0,0x1a4,2,1},
2645 {0x1a6,0x1a6,-1,218},
2646 {0x1a7,0x1a7,-1,1},
2647 {0x1a9,0x1a9,-1,218},
2648 {0x1ac,0x1ac,-1,1},
2649 {0x1ae,0x1ae,-1,218},
2650 {0x1af,0x1af,-1,1},
2651 {0x1b1,0x1b2,1,217},
2652 {0x1b3,0x1b5,2,1},
2653 {0x1b7,0x1b7,-1,219},
2654 {0x1b8,0x1bc,4,1},
2655 {0x1c4,0x1c4,-1,2},
2656 {0x1c5,0x1c5,-1,1},
2657 {0x1c7,0x1c7,-1,2},
2658 {0x1c8,0x1c8,-1,1},
2659 {0x1ca,0x1ca,-1,2},
2660 {0x1cb,0x1db,2,1},
2661 {0x1de,0x1ee,2,1},
2662 {0x1f1,0x1f1,-1,2},
2663 {0x1f2,0x1f4,2,1},
2664 {0x1f6,0x1f6,-1,-97},
2665 {0x1f7,0x1f7,-1,-56},
2666 {0x1f8,0x21e,2,1},
2667 {0x220,0x220,-1,-130},
2668 {0x222,0x232,2,1},
2669 {0x23a,0x23a,-1,10795},
2670 {0x23b,0x23b,-1,1},
2671 {0x23d,0x23d,-1,-163},
2672 {0x23e,0x23e,-1,10792},
2673 {0x241,0x241,-1,1},
2674 {0x243,0x243,-1,-195},
2675 {0x244,0x244,-1,69},
2676 {0x245,0x245,-1,71},
2677 {0x246,0x24e,2,1},
2678 {0x345,0x345,-1,116},
2679 {0x370,0x372,2,1},
2680 {0x376,0x376,-1,1},
2681 {0x386,0x386,-1,38},
2682 {0x388,0x38a,1,37},
2683 {0x38c,0x38c,-1,64},
2684 {0x38e,0x38f,1,63},
2685 {0x391,0x3a1,1,32},
2686 {0x3a3,0x3ab,1,32},
2687 {0x3c2,0x3c2,-1,1},
2688 {0x3cf,0x3cf,-1,8},
2689 {0x3d0,0x3d0,-1,-30},
2690 {0x3d1,0x3d1,-1,-25},
2691 {0x3d5,0x3d5,-1,-15},
2692 {0x3d6,0x3d6,-1,-22},
2693 {0x3d8,0x3ee,2,1},
2694 {0x3f0,0x3f0,-1,-54},
2695 {0x3f1,0x3f1,-1,-48},
2696 {0x3f4,0x3f4,-1,-60},
2697 {0x3f5,0x3f5,-1,-64},
2698 {0x3f7,0x3f7,-1,1},
2699 {0x3f9,0x3f9,-1,-7},
2700 {0x3fa,0x3fa,-1,1},
2701 {0x3fd,0x3ff,1,-130},
2702 {0x400,0x40f,1,80},
2703 {0x410,0x42f,1,32},
2704 {0x460,0x480,2,1},
2705 {0x48a,0x4be,2,1},
2706 {0x4c0,0x4c0,-1,15},
2707 {0x4c1,0x4cd,2,1},
2708 {0x4d0,0x524,2,1},
2709 {0x531,0x556,1,48},
2710 {0x10a0,0x10c5,1,7264},
2711 {0x1e00,0x1e94,2,1},
2712 {0x1e9b,0x1e9b,-1,-58},
2713 {0x1e9e,0x1e9e,-1,-7615},
2714 {0x1ea0,0x1efe,2,1},
2715 {0x1f08,0x1f0f,1,-8},
2716 {0x1f18,0x1f1d,1,-8},
2717 {0x1f28,0x1f2f,1,-8},
2718 {0x1f38,0x1f3f,1,-8},
2719 {0x1f48,0x1f4d,1,-8},
2720 {0x1f59,0x1f5f,2,-8},
2721 {0x1f68,0x1f6f,1,-8},
2722 {0x1f88,0x1f8f,1,-8},
2723 {0x1f98,0x1f9f,1,-8},
2724 {0x1fa8,0x1faf,1,-8},
2725 {0x1fb8,0x1fb9,1,-8},
2726 {0x1fba,0x1fbb,1,-74},
2727 {0x1fbc,0x1fbc,-1,-9},
2728 {0x1fbe,0x1fbe,-1,-7173},
2729 {0x1fc8,0x1fcb,1,-86},
2730 {0x1fcc,0x1fcc,-1,-9},
2731 {0x1fd8,0x1fd9,1,-8},
2732 {0x1fda,0x1fdb,1,-100},
2733 {0x1fe8,0x1fe9,1,-8},
2734 {0x1fea,0x1feb,1,-112},
2735 {0x1fec,0x1fec,-1,-7},
2736 {0x1ff8,0x1ff9,1,-128},
2737 {0x1ffa,0x1ffb,1,-126},
2738 {0x1ffc,0x1ffc,-1,-9},
2739 {0x2126,0x2126,-1,-7517},
2740 {0x212a,0x212a,-1,-8383},
2741 {0x212b,0x212b,-1,-8262},
2742 {0x2132,0x2132,-1,28},
2743 {0x2160,0x216f,1,16},
2744 {0x2183,0x2183,-1,1},
2745 {0x24b6,0x24cf,1,26},
2746 {0x2c00,0x2c2e,1,48},
2747 {0x2c60,0x2c60,-1,1},
2748 {0x2c62,0x2c62,-1,-10743},
2749 {0x2c63,0x2c63,-1,-3814},
2750 {0x2c64,0x2c64,-1,-10727},
2751 {0x2c67,0x2c6b,2,1},
2752 {0x2c6d,0x2c6d,-1,-10780},
2753 {0x2c6e,0x2c6e,-1,-10749},
2754 {0x2c6f,0x2c6f,-1,-10783},
2755 {0x2c70,0x2c70,-1,-10782},
2756 {0x2c72,0x2c75,3,1},
2757 {0x2c7e,0x2c7f,1,-10815},
2758 {0x2c80,0x2ce2,2,1},
2759 {0x2ceb,0x2ced,2,1},
2760 {0xa640,0xa65e,2,1},
2761 {0xa662,0xa66c,2,1},
2762 {0xa680,0xa696,2,1},
2763 {0xa722,0xa72e,2,1},
2764 {0xa732,0xa76e,2,1},
2765 {0xa779,0xa77b,2,1},
2766 {0xa77d,0xa77d,-1,-35332},
2767 {0xa77e,0xa786,2,1},
2768 {0xa78b,0xa78b,-1,1},
2769 {0xff21,0xff3a,1,32},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 {0x10400,0x10427,1,40}
2771};
2772
Bram Moolenaar35ee4522011-07-15 21:16:59 +02002773static int utf_convert __ARGS((int a, convertStruct table[], int tableSize));
2774static int utf_strnicmp __ARGS((char_u *s1, char_u *s2, size_t n1, size_t n2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776/*
2777 * Generic conversion function for case operations.
2778 * Return the converted equivalent of "a", which is a UCS-4 character. Use
2779 * the given conversion "table". Uses binary search on "table".
2780 */
2781 static int
2782utf_convert(a, table, tableSize)
2783 int a;
2784 convertStruct table[];
2785 int tableSize;
2786{
2787 int start, mid, end; /* indices into table */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002788 int entries = tableSize / sizeof(convertStruct);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 start = 0;
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002791 end = entries;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 while (start < end)
2793 {
2794 /* need to search further */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002795 mid = (end + start) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (table[mid].rangeEnd < a)
2797 start = mid + 1;
2798 else
2799 end = mid;
2800 }
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002801 if (start < entries
2802 && table[start].rangeStart <= a
2803 && a <= table[start].rangeEnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 && (a - table[start].rangeStart) % table[start].step == 0)
2805 return (a + table[start].offset);
2806 else
2807 return a;
2808}
2809
2810/*
2811 * Return the folded-case equivalent of "a", which is a UCS-4 character. Uses
2812 * simple case folding.
2813 */
2814 int
2815utf_fold(a)
2816 int a;
2817{
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002818 return utf_convert(a, foldCase, (int)sizeof(foldCase));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819}
2820
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002821static convertStruct toLower[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002823 {0x41,0x5a,1,32},
2824 {0xc0,0xd6,1,32},
2825 {0xd8,0xde,1,32},
2826 {0x100,0x12e,2,1},
2827 {0x130,0x130,-1,-199},
2828 {0x132,0x136,2,1},
2829 {0x139,0x147,2,1},
2830 {0x14a,0x176,2,1},
2831 {0x178,0x178,-1,-121},
2832 {0x179,0x17d,2,1},
2833 {0x181,0x181,-1,210},
2834 {0x182,0x184,2,1},
2835 {0x186,0x186,-1,206},
2836 {0x187,0x187,-1,1},
2837 {0x189,0x18a,1,205},
2838 {0x18b,0x18b,-1,1},
2839 {0x18e,0x18e,-1,79},
2840 {0x18f,0x18f,-1,202},
2841 {0x190,0x190,-1,203},
2842 {0x191,0x191,-1,1},
2843 {0x193,0x193,-1,205},
2844 {0x194,0x194,-1,207},
2845 {0x196,0x196,-1,211},
2846 {0x197,0x197,-1,209},
2847 {0x198,0x198,-1,1},
2848 {0x19c,0x19c,-1,211},
2849 {0x19d,0x19d,-1,213},
2850 {0x19f,0x19f,-1,214},
2851 {0x1a0,0x1a4,2,1},
2852 {0x1a6,0x1a6,-1,218},
2853 {0x1a7,0x1a7,-1,1},
2854 {0x1a9,0x1a9,-1,218},
2855 {0x1ac,0x1ac,-1,1},
2856 {0x1ae,0x1ae,-1,218},
2857 {0x1af,0x1af,-1,1},
2858 {0x1b1,0x1b2,1,217},
2859 {0x1b3,0x1b5,2,1},
2860 {0x1b7,0x1b7,-1,219},
2861 {0x1b8,0x1bc,4,1},
2862 {0x1c4,0x1c4,-1,2},
2863 {0x1c5,0x1c5,-1,1},
2864 {0x1c7,0x1c7,-1,2},
2865 {0x1c8,0x1c8,-1,1},
2866 {0x1ca,0x1ca,-1,2},
2867 {0x1cb,0x1db,2,1},
2868 {0x1de,0x1ee,2,1},
2869 {0x1f1,0x1f1,-1,2},
2870 {0x1f2,0x1f4,2,1},
2871 {0x1f6,0x1f6,-1,-97},
2872 {0x1f7,0x1f7,-1,-56},
2873 {0x1f8,0x21e,2,1},
2874 {0x220,0x220,-1,-130},
2875 {0x222,0x232,2,1},
2876 {0x23a,0x23a,-1,10795},
2877 {0x23b,0x23b,-1,1},
2878 {0x23d,0x23d,-1,-163},
2879 {0x23e,0x23e,-1,10792},
2880 {0x241,0x241,-1,1},
2881 {0x243,0x243,-1,-195},
2882 {0x244,0x244,-1,69},
2883 {0x245,0x245,-1,71},
2884 {0x246,0x24e,2,1},
2885 {0x370,0x372,2,1},
2886 {0x376,0x376,-1,1},
2887 {0x386,0x386,-1,38},
2888 {0x388,0x38a,1,37},
2889 {0x38c,0x38c,-1,64},
2890 {0x38e,0x38f,1,63},
2891 {0x391,0x3a1,1,32},
2892 {0x3a3,0x3ab,1,32},
2893 {0x3cf,0x3cf,-1,8},
2894 {0x3d8,0x3ee,2,1},
2895 {0x3f4,0x3f4,-1,-60},
2896 {0x3f7,0x3f7,-1,1},
2897 {0x3f9,0x3f9,-1,-7},
2898 {0x3fa,0x3fa,-1,1},
2899 {0x3fd,0x3ff,1,-130},
2900 {0x400,0x40f,1,80},
2901 {0x410,0x42f,1,32},
2902 {0x460,0x480,2,1},
2903 {0x48a,0x4be,2,1},
2904 {0x4c0,0x4c0,-1,15},
2905 {0x4c1,0x4cd,2,1},
2906 {0x4d0,0x524,2,1},
2907 {0x531,0x556,1,48},
2908 {0x10a0,0x10c5,1,7264},
2909 {0x1e00,0x1e94,2,1},
2910 {0x1e9e,0x1e9e,-1,-7615},
2911 {0x1ea0,0x1efe,2,1},
2912 {0x1f08,0x1f0f,1,-8},
2913 {0x1f18,0x1f1d,1,-8},
2914 {0x1f28,0x1f2f,1,-8},
2915 {0x1f38,0x1f3f,1,-8},
2916 {0x1f48,0x1f4d,1,-8},
2917 {0x1f59,0x1f5f,2,-8},
2918 {0x1f68,0x1f6f,1,-8},
2919 {0x1f88,0x1f8f,1,-8},
2920 {0x1f98,0x1f9f,1,-8},
2921 {0x1fa8,0x1faf,1,-8},
2922 {0x1fb8,0x1fb9,1,-8},
2923 {0x1fba,0x1fbb,1,-74},
2924 {0x1fbc,0x1fbc,-1,-9},
2925 {0x1fc8,0x1fcb,1,-86},
2926 {0x1fcc,0x1fcc,-1,-9},
2927 {0x1fd8,0x1fd9,1,-8},
2928 {0x1fda,0x1fdb,1,-100},
2929 {0x1fe8,0x1fe9,1,-8},
2930 {0x1fea,0x1feb,1,-112},
2931 {0x1fec,0x1fec,-1,-7},
2932 {0x1ff8,0x1ff9,1,-128},
2933 {0x1ffa,0x1ffb,1,-126},
2934 {0x1ffc,0x1ffc,-1,-9},
2935 {0x2126,0x2126,-1,-7517},
2936 {0x212a,0x212a,-1,-8383},
2937 {0x212b,0x212b,-1,-8262},
2938 {0x2132,0x2132,-1,28},
2939 {0x2160,0x216f,1,16},
2940 {0x2183,0x2183,-1,1},
2941 {0x24b6,0x24cf,1,26},
2942 {0x2c00,0x2c2e,1,48},
2943 {0x2c60,0x2c60,-1,1},
2944 {0x2c62,0x2c62,-1,-10743},
2945 {0x2c63,0x2c63,-1,-3814},
2946 {0x2c64,0x2c64,-1,-10727},
2947 {0x2c67,0x2c6b,2,1},
2948 {0x2c6d,0x2c6d,-1,-10780},
2949 {0x2c6e,0x2c6e,-1,-10749},
2950 {0x2c6f,0x2c6f,-1,-10783},
2951 {0x2c70,0x2c70,-1,-10782},
2952 {0x2c72,0x2c75,3,1},
2953 {0x2c7e,0x2c7f,1,-10815},
2954 {0x2c80,0x2ce2,2,1},
2955 {0x2ceb,0x2ced,2,1},
2956 {0xa640,0xa65e,2,1},
2957 {0xa662,0xa66c,2,1},
2958 {0xa680,0xa696,2,1},
2959 {0xa722,0xa72e,2,1},
2960 {0xa732,0xa76e,2,1},
2961 {0xa779,0xa77b,2,1},
2962 {0xa77d,0xa77d,-1,-35332},
2963 {0xa77e,0xa786,2,1},
2964 {0xa78b,0xa78b,-1,1},
2965 {0xff21,0xff3a,1,32},
2966 {0x10400,0x10427,1,40}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967};
2968
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002969static convertStruct toUpper[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002971 {0x61,0x7a,1,-32},
2972 {0xb5,0xb5,-1,743},
Bram Moolenaar88178de2012-06-01 17:46:59 +02002973 {0xe0,0xf6,1,-32}, /* 0xdf (German sharp s) is not upper-cased */
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002974 {0xf8,0xfe,1,-32},
2975 {0xff,0xff,-1,121},
2976 {0x101,0x12f,2,-1},
2977 {0x131,0x131,-1,-232},
2978 {0x133,0x137,2,-1},
2979 {0x13a,0x148,2,-1},
2980 {0x14b,0x177,2,-1},
2981 {0x17a,0x17e,2,-1},
2982 {0x17f,0x17f,-1,-300},
2983 {0x180,0x180,-1,195},
2984 {0x183,0x185,2,-1},
2985 {0x188,0x18c,4,-1},
2986 {0x192,0x192,-1,-1},
2987 {0x195,0x195,-1,97},
2988 {0x199,0x199,-1,-1},
2989 {0x19a,0x19a,-1,163},
2990 {0x19e,0x19e,-1,130},
2991 {0x1a1,0x1a5,2,-1},
2992 {0x1a8,0x1ad,5,-1},
2993 {0x1b0,0x1b4,4,-1},
2994 {0x1b6,0x1b9,3,-1},
2995 {0x1bd,0x1bd,-1,-1},
2996 {0x1bf,0x1bf,-1,56},
2997 {0x1c5,0x1c5,-1,-1},
2998 {0x1c6,0x1c6,-1,-2},
2999 {0x1c8,0x1c8,-1,-1},
3000 {0x1c9,0x1c9,-1,-2},
3001 {0x1cb,0x1cb,-1,-1},
3002 {0x1cc,0x1cc,-1,-2},
3003 {0x1ce,0x1dc,2,-1},
3004 {0x1dd,0x1dd,-1,-79},
3005 {0x1df,0x1ef,2,-1},
3006 {0x1f2,0x1f2,-1,-1},
3007 {0x1f3,0x1f3,-1,-2},
3008 {0x1f5,0x1f9,4,-1},
3009 {0x1fb,0x21f,2,-1},
3010 {0x223,0x233,2,-1},
3011 {0x23c,0x23c,-1,-1},
3012 {0x23f,0x240,1,10815},
3013 {0x242,0x247,5,-1},
3014 {0x249,0x24f,2,-1},
3015 {0x250,0x250,-1,10783},
3016 {0x251,0x251,-1,10780},
3017 {0x252,0x252,-1,10782},
3018 {0x253,0x253,-1,-210},
3019 {0x254,0x254,-1,-206},
3020 {0x256,0x257,1,-205},
3021 {0x259,0x259,-1,-202},
3022 {0x25b,0x25b,-1,-203},
3023 {0x260,0x260,-1,-205},
3024 {0x263,0x263,-1,-207},
3025 {0x268,0x268,-1,-209},
3026 {0x269,0x269,-1,-211},
3027 {0x26b,0x26b,-1,10743},
3028 {0x26f,0x26f,-1,-211},
3029 {0x271,0x271,-1,10749},
3030 {0x272,0x272,-1,-213},
3031 {0x275,0x275,-1,-214},
3032 {0x27d,0x27d,-1,10727},
3033 {0x280,0x283,3,-218},
3034 {0x288,0x288,-1,-218},
3035 {0x289,0x289,-1,-69},
3036 {0x28a,0x28b,1,-217},
3037 {0x28c,0x28c,-1,-71},
3038 {0x292,0x292,-1,-219},
3039 {0x345,0x345,-1,84},
3040 {0x371,0x373,2,-1},
3041 {0x377,0x377,-1,-1},
3042 {0x37b,0x37d,1,130},
3043 {0x3ac,0x3ac,-1,-38},
3044 {0x3ad,0x3af,1,-37},
3045 {0x3b1,0x3c1,1,-32},
3046 {0x3c2,0x3c2,-1,-31},
3047 {0x3c3,0x3cb,1,-32},
3048 {0x3cc,0x3cc,-1,-64},
3049 {0x3cd,0x3ce,1,-63},
3050 {0x3d0,0x3d0,-1,-62},
3051 {0x3d1,0x3d1,-1,-57},
3052 {0x3d5,0x3d5,-1,-47},
3053 {0x3d6,0x3d6,-1,-54},
3054 {0x3d7,0x3d7,-1,-8},
3055 {0x3d9,0x3ef,2,-1},
3056 {0x3f0,0x3f0,-1,-86},
3057 {0x3f1,0x3f1,-1,-80},
3058 {0x3f2,0x3f2,-1,7},
3059 {0x3f5,0x3f5,-1,-96},
3060 {0x3f8,0x3fb,3,-1},
3061 {0x430,0x44f,1,-32},
3062 {0x450,0x45f,1,-80},
3063 {0x461,0x481,2,-1},
3064 {0x48b,0x4bf,2,-1},
3065 {0x4c2,0x4ce,2,-1},
3066 {0x4cf,0x4cf,-1,-15},
3067 {0x4d1,0x525,2,-1},
3068 {0x561,0x586,1,-48},
3069 {0x1d79,0x1d79,-1,35332},
3070 {0x1d7d,0x1d7d,-1,3814},
3071 {0x1e01,0x1e95,2,-1},
3072 {0x1e9b,0x1e9b,-1,-59},
3073 {0x1ea1,0x1eff,2,-1},
3074 {0x1f00,0x1f07,1,8},
3075 {0x1f10,0x1f15,1,8},
3076 {0x1f20,0x1f27,1,8},
3077 {0x1f30,0x1f37,1,8},
3078 {0x1f40,0x1f45,1,8},
3079 {0x1f51,0x1f57,2,8},
3080 {0x1f60,0x1f67,1,8},
3081 {0x1f70,0x1f71,1,74},
3082 {0x1f72,0x1f75,1,86},
3083 {0x1f76,0x1f77,1,100},
3084 {0x1f78,0x1f79,1,128},
3085 {0x1f7a,0x1f7b,1,112},
3086 {0x1f7c,0x1f7d,1,126},
3087 {0x1f80,0x1f87,1,8},
3088 {0x1f90,0x1f97,1,8},
3089 {0x1fa0,0x1fa7,1,8},
3090 {0x1fb0,0x1fb1,1,8},
3091 {0x1fb3,0x1fb3,-1,9},
3092 {0x1fbe,0x1fbe,-1,-7205},
3093 {0x1fc3,0x1fc3,-1,9},
3094 {0x1fd0,0x1fd1,1,8},
3095 {0x1fe0,0x1fe1,1,8},
3096 {0x1fe5,0x1fe5,-1,7},
3097 {0x1ff3,0x1ff3,-1,9},
3098 {0x214e,0x214e,-1,-28},
3099 {0x2170,0x217f,1,-16},
3100 {0x2184,0x2184,-1,-1},
3101 {0x24d0,0x24e9,1,-26},
3102 {0x2c30,0x2c5e,1,-48},
3103 {0x2c61,0x2c61,-1,-1},
3104 {0x2c65,0x2c65,-1,-10795},
3105 {0x2c66,0x2c66,-1,-10792},
3106 {0x2c68,0x2c6c,2,-1},
3107 {0x2c73,0x2c76,3,-1},
3108 {0x2c81,0x2ce3,2,-1},
3109 {0x2cec,0x2cee,2,-1},
3110 {0x2d00,0x2d25,1,-7264},
3111 {0xa641,0xa65f,2,-1},
3112 {0xa663,0xa66d,2,-1},
3113 {0xa681,0xa697,2,-1},
3114 {0xa723,0xa72f,2,-1},
3115 {0xa733,0xa76f,2,-1},
3116 {0xa77a,0xa77c,2,-1},
3117 {0xa77f,0xa787,2,-1},
3118 {0xa78c,0xa78c,-1,-1},
3119 {0xff41,0xff5a,1,-32},
3120 {0x10428,0x1044f,1,-40}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121};
3122
3123/*
3124 * Return the upper-case equivalent of "a", which is a UCS-4 character. Use
3125 * simple case folding.
3126 */
3127 int
3128utf_toupper(a)
3129 int a;
3130{
3131 /* If 'casemap' contains "keepascii" use ASCII style toupper(). */
3132 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3133 return TOUPPER_ASC(a);
3134
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003135#if defined(HAVE_TOWUPPER) && defined(__STDC_ISO_10646__)
3136 /* If towupper() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (!(cmp_flags & CMP_INTERNAL))
3138 return towupper(a);
3139#endif
3140
3141 /* For characters below 128 use locale sensitive toupper(). */
3142 if (a < 128)
3143 return TOUPPER_LOC(a);
3144
3145 /* For any other characters use the above mapping table. */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003146 return utf_convert(a, toUpper, (int)sizeof(toUpper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147}
3148
3149 int
3150utf_islower(a)
3151 int a;
3152{
Bram Moolenaar88178de2012-06-01 17:46:59 +02003153 /* German sharp s is lower case but has no upper case equivalent. */
3154 return (utf_toupper(a) != a) || a == 0xdf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155}
3156
3157/*
3158 * Return the lower-case equivalent of "a", which is a UCS-4 character. Use
3159 * simple case folding.
3160 */
3161 int
3162utf_tolower(a)
3163 int a;
3164{
3165 /* If 'casemap' contains "keepascii" use ASCII style tolower(). */
3166 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3167 return TOLOWER_ASC(a);
3168
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003169#if defined(HAVE_TOWLOWER) && defined(__STDC_ISO_10646__)
Bram Moolenaar8fcc0f72005-04-23 20:45:11 +00003170 /* If towlower() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (!(cmp_flags & CMP_INTERNAL))
3172 return towlower(a);
3173#endif
3174
3175 /* For characters below 128 use locale sensitive tolower(). */
3176 if (a < 128)
3177 return TOLOWER_LOC(a);
3178
3179 /* For any other characters use the above mapping table. */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003180 return utf_convert(a, toLower, (int)sizeof(toLower));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181}
3182
3183 int
3184utf_isupper(a)
3185 int a;
3186{
3187 return (utf_tolower(a) != a);
3188}
3189
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003190 static int
3191utf_strnicmp(s1, s2, n1, n2)
3192 char_u *s1, *s2;
3193 size_t n1, n2;
3194{
3195 int c1, c2, cdiff;
3196 char_u buffer[6];
3197
3198 for (;;)
3199 {
3200 c1 = utf_safe_read_char_adv(&s1, &n1);
3201 c2 = utf_safe_read_char_adv(&s2, &n2);
3202
3203 if (c1 <= 0 || c2 <= 0)
3204 break;
3205
3206 if (c1 == c2)
3207 continue;
3208
3209 cdiff = utf_fold(c1) - utf_fold(c2);
3210 if (cdiff != 0)
3211 return cdiff;
3212 }
3213
3214 /* some string ended or has an incomplete/illegal character sequence */
3215
3216 if (c1 == 0 || c2 == 0)
3217 {
3218 /* some string ended. shorter string is smaller */
3219 if (c1 == 0 && c2 == 0)
3220 return 0;
3221 return c1 == 0 ? -1 : 1;
3222 }
3223
3224 /* Continue with bytewise comparison to produce some result that
3225 * would make comparison operations involving this function transitive.
3226 *
3227 * If only one string had an error, comparison should be made with
3228 * folded version of the other string. In this case it is enough
3229 * to fold just one character to determine the result of comparison. */
3230
3231 if (c1 != -1 && c2 == -1)
3232 {
3233 n1 = utf_char2bytes(utf_fold(c1), buffer);
3234 s1 = buffer;
3235 }
3236 else if (c2 != -1 && c1 == -1)
3237 {
3238 n2 = utf_char2bytes(utf_fold(c2), buffer);
3239 s2 = buffer;
3240 }
3241
3242 while (n1 > 0 && n2 > 0 && *s1 != NUL && *s2 != NUL)
3243 {
3244 cdiff = (int)(*s1) - (int)(*s2);
3245 if (cdiff != 0)
3246 return cdiff;
3247
3248 s1++;
3249 s2++;
3250 n1--;
3251 n2--;
3252 }
3253
3254 if (n1 > 0 && *s1 == NUL)
3255 n1 = 0;
3256 if (n2 > 0 && *s2 == NUL)
3257 n2 = 0;
3258
3259 if (n1 == 0 && n2 == 0)
3260 return 0;
3261 return n1 == 0 ? -1 : 1;
3262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
3265 * Version of strnicmp() that handles multi-byte characters.
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01003266 * Needed for Big5, Shift-JIS and UTF-8 encoding. Other DBCS encodings can
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 * probably use strnicmp(), because there are no ASCII characters in the
3268 * second byte.
3269 * Returns zero if s1 and s2 are equal (ignoring case), the difference between
3270 * two characters otherwise.
3271 */
3272 int
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003273mb_strnicmp(s1, s2, nn)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 char_u *s1, *s2;
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003275 size_t nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003277 int i, l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 int cdiff;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003279 int n = (int)nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003281 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003283 return utf_strnicmp(s1, s2, nn, nn);
3284 }
3285 else
3286 {
3287 for (i = 0; i < n; i += l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003289 if (s1[i] == NUL && s2[i] == NUL) /* both strings end */
3290 return 0;
3291
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003292 l = (*mb_ptr2len)(s1 + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (l <= 1)
3294 {
3295 /* Single byte: first check normally, then with ignore case. */
3296 if (s1[i] != s2[i])
3297 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003298 cdiff = MB_TOLOWER(s1[i]) - MB_TOLOWER(s2[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (cdiff != 0)
3300 return cdiff;
3301 }
3302 }
3303 else
3304 {
3305 /* For non-Unicode multi-byte don't ignore case. */
3306 if (l > n - i)
3307 l = n - i;
3308 cdiff = STRNCMP(s1 + i, s2 + i, l);
3309 if (cdiff != 0)
3310 return cdiff;
3311 }
3312 }
3313 }
3314 return 0;
3315}
3316
3317/*
3318 * "g8": show bytes of the UTF-8 char under the cursor. Doesn't matter what
3319 * 'encoding' has been set to.
3320 */
3321 void
3322show_utf8()
3323{
3324 int len;
Bram Moolenaar051b7822005-05-19 21:00:46 +00003325 int rlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 char_u *line;
3327 int clen;
3328 int i;
3329
3330 /* Get the byte length of the char under the cursor, including composing
3331 * characters. */
3332 line = ml_get_cursor();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003333 len = utfc_ptr2len(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (len == 0)
3335 {
3336 MSG("NUL");
3337 return;
3338 }
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 clen = 0;
3341 for (i = 0; i < len; ++i)
3342 {
3343 if (clen == 0)
3344 {
3345 /* start of (composing) character, get its length */
3346 if (i > 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +00003347 {
3348 STRCPY(IObuff + rlen, "+ ");
3349 rlen += 2;
3350 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003351 clen = utf_ptr2len(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
Bram Moolenaarb382ad12010-05-21 15:46:35 +02003353 sprintf((char *)IObuff + rlen, "%02x ",
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003354 (line[i] == NL) ? NUL : line[i]); /* NUL is stored as NL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 --clen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003356 rlen += (int)STRLEN(IObuff + rlen);
Bram Moolenaar051b7822005-05-19 21:00:46 +00003357 if (rlen > IOSIZE - 20)
3358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360
3361 msg(IObuff);
3362}
3363
3364/*
3365 * mb_head_off() function pointer.
3366 * Return offset from "p" to the first byte of the character it points into.
Bram Moolenaar24397332009-12-02 14:02:39 +00003367 * If "p" points to the NUL at the end of the string return 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * Returns 0 when already at the first byte of a character.
3369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 int
3371latin_head_off(base, p)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003372 char_u *base UNUSED;
3373 char_u *p UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 return 0;
3376}
3377
3378 int
3379dbcs_head_off(base, p)
3380 char_u *base;
3381 char_u *p;
3382{
3383 char_u *q;
3384
3385 /* It can't be a trailing byte when not using DBCS, at the start of the
3386 * string or the previous byte can't start a double-byte. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003387 if (p <= base || MB_BYTE2LEN(p[-1]) == 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return 0;
3389
3390 /* This is slow: need to start at the base and go forward until the
3391 * byte we are looking for. Return 1 when we went past it, 0 otherwise. */
3392 q = base;
3393 while (q < p)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003394 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return (q == p) ? 0 : 1;
3396}
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398/*
3399 * Special version of dbcs_head_off() that works for ScreenLines[], where
3400 * single-width DBCS_JPNU characters are stored separately.
3401 */
3402 int
3403dbcs_screen_head_off(base, p)
3404 char_u *base;
3405 char_u *p;
3406{
3407 char_u *q;
3408
3409 /* It can't be a trailing byte when not using DBCS, at the start of the
3410 * string or the previous byte can't start a double-byte.
3411 * For euc-jp an 0x8e byte in the previous cell always means we have a
3412 * lead byte in the current cell. */
3413 if (p <= base
3414 || (enc_dbcs == DBCS_JPNU && p[-1] == 0x8e)
Bram Moolenaar24397332009-12-02 14:02:39 +00003415 || MB_BYTE2LEN(p[-1]) == 1
3416 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return 0;
3418
3419 /* This is slow: need to start at the base and go forward until the
3420 * byte we are looking for. Return 1 when we went past it, 0 otherwise.
3421 * For DBCS_JPNU look out for 0x8e, which means the second byte is not
3422 * stored as the next byte. */
3423 q = base;
3424 while (q < p)
3425 {
3426 if (enc_dbcs == DBCS_JPNU && *q == 0x8e)
3427 ++q;
3428 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003429 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431 return (q == p) ? 0 : 1;
3432}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 int
3435utf_head_off(base, p)
3436 char_u *base;
3437 char_u *p;
3438{
3439 char_u *q;
3440 char_u *s;
3441 int c;
Bram Moolenaar24397332009-12-02 14:02:39 +00003442 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443#ifdef FEAT_ARABIC
3444 char_u *j;
3445#endif
3446
3447 if (*p < 0x80) /* be quick for ASCII */
3448 return 0;
3449
3450 /* Skip backwards over trailing bytes: 10xx.xxxx
3451 * Skip backwards again if on a composing char. */
3452 for (q = p; ; --q)
3453 {
3454 /* Move s to the last byte of this char. */
3455 for (s = q; (s[1] & 0xc0) == 0x80; ++s)
3456 ;
3457 /* Move q to the first byte of this char. */
3458 while (q > base && (*q & 0xc0) == 0x80)
3459 --q;
3460 /* Check for illegal sequence. Do allow an illegal byte after where we
3461 * started. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003462 len = utf8len_tab[*q];
3463 if (len != (int)(s - q + 1) && len != (int)(p - q + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 return 0;
3465
3466 if (q <= base)
3467 break;
3468
3469 c = utf_ptr2char(q);
3470 if (utf_iscomposing(c))
3471 continue;
3472
3473#ifdef FEAT_ARABIC
3474 if (arabic_maycombine(c))
3475 {
3476 /* Advance to get a sneak-peak at the next char */
3477 j = q;
3478 --j;
3479 /* Move j to the first byte of this char. */
3480 while (j > base && (*j & 0xc0) == 0x80)
3481 --j;
3482 if (arabic_combine(utf_ptr2char(j), c))
3483 continue;
3484 }
3485#endif
3486 break;
3487 }
3488
3489 return (int)(p - q);
3490}
3491
3492/*
Bram Moolenaard8b02732005-01-14 21:48:43 +00003493 * Copy a character from "*fp" to "*tp" and advance the pointers.
3494 */
3495 void
3496mb_copy_char(fp, tp)
3497 char_u **fp;
3498 char_u **tp;
3499{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003500 int l = (*mb_ptr2len)(*fp);
Bram Moolenaard8b02732005-01-14 21:48:43 +00003501
3502 mch_memmove(*tp, *fp, (size_t)l);
3503 *tp += l;
3504 *fp += l;
3505}
3506
3507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 * Return the offset from "p" to the first byte of a character. When "p" is
3509 * at the start of a character 0 is returned, otherwise the offset to the next
3510 * character. Can start anywhere in a stream of bytes.
3511 */
3512 int
3513mb_off_next(base, p)
3514 char_u *base;
3515 char_u *p;
3516{
3517 int i;
3518 int j;
3519
3520 if (enc_utf8)
3521 {
3522 if (*p < 0x80) /* be quick for ASCII */
3523 return 0;
3524
3525 /* Find the next character that isn't 10xx.xxxx */
3526 for (i = 0; (p[i] & 0xc0) == 0x80; ++i)
3527 ;
3528 if (i > 0)
3529 {
3530 /* Check for illegal sequence. */
3531 for (j = 0; p - j > base; ++j)
3532 if ((p[-j] & 0xc0) != 0x80)
3533 break;
3534 if (utf8len_tab[p[-j]] != i + j)
3535 return 0;
3536 }
3537 return i;
3538 }
3539
3540 /* Only need to check if we're on a trail byte, it doesn't matter if we
3541 * want the offset to the next or current character. */
3542 return (*mb_head_off)(base, p);
3543}
3544
3545/*
3546 * Return the offset from "p" to the last byte of the character it points
3547 * into. Can start anywhere in a stream of bytes.
3548 */
3549 int
3550mb_tail_off(base, p)
3551 char_u *base;
3552 char_u *p;
3553{
3554 int i;
3555 int j;
3556
3557 if (*p == NUL)
3558 return 0;
3559
3560 if (enc_utf8)
3561 {
3562 /* Find the last character that is 10xx.xxxx */
3563 for (i = 0; (p[i + 1] & 0xc0) == 0x80; ++i)
3564 ;
3565 /* Check for illegal sequence. */
3566 for (j = 0; p - j > base; ++j)
3567 if ((p[-j] & 0xc0) != 0x80)
3568 break;
3569 if (utf8len_tab[p[-j]] != i + j + 1)
3570 return 0;
3571 return i;
3572 }
3573
3574 /* It can't be the first byte if a double-byte when not using DBCS, at the
3575 * end of the string or the byte can't start a double-byte. */
3576 if (enc_dbcs == 0 || p[1] == NUL || MB_BYTE2LEN(*p) == 1)
3577 return 0;
3578
3579 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3580 return 1 - dbcs_head_off(base, p);
3581}
3582
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003583/*
3584 * Find the next illegal byte sequence.
3585 */
3586 void
3587utf_find_illegal()
3588{
3589 pos_T pos = curwin->w_cursor;
3590 char_u *p;
3591 int len;
3592 vimconv_T vimconv;
3593 char_u *tofree = NULL;
3594
3595 vimconv.vc_type = CONV_NONE;
3596 if (enc_utf8 && (enc_canon_props(curbuf->b_p_fenc) & ENC_8BIT))
3597 {
3598 /* 'encoding' is "utf-8" but we are editing a 8-bit encoded file,
3599 * possibly a utf-8 file with illegal bytes. Setup for conversion
3600 * from utf-8 to 'fileencoding'. */
3601 convert_setup(&vimconv, p_enc, curbuf->b_p_fenc);
3602 }
3603
3604#ifdef FEAT_VIRTUALEDIT
3605 curwin->w_cursor.coladd = 0;
3606#endif
3607 for (;;)
3608 {
3609 p = ml_get_cursor();
3610 if (vimconv.vc_type != CONV_NONE)
3611 {
3612 vim_free(tofree);
3613 tofree = string_convert(&vimconv, p, NULL);
3614 if (tofree == NULL)
3615 break;
3616 p = tofree;
3617 }
3618
3619 while (*p != NUL)
3620 {
3621 /* Illegal means that there are not enough trail bytes (checked by
3622 * utf_ptr2len()) or too many of them (overlong sequence). */
3623 len = utf_ptr2len(p);
3624 if (*p >= 0x80 && (len == 1
3625 || utf_char2len(utf_ptr2char(p)) != len))
3626 {
3627 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003628 curwin->w_cursor.col += (colnr_T)(p - ml_get_cursor());
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003629 else
3630 {
3631 int l;
3632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003633 len = (int)(p - tofree);
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003634 for (p = ml_get_cursor(); *p != NUL && len-- > 0; p += l)
3635 {
3636 l = utf_ptr2len(p);
3637 curwin->w_cursor.col += l;
3638 }
3639 }
3640 goto theend;
3641 }
3642 p += len;
3643 }
3644 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
3645 break;
3646 ++curwin->w_cursor.lnum;
3647 curwin->w_cursor.col = 0;
3648 }
3649
3650 /* didn't find it: don't move and beep */
3651 curwin->w_cursor = pos;
3652 beep_flush();
3653
3654theend:
3655 vim_free(tofree);
3656 convert_setup(&vimconv, NULL, NULL);
3657}
3658
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003659#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003660/*
3661 * Return TRUE if string "s" is a valid utf-8 string.
3662 * When "end" is NULL stop at the first NUL.
3663 * When "end" is positive stop there.
3664 */
3665 int
3666utf_valid_string(s, end)
3667 char_u *s;
3668 char_u *end;
3669{
3670 int l;
3671 char_u *p = s;
3672
3673 while (end == NULL ? *p != NUL : p < end)
3674 {
Bram Moolenaar24397332009-12-02 14:02:39 +00003675 l = utf8len_tab_zero[*p];
3676 if (l == 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003677 return FALSE; /* invalid lead byte */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003678 if (end != NULL && p + l > end)
3679 return FALSE; /* incomplete byte sequence */
3680 ++p;
3681 while (--l > 0)
3682 if ((*p++ & 0xc0) != 0x80)
3683 return FALSE; /* invalid trail byte */
3684 }
3685 return TRUE;
3686}
3687#endif
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689#if defined(FEAT_GUI) || defined(PROTO)
3690/*
3691 * Special version of mb_tail_off() for use in ScreenLines[].
3692 */
3693 int
3694dbcs_screen_tail_off(base, p)
3695 char_u *base;
3696 char_u *p;
3697{
3698 /* It can't be the first byte if a double-byte when not using DBCS, at the
3699 * end of the string or the byte can't start a double-byte.
3700 * For euc-jp an 0x8e byte always means we have a lead byte in the current
3701 * cell. */
3702 if (*p == NUL || p[1] == NUL
3703 || (enc_dbcs == DBCS_JPNU && *p == 0x8e)
3704 || MB_BYTE2LEN(*p) == 1)
3705 return 0;
3706
3707 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3708 return 1 - dbcs_screen_head_off(base, p);
3709}
3710#endif
3711
3712/*
3713 * If the cursor moves on an trail byte, set the cursor on the lead byte.
3714 * Thus it moves left if necessary.
3715 * Return TRUE when the cursor was adjusted.
3716 */
3717 void
3718mb_adjust_cursor()
3719{
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003720 mb_adjustpos(curbuf, &curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721}
3722
3723/*
3724 * Adjust position "*lp" to point to the first byte of a multi-byte character.
3725 * If it points to a tail byte it's moved backwards to the head byte.
3726 */
3727 void
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003728mb_adjustpos(buf, lp)
3729 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 pos_T *lp;
3731{
3732 char_u *p;
3733
3734 if (lp->col > 0
3735#ifdef FEAT_VIRTUALEDIT
3736 || lp->coladd > 1
3737#endif
3738 )
3739 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003740 p = ml_get_buf(buf, lp->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 lp->col -= (*mb_head_off)(p, p + lp->col);
3742#ifdef FEAT_VIRTUALEDIT
3743 /* Reset "coladd" when the cursor would be on the right half of a
3744 * double-wide character. */
3745 if (lp->coladd == 1
3746 && p[lp->col] != TAB
3747 && vim_isprintc((*mb_ptr2char)(p + lp->col))
3748 && ptr2cells(p + lp->col) > 1)
3749 lp->coladd = 0;
3750#endif
3751 }
3752}
3753
3754/*
3755 * Return a pointer to the character before "*p", if there is one.
3756 */
3757 char_u *
3758mb_prevptr(line, p)
3759 char_u *line; /* start of the string */
3760 char_u *p;
3761{
3762 if (p > line)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003763 mb_ptr_back(line, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return p;
3765}
3766
3767/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003768 * Return the character length of "str". Each multi-byte character (with
3769 * following composing characters) counts as one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 */
3771 int
3772mb_charlen(str)
3773 char_u *str;
3774{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003775 char_u *p = str;
3776 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003778 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 return 0;
3780
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003781 for (count = 0; *p != NUL; count++)
3782 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 return count;
3785}
3786
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00003787#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003788/*
3789 * Like mb_charlen() but for a string with specified length.
3790 */
3791 int
3792mb_charlen_len(str, len)
3793 char_u *str;
3794 int len;
3795{
3796 char_u *p = str;
3797 int count;
3798
3799 for (count = 0; *p != NUL && p < str + len; count++)
3800 p += (*mb_ptr2len)(p);
3801
3802 return count;
3803}
3804#endif
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806/*
3807 * Try to un-escape a multi-byte character.
3808 * Used for the "to" and "from" part of a mapping.
3809 * Return the un-escaped string if it is a multi-byte character, and advance
3810 * "pp" to just after the bytes that formed it.
3811 * Return NULL if no multi-byte char was found.
3812 */
3813 char_u *
3814mb_unescape(pp)
3815 char_u **pp;
3816{
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02003817 static char_u buf[6];
3818 int n;
3819 int m = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 char_u *str = *pp;
3821
3822 /* Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02003823 * KS_EXTRA KE_CSI to CSI.
3824 * Maximum length of a utf-8 character is 4 bytes. */
3825 for (n = 0; str[n] != NUL && m < 4; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
3827 if (str[n] == K_SPECIAL
3828 && str[n + 1] == KS_SPECIAL
3829 && str[n + 2] == KE_FILLER)
3830 {
3831 buf[m++] = K_SPECIAL;
3832 n += 2;
3833 }
Bram Moolenaar6203ff92008-01-06 16:18:56 +00003834 else if ((str[n] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835# ifdef FEAT_GUI
Bram Moolenaar6203ff92008-01-06 16:18:56 +00003836 || str[n] == CSI
3837# endif
3838 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 && str[n + 1] == KS_EXTRA
3840 && str[n + 2] == (int)KE_CSI)
3841 {
3842 buf[m++] = CSI;
3843 n += 2;
3844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 else if (str[n] == K_SPECIAL
3846# ifdef FEAT_GUI
3847 || str[n] == CSI
3848# endif
3849 )
3850 break; /* a special key can't be a multibyte char */
3851 else
3852 buf[m++] = str[n];
3853 buf[m] = NUL;
3854
3855 /* Return a multi-byte character if it's found. An illegal sequence
3856 * will result in a 1 here. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003857 if ((*mb_ptr2len)(buf) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859 *pp = str + n + 1;
3860 return buf;
3861 }
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02003862
3863 /* Bail out quickly for ASCII. */
3864 if (buf[0] < 128)
3865 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
3867 return NULL;
3868}
3869
3870/*
3871 * Return TRUE if the character at "row"/"col" on the screen is the left side
3872 * of a double-width character.
3873 * Caller must make sure "row" and "col" are not invalid!
3874 */
3875 int
3876mb_lefthalve(row, col)
3877 int row;
3878 int col;
3879{
3880#ifdef FEAT_HANGULIN
3881 if (composing_hangul)
3882 return TRUE;
3883#endif
Bram Moolenaar367329b2007-08-30 11:53:22 +00003884 return (*mb_off2cells)(LineOffset[row] + col,
3885 LineOffset[row] + screen_Columns) > 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886}
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003889 * Correct a position on the screen, if it's the right half of a double-wide
3890 * char move it to the left half. Returns the corrected column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 */
3892 int
3893mb_fix_col(col, row)
3894 int col;
3895 int row;
3896{
3897 col = check_col(col);
3898 row = check_row(row);
3899 if (has_mbyte && ScreenLines != NULL && col > 0
3900 && ((enc_dbcs
3901 && ScreenLines[LineOffset[row] + col] != NUL
3902 && dbcs_screen_head_off(ScreenLines + LineOffset[row],
3903 ScreenLines + LineOffset[row] + col))
3904 || (enc_utf8 && ScreenLines[LineOffset[row] + col] == 0)))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003905 return col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return col;
3907}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908#endif
3909
3910#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
3911static int enc_alias_search __ARGS((char_u *name));
3912
3913/*
3914 * Skip the Vim specific head of a 'encoding' name.
3915 */
3916 char_u *
3917enc_skip(p)
3918 char_u *p;
3919{
3920 if (STRNCMP(p, "2byte-", 6) == 0)
3921 return p + 6;
3922 if (STRNCMP(p, "8bit-", 5) == 0)
3923 return p + 5;
3924 return p;
3925}
3926
3927/*
3928 * Find the canonical name for encoding "enc".
3929 * When the name isn't recognized, returns "enc" itself, but with all lower
3930 * case characters and '_' replaced with '-'.
3931 * Returns an allocated string. NULL for out-of-memory.
3932 */
3933 char_u *
3934enc_canonize(enc)
3935 char_u *enc;
3936{
3937 char_u *r;
3938 char_u *p, *s;
3939 int i;
3940
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003941# ifdef FEAT_MBYTE
3942 if (STRCMP(enc, "default") == 0)
3943 {
3944 /* Use the default encoding as it's found by set_init_1(). */
3945 r = get_encoding_default();
3946 if (r == NULL)
3947 r = (char_u *)"latin1";
3948 return vim_strsave(r);
3949 }
3950# endif
3951
Bram Moolenaar29466f22007-05-10 17:45:37 +00003952 /* copy "enc" to allocated memory, with room for two '-' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 r = alloc((unsigned)(STRLEN(enc) + 3));
3954 if (r != NULL)
3955 {
3956 /* Make it all lower case and replace '_' with '-'. */
3957 p = r;
3958 for (s = enc; *s != NUL; ++s)
3959 {
3960 if (*s == '_')
3961 *p++ = '-';
3962 else
3963 *p++ = TOLOWER_ASC(*s);
3964 }
3965 *p = NUL;
3966
3967 /* Skip "2byte-" and "8bit-". */
3968 p = enc_skip(r);
3969
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003970 /* Change "microsoft-cp" to "cp". Used in some spell files. */
3971 if (STRNCMP(p, "microsoft-cp", 12) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003972 STRMOVE(p, p + 10);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 /* "iso8859" -> "iso-8859" */
3975 if (STRNCMP(p, "iso8859", 7) == 0)
3976 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003977 STRMOVE(p + 4, p + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 p[3] = '-';
3979 }
3980
3981 /* "iso-8859n" -> "iso-8859-n" */
3982 if (STRNCMP(p, "iso-8859", 8) == 0 && p[8] != '-')
3983 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003984 STRMOVE(p + 9, p + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 p[8] = '-';
3986 }
3987
3988 /* "latin-N" -> "latinN" */
3989 if (STRNCMP(p, "latin-", 6) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003990 STRMOVE(p + 5, p + 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
3992 if (enc_canon_search(p) >= 0)
3993 {
3994 /* canonical name can be used unmodified */
3995 if (p != r)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003996 STRMOVE(r, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998 else if ((i = enc_alias_search(p)) >= 0)
3999 {
4000 /* alias recognized, get canonical name */
4001 vim_free(r);
4002 r = vim_strsave((char_u *)enc_canon_table[i].name);
4003 }
4004 }
4005 return r;
4006}
4007
4008/*
4009 * Search for an encoding alias of "name".
4010 * Returns -1 when not found.
4011 */
4012 static int
4013enc_alias_search(name)
4014 char_u *name;
4015{
4016 int i;
4017
4018 for (i = 0; enc_alias_table[i].name != NULL; ++i)
4019 if (STRCMP(name, enc_alias_table[i].name) == 0)
4020 return enc_alias_table[i].canon;
4021 return -1;
4022}
4023#endif
4024
4025#if defined(FEAT_MBYTE) || defined(PROTO)
4026
4027#ifdef HAVE_LANGINFO_H
4028# include <langinfo.h>
4029#endif
4030
4031/*
4032 * Get the canonicalized encoding of the current locale.
4033 * Returns an allocated string when successful, NULL when not.
4034 */
4035 char_u *
4036enc_locale()
4037{
4038#ifndef WIN3264
4039 char *s;
4040 char *p;
4041 int i;
4042#endif
4043 char buf[50];
4044#ifdef WIN3264
4045 long acp = GetACP();
4046
4047 if (acp == 1200)
4048 STRCPY(buf, "ucs-2le");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004049 else if (acp == 1252) /* cp1252 is used as latin1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 STRCPY(buf, "latin1");
4051 else
4052 sprintf(buf, "cp%ld", acp);
4053#else
4054# ifdef HAVE_NL_LANGINFO_CODESET
4055 if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
4056# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004057# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if ((s = getenv("LC_ALL")) == NULL || *s == NUL)
4061 if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL)
4062 s = getenv("LANG");
4063
4064 if (s == NULL || *s == NUL)
4065 return FAIL;
4066
4067 /* The most generic locale format is:
4068 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
4069 * If there is a '.' remove the part before it.
4070 * if there is something after the codeset, remove it.
4071 * Make the name lowercase and replace '_' with '-'.
4072 * Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
4073 * "ko_KR.EUC" == "euc-kr"
4074 */
4075 if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL)
4076 {
4077 if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0
4078 && !isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_')
4079 {
4080 /* copy "XY.EUC" to "euc-XY" to buf[10] */
4081 STRCPY(buf + 10, "euc-");
4082 buf[14] = p[-2];
4083 buf[15] = p[-1];
4084 buf[16] = 0;
4085 s = buf + 10;
4086 }
4087 else
4088 s = p + 1;
4089 }
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004090 for (i = 0; s[i] != NUL && i < (int)sizeof(buf) - 1; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
4092 if (s[i] == '_' || s[i] == '-')
4093 buf[i] = '-';
4094 else if (isalnum((int)s[i]))
4095 buf[i] = TOLOWER_ASC(s[i]);
4096 else
4097 break;
4098 }
4099 buf[i] = NUL;
4100#endif
4101
4102 return enc_canonize((char_u *)buf);
4103}
4104
Bram Moolenaar693e40c2013-02-26 14:56:42 +01004105#if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106/*
4107 * Convert an encoding name to an MS-Windows codepage.
4108 * Returns zero if no codepage can be figured out.
4109 */
4110 int
4111encname2codepage(name)
4112 char_u *name;
4113{
4114 int cp;
4115 char_u *p = name;
4116 int idx;
4117
4118 if (STRNCMP(p, "8bit-", 5) == 0)
4119 p += 5;
4120 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
4121 p += 6;
4122
4123 if (p[0] == 'c' && p[1] == 'p')
Bram Moolenaar2c6f3dc2013-07-05 20:09:16 +02004124 cp = atoi((char *)p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else if ((idx = enc_canon_search(p)) >= 0)
4126 cp = enc_canon_table[idx].codepage;
4127 else
4128 return 0;
4129 if (IsValidCodePage(cp))
4130 return cp;
4131 return 0;
4132}
4133#endif
4134
4135# if defined(USE_ICONV) || defined(PROTO)
4136
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004137static char_u *iconv_string __ARGS((vimconv_T *vcp, char_u *str, int slen, int *unconvlenp, int *resultlenp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139/*
4140 * Call iconv_open() with a check if iconv() works properly (there are broken
4141 * versions).
4142 * Returns (void *)-1 if failed.
4143 * (should return iconv_t, but that causes problems with prototypes).
4144 */
4145 void *
4146my_iconv_open(to, from)
4147 char_u *to;
4148 char_u *from;
4149{
4150 iconv_t fd;
4151#define ICONV_TESTLEN 400
4152 char_u tobuf[ICONV_TESTLEN];
4153 char *p;
4154 size_t tolen;
4155 static int iconv_ok = -1;
4156
4157 if (iconv_ok == FALSE)
4158 return (void *)-1; /* detected a broken iconv() previously */
4159
4160#ifdef DYNAMIC_ICONV
4161 /* Check if the iconv.dll can be found. */
4162 if (!iconv_enabled(TRUE))
4163 return (void *)-1;
4164#endif
4165
4166 fd = iconv_open((char *)enc_skip(to), (char *)enc_skip(from));
4167
4168 if (fd != (iconv_t)-1 && iconv_ok == -1)
4169 {
4170 /*
4171 * Do a dummy iconv() call to check if it actually works. There is a
4172 * version of iconv() on Linux that is broken. We can't ignore it,
4173 * because it's wide-spread. The symptoms are that after outputting
4174 * the initial shift state the "to" pointer is NULL and conversion
4175 * stops for no apparent reason after about 8160 characters.
4176 */
4177 p = (char *)tobuf;
4178 tolen = ICONV_TESTLEN;
4179 (void)iconv(fd, NULL, NULL, &p, &tolen);
4180 if (p == NULL)
4181 {
4182 iconv_ok = FALSE;
4183 iconv_close(fd);
4184 fd = (iconv_t)-1;
4185 }
4186 else
4187 iconv_ok = TRUE;
4188 }
4189
4190 return (void *)fd;
4191}
4192
4193/*
4194 * Convert the string "str[slen]" with iconv().
4195 * If "unconvlenp" is not NULL handle the string ending in an incomplete
4196 * sequence and set "*unconvlenp" to the length of it.
4197 * Returns the converted string in allocated memory. NULL for an error.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004198 * If resultlenp is not NULL, sets it to the result length in bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 */
4200 static char_u *
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004201iconv_string(vcp, str, slen, unconvlenp, resultlenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 vimconv_T *vcp;
4203 char_u *str;
4204 int slen;
4205 int *unconvlenp;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004206 int *resultlenp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 const char *from;
4209 size_t fromlen;
4210 char *to;
4211 size_t tolen;
4212 size_t len = 0;
4213 size_t done = 0;
4214 char_u *result = NULL;
4215 char_u *p;
4216 int l;
4217
4218 from = (char *)str;
4219 fromlen = slen;
4220 for (;;)
4221 {
4222 if (len == 0 || ICONV_ERRNO == ICONV_E2BIG)
4223 {
4224 /* Allocate enough room for most conversions. When re-allocating
4225 * increase the buffer size. */
4226 len = len + fromlen * 2 + 40;
4227 p = alloc((unsigned)len);
4228 if (p != NULL && done > 0)
4229 mch_memmove(p, result, done);
4230 vim_free(result);
4231 result = p;
4232 if (result == NULL) /* out of memory */
4233 break;
4234 }
4235
4236 to = (char *)result + done;
4237 tolen = len - done - 2;
4238 /* Avoid a warning for systems with a wrong iconv() prototype by
4239 * casting the second argument to void *. */
4240 if (iconv(vcp->vc_fd, (void *)&from, &fromlen, &to, &tolen)
4241 != (size_t)-1)
4242 {
4243 /* Finished, append a NUL. */
4244 *to = NUL;
4245 break;
4246 }
4247
4248 /* Check both ICONV_EINVAL and EINVAL, because the dynamically loaded
4249 * iconv library may use one of them. */
4250 if (!vcp->vc_fail && unconvlenp != NULL
4251 && (ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4252 {
4253 /* Handle an incomplete sequence at the end. */
4254 *to = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004255 *unconvlenp = (int)fromlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 break;
4257 }
4258
4259 /* Check both ICONV_EILSEQ and EILSEQ, because the dynamically loaded
4260 * iconv library may use one of them. */
4261 else if (!vcp->vc_fail
4262 && (ICONV_ERRNO == ICONV_EILSEQ || ICONV_ERRNO == EILSEQ
4263 || ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4264 {
4265 /* Can't convert: insert a '?' and skip a character. This assumes
4266 * conversion from 'encoding' to something else. In other
4267 * situations we don't know what to skip anyway. */
4268 *to++ = '?';
4269 if ((*mb_ptr2cells)((char_u *)from) > 1)
4270 *to++ = '?';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004271 if (enc_utf8)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004272 l = utfc_ptr2len_len((char_u *)from, (int)fromlen);
Bram Moolenaar217ad922005-03-20 22:37:15 +00004273 else
4274 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004275 l = (*mb_ptr2len)((char_u *)from);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004276 if (l > (int)fromlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004277 l = (int)fromlen;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 from += l;
4280 fromlen -= l;
4281 }
4282 else if (ICONV_ERRNO != ICONV_E2BIG)
4283 {
4284 /* conversion failed */
4285 vim_free(result);
4286 result = NULL;
4287 break;
4288 }
4289 /* Not enough room or skipping illegal sequence. */
4290 done = to - (char *)result;
4291 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004292
Bram Moolenaar0d35e912011-04-11 14:29:17 +02004293 if (resultlenp != NULL && result != NULL)
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004294 *resultlenp = (int)(to - (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 return result;
4296}
4297
4298# if defined(DYNAMIC_ICONV) || defined(PROTO)
4299/*
4300 * Dynamically load the "iconv.dll" on Win32.
4301 */
4302
4303#ifndef DYNAMIC_ICONV /* just generating prototypes */
4304# define HINSTANCE int
4305#endif
Bram Moolenaard6f676d2005-06-01 21:51:55 +00004306static HINSTANCE hIconvDLL = 0;
4307static HINSTANCE hMsvcrtDLL = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309# ifndef DYNAMIC_ICONV_DLL
4310# define DYNAMIC_ICONV_DLL "iconv.dll"
4311# define DYNAMIC_ICONV_DLL_ALT "libiconv.dll"
4312# endif
4313# ifndef DYNAMIC_MSVCRT_DLL
4314# define DYNAMIC_MSVCRT_DLL "msvcrt.dll"
4315# endif
4316
4317/*
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004318 * Get the address of 'funcname' which is imported by 'hInst' DLL.
4319 */
4320 static void *
4321get_iconv_import_func(HINSTANCE hInst, const char *funcname)
4322{
4323 PBYTE pImage = (PBYTE)hInst;
4324 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
4325 PIMAGE_NT_HEADERS pPE;
4326 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
4327 PIMAGE_THUNK_DATA pIAT; /* Import Address Table */
4328 PIMAGE_THUNK_DATA pINT; /* Import Name Table */
4329 PIMAGE_IMPORT_BY_NAME pImpName;
4330
4331 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
4332 return NULL;
4333 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
4334 if (pPE->Signature != IMAGE_NT_SIGNATURE)
4335 return NULL;
4336 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
4337 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
4338 .VirtualAddress);
4339 for (; pImpDesc->FirstThunk; ++pImpDesc)
4340 {
Bram Moolenaarb5f7bf62013-01-19 14:02:02 +01004341 if (!pImpDesc->OriginalFirstThunk)
4342 continue;
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004343 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
4344 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
4345 for (; pIAT->u1.Function; ++pIAT, ++pINT)
4346 {
4347 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
4348 continue;
Bram Moolenaar94a8adf2013-01-23 16:19:23 +01004349 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
4350 + (UINT_PTR)(pINT->u1.AddressOfData));
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004351 if (strcmp(pImpName->Name, funcname) == 0)
4352 return (void *)pIAT->u1.Function;
4353 }
4354 }
4355 return NULL;
4356}
4357
4358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 * Try opening the iconv.dll and return TRUE if iconv() can be used.
4360 */
4361 int
4362iconv_enabled(verbose)
4363 int verbose;
4364{
4365 if (hIconvDLL != 0 && hMsvcrtDLL != 0)
4366 return TRUE;
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004367 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (hIconvDLL == 0) /* sometimes it's called libiconv.dll */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004369 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL_ALT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (hIconvDLL != 0)
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004371 hMsvcrtDLL = vimLoadLib(DYNAMIC_MSVCRT_DLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (hIconvDLL == 0 || hMsvcrtDLL == 0)
4373 {
4374 /* Only give the message when 'verbose' is set, otherwise it might be
4375 * done whenever a conversion is attempted. */
4376 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004377 {
4378 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 EMSG2(_(e_loadlib),
4380 hIconvDLL == 0 ? DYNAMIC_ICONV_DLL : DYNAMIC_MSVCRT_DLL);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004381 verbose_leave();
4382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 iconv_end();
4384 return FALSE;
4385 }
4386
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004387 iconv = (void *)GetProcAddress(hIconvDLL, "libiconv");
4388 iconv_open = (void *)GetProcAddress(hIconvDLL, "libiconv_open");
4389 iconv_close = (void *)GetProcAddress(hIconvDLL, "libiconv_close");
4390 iconvctl = (void *)GetProcAddress(hIconvDLL, "libiconvctl");
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004391 iconv_errno = get_iconv_import_func(hIconvDLL, "_errno");
4392 if (iconv_errno == NULL)
4393 iconv_errno = (void *)GetProcAddress(hMsvcrtDLL, "_errno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (iconv == NULL || iconv_open == NULL || iconv_close == NULL
4395 || iconvctl == NULL || iconv_errno == NULL)
4396 {
4397 iconv_end();
4398 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004399 {
4400 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 EMSG2(_(e_loadfunc), "for libiconv");
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004402 verbose_leave();
4403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return FALSE;
4405 }
4406 return TRUE;
4407}
4408
4409 void
4410iconv_end()
4411{
4412 /* Don't use iconv() when inputting or outputting characters. */
4413 if (input_conv.vc_type == CONV_ICONV)
4414 convert_setup(&input_conv, NULL, NULL);
4415 if (output_conv.vc_type == CONV_ICONV)
4416 convert_setup(&output_conv, NULL, NULL);
4417
4418 if (hIconvDLL != 0)
4419 FreeLibrary(hIconvDLL);
4420 if (hMsvcrtDLL != 0)
4421 FreeLibrary(hMsvcrtDLL);
4422 hIconvDLL = 0;
4423 hMsvcrtDLL = 0;
4424}
4425# endif /* DYNAMIC_ICONV */
4426# endif /* USE_ICONV */
4427
4428#endif /* FEAT_MBYTE */
4429
4430#if defined(FEAT_XIM) || defined(PROTO)
4431
Bram Moolenaar64404472010-06-26 06:24:45 +02004432# if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433static int xim_has_preediting INIT(= FALSE); /* IM current status */
4434
4435/*
4436 * Set preedit_start_col to the current cursor position.
4437 */
4438 static void
4439init_preedit_start_col(void)
4440{
4441 if (State & CMDLINE)
4442 preedit_start_col = cmdline_getvcol_cursor();
4443 else if (curwin != NULL)
4444 getvcol(curwin, &curwin->w_cursor, &preedit_start_col, NULL, NULL);
4445 /* Prevent that preediting marks the buffer as changed. */
4446 xim_changed_while_preediting = curbuf->b_changed;
4447}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449static int im_is_active = FALSE; /* IM is enabled for current mode */
Bram Moolenaarc236c162008-07-13 17:41:49 +00004450static int preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451static int im_preedit_cursor = 0; /* cursor offset in characters */
4452static int im_preedit_trailing = 0; /* number of characters after cursor */
4453
4454static unsigned long im_commit_handler_id = 0;
4455static unsigned int im_activatekey_keyval = GDK_VoidSymbol;
4456static unsigned int im_activatekey_state = 0;
4457
4458 void
4459im_set_active(int active)
4460{
4461 int was_active;
4462
Bram Moolenaarabab85a2013-06-26 19:18:05 +02004463 was_active = !!im_get_status();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 im_is_active = (active && !p_imdisable);
4465
4466 if (im_is_active != was_active)
4467 xim_reset();
4468}
4469
4470 void
4471xim_set_focus(int focus)
4472{
4473 if (xic != NULL)
4474 {
4475 if (focus)
4476 gtk_im_context_focus_in(xic);
4477 else
4478 gtk_im_context_focus_out(xic);
4479 }
4480}
4481
4482 void
4483im_set_position(int row, int col)
4484{
4485 if (xic != NULL)
4486 {
4487 GdkRectangle area;
4488
4489 area.x = FILL_X(col);
4490 area.y = FILL_Y(row);
4491 area.width = gui.char_width * (mb_lefthalve(row, col) ? 2 : 1);
4492 area.height = gui.char_height;
4493
4494 gtk_im_context_set_cursor_location(xic, &area);
4495 }
4496}
4497
4498# if 0 || defined(PROTO) /* apparently only used in gui_x11.c */
4499 void
4500xim_set_preedit(void)
4501{
4502 im_set_position(gui.row, gui.col);
4503}
4504# endif
4505
4506 static void
4507im_add_to_input(char_u *str, int len)
4508{
4509 /* Convert from 'termencoding' (always "utf-8") to 'encoding' */
4510 if (input_conv.vc_type != CONV_NONE)
4511 {
4512 str = string_convert(&input_conv, str, &len);
4513 g_return_if_fail(str != NULL);
4514 }
4515
4516 add_to_input_buf_csi(str, len);
4517
4518 if (input_conv.vc_type != CONV_NONE)
4519 vim_free(str);
4520
4521 if (p_mh) /* blank out the pointer if necessary */
4522 gui_mch_mousehide(TRUE);
4523}
4524
4525 static void
4526im_delete_preedit(void)
4527{
4528 char_u bskey[] = {CSI, 'k', 'b'};
4529 char_u delkey[] = {CSI, 'k', 'D'};
4530
4531 if (State & NORMAL)
4532 {
4533 im_preedit_cursor = 0;
4534 return;
4535 }
4536 for (; im_preedit_cursor > 0; --im_preedit_cursor)
4537 add_to_input_buf(bskey, (int)sizeof(bskey));
4538
4539 for (; im_preedit_trailing > 0; --im_preedit_trailing)
4540 add_to_input_buf(delkey, (int)sizeof(delkey));
4541}
4542
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004543/*
4544 * Move the cursor left by "num_move_back" characters.
4545 * Note that ins_left() checks im_is_preediting() to avoid breaking undo for
4546 * these K_LEFT keys.
4547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 static void
4549im_correct_cursor(int num_move_back)
4550{
4551 char_u backkey[] = {CSI, 'k', 'l'};
4552
4553 if (State & NORMAL)
4554 return;
4555# ifdef FEAT_RIGHTLEFT
4556 if ((State & CMDLINE) == 0 && curwin != NULL && curwin->w_p_rl)
4557 backkey[2] = 'r';
4558# endif
4559 for (; num_move_back > 0; --num_move_back)
4560 add_to_input_buf(backkey, (int)sizeof(backkey));
4561}
4562
4563static int xim_expected_char = NUL;
4564static int xim_ignored_char = FALSE;
4565
4566/*
4567 * Update the mode and cursor while in an IM callback.
4568 */
4569 static void
4570im_show_info(void)
4571{
4572 int old_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 old_vgetc_busy = vgetc_busy;
4575 vgetc_busy = TRUE;
4576 showmode();
4577 vgetc_busy = old_vgetc_busy;
Bram Moolenaar917ba892012-03-07 19:38:55 +01004578 if ((State & NORMAL) || (State & INSERT))
4579 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 out_flush();
4581}
4582
4583/*
4584 * Callback invoked when the user finished preediting.
4585 * Put the final string into the input buffer.
4586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004588im_commit_cb(GtkIMContext *context UNUSED,
4589 const gchar *str,
4590 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591{
Bram Moolenaar72597a52010-07-18 15:31:08 +02004592 int slen = (int)STRLEN(str);
4593 int add_to_input = TRUE;
4594 int clen;
4595 int len = slen;
4596 int commit_with_preedit = TRUE;
4597 char_u *im_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599#ifdef XIM_DEBUG
4600 xim_log("im_commit_cb(): %s\n", str);
4601#endif
4602
4603 /* The imhangul module doesn't reset the preedit string before
4604 * committing. Call im_delete_preedit() to work around that. */
4605 im_delete_preedit();
4606
4607 /* Indicate that preediting has finished. */
4608 if (preedit_start_col == MAXCOL)
4609 {
4610 init_preedit_start_col();
4611 commit_with_preedit = FALSE;
4612 }
4613
4614 /* The thing which setting "preedit_start_col" to MAXCOL means that
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004615 * "preedit_start_col" will be set forcedly when calling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 * preedit_changed_cb() next time.
4617 * "preedit_start_col" should not reset with MAXCOL on this part. Vim
4618 * is simulating the preediting by using add_to_input_str(). when
4619 * preedit begin immediately before committed, the typebuf is not
4620 * flushed to screen, then it can't get correct "preedit_start_col".
4621 * Thus, it should calculate the cells by adding cells of the committed
4622 * string. */
4623 if (input_conv.vc_type != CONV_NONE)
4624 {
4625 im_str = string_convert(&input_conv, (char_u *)str, &len);
4626 g_return_if_fail(im_str != NULL);
4627 }
4628 else
4629 im_str = (char_u *)str;
Bram Moolenaar72597a52010-07-18 15:31:08 +02004630
4631 clen = mb_string2cells(im_str, len);
4632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (input_conv.vc_type != CONV_NONE)
4634 vim_free(im_str);
4635 preedit_start_col += clen;
4636
4637 /* Is this a single character that matches a keypad key that's just
4638 * been pressed? If so, we don't want it to be entered as such - let
4639 * us carry on processing the raw keycode so that it may be used in
4640 * mappings as <kSomething>. */
4641 if (xim_expected_char != NUL)
4642 {
4643 /* We're currently processing a keypad or other special key */
4644 if (slen == 1 && str[0] == xim_expected_char)
4645 {
4646 /* It's a match - don't do it here */
4647 xim_ignored_char = TRUE;
4648 add_to_input = FALSE;
4649 }
4650 else
4651 {
4652 /* Not a match */
4653 xim_ignored_char = FALSE;
4654 }
4655 }
4656
4657 if (add_to_input)
4658 im_add_to_input((char_u *)str, slen);
4659
4660 /* Inserting chars while "im_is_active" is set does not cause a change of
4661 * buffer. When the chars are committed the buffer must be marked as
4662 * changed. */
4663 if (!commit_with_preedit)
4664 preedit_start_col = MAXCOL;
4665
4666 /* This flag is used in changed() at next call. */
4667 xim_changed_while_preediting = TRUE;
4668
4669 if (gtk_main_level() > 0)
4670 gtk_main_quit();
4671}
4672
4673/*
4674 * Callback invoked after start to the preedit.
4675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004677im_preedit_start_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679#ifdef XIM_DEBUG
4680 xim_log("im_preedit_start_cb()\n");
4681#endif
4682
4683 im_is_active = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00004684 preedit_is_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarc236c162008-07-13 17:41:49 +00004686 im_show_info();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687}
4688
4689/*
4690 * Callback invoked after end to the preedit.
4691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004693im_preedit_end_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695#ifdef XIM_DEBUG
4696 xim_log("im_preedit_end_cb()\n");
4697#endif
4698 im_delete_preedit();
4699
4700 /* Indicate that preediting has finished */
4701 preedit_start_col = MAXCOL;
4702 xim_has_preediting = FALSE;
4703
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004704#if 0
4705 /* Removal of this line suggested by Takuhiro Nishioka. Fixes that IM was
Bram Moolenaarc236c162008-07-13 17:41:49 +00004706 * switched off unintentionally. We now use preedit_is_active (added by
4707 * SungHyun Nam). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 im_is_active = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004709#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00004710 preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 gui_update_cursor(TRUE, FALSE);
4712 im_show_info();
4713}
4714
4715/*
4716 * Callback invoked after changes to the preedit string. If the preedit
4717 * string was empty before, remember the preedit start column so we know
4718 * where to apply feedback attributes. Delete the previous preedit string
4719 * if there was one, save the new preedit cursor offset, and put the new
4720 * string into the input buffer.
4721 *
4722 * TODO: The pragmatic "put into input buffer" approach used here has
4723 * several fundamental problems:
4724 *
4725 * - The characters in the preedit string are subject to remapping.
4726 * That's broken, only the finally committed string should be remapped.
4727 *
4728 * - There is a race condition involved: The retrieved value for the
4729 * current cursor position will be wrong if any unprocessed characters
4730 * are still queued in the input buffer.
4731 *
4732 * - Due to the lack of synchronization between the file buffer in memory
4733 * and any typed characters, it's practically impossible to implement the
4734 * "retrieve_surrounding" and "delete_surrounding" signals reliably. IM
4735 * modules for languages such as Thai are likely to rely on this feature
4736 * for proper operation.
4737 *
4738 * Conclusions: I think support for preediting needs to be moved to the
4739 * core parts of Vim. Ideally, until it has been committed, the preediting
4740 * string should only be displayed and not affect the buffer content at all.
4741 * The question how to deal with the synchronization issue still remains.
4742 * Circumventing the input buffer is probably not desirable. Anyway, I think
4743 * implementing "retrieve_surrounding" is the only hard problem.
4744 *
4745 * One way to solve all of this in a clean manner would be to queue all key
4746 * press/release events "as is" in the input buffer, and apply the IM filtering
4747 * at the receiving end of the queue. This, however, would have a rather large
4748 * impact on the code base. If there is an easy way to force processing of all
4749 * remaining input from within the "retrieve_surrounding" signal handler, this
4750 * might not be necessary. Gotta ask on vim-dev for opinions.
4751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004753im_preedit_changed_cb(GtkIMContext *context, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 char *preedit_string = NULL;
4756 int cursor_index = 0;
4757 int num_move_back = 0;
4758 char_u *str;
4759 char_u *p;
4760 int i;
4761
4762 gtk_im_context_get_preedit_string(context,
4763 &preedit_string, NULL,
4764 &cursor_index);
4765
4766#ifdef XIM_DEBUG
4767 xim_log("im_preedit_changed_cb(): %s\n", preedit_string);
4768#endif
4769
4770 g_return_if_fail(preedit_string != NULL); /* just in case */
4771
4772 /* If preedit_start_col is MAXCOL set it to the current cursor position. */
4773 if (preedit_start_col == MAXCOL && preedit_string[0] != '\0')
4774 {
4775 xim_has_preediting = TRUE;
4776
4777 /* Urgh, this breaks if the input buffer isn't empty now */
4778 init_preedit_start_col();
4779 }
4780 else if (cursor_index == 0 && preedit_string[0] == '\0')
4781 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004782 xim_has_preediting = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 /* If at the start position (after typing backspace)
4785 * preedit_start_col must be reset. */
4786 preedit_start_col = MAXCOL;
4787 }
4788
4789 im_delete_preedit();
4790
4791 /*
4792 * Compute the end of the preediting area: "preedit_end_col".
4793 * According to the documentation of gtk_im_context_get_preedit_string(),
4794 * the cursor_pos output argument returns the offset in bytes. This is
4795 * unfortunately not true -- real life shows the offset is in characters,
4796 * and the GTK+ source code agrees with me. Will file a bug later.
4797 */
4798 if (preedit_start_col != MAXCOL)
4799 preedit_end_col = preedit_start_col;
4800 str = (char_u *)preedit_string;
4801 for (p = str, i = 0; *p != NUL; p += utf_byte2len(*p), ++i)
4802 {
4803 int is_composing;
4804
4805 is_composing = ((*p & 0x80) != 0 && utf_iscomposing(utf_ptr2char(p)));
4806 /*
4807 * These offsets are used as counters when generating <BS> and <Del>
4808 * to delete the preedit string. So don't count composing characters
4809 * unless 'delcombine' is enabled.
4810 */
4811 if (!is_composing || p_deco)
4812 {
4813 if (i < cursor_index)
4814 ++im_preedit_cursor;
4815 else
4816 ++im_preedit_trailing;
4817 }
4818 if (!is_composing && i >= cursor_index)
4819 {
4820 /* This is essentially the same as im_preedit_trailing, except
4821 * composing characters are not counted even if p_deco is set. */
4822 ++num_move_back;
4823 }
4824 if (preedit_start_col != MAXCOL)
4825 preedit_end_col += utf_ptr2cells(p);
4826 }
4827
4828 if (p > str)
4829 {
4830 im_add_to_input(str, (int)(p - str));
4831 im_correct_cursor(num_move_back);
4832 }
4833
4834 g_free(preedit_string);
4835
4836 if (gtk_main_level() > 0)
4837 gtk_main_quit();
4838}
4839
4840/*
4841 * Translate the Pango attributes at iter to Vim highlighting attributes.
4842 * Ignore attributes not supported by Vim highlighting. This shouldn't have
4843 * too much impact -- right now we handle even more attributes than necessary
4844 * for the IM modules I tested with.
4845 */
4846 static int
4847translate_pango_attributes(PangoAttrIterator *iter)
4848{
4849 PangoAttribute *attr;
4850 int char_attr = HL_NORMAL;
4851
4852 attr = pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE);
4853 if (attr != NULL && ((PangoAttrInt *)attr)->value
4854 != (int)PANGO_UNDERLINE_NONE)
4855 char_attr |= HL_UNDERLINE;
4856
4857 attr = pango_attr_iterator_get(iter, PANGO_ATTR_WEIGHT);
4858 if (attr != NULL && ((PangoAttrInt *)attr)->value >= (int)PANGO_WEIGHT_BOLD)
4859 char_attr |= HL_BOLD;
4860
4861 attr = pango_attr_iterator_get(iter, PANGO_ATTR_STYLE);
4862 if (attr != NULL && ((PangoAttrInt *)attr)->value
4863 != (int)PANGO_STYLE_NORMAL)
4864 char_attr |= HL_ITALIC;
4865
4866 attr = pango_attr_iterator_get(iter, PANGO_ATTR_BACKGROUND);
4867 if (attr != NULL)
4868 {
4869 const PangoColor *color = &((PangoAttrColor *)attr)->color;
4870
4871 /* Assume inverse if black background is requested */
4872 if ((color->red | color->green | color->blue) == 0)
4873 char_attr |= HL_INVERSE;
4874 }
4875
4876 return char_attr;
4877}
4878
4879/*
4880 * Retrieve the highlighting attributes at column col in the preedit string.
4881 * Return -1 if not in preediting mode or if col is out of range.
4882 */
4883 int
4884im_get_feedback_attr(int col)
4885{
4886 char *preedit_string = NULL;
4887 PangoAttrList *attr_list = NULL;
4888 int char_attr = -1;
4889
4890 if (xic == NULL)
4891 return char_attr;
4892
4893 gtk_im_context_get_preedit_string(xic, &preedit_string, &attr_list, NULL);
4894
4895 if (preedit_string != NULL && attr_list != NULL)
4896 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004897 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
4899 /* Get the byte index as used by PangoAttrIterator */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004900 for (idx = 0; col > 0 && preedit_string[idx] != '\0'; --col)
4901 idx += utfc_ptr2len((char_u *)preedit_string + idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
Bram Moolenaar89d40322006-08-29 15:30:07 +00004903 if (preedit_string[idx] != '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 PangoAttrIterator *iter;
4906 int start, end;
4907
4908 char_attr = HL_NORMAL;
4909 iter = pango_attr_list_get_iterator(attr_list);
4910
4911 /* Extract all relevant attributes from the list. */
4912 do
4913 {
4914 pango_attr_iterator_range(iter, &start, &end);
4915
Bram Moolenaar89d40322006-08-29 15:30:07 +00004916 if (idx >= start && idx < end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 char_attr |= translate_pango_attributes(iter);
4918 }
4919 while (pango_attr_iterator_next(iter));
4920
4921 pango_attr_iterator_destroy(iter);
4922 }
4923 }
4924
4925 if (attr_list != NULL)
4926 pango_attr_list_unref(attr_list);
4927 g_free(preedit_string);
4928
4929 return char_attr;
4930}
4931
4932 void
4933xim_init(void)
4934{
4935#ifdef XIM_DEBUG
4936 xim_log("xim_init()\n");
4937#endif
4938
4939 g_return_if_fail(gui.drawarea != NULL);
4940 g_return_if_fail(gui.drawarea->window != NULL);
4941
4942 xic = gtk_im_multicontext_new();
4943 g_object_ref(xic);
4944
4945 im_commit_handler_id = g_signal_connect(G_OBJECT(xic), "commit",
4946 G_CALLBACK(&im_commit_cb), NULL);
4947 g_signal_connect(G_OBJECT(xic), "preedit_changed",
4948 G_CALLBACK(&im_preedit_changed_cb), NULL);
4949 g_signal_connect(G_OBJECT(xic), "preedit_start",
4950 G_CALLBACK(&im_preedit_start_cb), NULL);
4951 g_signal_connect(G_OBJECT(xic), "preedit_end",
4952 G_CALLBACK(&im_preedit_end_cb), NULL);
4953
4954 gtk_im_context_set_client_window(xic, gui.drawarea->window);
4955}
4956
4957 void
4958im_shutdown(void)
4959{
4960#ifdef XIM_DEBUG
4961 xim_log("im_shutdown()\n");
4962#endif
4963
4964 if (xic != NULL)
4965 {
4966 gtk_im_context_focus_out(xic);
4967 g_object_unref(xic);
4968 xic = NULL;
4969 }
4970 im_is_active = FALSE;
4971 im_commit_handler_id = 0;
4972 preedit_start_col = MAXCOL;
4973 xim_has_preediting = FALSE;
4974}
4975
4976/*
4977 * Convert the string argument to keyval and state for GdkEventKey.
4978 * If str is valid return TRUE, otherwise FALSE.
4979 *
4980 * See 'imactivatekey' for documentation of the format.
4981 */
4982 static int
4983im_string_to_keyval(const char *str, unsigned int *keyval, unsigned int *state)
4984{
4985 const char *mods_end;
4986 unsigned tmp_keyval;
4987 unsigned tmp_state = 0;
4988
4989 mods_end = strrchr(str, '-');
4990 mods_end = (mods_end != NULL) ? mods_end + 1 : str;
4991
4992 /* Parse modifier keys */
4993 while (str < mods_end)
4994 switch (*str++)
4995 {
4996 case '-': break;
4997 case 'S': case 's': tmp_state |= (unsigned)GDK_SHIFT_MASK; break;
4998 case 'L': case 'l': tmp_state |= (unsigned)GDK_LOCK_MASK; break;
4999 case 'C': case 'c': tmp_state |= (unsigned)GDK_CONTROL_MASK;break;
5000 case '1': tmp_state |= (unsigned)GDK_MOD1_MASK; break;
5001 case '2': tmp_state |= (unsigned)GDK_MOD2_MASK; break;
5002 case '3': tmp_state |= (unsigned)GDK_MOD3_MASK; break;
5003 case '4': tmp_state |= (unsigned)GDK_MOD4_MASK; break;
5004 case '5': tmp_state |= (unsigned)GDK_MOD5_MASK; break;
5005 default:
5006 return FALSE;
5007 }
5008
5009 tmp_keyval = gdk_keyval_from_name(str);
5010
5011 if (tmp_keyval == 0 || tmp_keyval == GDK_VoidSymbol)
5012 return FALSE;
5013
5014 if (keyval != NULL)
5015 *keyval = tmp_keyval;
5016 if (state != NULL)
5017 *state = tmp_state;
5018
5019 return TRUE;
5020}
5021
5022/*
5023 * Return TRUE if p_imak is valid, otherwise FALSE. As a special case, an
5024 * empty string is also regarded as valid.
5025 *
5026 * Note: The numerical key value of p_imak is cached if it was valid; thus
5027 * boldly assuming im_xim_isvalid_imactivate() will always be called whenever
5028 * 'imak' changes. This is currently the case but not obvious -- should
5029 * probably rename the function for clarity.
5030 */
5031 int
5032im_xim_isvalid_imactivate(void)
5033{
5034 if (p_imak[0] == NUL)
5035 {
5036 im_activatekey_keyval = GDK_VoidSymbol;
5037 im_activatekey_state = 0;
5038 return TRUE;
5039 }
5040
5041 return im_string_to_keyval((const char *)p_imak,
5042 &im_activatekey_keyval,
5043 &im_activatekey_state);
5044}
5045
5046 static void
5047im_synthesize_keypress(unsigned int keyval, unsigned int state)
5048{
5049 GdkEventKey *event;
5050
5051# ifdef HAVE_GTK_MULTIHEAD
5052 event = (GdkEventKey *)gdk_event_new(GDK_KEY_PRESS);
5053 g_object_ref(gui.drawarea->window); /* unreffed by gdk_event_free() */
5054# else
5055 event = (GdkEventKey *)g_malloc0((gulong)sizeof(GdkEvent));
5056 event->type = GDK_KEY_PRESS;
5057# endif
5058 event->window = gui.drawarea->window;
5059 event->send_event = TRUE;
5060 event->time = GDK_CURRENT_TIME;
5061 event->state = state;
5062 event->keyval = keyval;
5063 event->hardware_keycode = /* needed for XIM */
5064 XKeysymToKeycode(GDK_WINDOW_XDISPLAY(event->window), (KeySym)keyval);
5065 event->length = 0;
5066 event->string = NULL;
5067
5068 gtk_im_context_filter_keypress(xic, event);
5069
5070 /* For consistency, also send the corresponding release event. */
5071 event->type = GDK_KEY_RELEASE;
5072 event->send_event = FALSE;
5073 gtk_im_context_filter_keypress(xic, event);
5074
5075# ifdef HAVE_GTK_MULTIHEAD
5076 gdk_event_free((GdkEvent *)event);
5077# else
5078 g_free(event);
5079# endif
5080}
5081
5082 void
5083xim_reset(void)
5084{
5085 if (xic != NULL)
5086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 gtk_im_context_reset(xic);
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (p_imdisable)
5090 im_shutdown();
5091 else
5092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 xim_set_focus(gui.in_focus);
5094
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005095# ifdef FEAT_EVAL
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005096 if (p_imaf[0] != NUL)
5097 {
5098 char_u *argv[1];
5099
5100 if (im_is_active)
5101 argv[0] = (char_u *)"1";
5102 else
5103 argv[0] = (char_u *)"0";
5104 (void)call_func_retnr(p_imaf, 1, argv, FALSE);
5105 }
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005106 else
5107# endif
5108 if (im_activatekey_keyval != GDK_VoidSymbol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
5110 if (im_is_active)
5111 {
5112 g_signal_handler_block(xic, im_commit_handler_id);
5113 im_synthesize_keypress(im_activatekey_keyval,
5114 im_activatekey_state);
5115 g_signal_handler_unblock(xic, im_commit_handler_id);
5116 }
5117 }
5118 else
5119 {
5120 im_shutdown();
5121 xim_init();
5122 xim_set_focus(gui.in_focus);
5123 }
5124 }
5125 }
5126
5127 preedit_start_col = MAXCOL;
5128 xim_has_preediting = FALSE;
5129}
5130
5131 int
5132xim_queue_key_press_event(GdkEventKey *event, int down)
5133{
5134 if (down)
5135 {
5136 /*
5137 * Workaround GTK2 XIM 'feature' that always converts keypad keys to
5138 * chars., even when not part of an IM sequence (ref. feature of
5139 * gdk/gdkkeyuni.c).
5140 * Flag any keypad keys that might represent a single char.
5141 * If this (on its own - i.e., not part of an IM sequence) is
5142 * committed while we're processing one of these keys, we can ignore
5143 * that commit and go ahead & process it ourselves. That way we can
5144 * still distinguish keypad keys for use in mappings.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005145 * Also add GDK_space to make <S-Space> work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 */
5147 switch (event->keyval)
5148 {
5149 case GDK_KP_Add: xim_expected_char = '+'; break;
5150 case GDK_KP_Subtract: xim_expected_char = '-'; break;
5151 case GDK_KP_Divide: xim_expected_char = '/'; break;
5152 case GDK_KP_Multiply: xim_expected_char = '*'; break;
5153 case GDK_KP_Decimal: xim_expected_char = '.'; break;
5154 case GDK_KP_Equal: xim_expected_char = '='; break;
5155 case GDK_KP_0: xim_expected_char = '0'; break;
5156 case GDK_KP_1: xim_expected_char = '1'; break;
5157 case GDK_KP_2: xim_expected_char = '2'; break;
5158 case GDK_KP_3: xim_expected_char = '3'; break;
5159 case GDK_KP_4: xim_expected_char = '4'; break;
5160 case GDK_KP_5: xim_expected_char = '5'; break;
5161 case GDK_KP_6: xim_expected_char = '6'; break;
5162 case GDK_KP_7: xim_expected_char = '7'; break;
5163 case GDK_KP_8: xim_expected_char = '8'; break;
5164 case GDK_KP_9: xim_expected_char = '9'; break;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005165 case GDK_space: xim_expected_char = ' '; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 default: xim_expected_char = NUL;
5167 }
5168 xim_ignored_char = FALSE;
5169 }
5170
5171 /*
5172 * When typing fFtT, XIM may be activated. Thus it must pass
5173 * gtk_im_context_filter_keypress() in Normal mode.
5174 * And while doing :sh too.
5175 */
5176 if (xic != NULL && !p_imdisable
5177 && (State & (INSERT | CMDLINE | NORMAL | EXTERNCMD)) != 0)
5178 {
5179 /*
5180 * Filter 'imactivatekey' and map it to CTRL-^. This way, Vim is
5181 * always aware of the current status of IM, and can even emulate
5182 * the activation key for modules that don't support one.
5183 */
5184 if (event->keyval == im_activatekey_keyval
5185 && (event->state & im_activatekey_state) == im_activatekey_state)
5186 {
5187 unsigned int state_mask;
5188
5189 /* Require the state of the 3 most used modifiers to match exactly.
5190 * Otherwise e.g. <S-C-space> would be unusable for other purposes
5191 * if the IM activate key is <S-space>. */
5192 state_mask = im_activatekey_state;
5193 state_mask |= ((int)GDK_SHIFT_MASK | (int)GDK_CONTROL_MASK
5194 | (int)GDK_MOD1_MASK);
5195
5196 if ((event->state & state_mask) != im_activatekey_state)
5197 return FALSE;
5198
5199 /* Don't send it a second time on GDK_KEY_RELEASE. */
5200 if (event->type != GDK_KEY_PRESS)
5201 return TRUE;
5202
Bram Moolenaar658b74a2006-03-18 21:33:02 +00005203 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
5205 im_set_active(FALSE);
5206
5207 /* ":lmap" mappings exists, toggle use of mappings. */
5208 State ^= LANGMAP;
5209 if (State & LANGMAP)
5210 {
5211 curbuf->b_p_iminsert = B_IMODE_NONE;
5212 State &= ~LANGMAP;
5213 }
5214 else
5215 {
5216 curbuf->b_p_iminsert = B_IMODE_LMAP;
5217 State |= LANGMAP;
5218 }
5219 return TRUE;
5220 }
5221
5222 return gtk_im_context_filter_keypress(xic, event);
5223 }
5224
5225 /* Don't filter events through the IM context if IM isn't active
5226 * right now. Unlike with GTK+ 1.2 we cannot rely on the IM module
5227 * not doing anything before the activation key was sent. */
5228 if (im_activatekey_keyval == GDK_VoidSymbol || im_is_active)
5229 {
5230 int imresult = gtk_im_context_filter_keypress(xic, event);
5231
5232 /* Some XIM send following sequence:
5233 * 1. preedited string.
5234 * 2. committed string.
5235 * 3. line changed key.
5236 * 4. preedited string.
5237 * 5. remove preedited string.
5238 * if 3, Vim can't move back the above line for 5.
5239 * thus, this part should not parse the key. */
5240 if (!imresult && preedit_start_col != MAXCOL
5241 && event->keyval == GDK_Return)
5242 {
5243 im_synthesize_keypress(GDK_Return, 0U);
5244 return FALSE;
5245 }
5246
5247 /* If XIM tried to commit a keypad key as a single char.,
5248 * ignore it so we can use the keypad key 'raw', for mappings. */
5249 if (xim_expected_char != NUL && xim_ignored_char)
5250 /* We had a keypad key, and XIM tried to thieve it */
5251 return FALSE;
5252
Bram Moolenaar673214b2011-07-27 18:25:44 +02005253 /* This is supposed to fix a problem with iBus, that space
5254 * characters don't work in input mode. */
5255 xim_expected_char = NUL;
5256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 /* Normal processing */
5258 return imresult;
5259 }
5260 }
5261
5262 return FALSE;
5263}
5264
5265 int
5266im_get_status(void)
5267{
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005268# ifdef FEAT_EVAL
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005269 if (p_imsf[0] != NUL)
5270 {
5271 int is_active;
5272
5273 /* FIXME: Don't execute user function in unsafe situation. */
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005274 if (exiting
5275# ifdef FEAT_AUTOCMD
5276 || is_autocmd_blocked()
5277# endif
5278 )
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005279 return FALSE;
5280 /* FIXME: :py print 'xxx' is shown duplicate result.
5281 * Use silent to avoid it. */
5282 ++msg_silent;
5283 is_active = call_func_retnr(p_imsf, 0, NULL, FALSE);
5284 --msg_silent;
5285 return (is_active > 0);
5286 }
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 return im_is_active;
5289}
5290
Bram Moolenaar64404472010-06-26 06:24:45 +02005291 int
5292preedit_get_status(void)
5293{
5294 return preedit_is_active;
5295}
5296
5297 int
5298im_is_preediting()
5299{
5300 return xim_has_preediting;
5301}
5302
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005303# else /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305static int xim_is_active = FALSE; /* XIM should be active in the current
5306 mode */
5307static int xim_has_focus = FALSE; /* XIM is really being used for Vim */
5308#ifdef FEAT_GUI_X11
5309static XIMStyle input_style;
5310static int status_area_enabled = TRUE;
5311#endif
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313/*
5314 * Switch using XIM on/off. This is used by the code that changes "State".
5315 */
5316 void
5317im_set_active(active)
5318 int active;
5319{
5320 if (xic == NULL)
5321 return;
5322
5323 /* If 'imdisable' is set, XIM is never active. */
5324 if (p_imdisable)
5325 active = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005326#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 else if (input_style & XIMPreeditPosition)
5328 /* There is a problem in switching XIM off when preediting is used,
5329 * and it is not clear how this can be solved. For now, keep XIM on
5330 * all the time, like it was done in Vim 5.8. */
5331 active = TRUE;
5332#endif
5333
5334 /* Remember the active state, it is needed when Vim gets keyboard focus. */
5335 xim_is_active = active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 xim_set_preedit();
5337}
5338
5339/*
5340 * Adjust using XIM for gaining or losing keyboard focus. Also called when
5341 * "xim_is_active" changes.
5342 */
5343 void
5344xim_set_focus(focus)
5345 int focus;
5346{
5347 if (xic == NULL)
5348 return;
5349
5350 /*
5351 * XIM only gets focus when the Vim window has keyboard focus and XIM has
5352 * been set active for the current mode.
5353 */
5354 if (focus && xim_is_active)
5355 {
5356 if (!xim_has_focus)
5357 {
5358 xim_has_focus = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 XSetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361 }
5362 else
5363 {
5364 if (xim_has_focus)
5365 {
5366 xim_has_focus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 XUnsetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 }
5370}
5371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 void
5373im_set_position(row, col)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005374 int row UNUSED;
5375 int col UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 xim_set_preedit();
5378}
5379
5380/*
5381 * Set the XIM to the current cursor position.
5382 */
5383 void
5384xim_set_preedit()
5385{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005386 XVaNestedList attr_list;
5387 XRectangle spot_area;
5388 XPoint over_spot;
5389 int line_space;
5390
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02005391 if (xic == NULL)
5392 return;
5393
5394 xim_set_focus(TRUE);
5395
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005396 if (!xim_has_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005398 /* hide XIM cursor */
5399 over_spot.x = 0;
5400 over_spot.y = -100; /* arbitrary invisible position */
5401 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5402 XNSpotLocation,
5403 &over_spot,
5404 NULL);
5405 XSetICValues(xic, XNPreeditAttributes, attr_list, NULL);
5406 XFree(attr_list);
5407 return;
5408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005410 if (input_style & XIMPreeditPosition)
5411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (xim_fg_color == INVALCOLOR)
5413 {
5414 xim_fg_color = gui.def_norm_pixel;
5415 xim_bg_color = gui.def_back_pixel;
5416 }
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005417 over_spot.x = TEXT_X(gui.col);
5418 over_spot.y = TEXT_Y(gui.row);
5419 spot_area.x = 0;
5420 spot_area.y = 0;
5421 spot_area.height = gui.char_height * Rows;
5422 spot_area.width = gui.char_width * Columns;
5423 line_space = gui.char_height;
5424 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5425 XNSpotLocation, &over_spot,
5426 XNForeground, (Pixel) xim_fg_color,
5427 XNBackground, (Pixel) xim_bg_color,
5428 XNArea, &spot_area,
5429 XNLineSpace, line_space,
5430 NULL);
5431 if (XSetICValues(xic, XNPreeditAttributes, attr_list, NULL))
5432 EMSG(_("E284: Cannot set IC values"));
5433 XFree(attr_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435}
5436
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005437#if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438static char e_xim[] = N_("E285: Failed to create input context");
5439#endif
5440
5441#if defined(FEAT_GUI_X11) || defined(PROTO)
5442# if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && !defined(sun)
5443# define USE_X11R6_XIM
5444# endif
5445
5446static int xim_real_init __ARGS((Window x11_window, Display *x11_display));
5447
5448
5449#ifdef USE_X11R6_XIM
5450static void xim_instantiate_cb __ARGS((Display *display, XPointer client_data, XPointer call_data));
5451static void xim_destroy_cb __ARGS((XIM im, XPointer client_data, XPointer call_data));
5452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 static void
5454xim_instantiate_cb(display, client_data, call_data)
5455 Display *display;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005456 XPointer client_data UNUSED;
5457 XPointer call_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459 Window x11_window;
5460 Display *x11_display;
5461
5462#ifdef XIM_DEBUG
5463 xim_log("xim_instantiate_cb()\n");
5464#endif
5465
5466 gui_get_x11_windis(&x11_window, &x11_display);
5467 if (display != x11_display)
5468 return;
5469
5470 xim_real_init(x11_window, x11_display);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005471 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if (xic != NULL)
5473 XUnregisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5474 xim_instantiate_cb, NULL);
5475}
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 static void
5478xim_destroy_cb(im, client_data, call_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005479 XIM im UNUSED;
5480 XPointer client_data UNUSED;
5481 XPointer call_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 Window x11_window;
5484 Display *x11_display;
5485
5486#ifdef XIM_DEBUG
5487 xim_log("xim_destroy_cb()\n");
5488#endif
5489 gui_get_x11_windis(&x11_window, &x11_display);
5490
5491 xic = NULL;
5492 status_area_enabled = FALSE;
5493
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005494 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
5496 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5497 xim_instantiate_cb, NULL);
5498}
5499#endif
5500
5501 void
5502xim_init()
5503{
5504 Window x11_window;
5505 Display *x11_display;
5506
5507#ifdef XIM_DEBUG
5508 xim_log("xim_init()\n");
5509#endif
5510
5511 gui_get_x11_windis(&x11_window, &x11_display);
5512
5513 xic = NULL;
5514
5515 if (xim_real_init(x11_window, x11_display))
5516 return;
5517
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005518 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520#ifdef USE_X11R6_XIM
5521 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5522 xim_instantiate_cb, NULL);
5523#endif
5524}
5525
5526 static int
5527xim_real_init(x11_window, x11_display)
5528 Window x11_window;
5529 Display *x11_display;
5530{
5531 int i;
5532 char *p,
5533 *s,
5534 *ns,
5535 *end,
5536 tmp[1024];
5537#define IMLEN_MAX 40
5538 char buf[IMLEN_MAX + 7];
5539 XIM xim = NULL;
5540 XIMStyles *xim_styles;
5541 XIMStyle this_input_style = 0;
5542 Boolean found;
5543 XPoint over_spot;
5544 XVaNestedList preedit_list, status_list;
5545
5546 input_style = 0;
5547 status_area_enabled = FALSE;
5548
5549 if (xic != NULL)
5550 return FALSE;
5551
5552 if (gui.rsrc_input_method != NULL && *gui.rsrc_input_method != NUL)
5553 {
5554 strcpy(tmp, gui.rsrc_input_method);
5555 for (ns = s = tmp; ns != NULL && *s != NUL;)
5556 {
5557 s = (char *)skipwhite((char_u *)s);
5558 if (*s == NUL)
5559 break;
5560 if ((ns = end = strchr(s, ',')) == NULL)
5561 end = s + strlen(s);
5562 while (isspace(((char_u *)end)[-1]))
5563 end--;
5564 *end = NUL;
5565
5566 if (strlen(s) <= IMLEN_MAX)
5567 {
5568 strcpy(buf, "@im=");
5569 strcat(buf, s);
5570 if ((p = XSetLocaleModifiers(buf)) != NULL && *p != NUL
5571 && (xim = XOpenIM(x11_display, NULL, NULL, NULL))
5572 != NULL)
5573 break;
5574 }
5575
5576 s = ns + 1;
5577 }
5578 }
5579
5580 if (xim == NULL && (p = XSetLocaleModifiers("")) != NULL && *p != NUL)
5581 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5582
5583 /* This is supposed to be useful to obtain characters through
5584 * XmbLookupString() without really using a XIM. */
5585 if (xim == NULL && (p = XSetLocaleModifiers("@im=none")) != NULL
5586 && *p != NUL)
5587 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5588
5589 if (xim == NULL)
5590 {
5591 /* Only give this message when verbose is set, because too many people
5592 * got this message when they didn't want to use a XIM. */
5593 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005594 {
5595 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 EMSG(_("E286: Failed to open input method"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005597 verbose_leave();
5598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 return FALSE;
5600 }
5601
5602#ifdef USE_X11R6_XIM
5603 {
5604 XIMCallback destroy_cb;
5605
5606 destroy_cb.callback = xim_destroy_cb;
5607 destroy_cb.client_data = NULL;
5608 if (XSetIMValues(xim, XNDestroyCallback, &destroy_cb, NULL))
5609 EMSG(_("E287: Warning: Could not set destroy callback to IM"));
5610 }
5611#endif
5612
5613 if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL) || !xim_styles)
5614 {
5615 EMSG(_("E288: input method doesn't support any style"));
5616 XCloseIM(xim);
5617 return FALSE;
5618 }
5619
5620 found = False;
5621 strcpy(tmp, gui.rsrc_preedit_type_name);
5622 for (s = tmp; s && !found; )
5623 {
5624 while (*s && isspace((unsigned char)*s))
5625 s++;
5626 if (!*s)
5627 break;
5628 if ((ns = end = strchr(s, ',')) != 0)
5629 ns++;
5630 else
5631 end = s + strlen(s);
5632 while (isspace((unsigned char)*end))
5633 end--;
5634 *end = '\0';
5635
5636 if (!strcmp(s, "OverTheSpot"))
5637 this_input_style = (XIMPreeditPosition | XIMStatusArea);
5638 else if (!strcmp(s, "OffTheSpot"))
5639 this_input_style = (XIMPreeditArea | XIMStatusArea);
5640 else if (!strcmp(s, "Root"))
5641 this_input_style = (XIMPreeditNothing | XIMStatusNothing);
5642
5643 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5644 {
5645 if (this_input_style == xim_styles->supported_styles[i])
5646 {
5647 found = True;
5648 break;
5649 }
5650 }
5651 if (!found)
5652 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5653 {
5654 if ((xim_styles->supported_styles[i] & this_input_style)
5655 == (this_input_style & ~XIMStatusArea))
5656 {
5657 this_input_style &= ~XIMStatusArea;
5658 found = True;
5659 break;
5660 }
5661 }
5662
5663 s = ns;
5664 }
5665 XFree(xim_styles);
5666
5667 if (!found)
5668 {
5669 /* Only give this message when verbose is set, because too many people
5670 * got this message when they didn't want to use a XIM. */
5671 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005672 {
5673 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 EMSG(_("E289: input method doesn't support my preedit type"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005675 verbose_leave();
5676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 XCloseIM(xim);
5678 return FALSE;
5679 }
5680
5681 over_spot.x = TEXT_X(gui.col);
5682 over_spot.y = TEXT_Y(gui.row);
5683 input_style = this_input_style;
5684
5685 /* A crash was reported when trying to pass gui.norm_font as XNFontSet,
5686 * thus that has been removed. Hopefully the default works... */
5687#ifdef FEAT_XFONTSET
5688 if (gui.fontset != NOFONTSET)
5689 {
5690 preedit_list = XVaCreateNestedList(0,
5691 XNSpotLocation, &over_spot,
5692 XNForeground, (Pixel)gui.def_norm_pixel,
5693 XNBackground, (Pixel)gui.def_back_pixel,
5694 XNFontSet, (XFontSet)gui.fontset,
5695 NULL);
5696 status_list = XVaCreateNestedList(0,
5697 XNForeground, (Pixel)gui.def_norm_pixel,
5698 XNBackground, (Pixel)gui.def_back_pixel,
5699 XNFontSet, (XFontSet)gui.fontset,
5700 NULL);
5701 }
5702 else
5703#endif
5704 {
5705 preedit_list = XVaCreateNestedList(0,
5706 XNSpotLocation, &over_spot,
5707 XNForeground, (Pixel)gui.def_norm_pixel,
5708 XNBackground, (Pixel)gui.def_back_pixel,
5709 NULL);
5710 status_list = XVaCreateNestedList(0,
5711 XNForeground, (Pixel)gui.def_norm_pixel,
5712 XNBackground, (Pixel)gui.def_back_pixel,
5713 NULL);
5714 }
5715
5716 xic = XCreateIC(xim,
5717 XNInputStyle, input_style,
5718 XNClientWindow, x11_window,
5719 XNFocusWindow, gui.wid,
5720 XNPreeditAttributes, preedit_list,
5721 XNStatusAttributes, status_list,
5722 NULL);
5723 XFree(status_list);
5724 XFree(preedit_list);
5725 if (xic != NULL)
5726 {
5727 if (input_style & XIMStatusArea)
5728 {
5729 xim_set_status_area();
5730 status_area_enabled = TRUE;
5731 }
5732 else
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005733 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735 else
5736 {
5737 EMSG(_(e_xim));
5738 XCloseIM(xim);
5739 return FALSE;
5740 }
5741
5742 return TRUE;
5743}
5744
5745#endif /* FEAT_GUI_X11 */
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747/*
5748 * Get IM status. When IM is on, return TRUE. Else return FALSE.
5749 * FIXME: This doesn't work correctly: Having focus doesn't always mean XIM is
5750 * active, when not having focus XIM may still be active (e.g., when using a
5751 * tear-off menu item).
5752 */
5753 int
5754im_get_status()
5755{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 return xim_has_focus;
5757}
5758
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005759# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
Bram Moolenaar64404472010-06-26 06:24:45 +02005761# if !defined(FEAT_GUI_GTK) || defined(PROTO)
5762/*
5763 * Set up the status area.
5764 *
5765 * This should use a separate Widget, but that seems not possible, because
5766 * preedit_area and status_area should be set to the same window as for the
5767 * text input. Unfortunately this means the status area pollutes the text
5768 * window...
5769 */
5770 void
5771xim_set_status_area()
Bram Moolenaarc236c162008-07-13 17:41:49 +00005772{
Bram Moolenaar64404472010-06-26 06:24:45 +02005773 XVaNestedList preedit_list = 0, status_list = 0, list = 0;
5774 XRectangle pre_area, status_area;
5775
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02005776 if (xic == NULL)
5777 return;
5778
Bram Moolenaar64404472010-06-26 06:24:45 +02005779 if (input_style & XIMStatusArea)
5780 {
5781 if (input_style & XIMPreeditArea)
5782 {
5783 XRectangle *needed_rect;
5784
5785 /* to get status_area width */
5786 status_list = XVaCreateNestedList(0, XNAreaNeeded,
5787 &needed_rect, NULL);
5788 XGetICValues(xic, XNStatusAttributes, status_list, NULL);
5789 XFree(status_list);
5790
5791 status_area.width = needed_rect->width;
5792 }
5793 else
5794 status_area.width = gui.char_width * Columns;
5795
5796 status_area.x = 0;
5797 status_area.y = gui.char_height * Rows + gui.border_offset;
5798 if (gui.which_scrollbars[SBAR_BOTTOM])
5799 status_area.y += gui.scrollbar_height;
5800#ifdef FEAT_MENU
5801 if (gui.menu_is_active)
5802 status_area.y += gui.menu_height;
5803#endif
5804 status_area.height = gui.char_height;
5805 status_list = XVaCreateNestedList(0, XNArea, &status_area, NULL);
5806 }
5807 else
5808 {
5809 status_area.x = 0;
5810 status_area.y = gui.char_height * Rows + gui.border_offset;
5811 if (gui.which_scrollbars[SBAR_BOTTOM])
5812 status_area.y += gui.scrollbar_height;
5813#ifdef FEAT_MENU
5814 if (gui.menu_is_active)
5815 status_area.y += gui.menu_height;
5816#endif
5817 status_area.width = 0;
5818 status_area.height = gui.char_height;
5819 }
5820
5821 if (input_style & XIMPreeditArea) /* off-the-spot */
5822 {
5823 pre_area.x = status_area.x + status_area.width;
5824 pre_area.y = gui.char_height * Rows + gui.border_offset;
5825 pre_area.width = gui.char_width * Columns - pre_area.x;
5826 if (gui.which_scrollbars[SBAR_BOTTOM])
5827 pre_area.y += gui.scrollbar_height;
5828#ifdef FEAT_MENU
5829 if (gui.menu_is_active)
5830 pre_area.y += gui.menu_height;
5831#endif
5832 pre_area.height = gui.char_height;
5833 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
5834 }
5835 else if (input_style & XIMPreeditPosition) /* over-the-spot */
5836 {
5837 pre_area.x = 0;
5838 pre_area.y = 0;
5839 pre_area.height = gui.char_height * Rows;
5840 pre_area.width = gui.char_width * Columns;
5841 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
5842 }
5843
5844 if (preedit_list && status_list)
5845 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
5846 XNStatusAttributes, status_list, NULL);
5847 else if (preedit_list)
5848 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
5849 NULL);
5850 else if (status_list)
5851 list = XVaCreateNestedList(0, XNStatusAttributes, status_list,
5852 NULL);
5853 else
5854 list = NULL;
5855
5856 if (list)
5857 {
5858 XSetICValues(xic, XNVaNestedList, list, NULL);
5859 XFree(list);
5860 }
5861 if (status_list)
5862 XFree(status_list);
5863 if (preedit_list)
5864 XFree(preedit_list);
5865}
5866
5867 int
5868xim_get_status_area_height()
5869{
5870 if (status_area_enabled)
5871 return gui.char_height;
5872 return 0;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005873}
5874# endif
5875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876#endif /* FEAT_XIM */
5877
5878#if defined(FEAT_MBYTE) || defined(PROTO)
5879
5880/*
5881 * Setup "vcp" for conversion from "from" to "to".
5882 * The names must have been made canonical with enc_canonize().
5883 * vcp->vc_type must have been initialized to CONV_NONE.
5884 * Note: cannot be used for conversion from/to ucs-2 and ucs-4 (will use utf-8
5885 * instead).
5886 * Afterwards invoke with "from" and "to" equal to NULL to cleanup.
5887 * Return FAIL when conversion is not supported, OK otherwise.
5888 */
5889 int
5890convert_setup(vcp, from, to)
5891 vimconv_T *vcp;
5892 char_u *from;
5893 char_u *to;
5894{
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005895 return convert_setup_ext(vcp, from, TRUE, to, TRUE);
5896}
5897
5898/*
5899 * As convert_setup(), but only when from_unicode_is_utf8 is TRUE will all
5900 * "from" unicode charsets be considered utf-8. Same for "to".
5901 */
5902 int
5903convert_setup_ext(vcp, from, from_unicode_is_utf8, to, to_unicode_is_utf8)
5904 vimconv_T *vcp;
5905 char_u *from;
5906 int from_unicode_is_utf8;
5907 char_u *to;
5908 int to_unicode_is_utf8;
5909{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 int from_prop;
5911 int to_prop;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005912 int from_is_utf8;
5913 int to_is_utf8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
5915 /* Reset to no conversion. */
5916# ifdef USE_ICONV
5917 if (vcp->vc_type == CONV_ICONV && vcp->vc_fd != (iconv_t)-1)
5918 iconv_close(vcp->vc_fd);
5919# endif
5920 vcp->vc_type = CONV_NONE;
5921 vcp->vc_factor = 1;
5922 vcp->vc_fail = FALSE;
5923
5924 /* No conversion when one of the names is empty or they are equal. */
5925 if (from == NULL || *from == NUL || to == NULL || *to == NUL
5926 || STRCMP(from, to) == 0)
5927 return OK;
5928
5929 from_prop = enc_canon_props(from);
5930 to_prop = enc_canon_props(to);
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005931 if (from_unicode_is_utf8)
5932 from_is_utf8 = from_prop & ENC_UNICODE;
5933 else
5934 from_is_utf8 = from_prop == ENC_UNICODE;
5935 if (to_unicode_is_utf8)
5936 to_is_utf8 = to_prop & ENC_UNICODE;
5937 else
5938 to_is_utf8 = to_prop == ENC_UNICODE;
5939
5940 if ((from_prop & ENC_LATIN1) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {
5942 /* Internal latin1 -> utf-8 conversion. */
5943 vcp->vc_type = CONV_TO_UTF8;
5944 vcp->vc_factor = 2; /* up to twice as long */
5945 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005946 else if ((from_prop & ENC_LATIN9) && to_is_utf8)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005947 {
5948 /* Internal latin9 -> utf-8 conversion. */
5949 vcp->vc_type = CONV_9_TO_UTF8;
5950 vcp->vc_factor = 3; /* up to three as long (euro sign) */
5951 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005952 else if (from_is_utf8 && (to_prop & ENC_LATIN1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 /* Internal utf-8 -> latin1 conversion. */
5955 vcp->vc_type = CONV_TO_LATIN1;
5956 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005957 else if (from_is_utf8 && (to_prop & ENC_LATIN9))
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005958 {
5959 /* Internal utf-8 -> latin9 conversion. */
5960 vcp->vc_type = CONV_TO_LATIN9;
5961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962#ifdef WIN3264
5963 /* Win32-specific codepage <-> codepage conversion without iconv. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005964 else if ((from_is_utf8 || encname2codepage(from) > 0)
5965 && (to_is_utf8 || encname2codepage(to) > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {
5967 vcp->vc_type = CONV_CODEPAGE;
5968 vcp->vc_factor = 2; /* up to twice as long */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005969 vcp->vc_cpfrom = from_is_utf8 ? 0 : encname2codepage(from);
5970 vcp->vc_cpto = to_is_utf8 ? 0 : encname2codepage(to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 }
5972#endif
5973#ifdef MACOS_X
5974 else if ((from_prop & ENC_MACROMAN) && (to_prop & ENC_LATIN1))
5975 {
5976 vcp->vc_type = CONV_MAC_LATIN1;
5977 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005978 else if ((from_prop & ENC_MACROMAN) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 vcp->vc_type = CONV_MAC_UTF8;
5981 vcp->vc_factor = 2; /* up to twice as long */
5982 }
5983 else if ((from_prop & ENC_LATIN1) && (to_prop & ENC_MACROMAN))
5984 {
5985 vcp->vc_type = CONV_LATIN1_MAC;
5986 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005987 else if (from_is_utf8 && (to_prop & ENC_MACROMAN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {
5989 vcp->vc_type = CONV_UTF8_MAC;
5990 }
5991#endif
5992# ifdef USE_ICONV
5993 else
5994 {
5995 /* Use iconv() for conversion. */
5996 vcp->vc_fd = (iconv_t)my_iconv_open(
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005997 to_is_utf8 ? (char_u *)"utf-8" : to,
5998 from_is_utf8 ? (char_u *)"utf-8" : from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (vcp->vc_fd != (iconv_t)-1)
6000 {
6001 vcp->vc_type = CONV_ICONV;
6002 vcp->vc_factor = 4; /* could be longer too... */
6003 }
6004 }
6005# endif
6006 if (vcp->vc_type == CONV_NONE)
6007 return FAIL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 return OK;
6010}
6011
6012#if defined(FEAT_GUI) || defined(AMIGA) || defined(WIN3264) \
6013 || defined(MSDOS) || defined(PROTO)
6014/*
6015 * Do conversion on typed input characters in-place.
6016 * The input and output are not NUL terminated!
6017 * Returns the length after conversion.
6018 */
6019 int
6020convert_input(ptr, len, maxlen)
6021 char_u *ptr;
6022 int len;
6023 int maxlen;
6024{
6025 return convert_input_safe(ptr, len, maxlen, NULL, NULL);
6026}
6027#endif
6028
6029/*
6030 * Like convert_input(), but when there is an incomplete byte sequence at the
6031 * end return that as an allocated string in "restp" and set "*restlenp" to
6032 * the length. If "restp" is NULL it is not used.
6033 */
6034 int
6035convert_input_safe(ptr, len, maxlen, restp, restlenp)
6036 char_u *ptr;
6037 int len;
6038 int maxlen;
6039 char_u **restp;
6040 int *restlenp;
6041{
6042 char_u *d;
6043 int dlen = len;
6044 int unconvertlen = 0;
6045
6046 d = string_convert_ext(&input_conv, ptr, &dlen,
6047 restp == NULL ? NULL : &unconvertlen);
6048 if (d != NULL)
6049 {
6050 if (dlen <= maxlen)
6051 {
6052 if (unconvertlen > 0)
6053 {
6054 /* Move the unconverted characters to allocated memory. */
6055 *restp = alloc(unconvertlen);
6056 if (*restp != NULL)
6057 mch_memmove(*restp, ptr + len - unconvertlen, unconvertlen);
6058 *restlenp = unconvertlen;
6059 }
6060 mch_memmove(ptr, d, dlen);
6061 }
6062 else
6063 /* result is too long, keep the unconverted text (the caller must
6064 * have done something wrong!) */
6065 dlen = len;
6066 vim_free(d);
6067 }
6068 return dlen;
6069}
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071/*
6072 * Convert text "ptr[*lenp]" according to "vcp".
6073 * Returns the result in allocated memory and sets "*lenp".
6074 * When "lenp" is NULL, use NUL terminated strings.
6075 * Illegal chars are often changed to "?", unless vcp->vc_fail is set.
6076 * When something goes wrong, NULL is returned and "*lenp" is unchanged.
6077 */
6078 char_u *
6079string_convert(vcp, ptr, lenp)
6080 vimconv_T *vcp;
6081 char_u *ptr;
6082 int *lenp;
6083{
6084 return string_convert_ext(vcp, ptr, lenp, NULL);
6085}
6086
6087/*
6088 * Like string_convert(), but when "unconvlenp" is not NULL and there are is
6089 * an incomplete sequence at the end it is not converted and "*unconvlenp" is
6090 * set to the number of remaining bytes.
6091 */
6092 char_u *
6093string_convert_ext(vcp, ptr, lenp, unconvlenp)
6094 vimconv_T *vcp;
6095 char_u *ptr;
6096 int *lenp;
6097 int *unconvlenp;
6098{
6099 char_u *retval = NULL;
6100 char_u *d;
6101 int len;
6102 int i;
6103 int l;
6104 int c;
6105
6106 if (lenp == NULL)
6107 len = (int)STRLEN(ptr);
6108 else
6109 len = *lenp;
6110 if (len == 0)
6111 return vim_strsave((char_u *)"");
6112
6113 switch (vcp->vc_type)
6114 {
6115 case CONV_TO_UTF8: /* latin1 to utf-8 conversion */
6116 retval = alloc(len * 2 + 1);
6117 if (retval == NULL)
6118 break;
6119 d = retval;
6120 for (i = 0; i < len; ++i)
6121 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006122 c = ptr[i];
6123 if (c < 0x80)
6124 *d++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 else
6126 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006127 *d++ = 0xc0 + ((unsigned)c >> 6);
6128 *d++ = 0x80 + (c & 0x3f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 }
6130 }
6131 *d = NUL;
6132 if (lenp != NULL)
6133 *lenp = (int)(d - retval);
6134 break;
6135
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006136 case CONV_9_TO_UTF8: /* latin9 to utf-8 conversion */
6137 retval = alloc(len * 3 + 1);
6138 if (retval == NULL)
6139 break;
6140 d = retval;
6141 for (i = 0; i < len; ++i)
6142 {
6143 c = ptr[i];
6144 switch (c)
6145 {
6146 case 0xa4: c = 0x20ac; break; /* euro */
6147 case 0xa6: c = 0x0160; break; /* S hat */
6148 case 0xa8: c = 0x0161; break; /* S -hat */
6149 case 0xb4: c = 0x017d; break; /* Z hat */
6150 case 0xb8: c = 0x017e; break; /* Z -hat */
6151 case 0xbc: c = 0x0152; break; /* OE */
6152 case 0xbd: c = 0x0153; break; /* oe */
6153 case 0xbe: c = 0x0178; break; /* Y */
6154 }
6155 d += utf_char2bytes(c, d);
6156 }
6157 *d = NUL;
6158 if (lenp != NULL)
6159 *lenp = (int)(d - retval);
6160 break;
6161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 case CONV_TO_LATIN1: /* utf-8 to latin1 conversion */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006163 case CONV_TO_LATIN9: /* utf-8 to latin9 conversion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 retval = alloc(len + 1);
6165 if (retval == NULL)
6166 break;
6167 d = retval;
6168 for (i = 0; i < len; ++i)
6169 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006170 l = utf_ptr2len_len(ptr + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 if (l == 0)
6172 *d++ = NUL;
6173 else if (l == 1)
6174 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006175 int l_w = utf8len_tab_zero[ptr[i]];
6176
6177 if (l_w == 0)
6178 {
6179 /* Illegal utf-8 byte cannot be converted */
6180 vim_free(retval);
6181 return NULL;
6182 }
6183 if (unconvlenp != NULL && l_w > len - i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
6185 /* Incomplete sequence at the end. */
6186 *unconvlenp = len - i;
6187 break;
6188 }
6189 *d++ = ptr[i];
6190 }
6191 else
6192 {
6193 c = utf_ptr2char(ptr + i);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006194 if (vcp->vc_type == CONV_TO_LATIN9)
6195 switch (c)
6196 {
6197 case 0x20ac: c = 0xa4; break; /* euro */
6198 case 0x0160: c = 0xa6; break; /* S hat */
6199 case 0x0161: c = 0xa8; break; /* S -hat */
6200 case 0x017d: c = 0xb4; break; /* Z hat */
6201 case 0x017e: c = 0xb8; break; /* Z -hat */
6202 case 0x0152: c = 0xbc; break; /* OE */
6203 case 0x0153: c = 0xbd; break; /* oe */
6204 case 0x0178: c = 0xbe; break; /* Y */
6205 case 0xa4:
6206 case 0xa6:
6207 case 0xa8:
6208 case 0xb4:
6209 case 0xb8:
6210 case 0xbc:
6211 case 0xbd:
6212 case 0xbe: c = 0x100; break; /* not in latin9 */
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 if (!utf_iscomposing(c)) /* skip composing chars */
6215 {
6216 if (c < 0x100)
6217 *d++ = c;
6218 else if (vcp->vc_fail)
6219 {
6220 vim_free(retval);
6221 return NULL;
6222 }
6223 else
6224 {
6225 *d++ = 0xbf;
6226 if (utf_char2cells(c) > 1)
6227 *d++ = '?';
6228 }
6229 }
6230 i += l - 1;
6231 }
6232 }
6233 *d = NUL;
6234 if (lenp != NULL)
6235 *lenp = (int)(d - retval);
6236 break;
6237
Bram Moolenaar56718732006-03-15 22:53:57 +00006238# ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 case CONV_MAC_LATIN1:
6240 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006241 'm', 'l', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 break;
6243
6244 case CONV_LATIN1_MAC:
6245 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006246 'l', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 break;
6248
6249 case CONV_MAC_UTF8:
6250 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006251 'm', 'u', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 break;
6253
6254 case CONV_UTF8_MAC:
6255 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006256 'u', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 break;
6258# endif
6259
6260# ifdef USE_ICONV
6261 case CONV_ICONV: /* conversion with output_conv.vc_fd */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006262 retval = iconv_string(vcp, ptr, len, unconvlenp, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 break;
6264# endif
6265# ifdef WIN3264
6266 case CONV_CODEPAGE: /* codepage -> codepage */
6267 {
6268 int retlen;
6269 int tmp_len;
6270 short_u *tmp;
6271
6272 /* 1. codepage/UTF-8 -> ucs-2. */
6273 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006274 tmp_len = utf8_to_utf16(ptr, len, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 else
Bram Moolenaarffeedec2013-02-13 16:49:58 +01006276 {
6277 tmp_len = MultiByteToWideChar(vcp->vc_cpfrom,
6278 unconvlenp ? MB_ERR_INVALID_CHARS : 0,
6279 ptr, len, 0, 0);
6280 if (tmp_len == 0
6281 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION)
6282 {
6283 if (lenp != NULL)
6284 *lenp = 0;
6285 if (unconvlenp != NULL)
6286 *unconvlenp = len;
6287 retval = alloc(1);
6288 if (retval)
6289 retval[0] = NUL;
6290 return retval;
6291 }
6292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 tmp = (short_u *)alloc(sizeof(short_u) * tmp_len);
6294 if (tmp == NULL)
6295 break;
6296 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006297 utf8_to_utf16(ptr, len, tmp, unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 else
6299 MultiByteToWideChar(vcp->vc_cpfrom, 0, ptr, len, tmp, tmp_len);
6300
6301 /* 2. ucs-2 -> codepage/UTF-8. */
6302 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006303 retlen = utf16_to_utf8(tmp, tmp_len, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 else
6305 retlen = WideCharToMultiByte(vcp->vc_cpto, 0,
6306 tmp, tmp_len, 0, 0, 0, 0);
6307 retval = alloc(retlen + 1);
6308 if (retval != NULL)
6309 {
6310 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006311 utf16_to_utf8(tmp, tmp_len, retval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 else
6313 WideCharToMultiByte(vcp->vc_cpto, 0,
6314 tmp, tmp_len, retval, retlen, 0, 0);
6315 retval[retlen] = NUL;
6316 if (lenp != NULL)
6317 *lenp = retlen;
6318 }
6319 vim_free(tmp);
6320 break;
6321 }
6322# endif
6323 }
6324
6325 return retval;
6326}
6327#endif