Mercurial > vim
annotate src/charset.c @ 17789:0f7ae8010787 v8.1.1891
patch 8.1.1891: functions used in one file are global
commit https://github.com/vim/vim/commit/5843f5f37b0632e2d706abc9014bfd7d98f7b02e
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Aug 20 20:13:45 2019 +0200
patch 8.1.1891: functions used in one file are global
Problem: Functions used in one file are global.
Solution: Add "static". (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/4840)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Tue, 20 Aug 2019 20:15:07 +0200 |
parents | 91619e48e1a7 |
children | 59f8948b7590 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9399
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 #include "vim.h" | |
11 | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
12 #if defined(HAVE_WCHAR_H) |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
13 # include <wchar.h> /* for towupper() and towlower() */ |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
14 #endif |
7799
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7697
diff
changeset
|
15 static int win_nolbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp); |
7 | 16 |
7799
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7697
diff
changeset
|
17 static unsigned nr2hex(unsigned c); |
7 | 18 |
19 static int chartab_initialized = FALSE; | |
20 | |
21 /* b_chartab[] is an array of 32 bytes, each bit representing one of the | |
22 * characters 0-255. */ | |
23 #define SET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] |= (1 << ((c) & 0x7)) | |
24 #define RESET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] &= ~(1 << ((c) & 0x7)) | |
25 #define GET_CHARTAB(buf, c) ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7))) | |
26 | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
27 /* table used below, see init_chartab() for an explanation */ |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
28 static char_u g_chartab[256]; |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
29 |
7 | 30 /* |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
31 * Flags for g_chartab[]. |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
32 */ |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
33 #define CT_CELL_MASK 0x07 /* mask: nr of display cells (1, 2 or 4) */ |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
34 #define CT_PRINT_CHAR 0x10 /* flag: set for printable chars */ |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
35 #define CT_ID_CHAR 0x20 /* flag: set for ID chars */ |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
36 #define CT_FNAME_CHAR 0x40 /* flag: set for file name chars */ |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
37 |
17789
0f7ae8010787
patch 8.1.1891: functions used in one file are global
Bram Moolenaar <Bram@vim.org>
parents:
16819
diff
changeset
|
38 static int in_win_border(win_T *wp, colnr_T vcol); |
0f7ae8010787
patch 8.1.1891: functions used in one file are global
Bram Moolenaar <Bram@vim.org>
parents:
16819
diff
changeset
|
39 |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
40 /* |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
41 * Fill g_chartab[]. Also fills curbuf->b_chartab[] with flags for keyword |
7 | 42 * characters for current buffer. |
43 * | |
44 * Depends on the option settings 'iskeyword', 'isident', 'isfname', | |
45 * 'isprint' and 'encoding'. | |
46 * | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
47 * The index in g_chartab[] depends on 'encoding': |
7 | 48 * - For non-multi-byte index with the byte (same as the character). |
49 * - For DBCS index with the first byte. | |
50 * - For UTF-8 index with the character (when first byte is up to 0x80 it is | |
51 * the same as the character, if the first byte is 0x80 and above it depends | |
52 * on further bytes). | |
53 * | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
54 * The contents of g_chartab[]: |
7 | 55 * - The lower two bits, masked by CT_CELL_MASK, give the number of display |
56 * cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80. | |
57 * - CT_PRINT_CHAR bit is set when the character is printable (no need to | |
58 * translate the character before displaying it). Note that only DBCS | |
59 * characters can have 2 display cells and still be printable. | |
60 * - CT_FNAME_CHAR bit is set when the character can be in a file name. | |
61 * - CT_ID_CHAR bit is set when the character can be in an identifier. | |
62 * | |
63 * Return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has an | |
64 * error, OK otherwise. | |
65 */ | |
66 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
67 init_chartab(void) |
7 | 68 { |
69 return buf_init_chartab(curbuf, TRUE); | |
70 } | |
71 | |
72 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
73 buf_init_chartab( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
74 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
75 int global) /* FALSE: only set buf->b_chartab[] */ |
7 | 76 { |
77 int c; | |
78 int c2; | |
79 char_u *p; | |
80 int i; | |
81 int tilde; | |
82 int do_isalpha; | |
83 | |
84 if (global) | |
85 { | |
86 /* | |
87 * Set the default size for printable characters: | |
88 * From <Space> to '~' is 1 (printable), others are 2 (not printable). | |
89 * This also inits all 'isident' and 'isfname' flags to FALSE. | |
90 * | |
91 * EBCDIC: all chars below ' ' are not printable, all others are | |
92 * printable. | |
93 */ | |
94 c = 0; | |
95 while (c < ' ') | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
96 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; |
7 | 97 #ifdef EBCDIC |
98 while (c < 255) | |
99 #else | |
100 while (c <= '~') | |
101 #endif | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
102 g_chartab[c++] = 1 + CT_PRINT_CHAR; |
7 | 103 while (c < 256) |
104 { | |
105 /* UTF-8: bytes 0xa0 - 0xff are printable (latin1) */ | |
106 if (enc_utf8 && c >= 0xa0) | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
107 g_chartab[c++] = CT_PRINT_CHAR + 1; |
7 | 108 /* euc-jp characters starting with 0x8e are single width */ |
109 else if (enc_dbcs == DBCS_JPNU && c == 0x8e) | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
110 g_chartab[c++] = CT_PRINT_CHAR + 1; |
7 | 111 /* other double-byte chars can be printable AND double-width */ |
112 else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2) | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
113 g_chartab[c++] = CT_PRINT_CHAR + 2; |
7 | 114 else |
115 /* the rest is unprintable by default */ | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
116 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; |
7 | 117 } |
118 | |
119 /* Assume that every multi-byte char is a filename character. */ | |
120 for (c = 1; c < 256; ++c) | |
121 if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1) | |
122 || (enc_dbcs == DBCS_JPNU && c == 0x8e) | |
123 || (enc_utf8 && c >= 0xa0)) | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
124 g_chartab[c] |= CT_FNAME_CHAR; |
7 | 125 } |
126 | |
127 /* | |
128 * Init word char flags all to FALSE | |
129 */ | |
130 vim_memset(buf->b_chartab, 0, (size_t)32); | |
227 | 131 if (enc_dbcs != 0) |
132 for (c = 0; c < 256; ++c) | |
133 { | |
134 /* double-byte characters are probably word characters */ | |
135 if (MB_BYTE2LEN(c) == 2) | |
136 SET_CHARTAB(buf, c); | |
137 } | |
7 | 138 |
139 #ifdef FEAT_LISP | |
140 /* | |
141 * In lisp mode the '-' character is included in keywords. | |
142 */ | |
143 if (buf->b_p_lisp) | |
144 SET_CHARTAB(buf, '-'); | |
145 #endif | |
146 | |
147 /* Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint' | |
148 * options Each option is a list of characters, character numbers or | |
149 * ranges, separated by commas, e.g.: "200-210,x,#-178,-" | |
150 */ | |
151 for (i = global ? 0 : 3; i <= 3; ++i) | |
152 { | |
153 if (i == 0) | |
154 p = p_isi; /* first round: 'isident' */ | |
155 else if (i == 1) | |
156 p = p_isp; /* second round: 'isprint' */ | |
157 else if (i == 2) | |
158 p = p_isf; /* third round: 'isfname' */ | |
159 else /* i == 3 */ | |
160 p = buf->b_p_isk; /* fourth round: 'iskeyword' */ | |
161 | |
162 while (*p) | |
163 { | |
164 tilde = FALSE; | |
165 do_isalpha = FALSE; | |
166 if (*p == '^' && p[1] != NUL) | |
167 { | |
168 tilde = TRUE; | |
169 ++p; | |
170 } | |
171 if (VIM_ISDIGIT(*p)) | |
172 c = getdigits(&p); | |
173 else | |
1955 | 174 if (has_mbyte) |
175 c = mb_ptr2char_adv(&p); | |
176 else | |
7 | 177 c = *p++; |
178 c2 = -1; | |
179 if (*p == '-' && p[1] != NUL) | |
180 { | |
181 ++p; | |
182 if (VIM_ISDIGIT(*p)) | |
183 c2 = getdigits(&p); | |
184 else | |
1979 | 185 if (has_mbyte) |
186 c2 = mb_ptr2char_adv(&p); | |
187 else | |
7 | 188 c2 = *p++; |
189 } | |
1979 | 190 if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256 |
7 | 191 || !(*p == NUL || *p == ',')) |
192 return FAIL; | |
193 | |
194 if (c2 == -1) /* not a range */ | |
195 { | |
196 /* | |
197 * A single '@' (not "@-@"): | |
198 * Decide on letters being ID/printable/keyword chars with | |
199 * standard function isalpha(). This takes care of locale for | |
200 * single-byte characters). | |
201 */ | |
202 if (c == '@') | |
203 { | |
204 do_isalpha = TRUE; | |
205 c = 1; | |
206 c2 = 255; | |
207 } | |
208 else | |
209 c2 = c; | |
210 } | |
211 while (c <= c2) | |
212 { | |
1365 | 213 /* Use the MB_ functions here, because isalpha() doesn't |
214 * work properly when 'encoding' is "latin1" and the locale is | |
215 * "C". */ | |
15850
a6ca8cf07a98
patch 8.1.0932: Farsi support is outdated and unused
Bram Moolenaar <Bram@vim.org>
parents:
15636
diff
changeset
|
216 if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c)) |
7 | 217 { |
218 if (i == 0) /* (re)set ID flag */ | |
219 { | |
220 if (tilde) | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
221 g_chartab[c] &= ~CT_ID_CHAR; |
7 | 222 else |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
223 g_chartab[c] |= CT_ID_CHAR; |
7 | 224 } |
225 else if (i == 1) /* (re)set printable */ | |
226 { | |
227 if ((c < ' ' | |
228 #ifndef EBCDIC | |
229 || c > '~' | |
230 #endif | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
231 // For double-byte we keep the cell width, so |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
232 // that we can detect it from the first byte. |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
233 ) && !(enc_dbcs && MB_BYTE2LEN(c) == 2)) |
7 | 234 { |
235 if (tilde) | |
236 { | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
237 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) |
7 | 238 + ((dy_flags & DY_UHEX) ? 4 : 2); |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
239 g_chartab[c] &= ~CT_PRINT_CHAR; |
7 | 240 } |
241 else | |
242 { | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
243 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) + 1; |
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
244 g_chartab[c] |= CT_PRINT_CHAR; |
7 | 245 } |
246 } | |
247 } | |
248 else if (i == 2) /* (re)set fname flag */ | |
249 { | |
250 if (tilde) | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
251 g_chartab[c] &= ~CT_FNAME_CHAR; |
7 | 252 else |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
253 g_chartab[c] |= CT_FNAME_CHAR; |
7 | 254 } |
255 else /* i == 3 */ /* (re)set keyword flag */ | |
256 { | |
257 if (tilde) | |
258 RESET_CHARTAB(buf, c); | |
259 else | |
260 SET_CHARTAB(buf, c); | |
261 } | |
262 } | |
263 ++c; | |
264 } | |
4096 | 265 |
266 c = *p; | |
7 | 267 p = skip_to_option_part(p); |
4096 | 268 if (c == ',' && *p == NUL) |
269 /* Trailing comma is not allowed. */ | |
270 return FAIL; | |
7 | 271 } |
272 } | |
273 chartab_initialized = TRUE; | |
274 return OK; | |
275 } | |
276 | |
277 /* | |
278 * Translate any special characters in buf[bufsize] in-place. | |
279 * The result is a string with only printable characters, but if there is not | |
280 * enough room, not all characters will be translated. | |
281 */ | |
282 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
283 trans_characters( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
284 char_u *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
285 int bufsize) |
7 | 286 { |
287 int len; /* length of string needing translation */ | |
288 int room; /* room in buffer after string */ | |
289 char_u *trs; /* translated character */ | |
290 int trs_len; /* length of trs[] */ | |
291 | |
292 len = (int)STRLEN(buf); | |
293 room = bufsize - len; | |
294 while (*buf != 0) | |
295 { | |
296 /* Assume a multi-byte character doesn't need translation. */ | |
474 | 297 if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1) |
7 | 298 len -= trs_len; |
299 else | |
300 { | |
301 trs = transchar_byte(*buf); | |
302 trs_len = (int)STRLEN(trs); | |
303 if (trs_len > 1) | |
304 { | |
305 room -= trs_len - 1; | |
306 if (room <= 0) | |
307 return; | |
308 mch_memmove(buf + trs_len, buf + 1, (size_t)len); | |
309 } | |
310 mch_memmove(buf, trs, (size_t)trs_len); | |
311 --len; | |
312 } | |
313 buf += trs_len; | |
314 } | |
315 } | |
316 | |
1097 | 317 #if defined(FEAT_EVAL) || defined(FEAT_TITLE) || defined(FEAT_INS_EXPAND) \ |
318 || defined(PROTO) | |
7 | 319 /* |
320 * Translate a string into allocated memory, replacing special chars with | |
321 * printable chars. Returns NULL when out of memory. | |
322 */ | |
323 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
324 transstr(char_u *s) |
7 | 325 { |
326 char_u *res; | |
327 char_u *p; | |
328 int l, len, c; | |
329 char_u hexbuf[11]; | |
330 | |
331 if (has_mbyte) | |
332 { | |
333 /* Compute the length of the result, taking account of unprintable | |
334 * multi-byte characters. */ | |
335 len = 0; | |
336 p = s; | |
337 while (*p != NUL) | |
338 { | |
474 | 339 if ((l = (*mb_ptr2len)(p)) > 1) |
7 | 340 { |
341 c = (*mb_ptr2char)(p); | |
342 p += l; | |
343 if (vim_isprintc(c)) | |
344 len += l; | |
345 else | |
346 { | |
347 transchar_hex(hexbuf, c); | |
835 | 348 len += (int)STRLEN(hexbuf); |
7 | 349 } |
350 } | |
351 else | |
352 { | |
353 l = byte2cells(*p++); | |
354 if (l > 0) | |
355 len += l; | |
356 else | |
357 len += 4; /* illegal byte sequence */ | |
358 } | |
359 } | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16706
diff
changeset
|
360 res = alloc(len + 1); |
7 | 361 } |
362 else | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16706
diff
changeset
|
363 res = alloc(vim_strsize(s) + 1); |
7 | 364 if (res != NULL) |
365 { | |
366 *res = NUL; | |
367 p = s; | |
368 while (*p != NUL) | |
369 { | |
474 | 370 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
7 | 371 { |
372 c = (*mb_ptr2char)(p); | |
373 if (vim_isprintc(c)) | |
374 STRNCAT(res, p, l); /* append printable multi-byte char */ | |
375 else | |
376 transchar_hex(res + STRLEN(res), c); | |
377 p += l; | |
378 } | |
379 else | |
380 STRCAT(res, transchar_byte(*p++)); | |
381 } | |
382 } | |
383 return res; | |
384 } | |
385 #endif | |
386 | |
387 #if defined(FEAT_SYN_HL) || defined(FEAT_INS_EXPAND) || defined(PROTO) | |
388 /* | |
221 | 389 * Convert the string "str[orglen]" to do ignore-case comparing. Uses the |
390 * current locale. | |
130 | 391 * When "buf" is NULL returns an allocated string (NULL for out-of-memory). |
392 * Otherwise puts the result in "buf[buflen]". | |
7 | 393 */ |
394 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
395 str_foldcase( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
396 char_u *str, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
397 int orglen, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
398 char_u *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
399 int buflen) |
7 | 400 { |
401 garray_T ga; | |
402 int i; | |
130 | 403 int len = orglen; |
7 | 404 |
405 #define GA_CHAR(i) ((char_u *)ga.ga_data)[i] | |
406 #define GA_PTR(i) ((char_u *)ga.ga_data + i) | |
130 | 407 #define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i]) |
408 #define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + i) | |
7 | 409 |
130 | 410 /* Copy "str" into "buf" or allocated memory, unmodified. */ |
411 if (buf == NULL) | |
412 { | |
413 ga_init2(&ga, 1, 10); | |
414 if (ga_grow(&ga, len + 1) == FAIL) | |
415 return NULL; | |
416 mch_memmove(ga.ga_data, str, (size_t)len); | |
417 ga.ga_len = len; | |
418 } | |
419 else | |
420 { | |
421 if (len >= buflen) /* Ugly! */ | |
422 len = buflen - 1; | |
423 mch_memmove(buf, str, (size_t)len); | |
424 } | |
425 if (buf == NULL) | |
426 GA_CHAR(len) = NUL; | |
427 else | |
428 buf[len] = NUL; | |
7 | 429 |
430 /* Make each character lower case. */ | |
431 i = 0; | |
130 | 432 while (STR_CHAR(i) != NUL) |
7 | 433 { |
130 | 434 if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1)) |
7 | 435 { |
436 if (enc_utf8) | |
437 { | |
1654 | 438 int c = utf_ptr2char(STR_PTR(i)); |
3263 | 439 int olen = utf_ptr2len(STR_PTR(i)); |
1654 | 440 int lc = utf_tolower(c); |
7 | 441 |
1654 | 442 /* Only replace the character when it is not an invalid |
443 * sequence (ASCII character or more than one byte) and | |
444 * utf_tolower() doesn't return the original character. */ | |
3263 | 445 if ((c < 0x80 || olen > 1) && c != lc) |
7 | 446 { |
3263 | 447 int nlen = utf_char2len(lc); |
7 | 448 |
449 /* If the byte length changes need to shift the following | |
450 * characters forward or backward. */ | |
3263 | 451 if (olen != nlen) |
7 | 452 { |
3263 | 453 if (nlen > olen) |
130 | 454 { |
3263 | 455 if (buf == NULL |
456 ? ga_grow(&ga, nlen - olen + 1) == FAIL | |
457 : len + nlen - olen >= buflen) | |
7 | 458 { |
459 /* out of memory, keep old char */ | |
460 lc = c; | |
3263 | 461 nlen = olen; |
7 | 462 } |
130 | 463 } |
3263 | 464 if (olen != nlen) |
7 | 465 { |
130 | 466 if (buf == NULL) |
467 { | |
3263 | 468 STRMOVE(GA_PTR(i) + nlen, GA_PTR(i) + olen); |
469 ga.ga_len += nlen - olen; | |
130 | 470 } |
471 else | |
472 { | |
3263 | 473 STRMOVE(buf + i + nlen, buf + i + olen); |
474 len += nlen - olen; | |
130 | 475 } |
7 | 476 } |
477 } | |
130 | 478 (void)utf_char2bytes(lc, STR_PTR(i)); |
7 | 479 } |
480 } | |
481 /* skip to next multi-byte char */ | |
474 | 482 i += (*mb_ptr2len)(STR_PTR(i)); |
7 | 483 } |
484 else | |
485 { | |
130 | 486 if (buf == NULL) |
487 GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i)); | |
488 else | |
489 buf[i] = TOLOWER_LOC(buf[i]); | |
7 | 490 ++i; |
491 } | |
492 } | |
493 | |
130 | 494 if (buf == NULL) |
495 return (char_u *)ga.ga_data; | |
496 return buf; | |
7 | 497 } |
498 #endif | |
499 | |
500 /* | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
501 * Catch 22: g_chartab[] can't be initialized before the options are |
7 | 502 * initialized, and initializing options may cause transchar() to be called! |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
503 * When chartab_initialized == FALSE don't use g_chartab[]. |
7 | 504 * Does NOT work for multi-byte characters, c must be <= 255. |
505 * Also doesn't work for the first byte of a multi-byte, "c" must be a | |
506 * character! | |
507 */ | |
508 static char_u transchar_buf[7]; | |
509 | |
510 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
511 transchar(int c) |
7 | 512 { |
513 int i; | |
514 | |
515 i = 0; | |
516 if (IS_SPECIAL(c)) /* special key code, display as ~@ char */ | |
517 { | |
518 transchar_buf[0] = '~'; | |
519 transchar_buf[1] = '@'; | |
520 i = 2; | |
521 c = K_SECOND(c); | |
522 } | |
523 | |
524 if ((!chartab_initialized && ( | |
525 #ifdef EBCDIC | |
526 (c >= 64 && c < 255) | |
527 #else | |
528 (c >= ' ' && c <= '~') | |
529 #endif | |
530 )) || (c < 256 && vim_isprintc_strict(c))) | |
531 { | |
532 /* printable character */ | |
533 transchar_buf[i] = c; | |
534 transchar_buf[i + 1] = NUL; | |
535 } | |
536 else | |
537 transchar_nonprint(transchar_buf + i, c); | |
538 return transchar_buf; | |
539 } | |
540 | |
541 /* | |
542 * Like transchar(), but called with a byte instead of a character. Checks | |
543 * for an illegal UTF-8 byte. | |
544 */ | |
545 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
546 transchar_byte(int c) |
7 | 547 { |
548 if (enc_utf8 && c >= 0x80) | |
549 { | |
550 transchar_nonprint(transchar_buf, c); | |
551 return transchar_buf; | |
552 } | |
553 return transchar(c); | |
554 } | |
555 | |
556 /* | |
557 * Convert non-printable character to two or more printable characters in | |
558 * "buf[]". "buf" needs to be able to hold five bytes. | |
559 * Does NOT work for multi-byte characters, c must be <= 255. | |
560 */ | |
561 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
562 transchar_nonprint(char_u *buf, int c) |
7 | 563 { |
564 if (c == NL) | |
565 c = NUL; /* we use newline in place of a NUL */ | |
566 else if (c == CAR && get_fileformat(curbuf) == EOL_MAC) | |
567 c = NL; /* we use CR in place of NL in this case */ | |
568 | |
569 if (dy_flags & DY_UHEX) /* 'display' has "uhex" */ | |
570 transchar_hex(buf, c); | |
571 | |
572 #ifdef EBCDIC | |
573 /* For EBCDIC only the characters 0-63 and 255 are not printable */ | |
574 else if (CtrlChar(c) != 0 || c == DEL) | |
575 #else | |
576 else if (c <= 0x7f) /* 0x00 - 0x1f and 0x7f */ | |
577 #endif | |
578 { | |
579 buf[0] = '^'; | |
580 #ifdef EBCDIC | |
581 if (c == DEL) | |
582 buf[1] = '?'; /* DEL displayed as ^? */ | |
583 else | |
584 buf[1] = CtrlChar(c); | |
585 #else | |
586 buf[1] = c ^ 0x40; /* DEL displayed as ^? */ | |
587 #endif | |
588 | |
589 buf[2] = NUL; | |
590 } | |
591 else if (enc_utf8 && c >= 0x80) | |
592 { | |
593 transchar_hex(buf, c); | |
594 } | |
595 #ifndef EBCDIC | |
596 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) /* 0xa0 - 0xfe */ | |
597 { | |
598 buf[0] = '|'; | |
599 buf[1] = c - 0x80; | |
600 buf[2] = NUL; | |
601 } | |
602 #else | |
603 else if (c < 64) | |
604 { | |
605 buf[0] = '~'; | |
606 buf[1] = MetaChar(c); | |
607 buf[2] = NUL; | |
608 } | |
609 #endif | |
610 else /* 0x80 - 0x9f and 0xff */ | |
611 { | |
612 /* | |
613 * TODO: EBCDIC I don't know what to do with this chars, so I display | |
614 * them as '~?' for now | |
615 */ | |
616 buf[0] = '~'; | |
617 #ifdef EBCDIC | |
618 buf[1] = '?'; /* 0xff displayed as ~? */ | |
619 #else | |
620 buf[1] = (c - 0x80) ^ 0x40; /* 0xff displayed as ~? */ | |
621 #endif | |
622 buf[2] = NUL; | |
623 } | |
624 } | |
625 | |
626 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
627 transchar_hex(char_u *buf, int c) |
7 | 628 { |
629 int i = 0; | |
630 | |
631 buf[0] = '<'; | |
632 if (c > 255) | |
633 { | |
634 buf[++i] = nr2hex((unsigned)c >> 12); | |
635 buf[++i] = nr2hex((unsigned)c >> 8); | |
636 } | |
637 buf[++i] = nr2hex((unsigned)c >> 4); | |
1869 | 638 buf[++i] = nr2hex((unsigned)c); |
7 | 639 buf[++i] = '>'; |
640 buf[++i] = NUL; | |
641 } | |
642 | |
643 /* | |
644 * Convert the lower 4 bits of byte "c" to its hex character. | |
645 * Lower case letters are used to avoid the confusion of <F1> being 0xf1 or | |
646 * function key 1. | |
647 */ | |
1869 | 648 static unsigned |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
649 nr2hex(unsigned c) |
7 | 650 { |
651 if ((c & 0xf) <= 9) | |
652 return (c & 0xf) + '0'; | |
653 return (c & 0xf) - 10 + 'a'; | |
654 } | |
655 | |
656 /* | |
657 * Return number of display cells occupied by byte "b". | |
658 * Caller must make sure 0 <= b <= 255. | |
659 * For multi-byte mode "b" must be the first byte of a character. | |
660 * A TAB is counted as two cells: "^I". | |
661 * For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of | |
662 * cells depends on further bytes. | |
663 */ | |
664 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
665 byte2cells(int b) |
7 | 666 { |
667 if (enc_utf8 && b >= 0x80) | |
668 return 0; | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
669 return (g_chartab[b] & CT_CELL_MASK); |
7 | 670 } |
671 | |
672 /* | |
673 * Return number of display cells occupied by character "c". | |
674 * "c" can be a special key (negative number) in which case 3 or 4 is returned. | |
675 * A TAB is counted as two cells: "^I" or four: "<09>". | |
676 */ | |
677 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
678 char2cells(int c) |
7 | 679 { |
680 if (IS_SPECIAL(c)) | |
681 return char2cells(K_SECOND(c)) + 2; | |
682 if (c >= 0x80) | |
683 { | |
684 /* UTF-8: above 0x80 need to check the value */ | |
685 if (enc_utf8) | |
686 return utf_char2cells(c); | |
687 /* DBCS: double-byte means double-width, except for euc-jp with first | |
688 * byte 0x8e */ | |
689 if (enc_dbcs != 0 && c >= 0x100) | |
690 { | |
691 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e) | |
692 return 1; | |
693 return 2; | |
694 } | |
695 } | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
696 return (g_chartab[c & 0xff] & CT_CELL_MASK); |
7 | 697 } |
698 | |
699 /* | |
700 * Return number of display cells occupied by character at "*p". | |
701 * A TAB is counted as two cells: "^I" or four: "<09>". | |
702 */ | |
703 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
704 ptr2cells(char_u *p) |
7 | 705 { |
706 /* For UTF-8 we need to look at more bytes if the first byte is >= 0x80. */ | |
707 if (enc_utf8 && *p >= 0x80) | |
708 return utf_ptr2cells(p); | |
709 /* For DBCS we can tell the cell count from the first byte. */ | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
710 return (g_chartab[*p] & CT_CELL_MASK); |
7 | 711 } |
712 | |
713 /* | |
3292 | 714 * Return the number of character cells string "s" will take on the screen, |
7 | 715 * counting TABs as two characters: "^I". |
716 */ | |
717 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
718 vim_strsize(char_u *s) |
7 | 719 { |
720 return vim_strnsize(s, (int)MAXCOL); | |
721 } | |
722 | |
723 /* | |
3292 | 724 * Return the number of character cells string "s[len]" will take on the |
725 * screen, counting TABs as two characters: "^I". | |
7 | 726 */ |
727 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
728 vim_strnsize(char_u *s, int len) |
7 | 729 { |
730 int size = 0; | |
731 | |
732 while (*s != NUL && --len >= 0) | |
733 if (has_mbyte) | |
734 { | |
474 | 735 int l = (*mb_ptr2len)(s); |
7 | 736 |
737 size += ptr2cells(s); | |
738 s += l; | |
739 len -= l - 1; | |
740 } | |
741 else | |
742 size += byte2cells(*s++); | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
743 |
7 | 744 return size; |
745 } | |
746 | |
747 /* | |
748 * Return the number of characters 'c' will take on the screen, taking | |
749 * into account the size of a tab. | |
750 * Use a define to make it fast, this is used very often!!! | |
751 * Also see getvcol() below. | |
752 */ | |
753 | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
754 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
755 # define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
756 if (*(p) == TAB && (!(wp)->w_p_list || lcs_tab1)) \ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
757 { \ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
758 return tabstop_padding(col, (buf)->b_p_ts, (buf)->b_p_vts_array); \ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
759 } \ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
760 else \ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
761 return ptr2cells(p); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
762 #else |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
763 # define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \ |
7 | 764 if (*(p) == TAB && (!(wp)->w_p_list || lcs_tab1)) \ |
765 { \ | |
766 int ts; \ | |
767 ts = (buf)->b_p_ts; \ | |
768 return (int)(ts - (col % ts)); \ | |
769 } \ | |
770 else \ | |
771 return ptr2cells(p); | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
772 #endif |
7 | 773 |
774 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
775 chartabsize(char_u *p, colnr_T col) |
7 | 776 { |
777 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col) | |
778 } | |
779 | |
780 #ifdef FEAT_LINEBREAK | |
781 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
782 win_chartabsize(win_T *wp, char_u *p, colnr_T col) |
7 | 783 { |
784 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col) | |
785 } | |
786 #endif | |
787 | |
788 /* | |
2339
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
789 * Return the number of characters the string 's' will take on the screen, |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
790 * taking into account the size of a tab. |
7 | 791 */ |
792 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
793 linetabsize(char_u *s) |
7 | 794 { |
2339
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
795 return linetabsize_col(0, s); |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
796 } |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
797 |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
798 /* |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
799 * Like linetabsize(), but starting at column "startcol". |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
800 */ |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
801 int |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
802 linetabsize_col(int startcol, char_u *s) |
2339
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
803 { |
01e4b4d37842
Added strdisplaywidth() function.
Bram Moolenaar <bram@vim.org>
parents:
2108
diff
changeset
|
804 colnr_T col = startcol; |
5995 | 805 char_u *line = s; /* pointer to start of line, for breakindent */ |
7 | 806 |
807 while (*s != NUL) | |
5995 | 808 col += lbr_chartabsize_adv(line, &s, col); |
7 | 809 return (int)col; |
810 } | |
811 | |
812 /* | |
813 * Like linetabsize(), but for a given window instead of the current one. | |
814 */ | |
815 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
816 win_linetabsize(win_T *wp, char_u *line, colnr_T len) |
7 | 817 { |
818 colnr_T col = 0; | |
819 char_u *s; | |
820 | |
5995 | 821 for (s = line; *s != NUL && (len == MAXCOL || s < line + len); |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
822 MB_PTR_ADV(s)) |
5995 | 823 col += win_lbr_chartabsize(wp, line, s, col, NULL); |
7 | 824 return (int)col; |
825 } | |
826 | |
827 /* | |
42 | 828 * Return TRUE if 'c' is a normal identifier character: |
829 * Letters and characters from the 'isident' option. | |
7 | 830 */ |
831 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
832 vim_isIDc(int c) |
7 | 833 { |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
834 return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR)); |
7 | 835 } |
836 | |
837 /* | |
838 * return TRUE if 'c' is a keyword character: Letters and characters from | |
10549
055b1633aed7
patch 8.0.0164: outdated and misplaced comments
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
839 * 'iskeyword' option for the current buffer. |
7 | 840 * For multi-byte characters mb_get_class() is used (builtin rules). |
841 */ | |
842 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
843 vim_iswordc(int c) |
7 | 844 { |
4043 | 845 return vim_iswordc_buf(c, curbuf); |
846 } | |
847 | |
848 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
849 vim_iswordc_buf(int c, buf_T *buf) |
4043 | 850 { |
7 | 851 if (c >= 0x100) |
852 { | |
853 if (enc_dbcs != 0) | |
1869 | 854 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2; |
7 | 855 if (enc_utf8) |
10724
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
856 return utf_class_buf(c, buf) >= 2; |
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
857 return FALSE; |
7 | 858 } |
10724
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
859 return (c > 0 && GET_CHARTAB(buf, c) != 0); |
7 | 860 } |
861 | |
862 /* | |
863 * Just like vim_iswordc() but uses a pointer to the (multi-byte) character. | |
864 */ | |
865 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
866 vim_iswordp(char_u *p) |
7 | 867 { |
10724
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
868 return vim_iswordp_buf(p, curbuf); |
7 | 869 } |
870 | |
871 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
872 vim_iswordp_buf(char_u *p, buf_T *buf) |
7 | 873 { |
10724
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
874 int c = *p; |
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
875 |
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
876 if (has_mbyte && MB_BYTE2LEN(c) > 1) |
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
877 c = (*mb_ptr2char)(p); |
ae1c6bf22e5f
patch 8.0.0252: not properly recognizing word characters between 128 and 255
Christian Brabandt <cb@256bit.org>
parents:
10720
diff
changeset
|
878 return vim_iswordc_buf(c, buf); |
7 | 879 } |
880 | |
881 /* | |
882 * return TRUE if 'c' is a valid file-name character | |
883 * Assume characters above 0x100 are valid (multi-byte). | |
884 */ | |
885 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
886 vim_isfilec(int c) |
7 | 887 { |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
888 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR))); |
7 | 889 } |
890 | |
891 /* | |
1369 | 892 * return TRUE if 'c' is a valid file-name character or a wildcard character |
893 * Assume characters above 0x100 are valid (multi-byte). | |
894 * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]") | |
895 * returns false. | |
896 */ | |
897 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
898 vim_isfilec_or_wc(int c) |
1369 | 899 { |
900 char_u buf[2]; | |
901 | |
902 buf[0] = (char_u)c; | |
903 buf[1] = NUL; | |
904 return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf); | |
905 } | |
906 | |
907 /* | |
11333
fef09eb74832
patch 8.0.0552: toupper and tolower don't work properly for Turkish
Christian Brabandt <cb@256bit.org>
parents:
11133
diff
changeset
|
908 * Return TRUE if 'c' is a printable character. |
7 | 909 * Assume characters above 0x100 are printable (multi-byte), except for |
910 * Unicode. | |
911 */ | |
912 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
913 vim_isprintc(int c) |
7 | 914 { |
915 if (enc_utf8 && c >= 0x100) | |
916 return utf_printable(c); | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
917 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR))); |
7 | 918 } |
919 | |
920 /* | |
921 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head | |
922 * byte of a double-byte character. | |
923 */ | |
924 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
925 vim_isprintc_strict(int c) |
7 | 926 { |
927 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1) | |
928 return FALSE; | |
929 if (enc_utf8 && c >= 0x100) | |
930 return utf_printable(c); | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
931 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR))); |
7 | 932 } |
933 | |
934 /* | |
935 * like chartabsize(), but also check for line breaks on the screen | |
936 */ | |
937 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
938 lbr_chartabsize( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
939 char_u *line UNUSED, /* start of the line */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
940 unsigned char *s, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
941 colnr_T col) |
7 | 942 { |
943 #ifdef FEAT_LINEBREAK | |
5995 | 944 if (!curwin->w_p_lbr && *p_sbr == NUL && !curwin->w_p_bri) |
7 | 945 { |
946 #endif | |
947 if (curwin->w_p_wrap) | |
948 return win_nolbr_chartabsize(curwin, s, col, NULL); | |
949 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col) | |
950 #ifdef FEAT_LINEBREAK | |
951 } | |
5995 | 952 return win_lbr_chartabsize(curwin, line == NULL ? s : line, s, col, NULL); |
7 | 953 #endif |
954 } | |
955 | |
956 /* | |
957 * Call lbr_chartabsize() and advance the pointer. | |
958 */ | |
959 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
960 lbr_chartabsize_adv( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
961 char_u *line, /* start of the line */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
962 char_u **s, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
963 colnr_T col) |
7 | 964 { |
965 int retval; | |
966 | |
5995 | 967 retval = lbr_chartabsize(line, *s, col); |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
968 MB_PTR_ADV(*s); |
7 | 969 return retval; |
970 } | |
971 | |
972 /* | |
973 * This function is used very often, keep it fast!!!! | |
974 * | |
975 * If "headp" not NULL, set *headp to the size of what we for 'showbreak' | |
976 * string at start of line. Warning: *headp is only set if it's a non-zero | |
977 * value, init to 0 before calling. | |
978 */ | |
979 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
980 win_lbr_chartabsize( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
981 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
982 char_u *line UNUSED, /* start of the line */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
983 char_u *s, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
984 colnr_T col, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
985 int *headp UNUSED) |
7 | 986 { |
987 #ifdef FEAT_LINEBREAK | |
988 int c; | |
989 int size; | |
990 colnr_T col2; | |
6024 | 991 colnr_T col_adj = 0; /* col + screen size of tab */ |
7 | 992 colnr_T colmax; |
993 int added; | |
994 int mb_added = 0; | |
995 int numberextra; | |
996 char_u *ps; | |
997 int tab_corr = (*s == TAB); | |
236 | 998 int n; |
7 | 999 |
1000 /* | |
5995 | 1001 * No 'linebreak', 'showbreak' and 'breakindent': return quickly. |
7 | 1002 */ |
5995 | 1003 if (!wp->w_p_lbr && !wp->w_p_bri && *p_sbr == NUL) |
7 | 1004 #endif |
1005 { | |
1006 if (wp->w_p_wrap) | |
1007 return win_nolbr_chartabsize(wp, s, col, headp); | |
1008 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col) | |
1009 } | |
1010 | |
1011 #ifdef FEAT_LINEBREAK | |
1012 /* | |
1013 * First get normal size, without 'linebreak' | |
1014 */ | |
1015 size = win_chartabsize(wp, s, col); | |
1016 c = *s; | |
6024 | 1017 if (tab_corr) |
1018 col_adj = size - 1; | |
7 | 1019 |
1020 /* | |
1021 * If 'linebreak' set check at a blank before a non-blank if the line | |
1022 * needs a break here | |
1023 */ | |
1024 if (wp->w_p_lbr | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1025 && VIM_ISBREAK(c) |
11133
d8e830e32be9
patch 8.0.0454: compiler warnings for "always true" comparison
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1026 && !VIM_ISBREAK((int)s[1]) |
7 | 1027 && wp->w_p_wrap |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12323
diff
changeset
|
1028 && wp->w_width != 0) |
7 | 1029 { |
1030 /* | |
1031 * Count all characters from first non-blank after a blank up to next | |
1032 * non-blank after a blank. | |
1033 */ | |
1034 numberextra = win_col_off(wp); | |
1035 col2 = col; | |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1036 colmax = (colnr_T)(wp->w_width - numberextra - col_adj); |
7 | 1037 if (col >= colmax) |
236 | 1038 { |
6024 | 1039 colmax += col_adj; |
1040 n = colmax + win_col_off2(wp); | |
236 | 1041 if (n > 0) |
6024 | 1042 colmax += (((col - colmax) / n) + 1) * n - col_adj; |
236 | 1043 } |
1044 | |
7 | 1045 for (;;) |
1046 { | |
1047 ps = s; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1048 MB_PTR_ADV(s); |
7 | 1049 c = *s; |
1050 if (!(c != NUL | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1051 && (VIM_ISBREAK(c) |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1052 || (!VIM_ISBREAK(c) |
11133
d8e830e32be9
patch 8.0.0454: compiler warnings for "always true" comparison
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1053 && (col2 == col || !VIM_ISBREAK((int)*ps)))))) |
7 | 1054 break; |
1055 | |
1056 col2 += win_chartabsize(wp, s, col2); | |
1057 if (col2 >= colmax) /* doesn't fit */ | |
1058 { | |
6024 | 1059 size = colmax - col + col_adj; |
7 | 1060 break; |
1061 } | |
1062 } | |
1063 } | |
1064 else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1 | |
1065 && wp->w_p_wrap && in_win_border(wp, col)) | |
1066 { | |
1067 ++size; /* Count the ">" in the last column. */ | |
1068 mb_added = 1; | |
1069 } | |
1070 | |
1071 /* | |
5995 | 1072 * May have to add something for 'breakindent' and/or 'showbreak' |
1073 * string at start of line. | |
7 | 1074 * Set *headp to the size of what we add. |
1075 */ | |
1076 added = 0; | |
5995 | 1077 if ((*p_sbr != NUL || wp->w_p_bri) && wp->w_p_wrap && col != 0) |
7 | 1078 { |
6503 | 1079 colnr_T sbrlen = 0; |
1080 int numberwidth = win_col_off(wp); | |
1081 | |
1082 numberextra = numberwidth; | |
7 | 1083 col += numberextra + mb_added; |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1084 if (col >= (colnr_T)wp->w_width) |
7 | 1085 { |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1086 col -= wp->w_width; |
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1087 numberextra = wp->w_width - (numberextra - win_col_off2(wp)); |
6503 | 1088 if (col >= numberextra && numberextra > 0) |
6312 | 1089 col %= numberextra; |
6288 | 1090 if (*p_sbr != NUL) |
1091 { | |
6503 | 1092 sbrlen = (colnr_T)MB_CHARLEN(p_sbr); |
6288 | 1093 if (col >= sbrlen) |
1094 col -= sbrlen; | |
1095 } | |
6503 | 1096 if (col >= numberextra && numberextra > 0) |
7 | 1097 col = col % numberextra; |
6503 | 1098 else if (col > 0 && numberextra > 0) |
1099 col += numberwidth - win_col_off2(wp); | |
1100 | |
1101 numberwidth -= win_col_off2(wp); | |
7 | 1102 } |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1103 if (col == 0 || col + size + sbrlen > (colnr_T)wp->w_width) |
7 | 1104 { |
5995 | 1105 added = 0; |
1106 if (*p_sbr != NUL) | |
6503 | 1107 { |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1108 if (size + sbrlen + numberwidth > (colnr_T)wp->w_width) |
6503 | 1109 { |
16819
91619e48e1a7
patch 8.1.1411: Coverity warns for divide by zero
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
1110 // calculate effective window width |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1111 int width = (colnr_T)wp->w_width - sbrlen - numberwidth; |
16054
78faa25f9698
patch 8.1.1032: warnings from clang static analyzer
Bram Moolenaar <Bram@vim.org>
parents:
15850
diff
changeset
|
1112 int prev_width = col |
78faa25f9698
patch 8.1.1032: warnings from clang static analyzer
Bram Moolenaar <Bram@vim.org>
parents:
15850
diff
changeset
|
1113 ? ((colnr_T)wp->w_width - (sbrlen + col)) : 0; |
16819
91619e48e1a7
patch 8.1.1411: Coverity warns for divide by zero
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
1114 |
91619e48e1a7
patch 8.1.1411: Coverity warns for divide by zero
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
1115 if (width <= 0) |
91619e48e1a7
patch 8.1.1411: Coverity warns for divide by zero
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
1116 width = (colnr_T)1; |
6503 | 1117 added += ((size - prev_width) / width) * vim_strsize(p_sbr); |
1118 if ((size - prev_width) % width) | |
16819
91619e48e1a7
patch 8.1.1411: Coverity warns for divide by zero
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
1119 // wrapped, add another length of 'sbr' |
6503 | 1120 added += vim_strsize(p_sbr); |
1121 } | |
1122 else | |
1123 added += vim_strsize(p_sbr); | |
1124 } | |
5995 | 1125 if (wp->w_p_bri) |
1126 added += get_breakindent_win(wp, line); | |
1127 | |
6160 | 1128 size += added; |
7 | 1129 if (col != 0) |
1130 added = 0; | |
1131 } | |
1132 } | |
1133 if (headp != NULL) | |
1134 *headp = added + mb_added; | |
1135 return size; | |
1136 #endif | |
1137 } | |
1138 | |
1139 /* | |
1140 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and | |
1141 * 'wrap' is on. This means we need to check for a double-byte character that | |
1142 * doesn't fit at the end of the screen line. | |
1143 */ | |
1144 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1145 win_nolbr_chartabsize( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1146 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1147 char_u *s, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1148 colnr_T col, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1149 int *headp) |
7 | 1150 { |
1151 int n; | |
1152 | |
1153 if (*s == TAB && (!wp->w_p_list || lcs_tab1)) | |
1154 { | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1155 # ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1156 return tabstop_padding(col, wp->w_buffer->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1157 wp->w_buffer->b_p_vts_array); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1158 # else |
7 | 1159 n = wp->w_buffer->b_p_ts; |
1160 return (int)(n - (col % n)); | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1161 # endif |
7 | 1162 } |
1163 n = ptr2cells(s); | |
1164 /* Add one cell for a double-width character in the last column of the | |
1165 * window, displayed with a ">". */ | |
1166 if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col)) | |
1167 { | |
1168 if (headp != NULL) | |
1169 *headp = 1; | |
1170 return 3; | |
1171 } | |
1172 return n; | |
1173 } | |
1174 | |
1175 /* | |
1176 * Return TRUE if virtual column "vcol" is in the rightmost column of window | |
1177 * "wp". | |
1178 */ | |
17789
0f7ae8010787
patch 8.1.1891: functions used in one file are global
Bram Moolenaar <Bram@vim.org>
parents:
16819
diff
changeset
|
1179 static int |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1180 in_win_border(win_T *wp, colnr_T vcol) |
7 | 1181 { |
1869 | 1182 int width1; /* width of first line (after line number) */ |
1183 int width2; /* width of further lines */ | |
7 | 1184 |
1185 if (wp->w_width == 0) /* there is no border */ | |
1186 return FALSE; | |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1187 width1 = wp->w_width - win_col_off(wp); |
1869 | 1188 if ((int)vcol < width1 - 1) |
7 | 1189 return FALSE; |
1869 | 1190 if ((int)vcol == width1 - 1) |
7 | 1191 return TRUE; |
1192 width2 = width1 + win_col_off2(wp); | |
1970 | 1193 if (width2 <= 0) |
1194 return FALSE; | |
7 | 1195 return ((vcol - width1) % width2 == width2 - 1); |
1196 } | |
1197 | |
1198 /* | |
1199 * Get virtual column number of pos. | |
1200 * start: on the first position of this character (TAB, ctrl) | |
1201 * cursor: where the cursor is on this character (first char, except for TAB) | |
1202 * end: on the last position of this character (TAB, ctrl) | |
1203 * | |
1204 * This is used very often, keep it fast! | |
1205 */ | |
1206 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1207 getvcol( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1208 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1209 pos_T *pos, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1210 colnr_T *start, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1211 colnr_T *cursor, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1212 colnr_T *end) |
7 | 1213 { |
1214 colnr_T vcol; | |
1215 char_u *ptr; /* points to current char */ | |
1216 char_u *posptr; /* points to char at pos->col */ | |
5995 | 1217 char_u *line; /* start of the line */ |
7 | 1218 int incr; |
1219 int head; | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1220 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1221 int *vts = wp->w_buffer->b_p_vts_array; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1222 #endif |
7 | 1223 int ts = wp->w_buffer->b_p_ts; |
1224 int c; | |
1225 | |
1226 vcol = 0; | |
5995 | 1227 line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); |
2108
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
1979
diff
changeset
|
1228 if (pos->col == MAXCOL) |
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
1979
diff
changeset
|
1229 posptr = NULL; /* continue until the NUL */ |
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
1979
diff
changeset
|
1230 else |
10720
44a1661f4cfa
patch 8.0.0250: virtcol() does not work well for multi-byte characters
Christian Brabandt <cb@256bit.org>
parents:
10658
diff
changeset
|
1231 { |
10835
c9da7f9137af
patch 8.0.0307: asan detects a memory error when EXITFREE is defined
Christian Brabandt <cb@256bit.org>
parents:
10724
diff
changeset
|
1232 /* Special check for an empty line, which can happen on exit, when |
c9da7f9137af
patch 8.0.0307: asan detects a memory error when EXITFREE is defined
Christian Brabandt <cb@256bit.org>
parents:
10724
diff
changeset
|
1233 * ml_get_buf() always returns an empty string. */ |
c9da7f9137af
patch 8.0.0307: asan detects a memory error when EXITFREE is defined
Christian Brabandt <cb@256bit.org>
parents:
10724
diff
changeset
|
1234 if (*ptr == NUL) |
c9da7f9137af
patch 8.0.0307: asan detects a memory error when EXITFREE is defined
Christian Brabandt <cb@256bit.org>
parents:
10724
diff
changeset
|
1235 pos->col = 0; |
2108
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
1979
diff
changeset
|
1236 posptr = ptr + pos->col; |
10720
44a1661f4cfa
patch 8.0.0250: virtcol() does not work well for multi-byte characters
Christian Brabandt <cb@256bit.org>
parents:
10658
diff
changeset
|
1237 if (has_mbyte) |
44a1661f4cfa
patch 8.0.0250: virtcol() does not work well for multi-byte characters
Christian Brabandt <cb@256bit.org>
parents:
10658
diff
changeset
|
1238 /* always start on the first byte */ |
44a1661f4cfa
patch 8.0.0250: virtcol() does not work well for multi-byte characters
Christian Brabandt <cb@256bit.org>
parents:
10658
diff
changeset
|
1239 posptr -= (*mb_head_off)(line, posptr); |
44a1661f4cfa
patch 8.0.0250: virtcol() does not work well for multi-byte characters
Christian Brabandt <cb@256bit.org>
parents:
10658
diff
changeset
|
1240 } |
7 | 1241 |
1242 /* | |
1243 * This function is used very often, do some speed optimizations. | |
5995 | 1244 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set |
1245 * use a simple loop. | |
7 | 1246 * Also use this when 'list' is set but tabs take their normal size. |
1247 */ | |
1248 if ((!wp->w_p_list || lcs_tab1 != NUL) | |
1249 #ifdef FEAT_LINEBREAK | |
5995 | 1250 && !wp->w_p_lbr && *p_sbr == NUL && !wp->w_p_bri |
7 | 1251 #endif |
1252 ) | |
1253 { | |
1254 for (;;) | |
1255 { | |
1256 head = 0; | |
1257 c = *ptr; | |
1258 /* make sure we don't go past the end of the line */ | |
1259 if (c == NUL) | |
1260 { | |
1261 incr = 1; /* NUL at end of line only takes one column */ | |
1262 break; | |
1263 } | |
1264 /* A tab gets expanded, depending on the current column */ | |
1265 if (c == TAB) | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1266 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1267 incr = tabstop_padding(vcol, ts, vts); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1268 #else |
7 | 1269 incr = ts - (vcol % ts); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14061
diff
changeset
|
1270 #endif |
7 | 1271 else |
1272 { | |
1273 if (has_mbyte) | |
1274 { | |
1275 /* For utf-8, if the byte is >= 0x80, need to look at | |
1276 * further bytes to find the cell width. */ | |
1277 if (enc_utf8 && c >= 0x80) | |
1278 incr = utf_ptr2cells(ptr); | |
1279 else | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1280 incr = g_chartab[c] & CT_CELL_MASK; |
7 | 1281 |
1282 /* If a double-cell char doesn't fit at the end of a line | |
1283 * it wraps to the next line, it's like this char is three | |
1284 * cells wide. */ | |
1546 | 1285 if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1 |
1286 && in_win_border(wp, vcol)) | |
7 | 1287 { |
1288 ++incr; | |
1289 head = 1; | |
1290 } | |
1291 } | |
1292 else | |
7697
f04e2b6feea2
commit https://github.com/vim/vim/commit/88e8f9f14434a7cd538d0c159dc432bea869a5bd
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1293 incr = g_chartab[c] & CT_CELL_MASK; |
7 | 1294 } |
1295 | |
2108
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
1979
diff
changeset
|
1296 if (posptr != NULL && ptr >= posptr) /* character at pos->col */ |
7 | 1297 break; |
1298 | |
1299 vcol += incr; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1300 MB_PTR_ADV(ptr); |
7 | 1301 } |
1302 } | |
1303 else | |
1304 { | |
1305 for (;;) | |
1306 { | |
1307 /* A tab gets expanded, depending on the current column */ | |
1308 head = 0; | |
5995 | 1309 incr = win_lbr_chartabsize(wp, line, ptr, vcol, &head); |
7 | 1310 /* make sure we don't go past the end of the line */ |
1311 if (*ptr == NUL) | |
1312 { | |
1313 incr = 1; /* NUL at end of line only takes one column */ | |
1314 break; | |
1315 } | |
1316 | |
2108
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
1979
diff
changeset
|
1317 if (posptr != NULL && ptr >= posptr) /* character at pos->col */ |
7 | 1318 break; |
1319 | |
1320 vcol += incr; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1321 MB_PTR_ADV(ptr); |
7 | 1322 } |
1323 } | |
1324 if (start != NULL) | |
1325 *start = vcol + head; | |
1326 if (end != NULL) | |
1327 *end = vcol + incr - 1; | |
1328 if (cursor != NULL) | |
1329 { | |
1330 if (*ptr == TAB | |
1331 && (State & NORMAL) | |
1332 && !wp->w_p_list | |
1333 && !virtual_active() | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10835
diff
changeset
|
1334 && !(VIsual_active |
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10835
diff
changeset
|
1335 && (*p_sel == 'e' || LTOREQ_POS(*pos, VIsual))) |
7 | 1336 ) |
1337 *cursor = vcol + incr - 1; /* cursor at end */ | |
1338 else | |
1339 *cursor = vcol + head; /* cursor at start */ | |
1340 } | |
1341 } | |
1342 | |
1343 /* | |
1344 * Get virtual cursor column in the current window, pretending 'list' is off. | |
1345 */ | |
1346 colnr_T | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1347 getvcol_nolist(pos_T *posp) |
7 | 1348 { |
1349 int list_save = curwin->w_p_list; | |
1350 colnr_T vcol; | |
1351 | |
1352 curwin->w_p_list = FALSE; | |
13786
0fa21ba32e21
patch 8.0.1765: CTRL-G j in Insert mode is incorrect when 'virtualedit' set
Christian Brabandt <cb@256bit.org>
parents:
13553
diff
changeset
|
1353 if (posp->coladd) |
0fa21ba32e21
patch 8.0.1765: CTRL-G j in Insert mode is incorrect when 'virtualedit' set
Christian Brabandt <cb@256bit.org>
parents:
13553
diff
changeset
|
1354 getvvcol(curwin, posp, NULL, &vcol, NULL); |
0fa21ba32e21
patch 8.0.1765: CTRL-G j in Insert mode is incorrect when 'virtualedit' set
Christian Brabandt <cb@256bit.org>
parents:
13553
diff
changeset
|
1355 else |
0fa21ba32e21
patch 8.0.1765: CTRL-G j in Insert mode is incorrect when 'virtualedit' set
Christian Brabandt <cb@256bit.org>
parents:
13553
diff
changeset
|
1356 getvcol(curwin, posp, NULL, &vcol, NULL); |
7 | 1357 curwin->w_p_list = list_save; |
1358 return vcol; | |
1359 } | |
1360 | |
1361 /* | |
1362 * Get virtual column in virtual mode. | |
1363 */ | |
1364 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1365 getvvcol( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1366 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1367 pos_T *pos, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1368 colnr_T *start, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1369 colnr_T *cursor, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1370 colnr_T *end) |
7 | 1371 { |
1372 colnr_T col; | |
1373 colnr_T coladd; | |
1374 colnr_T endadd; | |
1375 char_u *ptr; | |
1376 | |
1377 if (virtual_active()) | |
1378 { | |
1379 /* For virtual mode, only want one value */ | |
1380 getvcol(wp, pos, &col, NULL, NULL); | |
1381 | |
1382 coladd = pos->coladd; | |
1383 endadd = 0; | |
1384 /* Cannot put the cursor on part of a wide character. */ | |
1385 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); | |
1869 | 1386 if (pos->col < (colnr_T)STRLEN(ptr)) |
7 | 1387 { |
1388 int c = (*mb_ptr2char)(ptr + pos->col); | |
1389 | |
1390 if (c != TAB && vim_isprintc(c)) | |
1391 { | |
1869 | 1392 endadd = (colnr_T)(char2cells(c) - 1); |
557 | 1393 if (coladd > endadd) /* past end of line */ |
1394 endadd = 0; | |
7 | 1395 else |
1396 coladd = 0; | |
1397 } | |
1398 } | |
1399 col += coladd; | |
1400 if (start != NULL) | |
1401 *start = col; | |
1402 if (cursor != NULL) | |
1403 *cursor = col; | |
1404 if (end != NULL) | |
1405 *end = col + endadd; | |
1406 } | |
1407 else | |
1408 getvcol(wp, pos, start, cursor, end); | |
1409 } | |
1410 | |
1411 /* | |
1412 * Get the leftmost and rightmost virtual column of pos1 and pos2. | |
1413 * Used for Visual block mode. | |
1414 */ | |
1415 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1416 getvcols( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1417 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1418 pos_T *pos1, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1419 pos_T *pos2, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1420 colnr_T *left, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1421 colnr_T *right) |
7 | 1422 { |
1423 colnr_T from1, from2, to1, to2; | |
1424 | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10835
diff
changeset
|
1425 if (LT_POSP(pos1, pos2)) |
7 | 1426 { |
1427 getvvcol(wp, pos1, &from1, NULL, &to1); | |
1428 getvvcol(wp, pos2, &from2, NULL, &to2); | |
1429 } | |
1430 else | |
1431 { | |
1432 getvvcol(wp, pos2, &from1, NULL, &to1); | |
1433 getvvcol(wp, pos1, &from2, NULL, &to2); | |
1434 } | |
1435 if (from2 < from1) | |
1436 *left = from2; | |
1437 else | |
1438 *left = from1; | |
1439 if (to2 > to1) | |
1440 { | |
1441 if (*p_sel == 'e' && from2 - 1 >= to1) | |
1442 *right = from2 - 1; | |
1443 else | |
1444 *right = to2; | |
1445 } | |
1446 else | |
1447 *right = to1; | |
1448 } | |
1449 | |
1450 /* | |
1451 * skipwhite: skip over ' ' and '\t'. | |
1452 */ | |
1453 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1454 skipwhite(char_u *q) |
7 | 1455 { |
1687 | 1456 char_u *p = q; |
1457 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
1458 while (VIM_ISWHITE(*p)) /* skip to next non-white */ |
7 | 1459 ++p; |
1460 return p; | |
1461 } | |
1462 | |
1463 /* | |
12323
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1464 * getwhitecols: return the number of whitespace |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1465 * columns (bytes) at the start of a given line |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1466 */ |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1467 int |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1468 getwhitecols_curline() |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1469 { |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1470 return getwhitecols(ml_get_curline()); |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1471 } |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1472 |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1473 int |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1474 getwhitecols(char_u *p) |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1475 { |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1476 return skipwhite(p) - p; |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1477 } |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1478 |
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11337
diff
changeset
|
1479 /* |
293 | 1480 * skip over digits |
7 | 1481 */ |
1482 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1483 skipdigits(char_u *q) |
7 | 1484 { |
1687 | 1485 char_u *p = q; |
1486 | |
7 | 1487 while (VIM_ISDIGIT(*p)) /* skip to next non-digit */ |
1488 ++p; | |
1489 return p; | |
1490 } | |
1491 | |
741 | 1492 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO) |
301 | 1493 /* |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1494 * skip over binary digits |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1495 */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1496 char_u * |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1497 skipbin(char_u *q) |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1498 { |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1499 char_u *p = q; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1500 |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1501 while (vim_isbdigit(*p)) /* skip to next non-digit */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1502 ++p; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1503 return p; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1504 } |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1505 |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1506 /* |
301 | 1507 * skip over digits and hex characters |
1508 */ | |
1509 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1510 skiphex(char_u *q) |
301 | 1511 { |
1687 | 1512 char_u *p = q; |
1513 | |
301 | 1514 while (vim_isxdigit(*p)) /* skip to next non-digit */ |
1515 ++p; | |
1516 return p; | |
1517 } | |
1518 #endif | |
1519 | |
293 | 1520 /* |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1521 * skip to bin digit (or NUL after the string) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1522 */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1523 char_u * |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1524 skiptobin(char_u *q) |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1525 { |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1526 char_u *p = q; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1527 |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1528 while (*p != NUL && !vim_isbdigit(*p)) /* skip to next digit */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1529 ++p; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1530 return p; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1531 } |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1532 |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1533 /* |
293 | 1534 * skip to digit (or NUL after the string) |
1535 */ | |
1536 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1537 skiptodigit(char_u *q) |
293 | 1538 { |
1687 | 1539 char_u *p = q; |
1540 | |
293 | 1541 while (*p != NUL && !VIM_ISDIGIT(*p)) /* skip to next digit */ |
1542 ++p; | |
1543 return p; | |
1544 } | |
1545 | |
1546 /* | |
1547 * skip to hex character (or NUL after the string) | |
1548 */ | |
1549 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1550 skiptohex(char_u *q) |
293 | 1551 { |
1687 | 1552 char_u *p = q; |
1553 | |
293 | 1554 while (*p != NUL && !vim_isxdigit(*p)) /* skip to next digit */ |
1555 ++p; | |
1556 return p; | |
1557 } | |
1558 | |
7 | 1559 /* |
1560 * Variant of isdigit() that can handle characters > 0x100. | |
1561 * We don't use isdigit() here, because on some systems it also considers | |
1562 * superscript 1 to be a digit. | |
1563 * Use the VIM_ISDIGIT() macro for simple arguments. | |
1564 */ | |
1565 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1566 vim_isdigit(int c) |
7 | 1567 { |
1568 return (c >= '0' && c <= '9'); | |
1569 } | |
1570 | |
1571 /* | |
1572 * Variant of isxdigit() that can handle characters > 0x100. | |
1573 * We don't use isxdigit() here, because on some systems it also considers | |
1574 * superscript 1 to be a digit. | |
1575 */ | |
1576 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1577 vim_isxdigit(int c) |
7 | 1578 { |
1579 return (c >= '0' && c <= '9') | |
1580 || (c >= 'a' && c <= 'f') | |
1581 || (c >= 'A' && c <= 'F'); | |
1582 } | |
1583 | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1584 /* |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1585 * Corollary of vim_isdigit and vim_isxdigit() that can handle |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1586 * characters > 0x100. |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1587 */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1588 int |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1589 vim_isbdigit(int c) |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1590 { |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1591 return (c == '0' || c == '1'); |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1592 } |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1593 |
492 | 1594 /* |
1595 * Vim's own character class functions. These exist because many library | |
1596 * islower()/toupper() etc. do not work properly: they crash when used with | |
1597 * invalid values or can't handle latin1 when the locale is C. | |
1598 * Speed is most important here. | |
1599 */ | |
1600 #define LATIN1LOWER 'l' | |
1601 #define LATIN1UPPER 'U' | |
1602 | |
497 | 1603 static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll"; |
3533 | 1604 static char_u latin1upper[257] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xf7\xd8\xd9\xda\xdb\xdc\xdd\xde\xff"; |
1605 static char_u latin1lower[257] = " !\"#$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xd7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; | |
492 | 1606 |
1607 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1608 vim_islower(int c) |
492 | 1609 { |
1610 if (c <= '@') | |
1611 return FALSE; | |
1612 if (c >= 0x80) | |
1613 { | |
1614 if (enc_utf8) | |
1615 return utf_islower(c); | |
1616 if (c >= 0x100) | |
1617 { | |
1618 #ifdef HAVE_ISWLOWER | |
1619 if (has_mbyte) | |
1620 return iswlower(c); | |
1621 #endif | |
1622 /* islower() can't handle these chars and may crash */ | |
1623 return FALSE; | |
1624 } | |
1625 if (enc_latin1like) | |
1626 return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER; | |
1627 } | |
1628 return islower(c); | |
1629 } | |
1630 | |
1631 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1632 vim_isupper(int c) |
492 | 1633 { |
1634 if (c <= '@') | |
1635 return FALSE; | |
1636 if (c >= 0x80) | |
1637 { | |
1638 if (enc_utf8) | |
1639 return utf_isupper(c); | |
1640 if (c >= 0x100) | |
1641 { | |
1642 #ifdef HAVE_ISWUPPER | |
1643 if (has_mbyte) | |
1644 return iswupper(c); | |
1645 #endif | |
1646 /* islower() can't handle these chars and may crash */ | |
1647 return FALSE; | |
1648 } | |
1649 if (enc_latin1like) | |
1650 return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER; | |
1651 } | |
1652 return isupper(c); | |
1653 } | |
1654 | |
1655 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1656 vim_toupper(int c) |
492 | 1657 { |
1658 if (c <= '@') | |
1659 return c; | |
11333
fef09eb74832
patch 8.0.0552: toupper and tolower don't work properly for Turkish
Christian Brabandt <cb@256bit.org>
parents:
11133
diff
changeset
|
1660 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII)) |
492 | 1661 { |
1662 if (enc_utf8) | |
1663 return utf_toupper(c); | |
1664 if (c >= 0x100) | |
1665 { | |
1666 #ifdef HAVE_TOWUPPER | |
1667 if (has_mbyte) | |
1668 return towupper(c); | |
1669 #endif | |
1670 /* toupper() can't handle these chars and may crash */ | |
1671 return c; | |
1672 } | |
1673 if (enc_latin1like) | |
1674 return latin1upper[c]; | |
1675 } | |
11337
f0fbebf19b80
patch 8.0.0554: toupper and tolower don't work properly for Turkish
Christian Brabandt <cb@256bit.org>
parents:
11333
diff
changeset
|
1676 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII)) |
f0fbebf19b80
patch 8.0.0554: toupper and tolower don't work properly for Turkish
Christian Brabandt <cb@256bit.org>
parents:
11333
diff
changeset
|
1677 return TOUPPER_ASC(c); |
492 | 1678 return TOUPPER_LOC(c); |
1679 } | |
1680 | |
1681 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1682 vim_tolower(int c) |
492 | 1683 { |
1684 if (c <= '@') | |
1685 return c; | |
11333
fef09eb74832
patch 8.0.0552: toupper and tolower don't work properly for Turkish
Christian Brabandt <cb@256bit.org>
parents:
11133
diff
changeset
|
1686 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII)) |
492 | 1687 { |
1688 if (enc_utf8) | |
1689 return utf_tolower(c); | |
1690 if (c >= 0x100) | |
1691 { | |
1692 #ifdef HAVE_TOWLOWER | |
1693 if (has_mbyte) | |
1694 return towlower(c); | |
1695 #endif | |
1696 /* tolower() can't handle these chars and may crash */ | |
1697 return c; | |
1698 } | |
1699 if (enc_latin1like) | |
1700 return latin1lower[c]; | |
1701 } | |
11337
f0fbebf19b80
patch 8.0.0554: toupper and tolower don't work properly for Turkish
Christian Brabandt <cb@256bit.org>
parents:
11333
diff
changeset
|
1702 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII)) |
f0fbebf19b80
patch 8.0.0554: toupper and tolower don't work properly for Turkish
Christian Brabandt <cb@256bit.org>
parents:
11333
diff
changeset
|
1703 return TOLOWER_ASC(c); |
492 | 1704 return TOLOWER_LOC(c); |
1705 } | |
1706 | |
7 | 1707 /* |
1708 * skiptowhite: skip over text until ' ' or '\t' or NUL. | |
1709 */ | |
1710 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1711 skiptowhite(char_u *p) |
7 | 1712 { |
1713 while (*p != ' ' && *p != '\t' && *p != NUL) | |
1714 ++p; | |
1715 return p; | |
1716 } | |
1717 | |
1718 /* | |
1719 * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars | |
1720 */ | |
1721 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1722 skiptowhite_esc(char_u *p) |
7 | 1723 { |
1724 while (*p != ' ' && *p != '\t' && *p != NUL) | |
1725 { | |
1726 if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL) | |
1727 ++p; | |
1728 ++p; | |
1729 } | |
1730 return p; | |
1731 } | |
1732 | |
1733 /* | |
1734 * Getdigits: Get a number from a string and skip over it. | |
1735 * Note: the argument is a pointer to a char_u pointer! | |
1736 */ | |
1737 long | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1738 getdigits(char_u **pp) |
7 | 1739 { |
1740 char_u *p; | |
1741 long retval; | |
1742 | |
1743 p = *pp; | |
1744 retval = atol((char *)p); | |
1745 if (*p == '-') /* skip negative sign */ | |
1746 ++p; | |
1747 p = skipdigits(p); /* skip to next non-digit */ | |
1748 *pp = p; | |
1749 return retval; | |
1750 } | |
1751 | |
1752 /* | |
1753 * Return TRUE if "lbuf" is empty or only contains blanks. | |
1754 */ | |
1755 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1756 vim_isblankline(char_u *lbuf) |
7 | 1757 { |
1758 char_u *p; | |
1759 | |
1760 p = skipwhite(lbuf); | |
1761 return (*p == NUL || *p == '\r' || *p == '\n'); | |
1762 } | |
1763 | |
1764 /* | |
1765 * Convert a string into a long and/or unsigned long, taking care of | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1766 * hexadecimal, octal, and binary numbers. Accepts a '-' sign. |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1767 * If "prep" is not NULL, returns a flag to indicate the type of the number: |
7 | 1768 * 0 decimal |
1769 * '0' octal | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1770 * 'B' bin |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1771 * 'b' bin |
7 | 1772 * 'X' hex |
1773 * 'x' hex | |
1774 * If "len" is not NULL, the length of the number in characters is returned. | |
1775 * If "nptr" is not NULL, the signed result is returned in it. | |
1776 * If "unptr" is not NULL, the unsigned result is returned in it. | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1777 * If "what" contains STR2NR_BIN recognize binary numbers |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1778 * If "what" contains STR2NR_OCT recognize octal numbers |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1779 * If "what" contains STR2NR_HEX recognize hex numbers |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1780 * If "what" contains STR2NR_FORCE always assume bin/oct/hex. |
12704
ee5f3f5d3c55
patch 8.0.1230: CTRL-A in Visual mode uses character after selection
Christian Brabandt <cb@256bit.org>
parents:
12702
diff
changeset
|
1781 * If maxlen > 0, check at a maximum maxlen chars. |
16706
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1782 * If strict is TRUE, check the number strictly. return *len = 0 if fail. |
7 | 1783 */ |
1784 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1785 vim_str2nr( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1786 char_u *start, |
16706
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1787 int *prep, // return: type of number 0 = decimal, 'x' |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1788 // or 'X' is hex, '0' = octal, 'b' or 'B' |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1789 // is bin |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1790 int *len, // return: detected length of number |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1791 int what, // what numbers to recognize |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1792 varnumber_T *nptr, // return: signed result |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1793 uvarnumber_T *unptr, // return: unsigned result |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1794 int maxlen, // max length of string to check |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1795 int strict) // check strictly |
7 | 1796 { |
1797 char_u *ptr = start; | |
16706
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1798 int pre = 0; // default is decimal |
7 | 1799 int negative = FALSE; |
9389
32e34e574716
commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents:
8643
diff
changeset
|
1800 uvarnumber_T un = 0; |
39 | 1801 int n; |
7 | 1802 |
16706
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1803 if (len != NULL) |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1804 *len = 0; |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1805 |
7 | 1806 if (ptr[0] == '-') |
1807 { | |
1808 negative = TRUE; | |
1809 ++ptr; | |
1810 } | |
1811 | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1812 /* Recognize hex, octal, and bin. */ |
6927 | 1813 if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9' |
1814 && (maxlen == 0 || maxlen > 1)) | |
7 | 1815 { |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1816 pre = ptr[1]; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1817 if ((what & STR2NR_HEX) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1818 && (pre == 'X' || pre == 'x') && vim_isxdigit(ptr[2]) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1819 && (maxlen == 0 || maxlen > 2)) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1820 /* hexadecimal */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1821 ptr += 2; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1822 else if ((what & STR2NR_BIN) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1823 && (pre == 'B' || pre == 'b') && vim_isbdigit(ptr[2]) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1824 && (maxlen == 0 || maxlen > 2)) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1825 /* binary */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1826 ptr += 2; |
7 | 1827 else |
1828 { | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1829 /* decimal or octal, default is decimal */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1830 pre = 0; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1831 if (what & STR2NR_OCT) |
39 | 1832 { |
1833 /* Don't interpret "0", "08" or "0129" as octal. */ | |
12704
ee5f3f5d3c55
patch 8.0.1230: CTRL-A in Visual mode uses character after selection
Christian Brabandt <cb@256bit.org>
parents:
12702
diff
changeset
|
1834 for (n = 1; n != maxlen && VIM_ISDIGIT(ptr[n]); ++n) |
39 | 1835 { |
1836 if (ptr[n] > '7') | |
1837 { | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1838 pre = 0; /* can't be octal */ |
39 | 1839 break; |
1840 } | |
12702
a99da335b6f1
patch 8.0.1229: condition in vim_str2nr() is always true
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1841 pre = '0'; /* assume octal */ |
39 | 1842 } |
1843 } | |
7 | 1844 } |
1845 } | |
1846 | |
16706
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1847 // Do the conversion manually to avoid sscanf() quirks. |
6927 | 1848 n = 1; |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1849 if (pre == 'B' || pre == 'b' || what == STR2NR_BIN + STR2NR_FORCE) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1850 { |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1851 /* bin */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1852 if (pre != 0) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1853 n += 2; /* skip over "0b" */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1854 while ('0' <= *ptr && *ptr <= '1') |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1855 { |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1856 /* avoid ubsan error for overflow */ |
14061
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1857 if (un <= UVARNUM_MAX / 2) |
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1858 un = 2 * un + (uvarnumber_T)(*ptr - '0'); |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1859 else |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1860 un = UVARNUM_MAX; |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1861 ++ptr; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1862 if (n++ == maxlen) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1863 break; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1864 } |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1865 } |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1866 else if (pre == '0' || what == STR2NR_OCT + STR2NR_FORCE) |
7 | 1867 { |
293 | 1868 /* octal */ |
1869 while ('0' <= *ptr && *ptr <= '7') | |
7 | 1870 { |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1871 /* avoid ubsan error for overflow */ |
14061
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1872 if (un <= UVARNUM_MAX / 8) |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1873 un = 8 * un + (uvarnumber_T)(*ptr - '0'); |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1874 else |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1875 un = UVARNUM_MAX; |
293 | 1876 ++ptr; |
6927 | 1877 if (n++ == maxlen) |
1878 break; | |
7 | 1879 } |
293 | 1880 } |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1881 else if (pre != 0 || what == STR2NR_HEX + STR2NR_FORCE) |
293 | 1882 { |
1883 /* hex */ | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1884 if (pre != 0) |
7072
05afb5be93bd
commit https://github.com/vim/vim/commit/5adfea1ac63e252556bccce54e92e8e10b58f592
Christian Brabandt <cb@256bit.org>
parents:
6927
diff
changeset
|
1885 n += 2; /* skip over "0x" */ |
293 | 1886 while (vim_isxdigit(*ptr)) |
7 | 1887 { |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1888 /* avoid ubsan error for overflow */ |
14061
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1889 if (un <= UVARNUM_MAX / 16) |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1890 un = 16 * un + (uvarnumber_T)hex2nr(*ptr); |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1891 else |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1892 un = UVARNUM_MAX; |
293 | 1893 ++ptr; |
6927 | 1894 if (n++ == maxlen) |
1895 break; | |
7 | 1896 } |
1897 } | |
1898 else | |
1899 { | |
1900 /* decimal */ | |
1901 while (VIM_ISDIGIT(*ptr)) | |
1902 { | |
14061
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1903 uvarnumber_T digit = (uvarnumber_T)(*ptr - '0'); |
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1904 |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1905 /* avoid ubsan error for overflow */ |
14061
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1906 if (un < UVARNUM_MAX / 10 |
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1907 || (un == UVARNUM_MAX / 10 && digit <= UVARNUM_MAX % 10)) |
47b2db8a5709
patch 8.1.0048: vim_str2nr() does not handle numbers close to the maximum
Christian Brabandt <cb@256bit.org>
parents:
13786
diff
changeset
|
1908 un = 10 * un + digit; |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1909 else |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1910 un = UVARNUM_MAX; |
7 | 1911 ++ptr; |
6927 | 1912 if (n++ == maxlen) |
1913 break; | |
7 | 1914 } |
1915 } | |
16706
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1916 // Check for an alpha-numeric character immediately following, that is |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1917 // most likely a typo. |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1918 if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr)) |
77bcb5055fec
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
1919 return; |
7 | 1920 |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1921 if (prep != NULL) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7072
diff
changeset
|
1922 *prep = pre; |
7 | 1923 if (len != NULL) |
1924 *len = (int)(ptr - start); | |
1925 if (nptr != NULL) | |
16 | 1926 { |
1927 if (negative) /* account for leading '-' for decimal numbers */ | |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1928 { |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1929 /* avoid ubsan error for overflow */ |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1930 if (un > VARNUM_MAX) |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1931 *nptr = VARNUM_MIN; |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1932 else |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1933 *nptr = -(varnumber_T)un; |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1934 } |
16 | 1935 else |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1936 { |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1937 if (un > VARNUM_MAX) |
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1938 un = VARNUM_MAX; |
9389
32e34e574716
commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents:
8643
diff
changeset
|
1939 *nptr = (varnumber_T)un; |
10658
77d66e9ac0ab
patch 8.0.0219: ubsan reports errors for overflow
Christian Brabandt <cb@256bit.org>
parents:
10549
diff
changeset
|
1940 } |
16 | 1941 } |
7 | 1942 if (unptr != NULL) |
1943 *unptr = un; | |
1944 } | |
1945 | |
1946 /* | |
1947 * Return the value of a single hex character. | |
1948 * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'. | |
1949 */ | |
1950 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1951 hex2nr(int c) |
7 | 1952 { |
1953 if (c >= 'a' && c <= 'f') | |
1954 return c - 'a' + 10; | |
1955 if (c >= 'A' && c <= 'F') | |
1956 return c - 'A' + 10; | |
1957 return c - '0'; | |
1958 } | |
1959 | |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12323
diff
changeset
|
1960 #if defined(FEAT_TERMRESPONSE) || defined(FEAT_GUI_GTK) || defined(PROTO) |
7 | 1961 /* |
1962 * Convert two hex characters to a byte. | |
1963 * Return -1 if one of the characters is not hex. | |
1964 */ | |
1965 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1966 hexhex2nr(char_u *p) |
7 | 1967 { |
1968 if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1])) | |
1969 return -1; | |
1970 return (hex2nr(p[0]) << 4) + hex2nr(p[1]); | |
1971 } | |
1972 #endif | |
1973 | |
1974 /* | |
1975 * Return TRUE if "str" starts with a backslash that should be removed. | |
16054
78faa25f9698
patch 8.1.1032: warnings from clang static analyzer
Bram Moolenaar <Bram@vim.org>
parents:
15850
diff
changeset
|
1976 * For MS-DOS, MSWIN and OS/2 this is only done when the character after the |
7 | 1977 * backslash is not a normal file name character. |
1978 * '$' is a valid file name character, we don't remove the backslash before | |
1979 * it. This means it is not possible to use an environment variable after a | |
1980 * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works. | |
1981 * Although "\ name" is valid, the backslash in "Program\ files" must be | |
1982 * removed. Assume a file name doesn't start with a space. | |
1983 * For multi-byte names, never remove a backslash before a non-ascii | |
1984 * character, assume that all multi-byte characters are valid file name | |
1985 * characters. | |
1986 */ | |
1987 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1988 rem_backslash(char_u *str) |
7 | 1989 { |
1990 #ifdef BACKSLASH_IN_FILENAME | |
1991 return (str[0] == '\\' | |
1992 && str[1] < 0x80 | |
1993 && (str[1] == ' ' | |
1994 || (str[1] != NUL | |
1995 && str[1] != '*' | |
1996 && str[1] != '?' | |
1997 && !vim_isfilec(str[1])))); | |
1998 #else | |
1999 return (str[0] == '\\' && str[1] != NUL); | |
2000 #endif | |
2001 } | |
2002 | |
2003 /* | |
2004 * Halve the number of backslashes in a file name argument. | |
2005 * For MS-DOS we only do this if the character after the backslash | |
2006 * is not a normal file character. | |
2007 */ | |
2008 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2009 backslash_halve(char_u *p) |
7 | 2010 { |
2011 for ( ; *p; ++p) | |
2012 if (rem_backslash(p)) | |
1621 | 2013 STRMOVE(p, p + 1); |
7 | 2014 } |
2015 | |
2016 /* | |
2017 * backslash_halve() plus save the result in allocated memory. | |
2018 */ | |
2019 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2020 backslash_halve_save(char_u *p) |
7 | 2021 { |
2022 char_u *res; | |
2023 | |
2024 res = vim_strsave(p); | |
2025 if (res == NULL) | |
2026 return p; | |
2027 backslash_halve(res); | |
2028 return res; | |
2029 } | |
2030 | |
2031 #if (defined(EBCDIC) && defined(FEAT_POSTSCRIPT)) || defined(PROTO) | |
2032 /* | |
2033 * Table for EBCDIC to ASCII conversion unashamedly taken from xxd.c! | |
2034 * The first 64 entries have been added to map control characters defined in | |
2035 * ascii.h | |
2036 */ | |
2037 static char_u ebcdic2ascii_tab[256] = | |
2038 { | |
2039 0000, 0001, 0002, 0003, 0004, 0011, 0006, 0177, | |
2040 0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017, | |
2041 0020, 0021, 0022, 0023, 0024, 0012, 0010, 0027, | |
2042 0030, 0031, 0032, 0033, 0033, 0035, 0036, 0037, | |
2043 0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047, | |
2044 0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057, | |
2045 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, | |
2046 0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077, | |
2047 0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246, | |
2048 0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174, | |
2049 0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257, | |
2050 0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176, | |
2051 0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267, | |
2052 0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077, | |
2053 0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301, | |
2054 0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042, | |
2055 0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147, | |
2056 0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311, | |
2057 0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160, | |
2058 0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320, | |
2059 0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170, | |
2060 0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327, | |
2061 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, | |
2062 0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347, | |
2063 0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107, | |
2064 0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355, | |
2065 0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120, | |
2066 0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363, | |
2067 0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130, | |
2068 0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371, | |
2069 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, | |
2070 0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377 | |
2071 }; | |
2072 | |
2073 /* | |
2074 * Convert a buffer worth of characters from EBCDIC to ASCII. Only useful if | |
2075 * wanting 7-bit ASCII characters out the other end. | |
2076 */ | |
2077 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2078 ebcdic2ascii(char_u *buffer, int len) |
7 | 2079 { |
2080 int i; | |
2081 | |
2082 for (i = 0; i < len; i++) | |
2083 buffer[i] = ebcdic2ascii_tab[buffer[i]]; | |
2084 } | |
2085 #endif |