blob: 9c68d4af75920a3c7e6443df77d8accf9507ec39 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar13505972019-01-24 15:04:48 +010012#if defined(HAVE_WCHAR_H)
Bram Moolenaarc667da52019-11-30 20:52:27 +010013# include <wchar.h> // for towupper() and towlower()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#endif
15
Milly5e7a6a42024-10-22 22:27:19 +020016static int parse_isopt(char_u *var, buf_T *buf, int only_check);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +010017static int win_nolbr_chartabsize(chartabsize_T *cts, int *headp);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010018static unsigned nr2hex(unsigned c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
20static int chartab_initialized = FALSE;
21
Bram Moolenaarc667da52019-11-30 20:52:27 +010022// b_chartab[] is an array of 32 bytes, each bit representing one of the
23// characters 0-255.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define SET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] |= (1 << ((c) & 0x7))
25#define RESET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] &= ~(1 << ((c) & 0x7))
26#define GET_CHARTAB(buf, c) ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7)))
27
Bram Moolenaarc667da52019-11-30 20:52:27 +010028// table used below, see init_chartab() for an explanation
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010029static char_u g_chartab[256];
30
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010032 * Flags for g_chartab[].
33 */
Bram Moolenaarc667da52019-11-30 20:52:27 +010034#define CT_CELL_MASK 0x07 // mask: nr of display cells (1, 2 or 4)
35#define CT_PRINT_CHAR 0x10 // flag: set for printable chars
36#define CT_ID_CHAR 0x20 // flag: set for ID chars
37#define CT_FNAME_CHAR 0x40 // flag: set for file name chars
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010038
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static int in_win_border(win_T *wp, colnr_T vcol);
40
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010041/*
42 * Fill g_chartab[]. Also fills curbuf->b_chartab[] with flags for keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 * characters for current buffer.
44 *
45 * Depends on the option settings 'iskeyword', 'isident', 'isfname',
46 * 'isprint' and 'encoding'.
47 *
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010048 * The index in g_chartab[] depends on 'encoding':
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 * - For non-multi-byte index with the byte (same as the character).
50 * - For DBCS index with the first byte.
51 * - For UTF-8 index with the character (when first byte is up to 0x80 it is
52 * the same as the character, if the first byte is 0x80 and above it depends
53 * on further bytes).
54 *
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010055 * The contents of g_chartab[]:
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 * - The lower two bits, masked by CT_CELL_MASK, give the number of display
57 * cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80.
58 * - CT_PRINT_CHAR bit is set when the character is printable (no need to
59 * translate the character before displaying it). Note that only DBCS
60 * characters can have 2 display cells and still be printable.
61 * - CT_FNAME_CHAR bit is set when the character can be in a file name.
62 * - CT_ID_CHAR bit is set when the character can be in an identifier.
63 *
64 * Return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has an
65 * error, OK otherwise.
66 */
67 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010068init_chartab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000069{
70 return buf_init_chartab(curbuf, TRUE);
71}
72
73 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010074buf_init_chartab(
75 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +010076 int global) // FALSE: only set buf->b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 char_u *p;
80 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
82 if (global)
83 {
84 /*
85 * Set the default size for printable characters:
86 * From <Space> to '~' is 1 (printable), others are 2 (not printable).
87 * This also inits all 'isident' and 'isfname' flags to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
89 c = 0;
90 while (c < ' ')
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010091 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 while (c <= '~')
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010093 g_chartab[c++] = 1 + CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 while (c < 256)
95 {
Bram Moolenaarc667da52019-11-30 20:52:27 +010096 // UTF-8: bytes 0xa0 - 0xff are printable (latin1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 if (enc_utf8 && c >= 0xa0)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010098 g_chartab[c++] = CT_PRINT_CHAR + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +010099 // euc-jp characters starting with 0x8e are single width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100101 g_chartab[c++] = CT_PRINT_CHAR + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100102 // other double-byte chars can be printable AND double-width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100104 g_chartab[c++] = CT_PRINT_CHAR + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 else
Bram Moolenaarc667da52019-11-30 20:52:27 +0100106 // the rest is unprintable by default
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100107 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 }
109
Bram Moolenaarc667da52019-11-30 20:52:27 +0100110 // Assume that every multi-byte char is a filename character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 for (c = 1; c < 256; ++c)
112 if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1)
113 || (enc_dbcs == DBCS_JPNU && c == 0x8e)
114 || (enc_utf8 && c >= 0xa0))
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100115 g_chartab[c] |= CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 }
117
118 /*
119 * Init word char flags all to FALSE
120 */
Bram Moolenaara80faa82020-04-12 19:37:17 +0200121 CLEAR_FIELD(buf->b_chartab);
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000122 if (enc_dbcs != 0)
123 for (c = 0; c < 256; ++c)
124 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100125 // double-byte characters are probably word characters
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000126 if (MB_BYTE2LEN(c) == 2)
127 SET_CHARTAB(buf, c);
128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 /*
131 * In lisp mode the '-' character is included in keywords.
132 */
133 if (buf->b_p_lisp)
134 SET_CHARTAB(buf, '-');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
Milly5e7a6a42024-10-22 22:27:19 +0200136 // Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint' options.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 for (i = global ? 0 : 3; i <= 3; ++i)
138 {
139 if (i == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100140 p = p_isi; // first round: 'isident'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 else if (i == 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100142 p = p_isp; // second round: 'isprint'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 else if (i == 2)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100144 p = p_isf; // third round: 'isfname'
145 else // i == 3
146 p = buf->b_p_isk; // fourth round: 'iskeyword'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Milly5e7a6a42024-10-22 22:27:19 +0200148 if (parse_isopt(p, buf, FALSE) == FAIL)
149 return FAIL;
150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Milly5e7a6a42024-10-22 22:27:19 +0200152 chartab_initialized = TRUE;
153 return OK;
154}
155
156/**
157 * Checks the format for the option settings 'iskeyword', 'isident', 'isfname'
158 * or 'isprint'.
159 * Returns FAIL if has an error, OK otherwise.
160 */
161 int
162check_isopt(char_u *var)
163{
164 return parse_isopt(var, NULL, TRUE);
165}
166
167 static int
168parse_isopt(
169 char_u *var,
170 buf_T *buf,
171 int only_check) // FALSE: refill g_chartab[]
172{
173 char_u *p = var;
174 int c;
175 int c2;
176 int tilde;
177 int do_isalpha;
178 int trail_comma;
179
180 // Parses the 'isident', 'iskeyword', 'isfname' and 'isprint' options.
181 // Each option is a list of characters, character numbers or ranges,
182 // separated by commas, e.g.: "200-210,x,#-178,-"
183 while (*p)
184 {
185 tilde = FALSE;
186 do_isalpha = FALSE;
187 if (*p == '^' && p[1] != NUL)
188 {
189 tilde = TRUE;
190 ++p;
191 }
192 if (VIM_ISDIGIT(*p))
193 c = getdigits(&p);
194 else if (has_mbyte)
195 c = mb_ptr2char_adv(&p);
196 else
197 c = *p++;
198 c2 = -1;
199 if (*p == '-' && p[1] != NUL)
200 {
201 ++p;
202 if (VIM_ISDIGIT(*p))
203 c2 = getdigits(&p);
204 else if (has_mbyte)
205 c2 = mb_ptr2char_adv(&p);
206 else
207 c2 = *p++;
208 }
209 if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256
210 || !(*p == NUL || *p == ','))
211 return FAIL;
212
213 trail_comma = *p == ',';
214 p = skip_to_option_part(p);
215 if (trail_comma && *p == NUL)
216 // Trailing comma is not allowed.
217 return FAIL;
218
219 if (only_check)
220 continue;
221
222 if (c2 == -1) // not a range
223 {
224 /*
225 * A single '@' (not "@-@"):
226 * Decide on letters being ID/printable/keyword chars with
227 * standard function isalpha(). This takes care of locale for
228 * single-byte characters).
229 */
230 if (c == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {
Milly5e7a6a42024-10-22 22:27:19 +0200232 do_isalpha = TRUE;
233 c = 1;
234 c2 = 255;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 }
Milly5e7a6a42024-10-22 22:27:19 +0200236 else
237 c2 = c;
238 }
239
240 while (c <= c2)
241 {
242 // Use the MB_ functions here, because isalpha() doesn't
243 // work properly when 'encoding' is "latin1" and the locale is
244 // "C".
245 if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {
Milly5e7a6a42024-10-22 22:27:19 +0200247 if (var == p_isi) // (re)set ID flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {
Milly5e7a6a42024-10-22 22:27:19 +0200249 if (tilde)
250 g_chartab[c] &= ~CT_ID_CHAR;
251 else
252 g_chartab[c] |= CT_ID_CHAR;
253 }
254 else if (var == p_isp) // (re)set printable
255 {
256 if ((c < ' ' || c > '~'
257 // For double-byte we keep the cell width, so
258 // that we can detect it from the first byte.
259 ) && !(enc_dbcs && MB_BYTE2LEN(c) == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 {
261 if (tilde)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
Milly5e7a6a42024-10-22 22:27:19 +0200263 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK)
264 + ((dy_flags & DY_UHEX) ? 4 : 2);
265 g_chartab[c] &= ~CT_PRINT_CHAR;
266 }
267 else
268 {
269 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) + 1;
270 g_chartab[c] |= CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 }
272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 }
Milly5e7a6a42024-10-22 22:27:19 +0200274 else if (var == p_isf) // (re)set fname flag
275 {
276 if (tilde)
277 g_chartab[c] &= ~CT_FNAME_CHAR;
278 else
279 g_chartab[c] |= CT_FNAME_CHAR;
280 }
281 else // var == p_isk || var == buf->b_p_isk
282 // (re)set keyword flag
283 {
284 if (tilde)
285 RESET_CHARTAB(buf, c);
286 else
287 SET_CHARTAB(buf, c);
288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 }
Milly5e7a6a42024-10-22 22:27:19 +0200290 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292 }
Milly5e7a6a42024-10-22 22:27:19 +0200293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 return OK;
295}
296
297/*
298 * Translate any special characters in buf[bufsize] in-place.
299 * The result is a string with only printable characters, but if there is not
300 * enough room, not all characters will be translated.
301 */
302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100303trans_characters(
304 char_u *buf,
305 int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100307 int len; // length of string needing translation
308 int room; // room in buffer after string
309 char_u *trs; // translated character
310 int trs_len; // length of trs[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312 len = (int)STRLEN(buf);
313 room = bufsize - len;
314 while (*buf != 0)
315 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100316 // Assume a multi-byte character doesn't need translation.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000317 if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 len -= trs_len;
319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 {
321 trs = transchar_byte(*buf);
322 trs_len = (int)STRLEN(trs);
323 if (trs_len > 1)
324 {
325 room -= trs_len - 1;
326 if (room <= 0)
327 return;
328 mch_memmove(buf + trs_len, buf + 1, (size_t)len);
329 }
330 mch_memmove(buf, trs, (size_t)trs_len);
331 --len;
332 }
333 buf += trs_len;
334 }
335}
336
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337/*
338 * Translate a string into allocated memory, replacing special chars with
339 * printable chars. Returns NULL when out of memory.
340 */
341 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100342transstr(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
344 char_u *res;
345 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 int l, len, c;
347 char_u hexbuf[11];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (has_mbyte)
350 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100351 // Compute the length of the result, taking account of unprintable
352 // multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 len = 0;
354 p = s;
355 while (*p != NUL)
356 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000357 if ((l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
359 c = (*mb_ptr2char)(p);
360 p += l;
361 if (vim_isprintc(c))
362 len += l;
363 else
364 {
365 transchar_hex(hexbuf, c);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000366 len += (int)STRLEN(hexbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 }
368 }
369 else
370 {
371 l = byte2cells(*p++);
372 if (l > 0)
373 len += l;
374 else
Bram Moolenaarc667da52019-11-30 20:52:27 +0100375 len += 4; // illegal byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
377 }
Bram Moolenaar964b3742019-05-24 18:54:09 +0200378 res = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 }
380 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200381 res = alloc(vim_strsize(s) + 1);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000382
383 if (res == NULL)
384 return NULL;
385
386 *res = NUL;
387 p = s;
388 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000390 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000392 c = (*mb_ptr2char)(p);
393 if (vim_isprintc(c))
394 STRNCAT(res, p, l); // append printable multi-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000396 transchar_hex(res + STRLEN(res), c);
397 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000399 else
400 STRCAT(res, transchar_byte(*p++));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 }
402 return res;
403}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
Bram Moolenaar217ad922005-03-20 22:37:15 +0000406 * Convert the string "str[orglen]" to do ignore-case comparing. Uses the
407 * current locale.
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000408 * When "buf" is NULL returns an allocated string (NULL for out-of-memory).
409 * Otherwise puts the result in "buf[buflen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 */
411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100412str_foldcase(
413 char_u *str,
414 int orglen,
415 char_u *buf,
416 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
418 garray_T ga;
419 int i;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000420 int len = orglen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422#define GA_CHAR(i) ((char_u *)ga.ga_data)[i]
kylo252ae6f1d82022-02-16 19:24:07 +0000423#define GA_PTR(i) ((char_u *)ga.ga_data + (i))
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000424#define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i])
kylo252ae6f1d82022-02-16 19:24:07 +0000425#define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + (i))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
Bram Moolenaarc667da52019-11-30 20:52:27 +0100427 // Copy "str" into "buf" or allocated memory, unmodified.
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000428 if (buf == NULL)
429 {
430 ga_init2(&ga, 1, 10);
431 if (ga_grow(&ga, len + 1) == FAIL)
432 return NULL;
433 mch_memmove(ga.ga_data, str, (size_t)len);
434 ga.ga_len = len;
435 }
436 else
437 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100438 if (len >= buflen) // Ugly!
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000439 len = buflen - 1;
440 mch_memmove(buf, str, (size_t)len);
441 }
442 if (buf == NULL)
443 GA_CHAR(len) = NUL;
444 else
445 buf[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaarc667da52019-11-30 20:52:27 +0100447 // Make each character lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 i = 0;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000449 while (STR_CHAR(i) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000451 if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 {
453 if (enc_utf8)
454 {
Bram Moolenaarb9839212008-06-28 11:03:50 +0000455 int c = utf_ptr2char(STR_PTR(i));
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100456 int olen = utf_ptr2len(STR_PTR(i));
Bram Moolenaarb9839212008-06-28 11:03:50 +0000457 int lc = utf_tolower(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458
Bram Moolenaarc667da52019-11-30 20:52:27 +0100459 // Only replace the character when it is not an invalid
460 // sequence (ASCII character or more than one byte) and
461 // utf_tolower() doesn't return the original character.
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100462 if ((c < 0x80 || olen > 1) && c != lc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100464 int nlen = utf_char2len(lc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
Bram Moolenaarc667da52019-11-30 20:52:27 +0100466 // If the byte length changes need to shift the following
467 // characters forward or backward.
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100468 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100470 if (nlen > olen)
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000471 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100472 if (buf == NULL
473 ? ga_grow(&ga, nlen - olen + 1) == FAIL
474 : len + nlen - olen >= buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100476 // out of memory, keep old char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 lc = c;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100478 nlen = olen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000480 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100481 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000483 if (buf == NULL)
484 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100485 STRMOVE(GA_PTR(i) + nlen, GA_PTR(i) + olen);
486 ga.ga_len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000487 }
488 else
489 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100490 STRMOVE(buf + i + nlen, buf + i + olen);
491 len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 }
494 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000495 (void)utf_char2bytes(lc, STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 }
497 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100498 // skip to next multi-byte char
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000499 i += (*mb_ptr2len)(STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 }
501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000503 if (buf == NULL)
504 GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i));
505 else
506 buf[i] = TOLOWER_LOC(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 ++i;
508 }
509 }
510
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000511 if (buf == NULL)
512 return (char_u *)ga.ga_data;
513 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516/*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100517 * Catch 22: g_chartab[] can't be initialized before the options are
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * initialized, and initializing options may cause transchar() to be called!
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100519 * When chartab_initialized == FALSE don't use g_chartab[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 * Does NOT work for multi-byte characters, c must be <= 255.
521 * Also doesn't work for the first byte of a multi-byte, "c" must be a
522 * character!
523 */
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200524static char_u transchar_charbuf[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100527transchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200529 return transchar_buf(curbuf, c);
530}
531
532 char_u *
533transchar_buf(buf_T *buf, int c)
534{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 int i;
536
537 i = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100538 if (IS_SPECIAL(c)) // special key code, display as ~@ char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200540 transchar_charbuf[0] = '~';
541 transchar_charbuf[1] = '@';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 i = 2;
543 c = K_SECOND(c);
544 }
545
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000546 if ((!chartab_initialized && ((c >= ' ' && c <= '~')))
547 || (c < 256 && vim_isprintc_strict(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100549 // printable character
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200550 transchar_charbuf[i] = c;
551 transchar_charbuf[i + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553 else
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200554 transchar_nonprint(buf, transchar_charbuf + i, c);
555 return transchar_charbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556}
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558/*
559 * Like transchar(), but called with a byte instead of a character. Checks
cero19881d87e112023-02-16 15:03:12 +0000560 * for an illegal UTF-8 byte. Uses 'fileformat' of the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 */
562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100563transchar_byte(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
cero19881d87e112023-02-16 15:03:12 +0000565 return transchar_byte_buf(curbuf, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
568/*
cero19881d87e112023-02-16 15:03:12 +0000569 * Like transchar_buf(), but called with a byte instead of a character. Checks
570 * for an illegal UTF-8 byte. Uses 'fileformat' of "buf", unless it is NULL.
571 */
572 char_u *
573transchar_byte_buf(buf_T *buf, int c)
574{
575 if (enc_utf8 && c >= 0x80)
576 {
577 transchar_nonprint(buf, transchar_charbuf, c);
578 return transchar_charbuf;
579 }
580 return transchar_buf(buf, c);
581}
582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 * Convert non-printable character to two or more printable characters in
Bram Moolenaara9a6b032023-02-05 18:00:42 +0000584 * "charbuf[]". "charbuf" needs to be able to hold five bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 * Does NOT work for multi-byte characters, c must be <= 255.
586 */
587 void
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200588transchar_nonprint(buf_T *buf, char_u *charbuf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 if (c == NL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100591 c = NUL; // we use newline in place of a NUL
cero19881d87e112023-02-16 15:03:12 +0000592 else if (buf != NULL && c == CAR && get_fileformat(buf) == EOL_MAC)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100593 c = NL; // we use CR in place of NL in this case
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
Bram Moolenaarc667da52019-11-30 20:52:27 +0100595 if (dy_flags & DY_UHEX) // 'display' has "uhex"
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200596 transchar_hex(charbuf, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaarc667da52019-11-30 20:52:27 +0100598 else if (c <= 0x7f) // 0x00 - 0x1f and 0x7f
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200600 charbuf[0] = '^';
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200601 charbuf[1] = c ^ 0x40; // DEL displayed as ^?
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200602 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +0000604 else if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200606 transchar_hex(charbuf, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100608 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) // 0xa0 - 0xfe
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200610 charbuf[0] = '|';
611 charbuf[1] = c - 0x80;
612 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100614 else // 0x80 - 0x9f and 0xff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200616 charbuf[0] = '~';
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200617 charbuf[1] = (c - 0x80) ^ 0x40; // 0xff displayed as ~?
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200618 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
620}
621
622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100623transchar_hex(char_u *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 int i = 0;
626
627 buf[0] = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 if (c > 255)
629 {
630 buf[++i] = nr2hex((unsigned)c >> 12);
631 buf[++i] = nr2hex((unsigned)c >> 8);
632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 buf[++i] = nr2hex((unsigned)c >> 4);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000634 buf[++i] = nr2hex((unsigned)c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 buf[++i] = '>';
636 buf[++i] = NUL;
637}
638
639/*
640 * Convert the lower 4 bits of byte "c" to its hex character.
641 * Lower case letters are used to avoid the confusion of <F1> being 0xf1 or
642 * function key 1.
643 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000644 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +0100645nr2hex(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
647 if ((c & 0xf) <= 9)
648 return (c & 0xf) + '0';
649 return (c & 0xf) - 10 + 'a';
650}
651
652/*
653 * Return number of display cells occupied by byte "b".
654 * Caller must make sure 0 <= b <= 255.
655 * For multi-byte mode "b" must be the first byte of a character.
656 * A TAB is counted as two cells: "^I".
657 * For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of
658 * cells depends on further bytes.
659 */
660 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100661byte2cells(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (enc_utf8 && b >= 0x80)
664 return 0;
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100665 return (g_chartab[b] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
668/*
669 * Return number of display cells occupied by character "c".
670 * "c" can be a special key (negative number) in which case 3 or 4 is returned.
671 * A TAB is counted as two cells: "^I" or four: "<09>".
672 */
673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100674char2cells(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 if (IS_SPECIAL(c))
677 return char2cells(K_SECOND(c)) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 if (c >= 0x80)
679 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100680 // UTF-8: above 0x80 need to check the value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (enc_utf8)
682 return utf_char2cells(c);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100683 // DBCS: double-byte means double-width, except for euc-jp with first
684 // byte 0x8e
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (enc_dbcs != 0 && c >= 0x100)
686 {
687 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
688 return 1;
689 return 2;
690 }
691 }
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100692 return (g_chartab[c & 0xff] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
695/*
696 * Return number of display cells occupied by character at "*p".
697 * A TAB is counted as two cells: "^I" or four: "<09>".
698 */
699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100700ptr2cells(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
Bram Moolenaar398649e2022-08-04 15:03:48 +0100702 if (!has_mbyte)
703 return byte2cells(*p);
704
Bram Moolenaarc667da52019-11-30 20:52:27 +0100705 // For UTF-8 we need to look at more bytes if the first byte is >= 0x80.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (enc_utf8 && *p >= 0x80)
707 return utf_ptr2cells(p);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100708 // For DBCS we can tell the cell count from the first byte.
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100709 return (g_chartab[*p] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710}
711
712/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100713 * Return the number of character cells string "s" will take on the screen,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 * counting TABs as two characters: "^I".
715 */
716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100717vim_strsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 return vim_strnsize(s, (int)MAXCOL);
720}
721
722/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100723 * Return the number of character cells string "s[len]" will take on the
724 * screen, counting TABs as two characters: "^I".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 */
726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100727vim_strnsize(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
729 int size = 0;
730
731 while (*s != NUL && --len >= 0)
Bram Moolenaar398649e2022-08-04 15:03:48 +0100732 {
733 int l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar398649e2022-08-04 15:03:48 +0100735 size += ptr2cells(s);
736 s += l;
737 len -= l - 1;
738 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 return size;
741}
742
743/*
744 * Return the number of characters 'c' will take on the screen, taking
745 * into account the size of a tab.
746 * Use a define to make it fast, this is used very often!!!
747 * Also see getvcol() below.
748 */
749
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200750#ifdef FEAT_VARTABS
751# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
kylo252ae6f1d82022-02-16 19:24:07 +0000752 if (*(p) == TAB && (!(wp)->w_p_list || (wp)->w_lcs_chars.tab1)) \
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200753 { \
754 return tabstop_padding(col, (buf)->b_p_ts, (buf)->b_p_vts_array); \
755 } \
756 else \
757 return ptr2cells(p);
758#else
759# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
Bram Moolenaareed9d462021-02-15 20:38:25 +0100760 if (*(p) == TAB && (!(wp)->w_p_list || wp->w_lcs_chars.tab1)) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 { \
762 int ts; \
763 ts = (buf)->b_p_ts; \
764 return (int)(ts - (col % ts)); \
765 } \
766 else \
767 return ptr2cells(p);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100771chartabsize(char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
773 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col)
774}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
zeertzjq61a6ac42024-09-07 11:23:54 +0200776#if defined(FEAT_LINEBREAK) || defined(PROTO)
777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100778win_chartabsize(win_T *wp, char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
780 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col)
781}
782#endif
783
784/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100785 * Return the number of characters the string "s" will take on the screen,
Bram Moolenaardc536092010-07-18 15:45:49 +0200786 * taking into account the size of a tab.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100787 * Does not handle text properties, since "s" is not a buffer line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 */
789 int
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100790linetabsize_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
Bram Moolenaardc536092010-07-18 15:45:49 +0200792 return linetabsize_col(0, s);
793}
794
795/*
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100796 * Like linetabsize_str(), but "s" starts at column "startcol".
Bram Moolenaardc536092010-07-18 15:45:49 +0200797 */
798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100799linetabsize_col(int startcol, char_u *s)
Bram Moolenaardc536092010-07-18 15:45:49 +0200800{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100801 chartabsize_T cts;
Christian Brabandteff20eb2024-05-15 21:35:36 +0200802 vimlong_T vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100804 init_chartabsize_arg(&cts, curwin, 0, startcol, s, s);
Christian Brabandteff20eb2024-05-15 21:35:36 +0200805 vcol = cts.cts_vcol;
806
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100807 while (*cts.cts_ptr != NUL)
Christian Brabandteff20eb2024-05-15 21:35:36 +0200808 {
809 vcol += lbr_chartabsize_adv(&cts);
810 if (vcol > MAXCOL)
811 {
812 cts.cts_vcol = MAXCOL;
813 break;
814 }
815 else
816 cts.cts_vcol = (int)vcol;
817 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100818 clear_chartabsize_arg(&cts);
819 return (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821
822/*
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100823 * Like linetabsize_str(), but for a given window instead of the current one.
zeertzjq2c47ab82025-02-13 20:34:34 +0100824 * Doesn't count the size of 'listchars' "eol".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 */
826 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100827win_linetabsize(win_T *wp, linenr_T lnum, char_u *line, colnr_T len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100829 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100831 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
Bram Moolenaar49a90792022-08-09 18:25:23 +0100832 win_linetabsize_cts(&cts, len);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100833 clear_chartabsize_arg(&cts);
834 return (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835}
836
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100837/*
838 * Return the number of cells line "lnum" of window "wp" will take on the
839 * screen, taking into account the size of a tab and text properties.
zeertzjq2c47ab82025-02-13 20:34:34 +0100840 * Doesn't count the size of 'listchars' "eol".
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100841 */
zeertzjq2c47ab82025-02-13 20:34:34 +0100842 int
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100843linetabsize(win_T *wp, linenr_T lnum)
844{
845 return win_linetabsize(wp, lnum,
846 ml_get_buf(wp->w_buffer, lnum, FALSE), (colnr_T)MAXCOL);
847}
848
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +0100849/*
zeertzjq2c47ab82025-02-13 20:34:34 +0100850 * Like linetabsize(), but counts the size of 'listchars' "eol".
851 */
852 int
853linetabsize_eol(win_T *wp, linenr_T lnum)
854{
855 return linetabsize(wp, lnum)
856 + ((wp->w_p_list && wp->w_lcs_chars.eol != NUL) ? 1 : 0);
857}
858
859/*
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +0100860 * Like linetabsize(), but excludes 'above'/'after'/'right'/'below' aligned
861 * virtual text, while keeping inline virtual text.
862 */
863 int
864linetabsize_no_outer(win_T *wp, linenr_T lnum)
865{
866#ifndef FEAT_PROP_POPUP
867 return linetabsize(wp, lnum);
868#else
869 chartabsize_T cts;
870 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
871
872 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
873
874 if (cts.cts_text_prop_count)
875 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200876 int write_idx = 0;
877 for (int read_idx = 0; read_idx < cts.cts_text_prop_count; read_idx++)
878 {
879 textprop_T *tp = &cts.cts_text_props[read_idx];
880 if (tp->tp_col != MAXCOL)
881 {
882 if (read_idx != write_idx)
883 cts.cts_text_props[write_idx] = *tp;
884 write_idx++;
885 }
886 }
887 cts.cts_text_prop_count = write_idx;
888 if (cts.cts_text_prop_count == 0)
889 VIM_CLEAR(cts.cts_text_props);
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +0100890 }
891
892 win_linetabsize_cts(&cts, (colnr_T)MAXCOL);
893 clear_chartabsize_arg(&cts);
894 return (int)cts.cts_vcol;
895#endif
896}
897
Bram Moolenaar49a90792022-08-09 18:25:23 +0100898 void
899win_linetabsize_cts(chartabsize_T *cts, colnr_T len)
900{
Christian Brabandteff20eb2024-05-15 21:35:36 +0200901 vimlong_T vcol = cts->cts_vcol;
Bram Moolenaar49a90792022-08-09 18:25:23 +0100902#ifdef FEAT_PROP_POPUP
903 cts->cts_with_trailing = len == MAXCOL;
904#endif
905 for ( ; *cts->cts_ptr != NUL && (len == MAXCOL || cts->cts_ptr < cts->cts_line + len);
906 MB_PTR_ADV(cts->cts_ptr))
Christian Brabandteff20eb2024-05-15 21:35:36 +0200907 {
908 vcol += win_lbr_chartabsize(cts, NULL);
909 if (vcol > MAXCOL)
910 {
911 cts->cts_vcol = MAXCOL;
912 break;
913 }
914 else
915 cts->cts_vcol = (int)vcol;
916 }
Bram Moolenaar49a90792022-08-09 18:25:23 +0100917#ifdef FEAT_PROP_POPUP
Ibbya6ab5e62023-08-20 20:24:18 +0200918 // check for a virtual text at the end of a line or on an empty line
zeertzjqe3daa062023-08-27 19:11:46 +0200919 if (len == MAXCOL && cts->cts_has_prop_with_text && *cts->cts_ptr == NUL)
Bram Moolenaar49a90792022-08-09 18:25:23 +0100920 {
921 (void)win_lbr_chartabsize(cts, NULL);
Christian Brabandteff20eb2024-05-15 21:35:36 +0200922 vcol += cts->cts_cur_text_width;
Bram Moolenaar55a27d82023-02-12 18:03:57 +0000923 // when properties are above or below the empty line must also be
924 // counted
Ibbya6ab5e62023-08-20 20:24:18 +0200925 if (cts->cts_ptr == cts->cts_line && cts->cts_prop_lines > 0)
Christian Brabandteff20eb2024-05-15 21:35:36 +0200926 ++vcol;
927 cts->cts_vcol = vcol > MAXCOL ? MAXCOL : (int)vcol;
Bram Moolenaar49a90792022-08-09 18:25:23 +0100928 }
929#endif
930}
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932/*
Bram Moolenaar81695252004-12-29 20:58:21 +0000933 * Return TRUE if 'c' is a normal identifier character:
934 * Letters and characters from the 'isident' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937vim_isIDc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100939 return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940}
941
942/*
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +0200943 * Like vim_isIDc() but not using the 'isident' option: letters, numbers and
944 * underscore.
945 */
946 int
947vim_isNormalIDc(int c)
948{
949 return ASCII_ISALNUM(c) || c == '_';
950}
951
952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 * return TRUE if 'c' is a keyword character: Letters and characters from
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100954 * 'iskeyword' option for the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * For multi-byte characters mb_get_class() is used (builtin rules).
956 */
957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100958vim_iswordc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100960 return vim_iswordc_buf(c, curbuf);
961}
962
963 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100964vim_iswordc_buf(int c, buf_T *buf)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100965{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (c >= 0x100)
967 {
968 if (enc_dbcs != 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000969 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (enc_utf8)
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100971 return utf_class_buf(c, buf) >= 2;
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100972 return FALSE;
973 }
974 return (c > 0 && GET_CHARTAB(buf, c) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975}
976
977/*
978 * Just like vim_iswordc() but uses a pointer to the (multi-byte) character.
979 */
980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100981vim_iswordp(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100983 return vim_iswordp_buf(p, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100987vim_iswordp_buf(char_u *p, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100989 int c = *p;
990
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100991 if (has_mbyte && MB_BYTE2LEN(c) > 1)
992 c = (*mb_ptr2char)(p);
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100993 return vim_iswordc_buf(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996/*
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +0100997 * Return TRUE if 'c' is a valid file-name character as specified with the
998 * 'isfname' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * Assume characters above 0x100 are valid (multi-byte).
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +01001000 * To be used for commands like "gf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 */
1002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001003vim_isfilec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001005 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
1007
Dominique Pellee764d1b2023-03-12 21:20:59 +00001008#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009/*
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +01001010 * Return TRUE if 'c' is a valid file-name character, including characters left
1011 * out of 'isfname' to make "gf" work, such as comma, space, '@', etc.
1012 */
1013 int
1014vim_is_fname_char(int c)
1015{
1016 return vim_isfilec(c) || c == ',' || c == ' ' || c == '@';
1017}
Dominique Pellee764d1b2023-03-12 21:20:59 +00001018#endif
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +01001019
1020/*
Bram Moolenaardd87969c2007-08-21 13:07:12 +00001021 * return TRUE if 'c' is a valid file-name character or a wildcard character
1022 * Assume characters above 0x100 are valid (multi-byte).
1023 * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]")
1024 * returns false.
1025 */
1026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001027vim_isfilec_or_wc(int c)
Bram Moolenaardd87969c2007-08-21 13:07:12 +00001028{
1029 char_u buf[2];
1030
1031 buf[0] = (char_u)c;
1032 buf[1] = NUL;
1033 return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf);
1034}
1035
1036/*
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001037 * Return TRUE if 'c' is a printable character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * Assume characters above 0x100 are printable (multi-byte), except for
1039 * Unicode.
1040 */
1041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001042vim_isprintc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (enc_utf8 && c >= 0x100)
1045 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001046 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047}
1048
1049/*
1050 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head
1051 * byte of a double-byte character.
1052 */
1053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001054vim_isprintc_strict(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1)
1057 return FALSE;
1058 if (enc_utf8 && c >= 0x100)
1059 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001060 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062
1063/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001064 * Prepare the structure passed to chartabsize functions.
1065 * "line" is the start of the line, "ptr" is the first relevant character.
1066 * When "lnum" is zero do not use text properties that insert text.
1067 */
1068 void
1069init_chartabsize_arg(
1070 chartabsize_T *cts,
1071 win_T *wp,
Bram Moolenaare5a420f2022-09-07 21:46:56 +01001072 linenr_T lnum UNUSED,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001073 colnr_T col,
1074 char_u *line,
1075 char_u *ptr)
1076{
Bram Moolenaar3f79b612022-08-01 12:06:40 +01001077 CLEAR_POINTER(cts);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001078 cts->cts_win = wp;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001079 cts->cts_vcol = col;
1080 cts->cts_line = line;
1081 cts->cts_ptr = ptr;
zeertzjq5b0722b2024-01-17 20:42:53 +01001082#ifdef FEAT_LINEBREAK
1083 cts->cts_bri_size = -1;
1084#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001085#ifdef FEAT_PROP_POPUP
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001086 if (lnum > 0 && !ignore_text_props)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001087 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001088 char_u *prop_start;
1089 int count;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001090
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001091 count = get_text_props(wp->w_buffer, lnum, &prop_start, FALSE);
1092 cts->cts_text_prop_count = count;
1093 if (count > 0)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001094 {
1095 // Make a copy of the properties, so that they are properly
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001096 // aligned. Make it twice as long for the sorting below.
1097 cts->cts_text_props = ALLOC_MULT(textprop_T, count * 2);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001098 if (cts->cts_text_props == NULL)
1099 cts->cts_text_prop_count = 0;
1100 else
1101 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001102 int i;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001103
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001104 mch_memmove(cts->cts_text_props + count, prop_start,
1105 count * sizeof(textprop_T));
1106 for (i = 0; i < count; ++i)
Bram Moolenaar89469d12022-12-02 20:46:26 +00001107 {
1108 textprop_T *tp = cts->cts_text_props + i + count;
1109 if (tp->tp_id < 0
1110 && text_prop_type_valid(wp->w_buffer, tp))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001111 {
1112 cts->cts_has_prop_with_text = TRUE;
1113 break;
1114 }
Bram Moolenaar89469d12022-12-02 20:46:26 +00001115 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001116 if (!cts->cts_has_prop_with_text)
1117 {
1118 // won't use the text properties, free them
Bram Moolenaar3f79b612022-08-01 12:06:40 +01001119 VIM_CLEAR(cts->cts_text_props);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001120 cts->cts_text_prop_count = 0;
1121 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001122 else
1123 {
1124 int *text_prop_idxs;
1125
1126 // Need to sort the array to get any truncation right.
1127 // Do the sorting in the second part of the array, then
1128 // move the sorted props to the first part of the array.
1129 text_prop_idxs = ALLOC_MULT(int, count);
1130 if (text_prop_idxs != NULL)
1131 {
1132 for (i = 0; i < count; ++i)
1133 text_prop_idxs[i] = i + count;
1134 sort_text_props(curbuf, cts->cts_text_props,
1135 text_prop_idxs, count);
1136 // Here we want the reverse order.
1137 for (i = 0; i < count; ++i)
1138 cts->cts_text_props[count - i - 1] =
1139 cts->cts_text_props[text_prop_idxs[i]];
1140 vim_free(text_prop_idxs);
1141 }
1142 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001143 }
1144 }
1145 }
1146#endif
1147}
1148
1149/*
1150 * Free any allocated item in "cts".
1151 */
1152 void
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001153clear_chartabsize_arg(chartabsize_T *cts UNUSED)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001154{
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001155#ifdef FEAT_PROP_POPUP
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001156 if (cts->cts_text_prop_count > 0)
Bram Moolenaar34a1f772022-07-26 11:20:48 +01001157 {
Bram Moolenaar3f79b612022-08-01 12:06:40 +01001158 VIM_CLEAR(cts->cts_text_props);
1159 cts->cts_text_prop_count = 0;
Bram Moolenaar34a1f772022-07-26 11:20:48 +01001160 }
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001161#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001162}
1163
1164/*
1165 * Like chartabsize(), but also check for line breaks on the screen and text
1166 * properties that insert text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
1168 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001169lbr_chartabsize(chartabsize_T *cts)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001171#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
1172 if (1
1173# ifdef FEAT_LINEBREAK
1174 && !curwin->w_p_lbr && *get_showbreak_value(curwin) == NUL
1175 && !curwin->w_p_bri
1176# endif
1177# ifdef FEAT_PROP_POPUP
1178 && !cts->cts_has_prop_with_text
1179#endif
1180 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (curwin->w_p_wrap)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001184 return win_nolbr_chartabsize(cts, NULL);
1185 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, cts->cts_ptr, cts->cts_vcol)
1186#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001188 return win_lbr_chartabsize(cts, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#endif
1190}
1191
1192/*
1193 * Call lbr_chartabsize() and advance the pointer.
1194 */
1195 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001196lbr_chartabsize_adv(chartabsize_T *cts)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 int retval;
1199
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001200 retval = lbr_chartabsize(cts);
1201 MB_PTR_ADV(cts->cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 return retval;
1203}
1204
1205/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001206 * Return the screen size of the character indicated by "cts".
1207 * "cts->cts_cur_text_width" is set to the extra size for a text property that
1208 * inserts text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 * This function is used very often, keep it fast!!!!
1210 *
zeertzjqb557f482023-08-22 22:07:34 +02001211 * If "headp" not NULL, set "*headp" to the size of 'showbreak'/'breakindent'
1212 * included in the return value.
1213 * When "cts->cts_max_head_vcol" is positive, only count in "*headp" the size
1214 * of 'showbreak'/'breakindent' before "cts->cts_max_head_vcol".
1215 * When "cts->cts_max_head_vcol" is negative, only count in "*headp" the size
1216 * of 'showbreak'/'breakindent' before where cursor should be placed.
1217 *
1218 * Warning: "*headp" may not be set if it's 0, init to 0 before calling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001221win_lbr_chartabsize(
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001222 chartabsize_T *cts,
1223 int *headp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001225 win_T *wp = cts->cts_win;
Martin Tournoijba43e762022-10-13 22:12:15 +01001226#if defined(FEAT_PROP_POPUP) || defined(FEAT_LINEBREAK)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001227 char_u *line = cts->cts_line; // start of the line
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001228#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001229 char_u *s = cts->cts_ptr;
1230 colnr_T vcol = cts->cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 int size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 int mb_added = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001234 int n;
Bram Moolenaaree857022019-11-09 23:26:40 +01001235 char_u *sbr;
Bram Moolenaarcba69522022-08-06 21:03:53 +01001236 int no_sbr = FALSE;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001239#if defined(FEAT_PROP_POPUP)
1240 cts->cts_cur_text_width = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001241 cts->cts_first_char = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001242#endif
1243
1244#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001246 * No 'linebreak', 'showbreak', 'breakindent' and text properties that
1247 * insert text: return quickly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001249 if (1
1250# ifdef FEAT_LINEBREAK
1251 && !wp->w_p_lbr && !wp->w_p_bri && *get_showbreak_value(wp) == NUL
1252# endif
1253# ifdef FEAT_PROP_POPUP
1254 && !cts->cts_has_prop_with_text
1255# endif
1256 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257#endif
1258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (wp->w_p_wrap)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001260 return win_nolbr_chartabsize(cts, headp);
1261 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001264#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
zeertzjq11939512023-08-23 20:58:01 +02001265 int has_lcs_eol = wp->w_p_list && wp->w_lcs_chars.eol != NUL;
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001268 * First get the normal size, without 'linebreak' or text properties
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001270 size = win_chartabsize(wp, s, vcol);
Dylan Thacker-Smithda0c9132024-02-27 20:25:10 +01001271 if (*s == NUL)
1272 {
1273 // 1 cell for EOL list char (if present), as opposed to the two cell ^@
1274 // for a NUL character in the text.
1275 size = has_lcs_eol ? 1 : 0;
1276 }
zeertzjqac2d8812023-08-31 18:07:30 +02001277# ifdef FEAT_LINEBREAK
1278 int is_doublewidth = has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1;
1279# endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001280
1281# ifdef FEAT_PROP_POPUP
Bram Moolenaar49a90792022-08-09 18:25:23 +01001282 if (cts->cts_has_prop_with_text)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001283 {
Bram Moolenaare428fa02022-08-09 16:55:41 +01001284 int tab_size = size;
Bram Moolenaar49a90792022-08-09 18:25:23 +01001285 int charlen = *s == NUL ? 1 : mb_ptr2len(s);
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001286 int i;
1287 int col = (int)(s - line);
1288 garray_T *gap = &wp->w_buffer->b_textprop_text;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001289
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001290 // The "$" for 'list' mode will go between the EOL and
1291 // the text prop, account for that.
zeertzjq11939512023-08-23 20:58:01 +02001292 if (has_lcs_eol)
1293 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001294 ++vcol;
zeertzjq11939512023-08-23 20:58:01 +02001295 --size;
1296 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001297
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001298 for (i = 0; i < cts->cts_text_prop_count; ++i)
1299 {
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01001300 textprop_T *tp = cts->cts_text_props + i;
1301 int col_off = win_col_off(wp);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001302
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001303 // Watch out for the text being deleted. "cts_text_props" is a
1304 // copy, the text prop may actually have been removed from the line.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001305 if (tp->tp_id < 0
Bram Moolenaar25463612022-08-08 11:07:47 +01001306 && ((tp->tp_col - 1 >= col
Bram Moolenaare428fa02022-08-09 16:55:41 +01001307 && tp->tp_col - 1 < col + charlen)
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001308 || (tp->tp_col == MAXCOL
1309 && ((tp->tp_flags & TP_FLAG_ALIGN_ABOVE)
1310 ? col == 0
zeertzjqe3daa062023-08-27 19:11:46 +02001311 : s[0] == NUL && cts->cts_with_trailing)))
Bram Moolenaar4ce1f992022-12-19 13:31:06 +00001312 && -tp->tp_id - 1 < gap->ga_len)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001313 {
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001314 char_u *p = ((char_u **)gap->ga_data)[-tp->tp_id - 1];
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001315
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001316 if (p != NULL)
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001317 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001318 int cells;
Bram Moolenaar398649e2022-08-04 15:03:48 +01001319
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001320 if (tp->tp_col == MAXCOL)
1321 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001322 int n_extra = (int)STRLEN(p);
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001323
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01001324 cells = text_prop_position(wp, tp, vcol,
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01001325 (vcol + size) % (wp->w_width - col_off) + col_off,
Bram Moolenaar56a40fe2022-12-06 14:17:57 +00001326 &n_extra, &p, NULL, NULL, FALSE);
zeertzjqce53e3e2023-09-01 18:49:30 +02001327# ifdef FEAT_LINEBREAK
Bram Moolenaarcba69522022-08-06 21:03:53 +01001328 no_sbr = TRUE; // don't use 'showbreak' now
zeertzjqce53e3e2023-09-01 18:49:30 +02001329# endif
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001330 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001331 else
1332 cells = vim_strsize(p);
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001333 cts->cts_cur_text_width += cells;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001334 if (tp->tp_flags & TP_FLAG_ALIGN_ABOVE)
1335 cts->cts_first_char += cells;
zeertzjqce53e3e2023-09-01 18:49:30 +02001336 else
1337 size += cells;
Bram Moolenaar28c9f892022-08-14 13:28:55 +01001338 cts->cts_start_incl = tp->tp_flags & TP_FLAG_START_INCL;
Bram Moolenaare428fa02022-08-09 16:55:41 +01001339 if (*s == TAB)
1340 {
1341 // tab size changes because of the inserted text
1342 size -= tab_size;
1343 tab_size = win_chartabsize(wp, s, vcol + size);
1344 size += tab_size;
1345 }
Bram Moolenaar55a27d82023-02-12 18:03:57 +00001346 if (tp->tp_col == MAXCOL && (tp->tp_flags
1347 & (TP_FLAG_ALIGN_ABOVE | TP_FLAG_ALIGN_BELOW)))
1348 // count extra line for property above/below
1349 ++cts->cts_prop_lines;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001350 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001351 }
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01001352 if (tp->tp_col != MAXCOL && tp->tp_col - 1 > col)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001353 break;
1354 }
zeertzjq11939512023-08-23 20:58:01 +02001355 if (has_lcs_eol)
1356 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001357 --vcol;
zeertzjq11939512023-08-23 20:58:01 +02001358 ++size;
1359 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001360 }
1361# endif
1362
1363# ifdef FEAT_LINEBREAK
zeertzjqac2d8812023-08-31 18:07:30 +02001364 if (is_doublewidth && wp->w_p_wrap && in_win_border(wp, vcol + size - 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001366 ++size; // Count the ">" in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 mb_added = 1;
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
1370 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +02001371 * May have to add something for 'breakindent' and/or 'showbreak'
zeertzjq11939512023-08-23 20:58:01 +02001372 * string at the start of a screen line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 */
zeertzjqb557f482023-08-22 22:07:34 +02001374 int head = mb_added;
zeertzjq11939512023-08-23 20:58:01 +02001375 sbr = no_sbr ? empty_option : get_showbreak_value(wp);
1376 // When "size" is 0, no new screen line is started.
1377 if (size > 0 && wp->w_p_wrap && (*sbr != NUL || wp->w_p_bri))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 {
zeertzjqb557f482023-08-22 22:07:34 +02001379 int col_off_prev = win_col_off(wp);
1380 int width2 = wp->w_width - col_off_prev + win_col_off2(wp);
zeertzjqb557f482023-08-22 22:07:34 +02001381 colnr_T wcol = vcol + col_off_prev;
zeertzjqce53e3e2023-09-01 18:49:30 +02001382# ifdef FEAT_PROP_POPUP
zeertzjq6e55e852023-08-30 16:55:09 +02001383 wcol -= wp->w_virtcol_first_char;
zeertzjqce53e3e2023-09-01 18:49:30 +02001384# endif
zeertzjq1d3e0e82023-08-28 21:20:16 +02001385 colnr_T max_head_vcol = cts->cts_max_head_vcol;
1386 int added = 0;
1387
zeertzjqb557f482023-08-22 22:07:34 +02001388 // cells taken by 'showbreak'/'breakindent' before current char
1389 int head_prev = 0;
1390 if (wcol >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
zeertzjqb557f482023-08-22 22:07:34 +02001392 wcol -= wp->w_width;
1393 col_off_prev = wp->w_width - width2;
1394 if (wcol >= width2 && width2 > 0)
1395 wcol %= width2;
Bram Moolenaaree857022019-11-09 23:26:40 +01001396 if (*sbr != NUL)
zeertzjqb557f482023-08-22 22:07:34 +02001397 head_prev += vim_strsize(sbr);
1398 if (wp->w_p_bri)
zeertzjq5b0722b2024-01-17 20:42:53 +01001399 {
1400 if (cts->cts_bri_size < 0)
1401 cts->cts_bri_size = get_breakindent_win(wp, line);
1402 head_prev += cts->cts_bri_size;
1403 }
zeertzjqb557f482023-08-22 22:07:34 +02001404 if (wcol < head_prev)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001405 {
zeertzjq1d3e0e82023-08-28 21:20:16 +02001406 head_prev -= wcol;
1407 wcol += head_prev;
zeertzjqb557f482023-08-22 22:07:34 +02001408 added += head_prev;
1409 if (max_head_vcol <= 0 || vcol < max_head_vcol)
1410 head += head_prev;
1411 }
zeertzjq6e55e852023-08-30 16:55:09 +02001412 else
1413 head_prev = 0;
zeertzjq1d3e0e82023-08-28 21:20:16 +02001414 wcol += col_off_prev;
1415 }
zeertzjqb557f482023-08-22 22:07:34 +02001416
zeertzjq1d3e0e82023-08-28 21:20:16 +02001417 if (wcol + size > wp->w_width)
1418 {
zeertzjqb557f482023-08-22 22:07:34 +02001419 // cells taken by 'showbreak'/'breakindent' halfway current char
1420 int head_mid = 0;
1421 if (*sbr != NUL)
1422 head_mid += vim_strsize(sbr);
1423 if (wp->w_p_bri)
zeertzjq5b0722b2024-01-17 20:42:53 +01001424 {
1425 if (cts->cts_bri_size < 0)
1426 cts->cts_bri_size = get_breakindent_win(wp, line);
1427 head_mid += cts->cts_bri_size;
1428 }
zeertzjq5532d3b2024-03-28 10:04:25 +01001429 if (head_mid > 0)
zeertzjqb557f482023-08-22 22:07:34 +02001430 {
zeertzjq6a389722023-08-27 19:04:14 +02001431 // Calculate effective window width.
zeertzjq11939512023-08-23 20:58:01 +02001432 int prev_rem = wp->w_width - wcol;
1433 int width = width2 - head_mid;
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001434
zeertzjq11939512023-08-23 20:58:01 +02001435 if (width <= 0)
1436 width = 1;
zeertzjq6a389722023-08-27 19:04:14 +02001437 // Divide "size - prev_rem" by "width", rounding up.
zeertzjq11939512023-08-23 20:58:01 +02001438 int cnt = (size - prev_rem + width - 1) / width;
1439 added += cnt * head_mid;
zeertzjqb557f482023-08-22 22:07:34 +02001440
zeertzjq11939512023-08-23 20:58:01 +02001441 if (max_head_vcol == 0 || vcol + size + added < max_head_vcol)
1442 head += cnt * head_mid;
1443 else if (max_head_vcol > vcol + head_prev + prev_rem)
1444 head += (max_head_vcol - (vcol + head_prev + prev_rem)
zeertzjqb557f482023-08-22 22:07:34 +02001445 + width2 - 1) / width2 * head_mid;
zeertzjq11939512023-08-23 20:58:01 +02001446 else if (max_head_vcol < 0)
1447 {
zeertzjqb5d6b5c2024-07-18 21:13:31 +02001448 int off = mb_added;
1449# ifdef FEAT_PROP_POPUP
zeertzjq6e55e852023-08-30 16:55:09 +02001450 if (*s != NUL
zeertzjq11939512023-08-23 20:58:01 +02001451 && ((State & MODE_NORMAL) || cts->cts_start_incl))
1452 off += cts->cts_cur_text_width;
zeertzjqb5d6b5c2024-07-18 21:13:31 +02001453# endif
zeertzjq11939512023-08-23 20:58:01 +02001454 if (off >= prev_rem)
1455 head += (1 + (off - prev_rem) / width) * head_mid;
Bram Moolenaard574ea22015-01-14 19:35:14 +01001456 }
Bram Moolenaard574ea22015-01-14 19:35:14 +01001457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
zeertzjq1d3e0e82023-08-28 21:20:16 +02001459
1460 size += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
zeertzjq6e55e852023-08-30 16:55:09 +02001462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (headp != NULL)
zeertzjqb557f482023-08-22 22:07:34 +02001464 *headp = head;
zeertzjq6e55e852023-08-30 16:55:09 +02001465
zeertzjq703f9bc2024-01-25 21:27:13 +01001466 int need_lbr = FALSE;
zeertzjq6e55e852023-08-30 16:55:09 +02001467 /*
1468 * If 'linebreak' set check at a blank before a non-blank if the line
zeertzjq703f9bc2024-01-25 21:27:13 +01001469 * needs a break here.
zeertzjq6e55e852023-08-30 16:55:09 +02001470 */
zeertzjq703f9bc2024-01-25 21:27:13 +01001471 if (wp->w_p_lbr && wp->w_p_wrap && wp->w_width != 0
1472 && VIM_ISBREAK((int)s[0]) && !VIM_ISBREAK((int)s[1]))
Christian Brabandtdd75fcf2023-10-11 21:51:19 +02001473 {
1474 char_u *t = cts->cts_line;
Christian Brabandtcd6ee692023-10-14 11:29:28 +02001475 while (VIM_ISBREAK((int)t[0]))
Christian Brabandtdd75fcf2023-10-11 21:51:19 +02001476 t++;
zeertzjq703f9bc2024-01-25 21:27:13 +01001477 // 'linebreak' is only needed when not in leading whitespace.
1478 need_lbr = s >= t;
Christian Brabandtdd75fcf2023-10-11 21:51:19 +02001479 }
zeertzjq703f9bc2024-01-25 21:27:13 +01001480 if (need_lbr)
zeertzjq6e55e852023-08-30 16:55:09 +02001481 {
1482 /*
1483 * Count all characters from first non-blank after a blank up to next
1484 * non-blank after a blank.
1485 */
1486 int numberextra = win_col_off(wp);
1487 colnr_T col_adj = size - 1;
1488 colnr_T colmax = (colnr_T)(wp->w_width - numberextra - col_adj);
1489 if (vcol >= colmax)
1490 {
1491 colmax += col_adj;
1492 n = colmax + win_col_off2(wp);
1493 if (n > 0)
1494 colmax += (((vcol - colmax) / n) + 1) * n - col_adj;
1495 }
1496
1497 colnr_T vcol2 = vcol;
1498 for (;;)
1499 {
1500 char_u *ps = s;
1501 MB_PTR_ADV(s);
1502 int c = *s;
1503 if (!(c != NUL
1504 && (VIM_ISBREAK(c)
1505 || (!VIM_ISBREAK(c)
1506 && (vcol2 == vcol || !VIM_ISBREAK((int)*ps))))))
1507 break;
1508
1509 vcol2 += win_chartabsize(wp, s, vcol2);
1510 if (vcol2 >= colmax) // doesn't fit
1511 {
1512 size = colmax - vcol + col_adj;
1513 break;
1514 }
1515 }
1516 }
1517
zeertzjqce53e3e2023-09-01 18:49:30 +02001518# ifdef FEAT_PROP_POPUP
1519 size += cts->cts_first_char;
1520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 return size;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523#endif
1524}
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001527 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off, 'wrap'
1528 * is on and there are no properties that insert text. This means we need to
1529 * check for a double-byte character that doesn't fit at the end of the screen
1530 * line.
1531 * Only uses "cts_win", "cts_ptr" and "cts_vcol" from "cts".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 */
1533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534win_nolbr_chartabsize(
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001535 chartabsize_T *cts,
1536 int *headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001538 win_T *wp = cts->cts_win;
1539 char_u *s = cts->cts_ptr;
1540 colnr_T col = cts->cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int n;
1542
Bram Moolenaareed9d462021-02-15 20:38:25 +01001543 if (*s == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001545# ifdef FEAT_VARTABS
1546 return tabstop_padding(col, wp->w_buffer->b_p_ts,
1547 wp->w_buffer->b_p_vts_array);
1548# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 n = wp->w_buffer->b_p_ts;
1550 return (int)(n - (col % n));
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553 n = ptr2cells(s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001554 // Add one cell for a double-width character in the last column of the
1555 // window, displayed with a ">".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col))
1557 {
1558 if (headp != NULL)
1559 *headp = 1;
1560 return 3;
1561 }
1562 return n;
1563}
1564
1565/*
1566 * Return TRUE if virtual column "vcol" is in the rightmost column of window
1567 * "wp".
1568 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001569 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001570in_win_border(win_T *wp, colnr_T vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001572 int width1; // width of first line (after line number)
1573 int width2; // width of further lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 if (wp->w_width == 0) // there is no border
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 return FALSE;
Bram Moolenaar02631462017-09-22 15:20:32 +02001577 width1 = wp->w_width - win_col_off(wp);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001578 if ((int)vcol < width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001580 if ((int)vcol == width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return TRUE;
1582 width2 = width1 + win_col_off2(wp);
Bram Moolenaar8701cd62009-10-07 14:20:30 +00001583 if (width2 <= 0)
1584 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 return ((vcol - width1) % width2 == width2 - 1);
1586}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588/*
1589 * Get virtual column number of pos.
1590 * start: on the first position of this character (TAB, ctrl)
1591 * cursor: where the cursor is on this character (first char, except for TAB)
1592 * end: on the last position of this character (TAB, ctrl)
1593 *
1594 * This is used very often, keep it fast!
1595 */
1596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001597getvcol(
1598 win_T *wp,
1599 pos_T *pos,
1600 colnr_T *start,
1601 colnr_T *cursor,
1602 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 colnr_T vcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001605 char_u *ptr; // points to current char
Bram Moolenaarc667da52019-11-30 20:52:27 +01001606 char_u *line; // start of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int incr;
1608 int head;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001609#ifdef FEAT_VARTABS
1610 int *vts = wp->w_buffer->b_p_vts_array;
1611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 int ts = wp->w_buffer->b_p_ts;
1613 int c;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001614 chartabsize_T cts;
Bram Moolenaar49a90792022-08-09 18:25:23 +01001615#ifdef FEAT_PROP_POPUP
1616 int on_NUL = FALSE;
1617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 vcol = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001620 line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001622 init_chartabsize_arg(&cts, wp, pos->lnum, 0, line, line);
zeertzjqb557f482023-08-22 22:07:34 +02001623 cts.cts_max_head_vcol = -1;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 /*
1626 * This function is used very often, do some speed optimizations.
Bram Moolenaar597a4222014-06-25 14:39:50 +02001627 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001628 * and there are no text properties with "text" use a simple loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 * Also use this when 'list' is set but tabs take their normal size.
1630 */
Bram Moolenaareed9d462021-02-15 20:38:25 +01001631 if ((!wp->w_p_list || wp->w_lcs_chars.tab1 != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001633 && !wp->w_p_lbr && *get_showbreak_value(wp) == NUL && !wp->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001635#ifdef FEAT_PROP_POPUP
1636 && !cts.cts_has_prop_with_text
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 )
1639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 for (;;)
1641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 head = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 c = *ptr;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001644 // make sure we don't go past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (c == NUL)
1646 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001647 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 break;
1649 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001650 // A tab gets expanded, depending on the current column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (c == TAB)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001652#ifdef FEAT_VARTABS
1653 incr = tabstop_padding(vcol, ts, vts);
1654#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 incr = ts - (vcol % ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else
1658 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (has_mbyte)
1660 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001661 // For utf-8, if the byte is >= 0x80, need to look at
1662 // further bytes to find the cell width.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (enc_utf8 && c >= 0x80)
1664 incr = utf_ptr2cells(ptr);
1665 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001666 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaarc667da52019-11-30 20:52:27 +01001668 // If a double-cell char doesn't fit at the end of a line
1669 // it wraps to the next line, it's like this char is three
1670 // cells wide.
Bram Moolenaar9c33a7c2008-02-20 13:59:32 +00001671 if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1
1672 && in_win_border(wp, vcol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 ++incr;
1675 head = 1;
1676 }
1677 }
1678 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001679 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681
zeertzjq4ea37f82024-01-17 20:52:13 +01001682 char_u *next_ptr = ptr + (*mb_ptr2len)(ptr);
1683 if (next_ptr - line > pos->col) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 break;
1685
1686 vcol += incr;
zeertzjq4ea37f82024-01-17 20:52:13 +01001687 ptr = next_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
1689 }
1690 else
1691 {
1692 for (;;)
1693 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001694 // A tab gets expanded, depending on the current column.
1695 // Other things also take up space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 head = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001697 incr = win_lbr_chartabsize(&cts, &head);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001698 // make sure we don't go past the end of the line
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001699 if (*cts.cts_ptr == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001701 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar49a90792022-08-09 18:25:23 +01001702#ifdef FEAT_PROP_POPUP
1703 if (cts.cts_cur_text_width > 0)
1704 incr = cts.cts_cur_text_width;
1705 on_NUL = TRUE;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 break;
1708 }
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001709#ifdef FEAT_PROP_POPUP
1710 if (cursor == &wp->w_virtcol && cts.cts_ptr == cts.cts_line)
1711 // do not count the virtual text above for w_curswant
1712 wp->w_virtcol_first_char = cts.cts_first_char;
1713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
zeertzjq4ea37f82024-01-17 20:52:13 +01001715 char_u *next_ptr = cts.cts_ptr + (*mb_ptr2len)(cts.cts_ptr);
1716 if (next_ptr - line > pos->col) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 break;
1718
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001719 cts.cts_vcol += incr;
zeertzjq4ea37f82024-01-17 20:52:13 +01001720 cts.cts_ptr = next_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001722 vcol = cts.cts_vcol;
1723 ptr = cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001725 clear_chartabsize_arg(&cts);
1726
Christian Brabandt396fd1e2024-08-31 17:58:16 +02001727 if (*ptr == NUL && pos->col < MAXCOL && pos->col > ptr - line)
1728 pos->col = ptr - line;
1729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (start != NULL)
1731 *start = vcol + head;
1732 if (end != NULL)
1733 *end = vcol + incr - 1;
1734 if (cursor != NULL)
1735 {
1736 if (*ptr == TAB
Bram Moolenaar24959102022-05-07 20:01:16 +01001737 && (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 && !wp->w_p_list
1739 && !virtual_active()
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001740 && !(VIsual_active
1741 && (*p_sel == 'e' || LTOREQ_POS(*pos, VIsual)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 )
Bram Moolenaarc667da52019-11-30 20:52:27 +01001743 *cursor = vcol + incr - 1; // cursor at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 else
Bram Moolenaare428fa02022-08-09 16:55:41 +01001745 {
1746#ifdef FEAT_PROP_POPUP
Bram Moolenaar28c9f892022-08-14 13:28:55 +01001747 // in Insert mode, if "start_incl" is true the text gets inserted
1748 // after the virtual text, thus add its width
1749 if (((State & MODE_INSERT) == 0 || cts.cts_start_incl) && !on_NUL)
Bram Moolenaar49a90792022-08-09 18:25:23 +01001750 // cursor is after inserted text, unless on the NUL
Bram Moolenaare428fa02022-08-09 16:55:41 +01001751 vcol += cts.cts_cur_text_width;
Bram Moolenaar88b79cb2022-09-10 22:32:14 +01001752 else
1753 // insertion also happens after the "above" virtual text
1754 vcol += cts.cts_first_char;
Bram Moolenaare428fa02022-08-09 16:55:41 +01001755#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001756 *cursor = vcol + head; // cursor at start
Bram Moolenaare428fa02022-08-09 16:55:41 +01001757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759}
1760
1761/*
1762 * Get virtual cursor column in the current window, pretending 'list' is off.
1763 */
1764 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765getvcol_nolist(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 int list_save = curwin->w_p_list;
1768 colnr_T vcol;
1769
1770 curwin->w_p_list = FALSE;
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001771 if (posp->coladd)
1772 getvvcol(curwin, posp, NULL, &vcol, NULL);
1773 else
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001774 getvcol(curwin, posp, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 curwin->w_p_list = list_save;
1776 return vcol;
1777}
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779/*
1780 * Get virtual column in virtual mode.
1781 */
1782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783getvvcol(
1784 win_T *wp,
1785 pos_T *pos,
1786 colnr_T *start,
1787 colnr_T *cursor,
1788 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 colnr_T col;
1791 colnr_T coladd;
1792 colnr_T endadd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 if (virtual_active())
1796 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001797 // For virtual mode, only want one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 getvcol(wp, pos, &col, NULL, NULL);
1799
1800 coladd = pos->coladd;
1801 endadd = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001802 // Cannot put the cursor on part of a wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001804 if (pos->col < ml_get_buf_len(wp->w_buffer, pos->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
1806 int c = (*mb_ptr2char)(ptr + pos->col);
1807
1808 if (c != TAB && vim_isprintc(c))
1809 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001810 endadd = (colnr_T)(char2cells(c) - 1);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001811 if (coladd > endadd) // past end of line
Bram Moolenaara5792f52005-11-23 21:25:05 +00001812 endadd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
1814 coladd = 0;
1815 }
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 col += coladd;
1818 if (start != NULL)
1819 *start = col;
1820 if (cursor != NULL)
1821 *cursor = col;
1822 if (end != NULL)
1823 *end = col + endadd;
1824 }
1825 else
1826 getvcol(wp, pos, start, cursor, end);
1827}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829/*
1830 * Get the leftmost and rightmost virtual column of pos1 and pos2.
1831 * Used for Visual block mode.
1832 */
1833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001834getvcols(
1835 win_T *wp,
1836 pos_T *pos1,
1837 pos_T *pos2,
1838 colnr_T *left,
1839 colnr_T *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 colnr_T from1, from2, to1, to2;
1842
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001843 if (LT_POSP(pos1, pos2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 getvvcol(wp, pos1, &from1, NULL, &to1);
1846 getvvcol(wp, pos2, &from2, NULL, &to2);
1847 }
1848 else
1849 {
1850 getvvcol(wp, pos2, &from1, NULL, &to1);
1851 getvvcol(wp, pos1, &from2, NULL, &to2);
1852 }
1853 if (from2 < from1)
1854 *left = from2;
1855 else
1856 *left = from1;
1857 if (to2 > to1)
1858 {
1859 if (*p_sel == 'e' && from2 - 1 >= to1)
1860 *right = from2 - 1;
1861 else
1862 *right = to2;
1863 }
1864 else
1865 *right = to1;
1866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868/*
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001869 * Skip over ' ' and '\t'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
1871 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001872skipwhite(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001874 char_u *p = q;
1875
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001876 while (VIM_ISWHITE(*p))
1877 ++p;
1878 return p;
1879}
1880
Dominique Pelle748b3082022-01-08 12:41:16 +00001881#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001882/*
1883 * skip over ' ', '\t' and '\n'.
1884 */
1885 char_u *
1886skipwhite_and_nl(char_u *q)
1887{
1888 char_u *p = q;
1889
1890 while (VIM_ISWHITE(*p) || *p == NL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 ++p;
1892 return p;
1893}
Dominique Pelle748b3082022-01-08 12:41:16 +00001894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896/*
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001897 * getwhitecols: return the number of whitespace
1898 * columns (bytes) at the start of a given line
1899 */
1900 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001901getwhitecols_curline(void)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001902{
1903 return getwhitecols(ml_get_curline());
1904}
1905
1906 int
1907getwhitecols(char_u *p)
1908{
1909 return skipwhite(p) - p;
1910}
1911
1912/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001913 * skip over digits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 */
1915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001916skipdigits(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001918 char_u *p = q;
1919
Bram Moolenaarc667da52019-11-30 20:52:27 +01001920 while (VIM_ISDIGIT(*p)) // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 ++p;
1922 return p;
1923}
1924
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001925#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001926/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001927 * skip over binary digits
1928 */
1929 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001930skipbin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001931{
1932 char_u *p = q;
1933
Bram Moolenaarc667da52019-11-30 20:52:27 +01001934 while (vim_isbdigit(*p)) // skip to next non-digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001935 ++p;
1936 return p;
1937}
1938
1939/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001940 * skip over digits and hex characters
1941 */
1942 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001943skiphex(char_u *q)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001944{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001945 char_u *p = q;
1946
Bram Moolenaarc667da52019-11-30 20:52:27 +01001947 while (vim_isxdigit(*p)) // skip to next non-digit
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001948 ++p;
1949 return p;
1950}
1951#endif
1952
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001953/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001954 * skip to bin digit (or NUL after the string)
1955 */
1956 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001957skiptobin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001958{
1959 char_u *p = q;
1960
Bram Moolenaarc667da52019-11-30 20:52:27 +01001961 while (*p != NUL && !vim_isbdigit(*p)) // skip to next digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001962 ++p;
1963 return p;
1964}
1965
1966/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001967 * skip to digit (or NUL after the string)
1968 */
1969 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970skiptodigit(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001971{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001972 char_u *p = q;
1973
Bram Moolenaarc667da52019-11-30 20:52:27 +01001974 while (*p != NUL && !VIM_ISDIGIT(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001975 ++p;
1976 return p;
1977}
1978
1979/*
1980 * skip to hex character (or NUL after the string)
1981 */
1982 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001983skiptohex(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001984{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001985 char_u *p = q;
1986
Bram Moolenaarc667da52019-11-30 20:52:27 +01001987 while (*p != NUL && !vim_isxdigit(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001988 ++p;
1989 return p;
1990}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992/*
1993 * Variant of isdigit() that can handle characters > 0x100.
1994 * We don't use isdigit() here, because on some systems it also considers
1995 * superscript 1 to be a digit.
1996 * Use the VIM_ISDIGIT() macro for simple arguments.
1997 */
1998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001999vim_isdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 return (c >= '0' && c <= '9');
2002}
2003
2004/*
2005 * Variant of isxdigit() that can handle characters > 0x100.
2006 * We don't use isxdigit() here, because on some systems it also considers
2007 * superscript 1 to be a digit.
2008 */
2009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002010vim_isxdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 return (c >= '0' && c <= '9')
2013 || (c >= 'a' && c <= 'f')
2014 || (c >= 'A' && c <= 'F');
2015}
2016
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002017/*
2018 * Corollary of vim_isdigit and vim_isxdigit() that can handle
2019 * characters > 0x100.
2020 */
2021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002022vim_isbdigit(int c)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002023{
2024 return (c == '0' || c == '1');
2025}
2026
Bram Moolenaarc37b6552021-01-07 19:36:30 +01002027 static int
2028vim_isodigit(int c)
2029{
2030 return (c >= '0' && c <= '7');
2031}
2032
Bram Moolenaar78622822005-08-23 21:00:13 +00002033/*
2034 * Vim's own character class functions. These exist because many library
2035 * islower()/toupper() etc. do not work properly: they crash when used with
2036 * invalid values or can't handle latin1 when the locale is C.
2037 * Speed is most important here.
2038 */
2039#define LATIN1LOWER 'l'
2040#define LATIN1UPPER 'U'
2041
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00002042static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll";
Bram Moolenaar936347b2012-05-25 11:56:22 +02002043static 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";
2044static 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";
Bram Moolenaar78622822005-08-23 21:00:13 +00002045
2046 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002047vim_islower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002048{
2049 if (c <= '@')
2050 return FALSE;
2051 if (c >= 0x80)
2052 {
2053 if (enc_utf8)
2054 return utf_islower(c);
2055 if (c >= 0x100)
2056 {
2057#ifdef HAVE_ISWLOWER
2058 if (has_mbyte)
2059 return iswlower(c);
2060#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002061 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002062 return FALSE;
2063 }
2064 if (enc_latin1like)
2065 return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER;
2066 }
Keith Thompson184f71c2024-01-04 21:19:04 +01002067 return SAFE_islower(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002068}
2069
2070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002071vim_isupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002072{
2073 if (c <= '@')
2074 return FALSE;
2075 if (c >= 0x80)
2076 {
2077 if (enc_utf8)
2078 return utf_isupper(c);
2079 if (c >= 0x100)
2080 {
2081#ifdef HAVE_ISWUPPER
2082 if (has_mbyte)
2083 return iswupper(c);
2084#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002085 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002086 return FALSE;
2087 }
2088 if (enc_latin1like)
2089 return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER;
2090 }
Keith Thompson184f71c2024-01-04 21:19:04 +01002091 return SAFE_isupper(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002092}
2093
2094 int
Bram Moolenaar5921aeb2022-02-19 11:20:12 +00002095vim_isalpha(int c)
2096{
2097 return vim_islower(c) || vim_isupper(c);
2098}
2099
2100 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002101vim_toupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002102{
2103 if (c <= '@')
2104 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02002105 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00002106 {
2107 if (enc_utf8)
2108 return utf_toupper(c);
2109 if (c >= 0x100)
2110 {
2111#ifdef HAVE_TOWUPPER
2112 if (has_mbyte)
2113 return towupper(c);
2114#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002115 // toupper() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002116 return c;
2117 }
2118 if (enc_latin1like)
2119 return latin1upper[c];
2120 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002121 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
2122 return TOUPPER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002123 return TOUPPER_LOC(c);
2124}
2125
2126 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002127vim_tolower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002128{
2129 if (c <= '@')
2130 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02002131 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00002132 {
2133 if (enc_utf8)
2134 return utf_tolower(c);
2135 if (c >= 0x100)
2136 {
2137#ifdef HAVE_TOWLOWER
2138 if (has_mbyte)
2139 return towlower(c);
2140#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002141 // tolower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002142 return c;
2143 }
2144 if (enc_latin1like)
2145 return latin1lower[c];
2146 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002147 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
2148 return TOLOWER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002149 return TOLOWER_LOC(c);
2150}
Bram Moolenaar78622822005-08-23 21:00:13 +00002151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152/*
2153 * skiptowhite: skip over text until ' ' or '\t' or NUL.
2154 */
2155 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002156skiptowhite(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157{
2158 while (*p != ' ' && *p != '\t' && *p != NUL)
2159 ++p;
2160 return p;
2161}
2162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163/*
2164 * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars
2165 */
2166 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167skiptowhite_esc(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 while (*p != ' ' && *p != '\t' && *p != NUL)
2170 {
2171 if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL)
2172 ++p;
2173 ++p;
2174 }
2175 return p;
2176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002179 * Get a number from a string and skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * Note: the argument is a pointer to a char_u pointer!
2181 */
2182 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01002183getdigits(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
2185 char_u *p;
2186 long retval;
2187
2188 p = *pp;
2189 retval = atol((char *)p);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002190 if (*p == '-') // skip negative sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 ++p;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002192 p = skipdigits(p); // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 *pp = p;
2194 return retval;
2195}
2196
2197/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002198 * Like getdigits() but allow for embedded single quotes.
2199 */
2200 long
2201getdigits_quoted(char_u **pp)
2202{
2203 char_u *p = *pp;
2204 long retval = 0;
2205
2206 if (*p == '-')
2207 ++p;
2208 while (VIM_ISDIGIT(*p))
2209 {
2210 if (retval >= LONG_MAX / 10 - 10)
2211 retval = LONG_MAX;
2212 else
2213 retval = retval * 10 - '0' + *p;
2214 ++p;
2215 if (in_vim9script() && *p == '\'' && VIM_ISDIGIT(p[1]))
2216 ++p;
2217 }
2218 if (**pp == '-')
2219 {
2220 if (retval == LONG_MAX)
2221 retval = LONG_MIN;
2222 else
2223 retval = -retval;
2224 }
2225 *pp = p;
2226 return retval;
2227}
2228
2229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 * Return TRUE if "lbuf" is empty or only contains blanks.
2231 */
2232 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233vim_isblankline(char_u *lbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 char_u *p;
2236
2237 p = skipwhite(lbuf);
2238 return (*p == NUL || *p == '\r' || *p == '\n');
2239}
2240
2241/*
2242 * Convert a string into a long and/or unsigned long, taking care of
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002243 * hexadecimal, octal, and binary numbers. Accepts a '-' sign.
2244 * If "prep" is not NULL, returns a flag to indicate the type of the number:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 * 0 decimal
2246 * '0' octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002247 * 'O' octal
2248 * 'o' octal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002249 * 'B' bin
2250 * 'b' bin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * 'X' hex
2252 * 'x' hex
2253 * If "len" is not NULL, the length of the number in characters is returned.
2254 * If "nptr" is not NULL, the signed result is returned in it.
2255 * If "unptr" is not NULL, the unsigned result is returned in it.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002256 * If "what" contains STR2NR_BIN recognize binary numbers
2257 * If "what" contains STR2NR_OCT recognize octal numbers
2258 * If "what" contains STR2NR_HEX recognize hex numbers
2259 * If "what" contains STR2NR_FORCE always assume bin/oct/hex.
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002260 * If "what" contains STR2NR_QUOTE ignore embedded single quotes
Bram Moolenaarce157752017-10-28 16:07:33 +02002261 * If maxlen > 0, check at a maximum maxlen chars.
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002262 * If strict is TRUE, check the number strictly. return *len = 0 if fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 */
2264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002265vim_str2nr(
2266 char_u *start,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002267 int *prep, // return: type of number 0 = decimal, 'x'
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002268 // or 'X' is hex, '0', 'o' or 'O' is octal,
2269 // 'b' or 'B' is bin
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002270 int *len, // return: detected length of number
2271 int what, // what numbers to recognize
2272 varnumber_T *nptr, // return: signed result
2273 uvarnumber_T *unptr, // return: unsigned result
2274 int maxlen, // max length of string to check
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002275 int strict, // check strictly
2276 int *overflow) // when not NULL set to TRUE for overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277{
2278 char_u *ptr = start;
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002279 int pre = 0; // default is decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 int negative = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002281 uvarnumber_T un = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002282 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002284 if (len != NULL)
2285 *len = 0;
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (ptr[0] == '-')
2288 {
2289 negative = TRUE;
2290 ++ptr;
2291 }
2292
Bram Moolenaarc667da52019-11-30 20:52:27 +01002293 // Recognize hex, octal, and bin.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002294 if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9'
2295 && (maxlen == 0 || maxlen > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002297 pre = ptr[1];
2298 if ((what & STR2NR_HEX)
2299 && (pre == 'X' || pre == 'x') && vim_isxdigit(ptr[2])
2300 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002301 // hexadecimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002302 ptr += 2;
2303 else if ((what & STR2NR_BIN)
2304 && (pre == 'B' || pre == 'b') && vim_isbdigit(ptr[2])
2305 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002306 // binary
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002307 ptr += 2;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002308 else if ((what & STR2NR_OOCT)
Bram Moolenaarc37b6552021-01-07 19:36:30 +01002309 && (pre == 'O' || pre == 'o') && vim_isodigit(ptr[2])
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002310 && (maxlen == 0 || maxlen > 2))
2311 // octal with prefix "0o"
2312 ptr += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 else
2314 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002315 // decimal or octal, default is decimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002316 pre = 0;
2317 if (what & STR2NR_OCT)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002318 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002319 // Don't interpret "0", "08" or "0129" as octal.
Bram Moolenaarce157752017-10-28 16:07:33 +02002320 for (n = 1; n != maxlen && VIM_ISDIGIT(ptr[n]); ++n)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002321 {
2322 if (ptr[n] > '7')
2323 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002324 pre = 0; // can't be octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002325 break;
2326 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002327 pre = '0'; // assume octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002328 }
2329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331 }
2332
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002333 // Do the conversion manually to avoid sscanf() quirks.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002334 n = 1;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002335 if (pre == 'B' || pre == 'b'
2336 || ((what & STR2NR_BIN) && (what & STR2NR_FORCE)))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002337 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002338 // bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002339 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002340 n += 2; // skip over "0b"
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002341 while ('0' <= *ptr && *ptr <= '1')
2342 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002343 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002344 if (un <= UVARNUM_MAX / 2)
2345 un = 2 * un + (uvarnumber_T)(*ptr - '0');
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002346 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002347 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002348 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002349 if (overflow != NULL)
2350 *overflow = TRUE;
2351 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002352 ++ptr;
2353 if (n++ == maxlen)
2354 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002355 if ((what & STR2NR_QUOTE) && *ptr == '\''
2356 && '0' <= ptr[1] && ptr[1] <= '1')
2357 {
2358 ++ptr;
2359 if (n++ == maxlen)
2360 break;
2361 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002362 }
2363 }
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002364 else if (pre == 'O' || pre == 'o' ||
2365 pre == '0' || ((what & STR2NR_OCT) && (what & STR2NR_FORCE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002367 // octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002368 if (pre != 0 && pre != '0')
2369 n += 2; // skip over "0o"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002370 while ('0' <= *ptr && *ptr <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002372 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002373 if (un <= UVARNUM_MAX / 8)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002374 un = 8 * un + (uvarnumber_T)(*ptr - '0');
2375 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002376 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002377 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002378 if (overflow != NULL)
2379 *overflow = TRUE;
2380 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002381 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002382 if (n++ == maxlen)
2383 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002384 if ((what & STR2NR_QUOTE) && *ptr == '\''
2385 && '0' <= ptr[1] && ptr[1] <= '7')
2386 {
2387 ++ptr;
2388 if (n++ == maxlen)
2389 break;
2390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002392 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002393 else if (pre != 0 || ((what & STR2NR_HEX) && (what & STR2NR_FORCE)))
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002394 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002395 // hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002396 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002397 n += 2; // skip over "0x"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002398 while (vim_isxdigit(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002400 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002401 if (un <= UVARNUM_MAX / 16)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002402 un = 16 * un + (uvarnumber_T)hex2nr(*ptr);
2403 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002404 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002405 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002406 if (overflow != NULL)
2407 *overflow = TRUE;
2408 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002409 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002410 if (n++ == maxlen)
2411 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002412 if ((what & STR2NR_QUOTE) && *ptr == '\'' && vim_isxdigit(ptr[1]))
2413 {
2414 ++ptr;
2415 if (n++ == maxlen)
2416 break;
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
2419 }
2420 else
2421 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002422 // decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 while (VIM_ISDIGIT(*ptr))
2424 {
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002425 uvarnumber_T digit = (uvarnumber_T)(*ptr - '0');
2426
Bram Moolenaarc667da52019-11-30 20:52:27 +01002427 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002428 if (un < UVARNUM_MAX / 10
2429 || (un == UVARNUM_MAX / 10 && digit <= UVARNUM_MAX % 10))
2430 un = 10 * un + digit;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002431 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002432 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002433 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002434 if (overflow != NULL)
2435 *overflow = TRUE;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002438 if (n++ == maxlen)
2439 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002440 if ((what & STR2NR_QUOTE) && *ptr == '\'' && VIM_ISDIGIT(ptr[1]))
2441 {
2442 ++ptr;
2443 if (n++ == maxlen)
2444 break;
2445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002448
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002449 // Check for an alphanumeric character immediately following, that is
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002450 // most likely a typo.
2451 if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr))
2452 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002454 if (prep != NULL)
2455 *prep = pre;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (len != NULL)
2457 *len = (int)(ptr - start);
2458 if (nptr != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002459 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002460 if (negative) // account for leading '-' for decimal numbers
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002461 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002462 // avoid ubsan error for overflow
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002463 if (un > VARNUM_MAX)
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002464 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002465 *nptr = VARNUM_MIN;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002466 if (overflow != NULL)
2467 *overflow = TRUE;
2468 }
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002469 else
2470 *nptr = -(varnumber_T)un;
2471 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002472 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002473 {
dundargocc57b5bc2022-11-02 13:30:51 +00002474 // prevent a large unsigned number to become negative
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002475 if (un > VARNUM_MAX)
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002476 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002477 un = VARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002478 if (overflow != NULL)
2479 *overflow = TRUE;
2480 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002481 *nptr = (varnumber_T)un;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002482 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (unptr != NULL)
2485 *unptr = un;
2486}
2487
2488/*
2489 * Return the value of a single hex character.
2490 * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'.
2491 */
2492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002493hex2nr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
2495 if (c >= 'a' && c <= 'f')
2496 return c - 'a' + 10;
2497 if (c >= 'A' && c <= 'F')
2498 return c - 'A' + 10;
2499 return c - '0';
2500}
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502/*
2503 * Convert two hex characters to a byte.
2504 * Return -1 if one of the characters is not hex.
2505 */
2506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002507hexhex2nr(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1]))
2510 return -1;
2511 return (hex2nr(p[0]) << 4) + hex2nr(p[1]);
2512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514/*
2515 * Return TRUE if "str" starts with a backslash that should be removed.
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01002516 * For MS-DOS, MSWIN and OS/2 this is only done when the character after the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 * backslash is not a normal file name character.
2518 * '$' is a valid file name character, we don't remove the backslash before
2519 * it. This means it is not possible to use an environment variable after a
2520 * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works.
2521 * Although "\ name" is valid, the backslash in "Program\ files" must be
2522 * removed. Assume a file name doesn't start with a space.
2523 * For multi-byte names, never remove a backslash before a non-ascii
2524 * character, assume that all multi-byte characters are valid file name
2525 * characters.
2526 */
2527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002528rem_backslash(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530#ifdef BACKSLASH_IN_FILENAME
2531 return (str[0] == '\\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 && str[1] < 0x80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 && (str[1] == ' '
2534 || (str[1] != NUL
2535 && str[1] != '*'
2536 && str[1] != '?'
2537 && !vim_isfilec(str[1]))));
2538#else
2539 return (str[0] == '\\' && str[1] != NUL);
2540#endif
2541}
2542
2543/*
2544 * Halve the number of backslashes in a file name argument.
2545 * For MS-DOS we only do this if the character after the backslash
2546 * is not a normal file character.
2547 */
2548 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549backslash_halve(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 for ( ; *p; ++p)
2552 if (rem_backslash(p))
Bram Moolenaar446cb832008-06-24 21:56:24 +00002553 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554}
2555
2556/*
2557 * backslash_halve() plus save the result in allocated memory.
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002558 * However, returns "p" when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 */
2560 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002561backslash_halve_save(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 char_u *res;
2564
2565 res = vim_strsave(p);
2566 if (res == NULL)
2567 return p;
2568 backslash_halve(res);
2569 return res;
2570}