Mercurial > vim
comparison src/charset.c @ 15595:1ec942f1b648 v8.1.0805
patch 8.1.0805: too many #ifdefs
commit https://github.com/vim/vim/commit/135059724f140ceac889c9f8136bd1bf5c41d49d
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jan 24 15:04:48 2019 +0100
patch 8.1.0805: too many #ifdefs
Problem: Too many #ifdefs.
Solution: Graduate FEAT_MBYTE, part 1.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 24 Jan 2019 15:15:06 +0100 |
parents | 27b9a84395b5 |
children | 536dd2bc5ac9 |
comparison
equal
deleted
inserted
replaced
15594:7a8983fe4f97 | 15595:1ec942f1b648 |
---|---|
7 * See README.txt for an overview of the Vim source code. | 7 * See README.txt for an overview of the Vim source code. |
8 */ | 8 */ |
9 | 9 |
10 #include "vim.h" | 10 #include "vim.h" |
11 | 11 |
12 #ifdef FEAT_MBYTE | 12 #if defined(HAVE_WCHAR_H) |
13 # if defined(HAVE_WCHAR_H) | 13 # include <wchar.h> /* for towupper() and towlower() */ |
14 # include <wchar.h> /* for towupper() and towlower() */ | 14 #endif |
15 # endif | |
16 static int win_nolbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp); | 15 static int win_nolbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp); |
17 #endif | |
18 | 16 |
19 static unsigned nr2hex(unsigned c); | 17 static unsigned nr2hex(unsigned c); |
20 | 18 |
21 static int chartab_initialized = FALSE; | 19 static int chartab_initialized = FALSE; |
22 | 20 |
107 g_chartab[c++] = 1 + CT_PRINT_CHAR; | 105 g_chartab[c++] = 1 + CT_PRINT_CHAR; |
108 } | 106 } |
109 #endif | 107 #endif |
110 while (c < 256) | 108 while (c < 256) |
111 { | 109 { |
112 #ifdef FEAT_MBYTE | |
113 /* UTF-8: bytes 0xa0 - 0xff are printable (latin1) */ | 110 /* UTF-8: bytes 0xa0 - 0xff are printable (latin1) */ |
114 if (enc_utf8 && c >= 0xa0) | 111 if (enc_utf8 && c >= 0xa0) |
115 g_chartab[c++] = CT_PRINT_CHAR + 1; | 112 g_chartab[c++] = CT_PRINT_CHAR + 1; |
116 /* euc-jp characters starting with 0x8e are single width */ | 113 /* euc-jp characters starting with 0x8e are single width */ |
117 else if (enc_dbcs == DBCS_JPNU && c == 0x8e) | 114 else if (enc_dbcs == DBCS_JPNU && c == 0x8e) |
118 g_chartab[c++] = CT_PRINT_CHAR + 1; | 115 g_chartab[c++] = CT_PRINT_CHAR + 1; |
119 /* other double-byte chars can be printable AND double-width */ | 116 /* other double-byte chars can be printable AND double-width */ |
120 else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2) | 117 else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2) |
121 g_chartab[c++] = CT_PRINT_CHAR + 2; | 118 g_chartab[c++] = CT_PRINT_CHAR + 2; |
122 else | 119 else |
123 #endif | |
124 /* the rest is unprintable by default */ | 120 /* the rest is unprintable by default */ |
125 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; | 121 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; |
126 } | 122 } |
127 | 123 |
128 #ifdef FEAT_MBYTE | |
129 /* Assume that every multi-byte char is a filename character. */ | 124 /* Assume that every multi-byte char is a filename character. */ |
130 for (c = 1; c < 256; ++c) | 125 for (c = 1; c < 256; ++c) |
131 if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1) | 126 if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1) |
132 || (enc_dbcs == DBCS_JPNU && c == 0x8e) | 127 || (enc_dbcs == DBCS_JPNU && c == 0x8e) |
133 || (enc_utf8 && c >= 0xa0)) | 128 || (enc_utf8 && c >= 0xa0)) |
134 g_chartab[c] |= CT_FNAME_CHAR; | 129 g_chartab[c] |= CT_FNAME_CHAR; |
135 #endif | |
136 } | 130 } |
137 | 131 |
138 /* | 132 /* |
139 * Init word char flags all to FALSE | 133 * Init word char flags all to FALSE |
140 */ | 134 */ |
141 vim_memset(buf->b_chartab, 0, (size_t)32); | 135 vim_memset(buf->b_chartab, 0, (size_t)32); |
142 #ifdef FEAT_MBYTE | |
143 if (enc_dbcs != 0) | 136 if (enc_dbcs != 0) |
144 for (c = 0; c < 256; ++c) | 137 for (c = 0; c < 256; ++c) |
145 { | 138 { |
146 /* double-byte characters are probably word characters */ | 139 /* double-byte characters are probably word characters */ |
147 if (MB_BYTE2LEN(c) == 2) | 140 if (MB_BYTE2LEN(c) == 2) |
148 SET_CHARTAB(buf, c); | 141 SET_CHARTAB(buf, c); |
149 } | 142 } |
150 #endif | |
151 | 143 |
152 #ifdef FEAT_LISP | 144 #ifdef FEAT_LISP |
153 /* | 145 /* |
154 * In lisp mode the '-' character is included in keywords. | 146 * In lisp mode the '-' character is included in keywords. |
155 */ | 147 */ |
182 ++p; | 174 ++p; |
183 } | 175 } |
184 if (VIM_ISDIGIT(*p)) | 176 if (VIM_ISDIGIT(*p)) |
185 c = getdigits(&p); | 177 c = getdigits(&p); |
186 else | 178 else |
187 #ifdef FEAT_MBYTE | |
188 if (has_mbyte) | 179 if (has_mbyte) |
189 c = mb_ptr2char_adv(&p); | 180 c = mb_ptr2char_adv(&p); |
190 else | 181 else |
191 #endif | |
192 c = *p++; | 182 c = *p++; |
193 c2 = -1; | 183 c2 = -1; |
194 if (*p == '-' && p[1] != NUL) | 184 if (*p == '-' && p[1] != NUL) |
195 { | 185 { |
196 ++p; | 186 ++p; |
197 if (VIM_ISDIGIT(*p)) | 187 if (VIM_ISDIGIT(*p)) |
198 c2 = getdigits(&p); | 188 c2 = getdigits(&p); |
199 else | 189 else |
200 #ifdef FEAT_MBYTE | |
201 if (has_mbyte) | 190 if (has_mbyte) |
202 c2 = mb_ptr2char_adv(&p); | 191 c2 = mb_ptr2char_adv(&p); |
203 else | 192 else |
204 #endif | |
205 c2 = *p++; | 193 c2 = *p++; |
206 } | 194 } |
207 if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256 | 195 if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256 |
208 || !(*p == NUL || *p == ',')) | 196 || !(*p == NUL || *p == ',')) |
209 return FAIL; | 197 return FAIL; |
251 #endif | 239 #endif |
252 #ifdef FEAT_FKMAP | 240 #ifdef FEAT_FKMAP |
253 || (p_altkeymap | 241 || (p_altkeymap |
254 && (F_isalpha(c) || F_isdigit(c))) | 242 && (F_isalpha(c) || F_isdigit(c))) |
255 #endif | 243 #endif |
256 ) | 244 // For double-byte we keep the cell width, so |
257 #ifdef FEAT_MBYTE | 245 // that we can detect it from the first byte. |
258 /* For double-byte we keep the cell width, so | 246 ) && !(enc_dbcs && MB_BYTE2LEN(c) == 2)) |
259 * that we can detect it from the first byte. */ | |
260 && !(enc_dbcs && MB_BYTE2LEN(c) == 2) | |
261 #endif | |
262 ) | |
263 { | 247 { |
264 if (tilde) | 248 if (tilde) |
265 { | 249 { |
266 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) | 250 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) |
267 + ((dy_flags & DY_UHEX) ? 4 : 2); | 251 + ((dy_flags & DY_UHEX) ? 4 : 2); |
320 | 304 |
321 len = (int)STRLEN(buf); | 305 len = (int)STRLEN(buf); |
322 room = bufsize - len; | 306 room = bufsize - len; |
323 while (*buf != 0) | 307 while (*buf != 0) |
324 { | 308 { |
325 # ifdef FEAT_MBYTE | |
326 /* Assume a multi-byte character doesn't need translation. */ | 309 /* Assume a multi-byte character doesn't need translation. */ |
327 if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1) | 310 if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1) |
328 len -= trs_len; | 311 len -= trs_len; |
329 else | 312 else |
330 # endif | |
331 { | 313 { |
332 trs = transchar_byte(*buf); | 314 trs = transchar_byte(*buf); |
333 trs_len = (int)STRLEN(trs); | 315 trs_len = (int)STRLEN(trs); |
334 if (trs_len > 1) | 316 if (trs_len > 1) |
335 { | 317 { |
354 char_u * | 336 char_u * |
355 transstr(char_u *s) | 337 transstr(char_u *s) |
356 { | 338 { |
357 char_u *res; | 339 char_u *res; |
358 char_u *p; | 340 char_u *p; |
359 #ifdef FEAT_MBYTE | |
360 int l, len, c; | 341 int l, len, c; |
361 char_u hexbuf[11]; | 342 char_u hexbuf[11]; |
362 #endif | 343 |
363 | |
364 #ifdef FEAT_MBYTE | |
365 if (has_mbyte) | 344 if (has_mbyte) |
366 { | 345 { |
367 /* Compute the length of the result, taking account of unprintable | 346 /* Compute the length of the result, taking account of unprintable |
368 * multi-byte characters. */ | 347 * multi-byte characters. */ |
369 len = 0; | 348 len = 0; |
392 } | 371 } |
393 } | 372 } |
394 res = alloc((unsigned)(len + 1)); | 373 res = alloc((unsigned)(len + 1)); |
395 } | 374 } |
396 else | 375 else |
397 #endif | |
398 res = alloc((unsigned)(vim_strsize(s) + 1)); | 376 res = alloc((unsigned)(vim_strsize(s) + 1)); |
399 if (res != NULL) | 377 if (res != NULL) |
400 { | 378 { |
401 *res = NUL; | 379 *res = NUL; |
402 p = s; | 380 p = s; |
403 while (*p != NUL) | 381 while (*p != NUL) |
404 { | 382 { |
405 #ifdef FEAT_MBYTE | |
406 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) | 383 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
407 { | 384 { |
408 c = (*mb_ptr2char)(p); | 385 c = (*mb_ptr2char)(p); |
409 if (vim_isprintc(c)) | 386 if (vim_isprintc(c)) |
410 STRNCAT(res, p, l); /* append printable multi-byte char */ | 387 STRNCAT(res, p, l); /* append printable multi-byte char */ |
411 else | 388 else |
412 transchar_hex(res + STRLEN(res), c); | 389 transchar_hex(res + STRLEN(res), c); |
413 p += l; | 390 p += l; |
414 } | 391 } |
415 else | 392 else |
416 #endif | |
417 STRCAT(res, transchar_byte(*p++)); | 393 STRCAT(res, transchar_byte(*p++)); |
418 } | 394 } |
419 } | 395 } |
420 return res; | 396 return res; |
421 } | 397 } |
466 | 442 |
467 /* Make each character lower case. */ | 443 /* Make each character lower case. */ |
468 i = 0; | 444 i = 0; |
469 while (STR_CHAR(i) != NUL) | 445 while (STR_CHAR(i) != NUL) |
470 { | 446 { |
471 #ifdef FEAT_MBYTE | |
472 if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1)) | 447 if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1)) |
473 { | 448 { |
474 if (enc_utf8) | 449 if (enc_utf8) |
475 { | 450 { |
476 int c = utf_ptr2char(STR_PTR(i)); | 451 int c = utf_ptr2char(STR_PTR(i)); |
518 } | 493 } |
519 /* skip to next multi-byte char */ | 494 /* skip to next multi-byte char */ |
520 i += (*mb_ptr2len)(STR_PTR(i)); | 495 i += (*mb_ptr2len)(STR_PTR(i)); |
521 } | 496 } |
522 else | 497 else |
523 #endif | |
524 { | 498 { |
525 if (buf == NULL) | 499 if (buf == NULL) |
526 GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i)); | 500 GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i)); |
527 else | 501 else |
528 buf[i] = TOLOWER_LOC(buf[i]); | 502 buf[i] = TOLOWER_LOC(buf[i]); |
578 else | 552 else |
579 transchar_nonprint(transchar_buf + i, c); | 553 transchar_nonprint(transchar_buf + i, c); |
580 return transchar_buf; | 554 return transchar_buf; |
581 } | 555 } |
582 | 556 |
583 #if defined(FEAT_MBYTE) || defined(PROTO) | |
584 /* | 557 /* |
585 * Like transchar(), but called with a byte instead of a character. Checks | 558 * Like transchar(), but called with a byte instead of a character. Checks |
586 * for an illegal UTF-8 byte. | 559 * for an illegal UTF-8 byte. |
587 */ | 560 */ |
588 char_u * | 561 char_u * |
593 transchar_nonprint(transchar_buf, c); | 566 transchar_nonprint(transchar_buf, c); |
594 return transchar_buf; | 567 return transchar_buf; |
595 } | 568 } |
596 return transchar(c); | 569 return transchar(c); |
597 } | 570 } |
598 #endif | |
599 | 571 |
600 /* | 572 /* |
601 * Convert non-printable character to two or more printable characters in | 573 * Convert non-printable character to two or more printable characters in |
602 * "buf[]". "buf" needs to be able to hold five bytes. | 574 * "buf[]". "buf" needs to be able to hold five bytes. |
603 * Does NOT work for multi-byte characters, c must be <= 255. | 575 * Does NOT work for multi-byte characters, c must be <= 255. |
630 buf[1] = c ^ 0x40; /* DEL displayed as ^? */ | 602 buf[1] = c ^ 0x40; /* DEL displayed as ^? */ |
631 #endif | 603 #endif |
632 | 604 |
633 buf[2] = NUL; | 605 buf[2] = NUL; |
634 } | 606 } |
635 #ifdef FEAT_MBYTE | |
636 else if (enc_utf8 && c >= 0x80) | 607 else if (enc_utf8 && c >= 0x80) |
637 { | 608 { |
638 transchar_hex(buf, c); | 609 transchar_hex(buf, c); |
639 } | 610 } |
640 #endif | |
641 #ifndef EBCDIC | 611 #ifndef EBCDIC |
642 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) /* 0xa0 - 0xfe */ | 612 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) /* 0xa0 - 0xfe */ |
643 { | 613 { |
644 buf[0] = '|'; | 614 buf[0] = '|'; |
645 buf[1] = c - 0x80; | 615 buf[1] = c - 0x80; |
673 transchar_hex(char_u *buf, int c) | 643 transchar_hex(char_u *buf, int c) |
674 { | 644 { |
675 int i = 0; | 645 int i = 0; |
676 | 646 |
677 buf[0] = '<'; | 647 buf[0] = '<'; |
678 #ifdef FEAT_MBYTE | |
679 if (c > 255) | 648 if (c > 255) |
680 { | 649 { |
681 buf[++i] = nr2hex((unsigned)c >> 12); | 650 buf[++i] = nr2hex((unsigned)c >> 12); |
682 buf[++i] = nr2hex((unsigned)c >> 8); | 651 buf[++i] = nr2hex((unsigned)c >> 8); |
683 } | 652 } |
684 #endif | |
685 buf[++i] = nr2hex((unsigned)c >> 4); | 653 buf[++i] = nr2hex((unsigned)c >> 4); |
686 buf[++i] = nr2hex((unsigned)c); | 654 buf[++i] = nr2hex((unsigned)c); |
687 buf[++i] = '>'; | 655 buf[++i] = '>'; |
688 buf[++i] = NUL; | 656 buf[++i] = NUL; |
689 } | 657 } |
710 * cells depends on further bytes. | 678 * cells depends on further bytes. |
711 */ | 679 */ |
712 int | 680 int |
713 byte2cells(int b) | 681 byte2cells(int b) |
714 { | 682 { |
715 #ifdef FEAT_MBYTE | |
716 if (enc_utf8 && b >= 0x80) | 683 if (enc_utf8 && b >= 0x80) |
717 return 0; | 684 return 0; |
718 #endif | |
719 return (g_chartab[b] & CT_CELL_MASK); | 685 return (g_chartab[b] & CT_CELL_MASK); |
720 } | 686 } |
721 | 687 |
722 /* | 688 /* |
723 * Return number of display cells occupied by character "c". | 689 * Return number of display cells occupied by character "c". |
727 int | 693 int |
728 char2cells(int c) | 694 char2cells(int c) |
729 { | 695 { |
730 if (IS_SPECIAL(c)) | 696 if (IS_SPECIAL(c)) |
731 return char2cells(K_SECOND(c)) + 2; | 697 return char2cells(K_SECOND(c)) + 2; |
732 #ifdef FEAT_MBYTE | |
733 if (c >= 0x80) | 698 if (c >= 0x80) |
734 { | 699 { |
735 /* UTF-8: above 0x80 need to check the value */ | 700 /* UTF-8: above 0x80 need to check the value */ |
736 if (enc_utf8) | 701 if (enc_utf8) |
737 return utf_char2cells(c); | 702 return utf_char2cells(c); |
742 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e) | 707 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e) |
743 return 1; | 708 return 1; |
744 return 2; | 709 return 2; |
745 } | 710 } |
746 } | 711 } |
747 #endif | |
748 return (g_chartab[c & 0xff] & CT_CELL_MASK); | 712 return (g_chartab[c & 0xff] & CT_CELL_MASK); |
749 } | 713 } |
750 | 714 |
751 /* | 715 /* |
752 * Return number of display cells occupied by character at "*p". | 716 * Return number of display cells occupied by character at "*p". |
753 * A TAB is counted as two cells: "^I" or four: "<09>". | 717 * A TAB is counted as two cells: "^I" or four: "<09>". |
754 */ | 718 */ |
755 int | 719 int |
756 ptr2cells(char_u *p) | 720 ptr2cells(char_u *p) |
757 { | 721 { |
758 #ifdef FEAT_MBYTE | |
759 /* For UTF-8 we need to look at more bytes if the first byte is >= 0x80. */ | 722 /* For UTF-8 we need to look at more bytes if the first byte is >= 0x80. */ |
760 if (enc_utf8 && *p >= 0x80) | 723 if (enc_utf8 && *p >= 0x80) |
761 return utf_ptr2cells(p); | 724 return utf_ptr2cells(p); |
762 /* For DBCS we can tell the cell count from the first byte. */ | 725 /* For DBCS we can tell the cell count from the first byte. */ |
763 #endif | |
764 return (g_chartab[*p] & CT_CELL_MASK); | 726 return (g_chartab[*p] & CT_CELL_MASK); |
765 } | 727 } |
766 | 728 |
767 /* | 729 /* |
768 * Return the number of character cells string "s" will take on the screen, | 730 * Return the number of character cells string "s" will take on the screen, |
782 vim_strnsize(char_u *s, int len) | 744 vim_strnsize(char_u *s, int len) |
783 { | 745 { |
784 int size = 0; | 746 int size = 0; |
785 | 747 |
786 while (*s != NUL && --len >= 0) | 748 while (*s != NUL && --len >= 0) |
787 { | |
788 #ifdef FEAT_MBYTE | |
789 if (has_mbyte) | 749 if (has_mbyte) |
790 { | 750 { |
791 int l = (*mb_ptr2len)(s); | 751 int l = (*mb_ptr2len)(s); |
792 | 752 |
793 size += ptr2cells(s); | 753 size += ptr2cells(s); |
794 s += l; | 754 s += l; |
795 len -= l - 1; | 755 len -= l - 1; |
796 } | 756 } |
797 else | 757 else |
798 #endif | |
799 size += byte2cells(*s++); | 758 size += byte2cells(*s++); |
800 } | 759 |
801 return size; | 760 return size; |
802 } | 761 } |
803 | 762 |
804 /* | 763 /* |
805 * Return the number of characters 'c' will take on the screen, taking | 764 * Return the number of characters 'c' will take on the screen, taking |
905 int | 864 int |
906 vim_iswordc_buf(int c, buf_T *buf) | 865 vim_iswordc_buf(int c, buf_T *buf) |
907 { | 866 { |
908 if (c >= 0x100) | 867 if (c >= 0x100) |
909 { | 868 { |
910 #ifdef FEAT_MBYTE | |
911 if (enc_dbcs != 0) | 869 if (enc_dbcs != 0) |
912 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2; | 870 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2; |
913 if (enc_utf8) | 871 if (enc_utf8) |
914 return utf_class_buf(c, buf) >= 2; | 872 return utf_class_buf(c, buf) >= 2; |
915 #endif | |
916 return FALSE; | 873 return FALSE; |
917 } | 874 } |
918 return (c > 0 && GET_CHARTAB(buf, c) != 0); | 875 return (c > 0 && GET_CHARTAB(buf, c) != 0); |
919 } | 876 } |
920 | 877 |
930 int | 887 int |
931 vim_iswordp_buf(char_u *p, buf_T *buf) | 888 vim_iswordp_buf(char_u *p, buf_T *buf) |
932 { | 889 { |
933 int c = *p; | 890 int c = *p; |
934 | 891 |
935 #ifdef FEAT_MBYTE | |
936 if (has_mbyte && MB_BYTE2LEN(c) > 1) | 892 if (has_mbyte && MB_BYTE2LEN(c) > 1) |
937 c = (*mb_ptr2char)(p); | 893 c = (*mb_ptr2char)(p); |
938 #endif | |
939 return vim_iswordc_buf(c, buf); | 894 return vim_iswordc_buf(c, buf); |
940 } | 895 } |
941 | 896 |
942 /* | 897 /* |
943 * return TRUE if 'c' is a valid file-name character | 898 * return TRUE if 'c' is a valid file-name character |
971 * Unicode. | 926 * Unicode. |
972 */ | 927 */ |
973 int | 928 int |
974 vim_isprintc(int c) | 929 vim_isprintc(int c) |
975 { | 930 { |
976 #ifdef FEAT_MBYTE | |
977 if (enc_utf8 && c >= 0x100) | 931 if (enc_utf8 && c >= 0x100) |
978 return utf_printable(c); | 932 return utf_printable(c); |
979 #endif | |
980 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR))); | 933 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR))); |
981 } | 934 } |
982 | 935 |
983 /* | 936 /* |
984 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head | 937 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head |
985 * byte of a double-byte character. | 938 * byte of a double-byte character. |
986 */ | 939 */ |
987 int | 940 int |
988 vim_isprintc_strict(int c) | 941 vim_isprintc_strict(int c) |
989 { | 942 { |
990 #ifdef FEAT_MBYTE | |
991 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1) | 943 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1) |
992 return FALSE; | 944 return FALSE; |
993 if (enc_utf8 && c >= 0x100) | 945 if (enc_utf8 && c >= 0x100) |
994 return utf_printable(c); | 946 return utf_printable(c); |
995 #endif | |
996 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR))); | 947 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR))); |
997 } | 948 } |
998 | 949 |
999 /* | 950 /* |
1000 * like chartabsize(), but also check for line breaks on the screen | 951 * like chartabsize(), but also check for line breaks on the screen |
1007 { | 958 { |
1008 #ifdef FEAT_LINEBREAK | 959 #ifdef FEAT_LINEBREAK |
1009 if (!curwin->w_p_lbr && *p_sbr == NUL && !curwin->w_p_bri) | 960 if (!curwin->w_p_lbr && *p_sbr == NUL && !curwin->w_p_bri) |
1010 { | 961 { |
1011 #endif | 962 #endif |
1012 #ifdef FEAT_MBYTE | |
1013 if (curwin->w_p_wrap) | 963 if (curwin->w_p_wrap) |
1014 return win_nolbr_chartabsize(curwin, s, col, NULL); | 964 return win_nolbr_chartabsize(curwin, s, col, NULL); |
1015 #endif | |
1016 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col) | 965 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col) |
1017 #ifdef FEAT_LINEBREAK | 966 #ifdef FEAT_LINEBREAK |
1018 } | 967 } |
1019 return win_lbr_chartabsize(curwin, line == NULL ? s : line, s, col, NULL); | 968 return win_lbr_chartabsize(curwin, line == NULL ? s : line, s, col, NULL); |
1020 #endif | 969 #endif |
1056 int size; | 1005 int size; |
1057 colnr_T col2; | 1006 colnr_T col2; |
1058 colnr_T col_adj = 0; /* col + screen size of tab */ | 1007 colnr_T col_adj = 0; /* col + screen size of tab */ |
1059 colnr_T colmax; | 1008 colnr_T colmax; |
1060 int added; | 1009 int added; |
1061 # ifdef FEAT_MBYTE | |
1062 int mb_added = 0; | 1010 int mb_added = 0; |
1063 # else | |
1064 # define mb_added 0 | |
1065 # endif | |
1066 int numberextra; | 1011 int numberextra; |
1067 char_u *ps; | 1012 char_u *ps; |
1068 int tab_corr = (*s == TAB); | 1013 int tab_corr = (*s == TAB); |
1069 int n; | 1014 int n; |
1070 | 1015 |
1072 * No 'linebreak', 'showbreak' and 'breakindent': return quickly. | 1017 * No 'linebreak', 'showbreak' and 'breakindent': return quickly. |
1073 */ | 1018 */ |
1074 if (!wp->w_p_lbr && !wp->w_p_bri && *p_sbr == NUL) | 1019 if (!wp->w_p_lbr && !wp->w_p_bri && *p_sbr == NUL) |
1075 #endif | 1020 #endif |
1076 { | 1021 { |
1077 #ifdef FEAT_MBYTE | |
1078 if (wp->w_p_wrap) | 1022 if (wp->w_p_wrap) |
1079 return win_nolbr_chartabsize(wp, s, col, headp); | 1023 return win_nolbr_chartabsize(wp, s, col, headp); |
1080 #endif | |
1081 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col) | 1024 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col) |
1082 } | 1025 } |
1083 | 1026 |
1084 #ifdef FEAT_LINEBREAK | 1027 #ifdef FEAT_LINEBREAK |
1085 /* | 1028 /* |
1133 tab_corr = FALSE; | 1076 tab_corr = FALSE; |
1134 break; | 1077 break; |
1135 } | 1078 } |
1136 } | 1079 } |
1137 } | 1080 } |
1138 # ifdef FEAT_MBYTE | |
1139 else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1 | 1081 else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1 |
1140 && wp->w_p_wrap && in_win_border(wp, col)) | 1082 && wp->w_p_wrap && in_win_border(wp, col)) |
1141 { | 1083 { |
1142 ++size; /* Count the ">" in the last column. */ | 1084 ++size; /* Count the ">" in the last column. */ |
1143 mb_added = 1; | 1085 mb_added = 1; |
1144 } | 1086 } |
1145 # endif | |
1146 | 1087 |
1147 /* | 1088 /* |
1148 * May have to add something for 'breakindent' and/or 'showbreak' | 1089 * May have to add something for 'breakindent' and/or 'showbreak' |
1149 * string at start of line. | 1090 * string at start of line. |
1150 * Set *headp to the size of what we add. | 1091 * Set *headp to the size of what we add. |
1208 *headp = added + mb_added; | 1149 *headp = added + mb_added; |
1209 return size; | 1150 return size; |
1210 #endif | 1151 #endif |
1211 } | 1152 } |
1212 | 1153 |
1213 #if defined(FEAT_MBYTE) || defined(PROTO) | |
1214 /* | 1154 /* |
1215 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and | 1155 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and |
1216 * 'wrap' is on. This means we need to check for a double-byte character that | 1156 * 'wrap' is on. This means we need to check for a double-byte character that |
1217 * doesn't fit at the end of the screen line. | 1157 * doesn't fit at the end of the screen line. |
1218 */ | 1158 */ |
1267 width2 = width1 + win_col_off2(wp); | 1207 width2 = width1 + win_col_off2(wp); |
1268 if (width2 <= 0) | 1208 if (width2 <= 0) |
1269 return FALSE; | 1209 return FALSE; |
1270 return ((vcol - width1) % width2 == width2 - 1); | 1210 return ((vcol - width1) % width2 == width2 - 1); |
1271 } | 1211 } |
1272 #endif /* FEAT_MBYTE */ | |
1273 | 1212 |
1274 /* | 1213 /* |
1275 * Get virtual column number of pos. | 1214 * Get virtual column number of pos. |
1276 * start: on the first position of this character (TAB, ctrl) | 1215 * start: on the first position of this character (TAB, ctrl) |
1277 * cursor: where the cursor is on this character (first char, except for TAB) | 1216 * cursor: where the cursor is on this character (first char, except for TAB) |
1308 /* Special check for an empty line, which can happen on exit, when | 1247 /* Special check for an empty line, which can happen on exit, when |
1309 * ml_get_buf() always returns an empty string. */ | 1248 * ml_get_buf() always returns an empty string. */ |
1310 if (*ptr == NUL) | 1249 if (*ptr == NUL) |
1311 pos->col = 0; | 1250 pos->col = 0; |
1312 posptr = ptr + pos->col; | 1251 posptr = ptr + pos->col; |
1313 #ifdef FEAT_MBYTE | |
1314 if (has_mbyte) | 1252 if (has_mbyte) |
1315 /* always start on the first byte */ | 1253 /* always start on the first byte */ |
1316 posptr -= (*mb_head_off)(line, posptr); | 1254 posptr -= (*mb_head_off)(line, posptr); |
1317 #endif | |
1318 } | 1255 } |
1319 | 1256 |
1320 /* | 1257 /* |
1321 * This function is used very often, do some speed optimizations. | 1258 * This function is used very often, do some speed optimizations. |
1322 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set | 1259 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set |
1327 #ifdef FEAT_LINEBREAK | 1264 #ifdef FEAT_LINEBREAK |
1328 && !wp->w_p_lbr && *p_sbr == NUL && !wp->w_p_bri | 1265 && !wp->w_p_lbr && *p_sbr == NUL && !wp->w_p_bri |
1329 #endif | 1266 #endif |
1330 ) | 1267 ) |
1331 { | 1268 { |
1332 #ifndef FEAT_MBYTE | |
1333 head = 0; | 1269 head = 0; |
1334 #endif | |
1335 for (;;) | 1270 for (;;) |
1336 { | 1271 { |
1337 #ifdef FEAT_MBYTE | |
1338 head = 0; | 1272 head = 0; |
1339 #endif | |
1340 c = *ptr; | 1273 c = *ptr; |
1341 /* make sure we don't go past the end of the line */ | 1274 /* make sure we don't go past the end of the line */ |
1342 if (c == NUL) | 1275 if (c == NUL) |
1343 { | 1276 { |
1344 incr = 1; /* NUL at end of line only takes one column */ | 1277 incr = 1; /* NUL at end of line only takes one column */ |
1351 #else | 1284 #else |
1352 incr = ts - (vcol % ts); | 1285 incr = ts - (vcol % ts); |
1353 #endif | 1286 #endif |
1354 else | 1287 else |
1355 { | 1288 { |
1356 #ifdef FEAT_MBYTE | |
1357 if (has_mbyte) | 1289 if (has_mbyte) |
1358 { | 1290 { |
1359 /* For utf-8, if the byte is >= 0x80, need to look at | 1291 /* For utf-8, if the byte is >= 0x80, need to look at |
1360 * further bytes to find the cell width. */ | 1292 * further bytes to find the cell width. */ |
1361 if (enc_utf8 && c >= 0x80) | 1293 if (enc_utf8 && c >= 0x80) |
1372 ++incr; | 1304 ++incr; |
1373 head = 1; | 1305 head = 1; |
1374 } | 1306 } |
1375 } | 1307 } |
1376 else | 1308 else |
1377 #endif | |
1378 incr = g_chartab[c] & CT_CELL_MASK; | 1309 incr = g_chartab[c] & CT_CELL_MASK; |
1379 } | 1310 } |
1380 | 1311 |
1381 if (posptr != NULL && ptr >= posptr) /* character at pos->col */ | 1312 if (posptr != NULL && ptr >= posptr) /* character at pos->col */ |
1382 break; | 1313 break; |
1458 colnr_T *end) | 1389 colnr_T *end) |
1459 { | 1390 { |
1460 colnr_T col; | 1391 colnr_T col; |
1461 colnr_T coladd; | 1392 colnr_T coladd; |
1462 colnr_T endadd; | 1393 colnr_T endadd; |
1463 # ifdef FEAT_MBYTE | |
1464 char_u *ptr; | 1394 char_u *ptr; |
1465 # endif | |
1466 | 1395 |
1467 if (virtual_active()) | 1396 if (virtual_active()) |
1468 { | 1397 { |
1469 /* For virtual mode, only want one value */ | 1398 /* For virtual mode, only want one value */ |
1470 getvcol(wp, pos, &col, NULL, NULL); | 1399 getvcol(wp, pos, &col, NULL, NULL); |
1471 | 1400 |
1472 coladd = pos->coladd; | 1401 coladd = pos->coladd; |
1473 endadd = 0; | 1402 endadd = 0; |
1474 # ifdef FEAT_MBYTE | |
1475 /* Cannot put the cursor on part of a wide character. */ | 1403 /* Cannot put the cursor on part of a wide character. */ |
1476 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); | 1404 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); |
1477 if (pos->col < (colnr_T)STRLEN(ptr)) | 1405 if (pos->col < (colnr_T)STRLEN(ptr)) |
1478 { | 1406 { |
1479 int c = (*mb_ptr2char)(ptr + pos->col); | 1407 int c = (*mb_ptr2char)(ptr + pos->col); |
1485 endadd = 0; | 1413 endadd = 0; |
1486 else | 1414 else |
1487 coladd = 0; | 1415 coladd = 0; |
1488 } | 1416 } |
1489 } | 1417 } |
1490 # endif | |
1491 col += coladd; | 1418 col += coladd; |
1492 if (start != NULL) | 1419 if (start != NULL) |
1493 *start = col; | 1420 *start = col; |
1494 if (cursor != NULL) | 1421 if (cursor != NULL) |
1495 *cursor = col; | 1422 *cursor = col; |
1682 vim_isbdigit(int c) | 1609 vim_isbdigit(int c) |
1683 { | 1610 { |
1684 return (c == '0' || c == '1'); | 1611 return (c == '0' || c == '1'); |
1685 } | 1612 } |
1686 | 1613 |
1687 #if defined(FEAT_MBYTE) || defined(PROTO) | |
1688 /* | 1614 /* |
1689 * Vim's own character class functions. These exist because many library | 1615 * Vim's own character class functions. These exist because many library |
1690 * islower()/toupper() etc. do not work properly: they crash when used with | 1616 * islower()/toupper() etc. do not work properly: they crash when used with |
1691 * invalid values or can't handle latin1 when the locale is C. | 1617 * invalid values or can't handle latin1 when the locale is C. |
1692 * Speed is most important here. | 1618 * Speed is most important here. |
1795 } | 1721 } |
1796 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII)) | 1722 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII)) |
1797 return TOLOWER_ASC(c); | 1723 return TOLOWER_ASC(c); |
1798 return TOLOWER_LOC(c); | 1724 return TOLOWER_LOC(c); |
1799 } | 1725 } |
1800 #endif | |
1801 | 1726 |
1802 /* | 1727 /* |
1803 * skiptowhite: skip over text until ' ' or '\t' or NUL. | 1728 * skiptowhite: skip over text until ' ' or '\t' or NUL. |
1804 */ | 1729 */ |
1805 char_u * | 1730 char_u * |
2075 int | 2000 int |
2076 rem_backslash(char_u *str) | 2001 rem_backslash(char_u *str) |
2077 { | 2002 { |
2078 #ifdef BACKSLASH_IN_FILENAME | 2003 #ifdef BACKSLASH_IN_FILENAME |
2079 return (str[0] == '\\' | 2004 return (str[0] == '\\' |
2080 # ifdef FEAT_MBYTE | |
2081 && str[1] < 0x80 | 2005 && str[1] < 0x80 |
2082 # endif | |
2083 && (str[1] == ' ' | 2006 && (str[1] == ' ' |
2084 || (str[1] != NUL | 2007 || (str[1] != NUL |
2085 && str[1] != '*' | 2008 && str[1] != '*' |
2086 && str[1] != '?' | 2009 && str[1] != '?' |
2087 && !vim_isfilec(str[1])))); | 2010 && !vim_isfilec(str[1])))); |