blob: 0531f18116817ad93a37603185d061dbd4b38b07 [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},
Bram Moolenaar57bc4632014-11-19 17:05:55 +0100408 {"pck", IDX_SJIS}, /* Sun: PCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 {"euckr", IDX_EUC_KR},
410 {"5601", IDX_EUC_KR}, /* Sun: KS C 5601 */
411 {"euccn", IDX_EUC_CN},
412 {"gb2312", IDX_EUC_CN},
413 {"euctw", IDX_EUC_TW},
414#if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
415 {"japan", IDX_CP932},
416 {"korea", IDX_CP949},
417 {"prc", IDX_CP936},
418 {"chinese", IDX_CP936},
419 {"taiwan", IDX_CP950},
420 {"big5", IDX_CP950},
421#else
422 {"japan", IDX_EUC_JP},
423 {"korea", IDX_EUC_KR},
424 {"prc", IDX_EUC_CN},
425 {"chinese", IDX_EUC_CN},
426 {"taiwan", IDX_EUC_TW},
427 {"cp950", IDX_BIG5},
428 {"950", IDX_BIG5},
429#endif
430 {"mac", IDX_MACROMAN},
Bram Moolenaarae177eb2006-05-13 15:06:23 +0000431 {"mac-roman", IDX_MACROMAN},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 {NULL, 0}
433};
434
435#ifndef CP_UTF8
436# define CP_UTF8 65001 /* magic number from winnls.h */
437#endif
438
439/*
440 * Find encoding "name" in the list of canonical encoding names.
441 * Returns -1 if not found.
442 */
443 static int
444enc_canon_search(name)
445 char_u *name;
446{
447 int i;
448
449 for (i = 0; i < IDX_COUNT; ++i)
450 if (STRCMP(name, enc_canon_table[i].name) == 0)
451 return i;
452 return -1;
453}
454
455#endif
456
457#if defined(FEAT_MBYTE) || defined(PROTO)
458
459/*
460 * Find canonical encoding "name" in the list and return its properties.
461 * Returns 0 if not found.
462 */
463 int
464enc_canon_props(name)
465 char_u *name;
466{
467 int i;
468
469 i = enc_canon_search(name);
470 if (i >= 0)
471 return enc_canon_table[i].prop;
472#ifdef WIN3264
473 if (name[0] == 'c' && name[1] == 'p' && VIM_ISDIGIT(name[2]))
474 {
475 CPINFO cpinfo;
476
477 /* Get info on this codepage to find out what it is. */
478 if (GetCPInfo(atoi(name + 2), &cpinfo) != 0)
479 {
480 if (cpinfo.MaxCharSize == 1) /* some single-byte encoding */
481 return ENC_8BIT;
482 if (cpinfo.MaxCharSize == 2
483 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
484 /* must be a DBCS encoding */
485 return ENC_DBCS;
486 }
487 return 0;
488 }
489#endif
490 if (STRNCMP(name, "2byte-", 6) == 0)
491 return ENC_DBCS;
492 if (STRNCMP(name, "8bit-", 5) == 0 || STRNCMP(name, "iso-8859-", 9) == 0)
493 return ENC_8BIT;
494 return 0;
495}
496
497/*
498 * Set up for using multi-byte characters.
499 * Called in three cases:
500 * - by main() to initialize (p_enc == NULL)
501 * - by set_init_1() after 'encoding' was set to its default.
502 * - by do_set() when 'encoding' has been set.
503 * p_enc must have been passed through enc_canonize() already.
504 * Sets the "enc_unicode", "enc_utf8", "enc_dbcs" and "has_mbyte" flags.
505 * Fills mb_bytelen_tab[] and returns NULL when there are no problems.
506 * When there is something wrong: Returns an error message and doesn't change
507 * anything.
508 */
509 char_u *
510mb_init()
511{
512 int i;
513 int idx;
514 int n;
515 int enc_dbcs_new = 0;
516#if defined(USE_ICONV) && !defined(WIN3264) && !defined(WIN32UNIX) \
517 && !defined(MACOS)
518# define LEN_FROM_CONV
519 vimconv_T vimconv;
520 char_u *p;
521#endif
522
523 if (p_enc == NULL)
524 {
525 /* Just starting up: set the whole table to one's. */
526 for (i = 0; i < 256; ++i)
527 mb_bytelen_tab[i] = 1;
528 input_conv.vc_type = CONV_NONE;
529 input_conv.vc_factor = 1;
530 output_conv.vc_type = CONV_NONE;
531 return NULL;
532 }
533
534#ifdef WIN3264
535 if (p_enc[0] == 'c' && p_enc[1] == 'p' && VIM_ISDIGIT(p_enc[2]))
536 {
537 CPINFO cpinfo;
538
539 /* Get info on this codepage to find out what it is. */
540 if (GetCPInfo(atoi(p_enc + 2), &cpinfo) != 0)
541 {
542 if (cpinfo.MaxCharSize == 1)
543 {
544 /* some single-byte encoding */
545 enc_unicode = 0;
546 enc_utf8 = FALSE;
547 }
548 else if (cpinfo.MaxCharSize == 2
549 && (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
550 {
551 /* must be a DBCS encoding, check below */
552 enc_dbcs_new = atoi(p_enc + 2);
553 }
554 else
555 goto codepage_invalid;
556 }
557 else if (GetLastError() == ERROR_INVALID_PARAMETER)
558 {
559codepage_invalid:
560 return (char_u *)N_("E543: Not a valid codepage");
561 }
562 }
563#endif
564 else if (STRNCMP(p_enc, "8bit-", 5) == 0
565 || STRNCMP(p_enc, "iso-8859-", 9) == 0)
566 {
567 /* Accept any "8bit-" or "iso-8859-" name. */
568 enc_unicode = 0;
569 enc_utf8 = FALSE;
570 }
571 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
572 {
573#ifdef WIN3264
574 /* Windows: accept only valid codepage numbers, check below. */
575 if (p_enc[6] != 'c' || p_enc[7] != 'p'
576 || (enc_dbcs_new = atoi(p_enc + 8)) == 0)
577 return e_invarg;
578#else
579 /* Unix: accept any "2byte-" name, assume current locale. */
580 enc_dbcs_new = DBCS_2BYTE;
581#endif
582 }
583 else if ((idx = enc_canon_search(p_enc)) >= 0)
584 {
585 i = enc_canon_table[idx].prop;
586 if (i & ENC_UNICODE)
587 {
588 /* Unicode */
589 enc_utf8 = TRUE;
590 if (i & (ENC_2BYTE | ENC_2WORD))
591 enc_unicode = 2;
592 else if (i & ENC_4BYTE)
593 enc_unicode = 4;
594 else
595 enc_unicode = 0;
596 }
597 else if (i & ENC_DBCS)
598 {
599 /* 2byte, handle below */
600 enc_dbcs_new = enc_canon_table[idx].codepage;
601 }
602 else
603 {
604 /* Must be 8-bit. */
605 enc_unicode = 0;
606 enc_utf8 = FALSE;
607 }
608 }
609 else /* Don't know what encoding this is, reject it. */
610 return e_invarg;
611
612 if (enc_dbcs_new != 0)
613 {
614#ifdef WIN3264
615 /* Check if the DBCS code page is OK. */
616 if (!IsValidCodePage(enc_dbcs_new))
617 goto codepage_invalid;
618#endif
619 enc_unicode = 0;
620 enc_utf8 = FALSE;
621 }
622 enc_dbcs = enc_dbcs_new;
623 has_mbyte = (enc_dbcs != 0 || enc_utf8);
624
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100625#if defined(WIN3264) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 enc_codepage = encname2codepage(p_enc);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000627 enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
629
Bram Moolenaar78622822005-08-23 21:00:13 +0000630 /* Detect an encoding that uses latin1 characters. */
631 enc_latin1like = (enc_utf8 || STRCMP(p_enc, "latin1") == 0
632 || STRCMP(p_enc, "iso-8859-15") == 0);
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 /*
635 * Set the function pointers.
636 */
637 if (enc_utf8)
638 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000639 mb_ptr2len = utfc_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000640 mb_ptr2len_len = utfc_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 mb_char2len = utf_char2len;
642 mb_char2bytes = utf_char2bytes;
643 mb_ptr2cells = utf_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000644 mb_ptr2cells_len = utf_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 mb_char2cells = utf_char2cells;
646 mb_off2cells = utf_off2cells;
647 mb_ptr2char = utf_ptr2char;
648 mb_head_off = utf_head_off;
649 }
650 else if (enc_dbcs != 0)
651 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000652 mb_ptr2len = dbcs_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000653 mb_ptr2len_len = dbcs_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 mb_char2len = dbcs_char2len;
655 mb_char2bytes = dbcs_char2bytes;
656 mb_ptr2cells = dbcs_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000657 mb_ptr2cells_len = dbcs_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 mb_char2cells = dbcs_char2cells;
659 mb_off2cells = dbcs_off2cells;
660 mb_ptr2char = dbcs_ptr2char;
661 mb_head_off = dbcs_head_off;
662 }
663 else
664 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000665 mb_ptr2len = latin_ptr2len;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000666 mb_ptr2len_len = latin_ptr2len_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 mb_char2len = latin_char2len;
668 mb_char2bytes = latin_char2bytes;
669 mb_ptr2cells = latin_ptr2cells;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +0000670 mb_ptr2cells_len = latin_ptr2cells_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 mb_char2cells = latin_char2cells;
672 mb_off2cells = latin_off2cells;
673 mb_ptr2char = latin_ptr2char;
674 mb_head_off = latin_head_off;
675 }
676
677 /*
678 * Fill the mb_bytelen_tab[] for MB_BYTE2LEN().
679 */
680#ifdef LEN_FROM_CONV
681 /* When 'encoding' is different from the current locale mblen() won't
682 * work. Use conversion to "utf-8" instead. */
683 vimconv.vc_type = CONV_NONE;
684 if (enc_dbcs)
685 {
686 p = enc_locale();
687 if (p == NULL || STRCMP(p, p_enc) != 0)
688 {
689 convert_setup(&vimconv, p_enc, (char_u *)"utf-8");
690 vimconv.vc_fail = TRUE;
691 }
692 vim_free(p);
693 }
694#endif
695
696 for (i = 0; i < 256; ++i)
697 {
698 /* Our own function to reliably check the length of UTF-8 characters,
699 * independent of mblen(). */
700 if (enc_utf8)
701 n = utf8len_tab[i];
702 else if (enc_dbcs == 0)
703 n = 1;
704 else
705 {
706#if defined(WIN3264) || defined(WIN32UNIX)
707 /* enc_dbcs is set by setting 'fileencoding'. It becomes a Windows
708 * CodePage identifier, which we can pass directly in to Windows
709 * API */
Bram Moolenaarca058dc2014-01-14 13:26:21 +0100710 n = IsDBCSLeadByteEx(enc_dbcs, (WINBYTE)i) ? 2 : 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711#else
Bram Moolenaar4064e242014-03-12 14:54:34 +0100712# if defined(MACOS) || defined(__amigaos4__) || defined(__ANDROID__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 /*
714 * if mblen() is not available, character which MSB is turned on
715 * are treated as leading byte character. (note : This assumption
716 * is not always true.)
717 */
718 n = (i & 0x80) ? 2 : 1;
719# else
Bram Moolenaar9a920d82012-06-01 15:21:02 +0200720 char buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721# ifdef X_LOCALE
722# ifndef mblen
723# define mblen _Xmblen
724# endif
725# endif
726 if (i == NUL) /* just in case mblen() can't handle "" */
727 n = 1;
728 else
729 {
730 buf[0] = i;
731 buf[1] = 0;
732#ifdef LEN_FROM_CONV
733 if (vimconv.vc_type != CONV_NONE)
734 {
735 /*
736 * string_convert() should fail when converting the first
737 * byte of a double-byte character.
738 */
739 p = string_convert(&vimconv, (char_u *)buf, NULL);
740 if (p != NULL)
741 {
742 vim_free(p);
743 n = 1;
744 }
745 else
746 n = 2;
747 }
748 else
749#endif
750 {
751 /*
752 * mblen() should return -1 for invalid (means the leading
753 * multibyte) character. However there are some platforms
754 * where mblen() returns 0 for invalid character.
755 * Therefore, following condition includes 0.
756 */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000757 ignored = mblen(NULL, 0); /* First reset the state. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (mblen(buf, (size_t)1) <= 0)
759 n = 2;
760 else
761 n = 1;
762 }
763 }
764# endif
765#endif
766 }
767
768 mb_bytelen_tab[i] = n;
769 }
770
771#ifdef LEN_FROM_CONV
772 convert_setup(&vimconv, NULL, NULL);
773#endif
774
775 /* The cell width depends on the type of multi-byte characters. */
776 (void)init_chartab();
777
778 /* When enc_utf8 is set or reset, (de)allocate ScreenLinesUC[] */
779 screenalloc(FALSE);
780
781 /* When using Unicode, set default for 'fileencodings'. */
782 if (enc_utf8 && !option_was_set((char_u *)"fencs"))
783 set_string_option_direct((char_u *)"fencs", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000784 (char_u *)"ucs-bom,utf-8,default,latin1", OPT_FREE, 0);
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786#if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
787 /* GNU gettext 0.10.37 supports this feature: set the codeset used for
788 * translated messages independently from the current locale. */
789 (void)bind_textdomain_codeset(VIMPACKAGE,
790 enc_utf8 ? "utf-8" : (char *)p_enc);
791#endif
792
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000793#ifdef WIN32
794 /* When changing 'encoding' while starting up, then convert the command
795 * line arguments from the active codepage to 'encoding'. */
796 if (starting != 0)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000797 fix_arg_enc();
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#ifdef FEAT_AUTOCMD
801 /* Fire an autocommand to let people do custom font setup. This must be
802 * after Vim has been setup for the new encoding. */
803 apply_autocmds(EVENT_ENCODINGCHANGED, NULL, (char_u *)"", FALSE, curbuf);
804#endif
805
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000806#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000807 /* Need to reload spell dictionaries */
808 spell_reload();
809#endif
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 return NULL;
812}
813
814/*
815 * Return the size of the BOM for the current buffer:
816 * 0 - no BOM
817 * 2 - UCS-2 or UTF-16 BOM
818 * 4 - UCS-4 BOM
819 * 3 - UTF-8 BOM
820 */
821 int
822bomb_size()
823{
824 int n = 0;
825
826 if (curbuf->b_p_bomb && !curbuf->b_p_bin)
827 {
828 if (*curbuf->b_p_fenc == NUL)
829 {
830 if (enc_utf8)
831 {
832 if (enc_unicode != 0)
833 n = enc_unicode;
834 else
835 n = 3;
836 }
837 }
838 else if (STRCMP(curbuf->b_p_fenc, "utf-8") == 0)
839 n = 3;
840 else if (STRNCMP(curbuf->b_p_fenc, "ucs-2", 5) == 0
841 || STRNCMP(curbuf->b_p_fenc, "utf-16", 6) == 0)
842 n = 2;
843 else if (STRNCMP(curbuf->b_p_fenc, "ucs-4", 5) == 0)
844 n = 4;
845 }
846 return n;
847}
848
849/*
Bram Moolenaar836082d2011-08-10 13:21:46 +0200850 * Remove all BOM from "s" by moving remaining text.
851 */
852 void
853remove_bom(s)
854 char_u *s;
855{
856 if (enc_utf8)
857 {
858 char_u *p = s;
859
860 while ((p = vim_strbyte(p, 0xef)) != NULL)
861 {
862 if (p[1] == 0xbb && p[2] == 0xbf)
863 STRMOVE(p, p + 3);
864 else
865 ++p;
866 }
867 }
868}
869
870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 * Get class of pointer:
872 * 0 for blank or NUL
873 * 1 for punctuation
874 * 2 for an (ASCII) word character
875 * >2 for other word characters
876 */
877 int
878mb_get_class(p)
879 char_u *p;
880{
Bram Moolenaarf813a182013-01-30 13:59:37 +0100881 return mb_get_class_buf(p, curbuf);
882}
883
884 int
885mb_get_class_buf(p, buf)
886 char_u *p;
887 buf_T *buf;
888{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (MB_BYTE2LEN(p[0]) == 1)
890 {
891 if (p[0] == NUL || vim_iswhite(p[0]))
892 return 0;
Bram Moolenaarf813a182013-01-30 13:59:37 +0100893 if (vim_iswordc_buf(p[0], buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 return 2;
895 return 1;
896 }
897 if (enc_dbcs != 0 && p[0] != NUL && p[1] != NUL)
898 return dbcs_class(p[0], p[1]);
899 if (enc_utf8)
900 return utf_class(utf_ptr2char(p));
901 return 0;
902}
903
904/*
905 * Get class of a double-byte character. This always returns 3 or bigger.
906 * TODO: Should return 1 for punctuation.
907 */
908 int
909dbcs_class(lead, trail)
910 unsigned lead;
911 unsigned trail;
912{
913 switch (enc_dbcs)
914 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200915 /* please add classify routine for your language in here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 case DBCS_JPNU: /* ? */
918 case DBCS_JPN:
919 {
920 /* JIS code classification */
921 unsigned char lb = lead;
922 unsigned char tb = trail;
923
924 /* convert process code to JIS */
925# if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
926 /* process code is SJIS */
927 if (lb <= 0x9f)
928 lb = (lb - 0x81) * 2 + 0x21;
929 else
930 lb = (lb - 0xc1) * 2 + 0x21;
931 if (tb <= 0x7e)
932 tb -= 0x1f;
933 else if (tb <= 0x9e)
934 tb -= 0x20;
935 else
936 {
937 tb -= 0x7e;
938 lb += 1;
939 }
940# else
941 /*
942 * XXX: Code page identification can not use with all
943 * system! So, some other encoding information
944 * will be needed.
945 * In japanese: SJIS,EUC,UNICODE,(JIS)
946 * Note that JIS-code system don't use as
947 * process code in most system because it uses
948 * escape sequences(JIS is context depend encoding).
949 */
950 /* assume process code is JAPANESE-EUC */
951 lb &= 0x7f;
952 tb &= 0x7f;
953# endif
954 /* exceptions */
955 switch (lb << 8 | tb)
956 {
957 case 0x2121: /* ZENKAKU space */
958 return 0;
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100959 case 0x2122: /* TOU-TEN (Japanese comma) */
960 case 0x2123: /* KU-TEN (Japanese period) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 case 0x2124: /* ZENKAKU comma */
962 case 0x2125: /* ZENKAKU period */
963 return 1;
964 case 0x213c: /* prolongedsound handled as KATAKANA */
965 return 13;
966 }
967 /* sieved by KU code */
968 switch (lb)
969 {
970 case 0x21:
971 case 0x22:
972 /* special symbols */
973 return 10;
974 case 0x23:
975 /* alpha-numeric */
976 return 11;
977 case 0x24:
978 /* hiragana */
979 return 12;
980 case 0x25:
981 /* katakana */
982 return 13;
983 case 0x26:
984 /* greek */
985 return 14;
986 case 0x27:
987 /* russian */
988 return 15;
989 case 0x28:
990 /* lines */
991 return 16;
992 default:
993 /* kanji */
994 return 17;
995 }
996 }
997
998 case DBCS_KORU: /* ? */
999 case DBCS_KOR:
1000 {
1001 /* KS code classification */
1002 unsigned char c1 = lead;
1003 unsigned char c2 = trail;
1004
1005 /*
1006 * 20 : Hangul
1007 * 21 : Hanja
1008 * 22 : Symbols
1009 * 23 : Alpha-numeric/Roman Letter (Full width)
1010 * 24 : Hangul Letter(Alphabet)
1011 * 25 : Roman Numeral/Greek Letter
1012 * 26 : Box Drawings
1013 * 27 : Unit Symbols
1014 * 28 : Circled/Parenthesized Letter
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001015 * 29 : Hiragana/Katakana
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * 30 : Cyrillic Letter
1017 */
1018
1019 if (c1 >= 0xB0 && c1 <= 0xC8)
1020 /* Hangul */
1021 return 20;
1022#if defined(WIN3264) || defined(WIN32UNIX)
1023 else if (c1 <= 0xA0 || c2 <= 0xA0)
1024 /* Extended Hangul Region : MS UHC(Unified Hangul Code) */
1025 /* c1: 0x81-0xA0 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xFE
1026 * c1: 0xA1-0xC6 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xA0
1027 */
1028 return 20;
1029#endif
1030
1031 else if (c1 >= 0xCA && c1 <= 0xFD)
1032 /* Hanja */
1033 return 21;
1034 else switch (c1)
1035 {
1036 case 0xA1:
1037 case 0xA2:
1038 /* Symbols */
1039 return 22;
1040 case 0xA3:
1041 /* Alpha-numeric */
1042 return 23;
1043 case 0xA4:
1044 /* Hangul Letter(Alphabet) */
1045 return 24;
1046 case 0xA5:
1047 /* Roman Numeral/Greek Letter */
1048 return 25;
1049 case 0xA6:
1050 /* Box Drawings */
1051 return 26;
1052 case 0xA7:
1053 /* Unit Symbols */
1054 return 27;
1055 case 0xA8:
1056 case 0xA9:
1057 if (c2 <= 0xAF)
1058 return 25; /* Roman Letter */
1059 else if (c2 >= 0xF6)
1060 return 22; /* Symbols */
1061 else
1062 /* Circled/Parenthesized Letter */
1063 return 28;
1064 case 0xAA:
1065 case 0xAB:
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001066 /* Hiragana/Katakana */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return 29;
1068 case 0xAC:
1069 /* Cyrillic Letter */
1070 return 30;
1071 }
1072 }
1073 default:
1074 break;
1075 }
1076 return 3;
1077}
1078
1079/*
1080 * mb_char2len() function pointer.
1081 * Return length in bytes of character "c".
1082 * Returns 1 for a single-byte character.
1083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 int
1085latin_char2len(c)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001086 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
1088 return 1;
1089}
1090
1091 static int
1092dbcs_char2len(c)
1093 int c;
1094{
1095 if (c >= 0x100)
1096 return 2;
1097 return 1;
1098}
1099
1100/*
1101 * mb_char2bytes() function pointer.
1102 * Convert a character to its bytes.
1103 * Returns the length in bytes.
1104 */
1105 int
1106latin_char2bytes(c, buf)
1107 int c;
1108 char_u *buf;
1109{
1110 buf[0] = c;
1111 return 1;
1112}
1113
1114 static int
1115dbcs_char2bytes(c, buf)
1116 int c;
1117 char_u *buf;
1118{
1119 if (c >= 0x100)
1120 {
1121 buf[0] = (unsigned)c >> 8;
1122 buf[1] = c;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001123 /* Never use a NUL byte, it causes lots of trouble. It's an invalid
1124 * character anyway. */
1125 if (buf[1] == NUL)
1126 buf[1] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 return 2;
1128 }
1129 buf[0] = c;
1130 return 1;
1131}
1132
1133/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001134 * mb_ptr2len() function pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 * Get byte length of character at "*p" but stop at a NUL.
1136 * For UTF-8 this includes following composing characters.
1137 * Returns 0 when *p is NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 */
1139 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001140latin_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 char_u *p;
1142{
1143 return MB_BYTE2LEN(*p);
1144}
1145
1146 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001147dbcs_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 char_u *p;
1149{
1150 int len;
1151
1152 /* Check if second byte is not missing. */
1153 len = MB_BYTE2LEN(*p);
1154 if (len == 2 && p[1] == NUL)
1155 len = 1;
1156 return len;
1157}
1158
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001159/*
1160 * mb_ptr2len_len() function pointer.
1161 * Like mb_ptr2len(), but limit to read "size" bytes.
1162 * Returns 0 for an empty string.
1163 * Returns 1 for an illegal char or an incomplete byte sequence.
1164 */
1165 int
1166latin_ptr2len_len(p, size)
1167 char_u *p;
1168 int size;
1169{
1170 if (size < 1 || *p == NUL)
1171 return 0;
1172 return 1;
1173}
1174
1175 static int
1176dbcs_ptr2len_len(p, size)
1177 char_u *p;
1178 int size;
1179{
1180 int len;
1181
1182 if (size < 1 || *p == NUL)
1183 return 0;
1184 if (size == 1)
1185 return 1;
1186 /* Check that second byte is not missing. */
1187 len = MB_BYTE2LEN(*p);
1188 if (len == 2 && p[1] == NUL)
1189 len = 1;
1190 return len;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193struct interval
1194{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01001195 long first;
1196 long last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197};
1198static int intable __ARGS((struct interval *table, size_t size, int c));
1199
1200/*
1201 * Return TRUE if "c" is in "table[size / sizeof(struct interval)]".
1202 */
1203 static int
1204intable(table, size, c)
1205 struct interval *table;
1206 size_t size;
1207 int c;
1208{
1209 int mid, bot, top;
1210
1211 /* first quick check for Latin1 etc. characters */
1212 if (c < table[0].first)
1213 return FALSE;
1214
1215 /* binary search in table */
1216 bot = 0;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001217 top = (int)(size / sizeof(struct interval) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 while (top >= bot)
1219 {
1220 mid = (bot + top) / 2;
1221 if (table[mid].last < c)
1222 bot = mid + 1;
1223 else if (table[mid].first > c)
1224 top = mid - 1;
1225 else
1226 return TRUE;
1227 }
1228 return FALSE;
1229}
1230
1231/*
1232 * For UTF-8 character "c" return 2 for a double-width character, 1 for others.
1233 * Returns 4 or 6 for an unprintable character.
1234 * Is only correct for characters >= 0x80.
1235 * When p_ambw is "double", return 2 for a character with East Asian Width
1236 * class 'A'(mbiguous).
1237 */
1238 int
1239utf_char2cells(c)
1240 int c;
1241{
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001242 /* Sorted list of non-overlapping intervals of East Asian double width
1243 * characters, generated with ../runtime/tools/unicode.vim. */
1244 static struct interval doublewidth[] =
1245 {
1246 {0x1100, 0x115f},
1247 {0x11a3, 0x11a7},
1248 {0x11fa, 0x11ff},
1249 {0x2329, 0x232a},
1250 {0x2e80, 0x2e99},
1251 {0x2e9b, 0x2ef3},
1252 {0x2f00, 0x2fd5},
1253 {0x2ff0, 0x2ffb},
1254 {0x3000, 0x3029},
1255 {0x3030, 0x303e},
1256 {0x3041, 0x3096},
1257 {0x309b, 0x30ff},
1258 {0x3105, 0x312d},
1259 {0x3131, 0x318e},
1260 {0x3190, 0x31b7},
1261 {0x31c0, 0x31e3},
1262 {0x31f0, 0x321e},
1263 {0x3220, 0x3247},
1264 {0x3250, 0x32fe},
1265 {0x3300, 0x4dbf},
1266 {0x4e00, 0xa48c},
1267 {0xa490, 0xa4c6},
1268 {0xa960, 0xa97c},
1269 {0xac00, 0xd7a3},
1270 {0xd7b0, 0xd7c6},
1271 {0xd7cb, 0xd7fb},
1272 {0xf900, 0xfaff},
1273 {0xfe10, 0xfe19},
1274 {0xfe30, 0xfe52},
1275 {0xfe54, 0xfe66},
1276 {0xfe68, 0xfe6b},
1277 {0xff01, 0xff60},
1278 {0xffe0, 0xffe6},
1279 {0x1f200, 0x1f200},
1280 {0x1f210, 0x1f231},
1281 {0x1f240, 0x1f248},
1282 {0x20000, 0x2fffd},
1283 {0x30000, 0x3fffd}
1284 };
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01001285 /* Sorted list of non-overlapping intervals of East Asian Ambiguous
1286 * characters, generated with ../runtime/tools/unicode.vim. */
1287 static struct interval ambiguous[] =
1288 {
1289 {0x00a1, 0x00a1},
1290 {0x00a4, 0x00a4},
1291 {0x00a7, 0x00a8},
1292 {0x00aa, 0x00aa},
1293 {0x00ad, 0x00ae},
1294 {0x00b0, 0x00b4},
1295 {0x00b6, 0x00ba},
1296 {0x00bc, 0x00bf},
1297 {0x00c6, 0x00c6},
1298 {0x00d0, 0x00d0},
1299 {0x00d7, 0x00d8},
1300 {0x00de, 0x00e1},
1301 {0x00e6, 0x00e6},
1302 {0x00e8, 0x00ea},
1303 {0x00ec, 0x00ed},
1304 {0x00f0, 0x00f0},
1305 {0x00f2, 0x00f3},
1306 {0x00f7, 0x00fa},
1307 {0x00fc, 0x00fc},
1308 {0x00fe, 0x00fe},
1309 {0x0101, 0x0101},
1310 {0x0111, 0x0111},
1311 {0x0113, 0x0113},
1312 {0x011b, 0x011b},
1313 {0x0126, 0x0127},
1314 {0x012b, 0x012b},
1315 {0x0131, 0x0133},
1316 {0x0138, 0x0138},
1317 {0x013f, 0x0142},
1318 {0x0144, 0x0144},
1319 {0x0148, 0x014b},
1320 {0x014d, 0x014d},
1321 {0x0152, 0x0153},
1322 {0x0166, 0x0167},
1323 {0x016b, 0x016b},
1324 {0x01ce, 0x01ce},
1325 {0x01d0, 0x01d0},
1326 {0x01d2, 0x01d2},
1327 {0x01d4, 0x01d4},
1328 {0x01d6, 0x01d6},
1329 {0x01d8, 0x01d8},
1330 {0x01da, 0x01da},
1331 {0x01dc, 0x01dc},
1332 {0x0251, 0x0251},
1333 {0x0261, 0x0261},
1334 {0x02c4, 0x02c4},
1335 {0x02c7, 0x02c7},
1336 {0x02c9, 0x02cb},
1337 {0x02cd, 0x02cd},
1338 {0x02d0, 0x02d0},
1339 {0x02d8, 0x02db},
1340 {0x02dd, 0x02dd},
1341 {0x02df, 0x02df},
1342 {0x0391, 0x03a1},
1343 {0x03a3, 0x03a9},
1344 {0x03b1, 0x03c1},
1345 {0x03c3, 0x03c9},
1346 {0x0401, 0x0401},
1347 {0x0410, 0x044f},
1348 {0x0451, 0x0451},
1349 {0x2010, 0x2010},
1350 {0x2013, 0x2016},
1351 {0x2018, 0x2019},
1352 {0x201c, 0x201d},
1353 {0x2020, 0x2022},
1354 {0x2024, 0x2027},
1355 {0x2030, 0x2030},
1356 {0x2032, 0x2033},
1357 {0x2035, 0x2035},
1358 {0x203b, 0x203b},
1359 {0x203e, 0x203e},
1360 {0x2074, 0x2074},
1361 {0x207f, 0x207f},
1362 {0x2081, 0x2084},
1363 {0x20ac, 0x20ac},
1364 {0x2103, 0x2103},
1365 {0x2105, 0x2105},
1366 {0x2109, 0x2109},
1367 {0x2113, 0x2113},
1368 {0x2116, 0x2116},
1369 {0x2121, 0x2122},
1370 {0x2126, 0x2126},
1371 {0x212b, 0x212b},
1372 {0x2153, 0x2154},
1373 {0x215b, 0x215e},
1374 {0x2160, 0x216b},
1375 {0x2170, 0x2179},
1376 {0x2189, 0x2189},
1377 {0x2190, 0x2199},
1378 {0x21b8, 0x21b9},
1379 {0x21d2, 0x21d2},
1380 {0x21d4, 0x21d4},
1381 {0x21e7, 0x21e7},
1382 {0x2200, 0x2200},
1383 {0x2202, 0x2203},
1384 {0x2207, 0x2208},
1385 {0x220b, 0x220b},
1386 {0x220f, 0x220f},
1387 {0x2211, 0x2211},
1388 {0x2215, 0x2215},
1389 {0x221a, 0x221a},
1390 {0x221d, 0x2220},
1391 {0x2223, 0x2223},
1392 {0x2225, 0x2225},
1393 {0x2227, 0x222c},
1394 {0x222e, 0x222e},
1395 {0x2234, 0x2237},
1396 {0x223c, 0x223d},
1397 {0x2248, 0x2248},
1398 {0x224c, 0x224c},
1399 {0x2252, 0x2252},
1400 {0x2260, 0x2261},
1401 {0x2264, 0x2267},
1402 {0x226a, 0x226b},
1403 {0x226e, 0x226f},
1404 {0x2282, 0x2283},
1405 {0x2286, 0x2287},
1406 {0x2295, 0x2295},
1407 {0x2299, 0x2299},
1408 {0x22a5, 0x22a5},
1409 {0x22bf, 0x22bf},
1410 {0x2312, 0x2312},
1411 {0x2460, 0x24e9},
1412 {0x24eb, 0x254b},
1413 {0x2550, 0x2573},
1414 {0x2580, 0x258f},
1415 {0x2592, 0x2595},
1416 {0x25a0, 0x25a1},
1417 {0x25a3, 0x25a9},
1418 {0x25b2, 0x25b3},
1419 {0x25b6, 0x25b7},
1420 {0x25bc, 0x25bd},
1421 {0x25c0, 0x25c1},
1422 {0x25c6, 0x25c8},
1423 {0x25cb, 0x25cb},
1424 {0x25ce, 0x25d1},
1425 {0x25e2, 0x25e5},
1426 {0x25ef, 0x25ef},
1427 {0x2605, 0x2606},
1428 {0x2609, 0x2609},
1429 {0x260e, 0x260f},
1430 {0x2614, 0x2615},
1431 {0x261c, 0x261c},
1432 {0x261e, 0x261e},
1433 {0x2640, 0x2640},
1434 {0x2642, 0x2642},
1435 {0x2660, 0x2661},
1436 {0x2663, 0x2665},
1437 {0x2667, 0x266a},
1438 {0x266c, 0x266d},
1439 {0x266f, 0x266f},
1440 {0x269e, 0x269f},
1441 {0x26be, 0x26bf},
1442 {0x26c4, 0x26cd},
1443 {0x26cf, 0x26e1},
1444 {0x26e3, 0x26e3},
1445 {0x26e8, 0x26ff},
1446 {0x273d, 0x273d},
1447 {0x2757, 0x2757},
1448 {0x2776, 0x277f},
1449 {0x2b55, 0x2b59},
1450 {0x3248, 0x324f},
1451 {0xe000, 0xf8ff},
1452 {0xfffd, 0xfffd},
1453 {0x1f100, 0x1f10a},
1454 {0x1f110, 0x1f12d},
1455 {0x1f131, 0x1f131},
1456 {0x1f13d, 0x1f13d},
1457 {0x1f13f, 0x1f13f},
1458 {0x1f142, 0x1f142},
1459 {0x1f146, 0x1f146},
1460 {0x1f14a, 0x1f14e},
1461 {0x1f157, 0x1f157},
1462 {0x1f15f, 0x1f15f},
1463 {0x1f179, 0x1f179},
1464 {0x1f17b, 0x1f17c},
1465 {0x1f17f, 0x1f17f},
1466 {0x1f18a, 0x1f18d},
1467 {0x1f190, 0x1f190},
1468 {0xf0000, 0xffffd},
1469 {0x100000, 0x10fffd}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 };
1471
1472 if (c >= 0x100)
1473 {
1474#ifdef USE_WCHAR_FUNCTIONS
1475 /*
1476 * Assume the library function wcwidth() works better than our own
1477 * stuff. It should return 1 for ambiguous width chars!
1478 */
1479 int n = wcwidth(c);
1480
1481 if (n < 0)
1482 return 6; /* unprintable, displays <xxxx> */
1483 if (n > 1)
1484 return n;
1485#else
1486 if (!utf_printable(c))
1487 return 6; /* unprintable, displays <xxxx> */
Bram Moolenaarda4d7a92010-01-27 18:29:26 +01001488 if (intable(doublewidth, sizeof(doublewidth), c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 return 2;
1490#endif
1491 }
1492
1493 /* Characters below 0x100 are influenced by 'isprint' option */
1494 else if (c >= 0x80 && !vim_isprintc(c))
1495 return 4; /* unprintable, displays <xx> */
1496
1497 if (c >= 0x80 && *p_ambw == 'd' && intable(ambiguous, sizeof(ambiguous), c))
1498 return 2;
1499
1500 return 1;
1501}
1502
1503/*
1504 * mb_ptr2cells() function pointer.
1505 * Return the number of display cells character at "*p" occupies.
1506 * This doesn't take care of unprintable characters, use ptr2cells() for that.
1507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 int
1509latin_ptr2cells(p)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001510 char_u *p UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 return 1;
1513}
1514
1515 int
1516utf_ptr2cells(p)
1517 char_u *p;
1518{
1519 int c;
1520
1521 /* Need to convert to a wide character. */
1522 if (*p >= 0x80)
1523 {
1524 c = utf_ptr2char(p);
1525 /* An illegal byte is displayed as <xx>. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001526 if (utf_ptr2len(p) == 1 || c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 return 4;
1528 /* If the char is ASCII it must be an overlong sequence. */
1529 if (c < 0x80)
1530 return char2cells(c);
1531 return utf_char2cells(c);
1532 }
1533 return 1;
1534}
1535
1536 int
1537dbcs_ptr2cells(p)
1538 char_u *p;
1539{
1540 /* Number of cells is equal to number of bytes, except for euc-jp when
1541 * the first byte is 0x8e. */
1542 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
1543 return 1;
1544 return MB_BYTE2LEN(*p);
1545}
1546
1547/*
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001548 * mb_ptr2cells_len() function pointer.
1549 * Like mb_ptr2cells(), but limit string length to "size".
1550 * For an empty string or truncated character returns 1.
1551 */
1552 int
1553latin_ptr2cells_len(p, size)
1554 char_u *p UNUSED;
1555 int size UNUSED;
1556{
1557 return 1;
1558}
1559
1560 static int
1561utf_ptr2cells_len(p, size)
1562 char_u *p;
1563 int size;
1564{
1565 int c;
1566
1567 /* Need to convert to a wide character. */
1568 if (size > 0 && *p >= 0x80)
1569 {
1570 if (utf_ptr2len_len(p, size) < utf8len_tab[*p])
Bram Moolenaar24397332009-12-02 14:02:39 +00001571 return 1; /* truncated */
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00001572 c = utf_ptr2char(p);
1573 /* An illegal byte is displayed as <xx>. */
1574 if (utf_ptr2len(p) == 1 || c == NUL)
1575 return 4;
1576 /* If the char is ASCII it must be an overlong sequence. */
1577 if (c < 0x80)
1578 return char2cells(c);
1579 return utf_char2cells(c);
1580 }
1581 return 1;
1582}
1583
1584 static int
1585dbcs_ptr2cells_len(p, size)
1586 char_u *p;
1587 int size;
1588{
1589 /* Number of cells is equal to number of bytes, except for euc-jp when
1590 * the first byte is 0x8e. */
1591 if (size <= 1 || (enc_dbcs == DBCS_JPNU && *p == 0x8e))
1592 return 1;
1593 return MB_BYTE2LEN(*p);
1594}
1595
1596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 * mb_char2cells() function pointer.
1598 * Return the number of display cells character "c" occupies.
1599 * Only takes care of multi-byte chars, not "^C" and such.
1600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 int
1602latin_char2cells(c)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001603 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605 return 1;
1606}
1607
1608 static int
1609dbcs_char2cells(c)
1610 int c;
1611{
1612 /* Number of cells is equal to number of bytes, except for euc-jp when
1613 * the first byte is 0x8e. */
1614 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
1615 return 1;
1616 /* use the first byte */
1617 return MB_BYTE2LEN((unsigned)c >> 8);
1618}
1619
1620/*
Bram Moolenaar72597a52010-07-18 15:31:08 +02001621 * Return the number of cells occupied by string "p".
1622 * Stop at a NUL character. When "len" >= 0 stop at character "p[len]".
1623 */
1624 int
1625mb_string2cells(p, len)
1626 char_u *p;
1627 int len;
1628{
1629 int i;
1630 int clen = 0;
1631
1632 for (i = 0; (len < 0 || i < len) && p[i] != NUL; i += (*mb_ptr2len)(p + i))
1633 clen += (*mb_ptr2cells)(p + i);
1634 return clen;
1635}
1636
1637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * mb_off2cells() function pointer.
1639 * Return number of display cells for char at ScreenLines[off].
Bram Moolenaar367329b2007-08-30 11:53:22 +00001640 * We make sure that the offset used is less than "max_off".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001643latin_off2cells(off, max_off)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001644 unsigned off UNUSED;
1645 unsigned max_off UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 return 1;
1648}
1649
1650 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001651dbcs_off2cells(off, max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 unsigned off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001653 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001655 /* never check beyond end of the line */
1656 if (off >= max_off)
1657 return 1;
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 /* Number of cells is equal to number of bytes, except for euc-jp when
1660 * the first byte is 0x8e. */
1661 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1662 return 1;
1663 return MB_BYTE2LEN(ScreenLines[off]);
1664}
1665
1666 int
Bram Moolenaar367329b2007-08-30 11:53:22 +00001667utf_off2cells(off, max_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 unsigned off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001669 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
Bram Moolenaar367329b2007-08-30 11:53:22 +00001671 return (off + 1 < max_off && ScreenLines[off + 1] == 0) ? 2 : 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672}
1673
1674/*
1675 * mb_ptr2char() function pointer.
1676 * Convert a byte sequence into a character.
1677 */
1678 int
1679latin_ptr2char(p)
1680 char_u *p;
1681{
1682 return *p;
1683}
1684
1685 static int
1686dbcs_ptr2char(p)
1687 char_u *p;
1688{
1689 if (MB_BYTE2LEN(*p) > 1 && p[1] != NUL)
1690 return (p[0] << 8) + p[1];
1691 return *p;
1692}
1693
1694/*
1695 * Convert a UTF-8 byte sequence to a wide character.
1696 * If the sequence is illegal or truncated by a NUL the first byte is
1697 * returned.
1698 * Does not include composing characters, of course.
1699 */
1700 int
1701utf_ptr2char(p)
1702 char_u *p;
1703{
1704 int len;
1705
1706 if (p[0] < 0x80) /* be quick for ASCII */
1707 return p[0];
1708
Bram Moolenaar24397332009-12-02 14:02:39 +00001709 len = utf8len_tab_zero[p[0]];
Bram Moolenaar89bf0922008-06-29 14:16:06 +00001710 if (len > 1 && (p[1] & 0xc0) == 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
1712 if (len == 2)
1713 return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
1714 if ((p[2] & 0xc0) == 0x80)
1715 {
1716 if (len == 3)
1717 return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
1718 + (p[2] & 0x3f);
1719 if ((p[3] & 0xc0) == 0x80)
1720 {
1721 if (len == 4)
1722 return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
1723 + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
1724 if ((p[4] & 0xc0) == 0x80)
1725 {
1726 if (len == 5)
1727 return ((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
1728 + ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
1729 + (p[4] & 0x3f);
1730 if ((p[5] & 0xc0) == 0x80 && len == 6)
1731 return ((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
1732 + ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
1733 + ((p[4] & 0x3f) << 6) + (p[5] & 0x3f);
1734 }
1735 }
1736 }
1737 }
1738 /* Illegal value, just return the first byte */
1739 return p[0];
1740}
1741
1742/*
Bram Moolenaar35ee4522011-07-15 21:16:59 +02001743 * Convert a UTF-8 byte sequence to a wide character.
1744 * String is assumed to be terminated by NUL or after "n" bytes, whichever
1745 * comes first.
1746 * The function is safe in the sense that it never accesses memory beyond the
1747 * first "n" bytes of "s".
1748 *
1749 * On success, returns decoded codepoint, advances "s" to the beginning of
1750 * next character and decreases "n" accordingly.
1751 *
1752 * If end of string was reached, returns 0 and, if "n" > 0, advances "s" past
1753 * NUL byte.
1754 *
1755 * If byte sequence is illegal or incomplete, returns -1 and does not advance
1756 * "s".
1757 */
1758 static int
1759utf_safe_read_char_adv(s, n)
1760 char_u **s;
1761 size_t *n;
1762{
1763 int c, k;
1764
1765 if (*n == 0) /* end of buffer */
1766 return 0;
1767
1768 k = utf8len_tab_zero[**s];
1769
1770 if (k == 1)
1771 {
1772 /* ASCII character or NUL */
1773 (*n)--;
1774 return *(*s)++;
1775 }
1776
1777 if ((size_t)k <= *n)
1778 {
1779 /* We have a multibyte sequence and it isn't truncated by buffer
1780 * limits so utf_ptr2char() is safe to use. Or the first byte is
1781 * illegal (k=0), and it's also safe to use utf_ptr2char(). */
1782 c = utf_ptr2char(*s);
1783
1784 /* On failure, utf_ptr2char() returns the first byte, so here we
1785 * check equality with the first byte. The only non-ASCII character
1786 * which equals the first byte of its own UTF-8 representation is
1787 * U+00C3 (UTF-8: 0xC3 0x83), so need to check that special case too.
1788 * It's safe even if n=1, else we would have k=2 > n. */
1789 if (c != (int)(**s) || (c == 0xC3 && (*s)[1] == 0x83))
1790 {
1791 /* byte sequence was successfully decoded */
1792 *s += k;
1793 *n -= k;
1794 return c;
1795 }
1796 }
1797
1798 /* byte sequence is incomplete or illegal */
1799 return -1;
1800}
1801
1802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 * Get character at **pp and advance *pp to the next character.
1804 * Note: composing characters are skipped!
1805 */
1806 int
1807mb_ptr2char_adv(pp)
1808 char_u **pp;
1809{
1810 int c;
1811
1812 c = (*mb_ptr2char)(*pp);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001813 *pp += (*mb_ptr2len)(*pp);
1814 return c;
1815}
1816
1817/*
1818 * Get character at **pp and advance *pp to the next character.
1819 * Note: composing characters are returned as separate characters.
1820 */
1821 int
1822mb_cptr2char_adv(pp)
1823 char_u **pp;
1824{
1825 int c;
1826
1827 c = (*mb_ptr2char)(*pp);
1828 if (enc_utf8)
1829 *pp += utf_ptr2len(*pp);
1830 else
1831 *pp += (*mb_ptr2len)(*pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 return c;
1833}
1834
1835#if defined(FEAT_ARABIC) || defined(PROTO)
1836/*
1837 * Check whether we are dealing with Arabic combining characters.
1838 * Note: these are NOT really composing characters!
1839 */
1840 int
1841arabic_combine(one, two)
1842 int one; /* first character */
1843 int two; /* character just after "one" */
1844{
1845 if (one == a_LAM)
1846 return arabic_maycombine(two);
1847 return FALSE;
1848}
1849
1850/*
1851 * Check whether we are dealing with a character that could be regarded as an
1852 * Arabic combining character, need to check the character before this.
1853 */
1854 int
1855arabic_maycombine(two)
1856 int two;
1857{
1858 if (p_arshape && !p_tbidi)
1859 return (two == a_ALEF_MADDA
1860 || two == a_ALEF_HAMZA_ABOVE
1861 || two == a_ALEF_HAMZA_BELOW
1862 || two == a_ALEF);
1863 return FALSE;
1864}
1865
1866/*
1867 * Check if the character pointed to by "p2" is a composing character when it
1868 * comes after "p1". For Arabic sometimes "ab" is replaced with "c", which
1869 * behaves like a composing character.
1870 */
1871 int
1872utf_composinglike(p1, p2)
1873 char_u *p1;
1874 char_u *p2;
1875{
1876 int c2;
1877
1878 c2 = utf_ptr2char(p2);
1879 if (utf_iscomposing(c2))
1880 return TRUE;
1881 if (!arabic_maycombine(c2))
1882 return FALSE;
1883 return arabic_combine(utf_ptr2char(p1), c2);
1884}
1885#endif
1886
1887/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001888 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 * composing characters.
1890 */
1891 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001892utfc_ptr2char(p, pcc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001894 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 int len;
1897 int c;
1898 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001899 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001902 len = utf_ptr2len(p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 /* Only accept a composing char when the first char isn't illegal. */
1905 if ((len > 1 || *p < 0x80)
1906 && p[len] >= 0x80
1907 && UTF_COMPOSINGLIKE(p, p + len))
1908 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001909 cc = utf_ptr2char(p + len);
1910 for (;;)
1911 {
1912 pcc[i++] = cc;
1913 if (i == MAX_MCO)
1914 break;
1915 len += utf_ptr2len(p + len);
1916 if (p[len] < 0x80 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1917 break;
1918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001920
1921 if (i < MAX_MCO) /* last composing char must be 0 */
1922 pcc[i] = 0;
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 return c;
1925}
1926
1927/*
Bram Moolenaar29466f22007-05-10 17:45:37 +00001928 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 * composing characters. Use no more than p[maxlen].
1930 */
1931 int
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001932utfc_ptr2char_len(p, pcc, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 char_u *p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001934 int *pcc; /* return: composing chars, last one is 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 int maxlen;
1936{
1937 int len;
1938 int c;
1939 int cc;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001940 int i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
1942 c = utf_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001943 len = utf_ptr2len_len(p, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 /* Only accept a composing char when the first char isn't illegal. */
1945 if ((len > 1 || *p < 0x80)
1946 && len < maxlen
1947 && p[len] >= 0x80
1948 && UTF_COMPOSINGLIKE(p, p + len))
1949 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001950 cc = utf_ptr2char(p + len);
1951 for (;;)
1952 {
1953 pcc[i++] = cc;
1954 if (i == MAX_MCO)
1955 break;
1956 len += utf_ptr2len_len(p + len, maxlen - len);
1957 if (len >= maxlen
1958 || p[len] < 0x80
1959 || !utf_iscomposing(cc = utf_ptr2char(p + len)))
1960 break;
1961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001963
1964 if (i < MAX_MCO) /* last composing char must be 0 */
1965 pcc[i] = 0;
1966
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 return c;
1968}
1969
1970/*
1971 * Convert the character at screen position "off" to a sequence of bytes.
1972 * Includes the composing characters.
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001973 * "buf" must at least have the length MB_MAXBYTES + 1.
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02001974 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 * Returns the produced number of bytes.
1976 */
1977 int
1978utfc_char2bytes(off, buf)
1979 int off;
1980 char_u *buf;
1981{
1982 int len;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001983 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985 len = utf_char2bytes(ScreenLinesUC[off], buf);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001986 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001988 if (ScreenLinesC[i][off] == 0)
1989 break;
1990 len += utf_char2bytes(ScreenLinesC[i][off], buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992 return len;
1993}
1994
1995/*
1996 * Get the length of a UTF-8 byte sequence, not including any following
1997 * composing characters.
1998 * Returns 0 for "".
1999 * Returns 1 for an illegal byte sequence.
2000 */
2001 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002002utf_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 char_u *p;
2004{
2005 int len;
2006 int i;
2007
2008 if (*p == NUL)
2009 return 0;
2010 len = utf8len_tab[*p];
2011 for (i = 1; i < len; ++i)
2012 if ((p[i] & 0xc0) != 0x80)
2013 return 1;
2014 return len;
2015}
2016
2017/*
2018 * Return length of UTF-8 character, obtained from the first byte.
2019 * "b" must be between 0 and 255!
Bram Moolenaar24397332009-12-02 14:02:39 +00002020 * Returns 1 for an invalid first byte value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 */
2022 int
2023utf_byte2len(b)
2024 int b;
2025{
2026 return utf8len_tab[b];
2027}
2028
2029/*
2030 * Get the length of UTF-8 byte sequence "p[size]". Does not include any
2031 * following composing characters.
2032 * Returns 1 for "".
Bram Moolenaarc048f672008-01-04 16:47:25 +00002033 * Returns 1 for an illegal byte sequence (also in incomplete byte seq.).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 * Returns number > "size" for an incomplete byte sequence.
Bram Moolenaar24397332009-12-02 14:02:39 +00002035 * Never returns zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 */
2037 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002038utf_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 char_u *p;
2040 int size;
2041{
2042 int len;
2043 int i;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002044 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
Bram Moolenaar24397332009-12-02 14:02:39 +00002046 len = utf8len_tab[*p];
2047 if (len == 1)
2048 return 1; /* NUL, ascii or illegal lead byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (len > size)
Bram Moolenaarc048f672008-01-04 16:47:25 +00002050 m = size; /* incomplete byte sequence. */
Bram Moolenaar24397332009-12-02 14:02:39 +00002051 else
2052 m = len;
Bram Moolenaarc048f672008-01-04 16:47:25 +00002053 for (i = 1; i < m; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 if ((p[i] & 0xc0) != 0x80)
2055 return 1;
2056 return len;
2057}
2058
2059/*
2060 * Return the number of bytes the UTF-8 encoding of the character at "p" takes.
2061 * This includes following composing characters.
2062 */
2063 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002064utfc_ptr2len(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 char_u *p;
2066{
2067 int len;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002068 int b0 = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#ifdef FEAT_ARABIC
2070 int prevlen;
2071#endif
2072
Bram Moolenaarc669e662005-06-08 22:00:03 +00002073 if (b0 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 return 0;
Bram Moolenaarc669e662005-06-08 22:00:03 +00002075 if (b0 < 0x80 && p[1] < 0x80) /* be quick for ASCII */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 return 1;
2077
2078 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002079 len = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 /* Check for illegal byte. */
Bram Moolenaarc669e662005-06-08 22:00:03 +00002082 if (len == 1 && b0 >= 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 return 1;
2084
2085 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002086 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 * skip all of them (otherwise the cursor would get stuck).
2088 */
2089#ifdef FEAT_ARABIC
2090 prevlen = 0;
2091#endif
2092 for (;;)
2093 {
2094 if (p[len] < 0x80 || !UTF_COMPOSINGLIKE(p + prevlen, p + len))
2095 return len;
2096
2097 /* Skip over composing char */
2098#ifdef FEAT_ARABIC
2099 prevlen = len;
2100#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002101 len += utf_ptr2len(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103}
2104
2105/*
2106 * Return the number of bytes the UTF-8 encoding of the character at "p[size]"
2107 * takes. This includes following composing characters.
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002108 * Returns 0 for an empty string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 * Returns 1 for an illegal char or an incomplete byte sequence.
2110 */
2111 int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002112utfc_ptr2len_len(p, size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 char_u *p;
2114 int size;
2115{
2116 int len;
2117#ifdef FEAT_ARABIC
2118 int prevlen;
2119#endif
2120
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00002121 if (size < 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 return 0;
2123 if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */
2124 return 1;
2125
2126 /* Skip over first UTF-8 char, stopping at a NUL byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002127 len = utf_ptr2len_len(p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /* Check for illegal byte and incomplete byte sequence. */
2130 if ((len == 1 && p[0] >= 0x80) || len > size)
2131 return 1;
2132
2133 /*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002134 * Check for composing characters. We can handle only the first six, but
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * skip all of them (otherwise the cursor would get stuck).
2136 */
2137#ifdef FEAT_ARABIC
2138 prevlen = 0;
2139#endif
2140 while (len < size)
2141 {
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002142 int len_next_char;
2143
2144 if (p[len] < 0x80)
2145 break;
2146
2147 /*
2148 * Next character length should not go beyond size to ensure that
2149 * UTF_COMPOSINGLIKE(...) does not read beyond size.
2150 */
2151 len_next_char = utf_ptr2len_len(p + len, size - len);
2152 if (len_next_char > size - len)
2153 break;
2154
2155 if (!UTF_COMPOSINGLIKE(p + prevlen, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 break;
2157
2158 /* Skip over composing char */
2159#ifdef FEAT_ARABIC
2160 prevlen = len;
2161#endif
Bram Moolenaar89bf0922008-06-29 14:16:06 +00002162 len += len_next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 return len;
2165}
2166
2167/*
2168 * Return the number of bytes the UTF-8 encoding of character "c" takes.
2169 * This does not include composing characters.
2170 */
2171 int
2172utf_char2len(c)
2173 int c;
2174{
2175 if (c < 0x80)
2176 return 1;
2177 if (c < 0x800)
2178 return 2;
2179 if (c < 0x10000)
2180 return 3;
2181 if (c < 0x200000)
2182 return 4;
2183 if (c < 0x4000000)
2184 return 5;
2185 return 6;
2186}
2187
2188/*
2189 * Convert Unicode character "c" to UTF-8 string in "buf[]".
2190 * Returns the number of bytes.
2191 * This does not include composing characters.
2192 */
2193 int
2194utf_char2bytes(c, buf)
2195 int c;
2196 char_u *buf;
2197{
2198 if (c < 0x80) /* 7 bits */
2199 {
2200 buf[0] = c;
2201 return 1;
2202 }
2203 if (c < 0x800) /* 11 bits */
2204 {
2205 buf[0] = 0xc0 + ((unsigned)c >> 6);
2206 buf[1] = 0x80 + (c & 0x3f);
2207 return 2;
2208 }
2209 if (c < 0x10000) /* 16 bits */
2210 {
2211 buf[0] = 0xe0 + ((unsigned)c >> 12);
2212 buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2213 buf[2] = 0x80 + (c & 0x3f);
2214 return 3;
2215 }
2216 if (c < 0x200000) /* 21 bits */
2217 {
2218 buf[0] = 0xf0 + ((unsigned)c >> 18);
2219 buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2220 buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2221 buf[3] = 0x80 + (c & 0x3f);
2222 return 4;
2223 }
2224 if (c < 0x4000000) /* 26 bits */
2225 {
2226 buf[0] = 0xf8 + ((unsigned)c >> 24);
2227 buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2228 buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2229 buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2230 buf[4] = 0x80 + (c & 0x3f);
2231 return 5;
2232 }
2233 /* 31 bits */
2234 buf[0] = 0xfc + ((unsigned)c >> 30);
2235 buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f);
2236 buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f);
2237 buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f);
2238 buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f);
2239 buf[5] = 0x80 + (c & 0x3f);
2240 return 6;
2241}
2242
2243/*
2244 * Return TRUE if "c" is a composing UTF-8 character. This means it will be
2245 * drawn on top of the preceding character.
2246 * Based on code from Markus Kuhn.
2247 */
2248 int
2249utf_iscomposing(c)
2250 int c;
2251{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002252 /* Sorted list of non-overlapping intervals.
2253 * Generated by ../runtime/tools/unicode.vim. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 static struct interval combining[] =
2255 {
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002256 {0x0300, 0x036f},
2257 {0x0483, 0x0489},
2258 {0x0591, 0x05bd},
2259 {0x05bf, 0x05bf},
2260 {0x05c1, 0x05c2},
2261 {0x05c4, 0x05c5},
2262 {0x05c7, 0x05c7},
2263 {0x0610, 0x061a},
2264 {0x064b, 0x065e},
2265 {0x0670, 0x0670},
2266 {0x06d6, 0x06dc},
2267 {0x06de, 0x06e4},
2268 {0x06e7, 0x06e8},
2269 {0x06ea, 0x06ed},
2270 {0x0711, 0x0711},
2271 {0x0730, 0x074a},
2272 {0x07a6, 0x07b0},
2273 {0x07eb, 0x07f3},
2274 {0x0816, 0x0819},
2275 {0x081b, 0x0823},
2276 {0x0825, 0x0827},
2277 {0x0829, 0x082d},
2278 {0x0900, 0x0903},
2279 {0x093c, 0x093c},
2280 {0x093e, 0x094e},
2281 {0x0951, 0x0955},
2282 {0x0962, 0x0963},
2283 {0x0981, 0x0983},
2284 {0x09bc, 0x09bc},
2285 {0x09be, 0x09c4},
2286 {0x09c7, 0x09c8},
2287 {0x09cb, 0x09cd},
2288 {0x09d7, 0x09d7},
2289 {0x09e2, 0x09e3},
2290 {0x0a01, 0x0a03},
2291 {0x0a3c, 0x0a3c},
2292 {0x0a3e, 0x0a42},
2293 {0x0a47, 0x0a48},
2294 {0x0a4b, 0x0a4d},
2295 {0x0a51, 0x0a51},
2296 {0x0a70, 0x0a71},
2297 {0x0a75, 0x0a75},
2298 {0x0a81, 0x0a83},
2299 {0x0abc, 0x0abc},
2300 {0x0abe, 0x0ac5},
2301 {0x0ac7, 0x0ac9},
2302 {0x0acb, 0x0acd},
2303 {0x0ae2, 0x0ae3},
2304 {0x0b01, 0x0b03},
2305 {0x0b3c, 0x0b3c},
2306 {0x0b3e, 0x0b44},
2307 {0x0b47, 0x0b48},
2308 {0x0b4b, 0x0b4d},
2309 {0x0b56, 0x0b57},
2310 {0x0b62, 0x0b63},
2311 {0x0b82, 0x0b82},
2312 {0x0bbe, 0x0bc2},
2313 {0x0bc6, 0x0bc8},
2314 {0x0bca, 0x0bcd},
2315 {0x0bd7, 0x0bd7},
2316 {0x0c01, 0x0c03},
2317 {0x0c3e, 0x0c44},
2318 {0x0c46, 0x0c48},
2319 {0x0c4a, 0x0c4d},
2320 {0x0c55, 0x0c56},
2321 {0x0c62, 0x0c63},
2322 {0x0c82, 0x0c83},
2323 {0x0cbc, 0x0cbc},
2324 {0x0cbe, 0x0cc4},
2325 {0x0cc6, 0x0cc8},
2326 {0x0cca, 0x0ccd},
2327 {0x0cd5, 0x0cd6},
2328 {0x0ce2, 0x0ce3},
2329 {0x0d02, 0x0d03},
2330 {0x0d3e, 0x0d44},
2331 {0x0d46, 0x0d48},
2332 {0x0d4a, 0x0d4d},
2333 {0x0d57, 0x0d57},
2334 {0x0d62, 0x0d63},
2335 {0x0d82, 0x0d83},
2336 {0x0dca, 0x0dca},
2337 {0x0dcf, 0x0dd4},
2338 {0x0dd6, 0x0dd6},
2339 {0x0dd8, 0x0ddf},
2340 {0x0df2, 0x0df3},
2341 {0x0e31, 0x0e31},
2342 {0x0e34, 0x0e3a},
2343 {0x0e47, 0x0e4e},
2344 {0x0eb1, 0x0eb1},
2345 {0x0eb4, 0x0eb9},
2346 {0x0ebb, 0x0ebc},
2347 {0x0ec8, 0x0ecd},
2348 {0x0f18, 0x0f19},
2349 {0x0f35, 0x0f35},
2350 {0x0f37, 0x0f37},
2351 {0x0f39, 0x0f39},
2352 {0x0f3e, 0x0f3f},
2353 {0x0f71, 0x0f84},
2354 {0x0f86, 0x0f87},
2355 {0x0f90, 0x0f97},
2356 {0x0f99, 0x0fbc},
2357 {0x0fc6, 0x0fc6},
2358 {0x102b, 0x103e},
2359 {0x1056, 0x1059},
2360 {0x105e, 0x1060},
2361 {0x1062, 0x1064},
2362 {0x1067, 0x106d},
2363 {0x1071, 0x1074},
2364 {0x1082, 0x108d},
2365 {0x108f, 0x108f},
2366 {0x109a, 0x109d},
2367 {0x135f, 0x135f},
2368 {0x1712, 0x1714},
2369 {0x1732, 0x1734},
2370 {0x1752, 0x1753},
2371 {0x1772, 0x1773},
2372 {0x17b6, 0x17d3},
2373 {0x17dd, 0x17dd},
2374 {0x180b, 0x180d},
2375 {0x18a9, 0x18a9},
2376 {0x1920, 0x192b},
2377 {0x1930, 0x193b},
2378 {0x19b0, 0x19c0},
2379 {0x19c8, 0x19c9},
2380 {0x1a17, 0x1a1b},
2381 {0x1a55, 0x1a5e},
2382 {0x1a60, 0x1a7c},
2383 {0x1a7f, 0x1a7f},
2384 {0x1b00, 0x1b04},
2385 {0x1b34, 0x1b44},
2386 {0x1b6b, 0x1b73},
2387 {0x1b80, 0x1b82},
2388 {0x1ba1, 0x1baa},
2389 {0x1c24, 0x1c37},
2390 {0x1cd0, 0x1cd2},
2391 {0x1cd4, 0x1ce8},
2392 {0x1ced, 0x1ced},
2393 {0x1cf2, 0x1cf2},
2394 {0x1dc0, 0x1de6},
2395 {0x1dfd, 0x1dff},
2396 {0x20d0, 0x20f0},
2397 {0x2cef, 0x2cf1},
2398 {0x2de0, 0x2dff},
2399 {0x302a, 0x302f},
2400 {0x3099, 0x309a},
2401 {0xa66f, 0xa672},
2402 {0xa67c, 0xa67d},
2403 {0xa6f0, 0xa6f1},
2404 {0xa802, 0xa802},
2405 {0xa806, 0xa806},
2406 {0xa80b, 0xa80b},
2407 {0xa823, 0xa827},
2408 {0xa880, 0xa881},
2409 {0xa8b4, 0xa8c4},
2410 {0xa8e0, 0xa8f1},
2411 {0xa926, 0xa92d},
2412 {0xa947, 0xa953},
2413 {0xa980, 0xa983},
2414 {0xa9b3, 0xa9c0},
2415 {0xaa29, 0xaa36},
2416 {0xaa43, 0xaa43},
2417 {0xaa4c, 0xaa4d},
2418 {0xaa7b, 0xaa7b},
2419 {0xaab0, 0xaab0},
2420 {0xaab2, 0xaab4},
2421 {0xaab7, 0xaab8},
2422 {0xaabe, 0xaabf},
2423 {0xaac1, 0xaac1},
2424 {0xabe3, 0xabea},
2425 {0xabec, 0xabed},
2426 {0xfb1e, 0xfb1e},
2427 {0xfe00, 0xfe0f},
2428 {0xfe20, 0xfe26},
2429 {0x101fd, 0x101fd},
2430 {0x10a01, 0x10a03},
2431 {0x10a05, 0x10a06},
2432 {0x10a0c, 0x10a0f},
2433 {0x10a38, 0x10a3a},
2434 {0x10a3f, 0x10a3f},
2435 {0x11080, 0x11082},
2436 {0x110b0, 0x110ba},
2437 {0x1d165, 0x1d169},
2438 {0x1d16d, 0x1d172},
2439 {0x1d17b, 0x1d182},
2440 {0x1d185, 0x1d18b},
2441 {0x1d1aa, 0x1d1ad},
2442 {0x1d242, 0x1d244},
2443 {0xe0100, 0xe01ef}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 };
2445
2446 return intable(combining, sizeof(combining), c);
2447}
2448
2449/*
2450 * Return TRUE for characters that can be displayed in a normal way.
2451 * Only for characters of 0x100 and above!
2452 */
2453 int
2454utf_printable(c)
2455 int c;
2456{
2457#ifdef USE_WCHAR_FUNCTIONS
2458 /*
2459 * Assume the iswprint() library function works better than our own stuff.
2460 */
2461 return iswprint(c);
2462#else
2463 /* Sorted list of non-overlapping intervals.
2464 * 0xd800-0xdfff is reserved for UTF-16, actually illegal. */
2465 static struct interval nonprint[] =
2466 {
2467 {0x070f, 0x070f}, {0x180b, 0x180e}, {0x200b, 0x200f}, {0x202a, 0x202e},
2468 {0x206a, 0x206f}, {0xd800, 0xdfff}, {0xfeff, 0xfeff}, {0xfff9, 0xfffb},
2469 {0xfffe, 0xffff}
2470 };
2471
2472 return !intable(nonprint, sizeof(nonprint), c);
2473#endif
2474}
2475
2476/*
2477 * Get class of a Unicode character.
2478 * 0: white space
2479 * 1: punctuation
2480 * 2 or bigger: some class of word character.
2481 */
2482 int
2483utf_class(c)
2484 int c;
2485{
2486 /* sorted list of non-overlapping intervals */
2487 static struct clinterval
2488 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002489 unsigned int first;
2490 unsigned int last;
2491 unsigned int class;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 } classes[] =
2493 {
2494 {0x037e, 0x037e, 1}, /* Greek question mark */
2495 {0x0387, 0x0387, 1}, /* Greek ano teleia */
2496 {0x055a, 0x055f, 1}, /* Armenian punctuation */
2497 {0x0589, 0x0589, 1}, /* Armenian full stop */
2498 {0x05be, 0x05be, 1},
2499 {0x05c0, 0x05c0, 1},
2500 {0x05c3, 0x05c3, 1},
2501 {0x05f3, 0x05f4, 1},
2502 {0x060c, 0x060c, 1},
2503 {0x061b, 0x061b, 1},
2504 {0x061f, 0x061f, 1},
2505 {0x066a, 0x066d, 1},
2506 {0x06d4, 0x06d4, 1},
2507 {0x0700, 0x070d, 1}, /* Syriac punctuation */
2508 {0x0964, 0x0965, 1},
2509 {0x0970, 0x0970, 1},
2510 {0x0df4, 0x0df4, 1},
2511 {0x0e4f, 0x0e4f, 1},
2512 {0x0e5a, 0x0e5b, 1},
2513 {0x0f04, 0x0f12, 1},
2514 {0x0f3a, 0x0f3d, 1},
2515 {0x0f85, 0x0f85, 1},
2516 {0x104a, 0x104f, 1}, /* Myanmar punctuation */
2517 {0x10fb, 0x10fb, 1}, /* Georgian punctuation */
2518 {0x1361, 0x1368, 1}, /* Ethiopic punctuation */
2519 {0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
2520 {0x1680, 0x1680, 0},
2521 {0x169b, 0x169c, 1},
2522 {0x16eb, 0x16ed, 1},
2523 {0x1735, 0x1736, 1},
2524 {0x17d4, 0x17dc, 1}, /* Khmer punctuation */
2525 {0x1800, 0x180a, 1}, /* Mongolian punctuation */
2526 {0x2000, 0x200b, 0}, /* spaces */
2527 {0x200c, 0x2027, 1}, /* punctuation and symbols */
2528 {0x2028, 0x2029, 0},
2529 {0x202a, 0x202e, 1}, /* punctuation and symbols */
2530 {0x202f, 0x202f, 0},
2531 {0x2030, 0x205e, 1}, /* punctuation and symbols */
2532 {0x205f, 0x205f, 0},
2533 {0x2060, 0x27ff, 1}, /* punctuation and symbols */
2534 {0x2070, 0x207f, 0x2070}, /* superscript */
Bram Moolenaarbbb79722008-06-04 09:00:32 +00002535 {0x2080, 0x2094, 0x2080}, /* subscript */
2536 {0x20a0, 0x27ff, 1}, /* all kinds of symbols */
2537 {0x2800, 0x28ff, 0x2800}, /* braille */
2538 {0x2900, 0x2998, 1}, /* arrows, brackets, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {0x29d8, 0x29db, 1},
2540 {0x29fc, 0x29fd, 1},
Bram Moolenaar103650d2014-09-15 14:25:54 +02002541 {0x2e00, 0x2e7f, 1}, /* supplemental punctuation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {0x3000, 0x3000, 0}, /* ideographic space */
2543 {0x3001, 0x3020, 1}, /* ideographic punctuation */
2544 {0x3030, 0x3030, 1},
2545 {0x303d, 0x303d, 1},
2546 {0x3040, 0x309f, 0x3040}, /* Hiragana */
2547 {0x30a0, 0x30ff, 0x30a0}, /* Katakana */
2548 {0x3300, 0x9fff, 0x4e00}, /* CJK Ideographs */
2549 {0xac00, 0xd7a3, 0xac00}, /* Hangul Syllables */
2550 {0xf900, 0xfaff, 0x4e00}, /* CJK Ideographs */
2551 {0xfd3e, 0xfd3f, 1},
2552 {0xfe30, 0xfe6b, 1}, /* punctuation forms */
2553 {0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
2554 {0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
2555 {0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
2556 {0xff5b, 0xff65, 1}, /* half/fullwidth ASCII */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002557 {0x20000, 0x2a6df, 0x4e00}, /* CJK Ideographs */
2558 {0x2a700, 0x2b73f, 0x4e00}, /* CJK Ideographs */
2559 {0x2b740, 0x2b81f, 0x4e00}, /* CJK Ideographs */
2560 {0x2f800, 0x2fa1f, 0x4e00}, /* CJK Ideographs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 };
2562 int bot = 0;
2563 int top = sizeof(classes) / sizeof(struct clinterval) - 1;
2564 int mid;
2565
2566 /* First quick check for Latin1 characters, use 'iskeyword'. */
2567 if (c < 0x100)
2568 {
Bram Moolenaarf7bbbc52005-06-17 21:55:00 +00002569 if (c == ' ' || c == '\t' || c == NUL || c == 0xa0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 return 0; /* blank */
2571 if (vim_iswordc(c))
2572 return 2; /* word character */
2573 return 1; /* punctuation */
2574 }
2575
2576 /* binary search in table */
2577 while (top >= bot)
2578 {
2579 mid = (bot + top) / 2;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002580 if (classes[mid].last < (unsigned int)c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 bot = mid + 1;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002582 else if (classes[mid].first > (unsigned int)c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 top = mid - 1;
2584 else
2585 return (int)classes[mid].class;
2586 }
2587
2588 /* most other characters are "word" characters */
2589 return 2;
2590}
2591
2592/*
2593 * Code for Unicode case-dependent operations. Based on notes in
2594 * http://www.unicode.org/Public/UNIDATA/CaseFolding.txt
2595 * This code uses simple case folding, not full case folding.
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002596 * Last updated for Unicode 5.2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 */
2598
2599/*
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002600 * The following tables are built by ../runtime/tools/unicode.vim.
2601 * They must be in numeric order, because we use binary search.
2602 * An entry such as {0x41,0x5a,1,32} means that Unicode characters in the
2603 * range from 0x41 to 0x5a inclusive, stepping by 1, are changed to
2604 * folded/upper/lower by adding 32.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606typedef struct
2607{
2608 int rangeStart;
2609 int rangeEnd;
2610 int step;
2611 int offset;
2612} convertStruct;
2613
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002614static convertStruct foldCase[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002616 {0x41,0x5a,1,32},
2617 {0xb5,0xb5,-1,775},
2618 {0xc0,0xd6,1,32},
2619 {0xd8,0xde,1,32},
2620 {0x100,0x12e,2,1},
2621 {0x132,0x136,2,1},
2622 {0x139,0x147,2,1},
2623 {0x14a,0x176,2,1},
2624 {0x178,0x178,-1,-121},
2625 {0x179,0x17d,2,1},
2626 {0x17f,0x17f,-1,-268},
2627 {0x181,0x181,-1,210},
2628 {0x182,0x184,2,1},
2629 {0x186,0x186,-1,206},
2630 {0x187,0x187,-1,1},
2631 {0x189,0x18a,1,205},
2632 {0x18b,0x18b,-1,1},
2633 {0x18e,0x18e,-1,79},
2634 {0x18f,0x18f,-1,202},
2635 {0x190,0x190,-1,203},
2636 {0x191,0x191,-1,1},
2637 {0x193,0x193,-1,205},
2638 {0x194,0x194,-1,207},
2639 {0x196,0x196,-1,211},
2640 {0x197,0x197,-1,209},
2641 {0x198,0x198,-1,1},
2642 {0x19c,0x19c,-1,211},
2643 {0x19d,0x19d,-1,213},
2644 {0x19f,0x19f,-1,214},
2645 {0x1a0,0x1a4,2,1},
2646 {0x1a6,0x1a6,-1,218},
2647 {0x1a7,0x1a7,-1,1},
2648 {0x1a9,0x1a9,-1,218},
2649 {0x1ac,0x1ac,-1,1},
2650 {0x1ae,0x1ae,-1,218},
2651 {0x1af,0x1af,-1,1},
2652 {0x1b1,0x1b2,1,217},
2653 {0x1b3,0x1b5,2,1},
2654 {0x1b7,0x1b7,-1,219},
2655 {0x1b8,0x1bc,4,1},
2656 {0x1c4,0x1c4,-1,2},
2657 {0x1c5,0x1c5,-1,1},
2658 {0x1c7,0x1c7,-1,2},
2659 {0x1c8,0x1c8,-1,1},
2660 {0x1ca,0x1ca,-1,2},
2661 {0x1cb,0x1db,2,1},
2662 {0x1de,0x1ee,2,1},
2663 {0x1f1,0x1f1,-1,2},
2664 {0x1f2,0x1f4,2,1},
2665 {0x1f6,0x1f6,-1,-97},
2666 {0x1f7,0x1f7,-1,-56},
2667 {0x1f8,0x21e,2,1},
2668 {0x220,0x220,-1,-130},
2669 {0x222,0x232,2,1},
2670 {0x23a,0x23a,-1,10795},
2671 {0x23b,0x23b,-1,1},
2672 {0x23d,0x23d,-1,-163},
2673 {0x23e,0x23e,-1,10792},
2674 {0x241,0x241,-1,1},
2675 {0x243,0x243,-1,-195},
2676 {0x244,0x244,-1,69},
2677 {0x245,0x245,-1,71},
2678 {0x246,0x24e,2,1},
2679 {0x345,0x345,-1,116},
2680 {0x370,0x372,2,1},
2681 {0x376,0x376,-1,1},
2682 {0x386,0x386,-1,38},
2683 {0x388,0x38a,1,37},
2684 {0x38c,0x38c,-1,64},
2685 {0x38e,0x38f,1,63},
2686 {0x391,0x3a1,1,32},
2687 {0x3a3,0x3ab,1,32},
2688 {0x3c2,0x3c2,-1,1},
2689 {0x3cf,0x3cf,-1,8},
2690 {0x3d0,0x3d0,-1,-30},
2691 {0x3d1,0x3d1,-1,-25},
2692 {0x3d5,0x3d5,-1,-15},
2693 {0x3d6,0x3d6,-1,-22},
2694 {0x3d8,0x3ee,2,1},
2695 {0x3f0,0x3f0,-1,-54},
2696 {0x3f1,0x3f1,-1,-48},
2697 {0x3f4,0x3f4,-1,-60},
2698 {0x3f5,0x3f5,-1,-64},
2699 {0x3f7,0x3f7,-1,1},
2700 {0x3f9,0x3f9,-1,-7},
2701 {0x3fa,0x3fa,-1,1},
2702 {0x3fd,0x3ff,1,-130},
2703 {0x400,0x40f,1,80},
2704 {0x410,0x42f,1,32},
2705 {0x460,0x480,2,1},
2706 {0x48a,0x4be,2,1},
2707 {0x4c0,0x4c0,-1,15},
2708 {0x4c1,0x4cd,2,1},
2709 {0x4d0,0x524,2,1},
2710 {0x531,0x556,1,48},
2711 {0x10a0,0x10c5,1,7264},
2712 {0x1e00,0x1e94,2,1},
2713 {0x1e9b,0x1e9b,-1,-58},
2714 {0x1e9e,0x1e9e,-1,-7615},
2715 {0x1ea0,0x1efe,2,1},
2716 {0x1f08,0x1f0f,1,-8},
2717 {0x1f18,0x1f1d,1,-8},
2718 {0x1f28,0x1f2f,1,-8},
2719 {0x1f38,0x1f3f,1,-8},
2720 {0x1f48,0x1f4d,1,-8},
2721 {0x1f59,0x1f5f,2,-8},
2722 {0x1f68,0x1f6f,1,-8},
2723 {0x1f88,0x1f8f,1,-8},
2724 {0x1f98,0x1f9f,1,-8},
2725 {0x1fa8,0x1faf,1,-8},
2726 {0x1fb8,0x1fb9,1,-8},
2727 {0x1fba,0x1fbb,1,-74},
2728 {0x1fbc,0x1fbc,-1,-9},
2729 {0x1fbe,0x1fbe,-1,-7173},
2730 {0x1fc8,0x1fcb,1,-86},
2731 {0x1fcc,0x1fcc,-1,-9},
2732 {0x1fd8,0x1fd9,1,-8},
2733 {0x1fda,0x1fdb,1,-100},
2734 {0x1fe8,0x1fe9,1,-8},
2735 {0x1fea,0x1feb,1,-112},
2736 {0x1fec,0x1fec,-1,-7},
2737 {0x1ff8,0x1ff9,1,-128},
2738 {0x1ffa,0x1ffb,1,-126},
2739 {0x1ffc,0x1ffc,-1,-9},
2740 {0x2126,0x2126,-1,-7517},
2741 {0x212a,0x212a,-1,-8383},
2742 {0x212b,0x212b,-1,-8262},
2743 {0x2132,0x2132,-1,28},
2744 {0x2160,0x216f,1,16},
2745 {0x2183,0x2183,-1,1},
2746 {0x24b6,0x24cf,1,26},
2747 {0x2c00,0x2c2e,1,48},
2748 {0x2c60,0x2c60,-1,1},
2749 {0x2c62,0x2c62,-1,-10743},
2750 {0x2c63,0x2c63,-1,-3814},
2751 {0x2c64,0x2c64,-1,-10727},
2752 {0x2c67,0x2c6b,2,1},
2753 {0x2c6d,0x2c6d,-1,-10780},
2754 {0x2c6e,0x2c6e,-1,-10749},
2755 {0x2c6f,0x2c6f,-1,-10783},
2756 {0x2c70,0x2c70,-1,-10782},
2757 {0x2c72,0x2c75,3,1},
2758 {0x2c7e,0x2c7f,1,-10815},
2759 {0x2c80,0x2ce2,2,1},
2760 {0x2ceb,0x2ced,2,1},
2761 {0xa640,0xa65e,2,1},
2762 {0xa662,0xa66c,2,1},
2763 {0xa680,0xa696,2,1},
2764 {0xa722,0xa72e,2,1},
2765 {0xa732,0xa76e,2,1},
2766 {0xa779,0xa77b,2,1},
2767 {0xa77d,0xa77d,-1,-35332},
2768 {0xa77e,0xa786,2,1},
2769 {0xa78b,0xa78b,-1,1},
2770 {0xff21,0xff3a,1,32},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {0x10400,0x10427,1,40}
2772};
2773
Bram Moolenaar35ee4522011-07-15 21:16:59 +02002774static int utf_convert __ARGS((int a, convertStruct table[], int tableSize));
2775static int utf_strnicmp __ARGS((char_u *s1, char_u *s2, size_t n1, size_t n2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
2777/*
2778 * Generic conversion function for case operations.
2779 * Return the converted equivalent of "a", which is a UCS-4 character. Use
2780 * the given conversion "table". Uses binary search on "table".
2781 */
2782 static int
2783utf_convert(a, table, tableSize)
2784 int a;
2785 convertStruct table[];
2786 int tableSize;
2787{
2788 int start, mid, end; /* indices into table */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002789 int entries = tableSize / sizeof(convertStruct);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 start = 0;
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002792 end = entries;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 while (start < end)
2794 {
2795 /* need to search further */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002796 mid = (end + start) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (table[mid].rangeEnd < a)
2798 start = mid + 1;
2799 else
2800 end = mid;
2801 }
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002802 if (start < entries
2803 && table[start].rangeStart <= a
2804 && a <= table[start].rangeEnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 && (a - table[start].rangeStart) % table[start].step == 0)
2806 return (a + table[start].offset);
2807 else
2808 return a;
2809}
2810
2811/*
2812 * Return the folded-case equivalent of "a", which is a UCS-4 character. Uses
2813 * simple case folding.
2814 */
2815 int
2816utf_fold(a)
2817 int a;
2818{
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01002819 return utf_convert(a, foldCase, (int)sizeof(foldCase));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820}
2821
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002822static convertStruct toLower[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002824 {0x41,0x5a,1,32},
2825 {0xc0,0xd6,1,32},
2826 {0xd8,0xde,1,32},
2827 {0x100,0x12e,2,1},
2828 {0x130,0x130,-1,-199},
2829 {0x132,0x136,2,1},
2830 {0x139,0x147,2,1},
2831 {0x14a,0x176,2,1},
2832 {0x178,0x178,-1,-121},
2833 {0x179,0x17d,2,1},
2834 {0x181,0x181,-1,210},
2835 {0x182,0x184,2,1},
2836 {0x186,0x186,-1,206},
2837 {0x187,0x187,-1,1},
2838 {0x189,0x18a,1,205},
2839 {0x18b,0x18b,-1,1},
2840 {0x18e,0x18e,-1,79},
2841 {0x18f,0x18f,-1,202},
2842 {0x190,0x190,-1,203},
2843 {0x191,0x191,-1,1},
2844 {0x193,0x193,-1,205},
2845 {0x194,0x194,-1,207},
2846 {0x196,0x196,-1,211},
2847 {0x197,0x197,-1,209},
2848 {0x198,0x198,-1,1},
2849 {0x19c,0x19c,-1,211},
2850 {0x19d,0x19d,-1,213},
2851 {0x19f,0x19f,-1,214},
2852 {0x1a0,0x1a4,2,1},
2853 {0x1a6,0x1a6,-1,218},
2854 {0x1a7,0x1a7,-1,1},
2855 {0x1a9,0x1a9,-1,218},
2856 {0x1ac,0x1ac,-1,1},
2857 {0x1ae,0x1ae,-1,218},
2858 {0x1af,0x1af,-1,1},
2859 {0x1b1,0x1b2,1,217},
2860 {0x1b3,0x1b5,2,1},
2861 {0x1b7,0x1b7,-1,219},
2862 {0x1b8,0x1bc,4,1},
2863 {0x1c4,0x1c4,-1,2},
2864 {0x1c5,0x1c5,-1,1},
2865 {0x1c7,0x1c7,-1,2},
2866 {0x1c8,0x1c8,-1,1},
2867 {0x1ca,0x1ca,-1,2},
2868 {0x1cb,0x1db,2,1},
2869 {0x1de,0x1ee,2,1},
2870 {0x1f1,0x1f1,-1,2},
2871 {0x1f2,0x1f4,2,1},
2872 {0x1f6,0x1f6,-1,-97},
2873 {0x1f7,0x1f7,-1,-56},
2874 {0x1f8,0x21e,2,1},
2875 {0x220,0x220,-1,-130},
2876 {0x222,0x232,2,1},
2877 {0x23a,0x23a,-1,10795},
2878 {0x23b,0x23b,-1,1},
2879 {0x23d,0x23d,-1,-163},
2880 {0x23e,0x23e,-1,10792},
2881 {0x241,0x241,-1,1},
2882 {0x243,0x243,-1,-195},
2883 {0x244,0x244,-1,69},
2884 {0x245,0x245,-1,71},
2885 {0x246,0x24e,2,1},
2886 {0x370,0x372,2,1},
2887 {0x376,0x376,-1,1},
2888 {0x386,0x386,-1,38},
2889 {0x388,0x38a,1,37},
2890 {0x38c,0x38c,-1,64},
2891 {0x38e,0x38f,1,63},
2892 {0x391,0x3a1,1,32},
2893 {0x3a3,0x3ab,1,32},
2894 {0x3cf,0x3cf,-1,8},
2895 {0x3d8,0x3ee,2,1},
2896 {0x3f4,0x3f4,-1,-60},
2897 {0x3f7,0x3f7,-1,1},
2898 {0x3f9,0x3f9,-1,-7},
2899 {0x3fa,0x3fa,-1,1},
2900 {0x3fd,0x3ff,1,-130},
2901 {0x400,0x40f,1,80},
2902 {0x410,0x42f,1,32},
2903 {0x460,0x480,2,1},
2904 {0x48a,0x4be,2,1},
2905 {0x4c0,0x4c0,-1,15},
2906 {0x4c1,0x4cd,2,1},
2907 {0x4d0,0x524,2,1},
2908 {0x531,0x556,1,48},
2909 {0x10a0,0x10c5,1,7264},
2910 {0x1e00,0x1e94,2,1},
2911 {0x1e9e,0x1e9e,-1,-7615},
2912 {0x1ea0,0x1efe,2,1},
2913 {0x1f08,0x1f0f,1,-8},
2914 {0x1f18,0x1f1d,1,-8},
2915 {0x1f28,0x1f2f,1,-8},
2916 {0x1f38,0x1f3f,1,-8},
2917 {0x1f48,0x1f4d,1,-8},
2918 {0x1f59,0x1f5f,2,-8},
2919 {0x1f68,0x1f6f,1,-8},
2920 {0x1f88,0x1f8f,1,-8},
2921 {0x1f98,0x1f9f,1,-8},
2922 {0x1fa8,0x1faf,1,-8},
2923 {0x1fb8,0x1fb9,1,-8},
2924 {0x1fba,0x1fbb,1,-74},
2925 {0x1fbc,0x1fbc,-1,-9},
2926 {0x1fc8,0x1fcb,1,-86},
2927 {0x1fcc,0x1fcc,-1,-9},
2928 {0x1fd8,0x1fd9,1,-8},
2929 {0x1fda,0x1fdb,1,-100},
2930 {0x1fe8,0x1fe9,1,-8},
2931 {0x1fea,0x1feb,1,-112},
2932 {0x1fec,0x1fec,-1,-7},
2933 {0x1ff8,0x1ff9,1,-128},
2934 {0x1ffa,0x1ffb,1,-126},
2935 {0x1ffc,0x1ffc,-1,-9},
2936 {0x2126,0x2126,-1,-7517},
2937 {0x212a,0x212a,-1,-8383},
2938 {0x212b,0x212b,-1,-8262},
2939 {0x2132,0x2132,-1,28},
2940 {0x2160,0x216f,1,16},
2941 {0x2183,0x2183,-1,1},
2942 {0x24b6,0x24cf,1,26},
2943 {0x2c00,0x2c2e,1,48},
2944 {0x2c60,0x2c60,-1,1},
2945 {0x2c62,0x2c62,-1,-10743},
2946 {0x2c63,0x2c63,-1,-3814},
2947 {0x2c64,0x2c64,-1,-10727},
2948 {0x2c67,0x2c6b,2,1},
2949 {0x2c6d,0x2c6d,-1,-10780},
2950 {0x2c6e,0x2c6e,-1,-10749},
2951 {0x2c6f,0x2c6f,-1,-10783},
2952 {0x2c70,0x2c70,-1,-10782},
2953 {0x2c72,0x2c75,3,1},
2954 {0x2c7e,0x2c7f,1,-10815},
2955 {0x2c80,0x2ce2,2,1},
2956 {0x2ceb,0x2ced,2,1},
2957 {0xa640,0xa65e,2,1},
2958 {0xa662,0xa66c,2,1},
2959 {0xa680,0xa696,2,1},
2960 {0xa722,0xa72e,2,1},
2961 {0xa732,0xa76e,2,1},
2962 {0xa779,0xa77b,2,1},
2963 {0xa77d,0xa77d,-1,-35332},
2964 {0xa77e,0xa786,2,1},
2965 {0xa78b,0xa78b,-1,1},
2966 {0xff21,0xff3a,1,32},
2967 {0x10400,0x10427,1,40}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968};
2969
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002970static convertStruct toUpper[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002972 {0x61,0x7a,1,-32},
2973 {0xb5,0xb5,-1,743},
Bram Moolenaar88178de2012-06-01 17:46:59 +02002974 {0xe0,0xf6,1,-32}, /* 0xdf (German sharp s) is not upper-cased */
Bram Moolenaar3e8cb582010-01-12 19:52:03 +01002975 {0xf8,0xfe,1,-32},
2976 {0xff,0xff,-1,121},
2977 {0x101,0x12f,2,-1},
2978 {0x131,0x131,-1,-232},
2979 {0x133,0x137,2,-1},
2980 {0x13a,0x148,2,-1},
2981 {0x14b,0x177,2,-1},
2982 {0x17a,0x17e,2,-1},
2983 {0x17f,0x17f,-1,-300},
2984 {0x180,0x180,-1,195},
2985 {0x183,0x185,2,-1},
2986 {0x188,0x18c,4,-1},
2987 {0x192,0x192,-1,-1},
2988 {0x195,0x195,-1,97},
2989 {0x199,0x199,-1,-1},
2990 {0x19a,0x19a,-1,163},
2991 {0x19e,0x19e,-1,130},
2992 {0x1a1,0x1a5,2,-1},
2993 {0x1a8,0x1ad,5,-1},
2994 {0x1b0,0x1b4,4,-1},
2995 {0x1b6,0x1b9,3,-1},
2996 {0x1bd,0x1bd,-1,-1},
2997 {0x1bf,0x1bf,-1,56},
2998 {0x1c5,0x1c5,-1,-1},
2999 {0x1c6,0x1c6,-1,-2},
3000 {0x1c8,0x1c8,-1,-1},
3001 {0x1c9,0x1c9,-1,-2},
3002 {0x1cb,0x1cb,-1,-1},
3003 {0x1cc,0x1cc,-1,-2},
3004 {0x1ce,0x1dc,2,-1},
3005 {0x1dd,0x1dd,-1,-79},
3006 {0x1df,0x1ef,2,-1},
3007 {0x1f2,0x1f2,-1,-1},
3008 {0x1f3,0x1f3,-1,-2},
3009 {0x1f5,0x1f9,4,-1},
3010 {0x1fb,0x21f,2,-1},
3011 {0x223,0x233,2,-1},
3012 {0x23c,0x23c,-1,-1},
3013 {0x23f,0x240,1,10815},
3014 {0x242,0x247,5,-1},
3015 {0x249,0x24f,2,-1},
3016 {0x250,0x250,-1,10783},
3017 {0x251,0x251,-1,10780},
3018 {0x252,0x252,-1,10782},
3019 {0x253,0x253,-1,-210},
3020 {0x254,0x254,-1,-206},
3021 {0x256,0x257,1,-205},
3022 {0x259,0x259,-1,-202},
3023 {0x25b,0x25b,-1,-203},
3024 {0x260,0x260,-1,-205},
3025 {0x263,0x263,-1,-207},
3026 {0x268,0x268,-1,-209},
3027 {0x269,0x269,-1,-211},
3028 {0x26b,0x26b,-1,10743},
3029 {0x26f,0x26f,-1,-211},
3030 {0x271,0x271,-1,10749},
3031 {0x272,0x272,-1,-213},
3032 {0x275,0x275,-1,-214},
3033 {0x27d,0x27d,-1,10727},
3034 {0x280,0x283,3,-218},
3035 {0x288,0x288,-1,-218},
3036 {0x289,0x289,-1,-69},
3037 {0x28a,0x28b,1,-217},
3038 {0x28c,0x28c,-1,-71},
3039 {0x292,0x292,-1,-219},
3040 {0x345,0x345,-1,84},
3041 {0x371,0x373,2,-1},
3042 {0x377,0x377,-1,-1},
3043 {0x37b,0x37d,1,130},
3044 {0x3ac,0x3ac,-1,-38},
3045 {0x3ad,0x3af,1,-37},
3046 {0x3b1,0x3c1,1,-32},
3047 {0x3c2,0x3c2,-1,-31},
3048 {0x3c3,0x3cb,1,-32},
3049 {0x3cc,0x3cc,-1,-64},
3050 {0x3cd,0x3ce,1,-63},
3051 {0x3d0,0x3d0,-1,-62},
3052 {0x3d1,0x3d1,-1,-57},
3053 {0x3d5,0x3d5,-1,-47},
3054 {0x3d6,0x3d6,-1,-54},
3055 {0x3d7,0x3d7,-1,-8},
3056 {0x3d9,0x3ef,2,-1},
3057 {0x3f0,0x3f0,-1,-86},
3058 {0x3f1,0x3f1,-1,-80},
3059 {0x3f2,0x3f2,-1,7},
3060 {0x3f5,0x3f5,-1,-96},
3061 {0x3f8,0x3fb,3,-1},
3062 {0x430,0x44f,1,-32},
3063 {0x450,0x45f,1,-80},
3064 {0x461,0x481,2,-1},
3065 {0x48b,0x4bf,2,-1},
3066 {0x4c2,0x4ce,2,-1},
3067 {0x4cf,0x4cf,-1,-15},
3068 {0x4d1,0x525,2,-1},
3069 {0x561,0x586,1,-48},
3070 {0x1d79,0x1d79,-1,35332},
3071 {0x1d7d,0x1d7d,-1,3814},
3072 {0x1e01,0x1e95,2,-1},
3073 {0x1e9b,0x1e9b,-1,-59},
3074 {0x1ea1,0x1eff,2,-1},
3075 {0x1f00,0x1f07,1,8},
3076 {0x1f10,0x1f15,1,8},
3077 {0x1f20,0x1f27,1,8},
3078 {0x1f30,0x1f37,1,8},
3079 {0x1f40,0x1f45,1,8},
3080 {0x1f51,0x1f57,2,8},
3081 {0x1f60,0x1f67,1,8},
3082 {0x1f70,0x1f71,1,74},
3083 {0x1f72,0x1f75,1,86},
3084 {0x1f76,0x1f77,1,100},
3085 {0x1f78,0x1f79,1,128},
3086 {0x1f7a,0x1f7b,1,112},
3087 {0x1f7c,0x1f7d,1,126},
3088 {0x1f80,0x1f87,1,8},
3089 {0x1f90,0x1f97,1,8},
3090 {0x1fa0,0x1fa7,1,8},
3091 {0x1fb0,0x1fb1,1,8},
3092 {0x1fb3,0x1fb3,-1,9},
3093 {0x1fbe,0x1fbe,-1,-7205},
3094 {0x1fc3,0x1fc3,-1,9},
3095 {0x1fd0,0x1fd1,1,8},
3096 {0x1fe0,0x1fe1,1,8},
3097 {0x1fe5,0x1fe5,-1,7},
3098 {0x1ff3,0x1ff3,-1,9},
3099 {0x214e,0x214e,-1,-28},
3100 {0x2170,0x217f,1,-16},
3101 {0x2184,0x2184,-1,-1},
3102 {0x24d0,0x24e9,1,-26},
3103 {0x2c30,0x2c5e,1,-48},
3104 {0x2c61,0x2c61,-1,-1},
3105 {0x2c65,0x2c65,-1,-10795},
3106 {0x2c66,0x2c66,-1,-10792},
3107 {0x2c68,0x2c6c,2,-1},
3108 {0x2c73,0x2c76,3,-1},
3109 {0x2c81,0x2ce3,2,-1},
3110 {0x2cec,0x2cee,2,-1},
3111 {0x2d00,0x2d25,1,-7264},
3112 {0xa641,0xa65f,2,-1},
3113 {0xa663,0xa66d,2,-1},
3114 {0xa681,0xa697,2,-1},
3115 {0xa723,0xa72f,2,-1},
3116 {0xa733,0xa76f,2,-1},
3117 {0xa77a,0xa77c,2,-1},
3118 {0xa77f,0xa787,2,-1},
3119 {0xa78c,0xa78c,-1,-1},
3120 {0xff41,0xff5a,1,-32},
3121 {0x10428,0x1044f,1,-40}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122};
3123
3124/*
3125 * Return the upper-case equivalent of "a", which is a UCS-4 character. Use
3126 * simple case folding.
3127 */
3128 int
3129utf_toupper(a)
3130 int a;
3131{
3132 /* If 'casemap' contains "keepascii" use ASCII style toupper(). */
3133 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3134 return TOUPPER_ASC(a);
3135
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003136#if defined(HAVE_TOWUPPER) && defined(__STDC_ISO_10646__)
3137 /* If towupper() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (!(cmp_flags & CMP_INTERNAL))
3139 return towupper(a);
3140#endif
3141
3142 /* For characters below 128 use locale sensitive toupper(). */
3143 if (a < 128)
3144 return TOUPPER_LOC(a);
3145
3146 /* For any other characters use the above mapping table. */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003147 return utf_convert(a, toUpper, (int)sizeof(toUpper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149
3150 int
3151utf_islower(a)
3152 int a;
3153{
Bram Moolenaar88178de2012-06-01 17:46:59 +02003154 /* German sharp s is lower case but has no upper case equivalent. */
3155 return (utf_toupper(a) != a) || a == 0xdf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158/*
3159 * Return the lower-case equivalent of "a", which is a UCS-4 character. Use
3160 * simple case folding.
3161 */
3162 int
3163utf_tolower(a)
3164 int a;
3165{
3166 /* If 'casemap' contains "keepascii" use ASCII style tolower(). */
3167 if (a < 128 && (cmp_flags & CMP_KEEPASCII))
3168 return TOLOWER_ASC(a);
3169
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003170#if defined(HAVE_TOWLOWER) && defined(__STDC_ISO_10646__)
Bram Moolenaar8fcc0f72005-04-23 20:45:11 +00003171 /* If towlower() is available and handles Unicode, use it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (!(cmp_flags & CMP_INTERNAL))
3173 return towlower(a);
3174#endif
3175
3176 /* For characters below 128 use locale sensitive tolower(). */
3177 if (a < 128)
3178 return TOLOWER_LOC(a);
3179
3180 /* For any other characters use the above mapping table. */
Bram Moolenaarf0b6b0c2011-12-08 15:09:52 +01003181 return utf_convert(a, toLower, (int)sizeof(toLower));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182}
3183
3184 int
3185utf_isupper(a)
3186 int a;
3187{
3188 return (utf_tolower(a) != a);
3189}
3190
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003191 static int
3192utf_strnicmp(s1, s2, n1, n2)
3193 char_u *s1, *s2;
3194 size_t n1, n2;
3195{
3196 int c1, c2, cdiff;
3197 char_u buffer[6];
3198
3199 for (;;)
3200 {
3201 c1 = utf_safe_read_char_adv(&s1, &n1);
3202 c2 = utf_safe_read_char_adv(&s2, &n2);
3203
3204 if (c1 <= 0 || c2 <= 0)
3205 break;
3206
3207 if (c1 == c2)
3208 continue;
3209
3210 cdiff = utf_fold(c1) - utf_fold(c2);
3211 if (cdiff != 0)
3212 return cdiff;
3213 }
3214
3215 /* some string ended or has an incomplete/illegal character sequence */
3216
3217 if (c1 == 0 || c2 == 0)
3218 {
3219 /* some string ended. shorter string is smaller */
3220 if (c1 == 0 && c2 == 0)
3221 return 0;
3222 return c1 == 0 ? -1 : 1;
3223 }
3224
3225 /* Continue with bytewise comparison to produce some result that
3226 * would make comparison operations involving this function transitive.
3227 *
3228 * If only one string had an error, comparison should be made with
3229 * folded version of the other string. In this case it is enough
3230 * to fold just one character to determine the result of comparison. */
3231
3232 if (c1 != -1 && c2 == -1)
3233 {
3234 n1 = utf_char2bytes(utf_fold(c1), buffer);
3235 s1 = buffer;
3236 }
3237 else if (c2 != -1 && c1 == -1)
3238 {
3239 n2 = utf_char2bytes(utf_fold(c2), buffer);
3240 s2 = buffer;
3241 }
3242
3243 while (n1 > 0 && n2 > 0 && *s1 != NUL && *s2 != NUL)
3244 {
3245 cdiff = (int)(*s1) - (int)(*s2);
3246 if (cdiff != 0)
3247 return cdiff;
3248
3249 s1++;
3250 s2++;
3251 n1--;
3252 n2--;
3253 }
3254
3255 if (n1 > 0 && *s1 == NUL)
3256 n1 = 0;
3257 if (n2 > 0 && *s2 == NUL)
3258 n2 = 0;
3259
3260 if (n1 == 0 && n2 == 0)
3261 return 0;
3262 return n1 == 0 ? -1 : 1;
3263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * Version of strnicmp() that handles multi-byte characters.
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01003267 * Needed for Big5, Shift-JIS and UTF-8 encoding. Other DBCS encodings can
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * probably use strnicmp(), because there are no ASCII characters in the
3269 * second byte.
3270 * Returns zero if s1 and s2 are equal (ignoring case), the difference between
3271 * two characters otherwise.
3272 */
3273 int
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003274mb_strnicmp(s1, s2, nn)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 char_u *s1, *s2;
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003276 size_t nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003278 int i, l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 int cdiff;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003280 int n = (int)nn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003282 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003284 return utf_strnicmp(s1, s2, nn, nn);
3285 }
3286 else
3287 {
3288 for (i = 0; i < n; i += l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaar35ee4522011-07-15 21:16:59 +02003290 if (s1[i] == NUL && s2[i] == NUL) /* both strings end */
3291 return 0;
3292
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003293 l = (*mb_ptr2len)(s1 + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 if (l <= 1)
3295 {
3296 /* Single byte: first check normally, then with ignore case. */
3297 if (s1[i] != s2[i])
3298 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003299 cdiff = MB_TOLOWER(s1[i]) - MB_TOLOWER(s2[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (cdiff != 0)
3301 return cdiff;
3302 }
3303 }
3304 else
3305 {
3306 /* For non-Unicode multi-byte don't ignore case. */
3307 if (l > n - i)
3308 l = n - i;
3309 cdiff = STRNCMP(s1 + i, s2 + i, l);
3310 if (cdiff != 0)
3311 return cdiff;
3312 }
3313 }
3314 }
3315 return 0;
3316}
3317
3318/*
3319 * "g8": show bytes of the UTF-8 char under the cursor. Doesn't matter what
3320 * 'encoding' has been set to.
3321 */
3322 void
3323show_utf8()
3324{
3325 int len;
Bram Moolenaar051b7822005-05-19 21:00:46 +00003326 int rlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 char_u *line;
3328 int clen;
3329 int i;
3330
3331 /* Get the byte length of the char under the cursor, including composing
3332 * characters. */
3333 line = ml_get_cursor();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003334 len = utfc_ptr2len(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (len == 0)
3336 {
3337 MSG("NUL");
3338 return;
3339 }
3340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 clen = 0;
3342 for (i = 0; i < len; ++i)
3343 {
3344 if (clen == 0)
3345 {
3346 /* start of (composing) character, get its length */
3347 if (i > 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +00003348 {
3349 STRCPY(IObuff + rlen, "+ ");
3350 rlen += 2;
3351 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003352 clen = utf_ptr2len(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
Bram Moolenaarb382ad12010-05-21 15:46:35 +02003354 sprintf((char *)IObuff + rlen, "%02x ",
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003355 (line[i] == NL) ? NUL : line[i]); /* NUL is stored as NL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 --clen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003357 rlen += (int)STRLEN(IObuff + rlen);
Bram Moolenaar051b7822005-05-19 21:00:46 +00003358 if (rlen > IOSIZE - 20)
3359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361
3362 msg(IObuff);
3363}
3364
3365/*
3366 * mb_head_off() function pointer.
3367 * Return offset from "p" to the first byte of the character it points into.
Bram Moolenaar24397332009-12-02 14:02:39 +00003368 * If "p" points to the NUL at the end of the string return 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 * Returns 0 when already at the first byte of a character.
3370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 int
3372latin_head_off(base, p)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003373 char_u *base UNUSED;
3374 char_u *p UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 return 0;
3377}
3378
3379 int
3380dbcs_head_off(base, p)
3381 char_u *base;
3382 char_u *p;
3383{
3384 char_u *q;
3385
3386 /* It can't be a trailing byte when not using DBCS, at the start of the
3387 * string or the previous byte can't start a double-byte. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003388 if (p <= base || MB_BYTE2LEN(p[-1]) == 1 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return 0;
3390
3391 /* This is slow: need to start at the base and go forward until the
3392 * byte we are looking for. Return 1 when we went past it, 0 otherwise. */
3393 q = base;
3394 while (q < p)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003395 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return (q == p) ? 0 : 1;
3397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * Special version of dbcs_head_off() that works for ScreenLines[], where
3401 * single-width DBCS_JPNU characters are stored separately.
3402 */
3403 int
3404dbcs_screen_head_off(base, p)
3405 char_u *base;
3406 char_u *p;
3407{
3408 char_u *q;
3409
3410 /* It can't be a trailing byte when not using DBCS, at the start of the
3411 * string or the previous byte can't start a double-byte.
3412 * For euc-jp an 0x8e byte in the previous cell always means we have a
3413 * lead byte in the current cell. */
3414 if (p <= base
3415 || (enc_dbcs == DBCS_JPNU && p[-1] == 0x8e)
Bram Moolenaar24397332009-12-02 14:02:39 +00003416 || MB_BYTE2LEN(p[-1]) == 1
3417 || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 return 0;
3419
3420 /* This is slow: need to start at the base and go forward until the
3421 * byte we are looking for. Return 1 when we went past it, 0 otherwise.
3422 * For DBCS_JPNU look out for 0x8e, which means the second byte is not
3423 * stored as the next byte. */
3424 q = base;
3425 while (q < p)
3426 {
3427 if (enc_dbcs == DBCS_JPNU && *q == 0x8e)
3428 ++q;
3429 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003430 q += dbcs_ptr2len(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
3432 return (q == p) ? 0 : 1;
3433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 int
3436utf_head_off(base, p)
3437 char_u *base;
3438 char_u *p;
3439{
3440 char_u *q;
3441 char_u *s;
3442 int c;
Bram Moolenaar24397332009-12-02 14:02:39 +00003443 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#ifdef FEAT_ARABIC
3445 char_u *j;
3446#endif
3447
3448 if (*p < 0x80) /* be quick for ASCII */
3449 return 0;
3450
3451 /* Skip backwards over trailing bytes: 10xx.xxxx
3452 * Skip backwards again if on a composing char. */
3453 for (q = p; ; --q)
3454 {
3455 /* Move s to the last byte of this char. */
3456 for (s = q; (s[1] & 0xc0) == 0x80; ++s)
3457 ;
3458 /* Move q to the first byte of this char. */
3459 while (q > base && (*q & 0xc0) == 0x80)
3460 --q;
3461 /* Check for illegal sequence. Do allow an illegal byte after where we
3462 * started. */
Bram Moolenaar24397332009-12-02 14:02:39 +00003463 len = utf8len_tab[*q];
3464 if (len != (int)(s - q + 1) && len != (int)(p - q + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 return 0;
3466
3467 if (q <= base)
3468 break;
3469
3470 c = utf_ptr2char(q);
3471 if (utf_iscomposing(c))
3472 continue;
3473
3474#ifdef FEAT_ARABIC
3475 if (arabic_maycombine(c))
3476 {
3477 /* Advance to get a sneak-peak at the next char */
3478 j = q;
3479 --j;
3480 /* Move j to the first byte of this char. */
3481 while (j > base && (*j & 0xc0) == 0x80)
3482 --j;
3483 if (arabic_combine(utf_ptr2char(j), c))
3484 continue;
3485 }
3486#endif
3487 break;
3488 }
3489
3490 return (int)(p - q);
3491}
3492
3493/*
Bram Moolenaard8b02732005-01-14 21:48:43 +00003494 * Copy a character from "*fp" to "*tp" and advance the pointers.
3495 */
3496 void
3497mb_copy_char(fp, tp)
3498 char_u **fp;
3499 char_u **tp;
3500{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003501 int l = (*mb_ptr2len)(*fp);
Bram Moolenaard8b02732005-01-14 21:48:43 +00003502
3503 mch_memmove(*tp, *fp, (size_t)l);
3504 *tp += l;
3505 *fp += l;
3506}
3507
3508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 * Return the offset from "p" to the first byte of a character. When "p" is
3510 * at the start of a character 0 is returned, otherwise the offset to the next
3511 * character. Can start anywhere in a stream of bytes.
3512 */
3513 int
3514mb_off_next(base, p)
3515 char_u *base;
3516 char_u *p;
3517{
3518 int i;
3519 int j;
3520
3521 if (enc_utf8)
3522 {
3523 if (*p < 0x80) /* be quick for ASCII */
3524 return 0;
3525
3526 /* Find the next character that isn't 10xx.xxxx */
3527 for (i = 0; (p[i] & 0xc0) == 0x80; ++i)
3528 ;
3529 if (i > 0)
3530 {
3531 /* Check for illegal sequence. */
3532 for (j = 0; p - j > base; ++j)
3533 if ((p[-j] & 0xc0) != 0x80)
3534 break;
3535 if (utf8len_tab[p[-j]] != i + j)
3536 return 0;
3537 }
3538 return i;
3539 }
3540
3541 /* Only need to check if we're on a trail byte, it doesn't matter if we
3542 * want the offset to the next or current character. */
3543 return (*mb_head_off)(base, p);
3544}
3545
3546/*
3547 * Return the offset from "p" to the last byte of the character it points
3548 * into. Can start anywhere in a stream of bytes.
3549 */
3550 int
3551mb_tail_off(base, p)
3552 char_u *base;
3553 char_u *p;
3554{
3555 int i;
3556 int j;
3557
3558 if (*p == NUL)
3559 return 0;
3560
3561 if (enc_utf8)
3562 {
3563 /* Find the last character that is 10xx.xxxx */
3564 for (i = 0; (p[i + 1] & 0xc0) == 0x80; ++i)
3565 ;
3566 /* Check for illegal sequence. */
3567 for (j = 0; p - j > base; ++j)
3568 if ((p[-j] & 0xc0) != 0x80)
3569 break;
3570 if (utf8len_tab[p[-j]] != i + j + 1)
3571 return 0;
3572 return i;
3573 }
3574
3575 /* It can't be the first byte if a double-byte when not using DBCS, at the
3576 * end of the string or the byte can't start a double-byte. */
3577 if (enc_dbcs == 0 || p[1] == NUL || MB_BYTE2LEN(*p) == 1)
3578 return 0;
3579
3580 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3581 return 1 - dbcs_head_off(base, p);
3582}
3583
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003584/*
3585 * Find the next illegal byte sequence.
3586 */
3587 void
3588utf_find_illegal()
3589{
3590 pos_T pos = curwin->w_cursor;
3591 char_u *p;
3592 int len;
3593 vimconv_T vimconv;
3594 char_u *tofree = NULL;
3595
3596 vimconv.vc_type = CONV_NONE;
3597 if (enc_utf8 && (enc_canon_props(curbuf->b_p_fenc) & ENC_8BIT))
3598 {
3599 /* 'encoding' is "utf-8" but we are editing a 8-bit encoded file,
3600 * possibly a utf-8 file with illegal bytes. Setup for conversion
3601 * from utf-8 to 'fileencoding'. */
3602 convert_setup(&vimconv, p_enc, curbuf->b_p_fenc);
3603 }
3604
3605#ifdef FEAT_VIRTUALEDIT
3606 curwin->w_cursor.coladd = 0;
3607#endif
3608 for (;;)
3609 {
3610 p = ml_get_cursor();
3611 if (vimconv.vc_type != CONV_NONE)
3612 {
3613 vim_free(tofree);
3614 tofree = string_convert(&vimconv, p, NULL);
3615 if (tofree == NULL)
3616 break;
3617 p = tofree;
3618 }
3619
3620 while (*p != NUL)
3621 {
3622 /* Illegal means that there are not enough trail bytes (checked by
3623 * utf_ptr2len()) or too many of them (overlong sequence). */
3624 len = utf_ptr2len(p);
3625 if (*p >= 0x80 && (len == 1
3626 || utf_char2len(utf_ptr2char(p)) != len))
3627 {
3628 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003629 curwin->w_cursor.col += (colnr_T)(p - ml_get_cursor());
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003630 else
3631 {
3632 int l;
3633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003634 len = (int)(p - tofree);
Bram Moolenaar68f1a482006-03-17 23:12:21 +00003635 for (p = ml_get_cursor(); *p != NUL && len-- > 0; p += l)
3636 {
3637 l = utf_ptr2len(p);
3638 curwin->w_cursor.col += l;
3639 }
3640 }
3641 goto theend;
3642 }
3643 p += len;
3644 }
3645 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
3646 break;
3647 ++curwin->w_cursor.lnum;
3648 curwin->w_cursor.col = 0;
3649 }
3650
3651 /* didn't find it: don't move and beep */
3652 curwin->w_cursor = pos;
3653 beep_flush();
3654
3655theend:
3656 vim_free(tofree);
3657 convert_setup(&vimconv, NULL, NULL);
3658}
3659
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003660#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003661/*
3662 * Return TRUE if string "s" is a valid utf-8 string.
3663 * When "end" is NULL stop at the first NUL.
3664 * When "end" is positive stop there.
3665 */
3666 int
3667utf_valid_string(s, end)
3668 char_u *s;
3669 char_u *end;
3670{
3671 int l;
3672 char_u *p = s;
3673
3674 while (end == NULL ? *p != NUL : p < end)
3675 {
Bram Moolenaar24397332009-12-02 14:02:39 +00003676 l = utf8len_tab_zero[*p];
3677 if (l == 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003678 return FALSE; /* invalid lead byte */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003679 if (end != NULL && p + l > end)
3680 return FALSE; /* incomplete byte sequence */
3681 ++p;
3682 while (--l > 0)
3683 if ((*p++ & 0xc0) != 0x80)
3684 return FALSE; /* invalid trail byte */
3685 }
3686 return TRUE;
3687}
3688#endif
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#if defined(FEAT_GUI) || defined(PROTO)
3691/*
3692 * Special version of mb_tail_off() for use in ScreenLines[].
3693 */
3694 int
3695dbcs_screen_tail_off(base, p)
3696 char_u *base;
3697 char_u *p;
3698{
3699 /* It can't be the first byte if a double-byte when not using DBCS, at the
3700 * end of the string or the byte can't start a double-byte.
3701 * For euc-jp an 0x8e byte always means we have a lead byte in the current
3702 * cell. */
3703 if (*p == NUL || p[1] == NUL
3704 || (enc_dbcs == DBCS_JPNU && *p == 0x8e)
3705 || MB_BYTE2LEN(*p) == 1)
3706 return 0;
3707
3708 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3709 return 1 - dbcs_screen_head_off(base, p);
3710}
3711#endif
3712
3713/*
3714 * If the cursor moves on an trail byte, set the cursor on the lead byte.
3715 * Thus it moves left if necessary.
3716 * Return TRUE when the cursor was adjusted.
3717 */
3718 void
3719mb_adjust_cursor()
3720{
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003721 mb_adjustpos(curbuf, &curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722}
3723
3724/*
3725 * Adjust position "*lp" to point to the first byte of a multi-byte character.
3726 * If it points to a tail byte it's moved backwards to the head byte.
3727 */
3728 void
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003729mb_adjustpos(buf, lp)
3730 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 pos_T *lp;
3732{
3733 char_u *p;
3734
3735 if (lp->col > 0
3736#ifdef FEAT_VIRTUALEDIT
3737 || lp->coladd > 1
3738#endif
3739 )
3740 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003741 p = ml_get_buf(buf, lp->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 lp->col -= (*mb_head_off)(p, p + lp->col);
3743#ifdef FEAT_VIRTUALEDIT
3744 /* Reset "coladd" when the cursor would be on the right half of a
3745 * double-wide character. */
3746 if (lp->coladd == 1
3747 && p[lp->col] != TAB
3748 && vim_isprintc((*mb_ptr2char)(p + lp->col))
3749 && ptr2cells(p + lp->col) > 1)
3750 lp->coladd = 0;
3751#endif
3752 }
3753}
3754
3755/*
3756 * Return a pointer to the character before "*p", if there is one.
3757 */
3758 char_u *
3759mb_prevptr(line, p)
3760 char_u *line; /* start of the string */
3761 char_u *p;
3762{
3763 if (p > line)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003764 mb_ptr_back(line, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return p;
3766}
3767
3768/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003769 * Return the character length of "str". Each multi-byte character (with
3770 * following composing characters) counts as one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 */
3772 int
3773mb_charlen(str)
3774 char_u *str;
3775{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003776 char_u *p = str;
3777 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003779 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return 0;
3781
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003782 for (count = 0; *p != NUL; count++)
3783 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
3785 return count;
3786}
3787
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00003788#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003789/*
3790 * Like mb_charlen() but for a string with specified length.
3791 */
3792 int
3793mb_charlen_len(str, len)
3794 char_u *str;
3795 int len;
3796{
3797 char_u *p = str;
3798 int count;
3799
3800 for (count = 0; *p != NUL && p < str + len; count++)
3801 p += (*mb_ptr2len)(p);
3802
3803 return count;
3804}
3805#endif
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807/*
3808 * Try to un-escape a multi-byte character.
3809 * Used for the "to" and "from" part of a mapping.
3810 * Return the un-escaped string if it is a multi-byte character, and advance
3811 * "pp" to just after the bytes that formed it.
3812 * Return NULL if no multi-byte char was found.
3813 */
3814 char_u *
3815mb_unescape(pp)
3816 char_u **pp;
3817{
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02003818 static char_u buf[6];
3819 int n;
3820 int m = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 char_u *str = *pp;
3822
3823 /* Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02003824 * KS_EXTRA KE_CSI to CSI.
3825 * Maximum length of a utf-8 character is 4 bytes. */
3826 for (n = 0; str[n] != NUL && m < 4; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
3828 if (str[n] == K_SPECIAL
3829 && str[n + 1] == KS_SPECIAL
3830 && str[n + 2] == KE_FILLER)
3831 {
3832 buf[m++] = K_SPECIAL;
3833 n += 2;
3834 }
Bram Moolenaar6203ff92008-01-06 16:18:56 +00003835 else if ((str[n] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836# ifdef FEAT_GUI
Bram Moolenaar6203ff92008-01-06 16:18:56 +00003837 || str[n] == CSI
3838# endif
3839 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 && str[n + 1] == KS_EXTRA
3841 && str[n + 2] == (int)KE_CSI)
3842 {
3843 buf[m++] = CSI;
3844 n += 2;
3845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 else if (str[n] == K_SPECIAL
3847# ifdef FEAT_GUI
3848 || str[n] == CSI
3849# endif
3850 )
3851 break; /* a special key can't be a multibyte char */
3852 else
3853 buf[m++] = str[n];
3854 buf[m] = NUL;
3855
3856 /* Return a multi-byte character if it's found. An illegal sequence
3857 * will result in a 1 here. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003858 if ((*mb_ptr2len)(buf) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 *pp = str + n + 1;
3861 return buf;
3862 }
Bram Moolenaar4fabd7d2012-09-18 18:03:37 +02003863
3864 /* Bail out quickly for ASCII. */
3865 if (buf[0] < 128)
3866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868 return NULL;
3869}
3870
3871/*
3872 * Return TRUE if the character at "row"/"col" on the screen is the left side
3873 * of a double-width character.
3874 * Caller must make sure "row" and "col" are not invalid!
3875 */
3876 int
3877mb_lefthalve(row, col)
3878 int row;
3879 int col;
3880{
3881#ifdef FEAT_HANGULIN
3882 if (composing_hangul)
3883 return TRUE;
3884#endif
Bram Moolenaar367329b2007-08-30 11:53:22 +00003885 return (*mb_off2cells)(LineOffset[row] + col,
3886 LineOffset[row] + screen_Columns) > 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887}
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889/*
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003890 * Correct a position on the screen, if it's the right half of a double-wide
3891 * char move it to the left half. Returns the corrected column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
3893 int
3894mb_fix_col(col, row)
3895 int col;
3896 int row;
3897{
3898 col = check_col(col);
3899 row = check_row(row);
3900 if (has_mbyte && ScreenLines != NULL && col > 0
3901 && ((enc_dbcs
3902 && ScreenLines[LineOffset[row] + col] != NUL
3903 && dbcs_screen_head_off(ScreenLines + LineOffset[row],
3904 ScreenLines + LineOffset[row] + col))
3905 || (enc_utf8 && ScreenLines[LineOffset[row] + col] == 0)))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003906 return col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 return col;
3908}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909#endif
3910
3911#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
3912static int enc_alias_search __ARGS((char_u *name));
3913
3914/*
3915 * Skip the Vim specific head of a 'encoding' name.
3916 */
3917 char_u *
3918enc_skip(p)
3919 char_u *p;
3920{
3921 if (STRNCMP(p, "2byte-", 6) == 0)
3922 return p + 6;
3923 if (STRNCMP(p, "8bit-", 5) == 0)
3924 return p + 5;
3925 return p;
3926}
3927
3928/*
3929 * Find the canonical name for encoding "enc".
3930 * When the name isn't recognized, returns "enc" itself, but with all lower
3931 * case characters and '_' replaced with '-'.
3932 * Returns an allocated string. NULL for out-of-memory.
3933 */
3934 char_u *
3935enc_canonize(enc)
3936 char_u *enc;
3937{
3938 char_u *r;
3939 char_u *p, *s;
3940 int i;
3941
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003942# ifdef FEAT_MBYTE
3943 if (STRCMP(enc, "default") == 0)
3944 {
3945 /* Use the default encoding as it's found by set_init_1(). */
3946 r = get_encoding_default();
3947 if (r == NULL)
3948 r = (char_u *)"latin1";
3949 return vim_strsave(r);
3950 }
3951# endif
3952
Bram Moolenaar29466f22007-05-10 17:45:37 +00003953 /* copy "enc" to allocated memory, with room for two '-' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 r = alloc((unsigned)(STRLEN(enc) + 3));
3955 if (r != NULL)
3956 {
3957 /* Make it all lower case and replace '_' with '-'. */
3958 p = r;
3959 for (s = enc; *s != NUL; ++s)
3960 {
3961 if (*s == '_')
3962 *p++ = '-';
3963 else
3964 *p++ = TOLOWER_ASC(*s);
3965 }
3966 *p = NUL;
3967
3968 /* Skip "2byte-" and "8bit-". */
3969 p = enc_skip(r);
3970
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003971 /* Change "microsoft-cp" to "cp". Used in some spell files. */
3972 if (STRNCMP(p, "microsoft-cp", 12) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003973 STRMOVE(p, p + 10);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 /* "iso8859" -> "iso-8859" */
3976 if (STRNCMP(p, "iso8859", 7) == 0)
3977 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003978 STRMOVE(p + 4, p + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 p[3] = '-';
3980 }
3981
3982 /* "iso-8859n" -> "iso-8859-n" */
3983 if (STRNCMP(p, "iso-8859", 8) == 0 && p[8] != '-')
3984 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003985 STRMOVE(p + 9, p + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 p[8] = '-';
3987 }
3988
3989 /* "latin-N" -> "latinN" */
3990 if (STRNCMP(p, "latin-", 6) == 0)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003991 STRMOVE(p + 5, p + 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 if (enc_canon_search(p) >= 0)
3994 {
3995 /* canonical name can be used unmodified */
3996 if (p != r)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003997 STRMOVE(r, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 else if ((i = enc_alias_search(p)) >= 0)
4000 {
4001 /* alias recognized, get canonical name */
4002 vim_free(r);
4003 r = vim_strsave((char_u *)enc_canon_table[i].name);
4004 }
4005 }
4006 return r;
4007}
4008
4009/*
4010 * Search for an encoding alias of "name".
4011 * Returns -1 when not found.
4012 */
4013 static int
4014enc_alias_search(name)
4015 char_u *name;
4016{
4017 int i;
4018
4019 for (i = 0; enc_alias_table[i].name != NULL; ++i)
4020 if (STRCMP(name, enc_alias_table[i].name) == 0)
4021 return enc_alias_table[i].canon;
4022 return -1;
4023}
4024#endif
4025
4026#if defined(FEAT_MBYTE) || defined(PROTO)
4027
4028#ifdef HAVE_LANGINFO_H
4029# include <langinfo.h>
4030#endif
4031
4032/*
4033 * Get the canonicalized encoding of the current locale.
4034 * Returns an allocated string when successful, NULL when not.
4035 */
4036 char_u *
4037enc_locale()
4038{
4039#ifndef WIN3264
4040 char *s;
4041 char *p;
4042 int i;
4043#endif
4044 char buf[50];
4045#ifdef WIN3264
4046 long acp = GetACP();
4047
4048 if (acp == 1200)
4049 STRCPY(buf, "ucs-2le");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004050 else if (acp == 1252) /* cp1252 is used as latin1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 STRCPY(buf, "latin1");
4052 else
4053 sprintf(buf, "cp%ld", acp);
4054#else
4055# ifdef HAVE_NL_LANGINFO_CODESET
4056 if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
4057# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004058# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if ((s = getenv("LC_ALL")) == NULL || *s == NUL)
4062 if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL)
4063 s = getenv("LANG");
4064
4065 if (s == NULL || *s == NUL)
4066 return FAIL;
4067
4068 /* The most generic locale format is:
4069 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
4070 * If there is a '.' remove the part before it.
4071 * if there is something after the codeset, remove it.
4072 * Make the name lowercase and replace '_' with '-'.
4073 * Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
4074 * "ko_KR.EUC" == "euc-kr"
4075 */
4076 if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL)
4077 {
4078 if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0
4079 && !isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_')
4080 {
4081 /* copy "XY.EUC" to "euc-XY" to buf[10] */
4082 STRCPY(buf + 10, "euc-");
4083 buf[14] = p[-2];
4084 buf[15] = p[-1];
4085 buf[16] = 0;
4086 s = buf + 10;
4087 }
4088 else
4089 s = p + 1;
4090 }
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004091 for (i = 0; s[i] != NUL && i < (int)sizeof(buf) - 1; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 if (s[i] == '_' || s[i] == '-')
4094 buf[i] = '-';
4095 else if (isalnum((int)s[i]))
4096 buf[i] = TOLOWER_ASC(s[i]);
4097 else
4098 break;
4099 }
4100 buf[i] = NUL;
4101#endif
4102
4103 return enc_canonize((char_u *)buf);
4104}
4105
Bram Moolenaar693e40c2013-02-26 14:56:42 +01004106#if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107/*
4108 * Convert an encoding name to an MS-Windows codepage.
4109 * Returns zero if no codepage can be figured out.
4110 */
4111 int
4112encname2codepage(name)
4113 char_u *name;
4114{
4115 int cp;
4116 char_u *p = name;
4117 int idx;
4118
4119 if (STRNCMP(p, "8bit-", 5) == 0)
4120 p += 5;
4121 else if (STRNCMP(p_enc, "2byte-", 6) == 0)
4122 p += 6;
4123
4124 if (p[0] == 'c' && p[1] == 'p')
Bram Moolenaar2c6f3dc2013-07-05 20:09:16 +02004125 cp = atoi((char *)p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else if ((idx = enc_canon_search(p)) >= 0)
4127 cp = enc_canon_table[idx].codepage;
4128 else
4129 return 0;
4130 if (IsValidCodePage(cp))
4131 return cp;
4132 return 0;
4133}
4134#endif
4135
4136# if defined(USE_ICONV) || defined(PROTO)
4137
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004138static char_u *iconv_string __ARGS((vimconv_T *vcp, char_u *str, int slen, int *unconvlenp, int *resultlenp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140/*
4141 * Call iconv_open() with a check if iconv() works properly (there are broken
4142 * versions).
4143 * Returns (void *)-1 if failed.
4144 * (should return iconv_t, but that causes problems with prototypes).
4145 */
4146 void *
4147my_iconv_open(to, from)
4148 char_u *to;
4149 char_u *from;
4150{
4151 iconv_t fd;
4152#define ICONV_TESTLEN 400
4153 char_u tobuf[ICONV_TESTLEN];
4154 char *p;
4155 size_t tolen;
4156 static int iconv_ok = -1;
4157
4158 if (iconv_ok == FALSE)
4159 return (void *)-1; /* detected a broken iconv() previously */
4160
4161#ifdef DYNAMIC_ICONV
4162 /* Check if the iconv.dll can be found. */
4163 if (!iconv_enabled(TRUE))
4164 return (void *)-1;
4165#endif
4166
4167 fd = iconv_open((char *)enc_skip(to), (char *)enc_skip(from));
4168
4169 if (fd != (iconv_t)-1 && iconv_ok == -1)
4170 {
4171 /*
4172 * Do a dummy iconv() call to check if it actually works. There is a
4173 * version of iconv() on Linux that is broken. We can't ignore it,
4174 * because it's wide-spread. The symptoms are that after outputting
4175 * the initial shift state the "to" pointer is NULL and conversion
4176 * stops for no apparent reason after about 8160 characters.
4177 */
4178 p = (char *)tobuf;
4179 tolen = ICONV_TESTLEN;
4180 (void)iconv(fd, NULL, NULL, &p, &tolen);
4181 if (p == NULL)
4182 {
4183 iconv_ok = FALSE;
4184 iconv_close(fd);
4185 fd = (iconv_t)-1;
4186 }
4187 else
4188 iconv_ok = TRUE;
4189 }
4190
4191 return (void *)fd;
4192}
4193
4194/*
4195 * Convert the string "str[slen]" with iconv().
4196 * If "unconvlenp" is not NULL handle the string ending in an incomplete
4197 * sequence and set "*unconvlenp" to the length of it.
4198 * Returns the converted string in allocated memory. NULL for an error.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004199 * If resultlenp is not NULL, sets it to the result length in bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
4201 static char_u *
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004202iconv_string(vcp, str, slen, unconvlenp, resultlenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 vimconv_T *vcp;
4204 char_u *str;
4205 int slen;
4206 int *unconvlenp;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004207 int *resultlenp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 const char *from;
4210 size_t fromlen;
4211 char *to;
4212 size_t tolen;
4213 size_t len = 0;
4214 size_t done = 0;
4215 char_u *result = NULL;
4216 char_u *p;
4217 int l;
4218
4219 from = (char *)str;
4220 fromlen = slen;
4221 for (;;)
4222 {
4223 if (len == 0 || ICONV_ERRNO == ICONV_E2BIG)
4224 {
4225 /* Allocate enough room for most conversions. When re-allocating
4226 * increase the buffer size. */
4227 len = len + fromlen * 2 + 40;
4228 p = alloc((unsigned)len);
4229 if (p != NULL && done > 0)
4230 mch_memmove(p, result, done);
4231 vim_free(result);
4232 result = p;
4233 if (result == NULL) /* out of memory */
4234 break;
4235 }
4236
4237 to = (char *)result + done;
4238 tolen = len - done - 2;
4239 /* Avoid a warning for systems with a wrong iconv() prototype by
4240 * casting the second argument to void *. */
4241 if (iconv(vcp->vc_fd, (void *)&from, &fromlen, &to, &tolen)
4242 != (size_t)-1)
4243 {
4244 /* Finished, append a NUL. */
4245 *to = NUL;
4246 break;
4247 }
4248
4249 /* Check both ICONV_EINVAL and EINVAL, because the dynamically loaded
4250 * iconv library may use one of them. */
4251 if (!vcp->vc_fail && unconvlenp != NULL
4252 && (ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4253 {
4254 /* Handle an incomplete sequence at the end. */
4255 *to = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004256 *unconvlenp = (int)fromlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 break;
4258 }
4259
4260 /* Check both ICONV_EILSEQ and EILSEQ, because the dynamically loaded
4261 * iconv library may use one of them. */
4262 else if (!vcp->vc_fail
4263 && (ICONV_ERRNO == ICONV_EILSEQ || ICONV_ERRNO == EILSEQ
4264 || ICONV_ERRNO == ICONV_EINVAL || ICONV_ERRNO == EINVAL))
4265 {
4266 /* Can't convert: insert a '?' and skip a character. This assumes
4267 * conversion from 'encoding' to something else. In other
4268 * situations we don't know what to skip anyway. */
4269 *to++ = '?';
4270 if ((*mb_ptr2cells)((char_u *)from) > 1)
4271 *to++ = '?';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004272 if (enc_utf8)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004273 l = utfc_ptr2len_len((char_u *)from, (int)fromlen);
Bram Moolenaar217ad922005-03-20 22:37:15 +00004274 else
4275 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004276 l = (*mb_ptr2len)((char_u *)from);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004277 if (l > (int)fromlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004278 l = (int)fromlen;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 from += l;
4281 fromlen -= l;
4282 }
4283 else if (ICONV_ERRNO != ICONV_E2BIG)
4284 {
4285 /* conversion failed */
4286 vim_free(result);
4287 result = NULL;
4288 break;
4289 }
4290 /* Not enough room or skipping illegal sequence. */
4291 done = to - (char *)result;
4292 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004293
Bram Moolenaar0d35e912011-04-11 14:29:17 +02004294 if (resultlenp != NULL && result != NULL)
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004295 *resultlenp = (int)(to - (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return result;
4297}
4298
4299# if defined(DYNAMIC_ICONV) || defined(PROTO)
4300/*
4301 * Dynamically load the "iconv.dll" on Win32.
4302 */
4303
4304#ifndef DYNAMIC_ICONV /* just generating prototypes */
4305# define HINSTANCE int
4306#endif
Bram Moolenaard6f676d2005-06-01 21:51:55 +00004307static HINSTANCE hIconvDLL = 0;
4308static HINSTANCE hMsvcrtDLL = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310# ifndef DYNAMIC_ICONV_DLL
4311# define DYNAMIC_ICONV_DLL "iconv.dll"
4312# define DYNAMIC_ICONV_DLL_ALT "libiconv.dll"
4313# endif
4314# ifndef DYNAMIC_MSVCRT_DLL
4315# define DYNAMIC_MSVCRT_DLL "msvcrt.dll"
4316# endif
4317
4318/*
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004319 * Get the address of 'funcname' which is imported by 'hInst' DLL.
4320 */
4321 static void *
4322get_iconv_import_func(HINSTANCE hInst, const char *funcname)
4323{
4324 PBYTE pImage = (PBYTE)hInst;
4325 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
4326 PIMAGE_NT_HEADERS pPE;
4327 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
4328 PIMAGE_THUNK_DATA pIAT; /* Import Address Table */
4329 PIMAGE_THUNK_DATA pINT; /* Import Name Table */
4330 PIMAGE_IMPORT_BY_NAME pImpName;
4331
4332 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
4333 return NULL;
4334 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
4335 if (pPE->Signature != IMAGE_NT_SIGNATURE)
4336 return NULL;
4337 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
4338 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
4339 .VirtualAddress);
4340 for (; pImpDesc->FirstThunk; ++pImpDesc)
4341 {
Bram Moolenaarb5f7bf62013-01-19 14:02:02 +01004342 if (!pImpDesc->OriginalFirstThunk)
4343 continue;
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004344 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
4345 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
4346 for (; pIAT->u1.Function; ++pIAT, ++pINT)
4347 {
4348 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
4349 continue;
Bram Moolenaar94a8adf2013-01-23 16:19:23 +01004350 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
4351 + (UINT_PTR)(pINT->u1.AddressOfData));
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004352 if (strcmp(pImpName->Name, funcname) == 0)
4353 return (void *)pIAT->u1.Function;
4354 }
4355 }
4356 return NULL;
4357}
4358
4359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 * Try opening the iconv.dll and return TRUE if iconv() can be used.
4361 */
4362 int
4363iconv_enabled(verbose)
4364 int verbose;
4365{
4366 if (hIconvDLL != 0 && hMsvcrtDLL != 0)
4367 return TRUE;
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004368 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (hIconvDLL == 0) /* sometimes it's called libiconv.dll */
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004370 hIconvDLL = vimLoadLib(DYNAMIC_ICONV_DLL_ALT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (hIconvDLL != 0)
Bram Moolenaarebbcb822010-10-23 14:02:54 +02004372 hMsvcrtDLL = vimLoadLib(DYNAMIC_MSVCRT_DLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (hIconvDLL == 0 || hMsvcrtDLL == 0)
4374 {
4375 /* Only give the message when 'verbose' is set, otherwise it might be
4376 * done whenever a conversion is attempted. */
4377 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004378 {
4379 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 EMSG2(_(e_loadlib),
4381 hIconvDLL == 0 ? DYNAMIC_ICONV_DLL : DYNAMIC_MSVCRT_DLL);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004382 verbose_leave();
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 iconv_end();
4385 return FALSE;
4386 }
4387
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004388 iconv = (void *)GetProcAddress(hIconvDLL, "libiconv");
4389 iconv_open = (void *)GetProcAddress(hIconvDLL, "libiconv_open");
4390 iconv_close = (void *)GetProcAddress(hIconvDLL, "libiconv_close");
4391 iconvctl = (void *)GetProcAddress(hIconvDLL, "libiconvctl");
Bram Moolenaar8fae8e62013-01-17 14:39:47 +01004392 iconv_errno = get_iconv_import_func(hIconvDLL, "_errno");
4393 if (iconv_errno == NULL)
4394 iconv_errno = (void *)GetProcAddress(hMsvcrtDLL, "_errno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (iconv == NULL || iconv_open == NULL || iconv_close == NULL
4396 || iconvctl == NULL || iconv_errno == NULL)
4397 {
4398 iconv_end();
4399 if (verbose && p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004400 {
4401 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 EMSG2(_(e_loadfunc), "for libiconv");
Bram Moolenaara04f10b2005-05-31 22:09:46 +00004403 verbose_leave();
4404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FALSE;
4406 }
4407 return TRUE;
4408}
4409
4410 void
4411iconv_end()
4412{
4413 /* Don't use iconv() when inputting or outputting characters. */
4414 if (input_conv.vc_type == CONV_ICONV)
4415 convert_setup(&input_conv, NULL, NULL);
4416 if (output_conv.vc_type == CONV_ICONV)
4417 convert_setup(&output_conv, NULL, NULL);
4418
4419 if (hIconvDLL != 0)
4420 FreeLibrary(hIconvDLL);
4421 if (hMsvcrtDLL != 0)
4422 FreeLibrary(hMsvcrtDLL);
4423 hIconvDLL = 0;
4424 hMsvcrtDLL = 0;
4425}
4426# endif /* DYNAMIC_ICONV */
4427# endif /* USE_ICONV */
4428
4429#endif /* FEAT_MBYTE */
4430
4431#if defined(FEAT_XIM) || defined(PROTO)
4432
Bram Moolenaar64404472010-06-26 06:24:45 +02004433# if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434static int xim_has_preediting INIT(= FALSE); /* IM current status */
4435
4436/*
4437 * Set preedit_start_col to the current cursor position.
4438 */
4439 static void
4440init_preedit_start_col(void)
4441{
4442 if (State & CMDLINE)
4443 preedit_start_col = cmdline_getvcol_cursor();
4444 else if (curwin != NULL)
4445 getvcol(curwin, &curwin->w_cursor, &preedit_start_col, NULL, NULL);
4446 /* Prevent that preediting marks the buffer as changed. */
4447 xim_changed_while_preediting = curbuf->b_changed;
4448}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450static int im_is_active = FALSE; /* IM is enabled for current mode */
Bram Moolenaarc236c162008-07-13 17:41:49 +00004451static int preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452static int im_preedit_cursor = 0; /* cursor offset in characters */
4453static int im_preedit_trailing = 0; /* number of characters after cursor */
4454
4455static unsigned long im_commit_handler_id = 0;
4456static unsigned int im_activatekey_keyval = GDK_VoidSymbol;
4457static unsigned int im_activatekey_state = 0;
4458
4459 void
4460im_set_active(int active)
4461{
4462 int was_active;
4463
Bram Moolenaarabab85a2013-06-26 19:18:05 +02004464 was_active = !!im_get_status();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 im_is_active = (active && !p_imdisable);
4466
4467 if (im_is_active != was_active)
4468 xim_reset();
4469}
4470
4471 void
4472xim_set_focus(int focus)
4473{
4474 if (xic != NULL)
4475 {
4476 if (focus)
4477 gtk_im_context_focus_in(xic);
4478 else
4479 gtk_im_context_focus_out(xic);
4480 }
4481}
4482
4483 void
4484im_set_position(int row, int col)
4485{
4486 if (xic != NULL)
4487 {
4488 GdkRectangle area;
4489
4490 area.x = FILL_X(col);
4491 area.y = FILL_Y(row);
4492 area.width = gui.char_width * (mb_lefthalve(row, col) ? 2 : 1);
4493 area.height = gui.char_height;
4494
4495 gtk_im_context_set_cursor_location(xic, &area);
4496 }
4497}
4498
4499# if 0 || defined(PROTO) /* apparently only used in gui_x11.c */
4500 void
4501xim_set_preedit(void)
4502{
4503 im_set_position(gui.row, gui.col);
4504}
4505# endif
4506
4507 static void
4508im_add_to_input(char_u *str, int len)
4509{
4510 /* Convert from 'termencoding' (always "utf-8") to 'encoding' */
4511 if (input_conv.vc_type != CONV_NONE)
4512 {
4513 str = string_convert(&input_conv, str, &len);
4514 g_return_if_fail(str != NULL);
4515 }
4516
4517 add_to_input_buf_csi(str, len);
4518
4519 if (input_conv.vc_type != CONV_NONE)
4520 vim_free(str);
4521
4522 if (p_mh) /* blank out the pointer if necessary */
4523 gui_mch_mousehide(TRUE);
4524}
4525
4526 static void
4527im_delete_preedit(void)
4528{
4529 char_u bskey[] = {CSI, 'k', 'b'};
4530 char_u delkey[] = {CSI, 'k', 'D'};
4531
4532 if (State & NORMAL)
4533 {
4534 im_preedit_cursor = 0;
4535 return;
4536 }
4537 for (; im_preedit_cursor > 0; --im_preedit_cursor)
4538 add_to_input_buf(bskey, (int)sizeof(bskey));
4539
4540 for (; im_preedit_trailing > 0; --im_preedit_trailing)
4541 add_to_input_buf(delkey, (int)sizeof(delkey));
4542}
4543
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004544/*
4545 * Move the cursor left by "num_move_back" characters.
4546 * Note that ins_left() checks im_is_preediting() to avoid breaking undo for
4547 * these K_LEFT keys.
4548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 static void
4550im_correct_cursor(int num_move_back)
4551{
4552 char_u backkey[] = {CSI, 'k', 'l'};
4553
4554 if (State & NORMAL)
4555 return;
4556# ifdef FEAT_RIGHTLEFT
4557 if ((State & CMDLINE) == 0 && curwin != NULL && curwin->w_p_rl)
4558 backkey[2] = 'r';
4559# endif
4560 for (; num_move_back > 0; --num_move_back)
4561 add_to_input_buf(backkey, (int)sizeof(backkey));
4562}
4563
4564static int xim_expected_char = NUL;
4565static int xim_ignored_char = FALSE;
4566
4567/*
4568 * Update the mode and cursor while in an IM callback.
4569 */
4570 static void
4571im_show_info(void)
4572{
4573 int old_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 old_vgetc_busy = vgetc_busy;
4576 vgetc_busy = TRUE;
4577 showmode();
4578 vgetc_busy = old_vgetc_busy;
Bram Moolenaar917ba892012-03-07 19:38:55 +01004579 if ((State & NORMAL) || (State & INSERT))
4580 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 out_flush();
4582}
4583
4584/*
4585 * Callback invoked when the user finished preediting.
4586 * Put the final string into the input buffer.
4587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004589im_commit_cb(GtkIMContext *context UNUSED,
4590 const gchar *str,
4591 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
Bram Moolenaar72597a52010-07-18 15:31:08 +02004593 int slen = (int)STRLEN(str);
4594 int add_to_input = TRUE;
4595 int clen;
4596 int len = slen;
4597 int commit_with_preedit = TRUE;
4598 char_u *im_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600#ifdef XIM_DEBUG
4601 xim_log("im_commit_cb(): %s\n", str);
4602#endif
4603
4604 /* The imhangul module doesn't reset the preedit string before
4605 * committing. Call im_delete_preedit() to work around that. */
4606 im_delete_preedit();
4607
4608 /* Indicate that preediting has finished. */
4609 if (preedit_start_col == MAXCOL)
4610 {
4611 init_preedit_start_col();
4612 commit_with_preedit = FALSE;
4613 }
4614
4615 /* The thing which setting "preedit_start_col" to MAXCOL means that
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004616 * "preedit_start_col" will be set forcedly when calling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 * preedit_changed_cb() next time.
4618 * "preedit_start_col" should not reset with MAXCOL on this part. Vim
4619 * is simulating the preediting by using add_to_input_str(). when
4620 * preedit begin immediately before committed, the typebuf is not
4621 * flushed to screen, then it can't get correct "preedit_start_col".
4622 * Thus, it should calculate the cells by adding cells of the committed
4623 * string. */
4624 if (input_conv.vc_type != CONV_NONE)
4625 {
4626 im_str = string_convert(&input_conv, (char_u *)str, &len);
4627 g_return_if_fail(im_str != NULL);
4628 }
4629 else
4630 im_str = (char_u *)str;
Bram Moolenaar72597a52010-07-18 15:31:08 +02004631
4632 clen = mb_string2cells(im_str, len);
4633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (input_conv.vc_type != CONV_NONE)
4635 vim_free(im_str);
4636 preedit_start_col += clen;
4637
4638 /* Is this a single character that matches a keypad key that's just
4639 * been pressed? If so, we don't want it to be entered as such - let
4640 * us carry on processing the raw keycode so that it may be used in
4641 * mappings as <kSomething>. */
4642 if (xim_expected_char != NUL)
4643 {
4644 /* We're currently processing a keypad or other special key */
4645 if (slen == 1 && str[0] == xim_expected_char)
4646 {
4647 /* It's a match - don't do it here */
4648 xim_ignored_char = TRUE;
4649 add_to_input = FALSE;
4650 }
4651 else
4652 {
4653 /* Not a match */
4654 xim_ignored_char = FALSE;
4655 }
4656 }
4657
4658 if (add_to_input)
4659 im_add_to_input((char_u *)str, slen);
4660
4661 /* Inserting chars while "im_is_active" is set does not cause a change of
4662 * buffer. When the chars are committed the buffer must be marked as
4663 * changed. */
4664 if (!commit_with_preedit)
4665 preedit_start_col = MAXCOL;
4666
4667 /* This flag is used in changed() at next call. */
4668 xim_changed_while_preediting = TRUE;
4669
4670 if (gtk_main_level() > 0)
4671 gtk_main_quit();
4672}
4673
4674/*
4675 * Callback invoked after start to the preedit.
4676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004678im_preedit_start_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680#ifdef XIM_DEBUG
4681 xim_log("im_preedit_start_cb()\n");
4682#endif
4683
4684 im_is_active = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00004685 preedit_is_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarc236c162008-07-13 17:41:49 +00004687 im_show_info();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688}
4689
4690/*
4691 * Callback invoked after end to the preedit.
4692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004694im_preedit_end_cb(GtkIMContext *context UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696#ifdef XIM_DEBUG
4697 xim_log("im_preedit_end_cb()\n");
4698#endif
4699 im_delete_preedit();
4700
4701 /* Indicate that preediting has finished */
4702 preedit_start_col = MAXCOL;
4703 xim_has_preediting = FALSE;
4704
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004705#if 0
4706 /* Removal of this line suggested by Takuhiro Nishioka. Fixes that IM was
Bram Moolenaarc236c162008-07-13 17:41:49 +00004707 * switched off unintentionally. We now use preedit_is_active (added by
4708 * SungHyun Nam). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 im_is_active = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004710#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00004711 preedit_is_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 gui_update_cursor(TRUE, FALSE);
4713 im_show_info();
4714}
4715
4716/*
4717 * Callback invoked after changes to the preedit string. If the preedit
4718 * string was empty before, remember the preedit start column so we know
4719 * where to apply feedback attributes. Delete the previous preedit string
4720 * if there was one, save the new preedit cursor offset, and put the new
4721 * string into the input buffer.
4722 *
4723 * TODO: The pragmatic "put into input buffer" approach used here has
4724 * several fundamental problems:
4725 *
4726 * - The characters in the preedit string are subject to remapping.
4727 * That's broken, only the finally committed string should be remapped.
4728 *
4729 * - There is a race condition involved: The retrieved value for the
4730 * current cursor position will be wrong if any unprocessed characters
4731 * are still queued in the input buffer.
4732 *
4733 * - Due to the lack of synchronization between the file buffer in memory
4734 * and any typed characters, it's practically impossible to implement the
4735 * "retrieve_surrounding" and "delete_surrounding" signals reliably. IM
4736 * modules for languages such as Thai are likely to rely on this feature
4737 * for proper operation.
4738 *
4739 * Conclusions: I think support for preediting needs to be moved to the
4740 * core parts of Vim. Ideally, until it has been committed, the preediting
4741 * string should only be displayed and not affect the buffer content at all.
4742 * The question how to deal with the synchronization issue still remains.
4743 * Circumventing the input buffer is probably not desirable. Anyway, I think
4744 * implementing "retrieve_surrounding" is the only hard problem.
4745 *
4746 * One way to solve all of this in a clean manner would be to queue all key
4747 * press/release events "as is" in the input buffer, and apply the IM filtering
4748 * at the receiving end of the queue. This, however, would have a rather large
4749 * impact on the code base. If there is an easy way to force processing of all
4750 * remaining input from within the "retrieve_surrounding" signal handler, this
4751 * might not be necessary. Gotta ask on vim-dev for opinions.
4752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 static void
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004754im_preedit_changed_cb(GtkIMContext *context, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 char *preedit_string = NULL;
4757 int cursor_index = 0;
4758 int num_move_back = 0;
4759 char_u *str;
4760 char_u *p;
4761 int i;
4762
4763 gtk_im_context_get_preedit_string(context,
4764 &preedit_string, NULL,
4765 &cursor_index);
4766
4767#ifdef XIM_DEBUG
4768 xim_log("im_preedit_changed_cb(): %s\n", preedit_string);
4769#endif
4770
4771 g_return_if_fail(preedit_string != NULL); /* just in case */
4772
4773 /* If preedit_start_col is MAXCOL set it to the current cursor position. */
4774 if (preedit_start_col == MAXCOL && preedit_string[0] != '\0')
4775 {
4776 xim_has_preediting = TRUE;
4777
4778 /* Urgh, this breaks if the input buffer isn't empty now */
4779 init_preedit_start_col();
4780 }
4781 else if (cursor_index == 0 && preedit_string[0] == '\0')
4782 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004783 xim_has_preediting = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785 /* If at the start position (after typing backspace)
4786 * preedit_start_col must be reset. */
4787 preedit_start_col = MAXCOL;
4788 }
4789
4790 im_delete_preedit();
4791
4792 /*
4793 * Compute the end of the preediting area: "preedit_end_col".
4794 * According to the documentation of gtk_im_context_get_preedit_string(),
4795 * the cursor_pos output argument returns the offset in bytes. This is
4796 * unfortunately not true -- real life shows the offset is in characters,
4797 * and the GTK+ source code agrees with me. Will file a bug later.
4798 */
4799 if (preedit_start_col != MAXCOL)
4800 preedit_end_col = preedit_start_col;
4801 str = (char_u *)preedit_string;
4802 for (p = str, i = 0; *p != NUL; p += utf_byte2len(*p), ++i)
4803 {
4804 int is_composing;
4805
4806 is_composing = ((*p & 0x80) != 0 && utf_iscomposing(utf_ptr2char(p)));
4807 /*
4808 * These offsets are used as counters when generating <BS> and <Del>
4809 * to delete the preedit string. So don't count composing characters
4810 * unless 'delcombine' is enabled.
4811 */
4812 if (!is_composing || p_deco)
4813 {
4814 if (i < cursor_index)
4815 ++im_preedit_cursor;
4816 else
4817 ++im_preedit_trailing;
4818 }
4819 if (!is_composing && i >= cursor_index)
4820 {
4821 /* This is essentially the same as im_preedit_trailing, except
4822 * composing characters are not counted even if p_deco is set. */
4823 ++num_move_back;
4824 }
4825 if (preedit_start_col != MAXCOL)
4826 preedit_end_col += utf_ptr2cells(p);
4827 }
4828
4829 if (p > str)
4830 {
4831 im_add_to_input(str, (int)(p - str));
4832 im_correct_cursor(num_move_back);
4833 }
4834
4835 g_free(preedit_string);
4836
4837 if (gtk_main_level() > 0)
4838 gtk_main_quit();
4839}
4840
4841/*
4842 * Translate the Pango attributes at iter to Vim highlighting attributes.
4843 * Ignore attributes not supported by Vim highlighting. This shouldn't have
4844 * too much impact -- right now we handle even more attributes than necessary
4845 * for the IM modules I tested with.
4846 */
4847 static int
4848translate_pango_attributes(PangoAttrIterator *iter)
4849{
4850 PangoAttribute *attr;
4851 int char_attr = HL_NORMAL;
4852
4853 attr = pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE);
4854 if (attr != NULL && ((PangoAttrInt *)attr)->value
4855 != (int)PANGO_UNDERLINE_NONE)
4856 char_attr |= HL_UNDERLINE;
4857
4858 attr = pango_attr_iterator_get(iter, PANGO_ATTR_WEIGHT);
4859 if (attr != NULL && ((PangoAttrInt *)attr)->value >= (int)PANGO_WEIGHT_BOLD)
4860 char_attr |= HL_BOLD;
4861
4862 attr = pango_attr_iterator_get(iter, PANGO_ATTR_STYLE);
4863 if (attr != NULL && ((PangoAttrInt *)attr)->value
4864 != (int)PANGO_STYLE_NORMAL)
4865 char_attr |= HL_ITALIC;
4866
4867 attr = pango_attr_iterator_get(iter, PANGO_ATTR_BACKGROUND);
4868 if (attr != NULL)
4869 {
4870 const PangoColor *color = &((PangoAttrColor *)attr)->color;
4871
4872 /* Assume inverse if black background is requested */
4873 if ((color->red | color->green | color->blue) == 0)
4874 char_attr |= HL_INVERSE;
4875 }
4876
4877 return char_attr;
4878}
4879
4880/*
4881 * Retrieve the highlighting attributes at column col in the preedit string.
4882 * Return -1 if not in preediting mode or if col is out of range.
4883 */
4884 int
4885im_get_feedback_attr(int col)
4886{
4887 char *preedit_string = NULL;
4888 PangoAttrList *attr_list = NULL;
4889 int char_attr = -1;
4890
4891 if (xic == NULL)
4892 return char_attr;
4893
4894 gtk_im_context_get_preedit_string(xic, &preedit_string, &attr_list, NULL);
4895
4896 if (preedit_string != NULL && attr_list != NULL)
4897 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004898 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 /* Get the byte index as used by PangoAttrIterator */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004901 for (idx = 0; col > 0 && preedit_string[idx] != '\0'; --col)
4902 idx += utfc_ptr2len((char_u *)preedit_string + idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar89d40322006-08-29 15:30:07 +00004904 if (preedit_string[idx] != '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 PangoAttrIterator *iter;
4907 int start, end;
4908
4909 char_attr = HL_NORMAL;
4910 iter = pango_attr_list_get_iterator(attr_list);
4911
4912 /* Extract all relevant attributes from the list. */
4913 do
4914 {
4915 pango_attr_iterator_range(iter, &start, &end);
4916
Bram Moolenaar89d40322006-08-29 15:30:07 +00004917 if (idx >= start && idx < end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 char_attr |= translate_pango_attributes(iter);
4919 }
4920 while (pango_attr_iterator_next(iter));
4921
4922 pango_attr_iterator_destroy(iter);
4923 }
4924 }
4925
4926 if (attr_list != NULL)
4927 pango_attr_list_unref(attr_list);
4928 g_free(preedit_string);
4929
4930 return char_attr;
4931}
4932
4933 void
4934xim_init(void)
4935{
4936#ifdef XIM_DEBUG
4937 xim_log("xim_init()\n");
4938#endif
4939
4940 g_return_if_fail(gui.drawarea != NULL);
4941 g_return_if_fail(gui.drawarea->window != NULL);
4942
4943 xic = gtk_im_multicontext_new();
4944 g_object_ref(xic);
4945
4946 im_commit_handler_id = g_signal_connect(G_OBJECT(xic), "commit",
4947 G_CALLBACK(&im_commit_cb), NULL);
4948 g_signal_connect(G_OBJECT(xic), "preedit_changed",
4949 G_CALLBACK(&im_preedit_changed_cb), NULL);
4950 g_signal_connect(G_OBJECT(xic), "preedit_start",
4951 G_CALLBACK(&im_preedit_start_cb), NULL);
4952 g_signal_connect(G_OBJECT(xic), "preedit_end",
4953 G_CALLBACK(&im_preedit_end_cb), NULL);
4954
4955 gtk_im_context_set_client_window(xic, gui.drawarea->window);
4956}
4957
4958 void
4959im_shutdown(void)
4960{
4961#ifdef XIM_DEBUG
4962 xim_log("im_shutdown()\n");
4963#endif
4964
4965 if (xic != NULL)
4966 {
4967 gtk_im_context_focus_out(xic);
4968 g_object_unref(xic);
4969 xic = NULL;
4970 }
4971 im_is_active = FALSE;
4972 im_commit_handler_id = 0;
4973 preedit_start_col = MAXCOL;
4974 xim_has_preediting = FALSE;
4975}
4976
4977/*
4978 * Convert the string argument to keyval and state for GdkEventKey.
4979 * If str is valid return TRUE, otherwise FALSE.
4980 *
4981 * See 'imactivatekey' for documentation of the format.
4982 */
4983 static int
4984im_string_to_keyval(const char *str, unsigned int *keyval, unsigned int *state)
4985{
4986 const char *mods_end;
4987 unsigned tmp_keyval;
4988 unsigned tmp_state = 0;
4989
4990 mods_end = strrchr(str, '-');
4991 mods_end = (mods_end != NULL) ? mods_end + 1 : str;
4992
4993 /* Parse modifier keys */
4994 while (str < mods_end)
4995 switch (*str++)
4996 {
4997 case '-': break;
4998 case 'S': case 's': tmp_state |= (unsigned)GDK_SHIFT_MASK; break;
4999 case 'L': case 'l': tmp_state |= (unsigned)GDK_LOCK_MASK; break;
5000 case 'C': case 'c': tmp_state |= (unsigned)GDK_CONTROL_MASK;break;
5001 case '1': tmp_state |= (unsigned)GDK_MOD1_MASK; break;
5002 case '2': tmp_state |= (unsigned)GDK_MOD2_MASK; break;
5003 case '3': tmp_state |= (unsigned)GDK_MOD3_MASK; break;
5004 case '4': tmp_state |= (unsigned)GDK_MOD4_MASK; break;
5005 case '5': tmp_state |= (unsigned)GDK_MOD5_MASK; break;
5006 default:
5007 return FALSE;
5008 }
5009
5010 tmp_keyval = gdk_keyval_from_name(str);
5011
5012 if (tmp_keyval == 0 || tmp_keyval == GDK_VoidSymbol)
5013 return FALSE;
5014
5015 if (keyval != NULL)
5016 *keyval = tmp_keyval;
5017 if (state != NULL)
5018 *state = tmp_state;
5019
5020 return TRUE;
5021}
5022
5023/*
5024 * Return TRUE if p_imak is valid, otherwise FALSE. As a special case, an
5025 * empty string is also regarded as valid.
5026 *
5027 * Note: The numerical key value of p_imak is cached if it was valid; thus
5028 * boldly assuming im_xim_isvalid_imactivate() will always be called whenever
5029 * 'imak' changes. This is currently the case but not obvious -- should
5030 * probably rename the function for clarity.
5031 */
5032 int
5033im_xim_isvalid_imactivate(void)
5034{
5035 if (p_imak[0] == NUL)
5036 {
5037 im_activatekey_keyval = GDK_VoidSymbol;
5038 im_activatekey_state = 0;
5039 return TRUE;
5040 }
5041
5042 return im_string_to_keyval((const char *)p_imak,
5043 &im_activatekey_keyval,
5044 &im_activatekey_state);
5045}
5046
5047 static void
5048im_synthesize_keypress(unsigned int keyval, unsigned int state)
5049{
5050 GdkEventKey *event;
5051
5052# ifdef HAVE_GTK_MULTIHEAD
5053 event = (GdkEventKey *)gdk_event_new(GDK_KEY_PRESS);
5054 g_object_ref(gui.drawarea->window); /* unreffed by gdk_event_free() */
5055# else
5056 event = (GdkEventKey *)g_malloc0((gulong)sizeof(GdkEvent));
5057 event->type = GDK_KEY_PRESS;
5058# endif
5059 event->window = gui.drawarea->window;
5060 event->send_event = TRUE;
5061 event->time = GDK_CURRENT_TIME;
5062 event->state = state;
5063 event->keyval = keyval;
5064 event->hardware_keycode = /* needed for XIM */
5065 XKeysymToKeycode(GDK_WINDOW_XDISPLAY(event->window), (KeySym)keyval);
5066 event->length = 0;
5067 event->string = NULL;
5068
5069 gtk_im_context_filter_keypress(xic, event);
5070
5071 /* For consistency, also send the corresponding release event. */
5072 event->type = GDK_KEY_RELEASE;
5073 event->send_event = FALSE;
5074 gtk_im_context_filter_keypress(xic, event);
5075
5076# ifdef HAVE_GTK_MULTIHEAD
5077 gdk_event_free((GdkEvent *)event);
5078# else
5079 g_free(event);
5080# endif
5081}
5082
5083 void
5084xim_reset(void)
5085{
5086 if (xic != NULL)
5087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 gtk_im_context_reset(xic);
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (p_imdisable)
5091 im_shutdown();
5092 else
5093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 xim_set_focus(gui.in_focus);
5095
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005096# ifdef FEAT_EVAL
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005097 if (p_imaf[0] != NUL)
5098 {
5099 char_u *argv[1];
5100
5101 if (im_is_active)
5102 argv[0] = (char_u *)"1";
5103 else
5104 argv[0] = (char_u *)"0";
5105 (void)call_func_retnr(p_imaf, 1, argv, FALSE);
5106 }
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005107 else
5108# endif
5109 if (im_activatekey_keyval != GDK_VoidSymbol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 if (im_is_active)
5112 {
5113 g_signal_handler_block(xic, im_commit_handler_id);
5114 im_synthesize_keypress(im_activatekey_keyval,
5115 im_activatekey_state);
5116 g_signal_handler_unblock(xic, im_commit_handler_id);
5117 }
5118 }
5119 else
5120 {
5121 im_shutdown();
5122 xim_init();
5123 xim_set_focus(gui.in_focus);
5124 }
5125 }
5126 }
5127
5128 preedit_start_col = MAXCOL;
5129 xim_has_preediting = FALSE;
5130}
5131
5132 int
5133xim_queue_key_press_event(GdkEventKey *event, int down)
5134{
5135 if (down)
5136 {
5137 /*
5138 * Workaround GTK2 XIM 'feature' that always converts keypad keys to
5139 * chars., even when not part of an IM sequence (ref. feature of
5140 * gdk/gdkkeyuni.c).
5141 * Flag any keypad keys that might represent a single char.
5142 * If this (on its own - i.e., not part of an IM sequence) is
5143 * committed while we're processing one of these keys, we can ignore
5144 * that commit and go ahead & process it ourselves. That way we can
5145 * still distinguish keypad keys for use in mappings.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005146 * Also add GDK_space to make <S-Space> work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 */
5148 switch (event->keyval)
5149 {
5150 case GDK_KP_Add: xim_expected_char = '+'; break;
5151 case GDK_KP_Subtract: xim_expected_char = '-'; break;
5152 case GDK_KP_Divide: xim_expected_char = '/'; break;
5153 case GDK_KP_Multiply: xim_expected_char = '*'; break;
5154 case GDK_KP_Decimal: xim_expected_char = '.'; break;
5155 case GDK_KP_Equal: xim_expected_char = '='; break;
5156 case GDK_KP_0: xim_expected_char = '0'; break;
5157 case GDK_KP_1: xim_expected_char = '1'; break;
5158 case GDK_KP_2: xim_expected_char = '2'; break;
5159 case GDK_KP_3: xim_expected_char = '3'; break;
5160 case GDK_KP_4: xim_expected_char = '4'; break;
5161 case GDK_KP_5: xim_expected_char = '5'; break;
5162 case GDK_KP_6: xim_expected_char = '6'; break;
5163 case GDK_KP_7: xim_expected_char = '7'; break;
5164 case GDK_KP_8: xim_expected_char = '8'; break;
5165 case GDK_KP_9: xim_expected_char = '9'; break;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005166 case GDK_space: xim_expected_char = ' '; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 default: xim_expected_char = NUL;
5168 }
5169 xim_ignored_char = FALSE;
5170 }
5171
5172 /*
5173 * When typing fFtT, XIM may be activated. Thus it must pass
5174 * gtk_im_context_filter_keypress() in Normal mode.
5175 * And while doing :sh too.
5176 */
5177 if (xic != NULL && !p_imdisable
5178 && (State & (INSERT | CMDLINE | NORMAL | EXTERNCMD)) != 0)
5179 {
5180 /*
5181 * Filter 'imactivatekey' and map it to CTRL-^. This way, Vim is
5182 * always aware of the current status of IM, and can even emulate
5183 * the activation key for modules that don't support one.
5184 */
5185 if (event->keyval == im_activatekey_keyval
5186 && (event->state & im_activatekey_state) == im_activatekey_state)
5187 {
5188 unsigned int state_mask;
5189
5190 /* Require the state of the 3 most used modifiers to match exactly.
5191 * Otherwise e.g. <S-C-space> would be unusable for other purposes
5192 * if the IM activate key is <S-space>. */
5193 state_mask = im_activatekey_state;
5194 state_mask |= ((int)GDK_SHIFT_MASK | (int)GDK_CONTROL_MASK
5195 | (int)GDK_MOD1_MASK);
5196
5197 if ((event->state & state_mask) != im_activatekey_state)
5198 return FALSE;
5199
5200 /* Don't send it a second time on GDK_KEY_RELEASE. */
5201 if (event->type != GDK_KEY_PRESS)
5202 return TRUE;
5203
Bram Moolenaar658b74a2006-03-18 21:33:02 +00005204 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
5206 im_set_active(FALSE);
5207
5208 /* ":lmap" mappings exists, toggle use of mappings. */
5209 State ^= LANGMAP;
5210 if (State & LANGMAP)
5211 {
5212 curbuf->b_p_iminsert = B_IMODE_NONE;
5213 State &= ~LANGMAP;
5214 }
5215 else
5216 {
5217 curbuf->b_p_iminsert = B_IMODE_LMAP;
5218 State |= LANGMAP;
5219 }
5220 return TRUE;
5221 }
5222
5223 return gtk_im_context_filter_keypress(xic, event);
5224 }
5225
5226 /* Don't filter events through the IM context if IM isn't active
5227 * right now. Unlike with GTK+ 1.2 we cannot rely on the IM module
5228 * not doing anything before the activation key was sent. */
5229 if (im_activatekey_keyval == GDK_VoidSymbol || im_is_active)
5230 {
5231 int imresult = gtk_im_context_filter_keypress(xic, event);
5232
5233 /* Some XIM send following sequence:
5234 * 1. preedited string.
5235 * 2. committed string.
5236 * 3. line changed key.
5237 * 4. preedited string.
5238 * 5. remove preedited string.
5239 * if 3, Vim can't move back the above line for 5.
5240 * thus, this part should not parse the key. */
5241 if (!imresult && preedit_start_col != MAXCOL
5242 && event->keyval == GDK_Return)
5243 {
5244 im_synthesize_keypress(GDK_Return, 0U);
5245 return FALSE;
5246 }
5247
5248 /* If XIM tried to commit a keypad key as a single char.,
5249 * ignore it so we can use the keypad key 'raw', for mappings. */
5250 if (xim_expected_char != NUL && xim_ignored_char)
5251 /* We had a keypad key, and XIM tried to thieve it */
5252 return FALSE;
5253
Bram Moolenaar673214b2011-07-27 18:25:44 +02005254 /* This is supposed to fix a problem with iBus, that space
5255 * characters don't work in input mode. */
5256 xim_expected_char = NUL;
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 /* Normal processing */
5259 return imresult;
5260 }
5261 }
5262
5263 return FALSE;
5264}
5265
5266 int
5267im_get_status(void)
5268{
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005269# ifdef FEAT_EVAL
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005270 if (p_imsf[0] != NUL)
5271 {
5272 int is_active;
5273
5274 /* FIXME: Don't execute user function in unsafe situation. */
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005275 if (exiting
5276# ifdef FEAT_AUTOCMD
5277 || is_autocmd_blocked()
5278# endif
5279 )
Bram Moolenaarabab85a2013-06-26 19:18:05 +02005280 return FALSE;
5281 /* FIXME: :py print 'xxx' is shown duplicate result.
5282 * Use silent to avoid it. */
5283 ++msg_silent;
5284 is_active = call_func_retnr(p_imsf, 0, NULL, FALSE);
5285 --msg_silent;
5286 return (is_active > 0);
5287 }
Bram Moolenaarf0327f62013-06-28 20:16:55 +02005288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return im_is_active;
5290}
5291
Bram Moolenaar64404472010-06-26 06:24:45 +02005292 int
5293preedit_get_status(void)
5294{
5295 return preedit_is_active;
5296}
5297
5298 int
5299im_is_preediting()
5300{
5301 return xim_has_preediting;
5302}
5303
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005304# else /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306static int xim_is_active = FALSE; /* XIM should be active in the current
5307 mode */
5308static int xim_has_focus = FALSE; /* XIM is really being used for Vim */
5309#ifdef FEAT_GUI_X11
5310static XIMStyle input_style;
5311static int status_area_enabled = TRUE;
5312#endif
5313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314/*
5315 * Switch using XIM on/off. This is used by the code that changes "State".
5316 */
5317 void
5318im_set_active(active)
5319 int active;
5320{
5321 if (xic == NULL)
5322 return;
5323
5324 /* If 'imdisable' is set, XIM is never active. */
5325 if (p_imdisable)
5326 active = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005327#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 else if (input_style & XIMPreeditPosition)
5329 /* There is a problem in switching XIM off when preediting is used,
5330 * and it is not clear how this can be solved. For now, keep XIM on
5331 * all the time, like it was done in Vim 5.8. */
5332 active = TRUE;
5333#endif
5334
5335 /* Remember the active state, it is needed when Vim gets keyboard focus. */
5336 xim_is_active = active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 xim_set_preedit();
5338}
5339
5340/*
5341 * Adjust using XIM for gaining or losing keyboard focus. Also called when
5342 * "xim_is_active" changes.
5343 */
5344 void
5345xim_set_focus(focus)
5346 int focus;
5347{
5348 if (xic == NULL)
5349 return;
5350
5351 /*
5352 * XIM only gets focus when the Vim window has keyboard focus and XIM has
5353 * been set active for the current mode.
5354 */
5355 if (focus && xim_is_active)
5356 {
5357 if (!xim_has_focus)
5358 {
5359 xim_has_focus = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 XSetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362 }
5363 else
5364 {
5365 if (xim_has_focus)
5366 {
5367 xim_has_focus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 XUnsetICFocus(xic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 }
5371}
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 void
5374im_set_position(row, col)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005375 int row UNUSED;
5376 int col UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 xim_set_preedit();
5379}
5380
5381/*
5382 * Set the XIM to the current cursor position.
5383 */
5384 void
5385xim_set_preedit()
5386{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005387 XVaNestedList attr_list;
5388 XRectangle spot_area;
5389 XPoint over_spot;
5390 int line_space;
5391
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02005392 if (xic == NULL)
5393 return;
5394
5395 xim_set_focus(TRUE);
5396
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005397 if (!xim_has_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005399 /* hide XIM cursor */
5400 over_spot.x = 0;
5401 over_spot.y = -100; /* arbitrary invisible position */
5402 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5403 XNSpotLocation,
5404 &over_spot,
5405 NULL);
5406 XSetICValues(xic, XNPreeditAttributes, attr_list, NULL);
5407 XFree(attr_list);
5408 return;
5409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005411 if (input_style & XIMPreeditPosition)
5412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (xim_fg_color == INVALCOLOR)
5414 {
5415 xim_fg_color = gui.def_norm_pixel;
5416 xim_bg_color = gui.def_back_pixel;
5417 }
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005418 over_spot.x = TEXT_X(gui.col);
5419 over_spot.y = TEXT_Y(gui.row);
5420 spot_area.x = 0;
5421 spot_area.y = 0;
5422 spot_area.height = gui.char_height * Rows;
5423 spot_area.width = gui.char_width * Columns;
5424 line_space = gui.char_height;
5425 attr_list = (XVaNestedList) XVaCreateNestedList(0,
5426 XNSpotLocation, &over_spot,
5427 XNForeground, (Pixel) xim_fg_color,
5428 XNBackground, (Pixel) xim_bg_color,
5429 XNArea, &spot_area,
5430 XNLineSpace, line_space,
5431 NULL);
5432 if (XSetICValues(xic, XNPreeditAttributes, attr_list, NULL))
5433 EMSG(_("E284: Cannot set IC values"));
5434 XFree(attr_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436}
5437
Bram Moolenaar182c5be2010-06-25 05:37:59 +02005438#if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439static char e_xim[] = N_("E285: Failed to create input context");
5440#endif
5441
5442#if defined(FEAT_GUI_X11) || defined(PROTO)
5443# if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && !defined(sun)
5444# define USE_X11R6_XIM
5445# endif
5446
5447static int xim_real_init __ARGS((Window x11_window, Display *x11_display));
5448
5449
5450#ifdef USE_X11R6_XIM
5451static void xim_instantiate_cb __ARGS((Display *display, XPointer client_data, XPointer call_data));
5452static void xim_destroy_cb __ARGS((XIM im, XPointer client_data, XPointer call_data));
5453
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 static void
5455xim_instantiate_cb(display, client_data, call_data)
5456 Display *display;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005457 XPointer client_data UNUSED;
5458 XPointer call_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 Window x11_window;
5461 Display *x11_display;
5462
5463#ifdef XIM_DEBUG
5464 xim_log("xim_instantiate_cb()\n");
5465#endif
5466
5467 gui_get_x11_windis(&x11_window, &x11_display);
5468 if (display != x11_display)
5469 return;
5470
5471 xim_real_init(x11_window, x11_display);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005472 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if (xic != NULL)
5474 XUnregisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5475 xim_instantiate_cb, NULL);
5476}
5477
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 static void
5479xim_destroy_cb(im, client_data, call_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005480 XIM im UNUSED;
5481 XPointer client_data UNUSED;
5482 XPointer call_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 Window x11_window;
5485 Display *x11_display;
5486
5487#ifdef XIM_DEBUG
5488 xim_log("xim_destroy_cb()\n");
5489#endif
5490 gui_get_x11_windis(&x11_window, &x11_display);
5491
5492 xic = NULL;
5493 status_area_enabled = FALSE;
5494
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005495 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5498 xim_instantiate_cb, NULL);
5499}
5500#endif
5501
5502 void
5503xim_init()
5504{
5505 Window x11_window;
5506 Display *x11_display;
5507
5508#ifdef XIM_DEBUG
5509 xim_log("xim_init()\n");
5510#endif
5511
5512 gui_get_x11_windis(&x11_window, &x11_display);
5513
5514 xic = NULL;
5515
5516 if (xim_real_init(x11_window, x11_display))
5517 return;
5518
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005519 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
5521#ifdef USE_X11R6_XIM
5522 XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL,
5523 xim_instantiate_cb, NULL);
5524#endif
5525}
5526
5527 static int
5528xim_real_init(x11_window, x11_display)
5529 Window x11_window;
5530 Display *x11_display;
5531{
5532 int i;
5533 char *p,
5534 *s,
5535 *ns,
5536 *end,
5537 tmp[1024];
5538#define IMLEN_MAX 40
5539 char buf[IMLEN_MAX + 7];
5540 XIM xim = NULL;
5541 XIMStyles *xim_styles;
5542 XIMStyle this_input_style = 0;
5543 Boolean found;
5544 XPoint over_spot;
5545 XVaNestedList preedit_list, status_list;
5546
5547 input_style = 0;
5548 status_area_enabled = FALSE;
5549
5550 if (xic != NULL)
5551 return FALSE;
5552
5553 if (gui.rsrc_input_method != NULL && *gui.rsrc_input_method != NUL)
5554 {
5555 strcpy(tmp, gui.rsrc_input_method);
5556 for (ns = s = tmp; ns != NULL && *s != NUL;)
5557 {
5558 s = (char *)skipwhite((char_u *)s);
5559 if (*s == NUL)
5560 break;
5561 if ((ns = end = strchr(s, ',')) == NULL)
5562 end = s + strlen(s);
5563 while (isspace(((char_u *)end)[-1]))
5564 end--;
5565 *end = NUL;
5566
5567 if (strlen(s) <= IMLEN_MAX)
5568 {
5569 strcpy(buf, "@im=");
5570 strcat(buf, s);
5571 if ((p = XSetLocaleModifiers(buf)) != NULL && *p != NUL
5572 && (xim = XOpenIM(x11_display, NULL, NULL, NULL))
5573 != NULL)
5574 break;
5575 }
5576
5577 s = ns + 1;
5578 }
5579 }
5580
5581 if (xim == NULL && (p = XSetLocaleModifiers("")) != NULL && *p != NUL)
5582 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5583
5584 /* This is supposed to be useful to obtain characters through
5585 * XmbLookupString() without really using a XIM. */
5586 if (xim == NULL && (p = XSetLocaleModifiers("@im=none")) != NULL
5587 && *p != NUL)
5588 xim = XOpenIM(x11_display, NULL, NULL, NULL);
5589
5590 if (xim == NULL)
5591 {
5592 /* Only give this message when verbose is set, because too many people
5593 * got this message when they didn't want to use a XIM. */
5594 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005595 {
5596 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 EMSG(_("E286: Failed to open input method"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005598 verbose_leave();
5599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 return FALSE;
5601 }
5602
5603#ifdef USE_X11R6_XIM
5604 {
5605 XIMCallback destroy_cb;
5606
5607 destroy_cb.callback = xim_destroy_cb;
5608 destroy_cb.client_data = NULL;
5609 if (XSetIMValues(xim, XNDestroyCallback, &destroy_cb, NULL))
5610 EMSG(_("E287: Warning: Could not set destroy callback to IM"));
5611 }
5612#endif
5613
5614 if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL) || !xim_styles)
5615 {
5616 EMSG(_("E288: input method doesn't support any style"));
5617 XCloseIM(xim);
5618 return FALSE;
5619 }
5620
5621 found = False;
5622 strcpy(tmp, gui.rsrc_preedit_type_name);
5623 for (s = tmp; s && !found; )
5624 {
5625 while (*s && isspace((unsigned char)*s))
5626 s++;
5627 if (!*s)
5628 break;
5629 if ((ns = end = strchr(s, ',')) != 0)
5630 ns++;
5631 else
5632 end = s + strlen(s);
5633 while (isspace((unsigned char)*end))
5634 end--;
5635 *end = '\0';
5636
5637 if (!strcmp(s, "OverTheSpot"))
5638 this_input_style = (XIMPreeditPosition | XIMStatusArea);
5639 else if (!strcmp(s, "OffTheSpot"))
5640 this_input_style = (XIMPreeditArea | XIMStatusArea);
5641 else if (!strcmp(s, "Root"))
5642 this_input_style = (XIMPreeditNothing | XIMStatusNothing);
5643
5644 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5645 {
5646 if (this_input_style == xim_styles->supported_styles[i])
5647 {
5648 found = True;
5649 break;
5650 }
5651 }
5652 if (!found)
5653 for (i = 0; (unsigned short)i < xim_styles->count_styles; i++)
5654 {
5655 if ((xim_styles->supported_styles[i] & this_input_style)
5656 == (this_input_style & ~XIMStatusArea))
5657 {
5658 this_input_style &= ~XIMStatusArea;
5659 found = True;
5660 break;
5661 }
5662 }
5663
5664 s = ns;
5665 }
5666 XFree(xim_styles);
5667
5668 if (!found)
5669 {
5670 /* Only give this message when verbose is set, because too many people
5671 * got this message when they didn't want to use a XIM. */
5672 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005673 {
5674 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 EMSG(_("E289: input method doesn't support my preedit type"));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005676 verbose_leave();
5677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 XCloseIM(xim);
5679 return FALSE;
5680 }
5681
5682 over_spot.x = TEXT_X(gui.col);
5683 over_spot.y = TEXT_Y(gui.row);
5684 input_style = this_input_style;
5685
5686 /* A crash was reported when trying to pass gui.norm_font as XNFontSet,
5687 * thus that has been removed. Hopefully the default works... */
5688#ifdef FEAT_XFONTSET
5689 if (gui.fontset != NOFONTSET)
5690 {
5691 preedit_list = XVaCreateNestedList(0,
5692 XNSpotLocation, &over_spot,
5693 XNForeground, (Pixel)gui.def_norm_pixel,
5694 XNBackground, (Pixel)gui.def_back_pixel,
5695 XNFontSet, (XFontSet)gui.fontset,
5696 NULL);
5697 status_list = XVaCreateNestedList(0,
5698 XNForeground, (Pixel)gui.def_norm_pixel,
5699 XNBackground, (Pixel)gui.def_back_pixel,
5700 XNFontSet, (XFontSet)gui.fontset,
5701 NULL);
5702 }
5703 else
5704#endif
5705 {
5706 preedit_list = XVaCreateNestedList(0,
5707 XNSpotLocation, &over_spot,
5708 XNForeground, (Pixel)gui.def_norm_pixel,
5709 XNBackground, (Pixel)gui.def_back_pixel,
5710 NULL);
5711 status_list = XVaCreateNestedList(0,
5712 XNForeground, (Pixel)gui.def_norm_pixel,
5713 XNBackground, (Pixel)gui.def_back_pixel,
5714 NULL);
5715 }
5716
5717 xic = XCreateIC(xim,
5718 XNInputStyle, input_style,
5719 XNClientWindow, x11_window,
5720 XNFocusWindow, gui.wid,
5721 XNPreeditAttributes, preedit_list,
5722 XNStatusAttributes, status_list,
5723 NULL);
5724 XFree(status_list);
5725 XFree(preedit_list);
5726 if (xic != NULL)
5727 {
5728 if (input_style & XIMStatusArea)
5729 {
5730 xim_set_status_area();
5731 status_area_enabled = TRUE;
5732 }
5733 else
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00005734 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
5736 else
5737 {
5738 EMSG(_(e_xim));
5739 XCloseIM(xim);
5740 return FALSE;
5741 }
5742
5743 return TRUE;
5744}
5745
5746#endif /* FEAT_GUI_X11 */
5747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748/*
5749 * Get IM status. When IM is on, return TRUE. Else return FALSE.
5750 * FIXME: This doesn't work correctly: Having focus doesn't always mean XIM is
5751 * active, when not having focus XIM may still be active (e.g., when using a
5752 * tear-off menu item).
5753 */
5754 int
5755im_get_status()
5756{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 return xim_has_focus;
5758}
5759
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005760# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761
Bram Moolenaar64404472010-06-26 06:24:45 +02005762# if !defined(FEAT_GUI_GTK) || defined(PROTO)
5763/*
5764 * Set up the status area.
5765 *
5766 * This should use a separate Widget, but that seems not possible, because
5767 * preedit_area and status_area should be set to the same window as for the
5768 * text input. Unfortunately this means the status area pollutes the text
5769 * window...
5770 */
5771 void
5772xim_set_status_area()
Bram Moolenaarc236c162008-07-13 17:41:49 +00005773{
Bram Moolenaar64404472010-06-26 06:24:45 +02005774 XVaNestedList preedit_list = 0, status_list = 0, list = 0;
5775 XRectangle pre_area, status_area;
5776
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02005777 if (xic == NULL)
5778 return;
5779
Bram Moolenaar64404472010-06-26 06:24:45 +02005780 if (input_style & XIMStatusArea)
5781 {
5782 if (input_style & XIMPreeditArea)
5783 {
5784 XRectangle *needed_rect;
5785
5786 /* to get status_area width */
5787 status_list = XVaCreateNestedList(0, XNAreaNeeded,
5788 &needed_rect, NULL);
5789 XGetICValues(xic, XNStatusAttributes, status_list, NULL);
5790 XFree(status_list);
5791
5792 status_area.width = needed_rect->width;
5793 }
5794 else
5795 status_area.width = gui.char_width * Columns;
5796
5797 status_area.x = 0;
5798 status_area.y = gui.char_height * Rows + gui.border_offset;
5799 if (gui.which_scrollbars[SBAR_BOTTOM])
5800 status_area.y += gui.scrollbar_height;
5801#ifdef FEAT_MENU
5802 if (gui.menu_is_active)
5803 status_area.y += gui.menu_height;
5804#endif
5805 status_area.height = gui.char_height;
5806 status_list = XVaCreateNestedList(0, XNArea, &status_area, NULL);
5807 }
5808 else
5809 {
5810 status_area.x = 0;
5811 status_area.y = gui.char_height * Rows + gui.border_offset;
5812 if (gui.which_scrollbars[SBAR_BOTTOM])
5813 status_area.y += gui.scrollbar_height;
5814#ifdef FEAT_MENU
5815 if (gui.menu_is_active)
5816 status_area.y += gui.menu_height;
5817#endif
5818 status_area.width = 0;
5819 status_area.height = gui.char_height;
5820 }
5821
5822 if (input_style & XIMPreeditArea) /* off-the-spot */
5823 {
5824 pre_area.x = status_area.x + status_area.width;
5825 pre_area.y = gui.char_height * Rows + gui.border_offset;
5826 pre_area.width = gui.char_width * Columns - pre_area.x;
5827 if (gui.which_scrollbars[SBAR_BOTTOM])
5828 pre_area.y += gui.scrollbar_height;
5829#ifdef FEAT_MENU
5830 if (gui.menu_is_active)
5831 pre_area.y += gui.menu_height;
5832#endif
5833 pre_area.height = gui.char_height;
5834 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
5835 }
5836 else if (input_style & XIMPreeditPosition) /* over-the-spot */
5837 {
5838 pre_area.x = 0;
5839 pre_area.y = 0;
5840 pre_area.height = gui.char_height * Rows;
5841 pre_area.width = gui.char_width * Columns;
5842 preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL);
5843 }
5844
5845 if (preedit_list && status_list)
5846 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
5847 XNStatusAttributes, status_list, NULL);
5848 else if (preedit_list)
5849 list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list,
5850 NULL);
5851 else if (status_list)
5852 list = XVaCreateNestedList(0, XNStatusAttributes, status_list,
5853 NULL);
5854 else
5855 list = NULL;
5856
5857 if (list)
5858 {
5859 XSetICValues(xic, XNVaNestedList, list, NULL);
5860 XFree(list);
5861 }
5862 if (status_list)
5863 XFree(status_list);
5864 if (preedit_list)
5865 XFree(preedit_list);
5866}
5867
5868 int
5869xim_get_status_area_height()
5870{
5871 if (status_area_enabled)
5872 return gui.char_height;
5873 return 0;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005874}
5875# endif
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#endif /* FEAT_XIM */
5878
5879#if defined(FEAT_MBYTE) || defined(PROTO)
5880
5881/*
5882 * Setup "vcp" for conversion from "from" to "to".
5883 * The names must have been made canonical with enc_canonize().
5884 * vcp->vc_type must have been initialized to CONV_NONE.
5885 * Note: cannot be used for conversion from/to ucs-2 and ucs-4 (will use utf-8
5886 * instead).
5887 * Afterwards invoke with "from" and "to" equal to NULL to cleanup.
5888 * Return FAIL when conversion is not supported, OK otherwise.
5889 */
5890 int
5891convert_setup(vcp, from, to)
5892 vimconv_T *vcp;
5893 char_u *from;
5894 char_u *to;
5895{
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005896 return convert_setup_ext(vcp, from, TRUE, to, TRUE);
5897}
5898
5899/*
5900 * As convert_setup(), but only when from_unicode_is_utf8 is TRUE will all
5901 * "from" unicode charsets be considered utf-8. Same for "to".
5902 */
5903 int
5904convert_setup_ext(vcp, from, from_unicode_is_utf8, to, to_unicode_is_utf8)
5905 vimconv_T *vcp;
5906 char_u *from;
5907 int from_unicode_is_utf8;
5908 char_u *to;
5909 int to_unicode_is_utf8;
5910{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 int from_prop;
5912 int to_prop;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005913 int from_is_utf8;
5914 int to_is_utf8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
5916 /* Reset to no conversion. */
5917# ifdef USE_ICONV
5918 if (vcp->vc_type == CONV_ICONV && vcp->vc_fd != (iconv_t)-1)
5919 iconv_close(vcp->vc_fd);
5920# endif
5921 vcp->vc_type = CONV_NONE;
5922 vcp->vc_factor = 1;
5923 vcp->vc_fail = FALSE;
5924
5925 /* No conversion when one of the names is empty or they are equal. */
5926 if (from == NULL || *from == NUL || to == NULL || *to == NUL
5927 || STRCMP(from, to) == 0)
5928 return OK;
5929
5930 from_prop = enc_canon_props(from);
5931 to_prop = enc_canon_props(to);
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005932 if (from_unicode_is_utf8)
5933 from_is_utf8 = from_prop & ENC_UNICODE;
5934 else
5935 from_is_utf8 = from_prop == ENC_UNICODE;
5936 if (to_unicode_is_utf8)
5937 to_is_utf8 = to_prop & ENC_UNICODE;
5938 else
5939 to_is_utf8 = to_prop == ENC_UNICODE;
5940
5941 if ((from_prop & ENC_LATIN1) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
5943 /* Internal latin1 -> utf-8 conversion. */
5944 vcp->vc_type = CONV_TO_UTF8;
5945 vcp->vc_factor = 2; /* up to twice as long */
5946 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005947 else if ((from_prop & ENC_LATIN9) && to_is_utf8)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005948 {
5949 /* Internal latin9 -> utf-8 conversion. */
5950 vcp->vc_type = CONV_9_TO_UTF8;
5951 vcp->vc_factor = 3; /* up to three as long (euro sign) */
5952 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005953 else if (from_is_utf8 && (to_prop & ENC_LATIN1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
5955 /* Internal utf-8 -> latin1 conversion. */
5956 vcp->vc_type = CONV_TO_LATIN1;
5957 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005958 else if (from_is_utf8 && (to_prop & ENC_LATIN9))
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005959 {
5960 /* Internal utf-8 -> latin9 conversion. */
5961 vcp->vc_type = CONV_TO_LATIN9;
5962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963#ifdef WIN3264
5964 /* Win32-specific codepage <-> codepage conversion without iconv. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005965 else if ((from_is_utf8 || encname2codepage(from) > 0)
5966 && (to_is_utf8 || encname2codepage(to) > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 vcp->vc_type = CONV_CODEPAGE;
5969 vcp->vc_factor = 2; /* up to twice as long */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005970 vcp->vc_cpfrom = from_is_utf8 ? 0 : encname2codepage(from);
5971 vcp->vc_cpto = to_is_utf8 ? 0 : encname2codepage(to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
5973#endif
5974#ifdef MACOS_X
5975 else if ((from_prop & ENC_MACROMAN) && (to_prop & ENC_LATIN1))
5976 {
5977 vcp->vc_type = CONV_MAC_LATIN1;
5978 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005979 else if ((from_prop & ENC_MACROMAN) && to_is_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
5981 vcp->vc_type = CONV_MAC_UTF8;
5982 vcp->vc_factor = 2; /* up to twice as long */
5983 }
5984 else if ((from_prop & ENC_LATIN1) && (to_prop & ENC_MACROMAN))
5985 {
5986 vcp->vc_type = CONV_LATIN1_MAC;
5987 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005988 else if (from_is_utf8 && (to_prop & ENC_MACROMAN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 {
5990 vcp->vc_type = CONV_UTF8_MAC;
5991 }
5992#endif
5993# ifdef USE_ICONV
5994 else
5995 {
5996 /* Use iconv() for conversion. */
5997 vcp->vc_fd = (iconv_t)my_iconv_open(
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005998 to_is_utf8 ? (char_u *)"utf-8" : to,
5999 from_is_utf8 ? (char_u *)"utf-8" : from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (vcp->vc_fd != (iconv_t)-1)
6001 {
6002 vcp->vc_type = CONV_ICONV;
6003 vcp->vc_factor = 4; /* could be longer too... */
6004 }
6005 }
6006# endif
6007 if (vcp->vc_type == CONV_NONE)
6008 return FAIL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 return OK;
6011}
6012
6013#if defined(FEAT_GUI) || defined(AMIGA) || defined(WIN3264) \
6014 || defined(MSDOS) || defined(PROTO)
6015/*
6016 * Do conversion on typed input characters in-place.
6017 * The input and output are not NUL terminated!
6018 * Returns the length after conversion.
6019 */
6020 int
6021convert_input(ptr, len, maxlen)
6022 char_u *ptr;
6023 int len;
6024 int maxlen;
6025{
6026 return convert_input_safe(ptr, len, maxlen, NULL, NULL);
6027}
6028#endif
6029
6030/*
6031 * Like convert_input(), but when there is an incomplete byte sequence at the
6032 * end return that as an allocated string in "restp" and set "*restlenp" to
6033 * the length. If "restp" is NULL it is not used.
6034 */
6035 int
6036convert_input_safe(ptr, len, maxlen, restp, restlenp)
6037 char_u *ptr;
6038 int len;
6039 int maxlen;
6040 char_u **restp;
6041 int *restlenp;
6042{
6043 char_u *d;
6044 int dlen = len;
6045 int unconvertlen = 0;
6046
6047 d = string_convert_ext(&input_conv, ptr, &dlen,
6048 restp == NULL ? NULL : &unconvertlen);
6049 if (d != NULL)
6050 {
6051 if (dlen <= maxlen)
6052 {
6053 if (unconvertlen > 0)
6054 {
6055 /* Move the unconverted characters to allocated memory. */
6056 *restp = alloc(unconvertlen);
6057 if (*restp != NULL)
6058 mch_memmove(*restp, ptr + len - unconvertlen, unconvertlen);
6059 *restlenp = unconvertlen;
6060 }
6061 mch_memmove(ptr, d, dlen);
6062 }
6063 else
6064 /* result is too long, keep the unconverted text (the caller must
6065 * have done something wrong!) */
6066 dlen = len;
6067 vim_free(d);
6068 }
6069 return dlen;
6070}
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072/*
6073 * Convert text "ptr[*lenp]" according to "vcp".
6074 * Returns the result in allocated memory and sets "*lenp".
6075 * When "lenp" is NULL, use NUL terminated strings.
6076 * Illegal chars are often changed to "?", unless vcp->vc_fail is set.
6077 * When something goes wrong, NULL is returned and "*lenp" is unchanged.
6078 */
6079 char_u *
6080string_convert(vcp, ptr, lenp)
6081 vimconv_T *vcp;
6082 char_u *ptr;
6083 int *lenp;
6084{
6085 return string_convert_ext(vcp, ptr, lenp, NULL);
6086}
6087
6088/*
6089 * Like string_convert(), but when "unconvlenp" is not NULL and there are is
6090 * an incomplete sequence at the end it is not converted and "*unconvlenp" is
6091 * set to the number of remaining bytes.
6092 */
6093 char_u *
6094string_convert_ext(vcp, ptr, lenp, unconvlenp)
6095 vimconv_T *vcp;
6096 char_u *ptr;
6097 int *lenp;
6098 int *unconvlenp;
6099{
6100 char_u *retval = NULL;
6101 char_u *d;
6102 int len;
6103 int i;
6104 int l;
6105 int c;
6106
6107 if (lenp == NULL)
6108 len = (int)STRLEN(ptr);
6109 else
6110 len = *lenp;
6111 if (len == 0)
6112 return vim_strsave((char_u *)"");
6113
6114 switch (vcp->vc_type)
6115 {
6116 case CONV_TO_UTF8: /* latin1 to utf-8 conversion */
6117 retval = alloc(len * 2 + 1);
6118 if (retval == NULL)
6119 break;
6120 d = retval;
6121 for (i = 0; i < len; ++i)
6122 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006123 c = ptr[i];
6124 if (c < 0x80)
6125 *d++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 else
6127 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006128 *d++ = 0xc0 + ((unsigned)c >> 6);
6129 *d++ = 0x80 + (c & 0x3f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 }
6131 }
6132 *d = NUL;
6133 if (lenp != NULL)
6134 *lenp = (int)(d - retval);
6135 break;
6136
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006137 case CONV_9_TO_UTF8: /* latin9 to utf-8 conversion */
6138 retval = alloc(len * 3 + 1);
6139 if (retval == NULL)
6140 break;
6141 d = retval;
6142 for (i = 0; i < len; ++i)
6143 {
6144 c = ptr[i];
6145 switch (c)
6146 {
6147 case 0xa4: c = 0x20ac; break; /* euro */
6148 case 0xa6: c = 0x0160; break; /* S hat */
6149 case 0xa8: c = 0x0161; break; /* S -hat */
6150 case 0xb4: c = 0x017d; break; /* Z hat */
6151 case 0xb8: c = 0x017e; break; /* Z -hat */
6152 case 0xbc: c = 0x0152; break; /* OE */
6153 case 0xbd: c = 0x0153; break; /* oe */
6154 case 0xbe: c = 0x0178; break; /* Y */
6155 }
6156 d += utf_char2bytes(c, d);
6157 }
6158 *d = NUL;
6159 if (lenp != NULL)
6160 *lenp = (int)(d - retval);
6161 break;
6162
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 case CONV_TO_LATIN1: /* utf-8 to latin1 conversion */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006164 case CONV_TO_LATIN9: /* utf-8 to latin9 conversion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 retval = alloc(len + 1);
6166 if (retval == NULL)
6167 break;
6168 d = retval;
6169 for (i = 0; i < len; ++i)
6170 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006171 l = utf_ptr2len_len(ptr + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (l == 0)
6173 *d++ = NUL;
6174 else if (l == 1)
6175 {
Bram Moolenaar24397332009-12-02 14:02:39 +00006176 int l_w = utf8len_tab_zero[ptr[i]];
6177
6178 if (l_w == 0)
6179 {
6180 /* Illegal utf-8 byte cannot be converted */
6181 vim_free(retval);
6182 return NULL;
6183 }
6184 if (unconvlenp != NULL && l_w > len - i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 {
6186 /* Incomplete sequence at the end. */
6187 *unconvlenp = len - i;
6188 break;
6189 }
6190 *d++ = ptr[i];
6191 }
6192 else
6193 {
6194 c = utf_ptr2char(ptr + i);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006195 if (vcp->vc_type == CONV_TO_LATIN9)
6196 switch (c)
6197 {
6198 case 0x20ac: c = 0xa4; break; /* euro */
6199 case 0x0160: c = 0xa6; break; /* S hat */
6200 case 0x0161: c = 0xa8; break; /* S -hat */
6201 case 0x017d: c = 0xb4; break; /* Z hat */
6202 case 0x017e: c = 0xb8; break; /* Z -hat */
6203 case 0x0152: c = 0xbc; break; /* OE */
6204 case 0x0153: c = 0xbd; break; /* oe */
6205 case 0x0178: c = 0xbe; break; /* Y */
6206 case 0xa4:
6207 case 0xa6:
6208 case 0xa8:
6209 case 0xb4:
6210 case 0xb8:
6211 case 0xbc:
6212 case 0xbd:
6213 case 0xbe: c = 0x100; break; /* not in latin9 */
6214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 if (!utf_iscomposing(c)) /* skip composing chars */
6216 {
6217 if (c < 0x100)
6218 *d++ = c;
6219 else if (vcp->vc_fail)
6220 {
6221 vim_free(retval);
6222 return NULL;
6223 }
6224 else
6225 {
6226 *d++ = 0xbf;
6227 if (utf_char2cells(c) > 1)
6228 *d++ = '?';
6229 }
6230 }
6231 i += l - 1;
6232 }
6233 }
6234 *d = NUL;
6235 if (lenp != NULL)
6236 *lenp = (int)(d - retval);
6237 break;
6238
Bram Moolenaar56718732006-03-15 22:53:57 +00006239# ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 case CONV_MAC_LATIN1:
6241 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006242 'm', 'l', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 break;
6244
6245 case CONV_LATIN1_MAC:
6246 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006247 'l', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 break;
6249
6250 case CONV_MAC_UTF8:
6251 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006252 'm', 'u', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 break;
6254
6255 case CONV_UTF8_MAC:
6256 retval = mac_string_convert(ptr, len, lenp, vcp->vc_fail,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006257 'u', 'm', unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 break;
6259# endif
6260
6261# ifdef USE_ICONV
6262 case CONV_ICONV: /* conversion with output_conv.vc_fd */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006263 retval = iconv_string(vcp, ptr, len, unconvlenp, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 break;
6265# endif
6266# ifdef WIN3264
6267 case CONV_CODEPAGE: /* codepage -> codepage */
6268 {
6269 int retlen;
6270 int tmp_len;
6271 short_u *tmp;
6272
6273 /* 1. codepage/UTF-8 -> ucs-2. */
6274 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006275 tmp_len = utf8_to_utf16(ptr, len, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 else
Bram Moolenaarffeedec2013-02-13 16:49:58 +01006277 {
6278 tmp_len = MultiByteToWideChar(vcp->vc_cpfrom,
6279 unconvlenp ? MB_ERR_INVALID_CHARS : 0,
6280 ptr, len, 0, 0);
6281 if (tmp_len == 0
6282 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION)
6283 {
6284 if (lenp != NULL)
6285 *lenp = 0;
6286 if (unconvlenp != NULL)
6287 *unconvlenp = len;
6288 retval = alloc(1);
6289 if (retval)
6290 retval[0] = NUL;
6291 return retval;
6292 }
6293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 tmp = (short_u *)alloc(sizeof(short_u) * tmp_len);
6295 if (tmp == NULL)
6296 break;
6297 if (vcp->vc_cpfrom == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006298 utf8_to_utf16(ptr, len, tmp, unconvlenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 else
6300 MultiByteToWideChar(vcp->vc_cpfrom, 0, ptr, len, tmp, tmp_len);
6301
6302 /* 2. ucs-2 -> codepage/UTF-8. */
6303 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006304 retlen = utf16_to_utf8(tmp, tmp_len, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 else
6306 retlen = WideCharToMultiByte(vcp->vc_cpto, 0,
6307 tmp, tmp_len, 0, 0, 0, 0);
6308 retval = alloc(retlen + 1);
6309 if (retval != NULL)
6310 {
6311 if (vcp->vc_cpto == 0)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006312 utf16_to_utf8(tmp, tmp_len, retval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 else
6314 WideCharToMultiByte(vcp->vc_cpto, 0,
6315 tmp, tmp_len, retval, retlen, 0, 0);
6316 retval[retlen] = NUL;
6317 if (lenp != NULL)
6318 *lenp = retlen;
6319 }
6320 vim_free(tmp);
6321 break;
6322 }
6323# endif
6324 }
6325
6326 return retval;
6327}
6328#endif