Bram Moolenaar | 4b47162 | 2019-01-31 13:48:09 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 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 | /* |
| 11 | * indent.c: Indentation related functions |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) |
| 17 | |
| 18 | /* |
| 19 | * Return TRUE if the string "line" starts with a word from 'cinwords'. |
| 20 | */ |
| 21 | int |
| 22 | cin_is_cinword(char_u *line) |
| 23 | { |
| 24 | char_u *cinw; |
| 25 | char_u *cinw_buf; |
| 26 | int cinw_len; |
| 27 | int retval = FALSE; |
| 28 | int len; |
| 29 | |
| 30 | cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; |
| 31 | cinw_buf = alloc((unsigned)cinw_len); |
| 32 | if (cinw_buf != NULL) |
| 33 | { |
| 34 | line = skipwhite(line); |
| 35 | for (cinw = curbuf->b_p_cinw; *cinw; ) |
| 36 | { |
| 37 | len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); |
| 38 | if (STRNCMP(line, cinw_buf, len) == 0 |
| 39 | && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) |
| 40 | { |
| 41 | retval = TRUE; |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | vim_free(cinw_buf); |
| 46 | } |
| 47 | return retval; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) |
| 52 | |
| 53 | static char_u *skip_string(char_u *p); |
| 54 | static pos_T *find_start_rawstring(int ind_maxcomment); |
| 55 | |
| 56 | /* |
| 57 | * Find the start of a comment, not knowing if we are in a comment right now. |
| 58 | * Search starts at w_cursor.lnum and goes backwards. |
| 59 | * Return NULL when not inside a comment. |
| 60 | */ |
| 61 | static pos_T * |
| 62 | ind_find_start_comment(void) /* XXX */ |
| 63 | { |
| 64 | return find_start_comment(curbuf->b_ind_maxcomment); |
| 65 | } |
| 66 | |
| 67 | pos_T * |
| 68 | find_start_comment(int ind_maxcomment) /* XXX */ |
| 69 | { |
| 70 | pos_T *pos; |
| 71 | char_u *line; |
| 72 | char_u *p; |
| 73 | int cur_maxcomment = ind_maxcomment; |
| 74 | |
| 75 | for (;;) |
| 76 | { |
| 77 | pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); |
| 78 | if (pos == NULL) |
| 79 | break; |
| 80 | |
| 81 | /* |
| 82 | * Check if the comment start we found is inside a string. |
| 83 | * If it is then restrict the search to below this line and try again. |
| 84 | */ |
| 85 | line = ml_get(pos->lnum); |
| 86 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
| 87 | p = skip_string(p); |
| 88 | if ((colnr_T)(p - line) <= pos->col) |
| 89 | break; |
| 90 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 91 | if (cur_maxcomment <= 0) |
| 92 | { |
| 93 | pos = NULL; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | return pos; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Find the start of a comment or raw string, not knowing if we are in a |
| 102 | * comment or raw string right now. |
| 103 | * Search starts at w_cursor.lnum and goes backwards. |
| 104 | * If is_raw is given and returns start of raw_string, sets it to true. |
| 105 | * Return NULL when not inside a comment or raw string. |
| 106 | * "CORS" -> Comment Or Raw String |
| 107 | */ |
| 108 | static pos_T * |
| 109 | ind_find_start_CORS(linenr_T *is_raw) /* XXX */ |
| 110 | { |
| 111 | static pos_T comment_pos_copy; |
| 112 | pos_T *comment_pos; |
| 113 | pos_T *rs_pos; |
| 114 | |
| 115 | comment_pos = find_start_comment(curbuf->b_ind_maxcomment); |
| 116 | if (comment_pos != NULL) |
| 117 | { |
| 118 | /* Need to make a copy of the static pos in findmatchlimit(), |
| 119 | * calling find_start_rawstring() may change it. */ |
| 120 | comment_pos_copy = *comment_pos; |
| 121 | comment_pos = &comment_pos_copy; |
| 122 | } |
| 123 | rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment); |
| 124 | |
| 125 | /* If comment_pos is before rs_pos the raw string is inside the comment. |
| 126 | * If rs_pos is before comment_pos the comment is inside the raw string. */ |
| 127 | if (comment_pos == NULL || (rs_pos != NULL |
| 128 | && LT_POS(*rs_pos, *comment_pos))) |
| 129 | { |
| 130 | if (is_raw != NULL && rs_pos != NULL) |
| 131 | *is_raw = rs_pos->lnum; |
| 132 | return rs_pos; |
| 133 | } |
| 134 | return comment_pos; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Find the start of a raw string, not knowing if we are in one right now. |
| 139 | * Search starts at w_cursor.lnum and goes backwards. |
| 140 | * Return NULL when not inside a raw string. |
| 141 | */ |
| 142 | static pos_T * |
| 143 | find_start_rawstring(int ind_maxcomment) /* XXX */ |
| 144 | { |
| 145 | pos_T *pos; |
| 146 | char_u *line; |
| 147 | char_u *p; |
| 148 | int cur_maxcomment = ind_maxcomment; |
| 149 | |
| 150 | for (;;) |
| 151 | { |
| 152 | pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); |
| 153 | if (pos == NULL) |
| 154 | break; |
| 155 | |
| 156 | /* |
| 157 | * Check if the raw string start we found is inside a string. |
| 158 | * If it is then restrict the search to below this line and try again. |
| 159 | */ |
| 160 | line = ml_get(pos->lnum); |
| 161 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
| 162 | p = skip_string(p); |
| 163 | if ((colnr_T)(p - line) <= pos->col) |
| 164 | break; |
| 165 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 166 | if (cur_maxcomment <= 0) |
| 167 | { |
| 168 | pos = NULL; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | return pos; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Skip to the end of a "string" and a 'c' character. |
| 177 | * If there is no string or character, return argument unmodified. |
| 178 | */ |
| 179 | static char_u * |
| 180 | skip_string(char_u *p) |
| 181 | { |
| 182 | int i; |
| 183 | |
| 184 | /* |
| 185 | * We loop, because strings may be concatenated: "date""time". |
| 186 | */ |
| 187 | for ( ; ; ++p) |
| 188 | { |
| 189 | if (p[0] == '\'') /* 'c' or '\n' or '\000' */ |
| 190 | { |
| 191 | if (!p[1]) /* ' at end of line */ |
| 192 | break; |
| 193 | i = 2; |
| 194 | if (p[1] == '\\') /* '\n' or '\000' */ |
| 195 | { |
| 196 | ++i; |
| 197 | while (vim_isdigit(p[i - 1])) /* '\000' */ |
| 198 | ++i; |
| 199 | } |
| 200 | if (p[i] == '\'') /* check for trailing ' */ |
| 201 | { |
| 202 | p += i; |
| 203 | continue; |
| 204 | } |
| 205 | } |
| 206 | else if (p[0] == '"') /* start of string */ |
| 207 | { |
| 208 | for (++p; p[0]; ++p) |
| 209 | { |
| 210 | if (p[0] == '\\' && p[1] != NUL) |
| 211 | ++p; |
| 212 | else if (p[0] == '"') /* end of string */ |
| 213 | break; |
| 214 | } |
| 215 | if (p[0] == '"') |
| 216 | continue; /* continue for another string */ |
| 217 | } |
| 218 | else if (p[0] == 'R' && p[1] == '"') |
| 219 | { |
| 220 | /* Raw string: R"[delim](...)[delim]" */ |
| 221 | char_u *delim = p + 2; |
| 222 | char_u *paren = vim_strchr(delim, '('); |
| 223 | |
| 224 | if (paren != NULL) |
| 225 | { |
| 226 | size_t delim_len = paren - delim; |
| 227 | |
| 228 | for (p += 3; *p; ++p) |
| 229 | if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 |
| 230 | && p[delim_len + 1] == '"') |
| 231 | { |
| 232 | p += delim_len + 1; |
| 233 | break; |
| 234 | } |
| 235 | if (p[0] == '"') |
| 236 | continue; /* continue for another string */ |
| 237 | } |
| 238 | } |
| 239 | break; /* no string found */ |
| 240 | } |
| 241 | if (!*p) |
| 242 | --p; /* backup from NUL */ |
| 243 | return p; |
| 244 | } |
| 245 | #endif /* FEAT_CINDENT || FEAT_SYN_HL */ |
| 246 | |
| 247 | #if defined(FEAT_CINDENT) || defined(PROTO) |
| 248 | |
| 249 | /* |
| 250 | * Return TRUE if C-indenting is on. |
| 251 | */ |
| 252 | int |
| 253 | cindent_on(void) |
| 254 | { |
| 255 | return (!p_paste && (curbuf->b_p_cin |
| 256 | # ifdef FEAT_EVAL |
| 257 | || *curbuf->b_p_inde != NUL |
| 258 | # endif |
| 259 | )); |
| 260 | } |
| 261 | |
| 262 | /* Find result cache for cpp_baseclass */ |
| 263 | typedef struct { |
| 264 | int found; |
| 265 | lpos_T lpos; |
| 266 | } cpp_baseclass_cache_T; |
| 267 | |
| 268 | /* |
| 269 | * Functions for C-indenting. |
| 270 | * Most of this originally comes from Eric Fischer. |
| 271 | */ |
| 272 | /* |
| 273 | * Below "XXX" means that this function may unlock the current line. |
| 274 | */ |
| 275 | |
| 276 | static int cin_isdefault(char_u *); |
| 277 | static int cin_ispreproc(char_u *); |
| 278 | static int cin_iscomment(char_u *); |
| 279 | static int cin_islinecomment(char_u *); |
| 280 | static int cin_isterminated(char_u *, int, int); |
| 281 | static int cin_iselse(char_u *); |
| 282 | static int cin_ends_in(char_u *, char_u *, char_u *); |
| 283 | static int cin_starts_with(char_u *s, char *word); |
| 284 | static pos_T *find_match_paren(int); |
| 285 | static pos_T *find_match_char(int c, int ind_maxparen); |
| 286 | static int find_last_paren(char_u *l, int start, int end); |
| 287 | static int find_match(int lookfor, linenr_T ourscope); |
| 288 | |
| 289 | /* |
| 290 | * Skip over white space and C comments within the line. |
| 291 | * Also skip over Perl/shell comments if desired. |
| 292 | */ |
| 293 | static char_u * |
| 294 | cin_skipcomment(char_u *s) |
| 295 | { |
| 296 | while (*s) |
| 297 | { |
| 298 | char_u *prev_s = s; |
| 299 | |
| 300 | s = skipwhite(s); |
| 301 | |
| 302 | /* Perl/shell # comment comment continues until eol. Require a space |
| 303 | * before # to avoid recognizing $#array. */ |
| 304 | if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') |
| 305 | { |
| 306 | s += STRLEN(s); |
| 307 | break; |
| 308 | } |
| 309 | if (*s != '/') |
| 310 | break; |
| 311 | ++s; |
| 312 | if (*s == '/') /* slash-slash comment continues till eol */ |
| 313 | { |
| 314 | s += STRLEN(s); |
| 315 | break; |
| 316 | } |
| 317 | if (*s != '*') |
| 318 | break; |
| 319 | for (++s; *s; ++s) /* skip slash-star comment */ |
| 320 | if (s[0] == '*' && s[1] == '/') |
| 321 | { |
| 322 | s += 2; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | return s; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Return TRUE if there is no code at *s. White space and comments are |
| 331 | * not considered code. |
| 332 | */ |
| 333 | static int |
| 334 | cin_nocode(char_u *s) |
| 335 | { |
| 336 | return *cin_skipcomment(s) == NUL; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Check previous lines for a "//" line comment, skipping over blank lines. |
| 341 | */ |
| 342 | static pos_T * |
| 343 | find_line_comment(void) /* XXX */ |
| 344 | { |
| 345 | static pos_T pos; |
| 346 | char_u *line; |
| 347 | char_u *p; |
| 348 | |
| 349 | pos = curwin->w_cursor; |
| 350 | while (--pos.lnum > 0) |
| 351 | { |
| 352 | line = ml_get(pos.lnum); |
| 353 | p = skipwhite(line); |
| 354 | if (cin_islinecomment(p)) |
| 355 | { |
| 356 | pos.col = (int)(p - line); |
| 357 | return &pos; |
| 358 | } |
| 359 | if (*p != NUL) |
| 360 | break; |
| 361 | } |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Return TRUE if "text" starts with "key:". |
| 367 | */ |
| 368 | static int |
| 369 | cin_has_js_key(char_u *text) |
| 370 | { |
| 371 | char_u *s = skipwhite(text); |
| 372 | int quote = -1; |
| 373 | |
| 374 | if (*s == '\'' || *s == '"') |
| 375 | { |
| 376 | /* can be 'key': or "key": */ |
| 377 | quote = *s; |
| 378 | ++s; |
| 379 | } |
| 380 | if (!vim_isIDc(*s)) /* need at least one ID character */ |
| 381 | return FALSE; |
| 382 | |
| 383 | while (vim_isIDc(*s)) |
| 384 | ++s; |
| 385 | if (*s == quote) |
| 386 | ++s; |
| 387 | |
| 388 | s = cin_skipcomment(s); |
| 389 | |
| 390 | /* "::" is not a label, it's C++ */ |
| 391 | return (*s == ':' && s[1] != ':'); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Check if string matches "label:"; move to character after ':' if true. |
| 396 | * "*s" must point to the start of the label, if there is one. |
| 397 | */ |
| 398 | static int |
| 399 | cin_islabel_skip(char_u **s) |
| 400 | { |
| 401 | if (!vim_isIDc(**s)) /* need at least one ID character */ |
| 402 | return FALSE; |
| 403 | |
| 404 | while (vim_isIDc(**s)) |
| 405 | (*s)++; |
| 406 | |
| 407 | *s = cin_skipcomment(*s); |
| 408 | |
| 409 | /* "::" is not a label, it's C++ */ |
| 410 | return (**s == ':' && *++*s != ':'); |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Recognize a label: "label:". |
| 415 | * Note: curwin->w_cursor must be where we are looking for the label. |
| 416 | */ |
| 417 | int |
| 418 | cin_islabel(void) /* XXX */ |
| 419 | { |
| 420 | char_u *s; |
| 421 | |
| 422 | s = cin_skipcomment(ml_get_curline()); |
| 423 | |
| 424 | /* |
| 425 | * Exclude "default" from labels, since it should be indented |
| 426 | * like a switch label. Same for C++ scope declarations. |
| 427 | */ |
| 428 | if (cin_isdefault(s)) |
| 429 | return FALSE; |
| 430 | if (cin_isscopedecl(s)) |
| 431 | return FALSE; |
| 432 | |
| 433 | if (cin_islabel_skip(&s)) |
| 434 | { |
| 435 | /* |
| 436 | * Only accept a label if the previous line is terminated or is a case |
| 437 | * label. |
| 438 | */ |
| 439 | pos_T cursor_save; |
| 440 | pos_T *trypos; |
| 441 | char_u *line; |
| 442 | |
| 443 | cursor_save = curwin->w_cursor; |
| 444 | while (curwin->w_cursor.lnum > 1) |
| 445 | { |
| 446 | --curwin->w_cursor.lnum; |
| 447 | |
| 448 | /* |
| 449 | * If we're in a comment or raw string now, skip to the start of |
| 450 | * it. |
| 451 | */ |
| 452 | curwin->w_cursor.col = 0; |
| 453 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
| 454 | curwin->w_cursor = *trypos; |
| 455 | |
| 456 | line = ml_get_curline(); |
| 457 | if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ |
| 458 | continue; |
| 459 | if (*(line = cin_skipcomment(line)) == NUL) |
| 460 | continue; |
| 461 | |
| 462 | curwin->w_cursor = cursor_save; |
| 463 | if (cin_isterminated(line, TRUE, FALSE) |
| 464 | || cin_isscopedecl(line) |
| 465 | || cin_iscase(line, TRUE) |
| 466 | || (cin_islabel_skip(&line) && cin_nocode(line))) |
| 467 | return TRUE; |
| 468 | return FALSE; |
| 469 | } |
| 470 | curwin->w_cursor = cursor_save; |
| 471 | return TRUE; /* label at start of file??? */ |
| 472 | } |
| 473 | return FALSE; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Recognize structure initialization and enumerations: |
| 478 | * "[typedef] [static|public|protected|private] enum" |
| 479 | * "[typedef] [static|public|protected|private] = {" |
| 480 | */ |
| 481 | static int |
| 482 | cin_isinit(void) |
| 483 | { |
| 484 | char_u *s; |
| 485 | static char *skip[] = {"static", "public", "protected", "private"}; |
| 486 | |
| 487 | s = cin_skipcomment(ml_get_curline()); |
| 488 | |
| 489 | if (cin_starts_with(s, "typedef")) |
| 490 | s = cin_skipcomment(s + 7); |
| 491 | |
| 492 | for (;;) |
| 493 | { |
| 494 | int i, l; |
| 495 | |
| 496 | for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) |
| 497 | { |
| 498 | l = (int)strlen(skip[i]); |
| 499 | if (cin_starts_with(s, skip[i])) |
| 500 | { |
| 501 | s = cin_skipcomment(s + l); |
| 502 | l = 0; |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | if (l != 0) |
| 507 | break; |
| 508 | } |
| 509 | |
| 510 | if (cin_starts_with(s, "enum")) |
| 511 | return TRUE; |
| 512 | |
| 513 | if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) |
| 514 | return TRUE; |
| 515 | |
| 516 | return FALSE; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Recognize a switch label: "case .*:" or "default:". |
| 521 | */ |
| 522 | int |
| 523 | cin_iscase( |
| 524 | char_u *s, |
| 525 | int strict) /* Allow relaxed check of case statement for JS */ |
| 526 | { |
| 527 | s = cin_skipcomment(s); |
| 528 | if (cin_starts_with(s, "case")) |
| 529 | { |
| 530 | for (s += 4; *s; ++s) |
| 531 | { |
| 532 | s = cin_skipcomment(s); |
| 533 | if (*s == ':') |
| 534 | { |
| 535 | if (s[1] == ':') /* skip over "::" for C++ */ |
| 536 | ++s; |
| 537 | else |
| 538 | return TRUE; |
| 539 | } |
| 540 | if (*s == '\'' && s[1] && s[2] == '\'') |
| 541 | s += 2; /* skip over ':' */ |
| 542 | else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
| 543 | return FALSE; /* stop at comment */ |
| 544 | else if (*s == '"') |
| 545 | { |
| 546 | /* JS etc. */ |
| 547 | if (strict) |
| 548 | return FALSE; /* stop at string */ |
| 549 | else |
| 550 | return TRUE; |
| 551 | } |
| 552 | } |
| 553 | return FALSE; |
| 554 | } |
| 555 | |
| 556 | if (cin_isdefault(s)) |
| 557 | return TRUE; |
| 558 | return FALSE; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Recognize a "default" switch label. |
| 563 | */ |
| 564 | static int |
| 565 | cin_isdefault(char_u *s) |
| 566 | { |
| 567 | return (STRNCMP(s, "default", 7) == 0 |
| 568 | && *(s = cin_skipcomment(s + 7)) == ':' |
| 569 | && s[1] != ':'); |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Recognize a "public/private/protected" scope declaration label. |
| 574 | */ |
| 575 | int |
| 576 | cin_isscopedecl(char_u *s) |
| 577 | { |
| 578 | int i; |
| 579 | |
| 580 | s = cin_skipcomment(s); |
| 581 | if (STRNCMP(s, "public", 6) == 0) |
| 582 | i = 6; |
| 583 | else if (STRNCMP(s, "protected", 9) == 0) |
| 584 | i = 9; |
| 585 | else if (STRNCMP(s, "private", 7) == 0) |
| 586 | i = 7; |
| 587 | else |
| 588 | return FALSE; |
| 589 | return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); |
| 590 | } |
| 591 | |
| 592 | /* Maximum number of lines to search back for a "namespace" line. */ |
| 593 | #define FIND_NAMESPACE_LIM 20 |
| 594 | |
| 595 | /* |
| 596 | * Recognize a "namespace" scope declaration. |
| 597 | */ |
| 598 | static int |
| 599 | cin_is_cpp_namespace(char_u *s) |
| 600 | { |
| 601 | char_u *p; |
| 602 | int has_name = FALSE; |
| 603 | int has_name_start = FALSE; |
| 604 | |
| 605 | s = cin_skipcomment(s); |
| 606 | if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) |
| 607 | { |
| 608 | p = cin_skipcomment(skipwhite(s + 9)); |
| 609 | while (*p != NUL) |
| 610 | { |
| 611 | if (VIM_ISWHITE(*p)) |
| 612 | { |
| 613 | has_name = TRUE; /* found end of a name */ |
| 614 | p = cin_skipcomment(skipwhite(p)); |
| 615 | } |
| 616 | else if (*p == '{') |
| 617 | { |
| 618 | break; |
| 619 | } |
| 620 | else if (vim_iswordc(*p)) |
| 621 | { |
| 622 | has_name_start = TRUE; |
| 623 | if (has_name) |
| 624 | return FALSE; /* word character after skipping past name */ |
| 625 | ++p; |
| 626 | } |
| 627 | else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) |
| 628 | { |
| 629 | if (!has_name_start || has_name) |
| 630 | return FALSE; |
| 631 | /* C++ 17 nested namespace */ |
| 632 | p += 3; |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | return FALSE; |
| 637 | } |
| 638 | } |
| 639 | return TRUE; |
| 640 | } |
| 641 | return FALSE; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Recognize a `extern "C"` or `extern "C++"` linkage specifications. |
| 646 | */ |
| 647 | static int |
| 648 | cin_is_cpp_extern_c(char_u *s) |
| 649 | { |
| 650 | char_u *p; |
| 651 | int has_string_literal = FALSE; |
| 652 | |
| 653 | s = cin_skipcomment(s); |
| 654 | if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) |
| 655 | { |
| 656 | p = cin_skipcomment(skipwhite(s + 6)); |
| 657 | while (*p != NUL) |
| 658 | { |
| 659 | if (VIM_ISWHITE(*p)) |
| 660 | { |
| 661 | p = cin_skipcomment(skipwhite(p)); |
| 662 | } |
| 663 | else if (*p == '{') |
| 664 | { |
| 665 | break; |
| 666 | } |
| 667 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') |
| 668 | { |
| 669 | if (has_string_literal) |
| 670 | return FALSE; |
| 671 | has_string_literal = TRUE; |
| 672 | p += 3; |
| 673 | } |
| 674 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+' |
| 675 | && p[4] == '"') |
| 676 | { |
| 677 | if (has_string_literal) |
| 678 | return FALSE; |
| 679 | has_string_literal = TRUE; |
| 680 | p += 5; |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | return FALSE; |
| 685 | } |
| 686 | } |
| 687 | return has_string_literal ? TRUE : FALSE; |
| 688 | } |
| 689 | return FALSE; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * Return a pointer to the first non-empty non-comment character after a ':'. |
| 694 | * Return NULL if not found. |
| 695 | * case 234: a = b; |
| 696 | * ^ |
| 697 | */ |
| 698 | static char_u * |
| 699 | after_label(char_u *l) |
| 700 | { |
| 701 | for ( ; *l; ++l) |
| 702 | { |
| 703 | if (*l == ':') |
| 704 | { |
| 705 | if (l[1] == ':') /* skip over "::" for C++ */ |
| 706 | ++l; |
| 707 | else if (!cin_iscase(l + 1, FALSE)) |
| 708 | break; |
| 709 | } |
| 710 | else if (*l == '\'' && l[1] && l[2] == '\'') |
| 711 | l += 2; /* skip over 'x' */ |
| 712 | } |
| 713 | if (*l == NUL) |
| 714 | return NULL; |
| 715 | l = cin_skipcomment(l + 1); |
| 716 | if (*l == NUL) |
| 717 | return NULL; |
| 718 | return l; |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Get indent of line "lnum", skipping a label. |
| 723 | * Return 0 if there is nothing after the label. |
| 724 | */ |
| 725 | static int |
| 726 | get_indent_nolabel (linenr_T lnum) /* XXX */ |
| 727 | { |
| 728 | char_u *l; |
| 729 | pos_T fp; |
| 730 | colnr_T col; |
| 731 | char_u *p; |
| 732 | |
| 733 | l = ml_get(lnum); |
| 734 | p = after_label(l); |
| 735 | if (p == NULL) |
| 736 | return 0; |
| 737 | |
| 738 | fp.col = (colnr_T)(p - l); |
| 739 | fp.lnum = lnum; |
| 740 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 741 | return (int)col; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Find indent for line "lnum", ignoring any case or jump label. |
| 746 | * Also return a pointer to the text (after the label) in "pp". |
| 747 | * label: if (asdf && asdfasdf) |
| 748 | * ^ |
| 749 | */ |
| 750 | static int |
| 751 | skip_label(linenr_T lnum, char_u **pp) |
| 752 | { |
| 753 | char_u *l; |
| 754 | int amount; |
| 755 | pos_T cursor_save; |
| 756 | |
| 757 | cursor_save = curwin->w_cursor; |
| 758 | curwin->w_cursor.lnum = lnum; |
| 759 | l = ml_get_curline(); |
| 760 | /* XXX */ |
| 761 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) |
| 762 | { |
| 763 | amount = get_indent_nolabel(lnum); |
| 764 | l = after_label(ml_get_curline()); |
| 765 | if (l == NULL) /* just in case */ |
| 766 | l = ml_get_curline(); |
| 767 | } |
| 768 | else |
| 769 | { |
| 770 | amount = get_indent(); |
| 771 | l = ml_get_curline(); |
| 772 | } |
| 773 | *pp = l; |
| 774 | |
| 775 | curwin->w_cursor = cursor_save; |
| 776 | return amount; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Return the indent of the first variable name after a type in a declaration. |
| 781 | * int a, indent of "a" |
| 782 | * static struct foo b, indent of "b" |
| 783 | * enum bla c, indent of "c" |
| 784 | * Returns zero when it doesn't look like a declaration. |
| 785 | */ |
| 786 | static int |
| 787 | cin_first_id_amount(void) |
| 788 | { |
| 789 | char_u *line, *p, *s; |
| 790 | int len; |
| 791 | pos_T fp; |
| 792 | colnr_T col; |
| 793 | |
| 794 | line = ml_get_curline(); |
| 795 | p = skipwhite(line); |
| 796 | len = (int)(skiptowhite(p) - p); |
| 797 | if (len == 6 && STRNCMP(p, "static", 6) == 0) |
| 798 | { |
| 799 | p = skipwhite(p + 6); |
| 800 | len = (int)(skiptowhite(p) - p); |
| 801 | } |
| 802 | if (len == 6 && STRNCMP(p, "struct", 6) == 0) |
| 803 | p = skipwhite(p + 6); |
| 804 | else if (len == 4 && STRNCMP(p, "enum", 4) == 0) |
| 805 | p = skipwhite(p + 4); |
| 806 | else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) |
| 807 | || (len == 6 && STRNCMP(p, "signed", 6) == 0)) |
| 808 | { |
| 809 | s = skipwhite(p + len); |
| 810 | if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3])) |
| 811 | || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4])) |
| 812 | || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5])) |
| 813 | || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4]))) |
| 814 | p = s; |
| 815 | } |
| 816 | for (len = 0; vim_isIDc(p[len]); ++len) |
| 817 | ; |
| 818 | if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p)) |
| 819 | return 0; |
| 820 | |
| 821 | p = skipwhite(p + len); |
| 822 | fp.lnum = curwin->w_cursor.lnum; |
| 823 | fp.col = (colnr_T)(p - line); |
| 824 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 825 | return (int)col; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Return the indent of the first non-blank after an equal sign. |
| 830 | * char *foo = "here"; |
| 831 | * Return zero if no (useful) equal sign found. |
| 832 | * Return -1 if the line above "lnum" ends in a backslash. |
| 833 | * foo = "asdf\ |
| 834 | * asdf\ |
| 835 | * here"; |
| 836 | */ |
| 837 | static int |
| 838 | cin_get_equal_amount(linenr_T lnum) |
| 839 | { |
| 840 | char_u *line; |
| 841 | char_u *s; |
| 842 | colnr_T col; |
| 843 | pos_T fp; |
| 844 | |
| 845 | if (lnum > 1) |
| 846 | { |
| 847 | line = ml_get(lnum - 1); |
| 848 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | line = s = ml_get(lnum); |
| 853 | while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) |
| 854 | { |
| 855 | if (cin_iscomment(s)) /* ignore comments */ |
| 856 | s = cin_skipcomment(s); |
| 857 | else |
| 858 | ++s; |
| 859 | } |
| 860 | if (*s != '=') |
| 861 | return 0; |
| 862 | |
| 863 | s = skipwhite(s + 1); |
| 864 | if (cin_nocode(s)) |
| 865 | return 0; |
| 866 | |
| 867 | if (*s == '"') /* nice alignment for continued strings */ |
| 868 | ++s; |
| 869 | |
| 870 | fp.lnum = lnum; |
| 871 | fp.col = (colnr_T)(s - line); |
| 872 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 873 | return (int)col; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Recognize a preprocessor statement: Any line that starts with '#'. |
| 878 | */ |
| 879 | static int |
| 880 | cin_ispreproc(char_u *s) |
| 881 | { |
| 882 | if (*skipwhite(s) == '#') |
| 883 | return TRUE; |
| 884 | return FALSE; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a |
| 889 | * continuation line of a preprocessor statement. Decrease "*lnump" to the |
| 890 | * start and return the line in "*pp". |
| 891 | * Put the amount of indent in "*amount". |
| 892 | */ |
| 893 | static int |
| 894 | cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) |
| 895 | { |
| 896 | char_u *line = *pp; |
| 897 | linenr_T lnum = *lnump; |
| 898 | int retval = FALSE; |
| 899 | int candidate_amount = *amount; |
| 900 | |
| 901 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 902 | candidate_amount = get_indent_lnum(lnum); |
| 903 | |
| 904 | for (;;) |
| 905 | { |
| 906 | if (cin_ispreproc(line)) |
| 907 | { |
| 908 | retval = TRUE; |
| 909 | *lnump = lnum; |
| 910 | break; |
| 911 | } |
| 912 | if (lnum == 1) |
| 913 | break; |
| 914 | line = ml_get(--lnum); |
| 915 | if (*line == NUL || line[STRLEN(line) - 1] != '\\') |
| 916 | break; |
| 917 | } |
| 918 | |
| 919 | if (lnum != *lnump) |
| 920 | *pp = ml_get(*lnump); |
| 921 | if (retval) |
| 922 | *amount = candidate_amount; |
| 923 | return retval; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Recognize the start of a C or C++ comment. |
| 928 | */ |
| 929 | static int |
| 930 | cin_iscomment(char_u *p) |
| 931 | { |
| 932 | return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Recognize the start of a "//" comment. |
| 937 | */ |
| 938 | static int |
| 939 | cin_islinecomment(char_u *p) |
| 940 | { |
| 941 | return (p[0] == '/' && p[1] == '/'); |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
| 946 | * '}'. |
| 947 | * Don't consider "} else" a terminated line. |
| 948 | * If a line begins with an "else", only consider it terminated if no unmatched |
| 949 | * opening braces follow (handle "else { foo();" correctly). |
| 950 | * Return the character terminating the line (ending char's have precedence if |
| 951 | * both apply in order to determine initializations). |
| 952 | */ |
| 953 | static int |
| 954 | cin_isterminated( |
| 955 | char_u *s, |
| 956 | int incl_open, /* include '{' at the end as terminator */ |
| 957 | int incl_comma) /* recognize a trailing comma */ |
| 958 | { |
| 959 | char_u found_start = 0; |
| 960 | unsigned n_open = 0; |
| 961 | int is_else = FALSE; |
| 962 | |
| 963 | s = cin_skipcomment(s); |
| 964 | |
| 965 | if (*s == '{' || (*s == '}' && !cin_iselse(s))) |
| 966 | found_start = *s; |
| 967 | |
| 968 | if (!found_start) |
| 969 | is_else = cin_iselse(s); |
| 970 | |
| 971 | while (*s) |
| 972 | { |
| 973 | /* skip over comments, "" strings and 'c'haracters */ |
| 974 | s = skip_string(cin_skipcomment(s)); |
| 975 | if (*s == '}' && n_open > 0) |
| 976 | --n_open; |
| 977 | if ((!is_else || n_open == 0) |
| 978 | && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) |
| 979 | && cin_nocode(s + 1)) |
| 980 | return *s; |
| 981 | else if (*s == '{') |
| 982 | { |
| 983 | if (incl_open && cin_nocode(s + 1)) |
| 984 | return *s; |
| 985 | else |
| 986 | ++n_open; |
| 987 | } |
| 988 | |
| 989 | if (*s) |
| 990 | s++; |
| 991 | } |
| 992 | return found_start; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * Recognize the basic picture of a function declaration -- it needs to |
| 997 | * have an open paren somewhere and a close paren at the end of the line and |
| 998 | * no semicolons anywhere. |
| 999 | * When a line ends in a comma we continue looking in the next line. |
| 1000 | * "sp" points to a string with the line. When looking at other lines it must |
| 1001 | * be restored to the line. When it's NULL fetch lines here. |
| 1002 | * "first_lnum" is where we start looking. |
| 1003 | * "min_lnum" is the line before which we will not be looking. |
| 1004 | */ |
| 1005 | static int |
| 1006 | cin_isfuncdecl( |
| 1007 | char_u **sp, |
| 1008 | linenr_T first_lnum, |
| 1009 | linenr_T min_lnum) |
| 1010 | { |
| 1011 | char_u *s; |
| 1012 | linenr_T lnum = first_lnum; |
| 1013 | linenr_T save_lnum = curwin->w_cursor.lnum; |
| 1014 | int retval = FALSE; |
| 1015 | pos_T *trypos; |
| 1016 | int just_started = TRUE; |
| 1017 | |
| 1018 | if (sp == NULL) |
| 1019 | s = ml_get(lnum); |
| 1020 | else |
| 1021 | s = *sp; |
| 1022 | |
| 1023 | curwin->w_cursor.lnum = lnum; |
| 1024 | if (find_last_paren(s, '(', ')') |
| 1025 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 1026 | { |
| 1027 | lnum = trypos->lnum; |
| 1028 | if (lnum < min_lnum) |
| 1029 | { |
| 1030 | curwin->w_cursor.lnum = save_lnum; |
| 1031 | return FALSE; |
| 1032 | } |
| 1033 | |
| 1034 | s = ml_get(lnum); |
| 1035 | } |
| 1036 | curwin->w_cursor.lnum = save_lnum; |
| 1037 | |
| 1038 | /* Ignore line starting with #. */ |
| 1039 | if (cin_ispreproc(s)) |
| 1040 | return FALSE; |
| 1041 | |
| 1042 | while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
| 1043 | { |
| 1044 | if (cin_iscomment(s)) /* ignore comments */ |
| 1045 | s = cin_skipcomment(s); |
| 1046 | else if (*s == ':') |
| 1047 | { |
| 1048 | if (*(s + 1) == ':') |
| 1049 | s += 2; |
| 1050 | else |
| 1051 | /* To avoid a mistake in the following situation: |
| 1052 | * A::A(int a, int b) |
| 1053 | * : a(0) // <--not a function decl |
| 1054 | * , b(0) |
| 1055 | * {... |
| 1056 | */ |
| 1057 | return FALSE; |
| 1058 | } |
| 1059 | else |
| 1060 | ++s; |
| 1061 | } |
| 1062 | if (*s != '(') |
| 1063 | return FALSE; /* ';', ' or " before any () or no '(' */ |
| 1064 | |
| 1065 | while (*s && *s != ';' && *s != '\'' && *s != '"') |
| 1066 | { |
| 1067 | if (*s == ')' && cin_nocode(s + 1)) |
| 1068 | { |
| 1069 | /* ')' at the end: may have found a match |
| 1070 | * Check for he previous line not to end in a backslash: |
| 1071 | * #if defined(x) && \ |
| 1072 | * defined(y) |
| 1073 | */ |
| 1074 | lnum = first_lnum - 1; |
| 1075 | s = ml_get(lnum); |
| 1076 | if (*s == NUL || s[STRLEN(s) - 1] != '\\') |
| 1077 | retval = TRUE; |
| 1078 | goto done; |
| 1079 | } |
| 1080 | if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
| 1081 | { |
| 1082 | int comma = (*s == ','); |
| 1083 | |
| 1084 | /* ',' at the end: continue looking in the next line. |
| 1085 | * At the end: check for ',' in the next line, for this style: |
| 1086 | * func(arg1 |
| 1087 | * , arg2) */ |
| 1088 | for (;;) |
| 1089 | { |
| 1090 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 1091 | break; |
| 1092 | s = ml_get(++lnum); |
| 1093 | if (!cin_ispreproc(s)) |
| 1094 | break; |
| 1095 | } |
| 1096 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 1097 | break; |
| 1098 | /* Require a comma at end of the line or a comma or ')' at the |
| 1099 | * start of next line. */ |
| 1100 | s = skipwhite(s); |
| 1101 | if (!just_started && (!comma && *s != ',' && *s != ')')) |
| 1102 | break; |
| 1103 | just_started = FALSE; |
| 1104 | } |
| 1105 | else if (cin_iscomment(s)) /* ignore comments */ |
| 1106 | s = cin_skipcomment(s); |
| 1107 | else |
| 1108 | { |
| 1109 | ++s; |
| 1110 | just_started = FALSE; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | done: |
| 1115 | if (lnum != first_lnum && sp != NULL) |
| 1116 | *sp = ml_get(first_lnum); |
| 1117 | |
| 1118 | return retval; |
| 1119 | } |
| 1120 | |
| 1121 | static int |
| 1122 | cin_isif(char_u *p) |
| 1123 | { |
| 1124 | return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); |
| 1125 | } |
| 1126 | |
| 1127 | static int |
| 1128 | cin_iselse( |
| 1129 | char_u *p) |
| 1130 | { |
| 1131 | if (*p == '}') /* accept "} else" */ |
| 1132 | p = cin_skipcomment(p + 1); |
| 1133 | return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); |
| 1134 | } |
| 1135 | |
| 1136 | static int |
| 1137 | cin_isdo(char_u *p) |
| 1138 | { |
| 1139 | return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Check if this is a "while" that should have a matching "do". |
| 1144 | * We only accept a "while (condition) ;", with only white space between the |
| 1145 | * ')' and ';'. The condition may be spread over several lines. |
| 1146 | */ |
| 1147 | static int |
| 1148 | cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */ |
| 1149 | { |
| 1150 | pos_T cursor_save; |
| 1151 | pos_T *trypos; |
| 1152 | int retval = FALSE; |
| 1153 | |
| 1154 | p = cin_skipcomment(p); |
| 1155 | if (*p == '}') /* accept "} while (cond);" */ |
| 1156 | p = cin_skipcomment(p + 1); |
| 1157 | if (cin_starts_with(p, "while")) |
| 1158 | { |
| 1159 | cursor_save = curwin->w_cursor; |
| 1160 | curwin->w_cursor.lnum = lnum; |
| 1161 | curwin->w_cursor.col = 0; |
| 1162 | p = ml_get_curline(); |
| 1163 | while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ |
| 1164 | { |
| 1165 | ++p; |
| 1166 | ++curwin->w_cursor.col; |
| 1167 | } |
| 1168 | if ((trypos = findmatchlimit(NULL, 0, 0, |
| 1169 | curbuf->b_ind_maxparen)) != NULL |
| 1170 | && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') |
| 1171 | retval = TRUE; |
| 1172 | curwin->w_cursor = cursor_save; |
| 1173 | } |
| 1174 | return retval; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Check whether in "p" there is an "if", "for" or "while" before "*poffset". |
| 1179 | * Return 0 if there is none. |
| 1180 | * Otherwise return !0 and update "*poffset" to point to the place where the |
| 1181 | * string was found. |
| 1182 | */ |
| 1183 | static int |
| 1184 | cin_is_if_for_while_before_offset(char_u *line, int *poffset) |
| 1185 | { |
| 1186 | int offset = *poffset; |
| 1187 | |
| 1188 | if (offset-- < 2) |
| 1189 | return 0; |
| 1190 | while (offset > 2 && VIM_ISWHITE(line[offset])) |
| 1191 | --offset; |
| 1192 | |
| 1193 | offset -= 1; |
| 1194 | if (!STRNCMP(line + offset, "if", 2)) |
| 1195 | goto probablyFound; |
| 1196 | |
| 1197 | if (offset >= 1) |
| 1198 | { |
| 1199 | offset -= 1; |
| 1200 | if (!STRNCMP(line + offset, "for", 3)) |
| 1201 | goto probablyFound; |
| 1202 | |
| 1203 | if (offset >= 2) |
| 1204 | { |
| 1205 | offset -= 2; |
| 1206 | if (!STRNCMP(line + offset, "while", 5)) |
| 1207 | goto probablyFound; |
| 1208 | } |
| 1209 | } |
| 1210 | return 0; |
| 1211 | |
| 1212 | probablyFound: |
| 1213 | if (!offset || !vim_isIDc(line[offset - 1])) |
| 1214 | { |
| 1215 | *poffset = offset; |
| 1216 | return 1; |
| 1217 | } |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Return TRUE if we are at the end of a do-while. |
| 1223 | * do |
| 1224 | * nothing; |
| 1225 | * while (foo |
| 1226 | * && bar); <-- here |
| 1227 | * Adjust the cursor to the line with "while". |
| 1228 | */ |
| 1229 | static int |
| 1230 | cin_iswhileofdo_end(int terminated) |
| 1231 | { |
| 1232 | char_u *line; |
| 1233 | char_u *p; |
| 1234 | char_u *s; |
| 1235 | pos_T *trypos; |
| 1236 | int i; |
| 1237 | |
| 1238 | if (terminated != ';') /* there must be a ';' at the end */ |
| 1239 | return FALSE; |
| 1240 | |
| 1241 | p = line = ml_get_curline(); |
| 1242 | while (*p != NUL) |
| 1243 | { |
| 1244 | p = cin_skipcomment(p); |
| 1245 | if (*p == ')') |
| 1246 | { |
| 1247 | s = skipwhite(p + 1); |
| 1248 | if (*s == ';' && cin_nocode(s + 1)) |
| 1249 | { |
| 1250 | /* Found ");" at end of the line, now check there is "while" |
| 1251 | * before the matching '('. XXX */ |
| 1252 | i = (int)(p - line); |
| 1253 | curwin->w_cursor.col = i; |
| 1254 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
| 1255 | if (trypos != NULL) |
| 1256 | { |
| 1257 | s = cin_skipcomment(ml_get(trypos->lnum)); |
| 1258 | if (*s == '}') /* accept "} while (cond);" */ |
| 1259 | s = cin_skipcomment(s + 1); |
| 1260 | if (cin_starts_with(s, "while")) |
| 1261 | { |
| 1262 | curwin->w_cursor.lnum = trypos->lnum; |
| 1263 | return TRUE; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | /* Searching may have made "line" invalid, get it again. */ |
| 1268 | line = ml_get_curline(); |
| 1269 | p = line + i; |
| 1270 | } |
| 1271 | } |
| 1272 | if (*p != NUL) |
| 1273 | ++p; |
| 1274 | } |
| 1275 | return FALSE; |
| 1276 | } |
| 1277 | |
| 1278 | static int |
| 1279 | cin_isbreak(char_u *p) |
| 1280 | { |
| 1281 | return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Find the position of a C++ base-class declaration or |
| 1286 | * constructor-initialization. eg: |
| 1287 | * |
| 1288 | * class MyClass : |
| 1289 | * baseClass <-- here |
| 1290 | * class MyClass : public baseClass, |
| 1291 | * anotherBaseClass <-- here (should probably lineup ??) |
| 1292 | * MyClass::MyClass(...) : |
| 1293 | * baseClass(...) <-- here (constructor-initialization) |
| 1294 | * |
| 1295 | * This is a lot of guessing. Watch out for "cond ? func() : foo". |
| 1296 | */ |
| 1297 | static int |
| 1298 | cin_is_cpp_baseclass( |
| 1299 | cpp_baseclass_cache_T *cached) /* input and output */ |
| 1300 | { |
| 1301 | lpos_T *pos = &cached->lpos; /* find position */ |
| 1302 | char_u *s; |
| 1303 | int class_or_struct, lookfor_ctor_init, cpp_base_class; |
| 1304 | linenr_T lnum = curwin->w_cursor.lnum; |
| 1305 | char_u *line = ml_get_curline(); |
| 1306 | |
| 1307 | if (pos->lnum <= lnum) |
| 1308 | return cached->found; /* Use the cached result */ |
| 1309 | |
| 1310 | pos->col = 0; |
| 1311 | |
| 1312 | s = skipwhite(line); |
| 1313 | if (*s == '#') /* skip #define FOO x ? (x) : x */ |
| 1314 | return FALSE; |
| 1315 | s = cin_skipcomment(s); |
| 1316 | if (*s == NUL) |
| 1317 | return FALSE; |
| 1318 | |
| 1319 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 1320 | |
| 1321 | /* Search for a line starting with '#', empty, ending in ';' or containing |
| 1322 | * '{' or '}' and start below it. This handles the following situations: |
| 1323 | * a = cond ? |
| 1324 | * func() : |
| 1325 | * asdf; |
| 1326 | * func::foo() |
| 1327 | * : something |
| 1328 | * {} |
| 1329 | * Foo::Foo (int one, int two) |
| 1330 | * : something(4), |
| 1331 | * somethingelse(3) |
| 1332 | * {} |
| 1333 | */ |
| 1334 | while (lnum > 1) |
| 1335 | { |
| 1336 | line = ml_get(lnum - 1); |
| 1337 | s = skipwhite(line); |
| 1338 | if (*s == '#' || *s == NUL) |
| 1339 | break; |
| 1340 | while (*s != NUL) |
| 1341 | { |
| 1342 | s = cin_skipcomment(s); |
| 1343 | if (*s == '{' || *s == '}' |
| 1344 | || (*s == ';' && cin_nocode(s + 1))) |
| 1345 | break; |
| 1346 | if (*s != NUL) |
| 1347 | ++s; |
| 1348 | } |
| 1349 | if (*s != NUL) |
| 1350 | break; |
| 1351 | --lnum; |
| 1352 | } |
| 1353 | |
| 1354 | pos->lnum = lnum; |
| 1355 | line = ml_get(lnum); |
| 1356 | s = line; |
| 1357 | for (;;) |
| 1358 | { |
| 1359 | if (*s == NUL) |
| 1360 | { |
| 1361 | if (lnum == curwin->w_cursor.lnum) |
| 1362 | break; |
| 1363 | /* Continue in the cursor line. */ |
| 1364 | line = ml_get(++lnum); |
| 1365 | s = line; |
| 1366 | } |
| 1367 | if (s == line) |
| 1368 | { |
| 1369 | /* don't recognize "case (foo):" as a baseclass */ |
| 1370 | if (cin_iscase(s, FALSE)) |
| 1371 | break; |
| 1372 | s = cin_skipcomment(line); |
| 1373 | if (*s == NUL) |
| 1374 | continue; |
| 1375 | } |
| 1376 | |
| 1377 | if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) |
| 1378 | s = skip_string(s) + 1; |
| 1379 | else if (s[0] == ':') |
| 1380 | { |
| 1381 | if (s[1] == ':') |
| 1382 | { |
| 1383 | /* skip double colon. It can't be a constructor |
| 1384 | * initialization any more */ |
| 1385 | lookfor_ctor_init = FALSE; |
| 1386 | s = cin_skipcomment(s + 2); |
| 1387 | } |
| 1388 | else if (lookfor_ctor_init || class_or_struct) |
| 1389 | { |
| 1390 | /* we have something found, that looks like the start of |
| 1391 | * cpp-base-class-declaration or constructor-initialization */ |
| 1392 | cpp_base_class = TRUE; |
| 1393 | lookfor_ctor_init = class_or_struct = FALSE; |
| 1394 | pos->col = 0; |
| 1395 | s = cin_skipcomment(s + 1); |
| 1396 | } |
| 1397 | else |
| 1398 | s = cin_skipcomment(s + 1); |
| 1399 | } |
| 1400 | else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) |
| 1401 | || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) |
| 1402 | { |
| 1403 | class_or_struct = TRUE; |
| 1404 | lookfor_ctor_init = FALSE; |
| 1405 | |
| 1406 | if (*s == 'c') |
| 1407 | s = cin_skipcomment(s + 5); |
| 1408 | else |
| 1409 | s = cin_skipcomment(s + 6); |
| 1410 | } |
| 1411 | else |
| 1412 | { |
| 1413 | if (s[0] == '{' || s[0] == '}' || s[0] == ';') |
| 1414 | { |
| 1415 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 1416 | } |
| 1417 | else if (s[0] == ')') |
| 1418 | { |
| 1419 | /* Constructor-initialization is assumed if we come across |
| 1420 | * something like "):" */ |
| 1421 | class_or_struct = FALSE; |
| 1422 | lookfor_ctor_init = TRUE; |
| 1423 | } |
| 1424 | else if (s[0] == '?') |
| 1425 | { |
| 1426 | /* Avoid seeing '() :' after '?' as constructor init. */ |
| 1427 | return FALSE; |
| 1428 | } |
| 1429 | else if (!vim_isIDc(s[0])) |
| 1430 | { |
| 1431 | /* if it is not an identifier, we are wrong */ |
| 1432 | class_or_struct = FALSE; |
| 1433 | lookfor_ctor_init = FALSE; |
| 1434 | } |
| 1435 | else if (pos->col == 0) |
| 1436 | { |
| 1437 | /* it can't be a constructor-initialization any more */ |
| 1438 | lookfor_ctor_init = FALSE; |
| 1439 | |
| 1440 | /* the first statement starts here: lineup with this one... */ |
| 1441 | if (cpp_base_class) |
| 1442 | pos->col = (colnr_T)(s - line); |
| 1443 | } |
| 1444 | |
| 1445 | /* When the line ends in a comma don't align with it. */ |
| 1446 | if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) |
| 1447 | pos->col = 0; |
| 1448 | |
| 1449 | s = cin_skipcomment(s + 1); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | cached->found = cpp_base_class; |
| 1454 | if (cpp_base_class) |
| 1455 | pos->lnum = lnum; |
| 1456 | return cpp_base_class; |
| 1457 | } |
| 1458 | |
| 1459 | static int |
| 1460 | get_baseclass_amount(int col) |
| 1461 | { |
| 1462 | int amount; |
| 1463 | colnr_T vcol; |
| 1464 | pos_T *trypos; |
| 1465 | |
| 1466 | if (col == 0) |
| 1467 | { |
| 1468 | amount = get_indent(); |
| 1469 | if (find_last_paren(ml_get_curline(), '(', ')') |
| 1470 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 1471 | amount = get_indent_lnum(trypos->lnum); /* XXX */ |
| 1472 | if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) |
| 1473 | amount += curbuf->b_ind_cpp_baseclass; |
| 1474 | } |
| 1475 | else |
| 1476 | { |
| 1477 | curwin->w_cursor.col = col; |
| 1478 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 1479 | amount = (int)vcol; |
| 1480 | } |
| 1481 | if (amount < curbuf->b_ind_cpp_baseclass) |
| 1482 | amount = curbuf->b_ind_cpp_baseclass; |
| 1483 | return amount; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Return TRUE if string "s" ends with the string "find", possibly followed by |
| 1488 | * white space and comments. Skip strings and comments. |
| 1489 | * Ignore "ignore" after "find" if it's not NULL. |
| 1490 | */ |
| 1491 | static int |
| 1492 | cin_ends_in(char_u *s, char_u *find, char_u *ignore) |
| 1493 | { |
| 1494 | char_u *p = s; |
| 1495 | char_u *r; |
| 1496 | int len = (int)STRLEN(find); |
| 1497 | |
| 1498 | while (*p != NUL) |
| 1499 | { |
| 1500 | p = cin_skipcomment(p); |
| 1501 | if (STRNCMP(p, find, len) == 0) |
| 1502 | { |
| 1503 | r = skipwhite(p + len); |
| 1504 | if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) |
| 1505 | r = skipwhite(r + STRLEN(ignore)); |
| 1506 | if (cin_nocode(r)) |
| 1507 | return TRUE; |
| 1508 | } |
| 1509 | if (*p != NUL) |
| 1510 | ++p; |
| 1511 | } |
| 1512 | return FALSE; |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | * Return TRUE when "s" starts with "word" and then a non-ID character. |
| 1517 | */ |
| 1518 | static int |
| 1519 | cin_starts_with(char_u *s, char *word) |
| 1520 | { |
| 1521 | int l = (int)STRLEN(word); |
| 1522 | |
| 1523 | return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * Skip strings, chars and comments until at or past "trypos". |
| 1528 | * Return the column found. |
| 1529 | */ |
| 1530 | static int |
| 1531 | cin_skip2pos(pos_T *trypos) |
| 1532 | { |
| 1533 | char_u *line; |
| 1534 | char_u *p; |
| 1535 | char_u *new_p; |
| 1536 | |
| 1537 | p = line = ml_get(trypos->lnum); |
| 1538 | while (*p && (colnr_T)(p - line) < trypos->col) |
| 1539 | { |
| 1540 | if (cin_iscomment(p)) |
| 1541 | p = cin_skipcomment(p); |
| 1542 | else |
| 1543 | { |
| 1544 | new_p = skip_string(p); |
| 1545 | if (new_p == p) |
| 1546 | ++p; |
| 1547 | else |
| 1548 | p = new_p; |
| 1549 | } |
| 1550 | } |
| 1551 | return (int)(p - line); |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * Find the '{' at the start of the block we are in. |
| 1556 | * Return NULL if no match found. |
| 1557 | * Ignore a '{' that is in a comment, makes indenting the next three lines |
| 1558 | * work. */ |
| 1559 | /* foo() */ |
| 1560 | /* { */ |
| 1561 | /* } */ |
| 1562 | |
| 1563 | static pos_T * |
| 1564 | find_start_brace(void) /* XXX */ |
| 1565 | { |
| 1566 | pos_T cursor_save; |
| 1567 | pos_T *trypos; |
| 1568 | pos_T *pos; |
| 1569 | static pos_T pos_copy; |
| 1570 | |
| 1571 | cursor_save = curwin->w_cursor; |
| 1572 | while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) |
| 1573 | { |
| 1574 | pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ |
| 1575 | trypos = &pos_copy; |
| 1576 | curwin->w_cursor = *trypos; |
| 1577 | pos = NULL; |
| 1578 | /* ignore the { if it's in a // or / * * / comment */ |
| 1579 | if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
| 1580 | && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */ |
| 1581 | break; |
| 1582 | if (pos != NULL) |
| 1583 | curwin->w_cursor.lnum = pos->lnum; |
| 1584 | } |
| 1585 | curwin->w_cursor = cursor_save; |
| 1586 | return trypos; |
| 1587 | } |
| 1588 | |
| 1589 | /* |
| 1590 | * Find the matching '(', ignoring it if it is in a comment. |
| 1591 | * Return NULL if no match found. |
| 1592 | */ |
| 1593 | static pos_T * |
| 1594 | find_match_paren(int ind_maxparen) /* XXX */ |
| 1595 | { |
| 1596 | return find_match_char('(', ind_maxparen); |
| 1597 | } |
| 1598 | |
| 1599 | static pos_T * |
| 1600 | find_match_char(int c, int ind_maxparen) /* XXX */ |
| 1601 | { |
| 1602 | pos_T cursor_save; |
| 1603 | pos_T *trypos; |
| 1604 | static pos_T pos_copy; |
| 1605 | int ind_maxp_wk; |
| 1606 | |
| 1607 | cursor_save = curwin->w_cursor; |
| 1608 | ind_maxp_wk = ind_maxparen; |
| 1609 | retry: |
| 1610 | if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL) |
| 1611 | { |
| 1612 | /* check if the ( is in a // comment */ |
| 1613 | if ((colnr_T)cin_skip2pos(trypos) > trypos->col) |
| 1614 | { |
| 1615 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum); |
| 1616 | if (ind_maxp_wk > 0) |
| 1617 | { |
| 1618 | curwin->w_cursor = *trypos; |
| 1619 | curwin->w_cursor.col = 0; /* XXX */ |
| 1620 | goto retry; |
| 1621 | } |
| 1622 | trypos = NULL; |
| 1623 | } |
| 1624 | else |
| 1625 | { |
| 1626 | pos_T *trypos_wk; |
| 1627 | |
| 1628 | pos_copy = *trypos; /* copy trypos, findmatch will change it */ |
| 1629 | trypos = &pos_copy; |
| 1630 | curwin->w_cursor = *trypos; |
| 1631 | if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
| 1632 | { |
| 1633 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum |
| 1634 | - trypos_wk->lnum); |
| 1635 | if (ind_maxp_wk > 0) |
| 1636 | { |
| 1637 | curwin->w_cursor = *trypos_wk; |
| 1638 | goto retry; |
| 1639 | } |
| 1640 | trypos = NULL; |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | curwin->w_cursor = cursor_save; |
| 1645 | return trypos; |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * Find the matching '(', ignoring it if it is in a comment or before an |
| 1650 | * unmatched {. |
| 1651 | * Return NULL if no match found. |
| 1652 | */ |
| 1653 | static pos_T * |
| 1654 | find_match_paren_after_brace (int ind_maxparen) /* XXX */ |
| 1655 | { |
| 1656 | pos_T *trypos = find_match_paren(ind_maxparen); |
| 1657 | |
| 1658 | if (trypos != NULL) |
| 1659 | { |
| 1660 | pos_T *tryposBrace = find_start_brace(); |
| 1661 | |
| 1662 | /* If both an unmatched '(' and '{' is found. Ignore the '(' |
| 1663 | * position if the '{' is further down. */ |
| 1664 | if (tryposBrace != NULL |
| 1665 | && (trypos->lnum != tryposBrace->lnum |
| 1666 | ? trypos->lnum < tryposBrace->lnum |
| 1667 | : trypos->col < tryposBrace->col)) |
| 1668 | trypos = NULL; |
| 1669 | } |
| 1670 | return trypos; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * Return ind_maxparen corrected for the difference in line number between the |
| 1675 | * cursor position and "startpos". This makes sure that searching for a |
| 1676 | * matching paren above the cursor line doesn't find a match because of |
| 1677 | * looking a few lines further. |
| 1678 | */ |
| 1679 | static int |
| 1680 | corr_ind_maxparen(pos_T *startpos) |
| 1681 | { |
| 1682 | long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; |
| 1683 | |
| 1684 | if (n > 0 && n < curbuf->b_ind_maxparen / 2) |
| 1685 | return curbuf->b_ind_maxparen - (int)n; |
| 1686 | return curbuf->b_ind_maxparen; |
| 1687 | } |
| 1688 | |
| 1689 | /* |
| 1690 | * Set w_cursor.col to the column number of the last unmatched ')' or '{' in |
| 1691 | * line "l". "l" must point to the start of the line. |
| 1692 | */ |
| 1693 | static int |
| 1694 | find_last_paren(char_u *l, int start, int end) |
| 1695 | { |
| 1696 | int i; |
| 1697 | int retval = FALSE; |
| 1698 | int open_count = 0; |
| 1699 | |
| 1700 | curwin->w_cursor.col = 0; /* default is start of line */ |
| 1701 | |
| 1702 | for (i = 0; l[i] != NUL; i++) |
| 1703 | { |
| 1704 | i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ |
| 1705 | i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ |
| 1706 | if (l[i] == start) |
| 1707 | ++open_count; |
| 1708 | else if (l[i] == end) |
| 1709 | { |
| 1710 | if (open_count > 0) |
| 1711 | --open_count; |
| 1712 | else |
| 1713 | { |
| 1714 | curwin->w_cursor.col = i; |
| 1715 | retval = TRUE; |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | return retval; |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * Parse 'cinoptions' and set the values in "curbuf". |
| 1724 | * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. |
| 1725 | */ |
| 1726 | void |
| 1727 | parse_cino(buf_T *buf) |
| 1728 | { |
| 1729 | char_u *p; |
| 1730 | char_u *l; |
| 1731 | char_u *digits; |
| 1732 | int n; |
| 1733 | int divider; |
| 1734 | int fraction = 0; |
| 1735 | int sw = (int)get_sw_value(buf); |
| 1736 | |
| 1737 | /* |
| 1738 | * Set the default values. |
| 1739 | */ |
| 1740 | /* Spaces from a block's opening brace the prevailing indent for that |
| 1741 | * block should be. */ |
| 1742 | buf->b_ind_level = sw; |
| 1743 | |
| 1744 | /* Spaces from the edge of the line an open brace that's at the end of a |
| 1745 | * line is imagined to be. */ |
| 1746 | buf->b_ind_open_imag = 0; |
| 1747 | |
| 1748 | /* Spaces from the prevailing indent for a line that is not preceded by |
| 1749 | * an opening brace. */ |
| 1750 | buf->b_ind_no_brace = 0; |
| 1751 | |
| 1752 | /* Column where the first { of a function should be located }. */ |
| 1753 | buf->b_ind_first_open = 0; |
| 1754 | |
| 1755 | /* Spaces from the prevailing indent a leftmost open brace should be |
| 1756 | * located. */ |
| 1757 | buf->b_ind_open_extra = 0; |
| 1758 | |
| 1759 | /* Spaces from the matching open brace (real location for one at the left |
| 1760 | * edge; imaginary location from one that ends a line) the matching close |
| 1761 | * brace should be located. */ |
| 1762 | buf->b_ind_close_extra = 0; |
| 1763 | |
| 1764 | /* Spaces from the edge of the line an open brace sitting in the leftmost |
| 1765 | * column is imagined to be. */ |
| 1766 | buf->b_ind_open_left_imag = 0; |
| 1767 | |
| 1768 | /* Spaces jump labels should be shifted to the left if N is non-negative, |
| 1769 | * otherwise the jump label will be put to column 1. */ |
| 1770 | buf->b_ind_jump_label = -1; |
| 1771 | |
| 1772 | /* Spaces from the switch() indent a "case xx" label should be located. */ |
| 1773 | buf->b_ind_case = sw; |
| 1774 | |
| 1775 | /* Spaces from the "case xx:" code after a switch() should be located. */ |
| 1776 | buf->b_ind_case_code = sw; |
| 1777 | |
| 1778 | /* Lineup break at end of case in switch() with case label. */ |
| 1779 | buf->b_ind_case_break = 0; |
| 1780 | |
| 1781 | /* Spaces from the class declaration indent a scope declaration label |
| 1782 | * should be located. */ |
| 1783 | buf->b_ind_scopedecl = sw; |
| 1784 | |
| 1785 | /* Spaces from the scope declaration label code should be located. */ |
| 1786 | buf->b_ind_scopedecl_code = sw; |
| 1787 | |
| 1788 | /* Amount K&R-style parameters should be indented. */ |
| 1789 | buf->b_ind_param = sw; |
| 1790 | |
| 1791 | /* Amount a function type spec should be indented. */ |
| 1792 | buf->b_ind_func_type = sw; |
| 1793 | |
| 1794 | /* Amount a cpp base class declaration or constructor initialization |
| 1795 | * should be indented. */ |
| 1796 | buf->b_ind_cpp_baseclass = sw; |
| 1797 | |
| 1798 | /* additional spaces beyond the prevailing indent a continuation line |
| 1799 | * should be located. */ |
| 1800 | buf->b_ind_continuation = sw; |
| 1801 | |
| 1802 | /* Spaces from the indent of the line with an unclosed parentheses. */ |
| 1803 | buf->b_ind_unclosed = sw * 2; |
| 1804 | |
| 1805 | /* Spaces from the indent of the line with an unclosed parentheses, which |
| 1806 | * itself is also unclosed. */ |
| 1807 | buf->b_ind_unclosed2 = sw; |
| 1808 | |
| 1809 | /* Suppress ignoring spaces from the indent of a line starting with an |
| 1810 | * unclosed parentheses. */ |
| 1811 | buf->b_ind_unclosed_noignore = 0; |
| 1812 | |
| 1813 | /* If the opening paren is the last nonwhite character on the line, and |
| 1814 | * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer |
| 1815 | * context (for very long lines). */ |
| 1816 | buf->b_ind_unclosed_wrapped = 0; |
| 1817 | |
| 1818 | /* Suppress ignoring white space when lining up with the character after |
| 1819 | * an unclosed parentheses. */ |
| 1820 | buf->b_ind_unclosed_whiteok = 0; |
| 1821 | |
| 1822 | /* Indent a closing parentheses under the line start of the matching |
| 1823 | * opening parentheses. */ |
| 1824 | buf->b_ind_matching_paren = 0; |
| 1825 | |
| 1826 | /* Indent a closing parentheses under the previous line. */ |
| 1827 | buf->b_ind_paren_prev = 0; |
| 1828 | |
| 1829 | /* Extra indent for comments. */ |
| 1830 | buf->b_ind_comment = 0; |
| 1831 | |
| 1832 | /* Spaces from the comment opener when there is nothing after it. */ |
| 1833 | buf->b_ind_in_comment = 3; |
| 1834 | |
| 1835 | /* Boolean: if non-zero, use b_ind_in_comment even if there is something |
| 1836 | * after the comment opener. */ |
| 1837 | buf->b_ind_in_comment2 = 0; |
| 1838 | |
| 1839 | /* Max lines to search for an open paren. */ |
| 1840 | buf->b_ind_maxparen = 20; |
| 1841 | |
| 1842 | /* Max lines to search for an open comment. */ |
| 1843 | buf->b_ind_maxcomment = 70; |
| 1844 | |
| 1845 | /* Handle braces for java code. */ |
| 1846 | buf->b_ind_java = 0; |
| 1847 | |
| 1848 | /* Not to confuse JS object properties with labels. */ |
| 1849 | buf->b_ind_js = 0; |
| 1850 | |
| 1851 | /* Handle blocked cases correctly. */ |
| 1852 | buf->b_ind_keep_case_label = 0; |
| 1853 | |
| 1854 | /* Handle C++ namespace. */ |
| 1855 | buf->b_ind_cpp_namespace = 0; |
| 1856 | |
| 1857 | /* Handle continuation lines containing conditions of if(), for() and |
| 1858 | * while(). */ |
| 1859 | buf->b_ind_if_for_while = 0; |
| 1860 | |
| 1861 | /* indentation for # comments */ |
| 1862 | buf->b_ind_hash_comment = 0; |
| 1863 | |
| 1864 | /* Handle C++ extern "C" or "C++" */ |
| 1865 | buf->b_ind_cpp_extern_c = 0; |
| 1866 | |
| 1867 | for (p = buf->b_p_cino; *p; ) |
| 1868 | { |
| 1869 | l = p++; |
| 1870 | if (*p == '-') |
| 1871 | ++p; |
| 1872 | digits = p; /* remember where the digits start */ |
| 1873 | n = getdigits(&p); |
| 1874 | divider = 0; |
| 1875 | if (*p == '.') /* ".5s" means a fraction */ |
| 1876 | { |
| 1877 | fraction = atol((char *)++p); |
| 1878 | while (VIM_ISDIGIT(*p)) |
| 1879 | { |
| 1880 | ++p; |
| 1881 | if (divider) |
| 1882 | divider *= 10; |
| 1883 | else |
| 1884 | divider = 10; |
| 1885 | } |
| 1886 | } |
| 1887 | if (*p == 's') /* "2s" means two times 'shiftwidth' */ |
| 1888 | { |
| 1889 | if (p == digits) |
| 1890 | n = sw; /* just "s" is one 'shiftwidth' */ |
| 1891 | else |
| 1892 | { |
| 1893 | n *= sw; |
| 1894 | if (divider) |
| 1895 | n += (sw * fraction + divider / 2) / divider; |
| 1896 | } |
| 1897 | ++p; |
| 1898 | } |
| 1899 | if (l[1] == '-') |
| 1900 | n = -n; |
| 1901 | |
| 1902 | /* When adding an entry here, also update the default 'cinoptions' in |
| 1903 | * doc/indent.txt, and add explanation for it! */ |
| 1904 | switch (*l) |
| 1905 | { |
| 1906 | case '>': buf->b_ind_level = n; break; |
| 1907 | case 'e': buf->b_ind_open_imag = n; break; |
| 1908 | case 'n': buf->b_ind_no_brace = n; break; |
| 1909 | case 'f': buf->b_ind_first_open = n; break; |
| 1910 | case '{': buf->b_ind_open_extra = n; break; |
| 1911 | case '}': buf->b_ind_close_extra = n; break; |
| 1912 | case '^': buf->b_ind_open_left_imag = n; break; |
| 1913 | case 'L': buf->b_ind_jump_label = n; break; |
| 1914 | case ':': buf->b_ind_case = n; break; |
| 1915 | case '=': buf->b_ind_case_code = n; break; |
| 1916 | case 'b': buf->b_ind_case_break = n; break; |
| 1917 | case 'p': buf->b_ind_param = n; break; |
| 1918 | case 't': buf->b_ind_func_type = n; break; |
| 1919 | case '/': buf->b_ind_comment = n; break; |
| 1920 | case 'c': buf->b_ind_in_comment = n; break; |
| 1921 | case 'C': buf->b_ind_in_comment2 = n; break; |
| 1922 | case 'i': buf->b_ind_cpp_baseclass = n; break; |
| 1923 | case '+': buf->b_ind_continuation = n; break; |
| 1924 | case '(': buf->b_ind_unclosed = n; break; |
| 1925 | case 'u': buf->b_ind_unclosed2 = n; break; |
| 1926 | case 'U': buf->b_ind_unclosed_noignore = n; break; |
| 1927 | case 'W': buf->b_ind_unclosed_wrapped = n; break; |
| 1928 | case 'w': buf->b_ind_unclosed_whiteok = n; break; |
| 1929 | case 'm': buf->b_ind_matching_paren = n; break; |
| 1930 | case 'M': buf->b_ind_paren_prev = n; break; |
| 1931 | case ')': buf->b_ind_maxparen = n; break; |
| 1932 | case '*': buf->b_ind_maxcomment = n; break; |
| 1933 | case 'g': buf->b_ind_scopedecl = n; break; |
| 1934 | case 'h': buf->b_ind_scopedecl_code = n; break; |
| 1935 | case 'j': buf->b_ind_java = n; break; |
| 1936 | case 'J': buf->b_ind_js = n; break; |
| 1937 | case 'l': buf->b_ind_keep_case_label = n; break; |
| 1938 | case '#': buf->b_ind_hash_comment = n; break; |
| 1939 | case 'N': buf->b_ind_cpp_namespace = n; break; |
| 1940 | case 'k': buf->b_ind_if_for_while = n; break; |
| 1941 | case 'E': buf->b_ind_cpp_extern_c = n; break; |
| 1942 | } |
| 1943 | if (*p == ',') |
| 1944 | ++p; |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | /* |
| 1949 | * Return the desired indent for C code. |
| 1950 | * Return -1 if the indent should be left alone (inside a raw string). |
| 1951 | */ |
| 1952 | int |
| 1953 | get_c_indent(void) |
| 1954 | { |
| 1955 | pos_T cur_curpos; |
| 1956 | int amount; |
| 1957 | int scope_amount; |
| 1958 | int cur_amount = MAXCOL; |
| 1959 | colnr_T col; |
| 1960 | char_u *theline; |
| 1961 | char_u *linecopy; |
| 1962 | pos_T *trypos; |
| 1963 | pos_T *comment_pos; |
| 1964 | pos_T *tryposBrace = NULL; |
| 1965 | pos_T tryposCopy; |
| 1966 | pos_T our_paren_pos; |
| 1967 | char_u *start; |
| 1968 | int start_brace; |
| 1969 | #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
| 1970 | #define BRACE_AT_START 2 /* '{' is at start of line */ |
| 1971 | #define BRACE_AT_END 3 /* '{' is at end of line */ |
| 1972 | linenr_T ourscope; |
| 1973 | char_u *l; |
| 1974 | char_u *look; |
| 1975 | char_u terminated; |
| 1976 | int lookfor; |
| 1977 | #define LOOKFOR_INITIAL 0 |
| 1978 | #define LOOKFOR_IF 1 |
| 1979 | #define LOOKFOR_DO 2 |
| 1980 | #define LOOKFOR_CASE 3 |
| 1981 | #define LOOKFOR_ANY 4 |
| 1982 | #define LOOKFOR_TERM 5 |
| 1983 | #define LOOKFOR_UNTERM 6 |
| 1984 | #define LOOKFOR_SCOPEDECL 7 |
| 1985 | #define LOOKFOR_NOBREAK 8 |
| 1986 | #define LOOKFOR_CPP_BASECLASS 9 |
| 1987 | #define LOOKFOR_ENUM_OR_INIT 10 |
| 1988 | #define LOOKFOR_JS_KEY 11 |
| 1989 | #define LOOKFOR_COMMA 12 |
| 1990 | |
| 1991 | int whilelevel; |
| 1992 | linenr_T lnum; |
| 1993 | int n; |
| 1994 | int iscase; |
| 1995 | int lookfor_break; |
| 1996 | int lookfor_cpp_namespace = FALSE; |
| 1997 | int cont_amount = 0; /* amount for continuation line */ |
| 1998 | int original_line_islabel; |
| 1999 | int added_to_amount = 0; |
| 2000 | int js_cur_has_key = 0; |
| 2001 | linenr_T raw_string_start = 0; |
| 2002 | cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } }; |
| 2003 | |
| 2004 | /* make a copy, value is changed below */ |
| 2005 | int ind_continuation = curbuf->b_ind_continuation; |
| 2006 | |
| 2007 | /* remember where the cursor was when we started */ |
| 2008 | cur_curpos = curwin->w_cursor; |
| 2009 | |
| 2010 | /* if we are at line 1 zero indent is fine, right? */ |
| 2011 | if (cur_curpos.lnum == 1) |
| 2012 | return 0; |
| 2013 | |
| 2014 | /* Get a copy of the current contents of the line. |
| 2015 | * This is required, because only the most recent line obtained with |
| 2016 | * ml_get is valid! */ |
| 2017 | linecopy = vim_strsave(ml_get(cur_curpos.lnum)); |
| 2018 | if (linecopy == NULL) |
| 2019 | return 0; |
| 2020 | |
| 2021 | /* |
| 2022 | * In insert mode and the cursor is on a ')' truncate the line at the |
| 2023 | * cursor position. We don't want to line up with the matching '(' when |
| 2024 | * inserting new stuff. |
| 2025 | * For unknown reasons the cursor might be past the end of the line, thus |
| 2026 | * check for that. |
| 2027 | */ |
| 2028 | if ((State & INSERT) |
| 2029 | && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
| 2030 | && linecopy[curwin->w_cursor.col] == ')') |
| 2031 | linecopy[curwin->w_cursor.col] = NUL; |
| 2032 | |
| 2033 | theline = skipwhite(linecopy); |
| 2034 | |
| 2035 | /* move the cursor to the start of the line */ |
| 2036 | |
| 2037 | curwin->w_cursor.col = 0; |
| 2038 | |
| 2039 | original_line_islabel = cin_islabel(); /* XXX */ |
| 2040 | |
| 2041 | /* |
| 2042 | * If we are inside a raw string don't change the indent. |
| 2043 | * Ignore a raw string inside a comment. |
| 2044 | */ |
| 2045 | comment_pos = ind_find_start_comment(); |
| 2046 | if (comment_pos != NULL) |
| 2047 | { |
| 2048 | /* findmatchlimit() static pos is overwritten, make a copy */ |
| 2049 | tryposCopy = *comment_pos; |
| 2050 | comment_pos = &tryposCopy; |
| 2051 | } |
| 2052 | trypos = find_start_rawstring(curbuf->b_ind_maxcomment); |
| 2053 | if (trypos != NULL && (comment_pos == NULL |
| 2054 | || LT_POS(*trypos, *comment_pos))) |
| 2055 | { |
| 2056 | amount = -1; |
| 2057 | goto laterend; |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * #defines and so on always go at the left when included in 'cinkeys'. |
| 2062 | */ |
| 2063 | if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) |
| 2064 | { |
| 2065 | amount = curbuf->b_ind_hash_comment; |
| 2066 | goto theend; |
| 2067 | } |
| 2068 | |
| 2069 | /* |
| 2070 | * Is it a non-case label? Then that goes at the left margin too unless: |
| 2071 | * - JS flag is set. |
| 2072 | * - 'L' item has a positive value. |
| 2073 | */ |
| 2074 | if (original_line_islabel && !curbuf->b_ind_js |
| 2075 | && curbuf->b_ind_jump_label < 0) |
| 2076 | { |
| 2077 | amount = 0; |
| 2078 | goto theend; |
| 2079 | } |
| 2080 | |
| 2081 | /* |
| 2082 | * If we're inside a "//" comment and there is a "//" comment in a |
| 2083 | * previous line, lineup with that one. |
| 2084 | */ |
| 2085 | if (cin_islinecomment(theline) |
| 2086 | && (trypos = find_line_comment()) != NULL) /* XXX */ |
| 2087 | { |
| 2088 | /* find how indented the line beginning the comment is */ |
| 2089 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 2090 | amount = col; |
| 2091 | goto theend; |
| 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * If we're inside a comment and not looking at the start of the |
| 2096 | * comment, try using the 'comments' option. |
| 2097 | */ |
| 2098 | if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */ |
| 2099 | { |
| 2100 | int lead_start_len = 2; |
| 2101 | int lead_middle_len = 1; |
| 2102 | char_u lead_start[COM_MAX_LEN]; /* start-comment string */ |
| 2103 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 2104 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 2105 | char_u *p; |
| 2106 | int start_align = 0; |
| 2107 | int start_off = 0; |
| 2108 | int done = FALSE; |
| 2109 | |
| 2110 | /* find how indented the line beginning the comment is */ |
| 2111 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
| 2112 | amount = col; |
| 2113 | *lead_start = NUL; |
| 2114 | *lead_middle = NUL; |
| 2115 | |
| 2116 | p = curbuf->b_p_com; |
| 2117 | while (*p != NUL) |
| 2118 | { |
| 2119 | int align = 0; |
| 2120 | int off = 0; |
| 2121 | int what = 0; |
| 2122 | |
| 2123 | while (*p != NUL && *p != ':') |
| 2124 | { |
| 2125 | if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) |
| 2126 | what = *p++; |
| 2127 | else if (*p == COM_LEFT || *p == COM_RIGHT) |
| 2128 | align = *p++; |
| 2129 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 2130 | off = getdigits(&p); |
| 2131 | else |
| 2132 | ++p; |
| 2133 | } |
| 2134 | |
| 2135 | if (*p == ':') |
| 2136 | ++p; |
| 2137 | (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 2138 | if (what == COM_START) |
| 2139 | { |
| 2140 | STRCPY(lead_start, lead_end); |
| 2141 | lead_start_len = (int)STRLEN(lead_start); |
| 2142 | start_off = off; |
| 2143 | start_align = align; |
| 2144 | } |
| 2145 | else if (what == COM_MIDDLE) |
| 2146 | { |
| 2147 | STRCPY(lead_middle, lead_end); |
| 2148 | lead_middle_len = (int)STRLEN(lead_middle); |
| 2149 | } |
| 2150 | else if (what == COM_END) |
| 2151 | { |
| 2152 | /* If our line starts with the middle comment string, line it |
| 2153 | * up with the comment opener per the 'comments' option. */ |
| 2154 | if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 |
| 2155 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) |
| 2156 | { |
| 2157 | done = TRUE; |
| 2158 | if (curwin->w_cursor.lnum > 1) |
| 2159 | { |
| 2160 | /* If the start comment string matches in the previous |
| 2161 | * line, use the indent of that line plus offset. If |
| 2162 | * the middle comment string matches in the previous |
| 2163 | * line, use the indent of that line. XXX */ |
| 2164 | look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); |
| 2165 | if (STRNCMP(look, lead_start, lead_start_len) == 0) |
| 2166 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 2167 | else if (STRNCMP(look, lead_middle, |
| 2168 | lead_middle_len) == 0) |
| 2169 | { |
| 2170 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 2171 | break; |
| 2172 | } |
| 2173 | /* If the start comment string doesn't match with the |
| 2174 | * start of the comment, skip this entry. XXX */ |
| 2175 | else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, |
| 2176 | lead_start, lead_start_len) != 0) |
| 2177 | continue; |
| 2178 | } |
| 2179 | if (start_off != 0) |
| 2180 | amount += start_off; |
| 2181 | else if (start_align == COM_RIGHT) |
| 2182 | amount += vim_strsize(lead_start) |
| 2183 | - vim_strsize(lead_middle); |
| 2184 | break; |
| 2185 | } |
| 2186 | |
| 2187 | /* If our line starts with the end comment string, line it up |
| 2188 | * with the middle comment */ |
| 2189 | if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 |
| 2190 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) |
| 2191 | { |
| 2192 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 2193 | /* XXX */ |
| 2194 | if (off != 0) |
| 2195 | amount += off; |
| 2196 | else if (align == COM_RIGHT) |
| 2197 | amount += vim_strsize(lead_start) |
| 2198 | - vim_strsize(lead_middle); |
| 2199 | done = TRUE; |
| 2200 | break; |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | /* If our line starts with an asterisk, line up with the |
| 2206 | * asterisk in the comment opener; otherwise, line up |
| 2207 | * with the first character of the comment text. |
| 2208 | */ |
| 2209 | if (done) |
| 2210 | ; |
| 2211 | else if (theline[0] == '*') |
| 2212 | amount += 1; |
| 2213 | else |
| 2214 | { |
| 2215 | /* |
| 2216 | * If we are more than one line away from the comment opener, take |
| 2217 | * the indent of the previous non-empty line. If 'cino' has "CO" |
| 2218 | * and we are just below the comment opener and there are any |
| 2219 | * white characters after it line up with the text after it; |
| 2220 | * otherwise, add the amount specified by "c" in 'cino' |
| 2221 | */ |
| 2222 | amount = -1; |
| 2223 | for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum) |
| 2224 | { |
| 2225 | if (linewhite(lnum)) /* skip blank lines */ |
| 2226 | continue; |
| 2227 | amount = get_indent_lnum(lnum); /* XXX */ |
| 2228 | break; |
| 2229 | } |
| 2230 | if (amount == -1) /* use the comment opener */ |
| 2231 | { |
| 2232 | if (!curbuf->b_ind_in_comment2) |
| 2233 | { |
| 2234 | start = ml_get(comment_pos->lnum); |
| 2235 | look = start + comment_pos->col + 2; /* skip / and * */ |
| 2236 | if (*look != NUL) /* if something after it */ |
| 2237 | comment_pos->col = (colnr_T)(skipwhite(look) - start); |
| 2238 | } |
| 2239 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
| 2240 | amount = col; |
| 2241 | if (curbuf->b_ind_in_comment2 || *look == NUL) |
| 2242 | amount += curbuf->b_ind_in_comment; |
| 2243 | } |
| 2244 | } |
| 2245 | goto theend; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * Are we looking at a ']' that has a match? |
| 2250 | */ |
| 2251 | if (*skipwhite(theline) == ']' |
| 2252 | && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) |
| 2253 | { |
| 2254 | /* align with the line containing the '['. */ |
| 2255 | amount = get_indent_lnum(trypos->lnum); |
| 2256 | goto theend; |
| 2257 | } |
| 2258 | |
| 2259 | /* |
| 2260 | * Are we inside parentheses or braces? |
| 2261 | */ /* XXX */ |
| 2262 | if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL |
| 2263 | && curbuf->b_ind_java == 0) |
| 2264 | || (tryposBrace = find_start_brace()) != NULL |
| 2265 | || trypos != NULL) |
| 2266 | { |
| 2267 | if (trypos != NULL && tryposBrace != NULL) |
| 2268 | { |
| 2269 | /* Both an unmatched '(' and '{' is found. Use the one which is |
| 2270 | * closer to the current cursor position, set the other to NULL. */ |
| 2271 | if (trypos->lnum != tryposBrace->lnum |
| 2272 | ? trypos->lnum < tryposBrace->lnum |
| 2273 | : trypos->col < tryposBrace->col) |
| 2274 | trypos = NULL; |
| 2275 | else |
| 2276 | tryposBrace = NULL; |
| 2277 | } |
| 2278 | |
| 2279 | if (trypos != NULL) |
| 2280 | { |
| 2281 | /* |
| 2282 | * If the matching paren is more than one line away, use the indent of |
| 2283 | * a previous non-empty line that matches the same paren. |
| 2284 | */ |
| 2285 | if (theline[0] == ')' && curbuf->b_ind_paren_prev) |
| 2286 | { |
| 2287 | /* Line up with the start of the matching paren line. */ |
| 2288 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ |
| 2289 | } |
| 2290 | else |
| 2291 | { |
| 2292 | amount = -1; |
| 2293 | our_paren_pos = *trypos; |
| 2294 | for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) |
| 2295 | { |
| 2296 | l = skipwhite(ml_get(lnum)); |
| 2297 | if (cin_nocode(l)) /* skip comment lines */ |
| 2298 | continue; |
| 2299 | if (cin_ispreproc_cont(&l, &lnum, &amount)) |
| 2300 | continue; /* ignore #define, #if, etc. */ |
| 2301 | curwin->w_cursor.lnum = lnum; |
| 2302 | |
| 2303 | /* Skip a comment or raw string. XXX */ |
| 2304 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
| 2305 | { |
| 2306 | lnum = trypos->lnum + 1; |
| 2307 | continue; |
| 2308 | } |
| 2309 | |
| 2310 | /* XXX */ |
| 2311 | if ((trypos = find_match_paren( |
| 2312 | corr_ind_maxparen(&cur_curpos))) != NULL |
| 2313 | && trypos->lnum == our_paren_pos.lnum |
| 2314 | && trypos->col == our_paren_pos.col) |
| 2315 | { |
| 2316 | amount = get_indent_lnum(lnum); /* XXX */ |
| 2317 | |
| 2318 | if (theline[0] == ')') |
| 2319 | { |
| 2320 | if (our_paren_pos.lnum != lnum |
| 2321 | && cur_amount > amount) |
| 2322 | cur_amount = amount; |
| 2323 | amount = -1; |
| 2324 | } |
| 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | * Line up with line where the matching paren is. XXX |
| 2332 | * If the line starts with a '(' or the indent for unclosed |
| 2333 | * parentheses is zero, line up with the unclosed parentheses. |
| 2334 | */ |
| 2335 | if (amount == -1) |
| 2336 | { |
| 2337 | int ignore_paren_col = 0; |
| 2338 | int is_if_for_while = 0; |
| 2339 | |
| 2340 | if (curbuf->b_ind_if_for_while) |
| 2341 | { |
| 2342 | /* Look for the outermost opening parenthesis on this line |
| 2343 | * and check whether it belongs to an "if", "for" or "while". */ |
| 2344 | |
| 2345 | pos_T cursor_save = curwin->w_cursor; |
| 2346 | pos_T outermost; |
| 2347 | char_u *line; |
| 2348 | |
| 2349 | trypos = &our_paren_pos; |
| 2350 | do { |
| 2351 | outermost = *trypos; |
| 2352 | curwin->w_cursor.lnum = outermost.lnum; |
| 2353 | curwin->w_cursor.col = outermost.col; |
| 2354 | |
| 2355 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
| 2356 | } while (trypos && trypos->lnum == outermost.lnum); |
| 2357 | |
| 2358 | curwin->w_cursor = cursor_save; |
| 2359 | |
| 2360 | line = ml_get(outermost.lnum); |
| 2361 | |
| 2362 | is_if_for_while = |
| 2363 | cin_is_if_for_while_before_offset(line, &outermost.col); |
| 2364 | } |
| 2365 | |
| 2366 | amount = skip_label(our_paren_pos.lnum, &look); |
| 2367 | look = skipwhite(look); |
| 2368 | if (*look == '(') |
| 2369 | { |
| 2370 | linenr_T save_lnum = curwin->w_cursor.lnum; |
| 2371 | char_u *line; |
| 2372 | int look_col; |
| 2373 | |
| 2374 | /* Ignore a '(' in front of the line that has a match before |
| 2375 | * our matching '('. */ |
| 2376 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 2377 | line = ml_get_curline(); |
| 2378 | look_col = (int)(look - line); |
| 2379 | curwin->w_cursor.col = look_col + 1; |
| 2380 | if ((trypos = findmatchlimit(NULL, ')', 0, |
| 2381 | curbuf->b_ind_maxparen)) |
| 2382 | != NULL |
| 2383 | && trypos->lnum == our_paren_pos.lnum |
| 2384 | && trypos->col < our_paren_pos.col) |
| 2385 | ignore_paren_col = trypos->col + 1; |
| 2386 | |
| 2387 | curwin->w_cursor.lnum = save_lnum; |
| 2388 | look = ml_get(our_paren_pos.lnum) + look_col; |
| 2389 | } |
| 2390 | if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 |
| 2391 | && is_if_for_while == 0) |
| 2392 | || (!curbuf->b_ind_unclosed_noignore && *look == '(' |
| 2393 | && ignore_paren_col == 0)) |
| 2394 | { |
| 2395 | /* |
| 2396 | * If we're looking at a close paren, line up right there; |
| 2397 | * otherwise, line up with the next (non-white) character. |
| 2398 | * When b_ind_unclosed_wrapped is set and the matching paren is |
| 2399 | * the last nonwhite character of the line, use either the |
| 2400 | * indent of the current line or the indentation of the next |
| 2401 | * outer paren and add b_ind_unclosed_wrapped (for very long |
| 2402 | * lines). |
| 2403 | */ |
| 2404 | if (theline[0] != ')') |
| 2405 | { |
| 2406 | cur_amount = MAXCOL; |
| 2407 | l = ml_get(our_paren_pos.lnum); |
| 2408 | if (curbuf->b_ind_unclosed_wrapped |
| 2409 | && cin_ends_in(l, (char_u *)"(", NULL)) |
| 2410 | { |
| 2411 | /* look for opening unmatched paren, indent one level |
| 2412 | * for each additional level */ |
| 2413 | n = 1; |
| 2414 | for (col = 0; col < our_paren_pos.col; ++col) |
| 2415 | { |
| 2416 | switch (l[col]) |
| 2417 | { |
| 2418 | case '(': |
| 2419 | case '{': ++n; |
| 2420 | break; |
| 2421 | |
| 2422 | case ')': |
| 2423 | case '}': if (n > 1) |
| 2424 | --n; |
| 2425 | break; |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | our_paren_pos.col = 0; |
| 2430 | amount += n * curbuf->b_ind_unclosed_wrapped; |
| 2431 | } |
| 2432 | else if (curbuf->b_ind_unclosed_whiteok) |
| 2433 | our_paren_pos.col++; |
| 2434 | else |
| 2435 | { |
| 2436 | col = our_paren_pos.col + 1; |
| 2437 | while (VIM_ISWHITE(l[col])) |
| 2438 | col++; |
| 2439 | if (l[col] != NUL) /* In case of trailing space */ |
| 2440 | our_paren_pos.col = col; |
| 2441 | else |
| 2442 | our_paren_pos.col++; |
| 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * Find how indented the paren is, or the character after it |
| 2448 | * if we did the above "if". |
| 2449 | */ |
| 2450 | if (our_paren_pos.col > 0) |
| 2451 | { |
| 2452 | getvcol(curwin, &our_paren_pos, &col, NULL, NULL); |
| 2453 | if (cur_amount > (int)col) |
| 2454 | cur_amount = col; |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | if (theline[0] == ')' && curbuf->b_ind_matching_paren) |
| 2459 | { |
| 2460 | /* Line up with the start of the matching paren line. */ |
| 2461 | } |
| 2462 | else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) |
| 2463 | || (!curbuf->b_ind_unclosed_noignore |
| 2464 | && *look == '(' && ignore_paren_col == 0)) |
| 2465 | { |
| 2466 | if (cur_amount != MAXCOL) |
| 2467 | amount = cur_amount; |
| 2468 | } |
| 2469 | else |
| 2470 | { |
| 2471 | /* Add b_ind_unclosed2 for each '(' before our matching one, |
| 2472 | * but ignore (void) before the line (ignore_paren_col). */ |
| 2473 | col = our_paren_pos.col; |
| 2474 | while ((int)our_paren_pos.col > ignore_paren_col) |
| 2475 | { |
| 2476 | --our_paren_pos.col; |
| 2477 | switch (*ml_get_pos(&our_paren_pos)) |
| 2478 | { |
| 2479 | case '(': amount += curbuf->b_ind_unclosed2; |
| 2480 | col = our_paren_pos.col; |
| 2481 | break; |
| 2482 | case ')': amount -= curbuf->b_ind_unclosed2; |
| 2483 | col = MAXCOL; |
| 2484 | break; |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | /* Use b_ind_unclosed once, when the first '(' is not inside |
| 2489 | * braces */ |
| 2490 | if (col == MAXCOL) |
| 2491 | amount += curbuf->b_ind_unclosed; |
| 2492 | else |
| 2493 | { |
| 2494 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 2495 | curwin->w_cursor.col = col; |
| 2496 | if (find_match_paren_after_brace(curbuf->b_ind_maxparen) |
| 2497 | != NULL) |
| 2498 | amount += curbuf->b_ind_unclosed2; |
| 2499 | else |
| 2500 | { |
| 2501 | if (is_if_for_while) |
| 2502 | amount += curbuf->b_ind_if_for_while; |
| 2503 | else |
| 2504 | amount += curbuf->b_ind_unclosed; |
| 2505 | } |
| 2506 | } |
| 2507 | /* |
| 2508 | * For a line starting with ')' use the minimum of the two |
| 2509 | * positions, to avoid giving it more indent than the previous |
| 2510 | * lines: |
| 2511 | * func_long_name( if (x |
| 2512 | * arg && yy |
| 2513 | * ) ^ not here ) ^ not here |
| 2514 | */ |
| 2515 | if (cur_amount < amount) |
| 2516 | amount = cur_amount; |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | /* add extra indent for a comment */ |
| 2521 | if (cin_iscomment(theline)) |
| 2522 | amount += curbuf->b_ind_comment; |
| 2523 | } |
| 2524 | else |
| 2525 | { |
| 2526 | /* |
| 2527 | * We are inside braces, there is a { before this line at the position |
| 2528 | * stored in tryposBrace. |
| 2529 | * Make a copy of tryposBrace, it may point to pos_copy inside |
| 2530 | * find_start_brace(), which may be changed somewhere. |
| 2531 | */ |
| 2532 | tryposCopy = *tryposBrace; |
| 2533 | tryposBrace = &tryposCopy; |
| 2534 | trypos = tryposBrace; |
| 2535 | ourscope = trypos->lnum; |
| 2536 | start = ml_get(ourscope); |
| 2537 | |
| 2538 | /* |
| 2539 | * Now figure out how indented the line is in general. |
| 2540 | * If the brace was at the start of the line, we use that; |
| 2541 | * otherwise, check out the indentation of the line as |
| 2542 | * a whole and then add the "imaginary indent" to that. |
| 2543 | */ |
| 2544 | look = skipwhite(start); |
| 2545 | if (*look == '{') |
| 2546 | { |
| 2547 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 2548 | amount = col; |
| 2549 | if (*start == '{') |
| 2550 | start_brace = BRACE_IN_COL0; |
| 2551 | else |
| 2552 | start_brace = BRACE_AT_START; |
| 2553 | } |
| 2554 | else |
| 2555 | { |
| 2556 | /* That opening brace might have been on a continuation |
| 2557 | * line. if so, find the start of the line. */ |
| 2558 | curwin->w_cursor.lnum = ourscope; |
| 2559 | |
| 2560 | /* Position the cursor over the rightmost paren, so that |
| 2561 | * matching it will take us back to the start of the line. */ |
| 2562 | lnum = ourscope; |
| 2563 | if (find_last_paren(start, '(', ')') |
| 2564 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) |
| 2565 | != NULL) |
| 2566 | lnum = trypos->lnum; |
| 2567 | |
| 2568 | /* It could have been something like |
| 2569 | * case 1: if (asdf && |
| 2570 | * ldfd) { |
| 2571 | * } |
| 2572 | */ |
| 2573 | if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) |
| 2574 | && cin_iscase(skipwhite(ml_get_curline()), FALSE)) |
| 2575 | amount = get_indent(); |
| 2576 | else if (curbuf->b_ind_js) |
| 2577 | amount = get_indent_lnum(lnum); |
| 2578 | else |
| 2579 | amount = skip_label(lnum, &l); |
| 2580 | |
| 2581 | start_brace = BRACE_AT_END; |
| 2582 | } |
| 2583 | |
| 2584 | /* For Javascript check if the line starts with "key:". */ |
| 2585 | if (curbuf->b_ind_js) |
| 2586 | js_cur_has_key = cin_has_js_key(theline); |
| 2587 | |
| 2588 | /* |
| 2589 | * If we're looking at a closing brace, that's where |
| 2590 | * we want to be. otherwise, add the amount of room |
| 2591 | * that an indent is supposed to be. |
| 2592 | */ |
| 2593 | if (theline[0] == '}') |
| 2594 | { |
| 2595 | /* |
| 2596 | * they may want closing braces to line up with something |
| 2597 | * other than the open brace. indulge them, if so. |
| 2598 | */ |
| 2599 | amount += curbuf->b_ind_close_extra; |
| 2600 | } |
| 2601 | else |
| 2602 | { |
| 2603 | /* |
| 2604 | * If we're looking at an "else", try to find an "if" |
| 2605 | * to match it with. |
| 2606 | * If we're looking at a "while", try to find a "do" |
| 2607 | * to match it with. |
| 2608 | */ |
| 2609 | lookfor = LOOKFOR_INITIAL; |
| 2610 | if (cin_iselse(theline)) |
| 2611 | lookfor = LOOKFOR_IF; |
| 2612 | else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */ |
| 2613 | lookfor = LOOKFOR_DO; |
| 2614 | if (lookfor != LOOKFOR_INITIAL) |
| 2615 | { |
| 2616 | curwin->w_cursor.lnum = cur_curpos.lnum; |
| 2617 | if (find_match(lookfor, ourscope) == OK) |
| 2618 | { |
| 2619 | amount = get_indent(); /* XXX */ |
| 2620 | goto theend; |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | /* |
| 2625 | * We get here if we are not on an "while-of-do" or "else" (or |
| 2626 | * failed to find a matching "if"). |
| 2627 | * Search backwards for something to line up with. |
| 2628 | * First set amount for when we don't find anything. |
| 2629 | */ |
| 2630 | |
| 2631 | /* |
| 2632 | * if the '{' is _really_ at the left margin, use the imaginary |
| 2633 | * location of a left-margin brace. Otherwise, correct the |
| 2634 | * location for b_ind_open_extra. |
| 2635 | */ |
| 2636 | |
| 2637 | if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ |
| 2638 | { |
| 2639 | amount = curbuf->b_ind_open_left_imag; |
| 2640 | lookfor_cpp_namespace = TRUE; |
| 2641 | } |
| 2642 | else if (start_brace == BRACE_AT_START && |
| 2643 | lookfor_cpp_namespace) /* '{' is at start */ |
| 2644 | { |
| 2645 | |
| 2646 | lookfor_cpp_namespace = TRUE; |
| 2647 | } |
| 2648 | else |
| 2649 | { |
| 2650 | if (start_brace == BRACE_AT_END) /* '{' is at end of line */ |
| 2651 | { |
| 2652 | amount += curbuf->b_ind_open_imag; |
| 2653 | |
| 2654 | l = skipwhite(ml_get_curline()); |
| 2655 | if (cin_is_cpp_namespace(l)) |
| 2656 | amount += curbuf->b_ind_cpp_namespace; |
| 2657 | else if (cin_is_cpp_extern_c(l)) |
| 2658 | amount += curbuf->b_ind_cpp_extern_c; |
| 2659 | } |
| 2660 | else |
| 2661 | { |
| 2662 | /* Compensate for adding b_ind_open_extra later. */ |
| 2663 | amount -= curbuf->b_ind_open_extra; |
| 2664 | if (amount < 0) |
| 2665 | amount = 0; |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | lookfor_break = FALSE; |
| 2670 | |
| 2671 | if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
| 2672 | { |
| 2673 | lookfor = LOOKFOR_CASE; /* find a previous switch() label */ |
| 2674 | amount += curbuf->b_ind_case; |
| 2675 | } |
| 2676 | else if (cin_isscopedecl(theline)) /* private:, ... */ |
| 2677 | { |
| 2678 | lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ |
| 2679 | amount += curbuf->b_ind_scopedecl; |
| 2680 | } |
| 2681 | else |
| 2682 | { |
| 2683 | if (curbuf->b_ind_case_break && cin_isbreak(theline)) |
| 2684 | /* break; ... */ |
| 2685 | lookfor_break = TRUE; |
| 2686 | |
| 2687 | lookfor = LOOKFOR_INITIAL; |
| 2688 | /* b_ind_level from start of block */ |
| 2689 | amount += curbuf->b_ind_level; |
| 2690 | } |
| 2691 | scope_amount = amount; |
| 2692 | whilelevel = 0; |
| 2693 | |
| 2694 | /* |
| 2695 | * Search backwards. If we find something we recognize, line up |
| 2696 | * with that. |
| 2697 | * |
| 2698 | * If we're looking at an open brace, indent |
| 2699 | * the usual amount relative to the conditional |
| 2700 | * that opens the block. |
| 2701 | */ |
| 2702 | curwin->w_cursor = cur_curpos; |
| 2703 | for (;;) |
| 2704 | { |
| 2705 | curwin->w_cursor.lnum--; |
| 2706 | curwin->w_cursor.col = 0; |
| 2707 | |
| 2708 | /* |
| 2709 | * If we went all the way back to the start of our scope, line |
| 2710 | * up with it. |
| 2711 | */ |
| 2712 | if (curwin->w_cursor.lnum <= ourscope) |
| 2713 | { |
| 2714 | /* We reached end of scope: |
| 2715 | * If looking for a enum or structure initialization |
| 2716 | * go further back: |
| 2717 | * If it is an initializer (enum xxx or xxx =), then |
| 2718 | * don't add ind_continuation, otherwise it is a variable |
| 2719 | * declaration: |
| 2720 | * int x, |
| 2721 | * here; <-- add ind_continuation |
| 2722 | */ |
| 2723 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 2724 | { |
| 2725 | if (curwin->w_cursor.lnum == 0 |
| 2726 | || curwin->w_cursor.lnum |
| 2727 | < ourscope - curbuf->b_ind_maxparen) |
| 2728 | { |
| 2729 | /* nothing found (abuse curbuf->b_ind_maxparen as |
| 2730 | * limit) assume terminated line (i.e. a variable |
| 2731 | * initialization) */ |
| 2732 | if (cont_amount > 0) |
| 2733 | amount = cont_amount; |
| 2734 | else if (!curbuf->b_ind_js) |
| 2735 | amount += ind_continuation; |
| 2736 | break; |
| 2737 | } |
| 2738 | |
| 2739 | l = ml_get_curline(); |
| 2740 | |
| 2741 | /* |
| 2742 | * If we're in a comment or raw string now, skip to |
| 2743 | * the start of it. |
| 2744 | */ |
| 2745 | trypos = ind_find_start_CORS(NULL); |
| 2746 | if (trypos != NULL) |
| 2747 | { |
| 2748 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2749 | curwin->w_cursor.col = 0; |
| 2750 | continue; |
| 2751 | } |
| 2752 | |
| 2753 | /* |
| 2754 | * Skip preprocessor directives and blank lines. |
| 2755 | */ |
| 2756 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 2757 | &amount)) |
| 2758 | continue; |
| 2759 | |
| 2760 | if (cin_nocode(l)) |
| 2761 | continue; |
| 2762 | |
| 2763 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 2764 | |
| 2765 | /* |
| 2766 | * If we are at top level and the line looks like a |
| 2767 | * function declaration, we are done |
| 2768 | * (it's a variable declaration). |
| 2769 | */ |
| 2770 | if (start_brace != BRACE_IN_COL0 |
| 2771 | || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 2772 | { |
| 2773 | /* if the line is terminated with another ',' |
| 2774 | * it is a continued variable initialization. |
| 2775 | * don't add extra indent. |
| 2776 | * TODO: does not work, if a function |
| 2777 | * declaration is split over multiple lines: |
| 2778 | * cin_isfuncdecl returns FALSE then. |
| 2779 | */ |
| 2780 | if (terminated == ',') |
| 2781 | break; |
| 2782 | |
| 2783 | /* if it es a enum declaration or an assignment, |
| 2784 | * we are done. |
| 2785 | */ |
| 2786 | if (terminated != ';' && cin_isinit()) |
| 2787 | break; |
| 2788 | |
| 2789 | /* nothing useful found */ |
| 2790 | if (terminated == 0 || terminated == '{') |
| 2791 | continue; |
| 2792 | } |
| 2793 | |
| 2794 | if (terminated != ';') |
| 2795 | { |
| 2796 | /* Skip parens and braces. Position the cursor |
| 2797 | * over the rightmost paren, so that matching it |
| 2798 | * will take us back to the start of the line. |
| 2799 | */ /* XXX */ |
| 2800 | trypos = NULL; |
| 2801 | if (find_last_paren(l, '(', ')')) |
| 2802 | trypos = find_match_paren( |
| 2803 | curbuf->b_ind_maxparen); |
| 2804 | |
| 2805 | if (trypos == NULL && find_last_paren(l, '{', '}')) |
| 2806 | trypos = find_start_brace(); |
| 2807 | |
| 2808 | if (trypos != NULL) |
| 2809 | { |
| 2810 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2811 | curwin->w_cursor.col = 0; |
| 2812 | continue; |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | /* it's a variable declaration, add indentation |
| 2817 | * like in |
| 2818 | * int a, |
| 2819 | * b; |
| 2820 | */ |
| 2821 | if (cont_amount > 0) |
| 2822 | amount = cont_amount; |
| 2823 | else |
| 2824 | amount += ind_continuation; |
| 2825 | } |
| 2826 | else if (lookfor == LOOKFOR_UNTERM) |
| 2827 | { |
| 2828 | if (cont_amount > 0) |
| 2829 | amount = cont_amount; |
| 2830 | else |
| 2831 | amount += ind_continuation; |
| 2832 | } |
| 2833 | else |
| 2834 | { |
| 2835 | if (lookfor != LOOKFOR_TERM |
| 2836 | && lookfor != LOOKFOR_CPP_BASECLASS |
| 2837 | && lookfor != LOOKFOR_COMMA) |
| 2838 | { |
| 2839 | amount = scope_amount; |
| 2840 | if (theline[0] == '{') |
| 2841 | { |
| 2842 | amount += curbuf->b_ind_open_extra; |
| 2843 | added_to_amount = curbuf->b_ind_open_extra; |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | if (lookfor_cpp_namespace) |
| 2848 | { |
| 2849 | /* |
| 2850 | * Looking for C++ namespace, need to look further |
| 2851 | * back. |
| 2852 | */ |
| 2853 | if (curwin->w_cursor.lnum == ourscope) |
| 2854 | continue; |
| 2855 | |
| 2856 | if (curwin->w_cursor.lnum == 0 |
| 2857 | || curwin->w_cursor.lnum |
| 2858 | < ourscope - FIND_NAMESPACE_LIM) |
| 2859 | break; |
| 2860 | |
| 2861 | l = ml_get_curline(); |
| 2862 | |
| 2863 | /* If we're in a comment or raw string now, skip |
| 2864 | * to the start of it. */ |
| 2865 | trypos = ind_find_start_CORS(NULL); |
| 2866 | if (trypos != NULL) |
| 2867 | { |
| 2868 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2869 | curwin->w_cursor.col = 0; |
| 2870 | continue; |
| 2871 | } |
| 2872 | |
| 2873 | /* Skip preprocessor directives and blank lines. */ |
| 2874 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 2875 | &amount)) |
| 2876 | continue; |
| 2877 | |
| 2878 | /* Finally the actual check for "namespace". */ |
| 2879 | if (cin_is_cpp_namespace(l)) |
| 2880 | { |
| 2881 | amount += curbuf->b_ind_cpp_namespace |
| 2882 | - added_to_amount; |
| 2883 | break; |
| 2884 | } |
| 2885 | else if (cin_is_cpp_extern_c(l)) |
| 2886 | { |
| 2887 | amount += curbuf->b_ind_cpp_extern_c |
| 2888 | - added_to_amount; |
| 2889 | break; |
| 2890 | } |
| 2891 | |
| 2892 | if (cin_nocode(l)) |
| 2893 | continue; |
| 2894 | } |
| 2895 | } |
| 2896 | break; |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * If we're in a comment or raw string now, skip to the start |
| 2901 | * of it. |
| 2902 | */ /* XXX */ |
| 2903 | if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL) |
| 2904 | { |
| 2905 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2906 | curwin->w_cursor.col = 0; |
| 2907 | continue; |
| 2908 | } |
| 2909 | |
| 2910 | l = ml_get_curline(); |
| 2911 | |
| 2912 | /* |
| 2913 | * If this is a switch() label, may line up relative to that. |
| 2914 | * If this is a C++ scope declaration, do the same. |
| 2915 | */ |
| 2916 | iscase = cin_iscase(l, FALSE); |
| 2917 | if (iscase || cin_isscopedecl(l)) |
| 2918 | { |
| 2919 | /* we are only looking for cpp base class |
| 2920 | * declaration/initialization any longer */ |
| 2921 | if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 2922 | break; |
| 2923 | |
| 2924 | /* When looking for a "do" we are not interested in |
| 2925 | * labels. */ |
| 2926 | if (whilelevel > 0) |
| 2927 | continue; |
| 2928 | |
| 2929 | /* |
| 2930 | * case xx: |
| 2931 | * c = 99 + <- this indent plus continuation |
| 2932 | *-> here; |
| 2933 | */ |
| 2934 | if (lookfor == LOOKFOR_UNTERM |
| 2935 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 2936 | { |
| 2937 | if (cont_amount > 0) |
| 2938 | amount = cont_amount; |
| 2939 | else |
| 2940 | amount += ind_continuation; |
| 2941 | break; |
| 2942 | } |
| 2943 | |
| 2944 | /* |
| 2945 | * case xx: <- line up with this case |
| 2946 | * x = 333; |
| 2947 | * case yy: |
| 2948 | */ |
| 2949 | if ( (iscase && lookfor == LOOKFOR_CASE) |
| 2950 | || (iscase && lookfor_break) |
| 2951 | || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) |
| 2952 | { |
| 2953 | /* |
| 2954 | * Check that this case label is not for another |
| 2955 | * switch() |
| 2956 | */ /* XXX */ |
| 2957 | if ((trypos = find_start_brace()) == NULL |
| 2958 | || trypos->lnum == ourscope) |
| 2959 | { |
| 2960 | amount = get_indent(); /* XXX */ |
| 2961 | break; |
| 2962 | } |
| 2963 | continue; |
| 2964 | } |
| 2965 | |
| 2966 | n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ |
| 2967 | |
| 2968 | /* |
| 2969 | * case xx: if (cond) <- line up with this if |
| 2970 | * y = y + 1; |
| 2971 | * -> s = 99; |
| 2972 | * |
| 2973 | * case xx: |
| 2974 | * if (cond) <- line up with this line |
| 2975 | * y = y + 1; |
| 2976 | * -> s = 99; |
| 2977 | */ |
| 2978 | if (lookfor == LOOKFOR_TERM) |
| 2979 | { |
| 2980 | if (n) |
| 2981 | amount = n; |
| 2982 | |
| 2983 | if (!lookfor_break) |
| 2984 | break; |
| 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | * case xx: x = x + 1; <- line up with this x |
| 2989 | * -> y = y + 1; |
| 2990 | * |
| 2991 | * case xx: if (cond) <- line up with this if |
| 2992 | * -> y = y + 1; |
| 2993 | */ |
| 2994 | if (n) |
| 2995 | { |
| 2996 | amount = n; |
| 2997 | l = after_label(ml_get_curline()); |
| 2998 | if (l != NULL && cin_is_cinword(l)) |
| 2999 | { |
| 3000 | if (theline[0] == '{') |
| 3001 | amount += curbuf->b_ind_open_extra; |
| 3002 | else |
| 3003 | amount += curbuf->b_ind_level |
| 3004 | + curbuf->b_ind_no_brace; |
| 3005 | } |
| 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | /* |
| 3010 | * Try to get the indent of a statement before the switch |
| 3011 | * label. If nothing is found, line up relative to the |
| 3012 | * switch label. |
| 3013 | * break; <- may line up with this line |
| 3014 | * case xx: |
| 3015 | * -> y = 1; |
| 3016 | */ |
| 3017 | scope_amount = get_indent() + (iscase /* XXX */ |
| 3018 | ? curbuf->b_ind_case_code |
| 3019 | : curbuf->b_ind_scopedecl_code); |
| 3020 | lookfor = curbuf->b_ind_case_break |
| 3021 | ? LOOKFOR_NOBREAK : LOOKFOR_ANY; |
| 3022 | continue; |
| 3023 | } |
| 3024 | |
| 3025 | /* |
| 3026 | * Looking for a switch() label or C++ scope declaration, |
| 3027 | * ignore other lines, skip {}-blocks. |
| 3028 | */ |
| 3029 | if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) |
| 3030 | { |
| 3031 | if (find_last_paren(l, '{', '}') |
| 3032 | && (trypos = find_start_brace()) != NULL) |
| 3033 | { |
| 3034 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 3035 | curwin->w_cursor.col = 0; |
| 3036 | } |
| 3037 | continue; |
| 3038 | } |
| 3039 | |
| 3040 | /* |
| 3041 | * Ignore jump labels with nothing after them. |
| 3042 | */ |
| 3043 | if (!curbuf->b_ind_js && cin_islabel()) |
| 3044 | { |
| 3045 | l = after_label(ml_get_curline()); |
| 3046 | if (l == NULL || cin_nocode(l)) |
| 3047 | continue; |
| 3048 | } |
| 3049 | |
| 3050 | /* |
| 3051 | * Ignore #defines, #if, etc. |
| 3052 | * Ignore comment and empty lines. |
| 3053 | * (need to get the line again, cin_islabel() may have |
| 3054 | * unlocked it) |
| 3055 | */ |
| 3056 | l = ml_get_curline(); |
| 3057 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) |
| 3058 | || cin_nocode(l)) |
| 3059 | continue; |
| 3060 | |
| 3061 | /* |
| 3062 | * Are we at the start of a cpp base class declaration or |
| 3063 | * constructor initialization? |
| 3064 | */ /* XXX */ |
| 3065 | n = FALSE; |
| 3066 | if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) |
| 3067 | { |
| 3068 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 3069 | l = ml_get_curline(); |
| 3070 | } |
| 3071 | if (n) |
| 3072 | { |
| 3073 | if (lookfor == LOOKFOR_UNTERM) |
| 3074 | { |
| 3075 | if (cont_amount > 0) |
| 3076 | amount = cont_amount; |
| 3077 | else |
| 3078 | amount += ind_continuation; |
| 3079 | } |
| 3080 | else if (theline[0] == '{') |
| 3081 | { |
| 3082 | /* Need to find start of the declaration. */ |
| 3083 | lookfor = LOOKFOR_UNTERM; |
| 3084 | ind_continuation = 0; |
| 3085 | continue; |
| 3086 | } |
| 3087 | else |
| 3088 | /* XXX */ |
| 3089 | amount = get_baseclass_amount( |
| 3090 | cache_cpp_baseclass.lpos.col); |
| 3091 | break; |
| 3092 | } |
| 3093 | else if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 3094 | { |
| 3095 | /* only look, whether there is a cpp base class |
| 3096 | * declaration or initialization before the opening brace. |
| 3097 | */ |
| 3098 | if (cin_isterminated(l, TRUE, FALSE)) |
| 3099 | break; |
| 3100 | else |
| 3101 | continue; |
| 3102 | } |
| 3103 | |
| 3104 | /* |
| 3105 | * What happens next depends on the line being terminated. |
| 3106 | * If terminated with a ',' only consider it terminating if |
| 3107 | * there is another unterminated statement behind, eg: |
| 3108 | * 123, |
| 3109 | * sizeof |
| 3110 | * here |
| 3111 | * Otherwise check whether it is a enumeration or structure |
| 3112 | * initialisation (not indented) or a variable declaration |
| 3113 | * (indented). |
| 3114 | */ |
| 3115 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 3116 | |
| 3117 | if (js_cur_has_key) |
| 3118 | { |
| 3119 | js_cur_has_key = 0; /* only check the first line */ |
| 3120 | if (curbuf->b_ind_js && terminated == ',') |
| 3121 | { |
| 3122 | /* For Javascript we might be inside an object: |
| 3123 | * key: something, <- align with this |
| 3124 | * key: something |
| 3125 | * or: |
| 3126 | * key: something + <- align with this |
| 3127 | * something, |
| 3128 | * key: something |
| 3129 | */ |
| 3130 | lookfor = LOOKFOR_JS_KEY; |
| 3131 | } |
| 3132 | } |
| 3133 | if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) |
| 3134 | { |
| 3135 | amount = get_indent(); |
| 3136 | break; |
| 3137 | } |
| 3138 | if (lookfor == LOOKFOR_COMMA) |
| 3139 | { |
| 3140 | if (tryposBrace != NULL && tryposBrace->lnum |
| 3141 | >= curwin->w_cursor.lnum) |
| 3142 | break; |
| 3143 | if (terminated == ',') |
| 3144 | /* line below current line is the one that starts a |
| 3145 | * (possibly broken) line ending in a comma. */ |
| 3146 | break; |
| 3147 | else |
| 3148 | { |
| 3149 | amount = get_indent(); |
| 3150 | if (curwin->w_cursor.lnum - 1 == ourscope) |
| 3151 | /* line above is start of the scope, thus current |
| 3152 | * line is the one that stars a (possibly broken) |
| 3153 | * line ending in a comma. */ |
| 3154 | break; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | if (terminated == 0 || (lookfor != LOOKFOR_UNTERM |
| 3159 | && terminated == ',')) |
| 3160 | { |
| 3161 | if (lookfor != LOOKFOR_ENUM_OR_INIT && |
| 3162 | (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) |
| 3163 | amount += ind_continuation; |
| 3164 | /* |
| 3165 | * if we're in the middle of a paren thing, |
| 3166 | * go back to the line that starts it so |
| 3167 | * we can get the right prevailing indent |
| 3168 | * if ( foo && |
| 3169 | * bar ) |
| 3170 | */ |
| 3171 | /* |
| 3172 | * Position the cursor over the rightmost paren, so that |
| 3173 | * matching it will take us back to the start of the line. |
| 3174 | * Ignore a match before the start of the block. |
| 3175 | */ |
| 3176 | (void)find_last_paren(l, '(', ')'); |
| 3177 | trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); |
| 3178 | if (trypos != NULL && (trypos->lnum < tryposBrace->lnum |
| 3179 | || (trypos->lnum == tryposBrace->lnum |
| 3180 | && trypos->col < tryposBrace->col))) |
| 3181 | trypos = NULL; |
| 3182 | |
| 3183 | /* |
| 3184 | * If we are looking for ',', we also look for matching |
| 3185 | * braces. |
| 3186 | */ |
| 3187 | if (trypos == NULL && terminated == ',' |
| 3188 | && find_last_paren(l, '{', '}')) |
| 3189 | trypos = find_start_brace(); |
| 3190 | |
| 3191 | if (trypos != NULL) |
| 3192 | { |
| 3193 | /* |
| 3194 | * Check if we are on a case label now. This is |
| 3195 | * handled above. |
| 3196 | * case xx: if ( asdf && |
| 3197 | * asdf) |
| 3198 | */ |
| 3199 | curwin->w_cursor = *trypos; |
| 3200 | l = ml_get_curline(); |
| 3201 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
| 3202 | { |
| 3203 | ++curwin->w_cursor.lnum; |
| 3204 | curwin->w_cursor.col = 0; |
| 3205 | continue; |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | /* |
| 3210 | * Skip over continuation lines to find the one to get the |
| 3211 | * indent from |
| 3212 | * char *usethis = "bla\ |
| 3213 | * bla", |
| 3214 | * here; |
| 3215 | */ |
| 3216 | if (terminated == ',') |
| 3217 | { |
| 3218 | while (curwin->w_cursor.lnum > 1) |
| 3219 | { |
| 3220 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 3221 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 3222 | break; |
| 3223 | --curwin->w_cursor.lnum; |
| 3224 | curwin->w_cursor.col = 0; |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | /* |
| 3229 | * Get indent and pointer to text for current line, |
| 3230 | * ignoring any jump label. XXX |
| 3231 | */ |
| 3232 | if (curbuf->b_ind_js) |
| 3233 | cur_amount = get_indent(); |
| 3234 | else |
| 3235 | cur_amount = skip_label(curwin->w_cursor.lnum, &l); |
| 3236 | /* |
| 3237 | * If this is just above the line we are indenting, and it |
| 3238 | * starts with a '{', line it up with this line. |
| 3239 | * while (not) |
| 3240 | * -> { |
| 3241 | * } |
| 3242 | */ |
| 3243 | if (terminated != ',' && lookfor != LOOKFOR_TERM |
| 3244 | && theline[0] == '{') |
| 3245 | { |
| 3246 | amount = cur_amount; |
| 3247 | /* |
| 3248 | * Only add b_ind_open_extra when the current line |
| 3249 | * doesn't start with a '{', which must have a match |
| 3250 | * in the same line (scope is the same). Probably: |
| 3251 | * { 1, 2 }, |
| 3252 | * -> { 3, 4 } |
| 3253 | */ |
| 3254 | if (*skipwhite(l) != '{') |
| 3255 | amount += curbuf->b_ind_open_extra; |
| 3256 | |
| 3257 | if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) |
| 3258 | { |
| 3259 | /* have to look back, whether it is a cpp base |
| 3260 | * class declaration or initialization */ |
| 3261 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 3262 | continue; |
| 3263 | } |
| 3264 | break; |
| 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | * Check if we are after an "if", "while", etc. |
| 3269 | * Also allow " } else". |
| 3270 | */ |
| 3271 | if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) |
| 3272 | { |
| 3273 | /* |
| 3274 | * Found an unterminated line after an if (), line up |
| 3275 | * with the last one. |
| 3276 | * if (cond) |
| 3277 | * 100 + |
| 3278 | * -> here; |
| 3279 | */ |
| 3280 | if (lookfor == LOOKFOR_UNTERM |
| 3281 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3282 | { |
| 3283 | if (cont_amount > 0) |
| 3284 | amount = cont_amount; |
| 3285 | else |
| 3286 | amount += ind_continuation; |
| 3287 | break; |
| 3288 | } |
| 3289 | |
| 3290 | /* |
| 3291 | * If this is just above the line we are indenting, we |
| 3292 | * are finished. |
| 3293 | * while (not) |
| 3294 | * -> here; |
| 3295 | * Otherwise this indent can be used when the line |
| 3296 | * before this is terminated. |
| 3297 | * yyy; |
| 3298 | * if (stat) |
| 3299 | * while (not) |
| 3300 | * xxx; |
| 3301 | * -> here; |
| 3302 | */ |
| 3303 | amount = cur_amount; |
| 3304 | if (theline[0] == '{') |
| 3305 | amount += curbuf->b_ind_open_extra; |
| 3306 | if (lookfor != LOOKFOR_TERM) |
| 3307 | { |
| 3308 | amount += curbuf->b_ind_level |
| 3309 | + curbuf->b_ind_no_brace; |
| 3310 | break; |
| 3311 | } |
| 3312 | |
| 3313 | /* |
| 3314 | * Special trick: when expecting the while () after a |
| 3315 | * do, line up with the while() |
| 3316 | * do |
| 3317 | * x = 1; |
| 3318 | * -> here |
| 3319 | */ |
| 3320 | l = skipwhite(ml_get_curline()); |
| 3321 | if (cin_isdo(l)) |
| 3322 | { |
| 3323 | if (whilelevel == 0) |
| 3324 | break; |
| 3325 | --whilelevel; |
| 3326 | } |
| 3327 | |
| 3328 | /* |
| 3329 | * When searching for a terminated line, don't use the |
| 3330 | * one between the "if" and the matching "else". |
| 3331 | * Need to use the scope of this "else". XXX |
| 3332 | * If whilelevel != 0 continue looking for a "do {". |
| 3333 | */ |
| 3334 | if (cin_iselse(l) && whilelevel == 0) |
| 3335 | { |
| 3336 | /* If we're looking at "} else", let's make sure we |
| 3337 | * find the opening brace of the enclosing scope, |
| 3338 | * not the one from "if () {". */ |
| 3339 | if (*l == '}') |
| 3340 | curwin->w_cursor.col = |
| 3341 | (colnr_T)(l - ml_get_curline()) + 1; |
| 3342 | |
| 3343 | if ((trypos = find_start_brace()) == NULL |
| 3344 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 3345 | == FAIL) |
| 3346 | break; |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | /* |
| 3351 | * If we're below an unterminated line that is not an |
| 3352 | * "if" or something, we may line up with this line or |
| 3353 | * add something for a continuation line, depending on |
| 3354 | * the line before this one. |
| 3355 | */ |
| 3356 | else |
| 3357 | { |
| 3358 | /* |
| 3359 | * Found two unterminated lines on a row, line up with |
| 3360 | * the last one. |
| 3361 | * c = 99 + |
| 3362 | * 100 + |
| 3363 | * -> here; |
| 3364 | */ |
| 3365 | if (lookfor == LOOKFOR_UNTERM) |
| 3366 | { |
| 3367 | /* When line ends in a comma add extra indent */ |
| 3368 | if (terminated == ',') |
| 3369 | amount += ind_continuation; |
| 3370 | break; |
| 3371 | } |
| 3372 | |
| 3373 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3374 | { |
| 3375 | /* Found two lines ending in ',', lineup with the |
| 3376 | * lowest one, but check for cpp base class |
| 3377 | * declaration/initialization, if it is an |
| 3378 | * opening brace or we are looking just for |
| 3379 | * enumerations/initializations. */ |
| 3380 | if (terminated == ',') |
| 3381 | { |
| 3382 | if (curbuf->b_ind_cpp_baseclass == 0) |
| 3383 | break; |
| 3384 | |
| 3385 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 3386 | continue; |
| 3387 | } |
| 3388 | |
| 3389 | /* Ignore unterminated lines in between, but |
| 3390 | * reduce indent. */ |
| 3391 | if (amount > cur_amount) |
| 3392 | amount = cur_amount; |
| 3393 | } |
| 3394 | else |
| 3395 | { |
| 3396 | /* |
| 3397 | * Found first unterminated line on a row, may |
| 3398 | * line up with this line, remember its indent |
| 3399 | * 100 + |
| 3400 | * -> here; |
| 3401 | */ |
| 3402 | l = ml_get_curline(); |
| 3403 | amount = cur_amount; |
| 3404 | |
| 3405 | n = (int)STRLEN(l); |
| 3406 | if (terminated == ',' && (*skipwhite(l) == ']' |
| 3407 | || (n >=2 && l[n - 2] == ']'))) |
| 3408 | break; |
| 3409 | |
| 3410 | /* |
| 3411 | * If previous line ends in ',', check whether we |
| 3412 | * are in an initialization or enum |
| 3413 | * struct xxx = |
| 3414 | * { |
| 3415 | * sizeof a, |
| 3416 | * 124 }; |
| 3417 | * or a normal possible continuation line. |
| 3418 | * but only, of no other statement has been found |
| 3419 | * yet. |
| 3420 | */ |
| 3421 | if (lookfor == LOOKFOR_INITIAL && terminated == ',') |
| 3422 | { |
| 3423 | if (curbuf->b_ind_js) |
| 3424 | { |
| 3425 | /* Search for a line ending in a comma |
| 3426 | * and line up with the line below it |
| 3427 | * (could be the current line). |
| 3428 | * some = [ |
| 3429 | * 1, <- line up here |
| 3430 | * 2, |
| 3431 | * some = [ |
| 3432 | * 3 + <- line up here |
| 3433 | * 4 * |
| 3434 | * 5, |
| 3435 | * 6, |
| 3436 | */ |
| 3437 | if (cin_iscomment(skipwhite(l))) |
| 3438 | break; |
| 3439 | lookfor = LOOKFOR_COMMA; |
| 3440 | trypos = find_match_char('[', |
| 3441 | curbuf->b_ind_maxparen); |
| 3442 | if (trypos != NULL) |
| 3443 | { |
| 3444 | if (trypos->lnum |
| 3445 | == curwin->w_cursor.lnum - 1) |
| 3446 | { |
| 3447 | /* Current line is first inside |
| 3448 | * [], line up with it. */ |
| 3449 | break; |
| 3450 | } |
| 3451 | ourscope = trypos->lnum; |
| 3452 | } |
| 3453 | } |
| 3454 | else |
| 3455 | { |
| 3456 | lookfor = LOOKFOR_ENUM_OR_INIT; |
| 3457 | cont_amount = cin_first_id_amount(); |
| 3458 | } |
| 3459 | } |
| 3460 | else |
| 3461 | { |
| 3462 | if (lookfor == LOOKFOR_INITIAL |
| 3463 | && *l != NUL |
| 3464 | && l[STRLEN(l) - 1] == '\\') |
| 3465 | /* XXX */ |
| 3466 | cont_amount = cin_get_equal_amount( |
| 3467 | curwin->w_cursor.lnum); |
| 3468 | if (lookfor != LOOKFOR_TERM |
| 3469 | && lookfor != LOOKFOR_JS_KEY |
| 3470 | && lookfor != LOOKFOR_COMMA |
| 3471 | && raw_string_start != curwin->w_cursor.lnum) |
| 3472 | lookfor = LOOKFOR_UNTERM; |
| 3473 | } |
| 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | /* |
| 3479 | * Check if we are after a while (cond); |
| 3480 | * If so: Ignore until the matching "do". |
| 3481 | */ |
| 3482 | else if (cin_iswhileofdo_end(terminated)) /* XXX */ |
| 3483 | { |
| 3484 | /* |
| 3485 | * Found an unterminated line after a while ();, line up |
| 3486 | * with the last one. |
| 3487 | * while (cond); |
| 3488 | * 100 + <- line up with this one |
| 3489 | * -> here; |
| 3490 | */ |
| 3491 | if (lookfor == LOOKFOR_UNTERM |
| 3492 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3493 | { |
| 3494 | if (cont_amount > 0) |
| 3495 | amount = cont_amount; |
| 3496 | else |
| 3497 | amount += ind_continuation; |
| 3498 | break; |
| 3499 | } |
| 3500 | |
| 3501 | if (whilelevel == 0) |
| 3502 | { |
| 3503 | lookfor = LOOKFOR_TERM; |
| 3504 | amount = get_indent(); /* XXX */ |
| 3505 | if (theline[0] == '{') |
| 3506 | amount += curbuf->b_ind_open_extra; |
| 3507 | } |
| 3508 | ++whilelevel; |
| 3509 | } |
| 3510 | |
| 3511 | /* |
| 3512 | * We are after a "normal" statement. |
| 3513 | * If we had another statement we can stop now and use the |
| 3514 | * indent of that other statement. |
| 3515 | * Otherwise the indent of the current statement may be used, |
| 3516 | * search backwards for the next "normal" statement. |
| 3517 | */ |
| 3518 | else |
| 3519 | { |
| 3520 | /* |
| 3521 | * Skip single break line, if before a switch label. It |
| 3522 | * may be lined up with the case label. |
| 3523 | */ |
| 3524 | if (lookfor == LOOKFOR_NOBREAK |
| 3525 | && cin_isbreak(skipwhite(ml_get_curline()))) |
| 3526 | { |
| 3527 | lookfor = LOOKFOR_ANY; |
| 3528 | continue; |
| 3529 | } |
| 3530 | |
| 3531 | /* |
| 3532 | * Handle "do {" line. |
| 3533 | */ |
| 3534 | if (whilelevel > 0) |
| 3535 | { |
| 3536 | l = cin_skipcomment(ml_get_curline()); |
| 3537 | if (cin_isdo(l)) |
| 3538 | { |
| 3539 | amount = get_indent(); /* XXX */ |
| 3540 | --whilelevel; |
| 3541 | continue; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | /* |
| 3546 | * Found a terminated line above an unterminated line. Add |
| 3547 | * the amount for a continuation line. |
| 3548 | * x = 1; |
| 3549 | * y = foo + |
| 3550 | * -> here; |
| 3551 | * or |
| 3552 | * int x = 1; |
| 3553 | * int foo, |
| 3554 | * -> here; |
| 3555 | */ |
| 3556 | if (lookfor == LOOKFOR_UNTERM |
| 3557 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3558 | { |
| 3559 | if (cont_amount > 0) |
| 3560 | amount = cont_amount; |
| 3561 | else |
| 3562 | amount += ind_continuation; |
| 3563 | break; |
| 3564 | } |
| 3565 | |
| 3566 | /* |
| 3567 | * Found a terminated line above a terminated line or "if" |
| 3568 | * etc. line. Use the amount of the line below us. |
| 3569 | * x = 1; x = 1; |
| 3570 | * if (asdf) y = 2; |
| 3571 | * while (asdf) ->here; |
| 3572 | * here; |
| 3573 | * ->foo; |
| 3574 | */ |
| 3575 | if (lookfor == LOOKFOR_TERM) |
| 3576 | { |
| 3577 | if (!lookfor_break && whilelevel == 0) |
| 3578 | break; |
| 3579 | } |
| 3580 | |
| 3581 | /* |
| 3582 | * First line above the one we're indenting is terminated. |
| 3583 | * To know what needs to be done look further backward for |
| 3584 | * a terminated line. |
| 3585 | */ |
| 3586 | else |
| 3587 | { |
| 3588 | /* |
| 3589 | * position the cursor over the rightmost paren, so |
| 3590 | * that matching it will take us back to the start of |
| 3591 | * the line. Helps for: |
| 3592 | * func(asdr, |
| 3593 | * asdfasdf); |
| 3594 | * here; |
| 3595 | */ |
| 3596 | term_again: |
| 3597 | l = ml_get_curline(); |
| 3598 | if (find_last_paren(l, '(', ')') |
| 3599 | && (trypos = find_match_paren( |
| 3600 | curbuf->b_ind_maxparen)) != NULL) |
| 3601 | { |
| 3602 | /* |
| 3603 | * Check if we are on a case label now. This is |
| 3604 | * handled above. |
| 3605 | * case xx: if ( asdf && |
| 3606 | * asdf) |
| 3607 | */ |
| 3608 | curwin->w_cursor = *trypos; |
| 3609 | l = ml_get_curline(); |
| 3610 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
| 3611 | { |
| 3612 | ++curwin->w_cursor.lnum; |
| 3613 | curwin->w_cursor.col = 0; |
| 3614 | continue; |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | /* When aligning with the case statement, don't align |
| 3619 | * with a statement after it. |
| 3620 | * case 1: { <-- don't use this { position |
| 3621 | * stat; |
| 3622 | * } |
| 3623 | * case 2: |
| 3624 | * stat; |
| 3625 | * } |
| 3626 | */ |
| 3627 | iscase = (curbuf->b_ind_keep_case_label |
| 3628 | && cin_iscase(l, FALSE)); |
| 3629 | |
| 3630 | /* |
| 3631 | * Get indent and pointer to text for current line, |
| 3632 | * ignoring any jump label. |
| 3633 | */ |
| 3634 | amount = skip_label(curwin->w_cursor.lnum, &l); |
| 3635 | |
| 3636 | if (theline[0] == '{') |
| 3637 | amount += curbuf->b_ind_open_extra; |
| 3638 | /* See remark above: "Only add b_ind_open_extra.." */ |
| 3639 | l = skipwhite(l); |
| 3640 | if (*l == '{') |
| 3641 | amount -= curbuf->b_ind_open_extra; |
| 3642 | lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; |
| 3643 | |
| 3644 | /* |
| 3645 | * When a terminated line starts with "else" skip to |
| 3646 | * the matching "if": |
| 3647 | * else 3; |
| 3648 | * indent this; |
| 3649 | * Need to use the scope of this "else". XXX |
| 3650 | * If whilelevel != 0 continue looking for a "do {". |
| 3651 | */ |
| 3652 | if (lookfor == LOOKFOR_TERM |
| 3653 | && *l != '}' |
| 3654 | && cin_iselse(l) |
| 3655 | && whilelevel == 0) |
| 3656 | { |
| 3657 | if ((trypos = find_start_brace()) == NULL |
| 3658 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 3659 | == FAIL) |
| 3660 | break; |
| 3661 | continue; |
| 3662 | } |
| 3663 | |
| 3664 | /* |
| 3665 | * If we're at the end of a block, skip to the start of |
| 3666 | * that block. |
| 3667 | */ |
| 3668 | l = ml_get_curline(); |
| 3669 | if (find_last_paren(l, '{', '}') /* XXX */ |
| 3670 | && (trypos = find_start_brace()) != NULL) |
| 3671 | { |
| 3672 | curwin->w_cursor = *trypos; |
| 3673 | /* if not "else {" check for terminated again */ |
| 3674 | /* but skip block for "} else {" */ |
| 3675 | l = cin_skipcomment(ml_get_curline()); |
| 3676 | if (*l == '}' || !cin_iselse(l)) |
| 3677 | goto term_again; |
| 3678 | ++curwin->w_cursor.lnum; |
| 3679 | curwin->w_cursor.col = 0; |
| 3680 | } |
| 3681 | } |
| 3682 | } |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | /* add extra indent for a comment */ |
| 3688 | if (cin_iscomment(theline)) |
| 3689 | amount += curbuf->b_ind_comment; |
| 3690 | |
| 3691 | /* subtract extra left-shift for jump labels */ |
| 3692 | if (curbuf->b_ind_jump_label > 0 && original_line_islabel) |
| 3693 | amount -= curbuf->b_ind_jump_label; |
| 3694 | |
| 3695 | goto theend; |
| 3696 | } |
| 3697 | |
| 3698 | /* |
| 3699 | * ok -- we're not inside any sort of structure at all! |
| 3700 | * |
| 3701 | * This means we're at the top level, and everything should |
| 3702 | * basically just match where the previous line is, except |
| 3703 | * for the lines immediately following a function declaration, |
| 3704 | * which are K&R-style parameters and need to be indented. |
| 3705 | * |
| 3706 | * if our line starts with an open brace, forget about any |
| 3707 | * prevailing indent and make sure it looks like the start |
| 3708 | * of a function |
| 3709 | */ |
| 3710 | |
| 3711 | if (theline[0] == '{') |
| 3712 | { |
| 3713 | amount = curbuf->b_ind_first_open; |
| 3714 | goto theend; |
| 3715 | } |
| 3716 | |
| 3717 | /* |
| 3718 | * If the NEXT line is a function declaration, the current |
| 3719 | * line needs to be indented as a function type spec. |
| 3720 | * Don't do this if the current line looks like a comment or if the |
| 3721 | * current line is terminated, ie. ends in ';', or if the current line |
| 3722 | * contains { or }: "void f() {\n if (1)" |
| 3723 | */ |
| 3724 | if (cur_curpos.lnum < curbuf->b_ml.ml_line_count |
| 3725 | && !cin_nocode(theline) |
| 3726 | && vim_strchr(theline, '{') == NULL |
| 3727 | && vim_strchr(theline, '}') == NULL |
| 3728 | && !cin_ends_in(theline, (char_u *)":", NULL) |
| 3729 | && !cin_ends_in(theline, (char_u *)",", NULL) |
| 3730 | && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
| 3731 | cur_curpos.lnum + 1) |
| 3732 | && !cin_isterminated(theline, FALSE, TRUE)) |
| 3733 | { |
| 3734 | amount = curbuf->b_ind_func_type; |
| 3735 | goto theend; |
| 3736 | } |
| 3737 | |
| 3738 | /* search backwards until we find something we recognize */ |
| 3739 | amount = 0; |
| 3740 | curwin->w_cursor = cur_curpos; |
| 3741 | while (curwin->w_cursor.lnum > 1) |
| 3742 | { |
| 3743 | curwin->w_cursor.lnum--; |
| 3744 | curwin->w_cursor.col = 0; |
| 3745 | |
| 3746 | l = ml_get_curline(); |
| 3747 | |
| 3748 | /* |
| 3749 | * If we're in a comment or raw string now, skip to the start |
| 3750 | * of it. |
| 3751 | */ /* XXX */ |
| 3752 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
| 3753 | { |
| 3754 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 3755 | curwin->w_cursor.col = 0; |
| 3756 | continue; |
| 3757 | } |
| 3758 | |
| 3759 | /* |
| 3760 | * Are we at the start of a cpp base class declaration or |
| 3761 | * constructor initialization? |
| 3762 | */ /* XXX */ |
| 3763 | n = FALSE; |
| 3764 | if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') |
| 3765 | { |
| 3766 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 3767 | l = ml_get_curline(); |
| 3768 | } |
| 3769 | if (n) |
| 3770 | { |
| 3771 | /* XXX */ |
| 3772 | amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col); |
| 3773 | break; |
| 3774 | } |
| 3775 | |
| 3776 | /* |
| 3777 | * Skip preprocessor directives and blank lines. |
| 3778 | */ |
| 3779 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) |
| 3780 | continue; |
| 3781 | |
| 3782 | if (cin_nocode(l)) |
| 3783 | continue; |
| 3784 | |
| 3785 | /* |
| 3786 | * If the previous line ends in ',', use one level of |
| 3787 | * indentation: |
| 3788 | * int foo, |
| 3789 | * bar; |
| 3790 | * do this before checking for '}' in case of eg. |
| 3791 | * enum foobar |
| 3792 | * { |
| 3793 | * ... |
| 3794 | * } foo, |
| 3795 | * bar; |
| 3796 | */ |
| 3797 | n = 0; |
| 3798 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 3799 | || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) |
| 3800 | { |
| 3801 | /* take us back to opening paren */ |
| 3802 | if (find_last_paren(l, '(', ')') |
| 3803 | && (trypos = find_match_paren( |
| 3804 | curbuf->b_ind_maxparen)) != NULL) |
| 3805 | curwin->w_cursor = *trypos; |
| 3806 | |
| 3807 | /* For a line ending in ',' that is a continuation line go |
| 3808 | * back to the first line with a backslash: |
| 3809 | * char *foo = "bla\ |
| 3810 | * bla", |
| 3811 | * here; |
| 3812 | */ |
| 3813 | while (n == 0 && curwin->w_cursor.lnum > 1) |
| 3814 | { |
| 3815 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 3816 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 3817 | break; |
| 3818 | --curwin->w_cursor.lnum; |
| 3819 | curwin->w_cursor.col = 0; |
| 3820 | } |
| 3821 | |
| 3822 | amount = get_indent(); /* XXX */ |
| 3823 | |
| 3824 | if (amount == 0) |
| 3825 | amount = cin_first_id_amount(); |
| 3826 | if (amount == 0) |
| 3827 | amount = ind_continuation; |
| 3828 | break; |
| 3829 | } |
| 3830 | |
| 3831 | /* |
| 3832 | * If the line looks like a function declaration, and we're |
| 3833 | * not in a comment, put it the left margin. |
| 3834 | */ |
| 3835 | if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */ |
| 3836 | break; |
| 3837 | l = ml_get_curline(); |
| 3838 | |
| 3839 | /* |
| 3840 | * Finding the closing '}' of a previous function. Put |
| 3841 | * current line at the left margin. For when 'cino' has "fs". |
| 3842 | */ |
| 3843 | if (*skipwhite(l) == '}') |
| 3844 | break; |
| 3845 | |
| 3846 | /* (matching {) |
| 3847 | * If the previous line ends on '};' (maybe followed by |
| 3848 | * comments) align at column 0. For example: |
| 3849 | * char *string_array[] = { "foo", |
| 3850 | * / * x * / "b};ar" }; / * foobar * / |
| 3851 | */ |
| 3852 | if (cin_ends_in(l, (char_u *)"};", NULL)) |
| 3853 | break; |
| 3854 | |
| 3855 | /* |
| 3856 | * If the previous line ends on '[' we are probably in an |
| 3857 | * array constant: |
| 3858 | * something = [ |
| 3859 | * 234, <- extra indent |
| 3860 | */ |
| 3861 | if (cin_ends_in(l, (char_u *)"[", NULL)) |
| 3862 | { |
| 3863 | amount = get_indent() + ind_continuation; |
| 3864 | break; |
| 3865 | } |
| 3866 | |
| 3867 | /* |
| 3868 | * Find a line only has a semicolon that belongs to a previous |
| 3869 | * line ending in '}', e.g. before an #endif. Don't increase |
| 3870 | * indent then. |
| 3871 | */ |
| 3872 | if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) |
| 3873 | { |
| 3874 | pos_T curpos_save = curwin->w_cursor; |
| 3875 | |
| 3876 | while (curwin->w_cursor.lnum > 1) |
| 3877 | { |
| 3878 | look = ml_get(--curwin->w_cursor.lnum); |
| 3879 | if (!(cin_nocode(look) || cin_ispreproc_cont( |
| 3880 | &look, &curwin->w_cursor.lnum, &amount))) |
| 3881 | break; |
| 3882 | } |
| 3883 | if (curwin->w_cursor.lnum > 0 |
| 3884 | && cin_ends_in(look, (char_u *)"}", NULL)) |
| 3885 | break; |
| 3886 | |
| 3887 | curwin->w_cursor = curpos_save; |
| 3888 | } |
| 3889 | |
| 3890 | /* |
| 3891 | * If the PREVIOUS line is a function declaration, the current |
| 3892 | * line (and the ones that follow) needs to be indented as |
| 3893 | * parameters. |
| 3894 | */ |
| 3895 | if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 3896 | { |
| 3897 | amount = curbuf->b_ind_param; |
| 3898 | break; |
| 3899 | } |
| 3900 | |
| 3901 | /* |
| 3902 | * If the previous line ends in ';' and the line before the |
| 3903 | * previous line ends in ',' or '\', ident to column zero: |
| 3904 | * int foo, |
| 3905 | * bar; |
| 3906 | * indent_to_0 here; |
| 3907 | */ |
| 3908 | if (cin_ends_in(l, (char_u *)";", NULL)) |
| 3909 | { |
| 3910 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 3911 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 3912 | || (*l != NUL && l[STRLEN(l) - 1] == '\\')) |
| 3913 | break; |
| 3914 | l = ml_get_curline(); |
| 3915 | } |
| 3916 | |
| 3917 | /* |
| 3918 | * Doesn't look like anything interesting -- so just |
| 3919 | * use the indent of this line. |
| 3920 | * |
| 3921 | * Position the cursor over the rightmost paren, so that |
| 3922 | * matching it will take us back to the start of the line. |
| 3923 | */ |
| 3924 | find_last_paren(l, '(', ')'); |
| 3925 | |
| 3926 | if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 3927 | curwin->w_cursor = *trypos; |
| 3928 | amount = get_indent(); /* XXX */ |
| 3929 | break; |
| 3930 | } |
| 3931 | |
| 3932 | /* add extra indent for a comment */ |
| 3933 | if (cin_iscomment(theline)) |
| 3934 | amount += curbuf->b_ind_comment; |
| 3935 | |
| 3936 | /* add extra indent if the previous line ended in a backslash: |
| 3937 | * "asdfasdf\ |
| 3938 | * here"; |
| 3939 | * char *foo = "asdf\ |
| 3940 | * here"; |
| 3941 | */ |
| 3942 | if (cur_curpos.lnum > 1) |
| 3943 | { |
| 3944 | l = ml_get(cur_curpos.lnum - 1); |
| 3945 | if (*l != NUL && l[STRLEN(l) - 1] == '\\') |
| 3946 | { |
| 3947 | cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); |
| 3948 | if (cur_amount > 0) |
| 3949 | amount = cur_amount; |
| 3950 | else if (cur_amount == 0) |
| 3951 | amount += ind_continuation; |
| 3952 | } |
| 3953 | } |
| 3954 | |
| 3955 | theend: |
| 3956 | if (amount < 0) |
| 3957 | amount = 0; |
| 3958 | |
| 3959 | laterend: |
| 3960 | /* put the cursor back where it belongs */ |
| 3961 | curwin->w_cursor = cur_curpos; |
| 3962 | |
| 3963 | vim_free(linecopy); |
| 3964 | |
| 3965 | return amount; |
| 3966 | } |
| 3967 | |
| 3968 | static int |
| 3969 | find_match(int lookfor, linenr_T ourscope) |
| 3970 | { |
| 3971 | char_u *look; |
| 3972 | pos_T *theirscope; |
| 3973 | char_u *mightbeif; |
| 3974 | int elselevel; |
| 3975 | int whilelevel; |
| 3976 | |
| 3977 | if (lookfor == LOOKFOR_IF) |
| 3978 | { |
| 3979 | elselevel = 1; |
| 3980 | whilelevel = 0; |
| 3981 | } |
| 3982 | else |
| 3983 | { |
| 3984 | elselevel = 0; |
| 3985 | whilelevel = 1; |
| 3986 | } |
| 3987 | |
| 3988 | curwin->w_cursor.col = 0; |
| 3989 | |
| 3990 | while (curwin->w_cursor.lnum > ourscope + 1) |
| 3991 | { |
| 3992 | curwin->w_cursor.lnum--; |
| 3993 | curwin->w_cursor.col = 0; |
| 3994 | |
| 3995 | look = cin_skipcomment(ml_get_curline()); |
| 3996 | if (cin_iselse(look) |
| 3997 | || cin_isif(look) |
| 3998 | || cin_isdo(look) /* XXX */ |
| 3999 | || cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
| 4000 | { |
| 4001 | /* |
| 4002 | * if we've gone outside the braces entirely, |
| 4003 | * we must be out of scope... |
| 4004 | */ |
| 4005 | theirscope = find_start_brace(); /* XXX */ |
| 4006 | if (theirscope == NULL) |
| 4007 | break; |
| 4008 | |
| 4009 | /* |
| 4010 | * and if the brace enclosing this is further |
| 4011 | * back than the one enclosing the else, we're |
| 4012 | * out of luck too. |
| 4013 | */ |
| 4014 | if (theirscope->lnum < ourscope) |
| 4015 | break; |
| 4016 | |
| 4017 | /* |
| 4018 | * and if they're enclosed in a *deeper* brace, |
| 4019 | * then we can ignore it because it's in a |
| 4020 | * different scope... |
| 4021 | */ |
| 4022 | if (theirscope->lnum > ourscope) |
| 4023 | continue; |
| 4024 | |
| 4025 | /* |
| 4026 | * if it was an "else" (that's not an "else if") |
| 4027 | * then we need to go back to another if, so |
| 4028 | * increment elselevel |
| 4029 | */ |
| 4030 | look = cin_skipcomment(ml_get_curline()); |
| 4031 | if (cin_iselse(look)) |
| 4032 | { |
| 4033 | mightbeif = cin_skipcomment(look + 4); |
| 4034 | if (!cin_isif(mightbeif)) |
| 4035 | ++elselevel; |
| 4036 | continue; |
| 4037 | } |
| 4038 | |
| 4039 | /* |
| 4040 | * if it was a "while" then we need to go back to |
| 4041 | * another "do", so increment whilelevel. XXX |
| 4042 | */ |
| 4043 | if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
| 4044 | { |
| 4045 | ++whilelevel; |
| 4046 | continue; |
| 4047 | } |
| 4048 | |
| 4049 | /* If it's an "if" decrement elselevel */ |
| 4050 | look = cin_skipcomment(ml_get_curline()); |
| 4051 | if (cin_isif(look)) |
| 4052 | { |
| 4053 | elselevel--; |
| 4054 | /* |
| 4055 | * When looking for an "if" ignore "while"s that |
| 4056 | * get in the way. |
| 4057 | */ |
| 4058 | if (elselevel == 0 && lookfor == LOOKFOR_IF) |
| 4059 | whilelevel = 0; |
| 4060 | } |
| 4061 | |
| 4062 | /* If it's a "do" decrement whilelevel */ |
| 4063 | if (cin_isdo(look)) |
| 4064 | whilelevel--; |
| 4065 | |
| 4066 | /* |
| 4067 | * if we've used up all the elses, then |
| 4068 | * this must be the if that we want! |
| 4069 | * match the indent level of that if. |
| 4070 | */ |
| 4071 | if (elselevel <= 0 && whilelevel <= 0) |
| 4072 | { |
| 4073 | return OK; |
| 4074 | } |
| 4075 | } |
| 4076 | } |
| 4077 | return FAIL; |
| 4078 | } |
| 4079 | |
| 4080 | # if defined(FEAT_EVAL) || defined(PROTO) |
| 4081 | /* |
| 4082 | * Get indent level from 'indentexpr'. |
| 4083 | */ |
| 4084 | int |
| 4085 | get_expr_indent(void) |
| 4086 | { |
| 4087 | int indent = -1; |
| 4088 | char_u *inde_copy; |
| 4089 | pos_T save_pos; |
| 4090 | colnr_T save_curswant; |
| 4091 | int save_set_curswant; |
| 4092 | int save_State; |
| 4093 | int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
| 4094 | OPT_LOCAL); |
| 4095 | |
| 4096 | /* Save and restore cursor position and curswant, in case it was changed |
| 4097 | * via :normal commands */ |
| 4098 | save_pos = curwin->w_cursor; |
| 4099 | save_curswant = curwin->w_curswant; |
| 4100 | save_set_curswant = curwin->w_set_curswant; |
| 4101 | set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
| 4102 | if (use_sandbox) |
| 4103 | ++sandbox; |
| 4104 | ++textlock; |
| 4105 | |
| 4106 | /* Need to make a copy, the 'indentexpr' option could be changed while |
| 4107 | * evaluating it. */ |
| 4108 | inde_copy = vim_strsave(curbuf->b_p_inde); |
| 4109 | if (inde_copy != NULL) |
| 4110 | { |
| 4111 | indent = (int)eval_to_number(inde_copy); |
| 4112 | vim_free(inde_copy); |
| 4113 | } |
| 4114 | |
| 4115 | if (use_sandbox) |
| 4116 | --sandbox; |
| 4117 | --textlock; |
| 4118 | |
| 4119 | /* Restore the cursor position so that 'indentexpr' doesn't need to. |
| 4120 | * Pretend to be in Insert mode, allow cursor past end of line for "o" |
| 4121 | * command. */ |
| 4122 | save_State = State; |
| 4123 | State = INSERT; |
| 4124 | curwin->w_cursor = save_pos; |
| 4125 | curwin->w_curswant = save_curswant; |
| 4126 | curwin->w_set_curswant = save_set_curswant; |
| 4127 | check_cursor(); |
| 4128 | State = save_State; |
| 4129 | |
| 4130 | /* If there is an error, just keep the current indent. */ |
| 4131 | if (indent < 0) |
| 4132 | indent = get_indent(); |
| 4133 | |
| 4134 | return indent; |
| 4135 | } |
| 4136 | # endif |
| 4137 | |
| 4138 | /* |
| 4139 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 4140 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 4141 | * when == '!': Only if key is preceded with '!' (don't insert) |
| 4142 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 4143 | * |
| 4144 | * "keytyped" can have a few special values: |
| 4145 | * KEY_OPEN_FORW |
| 4146 | * KEY_OPEN_BACK |
| 4147 | * KEY_COMPLETE just finished completion. |
| 4148 | * |
| 4149 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 4150 | */ |
| 4151 | int |
| 4152 | in_cinkeys( |
| 4153 | int keytyped, |
| 4154 | int when, |
| 4155 | int line_is_empty) |
| 4156 | { |
| 4157 | char_u *look; |
| 4158 | int try_match; |
| 4159 | int try_match_word; |
| 4160 | char_u *p; |
| 4161 | char_u *line; |
| 4162 | int icase; |
| 4163 | int i; |
| 4164 | |
| 4165 | if (keytyped == NUL) |
| 4166 | /* Can happen with CTRL-Y and CTRL-E on a short line. */ |
| 4167 | return FALSE; |
| 4168 | |
| 4169 | #ifdef FEAT_EVAL |
| 4170 | if (*curbuf->b_p_inde != NUL) |
| 4171 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 4172 | else |
| 4173 | #endif |
| 4174 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 4175 | while (*look) |
| 4176 | { |
| 4177 | /* |
| 4178 | * Find out if we want to try a match with this key, depending on |
| 4179 | * 'when' and a '*' or '!' before the key. |
| 4180 | */ |
| 4181 | switch (when) |
| 4182 | { |
| 4183 | case '*': try_match = (*look == '*'); break; |
| 4184 | case '!': try_match = (*look == '!'); break; |
| 4185 | default: try_match = (*look != '*'); break; |
| 4186 | } |
| 4187 | if (*look == '*' || *look == '!') |
| 4188 | ++look; |
| 4189 | |
| 4190 | /* |
| 4191 | * If there is a '0', only accept a match if the line is empty. |
| 4192 | * But may still match when typing last char of a word. |
| 4193 | */ |
| 4194 | if (*look == '0') |
| 4195 | { |
| 4196 | try_match_word = try_match; |
| 4197 | if (!line_is_empty) |
| 4198 | try_match = FALSE; |
| 4199 | ++look; |
| 4200 | } |
| 4201 | else |
| 4202 | try_match_word = FALSE; |
| 4203 | |
| 4204 | /* |
| 4205 | * does it look like a control character? |
| 4206 | */ |
| 4207 | if (*look == '^' |
| 4208 | #ifdef EBCDIC |
| 4209 | && (Ctrl_chr(look[1]) != 0) |
| 4210 | #else |
| 4211 | && look[1] >= '?' && look[1] <= '_' |
| 4212 | #endif |
| 4213 | ) |
| 4214 | { |
| 4215 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 4216 | return TRUE; |
| 4217 | look += 2; |
| 4218 | } |
| 4219 | /* |
| 4220 | * 'o' means "o" command, open forward. |
| 4221 | * 'O' means "O" command, open backward. |
| 4222 | */ |
| 4223 | else if (*look == 'o') |
| 4224 | { |
| 4225 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 4226 | return TRUE; |
| 4227 | ++look; |
| 4228 | } |
| 4229 | else if (*look == 'O') |
| 4230 | { |
| 4231 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 4232 | return TRUE; |
| 4233 | ++look; |
| 4234 | } |
| 4235 | |
| 4236 | /* |
| 4237 | * 'e' means to check for "else" at start of line and just before the |
| 4238 | * cursor. |
| 4239 | */ |
| 4240 | else if (*look == 'e') |
| 4241 | { |
| 4242 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 4243 | { |
| 4244 | p = ml_get_curline(); |
| 4245 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 4246 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 4247 | return TRUE; |
| 4248 | } |
| 4249 | ++look; |
| 4250 | } |
| 4251 | |
| 4252 | /* |
| 4253 | * ':' only causes an indent if it is at the end of a label or case |
| 4254 | * statement, or when it was before typing the ':' (to fix |
| 4255 | * class::method for C++). |
| 4256 | */ |
| 4257 | else if (*look == ':') |
| 4258 | { |
| 4259 | if (try_match && keytyped == ':') |
| 4260 | { |
| 4261 | p = ml_get_curline(); |
| 4262 | if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel()) |
| 4263 | return TRUE; |
| 4264 | /* Need to get the line again after cin_islabel(). */ |
| 4265 | p = ml_get_curline(); |
| 4266 | if (curwin->w_cursor.col > 2 |
| 4267 | && p[curwin->w_cursor.col - 1] == ':' |
| 4268 | && p[curwin->w_cursor.col - 2] == ':') |
| 4269 | { |
| 4270 | p[curwin->w_cursor.col - 1] = ' '; |
| 4271 | i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
| 4272 | || cin_islabel()); |
| 4273 | p = ml_get_curline(); |
| 4274 | p[curwin->w_cursor.col - 1] = ':'; |
| 4275 | if (i) |
| 4276 | return TRUE; |
| 4277 | } |
| 4278 | } |
| 4279 | ++look; |
| 4280 | } |
| 4281 | |
| 4282 | |
| 4283 | /* |
| 4284 | * Is it a key in <>, maybe? |
| 4285 | */ |
| 4286 | else if (*look == '<') |
| 4287 | { |
| 4288 | if (try_match) |
| 4289 | { |
| 4290 | /* |
| 4291 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 4292 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 4293 | * >, *, : and ! keys if they really really want to. |
| 4294 | */ |
| 4295 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 4296 | && keytyped == look[1]) |
| 4297 | return TRUE; |
| 4298 | |
| 4299 | if (keytyped == get_special_key_code(look + 1)) |
| 4300 | return TRUE; |
| 4301 | } |
| 4302 | while (*look && *look != '>') |
| 4303 | look++; |
| 4304 | while (*look == '>') |
| 4305 | look++; |
| 4306 | } |
| 4307 | |
| 4308 | /* |
| 4309 | * Is it a word: "=word"? |
| 4310 | */ |
| 4311 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 4312 | { |
| 4313 | ++look; |
| 4314 | if (*look == '~') |
| 4315 | { |
| 4316 | icase = TRUE; |
| 4317 | ++look; |
| 4318 | } |
| 4319 | else |
| 4320 | icase = FALSE; |
| 4321 | p = vim_strchr(look, ','); |
| 4322 | if (p == NULL) |
| 4323 | p = look + STRLEN(look); |
| 4324 | if ((try_match || try_match_word) |
| 4325 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 4326 | { |
| 4327 | int match = FALSE; |
| 4328 | |
| 4329 | #ifdef FEAT_INS_EXPAND |
| 4330 | if (keytyped == KEY_COMPLETE) |
| 4331 | { |
| 4332 | char_u *s; |
| 4333 | |
| 4334 | /* Just completed a word, check if it starts with "look". |
| 4335 | * search back for the start of a word. */ |
| 4336 | line = ml_get_curline(); |
| 4337 | if (has_mbyte) |
| 4338 | { |
| 4339 | char_u *n; |
| 4340 | |
| 4341 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 4342 | { |
| 4343 | n = mb_prevptr(line, s); |
| 4344 | if (!vim_iswordp(n)) |
| 4345 | break; |
| 4346 | } |
| 4347 | } |
| 4348 | else |
| 4349 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 4350 | if (!vim_iswordc(s[-1])) |
| 4351 | break; |
| 4352 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 4353 | && (icase |
| 4354 | ? MB_STRNICMP(s, look, p - look) |
| 4355 | : STRNCMP(s, look, p - look)) == 0) |
| 4356 | match = TRUE; |
| 4357 | } |
| 4358 | else |
| 4359 | #endif |
| 4360 | /* TODO: multi-byte */ |
| 4361 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 4362 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 4363 | { |
| 4364 | line = ml_get_cursor(); |
| 4365 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 4366 | || !vim_iswordc(line[-(p - look) - 1])) |
| 4367 | && (icase |
| 4368 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 4369 | : STRNCMP(line - (p - look), look, p - look)) |
| 4370 | == 0) |
| 4371 | match = TRUE; |
| 4372 | } |
| 4373 | if (match && try_match_word && !try_match) |
| 4374 | { |
| 4375 | /* "0=word": Check if there are only blanks before the |
| 4376 | * word. */ |
| 4377 | if (getwhitecols_curline() != |
| 4378 | (int)(curwin->w_cursor.col - (p - look))) |
| 4379 | match = FALSE; |
| 4380 | } |
| 4381 | if (match) |
| 4382 | return TRUE; |
| 4383 | } |
| 4384 | look = p; |
| 4385 | } |
| 4386 | |
| 4387 | /* |
| 4388 | * ok, it's a boring generic character. |
| 4389 | */ |
| 4390 | else |
| 4391 | { |
| 4392 | if (try_match && *look == keytyped) |
| 4393 | return TRUE; |
| 4394 | if (*look != NUL) |
| 4395 | ++look; |
| 4396 | } |
| 4397 | |
| 4398 | /* |
| 4399 | * Skip over ", ". |
| 4400 | */ |
| 4401 | look = skip_to_option_part(look); |
| 4402 | } |
| 4403 | return FALSE; |
| 4404 | } |
| 4405 | #endif /* FEAT_CINDENT */ |
| 4406 | |
| 4407 | #if defined(FEAT_LISP) || defined(PROTO) |
| 4408 | |
| 4409 | static int |
| 4410 | lisp_match(char_u *p) |
| 4411 | { |
| 4412 | char_u buf[LSIZE]; |
| 4413 | int len; |
| 4414 | char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords; |
| 4415 | |
| 4416 | while (*word != NUL) |
| 4417 | { |
| 4418 | (void)copy_option_part(&word, buf, LSIZE, ","); |
| 4419 | len = (int)STRLEN(buf); |
| 4420 | if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') |
| 4421 | return TRUE; |
| 4422 | } |
| 4423 | return FALSE; |
| 4424 | } |
| 4425 | |
| 4426 | /* |
| 4427 | * When 'p' is present in 'cpoptions, a Vi compatible method is used. |
| 4428 | * The incompatible newer method is quite a bit better at indenting |
| 4429 | * code in lisp-like languages than the traditional one; it's still |
| 4430 | * mostly heuristics however -- Dirk van Deun, dirk@rave.org |
| 4431 | * |
| 4432 | * TODO: |
| 4433 | * Findmatch() should be adapted for lisp, also to make showmatch |
| 4434 | * work correctly: now (v5.3) it seems all C/C++ oriented: |
| 4435 | * - it does not recognize the #\( and #\) notations as character literals |
| 4436 | * - it doesn't know about comments starting with a semicolon |
| 4437 | * - it incorrectly interprets '(' as a character literal |
| 4438 | * All this messes up get_lisp_indent in some rare cases. |
| 4439 | * Update from Sergey Khorev: |
| 4440 | * I tried to fix the first two issues. |
| 4441 | */ |
| 4442 | int |
| 4443 | get_lisp_indent(void) |
| 4444 | { |
| 4445 | pos_T *pos, realpos, paren; |
| 4446 | int amount; |
| 4447 | char_u *that; |
| 4448 | colnr_T col; |
| 4449 | colnr_T firsttry; |
| 4450 | int parencount, quotecount; |
| 4451 | int vi_lisp; |
| 4452 | |
| 4453 | /* Set vi_lisp to use the vi-compatible method */ |
| 4454 | vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); |
| 4455 | |
| 4456 | realpos = curwin->w_cursor; |
| 4457 | curwin->w_cursor.col = 0; |
| 4458 | |
| 4459 | if ((pos = findmatch(NULL, '(')) == NULL) |
| 4460 | pos = findmatch(NULL, '['); |
| 4461 | else |
| 4462 | { |
| 4463 | paren = *pos; |
| 4464 | pos = findmatch(NULL, '['); |
| 4465 | if (pos == NULL || LT_POSP(pos, &paren)) |
| 4466 | pos = &paren; |
| 4467 | } |
| 4468 | if (pos != NULL) |
| 4469 | { |
| 4470 | /* Extra trick: Take the indent of the first previous non-white |
| 4471 | * line that is at the same () level. */ |
| 4472 | amount = -1; |
| 4473 | parencount = 0; |
| 4474 | |
| 4475 | while (--curwin->w_cursor.lnum >= pos->lnum) |
| 4476 | { |
| 4477 | if (linewhite(curwin->w_cursor.lnum)) |
| 4478 | continue; |
| 4479 | for (that = ml_get_curline(); *that != NUL; ++that) |
| 4480 | { |
| 4481 | if (*that == ';') |
| 4482 | { |
| 4483 | while (*(that + 1) != NUL) |
| 4484 | ++that; |
| 4485 | continue; |
| 4486 | } |
| 4487 | if (*that == '\\') |
| 4488 | { |
| 4489 | if (*(that + 1) != NUL) |
| 4490 | ++that; |
| 4491 | continue; |
| 4492 | } |
| 4493 | if (*that == '"' && *(that + 1) != NUL) |
| 4494 | { |
| 4495 | while (*++that && *that != '"') |
| 4496 | { |
| 4497 | /* skipping escaped characters in the string */ |
| 4498 | if (*that == '\\') |
| 4499 | { |
| 4500 | if (*++that == NUL) |
| 4501 | break; |
| 4502 | if (that[1] == NUL) |
| 4503 | { |
| 4504 | ++that; |
| 4505 | break; |
| 4506 | } |
| 4507 | } |
| 4508 | } |
| 4509 | } |
| 4510 | if (*that == '(' || *that == '[') |
| 4511 | ++parencount; |
| 4512 | else if (*that == ')' || *that == ']') |
| 4513 | --parencount; |
| 4514 | } |
| 4515 | if (parencount == 0) |
| 4516 | { |
| 4517 | amount = get_indent(); |
| 4518 | break; |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | if (amount == -1) |
| 4523 | { |
| 4524 | curwin->w_cursor.lnum = pos->lnum; |
| 4525 | curwin->w_cursor.col = pos->col; |
| 4526 | col = pos->col; |
| 4527 | |
| 4528 | that = ml_get_curline(); |
| 4529 | |
| 4530 | if (vi_lisp && get_indent() == 0) |
| 4531 | amount = 2; |
| 4532 | else |
| 4533 | { |
| 4534 | char_u *line = that; |
| 4535 | |
| 4536 | amount = 0; |
| 4537 | while (*that && col) |
| 4538 | { |
| 4539 | amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount); |
| 4540 | col--; |
| 4541 | } |
| 4542 | |
| 4543 | /* |
| 4544 | * Some keywords require "body" indenting rules (the |
| 4545 | * non-standard-lisp ones are Scheme special forms): |
| 4546 | * |
| 4547 | * (let ((a 1)) instead (let ((a 1)) |
| 4548 | * (...)) of (...)) |
| 4549 | */ |
| 4550 | |
| 4551 | if (!vi_lisp && (*that == '(' || *that == '[') |
| 4552 | && lisp_match(that + 1)) |
| 4553 | amount += 2; |
| 4554 | else |
| 4555 | { |
| 4556 | that++; |
| 4557 | amount++; |
| 4558 | firsttry = amount; |
| 4559 | |
| 4560 | while (VIM_ISWHITE(*that)) |
| 4561 | { |
| 4562 | amount += lbr_chartabsize(line, that, (colnr_T)amount); |
| 4563 | ++that; |
| 4564 | } |
| 4565 | |
| 4566 | if (*that && *that != ';') /* not a comment line */ |
| 4567 | { |
| 4568 | /* test *that != '(' to accommodate first let/do |
| 4569 | * argument if it is more than one line */ |
| 4570 | if (!vi_lisp && *that != '(' && *that != '[') |
| 4571 | firsttry++; |
| 4572 | |
| 4573 | parencount = 0; |
| 4574 | quotecount = 0; |
| 4575 | |
| 4576 | if (vi_lisp |
| 4577 | || (*that != '"' |
| 4578 | && *that != '\'' |
| 4579 | && *that != '#' |
| 4580 | && (*that < '0' || *that > '9'))) |
| 4581 | { |
| 4582 | while (*that |
| 4583 | && (!VIM_ISWHITE(*that) |
| 4584 | || quotecount |
| 4585 | || parencount) |
| 4586 | && (!((*that == '(' || *that == '[') |
| 4587 | && !quotecount |
| 4588 | && !parencount |
| 4589 | && vi_lisp))) |
| 4590 | { |
| 4591 | if (*that == '"') |
| 4592 | quotecount = !quotecount; |
| 4593 | if ((*that == '(' || *that == '[') |
| 4594 | && !quotecount) |
| 4595 | ++parencount; |
| 4596 | if ((*that == ')' || *that == ']') |
| 4597 | && !quotecount) |
| 4598 | --parencount; |
| 4599 | if (*that == '\\' && *(that+1) != NUL) |
| 4600 | amount += lbr_chartabsize_adv( |
| 4601 | line, &that, (colnr_T)amount); |
| 4602 | amount += lbr_chartabsize_adv( |
| 4603 | line, &that, (colnr_T)amount); |
| 4604 | } |
| 4605 | } |
| 4606 | while (VIM_ISWHITE(*that)) |
| 4607 | { |
| 4608 | amount += lbr_chartabsize( |
| 4609 | line, that, (colnr_T)amount); |
| 4610 | that++; |
| 4611 | } |
| 4612 | if (!*that || *that == ';') |
| 4613 | amount = firsttry; |
| 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | } |
| 4618 | } |
| 4619 | else |
| 4620 | amount = 0; /* no matching '(' or '[' found, use zero indent */ |
| 4621 | |
| 4622 | curwin->w_cursor = realpos; |
| 4623 | |
| 4624 | return amount; |
| 4625 | } |
| 4626 | #endif /* FEAT_LISP */ |
| 4627 | |
| 4628 | #if defined(FEAT_CINDENT) || defined(PROTO) |
| 4629 | /* |
| 4630 | * Do C or expression indenting on the current line. |
| 4631 | */ |
| 4632 | void |
| 4633 | do_c_expr_indent(void) |
| 4634 | { |
| 4635 | # ifdef FEAT_EVAL |
| 4636 | if (*curbuf->b_p_inde != NUL) |
| 4637 | fixthisline(get_expr_indent); |
| 4638 | else |
| 4639 | # endif |
| 4640 | fixthisline(get_c_indent); |
| 4641 | } |
| 4642 | #endif |
| 4643 | |
| 4644 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 4645 | /* |
| 4646 | * Re-indent the current line, based on the current contents of it and the |
| 4647 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 4648 | * confused what all the part that handles Control-T is doing that I'm not. |
| 4649 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 4650 | */ |
| 4651 | |
| 4652 | void |
| 4653 | fixthisline(int (*get_the_indent)(void)) |
| 4654 | { |
| 4655 | int amount = get_the_indent(); |
| 4656 | |
| 4657 | if (amount >= 0) |
| 4658 | { |
| 4659 | change_indent(INDENT_SET, amount, FALSE, 0, TRUE); |
| 4660 | if (linewhite(curwin->w_cursor.lnum)) |
| 4661 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | void |
| 4666 | fix_indent(void) |
| 4667 | { |
| 4668 | if (p_paste) |
| 4669 | return; |
| 4670 | # ifdef FEAT_LISP |
| 4671 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 4672 | fixthisline(get_lisp_indent); |
| 4673 | # endif |
| 4674 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 4675 | else |
| 4676 | # endif |
| 4677 | # ifdef FEAT_CINDENT |
| 4678 | if (cindent_on()) |
| 4679 | do_c_expr_indent(); |
| 4680 | # endif |
| 4681 | } |
| 4682 | |
| 4683 | #endif |