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