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