blob: f3d12cbe86b9a142f2100c97ad927f8ccca0328e [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
Bram Moolenaar7f9969c2022-07-25 18:13:54 +010016static int win_nolbr_chartabsize(chartabsize_T *cts, int *headp);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010017static unsigned nr2hex(unsigned c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19static int chartab_initialized = FALSE;
20
Bram Moolenaarc667da52019-11-30 20:52:27 +010021// b_chartab[] is an array of 32 bytes, each bit representing one of the
22// characters 0-255.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#define SET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] |= (1 << ((c) & 0x7))
24#define RESET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] &= ~(1 << ((c) & 0x7))
25#define GET_CHARTAB(buf, c) ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7)))
26
Bram Moolenaarc667da52019-11-30 20:52:27 +010027// table used below, see init_chartab() for an explanation
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010028static char_u g_chartab[256];
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010031 * Flags for g_chartab[].
32 */
Bram Moolenaarc667da52019-11-30 20:52:27 +010033#define CT_CELL_MASK 0x07 // mask: nr of display cells (1, 2 or 4)
34#define CT_PRINT_CHAR 0x10 // flag: set for printable chars
35#define CT_ID_CHAR 0x20 // flag: set for ID chars
36#define CT_FNAME_CHAR 0x40 // flag: set for file name chars
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010037
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static int in_win_border(win_T *wp, colnr_T vcol);
39
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010040/*
41 * Fill g_chartab[]. Also fills curbuf->b_chartab[] with flags for keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 * characters for current buffer.
43 *
44 * Depends on the option settings 'iskeyword', 'isident', 'isfname',
45 * 'isprint' and 'encoding'.
46 *
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010047 * The index in g_chartab[] depends on 'encoding':
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 * - For non-multi-byte index with the byte (same as the character).
49 * - For DBCS index with the first byte.
50 * - For UTF-8 index with the character (when first byte is up to 0x80 it is
51 * the same as the character, if the first byte is 0x80 and above it depends
52 * on further bytes).
53 *
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010054 * The contents of g_chartab[]:
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 * - The lower two bits, masked by CT_CELL_MASK, give the number of display
56 * cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80.
57 * - CT_PRINT_CHAR bit is set when the character is printable (no need to
58 * translate the character before displaying it). Note that only DBCS
59 * characters can have 2 display cells and still be printable.
60 * - CT_FNAME_CHAR bit is set when the character can be in a file name.
61 * - CT_ID_CHAR bit is set when the character can be in an identifier.
62 *
63 * Return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has an
64 * error, OK otherwise.
65 */
66 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010067init_chartab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 return buf_init_chartab(curbuf, TRUE);
70}
71
72 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010073buf_init_chartab(
74 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +010075 int global) // FALSE: only set buf->b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 int c;
78 int c2;
79 char_u *p;
80 int i;
81 int tilde;
82 int do_isalpha;
83
84 if (global)
85 {
86 /*
87 * Set the default size for printable characters:
88 * From <Space> to '~' is 1 (printable), others are 2 (not printable).
89 * This also inits all 'isident' and 'isfname' flags to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 */
91 c = 0;
92 while (c < ' ')
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010093 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 while (c <= '~')
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010095 g_chartab[c++] = 1 + CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 while (c < 256)
97 {
Bram Moolenaarc667da52019-11-30 20:52:27 +010098 // UTF-8: bytes 0xa0 - 0xff are printable (latin1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 if (enc_utf8 && c >= 0xa0)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100100 g_chartab[c++] = CT_PRINT_CHAR + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100101 // euc-jp characters starting with 0x8e are single width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100103 g_chartab[c++] = CT_PRINT_CHAR + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100104 // other double-byte chars can be printable AND double-width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100106 g_chartab[c++] = CT_PRINT_CHAR + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 else
Bram Moolenaarc667da52019-11-30 20:52:27 +0100108 // the rest is unprintable by default
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100109 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 }
111
Bram Moolenaarc667da52019-11-30 20:52:27 +0100112 // Assume that every multi-byte char is a filename character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 for (c = 1; c < 256; ++c)
114 if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1)
115 || (enc_dbcs == DBCS_JPNU && c == 0x8e)
116 || (enc_utf8 && c >= 0xa0))
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100117 g_chartab[c] |= CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 }
119
120 /*
121 * Init word char flags all to FALSE
122 */
Bram Moolenaara80faa82020-04-12 19:37:17 +0200123 CLEAR_FIELD(buf->b_chartab);
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000124 if (enc_dbcs != 0)
125 for (c = 0; c < 256; ++c)
126 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100127 // double-byte characters are probably word characters
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000128 if (MB_BYTE2LEN(c) == 2)
129 SET_CHARTAB(buf, c);
130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 /*
133 * In lisp mode the '-' character is included in keywords.
134 */
135 if (buf->b_p_lisp)
136 SET_CHARTAB(buf, '-');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaarc667da52019-11-30 20:52:27 +0100138 // Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint'
139 // options Each option is a list of characters, character numbers or
140 // ranges, separated by commas, e.g.: "200-210,x,#-178,-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 for (i = global ? 0 : 3; i <= 3; ++i)
142 {
143 if (i == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100144 p = p_isi; // first round: 'isident'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 else if (i == 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100146 p = p_isp; // second round: 'isprint'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 else if (i == 2)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100148 p = p_isf; // third round: 'isfname'
149 else // i == 3
150 p = buf->b_p_isk; // fourth round: 'iskeyword'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152 while (*p)
153 {
154 tilde = FALSE;
155 do_isalpha = FALSE;
156 if (*p == '^' && p[1] != NUL)
157 {
158 tilde = TRUE;
159 ++p;
160 }
161 if (VIM_ISDIGIT(*p))
162 c = getdigits(&p);
Dominique Pelle4781d6f2021-05-18 21:46:31 +0200163 else if (has_mbyte)
Bram Moolenaar183bb3e2009-09-11 12:02:34 +0000164 c = mb_ptr2char_adv(&p);
165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 c = *p++;
167 c2 = -1;
168 if (*p == '-' && p[1] != NUL)
169 {
170 ++p;
171 if (VIM_ISDIGIT(*p))
172 c2 = getdigits(&p);
Dominique Pelle4781d6f2021-05-18 21:46:31 +0200173 else if (has_mbyte)
Bram Moolenaar2ac5e602009-11-03 15:04:20 +0000174 c2 = mb_ptr2char_adv(&p);
175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 c2 = *p++;
177 }
Bram Moolenaar2ac5e602009-11-03 15:04:20 +0000178 if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 || !(*p == NUL || *p == ','))
180 return FAIL;
181
Bram Moolenaarc667da52019-11-30 20:52:27 +0100182 if (c2 == -1) // not a range
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
184 /*
185 * A single '@' (not "@-@"):
186 * Decide on letters being ID/printable/keyword chars with
187 * standard function isalpha(). This takes care of locale for
188 * single-byte characters).
189 */
190 if (c == '@')
191 {
192 do_isalpha = TRUE;
193 c = 1;
194 c2 = 255;
195 }
196 else
197 c2 = c;
198 }
199 while (c <= c2)
200 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100201 // Use the MB_ functions here, because isalpha() doesn't
202 // work properly when 'encoding' is "latin1" and the locale is
203 // "C".
Bram Moolenaar14184a32019-02-16 15:10:30 +0100204 if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100206 if (i == 0) // (re)set ID flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 if (tilde)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100209 g_chartab[c] &= ~CT_ID_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100211 g_chartab[c] |= CT_ID_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100213 else if (i == 1) // (re)set printable
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000215 if ((c < ' ' || c > '~'
Bram Moolenaar13505972019-01-24 15:04:48 +0100216 // For double-byte we keep the cell width, so
217 // that we can detect it from the first byte.
218 ) && !(enc_dbcs && MB_BYTE2LEN(c) == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 {
220 if (tilde)
221 {
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100222 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 + ((dy_flags & DY_UHEX) ? 4 : 2);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100224 g_chartab[c] &= ~CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 }
226 else
227 {
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100228 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) + 1;
229 g_chartab[c] |= CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
231 }
232 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100233 else if (i == 2) // (re)set fname flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 {
235 if (tilde)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100236 g_chartab[c] &= ~CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100238 g_chartab[c] |= CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100240 else // i == 3 (re)set keyword flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {
242 if (tilde)
243 RESET_CHARTAB(buf, c);
244 else
245 SET_CHARTAB(buf, c);
246 }
247 }
248 ++c;
249 }
Bram Moolenaar309379f2013-02-06 16:26:26 +0100250
251 c = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 p = skip_to_option_part(p);
Bram Moolenaar309379f2013-02-06 16:26:26 +0100253 if (c == ',' && *p == NUL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100254 // Trailing comma is not allowed.
Bram Moolenaar309379f2013-02-06 16:26:26 +0100255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 }
257 }
258 chartab_initialized = TRUE;
259 return OK;
260}
261
262/*
263 * Translate any special characters in buf[bufsize] in-place.
264 * The result is a string with only printable characters, but if there is not
265 * enough room, not all characters will be translated.
266 */
267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100268trans_characters(
269 char_u *buf,
270 int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100272 int len; // length of string needing translation
273 int room; // room in buffer after string
274 char_u *trs; // translated character
275 int trs_len; // length of trs[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276
277 len = (int)STRLEN(buf);
278 room = bufsize - len;
279 while (*buf != 0)
280 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100281 // Assume a multi-byte character doesn't need translation.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000282 if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 len -= trs_len;
284 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {
286 trs = transchar_byte(*buf);
287 trs_len = (int)STRLEN(trs);
288 if (trs_len > 1)
289 {
290 room -= trs_len - 1;
291 if (room <= 0)
292 return;
293 mch_memmove(buf + trs_len, buf + 1, (size_t)len);
294 }
295 mch_memmove(buf, trs, (size_t)trs_len);
296 --len;
297 }
298 buf += trs_len;
299 }
300}
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302/*
303 * Translate a string into allocated memory, replacing special chars with
304 * printable chars. Returns NULL when out of memory.
305 */
306 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100307transstr(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308{
309 char_u *res;
310 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 int l, len, c;
312 char_u hexbuf[11];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (has_mbyte)
315 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100316 // Compute the length of the result, taking account of unprintable
317 // multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 len = 0;
319 p = s;
320 while (*p != NUL)
321 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000322 if ((l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 c = (*mb_ptr2char)(p);
325 p += l;
326 if (vim_isprintc(c))
327 len += l;
328 else
329 {
330 transchar_hex(hexbuf, c);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000331 len += (int)STRLEN(hexbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 }
333 }
334 else
335 {
336 l = byte2cells(*p++);
337 if (l > 0)
338 len += l;
339 else
Bram Moolenaarc667da52019-11-30 20:52:27 +0100340 len += 4; // illegal byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
342 }
Bram Moolenaar964b3742019-05-24 18:54:09 +0200343 res = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 }
345 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200346 res = alloc(vim_strsize(s) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (res != NULL)
348 {
349 *res = NUL;
350 p = s;
351 while (*p != NUL)
352 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000353 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 c = (*mb_ptr2char)(p);
356 if (vim_isprintc(c))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100357 STRNCAT(res, p, l); // append printable multi-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 else
359 transchar_hex(res + STRLEN(res), c);
360 p += l;
361 }
362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 STRCAT(res, transchar_byte(*p++));
364 }
365 }
366 return res;
367}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369/*
Bram Moolenaar217ad922005-03-20 22:37:15 +0000370 * Convert the string "str[orglen]" to do ignore-case comparing. Uses the
371 * current locale.
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000372 * When "buf" is NULL returns an allocated string (NULL for out-of-memory).
373 * Otherwise puts the result in "buf[buflen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 */
375 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100376str_foldcase(
377 char_u *str,
378 int orglen,
379 char_u *buf,
380 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
382 garray_T ga;
383 int i;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000384 int len = orglen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
386#define GA_CHAR(i) ((char_u *)ga.ga_data)[i]
kylo252ae6f1d82022-02-16 19:24:07 +0000387#define GA_PTR(i) ((char_u *)ga.ga_data + (i))
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000388#define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i])
kylo252ae6f1d82022-02-16 19:24:07 +0000389#define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + (i))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
Bram Moolenaarc667da52019-11-30 20:52:27 +0100391 // Copy "str" into "buf" or allocated memory, unmodified.
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000392 if (buf == NULL)
393 {
394 ga_init2(&ga, 1, 10);
395 if (ga_grow(&ga, len + 1) == FAIL)
396 return NULL;
397 mch_memmove(ga.ga_data, str, (size_t)len);
398 ga.ga_len = len;
399 }
400 else
401 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100402 if (len >= buflen) // Ugly!
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000403 len = buflen - 1;
404 mch_memmove(buf, str, (size_t)len);
405 }
406 if (buf == NULL)
407 GA_CHAR(len) = NUL;
408 else
409 buf[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410
Bram Moolenaarc667da52019-11-30 20:52:27 +0100411 // Make each character lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 i = 0;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000413 while (STR_CHAR(i) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000415 if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {
417 if (enc_utf8)
418 {
Bram Moolenaarb9839212008-06-28 11:03:50 +0000419 int c = utf_ptr2char(STR_PTR(i));
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100420 int olen = utf_ptr2len(STR_PTR(i));
Bram Moolenaarb9839212008-06-28 11:03:50 +0000421 int lc = utf_tolower(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaarc667da52019-11-30 20:52:27 +0100423 // Only replace the character when it is not an invalid
424 // sequence (ASCII character or more than one byte) and
425 // utf_tolower() doesn't return the original character.
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100426 if ((c < 0x80 || olen > 1) && c != lc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100428 int nlen = utf_char2len(lc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
Bram Moolenaarc667da52019-11-30 20:52:27 +0100430 // If the byte length changes need to shift the following
431 // characters forward or backward.
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100432 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100434 if (nlen > olen)
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000435 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100436 if (buf == NULL
437 ? ga_grow(&ga, nlen - olen + 1) == FAIL
438 : len + nlen - olen >= buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100440 // out of memory, keep old char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 lc = c;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100442 nlen = olen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000444 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100445 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000447 if (buf == NULL)
448 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100449 STRMOVE(GA_PTR(i) + nlen, GA_PTR(i) + olen);
450 ga.ga_len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000451 }
452 else
453 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100454 STRMOVE(buf + i + nlen, buf + i + olen);
455 len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 }
458 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000459 (void)utf_char2bytes(lc, STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
461 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100462 // skip to next multi-byte char
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000463 i += (*mb_ptr2len)(STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 }
465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000467 if (buf == NULL)
468 GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i));
469 else
470 buf[i] = TOLOWER_LOC(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 ++i;
472 }
473 }
474
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000475 if (buf == NULL)
476 return (char_u *)ga.ga_data;
477 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
480/*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100481 * Catch 22: g_chartab[] can't be initialized before the options are
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * initialized, and initializing options may cause transchar() to be called!
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100483 * When chartab_initialized == FALSE don't use g_chartab[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 * Does NOT work for multi-byte characters, c must be <= 255.
485 * Also doesn't work for the first byte of a multi-byte, "c" must be a
486 * character!
487 */
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200488static char_u transchar_charbuf[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100491transchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200493 return transchar_buf(curbuf, c);
494}
495
496 char_u *
497transchar_buf(buf_T *buf, int c)
498{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 int i;
500
501 i = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100502 if (IS_SPECIAL(c)) // special key code, display as ~@ char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200504 transchar_charbuf[0] = '~';
505 transchar_charbuf[1] = '@';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 i = 2;
507 c = K_SECOND(c);
508 }
509
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000510 if ((!chartab_initialized && ((c >= ' ' && c <= '~')))
511 || (c < 256 && vim_isprintc_strict(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100513 // printable character
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200514 transchar_charbuf[i] = c;
515 transchar_charbuf[i + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 }
517 else
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200518 transchar_nonprint(buf, transchar_charbuf + i, c);
519 return transchar_charbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520}
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522/*
523 * Like transchar(), but called with a byte instead of a character. Checks
524 * for an illegal UTF-8 byte.
525 */
526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100527transchar_byte(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
529 if (enc_utf8 && c >= 0x80)
530 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200531 transchar_nonprint(curbuf, transchar_charbuf, c);
532 return transchar_charbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
534 return transchar(c);
535}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537/*
538 * Convert non-printable character to two or more printable characters in
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200539 * "buf[]". "charbuf" needs to be able to hold five bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * Does NOT work for multi-byte characters, c must be <= 255.
541 */
542 void
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200543transchar_nonprint(buf_T *buf, char_u *charbuf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 if (c == NL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100546 c = NUL; // we use newline in place of a NUL
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200547 else if (c == CAR && get_fileformat(buf) == EOL_MAC)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100548 c = NL; // we use CR in place of NL in this case
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaarc667da52019-11-30 20:52:27 +0100550 if (dy_flags & DY_UHEX) // 'display' has "uhex"
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200551 transchar_hex(charbuf, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaarc667da52019-11-30 20:52:27 +0100553 else if (c <= 0x7f) // 0x00 - 0x1f and 0x7f
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200555 charbuf[0] = '^';
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200556 charbuf[1] = c ^ 0x40; // DEL displayed as ^?
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200557 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +0000559 else if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200561 transchar_hex(charbuf, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100563 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) // 0xa0 - 0xfe
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200565 charbuf[0] = '|';
566 charbuf[1] = c - 0x80;
567 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100569 else // 0x80 - 0x9f and 0xff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200571 charbuf[0] = '~';
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200572 charbuf[1] = (c - 0x80) ^ 0x40; // 0xff displayed as ~?
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200573 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575}
576
577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100578transchar_hex(char_u *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 int i = 0;
581
582 buf[0] = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (c > 255)
584 {
585 buf[++i] = nr2hex((unsigned)c >> 12);
586 buf[++i] = nr2hex((unsigned)c >> 8);
587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 buf[++i] = nr2hex((unsigned)c >> 4);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000589 buf[++i] = nr2hex((unsigned)c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 buf[++i] = '>';
591 buf[++i] = NUL;
592}
593
594/*
595 * Convert the lower 4 bits of byte "c" to its hex character.
596 * Lower case letters are used to avoid the confusion of <F1> being 0xf1 or
597 * function key 1.
598 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000599 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +0100600nr2hex(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 if ((c & 0xf) <= 9)
603 return (c & 0xf) + '0';
604 return (c & 0xf) - 10 + 'a';
605}
606
607/*
608 * Return number of display cells occupied by byte "b".
609 * Caller must make sure 0 <= b <= 255.
610 * For multi-byte mode "b" must be the first byte of a character.
611 * A TAB is counted as two cells: "^I".
612 * For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of
613 * cells depends on further bytes.
614 */
615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100616byte2cells(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 if (enc_utf8 && b >= 0x80)
619 return 0;
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100620 return (g_chartab[b] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621}
622
623/*
624 * Return number of display cells occupied by character "c".
625 * "c" can be a special key (negative number) in which case 3 or 4 is returned.
626 * A TAB is counted as two cells: "^I" or four: "<09>".
627 */
628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100629char2cells(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 if (IS_SPECIAL(c))
632 return char2cells(K_SECOND(c)) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (c >= 0x80)
634 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100635 // UTF-8: above 0x80 need to check the value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (enc_utf8)
637 return utf_char2cells(c);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100638 // DBCS: double-byte means double-width, except for euc-jp with first
639 // byte 0x8e
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (enc_dbcs != 0 && c >= 0x100)
641 {
642 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
643 return 1;
644 return 2;
645 }
646 }
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100647 return (g_chartab[c & 0xff] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648}
649
650/*
651 * Return number of display cells occupied by character at "*p".
652 * A TAB is counted as two cells: "^I" or four: "<09>".
653 */
654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100655ptr2cells(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100657 // For UTF-8 we need to look at more bytes if the first byte is >= 0x80.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (enc_utf8 && *p >= 0x80)
659 return utf_ptr2cells(p);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100660 // For DBCS we can tell the cell count from the first byte.
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100661 return (g_chartab[*p] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
664/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100665 * Return the number of character cells string "s" will take on the screen,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 * counting TABs as two characters: "^I".
667 */
668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669vim_strsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 return vim_strnsize(s, (int)MAXCOL);
672}
673
674/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100675 * Return the number of character cells string "s[len]" will take on the
676 * screen, counting TABs as two characters: "^I".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 */
678 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100679vim_strnsize(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{
681 int size = 0;
682
683 while (*s != NUL && --len >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (has_mbyte)
685 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000686 int l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
688 size += ptr2cells(s);
689 s += l;
690 len -= l - 1;
691 }
692 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 size += byte2cells(*s++);
Bram Moolenaar13505972019-01-24 15:04:48 +0100694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 return size;
696}
697
698/*
699 * Return the number of characters 'c' will take on the screen, taking
700 * into account the size of a tab.
701 * Use a define to make it fast, this is used very often!!!
702 * Also see getvcol() below.
703 */
704
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200705#ifdef FEAT_VARTABS
706# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
kylo252ae6f1d82022-02-16 19:24:07 +0000707 if (*(p) == TAB && (!(wp)->w_p_list || (wp)->w_lcs_chars.tab1)) \
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200708 { \
709 return tabstop_padding(col, (buf)->b_p_ts, (buf)->b_p_vts_array); \
710 } \
711 else \
712 return ptr2cells(p);
713#else
714# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
Bram Moolenaareed9d462021-02-15 20:38:25 +0100715 if (*(p) == TAB && (!(wp)->w_p_list || wp->w_lcs_chars.tab1)) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 { \
717 int ts; \
718 ts = (buf)->b_p_ts; \
719 return (int)(ts - (col % ts)); \
720 } \
721 else \
722 return ptr2cells(p);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100726chartabsize(char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col)
729}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
731#ifdef FEAT_LINEBREAK
732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100733win_chartabsize(win_T *wp, char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col)
736}
737#endif
738
739/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100740 * Return the number of characters the string "s" will take on the screen,
Bram Moolenaardc536092010-07-18 15:45:49 +0200741 * taking into account the size of a tab.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100742 * Does not handle text properties, since "s" is not a buffer line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 */
744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745linetabsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
Bram Moolenaardc536092010-07-18 15:45:49 +0200747 return linetabsize_col(0, s);
748}
749
750/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100751 * Like linetabsize(), but "s" starts at column "startcol".
Bram Moolenaardc536092010-07-18 15:45:49 +0200752 */
753 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100754linetabsize_col(int startcol, char_u *s)
Bram Moolenaardc536092010-07-18 15:45:49 +0200755{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100756 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100758 init_chartabsize_arg(&cts, curwin, 0, startcol, s, s);
759 while (*cts.cts_ptr != NUL)
760 cts.cts_vcol += lbr_chartabsize_adv(&cts);
761 clear_chartabsize_arg(&cts);
762 return (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763}
764
765/*
766 * Like linetabsize(), but for a given window instead of the current one.
767 */
768 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100769win_linetabsize(win_T *wp, linenr_T lnum, char_u *line, colnr_T len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100771 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100773 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
774 for ( ; *cts.cts_ptr != NUL && (len == MAXCOL || cts.cts_ptr < line + len);
775 MB_PTR_ADV(cts.cts_ptr))
776 cts.cts_vcol += win_lbr_chartabsize(&cts, NULL);
777 clear_chartabsize_arg(&cts);
778 return (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779}
780
781/*
Bram Moolenaar81695252004-12-29 20:58:21 +0000782 * Return TRUE if 'c' is a normal identifier character:
783 * Letters and characters from the 'isident' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
785 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100786vim_isIDc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100788 return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789}
790
791/*
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +0200792 * Like vim_isIDc() but not using the 'isident' option: letters, numbers and
793 * underscore.
794 */
795 int
796vim_isNormalIDc(int c)
797{
798 return ASCII_ISALNUM(c) || c == '_';
799}
800
801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * return TRUE if 'c' is a keyword character: Letters and characters from
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100803 * 'iskeyword' option for the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 * For multi-byte characters mb_get_class() is used (builtin rules).
805 */
806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100807vim_iswordc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100809 return vim_iswordc_buf(c, curbuf);
810}
811
812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813vim_iswordc_buf(int c, buf_T *buf)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100814{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (c >= 0x100)
816 {
817 if (enc_dbcs != 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000818 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (enc_utf8)
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100820 return utf_class_buf(c, buf) >= 2;
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100821 return FALSE;
822 }
823 return (c > 0 && GET_CHARTAB(buf, c) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825
826/*
827 * Just like vim_iswordc() but uses a pointer to the (multi-byte) character.
828 */
829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100830vim_iswordp(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100832 return vim_iswordp_buf(p, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836vim_iswordp_buf(char_u *p, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100838 int c = *p;
839
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100840 if (has_mbyte && MB_BYTE2LEN(c) > 1)
841 c = (*mb_ptr2char)(p);
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100842 return vim_iswordc_buf(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845/*
846 * return TRUE if 'c' is a valid file-name character
847 * Assume characters above 0x100 are valid (multi-byte).
848 */
849 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100850vim_isfilec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100852 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854
855/*
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000856 * return TRUE if 'c' is a valid file-name character or a wildcard character
857 * Assume characters above 0x100 are valid (multi-byte).
858 * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]")
859 * returns false.
860 */
861 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100862vim_isfilec_or_wc(int c)
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000863{
864 char_u buf[2];
865
866 buf[0] = (char_u)c;
867 buf[1] = NUL;
868 return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf);
869}
870
871/*
Bram Moolenaar3317d5e2017-04-08 19:12:06 +0200872 * Return TRUE if 'c' is a printable character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * Assume characters above 0x100 are printable (multi-byte), except for
874 * Unicode.
875 */
876 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100877vim_isprintc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (enc_utf8 && c >= 0x100)
880 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100881 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882}
883
884/*
885 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head
886 * byte of a double-byte character.
887 */
888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100889vim_isprintc_strict(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1)
892 return FALSE;
893 if (enc_utf8 && c >= 0x100)
894 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100895 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896}
897
898/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100899 * Prepare the structure passed to chartabsize functions.
900 * "line" is the start of the line, "ptr" is the first relevant character.
901 * When "lnum" is zero do not use text properties that insert text.
902 */
903 void
904init_chartabsize_arg(
905 chartabsize_T *cts,
906 win_T *wp,
907 linenr_T lnum,
908 colnr_T col,
909 char_u *line,
910 char_u *ptr)
911{
912 cts->cts_win = wp;
913 cts->cts_lnum = lnum;
914 cts->cts_vcol = col;
915 cts->cts_line = line;
916 cts->cts_ptr = ptr;
917#ifdef FEAT_PROP_POPUP
918 cts->cts_text_prop_count = 0;
919 cts->cts_has_prop_with_text = FALSE;
920 cts->cts_cur_text_width = 0;
921 if (lnum > 0)
922 {
923 char_u *prop_start;
924
925 cts->cts_text_prop_count = get_text_props(wp->w_buffer, lnum,
926 &prop_start, FALSE);
927 if (cts->cts_text_prop_count > 0)
928 {
929 // Make a copy of the properties, so that they are properly
930 // aligned.
931 cts->cts_text_props = ALLOC_MULT(textprop_T,
932 cts->cts_text_prop_count);
933 if (cts->cts_text_props == NULL)
934 cts->cts_text_prop_count = 0;
935 else
936 {
937 int i;
938
939 mch_memmove(cts->cts_text_props, prop_start,
940 cts->cts_text_prop_count * sizeof(textprop_T));
941 for (i = 0; i < cts->cts_text_prop_count; ++i)
942 if (cts->cts_text_props[i].tp_id < 0)
943 {
944 cts->cts_has_prop_with_text = TRUE;
945 break;
946 }
947 if (!cts->cts_has_prop_with_text)
948 {
949 // won't use the text properties, free them
950 vim_free(cts->cts_text_props);
951 cts->cts_text_prop_count = 0;
952 }
953 }
954 }
955 }
956#endif
957}
958
959/*
960 * Free any allocated item in "cts".
961 */
962 void
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +0100963clear_chartabsize_arg(chartabsize_T *cts UNUSED)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100964{
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +0100965#ifdef FEAT_PROP_POPUP
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100966 if (cts->cts_text_prop_count > 0)
Bram Moolenaar34a1f772022-07-26 11:20:48 +0100967 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100968 vim_free(cts->cts_text_props);
Bram Moolenaar34a1f772022-07-26 11:20:48 +0100969 cts->cts_text_prop_count = 0; // avoid double free
970 }
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +0100971#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100972}
973
974/*
975 * Like chartabsize(), but also check for line breaks on the screen and text
976 * properties that insert text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 */
978 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100979lbr_chartabsize(chartabsize_T *cts)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100981#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
982 if (1
983# ifdef FEAT_LINEBREAK
984 && !curwin->w_p_lbr && *get_showbreak_value(curwin) == NUL
985 && !curwin->w_p_bri
986# endif
987# ifdef FEAT_PROP_POPUP
988 && !cts->cts_has_prop_with_text
989#endif
990 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (curwin->w_p_wrap)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100994 return win_nolbr_chartabsize(cts, NULL);
995 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, cts->cts_ptr, cts->cts_vcol)
996#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100998 return win_lbr_chartabsize(cts, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
1000}
1001
1002/*
1003 * Call lbr_chartabsize() and advance the pointer.
1004 */
1005 int
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001006lbr_chartabsize_adv(chartabsize_T *cts)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
1008 int retval;
1009
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001010 retval = lbr_chartabsize(cts);
1011 MB_PTR_ADV(cts->cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 return retval;
1013}
1014
1015/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001016 * Return the screen size of the character indicated by "cts".
1017 * "cts->cts_cur_text_width" is set to the extra size for a text property that
1018 * inserts text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 * This function is used very often, keep it fast!!!!
1020 *
1021 * If "headp" not NULL, set *headp to the size of what we for 'showbreak'
1022 * string at start of line. Warning: *headp is only set if it's a non-zero
1023 * value, init to 0 before calling.
1024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026win_lbr_chartabsize(
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001027 chartabsize_T *cts,
1028 int *headp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001030 win_T *wp = cts->cts_win;
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001031#ifdef FEAT_PROP_POPUP
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001032 char_u *line = cts->cts_line; // start of the line
Bram Moolenaarfe3fb6e2022-07-25 18:35:15 +01001033#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001034 char_u *s = cts->cts_ptr;
1035 colnr_T vcol = cts->cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#ifdef FEAT_LINEBREAK
1037 int c;
1038 int size;
1039 colnr_T col2;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001040 colnr_T col_adj = 0; // vcol + screen size of tab
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 colnr_T colmax;
1042 int added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 int mb_added = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 int numberextra;
1045 char_u *ps;
1046 int tab_corr = (*s == TAB);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047 int n;
Bram Moolenaaree857022019-11-09 23:26:40 +01001048 char_u *sbr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001051#if defined(FEAT_PROP_POPUP)
1052 cts->cts_cur_text_width = 0;
1053#endif
1054
1055#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001057 * No 'linebreak', 'showbreak', 'breakindent' and text properties that
1058 * insert text: return quickly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001060 if (1
1061# ifdef FEAT_LINEBREAK
1062 && !wp->w_p_lbr && !wp->w_p_bri && *get_showbreak_value(wp) == NUL
1063# endif
1064# ifdef FEAT_PROP_POPUP
1065 && !cts->cts_has_prop_with_text
1066# endif
1067 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#endif
1069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (wp->w_p_wrap)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001071 return win_nolbr_chartabsize(cts, headp);
1072 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
1074
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001075#if defined(FEAT_LINEBREAK) || defined(FEAT_PROP_POPUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001077 * First get the normal size, without 'linebreak' or text properties
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001079 size = win_chartabsize(wp, s, vcol);
1080
1081# ifdef FEAT_PROP_POPUP
1082 if (cts->cts_has_prop_with_text)
1083 {
1084 int i;
1085 int col = (int)(s - line);
1086
1087 for (i = 0; i < cts->cts_text_prop_count; ++i)
1088 {
1089 textprop_T *tp = cts->cts_text_props + i;
1090
1091 if (tp->tp_id < 0
1092 && tp->tp_col - 1 >= col && tp->tp_col - 1 < col + size
1093 && -tp->tp_id <= wp->w_buffer->b_textprop_text.ga_len)
1094 {
1095 char_u *p = ((char_u **)wp->w_buffer->b_textprop_text.ga_data)[
1096 -tp->tp_id - 1];
1097 // TODO: count screen cells
Mike Williams04947892022-07-26 16:03:42 +01001098 cts->cts_cur_text_width = (int)STRLEN(p);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001099 size += cts->cts_cur_text_width;
1100 break;
1101 }
1102 if (tp->tp_col - 1 > col)
1103 break;
1104 }
1105 }
1106# endif
1107
1108# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 c = *s;
Bram Moolenaaree739b42014-07-02 19:37:42 +02001110 if (tab_corr)
1111 col_adj = size - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
1113 /*
1114 * If 'linebreak' set check at a blank before a non-blank if the line
1115 * needs a break here
1116 */
1117 if (wp->w_p_lbr
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001118 && VIM_ISBREAK(c)
Bram Moolenaar977d0372017-03-12 21:31:58 +01001119 && !VIM_ISBREAK((int)s[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 && wp->w_p_wrap
Bram Moolenaar4033c552017-09-16 20:54:51 +02001121 && wp->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 /*
1124 * Count all characters from first non-blank after a blank up to next
1125 * non-blank after a blank.
1126 */
1127 numberextra = win_col_off(wp);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001128 col2 = vcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02001129 colmax = (colnr_T)(wp->w_width - numberextra - col_adj);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001130 if (vcol >= colmax)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001131 {
Bram Moolenaaree739b42014-07-02 19:37:42 +02001132 colmax += col_adj;
1133 n = colmax + win_col_off2(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001134 if (n > 0)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001135 colmax += (((vcol - colmax) / n) + 1) * n - col_adj;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001136 }
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 for (;;)
1139 {
1140 ps = s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001141 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 c = *s;
1143 if (!(c != NUL
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001144 && (VIM_ISBREAK(c)
1145 || (!VIM_ISBREAK(c)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001146 && (col2 == vcol || !VIM_ISBREAK((int)*ps))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 break;
1148
1149 col2 += win_chartabsize(wp, s, col2);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001150 if (col2 >= colmax) // doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001152 size = colmax - vcol + col_adj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 break;
1154 }
1155 }
1156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001158 && wp->w_p_wrap && in_win_border(wp, vcol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001160 ++size; // Count the ">" in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 mb_added = 1;
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +02001165 * May have to add something for 'breakindent' and/or 'showbreak'
1166 * string at start of line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 * Set *headp to the size of what we add.
Bram Moolenaar21efafe2022-03-03 20:04:03 +00001168 * Do not use 'showbreak' at the NUL after the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
1170 added = 0;
Bram Moolenaar21efafe2022-03-03 20:04:03 +00001171 sbr = c == NUL ? empty_option : get_showbreak_value(wp);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001172 if ((*sbr != NUL || wp->w_p_bri) && wp->w_p_wrap && vcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaard574ea22015-01-14 19:35:14 +01001174 colnr_T sbrlen = 0;
1175 int numberwidth = win_col_off(wp);
1176
1177 numberextra = numberwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001178 vcol += numberextra + mb_added;
1179 if (vcol >= (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001181 vcol -= wp->w_width;
Bram Moolenaar02631462017-09-22 15:20:32 +02001182 numberextra = wp->w_width - (numberextra - win_col_off2(wp));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001183 if (vcol >= numberextra && numberextra > 0)
1184 vcol %= numberextra;
Bram Moolenaaree857022019-11-09 23:26:40 +01001185 if (*sbr != NUL)
Bram Moolenaar1c852102014-10-15 21:26:40 +02001186 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001187 sbrlen = (colnr_T)MB_CHARLEN(sbr);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001188 if (vcol >= sbrlen)
1189 vcol -= sbrlen;
Bram Moolenaar1c852102014-10-15 21:26:40 +02001190 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001191 if (vcol >= numberextra && numberextra > 0)
1192 vcol = vcol % numberextra;
1193 else if (vcol > 0 && numberextra > 0)
1194 vcol += numberwidth - win_col_off2(wp);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001195
1196 numberwidth -= win_col_off2(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001198 if (vcol == 0 || vcol + size + sbrlen > (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02001200 added = 0;
Bram Moolenaaree857022019-11-09 23:26:40 +01001201 if (*sbr != NUL)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001202 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001203 if (size + sbrlen + numberwidth > (colnr_T)wp->w_width)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001204 {
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001205 // calculate effective window width
Bram Moolenaar02631462017-09-22 15:20:32 +02001206 int width = (colnr_T)wp->w_width - sbrlen - numberwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001207 int prev_width = vcol
1208 ? ((colnr_T)wp->w_width - (sbrlen + vcol)) : 0;
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001209
1210 if (width <= 0)
1211 width = (colnr_T)1;
Bram Moolenaaree857022019-11-09 23:26:40 +01001212 added += ((size - prev_width) / width) * vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001213 if ((size - prev_width) % width)
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001214 // wrapped, add another length of 'sbr'
Bram Moolenaaree857022019-11-09 23:26:40 +01001215 added += vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001216 }
1217 else
Bram Moolenaaree857022019-11-09 23:26:40 +01001218 added += vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001219 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02001220 if (wp->w_p_bri)
1221 added += get_breakindent_win(wp, line);
1222
Bram Moolenaar95765082014-08-24 21:19:25 +02001223 size += added;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001224 if (vcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 added = 0;
1226 }
1227 }
1228 if (headp != NULL)
1229 *headp = added + mb_added;
1230 return size;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233}
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001236 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off, 'wrap'
1237 * is on and there are no properties that insert text. This means we need to
1238 * check for a double-byte character that doesn't fit at the end of the screen
1239 * line.
1240 * Only uses "cts_win", "cts_ptr" and "cts_vcol" from "cts".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 */
1242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001243win_nolbr_chartabsize(
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001244 chartabsize_T *cts,
1245 int *headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001247 win_T *wp = cts->cts_win;
1248 char_u *s = cts->cts_ptr;
1249 colnr_T col = cts->cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 int n;
1251
Bram Moolenaareed9d462021-02-15 20:38:25 +01001252 if (*s == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001254# ifdef FEAT_VARTABS
1255 return tabstop_padding(col, wp->w_buffer->b_p_ts,
1256 wp->w_buffer->b_p_vts_array);
1257# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 n = wp->w_buffer->b_p_ts;
1259 return (int)(n - (col % n));
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 n = ptr2cells(s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001263 // Add one cell for a double-width character in the last column of the
1264 // window, displayed with a ">".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col))
1266 {
1267 if (headp != NULL)
1268 *headp = 1;
1269 return 3;
1270 }
1271 return n;
1272}
1273
1274/*
1275 * Return TRUE if virtual column "vcol" is in the rightmost column of window
1276 * "wp".
1277 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279in_win_border(win_T *wp, colnr_T vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001281 int width1; // width of first line (after line number)
1282 int width2; // width of further lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
Bram Moolenaarc667da52019-11-30 20:52:27 +01001284 if (wp->w_width == 0) // there is no border
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 return FALSE;
Bram Moolenaar02631462017-09-22 15:20:32 +02001286 width1 = wp->w_width - win_col_off(wp);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001287 if ((int)vcol < width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001289 if ((int)vcol == width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 return TRUE;
1291 width2 = width1 + win_col_off2(wp);
Bram Moolenaar8701cd62009-10-07 14:20:30 +00001292 if (width2 <= 0)
1293 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 return ((vcol - width1) % width2 == width2 - 1);
1295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297/*
1298 * Get virtual column number of pos.
1299 * start: on the first position of this character (TAB, ctrl)
1300 * cursor: where the cursor is on this character (first char, except for TAB)
1301 * end: on the last position of this character (TAB, ctrl)
1302 *
1303 * This is used very often, keep it fast!
1304 */
1305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001306getvcol(
1307 win_T *wp,
1308 pos_T *pos,
1309 colnr_T *start,
1310 colnr_T *cursor,
1311 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 colnr_T vcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001314 char_u *ptr; // points to current char
1315 char_u *posptr; // points to char at pos->col
1316 char_u *line; // start of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 int incr;
1318 int head;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001319#ifdef FEAT_VARTABS
1320 int *vts = wp->w_buffer->b_p_vts_array;
1321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 int ts = wp->w_buffer->b_p_ts;
1323 int c;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001324 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326 vcol = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001327 line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001328 if (pos->col == MAXCOL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001329 posptr = NULL; // continue until the NUL
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001330 else
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001331 {
Bram Moolenaar94f31922021-12-30 15:29:18 +00001332 colnr_T i;
1333
1334 // In a few cases the position can be beyond the end of the line.
1335 for (i = 0; i < pos->col; ++i)
1336 if (ptr[i] == NUL)
1337 {
1338 pos->col = i;
1339 break;
1340 }
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001341 posptr = ptr + pos->col;
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001342 if (has_mbyte)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001343 // always start on the first byte
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001344 posptr -= (*mb_head_off)(line, posptr);
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001347 init_chartabsize_arg(&cts, wp, pos->lnum, 0, line, line);
1348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 /*
1350 * This function is used very often, do some speed optimizations.
Bram Moolenaar597a4222014-06-25 14:39:50 +02001351 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001352 * and there are no text properties with "text" use a simple loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Also use this when 'list' is set but tabs take their normal size.
1354 */
Bram Moolenaareed9d462021-02-15 20:38:25 +01001355 if ((!wp->w_p_list || wp->w_lcs_chars.tab1 != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001357 && !wp->w_p_lbr && *get_showbreak_value(wp) == NUL && !wp->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001359#ifdef FEAT_PROP_POPUP
1360 && !cts.cts_has_prop_with_text
1361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 )
1363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 for (;;)
1365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 head = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 c = *ptr;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001368 // make sure we don't go past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (c == NUL)
1370 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001371 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 break;
1373 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001374 // A tab gets expanded, depending on the current column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (c == TAB)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001376#ifdef FEAT_VARTABS
1377 incr = tabstop_padding(vcol, ts, vts);
1378#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 incr = ts - (vcol % ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 else
1382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (has_mbyte)
1384 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001385 // For utf-8, if the byte is >= 0x80, need to look at
1386 // further bytes to find the cell width.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (enc_utf8 && c >= 0x80)
1388 incr = utf_ptr2cells(ptr);
1389 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001390 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
Bram Moolenaarc667da52019-11-30 20:52:27 +01001392 // If a double-cell char doesn't fit at the end of a line
1393 // it wraps to the next line, it's like this char is three
1394 // cells wide.
Bram Moolenaar9c33a7c2008-02-20 13:59:32 +00001395 if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1
1396 && in_win_border(wp, vcol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
1398 ++incr;
1399 head = 1;
1400 }
1401 }
1402 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001403 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405
Bram Moolenaarc667da52019-11-30 20:52:27 +01001406 if (posptr != NULL && ptr >= posptr) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 break;
1408
1409 vcol += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001410 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 }
1413 else
1414 {
1415 for (;;)
1416 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001417 // A tab gets expanded, depending on the current column.
1418 // Other things also take up space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 head = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001420 incr = win_lbr_chartabsize(&cts, &head);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001421 // make sure we don't go past the end of the line
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001422 if (*cts.cts_ptr == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001424 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 break;
1426 }
1427
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001428 if (posptr != NULL && cts.cts_ptr >= posptr)
1429 // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 break;
1431
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001432 cts.cts_vcol += incr;
1433 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001435 vcol = cts.cts_vcol;
1436 ptr = cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001438 clear_chartabsize_arg(&cts);
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (start != NULL)
1441 *start = vcol + head;
1442 if (end != NULL)
1443 *end = vcol + incr - 1;
1444 if (cursor != NULL)
1445 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001446#ifdef FEAT_PROP_POPUP
1447 // cursor is after inserted text
1448 vcol += cts.cts_cur_text_width;
1449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 if (*ptr == TAB
Bram Moolenaar24959102022-05-07 20:01:16 +01001451 && (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 && !wp->w_p_list
1453 && !virtual_active()
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001454 && !(VIsual_active
1455 && (*p_sel == 'e' || LTOREQ_POS(*pos, VIsual)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 )
Bram Moolenaarc667da52019-11-30 20:52:27 +01001457 *cursor = vcol + incr - 1; // cursor at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01001459 *cursor = vcol + head; // cursor at start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461}
1462
1463/*
1464 * Get virtual cursor column in the current window, pretending 'list' is off.
1465 */
1466 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001467getvcol_nolist(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 int list_save = curwin->w_p_list;
1470 colnr_T vcol;
1471
1472 curwin->w_p_list = FALSE;
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001473 if (posp->coladd)
1474 getvvcol(curwin, posp, NULL, &vcol, NULL);
1475 else
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001476 getvcol(curwin, posp, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 curwin->w_p_list = list_save;
1478 return vcol;
1479}
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * Get virtual column in virtual mode.
1483 */
1484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485getvvcol(
1486 win_T *wp,
1487 pos_T *pos,
1488 colnr_T *start,
1489 colnr_T *cursor,
1490 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
1492 colnr_T col;
1493 colnr_T coladd;
1494 colnr_T endadd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 if (virtual_active())
1498 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001499 // For virtual mode, only want one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 getvcol(wp, pos, &col, NULL, NULL);
1501
1502 coladd = pos->coladd;
1503 endadd = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001504 // Cannot put the cursor on part of a wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001506 if (pos->col < (colnr_T)STRLEN(ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 int c = (*mb_ptr2char)(ptr + pos->col);
1509
1510 if (c != TAB && vim_isprintc(c))
1511 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001512 endadd = (colnr_T)(char2cells(c) - 1);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001513 if (coladd > endadd) // past end of line
Bram Moolenaara5792f52005-11-23 21:25:05 +00001514 endadd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 else
1516 coladd = 0;
1517 }
1518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 col += coladd;
1520 if (start != NULL)
1521 *start = col;
1522 if (cursor != NULL)
1523 *cursor = col;
1524 if (end != NULL)
1525 *end = col + endadd;
1526 }
1527 else
1528 getvcol(wp, pos, start, cursor, end);
1529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531/*
1532 * Get the leftmost and rightmost virtual column of pos1 and pos2.
1533 * Used for Visual block mode.
1534 */
1535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536getvcols(
1537 win_T *wp,
1538 pos_T *pos1,
1539 pos_T *pos2,
1540 colnr_T *left,
1541 colnr_T *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 colnr_T from1, from2, to1, to2;
1544
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001545 if (LT_POSP(pos1, pos2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 {
1547 getvvcol(wp, pos1, &from1, NULL, &to1);
1548 getvvcol(wp, pos2, &from2, NULL, &to2);
1549 }
1550 else
1551 {
1552 getvvcol(wp, pos2, &from1, NULL, &to1);
1553 getvvcol(wp, pos1, &from2, NULL, &to2);
1554 }
1555 if (from2 < from1)
1556 *left = from2;
1557 else
1558 *left = from1;
1559 if (to2 > to1)
1560 {
1561 if (*p_sel == 'e' && from2 - 1 >= to1)
1562 *right = from2 - 1;
1563 else
1564 *right = to2;
1565 }
1566 else
1567 *right = to1;
1568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
1570/*
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001571 * Skip over ' ' and '\t'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
1573 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574skipwhite(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001576 char_u *p = q;
1577
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001578 while (VIM_ISWHITE(*p))
1579 ++p;
1580 return p;
1581}
1582
Dominique Pelle748b3082022-01-08 12:41:16 +00001583#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001584/*
1585 * skip over ' ', '\t' and '\n'.
1586 */
1587 char_u *
1588skipwhite_and_nl(char_u *q)
1589{
1590 char_u *p = q;
1591
1592 while (VIM_ISWHITE(*p) || *p == NL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 ++p;
1594 return p;
1595}
Dominique Pelle748b3082022-01-08 12:41:16 +00001596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598/*
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001599 * getwhitecols: return the number of whitespace
1600 * columns (bytes) at the start of a given line
1601 */
1602 int
1603getwhitecols_curline()
1604{
1605 return getwhitecols(ml_get_curline());
1606}
1607
1608 int
1609getwhitecols(char_u *p)
1610{
1611 return skipwhite(p) - p;
1612}
1613
1614/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001615 * skip over digits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
1617 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001618skipdigits(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001620 char_u *p = q;
1621
Bram Moolenaarc667da52019-11-30 20:52:27 +01001622 while (VIM_ISDIGIT(*p)) // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 ++p;
1624 return p;
1625}
1626
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001627#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001628/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001629 * skip over binary digits
1630 */
1631 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001632skipbin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001633{
1634 char_u *p = q;
1635
Bram Moolenaarc667da52019-11-30 20:52:27 +01001636 while (vim_isbdigit(*p)) // skip to next non-digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001637 ++p;
1638 return p;
1639}
1640
1641/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001642 * skip over digits and hex characters
1643 */
1644 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001645skiphex(char_u *q)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001646{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001647 char_u *p = q;
1648
Bram Moolenaarc667da52019-11-30 20:52:27 +01001649 while (vim_isxdigit(*p)) // skip to next non-digit
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001650 ++p;
1651 return p;
1652}
1653#endif
1654
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001655/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001656 * skip to bin digit (or NUL after the string)
1657 */
1658 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659skiptobin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001660{
1661 char_u *p = q;
1662
Bram Moolenaarc667da52019-11-30 20:52:27 +01001663 while (*p != NUL && !vim_isbdigit(*p)) // skip to next digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001664 ++p;
1665 return p;
1666}
1667
1668/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001669 * skip to digit (or NUL after the string)
1670 */
1671 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672skiptodigit(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001673{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001674 char_u *p = q;
1675
Bram Moolenaarc667da52019-11-30 20:52:27 +01001676 while (*p != NUL && !VIM_ISDIGIT(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001677 ++p;
1678 return p;
1679}
1680
1681/*
1682 * skip to hex character (or NUL after the string)
1683 */
1684 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001685skiptohex(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001686{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001687 char_u *p = q;
1688
Bram Moolenaarc667da52019-11-30 20:52:27 +01001689 while (*p != NUL && !vim_isxdigit(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001690 ++p;
1691 return p;
1692}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694/*
1695 * Variant of isdigit() that can handle characters > 0x100.
1696 * We don't use isdigit() here, because on some systems it also considers
1697 * superscript 1 to be a digit.
1698 * Use the VIM_ISDIGIT() macro for simple arguments.
1699 */
1700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001701vim_isdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 return (c >= '0' && c <= '9');
1704}
1705
1706/*
1707 * Variant of isxdigit() that can handle characters > 0x100.
1708 * We don't use isxdigit() here, because on some systems it also considers
1709 * superscript 1 to be a digit.
1710 */
1711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712vim_isxdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 return (c >= '0' && c <= '9')
1715 || (c >= 'a' && c <= 'f')
1716 || (c >= 'A' && c <= 'F');
1717}
1718
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001719/*
1720 * Corollary of vim_isdigit and vim_isxdigit() that can handle
1721 * characters > 0x100.
1722 */
1723 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724vim_isbdigit(int c)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001725{
1726 return (c == '0' || c == '1');
1727}
1728
Bram Moolenaarc37b6552021-01-07 19:36:30 +01001729 static int
1730vim_isodigit(int c)
1731{
1732 return (c >= '0' && c <= '7');
1733}
1734
Bram Moolenaar78622822005-08-23 21:00:13 +00001735/*
1736 * Vim's own character class functions. These exist because many library
1737 * islower()/toupper() etc. do not work properly: they crash when used with
1738 * invalid values or can't handle latin1 when the locale is C.
1739 * Speed is most important here.
1740 */
1741#define LATIN1LOWER 'l'
1742#define LATIN1UPPER 'U'
1743
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00001744static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll";
Bram Moolenaar936347b2012-05-25 11:56:22 +02001745static 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";
1746static 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 +00001747
1748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001749vim_islower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001750{
1751 if (c <= '@')
1752 return FALSE;
1753 if (c >= 0x80)
1754 {
1755 if (enc_utf8)
1756 return utf_islower(c);
1757 if (c >= 0x100)
1758 {
1759#ifdef HAVE_ISWLOWER
1760 if (has_mbyte)
1761 return iswlower(c);
1762#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001763 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001764 return FALSE;
1765 }
1766 if (enc_latin1like)
1767 return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER;
1768 }
1769 return islower(c);
1770}
1771
1772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773vim_isupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001774{
1775 if (c <= '@')
1776 return FALSE;
1777 if (c >= 0x80)
1778 {
1779 if (enc_utf8)
1780 return utf_isupper(c);
1781 if (c >= 0x100)
1782 {
1783#ifdef HAVE_ISWUPPER
1784 if (has_mbyte)
1785 return iswupper(c);
1786#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001787 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001788 return FALSE;
1789 }
1790 if (enc_latin1like)
1791 return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER;
1792 }
1793 return isupper(c);
1794}
1795
1796 int
Bram Moolenaar5921aeb2022-02-19 11:20:12 +00001797vim_isalpha(int c)
1798{
1799 return vim_islower(c) || vim_isupper(c);
1800}
1801
1802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803vim_toupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001804{
1805 if (c <= '@')
1806 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001807 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00001808 {
1809 if (enc_utf8)
1810 return utf_toupper(c);
1811 if (c >= 0x100)
1812 {
1813#ifdef HAVE_TOWUPPER
1814 if (has_mbyte)
1815 return towupper(c);
1816#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001817 // toupper() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001818 return c;
1819 }
1820 if (enc_latin1like)
1821 return latin1upper[c];
1822 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02001823 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
1824 return TOUPPER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00001825 return TOUPPER_LOC(c);
1826}
1827
1828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001829vim_tolower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001830{
1831 if (c <= '@')
1832 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001833 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00001834 {
1835 if (enc_utf8)
1836 return utf_tolower(c);
1837 if (c >= 0x100)
1838 {
1839#ifdef HAVE_TOWLOWER
1840 if (has_mbyte)
1841 return towlower(c);
1842#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001843 // tolower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001844 return c;
1845 }
1846 if (enc_latin1like)
1847 return latin1lower[c];
1848 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02001849 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
1850 return TOLOWER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00001851 return TOLOWER_LOC(c);
1852}
Bram Moolenaar78622822005-08-23 21:00:13 +00001853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854/*
1855 * skiptowhite: skip over text until ' ' or '\t' or NUL.
1856 */
1857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001858skiptowhite(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859{
1860 while (*p != ' ' && *p != '\t' && *p != NUL)
1861 ++p;
1862 return p;
1863}
1864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
1866 * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars
1867 */
1868 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869skiptowhite_esc(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
1871 while (*p != ' ' && *p != '\t' && *p != NUL)
1872 {
1873 if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL)
1874 ++p;
1875 ++p;
1876 }
1877 return p;
1878}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00001881 * Get a number from a string and skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 * Note: the argument is a pointer to a char_u pointer!
1883 */
1884 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001885getdigits(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887 char_u *p;
1888 long retval;
1889
1890 p = *pp;
1891 retval = atol((char *)p);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001892 if (*p == '-') // skip negative sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 ++p;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001894 p = skipdigits(p); // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 *pp = p;
1896 return retval;
1897}
1898
1899/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00001900 * Like getdigits() but allow for embedded single quotes.
1901 */
1902 long
1903getdigits_quoted(char_u **pp)
1904{
1905 char_u *p = *pp;
1906 long retval = 0;
1907
1908 if (*p == '-')
1909 ++p;
1910 while (VIM_ISDIGIT(*p))
1911 {
1912 if (retval >= LONG_MAX / 10 - 10)
1913 retval = LONG_MAX;
1914 else
1915 retval = retval * 10 - '0' + *p;
1916 ++p;
1917 if (in_vim9script() && *p == '\'' && VIM_ISDIGIT(p[1]))
1918 ++p;
1919 }
1920 if (**pp == '-')
1921 {
1922 if (retval == LONG_MAX)
1923 retval = LONG_MIN;
1924 else
1925 retval = -retval;
1926 }
1927 *pp = p;
1928 return retval;
1929}
1930
1931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 * Return TRUE if "lbuf" is empty or only contains blanks.
1933 */
1934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001935vim_isblankline(char_u *lbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936{
1937 char_u *p;
1938
1939 p = skipwhite(lbuf);
1940 return (*p == NUL || *p == '\r' || *p == '\n');
1941}
1942
1943/*
1944 * Convert a string into a long and/or unsigned long, taking care of
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001945 * hexadecimal, octal, and binary numbers. Accepts a '-' sign.
1946 * If "prep" is not NULL, returns a flag to indicate the type of the number:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 * 0 decimal
1948 * '0' octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001949 * 'O' octal
1950 * 'o' octal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001951 * 'B' bin
1952 * 'b' bin
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 * 'X' hex
1954 * 'x' hex
1955 * If "len" is not NULL, the length of the number in characters is returned.
1956 * If "nptr" is not NULL, the signed result is returned in it.
1957 * If "unptr" is not NULL, the unsigned result is returned in it.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001958 * If "what" contains STR2NR_BIN recognize binary numbers
1959 * If "what" contains STR2NR_OCT recognize octal numbers
1960 * If "what" contains STR2NR_HEX recognize hex numbers
1961 * If "what" contains STR2NR_FORCE always assume bin/oct/hex.
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001962 * If "what" contains STR2NR_QUOTE ignore embedded single quotes
Bram Moolenaarce157752017-10-28 16:07:33 +02001963 * If maxlen > 0, check at a maximum maxlen chars.
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001964 * If strict is TRUE, check the number strictly. return *len = 0 if fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 */
1966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001967vim_str2nr(
1968 char_u *start,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001969 int *prep, // return: type of number 0 = decimal, 'x'
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001970 // or 'X' is hex, '0', 'o' or 'O' is octal,
1971 // 'b' or 'B' is bin
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001972 int *len, // return: detected length of number
1973 int what, // what numbers to recognize
1974 varnumber_T *nptr, // return: signed result
1975 uvarnumber_T *unptr, // return: unsigned result
1976 int maxlen, // max length of string to check
1977 int strict) // check strictly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
1979 char_u *ptr = start;
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001980 int pre = 0; // default is decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 int negative = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001982 uvarnumber_T un = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001983 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001985 if (len != NULL)
1986 *len = 0;
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (ptr[0] == '-')
1989 {
1990 negative = TRUE;
1991 ++ptr;
1992 }
1993
Bram Moolenaarc667da52019-11-30 20:52:27 +01001994 // Recognize hex, octal, and bin.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001995 if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9'
1996 && (maxlen == 0 || maxlen > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001998 pre = ptr[1];
1999 if ((what & STR2NR_HEX)
2000 && (pre == 'X' || pre == 'x') && vim_isxdigit(ptr[2])
2001 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002002 // hexadecimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002003 ptr += 2;
2004 else if ((what & STR2NR_BIN)
2005 && (pre == 'B' || pre == 'b') && vim_isbdigit(ptr[2])
2006 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002007 // binary
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002008 ptr += 2;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002009 else if ((what & STR2NR_OOCT)
Bram Moolenaarc37b6552021-01-07 19:36:30 +01002010 && (pre == 'O' || pre == 'o') && vim_isodigit(ptr[2])
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002011 && (maxlen == 0 || maxlen > 2))
2012 // octal with prefix "0o"
2013 ptr += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002016 // decimal or octal, default is decimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002017 pre = 0;
2018 if (what & STR2NR_OCT)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002019 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002020 // Don't interpret "0", "08" or "0129" as octal.
Bram Moolenaarce157752017-10-28 16:07:33 +02002021 for (n = 1; n != maxlen && VIM_ISDIGIT(ptr[n]); ++n)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002022 {
2023 if (ptr[n] > '7')
2024 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002025 pre = 0; // can't be octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002026 break;
2027 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002028 pre = '0'; // assume octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002029 }
2030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 }
2033
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002034 // Do the conversion manually to avoid sscanf() quirks.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002035 n = 1;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002036 if (pre == 'B' || pre == 'b'
2037 || ((what & STR2NR_BIN) && (what & STR2NR_FORCE)))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002038 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002039 // bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002040 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002041 n += 2; // skip over "0b"
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002042 while ('0' <= *ptr && *ptr <= '1')
2043 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002044 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002045 if (un <= UVARNUM_MAX / 2)
2046 un = 2 * un + (uvarnumber_T)(*ptr - '0');
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002047 else
2048 un = UVARNUM_MAX;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002049 ++ptr;
2050 if (n++ == maxlen)
2051 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002052 if ((what & STR2NR_QUOTE) && *ptr == '\''
2053 && '0' <= ptr[1] && ptr[1] <= '1')
2054 {
2055 ++ptr;
2056 if (n++ == maxlen)
2057 break;
2058 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002059 }
2060 }
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002061 else if (pre == 'O' || pre == 'o' ||
2062 pre == '0' || ((what & STR2NR_OCT) && (what & STR2NR_FORCE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002064 // octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02002065 if (pre != 0 && pre != '0')
2066 n += 2; // skip over "0o"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002067 while ('0' <= *ptr && *ptr <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002069 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002070 if (un <= UVARNUM_MAX / 8)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002071 un = 8 * un + (uvarnumber_T)(*ptr - '0');
2072 else
2073 un = UVARNUM_MAX;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002074 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002075 if (n++ == maxlen)
2076 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002077 if ((what & STR2NR_QUOTE) && *ptr == '\''
2078 && '0' <= ptr[1] && ptr[1] <= '7')
2079 {
2080 ++ptr;
2081 if (n++ == maxlen)
2082 break;
2083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002085 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002086 else if (pre != 0 || ((what & STR2NR_HEX) && (what & STR2NR_FORCE)))
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002087 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002088 // hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002089 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002090 n += 2; // skip over "0x"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002091 while (vim_isxdigit(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002093 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002094 if (un <= UVARNUM_MAX / 16)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002095 un = 16 * un + (uvarnumber_T)hex2nr(*ptr);
2096 else
2097 un = UVARNUM_MAX;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002098 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002099 if (n++ == maxlen)
2100 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002101 if ((what & STR2NR_QUOTE) && *ptr == '\'' && vim_isxdigit(ptr[1]))
2102 {
2103 ++ptr;
2104 if (n++ == maxlen)
2105 break;
2106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 }
2109 else
2110 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002111 // decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 while (VIM_ISDIGIT(*ptr))
2113 {
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002114 uvarnumber_T digit = (uvarnumber_T)(*ptr - '0');
2115
Bram Moolenaarc667da52019-11-30 20:52:27 +01002116 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02002117 if (un < UVARNUM_MAX / 10
2118 || (un == UVARNUM_MAX / 10 && digit <= UVARNUM_MAX % 10))
2119 un = 10 * un + digit;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002120 else
2121 un = UVARNUM_MAX;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002123 if (n++ == maxlen)
2124 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002125 if ((what & STR2NR_QUOTE) && *ptr == '\'' && VIM_ISDIGIT(ptr[1]))
2126 {
2127 ++ptr;
2128 if (n++ == maxlen)
2129 break;
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02002133
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002134 // Check for an alphanumeric character immediately following, that is
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002135 // most likely a typo.
2136 if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr))
2137 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002139 if (prep != NULL)
2140 *prep = pre;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (len != NULL)
2142 *len = (int)(ptr - start);
2143 if (nptr != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002144 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002145 if (negative) // account for leading '-' for decimal numbers
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002146 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002147 // avoid ubsan error for overflow
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002148 if (un > VARNUM_MAX)
2149 *nptr = VARNUM_MIN;
2150 else
2151 *nptr = -(varnumber_T)un;
2152 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002153 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002154 {
Bram Moolenaar338bf582022-05-22 20:16:32 +01002155 // prevent a larg unsigned number to become negative
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002156 if (un > VARNUM_MAX)
2157 un = VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002158 *nptr = (varnumber_T)un;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002159 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (unptr != NULL)
2162 *unptr = un;
2163}
2164
2165/*
2166 * Return the value of a single hex character.
2167 * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'.
2168 */
2169 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170hex2nr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 if (c >= 'a' && c <= 'f')
2173 return c - 'a' + 10;
2174 if (c >= 'A' && c <= 'F')
2175 return c - 'A' + 10;
2176 return c - '0';
2177}
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179/*
2180 * Convert two hex characters to a byte.
2181 * Return -1 if one of the characters is not hex.
2182 */
2183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184hexhex2nr(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1]))
2187 return -1;
2188 return (hex2nr(p[0]) << 4) + hex2nr(p[1]);
2189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191/*
2192 * Return TRUE if "str" starts with a backslash that should be removed.
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01002193 * For MS-DOS, MSWIN and OS/2 this is only done when the character after the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 * backslash is not a normal file name character.
2195 * '$' is a valid file name character, we don't remove the backslash before
2196 * it. This means it is not possible to use an environment variable after a
2197 * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works.
2198 * Although "\ name" is valid, the backslash in "Program\ files" must be
2199 * removed. Assume a file name doesn't start with a space.
2200 * For multi-byte names, never remove a backslash before a non-ascii
2201 * character, assume that all multi-byte characters are valid file name
2202 * characters.
2203 */
2204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205rem_backslash(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
2207#ifdef BACKSLASH_IN_FILENAME
2208 return (str[0] == '\\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 && str[1] < 0x80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 && (str[1] == ' '
2211 || (str[1] != NUL
2212 && str[1] != '*'
2213 && str[1] != '?'
2214 && !vim_isfilec(str[1]))));
2215#else
2216 return (str[0] == '\\' && str[1] != NUL);
2217#endif
2218}
2219
2220/*
2221 * Halve the number of backslashes in a file name argument.
2222 * For MS-DOS we only do this if the character after the backslash
2223 * is not a normal file character.
2224 */
2225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226backslash_halve(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228 for ( ; *p; ++p)
2229 if (rem_backslash(p))
Bram Moolenaar446cb832008-06-24 21:56:24 +00002230 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231}
2232
2233/*
2234 * backslash_halve() plus save the result in allocated memory.
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002235 * However, returns "p" when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 */
2237 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238backslash_halve_save(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 char_u *res;
2241
2242 res = vim_strsave(p);
2243 if (res == NULL)
2244 return p;
2245 backslash_halve(res);
2246 return res;
2247}