blob: df95ed1b50e67158c9e367c9144a901be65c3f82 [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
Bram Moolenaar13505972019-01-24 15:04:48 +010015static int win_nolbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
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
132#ifdef FEAT_LISP
133 /*
134 * In lisp mode the '-' character is included in keywords.
135 */
136 if (buf->b_p_lisp)
137 SET_CHARTAB(buf, '-');
138#endif
139
Bram Moolenaarc667da52019-11-30 20:52:27 +0100140 // Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint'
141 // options Each option is a list of characters, character numbers or
142 // ranges, separated by commas, e.g.: "200-210,x,#-178,-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 for (i = global ? 0 : 3; i <= 3; ++i)
144 {
145 if (i == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100146 p = p_isi; // first round: 'isident'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 else if (i == 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100148 p = p_isp; // second round: 'isprint'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 else if (i == 2)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100150 p = p_isf; // third round: 'isfname'
151 else // i == 3
152 p = buf->b_p_isk; // fourth round: 'iskeyword'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 while (*p)
155 {
156 tilde = FALSE;
157 do_isalpha = FALSE;
158 if (*p == '^' && p[1] != NUL)
159 {
160 tilde = TRUE;
161 ++p;
162 }
163 if (VIM_ISDIGIT(*p))
164 c = getdigits(&p);
Dominique Pelle4781d6f2021-05-18 21:46:31 +0200165 else if (has_mbyte)
Bram Moolenaar183bb3e2009-09-11 12:02:34 +0000166 c = mb_ptr2char_adv(&p);
167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 c = *p++;
169 c2 = -1;
170 if (*p == '-' && p[1] != NUL)
171 {
172 ++p;
173 if (VIM_ISDIGIT(*p))
174 c2 = getdigits(&p);
Dominique Pelle4781d6f2021-05-18 21:46:31 +0200175 else if (has_mbyte)
Bram Moolenaar2ac5e602009-11-03 15:04:20 +0000176 c2 = mb_ptr2char_adv(&p);
177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 c2 = *p++;
179 }
Bram Moolenaar2ac5e602009-11-03 15:04:20 +0000180 if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 || !(*p == NUL || *p == ','))
182 return FAIL;
183
Bram Moolenaarc667da52019-11-30 20:52:27 +0100184 if (c2 == -1) // not a range
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 {
186 /*
187 * A single '@' (not "@-@"):
188 * Decide on letters being ID/printable/keyword chars with
189 * standard function isalpha(). This takes care of locale for
190 * single-byte characters).
191 */
192 if (c == '@')
193 {
194 do_isalpha = TRUE;
195 c = 1;
196 c2 = 255;
197 }
198 else
199 c2 = c;
200 }
201 while (c <= c2)
202 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100203 // Use the MB_ functions here, because isalpha() doesn't
204 // work properly when 'encoding' is "latin1" and the locale is
205 // "C".
Bram Moolenaar14184a32019-02-16 15:10:30 +0100206 if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100208 if (i == 0) // (re)set ID flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 if (tilde)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100211 g_chartab[c] &= ~CT_ID_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100213 g_chartab[c] |= CT_ID_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100215 else if (i == 1) // (re)set printable
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000217 if ((c < ' ' || c > '~'
Bram Moolenaar13505972019-01-24 15:04:48 +0100218 // For double-byte we keep the cell width, so
219 // that we can detect it from the first byte.
220 ) && !(enc_dbcs && MB_BYTE2LEN(c) == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 {
222 if (tilde)
223 {
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100224 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 + ((dy_flags & DY_UHEX) ? 4 : 2);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100226 g_chartab[c] &= ~CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228 else
229 {
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100230 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) + 1;
231 g_chartab[c] |= CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 }
233 }
234 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100235 else if (i == 2) // (re)set fname flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {
237 if (tilde)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100238 g_chartab[c] &= ~CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100240 g_chartab[c] |= CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100242 else // i == 3 (re)set keyword flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {
244 if (tilde)
245 RESET_CHARTAB(buf, c);
246 else
247 SET_CHARTAB(buf, c);
248 }
249 }
250 ++c;
251 }
Bram Moolenaar309379f2013-02-06 16:26:26 +0100252
253 c = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 p = skip_to_option_part(p);
Bram Moolenaar309379f2013-02-06 16:26:26 +0100255 if (c == ',' && *p == NUL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100256 // Trailing comma is not allowed.
Bram Moolenaar309379f2013-02-06 16:26:26 +0100257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 }
259 }
260 chartab_initialized = TRUE;
261 return OK;
262}
263
264/*
265 * Translate any special characters in buf[bufsize] in-place.
266 * The result is a string with only printable characters, but if there is not
267 * enough room, not all characters will be translated.
268 */
269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100270trans_characters(
271 char_u *buf,
272 int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100274 int len; // length of string needing translation
275 int room; // room in buffer after string
276 char_u *trs; // translated character
277 int trs_len; // length of trs[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279 len = (int)STRLEN(buf);
280 room = bufsize - len;
281 while (*buf != 0)
282 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100283 // Assume a multi-byte character doesn't need translation.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000284 if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 len -= trs_len;
286 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 {
288 trs = transchar_byte(*buf);
289 trs_len = (int)STRLEN(trs);
290 if (trs_len > 1)
291 {
292 room -= trs_len - 1;
293 if (room <= 0)
294 return;
295 mch_memmove(buf + trs_len, buf + 1, (size_t)len);
296 }
297 mch_memmove(buf, trs, (size_t)trs_len);
298 --len;
299 }
300 buf += trs_len;
301 }
302}
303
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304/*
305 * Translate a string into allocated memory, replacing special chars with
306 * printable chars. Returns NULL when out of memory.
307 */
308 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100309transstr(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310{
311 char_u *res;
312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 int l, len, c;
314 char_u hexbuf[11];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 if (has_mbyte)
317 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100318 // Compute the length of the result, taking account of unprintable
319 // multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 len = 0;
321 p = s;
322 while (*p != NUL)
323 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000324 if ((l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {
326 c = (*mb_ptr2char)(p);
327 p += l;
328 if (vim_isprintc(c))
329 len += l;
330 else
331 {
332 transchar_hex(hexbuf, c);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000333 len += (int)STRLEN(hexbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 }
335 }
336 else
337 {
338 l = byte2cells(*p++);
339 if (l > 0)
340 len += l;
341 else
Bram Moolenaarc667da52019-11-30 20:52:27 +0100342 len += 4; // illegal byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
344 }
Bram Moolenaar964b3742019-05-24 18:54:09 +0200345 res = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 }
347 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200348 res = alloc(vim_strsize(s) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (res != NULL)
350 {
351 *res = NUL;
352 p = s;
353 while (*p != NUL)
354 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000355 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
357 c = (*mb_ptr2char)(p);
358 if (vim_isprintc(c))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100359 STRNCAT(res, p, l); // append printable multi-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 else
361 transchar_hex(res + STRLEN(res), c);
362 p += l;
363 }
364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 STRCAT(res, transchar_byte(*p++));
366 }
367 }
368 return res;
369}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371/*
Bram Moolenaar217ad922005-03-20 22:37:15 +0000372 * Convert the string "str[orglen]" to do ignore-case comparing. Uses the
373 * current locale.
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000374 * When "buf" is NULL returns an allocated string (NULL for out-of-memory).
375 * Otherwise puts the result in "buf[buflen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 */
377 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100378str_foldcase(
379 char_u *str,
380 int orglen,
381 char_u *buf,
382 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383{
384 garray_T ga;
385 int i;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000386 int len = orglen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
388#define GA_CHAR(i) ((char_u *)ga.ga_data)[i]
389#define GA_PTR(i) ((char_u *)ga.ga_data + i)
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000390#define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i])
391#define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Bram Moolenaarc667da52019-11-30 20:52:27 +0100393 // Copy "str" into "buf" or allocated memory, unmodified.
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000394 if (buf == NULL)
395 {
396 ga_init2(&ga, 1, 10);
397 if (ga_grow(&ga, len + 1) == FAIL)
398 return NULL;
399 mch_memmove(ga.ga_data, str, (size_t)len);
400 ga.ga_len = len;
401 }
402 else
403 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100404 if (len >= buflen) // Ugly!
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000405 len = buflen - 1;
406 mch_memmove(buf, str, (size_t)len);
407 }
408 if (buf == NULL)
409 GA_CHAR(len) = NUL;
410 else
411 buf[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412
Bram Moolenaarc667da52019-11-30 20:52:27 +0100413 // Make each character lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 i = 0;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000415 while (STR_CHAR(i) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000417 if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {
419 if (enc_utf8)
420 {
Bram Moolenaarb9839212008-06-28 11:03:50 +0000421 int c = utf_ptr2char(STR_PTR(i));
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100422 int olen = utf_ptr2len(STR_PTR(i));
Bram Moolenaarb9839212008-06-28 11:03:50 +0000423 int lc = utf_tolower(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
Bram Moolenaarc667da52019-11-30 20:52:27 +0100425 // Only replace the character when it is not an invalid
426 // sequence (ASCII character or more than one byte) and
427 // utf_tolower() doesn't return the original character.
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100428 if ((c < 0x80 || olen > 1) && c != lc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100430 int nlen = utf_char2len(lc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431
Bram Moolenaarc667da52019-11-30 20:52:27 +0100432 // If the byte length changes need to shift the following
433 // characters forward or backward.
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100434 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100436 if (nlen > olen)
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000437 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100438 if (buf == NULL
439 ? ga_grow(&ga, nlen - olen + 1) == FAIL
440 : len + nlen - olen >= buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100442 // out of memory, keep old char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 lc = c;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100444 nlen = olen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000446 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100447 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000449 if (buf == NULL)
450 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100451 STRMOVE(GA_PTR(i) + nlen, GA_PTR(i) + olen);
452 ga.ga_len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000453 }
454 else
455 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100456 STRMOVE(buf + i + nlen, buf + i + olen);
457 len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 }
460 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000461 (void)utf_char2bytes(lc, STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 }
463 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100464 // skip to next multi-byte char
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000465 i += (*mb_ptr2len)(STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 }
467 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000469 if (buf == NULL)
470 GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i));
471 else
472 buf[i] = TOLOWER_LOC(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 ++i;
474 }
475 }
476
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000477 if (buf == NULL)
478 return (char_u *)ga.ga_data;
479 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
482/*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100483 * Catch 22: g_chartab[] can't be initialized before the options are
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 * initialized, and initializing options may cause transchar() to be called!
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100485 * When chartab_initialized == FALSE don't use g_chartab[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 * Does NOT work for multi-byte characters, c must be <= 255.
487 * Also doesn't work for the first byte of a multi-byte, "c" must be a
488 * character!
489 */
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200490static char_u transchar_charbuf[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100493transchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200495 return transchar_buf(curbuf, c);
496}
497
498 char_u *
499transchar_buf(buf_T *buf, int c)
500{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 int i;
502
503 i = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100504 if (IS_SPECIAL(c)) // special key code, display as ~@ char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200506 transchar_charbuf[0] = '~';
507 transchar_charbuf[1] = '@';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 i = 2;
509 c = K_SECOND(c);
510 }
511
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000512 if ((!chartab_initialized && ((c >= ' ' && c <= '~')))
513 || (c < 256 && vim_isprintc_strict(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100515 // printable character
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200516 transchar_charbuf[i] = c;
517 transchar_charbuf[i + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 }
519 else
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200520 transchar_nonprint(buf, transchar_charbuf + i, c);
521 return transchar_charbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522}
523
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524/*
525 * Like transchar(), but called with a byte instead of a character. Checks
526 * for an illegal UTF-8 byte.
527 */
528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100529transchar_byte(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
531 if (enc_utf8 && c >= 0x80)
532 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200533 transchar_nonprint(curbuf, transchar_charbuf, c);
534 return transchar_charbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 }
536 return transchar(c);
537}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539/*
540 * Convert non-printable character to two or more printable characters in
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200541 * "buf[]". "charbuf" needs to be able to hold five bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * Does NOT work for multi-byte characters, c must be <= 255.
543 */
544 void
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200545transchar_nonprint(buf_T *buf, char_u *charbuf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 if (c == NL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100548 c = NUL; // we use newline in place of a NUL
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200549 else if (c == CAR && get_fileformat(buf) == EOL_MAC)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100550 c = NL; // we use CR in place of NL in this case
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaarc667da52019-11-30 20:52:27 +0100552 if (dy_flags & DY_UHEX) // 'display' has "uhex"
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200553 transchar_hex(charbuf, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaarc667da52019-11-30 20:52:27 +0100555 else if (c <= 0x7f) // 0x00 - 0x1f and 0x7f
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200557 charbuf[0] = '^';
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200558 charbuf[1] = c ^ 0x40; // DEL displayed as ^?
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200559 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 else if (enc_utf8 && c >= 0x80)
562 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200563 transchar_hex(charbuf, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100565 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) // 0xa0 - 0xfe
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200567 charbuf[0] = '|';
568 charbuf[1] = c - 0x80;
569 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 else // 0x80 - 0x9f and 0xff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200573 charbuf[0] = '~';
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200574 charbuf[1] = (c - 0x80) ^ 0x40; // 0xff displayed as ~?
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200575 charbuf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
577}
578
579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100580transchar_hex(char_u *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
582 int i = 0;
583
584 buf[0] = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (c > 255)
586 {
587 buf[++i] = nr2hex((unsigned)c >> 12);
588 buf[++i] = nr2hex((unsigned)c >> 8);
589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 buf[++i] = nr2hex((unsigned)c >> 4);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000591 buf[++i] = nr2hex((unsigned)c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 buf[++i] = '>';
593 buf[++i] = NUL;
594}
595
596/*
597 * Convert the lower 4 bits of byte "c" to its hex character.
598 * Lower case letters are used to avoid the confusion of <F1> being 0xf1 or
599 * function key 1.
600 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000601 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602nr2hex(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604 if ((c & 0xf) <= 9)
605 return (c & 0xf) + '0';
606 return (c & 0xf) - 10 + 'a';
607}
608
609/*
610 * Return number of display cells occupied by byte "b".
611 * Caller must make sure 0 <= b <= 255.
612 * For multi-byte mode "b" must be the first byte of a character.
613 * A TAB is counted as two cells: "^I".
614 * For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of
615 * cells depends on further bytes.
616 */
617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100618byte2cells(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (enc_utf8 && b >= 0x80)
621 return 0;
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100622 return (g_chartab[b] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623}
624
625/*
626 * Return number of display cells occupied by character "c".
627 * "c" can be a special key (negative number) in which case 3 or 4 is returned.
628 * A TAB is counted as two cells: "^I" or four: "<09>".
629 */
630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631char2cells(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 if (IS_SPECIAL(c))
634 return char2cells(K_SECOND(c)) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (c >= 0x80)
636 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100637 // UTF-8: above 0x80 need to check the value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 if (enc_utf8)
639 return utf_char2cells(c);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100640 // DBCS: double-byte means double-width, except for euc-jp with first
641 // byte 0x8e
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (enc_dbcs != 0 && c >= 0x100)
643 {
644 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
645 return 1;
646 return 2;
647 }
648 }
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100649 return (g_chartab[c & 0xff] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650}
651
652/*
653 * Return number of display cells occupied by character at "*p".
654 * A TAB is counted as two cells: "^I" or four: "<09>".
655 */
656 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100657ptr2cells(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100659 // For UTF-8 we need to look at more bytes if the first byte is >= 0x80.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 if (enc_utf8 && *p >= 0x80)
661 return utf_ptr2cells(p);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100662 // For DBCS we can tell the cell count from the first byte.
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100663 return (g_chartab[*p] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664}
665
666/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100667 * Return the number of character cells string "s" will take on the screen,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 * counting TABs as two characters: "^I".
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671vim_strsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
673 return vim_strnsize(s, (int)MAXCOL);
674}
675
676/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100677 * Return the number of character cells string "s[len]" will take on the
678 * screen, counting TABs as two characters: "^I".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 */
680 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681vim_strnsize(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 int size = 0;
684
685 while (*s != NUL && --len >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (has_mbyte)
687 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000688 int l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 size += ptr2cells(s);
691 s += l;
692 len -= l - 1;
693 }
694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 size += byte2cells(*s++);
Bram Moolenaar13505972019-01-24 15:04:48 +0100696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 return size;
698}
699
700/*
701 * Return the number of characters 'c' will take on the screen, taking
702 * into account the size of a tab.
703 * Use a define to make it fast, this is used very often!!!
704 * Also see getvcol() below.
705 */
706
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200707#ifdef FEAT_VARTABS
708# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
Bram Moolenaareed9d462021-02-15 20:38:25 +0100709 if (*(p) == TAB && (!(wp)->w_p_list || wp->w_lcs_chars.tab1)) \
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200710 { \
711 return tabstop_padding(col, (buf)->b_p_ts, (buf)->b_p_vts_array); \
712 } \
713 else \
714 return ptr2cells(p);
715#else
716# define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
Bram Moolenaareed9d462021-02-15 20:38:25 +0100717 if (*(p) == TAB && (!(wp)->w_p_list || wp->w_lcs_chars.tab1)) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 { \
719 int ts; \
720 ts = (buf)->b_p_ts; \
721 return (int)(ts - (col % ts)); \
722 } \
723 else \
724 return ptr2cells(p);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100728chartabsize(char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col)
731}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733#ifdef FEAT_LINEBREAK
734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100735win_chartabsize(win_T *wp, char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col)
738}
739#endif
740
741/*
Bram Moolenaardc536092010-07-18 15:45:49 +0200742 * Return the number of characters the string 's' will take on the screen,
743 * taking into account the size of a tab.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 */
745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100746linetabsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
Bram Moolenaardc536092010-07-18 15:45:49 +0200748 return linetabsize_col(0, s);
749}
750
751/*
752 * Like linetabsize(), but starting at column "startcol".
753 */
754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100755linetabsize_col(int startcol, char_u *s)
Bram Moolenaardc536092010-07-18 15:45:49 +0200756{
757 colnr_T col = startcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100758 char_u *line = s; // pointer to start of line, for breakindent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 while (*s != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200761 col += lbr_chartabsize_adv(line, &s, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 return (int)col;
763}
764
765/*
766 * Like linetabsize(), but for a given window instead of the current one.
767 */
768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100769win_linetabsize(win_T *wp, char_u *line, colnr_T len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 colnr_T col = 0;
772 char_u *s;
773
Bram Moolenaar597a4222014-06-25 14:39:50 +0200774 for (s = line; *s != NUL && (len == MAXCOL || s < line + len);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100775 MB_PTR_ADV(s))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200776 col += win_lbr_chartabsize(wp, line, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 return (int)col;
778}
779
780/*
Bram Moolenaar81695252004-12-29 20:58:21 +0000781 * Return TRUE if 'c' is a normal identifier character:
782 * Letters and characters from the 'isident' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 */
784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100785vim_isIDc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100787 return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790/*
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +0200791 * Like vim_isIDc() but not using the 'isident' option: letters, numbers and
792 * underscore.
793 */
794 int
795vim_isNormalIDc(int c)
796{
797 return ASCII_ISALNUM(c) || c == '_';
798}
799
800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 * return TRUE if 'c' is a keyword character: Letters and characters from
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100802 * 'iskeyword' option for the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * For multi-byte characters mb_get_class() is used (builtin rules).
804 */
805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100806vim_iswordc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100808 return vim_iswordc_buf(c, curbuf);
809}
810
811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100812vim_iswordc_buf(int c, buf_T *buf)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100813{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (c >= 0x100)
815 {
816 if (enc_dbcs != 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000817 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (enc_utf8)
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100819 return utf_class_buf(c, buf) >= 2;
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100820 return FALSE;
821 }
822 return (c > 0 && GET_CHARTAB(buf, c) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823}
824
825/*
826 * Just like vim_iswordc() but uses a pointer to the (multi-byte) character.
827 */
828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100829vim_iswordp(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100831 return vim_iswordp_buf(p, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835vim_iswordp_buf(char_u *p, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100837 int c = *p;
838
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100839 if (has_mbyte && MB_BYTE2LEN(c) > 1)
840 c = (*mb_ptr2char)(p);
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100841 return vim_iswordc_buf(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844/*
845 * return TRUE if 'c' is a valid file-name character
846 * Assume characters above 0x100 are valid (multi-byte).
847 */
848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100849vim_isfilec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100851 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853
854/*
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000855 * return TRUE if 'c' is a valid file-name character or a wildcard character
856 * Assume characters above 0x100 are valid (multi-byte).
857 * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]")
858 * returns false.
859 */
860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100861vim_isfilec_or_wc(int c)
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000862{
863 char_u buf[2];
864
865 buf[0] = (char_u)c;
866 buf[1] = NUL;
867 return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf);
868}
869
870/*
Bram Moolenaar3317d5e2017-04-08 19:12:06 +0200871 * Return TRUE if 'c' is a printable character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 * Assume characters above 0x100 are printable (multi-byte), except for
873 * Unicode.
874 */
875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100876vim_isprintc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (enc_utf8 && c >= 0x100)
879 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100880 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881}
882
883/*
884 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head
885 * byte of a double-byte character.
886 */
887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888vim_isprintc_strict(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1)
891 return FALSE;
892 if (enc_utf8 && c >= 0x100)
893 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100894 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895}
896
897/*
898 * like chartabsize(), but also check for line breaks on the screen
899 */
900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100901lbr_chartabsize(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100902 char_u *line UNUSED, // start of the line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100903 unsigned char *s,
904 colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +0100907 if (!curwin->w_p_lbr && *get_showbreak_value(curwin) == NUL
908 && !curwin->w_p_bri)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 {
910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (curwin->w_p_wrap)
912 return win_nolbr_chartabsize(curwin, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col)
914#ifdef FEAT_LINEBREAK
915 }
Bram Moolenaar597a4222014-06-25 14:39:50 +0200916 return win_lbr_chartabsize(curwin, line == NULL ? s : line, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#endif
918}
919
920/*
921 * Call lbr_chartabsize() and advance the pointer.
922 */
923 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100924lbr_chartabsize_adv(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100925 char_u *line, // start of the line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100926 char_u **s,
927 colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 int retval;
930
Bram Moolenaar597a4222014-06-25 14:39:50 +0200931 retval = lbr_chartabsize(line, *s, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100932 MB_PTR_ADV(*s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 return retval;
934}
935
936/*
937 * This function is used very often, keep it fast!!!!
938 *
939 * If "headp" not NULL, set *headp to the size of what we for 'showbreak'
940 * string at start of line. Warning: *headp is only set if it's a non-zero
941 * value, init to 0 before calling.
942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100944win_lbr_chartabsize(
945 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100946 char_u *line UNUSED, // start of the line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100947 char_u *s,
948 colnr_T col,
949 int *headp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951#ifdef FEAT_LINEBREAK
952 int c;
953 int size;
954 colnr_T col2;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100955 colnr_T col_adj = 0; // col + screen size of tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 colnr_T colmax;
957 int added;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 int mb_added = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 int numberextra;
960 char_u *ps;
961 int tab_corr = (*s == TAB);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000962 int n;
Bram Moolenaaree857022019-11-09 23:26:40 +0100963 char_u *sbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
965 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +0200966 * No 'linebreak', 'showbreak' and 'breakindent': return quickly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 */
Bram Moolenaaree857022019-11-09 23:26:40 +0100968 if (!wp->w_p_lbr && !wp->w_p_bri && *get_showbreak_value(wp) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#endif
970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (wp->w_p_wrap)
972 return win_nolbr_chartabsize(wp, s, col, headp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col)
974 }
975
976#ifdef FEAT_LINEBREAK
977 /*
978 * First get normal size, without 'linebreak'
979 */
980 size = win_chartabsize(wp, s, col);
981 c = *s;
Bram Moolenaaree739b42014-07-02 19:37:42 +0200982 if (tab_corr)
983 col_adj = size - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 /*
986 * If 'linebreak' set check at a blank before a non-blank if the line
987 * needs a break here
988 */
989 if (wp->w_p_lbr
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100990 && VIM_ISBREAK(c)
Bram Moolenaar977d0372017-03-12 21:31:58 +0100991 && !VIM_ISBREAK((int)s[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 && wp->w_p_wrap
Bram Moolenaar4033c552017-09-16 20:54:51 +0200993 && wp->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
995 /*
996 * Count all characters from first non-blank after a blank up to next
997 * non-blank after a blank.
998 */
999 numberextra = win_col_off(wp);
1000 col2 = col;
Bram Moolenaar02631462017-09-22 15:20:32 +02001001 colmax = (colnr_T)(wp->w_width - numberextra - col_adj);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (col >= colmax)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001003 {
Bram Moolenaaree739b42014-07-02 19:37:42 +02001004 colmax += col_adj;
1005 n = colmax + win_col_off2(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001006 if (n > 0)
Bram Moolenaaree739b42014-07-02 19:37:42 +02001007 colmax += (((col - colmax) / n) + 1) * n - col_adj;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008 }
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 for (;;)
1011 {
1012 ps = s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001013 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 c = *s;
1015 if (!(c != NUL
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001016 && (VIM_ISBREAK(c)
1017 || (!VIM_ISBREAK(c)
Bram Moolenaar977d0372017-03-12 21:31:58 +01001018 && (col2 == col || !VIM_ISBREAK((int)*ps))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 break;
1020
1021 col2 += win_chartabsize(wp, s, col2);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001022 if (col2 >= colmax) // doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaaree739b42014-07-02 19:37:42 +02001024 size = colmax - col + col_adj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 break;
1026 }
1027 }
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1
1030 && wp->w_p_wrap && in_win_border(wp, col))
1031 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001032 ++size; // Count the ">" in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 mb_added = 1;
1034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +02001037 * May have to add something for 'breakindent' and/or 'showbreak'
1038 * string at start of line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 * Set *headp to the size of what we add.
1040 */
1041 added = 0;
Bram Moolenaaree857022019-11-09 23:26:40 +01001042 sbr = get_showbreak_value(wp);
1043 if ((*sbr != NUL || wp->w_p_bri) && wp->w_p_wrap && col != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
Bram Moolenaard574ea22015-01-14 19:35:14 +01001045 colnr_T sbrlen = 0;
1046 int numberwidth = win_col_off(wp);
1047
1048 numberextra = numberwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 col += numberextra + mb_added;
Bram Moolenaar02631462017-09-22 15:20:32 +02001050 if (col >= (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001052 col -= wp->w_width;
1053 numberextra = wp->w_width - (numberextra - win_col_off2(wp));
Bram Moolenaard574ea22015-01-14 19:35:14 +01001054 if (col >= numberextra && numberextra > 0)
Bram Moolenaarfe3c4102014-10-31 12:42:01 +01001055 col %= numberextra;
Bram Moolenaaree857022019-11-09 23:26:40 +01001056 if (*sbr != NUL)
Bram Moolenaar1c852102014-10-15 21:26:40 +02001057 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001058 sbrlen = (colnr_T)MB_CHARLEN(sbr);
Bram Moolenaar1c852102014-10-15 21:26:40 +02001059 if (col >= sbrlen)
1060 col -= sbrlen;
1061 }
Bram Moolenaard574ea22015-01-14 19:35:14 +01001062 if (col >= numberextra && numberextra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 col = col % numberextra;
Bram Moolenaard574ea22015-01-14 19:35:14 +01001064 else if (col > 0 && numberextra > 0)
1065 col += numberwidth - win_col_off2(wp);
1066
1067 numberwidth -= win_col_off2(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001069 if (col == 0 || col + size + sbrlen > (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02001071 added = 0;
Bram Moolenaaree857022019-11-09 23:26:40 +01001072 if (*sbr != NUL)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001073 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001074 if (size + sbrlen + numberwidth > (colnr_T)wp->w_width)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001075 {
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001076 // calculate effective window width
Bram Moolenaar02631462017-09-22 15:20:32 +02001077 int width = (colnr_T)wp->w_width - sbrlen - numberwidth;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001078 int prev_width = col
1079 ? ((colnr_T)wp->w_width - (sbrlen + col)) : 0;
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001080
1081 if (width <= 0)
1082 width = (colnr_T)1;
Bram Moolenaaree857022019-11-09 23:26:40 +01001083 added += ((size - prev_width) / width) * vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001084 if ((size - prev_width) % width)
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001085 // wrapped, add another length of 'sbr'
Bram Moolenaaree857022019-11-09 23:26:40 +01001086 added += vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001087 }
1088 else
Bram Moolenaaree857022019-11-09 23:26:40 +01001089 added += vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001090 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02001091 if (wp->w_p_bri)
1092 added += get_breakindent_win(wp, line);
1093
Bram Moolenaar95765082014-08-24 21:19:25 +02001094 size += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (col != 0)
1096 added = 0;
1097 }
1098 }
1099 if (headp != NULL)
1100 *headp = added + mb_added;
1101 return size;
1102#endif
1103}
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105/*
1106 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and
1107 * 'wrap' is on. This means we need to check for a double-byte character that
1108 * doesn't fit at the end of the screen line.
1109 */
1110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001111win_nolbr_chartabsize(
1112 win_T *wp,
1113 char_u *s,
1114 colnr_T col,
1115 int *headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 int n;
1118
Bram Moolenaareed9d462021-02-15 20:38:25 +01001119 if (*s == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001121# ifdef FEAT_VARTABS
1122 return tabstop_padding(col, wp->w_buffer->b_p_ts,
1123 wp->w_buffer->b_p_vts_array);
1124# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 n = wp->w_buffer->b_p_ts;
1126 return (int)(n - (col % n));
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 n = ptr2cells(s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001130 // Add one cell for a double-width character in the last column of the
1131 // window, displayed with a ">".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col))
1133 {
1134 if (headp != NULL)
1135 *headp = 1;
1136 return 3;
1137 }
1138 return n;
1139}
1140
1141/*
1142 * Return TRUE if virtual column "vcol" is in the rightmost column of window
1143 * "wp".
1144 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001145 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001146in_win_border(win_T *wp, colnr_T vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001148 int width1; // width of first line (after line number)
1149 int width2; // width of further lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaarc667da52019-11-30 20:52:27 +01001151 if (wp->w_width == 0) // there is no border
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 return FALSE;
Bram Moolenaar02631462017-09-22 15:20:32 +02001153 width1 = wp->w_width - win_col_off(wp);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001154 if ((int)vcol < width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001156 if ((int)vcol == width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 return TRUE;
1158 width2 = width1 + win_col_off2(wp);
Bram Moolenaar8701cd62009-10-07 14:20:30 +00001159 if (width2 <= 0)
1160 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 return ((vcol - width1) % width2 == width2 - 1);
1162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164/*
1165 * Get virtual column number of pos.
1166 * start: on the first position of this character (TAB, ctrl)
1167 * cursor: where the cursor is on this character (first char, except for TAB)
1168 * end: on the last position of this character (TAB, ctrl)
1169 *
1170 * This is used very often, keep it fast!
1171 */
1172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001173getvcol(
1174 win_T *wp,
1175 pos_T *pos,
1176 colnr_T *start,
1177 colnr_T *cursor,
1178 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 colnr_T vcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001181 char_u *ptr; // points to current char
1182 char_u *posptr; // points to char at pos->col
1183 char_u *line; // start of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 int incr;
1185 int head;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001186#ifdef FEAT_VARTABS
1187 int *vts = wp->w_buffer->b_p_vts_array;
1188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 int ts = wp->w_buffer->b_p_ts;
1190 int c;
1191
1192 vcol = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001193 line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001194 if (pos->col == MAXCOL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001195 posptr = NULL; // continue until the NUL
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001196 else
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001197 {
Bram Moolenaar94f31922021-12-30 15:29:18 +00001198 colnr_T i;
1199
1200 // In a few cases the position can be beyond the end of the line.
1201 for (i = 0; i < pos->col; ++i)
1202 if (ptr[i] == NUL)
1203 {
1204 pos->col = i;
1205 break;
1206 }
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001207 posptr = ptr + pos->col;
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001208 if (has_mbyte)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001209 // always start on the first byte
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001210 posptr -= (*mb_head_off)(line, posptr);
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
1213 /*
1214 * This function is used very often, do some speed optimizations.
Bram Moolenaar597a4222014-06-25 14:39:50 +02001215 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set
1216 * use a simple loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 * Also use this when 'list' is set but tabs take their normal size.
1218 */
Bram Moolenaareed9d462021-02-15 20:38:25 +01001219 if ((!wp->w_p_list || wp->w_lcs_chars.tab1 != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001221 && !wp->w_p_lbr && *get_showbreak_value(wp) == NUL && !wp->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif
1223 )
1224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 for (;;)
1226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 head = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 c = *ptr;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001229 // make sure we don't go past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (c == NUL)
1231 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001232 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 break;
1234 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001235 // A tab gets expanded, depending on the current column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (c == TAB)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001237#ifdef FEAT_VARTABS
1238 incr = tabstop_padding(vcol, ts, vts);
1239#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 incr = ts - (vcol % ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 else
1243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (has_mbyte)
1245 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001246 // For utf-8, if the byte is >= 0x80, need to look at
1247 // further bytes to find the cell width.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (enc_utf8 && c >= 0x80)
1249 incr = utf_ptr2cells(ptr);
1250 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001251 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaarc667da52019-11-30 20:52:27 +01001253 // If a double-cell char doesn't fit at the end of a line
1254 // it wraps to the next line, it's like this char is three
1255 // cells wide.
Bram Moolenaar9c33a7c2008-02-20 13:59:32 +00001256 if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1
1257 && in_win_border(wp, vcol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 ++incr;
1260 head = 1;
1261 }
1262 }
1263 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001264 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
Bram Moolenaarc667da52019-11-30 20:52:27 +01001267 if (posptr != NULL && ptr >= posptr) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 break;
1269
1270 vcol += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001271 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 }
1274 else
1275 {
1276 for (;;)
1277 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001278 // A tab gets expanded, depending on the current column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 head = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001280 incr = win_lbr_chartabsize(wp, line, ptr, vcol, &head);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001281 // make sure we don't go past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (*ptr == NUL)
1283 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001284 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 break;
1286 }
1287
Bram Moolenaarc667da52019-11-30 20:52:27 +01001288 if (posptr != NULL && ptr >= posptr) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 break;
1290
1291 vcol += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001292 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294 }
1295 if (start != NULL)
1296 *start = vcol + head;
1297 if (end != NULL)
1298 *end = vcol + incr - 1;
1299 if (cursor != NULL)
1300 {
1301 if (*ptr == TAB
1302 && (State & NORMAL)
1303 && !wp->w_p_list
1304 && !virtual_active()
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001305 && !(VIsual_active
1306 && (*p_sel == 'e' || LTOREQ_POS(*pos, VIsual)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 )
Bram Moolenaarc667da52019-11-30 20:52:27 +01001308 *cursor = vcol + incr - 1; // cursor at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01001310 *cursor = vcol + head; // cursor at start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312}
1313
1314/*
1315 * Get virtual cursor column in the current window, pretending 'list' is off.
1316 */
1317 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318getvcol_nolist(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int list_save = curwin->w_p_list;
1321 colnr_T vcol;
1322
1323 curwin->w_p_list = FALSE;
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001324 if (posp->coladd)
1325 getvvcol(curwin, posp, NULL, &vcol, NULL);
1326 else
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001327 getvcol(curwin, posp, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 curwin->w_p_list = list_save;
1329 return vcol;
1330}
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332/*
1333 * Get virtual column in virtual mode.
1334 */
1335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001336getvvcol(
1337 win_T *wp,
1338 pos_T *pos,
1339 colnr_T *start,
1340 colnr_T *cursor,
1341 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 colnr_T col;
1344 colnr_T coladd;
1345 colnr_T endadd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 if (virtual_active())
1349 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001350 // For virtual mode, only want one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 getvcol(wp, pos, &col, NULL, NULL);
1352
1353 coladd = pos->coladd;
1354 endadd = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001355 // Cannot put the cursor on part of a wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001357 if (pos->col < (colnr_T)STRLEN(ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 int c = (*mb_ptr2char)(ptr + pos->col);
1360
1361 if (c != TAB && vim_isprintc(c))
1362 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001363 endadd = (colnr_T)(char2cells(c) - 1);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001364 if (coladd > endadd) // past end of line
Bram Moolenaara5792f52005-11-23 21:25:05 +00001365 endadd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 else
1367 coladd = 0;
1368 }
1369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 col += coladd;
1371 if (start != NULL)
1372 *start = col;
1373 if (cursor != NULL)
1374 *cursor = col;
1375 if (end != NULL)
1376 *end = col + endadd;
1377 }
1378 else
1379 getvcol(wp, pos, start, cursor, end);
1380}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382/*
1383 * Get the leftmost and rightmost virtual column of pos1 and pos2.
1384 * Used for Visual block mode.
1385 */
1386 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001387getvcols(
1388 win_T *wp,
1389 pos_T *pos1,
1390 pos_T *pos2,
1391 colnr_T *left,
1392 colnr_T *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 colnr_T from1, from2, to1, to2;
1395
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001396 if (LT_POSP(pos1, pos2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
1398 getvvcol(wp, pos1, &from1, NULL, &to1);
1399 getvvcol(wp, pos2, &from2, NULL, &to2);
1400 }
1401 else
1402 {
1403 getvvcol(wp, pos2, &from1, NULL, &to1);
1404 getvvcol(wp, pos1, &from2, NULL, &to2);
1405 }
1406 if (from2 < from1)
1407 *left = from2;
1408 else
1409 *left = from1;
1410 if (to2 > to1)
1411 {
1412 if (*p_sel == 'e' && from2 - 1 >= to1)
1413 *right = from2 - 1;
1414 else
1415 *right = to2;
1416 }
1417 else
1418 *right = to1;
1419}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421/*
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001422 * Skip over ' ' and '\t'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
1424 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001425skipwhite(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001427 char_u *p = q;
1428
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001429 while (VIM_ISWHITE(*p))
1430 ++p;
1431 return p;
1432}
1433
Dominique Pelle748b3082022-01-08 12:41:16 +00001434#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001435/*
1436 * skip over ' ', '\t' and '\n'.
1437 */
1438 char_u *
1439skipwhite_and_nl(char_u *q)
1440{
1441 char_u *p = q;
1442
1443 while (VIM_ISWHITE(*p) || *p == NL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 ++p;
1445 return p;
1446}
Dominique Pelle748b3082022-01-08 12:41:16 +00001447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449/*
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001450 * getwhitecols: return the number of whitespace
1451 * columns (bytes) at the start of a given line
1452 */
1453 int
1454getwhitecols_curline()
1455{
1456 return getwhitecols(ml_get_curline());
1457}
1458
1459 int
1460getwhitecols(char_u *p)
1461{
1462 return skipwhite(p) - p;
1463}
1464
1465/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001466 * skip over digits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001469skipdigits(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001471 char_u *p = q;
1472
Bram Moolenaarc667da52019-11-30 20:52:27 +01001473 while (VIM_ISDIGIT(*p)) // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ++p;
1475 return p;
1476}
1477
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001478#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001479/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001480 * skip over binary digits
1481 */
1482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483skipbin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001484{
1485 char_u *p = q;
1486
Bram Moolenaarc667da52019-11-30 20:52:27 +01001487 while (vim_isbdigit(*p)) // skip to next non-digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001488 ++p;
1489 return p;
1490}
1491
1492/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001493 * skip over digits and hex characters
1494 */
1495 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001496skiphex(char_u *q)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001497{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001498 char_u *p = q;
1499
Bram Moolenaarc667da52019-11-30 20:52:27 +01001500 while (vim_isxdigit(*p)) // skip to next non-digit
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001501 ++p;
1502 return p;
1503}
1504#endif
1505
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001506/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001507 * skip to bin digit (or NUL after the string)
1508 */
1509 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001510skiptobin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001511{
1512 char_u *p = q;
1513
Bram Moolenaarc667da52019-11-30 20:52:27 +01001514 while (*p != NUL && !vim_isbdigit(*p)) // skip to next digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001515 ++p;
1516 return p;
1517}
1518
1519/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001520 * skip to digit (or NUL after the string)
1521 */
1522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001523skiptodigit(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001524{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001525 char_u *p = q;
1526
Bram Moolenaarc667da52019-11-30 20:52:27 +01001527 while (*p != NUL && !VIM_ISDIGIT(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001528 ++p;
1529 return p;
1530}
1531
1532/*
1533 * skip to hex character (or NUL after the string)
1534 */
1535 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536skiptohex(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001537{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001538 char_u *p = q;
1539
Bram Moolenaarc667da52019-11-30 20:52:27 +01001540 while (*p != NUL && !vim_isxdigit(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001541 ++p;
1542 return p;
1543}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545/*
1546 * Variant of isdigit() that can handle characters > 0x100.
1547 * We don't use isdigit() here, because on some systems it also considers
1548 * superscript 1 to be a digit.
1549 * Use the VIM_ISDIGIT() macro for simple arguments.
1550 */
1551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552vim_isdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 return (c >= '0' && c <= '9');
1555}
1556
1557/*
1558 * Variant of isxdigit() that can handle characters > 0x100.
1559 * We don't use isxdigit() here, because on some systems it also considers
1560 * superscript 1 to be a digit.
1561 */
1562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001563vim_isxdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 return (c >= '0' && c <= '9')
1566 || (c >= 'a' && c <= 'f')
1567 || (c >= 'A' && c <= 'F');
1568}
1569
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001570/*
1571 * Corollary of vim_isdigit and vim_isxdigit() that can handle
1572 * characters > 0x100.
1573 */
1574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001575vim_isbdigit(int c)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001576{
1577 return (c == '0' || c == '1');
1578}
1579
Bram Moolenaarc37b6552021-01-07 19:36:30 +01001580 static int
1581vim_isodigit(int c)
1582{
1583 return (c >= '0' && c <= '7');
1584}
1585
Bram Moolenaar78622822005-08-23 21:00:13 +00001586/*
1587 * Vim's own character class functions. These exist because many library
1588 * islower()/toupper() etc. do not work properly: they crash when used with
1589 * invalid values or can't handle latin1 when the locale is C.
1590 * Speed is most important here.
1591 */
1592#define LATIN1LOWER 'l'
1593#define LATIN1UPPER 'U'
1594
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00001595static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll";
Bram Moolenaar936347b2012-05-25 11:56:22 +02001596static 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";
1597static 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 +00001598
1599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001600vim_islower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001601{
1602 if (c <= '@')
1603 return FALSE;
1604 if (c >= 0x80)
1605 {
1606 if (enc_utf8)
1607 return utf_islower(c);
1608 if (c >= 0x100)
1609 {
1610#ifdef HAVE_ISWLOWER
1611 if (has_mbyte)
1612 return iswlower(c);
1613#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001614 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001615 return FALSE;
1616 }
1617 if (enc_latin1like)
1618 return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER;
1619 }
1620 return islower(c);
1621}
1622
1623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001624vim_isupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001625{
1626 if (c <= '@')
1627 return FALSE;
1628 if (c >= 0x80)
1629 {
1630 if (enc_utf8)
1631 return utf_isupper(c);
1632 if (c >= 0x100)
1633 {
1634#ifdef HAVE_ISWUPPER
1635 if (has_mbyte)
1636 return iswupper(c);
1637#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001638 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001639 return FALSE;
1640 }
1641 if (enc_latin1like)
1642 return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER;
1643 }
1644 return isupper(c);
1645}
1646
1647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001648vim_toupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001649{
1650 if (c <= '@')
1651 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001652 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00001653 {
1654 if (enc_utf8)
1655 return utf_toupper(c);
1656 if (c >= 0x100)
1657 {
1658#ifdef HAVE_TOWUPPER
1659 if (has_mbyte)
1660 return towupper(c);
1661#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001662 // toupper() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001663 return c;
1664 }
1665 if (enc_latin1like)
1666 return latin1upper[c];
1667 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02001668 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
1669 return TOUPPER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00001670 return TOUPPER_LOC(c);
1671}
1672
1673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001674vim_tolower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001675{
1676 if (c <= '@')
1677 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001678 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00001679 {
1680 if (enc_utf8)
1681 return utf_tolower(c);
1682 if (c >= 0x100)
1683 {
1684#ifdef HAVE_TOWLOWER
1685 if (has_mbyte)
1686 return towlower(c);
1687#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001688 // tolower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001689 return c;
1690 }
1691 if (enc_latin1like)
1692 return latin1lower[c];
1693 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02001694 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
1695 return TOLOWER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00001696 return TOLOWER_LOC(c);
1697}
Bram Moolenaar78622822005-08-23 21:00:13 +00001698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699/*
1700 * skiptowhite: skip over text until ' ' or '\t' or NUL.
1701 */
1702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703skiptowhite(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 while (*p != ' ' && *p != '\t' && *p != NUL)
1706 ++p;
1707 return p;
1708}
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710/*
1711 * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars
1712 */
1713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001714skiptowhite_esc(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 while (*p != ' ' && *p != '\t' && *p != NUL)
1717 {
1718 if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL)
1719 ++p;
1720 ++p;
1721 }
1722 return p;
1723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
1725/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00001726 * Get a number from a string and skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 * Note: the argument is a pointer to a char_u pointer!
1728 */
1729 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001730getdigits(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
1732 char_u *p;
1733 long retval;
1734
1735 p = *pp;
1736 retval = atol((char *)p);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001737 if (*p == '-') // skip negative sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ++p;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001739 p = skipdigits(p); // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 *pp = p;
1741 return retval;
1742}
1743
1744/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00001745 * Like getdigits() but allow for embedded single quotes.
1746 */
1747 long
1748getdigits_quoted(char_u **pp)
1749{
1750 char_u *p = *pp;
1751 long retval = 0;
1752
1753 if (*p == '-')
1754 ++p;
1755 while (VIM_ISDIGIT(*p))
1756 {
1757 if (retval >= LONG_MAX / 10 - 10)
1758 retval = LONG_MAX;
1759 else
1760 retval = retval * 10 - '0' + *p;
1761 ++p;
1762 if (in_vim9script() && *p == '\'' && VIM_ISDIGIT(p[1]))
1763 ++p;
1764 }
1765 if (**pp == '-')
1766 {
1767 if (retval == LONG_MAX)
1768 retval = LONG_MIN;
1769 else
1770 retval = -retval;
1771 }
1772 *pp = p;
1773 return retval;
1774}
1775
1776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 * Return TRUE if "lbuf" is empty or only contains blanks.
1778 */
1779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780vim_isblankline(char_u *lbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 char_u *p;
1783
1784 p = skipwhite(lbuf);
1785 return (*p == NUL || *p == '\r' || *p == '\n');
1786}
1787
1788/*
1789 * Convert a string into a long and/or unsigned long, taking care of
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001790 * hexadecimal, octal, and binary numbers. Accepts a '-' sign.
1791 * If "prep" is not NULL, returns a flag to indicate the type of the number:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 * 0 decimal
1793 * '0' octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001794 * 'O' octal
1795 * 'o' octal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001796 * 'B' bin
1797 * 'b' bin
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 * 'X' hex
1799 * 'x' hex
1800 * If "len" is not NULL, the length of the number in characters is returned.
1801 * If "nptr" is not NULL, the signed result is returned in it.
1802 * If "unptr" is not NULL, the unsigned result is returned in it.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001803 * If "what" contains STR2NR_BIN recognize binary numbers
1804 * If "what" contains STR2NR_OCT recognize octal numbers
1805 * If "what" contains STR2NR_HEX recognize hex numbers
1806 * If "what" contains STR2NR_FORCE always assume bin/oct/hex.
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001807 * If "what" contains STR2NR_QUOTE ignore embedded single quotes
Bram Moolenaarce157752017-10-28 16:07:33 +02001808 * If maxlen > 0, check at a maximum maxlen chars.
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001809 * If strict is TRUE, check the number strictly. return *len = 0 if fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 */
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812vim_str2nr(
1813 char_u *start,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001814 int *prep, // return: type of number 0 = decimal, 'x'
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001815 // or 'X' is hex, '0', 'o' or 'O' is octal,
1816 // 'b' or 'B' is bin
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001817 int *len, // return: detected length of number
1818 int what, // what numbers to recognize
1819 varnumber_T *nptr, // return: signed result
1820 uvarnumber_T *unptr, // return: unsigned result
1821 int maxlen, // max length of string to check
1822 int strict) // check strictly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 char_u *ptr = start;
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001825 int pre = 0; // default is decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 int negative = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001827 uvarnumber_T un = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001828 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001830 if (len != NULL)
1831 *len = 0;
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 if (ptr[0] == '-')
1834 {
1835 negative = TRUE;
1836 ++ptr;
1837 }
1838
Bram Moolenaarc667da52019-11-30 20:52:27 +01001839 // Recognize hex, octal, and bin.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001840 if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9'
1841 && (maxlen == 0 || maxlen > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001843 pre = ptr[1];
1844 if ((what & STR2NR_HEX)
1845 && (pre == 'X' || pre == 'x') && vim_isxdigit(ptr[2])
1846 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001847 // hexadecimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001848 ptr += 2;
1849 else if ((what & STR2NR_BIN)
1850 && (pre == 'B' || pre == 'b') && vim_isbdigit(ptr[2])
1851 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001852 // binary
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001853 ptr += 2;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001854 else if ((what & STR2NR_OOCT)
Bram Moolenaarc37b6552021-01-07 19:36:30 +01001855 && (pre == 'O' || pre == 'o') && vim_isodigit(ptr[2])
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001856 && (maxlen == 0 || maxlen > 2))
1857 // octal with prefix "0o"
1858 ptr += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 else
1860 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001861 // decimal or octal, default is decimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001862 pre = 0;
1863 if (what & STR2NR_OCT)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001864 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001865 // Don't interpret "0", "08" or "0129" as octal.
Bram Moolenaarce157752017-10-28 16:07:33 +02001866 for (n = 1; n != maxlen && VIM_ISDIGIT(ptr[n]); ++n)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001867 {
1868 if (ptr[n] > '7')
1869 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001870 pre = 0; // can't be octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001871 break;
1872 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001873 pre = '0'; // assume octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001874 }
1875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 }
1878
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001879 // Do the conversion manually to avoid sscanf() quirks.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001880 n = 1;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001881 if (pre == 'B' || pre == 'b'
1882 || ((what & STR2NR_BIN) && (what & STR2NR_FORCE)))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001883 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001884 // bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001885 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001886 n += 2; // skip over "0b"
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001887 while ('0' <= *ptr && *ptr <= '1')
1888 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001889 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001890 if (un <= UVARNUM_MAX / 2)
1891 un = 2 * un + (uvarnumber_T)(*ptr - '0');
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001892 else
1893 un = UVARNUM_MAX;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001894 ++ptr;
1895 if (n++ == maxlen)
1896 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001897 if ((what & STR2NR_QUOTE) && *ptr == '\''
1898 && '0' <= ptr[1] && ptr[1] <= '1')
1899 {
1900 ++ptr;
1901 if (n++ == maxlen)
1902 break;
1903 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001904 }
1905 }
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001906 else if (pre == 'O' || pre == 'o' ||
1907 pre == '0' || ((what & STR2NR_OCT) && (what & STR2NR_FORCE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001909 // octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001910 if (pre != 0 && pre != '0')
1911 n += 2; // skip over "0o"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001912 while ('0' <= *ptr && *ptr <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001914 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001915 if (un <= UVARNUM_MAX / 8)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001916 un = 8 * un + (uvarnumber_T)(*ptr - '0');
1917 else
1918 un = UVARNUM_MAX;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001919 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001920 if (n++ == maxlen)
1921 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001922 if ((what & STR2NR_QUOTE) && *ptr == '\''
1923 && '0' <= ptr[1] && ptr[1] <= '7')
1924 {
1925 ++ptr;
1926 if (n++ == maxlen)
1927 break;
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001930 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001931 else if (pre != 0 || ((what & STR2NR_HEX) && (what & STR2NR_FORCE)))
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001932 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001933 // hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001934 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001935 n += 2; // skip over "0x"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001936 while (vim_isxdigit(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001938 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001939 if (un <= UVARNUM_MAX / 16)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001940 un = 16 * un + (uvarnumber_T)hex2nr(*ptr);
1941 else
1942 un = UVARNUM_MAX;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001943 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001944 if (n++ == maxlen)
1945 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001946 if ((what & STR2NR_QUOTE) && *ptr == '\'' && vim_isxdigit(ptr[1]))
1947 {
1948 ++ptr;
1949 if (n++ == maxlen)
1950 break;
1951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953 }
1954 else
1955 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001956 // decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 while (VIM_ISDIGIT(*ptr))
1958 {
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001959 uvarnumber_T digit = (uvarnumber_T)(*ptr - '0');
1960
Bram Moolenaarc667da52019-11-30 20:52:27 +01001961 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001962 if (un < UVARNUM_MAX / 10
1963 || (un == UVARNUM_MAX / 10 && digit <= UVARNUM_MAX % 10))
1964 un = 10 * un + digit;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001965 else
1966 un = UVARNUM_MAX;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001968 if (n++ == maxlen)
1969 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001970 if ((what & STR2NR_QUOTE) && *ptr == '\'' && VIM_ISDIGIT(ptr[1]))
1971 {
1972 ++ptr;
1973 if (n++ == maxlen)
1974 break;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001978
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001979 // Check for an alphanumeric character immediately following, that is
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001980 // most likely a typo.
1981 if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr))
1982 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001984 if (prep != NULL)
1985 *prep = pre;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (len != NULL)
1987 *len = (int)(ptr - start);
1988 if (nptr != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001989 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001990 if (negative) // account for leading '-' for decimal numbers
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001991 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001992 // avoid ubsan error for overflow
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001993 if (un > VARNUM_MAX)
1994 *nptr = VARNUM_MIN;
1995 else
1996 *nptr = -(varnumber_T)un;
1997 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001998 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001999 {
2000 if (un > VARNUM_MAX)
2001 un = VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002002 *nptr = (varnumber_T)un;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002003 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (unptr != NULL)
2006 *unptr = un;
2007}
2008
2009/*
2010 * Return the value of a single hex character.
2011 * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'.
2012 */
2013 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002014hex2nr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 if (c >= 'a' && c <= 'f')
2017 return c - 'a' + 10;
2018 if (c >= 'A' && c <= 'F')
2019 return c - 'A' + 10;
2020 return c - '0';
2021}
2022
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023/*
2024 * Convert two hex characters to a byte.
2025 * Return -1 if one of the characters is not hex.
2026 */
2027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002028hexhex2nr(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1]))
2031 return -1;
2032 return (hex2nr(p[0]) << 4) + hex2nr(p[1]);
2033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035/*
2036 * Return TRUE if "str" starts with a backslash that should be removed.
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01002037 * For MS-DOS, MSWIN and OS/2 this is only done when the character after the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 * backslash is not a normal file name character.
2039 * '$' is a valid file name character, we don't remove the backslash before
2040 * it. This means it is not possible to use an environment variable after a
2041 * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works.
2042 * Although "\ name" is valid, the backslash in "Program\ files" must be
2043 * removed. Assume a file name doesn't start with a space.
2044 * For multi-byte names, never remove a backslash before a non-ascii
2045 * character, assume that all multi-byte characters are valid file name
2046 * characters.
2047 */
2048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002049rem_backslash(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051#ifdef BACKSLASH_IN_FILENAME
2052 return (str[0] == '\\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 && str[1] < 0x80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 && (str[1] == ' '
2055 || (str[1] != NUL
2056 && str[1] != '*'
2057 && str[1] != '?'
2058 && !vim_isfilec(str[1]))));
2059#else
2060 return (str[0] == '\\' && str[1] != NUL);
2061#endif
2062}
2063
2064/*
2065 * Halve the number of backslashes in a file name argument.
2066 * For MS-DOS we only do this if the character after the backslash
2067 * is not a normal file character.
2068 */
2069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002070backslash_halve(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071{
2072 for ( ; *p; ++p)
2073 if (rem_backslash(p))
Bram Moolenaar446cb832008-06-24 21:56:24 +00002074 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075}
2076
2077/*
2078 * backslash_halve() plus save the result in allocated memory.
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002079 * However, returns "p" when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 */
2081 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002082backslash_halve_save(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
2084 char_u *res;
2085
2086 res = vim_strsave(p);
2087 if (res == NULL)
2088 return p;
2089 backslash_halve(res);
2090 return res;
2091}