blob: 34751e1862e4c208b20cb6537ee4f0c51ac3fbba [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
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 Moolenaardc536092010-07-18 15:45:49 +0200740 * Return the number of characters the string 's' will take on the screen,
741 * taking into account the size of a tab.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
743 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100744linetabsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
Bram Moolenaardc536092010-07-18 15:45:49 +0200746 return linetabsize_col(0, s);
747}
748
749/*
750 * Like linetabsize(), but starting at column "startcol".
751 */
752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100753linetabsize_col(int startcol, char_u *s)
Bram Moolenaardc536092010-07-18 15:45:49 +0200754{
755 colnr_T col = startcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100756 char_u *line = s; // pointer to start of line, for breakindent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 while (*s != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200759 col += lbr_chartabsize_adv(line, &s, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 return (int)col;
761}
762
763/*
764 * Like linetabsize(), but for a given window instead of the current one.
765 */
766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100767win_linetabsize(win_T *wp, char_u *line, colnr_T len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
769 colnr_T col = 0;
770 char_u *s;
771
Bram Moolenaar597a4222014-06-25 14:39:50 +0200772 for (s = line; *s != NUL && (len == MAXCOL || s < line + len);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100773 MB_PTR_ADV(s))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200774 col += win_lbr_chartabsize(wp, line, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 return (int)col;
776}
777
778/*
Bram Moolenaar81695252004-12-29 20:58:21 +0000779 * Return TRUE if 'c' is a normal identifier character:
780 * Letters and characters from the 'isident' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 */
782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100783vim_isIDc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100785 return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +0200789 * Like vim_isIDc() but not using the 'isident' option: letters, numbers and
790 * underscore.
791 */
792 int
793vim_isNormalIDc(int c)
794{
795 return ASCII_ISALNUM(c) || c == '_';
796}
797
798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 * return TRUE if 'c' is a keyword character: Letters and characters from
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100800 * 'iskeyword' option for the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 * For multi-byte characters mb_get_class() is used (builtin rules).
802 */
803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100804vim_iswordc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100806 return vim_iswordc_buf(c, curbuf);
807}
808
809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100810vim_iswordc_buf(int c, buf_T *buf)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100811{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if (c >= 0x100)
813 {
814 if (enc_dbcs != 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000815 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (enc_utf8)
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100817 return utf_class_buf(c, buf) >= 2;
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100818 return FALSE;
819 }
820 return (c > 0 && GET_CHARTAB(buf, c) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
823/*
824 * Just like vim_iswordc() but uses a pointer to the (multi-byte) character.
825 */
826 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100827vim_iswordp(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100829 return vim_iswordp_buf(p, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100833vim_iswordp_buf(char_u *p, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100835 int c = *p;
836
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100837 if (has_mbyte && MB_BYTE2LEN(c) > 1)
838 c = (*mb_ptr2char)(p);
Bram Moolenaar4019cf92017-01-28 16:39:34 +0100839 return vim_iswordc_buf(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842/*
843 * return TRUE if 'c' is a valid file-name character
844 * Assume characters above 0x100 are valid (multi-byte).
845 */
846 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847vim_isfilec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100849 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
852/*
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000853 * return TRUE if 'c' is a valid file-name character or a wildcard character
854 * Assume characters above 0x100 are valid (multi-byte).
855 * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]")
856 * returns false.
857 */
858 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100859vim_isfilec_or_wc(int c)
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000860{
861 char_u buf[2];
862
863 buf[0] = (char_u)c;
864 buf[1] = NUL;
865 return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf);
866}
867
868/*
Bram Moolenaar3317d5e2017-04-08 19:12:06 +0200869 * Return TRUE if 'c' is a printable character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 * Assume characters above 0x100 are printable (multi-byte), except for
871 * Unicode.
872 */
873 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100874vim_isprintc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (enc_utf8 && c >= 0x100)
877 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100878 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879}
880
881/*
882 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head
883 * byte of a double-byte character.
884 */
885 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886vim_isprintc_strict(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1)
889 return FALSE;
890 if (enc_utf8 && c >= 0x100)
891 return utf_printable(c);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100892 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893}
894
895/*
896 * like chartabsize(), but also check for line breaks on the screen
897 */
898 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100899lbr_chartabsize(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100900 char_u *line UNUSED, // start of the line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100901 unsigned char *s,
902 colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
904#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +0100905 if (!curwin->w_p_lbr && *get_showbreak_value(curwin) == NUL
906 && !curwin->w_p_bri)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (curwin->w_p_wrap)
910 return win_nolbr_chartabsize(curwin, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col)
912#ifdef FEAT_LINEBREAK
913 }
Bram Moolenaar597a4222014-06-25 14:39:50 +0200914 return win_lbr_chartabsize(curwin, line == NULL ? s : line, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915#endif
916}
917
918/*
919 * Call lbr_chartabsize() and advance the pointer.
920 */
921 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100922lbr_chartabsize_adv(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100923 char_u *line, // start of the line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100924 char_u **s,
925 colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 int retval;
928
Bram Moolenaar597a4222014-06-25 14:39:50 +0200929 retval = lbr_chartabsize(line, *s, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100930 MB_PTR_ADV(*s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 return retval;
932}
933
934/*
935 * This function is used very often, keep it fast!!!!
936 *
937 * If "headp" not NULL, set *headp to the size of what we for 'showbreak'
938 * string at start of line. Warning: *headp is only set if it's a non-zero
939 * value, init to 0 before calling.
940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100942win_lbr_chartabsize(
943 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100944 char_u *line UNUSED, // start of the line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945 char_u *s,
946 colnr_T col,
947 int *headp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949#ifdef FEAT_LINEBREAK
950 int c;
951 int size;
952 colnr_T col2;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100953 colnr_T col_adj = 0; // col + screen size of tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 colnr_T colmax;
955 int added;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 int mb_added = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 int numberextra;
958 char_u *ps;
959 int tab_corr = (*s == TAB);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000960 int n;
Bram Moolenaaree857022019-11-09 23:26:40 +0100961 char_u *sbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
963 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +0200964 * No 'linebreak', 'showbreak' and 'breakindent': return quickly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 */
Bram Moolenaaree857022019-11-09 23:26:40 +0100966 if (!wp->w_p_lbr && !wp->w_p_bri && *get_showbreak_value(wp) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#endif
968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (wp->w_p_wrap)
970 return win_nolbr_chartabsize(wp, s, col, headp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col)
972 }
973
974#ifdef FEAT_LINEBREAK
975 /*
976 * First get normal size, without 'linebreak'
977 */
978 size = win_chartabsize(wp, s, col);
979 c = *s;
Bram Moolenaaree739b42014-07-02 19:37:42 +0200980 if (tab_corr)
981 col_adj = size - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 /*
984 * If 'linebreak' set check at a blank before a non-blank if the line
985 * needs a break here
986 */
987 if (wp->w_p_lbr
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100988 && VIM_ISBREAK(c)
Bram Moolenaar977d0372017-03-12 21:31:58 +0100989 && !VIM_ISBREAK((int)s[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 && wp->w_p_wrap
Bram Moolenaar4033c552017-09-16 20:54:51 +0200991 && wp->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 /*
994 * Count all characters from first non-blank after a blank up to next
995 * non-blank after a blank.
996 */
997 numberextra = win_col_off(wp);
998 col2 = col;
Bram Moolenaar02631462017-09-22 15:20:32 +0200999 colmax = (colnr_T)(wp->w_width - numberextra - col_adj);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (col >= colmax)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 {
Bram Moolenaaree739b42014-07-02 19:37:42 +02001002 colmax += col_adj;
1003 n = colmax + win_col_off2(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001004 if (n > 0)
Bram Moolenaaree739b42014-07-02 19:37:42 +02001005 colmax += (((col - colmax) / n) + 1) * n - col_adj;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001006 }
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 for (;;)
1009 {
1010 ps = s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001011 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 c = *s;
1013 if (!(c != NUL
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001014 && (VIM_ISBREAK(c)
1015 || (!VIM_ISBREAK(c)
Bram Moolenaar977d0372017-03-12 21:31:58 +01001016 && (col2 == col || !VIM_ISBREAK((int)*ps))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 break;
1018
1019 col2 += win_chartabsize(wp, s, col2);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001020 if (col2 >= colmax) // doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
Bram Moolenaaree739b42014-07-02 19:37:42 +02001022 size = colmax - col + col_adj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 break;
1024 }
1025 }
1026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1
1028 && wp->w_p_wrap && in_win_border(wp, col))
1029 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001030 ++size; // Count the ">" in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 mb_added = 1;
1032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +02001035 * May have to add something for 'breakindent' and/or 'showbreak'
1036 * string at start of line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 * Set *headp to the size of what we add.
Bram Moolenaar21efafe2022-03-03 20:04:03 +00001038 * Do not use 'showbreak' at the NUL after the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
1040 added = 0;
Bram Moolenaar21efafe2022-03-03 20:04:03 +00001041 sbr = c == NUL ? empty_option : get_showbreak_value(wp);
Bram Moolenaaree857022019-11-09 23:26:40 +01001042 if ((*sbr != NUL || wp->w_p_bri) && wp->w_p_wrap && col != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaard574ea22015-01-14 19:35:14 +01001044 colnr_T sbrlen = 0;
1045 int numberwidth = win_col_off(wp);
1046
1047 numberextra = numberwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 col += numberextra + mb_added;
Bram Moolenaar02631462017-09-22 15:20:32 +02001049 if (col >= (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001051 col -= wp->w_width;
1052 numberextra = wp->w_width - (numberextra - win_col_off2(wp));
Bram Moolenaard574ea22015-01-14 19:35:14 +01001053 if (col >= numberextra && numberextra > 0)
Bram Moolenaarfe3c4102014-10-31 12:42:01 +01001054 col %= numberextra;
Bram Moolenaaree857022019-11-09 23:26:40 +01001055 if (*sbr != NUL)
Bram Moolenaar1c852102014-10-15 21:26:40 +02001056 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001057 sbrlen = (colnr_T)MB_CHARLEN(sbr);
Bram Moolenaar1c852102014-10-15 21:26:40 +02001058 if (col >= sbrlen)
1059 col -= sbrlen;
1060 }
Bram Moolenaard574ea22015-01-14 19:35:14 +01001061 if (col >= numberextra && numberextra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 col = col % numberextra;
Bram Moolenaard574ea22015-01-14 19:35:14 +01001063 else if (col > 0 && numberextra > 0)
1064 col += numberwidth - win_col_off2(wp);
1065
1066 numberwidth -= win_col_off2(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001068 if (col == 0 || col + size + sbrlen > (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02001070 added = 0;
Bram Moolenaaree857022019-11-09 23:26:40 +01001071 if (*sbr != NUL)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001072 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001073 if (size + sbrlen + numberwidth > (colnr_T)wp->w_width)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001074 {
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001075 // calculate effective window width
Bram Moolenaar02631462017-09-22 15:20:32 +02001076 int width = (colnr_T)wp->w_width - sbrlen - numberwidth;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001077 int prev_width = col
1078 ? ((colnr_T)wp->w_width - (sbrlen + col)) : 0;
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001079
1080 if (width <= 0)
1081 width = (colnr_T)1;
Bram Moolenaaree857022019-11-09 23:26:40 +01001082 added += ((size - prev_width) / width) * vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001083 if ((size - prev_width) % width)
Bram Moolenaar7833dab2019-05-27 22:01:40 +02001084 // wrapped, add another length of 'sbr'
Bram Moolenaaree857022019-11-09 23:26:40 +01001085 added += vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001086 }
1087 else
Bram Moolenaaree857022019-11-09 23:26:40 +01001088 added += vim_strsize(sbr);
Bram Moolenaard574ea22015-01-14 19:35:14 +01001089 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02001090 if (wp->w_p_bri)
1091 added += get_breakindent_win(wp, line);
1092
Bram Moolenaar95765082014-08-24 21:19:25 +02001093 size += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (col != 0)
1095 added = 0;
1096 }
1097 }
1098 if (headp != NULL)
1099 *headp = added + mb_added;
1100 return size;
1101#endif
1102}
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and
1106 * 'wrap' is on. This means we need to check for a double-byte character that
1107 * doesn't fit at the end of the screen line.
1108 */
1109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001110win_nolbr_chartabsize(
1111 win_T *wp,
1112 char_u *s,
1113 colnr_T col,
1114 int *headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 int n;
1117
Bram Moolenaareed9d462021-02-15 20:38:25 +01001118 if (*s == TAB && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001120# ifdef FEAT_VARTABS
1121 return tabstop_padding(col, wp->w_buffer->b_p_ts,
1122 wp->w_buffer->b_p_vts_array);
1123# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 n = wp->w_buffer->b_p_ts;
1125 return (int)(n - (col % n));
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 n = ptr2cells(s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001129 // Add one cell for a double-width character in the last column of the
1130 // window, displayed with a ">".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col))
1132 {
1133 if (headp != NULL)
1134 *headp = 1;
1135 return 3;
1136 }
1137 return n;
1138}
1139
1140/*
1141 * Return TRUE if virtual column "vcol" is in the rightmost column of window
1142 * "wp".
1143 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001144 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001145in_win_border(win_T *wp, colnr_T vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001147 int width1; // width of first line (after line number)
1148 int width2; // width of further lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
Bram Moolenaarc667da52019-11-30 20:52:27 +01001150 if (wp->w_width == 0) // there is no border
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 return FALSE;
Bram Moolenaar02631462017-09-22 15:20:32 +02001152 width1 = wp->w_width - win_col_off(wp);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001153 if ((int)vcol < width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001155 if ((int)vcol == width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 return TRUE;
1157 width2 = width1 + win_col_off2(wp);
Bram Moolenaar8701cd62009-10-07 14:20:30 +00001158 if (width2 <= 0)
1159 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 return ((vcol - width1) % width2 == width2 - 1);
1161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
1163/*
1164 * Get virtual column number of pos.
1165 * start: on the first position of this character (TAB, ctrl)
1166 * cursor: where the cursor is on this character (first char, except for TAB)
1167 * end: on the last position of this character (TAB, ctrl)
1168 *
1169 * This is used very often, keep it fast!
1170 */
1171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001172getvcol(
1173 win_T *wp,
1174 pos_T *pos,
1175 colnr_T *start,
1176 colnr_T *cursor,
1177 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178{
1179 colnr_T vcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001180 char_u *ptr; // points to current char
1181 char_u *posptr; // points to char at pos->col
1182 char_u *line; // start of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 int incr;
1184 int head;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001185#ifdef FEAT_VARTABS
1186 int *vts = wp->w_buffer->b_p_vts_array;
1187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 int ts = wp->w_buffer->b_p_ts;
1189 int c;
1190
1191 vcol = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001192 line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001193 if (pos->col == MAXCOL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001194 posptr = NULL; // continue until the NUL
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001195 else
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001196 {
Bram Moolenaar94f31922021-12-30 15:29:18 +00001197 colnr_T i;
1198
1199 // In a few cases the position can be beyond the end of the line.
1200 for (i = 0; i < pos->col; ++i)
1201 if (ptr[i] == NUL)
1202 {
1203 pos->col = i;
1204 break;
1205 }
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001206 posptr = ptr + pos->col;
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001207 if (has_mbyte)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001208 // always start on the first byte
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001209 posptr -= (*mb_head_off)(line, posptr);
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
1212 /*
1213 * This function is used very often, do some speed optimizations.
Bram Moolenaar597a4222014-06-25 14:39:50 +02001214 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set
1215 * use a simple loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 * Also use this when 'list' is set but tabs take their normal size.
1217 */
Bram Moolenaareed9d462021-02-15 20:38:25 +01001218 if ((!wp->w_p_list || wp->w_lcs_chars.tab1 != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001220 && !wp->w_p_lbr && *get_showbreak_value(wp) == NUL && !wp->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#endif
1222 )
1223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 for (;;)
1225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 head = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 c = *ptr;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001228 // make sure we don't go past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (c == NUL)
1230 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001231 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 break;
1233 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001234 // A tab gets expanded, depending on the current column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (c == TAB)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001236#ifdef FEAT_VARTABS
1237 incr = tabstop_padding(vcol, ts, vts);
1238#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 incr = ts - (vcol % ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 else
1242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (has_mbyte)
1244 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001245 // For utf-8, if the byte is >= 0x80, need to look at
1246 // further bytes to find the cell width.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (enc_utf8 && c >= 0x80)
1248 incr = utf_ptr2cells(ptr);
1249 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001250 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaarc667da52019-11-30 20:52:27 +01001252 // If a double-cell char doesn't fit at the end of a line
1253 // it wraps to the next line, it's like this char is three
1254 // cells wide.
Bram Moolenaar9c33a7c2008-02-20 13:59:32 +00001255 if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1
1256 && in_win_border(wp, vcol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 ++incr;
1259 head = 1;
1260 }
1261 }
1262 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001263 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265
Bram Moolenaarc667da52019-11-30 20:52:27 +01001266 if (posptr != NULL && ptr >= posptr) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 break;
1268
1269 vcol += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001270 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 }
1273 else
1274 {
1275 for (;;)
1276 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001277 // A tab gets expanded, depending on the current column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 head = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001279 incr = win_lbr_chartabsize(wp, line, ptr, vcol, &head);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001280 // make sure we don't go past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (*ptr == NUL)
1282 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001283 incr = 1; // NUL at end of line only takes one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 break;
1285 }
1286
Bram Moolenaarc667da52019-11-30 20:52:27 +01001287 if (posptr != NULL && ptr >= posptr) // character at pos->col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 break;
1289
1290 vcol += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001291 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 }
1294 if (start != NULL)
1295 *start = vcol + head;
1296 if (end != NULL)
1297 *end = vcol + incr - 1;
1298 if (cursor != NULL)
1299 {
1300 if (*ptr == TAB
Bram Moolenaar24959102022-05-07 20:01:16 +01001301 && (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && !wp->w_p_list
1303 && !virtual_active()
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001304 && !(VIsual_active
1305 && (*p_sel == 'e' || LTOREQ_POS(*pos, VIsual)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 )
Bram Moolenaarc667da52019-11-30 20:52:27 +01001307 *cursor = vcol + incr - 1; // cursor at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01001309 *cursor = vcol + head; // cursor at start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311}
1312
1313/*
1314 * Get virtual cursor column in the current window, pretending 'list' is off.
1315 */
1316 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001317getvcol_nolist(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
1319 int list_save = curwin->w_p_list;
1320 colnr_T vcol;
1321
1322 curwin->w_p_list = FALSE;
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001323 if (posp->coladd)
1324 getvvcol(curwin, posp, NULL, &vcol, NULL);
1325 else
Bram Moolenaardb0eede2018-04-25 22:38:17 +02001326 getvcol(curwin, posp, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 curwin->w_p_list = list_save;
1328 return vcol;
1329}
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331/*
1332 * Get virtual column in virtual mode.
1333 */
1334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001335getvvcol(
1336 win_T *wp,
1337 pos_T *pos,
1338 colnr_T *start,
1339 colnr_T *cursor,
1340 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
1342 colnr_T col;
1343 colnr_T coladd;
1344 colnr_T endadd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347 if (virtual_active())
1348 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001349 // For virtual mode, only want one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 getvcol(wp, pos, &col, NULL, NULL);
1351
1352 coladd = pos->coladd;
1353 endadd = 0;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001354 // Cannot put the cursor on part of a wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001356 if (pos->col < (colnr_T)STRLEN(ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 int c = (*mb_ptr2char)(ptr + pos->col);
1359
1360 if (c != TAB && vim_isprintc(c))
1361 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001362 endadd = (colnr_T)(char2cells(c) - 1);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001363 if (coladd > endadd) // past end of line
Bram Moolenaara5792f52005-11-23 21:25:05 +00001364 endadd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
1366 coladd = 0;
1367 }
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 col += coladd;
1370 if (start != NULL)
1371 *start = col;
1372 if (cursor != NULL)
1373 *cursor = col;
1374 if (end != NULL)
1375 *end = col + endadd;
1376 }
1377 else
1378 getvcol(wp, pos, start, cursor, end);
1379}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381/*
1382 * Get the leftmost and rightmost virtual column of pos1 and pos2.
1383 * Used for Visual block mode.
1384 */
1385 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001386getvcols(
1387 win_T *wp,
1388 pos_T *pos1,
1389 pos_T *pos2,
1390 colnr_T *left,
1391 colnr_T *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 colnr_T from1, from2, to1, to2;
1394
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001395 if (LT_POSP(pos1, pos2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 {
1397 getvvcol(wp, pos1, &from1, NULL, &to1);
1398 getvvcol(wp, pos2, &from2, NULL, &to2);
1399 }
1400 else
1401 {
1402 getvvcol(wp, pos2, &from1, NULL, &to1);
1403 getvvcol(wp, pos1, &from2, NULL, &to2);
1404 }
1405 if (from2 < from1)
1406 *left = from2;
1407 else
1408 *left = from1;
1409 if (to2 > to1)
1410 {
1411 if (*p_sel == 'e' && from2 - 1 >= to1)
1412 *right = from2 - 1;
1413 else
1414 *right = to2;
1415 }
1416 else
1417 *right = to1;
1418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420/*
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001421 * Skip over ' ' and '\t'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 */
1423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001424skipwhite(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001426 char_u *p = q;
1427
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001428 while (VIM_ISWHITE(*p))
1429 ++p;
1430 return p;
1431}
1432
Dominique Pelle748b3082022-01-08 12:41:16 +00001433#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarce7eada2021-12-15 15:41:44 +00001434/*
1435 * skip over ' ', '\t' and '\n'.
1436 */
1437 char_u *
1438skipwhite_and_nl(char_u *q)
1439{
1440 char_u *p = q;
1441
1442 while (VIM_ISWHITE(*p) || *p == NL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 ++p;
1444 return p;
1445}
Dominique Pelle748b3082022-01-08 12:41:16 +00001446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448/*
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001449 * getwhitecols: return the number of whitespace
1450 * columns (bytes) at the start of a given line
1451 */
1452 int
1453getwhitecols_curline()
1454{
1455 return getwhitecols(ml_get_curline());
1456}
1457
1458 int
1459getwhitecols(char_u *p)
1460{
1461 return skipwhite(p) - p;
1462}
1463
1464/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001465 * skip over digits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
1467 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001468skipdigits(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001470 char_u *p = q;
1471
Bram Moolenaarc667da52019-11-30 20:52:27 +01001472 while (VIM_ISDIGIT(*p)) // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ++p;
1474 return p;
1475}
1476
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001477#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001478/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001479 * skip over binary digits
1480 */
1481 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482skipbin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001483{
1484 char_u *p = q;
1485
Bram Moolenaarc667da52019-11-30 20:52:27 +01001486 while (vim_isbdigit(*p)) // skip to next non-digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001487 ++p;
1488 return p;
1489}
1490
1491/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001492 * skip over digits and hex characters
1493 */
1494 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495skiphex(char_u *q)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001496{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001497 char_u *p = q;
1498
Bram Moolenaarc667da52019-11-30 20:52:27 +01001499 while (vim_isxdigit(*p)) // skip to next non-digit
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001500 ++p;
1501 return p;
1502}
1503#endif
1504
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001505/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001506 * skip to bin digit (or NUL after the string)
1507 */
1508 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509skiptobin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001510{
1511 char_u *p = q;
1512
Bram Moolenaarc667da52019-11-30 20:52:27 +01001513 while (*p != NUL && !vim_isbdigit(*p)) // skip to next digit
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001514 ++p;
1515 return p;
1516}
1517
1518/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001519 * skip to digit (or NUL after the string)
1520 */
1521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522skiptodigit(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001523{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001524 char_u *p = q;
1525
Bram Moolenaarc667da52019-11-30 20:52:27 +01001526 while (*p != NUL && !VIM_ISDIGIT(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001527 ++p;
1528 return p;
1529}
1530
1531/*
1532 * skip to hex character (or NUL after the string)
1533 */
1534 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535skiptohex(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001536{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001537 char_u *p = q;
1538
Bram Moolenaarc667da52019-11-30 20:52:27 +01001539 while (*p != NUL && !vim_isxdigit(*p)) // skip to next digit
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001540 ++p;
1541 return p;
1542}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544/*
1545 * Variant of isdigit() that can handle characters > 0x100.
1546 * We don't use isdigit() here, because on some systems it also considers
1547 * superscript 1 to be a digit.
1548 * Use the VIM_ISDIGIT() macro for simple arguments.
1549 */
1550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001551vim_isdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 return (c >= '0' && c <= '9');
1554}
1555
1556/*
1557 * Variant of isxdigit() that can handle characters > 0x100.
1558 * We don't use isxdigit() here, because on some systems it also considers
1559 * superscript 1 to be a digit.
1560 */
1561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001562vim_isxdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 return (c >= '0' && c <= '9')
1565 || (c >= 'a' && c <= 'f')
1566 || (c >= 'A' && c <= 'F');
1567}
1568
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001569/*
1570 * Corollary of vim_isdigit and vim_isxdigit() that can handle
1571 * characters > 0x100.
1572 */
1573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574vim_isbdigit(int c)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001575{
1576 return (c == '0' || c == '1');
1577}
1578
Bram Moolenaarc37b6552021-01-07 19:36:30 +01001579 static int
1580vim_isodigit(int c)
1581{
1582 return (c >= '0' && c <= '7');
1583}
1584
Bram Moolenaar78622822005-08-23 21:00:13 +00001585/*
1586 * Vim's own character class functions. These exist because many library
1587 * islower()/toupper() etc. do not work properly: they crash when used with
1588 * invalid values or can't handle latin1 when the locale is C.
1589 * Speed is most important here.
1590 */
1591#define LATIN1LOWER 'l'
1592#define LATIN1UPPER 'U'
1593
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00001594static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll";
Bram Moolenaar936347b2012-05-25 11:56:22 +02001595static 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";
1596static 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 +00001597
1598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001599vim_islower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001600{
1601 if (c <= '@')
1602 return FALSE;
1603 if (c >= 0x80)
1604 {
1605 if (enc_utf8)
1606 return utf_islower(c);
1607 if (c >= 0x100)
1608 {
1609#ifdef HAVE_ISWLOWER
1610 if (has_mbyte)
1611 return iswlower(c);
1612#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001613 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001614 return FALSE;
1615 }
1616 if (enc_latin1like)
1617 return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER;
1618 }
1619 return islower(c);
1620}
1621
1622 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623vim_isupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001624{
1625 if (c <= '@')
1626 return FALSE;
1627 if (c >= 0x80)
1628 {
1629 if (enc_utf8)
1630 return utf_isupper(c);
1631 if (c >= 0x100)
1632 {
1633#ifdef HAVE_ISWUPPER
1634 if (has_mbyte)
1635 return iswupper(c);
1636#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001637 // islower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001638 return FALSE;
1639 }
1640 if (enc_latin1like)
1641 return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER;
1642 }
1643 return isupper(c);
1644}
1645
1646 int
Bram Moolenaar5921aeb2022-02-19 11:20:12 +00001647vim_isalpha(int c)
1648{
1649 return vim_islower(c) || vim_isupper(c);
1650}
1651
1652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001653vim_toupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001654{
1655 if (c <= '@')
1656 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001657 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00001658 {
1659 if (enc_utf8)
1660 return utf_toupper(c);
1661 if (c >= 0x100)
1662 {
1663#ifdef HAVE_TOWUPPER
1664 if (has_mbyte)
1665 return towupper(c);
1666#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001667 // toupper() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001668 return c;
1669 }
1670 if (enc_latin1like)
1671 return latin1upper[c];
1672 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02001673 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
1674 return TOUPPER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00001675 return TOUPPER_LOC(c);
1676}
1677
1678 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001679vim_tolower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001680{
1681 if (c <= '@')
1682 return c;
Bram Moolenaar3317d5e2017-04-08 19:12:06 +02001683 if (c >= 0x80 || !(cmp_flags & CMP_KEEPASCII))
Bram Moolenaar78622822005-08-23 21:00:13 +00001684 {
1685 if (enc_utf8)
1686 return utf_tolower(c);
1687 if (c >= 0x100)
1688 {
1689#ifdef HAVE_TOWLOWER
1690 if (has_mbyte)
1691 return towlower(c);
1692#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001693 // tolower() can't handle these chars and may crash
Bram Moolenaar78622822005-08-23 21:00:13 +00001694 return c;
1695 }
1696 if (enc_latin1like)
1697 return latin1lower[c];
1698 }
Bram Moolenaar1cc48202017-04-09 13:41:59 +02001699 if (c < 0x80 && (cmp_flags & CMP_KEEPASCII))
1700 return TOLOWER_ASC(c);
Bram Moolenaar78622822005-08-23 21:00:13 +00001701 return TOLOWER_LOC(c);
1702}
Bram Moolenaar78622822005-08-23 21:00:13 +00001703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704/*
1705 * skiptowhite: skip over text until ' ' or '\t' or NUL.
1706 */
1707 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708skiptowhite(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 while (*p != ' ' && *p != '\t' && *p != NUL)
1711 ++p;
1712 return p;
1713}
1714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715/*
1716 * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars
1717 */
1718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001719skiptowhite_esc(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 while (*p != ' ' && *p != '\t' && *p != NUL)
1722 {
1723 if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL)
1724 ++p;
1725 ++p;
1726 }
1727 return p;
1728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00001731 * Get a number from a string and skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 * Note: the argument is a pointer to a char_u pointer!
1733 */
1734 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735getdigits(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 char_u *p;
1738 long retval;
1739
1740 p = *pp;
1741 retval = atol((char *)p);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001742 if (*p == '-') // skip negative sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 ++p;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001744 p = skipdigits(p); // skip to next non-digit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 *pp = p;
1746 return retval;
1747}
1748
1749/*
Bram Moolenaaraf377e32021-11-29 12:12:43 +00001750 * Like getdigits() but allow for embedded single quotes.
1751 */
1752 long
1753getdigits_quoted(char_u **pp)
1754{
1755 char_u *p = *pp;
1756 long retval = 0;
1757
1758 if (*p == '-')
1759 ++p;
1760 while (VIM_ISDIGIT(*p))
1761 {
1762 if (retval >= LONG_MAX / 10 - 10)
1763 retval = LONG_MAX;
1764 else
1765 retval = retval * 10 - '0' + *p;
1766 ++p;
1767 if (in_vim9script() && *p == '\'' && VIM_ISDIGIT(p[1]))
1768 ++p;
1769 }
1770 if (**pp == '-')
1771 {
1772 if (retval == LONG_MAX)
1773 retval = LONG_MIN;
1774 else
1775 retval = -retval;
1776 }
1777 *pp = p;
1778 return retval;
1779}
1780
1781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 * Return TRUE if "lbuf" is empty or only contains blanks.
1783 */
1784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785vim_isblankline(char_u *lbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 char_u *p;
1788
1789 p = skipwhite(lbuf);
1790 return (*p == NUL || *p == '\r' || *p == '\n');
1791}
1792
1793/*
1794 * Convert a string into a long and/or unsigned long, taking care of
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001795 * hexadecimal, octal, and binary numbers. Accepts a '-' sign.
1796 * If "prep" is not NULL, returns a flag to indicate the type of the number:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 * 0 decimal
1798 * '0' octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001799 * 'O' octal
1800 * 'o' octal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001801 * 'B' bin
1802 * 'b' bin
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 * 'X' hex
1804 * 'x' hex
1805 * If "len" is not NULL, the length of the number in characters is returned.
1806 * If "nptr" is not NULL, the signed result is returned in it.
1807 * If "unptr" is not NULL, the unsigned result is returned in it.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001808 * If "what" contains STR2NR_BIN recognize binary numbers
1809 * If "what" contains STR2NR_OCT recognize octal numbers
1810 * If "what" contains STR2NR_HEX recognize hex numbers
1811 * If "what" contains STR2NR_FORCE always assume bin/oct/hex.
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001812 * If "what" contains STR2NR_QUOTE ignore embedded single quotes
Bram Moolenaarce157752017-10-28 16:07:33 +02001813 * If maxlen > 0, check at a maximum maxlen chars.
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001814 * If strict is TRUE, check the number strictly. return *len = 0 if fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817vim_str2nr(
1818 char_u *start,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001819 int *prep, // return: type of number 0 = decimal, 'x'
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001820 // or 'X' is hex, '0', 'o' or 'O' is octal,
1821 // 'b' or 'B' is bin
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001822 int *len, // return: detected length of number
1823 int what, // what numbers to recognize
1824 varnumber_T *nptr, // return: signed result
1825 uvarnumber_T *unptr, // return: unsigned result
1826 int maxlen, // max length of string to check
1827 int strict) // check strictly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 char_u *ptr = start;
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001830 int pre = 0; // default is decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 int negative = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001832 uvarnumber_T un = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001833 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001835 if (len != NULL)
1836 *len = 0;
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (ptr[0] == '-')
1839 {
1840 negative = TRUE;
1841 ++ptr;
1842 }
1843
Bram Moolenaarc667da52019-11-30 20:52:27 +01001844 // Recognize hex, octal, and bin.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001845 if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9'
1846 && (maxlen == 0 || maxlen > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001848 pre = ptr[1];
1849 if ((what & STR2NR_HEX)
1850 && (pre == 'X' || pre == 'x') && vim_isxdigit(ptr[2])
1851 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001852 // hexadecimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001853 ptr += 2;
1854 else if ((what & STR2NR_BIN)
1855 && (pre == 'B' || pre == 'b') && vim_isbdigit(ptr[2])
1856 && (maxlen == 0 || maxlen > 2))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001857 // binary
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001858 ptr += 2;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001859 else if ((what & STR2NR_OOCT)
Bram Moolenaarc37b6552021-01-07 19:36:30 +01001860 && (pre == 'O' || pre == 'o') && vim_isodigit(ptr[2])
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001861 && (maxlen == 0 || maxlen > 2))
1862 // octal with prefix "0o"
1863 ptr += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 else
1865 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001866 // decimal or octal, default is decimal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001867 pre = 0;
1868 if (what & STR2NR_OCT)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001869 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001870 // Don't interpret "0", "08" or "0129" as octal.
Bram Moolenaarce157752017-10-28 16:07:33 +02001871 for (n = 1; n != maxlen && VIM_ISDIGIT(ptr[n]); ++n)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001872 {
1873 if (ptr[n] > '7')
1874 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001875 pre = 0; // can't be octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001876 break;
1877 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001878 pre = '0'; // assume octal
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001879 }
1880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882 }
1883
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001884 // Do the conversion manually to avoid sscanf() quirks.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001885 n = 1;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001886 if (pre == 'B' || pre == 'b'
1887 || ((what & STR2NR_BIN) && (what & STR2NR_FORCE)))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001888 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001889 // bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001890 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001891 n += 2; // skip over "0b"
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001892 while ('0' <= *ptr && *ptr <= '1')
1893 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001894 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001895 if (un <= UVARNUM_MAX / 2)
1896 un = 2 * un + (uvarnumber_T)(*ptr - '0');
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001897 else
1898 un = UVARNUM_MAX;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001899 ++ptr;
1900 if (n++ == maxlen)
1901 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001902 if ((what & STR2NR_QUOTE) && *ptr == '\''
1903 && '0' <= ptr[1] && ptr[1] <= '1')
1904 {
1905 ++ptr;
1906 if (n++ == maxlen)
1907 break;
1908 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001909 }
1910 }
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001911 else if (pre == 'O' || pre == 'o' ||
1912 pre == '0' || ((what & STR2NR_OCT) && (what & STR2NR_FORCE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001914 // octal
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02001915 if (pre != 0 && pre != '0')
1916 n += 2; // skip over "0o"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001917 while ('0' <= *ptr && *ptr <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001919 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001920 if (un <= UVARNUM_MAX / 8)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001921 un = 8 * un + (uvarnumber_T)(*ptr - '0');
1922 else
1923 un = UVARNUM_MAX;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001924 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001925 if (n++ == maxlen)
1926 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001927 if ((what & STR2NR_QUOTE) && *ptr == '\''
1928 && '0' <= ptr[1] && ptr[1] <= '7')
1929 {
1930 ++ptr;
1931 if (n++ == maxlen)
1932 break;
1933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001935 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001936 else if (pre != 0 || ((what & STR2NR_HEX) && (what & STR2NR_FORCE)))
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001937 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001938 // hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001939 if (pre != 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001940 n += 2; // skip over "0x"
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001941 while (vim_isxdigit(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001943 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001944 if (un <= UVARNUM_MAX / 16)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001945 un = 16 * un + (uvarnumber_T)hex2nr(*ptr);
1946 else
1947 un = UVARNUM_MAX;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001948 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001949 if (n++ == maxlen)
1950 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001951 if ((what & STR2NR_QUOTE) && *ptr == '\'' && vim_isxdigit(ptr[1]))
1952 {
1953 ++ptr;
1954 if (n++ == maxlen)
1955 break;
1956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 }
1959 else
1960 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001961 // decimal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 while (VIM_ISDIGIT(*ptr))
1963 {
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001964 uvarnumber_T digit = (uvarnumber_T)(*ptr - '0');
1965
Bram Moolenaarc667da52019-11-30 20:52:27 +01001966 // avoid ubsan error for overflow
Bram Moolenaar07ccf7c2018-06-12 17:25:36 +02001967 if (un < UVARNUM_MAX / 10
1968 || (un == UVARNUM_MAX / 10 && digit <= UVARNUM_MAX % 10))
1969 un = 10 * un + digit;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001970 else
1971 un = UVARNUM_MAX;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001973 if (n++ == maxlen)
1974 break;
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001975 if ((what & STR2NR_QUOTE) && *ptr == '\'' && VIM_ISDIGIT(ptr[1]))
1976 {
1977 ++ptr;
1978 if (n++ == maxlen)
1979 break;
1980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982 }
Bram Moolenaar1ac90b42019-09-15 14:49:52 +02001983
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001984 // Check for an alphanumeric character immediately following, that is
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001985 // most likely a typo.
1986 if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr))
1987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001989 if (prep != NULL)
1990 *prep = pre;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (len != NULL)
1992 *len = (int)(ptr - start);
1993 if (nptr != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001994 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001995 if (negative) // account for leading '-' for decimal numbers
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001996 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001997 // avoid ubsan error for overflow
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001998 if (un > VARNUM_MAX)
1999 *nptr = VARNUM_MIN;
2000 else
2001 *nptr = -(varnumber_T)un;
2002 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002003 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002004 {
2005 if (un > VARNUM_MAX)
2006 un = VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002007 *nptr = (varnumber_T)un;
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002008 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (unptr != NULL)
2011 *unptr = un;
2012}
2013
2014/*
2015 * Return the value of a single hex character.
2016 * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'.
2017 */
2018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002019hex2nr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020{
2021 if (c >= 'a' && c <= 'f')
2022 return c - 'a' + 10;
2023 if (c >= 'A' && c <= 'F')
2024 return c - 'A' + 10;
2025 return c - '0';
2026}
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028/*
2029 * Convert two hex characters to a byte.
2030 * Return -1 if one of the characters is not hex.
2031 */
2032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002033hexhex2nr(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1]))
2036 return -1;
2037 return (hex2nr(p[0]) << 4) + hex2nr(p[1]);
2038}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040/*
2041 * Return TRUE if "str" starts with a backslash that should be removed.
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01002042 * For MS-DOS, MSWIN and OS/2 this is only done when the character after the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 * backslash is not a normal file name character.
2044 * '$' is a valid file name character, we don't remove the backslash before
2045 * it. This means it is not possible to use an environment variable after a
2046 * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works.
2047 * Although "\ name" is valid, the backslash in "Program\ files" must be
2048 * removed. Assume a file name doesn't start with a space.
2049 * For multi-byte names, never remove a backslash before a non-ascii
2050 * character, assume that all multi-byte characters are valid file name
2051 * characters.
2052 */
2053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002054rem_backslash(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056#ifdef BACKSLASH_IN_FILENAME
2057 return (str[0] == '\\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 && str[1] < 0x80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 && (str[1] == ' '
2060 || (str[1] != NUL
2061 && str[1] != '*'
2062 && str[1] != '?'
2063 && !vim_isfilec(str[1]))));
2064#else
2065 return (str[0] == '\\' && str[1] != NUL);
2066#endif
2067}
2068
2069/*
2070 * Halve the number of backslashes in a file name argument.
2071 * For MS-DOS we only do this if the character after the backslash
2072 * is not a normal file character.
2073 */
2074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002075backslash_halve(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
2077 for ( ; *p; ++p)
2078 if (rem_backslash(p))
Bram Moolenaar446cb832008-06-24 21:56:24 +00002079 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080}
2081
2082/*
2083 * backslash_halve() plus save the result in allocated memory.
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002084 * However, returns "p" when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 */
2086 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002087backslash_halve_save(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 char_u *res;
2090
2091 res = vim_strsave(p);
2092 if (res == NULL)
2093 return p;
2094 backslash_halve(res);
2095 return res;
2096}