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