1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Multibyte extensions partly by Sung-Hoon Baek
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.
11 * mbyte.c: Code specifically for handling multi-byte characters.
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:
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.
29 * To make things complicated, up to six composing characters
30 * 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.
39 * "has_mbyte" is set when "enc_dbcs" or "enc_utf8" is non-zero.
41 * If none of these is TRUE, 8-bit bytes are used for a character. The
42 * encoding isn't currently specified (TODO).
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:
54 * keyboard ----->| core |-----> display
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
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'.
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"
83 # ifndef WIN32_LEAN_AND_MEAN
84 # define WIN32_LEAN_AND_MEAN
88 # undef WIN32 /* Some windows.h define WIN32, we don't want that here. */
92 #if (defined(WIN3264) || defined(WIN32UNIX)) && !defined(__MINGW32__)
97 # include <X11/Intrinsic.h>
100 #include <X11/Xlocale.h>
103 #if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && defined(HAVE_GTK2)
104 # include <gdk/gdkkeysyms.h>
106 # include <gdk/gdkwin32.h>
108 # include <gdk/gdkx.h>
116 #if defined(FEAT_UIMFEP) || defined(PROTO)
117 static int uimfep_lastmode
= 1;
118 static void uimfep_set_active
__ARGS((int active
));
119 static int uimfep_get_status
__ARGS((void));
123 /* This has been disabled, because several people reported problems with the
124 * wcwidth() and iswprint() library functions, esp. for Hebrew. */
125 # ifdef __STDC_ISO_10646__
126 # define USE_WCHAR_FUNCTIONS
130 #if defined(FEAT_MBYTE) || defined(PROTO)
132 static int enc_canon_search
__ARGS((char_u
*name
));
133 static int dbcs_char2len
__ARGS((int c
));
134 static int dbcs_char2bytes
__ARGS((int c
, char_u
*buf
));
135 static int dbcs_ptr2len
__ARGS((char_u
*p
));
136 static int dbcs_ptr2len_len
__ARGS((char_u
*p
, int size
));
137 static int utf_ptr2cells_len
__ARGS((char_u
*p
, int size
));
138 static int dbcs_char2cells
__ARGS((int c
));
139 static int dbcs_ptr2cells_len
__ARGS((char_u
*p
, int size
));
140 static int dbcs_ptr2char
__ARGS((char_u
*p
));
143 * Lookup table to quickly get the length in bytes of a UTF-8 character from
144 * the first byte of a UTF-8 string.
145 * Bytes which are illegal when used as the first byte have a 1.
146 * The NUL byte has length 1.
148 static char utf8len_tab
[256] =
150 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
151 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
152 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
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 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,
157 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 * Like utf8len_tab above, but using a zero for illegal lead bytes.
163 static char utf8len_tab_zero
[256] =
165 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
166 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
167 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
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 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,
170 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,
171 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,
172 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 * XIM often causes trouble. Define XIM_DEBUG to get a log of XIM callbacks
177 * in the "xim.log" file.
179 /* #define XIM_DEBUG */
182 xim_log(char *s
, ...)
185 static FILE *fd
= NULL
;
187 if (fd
== (FILE *)-1)
191 fd
= mch_fopen("xim.log", "w");
194 EMSG("Cannot open xim.log");
200 va_start(arglist
, s
);
201 vfprintf(fd
, s
, arglist
);
208 #if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
210 * Canonical encoding names and their properties.
211 * "iso-8859-n" is handled by enc_canonize() directly.
214 { char *name
; int prop
; int codepage
;}
217 #define IDX_LATIN_1 0
218 {"latin1", ENC_8BIT
+ ENC_LATIN1
, 1252},
220 {"iso-8859-2", ENC_8BIT
, 0},
222 {"iso-8859-3", ENC_8BIT
, 0},
224 {"iso-8859-4", ENC_8BIT
, 0},
226 {"iso-8859-5", ENC_8BIT
, 0},
228 {"iso-8859-6", ENC_8BIT
, 0},
230 {"iso-8859-7", ENC_8BIT
, 0},
232 {"iso-8859-8", ENC_8BIT
, 0},
234 {"iso-8859-9", ENC_8BIT
, 0},
236 {"iso-8859-10", ENC_8BIT
, 0},
237 #define IDX_ISO_11 10
238 {"iso-8859-11", ENC_8BIT
, 0},
239 #define IDX_ISO_13 11
240 {"iso-8859-13", ENC_8BIT
, 0},
241 #define IDX_ISO_14 12
242 {"iso-8859-14", ENC_8BIT
, 0},
243 #define IDX_ISO_15 13
244 {"iso-8859-15", ENC_8BIT
+ ENC_LATIN9
, 0},
245 #define IDX_KOI8_R 14
246 {"koi8-r", ENC_8BIT
, 0},
247 #define IDX_KOI8_U 15
248 {"koi8-u", ENC_8BIT
, 0},
250 {"utf-8", ENC_UNICODE
, 0},
252 {"ucs-2", ENC_UNICODE
+ ENC_ENDIAN_B
+ ENC_2BYTE
, 0},
253 #define IDX_UCS2LE 18
254 {"ucs-2le", ENC_UNICODE
+ ENC_ENDIAN_L
+ ENC_2BYTE
, 0},
256 {"utf-16", ENC_UNICODE
+ ENC_ENDIAN_B
+ ENC_2WORD
, 0},
257 #define IDX_UTF16LE 20
258 {"utf-16le", ENC_UNICODE
+ ENC_ENDIAN_L
+ ENC_2WORD
, 0},
260 {"ucs-4", ENC_UNICODE
+ ENC_ENDIAN_B
+ ENC_4BYTE
, 0},
261 #define IDX_UCS4LE 22
262 {"ucs-4le", ENC_UNICODE
+ ENC_ENDIAN_L
+ ENC_4BYTE
, 0},
264 /* For debugging DBCS encoding on Unix. */
266 {"debug", ENC_DBCS
, DBCS_DEBUG
},
267 #define IDX_EUC_JP 24
268 {"euc-jp", ENC_DBCS
, DBCS_JPNU
},
270 {"sjis", ENC_DBCS
, DBCS_JPN
},
271 #define IDX_EUC_KR 26
272 {"euc-kr", ENC_DBCS
, DBCS_KORU
},
273 #define IDX_EUC_CN 27
274 {"euc-cn", ENC_DBCS
, DBCS_CHSU
},
275 #define IDX_EUC_TW 28
276 {"euc-tw", ENC_DBCS
, DBCS_CHTU
},
278 {"big5", ENC_DBCS
, DBCS_CHT
},
280 /* MS-DOS and MS-Windows codepages are included here, so that they can be
281 * used on Unix too. Most of them are similar to ISO-8859 encodings, but
282 * not exactly the same. */
284 {"cp437", ENC_8BIT
, 437}, /* like iso-8859-1 */
286 {"cp737", ENC_8BIT
, 737}, /* like iso-8859-7 */
288 {"cp775", ENC_8BIT
, 775}, /* Baltic */
290 {"cp850", ENC_8BIT
, 850}, /* like iso-8859-4 */
292 {"cp852", ENC_8BIT
, 852}, /* like iso-8859-1 */
294 {"cp855", ENC_8BIT
, 855}, /* like iso-8859-2 */
296 {"cp857", ENC_8BIT
, 857}, /* like iso-8859-5 */
298 {"cp860", ENC_8BIT
, 860}, /* like iso-8859-9 */
300 {"cp861", ENC_8BIT
, 861}, /* like iso-8859-1 */
302 {"cp862", ENC_8BIT
, 862}, /* like iso-8859-1 */
304 {"cp863", ENC_8BIT
, 863}, /* like iso-8859-8 */
306 {"cp865", ENC_8BIT
, 865}, /* like iso-8859-1 */
308 {"cp866", ENC_8BIT
, 866}, /* like iso-8859-5 */
310 {"cp869", ENC_8BIT
, 869}, /* like iso-8859-7 */
312 {"cp874", ENC_8BIT
, 874}, /* Thai */
314 {"cp932", ENC_DBCS
, DBCS_JPN
},
316 {"cp936", ENC_DBCS
, DBCS_CHS
},
318 {"cp949", ENC_DBCS
, DBCS_KOR
},
320 {"cp950", ENC_DBCS
, DBCS_CHT
},
321 #define IDX_CP1250 49
322 {"cp1250", ENC_8BIT
, 1250}, /* Czech, Polish, etc. */
323 #define IDX_CP1251 50
324 {"cp1251", ENC_8BIT
, 1251}, /* Cyrillic */
325 /* cp1252 is considered to be equal to latin1 */
326 #define IDX_CP1253 51
327 {"cp1253", ENC_8BIT
, 1253}, /* Greek */
328 #define IDX_CP1254 52
329 {"cp1254", ENC_8BIT
, 1254}, /* Turkish */
330 #define IDX_CP1255 53
331 {"cp1255", ENC_8BIT
, 1255}, /* Hebrew */
332 #define IDX_CP1256 54
333 {"cp1256", ENC_8BIT
, 1256}, /* Arabic */
334 #define IDX_CP1257 55
335 {"cp1257", ENC_8BIT
, 1257}, /* Baltic */
336 #define IDX_CP1258 56
337 {"cp1258", ENC_8BIT
, 1258}, /* Vietnamese */
339 #define IDX_MACROMAN 57
340 {"macroman", ENC_8BIT
+ ENC_MACROMAN
, 0}, /* Mac OS */
341 #define IDX_DECMCS 58
342 {"dec-mcs", ENC_8BIT
, 0}, /* DEC MCS */
343 #define IDX_HPROMAN8 59
344 {"hp-roman8", ENC_8BIT
, 0}, /* HP Roman8 */
349 * Aliases for encoding names.
352 { char *name
; int canon
;}
355 {"ansi", IDX_LATIN_1
},
356 {"iso-8859-1", IDX_LATIN_1
},
357 {"latin2", IDX_ISO_2
},
358 {"latin3", IDX_ISO_3
},
359 {"latin4", IDX_ISO_4
},
360 {"cyrillic", IDX_ISO_5
},
361 {"arabic", IDX_ISO_6
},
362 {"greek", IDX_ISO_7
},
364 {"hebrew", IDX_CP1255
},
366 {"hebrew", IDX_ISO_8
},
368 {"latin5", IDX_ISO_9
},
369 {"turkish", IDX_ISO_9
}, /* ? */
370 {"latin6", IDX_ISO_10
},
371 {"nordic", IDX_ISO_10
}, /* ? */
372 {"thai", IDX_ISO_11
}, /* ? */
373 {"latin7", IDX_ISO_13
},
374 {"latin8", IDX_ISO_14
},
375 {"latin9", IDX_ISO_15
},
377 {"unicode", IDX_UCS2
},
379 {"ucs2be", IDX_UCS2
},
380 {"ucs-2be", IDX_UCS2
},
381 {"ucs2le", IDX_UCS2LE
},
382 {"utf16", IDX_UTF16
},
383 {"utf16be", IDX_UTF16
},
384 {"utf-16be", IDX_UTF16
},
385 {"utf16le", IDX_UTF16LE
},
387 {"ucs4be", IDX_UCS4
},
388 {"ucs-4be", IDX_UCS4
},
389 {"ucs4le", IDX_UCS4LE
},
391 {"utf-32", IDX_UCS4
},
392 {"utf32be", IDX_UCS4
},
393 {"utf-32be", IDX_UCS4
},
394 {"utf32le", IDX_UCS4LE
},
395 {"utf-32le", IDX_UCS4LE
},
401 {"eucjp", IDX_EUC_JP
},
402 {"unix-jis", IDX_EUC_JP
},
403 {"ujis", IDX_EUC_JP
},
404 {"shift-jis", IDX_SJIS
},
405 {"euckr", IDX_EUC_KR
},
406 {"5601", IDX_EUC_KR
}, /* Sun: KS C 5601 */
407 {"euccn", IDX_EUC_CN
},
408 {"gb2312", IDX_EUC_CN
},
409 {"euctw", IDX_EUC_TW
},
410 #if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
411 {"japan", IDX_CP932
},
412 {"korea", IDX_CP949
},
414 {"chinese", IDX_CP936
},
415 {"taiwan", IDX_CP950
},
418 {"japan", IDX_EUC_JP
},
419 {"korea", IDX_EUC_KR
},
421 {"chinese", IDX_EUC_CN
},
422 {"taiwan", IDX_EUC_TW
},
426 {"mac", IDX_MACROMAN
},
427 {"mac-roman", IDX_MACROMAN
},
432 # define CP_UTF8 65001 /* magic number from winnls.h */
436 * Find encoding "name" in the list of canonical encoding names.
437 * Returns -1 if not found.
440 enc_canon_search(name
)
445 for (i
= 0; i
< IDX_COUNT
; ++i
)
446 if (STRCMP(name
, enc_canon_table
[i
].name
) == 0)
453 #if defined(FEAT_MBYTE) || defined(PROTO)
456 * Find canonical encoding "name" in the list and return its properties.
457 * Returns 0 if not found.
460 enc_canon_props(name
)
465 i
= enc_canon_search(name
);
467 return enc_canon_table
[i
].prop
;
469 if (name
[0] == 'c' && name
[1] == 'p' && VIM_ISDIGIT(name
[2]))
473 /* Get info on this codepage to find out what it is. */
474 if (GetCPInfo(atoi(name
+ 2), &cpinfo
) != 0)
476 if (cpinfo
.MaxCharSize
== 1) /* some single-byte encoding */
478 if (cpinfo
.MaxCharSize
== 2
479 && (cpinfo
.LeadByte
[0] != 0 || cpinfo
.LeadByte
[1] != 0))
480 /* must be a DBCS encoding */
486 if (STRNCMP(name
, "2byte-", 6) == 0)
488 if (STRNCMP(name
, "8bit-", 5) == 0 || STRNCMP(name
, "iso-8859-", 9) == 0)
494 * Set up for using multi-byte characters.
495 * Called in three cases:
496 * - by main() to initialize (p_enc == NULL)
497 * - by set_init_1() after 'encoding' was set to its default.
498 * - by do_set() when 'encoding' has been set.
499 * p_enc must have been passed through enc_canonize() already.
500 * Sets the "enc_unicode", "enc_utf8", "enc_dbcs" and "has_mbyte" flags.
501 * Fills mb_bytelen_tab[] and returns NULL when there are no problems.
502 * When there is something wrong: Returns an error message and doesn't change
511 int enc_dbcs_new
= 0;
512 #if defined(USE_ICONV) && !defined(WIN3264) && !defined(WIN32UNIX) \
514 # define LEN_FROM_CONV
521 /* Just starting up: set the whole table to one's. */
522 for (i
= 0; i
< 256; ++i
)
523 mb_bytelen_tab
[i
] = 1;
524 input_conv
.vc_type
= CONV_NONE
;
525 input_conv
.vc_factor
= 1;
526 output_conv
.vc_type
= CONV_NONE
;
531 if (p_enc
[0] == 'c' && p_enc
[1] == 'p' && VIM_ISDIGIT(p_enc
[2]))
535 /* Get info on this codepage to find out what it is. */
536 if (GetCPInfo(atoi(p_enc
+ 2), &cpinfo
) != 0)
538 if (cpinfo
.MaxCharSize
== 1)
540 /* some single-byte encoding */
544 else if (cpinfo
.MaxCharSize
== 2
545 && (cpinfo
.LeadByte
[0] != 0 || cpinfo
.LeadByte
[1] != 0))
547 /* must be a DBCS encoding, check below */
548 enc_dbcs_new
= atoi(p_enc
+ 2);
551 goto codepage_invalid
;
553 else if (GetLastError() == ERROR_INVALID_PARAMETER
)
556 return (char_u
*)N_("E543: Not a valid codepage");
560 else if (STRNCMP(p_enc
, "8bit-", 5) == 0
561 || STRNCMP(p_enc
, "iso-8859-", 9) == 0)
563 /* Accept any "8bit-" or "iso-8859-" name. */
567 else if (STRNCMP(p_enc
, "2byte-", 6) == 0)
570 /* Windows: accept only valid codepage numbers, check below. */
571 if (p_enc
[6] != 'c' || p_enc
[7] != 'p'
572 || (enc_dbcs_new
= atoi(p_enc
+ 8)) == 0)
575 /* Unix: accept any "2byte-" name, assume current locale. */
576 enc_dbcs_new
= DBCS_2BYTE
;
579 else if ((idx
= enc_canon_search(p_enc
)) >= 0)
581 i
= enc_canon_table
[idx
].prop
;
586 if (i
& (ENC_2BYTE
| ENC_2WORD
))
588 else if (i
& ENC_4BYTE
)
593 else if (i
& ENC_DBCS
)
595 /* 2byte, handle below */
596 enc_dbcs_new
= enc_canon_table
[idx
].codepage
;
605 else /* Don't know what encoding this is, reject it. */
608 if (enc_dbcs_new
!= 0)
611 /* Check if the DBCS code page is OK. */
612 if (!IsValidCodePage(enc_dbcs_new
))
613 goto codepage_invalid
;
618 enc_dbcs
= enc_dbcs_new
;
619 has_mbyte
= (enc_dbcs
!= 0 || enc_utf8
);
622 enc_codepage
= encname2codepage(p_enc
);
623 enc_latin9
= (STRCMP(p_enc
, "iso-8859-15") == 0);
626 /* Detect an encoding that uses latin1 characters. */
627 enc_latin1like
= (enc_utf8
|| STRCMP(p_enc
, "latin1") == 0
628 || STRCMP(p_enc
, "iso-8859-15") == 0);
631 * Set the function pointers.
635 mb_ptr2len
= utfc_ptr2len
;
636 mb_ptr2len_len
= utfc_ptr2len_len
;
637 mb_char2len
= utf_char2len
;
638 mb_char2bytes
= utf_char2bytes
;
639 mb_ptr2cells
= utf_ptr2cells
;
640 mb_ptr2cells_len
= utf_ptr2cells_len
;
641 mb_char2cells
= utf_char2cells
;
642 mb_off2cells
= utf_off2cells
;
643 mb_ptr2char
= utf_ptr2char
;
644 mb_head_off
= utf_head_off
;
646 else if (enc_dbcs
!= 0)
648 mb_ptr2len
= dbcs_ptr2len
;
649 mb_ptr2len_len
= dbcs_ptr2len_len
;
650 mb_char2len
= dbcs_char2len
;
651 mb_char2bytes
= dbcs_char2bytes
;
652 mb_ptr2cells
= dbcs_ptr2cells
;
653 mb_ptr2cells_len
= dbcs_ptr2cells_len
;
654 mb_char2cells
= dbcs_char2cells
;
655 mb_off2cells
= dbcs_off2cells
;
656 mb_ptr2char
= dbcs_ptr2char
;
657 mb_head_off
= dbcs_head_off
;
661 mb_ptr2len
= latin_ptr2len
;
662 mb_ptr2len_len
= latin_ptr2len_len
;
663 mb_char2len
= latin_char2len
;
664 mb_char2bytes
= latin_char2bytes
;
665 mb_ptr2cells
= latin_ptr2cells
;
666 mb_ptr2cells_len
= latin_ptr2cells_len
;
667 mb_char2cells
= latin_char2cells
;
668 mb_off2cells
= latin_off2cells
;
669 mb_ptr2char
= latin_ptr2char
;
670 mb_head_off
= latin_head_off
;
674 * Fill the mb_bytelen_tab[] for MB_BYTE2LEN().
677 /* When 'encoding' is different from the current locale mblen() won't
678 * work. Use conversion to "utf-8" instead. */
679 vimconv
.vc_type
= CONV_NONE
;
683 if (p
== NULL
|| STRCMP(p
, p_enc
) != 0)
685 convert_setup(&vimconv
, p_enc
, (char_u
*)"utf-8");
686 vimconv
.vc_fail
= TRUE
;
692 for (i
= 0; i
< 256; ++i
)
694 /* Our own function to reliably check the length of UTF-8 characters,
695 * independent of mblen(). */
698 else if (enc_dbcs
== 0)
702 #if defined(WIN3264) || defined(WIN32UNIX)
703 /* enc_dbcs is set by setting 'fileencoding'. It becomes a Windows
704 * CodePage identifier, which we can pass directly in to Windows
706 n
= IsDBCSLeadByteEx(enc_dbcs
, (BYTE
)i
) ? 2 : 1;
708 # if defined(MACOS) || defined(__amigaos4__)
710 * if mblen() is not available, character which MSB is turned on
711 * are treated as leading byte character. (note : This assumption
712 * is not always true.)
714 n
= (i
& 0x80) ? 2 : 1;
716 char buf
[MB_MAXBYTES
];
719 # define mblen _Xmblen
722 if (i
== NUL
) /* just in case mblen() can't handle "" */
729 if (vimconv
.vc_type
!= CONV_NONE
)
732 * string_convert() should fail when converting the first
733 * byte of a double-byte character.
735 p
= string_convert(&vimconv
, (char_u
*)buf
, NULL
);
748 * mblen() should return -1 for invalid (means the leading
749 * multibyte) character. However there are some platforms
750 * where mblen() returns 0 for invalid character.
751 * Therefore, following condition includes 0.
753 ignored
= mblen(NULL
, 0); /* First reset the state. */
754 if (mblen(buf
, (size_t)1) <= 0)
764 mb_bytelen_tab
[i
] = n
;
768 convert_setup(&vimconv
, NULL
, NULL
);
771 /* The cell width depends on the type of multi-byte characters. */
772 (void)init_chartab();
774 /* When enc_utf8 is set or reset, (de)allocate ScreenLinesUC[] */
777 /* When using Unicode, set default for 'fileencodings'. */
778 if (enc_utf8
&& !option_was_set((char_u
*)"fencs"))
779 set_string_option_direct((char_u
*)"fencs", -1,
780 (char_u
*)"ucs-bom,utf-8,default,latin1", OPT_FREE
, 0);
782 #if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
783 /* GNU gettext 0.10.37 supports this feature: set the codeset used for
784 * translated messages independently from the current locale. */
785 (void)bind_textdomain_codeset(VIMPACKAGE
,
786 enc_utf8
? "utf-8" : (char *)p_enc
);
790 /* When changing 'encoding' while starting up, then convert the command
791 * line arguments from the active codepage to 'encoding'. */
797 /* Fire an autocommand to let people do custom font setup. This must be
798 * after Vim has been setup for the new encoding. */
799 apply_autocmds(EVENT_ENCODINGCHANGED
, NULL
, (char_u
*)"", FALSE
, curbuf
);
803 /* Need to reload spell dictionaries */
811 * Return the size of the BOM for the current buffer:
813 * 2 - UCS-2 or UTF-16 BOM
822 if (curbuf
->b_p_bomb
&& !curbuf
->b_p_bin
)
824 if (*curbuf
->b_p_fenc
== NUL
)
828 if (enc_unicode
!= 0)
834 else if (STRCMP(curbuf
->b_p_fenc
, "utf-8") == 0)
836 else if (STRNCMP(curbuf
->b_p_fenc
, "ucs-2", 5) == 0
837 || STRNCMP(curbuf
->b_p_fenc
, "utf-16", 6) == 0)
839 else if (STRNCMP(curbuf
->b_p_fenc
, "ucs-4", 5) == 0)
846 * Get class of pointer:
849 * 2 for an (ASCII) word character
850 * >2 for other word characters
856 if (MB_BYTE2LEN(p
[0]) == 1)
858 if (p
[0] == NUL
|| vim_iswhite(p
[0]))
860 if (vim_iswordc(p
[0]))
864 if (enc_dbcs
!= 0 && p
[0] != NUL
&& p
[1] != NUL
)
865 return dbcs_class(p
[0], p
[1]);
867 return utf_class(utf_ptr2char(p
));
872 * Get class of a double-byte character. This always returns 3 or bigger.
873 * TODO: Should return 1 for punctuation.
876 dbcs_class(lead
, trail
)
882 /* please add classfy routine for your language in here */
884 case DBCS_JPNU
: /* ? */
887 /* JIS code classification */
888 unsigned char lb
= lead
;
889 unsigned char tb
= trail
;
891 /* convert process code to JIS */
892 # if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
893 /* process code is SJIS */
895 lb
= (lb
- 0x81) * 2 + 0x21;
897 lb
= (lb
- 0xc1) * 2 + 0x21;
909 * XXX: Code page identification can not use with all
910 * system! So, some other encoding information
912 * In japanese: SJIS,EUC,UNICODE,(JIS)
913 * Note that JIS-code system don't use as
914 * process code in most system because it uses
915 * escape sequences(JIS is context depend encoding).
917 /* assume process code is JAPANESE-EUC */
922 switch (lb
<< 8 | tb
)
924 case 0x2121: /* ZENKAKU space */
926 case 0x2122: /* KU-TEN (Japanese comma) */
927 case 0x2123: /* TOU-TEN (Japanese period) */
928 case 0x2124: /* ZENKAKU comma */
929 case 0x2125: /* ZENKAKU period */
931 case 0x213c: /* prolongedsound handled as KATAKANA */
934 /* sieved by KU code */
939 /* special symbols */
965 case DBCS_KORU
: /* ? */
968 /* KS code classification */
969 unsigned char c1
= lead
;
970 unsigned char c2
= trail
;
976 * 23 : Alpha-numeric/Roman Letter (Full width)
977 * 24 : Hangul Letter(Alphabet)
978 * 25 : Roman Numeral/Greek Letter
981 * 28 : Circled/Parenthesized Letter
982 * 29 : Hirigana/Katakana
983 * 30 : Cyrillic Letter
986 if (c1
>= 0xB0 && c1
<= 0xC8)
989 #if defined(WIN3264) || defined(WIN32UNIX)
990 else if (c1
<= 0xA0 || c2
<= 0xA0)
991 /* Extended Hangul Region : MS UHC(Unified Hangul Code) */
992 /* c1: 0x81-0xA0 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xFE
993 * c1: 0xA1-0xC6 with c2: 0x41-0x5A, 0x61-0x7A, 0x81-0xA0
998 else if (c1
>= 0xCA && c1
<= 0xFD)
1011 /* Hangul Letter(Alphabet) */
1014 /* Roman Numeral/Greek Letter */
1025 return 25; /* Roman Letter */
1026 else if (c2
>= 0xF6)
1027 return 22; /* Symbols */
1029 /* Circled/Parenthesized Letter */
1033 /* Hirigana/Katakana */
1036 /* Cyrillic Letter */
1047 * mb_char2len() function pointer.
1048 * Return length in bytes of character "c".
1049 * Returns 1 for a single-byte character.
1068 * mb_char2bytes() function pointer.
1069 * Convert a character to its bytes.
1070 * Returns the length in bytes.
1073 latin_char2bytes(c
, buf
)
1082 dbcs_char2bytes(c
, buf
)
1088 buf
[0] = (unsigned)c
>> 8;
1090 /* Never use a NUL byte, it causes lots of trouble. It's an invalid
1091 * character anyway. */
1101 * mb_ptr2len() function pointer.
1102 * Get byte length of character at "*p" but stop at a NUL.
1103 * For UTF-8 this includes following composing characters.
1104 * Returns 0 when *p is NUL.
1110 return MB_BYTE2LEN(*p
);
1119 /* Check if second byte is not missing. */
1120 len
= MB_BYTE2LEN(*p
);
1121 if (len
== 2 && p
[1] == NUL
)
1127 * mb_ptr2len_len() function pointer.
1128 * Like mb_ptr2len(), but limit to read "size" bytes.
1129 * Returns 0 for an empty string.
1130 * Returns 1 for an illegal char or an incomplete byte sequence.
1133 latin_ptr2len_len(p
, size
)
1137 if (size
< 1 || *p
== NUL
)
1143 dbcs_ptr2len_len(p
, size
)
1149 if (size
< 1 || *p
== NUL
)
1153 /* Check that second byte is not missing. */
1154 len
= MB_BYTE2LEN(*p
);
1155 if (len
== 2 && p
[1] == NUL
)
1165 static int intable
__ARGS((struct interval
*table
, size_t size
, int c
));
1168 * Return TRUE if "c" is in "table[size / sizeof(struct interval)]".
1171 intable(table
, size
, c
)
1172 struct interval
*table
;
1178 /* first quick check for Latin1 etc. characters */
1179 if (c
< table
[0].first
)
1182 /* binary search in table */
1184 top
= (int)(size
/ sizeof(struct interval
) - 1);
1187 mid
= (bot
+ top
) / 2;
1188 if (table
[mid
].last
< c
)
1190 else if (table
[mid
].first
> c
)
1199 * For UTF-8 character "c" return 2 for a double-width character, 1 for others.
1200 * Returns 4 or 6 for an unprintable character.
1201 * Is only correct for characters >= 0x80.
1202 * When p_ambw is "double", return 2 for a character with East Asian Width
1203 * class 'A'(mbiguous).
1209 /* Sorted list of non-overlapping intervals of East Asian double width
1210 * characters, generated with ../runtime/tools/unicode.vim. */
1211 static struct interval doublewidth
[] =
1252 /* Sorted list of non-overlapping intervals of East Asian Ambiguous
1253 * characters, generated with ../runtime/tools/unicode.vim. */
1254 static struct interval ambiguous
[] =
1436 {0x100000, 0x10fffd}
1439 #ifdef USE_AMBIWIDTH_AUTO
1440 if (gui
.in_use
&& *p_ambw
== 'a')
1444 /* This is required by screen.c implicitly. */
1447 if ((cell
= gui_mch_get_charwidth(c
)) > 0)
1454 #ifdef USE_WCHAR_FUNCTIONS
1456 * Assume the library function wcwidth() works better than our own
1457 * stuff. It should return 1 for ambiguous width chars!
1462 return 6; /* unprintable, displays <xxxx> */
1466 if (!utf_printable(c
))
1467 return 6; /* unprintable, displays <xxxx> */
1468 if (intable(doublewidth
, sizeof(doublewidth
), c
))
1473 /* Characters below 0x100 are influenced by 'isprint' option */
1474 else if (c
>= 0x80 && !vim_isprintc(c
))
1475 return 4; /* unprintable, displays <xx> */
1477 if (c
>= 0x80 && *p_ambw
== 'd' && intable(ambiguous
, sizeof(ambiguous
), c
))
1484 * mb_ptr2cells() function pointer.
1485 * Return the number of display cells character at "*p" occupies.
1486 * This doesn't take care of unprintable characters, use ptr2cells() for that.
1501 /* Need to convert to a wide character. */
1504 c
= utf_ptr2char(p
);
1505 /* An illegal byte is displayed as <xx>. */
1506 if (utf_ptr2len(p
) == 1 || c
== NUL
)
1508 /* If the char is ASCII it must be an overlong sequence. */
1510 return char2cells(c
);
1511 return utf_char2cells(c
);
1520 /* Number of cells is equal to number of bytes, except for euc-jp when
1521 * the first byte is 0x8e. */
1522 if (enc_dbcs
== DBCS_JPNU
&& *p
== 0x8e)
1524 return MB_BYTE2LEN(*p
);
1528 * mb_ptr2cells_len() function pointer.
1529 * Like mb_ptr2cells(), but limit string length to "size".
1530 * For an empty string or truncated character returns 1.
1533 latin_ptr2cells_len(p
, size
)
1541 utf_ptr2cells_len(p
, size
)
1547 /* Need to convert to a wide character. */
1548 if (size
> 0 && *p
>= 0x80)
1550 if (utf_ptr2len_len(p
, size
) < utf8len_tab
[*p
])
1551 return 1; /* truncated */
1552 c
= utf_ptr2char(p
);
1553 /* An illegal byte is displayed as <xx>. */
1554 if (utf_ptr2len(p
) == 1 || c
== NUL
)
1556 /* If the char is ASCII it must be an overlong sequence. */
1558 return char2cells(c
);
1559 return utf_char2cells(c
);
1565 dbcs_ptr2cells_len(p
, size
)
1569 /* Number of cells is equal to number of bytes, except for euc-jp when
1570 * the first byte is 0x8e. */
1571 if (size
<= 1 || (enc_dbcs
== DBCS_JPNU
&& *p
== 0x8e))
1573 return MB_BYTE2LEN(*p
);
1577 * mb_char2cells() function pointer.
1578 * Return the number of display cells character "c" occupies.
1579 * Only takes care of multi-byte chars, not "^C" and such.
1592 /* Number of cells is equal to number of bytes, except for euc-jp when
1593 * the first byte is 0x8e. */
1594 if (enc_dbcs
== DBCS_JPNU
&& ((unsigned)c
>> 8) == 0x8e)
1596 /* use the first byte */
1597 return MB_BYTE2LEN((unsigned)c
>> 8);
1601 * mb_off2cells() function pointer.
1602 * Return number of display cells for char at ScreenLines[off].
1603 * We make sure that the offset used is less than "max_off".
1606 latin_off2cells(off
, max_off
)
1607 unsigned off UNUSED
;
1608 unsigned max_off UNUSED
;
1614 dbcs_off2cells(off
, max_off
)
1618 /* never check beyond end of the line */
1622 /* Number of cells is equal to number of bytes, except for euc-jp when
1623 * the first byte is 0x8e. */
1624 if (enc_dbcs
== DBCS_JPNU
&& ScreenLines
[off
] == 0x8e)
1626 return MB_BYTE2LEN(ScreenLines
[off
]);
1630 utf_off2cells(off
, max_off
)
1634 return (off
+ 1 < max_off
&& ScreenLines
[off
+ 1] == 0) ? 2 : 1;
1638 * mb_ptr2char() function pointer.
1639 * Convert a byte sequence into a character.
1652 if (MB_BYTE2LEN(*p
) > 1 && p
[1] != NUL
)
1653 return (p
[0] << 8) + p
[1];
1658 * Convert a UTF-8 byte sequence to a wide character.
1659 * If the sequence is illegal or truncated by a NUL the first byte is
1661 * Does not include composing characters, of course.
1669 if (p
[0] < 0x80) /* be quick for ASCII */
1672 len
= utf8len_tab_zero
[p
[0]];
1673 if (len
> 1 && (p
[1] & 0xc0) == 0x80)
1676 return ((p
[0] & 0x1f) << 6) + (p
[1] & 0x3f);
1677 if ((p
[2] & 0xc0) == 0x80)
1680 return ((p
[0] & 0x0f) << 12) + ((p
[1] & 0x3f) << 6)
1682 if ((p
[3] & 0xc0) == 0x80)
1685 return ((p
[0] & 0x07) << 18) + ((p
[1] & 0x3f) << 12)
1686 + ((p
[2] & 0x3f) << 6) + (p
[3] & 0x3f);
1687 if ((p
[4] & 0xc0) == 0x80)
1690 return ((p
[0] & 0x03) << 24) + ((p
[1] & 0x3f) << 18)
1691 + ((p
[2] & 0x3f) << 12) + ((p
[3] & 0x3f) << 6)
1693 if ((p
[5] & 0xc0) == 0x80 && len
== 6)
1694 return ((p
[0] & 0x01) << 30) + ((p
[1] & 0x3f) << 24)
1695 + ((p
[2] & 0x3f) << 18) + ((p
[3] & 0x3f) << 12)
1696 + ((p
[4] & 0x3f) << 6) + (p
[5] & 0x3f);
1701 /* Illegal value, just return the first byte */
1706 * Get character at **pp and advance *pp to the next character.
1707 * Note: composing characters are skipped!
1715 c
= (*mb_ptr2char
)(*pp
);
1716 *pp
+= (*mb_ptr2len
)(*pp
);
1721 * Get character at **pp and advance *pp to the next character.
1722 * Note: composing characters are returned as separate characters.
1725 mb_cptr2char_adv(pp
)
1730 c
= (*mb_ptr2char
)(*pp
);
1732 *pp
+= utf_ptr2len(*pp
);
1734 *pp
+= (*mb_ptr2len
)(*pp
);
1738 #if defined(FEAT_ARABIC) || defined(PROTO)
1740 * Check whether we are dealing with Arabic combining characters.
1741 * Note: these are NOT really composing characters!
1744 arabic_combine(one
, two
)
1745 int one
; /* first character */
1746 int two
; /* character just after "one" */
1749 return arabic_maycombine(two
);
1754 * Check whether we are dealing with a character that could be regarded as an
1755 * Arabic combining character, need to check the character before this.
1758 arabic_maycombine(two
)
1761 if (p_arshape
&& !p_tbidi
)
1762 return (two
== a_ALEF_MADDA
1763 || two
== a_ALEF_HAMZA_ABOVE
1764 || two
== a_ALEF_HAMZA_BELOW
1770 * Check if the character pointed to by "p2" is a composing character when it
1771 * comes after "p1". For Arabic sometimes "ab" is replaced with "c", which
1772 * behaves like a composing character.
1775 utf_composinglike(p1
, p2
)
1781 c2
= utf_ptr2char(p2
);
1782 if (utf_iscomposing(c2
))
1784 if (!arabic_maycombine(c2
))
1786 return arabic_combine(utf_ptr2char(p1
), c2
);
1791 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
1792 * composing characters.
1795 utfc_ptr2char(p
, pcc
)
1797 int *pcc
; /* return: composing chars, last one is 0 */
1804 c
= utf_ptr2char(p
);
1805 len
= utf_ptr2len(p
);
1807 /* Only accept a composing char when the first char isn't illegal. */
1808 if ((len
> 1 || *p
< 0x80)
1810 && UTF_COMPOSINGLIKE(p
, p
+ len
))
1812 cc
= utf_ptr2char(p
+ len
);
1818 len
+= utf_ptr2len(p
+ len
);
1819 if (p
[len
] < 0x80 || !utf_iscomposing(cc
= utf_ptr2char(p
+ len
)))
1824 if (i
< MAX_MCO
) /* last composing char must be 0 */
1831 * Convert a UTF-8 byte string to a wide character. Also get up to MAX_MCO
1832 * composing characters. Use no more than p[maxlen].
1835 utfc_ptr2char_len(p
, pcc
, maxlen
)
1837 int *pcc
; /* return: composing chars, last one is 0 */
1845 c
= utf_ptr2char(p
);
1846 len
= utf_ptr2len_len(p
, maxlen
);
1847 /* Only accept a composing char when the first char isn't illegal. */
1848 if ((len
> 1 || *p
< 0x80)
1851 && UTF_COMPOSINGLIKE(p
, p
+ len
))
1853 cc
= utf_ptr2char(p
+ len
);
1859 len
+= utf_ptr2len_len(p
+ len
, maxlen
- len
);
1862 || !utf_iscomposing(cc
= utf_ptr2char(p
+ len
)))
1867 if (i
< MAX_MCO
) /* last composing char must be 0 */
1874 * Convert the character at screen position "off" to a sequence of bytes.
1875 * Includes the composing characters.
1876 * "buf" must at least have the length MB_MAXBYTES.
1877 * Returns the produced number of bytes.
1880 utfc_char2bytes(off
, buf
)
1887 len
= utf_char2bytes(ScreenLinesUC
[off
], buf
);
1888 for (i
= 0; i
< Screen_mco
; ++i
)
1890 if (ScreenLinesC
[i
][off
] == 0)
1892 len
+= utf_char2bytes(ScreenLinesC
[i
][off
], buf
+ len
);
1898 * Get the length of a UTF-8 byte sequence, not including any following
1899 * composing characters.
1901 * Returns 1 for an illegal byte sequence.
1912 len
= utf8len_tab
[*p
];
1913 for (i
= 1; i
< len
; ++i
)
1914 if ((p
[i
] & 0xc0) != 0x80)
1920 * Return length of UTF-8 character, obtained from the first byte.
1921 * "b" must be between 0 and 255!
1922 * Returns 1 for an invalid first byte value.
1928 return utf8len_tab
[b
];
1932 * Get the length of UTF-8 byte sequence "p[size]". Does not include any
1933 * following composing characters.
1935 * Returns 1 for an illegal byte sequence (also in incomplete byte seq.).
1936 * Returns number > "size" for an incomplete byte sequence.
1937 * Never returns zero.
1940 utf_ptr2len_len(p
, size
)
1948 len
= utf8len_tab
[*p
];
1950 return 1; /* NUL, ascii or illegal lead byte */
1952 m
= size
; /* incomplete byte sequence. */
1955 for (i
= 1; i
< m
; ++i
)
1956 if ((p
[i
] & 0xc0) != 0x80)
1962 * Return the number of bytes the UTF-8 encoding of the character at "p" takes.
1963 * This includes following composing characters.
1977 if (b0
< 0x80 && p
[1] < 0x80) /* be quick for ASCII */
1980 /* Skip over first UTF-8 char, stopping at a NUL byte. */
1981 len
= utf_ptr2len(p
);
1983 /* Check for illegal byte. */
1984 if (len
== 1 && b0
>= 0x80)
1988 * Check for composing characters. We can handle only the first six, but
1989 * skip all of them (otherwise the cursor would get stuck).
1996 if (p
[len
] < 0x80 || !UTF_COMPOSINGLIKE(p
+ prevlen
, p
+ len
))
1999 /* Skip over composing char */
2003 len
+= utf_ptr2len(p
+ len
);
2008 * Return the number of bytes the UTF-8 encoding of the character at "p[size]"
2009 * takes. This includes following composing characters.
2010 * Returns 0 for an empty string.
2011 * Returns 1 for an illegal char or an incomplete byte sequence.
2014 utfc_ptr2len_len(p
, size
)
2023 if (size
< 1 || *p
== NUL
)
2025 if (p
[0] < 0x80 && (size
== 1 || p
[1] < 0x80)) /* be quick for ASCII */
2028 /* Skip over first UTF-8 char, stopping at a NUL byte. */
2029 len
= utf_ptr2len_len(p
, size
);
2031 /* Check for illegal byte and incomplete byte sequence. */
2032 if ((len
== 1 && p
[0] >= 0x80) || len
> size
)
2036 * Check for composing characters. We can handle only the first six, but
2037 * skip all of them (otherwise the cursor would get stuck).
2050 * Next character length should not go beyond size to ensure that
2051 * UTF_COMPOSINGLIKE(...) does not read beyond size.
2053 len_next_char
= utf_ptr2len_len(p
+ len
, size
- len
);
2054 if (len_next_char
> size
- len
)
2057 if (!UTF_COMPOSINGLIKE(p
+ prevlen
, p
+ len
))
2060 /* Skip over composing char */
2064 len
+= len_next_char
;
2070 * Return the number of bytes the UTF-8 encoding of character "c" takes.
2071 * This does not include composing characters.
2091 * Convert Unicode character "c" to UTF-8 string in "buf[]".
2092 * Returns the number of bytes.
2093 * This does not include composing characters.
2096 utf_char2bytes(c
, buf
)
2100 if (c
< 0x80) /* 7 bits */
2105 if (c
< 0x800) /* 11 bits */
2107 buf
[0] = 0xc0 + ((unsigned)c
>> 6);
2108 buf
[1] = 0x80 + (c
& 0x3f);
2111 if (c
< 0x10000) /* 16 bits */
2113 buf
[0] = 0xe0 + ((unsigned)c
>> 12);
2114 buf
[1] = 0x80 + (((unsigned)c
>> 6) & 0x3f);
2115 buf
[2] = 0x80 + (c
& 0x3f);
2118 if (c
< 0x200000) /* 21 bits */
2120 buf
[0] = 0xf0 + ((unsigned)c
>> 18);
2121 buf
[1] = 0x80 + (((unsigned)c
>> 12) & 0x3f);
2122 buf
[2] = 0x80 + (((unsigned)c
>> 6) & 0x3f);
2123 buf
[3] = 0x80 + (c
& 0x3f);
2126 if (c
< 0x4000000) /* 26 bits */
2128 buf
[0] = 0xf8 + ((unsigned)c
>> 24);
2129 buf
[1] = 0x80 + (((unsigned)c
>> 18) & 0x3f);
2130 buf
[2] = 0x80 + (((unsigned)c
>> 12) & 0x3f);
2131 buf
[3] = 0x80 + (((unsigned)c
>> 6) & 0x3f);
2132 buf
[4] = 0x80 + (c
& 0x3f);
2136 buf
[0] = 0xfc + ((unsigned)c
>> 30);
2137 buf
[1] = 0x80 + (((unsigned)c
>> 24) & 0x3f);
2138 buf
[2] = 0x80 + (((unsigned)c
>> 18) & 0x3f);
2139 buf
[3] = 0x80 + (((unsigned)c
>> 12) & 0x3f);
2140 buf
[4] = 0x80 + (((unsigned)c
>> 6) & 0x3f);
2141 buf
[5] = 0x80 + (c
& 0x3f);
2146 * Return TRUE if "c" is a composing UTF-8 character. This means it will be
2147 * drawn on top of the preceding character.
2148 * Based on code from Markus Kuhn.
2154 /* Sorted list of non-overlapping intervals.
2155 * Generated by ../runtime/tools/unicode.vim. */
2156 static struct interval combining
[] =
2348 return intable(combining
, sizeof(combining
), c
);
2352 * Return TRUE for characters that can be displayed in a normal way.
2353 * Only for characters of 0x100 and above!
2359 #ifdef USE_WCHAR_FUNCTIONS
2361 * Assume the iswprint() library function works better than our own stuff.
2365 /* Sorted list of non-overlapping intervals.
2366 * 0xd800-0xdfff is reserved for UTF-16, actually illegal. */
2367 static struct interval nonprint
[] =
2369 {0x070f, 0x070f}, {0x180b, 0x180e}, {0x200b, 0x200f}, {0x202a, 0x202e},
2370 {0x206a, 0x206f}, {0xd800, 0xdfff}, {0xfeff, 0xfeff}, {0xfff9, 0xfffb},
2374 return !intable(nonprint
, sizeof(nonprint
), c
);
2379 * Get class of a Unicode character.
2382 * 2 or bigger: some class of word character.
2388 /* sorted list of non-overlapping intervals */
2389 static struct clinterval
2391 unsigned short first
;
2392 unsigned short last
;
2393 unsigned short class;
2396 {0x037e, 0x037e, 1}, /* Greek question mark */
2397 {0x0387, 0x0387, 1}, /* Greek ano teleia */
2398 {0x055a, 0x055f, 1}, /* Armenian punctuation */
2399 {0x0589, 0x0589, 1}, /* Armenian full stop */
2400 {0x05be, 0x05be, 1},
2401 {0x05c0, 0x05c0, 1},
2402 {0x05c3, 0x05c3, 1},
2403 {0x05f3, 0x05f4, 1},
2404 {0x060c, 0x060c, 1},
2405 {0x061b, 0x061b, 1},
2406 {0x061f, 0x061f, 1},
2407 {0x066a, 0x066d, 1},
2408 {0x06d4, 0x06d4, 1},
2409 {0x0700, 0x070d, 1}, /* Syriac punctuation */
2410 {0x0964, 0x0965, 1},
2411 {0x0970, 0x0970, 1},
2412 {0x0df4, 0x0df4, 1},
2413 {0x0e4f, 0x0e4f, 1},
2414 {0x0e5a, 0x0e5b, 1},
2415 {0x0f04, 0x0f12, 1},
2416 {0x0f3a, 0x0f3d, 1},
2417 {0x0f85, 0x0f85, 1},
2418 {0x104a, 0x104f, 1}, /* Myanmar punctuation */
2419 {0x10fb, 0x10fb, 1}, /* Georgian punctuation */
2420 {0x1361, 0x1368, 1}, /* Ethiopic punctuation */
2421 {0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
2422 {0x1680, 0x1680, 0},
2423 {0x169b, 0x169c, 1},
2424 {0x16eb, 0x16ed, 1},
2425 {0x1735, 0x1736, 1},
2426 {0x17d4, 0x17dc, 1}, /* Khmer punctuation */
2427 {0x1800, 0x180a, 1}, /* Mongolian punctuation */
2428 {0x2000, 0x200b, 0}, /* spaces */
2429 {0x200c, 0x2027, 1}, /* punctuation and symbols */
2430 {0x2028, 0x2029, 0},
2431 {0x202a, 0x202e, 1}, /* punctuation and symbols */
2432 {0x202f, 0x202f, 0},
2433 {0x2030, 0x205e, 1}, /* punctuation and symbols */
2434 {0x205f, 0x205f, 0},
2435 {0x2060, 0x27ff, 1}, /* punctuation and symbols */
2436 {0x2070, 0x207f, 0x2070}, /* superscript */
2437 {0x2080, 0x2094, 0x2080}, /* subscript */
2438 {0x20a0, 0x27ff, 1}, /* all kinds of symbols */
2439 {0x2800, 0x28ff, 0x2800}, /* braille */
2440 {0x2900, 0x2998, 1}, /* arrows, brackets, etc. */
2441 {0x29d8, 0x29db, 1},
2442 {0x29fc, 0x29fd, 1},
2443 {0x3000, 0x3000, 0}, /* ideographic space */
2444 {0x3001, 0x3020, 1}, /* ideographic punctuation */
2445 {0x3030, 0x3030, 1},
2446 {0x303d, 0x303d, 1},
2447 {0x3040, 0x309f, 0x3040}, /* Hiragana */
2448 {0x30a0, 0x30ff, 0x30a0}, /* Katakana */
2449 {0x3300, 0x9fff, 0x4e00}, /* CJK Ideographs */
2450 {0xac00, 0xd7a3, 0xac00}, /* Hangul Syllables */
2451 {0xf900, 0xfaff, 0x4e00}, /* CJK Ideographs */
2452 {0xfd3e, 0xfd3f, 1},
2453 {0xfe30, 0xfe6b, 1}, /* punctuation forms */
2454 {0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
2455 {0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
2456 {0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
2457 {0xff5b, 0xff65, 1}, /* half/fullwidth ASCII */
2460 int top
= sizeof(classes
) / sizeof(struct clinterval
) - 1;
2463 /* First quick check for Latin1 characters, use 'iskeyword'. */
2466 if (c
== ' ' || c
== '\t' || c
== NUL
|| c
== 0xa0)
2467 return 0; /* blank */
2469 return 2; /* word character */
2470 return 1; /* punctuation */
2473 /* binary search in table */
2476 mid
= (bot
+ top
) / 2;
2477 if (classes
[mid
].last
< c
)
2479 else if (classes
[mid
].first
> c
)
2482 return (int)classes
[mid
].class;
2485 /* most other characters are "word" characters */
2490 * Code for Unicode case-dependent operations. Based on notes in
2491 * http://www.unicode.org/Public/UNIDATA/CaseFolding.txt
2492 * This code uses simple case folding, not full case folding.
2493 * Last updated for Unicode 5.2.
2497 * The following tables are built by ../runtime/tools/unicode.vim.
2498 * They must be in numeric order, because we use binary search.
2499 * An entry such as {0x41,0x5a,1,32} means that Unicode characters in the
2500 * range from 0x41 to 0x5a inclusive, stepping by 1, are changed to
2501 * folded/upper/lower by adding 32.
2511 static convertStruct foldCase
[] =
2521 {0x178,0x178,-1,-121},
2523 {0x17f,0x17f,-1,-268},
2524 {0x181,0x181,-1,210},
2526 {0x186,0x186,-1,206},
2528 {0x189,0x18a,1,205},
2530 {0x18e,0x18e,-1,79},
2531 {0x18f,0x18f,-1,202},
2532 {0x190,0x190,-1,203},
2534 {0x193,0x193,-1,205},
2535 {0x194,0x194,-1,207},
2536 {0x196,0x196,-1,211},
2537 {0x197,0x197,-1,209},
2539 {0x19c,0x19c,-1,211},
2540 {0x19d,0x19d,-1,213},
2541 {0x19f,0x19f,-1,214},
2543 {0x1a6,0x1a6,-1,218},
2545 {0x1a9,0x1a9,-1,218},
2547 {0x1ae,0x1ae,-1,218},
2549 {0x1b1,0x1b2,1,217},
2551 {0x1b7,0x1b7,-1,219},
2562 {0x1f6,0x1f6,-1,-97},
2563 {0x1f7,0x1f7,-1,-56},
2565 {0x220,0x220,-1,-130},
2567 {0x23a,0x23a,-1,10795},
2569 {0x23d,0x23d,-1,-163},
2570 {0x23e,0x23e,-1,10792},
2572 {0x243,0x243,-1,-195},
2573 {0x244,0x244,-1,69},
2574 {0x245,0x245,-1,71},
2576 {0x345,0x345,-1,116},
2579 {0x386,0x386,-1,38},
2581 {0x38c,0x38c,-1,64},
2587 {0x3d0,0x3d0,-1,-30},
2588 {0x3d1,0x3d1,-1,-25},
2589 {0x3d5,0x3d5,-1,-15},
2590 {0x3d6,0x3d6,-1,-22},
2592 {0x3f0,0x3f0,-1,-54},
2593 {0x3f1,0x3f1,-1,-48},
2594 {0x3f4,0x3f4,-1,-60},
2595 {0x3f5,0x3f5,-1,-64},
2597 {0x3f9,0x3f9,-1,-7},
2599 {0x3fd,0x3ff,1,-130},
2604 {0x4c0,0x4c0,-1,15},
2608 {0x10a0,0x10c5,1,7264},
2609 {0x1e00,0x1e94,2,1},
2610 {0x1e9b,0x1e9b,-1,-58},
2611 {0x1e9e,0x1e9e,-1,-7615},
2612 {0x1ea0,0x1efe,2,1},
2613 {0x1f08,0x1f0f,1,-8},
2614 {0x1f18,0x1f1d,1,-8},
2615 {0x1f28,0x1f2f,1,-8},
2616 {0x1f38,0x1f3f,1,-8},
2617 {0x1f48,0x1f4d,1,-8},
2618 {0x1f59,0x1f5f,2,-8},
2619 {0x1f68,0x1f6f,1,-8},
2620 {0x1f88,0x1f8f,1,-8},
2621 {0x1f98,0x1f9f,1,-8},
2622 {0x1fa8,0x1faf,1,-8},
2623 {0x1fb8,0x1fb9,1,-8},
2624 {0x1fba,0x1fbb,1,-74},
2625 {0x1fbc,0x1fbc,-1,-9},
2626 {0x1fbe,0x1fbe,-1,-7173},
2627 {0x1fc8,0x1fcb,1,-86},
2628 {0x1fcc,0x1fcc,-1,-9},
2629 {0x1fd8,0x1fd9,1,-8},
2630 {0x1fda,0x1fdb,1,-100},
2631 {0x1fe8,0x1fe9,1,-8},
2632 {0x1fea,0x1feb,1,-112},
2633 {0x1fec,0x1fec,-1,-7},
2634 {0x1ff8,0x1ff9,1,-128},
2635 {0x1ffa,0x1ffb,1,-126},
2636 {0x1ffc,0x1ffc,-1,-9},
2637 {0x2126,0x2126,-1,-7517},
2638 {0x212a,0x212a,-1,-8383},
2639 {0x212b,0x212b,-1,-8262},
2640 {0x2132,0x2132,-1,28},
2641 {0x2160,0x216f,1,16},
2642 {0x2183,0x2183,-1,1},
2643 {0x24b6,0x24cf,1,26},
2644 {0x2c00,0x2c2e,1,48},
2645 {0x2c60,0x2c60,-1,1},
2646 {0x2c62,0x2c62,-1,-10743},
2647 {0x2c63,0x2c63,-1,-3814},
2648 {0x2c64,0x2c64,-1,-10727},
2649 {0x2c67,0x2c6b,2,1},
2650 {0x2c6d,0x2c6d,-1,-10780},
2651 {0x2c6e,0x2c6e,-1,-10749},
2652 {0x2c6f,0x2c6f,-1,-10783},
2653 {0x2c70,0x2c70,-1,-10782},
2654 {0x2c72,0x2c75,3,1},
2655 {0x2c7e,0x2c7f,1,-10815},
2656 {0x2c80,0x2ce2,2,1},
2657 {0x2ceb,0x2ced,2,1},
2658 {0xa640,0xa65e,2,1},
2659 {0xa662,0xa66c,2,1},
2660 {0xa680,0xa696,2,1},
2661 {0xa722,0xa72e,2,1},
2662 {0xa732,0xa76e,2,1},
2663 {0xa779,0xa77b,2,1},
2664 {0xa77d,0xa77d,-1,-35332},
2665 {0xa77e,0xa786,2,1},
2666 {0xa78b,0xa78b,-1,1},
2667 {0xff21,0xff3a,1,32},
2668 {0x10400,0x10427,1,40}
2671 static int utf_convert(int a
, convertStruct table
[], int tableSize
);
2674 * Generic conversion function for case operations.
2675 * Return the converted equivalent of "a", which is a UCS-4 character. Use
2676 * the given conversion "table". Uses binary search on "table".
2679 utf_convert(a
, table
, tableSize
)
2681 convertStruct table
[];
2684 int start
, mid
, end
; /* indices into table */
2687 end
= tableSize
/ sizeof(convertStruct
);
2690 /* need to search further */
2691 mid
= (end
+ start
) /2;
2692 if (table
[mid
].rangeEnd
< a
)
2697 if (table
[start
].rangeStart
<= a
&& a
<= table
[start
].rangeEnd
2698 && (a
- table
[start
].rangeStart
) % table
[start
].step
== 0)
2699 return (a
+ table
[start
].offset
);
2705 * Return the folded-case equivalent of "a", which is a UCS-4 character. Uses
2706 * simple case folding.
2712 return utf_convert(a
, foldCase
, sizeof(foldCase
));
2715 static convertStruct toLower
[] =
2721 {0x130,0x130,-1,-199},
2725 {0x178,0x178,-1,-121},
2727 {0x181,0x181,-1,210},
2729 {0x186,0x186,-1,206},
2731 {0x189,0x18a,1,205},
2733 {0x18e,0x18e,-1,79},
2734 {0x18f,0x18f,-1,202},
2735 {0x190,0x190,-1,203},
2737 {0x193,0x193,-1,205},
2738 {0x194,0x194,-1,207},
2739 {0x196,0x196,-1,211},
2740 {0x197,0x197,-1,209},
2742 {0x19c,0x19c,-1,211},
2743 {0x19d,0x19d,-1,213},
2744 {0x19f,0x19f,-1,214},
2746 {0x1a6,0x1a6,-1,218},
2748 {0x1a9,0x1a9,-1,218},
2750 {0x1ae,0x1ae,-1,218},
2752 {0x1b1,0x1b2,1,217},
2754 {0x1b7,0x1b7,-1,219},
2765 {0x1f6,0x1f6,-1,-97},
2766 {0x1f7,0x1f7,-1,-56},
2768 {0x220,0x220,-1,-130},
2770 {0x23a,0x23a,-1,10795},
2772 {0x23d,0x23d,-1,-163},
2773 {0x23e,0x23e,-1,10792},
2775 {0x243,0x243,-1,-195},
2776 {0x244,0x244,-1,69},
2777 {0x245,0x245,-1,71},
2781 {0x386,0x386,-1,38},
2783 {0x38c,0x38c,-1,64},
2789 {0x3f4,0x3f4,-1,-60},
2791 {0x3f9,0x3f9,-1,-7},
2793 {0x3fd,0x3ff,1,-130},
2798 {0x4c0,0x4c0,-1,15},
2802 {0x10a0,0x10c5,1,7264},
2803 {0x1e00,0x1e94,2,1},
2804 {0x1e9e,0x1e9e,-1,-7615},
2805 {0x1ea0,0x1efe,2,1},
2806 {0x1f08,0x1f0f,1,-8},
2807 {0x1f18,0x1f1d,1,-8},
2808 {0x1f28,0x1f2f,1,-8},
2809 {0x1f38,0x1f3f,1,-8},
2810 {0x1f48,0x1f4d,1,-8},
2811 {0x1f59,0x1f5f,2,-8},
2812 {0x1f68,0x1f6f,1,-8},
2813 {0x1f88,0x1f8f,1,-8},
2814 {0x1f98,0x1f9f,1,-8},
2815 {0x1fa8,0x1faf,1,-8},
2816 {0x1fb8,0x1fb9,1,-8},
2817 {0x1fba,0x1fbb,1,-74},
2818 {0x1fbc,0x1fbc,-1,-9},
2819 {0x1fc8,0x1fcb,1,-86},
2820 {0x1fcc,0x1fcc,-1,-9},
2821 {0x1fd8,0x1fd9,1,-8},
2822 {0x1fda,0x1fdb,1,-100},
2823 {0x1fe8,0x1fe9,1,-8},
2824 {0x1fea,0x1feb,1,-112},
2825 {0x1fec,0x1fec,-1,-7},
2826 {0x1ff8,0x1ff9,1,-128},
2827 {0x1ffa,0x1ffb,1,-126},
2828 {0x1ffc,0x1ffc,-1,-9},
2829 {0x2126,0x2126,-1,-7517},
2830 {0x212a,0x212a,-1,-8383},
2831 {0x212b,0x212b,-1,-8262},
2832 {0x2132,0x2132,-1,28},
2833 {0x2160,0x216f,1,16},
2834 {0x2183,0x2183,-1,1},
2835 {0x24b6,0x24cf,1,26},
2836 {0x2c00,0x2c2e,1,48},
2837 {0x2c60,0x2c60,-1,1},
2838 {0x2c62,0x2c62,-1,-10743},
2839 {0x2c63,0x2c63,-1,-3814},
2840 {0x2c64,0x2c64,-1,-10727},
2841 {0x2c67,0x2c6b,2,1},
2842 {0x2c6d,0x2c6d,-1,-10780},
2843 {0x2c6e,0x2c6e,-1,-10749},
2844 {0x2c6f,0x2c6f,-1,-10783},
2845 {0x2c70,0x2c70,-1,-10782},
2846 {0x2c72,0x2c75,3,1},
2847 {0x2c7e,0x2c7f,1,-10815},
2848 {0x2c80,0x2ce2,2,1},
2849 {0x2ceb,0x2ced,2,1},
2850 {0xa640,0xa65e,2,1},
2851 {0xa662,0xa66c,2,1},
2852 {0xa680,0xa696,2,1},
2853 {0xa722,0xa72e,2,1},
2854 {0xa732,0xa76e,2,1},
2855 {0xa779,0xa77b,2,1},
2856 {0xa77d,0xa77d,-1,-35332},
2857 {0xa77e,0xa786,2,1},
2858 {0xa78b,0xa78b,-1,1},
2859 {0xff21,0xff3a,1,32},
2860 {0x10400,0x10427,1,40}
2863 static convertStruct toUpper
[] =
2871 {0x131,0x131,-1,-232},
2876 {0x17f,0x17f,-1,-300},
2877 {0x180,0x180,-1,195},
2880 {0x192,0x192,-1,-1},
2881 {0x195,0x195,-1,97},
2882 {0x199,0x199,-1,-1},
2883 {0x19a,0x19a,-1,163},
2884 {0x19e,0x19e,-1,130},
2889 {0x1bd,0x1bd,-1,-1},
2890 {0x1bf,0x1bf,-1,56},
2891 {0x1c5,0x1c5,-1,-1},
2892 {0x1c6,0x1c6,-1,-2},
2893 {0x1c8,0x1c8,-1,-1},
2894 {0x1c9,0x1c9,-1,-2},
2895 {0x1cb,0x1cb,-1,-1},
2896 {0x1cc,0x1cc,-1,-2},
2898 {0x1dd,0x1dd,-1,-79},
2900 {0x1f2,0x1f2,-1,-1},
2901 {0x1f3,0x1f3,-1,-2},
2905 {0x23c,0x23c,-1,-1},
2906 {0x23f,0x240,1,10815},
2909 {0x250,0x250,-1,10783},
2910 {0x251,0x251,-1,10780},
2911 {0x252,0x252,-1,10782},
2912 {0x253,0x253,-1,-210},
2913 {0x254,0x254,-1,-206},
2914 {0x256,0x257,1,-205},
2915 {0x259,0x259,-1,-202},
2916 {0x25b,0x25b,-1,-203},
2917 {0x260,0x260,-1,-205},
2918 {0x263,0x263,-1,-207},
2919 {0x268,0x268,-1,-209},
2920 {0x269,0x269,-1,-211},
2921 {0x26b,0x26b,-1,10743},
2922 {0x26f,0x26f,-1,-211},
2923 {0x271,0x271,-1,10749},
2924 {0x272,0x272,-1,-213},
2925 {0x275,0x275,-1,-214},
2926 {0x27d,0x27d,-1,10727},
2927 {0x280,0x283,3,-218},
2928 {0x288,0x288,-1,-218},
2929 {0x289,0x289,-1,-69},
2930 {0x28a,0x28b,1,-217},
2931 {0x28c,0x28c,-1,-71},
2932 {0x292,0x292,-1,-219},
2933 {0x345,0x345,-1,84},
2935 {0x377,0x377,-1,-1},
2936 {0x37b,0x37d,1,130},
2937 {0x3ac,0x3ac,-1,-38},
2938 {0x3ad,0x3af,1,-37},
2939 {0x3b1,0x3c1,1,-32},
2940 {0x3c2,0x3c2,-1,-31},
2941 {0x3c3,0x3cb,1,-32},
2942 {0x3cc,0x3cc,-1,-64},
2943 {0x3cd,0x3ce,1,-63},
2944 {0x3d0,0x3d0,-1,-62},
2945 {0x3d1,0x3d1,-1,-57},
2946 {0x3d5,0x3d5,-1,-47},
2947 {0x3d6,0x3d6,-1,-54},
2948 {0x3d7,0x3d7,-1,-8},
2950 {0x3f0,0x3f0,-1,-86},
2951 {0x3f1,0x3f1,-1,-80},
2953 {0x3f5,0x3f5,-1,-96},
2955 {0x430,0x44f,1,-32},
2956 {0x450,0x45f,1,-80},
2960 {0x4cf,0x4cf,-1,-15},
2962 {0x561,0x586,1,-48},
2963 {0x1d79,0x1d79,-1,35332},
2964 {0x1d7d,0x1d7d,-1,3814},
2965 {0x1e01,0x1e95,2,-1},
2966 {0x1e9b,0x1e9b,-1,-59},
2967 {0x1ea1,0x1eff,2,-1},
2968 {0x1f00,0x1f07,1,8},
2969 {0x1f10,0x1f15,1,8},
2970 {0x1f20,0x1f27,1,8},
2971 {0x1f30,0x1f37,1,8},
2972 {0x1f40,0x1f45,1,8},
2973 {0x1f51,0x1f57,2,8},
2974 {0x1f60,0x1f67,1,8},
2975 {0x1f70,0x1f71,1,74},
2976 {0x1f72,0x1f75,1,86},
2977 {0x1f76,0x1f77,1,100},
2978 {0x1f78,0x1f79,1,128},
2979 {0x1f7a,0x1f7b,1,112},
2980 {0x1f7c,0x1f7d,1,126},
2981 {0x1f80,0x1f87,1,8},
2982 {0x1f90,0x1f97,1,8},
2983 {0x1fa0,0x1fa7,1,8},
2984 {0x1fb0,0x1fb1,1,8},
2985 {0x1fb3,0x1fb3,-1,9},
2986 {0x1fbe,0x1fbe,-1,-7205},
2987 {0x1fc3,0x1fc3,-1,9},
2988 {0x1fd0,0x1fd1,1,8},
2989 {0x1fe0,0x1fe1,1,8},
2990 {0x1fe5,0x1fe5,-1,7},
2991 {0x1ff3,0x1ff3,-1,9},
2992 {0x214e,0x214e,-1,-28},
2993 {0x2170,0x217f,1,-16},
2994 {0x2184,0x2184,-1,-1},
2995 {0x24d0,0x24e9,1,-26},
2996 {0x2c30,0x2c5e,1,-48},
2997 {0x2c61,0x2c61,-1,-1},
2998 {0x2c65,0x2c65,-1,-10795},
2999 {0x2c66,0x2c66,-1,-10792},
3000 {0x2c68,0x2c6c,2,-1},
3001 {0x2c73,0x2c76,3,-1},
3002 {0x2c81,0x2ce3,2,-1},
3003 {0x2cec,0x2cee,2,-1},
3004 {0x2d00,0x2d25,1,-7264},
3005 {0xa641,0xa65f,2,-1},
3006 {0xa663,0xa66d,2,-1},
3007 {0xa681,0xa697,2,-1},
3008 {0xa723,0xa72f,2,-1},
3009 {0xa733,0xa76f,2,-1},
3010 {0xa77a,0xa77c,2,-1},
3011 {0xa77f,0xa787,2,-1},
3012 {0xa78c,0xa78c,-1,-1},
3013 {0xff41,0xff5a,1,-32},
3014 {0x10428,0x1044f,1,-40}
3018 * Return the upper-case equivalent of "a", which is a UCS-4 character. Use
3019 * simple case folding.
3025 /* If 'casemap' contains "keepascii" use ASCII style toupper(). */
3026 if (a
< 128 && (cmp_flags
& CMP_KEEPASCII
))
3027 return TOUPPER_ASC(a
);
3029 #if defined(HAVE_TOWUPPER) && defined(__STDC_ISO_10646__)
3030 /* If towupper() is available and handles Unicode, use it. */
3031 if (!(cmp_flags
& CMP_INTERNAL
))
3035 /* For characters below 128 use locale sensitive toupper(). */
3037 return TOUPPER_LOC(a
);
3039 /* For any other characters use the above mapping table. */
3040 return utf_convert(a
, toUpper
, sizeof(toUpper
));
3047 return (utf_toupper(a
) != a
);
3051 * Return the lower-case equivalent of "a", which is a UCS-4 character. Use
3052 * simple case folding.
3058 /* If 'casemap' contains "keepascii" use ASCII style tolower(). */
3059 if (a
< 128 && (cmp_flags
& CMP_KEEPASCII
))
3060 return TOLOWER_ASC(a
);
3062 #if defined(HAVE_TOWLOWER) && defined(__STDC_ISO_10646__)
3063 /* If towlower() is available and handles Unicode, use it. */
3064 if (!(cmp_flags
& CMP_INTERNAL
))
3068 /* For characters below 128 use locale sensitive tolower(). */
3070 return TOLOWER_LOC(a
);
3072 /* For any other characters use the above mapping table. */
3073 return utf_convert(a
, toLower
, sizeof(toLower
));
3080 return (utf_tolower(a
) != a
);
3084 * Version of strnicmp() that handles multi-byte characters.
3085 * Needed for Big5, Sjift-JIS and UTF-8 encoding. Other DBCS encodings can
3086 * probably use strnicmp(), because there are no ASCII characters in the
3088 * Returns zero if s1 and s2 are equal (ignoring case), the difference between
3089 * two characters otherwise.
3092 mb_strnicmp(s1
, s2
, nn
)
3098 int incomplete
= FALSE
;
3101 for (i
= 0; i
< n
; i
+= l
)
3103 if (s1
[i
] == NUL
&& s2
[i
] == NUL
) /* both strings end */
3107 l
= utf_byte2len(s1
[i
]);
3110 l
= n
- i
; /* incomplete character */
3113 /* Check directly first, it's faster. */
3114 for (j
= 0; j
< l
; ++j
)
3116 if (s1
[i
+ j
] != s2
[i
+ j
])
3119 /* Both stings have the same bytes but are incomplete or
3120 * have illegal bytes, accept them as equal. */
3125 /* If one of the two characters is incomplete return -1. */
3126 if (incomplete
|| i
+ utf_byte2len(s2
[i
]) > n
)
3128 cdiff
= utf_fold(utf_ptr2char(s1
+ i
))
3129 - utf_fold(utf_ptr2char(s2
+ i
));
3136 l
= (*mb_ptr2len
)(s1
+ i
);
3139 /* Single byte: first check normally, then with ignore case. */
3142 cdiff
= MB_TOLOWER(s1
[i
]) - MB_TOLOWER(s2
[i
]);
3149 /* For non-Unicode multi-byte don't ignore case. */
3152 cdiff
= STRNCMP(s1
+ i
, s2
+ i
, l
);
3162 * "g8": show bytes of the UTF-8 char under the cursor. Doesn't matter what
3163 * 'encoding' has been set to.
3174 /* Get the byte length of the char under the cursor, including composing
3176 line
= ml_get_cursor();
3177 len
= utfc_ptr2len(line
);
3185 for (i
= 0; i
< len
; ++i
)
3189 /* start of (composing) character, get its length */
3192 STRCPY(IObuff
+ rlen
, "+ ");
3195 clen
= utf_ptr2len(line
+ i
);
3197 sprintf((char *)IObuff
+ rlen
, "%02x ", line
[i
]);
3199 rlen
+= (int)STRLEN(IObuff
+ rlen
);
3200 if (rlen
> IOSIZE
- 20)
3208 * mb_head_off() function pointer.
3209 * Return offset from "p" to the first byte of the character it points into.
3210 * If "p" points to the NUL at the end of the string return 0.
3211 * Returns 0 when already at the first byte of a character.
3214 latin_head_off(base
, p
)
3215 char_u
*base UNUSED
;
3222 dbcs_head_off(base
, p
)
3228 /* It can't be a trailing byte when not using DBCS, at the start of the
3229 * string or the previous byte can't start a double-byte. */
3230 if (p
<= base
|| MB_BYTE2LEN(p
[-1]) == 1 || *p
== NUL
)
3233 /* This is slow: need to start at the base and go forward until the
3234 * byte we are looking for. Return 1 when we went past it, 0 otherwise. */
3237 q
+= dbcs_ptr2len(q
);
3238 return (q
== p
) ? 0 : 1;
3242 * Special version of dbcs_head_off() that works for ScreenLines[], where
3243 * single-width DBCS_JPNU characters are stored separately.
3246 dbcs_screen_head_off(base
, p
)
3252 /* It can't be a trailing byte when not using DBCS, at the start of the
3253 * string or the previous byte can't start a double-byte.
3254 * For euc-jp an 0x8e byte in the previous cell always means we have a
3255 * lead byte in the current cell. */
3257 || (enc_dbcs
== DBCS_JPNU
&& p
[-1] == 0x8e)
3258 || MB_BYTE2LEN(p
[-1]) == 1
3262 /* This is slow: need to start at the base and go forward until the
3263 * byte we are looking for. Return 1 when we went past it, 0 otherwise.
3264 * For DBCS_JPNU look out for 0x8e, which means the second byte is not
3265 * stored as the next byte. */
3269 if (enc_dbcs
== DBCS_JPNU
&& *q
== 0x8e)
3272 q
+= dbcs_ptr2len(q
);
3274 return (q
== p
) ? 0 : 1;
3278 utf_head_off(base
, p
)
3290 if (*p
< 0x80) /* be quick for ASCII */
3293 /* Skip backwards over trailing bytes: 10xx.xxxx
3294 * Skip backwards again if on a composing char. */
3297 /* Move s to the last byte of this char. */
3298 for (s
= q
; (s
[1] & 0xc0) == 0x80; ++s
)
3300 /* Move q to the first byte of this char. */
3301 while (q
> base
&& (*q
& 0xc0) == 0x80)
3303 /* Check for illegal sequence. Do allow an illegal byte after where we
3305 len
= utf8len_tab
[*q
];
3306 if (len
!= (int)(s
- q
+ 1) && len
!= (int)(p
- q
+ 1))
3312 c
= utf_ptr2char(q
);
3313 if (utf_iscomposing(c
))
3317 if (arabic_maycombine(c
))
3319 /* Advance to get a sneak-peak at the next char */
3322 /* Move j to the first byte of this char. */
3323 while (j
> base
&& (*j
& 0xc0) == 0x80)
3325 if (arabic_combine(utf_ptr2char(j
), c
))
3332 return (int)(p
- q
);
3336 * Copy a character from "*fp" to "*tp" and advance the pointers.
3339 mb_copy_char(fp
, tp
)
3343 int l
= (*mb_ptr2len
)(*fp
);
3345 mch_memmove(*tp
, *fp
, (size_t)l
);
3351 * Return the offset from "p" to the first byte of a character. When "p" is
3352 * at the start of a character 0 is returned, otherwise the offset to the next
3353 * character. Can start anywhere in a stream of bytes.
3356 mb_off_next(base
, p
)
3365 if (*p
< 0x80) /* be quick for ASCII */
3368 /* Find the next character that isn't 10xx.xxxx */
3369 for (i
= 0; (p
[i
] & 0xc0) == 0x80; ++i
)
3373 /* Check for illegal sequence. */
3374 for (j
= 0; p
- j
> base
; ++j
)
3375 if ((p
[-j
] & 0xc0) != 0x80)
3377 if (utf8len_tab
[p
[-j
]] != i
+ j
)
3383 /* Only need to check if we're on a trail byte, it doesn't matter if we
3384 * want the offset to the next or current character. */
3385 return (*mb_head_off
)(base
, p
);
3389 * Return the offset from "p" to the last byte of the character it points
3390 * into. Can start anywhere in a stream of bytes.
3393 mb_tail_off(base
, p
)
3405 /* Find the last character that is 10xx.xxxx */
3406 for (i
= 0; (p
[i
+ 1] & 0xc0) == 0x80; ++i
)
3408 /* Check for illegal sequence. */
3409 for (j
= 0; p
- j
> base
; ++j
)
3410 if ((p
[-j
] & 0xc0) != 0x80)
3412 if (utf8len_tab
[p
[-j
]] != i
+ j
+ 1)
3417 /* It can't be the first byte if a double-byte when not using DBCS, at the
3418 * end of the string or the byte can't start a double-byte. */
3419 if (enc_dbcs
== 0 || p
[1] == NUL
|| MB_BYTE2LEN(*p
) == 1)
3422 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3423 return 1 - dbcs_head_off(base
, p
);
3427 * Find the next illegal byte sequence.
3432 pos_T pos
= curwin
->w_cursor
;
3436 char_u
*tofree
= NULL
;
3438 vimconv
.vc_type
= CONV_NONE
;
3439 if (enc_utf8
&& (enc_canon_props(curbuf
->b_p_fenc
) & ENC_8BIT
))
3441 /* 'encoding' is "utf-8" but we are editing a 8-bit encoded file,
3442 * possibly a utf-8 file with illegal bytes. Setup for conversion
3443 * from utf-8 to 'fileencoding'. */
3444 convert_setup(&vimconv
, p_enc
, curbuf
->b_p_fenc
);
3447 #ifdef FEAT_VIRTUALEDIT
3448 curwin
->w_cursor
.coladd
= 0;
3452 p
= ml_get_cursor();
3453 if (vimconv
.vc_type
!= CONV_NONE
)
3456 tofree
= string_convert(&vimconv
, p
, NULL
);
3464 /* Illegal means that there are not enough trail bytes (checked by
3465 * utf_ptr2len()) or too many of them (overlong sequence). */
3466 len
= utf_ptr2len(p
);
3467 if (*p
>= 0x80 && (len
== 1
3468 || utf_char2len(utf_ptr2char(p
)) != len
))
3470 if (vimconv
.vc_type
== CONV_NONE
)
3471 curwin
->w_cursor
.col
+= (colnr_T
)(p
- ml_get_cursor());
3476 len
= (int)(p
- tofree
);
3477 for (p
= ml_get_cursor(); *p
!= NUL
&& len
-- > 0; p
+= l
)
3480 curwin
->w_cursor
.col
+= l
;
3487 if (curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
3489 ++curwin
->w_cursor
.lnum
;
3490 curwin
->w_cursor
.col
= 0;
3493 /* didn't find it: don't move and beep */
3494 curwin
->w_cursor
= pos
;
3499 convert_setup(&vimconv
, NULL
, NULL
);
3502 #if defined(HAVE_GTK2) || defined(PROTO)
3504 * Return TRUE if string "s" is a valid utf-8 string.
3505 * When "end" is NULL stop at the first NUL.
3506 * When "end" is positive stop there.
3509 utf_valid_string(s
, end
)
3516 while (end
== NULL
? *p
!= NUL
: p
< end
)
3518 l
= utf8len_tab_zero
[*p
];
3520 return FALSE
; /* invalid lead byte */
3521 if (end
!= NULL
&& p
+ l
> end
)
3522 return FALSE
; /* incomplete byte sequence */
3525 if ((*p
++ & 0xc0) != 0x80)
3526 return FALSE
; /* invalid trail byte */
3532 #if defined(FEAT_GUI) || defined(PROTO)
3534 * Special version of mb_tail_off() for use in ScreenLines[].
3537 dbcs_screen_tail_off(base
, p
)
3541 /* It can't be the first byte if a double-byte when not using DBCS, at the
3542 * end of the string or the byte can't start a double-byte.
3543 * For euc-jp an 0x8e byte always means we have a lead byte in the current
3545 if (*p
== NUL
|| p
[1] == NUL
3546 || (enc_dbcs
== DBCS_JPNU
&& *p
== 0x8e)
3547 || MB_BYTE2LEN(*p
) == 1)
3550 /* Return 1 when on the lead byte, 0 when on the tail byte. */
3551 return 1 - dbcs_screen_head_off(base
, p
);
3556 * If the cursor moves on an trail byte, set the cursor on the lead byte.
3557 * Thus it moves left if necessary.
3558 * Return TRUE when the cursor was adjusted.
3563 mb_adjustpos(&curwin
->w_cursor
);
3567 * Adjust position "*lp" to point to the first byte of a multi-byte character.
3568 * If it points to a tail byte it's moved backwards to the head byte.
3577 #ifdef FEAT_VIRTUALEDIT
3582 p
= ml_get(lp
->lnum
);
3583 lp
->col
-= (*mb_head_off
)(p
, p
+ lp
->col
);
3584 #ifdef FEAT_VIRTUALEDIT
3585 /* Reset "coladd" when the cursor would be on the right half of a
3586 * double-wide character. */
3588 && p
[lp
->col
] != TAB
3589 && vim_isprintc((*mb_ptr2char
)(p
+ lp
->col
))
3590 && ptr2cells(p
+ lp
->col
) > 1)
3597 * Return a pointer to the character before "*p", if there is one.
3601 char_u
*line
; /* start of the string */
3605 mb_ptr_back(line
, p
);
3610 * Return the character length of "str". Each multi-byte character (with
3611 * following composing characters) counts as one.
3623 for (count
= 0; *p
!= NUL
; count
++)
3624 p
+= (*mb_ptr2len
)(p
);
3629 #if defined(FEAT_SPELL) || defined(PROTO)
3631 * Like mb_charlen() but for a string with specified length.
3634 mb_charlen_len(str
, len
)
3641 for (count
= 0; *p
!= NUL
&& p
< str
+ len
; count
++)
3642 p
+= (*mb_ptr2len
)(p
);
3649 * Try to un-escape a multi-byte character.
3650 * Used for the "to" and "from" part of a mapping.
3651 * Return the un-escaped string if it is a multi-byte character, and advance
3652 * "pp" to just after the bytes that formed it.
3653 * Return NULL if no multi-byte char was found.
3659 static char_u buf
[MB_MAXBYTES
+ 1];
3663 /* Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI
3664 * KS_EXTRA KE_CSI to CSI. */
3665 for (n
= 0; str
[n
] != NUL
&& m
<= MB_MAXBYTES
; ++n
)
3667 if (str
[n
] == K_SPECIAL
3668 && str
[n
+ 1] == KS_SPECIAL
3669 && str
[n
+ 2] == KE_FILLER
)
3671 buf
[m
++] = K_SPECIAL
;
3674 else if ((str
[n
] == K_SPECIAL
3679 && str
[n
+ 1] == KS_EXTRA
3680 && str
[n
+ 2] == (int)KE_CSI
)
3685 else if (str
[n
] == K_SPECIAL
3690 break; /* a special key can't be a multibyte char */
3695 /* Return a multi-byte character if it's found. An illegal sequence
3696 * will result in a 1 here. */
3697 if ((*mb_ptr2len
)(buf
) > 1)
3707 * Return TRUE if the character at "row"/"col" on the screen is the left side
3708 * of a double-width character.
3709 * Caller must make sure "row" and "col" are not invalid!
3712 mb_lefthalve(row
, col
)
3716 #ifdef FEAT_HANGULIN
3717 if (composing_hangul
)
3720 return (*mb_off2cells
)(LineOffset
[row
] + col
,
3721 LineOffset
[row
] + screen_Columns
) > 1;
3725 * Correct a position on the screen, if it's the right half of a double-wide
3726 * char move it to the left half. Returns the corrected column.
3729 mb_fix_col(col
, row
)
3733 col
= check_col(col
);
3734 row
= check_row(row
);
3735 if (has_mbyte
&& ScreenLines
!= NULL
&& col
> 0
3737 && ScreenLines
[LineOffset
[row
] + col
] != NUL
3738 && dbcs_screen_head_off(ScreenLines
+ LineOffset
[row
],
3739 ScreenLines
+ LineOffset
[row
] + col
))
3740 || (enc_utf8
&& ScreenLines
[LineOffset
[row
] + col
] == 0)))
3746 #if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
3747 static int enc_alias_search
__ARGS((char_u
*name
));
3750 * Skip the Vim specific head of a 'encoding' name.
3756 if (STRNCMP(p
, "2byte-", 6) == 0)
3758 if (STRNCMP(p
, "8bit-", 5) == 0)
3764 * Find the canonical name for encoding "enc".
3765 * When the name isn't recognized, returns "enc" itself, but with all lower
3766 * case characters and '_' replaced with '-'.
3767 * Returns an allocated string. NULL for out-of-memory.
3778 if (STRCMP(enc
, "default") == 0)
3780 /* Use the default encoding as it's found by set_init_1(). */
3781 r
= get_encoding_default();
3783 r
= (char_u
*)"latin1";
3784 return vim_strsave(r
);
3788 /* copy "enc" to allocated memory, with room for two '-' */
3789 r
= alloc((unsigned)(STRLEN(enc
) + 3));
3792 /* Make it all lower case and replace '_' with '-'. */
3794 for (s
= enc
; *s
!= NUL
; ++s
)
3799 *p
++ = TOLOWER_ASC(*s
);
3803 /* Skip "2byte-" and "8bit-". */
3806 /* Change "microsoft-cp" to "cp". Used in some spell files. */
3807 if (STRNCMP(p
, "microsoft-cp", 12) == 0)
3810 /* "iso8859" -> "iso-8859" */
3811 if (STRNCMP(p
, "iso8859", 7) == 0)
3813 STRMOVE(p
+ 4, p
+ 3);
3817 /* "iso-8859n" -> "iso-8859-n" */
3818 if (STRNCMP(p
, "iso-8859", 8) == 0 && p
[8] != '-')
3820 STRMOVE(p
+ 9, p
+ 8);
3824 /* "latin-N" -> "latinN" */
3825 if (STRNCMP(p
, "latin-", 6) == 0)
3826 STRMOVE(p
+ 5, p
+ 6);
3828 if (enc_canon_search(p
) >= 0)
3830 /* canonical name can be used unmodified */
3834 else if ((i
= enc_alias_search(p
)) >= 0)
3836 /* alias recognized, get canonical name */
3838 r
= vim_strsave((char_u
*)enc_canon_table
[i
].name
);
3845 * Search for an encoding alias of "name".
3846 * Returns -1 when not found.
3849 enc_alias_search(name
)
3854 for (i
= 0; enc_alias_table
[i
].name
!= NULL
; ++i
)
3855 if (STRCMP(name
, enc_alias_table
[i
].name
) == 0)
3856 return enc_alias_table
[i
].canon
;
3861 #if defined(FEAT_MBYTE) || defined(PROTO)
3863 #ifdef HAVE_LANGINFO_H
3864 # include <langinfo.h>
3868 * Get the canonicalized encoding of the current locale.
3869 * Returns an allocated string when successful, NULL when not.
3881 long acp
= GetACP();
3884 STRCPY(buf
, "ucs-2le");
3885 else if (acp
== 1252) /* cp1252 is used as latin1 */
3886 STRCPY(buf
, "latin1");
3888 sprintf(buf
, "cp%ld", acp
);
3890 # ifdef HAVE_NL_LANGINFO_CODESET
3891 if ((s
= nl_langinfo(CODESET
)) == NULL
|| *s
== NUL
)
3893 # if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3894 if ((s
= setlocale(LC_CTYPE
, NULL
)) == NULL
|| *s
== NUL
)
3896 if ((s
= getenv("LC_ALL")) == NULL
|| *s
== NUL
)
3897 if ((s
= getenv("LC_CTYPE")) == NULL
|| *s
== NUL
)
3900 if (s
== NULL
|| *s
== NUL
)
3903 /* The most generic locale format is:
3904 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
3905 * If there is a '.' remove the part before it.
3906 * if there is something after the codeset, remove it.
3907 * Make the name lowercase and replace '_' with '-'.
3908 * Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
3909 * "ko_KR.EUC" == "euc-kr"
3911 if ((p
= (char *)vim_strchr((char_u
*)s
, '.')) != NULL
)
3913 if (p
> s
+ 2 && STRNICMP(p
+ 1, "EUC", 3) == 0
3914 && !isalnum((int)p
[4]) && p
[4] != '-' && p
[-3] == '_')
3916 /* copy "XY.EUC" to "euc-XY" to buf[10] */
3917 STRCPY(buf
+ 10, "euc-");
3926 for (i
= 0; s
[i
] != NUL
&& i
< (int)sizeof(buf
) - 1; ++i
)
3928 if (s
[i
] == '_' || s
[i
] == '-')
3930 else if (isalnum((int)s
[i
]))
3931 buf
[i
] = TOLOWER_ASC(s
[i
]);
3938 return enc_canonize((char_u
*)buf
);
3941 #if defined(WIN3264) || defined(PROTO)
3943 * Convert an encoding name to an MS-Windows codepage.
3944 * Returns zero if no codepage can be figured out.
3947 encname2codepage(name
)
3954 if (STRNCMP(p
, "8bit-", 5) == 0)
3956 else if (STRNCMP(p_enc
, "2byte-", 6) == 0)
3959 if (p
[0] == 'c' && p
[1] == 'p')
3961 else if ((idx
= enc_canon_search(p
)) >= 0)
3962 cp
= enc_canon_table
[idx
].codepage
;
3965 if (IsValidCodePage(cp
))
3971 # if defined(USE_ICONV) || defined(PROTO)
3973 static char_u
*iconv_string
__ARGS((vimconv_T
*vcp
, char_u
*str
, int slen
, int *unconvlenp
, int *resultlenp
));
3976 * Call iconv_open() with a check if iconv() works properly (there are broken
3978 * Returns (void *)-1 if failed.
3979 * (should return iconv_t, but that causes problems with prototypes).
3982 my_iconv_open(to
, from
)
3987 #define ICONV_TESTLEN 400
3988 char_u tobuf
[ICONV_TESTLEN
];
3991 static int iconv_ok
= -1;
3993 if (iconv_ok
== FALSE
)
3994 return (void *)-1; /* detected a broken iconv() previously */
3996 #ifdef DYNAMIC_ICONV
3997 /* Check if the iconv.dll can be found. */
3998 if (!iconv_enabled(TRUE
))
4002 fd
= iconv_open((char *)enc_skip(to
), (char *)enc_skip(from
));
4004 if (fd
!= (iconv_t
)-1 && iconv_ok
== -1)
4007 * Do a dummy iconv() call to check if it actually works. There is a
4008 * version of iconv() on Linux that is broken. We can't ignore it,
4009 * because it's wide-spread. The symptoms are that after outputting
4010 * the initial shift state the "to" pointer is NULL and conversion
4011 * stops for no apparent reason after about 8160 characters.
4014 tolen
= ICONV_TESTLEN
;
4015 (void)iconv(fd
, NULL
, NULL
, &p
, &tolen
);
4030 * Convert the string "str[slen]" with iconv().
4031 * If "unconvlenp" is not NULL handle the string ending in an incomplete
4032 * sequence and set "*unconvlenp" to the length of it.
4033 * Returns the converted string in allocated memory. NULL for an error.
4034 * If resultlenp is not NULL, sets it to the result length in bytes.
4037 iconv_string(vcp
, str
, slen
, unconvlenp
, resultlenp
)
4050 char_u
*result
= NULL
;
4058 if (len
== 0 || ICONV_ERRNO
== ICONV_E2BIG
)
4060 /* Allocate enough room for most conversions. When re-allocating
4061 * increase the buffer size. */
4062 len
= len
+ fromlen
* 2 + 40;
4063 p
= alloc((unsigned)len
);
4064 if (p
!= NULL
&& done
> 0)
4065 mch_memmove(p
, result
, done
);
4068 if (result
== NULL
) /* out of memory */
4072 to
= (char *)result
+ done
;
4073 tolen
= len
- done
- 2;
4074 /* Avoid a warning for systems with a wrong iconv() prototype by
4075 * casting the second argument to void *. */
4076 if (iconv(vcp
->vc_fd
, (void *)&from
, &fromlen
, &to
, &tolen
)
4079 /* Finished, append a NUL. */
4084 /* Check both ICONV_EINVAL and EINVAL, because the dynamically loaded
4085 * iconv library may use one of them. */
4086 if (!vcp
->vc_fail
&& unconvlenp
!= NULL
4087 && (ICONV_ERRNO
== ICONV_EINVAL
|| ICONV_ERRNO
== EINVAL
))
4089 /* Handle an incomplete sequence at the end. */
4091 *unconvlenp
= (int)fromlen
;
4095 /* Check both ICONV_EILSEQ and EILSEQ, because the dynamically loaded
4096 * iconv library may use one of them. */
4097 else if (!vcp
->vc_fail
4098 && (ICONV_ERRNO
== ICONV_EILSEQ
|| ICONV_ERRNO
== EILSEQ
4099 || ICONV_ERRNO
== ICONV_EINVAL
|| ICONV_ERRNO
== EINVAL
))
4101 /* Can't convert: insert a '?' and skip a character. This assumes
4102 * conversion from 'encoding' to something else. In other
4103 * situations we don't know what to skip anyway. */
4105 if ((*mb_ptr2cells
)((char_u
*)from
) > 1)
4108 l
= utfc_ptr2len_len((char_u
*)from
, (int)fromlen
);
4111 l
= (*mb_ptr2len
)((char_u
*)from
);
4112 if (l
> (int)fromlen
)
4118 else if (ICONV_ERRNO
!= ICONV_E2BIG
)
4120 /* conversion failed */
4125 /* Not enough room or skipping illegal sequence. */
4126 done
= to
- (char *)result
;
4129 if (resultlenp
!= NULL
)
4130 *resultlenp
= (int)(to
- (char *)result
);
4134 # if defined(DYNAMIC_ICONV) || defined(PROTO)
4136 * Dynamically load the "iconv.dll" on Win32.
4139 #ifndef DYNAMIC_ICONV /* just generating prototypes */
4140 # define HINSTANCE int
4142 static HINSTANCE hIconvDLL
= 0;
4143 static HINSTANCE hMsvcrtDLL
= 0;
4145 # ifndef DYNAMIC_ICONV_DLL
4146 # define DYNAMIC_ICONV_DLL "iconv.dll"
4147 # define DYNAMIC_ICONV_DLL_ALT "libiconv.dll"
4149 # ifndef DYNAMIC_MSVCRT_DLL
4150 # define DYNAMIC_MSVCRT_DLL "msvcrt.dll"
4154 * Try opening the iconv.dll and return TRUE if iconv() can be used.
4157 iconv_enabled(verbose
)
4160 if (hIconvDLL
!= 0 && hMsvcrtDLL
!= 0)
4162 hIconvDLL
= LoadLibrary(DYNAMIC_ICONV_DLL
);
4163 if (hIconvDLL
== 0) /* sometimes it's called libiconv.dll */
4164 hIconvDLL
= LoadLibrary(DYNAMIC_ICONV_DLL_ALT
);
4166 hMsvcrtDLL
= LoadLibrary(DYNAMIC_MSVCRT_DLL
);
4167 if (hIconvDLL
== 0 || hMsvcrtDLL
== 0)
4169 /* Only give the message when 'verbose' is set, otherwise it might be
4170 * done whenever a conversion is attempted. */
4171 if (verbose
&& p_verbose
> 0)
4175 hIconvDLL
== 0 ? DYNAMIC_ICONV_DLL
: DYNAMIC_MSVCRT_DLL
);
4182 iconv
= (void *)GetProcAddress(hIconvDLL
, "libiconv");
4183 iconv_open
= (void *)GetProcAddress(hIconvDLL
, "libiconv_open");
4184 iconv_close
= (void *)GetProcAddress(hIconvDLL
, "libiconv_close");
4185 iconvctl
= (void *)GetProcAddress(hIconvDLL
, "libiconvctl");
4186 iconv_errno
= (void *)GetProcAddress(hMsvcrtDLL
, "_errno");
4187 if (iconv
== NULL
|| iconv_open
== NULL
|| iconv_close
== NULL
4188 || iconvctl
== NULL
|| iconv_errno
== NULL
)
4191 if (verbose
&& p_verbose
> 0)
4194 EMSG2(_(e_loadfunc
), "for libiconv");
4205 /* Don't use iconv() when inputting or outputting characters. */
4206 if (input_conv
.vc_type
== CONV_ICONV
)
4207 convert_setup(&input_conv
, NULL
, NULL
);
4208 if (output_conv
.vc_type
== CONV_ICONV
)
4209 convert_setup(&output_conv
, NULL
, NULL
);
4212 FreeLibrary(hIconvDLL
);
4213 if (hMsvcrtDLL
!= 0)
4214 FreeLibrary(hMsvcrtDLL
);
4218 # endif /* DYNAMIC_ICONV */
4219 # endif /* USE_ICONV */
4221 #endif /* FEAT_MBYTE */
4223 #if defined(FEAT_XIM) || defined(PROTO)
4225 # ifdef FEAT_GUI_MACVIM
4226 typedef int GtkIMContext
;
4227 typedef int * gpointer
;
4229 # define g_return_if_fail(x) if (!(x)) return;
4232 # if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM)
4233 static int xim_has_preediting
INIT(= FALSE
); /* IM current status */
4236 * Set preedit_start_col to the current cursor position.
4239 init_preedit_start_col(void)
4241 if (State
& CMDLINE
)
4242 preedit_start_col
= cmdline_getvcol_cursor();
4243 else if (curwin
!= NULL
)
4244 getvcol(curwin
, &curwin
->w_cursor
, &preedit_start_col
, NULL
, NULL
);
4245 /* Prevent that preediting marks the buffer as changed. */
4246 xim_changed_while_preediting
= curbuf
->b_changed
;
4250 # if (defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)) && !defined(PROTO)
4252 static int im_is_active
= FALSE
; /* IM is enabled for current mode */
4253 static int preedit_is_active
= FALSE
;
4254 static int im_preedit_start
= 0; /* start offset in characters */
4255 static int im_preedit_cursor
= 0; /* cursor offset in characters */
4256 static int im_preedit_trailing
= 0; /* number of characters after cursor */
4258 static unsigned long im_commit_handler_id
= 0;
4259 # ifndef FEAT_GUI_MACVIM
4260 static unsigned int im_activatekey_keyval
= GDK_VoidSymbol
;
4261 static unsigned int im_activatekey_state
= 0;
4264 # ifndef FEAT_GUI_MACVIM
4266 im_set_active(int active
)
4273 uimfep_set_active(active
);
4278 was_active
= !!im_is_active
;
4279 im_is_active
= (active
&& !p_imdisable
);
4281 if (im_is_active
!= was_active
)
4284 # else /* FEAT_GUI_MACVIM */
4286 im_set_active(int active
)
4289 gui_im_set_active(active
);
4291 uimfep_set_active(active
);
4296 xim_set_focus(int focus
)
4298 # ifndef FEAT_GUI_MACVIM
4302 gtk_im_context_focus_in(xic
);
4304 gtk_im_context_focus_out(xic
);
4309 # ifndef FEAT_GUI_MACVIM
4311 im_set_position(int row
, int col
)
4317 area
.x
= FILL_X(col
);
4318 area
.y
= FILL_Y(row
);
4319 area
.width
= gui
.char_width
* (mb_lefthalve(row
, col
) ? 2 : 1);
4320 area
.height
= gui
.char_height
;
4322 gtk_im_context_set_cursor_location(xic
, &area
);
4327 # if 0 || defined(PROTO) /* apparently only used in gui_x11.c */
4329 xim_set_preedit(void)
4331 im_set_position(gui
.row
, gui
.col
);
4336 im_add_to_input(char_u
*str
, int len
)
4338 /* Convert from 'termencoding' (always "utf-8") to 'encoding' */
4339 if (input_conv
.vc_type
!= CONV_NONE
)
4341 str
= string_convert(&input_conv
, str
, &len
);
4342 g_return_if_fail(str
!= NULL
);
4345 add_to_input_buf_csi(str
, len
);
4347 if (input_conv
.vc_type
!= CONV_NONE
)
4350 # ifndef FEAT_GUI_MACVIM
4351 if (p_mh
) /* blank out the pointer if necessary */
4352 gui_mch_mousehide(TRUE
);
4357 im_delete_preedit(void)
4359 char_u bskey
[] = {CSI
, 'k', 'b'};
4360 char_u delkey
[] = {CSI
, 'k', 'D'};
4364 im_preedit_cursor
= 0;
4367 for (; im_preedit_cursor
> 0; --im_preedit_cursor
)
4368 add_to_input_buf(bskey
, (int)sizeof(bskey
));
4370 for (; im_preedit_trailing
> 0; --im_preedit_trailing
)
4371 add_to_input_buf(delkey
, (int)sizeof(delkey
));
4375 * Move the cursor left by "num_move_back" characters.
4376 * Note that ins_left() checks im_is_preediting() to avoid breaking undo for
4377 * these K_LEFT keys.
4380 im_correct_cursor(int num_move_back
)
4382 char_u backkey
[] = {CSI
, 'k', 'l'};
4386 # ifdef FEAT_RIGHTLEFT
4387 if ((State
& CMDLINE
) == 0 && curwin
!= NULL
&& curwin
->w_p_rl
)
4390 for (; num_move_back
> 0; --num_move_back
)
4391 add_to_input_buf(backkey
, (int)sizeof(backkey
));
4394 # ifndef FEAT_GUI_MACVIM
4395 static int xim_expected_char
= NUL
;
4396 static int xim_ignored_char
= FALSE
;
4400 * Update the mode and cursor while in an IM callback.
4407 old_vgetc_busy
= vgetc_busy
;
4410 vgetc_busy
= old_vgetc_busy
;
4415 # ifndef FEAT_GUI_MACVIM
4417 * Callback invoked when the user finished preediting.
4418 * Put the final string into the input buffer.
4421 im_commit_cb(GtkIMContext
*context UNUSED
,
4423 gpointer data UNUSED
)
4425 int slen
= (int)STRLEN(str
);
4426 int add_to_input
= TRUE
;
4429 int commit_with_preedit
= TRUE
;
4433 xim_log("im_commit_cb(): %s\n", str
);
4436 /* The imhangul module doesn't reset the preedit string before
4437 * committing. Call im_delete_preedit() to work around that. */
4438 im_delete_preedit();
4440 /* Indicate that preediting has finished. */
4441 if (preedit_start_col
== MAXCOL
)
4443 init_preedit_start_col();
4444 commit_with_preedit
= FALSE
;
4447 /* The thing which setting "preedit_start_col" to MAXCOL means that
4448 * "preedit_start_col" will be set forcely when calling
4449 * preedit_changed_cb() next time.
4450 * "preedit_start_col" should not reset with MAXCOL on this part. Vim
4451 * is simulating the preediting by using add_to_input_str(). when
4452 * preedit begin immediately before committed, the typebuf is not
4453 * flushed to screen, then it can't get correct "preedit_start_col".
4454 * Thus, it should calculate the cells by adding cells of the committed
4456 if (input_conv
.vc_type
!= CONV_NONE
)
4458 im_str
= string_convert(&input_conv
, (char_u
*)str
, &len
);
4459 g_return_if_fail(im_str
!= NULL
);
4462 im_str
= (char_u
*)str
;
4464 for (p
= im_str
; p
< im_str
+ len
; p
+= (*mb_ptr2len
)(p
))
4465 clen
+= (*mb_ptr2cells
)(p
);
4466 if (input_conv
.vc_type
!= CONV_NONE
)
4468 preedit_start_col
+= clen
;
4470 /* Is this a single character that matches a keypad key that's just
4471 * been pressed? If so, we don't want it to be entered as such - let
4472 * us carry on processing the raw keycode so that it may be used in
4473 * mappings as <kSomething>. */
4474 if (xim_expected_char
!= NUL
)
4476 /* We're currently processing a keypad or other special key */
4477 if (slen
== 1 && str
[0] == xim_expected_char
)
4479 /* It's a match - don't do it here */
4480 xim_ignored_char
= TRUE
;
4481 add_to_input
= FALSE
;
4486 xim_ignored_char
= FALSE
;
4491 im_add_to_input((char_u
*)str
, slen
);
4493 /* Inserting chars while "im_is_active" is set does not cause a change of
4494 * buffer. When the chars are committed the buffer must be marked as
4496 if (!commit_with_preedit
)
4497 preedit_start_col
= MAXCOL
;
4499 /* This flag is used in changed() at next call. */
4500 xim_changed_while_preediting
= TRUE
;
4502 if (gtk_main_level() > 0)
4508 * Callback invoked after start to the preedit.
4510 # ifndef FEAT_GUI_MACVIM
4512 im_preedit_start_cb(GtkIMContext
*context UNUSED
, gpointer data UNUSED
)
4515 im_preedit_start_macvim()
4519 xim_log("im_preedit_start_cb()\n");
4522 im_is_active
= TRUE
;
4523 preedit_is_active
= TRUE
;
4524 gui_update_cursor(TRUE
, FALSE
);
4529 * Callback invoked after end to the preedit.
4531 # ifndef FEAT_GUI_MACVIM
4533 im_preedit_end_cb(GtkIMContext
*context UNUSED
, gpointer data UNUSED
)
4536 im_preedit_end_macvim()
4540 xim_log("im_preedit_end_cb()\n");
4542 im_delete_preedit();
4544 /* Indicate that preediting has finished */
4545 preedit_start_col
= MAXCOL
;
4546 xim_has_preediting
= FALSE
;
4549 /* Removal of this line suggested by Takuhiro Nishioka. Fixes that IM was
4550 * switched off unintentionally. We now use preedit_is_active (added by
4552 im_is_active
= FALSE
;
4554 preedit_is_active
= FALSE
;
4555 gui_update_cursor(TRUE
, FALSE
);
4559 #ifdef FEAT_GUI_MACVIM
4561 im_preedit_abandon_macvim()
4563 /* Abandon preedit text, don't send any backspace sequences. */
4564 im_preedit_cursor
= 0;
4565 im_preedit_trailing
= 0;
4567 im_preedit_end_macvim();
4572 * Callback invoked after changes to the preedit string. If the preedit
4573 * string was empty before, remember the preedit start column so we know
4574 * where to apply feedback attributes. Delete the previous preedit string
4575 * if there was one, save the new preedit cursor offset, and put the new
4576 * string into the input buffer.
4578 * TODO: The pragmatic "put into input buffer" approach used here has
4579 * several fundamental problems:
4581 * - The characters in the preedit string are subject to remapping.
4582 * That's broken, only the finally committed string should be remapped.
4584 * - There is a race condition involved: The retrieved value for the
4585 * current cursor position will be wrong if any unprocessed characters
4586 * are still queued in the input buffer.
4588 * - Due to the lack of synchronization between the file buffer in memory
4589 * and any typed characters, it's practically impossible to implement the
4590 * "retrieve_surrounding" and "delete_surrounding" signals reliably. IM
4591 * modules for languages such as Thai are likely to rely on this feature
4592 * for proper operation.
4594 * Conclusions: I think support for preediting needs to be moved to the
4595 * core parts of Vim. Ideally, until it has been committed, the preediting
4596 * string should only be displayed and not affect the buffer content at all.
4597 * The question how to deal with the synchronization issue still remains.
4598 * Circumventing the input buffer is probably not desirable. Anyway, I think
4599 * implementing "retrieve_surrounding" is the only hard problem.
4601 * One way to solve all of this in a clean manner would be to queue all key
4602 * press/release events "as is" in the input buffer, and apply the IM filtering
4603 * at the receiving end of the queue. This, however, would have a rather large
4604 * impact on the code base. If there is an easy way to force processing of all
4605 * remaining input from within the "retrieve_surrounding" signal handler, this
4606 * might not be necessary. Gotta ask on vim-dev for opinions.
4608 # ifndef FEAT_GUI_MACVIM
4610 im_preedit_changed_cb(GtkIMContext
*context
, gpointer data UNUSED
)
4613 im_preedit_changed_macvim(char *preedit_string
, int start_index
, int cursor_index
)
4616 # ifndef FEAT_GUI_MACVIM
4617 char *preedit_string
= NULL
;
4618 int cursor_index
= 0;
4620 int num_move_back
= 0;
4625 # ifndef FEAT_GUI_MACVIM
4626 gtk_im_context_get_preedit_string(context
,
4627 &preedit_string
, NULL
,
4630 im_preedit_start
= start_index
;
4634 xim_log("im_preedit_changed_cb(): %s\n", preedit_string
);
4637 g_return_if_fail(preedit_string
!= NULL
); /* just in case */
4639 /* If preedit_start_col is MAXCOL set it to the current cursor position. */
4640 if (preedit_start_col
== MAXCOL
&& preedit_string
[0] != '\0')
4642 xim_has_preediting
= TRUE
;
4644 /* Urgh, this breaks if the input buffer isn't empty now */
4645 init_preedit_start_col();
4647 else if (cursor_index
== 0 && preedit_string
[0] == '\0')
4649 xim_has_preediting
= FALSE
;
4651 /* If at the start position (after typing backspace)
4652 * preedit_start_col must be reset. */
4653 preedit_start_col
= MAXCOL
;
4656 im_delete_preedit();
4659 * Compute the end of the preediting area: "preedit_end_col".
4660 * According to the documentation of gtk_im_context_get_preedit_string(),
4661 * the cursor_pos output argument returns the offset in bytes. This is
4662 * unfortunately not true -- real life shows the offset is in characters,
4663 * and the GTK+ source code agrees with me. Will file a bug later.
4665 if (preedit_start_col
!= MAXCOL
)
4666 preedit_end_col
= preedit_start_col
;
4667 str
= (char_u
*)preedit_string
;
4668 for (p
= str
, i
= 0; *p
!= NUL
; p
+= utf_byte2len(*p
), ++i
)
4672 is_composing
= ((*p
& 0x80) != 0 && utf_iscomposing(utf_ptr2char(p
)));
4674 * These offsets are used as counters when generating <BS> and <Del>
4675 * to delete the preedit string. So don't count composing characters
4676 * unless 'delcombine' is enabled.
4678 if (!is_composing
|| p_deco
)
4680 if (i
< cursor_index
)
4681 ++im_preedit_cursor
;
4683 ++im_preedit_trailing
;
4685 if (!is_composing
&& i
>= cursor_index
)
4687 /* This is essentially the same as im_preedit_trailing, except
4688 * composing characters are not counted even if p_deco is set. */
4691 if (preedit_start_col
!= MAXCOL
)
4692 preedit_end_col
+= utf_ptr2cells(p
);
4697 im_add_to_input(str
, (int)(p
- str
));
4698 im_correct_cursor(num_move_back
);
4701 # ifndef FEAT_GUI_MACVIM
4702 g_free(preedit_string
);
4704 if (gtk_main_level() > 0)
4709 # ifndef FEAT_GUI_MACVIM
4711 * Translate the Pango attributes at iter to Vim highlighting attributes.
4712 * Ignore attributes not supported by Vim highlighting. This shouldn't have
4713 * too much impact -- right now we handle even more attributes than necessary
4714 * for the IM modules I tested with.
4717 translate_pango_attributes(PangoAttrIterator
*iter
)
4719 PangoAttribute
*attr
;
4720 int char_attr
= HL_NORMAL
;
4722 attr
= pango_attr_iterator_get(iter
, PANGO_ATTR_UNDERLINE
);
4723 if (attr
!= NULL
&& ((PangoAttrInt
*)attr
)->value
4724 != (int)PANGO_UNDERLINE_NONE
)
4725 char_attr
|= HL_UNDERLINE
;
4727 attr
= pango_attr_iterator_get(iter
, PANGO_ATTR_WEIGHT
);
4728 if (attr
!= NULL
&& ((PangoAttrInt
*)attr
)->value
>= (int)PANGO_WEIGHT_BOLD
)
4729 char_attr
|= HL_BOLD
;
4731 attr
= pango_attr_iterator_get(iter
, PANGO_ATTR_STYLE
);
4732 if (attr
!= NULL
&& ((PangoAttrInt
*)attr
)->value
4733 != (int)PANGO_STYLE_NORMAL
)
4734 char_attr
|= HL_ITALIC
;
4736 attr
= pango_attr_iterator_get(iter
, PANGO_ATTR_BACKGROUND
);
4739 const PangoColor
*color
= &((PangoAttrColor
*)attr
)->color
;
4741 /* Assume inverse if black background is requested */
4742 if ((color
->red
| color
->green
| color
->blue
) == 0)
4743 char_attr
|= HL_INVERSE
;
4751 * Retrieve the highlighting attributes at column col in the preedit string.
4752 * Return -1 if not in preediting mode or if col is out of range.
4755 im_get_feedback_attr(int col
)
4757 # ifndef FEAT_GUI_MACVIM
4758 char *preedit_string
= NULL
;
4759 PangoAttrList
*attr_list
= NULL
;
4765 gtk_im_context_get_preedit_string(xic
, &preedit_string
, &attr_list
, NULL
);
4767 if (preedit_string
!= NULL
&& attr_list
!= NULL
)
4771 /* Get the byte index as used by PangoAttrIterator */
4772 for (idx
= 0; col
> 0 && preedit_string
[idx
] != '\0'; --col
)
4773 idx
+= utfc_ptr2len((char_u
*)preedit_string
+ idx
);
4775 if (preedit_string
[idx
] != '\0')
4777 PangoAttrIterator
*iter
;
4780 char_attr
= HL_NORMAL
;
4781 iter
= pango_attr_list_get_iterator(attr_list
);
4783 /* Extract all relevant attributes from the list. */
4786 pango_attr_iterator_range(iter
, &start
, &end
);
4788 if (idx
>= start
&& idx
< end
)
4789 char_attr
|= translate_pango_attributes(iter
);
4791 while (pango_attr_iterator_next(iter
));
4793 pango_attr_iterator_destroy(iter
);
4797 if (attr_list
!= NULL
)
4798 pango_attr_list_unref(attr_list
);
4799 g_free(preedit_string
);
4803 if (col
>= im_preedit_start
&& col
< im_preedit_cursor
)
4804 return HL_THICKUNDERLINE
;
4806 return HL_UNDERLINE
;
4814 xim_log("xim_init()\n");
4817 # ifndef FEAT_GUI_MACVIM
4818 g_return_if_fail(gui
.drawarea
!= NULL
);
4819 g_return_if_fail(gui
.drawarea
->window
!= NULL
);
4821 xic
= gtk_im_multicontext_new();
4824 im_commit_handler_id
= g_signal_connect(G_OBJECT(xic
), "commit",
4825 G_CALLBACK(&im_commit_cb
), NULL
);
4826 g_signal_connect(G_OBJECT(xic
), "preedit_changed",
4827 G_CALLBACK(&im_preedit_changed_cb
), NULL
);
4828 g_signal_connect(G_OBJECT(xic
), "preedit_start",
4829 G_CALLBACK(&im_preedit_start_cb
), NULL
);
4830 g_signal_connect(G_OBJECT(xic
), "preedit_end",
4831 G_CALLBACK(&im_preedit_end_cb
), NULL
);
4833 gtk_im_context_set_client_window(xic
, gui
.drawarea
->window
);
4841 xim_log("im_shutdown()\n");
4844 # ifndef FEAT_GUI_MACVIM
4847 gtk_im_context_focus_out(xic
);
4848 g_object_unref(xic
);
4852 im_is_active
= FALSE
;
4853 im_commit_handler_id
= 0;
4854 preedit_start_col
= MAXCOL
;
4855 xim_has_preediting
= FALSE
;
4858 # ifndef FEAT_GUI_MACVIM
4860 * Convert the string argument to keyval and state for GdkEventKey.
4861 * If str is valid return TRUE, otherwise FALSE.
4863 * See 'imactivatekey' for documentation of the format.
4866 im_string_to_keyval(const char *str
, unsigned int *keyval
, unsigned int *state
)
4868 const char *mods_end
;
4869 unsigned tmp_keyval
;
4870 unsigned tmp_state
= 0;
4872 mods_end
= strrchr(str
, '-');
4873 mods_end
= (mods_end
!= NULL
) ? mods_end
+ 1 : str
;
4875 /* Parse modifier keys */
4876 while (str
< mods_end
)
4880 case 'S': case 's': tmp_state
|= (unsigned)GDK_SHIFT_MASK
; break;
4881 case 'L': case 'l': tmp_state
|= (unsigned)GDK_LOCK_MASK
; break;
4882 case 'C': case 'c': tmp_state
|= (unsigned)GDK_CONTROL_MASK
;break;
4883 case '1': tmp_state
|= (unsigned)GDK_MOD1_MASK
; break;
4884 case '2': tmp_state
|= (unsigned)GDK_MOD2_MASK
; break;
4885 case '3': tmp_state
|= (unsigned)GDK_MOD3_MASK
; break;
4886 case '4': tmp_state
|= (unsigned)GDK_MOD4_MASK
; break;
4887 case '5': tmp_state
|= (unsigned)GDK_MOD5_MASK
; break;
4892 tmp_keyval
= gdk_keyval_from_name(str
);
4894 if (tmp_keyval
== 0 || tmp_keyval
== GDK_VoidSymbol
)
4898 *keyval
= tmp_keyval
;
4906 * Return TRUE if p_imak is valid, otherwise FALSE. As a special case, an
4907 * empty string is also regarded as valid.
4909 * Note: The numerical key value of p_imak is cached if it was valid; thus
4910 * boldly assuming im_xim_isvalid_imactivate() will always be called whenever
4911 * 'imak' changes. This is currently the case but not obvious -- should
4912 * probably rename the function for clarity.
4915 im_xim_isvalid_imactivate(void)
4917 if (p_imak
[0] == NUL
)
4919 im_activatekey_keyval
= GDK_VoidSymbol
;
4920 im_activatekey_state
= 0;
4924 return im_string_to_keyval((const char *)p_imak
,
4925 &im_activatekey_keyval
,
4926 &im_activatekey_state
);
4930 im_synthesize_keypress(unsigned int keyval
, unsigned int state
)
4934 # ifdef HAVE_GTK_MULTIHEAD
4935 event
= (GdkEventKey
*)gdk_event_new(GDK_KEY_PRESS
);
4936 g_object_ref(gui
.drawarea
->window
); /* unreffed by gdk_event_free() */
4938 event
= (GdkEventKey
*)g_malloc0((gulong
)sizeof(GdkEvent
));
4939 event
->type
= GDK_KEY_PRESS
;
4941 event
->window
= gui
.drawarea
->window
;
4942 event
->send_event
= TRUE
;
4943 event
->time
= GDK_CURRENT_TIME
;
4944 event
->state
= state
;
4945 event
->keyval
= keyval
;
4946 event
->hardware_keycode
= /* needed for XIM */
4947 XKeysymToKeycode(GDK_WINDOW_XDISPLAY(event
->window
), (KeySym
)keyval
);
4949 event
->string
= NULL
;
4951 gtk_im_context_filter_keypress(xic
, event
);
4953 /* For consistency, also send the corresponding release event. */
4954 event
->type
= GDK_KEY_RELEASE
;
4955 event
->send_event
= FALSE
;
4956 gtk_im_context_filter_keypress(xic
, event
);
4958 # ifdef HAVE_GTK_MULTIHEAD
4959 gdk_event_free((GdkEvent
*)event
);
4964 # endif // FEAT_GUI_MACVIM
4969 # ifndef FEAT_GUI_MACVIM
4973 * The third-party imhangul module (and maybe others too) ignores
4974 * gtk_im_context_reset() or at least doesn't reset the active state.
4975 * Thus sending imactivatekey would turn it off if it was on before,
4976 * which is clearly not what we want. Fortunately we can work around
4977 * that for imhangul by sending GDK_Escape, but I don't know if it
4978 * works with all IM modules that support an activation key :/
4980 * An alternative approach would be to destroy the IM context and
4981 * recreate it. But that means loading/unloading the IM module on
4982 * every mode switch, which causes a quite noticable delay even on
4983 * my rather fast box...
4985 * Moreover, there are some XIM which cannot respond to
4986 * im_synthesize_keypress(). we hope that they reset by
4989 if (im_activatekey_keyval
!= GDK_VoidSymbol
&& im_is_active
)
4990 im_synthesize_keypress(GDK_Escape
, 0U);
4992 gtk_im_context_reset(xic
);
4995 * HACK for Ami: This sequence of function calls makes Ami handle
4996 * the IM reset graciously, without breaking loads of other stuff.
4997 * It seems to force English mode as well, which is exactly what we
4998 * want because it makes the Ami status display work reliably.
5000 gtk_im_context_set_use_preedit(xic
, FALSE
);
5006 gtk_im_context_set_use_preedit(xic
, TRUE
);
5007 xim_set_focus(gui
.in_focus
);
5009 if (im_activatekey_keyval
!= GDK_VoidSymbol
)
5013 g_signal_handler_block(xic
, im_commit_handler_id
);
5014 im_synthesize_keypress(im_activatekey_keyval
,
5015 im_activatekey_state
);
5016 g_signal_handler_unblock(xic
, im_commit_handler_id
);
5023 xim_set_focus(gui
.in_focus
);
5029 preedit_start_col
= MAXCOL
;
5030 xim_has_preediting
= FALSE
;
5033 # ifndef FEAT_GUI_MACVIM
5035 xim_queue_key_press_event(GdkEventKey
*event
, int down
)
5040 * Workaround GTK2 XIM 'feature' that always converts keypad keys to
5041 * chars., even when not part of an IM sequence (ref. feature of
5043 * Flag any keypad keys that might represent a single char.
5044 * If this (on its own - i.e., not part of an IM sequence) is
5045 * committed while we're processing one of these keys, we can ignore
5046 * that commit and go ahead & process it ourselves. That way we can
5047 * still distinguish keypad keys for use in mappings.
5048 * Also add GDK_space to make <S-Space> work.
5050 switch (event
->keyval
)
5052 case GDK_KP_Add
: xim_expected_char
= '+'; break;
5053 case GDK_KP_Subtract
: xim_expected_char
= '-'; break;
5054 case GDK_KP_Divide
: xim_expected_char
= '/'; break;
5055 case GDK_KP_Multiply
: xim_expected_char
= '*'; break;
5056 case GDK_KP_Decimal
: xim_expected_char
= '.'; break;
5057 case GDK_KP_Equal
: xim_expected_char
= '='; break;
5058 case GDK_KP_0
: xim_expected_char
= '0'; break;
5059 case GDK_KP_1
: xim_expected_char
= '1'; break;
5060 case GDK_KP_2
: xim_expected_char
= '2'; break;
5061 case GDK_KP_3
: xim_expected_char
= '3'; break;
5062 case GDK_KP_4
: xim_expected_char
= '4'; break;
5063 case GDK_KP_5
: xim_expected_char
= '5'; break;
5064 case GDK_KP_6
: xim_expected_char
= '6'; break;
5065 case GDK_KP_7
: xim_expected_char
= '7'; break;
5066 case GDK_KP_8
: xim_expected_char
= '8'; break;
5067 case GDK_KP_9
: xim_expected_char
= '9'; break;
5068 case GDK_space
: xim_expected_char
= ' '; break;
5069 default: xim_expected_char
= NUL
;
5071 xim_ignored_char
= FALSE
;
5075 * When typing fFtT, XIM may be activated. Thus it must pass
5076 * gtk_im_context_filter_keypress() in Normal mode.
5077 * And while doing :sh too.
5079 if (xic
!= NULL
&& !p_imdisable
5080 && (State
& (INSERT
| CMDLINE
| NORMAL
| EXTERNCMD
)) != 0)
5083 * Filter 'imactivatekey' and map it to CTRL-^. This way, Vim is
5084 * always aware of the current status of IM, and can even emulate
5085 * the activation key for modules that don't support one.
5087 if (event
->keyval
== im_activatekey_keyval
5088 && (event
->state
& im_activatekey_state
) == im_activatekey_state
)
5090 unsigned int state_mask
;
5092 /* Require the state of the 3 most used modifiers to match exactly.
5093 * Otherwise e.g. <S-C-space> would be unusable for other purposes
5094 * if the IM activate key is <S-space>. */
5095 state_mask
= im_activatekey_state
;
5096 state_mask
|= ((int)GDK_SHIFT_MASK
| (int)GDK_CONTROL_MASK
5097 | (int)GDK_MOD1_MASK
);
5099 if ((event
->state
& state_mask
) != im_activatekey_state
)
5102 /* Don't send it a second time on GDK_KEY_RELEASE. */
5103 if (event
->type
!= GDK_KEY_PRESS
)
5106 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
5108 im_set_active(FALSE
);
5110 /* ":lmap" mappings exists, toggle use of mappings. */
5112 if (State
& LANGMAP
)
5114 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
5119 curbuf
->b_p_iminsert
= B_IMODE_LMAP
;
5125 return gtk_im_context_filter_keypress(xic
, event
);
5128 /* Don't filter events through the IM context if IM isn't active
5129 * right now. Unlike with GTK+ 1.2 we cannot rely on the IM module
5130 * not doing anything before the activation key was sent. */
5131 if (im_activatekey_keyval
== GDK_VoidSymbol
|| im_is_active
)
5133 int imresult
= gtk_im_context_filter_keypress(xic
, event
);
5135 /* Some XIM send following sequence:
5136 * 1. preedited string.
5137 * 2. committed string.
5138 * 3. line changed key.
5139 * 4. preedited string.
5140 * 5. remove preedited string.
5141 * if 3, Vim can't move back the above line for 5.
5142 * thus, this part should not parse the key. */
5143 if (!imresult
&& preedit_start_col
!= MAXCOL
5144 && event
->keyval
== GDK_Return
)
5146 im_synthesize_keypress(GDK_Return
, 0U);
5150 /* If XIM tried to commit a keypad key as a single char.,
5151 * ignore it so we can use the keypad key 'raw', for mappings. */
5152 if (xim_expected_char
!= NUL
&& xim_ignored_char
)
5153 /* We had a keypad key, and XIM tried to thieve it */
5156 /* Normal processing */
5165 # ifndef FEAT_GUI_MACVIM
5171 return uimfep_get_status();
5173 return im_is_active
;
5175 # else /* FEAT_GUI_MACVIM */
5180 return gui_im_get_status();
5182 return uimfep_get_status();
5186 # else /* !HAVE_GTK2 */
5188 static int xim_is_active
= FALSE
; /* XIM should be active in the current
5190 static int xim_has_focus
= FALSE
; /* XIM is really being used for Vim */
5192 static XIMStyle input_style
;
5193 static int status_area_enabled
= TRUE
;
5198 # include <gdk/gdkwin32.h>
5200 # include <gdk/gdkx.h>
5204 /* Define a few things to be able to generate prototypes while not configured
5207 # define gboolean int
5208 typedef int GdkEvent
;
5209 typedef int GdkEventKey
;
5214 #if defined(FEAT_GUI_GTK) || defined(PROTO)
5215 static int preedit_buf_len
= 0;
5216 static int xim_can_preediting
INIT(= FALSE
); /* XIM in showmode() */
5217 static int xim_input_style
;
5218 #ifndef FEAT_GUI_GTK
5219 # define gboolean int
5221 static gboolean use_status_area
= 0;
5223 static int im_xim_str2keycode
__ARGS((unsigned int *code
, unsigned int *state
));
5224 static void im_xim_send_event_imactivate
__ARGS((void));
5227 * Convert string to keycode and state for XKeyEvent.
5228 * When string is valid return OK, when invalid return FAIL.
5230 * See 'imactivatekey' documentation for the format.
5233 im_xim_str2keycode(code
, state
)
5235 unsigned int *state
;
5239 unsigned keycode
= 0, keystate
= 0;
5247 len
= STRLEN(p_imak
);
5248 for (flag_end
= p_imak
+ len
- 1;
5249 flag_end
> p_imak
&& *flag_end
!= '-'; --flag_end
)
5252 /* Parse modifier keys */
5253 for (str
= p_imak
; str
< flag_end
; ++str
)
5258 keystate
|= ShiftMask
;
5261 keystate
|= LockMask
;
5264 keystate
|= ControlMask
;
5267 keystate
|= Mod1Mask
;
5270 keystate
|= Mod2Mask
;
5273 keystate
|= Mod3Mask
;
5276 keystate
|= Mod4Mask
;
5279 keystate
|= Mod5Mask
;
5290 /* Get keycode from string. */
5291 gui_get_x11_windis(&window
, &display
);
5293 keycode
= XKeysymToKeycode(display
, XStringToKeysym((char *)str
));
5306 im_xim_send_event_imactivate()
5308 /* Force turn on preedit state by symulate keypress event.
5309 * Keycode and state is specified by 'imactivatekey'.
5313 gui_get_x11_windis(&ev
.window
, &ev
.display
);
5314 ev
.root
= RootWindow(ev
.display
, DefaultScreen(ev
.display
));
5315 ev
.subwindow
= None
;
5316 ev
.time
= CurrentTime
;
5323 if (im_xim_str2keycode(&ev
.keycode
, &ev
.state
) == OK
)
5324 XSendEvent(ev
.display
, ev
.window
, 1, KeyPressMask
, (XEvent
*)&ev
);
5328 * Return TRUE if 'imactivatekey' has a valid value.
5331 im_xim_isvalid_imactivate()
5333 return im_xim_str2keycode(NULL
, NULL
) == OK
;
5335 #endif /* FEAT_GUI_GTK */
5338 * Switch using XIM on/off. This is used by the code that changes "State".
5341 im_set_active(active
)
5348 uimfep_set_active(active
);
5353 /* If 'imdisable' is set, XIM is never active. */
5356 #if !defined (FEAT_GUI_GTK)
5357 else if (input_style
& XIMPreeditPosition
)
5358 /* There is a problem in switching XIM off when preediting is used,
5359 * and it is not clear how this can be solved. For now, keep XIM on
5360 * all the time, like it was done in Vim 5.8. */
5364 /* Remember the active state, it is needed when Vim gets keyboard focus. */
5365 xim_is_active
= active
;
5368 /* When 'imactivatekey' has valid key-string, try to control XIM preedit
5369 * state. When 'imactivatekey' has no or invalid string, try old XIM
5375 * Destroy old Input Context (XIC), and create new one. New XIC
5376 * would have a state of preedit that is off. When argument:active
5377 * is false, that's all. Else argument:active is true, send a key
5378 * event specified by 'imactivatekey' to activate XIM preedit state.
5381 xim_is_active
= TRUE
; /* Disable old XIM focus control */
5382 /* If we can monitor preedit state with preedit callback functions,
5383 * try least creation of new XIC.
5385 if (xim_input_style
& (int)GDK_IM_PREEDIT_CALLBACKS
)
5387 if (xim_can_preediting
&& !active
)
5389 /* Force turn off preedit state. With some IM
5390 * implementations, we cannot turn off preedit state by
5391 * symulate keypress event. It is why using such a method
5392 * that destroy old IC (input context), and create new one.
5393 * When create new IC, its preedit state is usually off.
5396 xim_set_focus(FALSE
);
5397 gdk_ic_destroy(xic
);
5399 xim_can_preediting
= FALSE
;
5401 else if (!xim_can_preediting
&& active
)
5402 im_xim_send_event_imactivate();
5406 /* First, force destroy old IC, and create new one. It
5407 * symulates "turning off preedit state".
5409 xim_set_focus(FALSE
);
5410 gdk_ic_destroy(xic
);
5412 xim_can_preediting
= FALSE
;
5414 /* 2nd, when requested to activate IM, symulate this by sending
5419 im_xim_send_event_imactivate();
5420 xim_can_preediting
= TRUE
;
5426 # ifndef XIMPreeditUnKnown
5427 /* X11R5 doesn't have these, it looks safe enough to define here. */
5428 typedef unsigned long XIMPreeditState
;
5429 # define XIMPreeditUnKnown 0L
5430 # define XIMPreeditEnable 1L
5431 # define XIMPreeditDisable (1L<<1)
5432 # define XNPreeditState "preeditState"
5434 XIMPreeditState preedit_state
= XIMPreeditUnKnown
;
5435 XVaNestedList preedit_attr
;
5438 preedit_attr
= XVaCreateNestedList(0,
5439 XNPreeditState
, &preedit_state
,
5441 pxic
= ((GdkICPrivate
*)xic
)->xic
;
5443 if (!XGetICValues(pxic
, XNPreeditAttributes
, preedit_attr
, NULL
))
5445 XFree(preedit_attr
);
5446 preedit_attr
= XVaCreateNestedList(0,
5448 active
? XIMPreeditEnable
: XIMPreeditDisable
,
5450 XSetICValues(pxic
, XNPreeditAttributes
, preedit_attr
, NULL
);
5451 xim_can_preediting
= active
;
5452 xim_is_active
= active
;
5454 XFree(preedit_attr
);
5456 if (xim_input_style
& XIMPreeditCallbacks
)
5458 preedit_buf_len
= 0;
5459 init_preedit_start_col();
5463 /* When had tested kinput2 + canna + Athena GUI version with
5464 * 'imactivatekey' is "s-space", im_xim_send_event_imactivate() did not
5465 * work correctly. It just inserted one space. I don't know why we
5466 * couldn't switch state of XIM preediting. This is reason why these
5467 * codes are commented out.
5469 /* First, force destroy old IC, and create new one. It symulates
5470 * "turning off preedit state".
5472 xim_set_focus(FALSE
);
5477 /* 2nd, when requested to activate IM, symulate this by sending the
5481 im_xim_send_event_imactivate();
5488 * Adjust using XIM for gaining or losing keyboard focus. Also called when
5489 * "xim_is_active" changes.
5492 xim_set_focus(focus
)
5499 * XIM only gets focus when the Vim window has keyboard focus and XIM has
5500 * been set active for the current mode.
5502 if (focus
&& xim_is_active
)
5506 xim_has_focus
= TRUE
;
5508 gdk_im_begin(xic
, gui
.drawarea
->window
);
5518 xim_has_focus
= FALSE
;
5529 im_set_position(row
, col
)
5537 * Set the XIM to the current cursor position.
5545 xim_set_focus(TRUE
);
5559 # ifdef FEAT_XFONTSET
5560 if ((xim_input_style
& (int)GDK_IM_PREEDIT_POSITION
)
5561 && gui
.fontset
!= NOFONTSET
5562 && gui
.fontset
->type
== GDK_FONT_FONTSET
)
5566 if (attr
->spot_location
.y
>= 0)
5568 attr
->spot_location
.x
= 0;
5569 attr
->spot_location
.y
= -100;
5570 attrmask
|= (int)GDK_IC_SPOT_LOCATION
;
5577 if (attr
->spot_location
.x
!= TEXT_X(gui
.col
)
5578 || attr
->spot_location
.y
!= TEXT_Y(gui
.row
))
5580 attr
->spot_location
.x
= TEXT_X(gui
.col
);
5581 attr
->spot_location
.y
= TEXT_Y(gui
.row
);
5582 attrmask
|= (int)GDK_IC_SPOT_LOCATION
;
5585 gdk_window_get_size(gui
.drawarea
->window
, &width
, &height
);
5586 width
-= 2 * gui
.border_offset
;
5587 height
-= 2 * gui
.border_offset
;
5588 if (xim_input_style
& (int)GDK_IM_STATUS_AREA
)
5589 height
-= gui
.char_height
;
5590 if (attr
->preedit_area
.width
!= width
5591 || attr
->preedit_area
.height
!= height
)
5593 attr
->preedit_area
.x
= gui
.border_offset
;
5594 attr
->preedit_area
.y
= gui
.border_offset
;
5595 attr
->preedit_area
.width
= width
;
5596 attr
->preedit_area
.height
= height
;
5597 attrmask
|= (int)GDK_IC_PREEDIT_AREA
;
5600 if (attr
->preedit_fontset
!= gui
.current_font
)
5602 attr
->preedit_fontset
= gui
.current_font
;
5603 attrmask
|= (int)GDK_IC_PREEDIT_FONTSET
;
5607 # endif /* FEAT_XFONTSET */
5609 if (xim_fg_color
== INVALCOLOR
)
5611 xim_fg_color
= gui
.def_norm_pixel
;
5612 xim_bg_color
= gui
.def_back_pixel
;
5614 if (attr
->preedit_foreground
.pixel
!= xim_fg_color
)
5616 attr
->preedit_foreground
.pixel
= xim_fg_color
;
5617 attrmask
|= (int)GDK_IC_PREEDIT_FOREGROUND
;
5619 if (attr
->preedit_background
.pixel
!= xim_bg_color
)
5621 attr
->preedit_background
.pixel
= xim_bg_color
;
5622 attrmask
|= (int)GDK_IC_PREEDIT_BACKGROUND
;
5626 gdk_ic_set_attr(xic
, attr
, (GdkICAttributesType
)attrmask
);
5628 #else /* FEAT_GUI_GTK */
5630 XVaNestedList attr_list
;
5631 XRectangle spot_area
;
5637 /* hide XIM cursor */
5639 over_spot
.y
= -100; /* arbitrary invisible position */
5640 attr_list
= (XVaNestedList
) XVaCreateNestedList(0,
5644 XSetICValues(xic
, XNPreeditAttributes
, attr_list
, NULL
);
5649 if (input_style
& XIMPreeditPosition
)
5651 if (xim_fg_color
== INVALCOLOR
)
5653 xim_fg_color
= gui
.def_norm_pixel
;
5654 xim_bg_color
= gui
.def_back_pixel
;
5656 over_spot
.x
= TEXT_X(gui
.col
);
5657 over_spot
.y
= TEXT_Y(gui
.row
);
5660 spot_area
.height
= gui
.char_height
* Rows
;
5661 spot_area
.width
= gui
.char_width
* Columns
;
5662 line_space
= gui
.char_height
;
5663 attr_list
= (XVaNestedList
) XVaCreateNestedList(0,
5664 XNSpotLocation
, &over_spot
,
5665 XNForeground
, (Pixel
) xim_fg_color
,
5666 XNBackground
, (Pixel
) xim_bg_color
,
5668 XNLineSpace
, line_space
,
5670 if (XSetICValues(xic
, XNPreeditAttributes
, attr_list
, NULL
))
5671 EMSG(_("E284: Cannot set IC values"));
5675 #endif /* FEAT_GUI_GTK */
5679 * Set up the status area.
5681 * This should use a separate Widget, but that seems not possible, because
5682 * preedit_area and status_area should be set to the same window as for the
5683 * text input. Unfortunately this means the status area pollutes the text
5687 xim_set_status_area()
5693 # if defined(FEAT_XFONTSET)
5694 if (use_status_area
)
5707 style
= (int)gdk_ic_get_style(xic
);
5708 if ((style
& (int)GDK_IM_STATUS_MASK
) == (int)GDK_IM_STATUS_AREA
)
5710 if (gui
.fontset
!= NOFONTSET
5711 && gui
.fontset
->type
== GDK_FONT_FONTSET
)
5713 widget
= gui
.mainwin
;
5714 gdk_window_get_size(widget
->window
, &width
, &height
);
5716 attrmask
|= (int)GDK_IC_STATUS_AREA
;
5717 attr
->status_area
.x
= 0;
5718 attr
->status_area
.y
= height
- gui
.char_height
- 1;
5719 attr
->status_area
.width
= width
;
5720 attr
->status_area
.height
= gui
.char_height
;
5724 gdk_ic_set_attr(xic
, attr
, (GdkICAttributesType
)attrmask
);
5729 XVaNestedList preedit_list
= 0, status_list
= 0, list
= 0;
5730 XRectangle pre_area
, status_area
;
5732 if (input_style
& XIMStatusArea
)
5734 if (input_style
& XIMPreeditArea
)
5736 XRectangle
*needed_rect
;
5738 /* to get status_area width */
5739 status_list
= XVaCreateNestedList(0, XNAreaNeeded
,
5740 &needed_rect
, NULL
);
5741 XGetICValues(xic
, XNStatusAttributes
, status_list
, NULL
);
5744 status_area
.width
= needed_rect
->width
;
5747 status_area
.width
= gui
.char_width
* Columns
;
5750 status_area
.y
= gui
.char_height
* Rows
+ gui
.border_offset
;
5751 if (gui
.which_scrollbars
[SBAR_BOTTOM
])
5752 status_area
.y
+= gui
.scrollbar_height
;
5754 if (gui
.menu_is_active
)
5755 status_area
.y
+= gui
.menu_height
;
5757 status_area
.height
= gui
.char_height
;
5758 status_list
= XVaCreateNestedList(0, XNArea
, &status_area
, NULL
);
5763 status_area
.y
= gui
.char_height
* Rows
+ gui
.border_offset
;
5764 if (gui
.which_scrollbars
[SBAR_BOTTOM
])
5765 status_area
.y
+= gui
.scrollbar_height
;
5767 if (gui
.menu_is_active
)
5768 status_area
.y
+= gui
.menu_height
;
5770 status_area
.width
= 0;
5771 status_area
.height
= gui
.char_height
;
5774 if (input_style
& XIMPreeditArea
) /* off-the-spot */
5776 pre_area
.x
= status_area
.x
+ status_area
.width
;
5777 pre_area
.y
= gui
.char_height
* Rows
+ gui
.border_offset
;
5778 pre_area
.width
= gui
.char_width
* Columns
- pre_area
.x
;
5779 if (gui
.which_scrollbars
[SBAR_BOTTOM
])
5780 pre_area
.y
+= gui
.scrollbar_height
;
5782 if (gui
.menu_is_active
)
5783 pre_area
.y
+= gui
.menu_height
;
5785 pre_area
.height
= gui
.char_height
;
5786 preedit_list
= XVaCreateNestedList(0, XNArea
, &pre_area
, NULL
);
5788 else if (input_style
& XIMPreeditPosition
) /* over-the-spot */
5792 pre_area
.height
= gui
.char_height
* Rows
;
5793 pre_area
.width
= gui
.char_width
* Columns
;
5794 preedit_list
= XVaCreateNestedList(0, XNArea
, &pre_area
, NULL
);
5797 if (preedit_list
&& status_list
)
5798 list
= XVaCreateNestedList(0, XNPreeditAttributes
, preedit_list
,
5799 XNStatusAttributes
, status_list
, NULL
);
5800 else if (preedit_list
)
5801 list
= XVaCreateNestedList(0, XNPreeditAttributes
, preedit_list
,
5803 else if (status_list
)
5804 list
= XVaCreateNestedList(0, XNStatusAttributes
, status_list
,
5811 XSetICValues(xic
, XNVaNestedList
, list
, NULL
);
5817 XFree(preedit_list
);
5822 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
5823 static char e_xim
[] = N_("E285: Failed to create input context");
5826 #if defined(FEAT_GUI_X11) || defined(PROTO)
5827 # if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && !defined(sun)
5828 # define USE_X11R6_XIM
5831 static int xim_real_init
__ARGS((Window x11_window
, Display
*x11_display
));
5834 #ifdef USE_X11R6_XIM
5835 static void xim_instantiate_cb
__ARGS((Display
*display
, XPointer client_data
, XPointer call_data
));
5836 static void xim_destroy_cb
__ARGS((XIM im
, XPointer client_data
, XPointer call_data
));
5839 xim_instantiate_cb(display
, client_data
, call_data
)
5841 XPointer client_data UNUSED
;
5842 XPointer call_data UNUSED
;
5845 Display
*x11_display
;
5848 xim_log("xim_instantiate_cb()\n");
5851 gui_get_x11_windis(&x11_window
, &x11_display
);
5852 if (display
!= x11_display
)
5855 xim_real_init(x11_window
, x11_display
);
5856 gui_set_shellsize(FALSE
, FALSE
, RESIZE_BOTH
);
5858 XUnregisterIMInstantiateCallback(x11_display
, NULL
, NULL
, NULL
,
5859 xim_instantiate_cb
, NULL
);
5863 xim_destroy_cb(im
, client_data
, call_data
)
5865 XPointer client_data UNUSED
;
5866 XPointer call_data UNUSED
;
5869 Display
*x11_display
;
5872 xim_log("xim_destroy_cb()\n");
5874 gui_get_x11_windis(&x11_window
, &x11_display
);
5877 status_area_enabled
= FALSE
;
5879 gui_set_shellsize(FALSE
, FALSE
, RESIZE_BOTH
);
5881 XRegisterIMInstantiateCallback(x11_display
, NULL
, NULL
, NULL
,
5882 xim_instantiate_cb
, NULL
);
5890 Display
*x11_display
;
5893 xim_log("xim_init()\n");
5896 gui_get_x11_windis(&x11_window
, &x11_display
);
5900 if (xim_real_init(x11_window
, x11_display
))
5903 gui_set_shellsize(FALSE
, FALSE
, RESIZE_BOTH
);
5905 #ifdef USE_X11R6_XIM
5906 XRegisterIMInstantiateCallback(x11_display
, NULL
, NULL
, NULL
,
5907 xim_instantiate_cb
, NULL
);
5912 xim_real_init(x11_window
, x11_display
)
5914 Display
*x11_display
;
5922 #define IMLEN_MAX 40
5923 char buf
[IMLEN_MAX
+ 7];
5925 XIMStyles
*xim_styles
;
5926 XIMStyle this_input_style
= 0;
5929 XVaNestedList preedit_list
, status_list
;
5932 status_area_enabled
= FALSE
;
5937 if (gui
.rsrc_input_method
!= NULL
&& *gui
.rsrc_input_method
!= NUL
)
5939 strcpy(tmp
, gui
.rsrc_input_method
);
5940 for (ns
= s
= tmp
; ns
!= NULL
&& *s
!= NUL
;)
5942 s
= (char *)skipwhite((char_u
*)s
);
5945 if ((ns
= end
= strchr(s
, ',')) == NULL
)
5946 end
= s
+ strlen(s
);
5947 while (isspace(((char_u
*)end
)[-1]))
5951 if (strlen(s
) <= IMLEN_MAX
)
5953 strcpy(buf
, "@im=");
5955 if ((p
= XSetLocaleModifiers(buf
)) != NULL
&& *p
!= NUL
5956 && (xim
= XOpenIM(x11_display
, NULL
, NULL
, NULL
))
5965 if (xim
== NULL
&& (p
= XSetLocaleModifiers("")) != NULL
&& *p
!= NUL
)
5966 xim
= XOpenIM(x11_display
, NULL
, NULL
, NULL
);
5968 /* This is supposed to be useful to obtain characters through
5969 * XmbLookupString() without really using a XIM. */
5970 if (xim
== NULL
&& (p
= XSetLocaleModifiers("@im=none")) != NULL
5972 xim
= XOpenIM(x11_display
, NULL
, NULL
, NULL
);
5976 /* Only give this message when verbose is set, because too many people
5977 * got this message when they didn't want to use a XIM. */
5981 EMSG(_("E286: Failed to open input method"));
5987 #ifdef USE_X11R6_XIM
5989 XIMCallback destroy_cb
;
5991 destroy_cb
.callback
= xim_destroy_cb
;
5992 destroy_cb
.client_data
= NULL
;
5993 if (XSetIMValues(xim
, XNDestroyCallback
, &destroy_cb
, NULL
))
5994 EMSG(_("E287: Warning: Could not set destroy callback to IM"));
5998 if (XGetIMValues(xim
, XNQueryInputStyle
, &xim_styles
, NULL
) || !xim_styles
)
6000 EMSG(_("E288: input method doesn't support any style"));
6006 strcpy(tmp
, gui
.rsrc_preedit_type_name
);
6007 for (s
= tmp
; s
&& !found
; )
6009 while (*s
&& isspace((unsigned char)*s
))
6013 if ((ns
= end
= strchr(s
, ',')) != 0)
6016 end
= s
+ strlen(s
);
6017 while (isspace((unsigned char)*end
))
6021 if (!strcmp(s
, "OverTheSpot"))
6022 this_input_style
= (XIMPreeditPosition
| XIMStatusArea
);
6023 else if (!strcmp(s
, "OffTheSpot"))
6024 this_input_style
= (XIMPreeditArea
| XIMStatusArea
);
6025 else if (!strcmp(s
, "Root"))
6026 this_input_style
= (XIMPreeditNothing
| XIMStatusNothing
);
6028 for (i
= 0; (unsigned short)i
< xim_styles
->count_styles
; i
++)
6030 if (this_input_style
== xim_styles
->supported_styles
[i
])
6037 for (i
= 0; (unsigned short)i
< xim_styles
->count_styles
; i
++)
6039 if ((xim_styles
->supported_styles
[i
] & this_input_style
)
6040 == (this_input_style
& ~XIMStatusArea
))
6042 this_input_style
&= ~XIMStatusArea
;
6054 /* Only give this message when verbose is set, because too many people
6055 * got this message when they didn't want to use a XIM. */
6059 EMSG(_("E289: input method doesn't support my preedit type"));
6066 over_spot
.x
= TEXT_X(gui
.col
);
6067 over_spot
.y
= TEXT_Y(gui
.row
);
6068 input_style
= this_input_style
;
6070 /* A crash was reported when trying to pass gui.norm_font as XNFontSet,
6071 * thus that has been removed. Hopefully the default works... */
6072 #ifdef FEAT_XFONTSET
6073 if (gui
.fontset
!= NOFONTSET
)
6075 preedit_list
= XVaCreateNestedList(0,
6076 XNSpotLocation
, &over_spot
,
6077 XNForeground
, (Pixel
)gui
.def_norm_pixel
,
6078 XNBackground
, (Pixel
)gui
.def_back_pixel
,
6079 XNFontSet
, (XFontSet
)gui
.fontset
,
6081 status_list
= XVaCreateNestedList(0,
6082 XNForeground
, (Pixel
)gui
.def_norm_pixel
,
6083 XNBackground
, (Pixel
)gui
.def_back_pixel
,
6084 XNFontSet
, (XFontSet
)gui
.fontset
,
6090 preedit_list
= XVaCreateNestedList(0,
6091 XNSpotLocation
, &over_spot
,
6092 XNForeground
, (Pixel
)gui
.def_norm_pixel
,
6093 XNBackground
, (Pixel
)gui
.def_back_pixel
,
6095 status_list
= XVaCreateNestedList(0,
6096 XNForeground
, (Pixel
)gui
.def_norm_pixel
,
6097 XNBackground
, (Pixel
)gui
.def_back_pixel
,
6101 xic
= XCreateIC(xim
,
6102 XNInputStyle
, input_style
,
6103 XNClientWindow
, x11_window
,
6104 XNFocusWindow
, gui
.wid
,
6105 XNPreeditAttributes
, preedit_list
,
6106 XNStatusAttributes
, status_list
,
6109 XFree(preedit_list
);
6112 if (input_style
& XIMStatusArea
)
6114 xim_set_status_area();
6115 status_area_enabled
= TRUE
;
6118 gui_set_shellsize(FALSE
, FALSE
, RESIZE_BOTH
);
6130 #endif /* FEAT_GUI_X11 */
6132 #if defined(FEAT_GUI_GTK) || defined(PROTO)
6134 # ifdef FEAT_XFONTSET
6135 static char e_overthespot
[] = N_("E290: over-the-spot style requires fontset");
6143 xim_decide_input_style()
6145 /* GDK_IM_STATUS_CALLBACKS was disabled, enabled it to allow Japanese
6147 int supported_style
= (int)GDK_IM_PREEDIT_NONE
|
6148 (int)GDK_IM_PREEDIT_NOTHING
|
6149 (int)GDK_IM_PREEDIT_POSITION
|
6150 (int)GDK_IM_PREEDIT_CALLBACKS
|
6151 (int)GDK_IM_STATUS_CALLBACKS
|
6152 (int)GDK_IM_STATUS_AREA
|
6153 (int)GDK_IM_STATUS_NONE
|
6154 (int)GDK_IM_STATUS_NOTHING
;
6157 xim_log("xim_decide_input_style()\n");
6160 if (!gdk_im_ready())
6161 xim_input_style
= 0;
6164 if (gtk_major_version
> 1
6165 || (gtk_major_version
== 1
6166 && (gtk_minor_version
> 2
6167 || (gtk_minor_version
== 2 && gtk_micro_version
>= 3))))
6168 use_status_area
= TRUE
;
6171 EMSG(_("E291: Your GTK+ is older than 1.2.3. Status area disabled"));
6172 use_status_area
= FALSE
;
6174 #ifdef FEAT_XFONTSET
6175 if (gui
.fontset
== NOFONTSET
|| gui
.fontset
->type
!= GDK_FONT_FONTSET
)
6177 supported_style
&= ~((int)GDK_IM_PREEDIT_POSITION
6178 | (int)GDK_IM_STATUS_AREA
);
6179 if (!use_status_area
)
6180 supported_style
&= ~(int)GDK_IM_STATUS_AREA
;
6181 xim_input_style
= (int)gdk_im_decide_style((GdkIMStyle
)supported_style
);
6186 preedit_start_cbproc(XIC thexic UNUSED
,
6187 XPointer client_data UNUSED
,
6188 XPointer call_data UNUSED
)
6191 xim_log("xim_decide_input_style()\n");
6194 draw_feedback
= NULL
;
6195 xim_can_preediting
= TRUE
;
6196 xim_has_preediting
= TRUE
;
6197 gui_update_cursor(TRUE
, FALSE
);
6206 xim_back_delete(int n
)
6214 add_to_input_buf(str
, 3);
6217 static GSList
*key_press_event_queue
= NULL
;
6218 static gboolean processing_queued_event
= FALSE
;
6221 preedit_draw_cbproc(XIC thexic UNUSED
,
6222 XPointer client_data UNUSED
,
6225 XIMPreeditDrawCallbackStruct
*draw_data
;
6228 GSList
*event_queue
;
6231 xim_log("preedit_draw_cbproc()\n");
6234 draw_data
= (XIMPreeditDrawCallbackStruct
*) call_data
;
6235 text
= (XIMText
*) draw_data
->text
;
6237 if ((text
== NULL
&& draw_data
->chg_length
== preedit_buf_len
)
6238 || preedit_buf_len
== 0)
6240 init_preedit_start_col();
6241 vim_free(draw_feedback
);
6242 draw_feedback
= NULL
;
6244 if (draw_data
->chg_length
> 0)
6248 if (draw_data
->chg_length
> preedit_buf_len
)
6249 bs_cnt
= preedit_buf_len
;
6251 bs_cnt
= draw_data
->chg_length
;
6252 xim_back_delete(bs_cnt
);
6253 preedit_buf_len
-= bs_cnt
;
6260 unsigned int nfeedback
= 0;
6264 src
= text
->string
.multi_byte
;
6265 if (src
!= NULL
&& !text
->encoding_is_wchar
)
6268 ptr
= (char_u
*)src
;
6269 /* Avoid the enter for decision */
6274 if (input_conv
.vc_type
!= CONV_NONE
6275 && (buf
= string_convert(&input_conv
,
6276 (char_u
*)src
, &len
)) != NULL
)
6278 /* Converted from 'termencoding' to 'encoding'. */
6279 add_to_input_buf_csi(buf
, len
);
6284 add_to_input_buf_csi((char_u
*)src
, len
);
6285 /* Add count of character to preedit_buf_len */
6289 if (draw_data
->text
->feedback
!= NULL
)
6291 if (draw_feedback
== NULL
)
6292 draw_feedback
= (char *)alloc(draw_data
->chg_first
6295 draw_feedback
= vim_realloc(draw_feedback
,
6296 draw_data
->chg_first
+ text
->length
);
6297 if (draw_feedback
!= NULL
)
6299 draw_feedback
[nfeedback
+ draw_data
->chg_first
]
6300 = draw_data
->text
->feedback
[nfeedback
];
6305 ptr
+= (*mb_ptr2len
)(ptr
);
6314 preedit_end_col
= MAXCOL
;
6317 if (text
!= NULL
|| draw_data
->chg_length
> 0)
6319 event_queue
= key_press_event_queue
;
6320 processing_queued_event
= TRUE
;
6321 while (event_queue
!= NULL
&& processing_queued_event
)
6323 GdkEvent
*ev
= event_queue
->data
;
6326 gtk_signal_emit_by_name((GtkObject
*)gui
.mainwin
, "key_press_event",
6329 event_queue
= event_queue
->next
;
6331 processing_queued_event
= FALSE
;
6332 if (key_press_event_queue
)
6334 g_slist_free(key_press_event_queue
);
6335 key_press_event_queue
= NULL
;
6338 if (gtk_main_level() > 0)
6343 * Retrieve the highlighting attributes at column col in the preedit string.
6344 * Return -1 if not in preediting mode or if col is out of range.
6347 im_get_feedback_attr(int col
)
6349 if (draw_feedback
!= NULL
&& col
< preedit_buf_len
)
6351 if (draw_feedback
[col
] & XIMReverse
)
6353 else if (draw_feedback
[col
] & XIMUnderline
)
6354 return HL_UNDERLINE
;
6356 return hl_attr(HLF_V
);
6363 preedit_caret_cbproc(XIC thexic UNUSED
,
6364 XPointer client_data UNUSED
,
6365 XPointer call_data UNUSED
)
6368 xim_log("preedit_caret_cbproc()\n");
6373 preedit_done_cbproc(XIC thexic UNUSED
,
6374 XPointer client_data UNUSED
,
6375 XPointer call_data UNUSED
)
6378 xim_log("preedit_done_cbproc()\n");
6381 vim_free(draw_feedback
);
6382 draw_feedback
= NULL
;
6383 xim_can_preediting
= FALSE
;
6384 xim_has_preediting
= FALSE
;
6385 gui_update_cursor(TRUE
, FALSE
);
6399 xim_log("xim_reset()\n");
6404 text
= XmbResetIC(((GdkICPrivate
*)xic
)->xic
);
6405 if (text
!= NULL
&& !(xim_input_style
& (int)GDK_IM_PREEDIT_CALLBACKS
))
6406 add_to_input_buf_csi((char_u
*)text
, strlen(text
));
6408 preedit_buf_len
= 0;
6415 xim_queue_key_press_event(GdkEventKey
*event
, int down UNUSED
)
6418 xim_log("xim_queue_key_press_event()\n");
6421 if (preedit_buf_len
<= 0)
6423 if (processing_queued_event
)
6424 processing_queued_event
= FALSE
;
6426 key_press_event_queue
= g_slist_append(key_press_event_queue
,
6427 gdk_event_copy((GdkEvent
*)event
));
6432 preedit_callback_setup(GdkIC
*ic UNUSED
)
6435 XVaNestedList preedit_attr
;
6436 XIMCallback preedit_start_cb
;
6437 XIMCallback preedit_draw_cb
;
6438 XIMCallback preedit_caret_cb
;
6439 XIMCallback preedit_done_cb
;
6441 xxic
= ((GdkICPrivate
*)xic
)->xic
;
6442 preedit_start_cb
.callback
= (XIMProc
)preedit_start_cbproc
;
6443 preedit_draw_cb
.callback
= (XIMProc
)preedit_draw_cbproc
;
6444 preedit_caret_cb
.callback
= (XIMProc
)preedit_caret_cbproc
;
6445 preedit_done_cb
.callback
= (XIMProc
)preedit_done_cbproc
;
6447 = XVaCreateNestedList(0,
6448 XNPreeditStartCallback
, &preedit_start_cb
,
6449 XNPreeditDrawCallback
, &preedit_draw_cb
,
6450 XNPreeditCaretCallback
, &preedit_caret_cb
,
6451 XNPreeditDoneCallback
, &preedit_done_cb
,
6453 XSetICValues(xxic
, XNPreeditAttributes
, preedit_attr
, NULL
);
6454 XFree(preedit_attr
);
6458 reset_state_setup(GdkIC
*ic UNUSED
)
6460 #ifdef USE_X11R6_XIM
6461 /* don't change the input context when we call reset */
6462 XSetICValues(((GdkICPrivate
*)ic
)->xic
, XNResetState
, XIMPreserveState
,
6471 xim_log("xim_init()\n");
6477 if (!gdk_im_ready())
6482 EMSG(_("E292: Input Method Server is not running"));
6487 if ((xic_attr
= gdk_ic_attr_new()) != NULL
)
6489 #ifdef FEAT_XFONTSET
6493 GdkColormap
*colormap
;
6494 GdkICAttr
*attr
= xic_attr
;
6495 int attrmask
= (int)GDK_IC_ALL_REQ
;
6496 GtkWidget
*widget
= gui
.drawarea
;
6498 attr
->style
= (GdkIMStyle
)xim_input_style
;
6499 attr
->client_window
= gui
.mainwin
->window
;
6501 if ((colormap
= gtk_widget_get_colormap(widget
)) !=
6502 gtk_widget_get_default_colormap())
6504 attrmask
|= (int)GDK_IC_PREEDIT_COLORMAP
;
6505 attr
->preedit_colormap
= colormap
;
6507 attrmask
|= (int)GDK_IC_PREEDIT_FOREGROUND
;
6508 attrmask
|= (int)GDK_IC_PREEDIT_BACKGROUND
;
6509 attr
->preedit_foreground
= widget
->style
->fg
[GTK_STATE_NORMAL
];
6510 attr
->preedit_background
= widget
->style
->base
[GTK_STATE_NORMAL
];
6512 #ifdef FEAT_XFONTSET
6513 if ((xim_input_style
& (int)GDK_IM_PREEDIT_MASK
)
6514 == (int)GDK_IM_PREEDIT_POSITION
)
6516 if (gui
.fontset
== NOFONTSET
6517 || gui
.fontset
->type
!= GDK_FONT_FONTSET
)
6519 EMSG(_(e_overthespot
));
6523 gdk_window_get_size(widget
->window
, &width
, &height
);
6525 attrmask
|= (int)GDK_IC_PREEDIT_POSITION_REQ
;
6526 attr
->spot_location
.x
= TEXT_X(0);
6527 attr
->spot_location
.y
= TEXT_Y(0);
6528 attr
->preedit_area
.x
= gui
.border_offset
;
6529 attr
->preedit_area
.y
= gui
.border_offset
;
6530 attr
->preedit_area
.width
= width
- 2*gui
.border_offset
;
6531 attr
->preedit_area
.height
= height
- 2*gui
.border_offset
;
6532 attr
->preedit_fontset
= gui
.fontset
;
6536 if ((xim_input_style
& (int)GDK_IM_STATUS_MASK
)
6537 == (int)GDK_IM_STATUS_AREA
)
6539 if (gui
.fontset
== NOFONTSET
6540 || gui
.fontset
->type
!= GDK_FONT_FONTSET
)
6542 EMSG(_(e_overthespot
));
6546 gdk_window_get_size(gui
.mainwin
->window
, &width
, &height
);
6547 attrmask
|= (int)GDK_IC_STATUS_AREA_REQ
;
6548 attr
->status_area
.x
= 0;
6549 attr
->status_area
.y
= height
- gui
.char_height
- 1;
6550 attr
->status_area
.width
= width
;
6551 attr
->status_area
.height
= gui
.char_height
;
6552 attr
->status_fontset
= gui
.fontset
;
6555 else if ((xim_input_style
& (int)GDK_IM_STATUS_MASK
)
6556 == (int)GDK_IM_STATUS_CALLBACKS
)
6562 xic
= gdk_ic_new(attr
, (GdkICAttributesType
)attrmask
);
6568 mask
= (int)gdk_window_get_events(widget
->window
);
6569 mask
|= (int)gdk_ic_get_events(xic
);
6570 gdk_window_set_events(widget
->window
, (GdkEventMask
)mask
);
6571 if (xim_input_style
& (int)GDK_IM_PREEDIT_CALLBACKS
)
6572 preedit_callback_setup(xic
);
6573 reset_state_setup(xic
);
6582 xim_log("im_shutdown()\n");
6588 gdk_ic_destroy(xic
);
6591 xim_is_active
= FALSE
;
6592 xim_can_preediting
= FALSE
;
6593 preedit_start_col
= MAXCOL
;
6594 xim_has_preediting
= FALSE
;
6597 #endif /* FEAT_GUI_GTK */
6600 xim_get_status_area_height()
6603 if (xim_input_style
& (int)GDK_IM_STATUS_AREA
)
6604 return gui
.char_height
;
6606 if (status_area_enabled
)
6607 return gui
.char_height
;
6613 * Get IM status. When IM is on, return TRUE. Else return FALSE.
6614 * FIXME: This doesn't work correctly: Having focus doesn't always mean XIM is
6615 * active, when not having focus XIM may still be active (e.g., when using a
6616 * tear-off menu item).
6623 return uimfep_get_status();
6625 # ifdef FEAT_GUI_GTK
6626 if (xim_input_style
& (int)GDK_IM_PREEDIT_CALLBACKS
)
6627 return xim_can_preediting
;
6629 return xim_has_focus
;
6632 # endif /* !HAVE_GTK2 */
6634 # if (defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)) || defined(PROTO)
6636 preedit_get_status(void)
6638 return preedit_is_active
;
6642 # if (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM)) || defined(PROTO)
6646 return xim_has_preediting
;
6649 #endif /* FEAT_XIM */
6651 #if defined(FEAT_MBYTE) || defined(PROTO)
6654 * Setup "vcp" for conversion from "from" to "to".
6655 * The names must have been made canonical with enc_canonize().
6656 * vcp->vc_type must have been initialized to CONV_NONE.
6657 * Note: cannot be used for conversion from/to ucs-2 and ucs-4 (will use utf-8
6659 * Afterwards invoke with "from" and "to" equal to NULL to cleanup.
6660 * Return FAIL when conversion is not supported, OK otherwise.
6663 convert_setup(vcp
, from
, to
)
6668 return convert_setup_ext(vcp
, from
, TRUE
, to
, TRUE
);
6672 * As convert_setup(), but only when from_unicode_is_utf8 is TRUE will all
6673 * "from" unicode charsets be considered utf-8. Same for "to".
6676 convert_setup_ext(vcp
, from
, from_unicode_is_utf8
, to
, to_unicode_is_utf8
)
6679 int from_unicode_is_utf8
;
6681 int to_unicode_is_utf8
;
6688 /* Reset to no conversion. */
6690 if (vcp
->vc_type
== CONV_ICONV
&& vcp
->vc_fd
!= (iconv_t
)-1)
6691 iconv_close(vcp
->vc_fd
);
6693 vcp
->vc_type
= CONV_NONE
;
6695 vcp
->vc_fail
= FALSE
;
6697 /* No conversion when one of the names is empty or they are equal. */
6698 if (from
== NULL
|| *from
== NUL
|| to
== NULL
|| *to
== NUL
6699 || STRCMP(from
, to
) == 0)
6702 from_prop
= enc_canon_props(from
);
6703 to_prop
= enc_canon_props(to
);
6704 if (from_unicode_is_utf8
)
6705 from_is_utf8
= from_prop
& ENC_UNICODE
;
6707 from_is_utf8
= from_prop
== ENC_UNICODE
;
6708 if (to_unicode_is_utf8
)
6709 to_is_utf8
= to_prop
& ENC_UNICODE
;
6711 to_is_utf8
= to_prop
== ENC_UNICODE
;
6713 if ((from_prop
& ENC_LATIN1
) && to_is_utf8
)
6715 /* Internal latin1 -> utf-8 conversion. */
6716 vcp
->vc_type
= CONV_TO_UTF8
;
6717 vcp
->vc_factor
= 2; /* up to twice as long */
6719 else if ((from_prop
& ENC_LATIN9
) && to_is_utf8
)
6721 /* Internal latin9 -> utf-8 conversion. */
6722 vcp
->vc_type
= CONV_9_TO_UTF8
;
6723 vcp
->vc_factor
= 3; /* up to three as long (euro sign) */
6725 else if (from_is_utf8
&& (to_prop
& ENC_LATIN1
))
6727 /* Internal utf-8 -> latin1 conversion. */
6728 vcp
->vc_type
= CONV_TO_LATIN1
;
6730 else if (from_is_utf8
&& (to_prop
& ENC_LATIN9
))
6732 /* Internal utf-8 -> latin9 conversion. */
6733 vcp
->vc_type
= CONV_TO_LATIN9
;
6736 /* Win32-specific codepage <-> codepage conversion without iconv. */
6737 else if ((from_is_utf8
|| encname2codepage(from
) > 0)
6738 && (to_is_utf8
|| encname2codepage(to
) > 0))
6740 vcp
->vc_type
= CONV_CODEPAGE
;
6741 vcp
->vc_factor
= 2; /* up to twice as long */
6742 vcp
->vc_cpfrom
= from_is_utf8
? 0 : encname2codepage(from
);
6743 vcp
->vc_cpto
= to_is_utf8
? 0 : encname2codepage(to
);
6747 else if ((from_prop
& ENC_MACROMAN
) && (to_prop
& ENC_LATIN1
))
6749 vcp
->vc_type
= CONV_MAC_LATIN1
;
6751 else if ((from_prop
& ENC_MACROMAN
) && to_is_utf8
)
6753 vcp
->vc_type
= CONV_MAC_UTF8
;
6754 vcp
->vc_factor
= 2; /* up to twice as long */
6756 else if ((from_prop
& ENC_LATIN1
) && (to_prop
& ENC_MACROMAN
))
6758 vcp
->vc_type
= CONV_LATIN1_MAC
;
6760 else if (from_is_utf8
&& (to_prop
& ENC_MACROMAN
))
6762 vcp
->vc_type
= CONV_UTF8_MAC
;
6768 /* Use iconv() for conversion. */
6769 vcp
->vc_fd
= (iconv_t
)my_iconv_open(
6770 to_is_utf8
? (char_u
*)"utf-8" : to
,
6771 from_is_utf8
? (char_u
*)"utf-8" : from
);
6772 if (vcp
->vc_fd
!= (iconv_t
)-1)
6774 vcp
->vc_type
= CONV_ICONV
;
6775 vcp
->vc_factor
= 4; /* could be longer too... */
6779 if (vcp
->vc_type
== CONV_NONE
)
6785 #if defined(FEAT_GUI) || defined(AMIGA) || defined(WIN3264) \
6786 || defined(MSDOS) || defined(PROTO)
6788 * Do conversion on typed input characters in-place.
6789 * The input and output are not NUL terminated!
6790 * Returns the length after conversion.
6793 convert_input(ptr
, len
, maxlen
)
6798 return convert_input_safe(ptr
, len
, maxlen
, NULL
, NULL
);
6803 * Like convert_input(), but when there is an incomplete byte sequence at the
6804 * end return that as an allocated string in "restp" and set "*restlenp" to
6805 * the length. If "restp" is NULL it is not used.
6808 convert_input_safe(ptr
, len
, maxlen
, restp
, restlenp
)
6817 int unconvertlen
= 0;
6819 d
= string_convert_ext(&input_conv
, ptr
, &dlen
,
6820 restp
== NULL
? NULL
: &unconvertlen
);
6825 if (unconvertlen
> 0)
6827 /* Move the unconverted characters to allocated memory. */
6828 *restp
= alloc(unconvertlen
);
6830 mch_memmove(*restp
, ptr
+ len
- unconvertlen
, unconvertlen
);
6831 *restlenp
= unconvertlen
;
6833 mch_memmove(ptr
, d
, dlen
);
6836 /* result is too long, keep the unconverted text (the caller must
6837 * have done something wrong!) */
6845 * Convert text "ptr[*lenp]" according to "vcp".
6846 * Returns the result in allocated memory and sets "*lenp".
6847 * When "lenp" is NULL, use NUL terminated strings.
6848 * Illegal chars are often changed to "?", unless vcp->vc_fail is set.
6849 * When something goes wrong, NULL is returned and "*lenp" is unchanged.
6852 string_convert(vcp
, ptr
, lenp
)
6857 return string_convert_ext(vcp
, ptr
, lenp
, NULL
);
6861 * Like string_convert(), but when "unconvlenp" is not NULL and there are is
6862 * an incomplete sequence at the end it is not converted and "*unconvlenp" is
6863 * set to the number of remaining bytes.
6866 string_convert_ext(vcp
, ptr
, lenp
, unconvlenp
)
6872 char_u
*retval
= NULL
;
6880 len
= (int)STRLEN(ptr
);
6884 return vim_strsave((char_u
*)"");
6886 switch (vcp
->vc_type
)
6888 case CONV_TO_UTF8
: /* latin1 to utf-8 conversion */
6889 retval
= alloc(len
* 2 + 1);
6893 for (i
= 0; i
< len
; ++i
)
6900 *d
++ = 0xc0 + ((unsigned)c
>> 6);
6901 *d
++ = 0x80 + (c
& 0x3f);
6906 *lenp
= (int)(d
- retval
);
6909 case CONV_9_TO_UTF8
: /* latin9 to utf-8 conversion */
6910 retval
= alloc(len
* 3 + 1);
6914 for (i
= 0; i
< len
; ++i
)
6919 case 0xa4: c
= 0x20ac; break; /* euro */
6920 case 0xa6: c
= 0x0160; break; /* S hat */
6921 case 0xa8: c
= 0x0161; break; /* S -hat */
6922 case 0xb4: c
= 0x017d; break; /* Z hat */
6923 case 0xb8: c
= 0x017e; break; /* Z -hat */
6924 case 0xbc: c
= 0x0152; break; /* OE */
6925 case 0xbd: c
= 0x0153; break; /* oe */
6926 case 0xbe: c
= 0x0178; break; /* Y */
6928 d
+= utf_char2bytes(c
, d
);
6932 *lenp
= (int)(d
- retval
);
6935 case CONV_TO_LATIN1
: /* utf-8 to latin1 conversion */
6936 case CONV_TO_LATIN9
: /* utf-8 to latin9 conversion */
6937 retval
= alloc(len
+ 1);
6941 for (i
= 0; i
< len
; ++i
)
6943 l
= utf_ptr2len_len(ptr
+ i
, len
- i
);
6948 int l_w
= utf8len_tab_zero
[ptr
[i
]];
6952 /* Illegal utf-8 byte cannot be converted */
6956 if (unconvlenp
!= NULL
&& l_w
> len
- i
)
6958 /* Incomplete sequence at the end. */
6959 *unconvlenp
= len
- i
;
6966 c
= utf_ptr2char(ptr
+ i
);
6967 if (vcp
->vc_type
== CONV_TO_LATIN9
)
6970 case 0x20ac: c
= 0xa4; break; /* euro */
6971 case 0x0160: c
= 0xa6; break; /* S hat */
6972 case 0x0161: c
= 0xa8; break; /* S -hat */
6973 case 0x017d: c
= 0xb4; break; /* Z hat */
6974 case 0x017e: c
= 0xb8; break; /* Z -hat */
6975 case 0x0152: c
= 0xbc; break; /* OE */
6976 case 0x0153: c
= 0xbd; break; /* oe */
6977 case 0x0178: c
= 0xbe; break; /* Y */
6985 case 0xbe: c
= 0x100; break; /* not in latin9 */
6987 if (!utf_iscomposing(c
)) /* skip composing chars */
6991 else if (vcp
->vc_fail
)
6999 if (utf_char2cells(c
) > 1)
7008 *lenp
= (int)(d
- retval
);
7011 # ifdef MACOS_CONVERT
7012 case CONV_MAC_LATIN1
:
7013 retval
= mac_string_convert(ptr
, len
, lenp
, vcp
->vc_fail
,
7014 'm', 'l', unconvlenp
);
7017 case CONV_LATIN1_MAC
:
7018 retval
= mac_string_convert(ptr
, len
, lenp
, vcp
->vc_fail
,
7019 'l', 'm', unconvlenp
);
7023 retval
= mac_string_convert(ptr
, len
, lenp
, vcp
->vc_fail
,
7024 'm', 'u', unconvlenp
);
7028 retval
= mac_string_convert(ptr
, len
, lenp
, vcp
->vc_fail
,
7029 'u', 'm', unconvlenp
);
7034 case CONV_ICONV
: /* conversion with output_conv.vc_fd */
7035 retval
= iconv_string(vcp
, ptr
, len
, unconvlenp
, lenp
);
7039 case CONV_CODEPAGE
: /* codepage -> codepage */
7045 /* 1. codepage/UTF-8 -> ucs-2. */
7046 if (vcp
->vc_cpfrom
== 0)
7047 tmp_len
= utf8_to_utf16(ptr
, len
, NULL
, NULL
);
7049 tmp_len
= MultiByteToWideChar(vcp
->vc_cpfrom
, 0,
7051 tmp
= (short_u
*)alloc(sizeof(short_u
) * tmp_len
);
7054 if (vcp
->vc_cpfrom
== 0)
7055 utf8_to_utf16(ptr
, len
, tmp
, unconvlenp
);
7057 MultiByteToWideChar(vcp
->vc_cpfrom
, 0, ptr
, len
, tmp
, tmp_len
);
7059 /* 2. ucs-2 -> codepage/UTF-8. */
7060 if (vcp
->vc_cpto
== 0)
7061 retlen
= utf16_to_utf8(tmp
, tmp_len
, NULL
);
7063 retlen
= WideCharToMultiByte(vcp
->vc_cpto
, 0,
7064 tmp
, tmp_len
, 0, 0, 0, 0);
7065 retval
= alloc(retlen
+ 1);
7068 if (vcp
->vc_cpto
== 0)
7069 utf16_to_utf8(tmp
, tmp_len
, retval
);
7071 WideCharToMultiByte(vcp
->vc_cpto
, 0,
7072 tmp
, tmp_len
, retval
, retlen
, 0, 0);
7073 retval
[retlen
] = NUL
;
7087 #if defined(FEAT_UIMFEP)
7089 uimfep_set_active(int active
)
7093 setmode
= vim_getenv("UIM_FEP_SETMODE", &mustfree
);
7094 if (setmode
!= NULL
)
7096 FILE *fp
= fopen(setmode
, "w");
7099 fprintf(fp
, "%d\n", active
? uimfep_lastmode
: 0);
7109 uimfep_get_status(void)
7114 getmode
= vim_getenv("UIM_FEP_GETMODE", &mustfree
);
7115 if (getmode
!= NULL
)
7117 FILE *fp
= fopen(getmode
, "r");
7121 if (fgets(buf
, sizeof(buf
), fp
))
7129 uimfep_lastmode
= mode
;
7133 # if defined(USE_IM_CONTROL) && (!defined(FEAT_XIM) \
7134 && !defined(FEAT_MBYTE_IME) && !defined(GLOBAL_IME))
7136 im_set_active(int active
)
7138 # if defined(FEAT_GUI_MAC) || defined(FEAT_GUI_MACVIM)
7140 gui_im_set_active(active
);
7142 uimfep_set_active(active
);
7143 # else // FEAT_GUI_MAC || FEAT_GUI_MACVIM
7144 uimfep_set_active(active
);
7145 # endif // FEAT_GUI_MAC || FEAT_GUI_MACVIM
7151 # if defined(FEAT_GUI_MAC) || defined(FEAT_GUI_MACVIM)
7153 return gui_im_get_status();
7155 return uimfep_get_status();
7156 # else // FEAT_GUI_MAC || FEAT_GUI_MACVIM
7157 return uimfep_get_status();
7158 # endif // FEAT_GUI_MAC || FEAT_GUI_MACVIM
7162 #endif /* defined(FEAT_UIMFEP) */