blob: 538d82115a9d64d5575ed2ef3f2598cbbdc9ab13 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
12#ifdef FEAT_LINEBREAK
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010013static int win_chartabsize(win_T *wp, char_u *p, colnr_T col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#endif
15
16#ifdef FEAT_MBYTE
Bram Moolenaard7b734a2010-08-12 20:17:02 +020017# if defined(HAVE_WCHAR_H)
18# include <wchar.h> /* for towupper() and towlower() */
19# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010020static int win_nolbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#endif
22
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010023static unsigned nr2hex(unsigned c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int chartab_initialized = FALSE;
26
27/* b_chartab[] is an array of 32 bytes, each bit representing one of the
28 * characters 0-255. */
29#define SET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] |= (1 << ((c) & 0x7))
30#define RESET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] &= ~(1 << ((c) & 0x7))
31#define GET_CHARTAB(buf, c) ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7)))
32
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010033/* table used below, see init_chartab() for an explanation */
34static char_u g_chartab[256];
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010037 * Flags for g_chartab[].
38 */
39#define CT_CELL_MASK 0x07 /* mask: nr of display cells (1, 2 or 4) */
40#define CT_PRINT_CHAR 0x10 /* flag: set for printable chars */
41#define CT_ID_CHAR 0x20 /* flag: set for ID chars */
42#define CT_FNAME_CHAR 0x40 /* flag: set for file name chars */
43
44/*
45 * Fill g_chartab[]. Also fills curbuf->b_chartab[] with flags for keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * characters for current buffer.
47 *
48 * Depends on the option settings 'iskeyword', 'isident', 'isfname',
49 * 'isprint' and 'encoding'.
50 *
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010051 * The index in g_chartab[] depends on 'encoding':
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 * - For non-multi-byte index with the byte (same as the character).
53 * - For DBCS index with the first byte.
54 * - For UTF-8 index with the character (when first byte is up to 0x80 it is
55 * the same as the character, if the first byte is 0x80 and above it depends
56 * on further bytes).
57 *
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +010058 * The contents of g_chartab[]:
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 * - The lower two bits, masked by CT_CELL_MASK, give the number of display
60 * cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80.
61 * - CT_PRINT_CHAR bit is set when the character is printable (no need to
62 * translate the character before displaying it). Note that only DBCS
63 * characters can have 2 display cells and still be printable.
64 * - CT_FNAME_CHAR bit is set when the character can be in a file name.
65 * - CT_ID_CHAR bit is set when the character can be in an identifier.
66 *
67 * Return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has an
68 * error, OK otherwise.
69 */
70 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010071init_chartab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
73 return buf_init_chartab(curbuf, TRUE);
74}
75
76 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010077buf_init_chartab(
78 buf_T *buf,
79 int global) /* FALSE: only set buf->b_chartab[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 int c;
82 int c2;
83 char_u *p;
84 int i;
85 int tilde;
86 int do_isalpha;
87
88 if (global)
89 {
90 /*
91 * Set the default size for printable characters:
92 * From <Space> to '~' is 1 (printable), others are 2 (not printable).
93 * This also inits all 'isident' and 'isfname' flags to FALSE.
94 *
95 * EBCDIC: all chars below ' ' are not printable, all others are
96 * printable.
97 */
98 c = 0;
99 while (c < ' ')
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100100 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#ifdef EBCDIC
102 while (c < 255)
103#else
104 while (c <= '~')
105#endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100106 g_chartab[c++] = 1 + CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#ifdef FEAT_FKMAP
108 if (p_altkeymap)
109 {
110 while (c < YE)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100111 g_chartab[c++] = 1 + CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 }
113#endif
114 while (c < 256)
115 {
116#ifdef FEAT_MBYTE
117 /* UTF-8: bytes 0xa0 - 0xff are printable (latin1) */
118 if (enc_utf8 && c >= 0xa0)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100119 g_chartab[c++] = CT_PRINT_CHAR + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 /* euc-jp characters starting with 0x8e are single width */
121 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100122 g_chartab[c++] = CT_PRINT_CHAR + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 /* other double-byte chars can be printable AND double-width */
124 else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100125 g_chartab[c++] = CT_PRINT_CHAR + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 else
127#endif
128 /* the rest is unprintable by default */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100129 g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 }
131
132#ifdef FEAT_MBYTE
133 /* Assume that every multi-byte char is a filename character. */
134 for (c = 1; c < 256; ++c)
135 if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1)
136 || (enc_dbcs == DBCS_JPNU && c == 0x8e)
137 || (enc_utf8 && c >= 0xa0))
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100138 g_chartab[c] |= CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140 }
141
142 /*
143 * Init word char flags all to FALSE
144 */
145 vim_memset(buf->b_chartab, 0, (size_t)32);
146#ifdef FEAT_MBYTE
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000147 if (enc_dbcs != 0)
148 for (c = 0; c < 256; ++c)
149 {
150 /* double-byte characters are probably word characters */
151 if (MB_BYTE2LEN(c) == 2)
152 SET_CHARTAB(buf, c);
153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
155
156#ifdef FEAT_LISP
157 /*
158 * In lisp mode the '-' character is included in keywords.
159 */
160 if (buf->b_p_lisp)
161 SET_CHARTAB(buf, '-');
162#endif
163
164 /* Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint'
165 * options Each option is a list of characters, character numbers or
166 * ranges, separated by commas, e.g.: "200-210,x,#-178,-"
167 */
168 for (i = global ? 0 : 3; i <= 3; ++i)
169 {
170 if (i == 0)
171 p = p_isi; /* first round: 'isident' */
172 else if (i == 1)
173 p = p_isp; /* second round: 'isprint' */
174 else if (i == 2)
175 p = p_isf; /* third round: 'isfname' */
176 else /* i == 3 */
177 p = buf->b_p_isk; /* fourth round: 'iskeyword' */
178
179 while (*p)
180 {
181 tilde = FALSE;
182 do_isalpha = FALSE;
183 if (*p == '^' && p[1] != NUL)
184 {
185 tilde = TRUE;
186 ++p;
187 }
188 if (VIM_ISDIGIT(*p))
189 c = getdigits(&p);
190 else
Bram Moolenaar183bb3e2009-09-11 12:02:34 +0000191#ifdef FEAT_MBYTE
192 if (has_mbyte)
193 c = mb_ptr2char_adv(&p);
194 else
195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 c = *p++;
197 c2 = -1;
198 if (*p == '-' && p[1] != NUL)
199 {
200 ++p;
201 if (VIM_ISDIGIT(*p))
202 c2 = getdigits(&p);
203 else
Bram Moolenaar2ac5e602009-11-03 15:04:20 +0000204#ifdef FEAT_MBYTE
205 if (has_mbyte)
206 c2 = mb_ptr2char_adv(&p);
207 else
208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 c2 = *p++;
210 }
Bram Moolenaar2ac5e602009-11-03 15:04:20 +0000211 if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 || !(*p == NUL || *p == ','))
213 return FAIL;
214
215 if (c2 == -1) /* not a range */
216 {
217 /*
218 * A single '@' (not "@-@"):
219 * Decide on letters being ID/printable/keyword chars with
220 * standard function isalpha(). This takes care of locale for
221 * single-byte characters).
222 */
223 if (c == '@')
224 {
225 do_isalpha = TRUE;
226 c = 1;
227 c2 = 255;
228 }
229 else
230 c2 = c;
231 }
232 while (c <= c2)
233 {
Bram Moolenaardeefb632007-08-15 18:41:34 +0000234 /* Use the MB_ functions here, because isalpha() doesn't
235 * work properly when 'encoding' is "latin1" and the locale is
236 * "C". */
237 if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#ifdef FEAT_FKMAP
239 || (p_altkeymap && (F_isalpha(c) || F_isdigit(c)))
240#endif
241 )
242 {
243 if (i == 0) /* (re)set ID flag */
244 {
245 if (tilde)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100246 g_chartab[c] &= ~CT_ID_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100248 g_chartab[c] |= CT_ID_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 }
250 else if (i == 1) /* (re)set printable */
251 {
252 if ((c < ' '
253#ifndef EBCDIC
254 || c > '~'
255#endif
256#ifdef FEAT_FKMAP
257 || (p_altkeymap
258 && (F_isalpha(c) || F_isdigit(c)))
259#endif
260 )
261#ifdef FEAT_MBYTE
262 /* For double-byte we keep the cell width, so
263 * that we can detect it from the first byte. */
264 && !(enc_dbcs && MB_BYTE2LEN(c) == 2)
265#endif
266 )
267 {
268 if (tilde)
269 {
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100270 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 + ((dy_flags & DY_UHEX) ? 4 : 2);
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100272 g_chartab[c] &= ~CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 }
274 else
275 {
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100276 g_chartab[c] = (g_chartab[c] & ~CT_CELL_MASK) + 1;
277 g_chartab[c] |= CT_PRINT_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 }
279 }
280 }
281 else if (i == 2) /* (re)set fname flag */
282 {
283 if (tilde)
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100284 g_chartab[c] &= ~CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100286 g_chartab[c] |= CT_FNAME_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 }
288 else /* i == 3 */ /* (re)set keyword flag */
289 {
290 if (tilde)
291 RESET_CHARTAB(buf, c);
292 else
293 SET_CHARTAB(buf, c);
294 }
295 }
296 ++c;
297 }
Bram Moolenaar309379f2013-02-06 16:26:26 +0100298
299 c = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 p = skip_to_option_part(p);
Bram Moolenaar309379f2013-02-06 16:26:26 +0100301 if (c == ',' && *p == NUL)
302 /* Trailing comma is not allowed. */
303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 }
305 }
306 chartab_initialized = TRUE;
307 return OK;
308}
309
310/*
311 * Translate any special characters in buf[bufsize] in-place.
312 * The result is a string with only printable characters, but if there is not
313 * enough room, not all characters will be translated.
314 */
315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100316trans_characters(
317 char_u *buf,
318 int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319{
320 int len; /* length of string needing translation */
321 int room; /* room in buffer after string */
322 char_u *trs; /* translated character */
323 int trs_len; /* length of trs[] */
324
325 len = (int)STRLEN(buf);
326 room = bufsize - len;
327 while (*buf != 0)
328 {
329# ifdef FEAT_MBYTE
330 /* Assume a multi-byte character doesn't need translation. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000331 if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 len -= trs_len;
333 else
334# endif
335 {
336 trs = transchar_byte(*buf);
337 trs_len = (int)STRLEN(trs);
338 if (trs_len > 1)
339 {
340 room -= trs_len - 1;
341 if (room <= 0)
342 return;
343 mch_memmove(buf + trs_len, buf + 1, (size_t)len);
344 }
345 mch_memmove(buf, trs, (size_t)trs_len);
346 --len;
347 }
348 buf += trs_len;
349 }
350}
351
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000352#if defined(FEAT_EVAL) || defined(FEAT_TITLE) || defined(FEAT_INS_EXPAND) \
353 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/*
355 * Translate a string into allocated memory, replacing special chars with
356 * printable chars. Returns NULL when out of memory.
357 */
358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100359transstr(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
361 char_u *res;
362 char_u *p;
363#ifdef FEAT_MBYTE
364 int l, len, c;
365 char_u hexbuf[11];
366#endif
367
368#ifdef FEAT_MBYTE
369 if (has_mbyte)
370 {
371 /* Compute the length of the result, taking account of unprintable
372 * multi-byte characters. */
373 len = 0;
374 p = s;
375 while (*p != NUL)
376 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000377 if ((l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
379 c = (*mb_ptr2char)(p);
380 p += l;
381 if (vim_isprintc(c))
382 len += l;
383 else
384 {
385 transchar_hex(hexbuf, c);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000386 len += (int)STRLEN(hexbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 }
389 else
390 {
391 l = byte2cells(*p++);
392 if (l > 0)
393 len += l;
394 else
395 len += 4; /* illegal byte sequence */
396 }
397 }
398 res = alloc((unsigned)(len + 1));
399 }
400 else
401#endif
402 res = alloc((unsigned)(vim_strsize(s) + 1));
403 if (res != NULL)
404 {
405 *res = NUL;
406 p = s;
407 while (*p != NUL)
408 {
409#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000410 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 {
412 c = (*mb_ptr2char)(p);
413 if (vim_isprintc(c))
414 STRNCAT(res, p, l); /* append printable multi-byte char */
415 else
416 transchar_hex(res + STRLEN(res), c);
417 p += l;
418 }
419 else
420#endif
421 STRCAT(res, transchar_byte(*p++));
422 }
423 }
424 return res;
425}
426#endif
427
428#if defined(FEAT_SYN_HL) || defined(FEAT_INS_EXPAND) || defined(PROTO)
429/*
Bram Moolenaar217ad922005-03-20 22:37:15 +0000430 * Convert the string "str[orglen]" to do ignore-case comparing. Uses the
431 * current locale.
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000432 * When "buf" is NULL returns an allocated string (NULL for out-of-memory).
433 * Otherwise puts the result in "buf[buflen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 */
435 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100436str_foldcase(
437 char_u *str,
438 int orglen,
439 char_u *buf,
440 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
442 garray_T ga;
443 int i;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000444 int len = orglen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446#define GA_CHAR(i) ((char_u *)ga.ga_data)[i]
447#define GA_PTR(i) ((char_u *)ga.ga_data + i)
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000448#define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i])
449#define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000451 /* Copy "str" into "buf" or allocated memory, unmodified. */
452 if (buf == NULL)
453 {
454 ga_init2(&ga, 1, 10);
455 if (ga_grow(&ga, len + 1) == FAIL)
456 return NULL;
457 mch_memmove(ga.ga_data, str, (size_t)len);
458 ga.ga_len = len;
459 }
460 else
461 {
462 if (len >= buflen) /* Ugly! */
463 len = buflen - 1;
464 mch_memmove(buf, str, (size_t)len);
465 }
466 if (buf == NULL)
467 GA_CHAR(len) = NUL;
468 else
469 buf[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471 /* Make each character lower case. */
472 i = 0;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000473 while (STR_CHAR(i) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {
475#ifdef FEAT_MBYTE
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000476 if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {
478 if (enc_utf8)
479 {
Bram Moolenaarb9839212008-06-28 11:03:50 +0000480 int c = utf_ptr2char(STR_PTR(i));
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100481 int olen = utf_ptr2len(STR_PTR(i));
Bram Moolenaarb9839212008-06-28 11:03:50 +0000482 int lc = utf_tolower(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
Bram Moolenaarb9839212008-06-28 11:03:50 +0000484 /* Only replace the character when it is not an invalid
485 * sequence (ASCII character or more than one byte) and
486 * utf_tolower() doesn't return the original character. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100487 if ((c < 0x80 || olen > 1) && c != lc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100489 int nlen = utf_char2len(lc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
491 /* If the byte length changes need to shift the following
492 * characters forward or backward. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100493 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100495 if (nlen > olen)
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000496 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100497 if (buf == NULL
498 ? ga_grow(&ga, nlen - olen + 1) == FAIL
499 : len + nlen - olen >= buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 /* out of memory, keep old char */
502 lc = c;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100503 nlen = olen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000505 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100506 if (olen != nlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000508 if (buf == NULL)
509 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100510 STRMOVE(GA_PTR(i) + nlen, GA_PTR(i) + olen);
511 ga.ga_len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000512 }
513 else
514 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100515 STRMOVE(buf + i + nlen, buf + i + olen);
516 len += nlen - olen;
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 }
519 }
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000520 (void)utf_char2bytes(lc, STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 }
522 }
523 /* skip to next multi-byte char */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000524 i += (*mb_ptr2len)(STR_PTR(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
526 else
527#endif
528 {
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000529 if (buf == NULL)
530 GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i));
531 else
532 buf[i] = TOLOWER_LOC(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 ++i;
534 }
535 }
536
Bram Moolenaar6ebb1142005-01-25 21:58:26 +0000537 if (buf == NULL)
538 return (char_u *)ga.ga_data;
539 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
541#endif
542
543/*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100544 * Catch 22: g_chartab[] can't be initialized before the options are
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * initialized, and initializing options may cause transchar() to be called!
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100546 * When chartab_initialized == FALSE don't use g_chartab[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Does NOT work for multi-byte characters, c must be <= 255.
548 * Also doesn't work for the first byte of a multi-byte, "c" must be a
549 * character!
550 */
551static char_u transchar_buf[7];
552
553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554transchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 int i;
557
558 i = 0;
559 if (IS_SPECIAL(c)) /* special key code, display as ~@ char */
560 {
561 transchar_buf[0] = '~';
562 transchar_buf[1] = '@';
563 i = 2;
564 c = K_SECOND(c);
565 }
566
567 if ((!chartab_initialized && (
568#ifdef EBCDIC
569 (c >= 64 && c < 255)
570#else
571 (c >= ' ' && c <= '~')
572#endif
573#ifdef FEAT_FKMAP
574 || F_ischar(c)
575#endif
576 )) || (c < 256 && vim_isprintc_strict(c)))
577 {
578 /* printable character */
579 transchar_buf[i] = c;
580 transchar_buf[i + 1] = NUL;
581 }
582 else
583 transchar_nonprint(transchar_buf + i, c);
584 return transchar_buf;
585}
586
587#if defined(FEAT_MBYTE) || defined(PROTO)
588/*
589 * Like transchar(), but called with a byte instead of a character. Checks
590 * for an illegal UTF-8 byte.
591 */
592 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100593transchar_byte(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 if (enc_utf8 && c >= 0x80)
596 {
597 transchar_nonprint(transchar_buf, c);
598 return transchar_buf;
599 }
600 return transchar(c);
601}
602#endif
603
604/*
605 * Convert non-printable character to two or more printable characters in
606 * "buf[]". "buf" needs to be able to hold five bytes.
607 * Does NOT work for multi-byte characters, c must be <= 255.
608 */
609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100610transchar_nonprint(char_u *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 if (c == NL)
613 c = NUL; /* we use newline in place of a NUL */
614 else if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
615 c = NL; /* we use CR in place of NL in this case */
616
617 if (dy_flags & DY_UHEX) /* 'display' has "uhex" */
618 transchar_hex(buf, c);
619
620#ifdef EBCDIC
621 /* For EBCDIC only the characters 0-63 and 255 are not printable */
622 else if (CtrlChar(c) != 0 || c == DEL)
623#else
624 else if (c <= 0x7f) /* 0x00 - 0x1f and 0x7f */
625#endif
626 {
627 buf[0] = '^';
628#ifdef EBCDIC
629 if (c == DEL)
630 buf[1] = '?'; /* DEL displayed as ^? */
631 else
632 buf[1] = CtrlChar(c);
633#else
634 buf[1] = c ^ 0x40; /* DEL displayed as ^? */
635#endif
636
637 buf[2] = NUL;
638 }
639#ifdef FEAT_MBYTE
640 else if (enc_utf8 && c >= 0x80)
641 {
642 transchar_hex(buf, c);
643 }
644#endif
645#ifndef EBCDIC
646 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) /* 0xa0 - 0xfe */
647 {
648 buf[0] = '|';
649 buf[1] = c - 0x80;
650 buf[2] = NUL;
651 }
652#else
653 else if (c < 64)
654 {
655 buf[0] = '~';
656 buf[1] = MetaChar(c);
657 buf[2] = NUL;
658 }
659#endif
660 else /* 0x80 - 0x9f and 0xff */
661 {
662 /*
663 * TODO: EBCDIC I don't know what to do with this chars, so I display
664 * them as '~?' for now
665 */
666 buf[0] = '~';
667#ifdef EBCDIC
668 buf[1] = '?'; /* 0xff displayed as ~? */
669#else
670 buf[1] = (c - 0x80) ^ 0x40; /* 0xff displayed as ~? */
671#endif
672 buf[2] = NUL;
673 }
674}
675
676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100677transchar_hex(char_u *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
679 int i = 0;
680
681 buf[0] = '<';
682#ifdef FEAT_MBYTE
683 if (c > 255)
684 {
685 buf[++i] = nr2hex((unsigned)c >> 12);
686 buf[++i] = nr2hex((unsigned)c >> 8);
687 }
688#endif
689 buf[++i] = nr2hex((unsigned)c >> 4);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000690 buf[++i] = nr2hex((unsigned)c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 buf[++i] = '>';
692 buf[++i] = NUL;
693}
694
695/*
696 * Convert the lower 4 bits of byte "c" to its hex character.
697 * Lower case letters are used to avoid the confusion of <F1> being 0xf1 or
698 * function key 1.
699 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000700 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701nr2hex(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 if ((c & 0xf) <= 9)
704 return (c & 0xf) + '0';
705 return (c & 0xf) - 10 + 'a';
706}
707
708/*
709 * Return number of display cells occupied by byte "b".
710 * Caller must make sure 0 <= b <= 255.
711 * For multi-byte mode "b" must be the first byte of a character.
712 * A TAB is counted as two cells: "^I".
713 * For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of
714 * cells depends on further bytes.
715 */
716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100717byte2cells(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719#ifdef FEAT_MBYTE
720 if (enc_utf8 && b >= 0x80)
721 return 0;
722#endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100723 return (g_chartab[b] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724}
725
726/*
727 * Return number of display cells occupied by character "c".
728 * "c" can be a special key (negative number) in which case 3 or 4 is returned.
729 * A TAB is counted as two cells: "^I" or four: "<09>".
730 */
731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100732char2cells(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 if (IS_SPECIAL(c))
735 return char2cells(K_SECOND(c)) + 2;
736#ifdef FEAT_MBYTE
737 if (c >= 0x80)
738 {
739 /* UTF-8: above 0x80 need to check the value */
740 if (enc_utf8)
741 return utf_char2cells(c);
742 /* DBCS: double-byte means double-width, except for euc-jp with first
743 * byte 0x8e */
744 if (enc_dbcs != 0 && c >= 0x100)
745 {
746 if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e)
747 return 1;
748 return 2;
749 }
750 }
751#endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100752 return (g_chartab[c & 0xff] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753}
754
755/*
756 * Return number of display cells occupied by character at "*p".
757 * A TAB is counted as two cells: "^I" or four: "<09>".
758 */
759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100760ptr2cells(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
762#ifdef FEAT_MBYTE
763 /* For UTF-8 we need to look at more bytes if the first byte is >= 0x80. */
764 if (enc_utf8 && *p >= 0x80)
765 return utf_ptr2cells(p);
766 /* For DBCS we can tell the cell count from the first byte. */
767#endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100768 return (g_chartab[*p] & CT_CELL_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769}
770
771/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100772 * Return the number of character cells string "s" will take on the screen,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 * counting TABs as two characters: "^I".
774 */
775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100776vim_strsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 return vim_strnsize(s, (int)MAXCOL);
779}
780
781/*
Bram Moolenaar06af6022012-01-26 13:40:08 +0100782 * Return the number of character cells string "s[len]" will take on the
783 * screen, counting TABs as two characters: "^I".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
785 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100786vim_strnsize(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 int size = 0;
789
790 while (*s != NUL && --len >= 0)
791 {
792#ifdef FEAT_MBYTE
793 if (has_mbyte)
794 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000795 int l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 size += ptr2cells(s);
798 s += l;
799 len -= l - 1;
800 }
801 else
802#endif
803 size += byte2cells(*s++);
804 }
805 return size;
806}
807
808/*
809 * Return the number of characters 'c' will take on the screen, taking
810 * into account the size of a tab.
811 * Use a define to make it fast, this is used very often!!!
812 * Also see getvcol() below.
813 */
814
815#define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \
816 if (*(p) == TAB && (!(wp)->w_p_list || lcs_tab1)) \
817 { \
818 int ts; \
819 ts = (buf)->b_p_ts; \
820 return (int)(ts - (col % ts)); \
821 } \
822 else \
823 return ptr2cells(p);
824
825#if defined(FEAT_VREPLACE) || defined(FEAT_EX_EXTRA) || defined(FEAT_GUI) \
826 || defined(FEAT_VIRTUALEDIT) || defined(PROTO)
827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100828chartabsize(char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829{
830 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col)
831}
832#endif
833
834#ifdef FEAT_LINEBREAK
835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836win_chartabsize(win_T *wp, char_u *p, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col)
839}
840#endif
841
842/*
Bram Moolenaardc536092010-07-18 15:45:49 +0200843 * Return the number of characters the string 's' will take on the screen,
844 * taking into account the size of a tab.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 */
846 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847linetabsize(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
Bram Moolenaardc536092010-07-18 15:45:49 +0200849 return linetabsize_col(0, s);
850}
851
852/*
853 * Like linetabsize(), but starting at column "startcol".
854 */
855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100856linetabsize_col(int startcol, char_u *s)
Bram Moolenaardc536092010-07-18 15:45:49 +0200857{
858 colnr_T col = startcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200859 char_u *line = s; /* pointer to start of line, for breakindent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861 while (*s != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200862 col += lbr_chartabsize_adv(line, &s, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 return (int)col;
864}
865
866/*
867 * Like linetabsize(), but for a given window instead of the current one.
868 */
869 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100870win_linetabsize(win_T *wp, char_u *line, colnr_T len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871{
872 colnr_T col = 0;
873 char_u *s;
874
Bram Moolenaar597a4222014-06-25 14:39:50 +0200875 for (s = line; *s != NUL && (len == MAXCOL || s < line + len);
876 mb_ptr_adv(s))
877 col += win_lbr_chartabsize(wp, line, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 return (int)col;
879}
880
881/*
Bram Moolenaar81695252004-12-29 20:58:21 +0000882 * Return TRUE if 'c' is a normal identifier character:
883 * Letters and characters from the 'isident' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 */
885 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886vim_isIDc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100888 return (c > 0 && c < 0x100 && (g_chartab[c] & CT_ID_CHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889}
890
891/*
892 * return TRUE if 'c' is a keyword character: Letters and characters from
893 * 'iskeyword' option for current buffer.
894 * For multi-byte characters mb_get_class() is used (builtin rules).
895 */
896 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100897vim_iswordc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100899 return vim_iswordc_buf(c, curbuf);
900}
901
902 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100903vim_iswordc_buf(int c, buf_T *buf)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100904{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905#ifdef FEAT_MBYTE
906 if (c >= 0x100)
907 {
908 if (enc_dbcs != 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000909 return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (enc_utf8)
911 return utf_class(c) >= 2;
912 }
913#endif
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100914 return (c > 0 && c < 0x100 && GET_CHARTAB(buf, c) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915}
916
917/*
918 * Just like vim_iswordc() but uses a pointer to the (multi-byte) character.
919 */
920 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100921vim_iswordp(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923#ifdef FEAT_MBYTE
924 if (has_mbyte && MB_BYTE2LEN(*p) > 1)
925 return mb_get_class(p) >= 2;
926#endif
927 return GET_CHARTAB(curbuf, *p) != 0;
928}
929
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100931vim_iswordp_buf(char_u *p, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaara50e5862013-01-30 17:30:17 +0100933#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (has_mbyte && MB_BYTE2LEN(*p) > 1)
935 return mb_get_class(p) >= 2;
Bram Moolenaara50e5862013-01-30 17:30:17 +0100936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 return (GET_CHARTAB(buf, *p) != 0);
938}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940/*
941 * return TRUE if 'c' is a valid file-name character
942 * Assume characters above 0x100 are valid (multi-byte).
943 */
944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945vim_isfilec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100947 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_FNAME_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000951 * return TRUE if 'c' is a valid file-name character or a wildcard character
952 * Assume characters above 0x100 are valid (multi-byte).
953 * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]")
954 * returns false.
955 */
956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100957vim_isfilec_or_wc(int c)
Bram Moolenaardd87969c2007-08-21 13:07:12 +0000958{
959 char_u buf[2];
960
961 buf[0] = (char_u)c;
962 buf[1] = NUL;
963 return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf);
964}
965
966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 * return TRUE if 'c' is a printable character
968 * Assume characters above 0x100 are printable (multi-byte), except for
969 * Unicode.
970 */
971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972vim_isprintc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974#ifdef FEAT_MBYTE
975 if (enc_utf8 && c >= 0x100)
976 return utf_printable(c);
977#endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100978 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
982 * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head
983 * byte of a double-byte character.
984 */
985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100986vim_isprintc_strict(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988#ifdef FEAT_MBYTE
989 if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1)
990 return FALSE;
991 if (enc_utf8 && c >= 0x100)
992 return utf_printable(c);
993#endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +0100994 return (c >= 0x100 || (c > 0 && (g_chartab[c] & CT_PRINT_CHAR)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995}
996
997/*
998 * like chartabsize(), but also check for line breaks on the screen
999 */
1000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001001lbr_chartabsize(
1002 char_u *line UNUSED, /* start of the line */
1003 unsigned char *s,
1004 colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +02001007 if (!curwin->w_p_lbr && *p_sbr == NUL && !curwin->w_p_bri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 {
1009#endif
1010#ifdef FEAT_MBYTE
1011 if (curwin->w_p_wrap)
1012 return win_nolbr_chartabsize(curwin, s, col, NULL);
1013#endif
1014 RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col)
1015#ifdef FEAT_LINEBREAK
1016 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02001017 return win_lbr_chartabsize(curwin, line == NULL ? s : line, s, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#endif
1019}
1020
1021/*
1022 * Call lbr_chartabsize() and advance the pointer.
1023 */
1024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001025lbr_chartabsize_adv(
1026 char_u *line, /* start of the line */
1027 char_u **s,
1028 colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
1030 int retval;
1031
Bram Moolenaar597a4222014-06-25 14:39:50 +02001032 retval = lbr_chartabsize(line, *s, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001033 mb_ptr_adv(*s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 return retval;
1035}
1036
1037/*
1038 * This function is used very often, keep it fast!!!!
1039 *
1040 * If "headp" not NULL, set *headp to the size of what we for 'showbreak'
1041 * string at start of line. Warning: *headp is only set if it's a non-zero
1042 * value, init to 0 before calling.
1043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001045win_lbr_chartabsize(
1046 win_T *wp,
1047 char_u *line UNUSED, /* start of the line */
1048 char_u *s,
1049 colnr_T col,
1050 int *headp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052#ifdef FEAT_LINEBREAK
1053 int c;
1054 int size;
1055 colnr_T col2;
Bram Moolenaaree739b42014-07-02 19:37:42 +02001056 colnr_T col_adj = 0; /* col + screen size of tab */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 colnr_T colmax;
1058 int added;
1059# ifdef FEAT_MBYTE
1060 int mb_added = 0;
1061# else
1062# define mb_added 0
1063# endif
1064 int numberextra;
1065 char_u *ps;
1066 int tab_corr = (*s == TAB);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001067 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +02001070 * No 'linebreak', 'showbreak' and 'breakindent': return quickly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 */
Bram Moolenaar597a4222014-06-25 14:39:50 +02001072 if (!wp->w_p_lbr && !wp->w_p_bri && *p_sbr == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#endif
1074 {
1075#ifdef FEAT_MBYTE
1076 if (wp->w_p_wrap)
1077 return win_nolbr_chartabsize(wp, s, col, headp);
1078#endif
1079 RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col)
1080 }
1081
1082#ifdef FEAT_LINEBREAK
1083 /*
1084 * First get normal size, without 'linebreak'
1085 */
1086 size = win_chartabsize(wp, s, col);
1087 c = *s;
Bram Moolenaaree739b42014-07-02 19:37:42 +02001088 if (tab_corr)
1089 col_adj = size - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 /*
1092 * If 'linebreak' set check at a blank before a non-blank if the line
1093 * needs a break here
1094 */
1095 if (wp->w_p_lbr
1096 && vim_isbreak(c)
1097 && !vim_isbreak(s[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 && wp->w_p_wrap
1099# ifdef FEAT_VERTSPLIT
1100 && wp->w_width != 0
1101# endif
1102 )
1103 {
1104 /*
1105 * Count all characters from first non-blank after a blank up to next
1106 * non-blank after a blank.
1107 */
1108 numberextra = win_col_off(wp);
1109 col2 = col;
Bram Moolenaaree739b42014-07-02 19:37:42 +02001110 colmax = (colnr_T)(W_WIDTH(wp) - numberextra - col_adj);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (col >= colmax)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001112 {
Bram Moolenaaree739b42014-07-02 19:37:42 +02001113 colmax += col_adj;
1114 n = colmax + win_col_off2(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001115 if (n > 0)
Bram Moolenaaree739b42014-07-02 19:37:42 +02001116 colmax += (((col - colmax) / n) + 1) * n - col_adj;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001117 }
1118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 for (;;)
1120 {
1121 ps = s;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001122 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 c = *s;
1124 if (!(c != NUL
1125 && (vim_isbreak(c)
1126 || (!vim_isbreak(c)
1127 && (col2 == col || !vim_isbreak(*ps))))))
1128 break;
1129
1130 col2 += win_chartabsize(wp, s, col2);
1131 if (col2 >= colmax) /* doesn't fit */
1132 {
Bram Moolenaaree739b42014-07-02 19:37:42 +02001133 size = colmax - col + col_adj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 tab_corr = FALSE;
1135 break;
1136 }
1137 }
1138 }
1139# ifdef FEAT_MBYTE
1140 else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1
1141 && wp->w_p_wrap && in_win_border(wp, col))
1142 {
1143 ++size; /* Count the ">" in the last column. */
1144 mb_added = 1;
1145 }
1146# endif
1147
1148 /*
Bram Moolenaar597a4222014-06-25 14:39:50 +02001149 * May have to add something for 'breakindent' and/or 'showbreak'
1150 * string at start of line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * Set *headp to the size of what we add.
1152 */
1153 added = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001154 if ((*p_sbr != NUL || wp->w_p_bri) && wp->w_p_wrap && col != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
Bram Moolenaard574ea22015-01-14 19:35:14 +01001156 colnr_T sbrlen = 0;
1157 int numberwidth = win_col_off(wp);
1158
1159 numberextra = numberwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 col += numberextra + mb_added;
1161 if (col >= (colnr_T)W_WIDTH(wp))
1162 {
1163 col -= W_WIDTH(wp);
1164 numberextra = W_WIDTH(wp) - (numberextra - win_col_off2(wp));
Bram Moolenaard574ea22015-01-14 19:35:14 +01001165 if (col >= numberextra && numberextra > 0)
Bram Moolenaarfe3c4102014-10-31 12:42:01 +01001166 col %= numberextra;
Bram Moolenaar1c852102014-10-15 21:26:40 +02001167 if (*p_sbr != NUL)
1168 {
Bram Moolenaard574ea22015-01-14 19:35:14 +01001169 sbrlen = (colnr_T)MB_CHARLEN(p_sbr);
Bram Moolenaar1c852102014-10-15 21:26:40 +02001170 if (col >= sbrlen)
1171 col -= sbrlen;
1172 }
Bram Moolenaard574ea22015-01-14 19:35:14 +01001173 if (col >= numberextra && numberextra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 col = col % numberextra;
Bram Moolenaard574ea22015-01-14 19:35:14 +01001175 else if (col > 0 && numberextra > 0)
1176 col += numberwidth - win_col_off2(wp);
1177
1178 numberwidth -= win_col_off2(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 }
Bram Moolenaard574ea22015-01-14 19:35:14 +01001180 if (col == 0 || col + size + sbrlen > (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02001182 added = 0;
1183 if (*p_sbr != NUL)
Bram Moolenaard574ea22015-01-14 19:35:14 +01001184 {
1185 if (size + sbrlen + numberwidth > (colnr_T)W_WIDTH(wp))
1186 {
1187 /* calculate effective window width */
1188 int width = (colnr_T)W_WIDTH(wp) - sbrlen - numberwidth;
1189 int prev_width = col ? ((colnr_T)W_WIDTH(wp) - (sbrlen + col)) : 0;
1190 if (width == 0)
1191 width = (colnr_T)W_WIDTH(wp);
1192 added += ((size - prev_width) / width) * vim_strsize(p_sbr);
1193 if ((size - prev_width) % width)
1194 /* wrapped, add another length of 'sbr' */
1195 added += vim_strsize(p_sbr);
1196 }
1197 else
1198 added += vim_strsize(p_sbr);
1199 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02001200 if (wp->w_p_bri)
1201 added += get_breakindent_win(wp, line);
1202
Bram Moolenaar95765082014-08-24 21:19:25 +02001203 size += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (col != 0)
1205 added = 0;
1206 }
1207 }
1208 if (headp != NULL)
1209 *headp = added + mb_added;
1210 return size;
1211#endif
1212}
1213
1214#if defined(FEAT_MBYTE) || defined(PROTO)
1215/*
1216 * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and
1217 * 'wrap' is on. This means we need to check for a double-byte character that
1218 * doesn't fit at the end of the screen line.
1219 */
1220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001221win_nolbr_chartabsize(
1222 win_T *wp,
1223 char_u *s,
1224 colnr_T col,
1225 int *headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226{
1227 int n;
1228
1229 if (*s == TAB && (!wp->w_p_list || lcs_tab1))
1230 {
1231 n = wp->w_buffer->b_p_ts;
1232 return (int)(n - (col % n));
1233 }
1234 n = ptr2cells(s);
1235 /* Add one cell for a double-width character in the last column of the
1236 * window, displayed with a ">". */
1237 if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col))
1238 {
1239 if (headp != NULL)
1240 *headp = 1;
1241 return 3;
1242 }
1243 return n;
1244}
1245
1246/*
1247 * Return TRUE if virtual column "vcol" is in the rightmost column of window
1248 * "wp".
1249 */
1250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001251in_win_border(win_T *wp, colnr_T vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001253 int width1; /* width of first line (after line number) */
1254 int width2; /* width of further lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256#ifdef FEAT_VERTSPLIT
1257 if (wp->w_width == 0) /* there is no border */
1258 return FALSE;
1259#endif
1260 width1 = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001261 if ((int)vcol < width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001263 if ((int)vcol == width1 - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 return TRUE;
1265 width2 = width1 + win_col_off2(wp);
Bram Moolenaar8701cd62009-10-07 14:20:30 +00001266 if (width2 <= 0)
1267 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 return ((vcol - width1) % width2 == width2 - 1);
1269}
1270#endif /* FEAT_MBYTE */
1271
1272/*
1273 * Get virtual column number of pos.
1274 * start: on the first position of this character (TAB, ctrl)
1275 * cursor: where the cursor is on this character (first char, except for TAB)
1276 * end: on the last position of this character (TAB, ctrl)
1277 *
1278 * This is used very often, keep it fast!
1279 */
1280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001281getvcol(
1282 win_T *wp,
1283 pos_T *pos,
1284 colnr_T *start,
1285 colnr_T *cursor,
1286 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 colnr_T vcol;
1289 char_u *ptr; /* points to current char */
1290 char_u *posptr; /* points to char at pos->col */
Bram Moolenaar597a4222014-06-25 14:39:50 +02001291 char_u *line; /* start of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 int incr;
1293 int head;
1294 int ts = wp->w_buffer->b_p_ts;
1295 int c;
1296
1297 vcol = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001298 line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001299 if (pos->col == MAXCOL)
1300 posptr = NULL; /* continue until the NUL */
1301 else
1302 posptr = ptr + pos->col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 /*
1305 * This function is used very often, do some speed optimizations.
Bram Moolenaar597a4222014-06-25 14:39:50 +02001306 * When 'list', 'linebreak', 'showbreak' and 'breakindent' are not set
1307 * use a simple loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 * Also use this when 'list' is set but tabs take their normal size.
1309 */
1310 if ((!wp->w_p_list || lcs_tab1 != NUL)
1311#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +02001312 && !wp->w_p_lbr && *p_sbr == NUL && !wp->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313#endif
1314 )
1315 {
1316#ifndef FEAT_MBYTE
1317 head = 0;
1318#endif
1319 for (;;)
1320 {
1321#ifdef FEAT_MBYTE
1322 head = 0;
1323#endif
1324 c = *ptr;
1325 /* make sure we don't go past the end of the line */
1326 if (c == NUL)
1327 {
1328 incr = 1; /* NUL at end of line only takes one column */
1329 break;
1330 }
1331 /* A tab gets expanded, depending on the current column */
1332 if (c == TAB)
1333 incr = ts - (vcol % ts);
1334 else
1335 {
1336#ifdef FEAT_MBYTE
1337 if (has_mbyte)
1338 {
1339 /* For utf-8, if the byte is >= 0x80, need to look at
1340 * further bytes to find the cell width. */
1341 if (enc_utf8 && c >= 0x80)
1342 incr = utf_ptr2cells(ptr);
1343 else
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001344 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 /* If a double-cell char doesn't fit at the end of a line
1347 * it wraps to the next line, it's like this char is three
1348 * cells wide. */
Bram Moolenaar9c33a7c2008-02-20 13:59:32 +00001349 if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1
1350 && in_win_border(wp, vcol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
1352 ++incr;
1353 head = 1;
1354 }
1355 }
1356 else
1357#endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001358 incr = g_chartab[c] & CT_CELL_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001361 if (posptr != NULL && ptr >= posptr) /* character at pos->col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 break;
1363
1364 vcol += incr;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001365 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367 }
1368 else
1369 {
1370 for (;;)
1371 {
1372 /* A tab gets expanded, depending on the current column */
1373 head = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001374 incr = win_lbr_chartabsize(wp, line, ptr, vcol, &head);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /* make sure we don't go past the end of the line */
1376 if (*ptr == NUL)
1377 {
1378 incr = 1; /* NUL at end of line only takes one column */
1379 break;
1380 }
1381
Bram Moolenaar37d619f2010-03-10 14:46:26 +01001382 if (posptr != NULL && ptr >= posptr) /* character at pos->col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 break;
1384
1385 vcol += incr;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001386 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389 if (start != NULL)
1390 *start = vcol + head;
1391 if (end != NULL)
1392 *end = vcol + incr - 1;
1393 if (cursor != NULL)
1394 {
1395 if (*ptr == TAB
1396 && (State & NORMAL)
1397 && !wp->w_p_list
1398 && !virtual_active()
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001399 && !(VIsual_active && (*p_sel == 'e' || ltoreq(*pos, VIsual)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 )
1401 *cursor = vcol + incr - 1; /* cursor at end */
1402 else
1403 *cursor = vcol + head; /* cursor at start */
1404 }
1405}
1406
1407/*
1408 * Get virtual cursor column in the current window, pretending 'list' is off.
1409 */
1410 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001411getvcol_nolist(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 int list_save = curwin->w_p_list;
1414 colnr_T vcol;
1415
1416 curwin->w_p_list = FALSE;
1417 getvcol(curwin, posp, NULL, &vcol, NULL);
1418 curwin->w_p_list = list_save;
1419 return vcol;
1420}
1421
1422#if defined(FEAT_VIRTUALEDIT) || defined(PROTO)
1423/*
1424 * Get virtual column in virtual mode.
1425 */
1426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001427getvvcol(
1428 win_T *wp,
1429 pos_T *pos,
1430 colnr_T *start,
1431 colnr_T *cursor,
1432 colnr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 colnr_T col;
1435 colnr_T coladd;
1436 colnr_T endadd;
1437# ifdef FEAT_MBYTE
1438 char_u *ptr;
1439# endif
1440
1441 if (virtual_active())
1442 {
1443 /* For virtual mode, only want one value */
1444 getvcol(wp, pos, &col, NULL, NULL);
1445
1446 coladd = pos->coladd;
1447 endadd = 0;
1448# ifdef FEAT_MBYTE
1449 /* Cannot put the cursor on part of a wide character. */
1450 ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001451 if (pos->col < (colnr_T)STRLEN(ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
1453 int c = (*mb_ptr2char)(ptr + pos->col);
1454
1455 if (c != TAB && vim_isprintc(c))
1456 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001457 endadd = (colnr_T)(char2cells(c) - 1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00001458 if (coladd > endadd) /* past end of line */
1459 endadd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 else
1461 coladd = 0;
1462 }
1463 }
1464# endif
1465 col += coladd;
1466 if (start != NULL)
1467 *start = col;
1468 if (cursor != NULL)
1469 *cursor = col;
1470 if (end != NULL)
1471 *end = col + endadd;
1472 }
1473 else
1474 getvcol(wp, pos, start, cursor, end);
1475}
1476#endif
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478/*
1479 * Get the leftmost and rightmost virtual column of pos1 and pos2.
1480 * Used for Visual block mode.
1481 */
1482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483getvcols(
1484 win_T *wp,
1485 pos_T *pos1,
1486 pos_T *pos2,
1487 colnr_T *left,
1488 colnr_T *right)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 colnr_T from1, from2, to1, to2;
1491
1492 if (ltp(pos1, pos2))
1493 {
1494 getvvcol(wp, pos1, &from1, NULL, &to1);
1495 getvvcol(wp, pos2, &from2, NULL, &to2);
1496 }
1497 else
1498 {
1499 getvvcol(wp, pos2, &from1, NULL, &to1);
1500 getvvcol(wp, pos1, &from2, NULL, &to2);
1501 }
1502 if (from2 < from1)
1503 *left = from2;
1504 else
1505 *left = from1;
1506 if (to2 > to1)
1507 {
1508 if (*p_sel == 'e' && from2 - 1 >= to1)
1509 *right = from2 - 1;
1510 else
1511 *right = to2;
1512 }
1513 else
1514 *right = to1;
1515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
1517/*
1518 * skipwhite: skip over ' ' and '\t'.
1519 */
1520 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521skipwhite(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001523 char_u *p = q;
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 while (vim_iswhite(*p)) /* skip to next non-white */
1526 ++p;
1527 return p;
1528}
1529
1530/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001531 * skip over digits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 */
1533 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534skipdigits(char_u *q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001536 char_u *p = q;
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 while (VIM_ISDIGIT(*p)) /* skip to next non-digit */
1539 ++p;
1540 return p;
1541}
1542
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001543#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001544/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001545 * skip over binary digits
1546 */
1547 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001548skipbin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001549{
1550 char_u *p = q;
1551
1552 while (vim_isbdigit(*p)) /* skip to next non-digit */
1553 ++p;
1554 return p;
1555}
1556
1557/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001558 * skip over digits and hex characters
1559 */
1560 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561skiphex(char_u *q)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001562{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001563 char_u *p = q;
1564
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001565 while (vim_isxdigit(*p)) /* skip to next non-digit */
1566 ++p;
1567 return p;
1568}
1569#endif
1570
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001571#if defined(FEAT_EX_EXTRA) || defined(PROTO)
1572/*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001573 * skip to bin digit (or NUL after the string)
1574 */
1575 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001576skiptobin(char_u *q)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001577{
1578 char_u *p = q;
1579
1580 while (*p != NUL && !vim_isbdigit(*p)) /* skip to next digit */
1581 ++p;
1582 return p;
1583}
1584
1585/*
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001586 * skip to digit (or NUL after the string)
1587 */
1588 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001589skiptodigit(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001590{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001591 char_u *p = q;
1592
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001593 while (*p != NUL && !VIM_ISDIGIT(*p)) /* skip to next digit */
1594 ++p;
1595 return p;
1596}
1597
1598/*
1599 * skip to hex character (or NUL after the string)
1600 */
1601 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602skiptohex(char_u *q)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001603{
Bram Moolenaar1387a602008-07-24 19:31:11 +00001604 char_u *p = q;
1605
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001606 while (*p != NUL && !vim_isxdigit(*p)) /* skip to next digit */
1607 ++p;
1608 return p;
1609}
1610#endif
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612/*
1613 * Variant of isdigit() that can handle characters > 0x100.
1614 * We don't use isdigit() here, because on some systems it also considers
1615 * superscript 1 to be a digit.
1616 * Use the VIM_ISDIGIT() macro for simple arguments.
1617 */
1618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001619vim_isdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 return (c >= '0' && c <= '9');
1622}
1623
1624/*
1625 * Variant of isxdigit() that can handle characters > 0x100.
1626 * We don't use isxdigit() here, because on some systems it also considers
1627 * superscript 1 to be a digit.
1628 */
1629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001630vim_isxdigit(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 return (c >= '0' && c <= '9')
1633 || (c >= 'a' && c <= 'f')
1634 || (c >= 'A' && c <= 'F');
1635}
1636
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001637/*
1638 * Corollary of vim_isdigit and vim_isxdigit() that can handle
1639 * characters > 0x100.
1640 */
1641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001642vim_isbdigit(int c)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001643{
1644 return (c == '0' || c == '1');
1645}
1646
Bram Moolenaar78622822005-08-23 21:00:13 +00001647#if defined(FEAT_MBYTE) || defined(PROTO)
1648/*
1649 * Vim's own character class functions. These exist because many library
1650 * islower()/toupper() etc. do not work properly: they crash when used with
1651 * invalid values or can't handle latin1 when the locale is C.
1652 * Speed is most important here.
1653 */
1654#define LATIN1LOWER 'l'
1655#define LATIN1UPPER 'U'
1656
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00001657static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll";
Bram Moolenaar936347b2012-05-25 11:56:22 +02001658static 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";
1659static 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 +00001660
1661 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662vim_islower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001663{
1664 if (c <= '@')
1665 return FALSE;
1666 if (c >= 0x80)
1667 {
1668 if (enc_utf8)
1669 return utf_islower(c);
1670 if (c >= 0x100)
1671 {
1672#ifdef HAVE_ISWLOWER
1673 if (has_mbyte)
1674 return iswlower(c);
1675#endif
1676 /* islower() can't handle these chars and may crash */
1677 return FALSE;
1678 }
1679 if (enc_latin1like)
1680 return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER;
1681 }
1682 return islower(c);
1683}
1684
1685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001686vim_isupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001687{
1688 if (c <= '@')
1689 return FALSE;
1690 if (c >= 0x80)
1691 {
1692 if (enc_utf8)
1693 return utf_isupper(c);
1694 if (c >= 0x100)
1695 {
1696#ifdef HAVE_ISWUPPER
1697 if (has_mbyte)
1698 return iswupper(c);
1699#endif
1700 /* islower() can't handle these chars and may crash */
1701 return FALSE;
1702 }
1703 if (enc_latin1like)
1704 return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER;
1705 }
1706 return isupper(c);
1707}
1708
1709 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001710vim_toupper(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001711{
1712 if (c <= '@')
1713 return c;
1714 if (c >= 0x80)
1715 {
1716 if (enc_utf8)
1717 return utf_toupper(c);
1718 if (c >= 0x100)
1719 {
1720#ifdef HAVE_TOWUPPER
1721 if (has_mbyte)
1722 return towupper(c);
1723#endif
1724 /* toupper() can't handle these chars and may crash */
1725 return c;
1726 }
1727 if (enc_latin1like)
1728 return latin1upper[c];
1729 }
1730 return TOUPPER_LOC(c);
1731}
1732
1733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734vim_tolower(int c)
Bram Moolenaar78622822005-08-23 21:00:13 +00001735{
1736 if (c <= '@')
1737 return c;
1738 if (c >= 0x80)
1739 {
1740 if (enc_utf8)
1741 return utf_tolower(c);
1742 if (c >= 0x100)
1743 {
1744#ifdef HAVE_TOWLOWER
1745 if (has_mbyte)
1746 return towlower(c);
1747#endif
1748 /* tolower() can't handle these chars and may crash */
1749 return c;
1750 }
1751 if (enc_latin1like)
1752 return latin1lower[c];
1753 }
1754 return TOLOWER_LOC(c);
1755}
1756#endif
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758/*
1759 * skiptowhite: skip over text until ' ' or '\t' or NUL.
1760 */
1761 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762skiptowhite(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 while (*p != ' ' && *p != '\t' && *p != NUL)
1765 ++p;
1766 return p;
1767}
1768
1769#if defined(FEAT_LISTCMDS) || defined(FEAT_SIGNS) || defined(FEAT_SNIFF) \
1770 || defined(PROTO)
1771/*
1772 * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars
1773 */
1774 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775skiptowhite_esc(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 while (*p != ' ' && *p != '\t' && *p != NUL)
1778 {
1779 if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL)
1780 ++p;
1781 ++p;
1782 }
1783 return p;
1784}
1785#endif
1786
1787/*
1788 * Getdigits: Get a number from a string and skip over it.
1789 * Note: the argument is a pointer to a char_u pointer!
1790 */
1791 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792getdigits(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
1794 char_u *p;
1795 long retval;
1796
1797 p = *pp;
1798 retval = atol((char *)p);
1799 if (*p == '-') /* skip negative sign */
1800 ++p;
1801 p = skipdigits(p); /* skip to next non-digit */
1802 *pp = p;
1803 return retval;
1804}
1805
1806/*
1807 * Return TRUE if "lbuf" is empty or only contains blanks.
1808 */
1809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810vim_isblankline(char_u *lbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 char_u *p;
1813
1814 p = skipwhite(lbuf);
1815 return (*p == NUL || *p == '\r' || *p == '\n');
1816}
1817
1818/*
1819 * Convert a string into a long and/or unsigned long, taking care of
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001820 * hexadecimal, octal, and binary numbers. Accepts a '-' sign.
1821 * If "prep" is not NULL, returns a flag to indicate the type of the number:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 * 0 decimal
1823 * '0' octal
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001824 * 'B' bin
1825 * 'b' bin
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 * 'X' hex
1827 * 'x' hex
1828 * If "len" is not NULL, the length of the number in characters is returned.
1829 * If "nptr" is not NULL, the signed result is returned in it.
1830 * If "unptr" is not NULL, the unsigned result is returned in it.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001831 * If "what" contains STR2NR_BIN recognize binary numbers
1832 * If "what" contains STR2NR_OCT recognize octal numbers
1833 * If "what" contains STR2NR_HEX recognize hex numbers
1834 * If "what" contains STR2NR_FORCE always assume bin/oct/hex.
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001835 * If maxlen > 0, check at a maximum maxlen chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838vim_str2nr(
1839 char_u *start,
1840 int *prep, /* return: type of number 0 = decimal, 'x'
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001841 or 'X' is hex, '0' = octal, 'b' or 'B'
1842 is bin */
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843 int *len, /* return: detected length of number */
1844 int what, /* what numbers to recognize */
1845 long *nptr, /* return: signed result */
1846 unsigned long *unptr, /* return: unsigned result */
1847 int maxlen) /* max length of string to check */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 char_u *ptr = start;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001850 int pre = 0; /* default is decimal */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 int negative = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 unsigned long un = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001853 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 if (ptr[0] == '-')
1856 {
1857 negative = TRUE;
1858 ++ptr;
1859 }
1860
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001861 /* Recognize hex, octal, and bin. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001862 if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9'
1863 && (maxlen == 0 || maxlen > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001865 pre = ptr[1];
1866 if ((what & STR2NR_HEX)
1867 && (pre == 'X' || pre == 'x') && vim_isxdigit(ptr[2])
1868 && (maxlen == 0 || maxlen > 2))
1869 /* hexadecimal */
1870 ptr += 2;
1871 else if ((what & STR2NR_BIN)
1872 && (pre == 'B' || pre == 'b') && vim_isbdigit(ptr[2])
1873 && (maxlen == 0 || maxlen > 2))
1874 /* binary */
1875 ptr += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 else
1877 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001878 /* decimal or octal, default is decimal */
1879 pre = 0;
1880 if (what & STR2NR_OCT)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001881 {
1882 /* Don't interpret "0", "08" or "0129" as octal. */
1883 for (n = 1; VIM_ISDIGIT(ptr[n]); ++n)
1884 {
1885 if (ptr[n] > '7')
1886 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001887 pre = 0; /* can't be octal */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001888 break;
1889 }
Bram Moolenaar06af6022012-01-26 13:40:08 +01001890 if (ptr[n] >= '0')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001891 pre = '0'; /* assume octal */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001892 if (n == maxlen)
1893 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001894 }
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898
1899 /*
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001900 * Do the string-to-numeric conversion "manually" to avoid sscanf quirks.
1901 */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001902 n = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001903 if (pre == 'B' || pre == 'b' || what == STR2NR_BIN + STR2NR_FORCE)
1904 {
1905 /* bin */
1906 if (pre != 0)
1907 n += 2; /* skip over "0b" */
1908 while ('0' <= *ptr && *ptr <= '1')
1909 {
1910 un = 2 * un + (unsigned long)(*ptr - '0');
1911 ++ptr;
1912 if (n++ == maxlen)
1913 break;
1914 }
1915 }
1916 else if (pre == '0' || what == STR2NR_OCT + STR2NR_FORCE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001918 /* octal */
1919 while ('0' <= *ptr && *ptr <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001921 un = 8 * un + (unsigned long)(*ptr - '0');
1922 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001923 if (n++ == maxlen)
1924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001926 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001927 else if (pre != 0 || what == STR2NR_HEX + STR2NR_FORCE)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001928 {
1929 /* hex */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001930 if (pre != 0)
Bram Moolenaar5adfea12015-09-01 18:51:39 +02001931 n += 2; /* skip over "0x" */
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001932 while (vim_isxdigit(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001934 un = 16 * un + (unsigned long)hex2nr(*ptr);
1935 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001936 if (n++ == maxlen)
1937 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 }
1940 else
1941 {
1942 /* decimal */
1943 while (VIM_ISDIGIT(*ptr))
1944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 un = 10 * un + (unsigned long)(*ptr - '0');
1946 ++ptr;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001947 if (n++ == maxlen)
1948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
1951
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001952 if (prep != NULL)
1953 *prep = pre;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (len != NULL)
1955 *len = (int)(ptr - start);
1956 if (nptr != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001957 {
1958 if (negative) /* account for leading '-' for decimal numbers */
1959 *nptr = -(long)un;
1960 else
1961 *nptr = (long)un;
1962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (unptr != NULL)
1964 *unptr = un;
1965}
1966
1967/*
1968 * Return the value of a single hex character.
1969 * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'.
1970 */
1971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001972hex2nr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974 if (c >= 'a' && c <= 'f')
1975 return c - 'a' + 10;
1976 if (c >= 'A' && c <= 'F')
1977 return c - 'A' + 10;
1978 return c - '0';
1979}
1980
1981#if defined(FEAT_TERMRESPONSE) \
1982 || (defined(FEAT_GUI_GTK) && defined(FEAT_WINDOWS)) || defined(PROTO)
1983/*
1984 * Convert two hex characters to a byte.
1985 * Return -1 if one of the characters is not hex.
1986 */
1987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001988hexhex2nr(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1]))
1991 return -1;
1992 return (hex2nr(p[0]) << 4) + hex2nr(p[1]);
1993}
1994#endif
1995
1996/*
1997 * Return TRUE if "str" starts with a backslash that should be removed.
1998 * For MS-DOS, WIN32 and OS/2 this is only done when the character after the
1999 * backslash is not a normal file name character.
2000 * '$' is a valid file name character, we don't remove the backslash before
2001 * it. This means it is not possible to use an environment variable after a
2002 * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works.
2003 * Although "\ name" is valid, the backslash in "Program\ files" must be
2004 * removed. Assume a file name doesn't start with a space.
2005 * For multi-byte names, never remove a backslash before a non-ascii
2006 * character, assume that all multi-byte characters are valid file name
2007 * characters.
2008 */
2009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002010rem_backslash(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012#ifdef BACKSLASH_IN_FILENAME
2013 return (str[0] == '\\'
2014# ifdef FEAT_MBYTE
2015 && str[1] < 0x80
2016# endif
2017 && (str[1] == ' '
2018 || (str[1] != NUL
2019 && str[1] != '*'
2020 && str[1] != '?'
2021 && !vim_isfilec(str[1]))));
2022#else
2023 return (str[0] == '\\' && str[1] != NUL);
2024#endif
2025}
2026
2027/*
2028 * Halve the number of backslashes in a file name argument.
2029 * For MS-DOS we only do this if the character after the backslash
2030 * is not a normal file character.
2031 */
2032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002033backslash_halve(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 for ( ; *p; ++p)
2036 if (rem_backslash(p))
Bram Moolenaar446cb832008-06-24 21:56:24 +00002037 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038}
2039
2040/*
2041 * backslash_halve() plus save the result in allocated memory.
2042 */
2043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002044backslash_halve_save(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 char_u *res;
2047
2048 res = vim_strsave(p);
2049 if (res == NULL)
2050 return p;
2051 backslash_halve(res);
2052 return res;
2053}
2054
2055#if (defined(EBCDIC) && defined(FEAT_POSTSCRIPT)) || defined(PROTO)
2056/*
2057 * Table for EBCDIC to ASCII conversion unashamedly taken from xxd.c!
2058 * The first 64 entries have been added to map control characters defined in
2059 * ascii.h
2060 */
2061static char_u ebcdic2ascii_tab[256] =
2062{
2063 0000, 0001, 0002, 0003, 0004, 0011, 0006, 0177,
2064 0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
2065 0020, 0021, 0022, 0023, 0024, 0012, 0010, 0027,
2066 0030, 0031, 0032, 0033, 0033, 0035, 0036, 0037,
2067 0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
2068 0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
2069 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
2070 0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
2071 0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
2072 0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174,
2073 0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
2074 0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176,
2075 0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
2076 0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077,
2077 0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
2078 0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
2079 0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
2080 0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
2081 0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
2082 0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320,
2083 0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170,
2084 0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327,
2085 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
2086 0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347,
2087 0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
2088 0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
2089 0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
2090 0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
2091 0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
2092 0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
2093 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
2094 0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377
2095};
2096
2097/*
2098 * Convert a buffer worth of characters from EBCDIC to ASCII. Only useful if
2099 * wanting 7-bit ASCII characters out the other end.
2100 */
2101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002102ebcdic2ascii(char_u *buffer, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 int i;
2105
2106 for (i = 0; i < len; i++)
2107 buffer[i] = ebcdic2ascii_tab[buffer[i]];
2108}
2109#endif