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