-
Notifications
You must be signed in to change notification settings - Fork 0
/
charset.c
2464 lines (2282 loc) · 57.7 KB
/
charset.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
*
* 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.
*/
#include "vim.h"
#if defined(HAVE_WCHAR_H)
# include <wchar.h> // for towupper() and towlower()
#endif
static int win_nolbr_chartabsize(chartabsize_T *cts, int *headp);
static unsigned nr2hex(unsigned c);
static int chartab_initialized = FALSE;
// b_chartab[] is an array of 32 bytes, each bit representing one of the
// characters 0-255.
#define SET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] |= (1 << ((c) & 0x7))
#define RESET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] &= ~(1 << ((c) & 0x7))
#define GET_CHARTAB(buf, c) ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7)))
// table used below, see init_chartab() for an explanation
static char_u g_chartab[256];
/*
* Flags for g_chartab[].
*/
#define CT_CELL_MASK 0x07 // mask: nr of display cells (1, 2 or 4)
#define CT_PRINT_CHAR 0x10 // flag: set for printable chars
#define CT_ID_CHAR 0x20 // flag: set for ID chars
#define CT_FNAME_CHAR 0x40 // flag: set for file name chars
static int in_win_border(win_T *wp, colnr_T vcol);
/*
* Fill g_chartab[]. Also fills curbuf->b_chartab[] with flags for keyword
* characters for current buffer.
*
* Depends on the option settings 'iskeyword', 'isident', 'isfname',
* 'isprint' and 'encoding'.
*
* The index in g_chartab[] depends on 'encoding':
* - For non-multi-byte index with the byte (same as the character).
* - For DBCS index with the first byte.
* - For UTF-8 index with the character (when first byte is up to 0x80 it is
* the same as the character, if the first byte is 0x80 and above it depends
* on further bytes).
*
* The contents of g_chartab[]:
* - The lower two bits, masked by CT_CELL_MASK, give the number of display
* cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80.
* - CT_PRINT_CHAR bit is set when the character is printable (no need to
* translate the character before displaying it). Note that only DBCS
* characters can have 2 display cells and still be printable.
* - CT_FNAME_CHAR bit is set when the character can be in a file name.
* - CT_ID_CHAR bit is set when the character can be in an identifier.
*
* Return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has an
* error, OK otherwise.
*/
int
init_chartab(void)
{
return buf_init_chartab(curbuf, TRUE);
}
int
buf_init_chartab(
buf_T *buf,
int global) // FALSE: only set buf->b_chartab[]
{
int c;
int c2;
char_u *p;
int i;
int tilde;
int do_isalpha;
if (global)
{
/*
* Set the default size for printable characters:
* From <Space> to '~' is 1 (printable), others are 2 (not printable).
* This also inits all 'isident' and 'isfname' flags to FALSE.
*/
c = 0;
while (c < ' ')
g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
while (c <= '~')
g_chartab[c++] = 1 + CT_PRINT_CHAR;
while (c < 256)
{
// UTF-8: bytes 0xa0 - 0xff are printable (latin1)
if (enc_utf8 && c >= 0xa0)
g_chartab[c++] = CT_PRINT_CHAR + 1;
// euc-jp characters starting with 0x8e are single width
else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
g_chartab[c++] = CT_PRINT_CHAR + 1;
// other double-byte chars can be printable AND double-width
else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2)
g_chartab[c++] = CT_PRINT_CHAR + 2;
else
// the rest is unprintable by default
g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
}
// Assume that every multi-byte char is a filename character.
for (c = 1; c < 256; ++c)
if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1)
|| (enc_dbcs == DBCS_JPNU && c == 0x8e)
|| (enc_utf8 && c >= 0xa0))
g_chartab[c] |= CT_FNAME_CHAR;
}
/*
* Init word char flags all to FALSE
*/
CLEAR_FIELD(buf->b_chartab);
if (enc_dbcs != 0)
for (c = 0; c < 256; ++c)
{
// double-byte characters are probably word characters
if (MB_BYTE2LEN(c) == 2)
SET_CHARTAB(buf, c);
}
/*
* In lisp mode the '-' character is included in keywords.
*/
if (buf->b_p_lisp)
SET_CHARTAB(buf, '-');
// Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint'
// options Each option is a list of characters, character numbers or
// ranges, separated by commas, e.g.: "200-210,x,#-178,-"
for (i = global ? 0 : 3; i <= 3; ++i)
{
if (i == 0)
p = p_isi; // first round: 'isident'
else if (i == 1)
p = p_isp; // second round: 'isprint'
else if (i == 2)
p = p_isf; // third round: 'isfname'
else // i == 3
p = buf->b_p_isk; // fourth round: 'iskeyword'
while (*p)
{
tilde = FALSE;
do_isalpha = FALSE;
if (*p == '^' && p[1] != NUL)
{
tilde = TRUE;
++p;
}
if (VIM_ISDIGIT(*p))
c = getdigits(&p);
else if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
c = *p++;
c2 = -1;
if (*p == '-' && p[1] != NUL)
{
++p;
if (VIM_ISDIGIT(*p))
c2 = getdigits(&p);
else if (has_mbyte)
c2 = mb_ptr2char_adv(&p);
else
c2 = *p++;
}
if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256
|| !(*p == NUL || *p == ','))
return FAIL;
if (c2 == -1) // not a range
{
/*
* A single '@' (not "@-@"):
* Decide on letters being ID/printable/keyword chars with
* standard function isalpha(). This takes care of locale for
* single-byte characters).
*/
if (c == '@')
{
do_isalpha = TRUE;
c = 1;
c2 = 255;
}
else
c2 = c;
}
while (c <= c2)
{
// Use the MB_ functions here, because isalpha() doesn't
// work properly when 'encoding' is "latin1" and the locale is
// "C".
if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c))
{
if (i == 0) // (re)set ID flag
{
if (tilde)
g_chartab[c] &= ~CT_ID_CHAR;
else
g_chartab[c] |= CT_ID_CHAR;
}
else if (i == 1) // (re)set printable
{
if ((c < ' ' || c > '~'
// For double-byte we keep the cell width, so
// that we can detect it from the first byte.
) && !(enc_dbcs && MB_BYTE2LEN(c) == 2))
{
if (tilde)
{
g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK)
+ ((dy_flags & DY_UHEX) ? 4 : 2);
g_chartab[c] &= ~CT_PRINT_CHAR;
}
else
{
g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK)
+ 1;
g_chartab[c] |= CT_PRINT_CHAR;
}
}
}
else if (i == 2) // (re)set fname flag
{
if (tilde)
g_chartab[c] &= ~CT_FNAME_CHAR;
else
g_chartab[c] |= CT_FNAME_CHAR;
}
else // i == 3 (re)set keyword flag
{
if (tilde)
RESET_CHARTAB(buf, c);
else
SET_CHARTAB(buf, c);
}
}
++c;
}
c = *p;
p = skip_to_option_part(p);
if (c == ',' && *p == NUL)
// Trailing comma is not allowed.
return FAIL;
}
}
chartab_initialized = TRUE;
return OK;
}
/*
* Translate any special characters in buf[bufsize] in-place.
* The result is a string with only printable characters, but if there is not
* enough room, not all characters will be translated.
*/
void
trans_characters(
char_u *buf,
int bufsize)
{
int len; // length of string needing translation
int room; // room in buffer after string
char_u *trs; // translated character
int trs_len; // length of trs[]
len = (int)STRLEN(buf);
room = bufsize - len;
while (*buf != 0)
{
// Assume a multi-byte character doesn't need translation.
if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1)
len -= trs_len;
else
{
trs = transchar_byte(*buf);
trs_len = (int)STRLEN(trs);
if (trs_len > 1)
{
room -= trs_len - 1;
if (room <= 0)
return;
mch_memmove(buf + trs_len, buf + 1, (size_t)len);
}
mch_memmove(buf, trs, (size_t)trs_len);
--len;
}
buf += trs_len;
}
}
/*
* Translate a string into allocated memory, replacing special chars with
* printable chars. Returns NULL when out of memory.
*/
char_u *
transstr(char_u *s)
{
char_u *res;
char_u *p;
int l, len, c;
char_u hexbuf[11];
if (has_mbyte)
{
// Compute the length of the result, taking account of unprintable
// multi-byte characters.
len = 0;
p = s;
while (*p != NUL)
{
if ((l = (*mb_ptr2len)(p)) > 1)
{
c = (*mb_ptr2char)(p);
p += l;
if (vim_isprintc(c))
len += l;
else
{
transchar_hex(hexbuf, c);
len += (int)STRLEN(hexbuf);
}
}
else
{
l = byte2cells(*p++);
if (l > 0)
len += l;
else
len += 4; // illegal byte sequence
}
}
res = alloc(len + 1);
}
else
res = alloc(vim_strsize(s) + 1);
if (res == NULL)
return NULL;
*res = NUL;
p = s;
while (*p != NUL)
{
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
{
c = (*mb_ptr2char)(p);
if (vim_isprintc(c))
STRNCAT(res, p, l); // append printable multi-byte char
else
transchar_hex(res + STRLEN(res), c);
p += l;
}
else
STRCAT(res, transchar_byte(*p++));
}
return res;
}
/*
* Convert the string "str[orglen]" to do ignore-case comparing. Uses the
* current locale.
* When "buf" is NULL returns an allocated string (NULL for out-of-memory).
* Otherwise puts the result in "buf[buflen]".
*/
char_u *
str_foldcase(
char_u *str,
int orglen,
char_u *buf,
int buflen)
{
garray_T ga;
int i;
int len = orglen;
#define GA_CHAR(i) ((char_u *)ga.ga_data)[i]
#define GA_PTR(i) ((char_u *)ga.ga_data + (i))
#define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i])
#define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + (i))
// Copy "str" into "buf" or allocated memory, unmodified.
if (buf == NULL)
{
ga_init2(&ga, 1, 10);
if (ga_grow(&ga, len + 1) == FAIL)
return NULL;
mch_memmove(ga.ga_data, str, (size_t)len);
ga.ga_len = len;
}
else
{
if (len >= buflen) // Ugly!
len = buflen - 1;
mch_memmove(buf, str, (size_t)len);
}
if (buf == NULL)
GA_CHAR(len) = NUL;
else
buf[len] = NUL;
// Make each character lower case.
i = 0;
while (STR_CHAR(i) != NUL)
{
if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1))
{
if (enc_utf8)
{
int c = utf_ptr2char(STR_PTR(i));
int olen = utf_ptr2len(STR_PTR(i));
int lc = utf_tolower(c);
// Only replace the character when it is not an invalid
// sequence (ASCII character or more than one byte) and
// utf_tolower() doesn't return the original character.
if ((c < 0x80 || olen > 1) && c != lc)
{
int nlen = utf_char2len(lc);
// If the byte length changes need to shift the following
// characters forward or backward.
if (olen != nlen)
{
if (nlen > olen)
{
if (buf == NULL
? ga_grow(&ga, nlen - olen + 1) == FAIL
: len + nlen - olen >= buflen)
{
// out of memory, keep old char
lc = c;
nlen = olen;
}
}
if (olen != nlen)
{
if (buf == NULL)
{
STRMOVE(GA_PTR(i) + nlen, GA_PTR(i) + olen);
ga.ga_len += nlen - olen;
}
else
{
STRMOVE(buf + i + nlen, buf + i + olen);
len += nlen - olen;
}
}
}
(void)utf_char2bytes(lc, STR_PTR(i));
}
}
// skip to next multi-byte char
i += (*mb_ptr2len)(STR_PTR(i));
}
else
{
if (buf == NULL)
GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i));
else
buf[i] = TOLOWER_LOC(buf[i]);
++i;
}
}
if (buf == NULL)
return (char_u *)ga.ga_data;
return buf;
}
/*
* Catch 22: g_chartab[] can't be initialized before the options are
* initialized, and initializing options may cause transchar() to be called!
* When chartab_initialized == FALSE don't use g_chartab[].
* Does NOT work for multi-byte characters, c must be <= 255.
* Also doesn't work for the first byte of a multi-byte, "c" must be a
* character!
*/
static char_u transchar_charbuf[7];
char_u *
transchar(int c)
{
return transchar_buf(curbuf, c);
}
char_u *
transchar_buf(buf_T *buf, int c)
{
int i;
i = 0;
if (IS_SPECIAL(c)) // special key code, display as ~@ char
{
transchar_charbuf[0] = '~';
transchar_charbuf[1] = '@';
i = 2;
c = K_SECOND(c);
}
if ((!chartab_initialized && ((c >= ' ' && c <= '~')))
|| (c < 256 && vim_isprintc_strict(c)))
{
// printable character
transchar_charbuf[i] = c;
transchar_charbuf[i + 1] = NUL;
}
else
transchar_nonprint(buf, transchar_charbuf + i, c);
return transchar_charbuf;
}
/*
* Like transchar(), but called with a byte instead of a character. Checks
* for an illegal UTF-8 byte. Uses 'fileformat' of the current buffer.
*/
char_u *
transchar_byte(int c)
{
return transchar_byte_buf(curbuf, c);
}
/*
* Like transchar_buf(), but called with a byte instead of a character. Checks
* for an illegal UTF-8 byte. Uses 'fileformat' of "buf", unless it is NULL.
*/
char_u *
transchar_byte_buf(buf_T *buf, int c)
{
if (enc_utf8 && c >= 0x80)
{
transchar_nonprint(buf, transchar_charbuf, c);
return transchar_charbuf;
}
return transchar_buf(buf, c);
}
/*
* Convert non-printable character to two or more printable characters in
* "charbuf[]". "charbuf" needs to be able to hold five bytes.
* Does NOT work for multi-byte characters, c must be <= 255.
*/
void
transchar_nonprint(buf_T *buf, char_u *charbuf, int c)
{
if (c == NL)
c = NUL; // we use newline in place of a NUL
else if (buf != NULL && c == CAR && get_fileformat(buf) == EOL_MAC)
c = NL; // we use CR in place of NL in this case
if (dy_flags & DY_UHEX) // 'display' has "uhex"
transchar_hex(charbuf, c);
else if (c <= 0x7f) // 0x00 - 0x1f and 0x7f
{
charbuf[0] = '^';
charbuf[1] = c ^ 0x40; // DEL displayed as ^?
charbuf[2] = NUL;
}
else if (enc_utf8)
{
transchar_hex(charbuf, c);
}
else if (c >= ' ' + 0x80 && c <= '~' + 0x80) // 0xa0 - 0xfe
{
charbuf[0] = '|';
charbuf[1] = c - 0x80;
charbuf[2] = NUL;
}
else // 0x80 - 0x9f and 0xff
{
charbuf[0] = '~';
charbuf[1] = (c - 0x80) ^ 0x40; // 0xff displayed as ~?
charbuf[2] = NUL;
}
}
void
transchar_hex(char_u *buf, int c)
{
int i = 0;
buf[0] = '<';
if (c > 255)
{
buf[++i] = nr2hex((unsigned)c >> 12);
buf[++i] = nr2hex((unsigned)c >> 8);
}
buf[++i] = nr2hex((unsigned)c >> 4);
buf[++i] = nr2hex((unsigned)c);
buf[++i] = '>';
buf[++i] = NUL;
}
/*
* Convert the lower 4 bits of byte "c" to its hex character.
* Lower case letters are used to avoid the confusion of <F1> being 0xf1 or
* function key 1.
*/
static unsigned
nr2hex(unsigned c)
{
if ((c & 0xf) <= 9)
return (c & 0xf) + '0';
return (c & 0xf) - 10 + 'a';
}
/*
* Return number of display cells occupied by byte "b".
* Caller must make sure 0 <= b <= 255.
* For multi-byte mode "b" must be the first byte of a character.
* A TAB is counted as two cells: "^I".
* For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of
* cells depends on further bytes.
*/
int
byte2cells(int b)
{
if (enc_utf8 && b >= 0x80)
return 0;
return (g_chartab[b] & CT_CELL_MASK);
}
/*
* Return number of display cells occupied by character "c".
* "c" can be a special key (negative number) in which case 3 or 4 is returned.
* A TAB is counted as two cells: "^I" or four: "<09>".
*/
int
char2cells(int c)
{
if (IS_SPECIAL(c))
return char2cells(K_SECOND(c)) + 2;
if (c >= 0x80)
{
// UTF-8: above 0x80 need to check the value
if (enc_utf8)
return utf_char2cells(c);
// DBCS: double-byte means double-width, except for euc-jp with first
// byte 0x8e
if (enc_dbcs != 0 && c >= 0x100)
{
if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
return 1;
return 2;
}
}
return (g_chartab[c & 0xff] & CT_CELL_MASK);
}
/*
* Return number of display cells occupied by character at "*p".
* A TAB is counted as two cells: "^I" or four: "<09>".
*/
int
ptr2cells(char_u *p)
{
if (!has_mbyte)
return byte2cells(*p);
// For UTF-8 we need to look at more bytes if the first byte is >= 0x80.
if (enc_utf8 && *p >= 0x80)
return utf_ptr2cells(p);
// For DBCS we can tell the cell count from the first byte.
return (g_chartab[*p] & CT_CELL_MASK);
}
/*
* Return the number of character cells string "s" will take on the screen,
* counting TABs as two characters: "^I".
*/
int
vim_strsize(char_u *s)
{
return vim_strnsize(s, (int)MAXCOL);
}
/*
* Return the number of character cells string "s[len]" will take on the
* screen, counting TABs as two characters: "^I".
*/
int
vim_strnsize(char_u *s, int len)
{
int size = 0;
while (*s != NUL && --len >= 0)
{
int l = (*mb_ptr2len)(s);
size += ptr2cells(s);
s += l;
len -= l - 1;
}
return size;
}
/*
* Return the number of characters 'c' will take on the screen, taking
* into account the size of a tab.
* Use a define to make it fast, this is used very often!!!
* Also see getvcol() below.
*/
#ifdef FEAT_VARTABS
# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
if (*(p) == TAB && (!(wp)->w_p_list || (wp)->w_lcs_chars.tab1)) \
{ \
return tabstop_padding(col, (buf)->b_p_ts, (buf)->b_p_vts_array); \
} \
else \
return ptr2cells(p);
#else
# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
if (*(p) == TAB && (!(wp)->w_p_list || wp->w_lcs_chars.tab1)) \
{ \
int ts; \
ts = (buf)->b_p_ts; \
return (int)(ts - (col % ts)); \
} \
else \
return ptr2cells(p);
#endif
int
chartabsize(char_u *p, colnr_T col)
{
RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col)
}
#ifdef FEAT_LINEBREAK
static int
win_chartabsize(win_T *wp, char_u *p, colnr_T col)
{
RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col)
}
#endif
/*
* Return the number of characters the string "s" will take on the screen,
* taking into account the size of a tab.
* Does not handle text properties, since "s" is not a buffer line.
*/
int
linetabsize_str(char_u *s)
{
return linetabsize_col(0, s);
}
/*
* Like linetabsize_str(), but "s" starts at column "startcol".
*/
int
linetabsize_col(int startcol, char_u *s)
{
chartabsize_T cts;
init_chartabsize_arg(&cts, curwin, 0, startcol, s, s);
while (*cts.cts_ptr != NUL)
cts.cts_vcol += lbr_chartabsize_adv(&cts);
clear_chartabsize_arg(&cts);
return (int)cts.cts_vcol;
}
/*
* Like linetabsize_str(), but for a given window instead of the current one.
*/
int
win_linetabsize(win_T *wp, linenr_T lnum, char_u *line, colnr_T len)
{
chartabsize_T cts;
init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
win_linetabsize_cts(&cts, len);
clear_chartabsize_arg(&cts);
return (int)cts.cts_vcol;
}
/*
* Return the number of cells line "lnum" of window "wp" will take on the
* screen, taking into account the size of a tab and text properties.
*/
int
linetabsize(win_T *wp, linenr_T lnum)
{
return win_linetabsize(wp, lnum,
ml_get_buf(wp->w_buffer, lnum, FALSE), (colnr_T)MAXCOL);
}
void
win_linetabsize_cts(chartabsize_T *cts, colnr_T len)
{
#ifdef FEAT_PROP_POPUP
cts->cts_with_trailing = len == MAXCOL;
#endif
for ( ; *cts->cts_ptr != NUL && (len == MAXCOL || cts->cts_ptr < cts->cts_line + len);
MB_PTR_ADV(cts->cts_ptr))
cts->cts_vcol += win_lbr_chartabsize(cts, NULL);
#ifdef FEAT_PROP_POPUP
// check for a virtual text at the end of a line or on an empty line
if (len == MAXCOL && cts->cts_has_prop_with_text && *cts->cts_ptr == NUL)
{
(void)win_lbr_chartabsize(cts, NULL);
cts->cts_vcol += cts->cts_cur_text_width;
// when properties are above or below the empty line must also be
// counted
if (cts->cts_ptr == cts->cts_line && cts->cts_prop_lines > 0)
++cts->cts_vcol;
}
#endif
}
/*
* Return TRUE if 'c' is a normal identifier character:
* Letters and characters from the 'isident' option.
*/
int
vim_isIDc(int c)
{
return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR));
}
/*
* Like vim_isIDc() but not using the 'isident' option: letters, numbers and
* underscore.
*/
int
vim_isNormalIDc(int c)
{
return ASCII_ISALNUM(c) || c == '_';
}
/*
* return TRUE if 'c' is a keyword character: Letters and characters from
* 'iskeyword' option for the current buffer.
* For multi-byte characters mb_get_class() is used (builtin rules).
*/
int
vim_iswordc(int c)
{
return vim_iswordc_buf(c, curbuf);
}
int
vim_iswordc_buf(int c, buf_T *buf)
{
if (c >= 0x100)
{
if (enc_dbcs != 0)
return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2;
if (enc_utf8)
return utf_class_buf(c, buf) >= 2;
return FALSE;
}
return (c > 0 && GET_CHARTAB(buf, c) != 0);
}
/*
* Just like vim_iswordc() but uses a pointer to the (multi-byte) character.
*/
int
vim_iswordp(char_u *p)
{
return vim_iswordp_buf(p, curbuf);
}
int
vim_iswordp_buf(char_u *p, buf_T *buf)
{
int c = *p;
if (has_mbyte && MB_BYTE2LEN(c) > 1)
c = (*mb_ptr2char)(p);
return vim_iswordc_buf(c, buf);
}
/*
* Return TRUE if 'c' is a valid file-name character as specified with the
* 'isfname' option.
* Assume characters above 0x100 are valid (multi-byte).
* To be used for commands like "gf".
*/
int
vim_isfilec(int c)
{
return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR)));
}
#if defined(FEAT_SPELL) || defined(PROTO)
/*
* Return TRUE if 'c' is a valid file-name character, including characters left
* out of 'isfname' to make "gf" work, such as comma, space, '@', etc.
*/
int
vim_is_fname_char(int c)
{
return vim_isfilec(c) || c == ',' || c == ' ' || c == '@';
}
#endif
/*
* return TRUE if 'c' is a valid file-name character or a wildcard character
* Assume characters above 0x100 are valid (multi-byte).
* Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]")
* returns false.
*/
int
vim_isfilec_or_wc(int c)
{
char_u buf[2];
buf[0] = (char_u)c;
buf[1] = NUL;
return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf);
}
/*
* Return TRUE if 'c' is a printable character.
* Assume characters above 0x100 are printable (multi-byte), except for
* Unicode.
*/
int
vim_isprintc(int c)
{
if (enc_utf8 && c >= 0x100)
return utf_printable(c);
return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
}
/*
* Strict version of vim_isprintc(c), don't return TRUE if "c" is the head
* byte of a double-byte character.
*/
int
vim_isprintc_strict(int c)
{
if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1)
return FALSE;
if (enc_utf8 && c >= 0x100)
return utf_printable(c);
return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
}
/*
* Prepare the structure passed to chartabsize functions.
* "line" is the start of the line, "ptr" is the first relevant character.
* When "lnum" is zero do not use text properties that insert text.
*/
void
init_chartabsize_arg(
chartabsize_T *cts,
win_T *wp,
linenr_T lnum UNUSED,
colnr_T col,
char_u *line,
char_u *ptr)
{
CLEAR_POINTER(cts);
cts->cts_win = wp;
cts->cts_vcol = col;
cts->cts_line = line;
cts->cts_ptr = ptr;
#ifdef FEAT_PROP_POPUP
if (lnum > 0 && !ignore_text_props)
{
char_u *prop_start;
int count;
count = get_text_props(wp->w_buffer, lnum, &prop_start, FALSE);
cts->cts_text_prop_count = count;
if (count > 0)
{
// Make a copy of the properties, so that they are properly
// aligned. Make it twice as long for the sorting below.
cts->cts_text_props = ALLOC_MULT(textprop_T, count * 2);
if (cts->cts_text_props == NULL)
cts->cts_text_prop_count = 0;
else
{
int i;
mch_memmove(cts->cts_text_props + count, prop_start,
count * sizeof(textprop_T));
for (i = 0; i < count; ++i)
{
textprop_T *tp = cts->cts_text_props + i + count;
if (tp->tp_id < 0
&& text_prop_type_valid(wp->w_buffer, tp))
{