blob: b4bc812fa81372bea0ca0d35b156b791e276dffc [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.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 */
825 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100826win_linetabsize(win_T *wp, linenr_T lnum, char_u *line, colnr_T len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100828 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100830 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
Bram Moolenaar49a90792022-08-09 18:25:23 +0100831 win_linetabsize_cts(&cts, len);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100832 clear_chartabsize_arg(&cts);
833 return (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834}
835
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100836/*
837 * Return the number of cells line "lnum" of window "wp" will take on the
838 * screen, taking into account the size of a tab and text properties.
839 */
840 int
841linetabsize(win_T *wp, linenr_T lnum)
842{
843 return win_linetabsize(wp, lnum,
844 ml_get_buf(wp->w_buffer, lnum, FALSE), (colnr_T)MAXCOL);
845}
846
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +0100847/*
848 * Like linetabsize(), but excludes 'above'/'after'/'right'/'below' aligned
849 * virtual text, while keeping inline virtual text.
850 */
851 int
852linetabsize_no_outer(win_T *wp, linenr_T lnum)
853{
854#ifndef FEAT_PROP_POPUP
855 return linetabsize(wp, lnum);
856#else
857 chartabsize_T cts;
858 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
859
860 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
861
862 if (cts.cts_text_prop_count)
863 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200864 int write_idx = 0;
865 for (int read_idx = 0; read_idx < cts.cts_text_prop_count; read_idx++)
866 {
867 textprop_T *tp = &cts.cts_text_props[read_idx];
868 if (tp->tp_col != MAXCOL)
869 {
870 if (read_idx != write_idx)
871 cts.cts_text_props[write_idx] = *tp;
872 write_idx++;
873 }
874 }
875 cts.cts_text_prop_count = write_idx;
876 if (cts.cts_text_prop_count == 0)
877 VIM_CLEAR(cts.cts_text_props);
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +0100878 }
879
880 win_linetabsize_cts(&cts, (colnr_T)MAXCOL);
881 clear_chartabsize_arg(&cts);
882 return (int)cts.cts_vcol;
883#endif
884}
885
Bram Moolenaar49a90792022-08-09 18:25:23 +0100886 void
887win_linetabsize_cts(chartabsize_T *cts, colnr_T len)
888{
Christian Brabandteff20eb2024-05-15 21:35:36 +0200889 vimlong_T vcol = cts->cts_vcol;
Bram Moolenaar49a90792022-08-09 18:25:23 +0100890#ifdef FEAT_PROP_POPUP
891 cts->cts_with_trailing = len == MAXCOL;
892#endif
893 for ( ; *cts->cts_ptr != NUL && (len == MAXCOL || cts->cts_ptr < cts->cts_line + len);
894 MB_PTR_ADV(cts->cts_ptr))
Christian Brabandteff20eb2024-05-15 21:35:36 +0200895 {
896 vcol += win_lbr_chartabsize(cts, NULL);
897 if (vcol > MAXCOL)
898 {
899 cts->cts_vcol = MAXCOL;
900 break;
901 }
902 else
903 cts->cts_vcol = (int)vcol;
904 }
Bram Moolenaar49a90792022-08-09 18:25:23 +0100905#ifdef FEAT_PROP_POPUP
Ibbya6ab5e62023-08-20 20:24:18 +0200906 // check for a virtual text at the end of a line or on an empty line
zeertzjqe3daa062023-08-27 19:11:46 +0200907 if (len == MAXCOL && cts->cts_has_prop_with_text && *cts->cts_ptr == NUL)
Bram Moolenaar49a90792022-08-09 18:25:23 +0100908 {
909 (void)win_lbr_chartabsize(cts, NULL);
Christian Brabandteff20eb2024-05-15 21:35:36 +0200910 vcol += cts->cts_cur_text_width;
Bram Moolenaar55a27d82023-02-12 18:03:57 +0000911 // when properties are above or below the empty line must also be
912 // counted
Ibbya6ab5e62023-08-20 20:24:18 +0200913 if (cts->cts_ptr == cts->cts_line && cts->cts_prop_lines > 0)
Christian Brabandteff20eb2024-05-15 21:35:36 +0200914 ++vcol;
915 cts->cts_vcol = vcol > MAXCOL ? MAXCOL : (int)vcol;
Bram Moolenaar49a90792022-08-09 18:25:23 +0100916 }
917#endif
918}
919
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920/*
Bram Moolenaar81695252004-12-29 20:58:21 +0000921 * Return TRUE if 'c' is a normal identifier character:
922 * Letters and characters from the 'isident' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925vim_isIDc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100927 return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928}
929
930/*
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +0200931 * Like vim_isIDc() but not using the 'isident' option: letters, numbers and
932 * underscore.
933 */
934 int
935vim_isNormalIDc(int c)
936{
937 return ASCII_ISALNUM(c) || c == '_';
938}
939
940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * return TRUE if 'c' is a keyword character: Letters and characters from
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100942 * 'iskeyword' option for the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * For multi-byte characters mb_get_class() is used (builtin rules).
944 */
945 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946vim_iswordc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100948 return vim_iswordc_buf(c, curbuf);
949}
950
951 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100952vim_iswordc_buf(int c, buf_T *buf)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100953{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (c >= 0x100)
955 {
956 if (enc_dbcs != 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000957 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (enc_utf8)
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100959 return utf_class_buf(c, buf) >= 2;
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100960 return FALSE;
961 }
962 return (c > 0 && GET_CHARTAB(buf, c) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Just like vim_iswordc() but uses a pointer to the (multi-byte) character.
967 */
968 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100969vim_iswordp(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100971 return vim_iswordp_buf(p, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972}
973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100975vim_iswordp_buf(char_u *p, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100977 int c = *p;
978
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100979 if (has_mbyte && MB_BYTE2LEN(c) > 1)
980 c = (*mb_ptr2char)(p);
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100981 return vim_iswordc_buf(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984/*
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +0100985 * Return TRUE if 'c' is a valid file-name character as specified with the
986 * 'isfname' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 * Assume characters above 0x100 are valid (multi-byte).
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +0100988 * To be used for commands like "gf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 */
990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100991vim_isfilec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100993 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994}
995
Dominique Pellee764d1b2023-03-12 21:20:59 +0000996#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997/*
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +0100998 * Return TRUE if 'c' is a valid file-name character, including characters left
999 * out of 'isfname' to make "gf" work, such as comma, space, '@', etc.
1000 */
1001 int
1002vim_is_fname_char(int c)
1003{
1004 return vim_isfilec(c) || c == ',' || c == ' ' || c == '@';
1005}
Dominique Pellee764d1b2023-03-12 21:20:59 +00001006#endif
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +01001007
1008/*
Bram Moolenaardd87969c2007-08-21 13:07:12 +00001009 * return TRUE if 'c' is a valid file-name character or a wildcard character
1010 * Assume characters above 0x100 are valid (multi-byte).
1011 * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]")
1012 * returns false.
1013 */
1014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001015vim_isfilec_or_wc(int c)
Bram Moolenaardd87969c2007-08-21 13:07:12 +00001016{
1017 char_u buf[2];
1018
1019 buf[0] = (char_u)c;
1020 buf[1] = NUL;
1021 return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf);
1022}
1023
1024/*
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001025 * Return TRUE if 'c' is a printable character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * Assume characters above 0x100 are printable (multi-byte), except for
1027 * Unicode.
1028 */
1029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001030vim_isprintc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (enc_utf8 && c >= 0x100)
1033 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001034 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037/*
1038 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head
1039 * byte of a double-byte character.
1040 */
1041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001042vim_isprintc_strict(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1)
1045 return FALSE;
1046 if (enc_utf8 && c >= 0x100)
1047 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001048 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001052 * Prepare the structure passed to chartabsize functions.
1053 * "line" is the start of the line, "ptr" is the first relevant character.
1054 * When "lnum" is zero do not use text properties that insert text.
1055 */
1056 void
1057init_chartabsize_arg(
1058 chartabsize_T *cts,
1059 win_T *wp,
Bram Moolenaare5a420f2022-09-07 21:46:56 +01001060 linenr_T lnum UNUSED,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001061 colnr_T col,
1062 char_u *line,
1063 char_u *ptr)
1064{
Bram Moolenaar3f79b612022-08-01 12:06:40 +01001065 CLEAR_POINTER(cts);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001066 cts->cts_win = wp;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001067 cts->cts_vcol = col;
1068 cts->cts_line = line;
1069 cts->cts_ptr = ptr;
zeertzjq5b0722b2024-01-17 20:42:53 +01001070#ifdef FEAT_LINEBREAK
1071 cts->cts_bri_size = -1;
1072#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001073#ifdef FEAT_PROP_POPUP
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001074 if (lnum > 0 && !ignore_text_props)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001075 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001076 char_u *prop_start;
1077 int count;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001078
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001079 count = get_text_props(wp->w_buffer, lnum, &prop_start, FALSE);
1080 cts->cts_text_prop_count = count;
1081 if (count > 0)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001082 {
1083 // Make a copy of the properties, so that they are properly
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001084 // aligned. Make it twice as long for the sorting below.
1085 cts->cts_text_props = ALLOC_MULT(textprop_T, count * 2);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001086 if (cts->cts_text_props == NULL)
1087 cts->cts_text_prop_count = 0;
1088 else
1089 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001090 int i;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001091
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001092 mch_memmove(cts->cts_text_props + count, prop_start,
1093 count * sizeof(textprop_T));
1094 for (i = 0; i < count; ++i)
Bram Moolenaar89469d12022-12-02 20:46:26 +00001095 {
1096 textprop_T *tp = cts->cts_text_props + i + count;
1097 if (tp->tp_id < 0
1098 && text_prop_type_valid(wp->w_buffer, tp))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001099 {
1100 cts->cts_has_prop_with_text = TRUE;
1101 break;
1102 }
Bram Moolenaar89469d12022-12-02 20:46:26 +00001103 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001104 if (!cts->cts_has_prop_with_text)
1105 {
1106 // won't use the text properties, free them
Bram Moolenaar3f79b612022-08-01 12:06:40 +01001107 VIM_CLEAR(cts->cts_text_props);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001108 cts->cts_text_prop_count = 0;
1109 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001110 else
1111 {
1112 int *text_prop_idxs;
1113
1114 // Need to sort the array to get any truncation right.
1115 // Do the sorting in the second part of the array, then
1116 // move the sorted props to the first part of the array.
1117 text_prop_idxs = ALLOC_MULT(int, count);
1118 if (text_prop_idxs != NULL)
1119 {
1120 for (i = 0; i < count; ++i)
1121 text_prop_idxs[i] = i + count;
1122 sort_text_props(curbuf, cts->cts_text_props,
1123 text_prop_idxs, count);
1124 // Here we want the reverse order.
1125 for (i = 0; i < count; ++i)
1126 cts->cts_text_props[count - i - 1] =
1127 cts->cts_text_props[text_prop_idxs[i]];
1128 vim_free(text_prop_idxs);
1129 }
1130 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001131 }
1132 }
1133 }
1134#endif
1135}
1136
1137/*
1138 * Free any allocated item in "cts".
1139 */
1140 void
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001141clear_chartabsize_arg(chartabsize_T *cts UNUSED)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001142{
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001143#ifdef FEAT_PROP_POPUP
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001144 if (cts->cts_text_prop_count > 0)
Bram Moolenaar34a1f772022-07-26 11:20:48 +01001145 {
Bram Moolenaar3f79b612022-08-01 12:06:40 +01001146 VIM_CLEAR(cts->cts_text_props);
1147 cts->cts_text_prop_count = 0;
Bram Moolenaar34a1f772022-07-26 11:20:48 +01001148 }
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001149#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001150}
1151
1152/*
1153 * Like chartabsize(), but also check for line breaks on the screen and text
1154 * properties that insert text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 */
1156 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001157lbr_chartabsize(chartabsize_T *cts)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001159#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
1160 if (1
1161# ifdef FEAT_LINEBREAK
1162 && !curwin->w_p_lbr && *get_showbreak_value(curwin) == NUL
1163 && !curwin->w_p_bri
1164# endif
1165# ifdef FEAT_PROP_POPUP
1166 && !cts->cts_has_prop_with_text
1167#endif
1168 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (curwin->w_p_wrap)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001172 return win_nolbr_chartabsize(cts, NULL);
1173 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, cts->cts_ptr, cts->cts_vcol)
1174#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001176 return win_lbr_chartabsize(cts, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#endif
1178}
1179
1180/*
1181 * Call lbr_chartabsize() and advance the pointer.
1182 */
1183 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001184lbr_chartabsize_adv(chartabsize_T *cts)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
1186 int retval;
1187
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001188 retval = lbr_chartabsize(cts);
1189 MB_PTR_ADV(cts->cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 return retval;
1191}
1192
1193/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001194 * Return the screen size of the character indicated by "cts".
1195 * "cts->cts_cur_text_width" is set to the extra size for a text property that
1196 * inserts text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 * This function is used very often, keep it fast!!!!
1198 *
zeertzjqb557f482023-08-22 22:07:34 +02001199 * If "headp" not NULL, set "*headp" to the size of 'showbreak'/'breakindent'
1200 * included in the return value.
1201 * When "cts->cts_max_head_vcol" is positive, only count in "*headp" the size
1202 * of 'showbreak'/'breakindent' before "cts->cts_max_head_vcol".
1203 * When "cts->cts_max_head_vcol" is negative, only count in "*headp" the size
1204 * of 'showbreak'/'breakindent' before where cursor should be placed.
1205 *
1206 * Warning: "*headp" may not be set if it's 0, init to 0 before calling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001209win_lbr_chartabsize(
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001210 chartabsize_T *cts,
1211 int *headp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001213 win_T *wp = cts->cts_win;
Martin Tournoijba43e762022-10-13 22:12:15 +01001214#if defined(FEAT_PROP_POPUP) || defined(FEAT_LINEBREAK)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001215 char_u *line = cts->cts_line; // start of the line
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001216#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001217 char_u *s = cts->cts_ptr;
1218 colnr_T vcol = cts->cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 int size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 int mb_added = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001222 int n;
Bram Moolenaaree857022019-11-09 23:26:40 +01001223 char_u *sbr;
Bram Moolenaarcba69522022-08-06 21:03:53 +01001224 int no_sbr = FALSE;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001227#if defined(FEAT_PROP_POPUP)
1228 cts->cts_cur_text_width = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001229 cts->cts_first_char = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001230#endif
1231
1232#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001234 * No 'linebreak', 'showbreak', 'breakindent' and text properties that
1235 * insert text: return quickly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001237 if (1
1238# ifdef FEAT_LINEBREAK
1239 && !wp->w_p_lbr && !wp->w_p_bri && *get_showbreak_value(wp) == NUL
1240# endif
1241# ifdef FEAT_PROP_POPUP
1242 && !cts->cts_has_prop_with_text
1243# endif
1244 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245#endif
1246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (wp->w_p_wrap)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001248 return win_nolbr_chartabsize(cts, headp);
1249 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001252#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
zeertzjq11939512023-08-23 20:58:01 +02001253 int has_lcs_eol = wp->w_p_list && wp->w_lcs_chars.eol != NUL;
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001256 * First get the normal size, without 'linebreak' or text properties
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001258 size = win_chartabsize(wp, s, vcol);
Dylan Thacker-Smithda0c9132024-02-27 20:25:10 +01001259 if (*s == NUL)
1260 {
1261 // 1 cell for EOL list char (if present), as opposed to the two cell ^@
1262 // for a NUL character in the text.
1263 size = has_lcs_eol ? 1 : 0;
1264 }
zeertzjqac2d8812023-08-31 18:07:30 +02001265# ifdef FEAT_LINEBREAK
1266 int is_doublewidth = has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1;
1267# endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001268
1269# ifdef FEAT_PROP_POPUP
Bram Moolenaar49a90792022-08-09 18:25:23 +01001270 if (cts->cts_has_prop_with_text)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001271 {
Bram Moolenaare428fa02022-08-09 16:55:41 +01001272 int tab_size = size;
Bram Moolenaar49a90792022-08-09 18:25:23 +01001273 int charlen = *s == NUL ? 1 : mb_ptr2len(s);
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001274 int i;
1275 int col = (int)(s - line);
1276 garray_T *gap = &wp->w_buffer->b_textprop_text;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001277
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001278 // The "$" for 'list' mode will go between the EOL and
1279 // the text prop, account for that.
zeertzjq11939512023-08-23 20:58:01 +02001280 if (has_lcs_eol)
1281 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001282 ++vcol;
zeertzjq11939512023-08-23 20:58:01 +02001283 --size;
1284 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001285
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001286 for (i = 0; i < cts->cts_text_prop_count; ++i)
1287 {
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01001288 textprop_T *tp = cts->cts_text_props + i;
1289 int col_off = win_col_off(wp);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001290
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001291 // Watch out for the text being deleted. "cts_text_props" is a
1292 // copy, the text prop may actually have been removed from the line.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001293 if (tp->tp_id < 0
Bram Moolenaar25463612022-08-08 11:07:47 +01001294 && ((tp->tp_col - 1 >= col
Bram Moolenaare428fa02022-08-09 16:55:41 +01001295 && tp->tp_col - 1 < col + charlen)
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001296 || (tp->tp_col == MAXCOL
1297 && ((tp->tp_flags & TP_FLAG_ALIGN_ABOVE)
1298 ? col == 0
zeertzjqe3daa062023-08-27 19:11:46 +02001299 : s[0] == NUL && cts->cts_with_trailing)))
Bram Moolenaar4ce1f992022-12-19 13:31:06 +00001300 && -tp->tp_id - 1 < gap->ga_len)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001301 {
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001302 char_u *p = ((char_u **)gap->ga_data)[-tp->tp_id - 1];
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001303
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001304 if (p != NULL)
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001305 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001306 int cells;
Bram Moolenaar398649e2022-08-04 15:03:48 +01001307
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001308 if (tp->tp_col == MAXCOL)
1309 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001310 int n_extra = (int)STRLEN(p);
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001311
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01001312 cells = text_prop_position(wp, tp, vcol,
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01001313 (vcol + size) % (wp->w_width - col_off) + col_off,
Bram Moolenaar56a40fe2022-12-06 14:17:57 +00001314 &n_extra, &p, NULL, NULL, FALSE);
zeertzjqce53e3e2023-09-01 18:49:30 +02001315# ifdef FEAT_LINEBREAK
Bram Moolenaarcba69522022-08-06 21:03:53 +01001316 no_sbr = TRUE; // don't use 'showbreak' now
zeertzjqce53e3e2023-09-01 18:49:30 +02001317# endif
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001318 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001319 else
1320 cells = vim_strsize(p);
Bram Moolenaar2f83cc42022-08-05 11:45:17 +01001321 cts->cts_cur_text_width += cells;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001322 if (tp->tp_flags & TP_FLAG_ALIGN_ABOVE)
1323 cts->cts_first_char += cells;
zeertzjqce53e3e2023-09-01 18:49:30 +02001324 else
1325 size += cells;
Bram Moolenaar28c9f892022-08-14 13:28:55 +01001326 cts->cts_start_incl = tp->tp_flags & TP_FLAG_START_INCL;
Bram Moolenaare428fa02022-08-09 16:55:41 +01001327 if (*s == TAB)
1328 {
1329 // tab size changes because of the inserted text
1330 size -= tab_size;
1331 tab_size = win_chartabsize(wp, s, vcol + size);
1332 size += tab_size;
1333 }
Bram Moolenaar55a27d82023-02-12 18:03:57 +00001334 if (tp->tp_col == MAXCOL && (tp->tp_flags
1335 & (TP_FLAG_ALIGN_ABOVE | TP_FLAG_ALIGN_BELOW)))
1336 // count extra line for property above/below
1337 ++cts->cts_prop_lines;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001338 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001339 }
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01001340 if (tp->tp_col != MAXCOL && tp->tp_col - 1 > col)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001341 break;
1342 }
zeertzjq11939512023-08-23 20:58:01 +02001343 if (has_lcs_eol)
1344 {
Bram Moolenaarf396ce82022-08-23 18:39:37 +01001345 --vcol;
zeertzjq11939512023-08-23 20:58:01 +02001346 ++size;
1347 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001348 }
1349# endif
1350
1351# ifdef FEAT_LINEBREAK
zeertzjqac2d8812023-08-31 18:07:30 +02001352 if (is_doublewidth && wp->w_p_wrap && in_win_border(wp, vcol + size - 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001354 ++size; // Count the ">" in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 mb_added = 1;
1356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
1358 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +02001359 * May have to add something for 'breakindent' and/or 'showbreak'
zeertzjq11939512023-08-23 20:58:01 +02001360 * string at the start of a screen line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 */
zeertzjqb557f482023-08-22 22:07:34 +02001362 int head = mb_added;
zeertzjq11939512023-08-23 20:58:01 +02001363 sbr = no_sbr ? empty_option : get_showbreak_value(wp);
1364 // When "size" is 0, no new screen line is started.
1365 if (size > 0 && wp->w_p_wrap && (*sbr != NUL || wp->w_p_bri))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
zeertzjqb557f482023-08-22 22:07:34 +02001367 int col_off_prev = win_col_off(wp);
1368 int width2 = wp->w_width - col_off_prev + win_col_off2(wp);
zeertzjqb557f482023-08-22 22:07:34 +02001369 colnr_T wcol = vcol + col_off_prev;
zeertzjqce53e3e2023-09-01 18:49:30 +02001370# ifdef FEAT_PROP_POPUP
zeertzjq6e55e852023-08-30 16:55:09 +02001371 wcol -= wp->w_virtcol_first_char;
zeertzjqce53e3e2023-09-01 18:49:30 +02001372# endif
zeertzjq1d3e0e82023-08-28 21:20:16 +02001373 colnr_T max_head_vcol = cts->cts_max_head_vcol;
1374 int added = 0;
1375
zeertzjqb557f482023-08-22 22:07:34 +02001376 // cells taken by 'showbreak'/'breakindent' before current char
1377 int head_prev = 0;
1378 if (wcol >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
zeertzjqb557f482023-08-22 22:07:34 +02001380 wcol -= wp->w_width;
1381 col_off_prev = wp->w_width - width2;
1382 if (wcol >= width2 && width2 > 0)
1383 wcol %= width2;
Bram Moolenaaree857022019-11-09 23:26:40 +01001384 if (*sbr != NUL)
zeertzjqb557f482023-08-22 22:07:34 +02001385 head_prev += vim_strsize(sbr);
1386 if (wp->w_p_bri)
zeertzjq5b0722b2024-01-17 20:42:53 +01001387 {
1388 if (cts->cts_bri_size < 0)
1389 cts->cts_bri_size = get_breakindent_win(wp, line);
1390 head_prev += cts->cts_bri_size;
1391 }
zeertzjqb557f482023-08-22 22:07:34 +02001392 if (wcol < head_prev)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001393 {
zeertzjq1d3e0e82023-08-28 21:20:16 +02001394 head_prev -= wcol;
1395 wcol += head_prev;
zeertzjqb557f482023-08-22 22:07:34 +02001396 added += head_prev;
1397 if (max_head_vcol <= 0 || vcol < max_head_vcol)
1398 head += head_prev;
1399 }
zeertzjq6e55e852023-08-30 16:55:09 +02001400 else
1401 head_prev = 0;
zeertzjq1d3e0e82023-08-28 21:20:16 +02001402 wcol += col_off_prev;
1403 }
zeertzjqb557f482023-08-22 22:07:34 +02001404
zeertzjq1d3e0e82023-08-28 21:20:16 +02001405 if (wcol + size > wp->w_width)
1406 {
zeertzjqb557f482023-08-22 22:07:34 +02001407 // cells taken by 'showbreak'/'breakindent' halfway current char
1408 int head_mid = 0;
1409 if (*sbr != NUL)
1410 head_mid += vim_strsize(sbr);
1411 if (wp->w_p_bri)
zeertzjq5b0722b2024-01-17 20:42:53 +01001412 {
1413 if (cts->cts_bri_size < 0)
1414 cts->cts_bri_size = get_breakindent_win(wp, line);
1415 head_mid += cts->cts_bri_size;
1416 }
zeertzjq5532d3b2024-03-28 10:04:25 +01001417 if (head_mid > 0)
zeertzjqb557f482023-08-22 22:07:34 +02001418 {
zeertzjq6a389722023-08-27 19:04:14 +02001419 // Calculate effective window width.
zeertzjq11939512023-08-23 20:58:01 +02001420 int prev_rem = wp->w_width - wcol;
1421 int width = width2 - head_mid;
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001422
zeertzjq11939512023-08-23 20:58:01 +02001423 if (width <= 0)
1424 width = 1;
zeertzjq6a389722023-08-27 19:04:14 +02001425 // Divide "size - prev_rem" by "width", rounding up.
zeertzjq11939512023-08-23 20:58:01 +02001426 int cnt = (size - prev_rem + width - 1) / width;
1427 added += cnt * head_mid;
zeertzjqb557f482023-08-22 22:07:34 +02001428
zeertzjq11939512023-08-23 20:58:01 +02001429 if (max_head_vcol == 0 || vcol + size + added < max_head_vcol)
1430 head += cnt * head_mid;
1431 else if (max_head_vcol > vcol + head_prev + prev_rem)
1432 head += (max_head_vcol - (vcol + head_prev + prev_rem)
zeertzjqb557f482023-08-22 22:07:34 +02001433 + width2 - 1) / width2 * head_mid;
zeertzjq11939512023-08-23 20:58:01 +02001434 else if (max_head_vcol < 0)
1435 {
zeertzjqb5d6b5c2024-07-18 21:13:31 +02001436 int off = mb_added;
1437# ifdef FEAT_PROP_POPUP
zeertzjq6e55e852023-08-30 16:55:09 +02001438 if (*s != NUL
zeertzjq11939512023-08-23 20:58:01 +02001439 && ((State & MODE_NORMAL) || cts->cts_start_incl))
1440 off += cts->cts_cur_text_width;
zeertzjqb5d6b5c2024-07-18 21:13:31 +02001441# endif
zeertzjq11939512023-08-23 20:58:01 +02001442 if (off >= prev_rem)
1443 head += (1 + (off - prev_rem) / width) * head_mid;
Bram Moolenaard574ea22015-01-14 19:35:14 +01001444 }
Bram Moolenaard574ea22015-01-14 19:35:14 +01001445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
zeertzjq1d3e0e82023-08-28 21:20:16 +02001447
1448 size += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
zeertzjq6e55e852023-08-30 16:55:09 +02001450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 if (headp != NULL)
zeertzjqb557f482023-08-22 22:07:34 +02001452 *headp = head;
zeertzjq6e55e852023-08-30 16:55:09 +02001453
zeertzjq703f9bc2024-01-25 21:27:13 +01001454 int need_lbr = FALSE;
zeertzjq6e55e852023-08-30 16:55:09 +02001455 /*
1456 * If 'linebreak' set check at a blank before a non-blank if the line
zeertzjq703f9bc2024-01-25 21:27:13 +01001457 * needs a break here.
zeertzjq6e55e852023-08-30 16:55:09 +02001458 */
zeertzjq703f9bc2024-01-25 21:27:13 +01001459 if (wp->w_p_lbr && wp->w_p_wrap && wp->w_width != 0
1460 && VIM_ISBREAK((int)s[0]) && !VIM_ISBREAK((int)s[1]))
Christian Brabandtdd75fcf2023-10-11 21:51:19 +02001461 {
1462 char_u *t = cts->cts_line;
Christian Brabandtcd6ee692023-10-14 11:29:28 +02001463 while (VIM_ISBREAK((int)t[0]))
Christian Brabandtdd75fcf2023-10-11 21:51:19 +02001464 t++;
zeertzjq703f9bc2024-01-25 21:27:13 +01001465 // 'linebreak' is only needed when not in leading whitespace.
1466 need_lbr = s >= t;
Christian Brabandtdd75fcf2023-10-11 21:51:19 +02001467 }
zeertzjq703f9bc2024-01-25 21:27:13 +01001468 if (need_lbr)
zeertzjq6e55e852023-08-30 16:55:09 +02001469 {
1470 /*
1471 * Count all characters from first non-blank after a blank up to next
1472 * non-blank after a blank.
1473 */
1474 int numberextra = win_col_off(wp);
1475 colnr_T col_adj = size - 1;
1476 colnr_T colmax = (colnr_T)(wp->w_width - numberextra - col_adj);
1477 if (vcol >= colmax)
1478 {
1479 colmax += col_adj;
1480 n = colmax + win_col_off2(wp);
1481 if (n > 0)
1482 colmax += (((vcol - colmax) / n) + 1) * n - col_adj;
1483 }
1484
1485 colnr_T vcol2 = vcol;
1486 for (;;)
1487 {
1488 char_u *ps = s;
1489 MB_PTR_ADV(s);
1490 int c = *s;
1491 if (!(c != NUL
1492 && (VIM_ISBREAK(c)
1493 || (!VIM_ISBREAK(c)
1494 && (vcol2 == vcol || !VIM_ISBREAK((int)*ps))))))
1495 break;
1496
1497 vcol2 += win_chartabsize(wp, s, vcol2);
1498 if (vcol2 >= colmax) // doesn't fit
1499 {
1500 size = colmax - vcol + col_adj;
1501 break;
1502 }
1503 }
1504 }
1505
zeertzjqce53e3e2023-09-01 18:49:30 +02001506# ifdef FEAT_PROP_POPUP
1507 size += cts->cts_first_char;
1508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 return size;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
1512}
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001515 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off, 'wrap'
1516 * is on and there are no properties that insert text. This means we need to
1517 * check for a double-byte character that doesn't fit at the end of the screen
1518 * line.
1519 * Only uses "cts_win", "cts_ptr" and "cts_vcol" from "cts".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
1521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522win_nolbr_chartabsize(
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001523 chartabsize_T *cts,
1524 int *headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001526 win_T *wp = cts->cts_win;
1527 char_u *s = cts->cts_ptr;
1528 colnr_T col = cts->cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 int n;
1530
Bram Moolenaareed9d462021-02-15 20:38:25 +01001531 if (*s == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001533# ifdef FEAT_VARTABS
1534 return tabstop_padding(col, wp->w_buffer->b_p_ts,
1535 wp->w_buffer->b_p_vts_array);
1536# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 n = wp->w_buffer->b_p_ts;
1538 return (int)(n - (col % n));
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
1541 n = ptr2cells(s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001542 // Add one cell for a double-width character in the last column of the
1543 // window, displayed with a ">".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col))
1545 {
1546 if (headp != NULL)
1547 *headp = 1;
1548 return 3;
1549 }
1550 return n;
1551}
1552
1553/*
1554 * Return TRUE if virtual column "vcol" is in the rightmost column of window
1555 * "wp".
1556 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001558in_win_border(win_T *wp, colnr_T vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001560 int width1; // width of first line (after line number)
1561 int width2; // width of further lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaarc667da52019-11-30 20:52:27 +01001563 if (wp->w_width == 0) // there is no border
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 return FALSE;
Bram Moolenaar02631462017-09-22 15:20:32 +02001565 width1 = wp->w_width - win_col_off(wp);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001566 if ((int)vcol < width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001568 if ((int)vcol == width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return TRUE;
1570 width2 = width1 + win_col_off2(wp);
Bram Moolenaar8701cd62009-10-07 14:20:30 +00001571 if (width2 <= 0)
1572 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 return ((vcol - width1) % width2 == width2 - 1);
1574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576/*
1577 * Get virtual column number of pos.
1578 * start: on the first position of this character (TAB, ctrl)
1579 * cursor: where the cursor is on this character (first char, except for TAB)
1580 * end: on the last position of this character (TAB, ctrl)
1581 *
1582 * This is used very often, keep it fast!
1583 */
1584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001585getvcol(
1586 win_T *wp,
1587 pos_T *pos,
1588 colnr_T *start,
1589 colnr_T *cursor,
1590 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
1592 colnr_T vcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001593 char_u *ptr; // points to current char
Bram Moolenaarc667da52019-11-30 20:52:27 +01001594 char_u *line; // start of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 int incr;
1596 int head;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001597#ifdef FEAT_VARTABS
1598 int *vts = wp->w_buffer->b_p_vts_array;
1599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 int ts = wp->w_buffer->b_p_ts;
1601 int c;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001602 chartabsize_T cts;
Bram Moolenaar49a90792022-08-09 18:25:23 +01001603#ifdef FEAT_PROP_POPUP
1604 int on_NUL = FALSE;
1605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 vcol = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001608 line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001610 init_chartabsize_arg(&cts, wp, pos->lnum, 0, line, line);
zeertzjqb557f482023-08-22 22:07:34 +02001611 cts.cts_max_head_vcol = -1;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /*
1614 * This function is used very often, do some speed optimizations.
Bram Moolenaar597a4222014-06-25 14:39:50 +02001615 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001616 * and there are no text properties with "text" use a simple loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 * Also use this when 'list' is set but tabs take their normal size.
1618 */
Bram Moolenaareed9d462021-02-15 20:38:25 +01001619 if ((!wp->w_p_list || wp->w_lcs_chars.tab1 != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001621 && !wp->w_p_lbr && *get_showbreak_value(wp) == NUL && !wp->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001623#ifdef FEAT_PROP_POPUP
1624 && !cts.cts_has_prop_with_text
1625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 )
1627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 for (;;)
1629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 head = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 c = *ptr;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001632 // make sure we don't go past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (c == NUL)
1634 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001635 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 break;
1637 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001638 // A tab gets expanded, depending on the current column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (c == TAB)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001640#ifdef FEAT_VARTABS
1641 incr = tabstop_padding(vcol, ts, vts);
1642#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 incr = ts - (vcol % ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 else
1646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (has_mbyte)
1648 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001649 // For utf-8, if the byte is >= 0x80, need to look at
1650 // further bytes to find the cell width.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (enc_utf8 && c >= 0x80)
1652 incr = utf_ptr2cells(ptr);
1653 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001654 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaarc667da52019-11-30 20:52:27 +01001656 // If a double-cell char doesn't fit at the end of a line
1657 // it wraps to the next line, it's like this char is three
1658 // cells wide.
Bram Moolenaar9c33a7c2008-02-20 13:59:32 +00001659 if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1
1660 && in_win_border(wp, vcol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
1662 ++incr;
1663 head = 1;
1664 }
1665 }
1666 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001667 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669
zeertzjq4ea37f82024-01-17 20:52:13 +01001670 char_u *next_ptr = ptr + (*mb_ptr2len)(ptr);
1671 if (next_ptr - line > pos->col) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 break;
1673
1674 vcol += incr;
zeertzjq4ea37f82024-01-17 20:52:13 +01001675 ptr = next_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677 }
1678 else
1679 {
1680 for (;;)
1681 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001682 // A tab gets expanded, depending on the current column.
1683 // Other things also take up space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 head = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001685 incr = win_lbr_chartabsize(&cts, &head);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001686 // make sure we don't go past the end of the line
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001687 if (*cts.cts_ptr == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001689 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar49a90792022-08-09 18:25:23 +01001690#ifdef FEAT_PROP_POPUP
1691 if (cts.cts_cur_text_width > 0)
1692 incr = cts.cts_cur_text_width;
1693 on_NUL = TRUE;
1694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 break;
1696 }
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001697#ifdef FEAT_PROP_POPUP
1698 if (cursor == &wp->w_virtcol && cts.cts_ptr == cts.cts_line)
1699 // do not count the virtual text above for w_curswant
1700 wp->w_virtcol_first_char = cts.cts_first_char;
1701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
zeertzjq4ea37f82024-01-17 20:52:13 +01001703 char_u *next_ptr = cts.cts_ptr + (*mb_ptr2len)(cts.cts_ptr);
1704 if (next_ptr - line > pos->col) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 break;
1706
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001707 cts.cts_vcol += incr;
zeertzjq4ea37f82024-01-17 20:52:13 +01001708 cts.cts_ptr = next_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001710 vcol = cts.cts_vcol;
1711 ptr = cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001713 clear_chartabsize_arg(&cts);
1714
Christian Brabandt396fd1e2024-08-31 17:58:16 +02001715 if (*ptr == NUL && pos->col < MAXCOL && pos->col > ptr - line)
1716 pos->col = ptr - line;
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (start != NULL)
1719 *start = vcol + head;
1720 if (end != NULL)
1721 *end = vcol + incr - 1;
1722 if (cursor != NULL)
1723 {
1724 if (*ptr == TAB
Bram Moolenaar24959102022-05-07 20:01:16 +01001725 && (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 && !wp->w_p_list
1727 && !virtual_active()
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001728 && !(VIsual_active
1729 && (*p_sel == 'e' || LTOREQ_POS(*pos, VIsual)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 )
Bram Moolenaarc667da52019-11-30 20:52:27 +01001731 *cursor = vcol + incr - 1; // cursor at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 else
Bram Moolenaare428fa02022-08-09 16:55:41 +01001733 {
1734#ifdef FEAT_PROP_POPUP
Bram Moolenaar28c9f892022-08-14 13:28:55 +01001735 // in Insert mode, if "start_incl" is true the text gets inserted
1736 // after the virtual text, thus add its width
1737 if (((State & MODE_INSERT) == 0 || cts.cts_start_incl) && !on_NUL)
Bram Moolenaar49a90792022-08-09 18:25:23 +01001738 // cursor is after inserted text, unless on the NUL
Bram Moolenaare428fa02022-08-09 16:55:41 +01001739 vcol += cts.cts_cur_text_width;
Bram Moolenaar88b79cb2022-09-10 22:32:14 +01001740 else
1741 // insertion also happens after the "above" virtual text
1742 vcol += cts.cts_first_char;
Bram Moolenaare428fa02022-08-09 16:55:41 +01001743#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001744 *cursor = vcol + head; // cursor at start
Bram Moolenaare428fa02022-08-09 16:55:41 +01001745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747}
1748
1749/*
1750 * Get virtual cursor column in the current window, pretending 'list' is off.
1751 */
1752 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753getvcol_nolist(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 int list_save = curwin->w_p_list;
1756 colnr_T vcol;
1757
1758 curwin->w_p_list = FALSE;
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001759 if (posp->coladd)
1760 getvvcol(curwin, posp, NULL, &vcol, NULL);
1761 else
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001762 getvcol(curwin, posp, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 curwin->w_p_list = list_save;
1764 return vcol;
1765}
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767/*
1768 * Get virtual column in virtual mode.
1769 */
1770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001771getvvcol(
1772 win_T *wp,
1773 pos_T *pos,
1774 colnr_T *start,
1775 colnr_T *cursor,
1776 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 colnr_T col;
1779 colnr_T coladd;
1780 colnr_T endadd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 if (virtual_active())
1784 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001785 // For virtual mode, only want one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 getvcol(wp, pos, &col, NULL, NULL);
1787
1788 coladd = pos->coladd;
1789 endadd = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001790 // Cannot put the cursor on part of a wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001792 if (pos->col < ml_get_buf_len(wp->w_buffer, pos->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
1794 int c = (*mb_ptr2char)(ptr + pos->col);
1795
1796 if (c != TAB && vim_isprintc(c))
1797 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001798 endadd = (colnr_T)(char2cells(c) - 1);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001799 if (coladd > endadd) // past end of line
Bram Moolenaara5792f52005-11-23 21:25:05 +00001800 endadd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 else
1802 coladd = 0;
1803 }
1804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 col += coladd;
1806 if (start != NULL)
1807 *start = col;
1808 if (cursor != NULL)
1809 *cursor = col;
1810 if (end != NULL)
1811 *end = col + endadd;
1812 }
1813 else
1814 getvcol(wp, pos, start, cursor, end);
1815}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
1818 * Get the leftmost and rightmost virtual column of pos1 and pos2.
1819 * Used for Visual block mode.
1820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822getvcols(
1823 win_T *wp,
1824 pos_T *pos1,
1825 pos_T *pos2,
1826 colnr_T *left,
1827 colnr_T *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 colnr_T from1, from2, to1, to2;
1830
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001831 if (LT_POSP(pos1, pos2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833 getvvcol(wp, pos1, &from1, NULL, &to1);
1834 getvvcol(wp, pos2, &from2, NULL, &to2);
1835 }
1836 else
1837 {
1838 getvvcol(wp, pos2, &from1, NULL, &to1);
1839 getvvcol(wp, pos1, &from2, NULL, &to2);
1840 }
1841 if (from2 < from1)
1842 *left = from2;
1843 else
1844 *left = from1;
1845 if (to2 > to1)
1846 {
1847 if (*p_sel == 'e' && from2 - 1 >= to1)
1848 *right = from2 - 1;
1849 else
1850 *right = to2;
1851 }
1852 else
1853 *right = to1;
1854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856/*
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001857 * Skip over ' ' and '\t'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 */
1859 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001860skipwhite(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001862 char_u *p = q;
1863
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001864 while (VIM_ISWHITE(*p))
1865 ++p;
1866 return p;
1867}
1868
Dominique Pelle748b3082022-01-08 12:41:16 +00001869#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001870/*
1871 * skip over ' ', '\t' and '\n'.
1872 */
1873 char_u *
1874skipwhite_and_nl(char_u *q)
1875{
1876 char_u *p = q;
1877
1878 while (VIM_ISWHITE(*p) || *p == NL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 ++p;
1880 return p;
1881}
Dominique Pelle748b3082022-01-08 12:41:16 +00001882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884/*
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001885 * getwhitecols: return the number of whitespace
1886 * columns (bytes) at the start of a given line
1887 */
1888 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001889getwhitecols_curline(void)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001890{
1891 return getwhitecols(ml_get_curline());
1892}
1893
1894 int
1895getwhitecols(char_u *p)
1896{
1897 return skipwhite(p) - p;
1898}
1899
1900/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001901 * skip over digits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 */
1903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001904skipdigits(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001906 char_u *p = q;
1907
Bram Moolenaarc667da52019-11-30 20:52:27 +01001908 while (VIM_ISDIGIT(*p)) // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 ++p;
1910 return p;
1911}
1912
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001913#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001914/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001915 * skip over binary digits
1916 */
1917 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918skipbin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001919{
1920 char_u *p = q;
1921
Bram Moolenaarc667da52019-11-30 20:52:27 +01001922 while (vim_isbdigit(*p)) // skip to next non-digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001923 ++p;
1924 return p;
1925}
1926
1927/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001928 * skip over digits and hex characters
1929 */
1930 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001931skiphex(char_u *q)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001932{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001933 char_u *p = q;
1934
Bram Moolenaarc667da52019-11-30 20:52:27 +01001935 while (vim_isxdigit(*p)) // skip to next non-digit
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001936 ++p;
1937 return p;
1938}
1939#endif
1940
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001941/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001942 * skip to bin digit (or NUL after the string)
1943 */
1944 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001945skiptobin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001946{
1947 char_u *p = q;
1948
Bram Moolenaarc667da52019-11-30 20:52:27 +01001949 while (*p != NUL && !vim_isbdigit(*p)) // skip to next digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001950 ++p;
1951 return p;
1952}
1953
1954/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001955 * skip to digit (or NUL after the string)
1956 */
1957 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001958skiptodigit(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001959{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001960 char_u *p = q;
1961
Bram Moolenaarc667da52019-11-30 20:52:27 +01001962 while (*p != NUL && !VIM_ISDIGIT(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001963 ++p;
1964 return p;
1965}
1966
1967/*
1968 * skip to hex character (or NUL after the string)
1969 */
1970 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001971skiptohex(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001972{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001973 char_u *p = q;
1974
Bram Moolenaarc667da52019-11-30 20:52:27 +01001975 while (*p != NUL && !vim_isxdigit(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001976 ++p;
1977 return p;
1978}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980/*
1981 * Variant of isdigit() that can handle characters > 0x100.
1982 * We don't use isdigit() here, because on some systems it also considers
1983 * superscript 1 to be a digit.
1984 * Use the VIM_ISDIGIT() macro for simple arguments.
1985 */
1986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001987vim_isdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 return (c >= '0' && c <= '9');
1990}
1991
1992/*
1993 * Variant of isxdigit() that can handle characters > 0x100.
1994 * We don't use isxdigit() here, because on some systems it also considers
1995 * superscript 1 to be a digit.
1996 */
1997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001998vim_isxdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 return (c >= '0' && c <= '9')
2001 || (c >= 'a' && c <= 'f')
2002 || (c >= 'A' && c <= 'F');
2003}
2004
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002005/*
2006 * Corollary of vim_isdigit and vim_isxdigit() that can handle
2007 * characters > 0x100.
2008 */
2009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002010vim_isbdigit(int c)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002011{
2012 return (c == '0' || c == '1');
2013}
2014
Bram Moolenaarc37b6552021-01-07 19:36:30 +01002015 static int
2016vim_isodigit(int c)
2017{
2018 return (c >= '0' && c <= '7');
2019}
2020
Bram Moolenaar78622822005-08-23 21:00:13 +00002021/*
2022 * Vim's own character class functions. These exist because many library
2023 * islower()/toupper() etc. do not work properly: they crash when used with
2024 * invalid values or can't handle latin1 when the locale is C.
2025 * Speed is most important here.
2026 */
2027#define LATIN1LOWER 'l'
2028#define LATIN1UPPER 'U'
2029
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00002030static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll";
Bram Moolenaar936347b2012-05-25 11:56:22 +02002031static 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";
2032static 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 +00002033
2034 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002035vim_islower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002036{
2037 if (c <= '@')
2038 return FALSE;
2039 if (c >= 0x80)
2040 {
2041 if (enc_utf8)
2042 return utf_islower(c);
2043 if (c >= 0x100)
2044 {
2045#ifdef HAVE_ISWLOWER
2046 if (has_mbyte)
2047 return iswlower(c);
2048#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002049 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002050 return FALSE;
2051 }
2052 if (enc_latin1like)
2053 return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER;
2054 }
Keith Thompson184f71c2024-01-04 21:19:04 +01002055 return SAFE_islower(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002056}
2057
2058 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002059vim_isupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002060{
2061 if (c <= '@')
2062 return FALSE;
2063 if (c >= 0x80)
2064 {
2065 if (enc_utf8)
2066 return utf_isupper(c);
2067 if (c >= 0x100)
2068 {
2069#ifdef HAVE_ISWUPPER
2070 if (has_mbyte)
2071 return iswupper(c);
2072#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002073 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002074 return FALSE;
2075 }
2076 if (enc_latin1like)
2077 return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER;
2078 }
Keith Thompson184f71c2024-01-04 21:19:04 +01002079 return SAFE_isupper(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002080}
2081
2082 int
Bram Moolenaar5921aeb2022-02-19 11:20:12 +00002083vim_isalpha(int c)
2084{
2085 return vim_islower(c) || vim_isupper(c);
2086}
2087
2088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002089vim_toupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002090{
2091 if (c <= '@')
2092 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02002093 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00002094 {
2095 if (enc_utf8)
2096 return utf_toupper(c);
2097 if (c >= 0x100)
2098 {
2099#ifdef HAVE_TOWUPPER
2100 if (has_mbyte)
2101 return towupper(c);
2102#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002103 // toupper() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002104 return c;
2105 }
2106 if (enc_latin1like)
2107 return latin1upper[c];
2108 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002109 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
2110 return TOUPPER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002111 return TOUPPER_LOC(c);
2112}
2113
2114 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002115vim_tolower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00002116{
2117 if (c <= '@')
2118 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02002119 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00002120 {
2121 if (enc_utf8)
2122 return utf_tolower(c);
2123 if (c >= 0x100)
2124 {
2125#ifdef HAVE_TOWLOWER
2126 if (has_mbyte)
2127 return towlower(c);
2128#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002129 // tolower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00002130 return c;
2131 }
2132 if (enc_latin1like)
2133 return latin1lower[c];
2134 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02002135 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
2136 return TOLOWER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00002137 return TOLOWER_LOC(c);
2138}
Bram Moolenaar78622822005-08-23 21:00:13 +00002139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140/*
2141 * skiptowhite: skip over text until ' ' or '\t' or NUL.
2142 */
2143 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002144skiptowhite(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 while (*p != ' ' && *p != '\t' && *p != NUL)
2147 ++p;
2148 return p;
2149}
2150
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151/*
2152 * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars
2153 */
2154 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002155skiptowhite_esc(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 while (*p != ' ' && *p != '\t' && *p != NUL)
2158 {
2159 if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL)
2160 ++p;
2161 ++p;
2162 }
2163 return p;
2164}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
2166/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002167 * Get a number from a string and skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 * Note: the argument is a pointer to a char_u pointer!
2169 */
2170 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171getdigits(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 char_u *p;
2174 long retval;
2175
2176 p = *pp;
2177 retval = atol((char *)p);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002178 if (*p == '-') // skip negative sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 ++p;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002180 p = skipdigits(p); // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 *pp = p;
2182 return retval;
2183}
2184
2185/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002186 * Like getdigits() but allow for embedded single quotes.
2187 */
2188 long
2189getdigits_quoted(char_u **pp)
2190{
2191 char_u *p = *pp;
2192 long retval = 0;
2193
2194 if (*p == '-')
2195 ++p;
2196 while (VIM_ISDIGIT(*p))
2197 {
2198 if (retval >= LONG_MAX / 10 - 10)
2199 retval = LONG_MAX;
2200 else
2201 retval = retval * 10 - '0' + *p;
2202 ++p;
2203 if (in_vim9script() && *p == '\'' && VIM_ISDIGIT(p[1]))
2204 ++p;
2205 }
2206 if (**pp == '-')
2207 {
2208 if (retval == LONG_MAX)
2209 retval = LONG_MIN;
2210 else
2211 retval = -retval;
2212 }
2213 *pp = p;
2214 return retval;
2215}
2216
2217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 * Return TRUE if "lbuf" is empty or only contains blanks.
2219 */
2220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002221vim_isblankline(char_u *lbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 char_u *p;
2224
2225 p = skipwhite(lbuf);
2226 return (*p == NUL || *p == '\r' || *p == '\n');
2227}
2228
2229/*
2230 * Convert a string into a long and/or unsigned long, taking care of
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002231 * hexadecimal, octal, and binary numbers. Accepts a '-' sign.
2232 * If "prep" is not NULL, returns a flag to indicate the type of the number:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 * 0 decimal
2234 * '0' octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002235 * 'O' octal
2236 * 'o' octal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002237 * 'B' bin
2238 * 'b' bin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 * 'X' hex
2240 * 'x' hex
2241 * If "len" is not NULL, the length of the number in characters is returned.
2242 * If "nptr" is not NULL, the signed result is returned in it.
2243 * If "unptr" is not NULL, the unsigned result is returned in it.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002244 * If "what" contains STR2NR_BIN recognize binary numbers
2245 * If "what" contains STR2NR_OCT recognize octal numbers
2246 * If "what" contains STR2NR_HEX recognize hex numbers
2247 * If "what" contains STR2NR_FORCE always assume bin/oct/hex.
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002248 * If "what" contains STR2NR_QUOTE ignore embedded single quotes
Bram Moolenaarce157752017-10-28 16:07:33 +02002249 * If maxlen > 0, check at a maximum maxlen chars.
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002250 * If strict is TRUE, check the number strictly. return *len = 0 if fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
2252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002253vim_str2nr(
2254 char_u *start,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002255 int *prep, // return: type of number 0 = decimal, 'x'
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002256 // or 'X' is hex, '0', 'o' or 'O' is octal,
2257 // 'b' or 'B' is bin
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002258 int *len, // return: detected length of number
2259 int what, // what numbers to recognize
2260 varnumber_T *nptr, // return: signed result
2261 uvarnumber_T *unptr, // return: unsigned result
2262 int maxlen, // max length of string to check
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002263 int strict, // check strictly
2264 int *overflow) // when not NULL set to TRUE for overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 char_u *ptr = start;
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002267 int pre = 0; // default is decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 int negative = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002269 uvarnumber_T un = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002270 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002272 if (len != NULL)
2273 *len = 0;
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (ptr[0] == '-')
2276 {
2277 negative = TRUE;
2278 ++ptr;
2279 }
2280
Bram Moolenaarc667da52019-11-30 20:52:27 +01002281 // Recognize hex, octal, and bin.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002282 if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9'
2283 && (maxlen == 0 || maxlen > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002285 pre = ptr[1];
2286 if ((what & STR2NR_HEX)
2287 && (pre == 'X' || pre == 'x') && vim_isxdigit(ptr[2])
2288 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002289 // hexadecimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002290 ptr += 2;
2291 else if ((what & STR2NR_BIN)
2292 && (pre == 'B' || pre == 'b') && vim_isbdigit(ptr[2])
2293 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002294 // binary
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002295 ptr += 2;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002296 else if ((what & STR2NR_OOCT)
Bram Moolenaarc37b6552021-01-07 19:36:30 +01002297 && (pre == 'O' || pre == 'o') && vim_isodigit(ptr[2])
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002298 && (maxlen == 0 || maxlen > 2))
2299 // octal with prefix "0o"
2300 ptr += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 else
2302 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002303 // decimal or octal, default is decimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002304 pre = 0;
2305 if (what & STR2NR_OCT)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002306 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002307 // Don't interpret "0", "08" or "0129" as octal.
Bram Moolenaarce157752017-10-28 16:07:33 +02002308 for (n = 1; n != maxlen && VIM_ISDIGIT(ptr[n]); ++n)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002309 {
2310 if (ptr[n] > '7')
2311 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002312 pre = 0; // can't be octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002313 break;
2314 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002315 pre = '0'; // assume octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002316 }
2317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319 }
2320
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002321 // Do the conversion manually to avoid sscanf() quirks.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002322 n = 1;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002323 if (pre == 'B' || pre == 'b'
2324 || ((what & STR2NR_BIN) && (what & STR2NR_FORCE)))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002325 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002326 // bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002327 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002328 n += 2; // skip over "0b"
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002329 while ('0' <= *ptr && *ptr <= '1')
2330 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002331 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002332 if (un <= UVARNUM_MAX / 2)
2333 un = 2 * un + (uvarnumber_T)(*ptr - '0');
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002334 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002335 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002336 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002337 if (overflow != NULL)
2338 *overflow = TRUE;
2339 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002340 ++ptr;
2341 if (n++ == maxlen)
2342 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002343 if ((what & STR2NR_QUOTE) && *ptr == '\''
2344 && '0' <= ptr[1] && ptr[1] <= '1')
2345 {
2346 ++ptr;
2347 if (n++ == maxlen)
2348 break;
2349 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002350 }
2351 }
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002352 else if (pre == 'O' || pre == 'o' ||
2353 pre == '0' || ((what & STR2NR_OCT) && (what & STR2NR_FORCE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002355 // octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002356 if (pre != 0 && pre != '0')
2357 n += 2; // skip over "0o"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002358 while ('0' <= *ptr && *ptr <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002360 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002361 if (un <= UVARNUM_MAX / 8)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002362 un = 8 * un + (uvarnumber_T)(*ptr - '0');
2363 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002364 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002365 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002366 if (overflow != NULL)
2367 *overflow = TRUE;
2368 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002369 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002370 if (n++ == maxlen)
2371 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002372 if ((what & STR2NR_QUOTE) && *ptr == '\''
2373 && '0' <= ptr[1] && ptr[1] <= '7')
2374 {
2375 ++ptr;
2376 if (n++ == maxlen)
2377 break;
2378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002380 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002381 else if (pre != 0 || ((what & STR2NR_HEX) && (what & STR2NR_FORCE)))
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002382 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002383 // hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002384 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002385 n += 2; // skip over "0x"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002386 while (vim_isxdigit(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002388 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002389 if (un <= UVARNUM_MAX / 16)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002390 un = 16 * un + (uvarnumber_T)hex2nr(*ptr);
2391 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002392 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002393 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002394 if (overflow != NULL)
2395 *overflow = TRUE;
2396 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002397 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002398 if (n++ == maxlen)
2399 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002400 if ((what & STR2NR_QUOTE) && *ptr == '\'' && vim_isxdigit(ptr[1]))
2401 {
2402 ++ptr;
2403 if (n++ == maxlen)
2404 break;
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407 }
2408 else
2409 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002410 // decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 while (VIM_ISDIGIT(*ptr))
2412 {
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002413 uvarnumber_T digit = (uvarnumber_T)(*ptr - '0');
2414
Bram Moolenaarc667da52019-11-30 20:52:27 +01002415 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002416 if (un < UVARNUM_MAX / 10
2417 || (un == UVARNUM_MAX / 10 && digit <= UVARNUM_MAX % 10))
2418 un = 10 * un + digit;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002419 else
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002420 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002421 un = UVARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002422 if (overflow != NULL)
2423 *overflow = TRUE;
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002426 if (n++ == maxlen)
2427 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002428 if ((what & STR2NR_QUOTE) && *ptr == '\'' && VIM_ISDIGIT(ptr[1]))
2429 {
2430 ++ptr;
2431 if (n++ == maxlen)
2432 break;
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002436
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002437 // Check for an alphanumeric character immediately following, that is
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002438 // most likely a typo.
2439 if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr))
2440 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002442 if (prep != NULL)
2443 *prep = pre;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (len != NULL)
2445 *len = (int)(ptr - start);
2446 if (nptr != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002447 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002448 if (negative) // account for leading '-' for decimal numbers
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002449 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002450 // avoid ubsan error for overflow
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002451 if (un > VARNUM_MAX)
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002452 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002453 *nptr = VARNUM_MIN;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002454 if (overflow != NULL)
2455 *overflow = TRUE;
2456 }
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002457 else
2458 *nptr = -(varnumber_T)un;
2459 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002460 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002461 {
dundargocc57b5bc2022-11-02 13:30:51 +00002462 // prevent a large unsigned number to become negative
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 un = VARNUM_MAX;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002466 if (overflow != NULL)
2467 *overflow = TRUE;
2468 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002469 *nptr = (varnumber_T)un;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002470 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (unptr != NULL)
2473 *unptr = un;
2474}
2475
2476/*
2477 * Return the value of a single hex character.
2478 * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'.
2479 */
2480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002481hex2nr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 if (c >= 'a' && c <= 'f')
2484 return c - 'a' + 10;
2485 if (c >= 'A' && c <= 'F')
2486 return c - 'A' + 10;
2487 return c - '0';
2488}
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490/*
2491 * Convert two hex characters to a byte.
2492 * Return -1 if one of the characters is not hex.
2493 */
2494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002495hexhex2nr(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1]))
2498 return -1;
2499 return (hex2nr(p[0]) << 4) + hex2nr(p[1]);
2500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502/*
2503 * Return TRUE if "str" starts with a backslash that should be removed.
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01002504 * For MS-DOS, MSWIN and OS/2 this is only done when the character after the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * backslash is not a normal file name character.
2506 * '$' is a valid file name character, we don't remove the backslash before
2507 * it. This means it is not possible to use an environment variable after a
2508 * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works.
2509 * Although "\ name" is valid, the backslash in "Program\ files" must be
2510 * removed. Assume a file name doesn't start with a space.
2511 * For multi-byte names, never remove a backslash before a non-ascii
2512 * character, assume that all multi-byte characters are valid file name
2513 * characters.
2514 */
2515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002516rem_backslash(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
2518#ifdef BACKSLASH_IN_FILENAME
2519 return (str[0] == '\\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 && str[1] < 0x80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 && (str[1] == ' '
2522 || (str[1] != NUL
2523 && str[1] != '*'
2524 && str[1] != '?'
2525 && !vim_isfilec(str[1]))));
2526#else
2527 return (str[0] == '\\' && str[1] != NUL);
2528#endif
2529}
2530
2531/*
2532 * Halve the number of backslashes in a file name argument.
2533 * For MS-DOS we only do this if the character after the backslash
2534 * is not a normal file character.
2535 */
2536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002537backslash_halve(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 for ( ; *p; ++p)
2540 if (rem_backslash(p))
Bram Moolenaar446cb832008-06-24 21:56:24 +00002541 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542}
2543
2544/*
2545 * backslash_halve() plus save the result in allocated memory.
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002546 * However, returns "p" when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 */
2548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549backslash_halve_save(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 char_u *res;
2552
2553 res = vim_strsave(p);
2554 if (res == NULL)
2555 return p;
2556 backslash_halve(res);
2557 return res;
2558}