-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbyte.c
6796 lines (6256 loc) · 161 KB
/
mbyte.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
* Multibyte extensions partly by Sung-Hoon Baek
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* mbyte.c: Code specifically for handling multi-byte characters.
*
* The encoding used in the core is set with 'encoding'. When 'encoding' is
* changed, the following four variables are set (for speed).
* Currently these types of character encodings are supported:
*
* "enc_dbcs" When non-zero it tells the type of double byte character
* encoding (Chinese, Korean, Japanese, etc.).
* The cell width on the display is equal to the number of
* bytes. (exception: DBCS_JPNU with first byte 0x8e)
* Recognizing the first or second byte is difficult, it
* requires checking a byte sequence from the start.
* "enc_utf8" When TRUE use Unicode characters in UTF-8 encoding.
* The cell width on the display needs to be determined from
* the character value.
* Recognizing bytes is easy: 0xxx.xxxx is a single-byte
* char, 10xx.xxxx is a trailing byte, 11xx.xxxx is a leading
* byte of a multi-byte character.
* To make things complicated, up to six composing characters
* are allowed. These are drawn on top of the first char.
* For most editing the sequence of bytes with composing
* characters included is considered to be one character.
* "enc_unicode" When 2 use 16-bit Unicode characters (or UTF-16).
* When 4 use 32-but Unicode characters.
* Internally characters are stored in UTF-8 encoding to
* avoid NUL bytes. Conversion happens when doing I/O.
* "enc_utf8" will also be TRUE.
*
* "has_mbyte" is set when "enc_dbcs" or "enc_utf8" is non-zero.
*
* If none of these is TRUE, 8-bit bytes are used for a character. The
* encoding isn't currently specified (TODO).
*
* 'encoding' specifies the encoding used in the core. This is in registers,
* text manipulation, buffers, etc. Conversion has to be done when characters
* in another encoding are received or send:
*
* clipboard
* ^
* | (2)
* V
* +---------------+
* (1) | | (3)
* keyboard ----->| core |-----> display
* | |
* +---------------+
* ^
* | (4)
* V
* file
*
* (1) Typed characters arrive in the current locale. Conversion is to be
* done when 'encoding' is different from 'termencoding'.
* (2) Text will be made available with the encoding specified with
* 'encoding'. If this is not sufficient, system-specific conversion
* might be required.
* (3) For the GUI the correct font must be selected, no conversion done.
* Otherwise, conversion is to be done when 'encoding' differs from
* 'termencoding'. (Different in the GTK+ 2 port -- 'termencoding'
* is always used for both input and output and must always be set to
* "utf-8". gui_mch_init() does this automatically.)
* (4) The encoding of the file is specified with 'fileencoding'. Conversion
* is to be done when it's different from 'encoding'.
*
* The viminfo file is a special case: Only text is converted, not file names.
* Vim scripts may contain an ":encoding" command. This has an effect for
* some commands, like ":menutrans"
*/
#include "vim.h"
#ifdef WIN32UNIX
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD)
# include <X11/Xwindows.h>
# define WINBYTE wBYTE
# else
# include <windows.h>
# define WINBYTE BYTE
# endif
# ifdef WIN32
# undef WIN32 /* Some windows.h define WIN32, we don't want that here. */
# endif
#else
# define WINBYTE BYTE
#endif
#if (defined(WIN3264) || defined(WIN32UNIX)) && !defined(__MINGW32__)
# include <winnls.h>
#endif
#ifdef FEAT_GUI_X11
# include <X11/Intrinsic.h>
#endif
#ifdef X_LOCALE
#include <X11/Xlocale.h>
#endif
#if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
# if GTK_CHECK_VERSION(3,0,0)
# include <gdk/gdkkeysyms-compat.h>
# else
# include <gdk/gdkkeysyms.h>
# endif
# ifdef WIN3264
# include <gdk/gdkwin32.h>
# else
# include <gdk/gdkx.h>
# endif
#endif
#ifdef HAVE_WCHAR_H
# include <wchar.h>
#endif
#if 0
/* This has been disabled, because several people reported problems with the
* wcwidth() and iswprint() library functions, esp. for Hebrew. */
# ifdef __STDC_ISO_10646__
# define USE_WCHAR_FUNCTIONS
# endif
#endif
#if defined(FEAT_MBYTE) || defined(PROTO)
static int enc_canon_search(char_u *name);
static int dbcs_char2len(int c);
static int dbcs_char2bytes(int c, char_u *buf);
static int dbcs_ptr2len(char_u *p);
static int dbcs_ptr2len_len(char_u *p, int size);
static int utf_ptr2cells_len(char_u *p, int size);
static int dbcs_char2cells(int c);
static int dbcs_ptr2cells_len(char_u *p, int size);
static int dbcs_ptr2char(char_u *p);
static int utf_safe_read_char_adv(char_u **s, size_t *n);
/*
* Lookup table to quickly get the length in bytes of a UTF-8 character from
* the first byte of a UTF-8 string.
* Bytes which are illegal when used as the first byte have a 1.
* The NUL byte has length 1.
*/
static char utf8len_tab[256] =
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
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,
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,
};
/*
* Like utf8len_tab above, but using a zero for illegal lead bytes.
*/
static char utf8len_tab_zero[256] =
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
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,
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,
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,
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,
};
/*
* XIM often causes trouble. Define XIM_DEBUG to get a log of XIM callbacks
* in the "xim.log" file.
*/
/* #define XIM_DEBUG */
#ifdef XIM_DEBUG
static void
xim_log(char *s, ...)
{
va_list arglist;
static FILE *fd = NULL;
if (fd == (FILE *)-1)
return;
if (fd == NULL)
{
fd = mch_fopen("xim.log", "w");
if (fd == NULL)
{
EMSG("Cannot open xim.log");
fd = (FILE *)-1;
return;
}
}
va_start(arglist, s);
vfprintf(fd, s, arglist);
va_end(arglist);
}
#endif
#endif
#if defined(FEAT_MBYTE) || defined(FEAT_POSTSCRIPT) || defined(PROTO)
/*
* Canonical encoding names and their properties.
* "iso-8859-n" is handled by enc_canonize() directly.
*/
static struct
{ char *name; int prop; int codepage;}
enc_canon_table[] =
{
#define IDX_LATIN_1 0
{"latin1", ENC_8BIT + ENC_LATIN1, 1252},
#define IDX_ISO_2 1
{"iso-8859-2", ENC_8BIT, 0},
#define IDX_ISO_3 2
{"iso-8859-3", ENC_8BIT, 0},
#define IDX_ISO_4 3
{"iso-8859-4", ENC_8BIT, 0},
#define IDX_ISO_5 4
{"iso-8859-5", ENC_8BIT, 0},
#define IDX_ISO_6 5
{"iso-8859-6", ENC_8BIT, 0},
#define IDX_ISO_7 6
{"iso-8859-7", ENC_8BIT, 0},
#define IDX_ISO_8 7
{"iso-8859-8", ENC_8BIT, 0},
#define IDX_ISO_9 8
{"iso-8859-9", ENC_8BIT, 0},
#define IDX_ISO_10 9
{"iso-8859-10", ENC_8BIT, 0},
#define IDX_ISO_11 10
{"iso-8859-11", ENC_8BIT, 0},
#define IDX_ISO_13 11
{"iso-8859-13", ENC_8BIT, 0},
#define IDX_ISO_14 12
{"iso-8859-14", ENC_8BIT, 0},
#define IDX_ISO_15 13
{"iso-8859-15", ENC_8BIT + ENC_LATIN9, 0},
#define IDX_KOI8_R 14
{"koi8-r", ENC_8BIT, 0},
#define IDX_KOI8_U 15
{"koi8-u", ENC_8BIT, 0},
#define IDX_UTF8 16
{"utf-8", ENC_UNICODE, 0},
#define IDX_UCS2 17
{"ucs-2", ENC_UNICODE + ENC_ENDIAN_B + ENC_2BYTE, 0},
#define IDX_UCS2LE 18
{"ucs-2le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2BYTE, 0},
#define IDX_UTF16 19
{"utf-16", ENC_UNICODE + ENC_ENDIAN_B + ENC_2WORD, 0},
#define IDX_UTF16LE 20
{"utf-16le", ENC_UNICODE + ENC_ENDIAN_L + ENC_2WORD, 0},
#define IDX_UCS4 21
{"ucs-4", ENC_UNICODE + ENC_ENDIAN_B + ENC_4BYTE, 0},
#define IDX_UCS4LE 22
{"ucs-4le", ENC_UNICODE + ENC_ENDIAN_L + ENC_4BYTE, 0},
/* For debugging DBCS encoding on Unix. */
#define IDX_DEBUG 23
{"debug", ENC_DBCS, DBCS_DEBUG},
#define IDX_EUC_JP 24
{"euc-jp", ENC_DBCS, DBCS_JPNU},
#define IDX_SJIS 25
{"sjis", ENC_DBCS, DBCS_JPN},
#define IDX_EUC_KR 26
{"euc-kr", ENC_DBCS, DBCS_KORU},
#define IDX_EUC_CN 27
{"euc-cn", ENC_DBCS, DBCS_CHSU},
#define IDX_EUC_TW 28
{"euc-tw", ENC_DBCS, DBCS_CHTU},
#define IDX_BIG5 29
{"big5", ENC_DBCS, DBCS_CHT},
/* MS-DOS and MS-Windows codepages are included here, so that they can be
* used on Unix too. Most of them are similar to ISO-8859 encodings, but
* not exactly the same. */
#define IDX_CP437 30
{"cp437", ENC_8BIT, 437}, /* like iso-8859-1 */
#define IDX_CP737 31
{"cp737", ENC_8BIT, 737}, /* like iso-8859-7 */
#define IDX_CP775 32
{"cp775", ENC_8BIT, 775}, /* Baltic */
#define IDX_CP850 33
{"cp850", ENC_8BIT, 850}, /* like iso-8859-4 */
#define IDX_CP852 34
{"cp852", ENC_8BIT, 852}, /* like iso-8859-1 */
#define IDX_CP855 35
{"cp855", ENC_8BIT, 855}, /* like iso-8859-2 */
#define IDX_CP857 36
{"cp857", ENC_8BIT, 857}, /* like iso-8859-5 */
#define IDX_CP860 37
{"cp860", ENC_8BIT, 860}, /* like iso-8859-9 */
#define IDX_CP861 38
{"cp861", ENC_8BIT, 861}, /* like iso-8859-1 */
#define IDX_CP862 39
{"cp862", ENC_8BIT, 862}, /* like iso-8859-1 */
#define IDX_CP863 40
{"cp863", ENC_8BIT, 863}, /* like iso-8859-8 */
#define IDX_CP865 41
{"cp865", ENC_8BIT, 865}, /* like iso-8859-1 */
#define IDX_CP866 42
{"cp866", ENC_8BIT, 866}, /* like iso-8859-5 */
#define IDX_CP869 43
{"cp869", ENC_8BIT, 869}, /* like iso-8859-7 */
#define IDX_CP874 44
{"cp874", ENC_8BIT, 874}, /* Thai */
#define IDX_CP932 45
{"cp932", ENC_DBCS, DBCS_JPN},
#define IDX_CP936 46
{"cp936", ENC_DBCS, DBCS_CHS},
#define IDX_CP949 47
{"cp949", ENC_DBCS, DBCS_KOR},
#define IDX_CP950 48
{"cp950", ENC_DBCS, DBCS_CHT},
#define IDX_CP1250 49
{"cp1250", ENC_8BIT, 1250}, /* Czech, Polish, etc. */
#define IDX_CP1251 50
{"cp1251", ENC_8BIT, 1251}, /* Cyrillic */
/* cp1252 is considered to be equal to latin1 */
#define IDX_CP1253 51
{"cp1253", ENC_8BIT, 1253}, /* Greek */
#define IDX_CP1254 52
{"cp1254", ENC_8BIT, 1254}, /* Turkish */
#define IDX_CP1255 53
{"cp1255", ENC_8BIT, 1255}, /* Hebrew */
#define IDX_CP1256 54
{"cp1256", ENC_8BIT, 1256}, /* Arabic */
#define IDX_CP1257 55
{"cp1257", ENC_8BIT, 1257}, /* Baltic */
#define IDX_CP1258 56
{"cp1258", ENC_8BIT, 1258}, /* Vietnamese */
#define IDX_MACROMAN 57
{"macroman", ENC_8BIT + ENC_MACROMAN, 0}, /* Mac OS */
#define IDX_DECMCS 58
{"dec-mcs", ENC_8BIT, 0}, /* DEC MCS */
#define IDX_HPROMAN8 59
{"hp-roman8", ENC_8BIT, 0}, /* HP Roman8 */
#define IDX_COUNT 60
};
/*
* Aliases for encoding names.
*/
static struct
{ char *name; int canon;}
enc_alias_table[] =
{
{"ansi", IDX_LATIN_1},
{"iso-8859-1", IDX_LATIN_1},
{"latin2", IDX_ISO_2},
{"latin3", IDX_ISO_3},
{"latin4", IDX_ISO_4},
{"cyrillic", IDX_ISO_5},
{"arabic", IDX_ISO_6},
{"greek", IDX_ISO_7},
#ifdef WIN3264
{"hebrew", IDX_CP1255},
#else
{"hebrew", IDX_ISO_8},
#endif
{"latin5", IDX_ISO_9},
{"turkish", IDX_ISO_9}, /* ? */
{"latin6", IDX_ISO_10},
{"nordic", IDX_ISO_10}, /* ? */
{"thai", IDX_ISO_11}, /* ? */
{"latin7", IDX_ISO_13},
{"latin8", IDX_ISO_14},
{"latin9", IDX_ISO_15},
{"utf8", IDX_UTF8},
{"unicode", IDX_UCS2},
{"ucs2", IDX_UCS2},
{"ucs2be", IDX_UCS2},
{"ucs-2be", IDX_UCS2},
{"ucs2le", IDX_UCS2LE},
{"utf16", IDX_UTF16},
{"utf16be", IDX_UTF16},
{"utf-16be", IDX_UTF16},
{"utf16le", IDX_UTF16LE},
{"ucs4", IDX_UCS4},
{"ucs4be", IDX_UCS4},
{"ucs-4be", IDX_UCS4},
{"ucs4le", IDX_UCS4LE},
{"utf32", IDX_UCS4},
{"utf-32", IDX_UCS4},
{"utf32be", IDX_UCS4},
{"utf-32be", IDX_UCS4},
{"utf32le", IDX_UCS4LE},
{"utf-32le", IDX_UCS4LE},
{"932", IDX_CP932},
{"949", IDX_CP949},
{"936", IDX_CP936},
{"gbk", IDX_CP936},
{"950", IDX_CP950},
{"eucjp", IDX_EUC_JP},
{"unix-jis", IDX_EUC_JP},
{"ujis", IDX_EUC_JP},
{"shift-jis", IDX_SJIS},
{"pck", IDX_SJIS}, /* Sun: PCK */
{"euckr", IDX_EUC_KR},
{"5601", IDX_EUC_KR}, /* Sun: KS C 5601 */
{"euccn", IDX_EUC_CN},
{"gb2312", IDX_EUC_CN},
{"euctw", IDX_EUC_TW},
#if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
{"japan", IDX_CP932},
{"korea", IDX_CP949},
{"prc", IDX_CP936},
{"chinese", IDX_CP936},
{"taiwan", IDX_CP950},
{"big5", IDX_CP950},
#else
{"japan", IDX_EUC_JP},
{"korea", IDX_EUC_KR},
{"prc", IDX_EUC_CN},
{"chinese", IDX_EUC_CN},
{"taiwan", IDX_EUC_TW},
{"cp950", IDX_BIG5},
{"950", IDX_BIG5},
#endif
{"mac", IDX_MACROMAN},
{"mac-roman", IDX_MACROMAN},
{NULL, 0}
};
#ifndef CP_UTF8
# define CP_UTF8 65001 /* magic number from winnls.h */
#endif
/*
* Find encoding "name" in the list of canonical encoding names.
* Returns -1 if not found.
*/
static int
enc_canon_search(char_u *name)
{
int i;
for (i = 0; i < IDX_COUNT; ++i)
if (STRCMP(name, enc_canon_table[i].name) == 0)
return i;
return -1;
}
#endif
#if defined(FEAT_MBYTE) || defined(PROTO)
/*
* Find canonical encoding "name" in the list and return its properties.
* Returns 0 if not found.
*/
int
enc_canon_props(char_u *name)
{
int i;
i = enc_canon_search(name);
if (i >= 0)
return enc_canon_table[i].prop;
#ifdef WIN3264
if (name[0] == 'c' && name[1] == 'p' && VIM_ISDIGIT(name[2]))
{
CPINFO cpinfo;
/* Get info on this codepage to find out what it is. */
if (GetCPInfo(atoi((char *)name + 2), &cpinfo) != 0)
{
if (cpinfo.MaxCharSize == 1) /* some single-byte encoding */
return ENC_8BIT;
if (cpinfo.MaxCharSize == 2
&& (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
/* must be a DBCS encoding */
return ENC_DBCS;
}
return 0;
}
#endif
if (STRNCMP(name, "2byte-", 6) == 0)
return ENC_DBCS;
if (STRNCMP(name, "8bit-", 5) == 0 || STRNCMP(name, "iso-8859-", 9) == 0)
return ENC_8BIT;
return 0;
}
/*
* Set up for using multi-byte characters.
* Called in three cases:
* - by main() to initialize (p_enc == NULL)
* - by set_init_1() after 'encoding' was set to its default.
* - by do_set() when 'encoding' has been set.
* p_enc must have been passed through enc_canonize() already.
* Sets the "enc_unicode", "enc_utf8", "enc_dbcs" and "has_mbyte" flags.
* Fills mb_bytelen_tab[] and returns NULL when there are no problems.
* When there is something wrong: Returns an error message and doesn't change
* anything.
*/
char_u *
mb_init(void)
{
int i;
int idx;
int n;
int enc_dbcs_new = 0;
#if defined(USE_ICONV) && !defined(WIN3264) && !defined(WIN32UNIX) \
&& !defined(MACOS)
# define LEN_FROM_CONV
vimconv_T vimconv;
char_u *p;
#endif
if (p_enc == NULL)
{
/* Just starting up: set the whole table to one's. */
for (i = 0; i < 256; ++i)
mb_bytelen_tab[i] = 1;
input_conv.vc_type = CONV_NONE;
input_conv.vc_factor = 1;
output_conv.vc_type = CONV_NONE;
return NULL;
}
#ifdef WIN3264
if (p_enc[0] == 'c' && p_enc[1] == 'p' && VIM_ISDIGIT(p_enc[2]))
{
CPINFO cpinfo;
/* Get info on this codepage to find out what it is. */
if (GetCPInfo(atoi((char *)p_enc + 2), &cpinfo) != 0)
{
if (cpinfo.MaxCharSize == 1)
{
/* some single-byte encoding */
enc_unicode = 0;
enc_utf8 = FALSE;
}
else if (cpinfo.MaxCharSize == 2
&& (cpinfo.LeadByte[0] != 0 || cpinfo.LeadByte[1] != 0))
{
/* must be a DBCS encoding, check below */
enc_dbcs_new = atoi((char *)p_enc + 2);
}
else
goto codepage_invalid;
}
else if (GetLastError() == ERROR_INVALID_PARAMETER)
{
codepage_invalid:
return (char_u *)N_("E543: Not a valid codepage");
}
}
#endif
else if (STRNCMP(p_enc, "8bit-", 5) == 0
|| STRNCMP(p_enc, "iso-8859-", 9) == 0)
{
/* Accept any "8bit-" or "iso-8859-" name. */
enc_unicode = 0;
enc_utf8 = FALSE;
}
else if (STRNCMP(p_enc, "2byte-", 6) == 0)
{
#ifdef WIN3264
/* Windows: accept only valid codepage numbers, check below. */
if (p_enc[6] != 'c' || p_enc[7] != 'p'
|| (enc_dbcs_new = atoi((char *)p_enc + 8)) == 0)
return e_invarg;
#else
/* Unix: accept any "2byte-" name, assume current locale. */
enc_dbcs_new = DBCS_2BYTE;
#endif
}
else if ((idx = enc_canon_search(p_enc)) >= 0)
{
i = enc_canon_table[idx].prop;
if (i & ENC_UNICODE)
{
/* Unicode */
enc_utf8 = TRUE;
if (i & (ENC_2BYTE | ENC_2WORD))
enc_unicode = 2;
else if (i & ENC_4BYTE)
enc_unicode = 4;
else
enc_unicode = 0;
}
else if (i & ENC_DBCS)
{
/* 2byte, handle below */
enc_dbcs_new = enc_canon_table[idx].codepage;
}
else
{
/* Must be 8-bit. */
enc_unicode = 0;
enc_utf8 = FALSE;
}
}
else /* Don't know what encoding this is, reject it. */
return e_invarg;
if (enc_dbcs_new != 0)
{
#ifdef WIN3264
/* Check if the DBCS code page is OK. */
if (!IsValidCodePage(enc_dbcs_new))
goto codepage_invalid;
#endif
enc_unicode = 0;
enc_utf8 = FALSE;
}
enc_dbcs = enc_dbcs_new;
has_mbyte = (enc_dbcs != 0 || enc_utf8);
#if defined(WIN3264) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
enc_codepage = encname2codepage(p_enc);
enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
#endif
/* Detect an encoding that uses latin1 characters. */
enc_latin1like = (enc_utf8 || STRCMP(p_enc, "latin1") == 0
|| STRCMP(p_enc, "iso-8859-15") == 0);
/*
* Set the function pointers.
*/
if (enc_utf8)
{
mb_ptr2len = utfc_ptr2len;
mb_ptr2len_len = utfc_ptr2len_len;
mb_char2len = utf_char2len;
mb_char2bytes = utf_char2bytes;
mb_ptr2cells = utf_ptr2cells;
mb_ptr2cells_len = utf_ptr2cells_len;
mb_char2cells = utf_char2cells;
mb_off2cells = utf_off2cells;
mb_ptr2char = utf_ptr2char;
mb_head_off = utf_head_off;
}
else if (enc_dbcs != 0)
{
mb_ptr2len = dbcs_ptr2len;
mb_ptr2len_len = dbcs_ptr2len_len;
mb_char2len = dbcs_char2len;
mb_char2bytes = dbcs_char2bytes;
mb_ptr2cells = dbcs_ptr2cells;
mb_ptr2cells_len = dbcs_ptr2cells_len;
mb_char2cells = dbcs_char2cells;
mb_off2cells = dbcs_off2cells;
mb_ptr2char = dbcs_ptr2char;
mb_head_off = dbcs_head_off;
}
else
{
mb_ptr2len = latin_ptr2len;
mb_ptr2len_len = latin_ptr2len_len;
mb_char2len = latin_char2len;
mb_char2bytes = latin_char2bytes;
mb_ptr2cells = latin_ptr2cells;
mb_ptr2cells_len = latin_ptr2cells_len;
mb_char2cells = latin_char2cells;
mb_off2cells = latin_off2cells;
mb_ptr2char = latin_ptr2char;
mb_head_off = latin_head_off;
}
/*
* Fill the mb_bytelen_tab[] for MB_BYTE2LEN().
*/
#ifdef LEN_FROM_CONV
/* When 'encoding' is different from the current locale mblen() won't
* work. Use conversion to "utf-8" instead. */
vimconv.vc_type = CONV_NONE;
if (enc_dbcs)
{
p = enc_locale();
if (p == NULL || STRCMP(p, p_enc) != 0)
{
convert_setup(&vimconv, p_enc, (char_u *)"utf-8");
vimconv.vc_fail = TRUE;
}
vim_free(p);
}
#endif
for (i = 0; i < 256; ++i)
{
/* Our own function to reliably check the length of UTF-8 characters,
* independent of mblen(). */
if (enc_utf8)
n = utf8len_tab[i];
else if (enc_dbcs == 0)
n = 1;
else
{
#if defined(WIN3264) || defined(WIN32UNIX)
/* enc_dbcs is set by setting 'fileencoding'. It becomes a Windows
* CodePage identifier, which we can pass directly in to Windows
* API */
n = IsDBCSLeadByteEx(enc_dbcs, (WINBYTE)i) ? 2 : 1;
#else
# if defined(MACOS) || defined(__amigaos4__) || defined(__ANDROID__)
/*
* if mblen() is not available, character which MSB is turned on
* are treated as leading byte character. (note : This assumption
* is not always true.)
*/
n = (i & 0x80) ? 2 : 1;
# else
char buf[MB_MAXBYTES + 1];
# ifdef X_LOCALE
# ifndef mblen
# define mblen _Xmblen
# endif
# endif
if (i == NUL) /* just in case mblen() can't handle "" */
n = 1;
else
{
buf[0] = i;
buf[1] = 0;
#ifdef LEN_FROM_CONV
if (vimconv.vc_type != CONV_NONE)
{
/*
* string_convert() should fail when converting the first
* byte of a double-byte character.
*/
p = string_convert(&vimconv, (char_u *)buf, NULL);
if (p != NULL)
{
vim_free(p);
n = 1;
}
else
n = 2;
}
else
#endif
{
/*
* mblen() should return -1 for invalid (means the leading
* multibyte) character. However there are some platforms
* where mblen() returns 0 for invalid character.
* Therefore, following condition includes 0.
*/
ignored = mblen(NULL, 0); /* First reset the state. */
if (mblen(buf, (size_t)1) <= 0)
n = 2;
else
n = 1;
}
}
# endif
#endif
}
mb_bytelen_tab[i] = n;
}
#ifdef LEN_FROM_CONV
convert_setup(&vimconv, NULL, NULL);
#endif
/* The cell width depends on the type of multi-byte characters. */
(void)init_chartab();
/* When enc_utf8 is set or reset, (de)allocate ScreenLinesUC[] */
screenalloc(FALSE);
/* When using Unicode, set default for 'fileencodings'. */
if (enc_utf8 && !option_was_set((char_u *)"fencs"))
set_string_option_direct((char_u *)"fencs", -1,
(char_u *)"ucs-bom,utf-8,default,latin1", OPT_FREE, 0);
#if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
/* GNU gettext 0.10.37 supports this feature: set the codeset used for
* translated messages independently from the current locale. */
(void)bind_textdomain_codeset(VIMPACKAGE,
enc_utf8 ? "utf-8" : (char *)p_enc);
#endif
#ifdef WIN32
/* When changing 'encoding' while starting up, then convert the command
* line arguments from the active codepage to 'encoding'. */
if (starting != 0)
fix_arg_enc();
#endif
#ifdef FEAT_AUTOCMD
/* Fire an autocommand to let people do custom font setup. This must be
* after Vim has been setup for the new encoding. */
apply_autocmds(EVENT_ENCODINGCHANGED, NULL, (char_u *)"", FALSE, curbuf);
#endif
#ifdef FEAT_SPELL
/* Need to reload spell dictionaries */
spell_reload();
#endif
return NULL;
}
/*
* Return the size of the BOM for the current buffer:
* 0 - no BOM
* 2 - UCS-2 or UTF-16 BOM
* 4 - UCS-4 BOM
* 3 - UTF-8 BOM
*/
int
bomb_size(void)
{
int n = 0;
if (curbuf->b_p_bomb && !curbuf->b_p_bin)
{
if (*curbuf->b_p_fenc == NUL)
{
if (enc_utf8)
{
if (enc_unicode != 0)
n = enc_unicode;
else
n = 3;
}
}
else if (STRCMP(curbuf->b_p_fenc, "utf-8") == 0)
n = 3;
else if (STRNCMP(curbuf->b_p_fenc, "ucs-2", 5) == 0
|| STRNCMP(curbuf->b_p_fenc, "utf-16", 6) == 0)
n = 2;
else if (STRNCMP(curbuf->b_p_fenc, "ucs-4", 5) == 0)
n = 4;
}
return n;
}
/*
* Remove all BOM from "s" by moving remaining text.
*/
void
remove_bom(char_u *s)
{
if (enc_utf8)
{
char_u *p = s;
while ((p = vim_strbyte(p, 0xef)) != NULL)
{
if (p[1] == 0xbb && p[2] == 0xbf)
STRMOVE(p, p + 3);
else
++p;
}
}
}
/*
* Get class of pointer:
* 0 for blank or NUL
* 1 for punctuation
* 2 for an (ASCII) word character
* >2 for other word characters
*/
int
mb_get_class(char_u *p)
{
return mb_get_class_buf(p, curbuf);
}
int
mb_get_class_buf(char_u *p, buf_T *buf)
{
if (MB_BYTE2LEN(p[0]) == 1)
{
if (p[0] == NUL || VIM_ISWHITE(p[0]))
return 0;
if (vim_iswordc_buf(p[0], buf))
return 2;
return 1;
}
if (enc_dbcs != 0 && p[0] != NUL && p[1] != NUL)
return dbcs_class(p[0], p[1]);
if (enc_utf8)
return utf_class_buf(utf_ptr2char(p), buf);
return 0;
}
/*
* Get class of a double-byte character. This always returns 3 or bigger.
* TODO: Should return 1 for punctuation.
*/
int
dbcs_class(unsigned lead, unsigned trail)
{
switch (enc_dbcs)
{
/* please add classify routine for your language in here */
case DBCS_JPNU: /* ? */
case DBCS_JPN:
{
/* JIS code classification */
unsigned char lb = lead;
unsigned char tb = trail;
/* convert process code to JIS */
# if defined(WIN3264) || defined(WIN32UNIX) || defined(MACOS)
/* process code is SJIS */
if (lb <= 0x9f)
lb = (lb - 0x81) * 2 + 0x21;
else
lb = (lb - 0xc1) * 2 + 0x21;
if (tb <= 0x7e)
tb -= 0x1f;
else if (tb <= 0x9e)
tb -= 0x20;
else
{
tb -= 0x7e;
lb += 1;
}
# else
/*
* XXX: Code page identification can not use with all
* system! So, some other encoding information
* will be needed.
* In japanese: SJIS,EUC,UNICODE,(JIS)
* Note that JIS-code system don't use as
* process code in most system because it uses
* escape sequences(JIS is context depend encoding).
*/
/* assume process code is JAPANESE-EUC */
lb &= 0x7f;
tb &= 0x7f;
# endif
/* exceptions */
switch (lb << 8 | tb)
{
case 0x2121: /* ZENKAKU space */
return 0;
case 0x2122: /* TOU-TEN (Japanese comma) */
case 0x2123: /* KU-TEN (Japanese period) */
case 0x2124: /* ZENKAKU comma */
case 0x2125: /* ZENKAKU period */
return 1;
case 0x213c: /* prolongedsound handled as KATAKANA */
return 13;
}
/* sieved by KU code */
switch (lb)
{
case 0x21:
case 0x22:
/* special symbols */
return 10;
case 0x23:
/* alpha-numeric */
return 11;
case 0x24:
/* hiragana */
return 12;
case 0x25:
/* katakana */
return 13;
case 0x26:
/* greek */
return 14;
case 0x27:
/* russian */
return 15;
case 0x28:
/* lines */
return 16;
default:
/* kanji */
return 17;
}
}
case DBCS_KORU: /* ? */
case DBCS_KOR:
{
/* KS code classification */
unsigned char c1 = lead;
unsigned char c2 = trail;