Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* 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 |
| 13 | static int win_chartabsize __ARGS((win_T *wp, char_u *p, colnr_T col)); |
| 14 | #endif |
| 15 | |
| 16 | #ifdef FEAT_MBYTE |
| 17 | static int win_nolbr_chartabsize __ARGS((win_T *wp, char_u *s, colnr_T col, int *headp)); |
| 18 | #endif |
| 19 | |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 20 | static unsigned nr2hex __ARGS((unsigned c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | |
| 22 | static int chartab_initialized = FALSE; |
| 23 | |
| 24 | /* b_chartab[] is an array of 32 bytes, each bit representing one of the |
| 25 | * characters 0-255. */ |
| 26 | #define SET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] |= (1 << ((c) & 0x7)) |
| 27 | #define RESET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> 3] &= ~(1 << ((c) & 0x7)) |
| 28 | #define GET_CHARTAB(buf, c) ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7))) |
| 29 | |
| 30 | /* |
| 31 | * Fill chartab[]. Also fills curbuf->b_chartab[] with flags for keyword |
| 32 | * characters for current buffer. |
| 33 | * |
| 34 | * Depends on the option settings 'iskeyword', 'isident', 'isfname', |
| 35 | * 'isprint' and 'encoding'. |
| 36 | * |
| 37 | * The index in chartab[] depends on 'encoding': |
| 38 | * - For non-multi-byte index with the byte (same as the character). |
| 39 | * - For DBCS index with the first byte. |
| 40 | * - For UTF-8 index with the character (when first byte is up to 0x80 it is |
| 41 | * the same as the character, if the first byte is 0x80 and above it depends |
| 42 | * on further bytes). |
| 43 | * |
| 44 | * The contents of chartab[]: |
| 45 | * - The lower two bits, masked by CT_CELL_MASK, give the number of display |
| 46 | * cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80. |
| 47 | * - CT_PRINT_CHAR bit is set when the character is printable (no need to |
| 48 | * translate the character before displaying it). Note that only DBCS |
| 49 | * characters can have 2 display cells and still be printable. |
| 50 | * - CT_FNAME_CHAR bit is set when the character can be in a file name. |
| 51 | * - CT_ID_CHAR bit is set when the character can be in an identifier. |
| 52 | * |
| 53 | * Return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has an |
| 54 | * error, OK otherwise. |
| 55 | */ |
| 56 | int |
| 57 | init_chartab() |
| 58 | { |
| 59 | return buf_init_chartab(curbuf, TRUE); |
| 60 | } |
| 61 | |
| 62 | int |
| 63 | buf_init_chartab(buf, global) |
| 64 | buf_T *buf; |
| 65 | int global; /* FALSE: only set buf->b_chartab[] */ |
| 66 | { |
| 67 | int c; |
| 68 | int c2; |
| 69 | char_u *p; |
| 70 | int i; |
| 71 | int tilde; |
| 72 | int do_isalpha; |
| 73 | |
| 74 | if (global) |
| 75 | { |
| 76 | /* |
| 77 | * Set the default size for printable characters: |
| 78 | * From <Space> to '~' is 1 (printable), others are 2 (not printable). |
| 79 | * This also inits all 'isident' and 'isfname' flags to FALSE. |
| 80 | * |
| 81 | * EBCDIC: all chars below ' ' are not printable, all others are |
| 82 | * printable. |
| 83 | */ |
| 84 | c = 0; |
| 85 | while (c < ' ') |
| 86 | chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; |
| 87 | #ifdef EBCDIC |
| 88 | while (c < 255) |
| 89 | #else |
| 90 | while (c <= '~') |
| 91 | #endif |
| 92 | chartab[c++] = 1 + CT_PRINT_CHAR; |
| 93 | #ifdef FEAT_FKMAP |
| 94 | if (p_altkeymap) |
| 95 | { |
| 96 | while (c < YE) |
| 97 | chartab[c++] = 1 + CT_PRINT_CHAR; |
| 98 | } |
| 99 | #endif |
| 100 | while (c < 256) |
| 101 | { |
| 102 | #ifdef FEAT_MBYTE |
| 103 | /* UTF-8: bytes 0xa0 - 0xff are printable (latin1) */ |
| 104 | if (enc_utf8 && c >= 0xa0) |
| 105 | chartab[c++] = CT_PRINT_CHAR + 1; |
| 106 | /* euc-jp characters starting with 0x8e are single width */ |
| 107 | else if (enc_dbcs == DBCS_JPNU && c == 0x8e) |
| 108 | chartab[c++] = CT_PRINT_CHAR + 1; |
| 109 | /* other double-byte chars can be printable AND double-width */ |
| 110 | else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2) |
| 111 | chartab[c++] = CT_PRINT_CHAR + 2; |
| 112 | else |
| 113 | #endif |
| 114 | /* the rest is unprintable by default */ |
| 115 | chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; |
| 116 | } |
| 117 | |
| 118 | #ifdef FEAT_MBYTE |
| 119 | /* Assume that every multi-byte char is a filename character. */ |
| 120 | for (c = 1; c < 256; ++c) |
| 121 | if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1) |
| 122 | || (enc_dbcs == DBCS_JPNU && c == 0x8e) |
| 123 | || (enc_utf8 && c >= 0xa0)) |
| 124 | chartab[c] |= CT_FNAME_CHAR; |
| 125 | #endif |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Init word char flags all to FALSE |
| 130 | */ |
| 131 | vim_memset(buf->b_chartab, 0, (size_t)32); |
| 132 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 133 | if (enc_dbcs != 0) |
| 134 | for (c = 0; c < 256; ++c) |
| 135 | { |
| 136 | /* double-byte characters are probably word characters */ |
| 137 | if (MB_BYTE2LEN(c) == 2) |
| 138 | SET_CHARTAB(buf, c); |
| 139 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | #ifdef FEAT_LISP |
| 143 | /* |
| 144 | * In lisp mode the '-' character is included in keywords. |
| 145 | */ |
| 146 | if (buf->b_p_lisp) |
| 147 | SET_CHARTAB(buf, '-'); |
| 148 | #endif |
| 149 | |
| 150 | /* Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint' |
| 151 | * options Each option is a list of characters, character numbers or |
| 152 | * ranges, separated by commas, e.g.: "200-210,x,#-178,-" |
| 153 | */ |
| 154 | for (i = global ? 0 : 3; i <= 3; ++i) |
| 155 | { |
| 156 | if (i == 0) |
| 157 | p = p_isi; /* first round: 'isident' */ |
| 158 | else if (i == 1) |
| 159 | p = p_isp; /* second round: 'isprint' */ |
| 160 | else if (i == 2) |
| 161 | p = p_isf; /* third round: 'isfname' */ |
| 162 | else /* i == 3 */ |
| 163 | p = buf->b_p_isk; /* fourth round: 'iskeyword' */ |
| 164 | |
| 165 | while (*p) |
| 166 | { |
| 167 | tilde = FALSE; |
| 168 | do_isalpha = FALSE; |
| 169 | if (*p == '^' && p[1] != NUL) |
| 170 | { |
| 171 | tilde = TRUE; |
| 172 | ++p; |
| 173 | } |
| 174 | if (VIM_ISDIGIT(*p)) |
| 175 | c = getdigits(&p); |
| 176 | else |
Bram Moolenaar | 183bb3e | 2009-09-11 12:02:34 +0000 | [diff] [blame] | 177 | #ifdef FEAT_MBYTE |
| 178 | if (has_mbyte) |
| 179 | c = mb_ptr2char_adv(&p); |
| 180 | else |
| 181 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | c = *p++; |
| 183 | c2 = -1; |
| 184 | if (*p == '-' && p[1] != NUL) |
| 185 | { |
| 186 | ++p; |
| 187 | if (VIM_ISDIGIT(*p)) |
| 188 | c2 = getdigits(&p); |
| 189 | else |
| 190 | c2 = *p++; |
| 191 | } |
| 192 | if (c <= 0 || (c2 < c && c2 != -1) || c2 >= 256 |
| 193 | || !(*p == NUL || *p == ',')) |
| 194 | return FAIL; |
| 195 | |
| 196 | if (c2 == -1) /* not a range */ |
| 197 | { |
| 198 | /* |
| 199 | * A single '@' (not "@-@"): |
| 200 | * Decide on letters being ID/printable/keyword chars with |
| 201 | * standard function isalpha(). This takes care of locale for |
| 202 | * single-byte characters). |
| 203 | */ |
| 204 | if (c == '@') |
| 205 | { |
| 206 | do_isalpha = TRUE; |
| 207 | c = 1; |
| 208 | c2 = 255; |
| 209 | } |
| 210 | else |
| 211 | c2 = c; |
| 212 | } |
| 213 | while (c <= c2) |
| 214 | { |
Bram Moolenaar | deefb63 | 2007-08-15 18:41:34 +0000 | [diff] [blame] | 215 | /* Use the MB_ functions here, because isalpha() doesn't |
| 216 | * work properly when 'encoding' is "latin1" and the locale is |
| 217 | * "C". */ |
| 218 | if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | #ifdef FEAT_FKMAP |
| 220 | || (p_altkeymap && (F_isalpha(c) || F_isdigit(c))) |
| 221 | #endif |
| 222 | ) |
| 223 | { |
| 224 | if (i == 0) /* (re)set ID flag */ |
| 225 | { |
| 226 | if (tilde) |
| 227 | chartab[c] &= ~CT_ID_CHAR; |
| 228 | else |
| 229 | chartab[c] |= CT_ID_CHAR; |
| 230 | } |
| 231 | else if (i == 1) /* (re)set printable */ |
| 232 | { |
| 233 | if ((c < ' ' |
| 234 | #ifndef EBCDIC |
| 235 | || c > '~' |
| 236 | #endif |
| 237 | #ifdef FEAT_FKMAP |
| 238 | || (p_altkeymap |
| 239 | && (F_isalpha(c) || F_isdigit(c))) |
| 240 | #endif |
| 241 | ) |
| 242 | #ifdef FEAT_MBYTE |
| 243 | /* For double-byte we keep the cell width, so |
| 244 | * that we can detect it from the first byte. */ |
| 245 | && !(enc_dbcs && MB_BYTE2LEN(c) == 2) |
| 246 | #endif |
| 247 | ) |
| 248 | { |
| 249 | if (tilde) |
| 250 | { |
| 251 | chartab[c] = (chartab[c] & ~CT_CELL_MASK) |
| 252 | + ((dy_flags & DY_UHEX) ? 4 : 2); |
| 253 | chartab[c] &= ~CT_PRINT_CHAR; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | chartab[c] = (chartab[c] & ~CT_CELL_MASK) + 1; |
| 258 | chartab[c] |= CT_PRINT_CHAR; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | else if (i == 2) /* (re)set fname flag */ |
| 263 | { |
| 264 | if (tilde) |
| 265 | chartab[c] &= ~CT_FNAME_CHAR; |
| 266 | else |
| 267 | chartab[c] |= CT_FNAME_CHAR; |
| 268 | } |
| 269 | else /* i == 3 */ /* (re)set keyword flag */ |
| 270 | { |
| 271 | if (tilde) |
| 272 | RESET_CHARTAB(buf, c); |
| 273 | else |
| 274 | SET_CHARTAB(buf, c); |
| 275 | } |
| 276 | } |
| 277 | ++c; |
| 278 | } |
| 279 | p = skip_to_option_part(p); |
| 280 | } |
| 281 | } |
| 282 | chartab_initialized = TRUE; |
| 283 | return OK; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Translate any special characters in buf[bufsize] in-place. |
| 288 | * The result is a string with only printable characters, but if there is not |
| 289 | * enough room, not all characters will be translated. |
| 290 | */ |
| 291 | void |
| 292 | trans_characters(buf, bufsize) |
| 293 | char_u *buf; |
| 294 | int bufsize; |
| 295 | { |
| 296 | int len; /* length of string needing translation */ |
| 297 | int room; /* room in buffer after string */ |
| 298 | char_u *trs; /* translated character */ |
| 299 | int trs_len; /* length of trs[] */ |
| 300 | |
| 301 | len = (int)STRLEN(buf); |
| 302 | room = bufsize - len; |
| 303 | while (*buf != 0) |
| 304 | { |
| 305 | # ifdef FEAT_MBYTE |
| 306 | /* Assume a multi-byte character doesn't need translation. */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 307 | if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 308 | len -= trs_len; |
| 309 | else |
| 310 | # endif |
| 311 | { |
| 312 | trs = transchar_byte(*buf); |
| 313 | trs_len = (int)STRLEN(trs); |
| 314 | if (trs_len > 1) |
| 315 | { |
| 316 | room -= trs_len - 1; |
| 317 | if (room <= 0) |
| 318 | return; |
| 319 | mch_memmove(buf + trs_len, buf + 1, (size_t)len); |
| 320 | } |
| 321 | mch_memmove(buf, trs, (size_t)trs_len); |
| 322 | --len; |
| 323 | } |
| 324 | buf += trs_len; |
| 325 | } |
| 326 | } |
| 327 | |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 328 | #if defined(FEAT_EVAL) || defined(FEAT_TITLE) || defined(FEAT_INS_EXPAND) \ |
| 329 | || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 330 | /* |
| 331 | * Translate a string into allocated memory, replacing special chars with |
| 332 | * printable chars. Returns NULL when out of memory. |
| 333 | */ |
| 334 | char_u * |
| 335 | transstr(s) |
| 336 | char_u *s; |
| 337 | { |
| 338 | char_u *res; |
| 339 | char_u *p; |
| 340 | #ifdef FEAT_MBYTE |
| 341 | int l, len, c; |
| 342 | char_u hexbuf[11]; |
| 343 | #endif |
| 344 | |
| 345 | #ifdef FEAT_MBYTE |
| 346 | if (has_mbyte) |
| 347 | { |
| 348 | /* Compute the length of the result, taking account of unprintable |
| 349 | * multi-byte characters. */ |
| 350 | len = 0; |
| 351 | p = s; |
| 352 | while (*p != NUL) |
| 353 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 354 | if ((l = (*mb_ptr2len)(p)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 355 | { |
| 356 | c = (*mb_ptr2char)(p); |
| 357 | p += l; |
| 358 | if (vim_isprintc(c)) |
| 359 | len += l; |
| 360 | else |
| 361 | { |
| 362 | transchar_hex(hexbuf, c); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 363 | len += (int)STRLEN(hexbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | l = byte2cells(*p++); |
| 369 | if (l > 0) |
| 370 | len += l; |
| 371 | else |
| 372 | len += 4; /* illegal byte sequence */ |
| 373 | } |
| 374 | } |
| 375 | res = alloc((unsigned)(len + 1)); |
| 376 | } |
| 377 | else |
| 378 | #endif |
| 379 | res = alloc((unsigned)(vim_strsize(s) + 1)); |
| 380 | if (res != NULL) |
| 381 | { |
| 382 | *res = NUL; |
| 383 | p = s; |
| 384 | while (*p != NUL) |
| 385 | { |
| 386 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 387 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 388 | { |
| 389 | c = (*mb_ptr2char)(p); |
| 390 | if (vim_isprintc(c)) |
| 391 | STRNCAT(res, p, l); /* append printable multi-byte char */ |
| 392 | else |
| 393 | transchar_hex(res + STRLEN(res), c); |
| 394 | p += l; |
| 395 | } |
| 396 | else |
| 397 | #endif |
| 398 | STRCAT(res, transchar_byte(*p++)); |
| 399 | } |
| 400 | } |
| 401 | return res; |
| 402 | } |
| 403 | #endif |
| 404 | |
| 405 | #if defined(FEAT_SYN_HL) || defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 406 | /* |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 407 | * Convert the string "str[orglen]" to do ignore-case comparing. Uses the |
| 408 | * current locale. |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 409 | * When "buf" is NULL returns an allocated string (NULL for out-of-memory). |
| 410 | * Otherwise puts the result in "buf[buflen]". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 411 | */ |
| 412 | char_u * |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 413 | str_foldcase(str, orglen, buf, buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 414 | char_u *str; |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 415 | int orglen; |
| 416 | char_u *buf; |
| 417 | int buflen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | { |
| 419 | garray_T ga; |
| 420 | int i; |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 421 | int len = orglen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 422 | |
| 423 | #define GA_CHAR(i) ((char_u *)ga.ga_data)[i] |
| 424 | #define GA_PTR(i) ((char_u *)ga.ga_data + i) |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 425 | #define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i]) |
| 426 | #define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 427 | |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 428 | /* Copy "str" into "buf" or allocated memory, unmodified. */ |
| 429 | if (buf == NULL) |
| 430 | { |
| 431 | ga_init2(&ga, 1, 10); |
| 432 | if (ga_grow(&ga, len + 1) == FAIL) |
| 433 | return NULL; |
| 434 | mch_memmove(ga.ga_data, str, (size_t)len); |
| 435 | ga.ga_len = len; |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | if (len >= buflen) /* Ugly! */ |
| 440 | len = buflen - 1; |
| 441 | mch_memmove(buf, str, (size_t)len); |
| 442 | } |
| 443 | if (buf == NULL) |
| 444 | GA_CHAR(len) = NUL; |
| 445 | else |
| 446 | buf[len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 447 | |
| 448 | /* Make each character lower case. */ |
| 449 | i = 0; |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 450 | while (STR_CHAR(i) != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | { |
| 452 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 453 | if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 454 | { |
| 455 | if (enc_utf8) |
| 456 | { |
Bram Moolenaar | b983921 | 2008-06-28 11:03:50 +0000 | [diff] [blame] | 457 | int c = utf_ptr2char(STR_PTR(i)); |
| 458 | int ol = utf_ptr2len(STR_PTR(i)); |
| 459 | int lc = utf_tolower(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 460 | |
Bram Moolenaar | b983921 | 2008-06-28 11:03:50 +0000 | [diff] [blame] | 461 | /* Only replace the character when it is not an invalid |
| 462 | * sequence (ASCII character or more than one byte) and |
| 463 | * utf_tolower() doesn't return the original character. */ |
| 464 | if ((c < 0x80 || ol > 1) && c != lc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 465 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 466 | int nl = utf_char2len(lc); |
| 467 | |
| 468 | /* If the byte length changes need to shift the following |
| 469 | * characters forward or backward. */ |
| 470 | if (ol != nl) |
| 471 | { |
| 472 | if (nl > ol) |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 473 | { |
| 474 | if (buf == NULL ? ga_grow(&ga, nl - ol + 1) == FAIL |
| 475 | : len + nl - ol >= buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 476 | { |
| 477 | /* out of memory, keep old char */ |
| 478 | lc = c; |
| 479 | nl = ol; |
| 480 | } |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 481 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 482 | if (ol != nl) |
| 483 | { |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 484 | if (buf == NULL) |
| 485 | { |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 486 | STRMOVE(GA_PTR(i) + nl, GA_PTR(i) + ol); |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 487 | ga.ga_len += nl - ol; |
| 488 | } |
| 489 | else |
| 490 | { |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 491 | STRMOVE(buf + i + nl, buf + i + ol); |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 492 | len += nl - ol; |
| 493 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 496 | (void)utf_char2bytes(lc, STR_PTR(i)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | /* skip to next multi-byte char */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 500 | i += (*mb_ptr2len)(STR_PTR(i)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | } |
| 502 | else |
| 503 | #endif |
| 504 | { |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 505 | if (buf == NULL) |
| 506 | GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i)); |
| 507 | else |
| 508 | buf[i] = TOLOWER_LOC(buf[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 509 | ++i; |
| 510 | } |
| 511 | } |
| 512 | |
Bram Moolenaar | 6ebb114 | 2005-01-25 21:58:26 +0000 | [diff] [blame] | 513 | if (buf == NULL) |
| 514 | return (char_u *)ga.ga_data; |
| 515 | return buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 516 | } |
| 517 | #endif |
| 518 | |
| 519 | /* |
| 520 | * Catch 22: chartab[] can't be initialized before the options are |
| 521 | * initialized, and initializing options may cause transchar() to be called! |
| 522 | * When chartab_initialized == FALSE don't use chartab[]. |
| 523 | * Does NOT work for multi-byte characters, c must be <= 255. |
| 524 | * Also doesn't work for the first byte of a multi-byte, "c" must be a |
| 525 | * character! |
| 526 | */ |
| 527 | static char_u transchar_buf[7]; |
| 528 | |
| 529 | char_u * |
| 530 | transchar(c) |
| 531 | int c; |
| 532 | { |
| 533 | int i; |
| 534 | |
| 535 | i = 0; |
| 536 | if (IS_SPECIAL(c)) /* special key code, display as ~@ char */ |
| 537 | { |
| 538 | transchar_buf[0] = '~'; |
| 539 | transchar_buf[1] = '@'; |
| 540 | i = 2; |
| 541 | c = K_SECOND(c); |
| 542 | } |
| 543 | |
| 544 | if ((!chartab_initialized && ( |
| 545 | #ifdef EBCDIC |
| 546 | (c >= 64 && c < 255) |
| 547 | #else |
| 548 | (c >= ' ' && c <= '~') |
| 549 | #endif |
| 550 | #ifdef FEAT_FKMAP |
| 551 | || F_ischar(c) |
| 552 | #endif |
| 553 | )) || (c < 256 && vim_isprintc_strict(c))) |
| 554 | { |
| 555 | /* printable character */ |
| 556 | transchar_buf[i] = c; |
| 557 | transchar_buf[i + 1] = NUL; |
| 558 | } |
| 559 | else |
| 560 | transchar_nonprint(transchar_buf + i, c); |
| 561 | return transchar_buf; |
| 562 | } |
| 563 | |
| 564 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 565 | /* |
| 566 | * Like transchar(), but called with a byte instead of a character. Checks |
| 567 | * for an illegal UTF-8 byte. |
| 568 | */ |
| 569 | char_u * |
| 570 | transchar_byte(c) |
| 571 | int c; |
| 572 | { |
| 573 | if (enc_utf8 && c >= 0x80) |
| 574 | { |
| 575 | transchar_nonprint(transchar_buf, c); |
| 576 | return transchar_buf; |
| 577 | } |
| 578 | return transchar(c); |
| 579 | } |
| 580 | #endif |
| 581 | |
| 582 | /* |
| 583 | * Convert non-printable character to two or more printable characters in |
| 584 | * "buf[]". "buf" needs to be able to hold five bytes. |
| 585 | * Does NOT work for multi-byte characters, c must be <= 255. |
| 586 | */ |
| 587 | void |
| 588 | transchar_nonprint(buf, c) |
| 589 | char_u *buf; |
| 590 | int c; |
| 591 | { |
| 592 | if (c == NL) |
| 593 | c = NUL; /* we use newline in place of a NUL */ |
| 594 | else if (c == CAR && get_fileformat(curbuf) == EOL_MAC) |
| 595 | c = NL; /* we use CR in place of NL in this case */ |
| 596 | |
| 597 | if (dy_flags & DY_UHEX) /* 'display' has "uhex" */ |
| 598 | transchar_hex(buf, c); |
| 599 | |
| 600 | #ifdef EBCDIC |
| 601 | /* For EBCDIC only the characters 0-63 and 255 are not printable */ |
| 602 | else if (CtrlChar(c) != 0 || c == DEL) |
| 603 | #else |
| 604 | else if (c <= 0x7f) /* 0x00 - 0x1f and 0x7f */ |
| 605 | #endif |
| 606 | { |
| 607 | buf[0] = '^'; |
| 608 | #ifdef EBCDIC |
| 609 | if (c == DEL) |
| 610 | buf[1] = '?'; /* DEL displayed as ^? */ |
| 611 | else |
| 612 | buf[1] = CtrlChar(c); |
| 613 | #else |
| 614 | buf[1] = c ^ 0x40; /* DEL displayed as ^? */ |
| 615 | #endif |
| 616 | |
| 617 | buf[2] = NUL; |
| 618 | } |
| 619 | #ifdef FEAT_MBYTE |
| 620 | else if (enc_utf8 && c >= 0x80) |
| 621 | { |
| 622 | transchar_hex(buf, c); |
| 623 | } |
| 624 | #endif |
| 625 | #ifndef EBCDIC |
| 626 | else if (c >= ' ' + 0x80 && c <= '~' + 0x80) /* 0xa0 - 0xfe */ |
| 627 | { |
| 628 | buf[0] = '|'; |
| 629 | buf[1] = c - 0x80; |
| 630 | buf[2] = NUL; |
| 631 | } |
| 632 | #else |
| 633 | else if (c < 64) |
| 634 | { |
| 635 | buf[0] = '~'; |
| 636 | buf[1] = MetaChar(c); |
| 637 | buf[2] = NUL; |
| 638 | } |
| 639 | #endif |
| 640 | else /* 0x80 - 0x9f and 0xff */ |
| 641 | { |
| 642 | /* |
| 643 | * TODO: EBCDIC I don't know what to do with this chars, so I display |
| 644 | * them as '~?' for now |
| 645 | */ |
| 646 | buf[0] = '~'; |
| 647 | #ifdef EBCDIC |
| 648 | buf[1] = '?'; /* 0xff displayed as ~? */ |
| 649 | #else |
| 650 | buf[1] = (c - 0x80) ^ 0x40; /* 0xff displayed as ~? */ |
| 651 | #endif |
| 652 | buf[2] = NUL; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | void |
| 657 | transchar_hex(buf, c) |
| 658 | char_u *buf; |
| 659 | int c; |
| 660 | { |
| 661 | int i = 0; |
| 662 | |
| 663 | buf[0] = '<'; |
| 664 | #ifdef FEAT_MBYTE |
| 665 | if (c > 255) |
| 666 | { |
| 667 | buf[++i] = nr2hex((unsigned)c >> 12); |
| 668 | buf[++i] = nr2hex((unsigned)c >> 8); |
| 669 | } |
| 670 | #endif |
| 671 | buf[++i] = nr2hex((unsigned)c >> 4); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 672 | buf[++i] = nr2hex((unsigned)c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | buf[++i] = '>'; |
| 674 | buf[++i] = NUL; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Convert the lower 4 bits of byte "c" to its hex character. |
| 679 | * Lower case letters are used to avoid the confusion of <F1> being 0xf1 or |
| 680 | * function key 1. |
| 681 | */ |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 682 | static unsigned |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 683 | nr2hex(c) |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 684 | unsigned c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | { |
| 686 | if ((c & 0xf) <= 9) |
| 687 | return (c & 0xf) + '0'; |
| 688 | return (c & 0xf) - 10 + 'a'; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Return number of display cells occupied by byte "b". |
| 693 | * Caller must make sure 0 <= b <= 255. |
| 694 | * For multi-byte mode "b" must be the first byte of a character. |
| 695 | * A TAB is counted as two cells: "^I". |
| 696 | * For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of |
| 697 | * cells depends on further bytes. |
| 698 | */ |
| 699 | int |
| 700 | byte2cells(b) |
| 701 | int b; |
| 702 | { |
| 703 | #ifdef FEAT_MBYTE |
| 704 | if (enc_utf8 && b >= 0x80) |
| 705 | return 0; |
| 706 | #endif |
| 707 | return (chartab[b] & CT_CELL_MASK); |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Return number of display cells occupied by character "c". |
| 712 | * "c" can be a special key (negative number) in which case 3 or 4 is returned. |
| 713 | * A TAB is counted as two cells: "^I" or four: "<09>". |
| 714 | */ |
| 715 | int |
| 716 | char2cells(c) |
| 717 | int c; |
| 718 | { |
| 719 | if (IS_SPECIAL(c)) |
| 720 | return char2cells(K_SECOND(c)) + 2; |
| 721 | #ifdef FEAT_MBYTE |
| 722 | if (c >= 0x80) |
| 723 | { |
| 724 | /* UTF-8: above 0x80 need to check the value */ |
| 725 | if (enc_utf8) |
| 726 | return utf_char2cells(c); |
| 727 | /* DBCS: double-byte means double-width, except for euc-jp with first |
| 728 | * byte 0x8e */ |
| 729 | if (enc_dbcs != 0 && c >= 0x100) |
| 730 | { |
| 731 | if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e) |
| 732 | return 1; |
| 733 | return 2; |
| 734 | } |
| 735 | } |
| 736 | #endif |
| 737 | return (chartab[c & 0xff] & CT_CELL_MASK); |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Return number of display cells occupied by character at "*p". |
| 742 | * A TAB is counted as two cells: "^I" or four: "<09>". |
| 743 | */ |
| 744 | int |
| 745 | ptr2cells(p) |
| 746 | char_u *p; |
| 747 | { |
| 748 | #ifdef FEAT_MBYTE |
| 749 | /* For UTF-8 we need to look at more bytes if the first byte is >= 0x80. */ |
| 750 | if (enc_utf8 && *p >= 0x80) |
| 751 | return utf_ptr2cells(p); |
| 752 | /* For DBCS we can tell the cell count from the first byte. */ |
| 753 | #endif |
| 754 | return (chartab[*p] & CT_CELL_MASK); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Return the number of characters string "s" will take on the screen, |
| 759 | * counting TABs as two characters: "^I". |
| 760 | */ |
| 761 | int |
| 762 | vim_strsize(s) |
| 763 | char_u *s; |
| 764 | { |
| 765 | return vim_strnsize(s, (int)MAXCOL); |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Return the number of characters string "s[len]" will take on the screen, |
| 770 | * counting TABs as two characters: "^I". |
| 771 | */ |
| 772 | int |
| 773 | vim_strnsize(s, len) |
| 774 | char_u *s; |
| 775 | int len; |
| 776 | { |
| 777 | int size = 0; |
| 778 | |
| 779 | while (*s != NUL && --len >= 0) |
| 780 | { |
| 781 | #ifdef FEAT_MBYTE |
| 782 | if (has_mbyte) |
| 783 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 784 | int l = (*mb_ptr2len)(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 785 | |
| 786 | size += ptr2cells(s); |
| 787 | s += l; |
| 788 | len -= l - 1; |
| 789 | } |
| 790 | else |
| 791 | #endif |
| 792 | size += byte2cells(*s++); |
| 793 | } |
| 794 | return size; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Return the number of characters 'c' will take on the screen, taking |
| 799 | * into account the size of a tab. |
| 800 | * Use a define to make it fast, this is used very often!!! |
| 801 | * Also see getvcol() below. |
| 802 | */ |
| 803 | |
| 804 | #define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \ |
| 805 | if (*(p) == TAB && (!(wp)->w_p_list || lcs_tab1)) \ |
| 806 | { \ |
| 807 | int ts; \ |
| 808 | ts = (buf)->b_p_ts; \ |
| 809 | return (int)(ts - (col % ts)); \ |
| 810 | } \ |
| 811 | else \ |
| 812 | return ptr2cells(p); |
| 813 | |
| 814 | #if defined(FEAT_VREPLACE) || defined(FEAT_EX_EXTRA) || defined(FEAT_GUI) \ |
| 815 | || defined(FEAT_VIRTUALEDIT) || defined(PROTO) |
| 816 | int |
| 817 | chartabsize(p, col) |
| 818 | char_u *p; |
| 819 | colnr_T col; |
| 820 | { |
| 821 | RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col) |
| 822 | } |
| 823 | #endif |
| 824 | |
| 825 | #ifdef FEAT_LINEBREAK |
| 826 | static int |
| 827 | win_chartabsize(wp, p, col) |
| 828 | win_T *wp; |
| 829 | char_u *p; |
| 830 | colnr_T col; |
| 831 | { |
| 832 | RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col) |
| 833 | } |
| 834 | #endif |
| 835 | |
| 836 | /* |
| 837 | * return the number of characters the string 's' will take on the screen, |
| 838 | * taking into account the size of a tab |
| 839 | */ |
| 840 | int |
| 841 | linetabsize(s) |
| 842 | char_u *s; |
| 843 | { |
| 844 | colnr_T col = 0; |
| 845 | |
| 846 | while (*s != NUL) |
| 847 | col += lbr_chartabsize_adv(&s, col); |
| 848 | return (int)col; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Like linetabsize(), but for a given window instead of the current one. |
| 853 | */ |
| 854 | int |
| 855 | win_linetabsize(wp, p, len) |
| 856 | win_T *wp; |
| 857 | char_u *p; |
| 858 | colnr_T len; |
| 859 | { |
| 860 | colnr_T col = 0; |
| 861 | char_u *s; |
| 862 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 863 | for (s = p; *s != NUL && (len == MAXCOL || s < p + len); mb_ptr_adv(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | col += win_lbr_chartabsize(wp, s, col, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 865 | return (int)col; |
| 866 | } |
| 867 | |
| 868 | /* |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 869 | * Return TRUE if 'c' is a normal identifier character: |
| 870 | * Letters and characters from the 'isident' option. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 871 | */ |
| 872 | int |
| 873 | vim_isIDc(c) |
| 874 | int c; |
| 875 | { |
| 876 | return (c > 0 && c < 0x100 && (chartab[c] & CT_ID_CHAR)); |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * return TRUE if 'c' is a keyword character: Letters and characters from |
| 881 | * 'iskeyword' option for current buffer. |
| 882 | * For multi-byte characters mb_get_class() is used (builtin rules). |
| 883 | */ |
| 884 | int |
| 885 | vim_iswordc(c) |
| 886 | int c; |
| 887 | { |
| 888 | #ifdef FEAT_MBYTE |
| 889 | if (c >= 0x100) |
| 890 | { |
| 891 | if (enc_dbcs != 0) |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 892 | return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 893 | if (enc_utf8) |
| 894 | return utf_class(c) >= 2; |
| 895 | } |
| 896 | #endif |
| 897 | return (c > 0 && c < 0x100 && GET_CHARTAB(curbuf, c) != 0); |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Just like vim_iswordc() but uses a pointer to the (multi-byte) character. |
| 902 | */ |
| 903 | int |
| 904 | vim_iswordp(p) |
| 905 | char_u *p; |
| 906 | { |
| 907 | #ifdef FEAT_MBYTE |
| 908 | if (has_mbyte && MB_BYTE2LEN(*p) > 1) |
| 909 | return mb_get_class(p) >= 2; |
| 910 | #endif |
| 911 | return GET_CHARTAB(curbuf, *p) != 0; |
| 912 | } |
| 913 | |
| 914 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 915 | int |
| 916 | vim_iswordc_buf(p, buf) |
| 917 | char_u *p; |
| 918 | buf_T *buf; |
| 919 | { |
| 920 | # ifdef FEAT_MBYTE |
| 921 | if (has_mbyte && MB_BYTE2LEN(*p) > 1) |
| 922 | return mb_get_class(p) >= 2; |
| 923 | # endif |
| 924 | return (GET_CHARTAB(buf, *p) != 0); |
| 925 | } |
Bram Moolenaar | c4956c8 | 2006-03-12 21:58:43 +0000 | [diff] [blame] | 926 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | |
| 928 | /* |
| 929 | * return TRUE if 'c' is a valid file-name character |
| 930 | * Assume characters above 0x100 are valid (multi-byte). |
| 931 | */ |
| 932 | int |
| 933 | vim_isfilec(c) |
| 934 | int c; |
| 935 | { |
| 936 | return (c >= 0x100 || (c > 0 && (chartab[c] & CT_FNAME_CHAR))); |
| 937 | } |
| 938 | |
| 939 | /* |
Bram Moolenaar | dd87969c | 2007-08-21 13:07:12 +0000 | [diff] [blame] | 940 | * return TRUE if 'c' is a valid file-name character or a wildcard character |
| 941 | * Assume characters above 0x100 are valid (multi-byte). |
| 942 | * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]") |
| 943 | * returns false. |
| 944 | */ |
| 945 | int |
| 946 | vim_isfilec_or_wc(c) |
| 947 | int c; |
| 948 | { |
| 949 | char_u buf[2]; |
| 950 | |
| 951 | buf[0] = (char_u)c; |
| 952 | buf[1] = NUL; |
| 953 | return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf); |
| 954 | } |
| 955 | |
| 956 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 957 | * return TRUE if 'c' is a printable character |
| 958 | * Assume characters above 0x100 are printable (multi-byte), except for |
| 959 | * Unicode. |
| 960 | */ |
| 961 | int |
| 962 | vim_isprintc(c) |
| 963 | int c; |
| 964 | { |
| 965 | #ifdef FEAT_MBYTE |
| 966 | if (enc_utf8 && c >= 0x100) |
| 967 | return utf_printable(c); |
| 968 | #endif |
| 969 | return (c >= 0x100 || (c > 0 && (chartab[c] & CT_PRINT_CHAR))); |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head |
| 974 | * byte of a double-byte character. |
| 975 | */ |
| 976 | int |
| 977 | vim_isprintc_strict(c) |
| 978 | int c; |
| 979 | { |
| 980 | #ifdef FEAT_MBYTE |
| 981 | if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1) |
| 982 | return FALSE; |
| 983 | if (enc_utf8 && c >= 0x100) |
| 984 | return utf_printable(c); |
| 985 | #endif |
| 986 | return (c >= 0x100 || (c > 0 && (chartab[c] & CT_PRINT_CHAR))); |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * like chartabsize(), but also check for line breaks on the screen |
| 991 | */ |
| 992 | int |
| 993 | lbr_chartabsize(s, col) |
| 994 | unsigned char *s; |
| 995 | colnr_T col; |
| 996 | { |
| 997 | #ifdef FEAT_LINEBREAK |
| 998 | if (!curwin->w_p_lbr && *p_sbr == NUL) |
| 999 | { |
| 1000 | #endif |
| 1001 | #ifdef FEAT_MBYTE |
| 1002 | if (curwin->w_p_wrap) |
| 1003 | return win_nolbr_chartabsize(curwin, s, col, NULL); |
| 1004 | #endif |
| 1005 | RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col) |
| 1006 | #ifdef FEAT_LINEBREAK |
| 1007 | } |
| 1008 | return win_lbr_chartabsize(curwin, s, col, NULL); |
| 1009 | #endif |
| 1010 | } |
| 1011 | |
| 1012 | /* |
| 1013 | * Call lbr_chartabsize() and advance the pointer. |
| 1014 | */ |
| 1015 | int |
| 1016 | lbr_chartabsize_adv(s, col) |
| 1017 | char_u **s; |
| 1018 | colnr_T col; |
| 1019 | { |
| 1020 | int retval; |
| 1021 | |
| 1022 | retval = lbr_chartabsize(*s, col); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1023 | mb_ptr_adv(*s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | return retval; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * This function is used very often, keep it fast!!!! |
| 1029 | * |
| 1030 | * If "headp" not NULL, set *headp to the size of what we for 'showbreak' |
| 1031 | * string at start of line. Warning: *headp is only set if it's a non-zero |
| 1032 | * value, init to 0 before calling. |
| 1033 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1034 | int |
| 1035 | win_lbr_chartabsize(wp, s, col, headp) |
| 1036 | win_T *wp; |
| 1037 | char_u *s; |
| 1038 | colnr_T col; |
Bram Moolenaar | 0c094b9 | 2009-05-14 20:20:33 +0000 | [diff] [blame] | 1039 | int *headp UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1040 | { |
| 1041 | #ifdef FEAT_LINEBREAK |
| 1042 | int c; |
| 1043 | int size; |
| 1044 | colnr_T col2; |
| 1045 | colnr_T colmax; |
| 1046 | int added; |
| 1047 | # ifdef FEAT_MBYTE |
| 1048 | int mb_added = 0; |
| 1049 | # else |
| 1050 | # define mb_added 0 |
| 1051 | # endif |
| 1052 | int numberextra; |
| 1053 | char_u *ps; |
| 1054 | int tab_corr = (*s == TAB); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1055 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* |
| 1058 | * No 'linebreak' and 'showbreak': return quickly. |
| 1059 | */ |
| 1060 | if (!wp->w_p_lbr && *p_sbr == NUL) |
| 1061 | #endif |
| 1062 | { |
| 1063 | #ifdef FEAT_MBYTE |
| 1064 | if (wp->w_p_wrap) |
| 1065 | return win_nolbr_chartabsize(wp, s, col, headp); |
| 1066 | #endif |
| 1067 | RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col) |
| 1068 | } |
| 1069 | |
| 1070 | #ifdef FEAT_LINEBREAK |
| 1071 | /* |
| 1072 | * First get normal size, without 'linebreak' |
| 1073 | */ |
| 1074 | size = win_chartabsize(wp, s, col); |
| 1075 | c = *s; |
| 1076 | |
| 1077 | /* |
| 1078 | * If 'linebreak' set check at a blank before a non-blank if the line |
| 1079 | * needs a break here |
| 1080 | */ |
| 1081 | if (wp->w_p_lbr |
| 1082 | && vim_isbreak(c) |
| 1083 | && !vim_isbreak(s[1]) |
| 1084 | && !wp->w_p_list |
| 1085 | && wp->w_p_wrap |
| 1086 | # ifdef FEAT_VERTSPLIT |
| 1087 | && wp->w_width != 0 |
| 1088 | # endif |
| 1089 | ) |
| 1090 | { |
| 1091 | /* |
| 1092 | * Count all characters from first non-blank after a blank up to next |
| 1093 | * non-blank after a blank. |
| 1094 | */ |
| 1095 | numberextra = win_col_off(wp); |
| 1096 | col2 = col; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1097 | colmax = (colnr_T)(W_WIDTH(wp) - numberextra); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | if (col >= colmax) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1099 | { |
| 1100 | n = colmax + win_col_off2(wp); |
| 1101 | if (n > 0) |
| 1102 | colmax += (((col - colmax) / n) + 1) * n; |
| 1103 | } |
| 1104 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | for (;;) |
| 1106 | { |
| 1107 | ps = s; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1108 | mb_ptr_adv(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1109 | c = *s; |
| 1110 | if (!(c != NUL |
| 1111 | && (vim_isbreak(c) |
| 1112 | || (!vim_isbreak(c) |
| 1113 | && (col2 == col || !vim_isbreak(*ps)))))) |
| 1114 | break; |
| 1115 | |
| 1116 | col2 += win_chartabsize(wp, s, col2); |
| 1117 | if (col2 >= colmax) /* doesn't fit */ |
| 1118 | { |
| 1119 | size = colmax - col; |
| 1120 | tab_corr = FALSE; |
| 1121 | break; |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | # ifdef FEAT_MBYTE |
| 1126 | else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1 |
| 1127 | && wp->w_p_wrap && in_win_border(wp, col)) |
| 1128 | { |
| 1129 | ++size; /* Count the ">" in the last column. */ |
| 1130 | mb_added = 1; |
| 1131 | } |
| 1132 | # endif |
| 1133 | |
| 1134 | /* |
| 1135 | * May have to add something for 'showbreak' string at start of line |
| 1136 | * Set *headp to the size of what we add. |
| 1137 | */ |
| 1138 | added = 0; |
| 1139 | if (*p_sbr != NUL && wp->w_p_wrap && col != 0) |
| 1140 | { |
| 1141 | numberextra = win_col_off(wp); |
| 1142 | col += numberextra + mb_added; |
| 1143 | if (col >= (colnr_T)W_WIDTH(wp)) |
| 1144 | { |
| 1145 | col -= W_WIDTH(wp); |
| 1146 | numberextra = W_WIDTH(wp) - (numberextra - win_col_off2(wp)); |
| 1147 | if (numberextra > 0) |
| 1148 | col = col % numberextra; |
| 1149 | } |
| 1150 | if (col == 0 || col + size > (colnr_T)W_WIDTH(wp)) |
| 1151 | { |
| 1152 | added = vim_strsize(p_sbr); |
| 1153 | if (tab_corr) |
| 1154 | size += (added / wp->w_buffer->b_p_ts) * wp->w_buffer->b_p_ts; |
| 1155 | else |
| 1156 | size += added; |
| 1157 | if (col != 0) |
| 1158 | added = 0; |
| 1159 | } |
| 1160 | } |
| 1161 | if (headp != NULL) |
| 1162 | *headp = added + mb_added; |
| 1163 | return size; |
| 1164 | #endif |
| 1165 | } |
| 1166 | |
| 1167 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 1168 | /* |
| 1169 | * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and |
| 1170 | * 'wrap' is on. This means we need to check for a double-byte character that |
| 1171 | * doesn't fit at the end of the screen line. |
| 1172 | */ |
| 1173 | static int |
| 1174 | win_nolbr_chartabsize(wp, s, col, headp) |
| 1175 | win_T *wp; |
| 1176 | char_u *s; |
| 1177 | colnr_T col; |
| 1178 | int *headp; |
| 1179 | { |
| 1180 | int n; |
| 1181 | |
| 1182 | if (*s == TAB && (!wp->w_p_list || lcs_tab1)) |
| 1183 | { |
| 1184 | n = wp->w_buffer->b_p_ts; |
| 1185 | return (int)(n - (col % n)); |
| 1186 | } |
| 1187 | n = ptr2cells(s); |
| 1188 | /* Add one cell for a double-width character in the last column of the |
| 1189 | * window, displayed with a ">". */ |
| 1190 | if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col)) |
| 1191 | { |
| 1192 | if (headp != NULL) |
| 1193 | *headp = 1; |
| 1194 | return 3; |
| 1195 | } |
| 1196 | return n; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Return TRUE if virtual column "vcol" is in the rightmost column of window |
| 1201 | * "wp". |
| 1202 | */ |
| 1203 | int |
| 1204 | in_win_border(wp, vcol) |
| 1205 | win_T *wp; |
| 1206 | colnr_T vcol; |
| 1207 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1208 | int width1; /* width of first line (after line number) */ |
| 1209 | int width2; /* width of further lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1210 | |
| 1211 | #ifdef FEAT_VERTSPLIT |
| 1212 | if (wp->w_width == 0) /* there is no border */ |
| 1213 | return FALSE; |
| 1214 | #endif |
| 1215 | width1 = W_WIDTH(wp) - win_col_off(wp); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1216 | if ((int)vcol < width1 - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | return FALSE; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1218 | if ((int)vcol == width1 - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1219 | return TRUE; |
| 1220 | width2 = width1 + win_col_off2(wp); |
| 1221 | return ((vcol - width1) % width2 == width2 - 1); |
| 1222 | } |
| 1223 | #endif /* FEAT_MBYTE */ |
| 1224 | |
| 1225 | /* |
| 1226 | * Get virtual column number of pos. |
| 1227 | * start: on the first position of this character (TAB, ctrl) |
| 1228 | * cursor: where the cursor is on this character (first char, except for TAB) |
| 1229 | * end: on the last position of this character (TAB, ctrl) |
| 1230 | * |
| 1231 | * This is used very often, keep it fast! |
| 1232 | */ |
| 1233 | void |
| 1234 | getvcol(wp, pos, start, cursor, end) |
| 1235 | win_T *wp; |
| 1236 | pos_T *pos; |
| 1237 | colnr_T *start; |
| 1238 | colnr_T *cursor; |
| 1239 | colnr_T *end; |
| 1240 | { |
| 1241 | colnr_T vcol; |
| 1242 | char_u *ptr; /* points to current char */ |
| 1243 | char_u *posptr; /* points to char at pos->col */ |
| 1244 | int incr; |
| 1245 | int head; |
| 1246 | int ts = wp->w_buffer->b_p_ts; |
| 1247 | int c; |
| 1248 | |
| 1249 | vcol = 0; |
| 1250 | ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); |
| 1251 | posptr = ptr + pos->col; |
| 1252 | |
| 1253 | /* |
| 1254 | * This function is used very often, do some speed optimizations. |
| 1255 | * When 'list', 'linebreak' and 'showbreak' are not set use a simple loop. |
| 1256 | * Also use this when 'list' is set but tabs take their normal size. |
| 1257 | */ |
| 1258 | if ((!wp->w_p_list || lcs_tab1 != NUL) |
| 1259 | #ifdef FEAT_LINEBREAK |
| 1260 | && !wp->w_p_lbr && *p_sbr == NUL |
| 1261 | #endif |
| 1262 | ) |
| 1263 | { |
| 1264 | #ifndef FEAT_MBYTE |
| 1265 | head = 0; |
| 1266 | #endif |
| 1267 | for (;;) |
| 1268 | { |
| 1269 | #ifdef FEAT_MBYTE |
| 1270 | head = 0; |
| 1271 | #endif |
| 1272 | c = *ptr; |
| 1273 | /* make sure we don't go past the end of the line */ |
| 1274 | if (c == NUL) |
| 1275 | { |
| 1276 | incr = 1; /* NUL at end of line only takes one column */ |
| 1277 | break; |
| 1278 | } |
| 1279 | /* A tab gets expanded, depending on the current column */ |
| 1280 | if (c == TAB) |
| 1281 | incr = ts - (vcol % ts); |
| 1282 | else |
| 1283 | { |
| 1284 | #ifdef FEAT_MBYTE |
| 1285 | if (has_mbyte) |
| 1286 | { |
| 1287 | /* For utf-8, if the byte is >= 0x80, need to look at |
| 1288 | * further bytes to find the cell width. */ |
| 1289 | if (enc_utf8 && c >= 0x80) |
| 1290 | incr = utf_ptr2cells(ptr); |
| 1291 | else |
| 1292 | incr = CHARSIZE(c); |
| 1293 | |
| 1294 | /* If a double-cell char doesn't fit at the end of a line |
| 1295 | * it wraps to the next line, it's like this char is three |
| 1296 | * cells wide. */ |
Bram Moolenaar | 9c33a7c | 2008-02-20 13:59:32 +0000 | [diff] [blame] | 1297 | if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1 |
| 1298 | && in_win_border(wp, vcol)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | { |
| 1300 | ++incr; |
| 1301 | head = 1; |
| 1302 | } |
| 1303 | } |
| 1304 | else |
| 1305 | #endif |
| 1306 | incr = CHARSIZE(c); |
| 1307 | } |
| 1308 | |
| 1309 | if (ptr >= posptr) /* character at pos->col */ |
| 1310 | break; |
| 1311 | |
| 1312 | vcol += incr; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1313 | mb_ptr_adv(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | else |
| 1317 | { |
| 1318 | for (;;) |
| 1319 | { |
| 1320 | /* A tab gets expanded, depending on the current column */ |
| 1321 | head = 0; |
| 1322 | incr = win_lbr_chartabsize(wp, ptr, vcol, &head); |
| 1323 | /* make sure we don't go past the end of the line */ |
| 1324 | if (*ptr == NUL) |
| 1325 | { |
| 1326 | incr = 1; /* NUL at end of line only takes one column */ |
| 1327 | break; |
| 1328 | } |
| 1329 | |
| 1330 | if (ptr >= posptr) /* character at pos->col */ |
| 1331 | break; |
| 1332 | |
| 1333 | vcol += incr; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1334 | mb_ptr_adv(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | if (start != NULL) |
| 1338 | *start = vcol + head; |
| 1339 | if (end != NULL) |
| 1340 | *end = vcol + incr - 1; |
| 1341 | if (cursor != NULL) |
| 1342 | { |
| 1343 | if (*ptr == TAB |
| 1344 | && (State & NORMAL) |
| 1345 | && !wp->w_p_list |
| 1346 | && !virtual_active() |
| 1347 | #ifdef FEAT_VISUAL |
| 1348 | && !(VIsual_active |
| 1349 | && (*p_sel == 'e' || ltoreq(*pos, VIsual))) |
| 1350 | #endif |
| 1351 | ) |
| 1352 | *cursor = vcol + incr - 1; /* cursor at end */ |
| 1353 | else |
| 1354 | *cursor = vcol + head; /* cursor at start */ |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | * Get virtual cursor column in the current window, pretending 'list' is off. |
| 1360 | */ |
| 1361 | colnr_T |
| 1362 | getvcol_nolist(posp) |
| 1363 | pos_T *posp; |
| 1364 | { |
| 1365 | int list_save = curwin->w_p_list; |
| 1366 | colnr_T vcol; |
| 1367 | |
| 1368 | curwin->w_p_list = FALSE; |
| 1369 | getvcol(curwin, posp, NULL, &vcol, NULL); |
| 1370 | curwin->w_p_list = list_save; |
| 1371 | return vcol; |
| 1372 | } |
| 1373 | |
| 1374 | #if defined(FEAT_VIRTUALEDIT) || defined(PROTO) |
| 1375 | /* |
| 1376 | * Get virtual column in virtual mode. |
| 1377 | */ |
| 1378 | void |
| 1379 | getvvcol(wp, pos, start, cursor, end) |
| 1380 | win_T *wp; |
| 1381 | pos_T *pos; |
| 1382 | colnr_T *start; |
| 1383 | colnr_T *cursor; |
| 1384 | colnr_T *end; |
| 1385 | { |
| 1386 | colnr_T col; |
| 1387 | colnr_T coladd; |
| 1388 | colnr_T endadd; |
| 1389 | # ifdef FEAT_MBYTE |
| 1390 | char_u *ptr; |
| 1391 | # endif |
| 1392 | |
| 1393 | if (virtual_active()) |
| 1394 | { |
| 1395 | /* For virtual mode, only want one value */ |
| 1396 | getvcol(wp, pos, &col, NULL, NULL); |
| 1397 | |
| 1398 | coladd = pos->coladd; |
| 1399 | endadd = 0; |
| 1400 | # ifdef FEAT_MBYTE |
| 1401 | /* Cannot put the cursor on part of a wide character. */ |
| 1402 | ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1403 | if (pos->col < (colnr_T)STRLEN(ptr)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1404 | { |
| 1405 | int c = (*mb_ptr2char)(ptr + pos->col); |
| 1406 | |
| 1407 | if (c != TAB && vim_isprintc(c)) |
| 1408 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1409 | endadd = (colnr_T)(char2cells(c) - 1); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1410 | if (coladd > endadd) /* past end of line */ |
| 1411 | endadd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1412 | else |
| 1413 | coladd = 0; |
| 1414 | } |
| 1415 | } |
| 1416 | # endif |
| 1417 | col += coladd; |
| 1418 | if (start != NULL) |
| 1419 | *start = col; |
| 1420 | if (cursor != NULL) |
| 1421 | *cursor = col; |
| 1422 | if (end != NULL) |
| 1423 | *end = col + endadd; |
| 1424 | } |
| 1425 | else |
| 1426 | getvcol(wp, pos, start, cursor, end); |
| 1427 | } |
| 1428 | #endif |
| 1429 | |
| 1430 | #if defined(FEAT_VISUAL) || defined(PROTO) |
| 1431 | /* |
| 1432 | * Get the leftmost and rightmost virtual column of pos1 and pos2. |
| 1433 | * Used for Visual block mode. |
| 1434 | */ |
| 1435 | void |
| 1436 | getvcols(wp, pos1, pos2, left, right) |
| 1437 | win_T *wp; |
| 1438 | pos_T *pos1, *pos2; |
| 1439 | colnr_T *left, *right; |
| 1440 | { |
| 1441 | colnr_T from1, from2, to1, to2; |
| 1442 | |
| 1443 | if (ltp(pos1, pos2)) |
| 1444 | { |
| 1445 | getvvcol(wp, pos1, &from1, NULL, &to1); |
| 1446 | getvvcol(wp, pos2, &from2, NULL, &to2); |
| 1447 | } |
| 1448 | else |
| 1449 | { |
| 1450 | getvvcol(wp, pos2, &from1, NULL, &to1); |
| 1451 | getvvcol(wp, pos1, &from2, NULL, &to2); |
| 1452 | } |
| 1453 | if (from2 < from1) |
| 1454 | *left = from2; |
| 1455 | else |
| 1456 | *left = from1; |
| 1457 | if (to2 > to1) |
| 1458 | { |
| 1459 | if (*p_sel == 'e' && from2 - 1 >= to1) |
| 1460 | *right = from2 - 1; |
| 1461 | else |
| 1462 | *right = to2; |
| 1463 | } |
| 1464 | else |
| 1465 | *right = to1; |
| 1466 | } |
| 1467 | #endif |
| 1468 | |
| 1469 | /* |
| 1470 | * skipwhite: skip over ' ' and '\t'. |
| 1471 | */ |
| 1472 | char_u * |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1473 | skipwhite(q) |
| 1474 | char_u *q; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1475 | { |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1476 | char_u *p = q; |
| 1477 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1478 | while (vim_iswhite(*p)) /* skip to next non-white */ |
| 1479 | ++p; |
| 1480 | return p; |
| 1481 | } |
| 1482 | |
| 1483 | /* |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1484 | * skip over digits |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1485 | */ |
| 1486 | char_u * |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1487 | skipdigits(q) |
| 1488 | char_u *q; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1489 | { |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1490 | char_u *p = q; |
| 1491 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1492 | while (VIM_ISDIGIT(*p)) /* skip to next non-digit */ |
| 1493 | ++p; |
| 1494 | return p; |
| 1495 | } |
| 1496 | |
Bram Moolenaar | c4956c8 | 2006-03-12 21:58:43 +0000 | [diff] [blame] | 1497 | #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO) |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 1498 | /* |
| 1499 | * skip over digits and hex characters |
| 1500 | */ |
| 1501 | char_u * |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1502 | skiphex(q) |
| 1503 | char_u *q; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 1504 | { |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1505 | char_u *p = q; |
| 1506 | |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 1507 | while (vim_isxdigit(*p)) /* skip to next non-digit */ |
| 1508 | ++p; |
| 1509 | return p; |
| 1510 | } |
| 1511 | #endif |
| 1512 | |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1513 | #if defined(FEAT_EX_EXTRA) || defined(PROTO) |
| 1514 | /* |
| 1515 | * skip to digit (or NUL after the string) |
| 1516 | */ |
| 1517 | char_u * |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1518 | skiptodigit(q) |
| 1519 | char_u *q; |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1520 | { |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1521 | char_u *p = q; |
| 1522 | |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1523 | while (*p != NUL && !VIM_ISDIGIT(*p)) /* skip to next digit */ |
| 1524 | ++p; |
| 1525 | return p; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * skip to hex character (or NUL after the string) |
| 1530 | */ |
| 1531 | char_u * |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1532 | skiptohex(q) |
| 1533 | char_u *q; |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1534 | { |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 1535 | char_u *p = q; |
| 1536 | |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1537 | while (*p != NUL && !vim_isxdigit(*p)) /* skip to next digit */ |
| 1538 | ++p; |
| 1539 | return p; |
| 1540 | } |
| 1541 | #endif |
| 1542 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1543 | /* |
| 1544 | * Variant of isdigit() that can handle characters > 0x100. |
| 1545 | * We don't use isdigit() here, because on some systems it also considers |
| 1546 | * superscript 1 to be a digit. |
| 1547 | * Use the VIM_ISDIGIT() macro for simple arguments. |
| 1548 | */ |
| 1549 | int |
| 1550 | vim_isdigit(c) |
| 1551 | int c; |
| 1552 | { |
| 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 |
| 1562 | vim_isxdigit(c) |
| 1563 | int c; |
| 1564 | { |
| 1565 | return (c >= '0' && c <= '9') |
| 1566 | || (c >= 'a' && c <= 'f') |
| 1567 | || (c >= 'A' && c <= 'F'); |
| 1568 | } |
| 1569 | |
Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1570 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 1571 | /* |
| 1572 | * Vim's own character class functions. These exist because many library |
| 1573 | * islower()/toupper() etc. do not work properly: they crash when used with |
| 1574 | * invalid values or can't handle latin1 when the locale is C. |
| 1575 | * Speed is most important here. |
| 1576 | */ |
| 1577 | #define LATIN1LOWER 'l' |
| 1578 | #define LATIN1UPPER 'U' |
| 1579 | |
| 1580 | /* !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]%_'abcdefghijklmnopqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ */ |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 1581 | static char_u latin1flags[257] = " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll"; |
| 1582 | static char_u latin1upper[257] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~
¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ÷ØÙÚÛÜÝÞÿ"; |
| 1583 | static char_u latin1lower[257] = " !\"#$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~
¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿àáâãäåæçèéêëìíîïðñòóôõö×øùúûüýþßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"; |
Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1584 | |
| 1585 | int |
| 1586 | vim_islower(c) |
| 1587 | int c; |
| 1588 | { |
| 1589 | if (c <= '@') |
| 1590 | return FALSE; |
| 1591 | if (c >= 0x80) |
| 1592 | { |
| 1593 | if (enc_utf8) |
| 1594 | return utf_islower(c); |
| 1595 | if (c >= 0x100) |
| 1596 | { |
| 1597 | #ifdef HAVE_ISWLOWER |
| 1598 | if (has_mbyte) |
| 1599 | return iswlower(c); |
| 1600 | #endif |
| 1601 | /* islower() can't handle these chars and may crash */ |
| 1602 | return FALSE; |
| 1603 | } |
| 1604 | if (enc_latin1like) |
| 1605 | return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER; |
| 1606 | } |
| 1607 | return islower(c); |
| 1608 | } |
| 1609 | |
| 1610 | int |
| 1611 | vim_isupper(c) |
| 1612 | int c; |
| 1613 | { |
| 1614 | if (c <= '@') |
| 1615 | return FALSE; |
| 1616 | if (c >= 0x80) |
| 1617 | { |
| 1618 | if (enc_utf8) |
| 1619 | return utf_isupper(c); |
| 1620 | if (c >= 0x100) |
| 1621 | { |
| 1622 | #ifdef HAVE_ISWUPPER |
| 1623 | if (has_mbyte) |
| 1624 | return iswupper(c); |
| 1625 | #endif |
| 1626 | /* islower() can't handle these chars and may crash */ |
| 1627 | return FALSE; |
| 1628 | } |
| 1629 | if (enc_latin1like) |
| 1630 | return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER; |
| 1631 | } |
| 1632 | return isupper(c); |
| 1633 | } |
| 1634 | |
| 1635 | int |
| 1636 | vim_toupper(c) |
| 1637 | int c; |
| 1638 | { |
| 1639 | if (c <= '@') |
| 1640 | return c; |
| 1641 | if (c >= 0x80) |
| 1642 | { |
| 1643 | if (enc_utf8) |
| 1644 | return utf_toupper(c); |
| 1645 | if (c >= 0x100) |
| 1646 | { |
| 1647 | #ifdef HAVE_TOWUPPER |
| 1648 | if (has_mbyte) |
| 1649 | return towupper(c); |
| 1650 | #endif |
| 1651 | /* toupper() can't handle these chars and may crash */ |
| 1652 | return c; |
| 1653 | } |
| 1654 | if (enc_latin1like) |
| 1655 | return latin1upper[c]; |
| 1656 | } |
| 1657 | return TOUPPER_LOC(c); |
| 1658 | } |
| 1659 | |
| 1660 | int |
| 1661 | vim_tolower(c) |
| 1662 | int c; |
| 1663 | { |
| 1664 | if (c <= '@') |
| 1665 | return c; |
| 1666 | if (c >= 0x80) |
| 1667 | { |
| 1668 | if (enc_utf8) |
| 1669 | return utf_tolower(c); |
| 1670 | if (c >= 0x100) |
| 1671 | { |
| 1672 | #ifdef HAVE_TOWLOWER |
| 1673 | if (has_mbyte) |
| 1674 | return towlower(c); |
| 1675 | #endif |
| 1676 | /* tolower() can't handle these chars and may crash */ |
| 1677 | return c; |
| 1678 | } |
| 1679 | if (enc_latin1like) |
| 1680 | return latin1lower[c]; |
| 1681 | } |
| 1682 | return TOLOWER_LOC(c); |
| 1683 | } |
| 1684 | #endif |
| 1685 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1686 | /* |
| 1687 | * skiptowhite: skip over text until ' ' or '\t' or NUL. |
| 1688 | */ |
| 1689 | char_u * |
| 1690 | skiptowhite(p) |
| 1691 | char_u *p; |
| 1692 | { |
| 1693 | while (*p != ' ' && *p != '\t' && *p != NUL) |
| 1694 | ++p; |
| 1695 | return p; |
| 1696 | } |
| 1697 | |
| 1698 | #if defined(FEAT_LISTCMDS) || defined(FEAT_SIGNS) || defined(FEAT_SNIFF) \ |
| 1699 | || defined(PROTO) |
| 1700 | /* |
| 1701 | * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars |
| 1702 | */ |
| 1703 | char_u * |
| 1704 | skiptowhite_esc(p) |
| 1705 | char_u *p; |
| 1706 | { |
| 1707 | while (*p != ' ' && *p != '\t' && *p != NUL) |
| 1708 | { |
| 1709 | if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL) |
| 1710 | ++p; |
| 1711 | ++p; |
| 1712 | } |
| 1713 | return p; |
| 1714 | } |
| 1715 | #endif |
| 1716 | |
| 1717 | /* |
| 1718 | * Getdigits: Get a number from a string and skip over it. |
| 1719 | * Note: the argument is a pointer to a char_u pointer! |
| 1720 | */ |
| 1721 | long |
| 1722 | getdigits(pp) |
| 1723 | char_u **pp; |
| 1724 | { |
| 1725 | char_u *p; |
| 1726 | long retval; |
| 1727 | |
| 1728 | p = *pp; |
| 1729 | retval = atol((char *)p); |
| 1730 | if (*p == '-') /* skip negative sign */ |
| 1731 | ++p; |
| 1732 | p = skipdigits(p); /* skip to next non-digit */ |
| 1733 | *pp = p; |
| 1734 | return retval; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
| 1738 | * Return TRUE if "lbuf" is empty or only contains blanks. |
| 1739 | */ |
| 1740 | int |
| 1741 | vim_isblankline(lbuf) |
| 1742 | char_u *lbuf; |
| 1743 | { |
| 1744 | char_u *p; |
| 1745 | |
| 1746 | p = skipwhite(lbuf); |
| 1747 | return (*p == NUL || *p == '\r' || *p == '\n'); |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * Convert a string into a long and/or unsigned long, taking care of |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1752 | * hexadecimal and octal numbers. Accepts a '-' sign. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1753 | * If "hexp" is not NULL, returns a flag to indicate the type of the number: |
| 1754 | * 0 decimal |
| 1755 | * '0' octal |
| 1756 | * 'X' hex |
| 1757 | * 'x' hex |
| 1758 | * If "len" is not NULL, the length of the number in characters is returned. |
| 1759 | * If "nptr" is not NULL, the signed result is returned in it. |
| 1760 | * If "unptr" is not NULL, the unsigned result is returned in it. |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1761 | * If "dooct" is non-zero recognize octal numbers, when > 1 always assume |
| 1762 | * octal number. |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1763 | * If "dohex" is non-zero recognize hex numbers, when > 1 always assume |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1764 | * hex number. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1765 | */ |
| 1766 | void |
| 1767 | vim_str2nr(start, hexp, len, dooct, dohex, nptr, unptr) |
| 1768 | char_u *start; |
| 1769 | int *hexp; /* return: type of number 0 = decimal, 'x' |
| 1770 | or 'X' is hex, '0' = octal */ |
| 1771 | int *len; /* return: detected length of number */ |
| 1772 | int dooct; /* recognize octal number */ |
| 1773 | int dohex; /* recognize hex number */ |
| 1774 | long *nptr; /* return: signed result */ |
| 1775 | unsigned long *unptr; /* return: unsigned result */ |
| 1776 | { |
| 1777 | char_u *ptr = start; |
| 1778 | int hex = 0; /* default is decimal */ |
| 1779 | int negative = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1780 | unsigned long un = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1781 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1782 | |
| 1783 | if (ptr[0] == '-') |
| 1784 | { |
| 1785 | negative = TRUE; |
| 1786 | ++ptr; |
| 1787 | } |
| 1788 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1789 | /* Recognize hex and octal. */ |
| 1790 | if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1791 | { |
| 1792 | hex = ptr[1]; |
| 1793 | if (dohex && (hex == 'X' || hex == 'x') && vim_isxdigit(ptr[2])) |
| 1794 | ptr += 2; /* hexadecimal */ |
| 1795 | else |
| 1796 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1797 | hex = 0; /* default is decimal */ |
| 1798 | if (dooct) |
| 1799 | { |
| 1800 | /* Don't interpret "0", "08" or "0129" as octal. */ |
| 1801 | for (n = 1; VIM_ISDIGIT(ptr[n]); ++n) |
| 1802 | { |
| 1803 | if (ptr[n] > '7') |
| 1804 | { |
| 1805 | hex = 0; /* can't be octal */ |
| 1806 | break; |
| 1807 | } |
| 1808 | if (ptr[n] > '0') |
| 1809 | hex = '0'; /* assume octal */ |
| 1810 | } |
| 1811 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | /* |
| 1816 | * Do the string-to-numeric conversion "manually" to avoid sscanf quirks. |
| 1817 | */ |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1818 | if (hex == '0' || dooct > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1819 | { |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1820 | /* octal */ |
| 1821 | while ('0' <= *ptr && *ptr <= '7') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | { |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1823 | un = 8 * un + (unsigned long)(*ptr - '0'); |
| 1824 | ++ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1825 | } |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1826 | } |
| 1827 | else if (hex != 0 || dohex > 1) |
| 1828 | { |
| 1829 | /* hex */ |
| 1830 | while (vim_isxdigit(*ptr)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1831 | { |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1832 | un = 16 * un + (unsigned long)hex2nr(*ptr); |
| 1833 | ++ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | else |
| 1837 | { |
| 1838 | /* decimal */ |
| 1839 | while (VIM_ISDIGIT(*ptr)) |
| 1840 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1841 | un = 10 * un + (unsigned long)(*ptr - '0'); |
| 1842 | ++ptr; |
| 1843 | } |
| 1844 | } |
| 1845 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1846 | if (hexp != NULL) |
| 1847 | *hexp = hex; |
| 1848 | if (len != NULL) |
| 1849 | *len = (int)(ptr - start); |
| 1850 | if (nptr != NULL) |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1851 | { |
| 1852 | if (negative) /* account for leading '-' for decimal numbers */ |
| 1853 | *nptr = -(long)un; |
| 1854 | else |
| 1855 | *nptr = (long)un; |
| 1856 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1857 | if (unptr != NULL) |
| 1858 | *unptr = un; |
| 1859 | } |
| 1860 | |
| 1861 | /* |
| 1862 | * Return the value of a single hex character. |
| 1863 | * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'. |
| 1864 | */ |
| 1865 | int |
| 1866 | hex2nr(c) |
| 1867 | int c; |
| 1868 | { |
| 1869 | if (c >= 'a' && c <= 'f') |
| 1870 | return c - 'a' + 10; |
| 1871 | if (c >= 'A' && c <= 'F') |
| 1872 | return c - 'A' + 10; |
| 1873 | return c - '0'; |
| 1874 | } |
| 1875 | |
| 1876 | #if defined(FEAT_TERMRESPONSE) \ |
| 1877 | || (defined(FEAT_GUI_GTK) && defined(FEAT_WINDOWS)) || defined(PROTO) |
| 1878 | /* |
| 1879 | * Convert two hex characters to a byte. |
| 1880 | * Return -1 if one of the characters is not hex. |
| 1881 | */ |
| 1882 | int |
| 1883 | hexhex2nr(p) |
| 1884 | char_u *p; |
| 1885 | { |
| 1886 | if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1])) |
| 1887 | return -1; |
| 1888 | return (hex2nr(p[0]) << 4) + hex2nr(p[1]); |
| 1889 | } |
| 1890 | #endif |
| 1891 | |
| 1892 | /* |
| 1893 | * Return TRUE if "str" starts with a backslash that should be removed. |
| 1894 | * For MS-DOS, WIN32 and OS/2 this is only done when the character after the |
| 1895 | * backslash is not a normal file name character. |
| 1896 | * '$' is a valid file name character, we don't remove the backslash before |
| 1897 | * it. This means it is not possible to use an environment variable after a |
| 1898 | * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works. |
| 1899 | * Although "\ name" is valid, the backslash in "Program\ files" must be |
| 1900 | * removed. Assume a file name doesn't start with a space. |
| 1901 | * For multi-byte names, never remove a backslash before a non-ascii |
| 1902 | * character, assume that all multi-byte characters are valid file name |
| 1903 | * characters. |
| 1904 | */ |
| 1905 | int |
| 1906 | rem_backslash(str) |
| 1907 | char_u *str; |
| 1908 | { |
| 1909 | #ifdef BACKSLASH_IN_FILENAME |
| 1910 | return (str[0] == '\\' |
| 1911 | # ifdef FEAT_MBYTE |
| 1912 | && str[1] < 0x80 |
| 1913 | # endif |
| 1914 | && (str[1] == ' ' |
| 1915 | || (str[1] != NUL |
| 1916 | && str[1] != '*' |
| 1917 | && str[1] != '?' |
| 1918 | && !vim_isfilec(str[1])))); |
| 1919 | #else |
| 1920 | return (str[0] == '\\' && str[1] != NUL); |
| 1921 | #endif |
| 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * Halve the number of backslashes in a file name argument. |
| 1926 | * For MS-DOS we only do this if the character after the backslash |
| 1927 | * is not a normal file character. |
| 1928 | */ |
| 1929 | void |
| 1930 | backslash_halve(p) |
| 1931 | char_u *p; |
| 1932 | { |
| 1933 | for ( ; *p; ++p) |
| 1934 | if (rem_backslash(p)) |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1935 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | * backslash_halve() plus save the result in allocated memory. |
| 1940 | */ |
| 1941 | char_u * |
| 1942 | backslash_halve_save(p) |
| 1943 | char_u *p; |
| 1944 | { |
| 1945 | char_u *res; |
| 1946 | |
| 1947 | res = vim_strsave(p); |
| 1948 | if (res == NULL) |
| 1949 | return p; |
| 1950 | backslash_halve(res); |
| 1951 | return res; |
| 1952 | } |
| 1953 | |
| 1954 | #if (defined(EBCDIC) && defined(FEAT_POSTSCRIPT)) || defined(PROTO) |
| 1955 | /* |
| 1956 | * Table for EBCDIC to ASCII conversion unashamedly taken from xxd.c! |
| 1957 | * The first 64 entries have been added to map control characters defined in |
| 1958 | * ascii.h |
| 1959 | */ |
| 1960 | static char_u ebcdic2ascii_tab[256] = |
| 1961 | { |
| 1962 | 0000, 0001, 0002, 0003, 0004, 0011, 0006, 0177, |
| 1963 | 0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017, |
| 1964 | 0020, 0021, 0022, 0023, 0024, 0012, 0010, 0027, |
| 1965 | 0030, 0031, 0032, 0033, 0033, 0035, 0036, 0037, |
| 1966 | 0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047, |
| 1967 | 0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057, |
| 1968 | 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, |
| 1969 | 0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077, |
| 1970 | 0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246, |
| 1971 | 0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174, |
| 1972 | 0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257, |
| 1973 | 0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176, |
| 1974 | 0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267, |
| 1975 | 0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077, |
| 1976 | 0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301, |
| 1977 | 0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042, |
| 1978 | 0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147, |
| 1979 | 0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311, |
| 1980 | 0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160, |
| 1981 | 0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320, |
| 1982 | 0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170, |
| 1983 | 0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327, |
| 1984 | 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, |
| 1985 | 0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347, |
| 1986 | 0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107, |
| 1987 | 0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355, |
| 1988 | 0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120, |
| 1989 | 0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363, |
| 1990 | 0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130, |
| 1991 | 0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371, |
| 1992 | 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, |
| 1993 | 0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377 |
| 1994 | }; |
| 1995 | |
| 1996 | /* |
| 1997 | * Convert a buffer worth of characters from EBCDIC to ASCII. Only useful if |
| 1998 | * wanting 7-bit ASCII characters out the other end. |
| 1999 | */ |
| 2000 | void |
| 2001 | ebcdic2ascii(buffer, len) |
| 2002 | char_u *buffer; |
| 2003 | int len; |
| 2004 | { |
| 2005 | int i; |
| 2006 | |
| 2007 | for (i = 0; i < len; i++) |
| 2008 | buffer[i] = ebcdic2ascii_tab[buffer[i]]; |
| 2009 | } |
| 2010 | #endif |