Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * 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); |
Bram Moolenaar | 02ad463 | 2020-01-12 13:48:18 +0100 | [diff] [blame] | 585 | if (*s == NUL) |
| 586 | break; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 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 | */ |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1633 | // foo() |
| 1634 | // { |
| 1635 | // } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 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 | |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 1783 | // Spaces from the indent of the line with an unclosed parenthesis. |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1784 | buf->b_ind_unclosed = sw * 2; |
| 1785 | |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 1786 | // Spaces from the indent of the line with an unclosed parenthesis, which |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 1800 | // an unclosed parenthesis. |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 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 | for (p = buf->b_p_cino; *p; ) |
| 1849 | { |
| 1850 | l = p++; |
| 1851 | if (*p == '-') |
| 1852 | ++p; |
| 1853 | digits = p; // remember where the digits start |
| 1854 | n = getdigits(&p); |
| 1855 | divider = 0; |
| 1856 | if (*p == '.') // ".5s" means a fraction |
| 1857 | { |
| 1858 | fraction = atol((char *)++p); |
| 1859 | while (VIM_ISDIGIT(*p)) |
| 1860 | { |
| 1861 | ++p; |
| 1862 | if (divider) |
| 1863 | divider *= 10; |
| 1864 | else |
| 1865 | divider = 10; |
| 1866 | } |
| 1867 | } |
| 1868 | if (*p == 's') // "2s" means two times 'shiftwidth' |
| 1869 | { |
| 1870 | if (p == digits) |
| 1871 | n = sw; // just "s" is one 'shiftwidth' |
| 1872 | else |
| 1873 | { |
| 1874 | n *= sw; |
| 1875 | if (divider) |
| 1876 | n += (sw * fraction + divider / 2) / divider; |
| 1877 | } |
| 1878 | ++p; |
| 1879 | } |
| 1880 | if (l[1] == '-') |
| 1881 | n = -n; |
| 1882 | |
| 1883 | // When adding an entry here, also update the default 'cinoptions' in |
| 1884 | // doc/indent.txt, and add explanation for it! |
| 1885 | switch (*l) |
| 1886 | { |
| 1887 | case '>': buf->b_ind_level = n; break; |
| 1888 | case 'e': buf->b_ind_open_imag = n; break; |
| 1889 | case 'n': buf->b_ind_no_brace = n; break; |
| 1890 | case 'f': buf->b_ind_first_open = n; break; |
| 1891 | case '{': buf->b_ind_open_extra = n; break; |
| 1892 | case '}': buf->b_ind_close_extra = n; break; |
| 1893 | case '^': buf->b_ind_open_left_imag = n; break; |
| 1894 | case 'L': buf->b_ind_jump_label = n; break; |
| 1895 | case ':': buf->b_ind_case = n; break; |
| 1896 | case '=': buf->b_ind_case_code = n; break; |
| 1897 | case 'b': buf->b_ind_case_break = n; break; |
| 1898 | case 'p': buf->b_ind_param = n; break; |
| 1899 | case 't': buf->b_ind_func_type = n; break; |
| 1900 | case '/': buf->b_ind_comment = n; break; |
| 1901 | case 'c': buf->b_ind_in_comment = n; break; |
| 1902 | case 'C': buf->b_ind_in_comment2 = n; break; |
| 1903 | case 'i': buf->b_ind_cpp_baseclass = n; break; |
| 1904 | case '+': buf->b_ind_continuation = n; break; |
| 1905 | case '(': buf->b_ind_unclosed = n; break; |
| 1906 | case 'u': buf->b_ind_unclosed2 = n; break; |
| 1907 | case 'U': buf->b_ind_unclosed_noignore = n; break; |
| 1908 | case 'W': buf->b_ind_unclosed_wrapped = n; break; |
| 1909 | case 'w': buf->b_ind_unclosed_whiteok = n; break; |
| 1910 | case 'm': buf->b_ind_matching_paren = n; break; |
| 1911 | case 'M': buf->b_ind_paren_prev = n; break; |
| 1912 | case ')': buf->b_ind_maxparen = n; break; |
| 1913 | case '*': buf->b_ind_maxcomment = n; break; |
| 1914 | case 'g': buf->b_ind_scopedecl = n; break; |
| 1915 | case 'h': buf->b_ind_scopedecl_code = n; break; |
| 1916 | case 'j': buf->b_ind_java = n; break; |
| 1917 | case 'J': buf->b_ind_js = n; break; |
| 1918 | case 'l': buf->b_ind_keep_case_label = n; break; |
| 1919 | case '#': buf->b_ind_hash_comment = n; break; |
| 1920 | case 'N': buf->b_ind_cpp_namespace = n; break; |
| 1921 | case 'k': buf->b_ind_if_for_while = n; break; |
| 1922 | case 'E': buf->b_ind_cpp_extern_c = n; break; |
| 1923 | } |
| 1924 | if (*p == ',') |
| 1925 | ++p; |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | static int |
| 1930 | find_match(int lookfor, linenr_T ourscope) |
| 1931 | { |
| 1932 | char_u *look; |
| 1933 | pos_T *theirscope; |
| 1934 | char_u *mightbeif; |
| 1935 | int elselevel; |
| 1936 | int whilelevel; |
| 1937 | |
| 1938 | if (lookfor == LOOKFOR_IF) |
| 1939 | { |
| 1940 | elselevel = 1; |
| 1941 | whilelevel = 0; |
| 1942 | } |
| 1943 | else |
| 1944 | { |
| 1945 | elselevel = 0; |
| 1946 | whilelevel = 1; |
| 1947 | } |
| 1948 | |
| 1949 | curwin->w_cursor.col = 0; |
| 1950 | |
| 1951 | while (curwin->w_cursor.lnum > ourscope + 1) |
| 1952 | { |
| 1953 | curwin->w_cursor.lnum--; |
| 1954 | curwin->w_cursor.col = 0; |
| 1955 | |
| 1956 | look = cin_skipcomment(ml_get_curline()); |
| 1957 | if (cin_iselse(look) |
| 1958 | || cin_isif(look) |
| 1959 | || cin_isdo(look) // XXX |
| 1960 | || cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
| 1961 | { |
| 1962 | // if we've gone outside the braces entirely, |
| 1963 | // we must be out of scope... |
| 1964 | theirscope = find_start_brace(); // XXX |
| 1965 | if (theirscope == NULL) |
| 1966 | break; |
| 1967 | |
| 1968 | // and if the brace enclosing this is further |
| 1969 | // back than the one enclosing the else, we're |
| 1970 | // out of luck too. |
| 1971 | if (theirscope->lnum < ourscope) |
| 1972 | break; |
| 1973 | |
| 1974 | // and if they're enclosed in a *deeper* brace, |
| 1975 | // then we can ignore it because it's in a |
| 1976 | // different scope... |
| 1977 | if (theirscope->lnum > ourscope) |
| 1978 | continue; |
| 1979 | |
| 1980 | // if it was an "else" (that's not an "else if") |
| 1981 | // then we need to go back to another if, so |
| 1982 | // increment elselevel |
| 1983 | look = cin_skipcomment(ml_get_curline()); |
| 1984 | if (cin_iselse(look)) |
| 1985 | { |
| 1986 | mightbeif = cin_skipcomment(look + 4); |
| 1987 | if (!cin_isif(mightbeif)) |
| 1988 | ++elselevel; |
| 1989 | continue; |
| 1990 | } |
| 1991 | |
| 1992 | // if it was a "while" then we need to go back to |
| 1993 | // another "do", so increment whilelevel. XXX |
| 1994 | if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
| 1995 | { |
| 1996 | ++whilelevel; |
| 1997 | continue; |
| 1998 | } |
| 1999 | |
| 2000 | // If it's an "if" decrement elselevel |
| 2001 | look = cin_skipcomment(ml_get_curline()); |
| 2002 | if (cin_isif(look)) |
| 2003 | { |
| 2004 | elselevel--; |
| 2005 | // When looking for an "if" ignore "while"s that |
| 2006 | // get in the way. |
| 2007 | if (elselevel == 0 && lookfor == LOOKFOR_IF) |
| 2008 | whilelevel = 0; |
| 2009 | } |
| 2010 | |
| 2011 | // If it's a "do" decrement whilelevel |
| 2012 | if (cin_isdo(look)) |
| 2013 | whilelevel--; |
| 2014 | |
| 2015 | // if we've used up all the elses, then |
| 2016 | // this must be the if that we want! |
| 2017 | // match the indent level of that if. |
| 2018 | if (elselevel <= 0 && whilelevel <= 0) |
| 2019 | return OK; |
| 2020 | } |
| 2021 | } |
| 2022 | return FAIL; |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * Return the desired indent for C code. |
| 2027 | * Return -1 if the indent should be left alone (inside a raw string). |
| 2028 | */ |
| 2029 | int |
| 2030 | get_c_indent(void) |
| 2031 | { |
| 2032 | pos_T cur_curpos; |
| 2033 | int amount; |
| 2034 | int scope_amount; |
| 2035 | int cur_amount = MAXCOL; |
| 2036 | colnr_T col; |
| 2037 | char_u *theline; |
| 2038 | char_u *linecopy; |
| 2039 | pos_T *trypos; |
| 2040 | pos_T *comment_pos; |
| 2041 | pos_T *tryposBrace = NULL; |
| 2042 | pos_T tryposCopy; |
| 2043 | pos_T our_paren_pos; |
| 2044 | char_u *start; |
| 2045 | int start_brace; |
| 2046 | #define BRACE_IN_COL0 1 // '{' is in column 0 |
| 2047 | #define BRACE_AT_START 2 // '{' is at start of line |
| 2048 | #define BRACE_AT_END 3 // '{' is at end of line |
| 2049 | linenr_T ourscope; |
| 2050 | char_u *l; |
| 2051 | char_u *look; |
| 2052 | char_u terminated; |
| 2053 | int lookfor; |
| 2054 | int whilelevel; |
| 2055 | linenr_T lnum; |
| 2056 | int n; |
| 2057 | int iscase; |
| 2058 | int lookfor_break; |
| 2059 | int lookfor_cpp_namespace = FALSE; |
| 2060 | int cont_amount = 0; // amount for continuation line |
| 2061 | int original_line_islabel; |
| 2062 | int added_to_amount = 0; |
| 2063 | int js_cur_has_key = 0; |
| 2064 | linenr_T raw_string_start = 0; |
| 2065 | cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } }; |
| 2066 | |
| 2067 | // make a copy, value is changed below |
| 2068 | int ind_continuation = curbuf->b_ind_continuation; |
| 2069 | |
| 2070 | // remember where the cursor was when we started |
| 2071 | cur_curpos = curwin->w_cursor; |
| 2072 | |
| 2073 | // if we are at line 1 zero indent is fine, right? |
| 2074 | if (cur_curpos.lnum == 1) |
| 2075 | return 0; |
| 2076 | |
| 2077 | // Get a copy of the current contents of the line. |
| 2078 | // This is required, because only the most recent line obtained with |
| 2079 | // ml_get is valid! |
| 2080 | linecopy = vim_strsave(ml_get(cur_curpos.lnum)); |
| 2081 | if (linecopy == NULL) |
| 2082 | return 0; |
| 2083 | |
| 2084 | // In insert mode and the cursor is on a ')' truncate the line at the |
| 2085 | // cursor position. We don't want to line up with the matching '(' when |
| 2086 | // inserting new stuff. |
| 2087 | // For unknown reasons the cursor might be past the end of the line, thus |
| 2088 | // check for that. |
| 2089 | if ((State & INSERT) |
| 2090 | && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
| 2091 | && linecopy[curwin->w_cursor.col] == ')') |
| 2092 | linecopy[curwin->w_cursor.col] = NUL; |
| 2093 | |
| 2094 | theline = skipwhite(linecopy); |
| 2095 | |
| 2096 | // move the cursor to the start of the line |
| 2097 | |
| 2098 | curwin->w_cursor.col = 0; |
| 2099 | |
| 2100 | original_line_islabel = cin_islabel(); // XXX |
| 2101 | |
| 2102 | // If we are inside a raw string don't change the indent. |
| 2103 | // Ignore a raw string inside a comment. |
| 2104 | comment_pos = ind_find_start_comment(); |
| 2105 | if (comment_pos != NULL) |
| 2106 | { |
| 2107 | // findmatchlimit() static pos is overwritten, make a copy |
| 2108 | tryposCopy = *comment_pos; |
| 2109 | comment_pos = &tryposCopy; |
| 2110 | } |
| 2111 | trypos = find_start_rawstring(curbuf->b_ind_maxcomment); |
| 2112 | if (trypos != NULL && (comment_pos == NULL |
| 2113 | || LT_POS(*trypos, *comment_pos))) |
| 2114 | { |
| 2115 | amount = -1; |
| 2116 | goto laterend; |
| 2117 | } |
| 2118 | |
| 2119 | // #defines and so on always go at the left when included in 'cinkeys'. |
| 2120 | if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) |
| 2121 | { |
| 2122 | amount = curbuf->b_ind_hash_comment; |
| 2123 | goto theend; |
| 2124 | } |
| 2125 | |
| 2126 | // Is it a non-case label? Then that goes at the left margin too unless: |
| 2127 | // - JS flag is set. |
| 2128 | // - 'L' item has a positive value. |
| 2129 | if (original_line_islabel && !curbuf->b_ind_js |
| 2130 | && curbuf->b_ind_jump_label < 0) |
| 2131 | { |
| 2132 | amount = 0; |
| 2133 | goto theend; |
| 2134 | } |
| 2135 | |
| 2136 | // If we're inside a "//" comment and there is a "//" comment in a |
| 2137 | // previous line, lineup with that one. |
| 2138 | if (cin_islinecomment(theline) |
| 2139 | && (trypos = find_line_comment()) != NULL) // XXX |
| 2140 | { |
| 2141 | // find how indented the line beginning the comment is |
| 2142 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 2143 | amount = col; |
| 2144 | goto theend; |
| 2145 | } |
| 2146 | |
| 2147 | // If we're inside a comment and not looking at the start of the |
| 2148 | // comment, try using the 'comments' option. |
| 2149 | if (!cin_iscomment(theline) && comment_pos != NULL) // XXX |
| 2150 | { |
| 2151 | int lead_start_len = 2; |
| 2152 | int lead_middle_len = 1; |
| 2153 | char_u lead_start[COM_MAX_LEN]; // start-comment string |
| 2154 | char_u lead_middle[COM_MAX_LEN]; // middle-comment string |
| 2155 | char_u lead_end[COM_MAX_LEN]; // end-comment string |
| 2156 | char_u *p; |
| 2157 | int start_align = 0; |
| 2158 | int start_off = 0; |
| 2159 | int done = FALSE; |
| 2160 | |
| 2161 | // find how indented the line beginning the comment is |
| 2162 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
| 2163 | amount = col; |
| 2164 | *lead_start = NUL; |
| 2165 | *lead_middle = NUL; |
| 2166 | |
| 2167 | p = curbuf->b_p_com; |
| 2168 | while (*p != NUL) |
| 2169 | { |
| 2170 | int align = 0; |
| 2171 | int off = 0; |
| 2172 | int what = 0; |
| 2173 | |
| 2174 | while (*p != NUL && *p != ':') |
| 2175 | { |
| 2176 | if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) |
| 2177 | what = *p++; |
| 2178 | else if (*p == COM_LEFT || *p == COM_RIGHT) |
| 2179 | align = *p++; |
| 2180 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 2181 | off = getdigits(&p); |
| 2182 | else |
| 2183 | ++p; |
| 2184 | } |
| 2185 | |
| 2186 | if (*p == ':') |
| 2187 | ++p; |
| 2188 | (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 2189 | if (what == COM_START) |
| 2190 | { |
| 2191 | STRCPY(lead_start, lead_end); |
| 2192 | lead_start_len = (int)STRLEN(lead_start); |
| 2193 | start_off = off; |
| 2194 | start_align = align; |
| 2195 | } |
| 2196 | else if (what == COM_MIDDLE) |
| 2197 | { |
| 2198 | STRCPY(lead_middle, lead_end); |
| 2199 | lead_middle_len = (int)STRLEN(lead_middle); |
| 2200 | } |
| 2201 | else if (what == COM_END) |
| 2202 | { |
| 2203 | // If our line starts with the middle comment string, line it |
| 2204 | // up with the comment opener per the 'comments' option. |
| 2205 | if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 |
| 2206 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) |
| 2207 | { |
| 2208 | done = TRUE; |
| 2209 | if (curwin->w_cursor.lnum > 1) |
| 2210 | { |
| 2211 | // If the start comment string matches in the previous |
| 2212 | // line, use the indent of that line plus offset. If |
| 2213 | // the middle comment string matches in the previous |
| 2214 | // line, use the indent of that line. XXX |
| 2215 | look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); |
| 2216 | if (STRNCMP(look, lead_start, lead_start_len) == 0) |
| 2217 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 2218 | else if (STRNCMP(look, lead_middle, |
| 2219 | lead_middle_len) == 0) |
| 2220 | { |
| 2221 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 2222 | break; |
| 2223 | } |
| 2224 | // If the start comment string doesn't match with the |
| 2225 | // start of the comment, skip this entry. XXX |
| 2226 | else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, |
| 2227 | lead_start, lead_start_len) != 0) |
| 2228 | continue; |
| 2229 | } |
| 2230 | if (start_off != 0) |
| 2231 | amount += start_off; |
| 2232 | else if (start_align == COM_RIGHT) |
| 2233 | amount += vim_strsize(lead_start) |
| 2234 | - vim_strsize(lead_middle); |
| 2235 | break; |
| 2236 | } |
| 2237 | |
| 2238 | // If our line starts with the end comment string, line it up |
| 2239 | // with the middle comment |
| 2240 | if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 |
| 2241 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) |
| 2242 | { |
| 2243 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 2244 | // XXX |
| 2245 | if (off != 0) |
| 2246 | amount += off; |
| 2247 | else if (align == COM_RIGHT) |
| 2248 | amount += vim_strsize(lead_start) |
| 2249 | - vim_strsize(lead_middle); |
| 2250 | done = TRUE; |
| 2251 | break; |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | // If our line starts with an asterisk, line up with the |
| 2257 | // asterisk in the comment opener; otherwise, line up |
| 2258 | // with the first character of the comment text. |
| 2259 | if (done) |
| 2260 | ; |
| 2261 | else if (theline[0] == '*') |
| 2262 | amount += 1; |
| 2263 | else |
| 2264 | { |
| 2265 | // If we are more than one line away from the comment opener, take |
| 2266 | // the indent of the previous non-empty line. If 'cino' has "CO" |
| 2267 | // and we are just below the comment opener and there are any |
| 2268 | // white characters after it line up with the text after it; |
| 2269 | // otherwise, add the amount specified by "c" in 'cino' |
| 2270 | amount = -1; |
| 2271 | for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum) |
| 2272 | { |
| 2273 | if (linewhite(lnum)) // skip blank lines |
| 2274 | continue; |
| 2275 | amount = get_indent_lnum(lnum); // XXX |
| 2276 | break; |
| 2277 | } |
| 2278 | if (amount == -1) // use the comment opener |
| 2279 | { |
| 2280 | if (!curbuf->b_ind_in_comment2) |
| 2281 | { |
| 2282 | start = ml_get(comment_pos->lnum); |
| 2283 | look = start + comment_pos->col + 2; // skip / and * |
| 2284 | if (*look != NUL) // if something after it |
| 2285 | comment_pos->col = (colnr_T)(skipwhite(look) - start); |
| 2286 | } |
| 2287 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
| 2288 | amount = col; |
| 2289 | if (curbuf->b_ind_in_comment2 || *look == NUL) |
| 2290 | amount += curbuf->b_ind_in_comment; |
| 2291 | } |
| 2292 | } |
| 2293 | goto theend; |
| 2294 | } |
| 2295 | |
| 2296 | // Are we looking at a ']' that has a match? |
| 2297 | if (*skipwhite(theline) == ']' |
| 2298 | && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) |
| 2299 | { |
| 2300 | // align with the line containing the '['. |
| 2301 | amount = get_indent_lnum(trypos->lnum); |
| 2302 | goto theend; |
| 2303 | } |
| 2304 | |
| 2305 | // Are we inside parentheses or braces? XXX |
| 2306 | if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL |
| 2307 | && curbuf->b_ind_java == 0) |
| 2308 | || (tryposBrace = find_start_brace()) != NULL |
| 2309 | || trypos != NULL) |
| 2310 | { |
| 2311 | if (trypos != NULL && tryposBrace != NULL) |
| 2312 | { |
| 2313 | // Both an unmatched '(' and '{' is found. Use the one which is |
| 2314 | // closer to the current cursor position, set the other to NULL. |
| 2315 | if (trypos->lnum != tryposBrace->lnum |
| 2316 | ? trypos->lnum < tryposBrace->lnum |
| 2317 | : trypos->col < tryposBrace->col) |
| 2318 | trypos = NULL; |
| 2319 | else |
| 2320 | tryposBrace = NULL; |
| 2321 | } |
| 2322 | |
| 2323 | if (trypos != NULL) |
| 2324 | { |
| 2325 | // If the matching paren is more than one line away, use the indent of |
| 2326 | // a previous non-empty line that matches the same paren. |
| 2327 | if (theline[0] == ')' && curbuf->b_ind_paren_prev) |
| 2328 | { |
| 2329 | // Line up with the start of the matching paren line. |
| 2330 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX |
| 2331 | } |
| 2332 | else |
| 2333 | { |
| 2334 | amount = -1; |
| 2335 | our_paren_pos = *trypos; |
| 2336 | for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) |
| 2337 | { |
| 2338 | l = skipwhite(ml_get(lnum)); |
| 2339 | if (cin_nocode(l)) // skip comment lines |
| 2340 | continue; |
| 2341 | if (cin_ispreproc_cont(&l, &lnum, &amount)) |
| 2342 | continue; // ignore #define, #if, etc. |
| 2343 | curwin->w_cursor.lnum = lnum; |
| 2344 | |
| 2345 | // Skip a comment or raw string. XXX |
| 2346 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
| 2347 | { |
| 2348 | lnum = trypos->lnum + 1; |
| 2349 | continue; |
| 2350 | } |
| 2351 | |
| 2352 | // XXX |
| 2353 | if ((trypos = find_match_paren( |
| 2354 | corr_ind_maxparen(&cur_curpos))) != NULL |
| 2355 | && trypos->lnum == our_paren_pos.lnum |
| 2356 | && trypos->col == our_paren_pos.col) |
| 2357 | { |
| 2358 | amount = get_indent_lnum(lnum); // XXX |
| 2359 | |
| 2360 | if (theline[0] == ')') |
| 2361 | { |
| 2362 | if (our_paren_pos.lnum != lnum |
| 2363 | && cur_amount > amount) |
| 2364 | cur_amount = amount; |
| 2365 | amount = -1; |
| 2366 | } |
| 2367 | break; |
| 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | // Line up with line where the matching paren is. XXX |
| 2373 | // If the line starts with a '(' or the indent for unclosed |
| 2374 | // parentheses is zero, line up with the unclosed parentheses. |
| 2375 | if (amount == -1) |
| 2376 | { |
| 2377 | int ignore_paren_col = 0; |
| 2378 | int is_if_for_while = 0; |
| 2379 | |
| 2380 | if (curbuf->b_ind_if_for_while) |
| 2381 | { |
| 2382 | // Look for the outermost opening parenthesis on this line |
| 2383 | // and check whether it belongs to an "if", "for" or "while". |
| 2384 | |
| 2385 | pos_T cursor_save = curwin->w_cursor; |
| 2386 | pos_T outermost; |
| 2387 | char_u *line; |
| 2388 | |
| 2389 | trypos = &our_paren_pos; |
| 2390 | do { |
| 2391 | outermost = *trypos; |
| 2392 | curwin->w_cursor.lnum = outermost.lnum; |
| 2393 | curwin->w_cursor.col = outermost.col; |
| 2394 | |
| 2395 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
| 2396 | } while (trypos && trypos->lnum == outermost.lnum); |
| 2397 | |
| 2398 | curwin->w_cursor = cursor_save; |
| 2399 | |
| 2400 | line = ml_get(outermost.lnum); |
| 2401 | |
| 2402 | is_if_for_while = |
| 2403 | cin_is_if_for_while_before_offset(line, &outermost.col); |
| 2404 | } |
| 2405 | |
| 2406 | amount = skip_label(our_paren_pos.lnum, &look); |
| 2407 | look = skipwhite(look); |
| 2408 | if (*look == '(') |
| 2409 | { |
| 2410 | linenr_T save_lnum = curwin->w_cursor.lnum; |
| 2411 | char_u *line; |
| 2412 | int look_col; |
| 2413 | |
| 2414 | // Ignore a '(' in front of the line that has a match before |
| 2415 | // our matching '('. |
| 2416 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 2417 | line = ml_get_curline(); |
| 2418 | look_col = (int)(look - line); |
| 2419 | curwin->w_cursor.col = look_col + 1; |
| 2420 | if ((trypos = findmatchlimit(NULL, ')', 0, |
| 2421 | curbuf->b_ind_maxparen)) |
| 2422 | != NULL |
| 2423 | && trypos->lnum == our_paren_pos.lnum |
| 2424 | && trypos->col < our_paren_pos.col) |
| 2425 | ignore_paren_col = trypos->col + 1; |
| 2426 | |
| 2427 | curwin->w_cursor.lnum = save_lnum; |
| 2428 | look = ml_get(our_paren_pos.lnum) + look_col; |
| 2429 | } |
| 2430 | if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 |
| 2431 | && is_if_for_while == 0) |
| 2432 | || (!curbuf->b_ind_unclosed_noignore && *look == '(' |
| 2433 | && ignore_paren_col == 0)) |
| 2434 | { |
| 2435 | // If we're looking at a close paren, line up right there; |
| 2436 | // otherwise, line up with the next (non-white) character. |
| 2437 | // When b_ind_unclosed_wrapped is set and the matching paren is |
| 2438 | // the last nonwhite character of the line, use either the |
| 2439 | // indent of the current line or the indentation of the next |
| 2440 | // outer paren and add b_ind_unclosed_wrapped (for very long |
| 2441 | // lines). |
| 2442 | if (theline[0] != ')') |
| 2443 | { |
| 2444 | cur_amount = MAXCOL; |
| 2445 | l = ml_get(our_paren_pos.lnum); |
| 2446 | if (curbuf->b_ind_unclosed_wrapped |
| 2447 | && cin_ends_in(l, (char_u *)"(", NULL)) |
| 2448 | { |
| 2449 | // look for opening unmatched paren, indent one level |
| 2450 | // for each additional level |
| 2451 | n = 1; |
| 2452 | for (col = 0; col < our_paren_pos.col; ++col) |
| 2453 | { |
| 2454 | switch (l[col]) |
| 2455 | { |
| 2456 | case '(': |
| 2457 | case '{': ++n; |
| 2458 | break; |
| 2459 | |
| 2460 | case ')': |
| 2461 | case '}': if (n > 1) |
| 2462 | --n; |
| 2463 | break; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | our_paren_pos.col = 0; |
| 2468 | amount += n * curbuf->b_ind_unclosed_wrapped; |
| 2469 | } |
| 2470 | else if (curbuf->b_ind_unclosed_whiteok) |
| 2471 | our_paren_pos.col++; |
| 2472 | else |
| 2473 | { |
| 2474 | col = our_paren_pos.col + 1; |
| 2475 | while (VIM_ISWHITE(l[col])) |
| 2476 | col++; |
| 2477 | if (l[col] != NUL) // In case of trailing space |
| 2478 | our_paren_pos.col = col; |
| 2479 | else |
| 2480 | our_paren_pos.col++; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | // Find how indented the paren is, or the character after it |
| 2485 | // if we did the above "if". |
| 2486 | if (our_paren_pos.col > 0) |
| 2487 | { |
| 2488 | getvcol(curwin, &our_paren_pos, &col, NULL, NULL); |
| 2489 | if (cur_amount > (int)col) |
| 2490 | cur_amount = col; |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | if (theline[0] == ')' && curbuf->b_ind_matching_paren) |
| 2495 | { |
| 2496 | // Line up with the start of the matching paren line. |
| 2497 | } |
| 2498 | else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) |
| 2499 | || (!curbuf->b_ind_unclosed_noignore |
| 2500 | && *look == '(' && ignore_paren_col == 0)) |
| 2501 | { |
| 2502 | if (cur_amount != MAXCOL) |
| 2503 | amount = cur_amount; |
| 2504 | } |
| 2505 | else |
| 2506 | { |
| 2507 | // Add b_ind_unclosed2 for each '(' before our matching one, |
| 2508 | // but ignore (void) before the line (ignore_paren_col). |
| 2509 | col = our_paren_pos.col; |
| 2510 | while ((int)our_paren_pos.col > ignore_paren_col) |
| 2511 | { |
| 2512 | --our_paren_pos.col; |
| 2513 | switch (*ml_get_pos(&our_paren_pos)) |
| 2514 | { |
| 2515 | case '(': amount += curbuf->b_ind_unclosed2; |
| 2516 | col = our_paren_pos.col; |
| 2517 | break; |
| 2518 | case ')': amount -= curbuf->b_ind_unclosed2; |
| 2519 | col = MAXCOL; |
| 2520 | break; |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | // Use b_ind_unclosed once, when the first '(' is not inside |
| 2525 | // braces |
| 2526 | if (col == MAXCOL) |
| 2527 | amount += curbuf->b_ind_unclosed; |
| 2528 | else |
| 2529 | { |
| 2530 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 2531 | curwin->w_cursor.col = col; |
| 2532 | if (find_match_paren_after_brace(curbuf->b_ind_maxparen) |
| 2533 | != NULL) |
| 2534 | amount += curbuf->b_ind_unclosed2; |
| 2535 | else |
| 2536 | { |
| 2537 | if (is_if_for_while) |
| 2538 | amount += curbuf->b_ind_if_for_while; |
| 2539 | else |
| 2540 | amount += curbuf->b_ind_unclosed; |
| 2541 | } |
| 2542 | } |
| 2543 | // For a line starting with ')' use the minimum of the two |
| 2544 | // positions, to avoid giving it more indent than the previous |
| 2545 | // lines: |
| 2546 | // func_long_name( if (x |
| 2547 | // arg && yy |
| 2548 | // ) ^ not here ) ^ not here |
| 2549 | if (cur_amount < amount) |
| 2550 | amount = cur_amount; |
| 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | // add extra indent for a comment |
| 2555 | if (cin_iscomment(theline)) |
| 2556 | amount += curbuf->b_ind_comment; |
| 2557 | } |
| 2558 | else |
| 2559 | { |
| 2560 | // We are inside braces, there is a { before this line at the position |
| 2561 | // stored in tryposBrace. |
| 2562 | // Make a copy of tryposBrace, it may point to pos_copy inside |
| 2563 | // find_start_brace(), which may be changed somewhere. |
| 2564 | tryposCopy = *tryposBrace; |
| 2565 | tryposBrace = &tryposCopy; |
| 2566 | trypos = tryposBrace; |
| 2567 | ourscope = trypos->lnum; |
| 2568 | start = ml_get(ourscope); |
| 2569 | |
| 2570 | // Now figure out how indented the line is in general. |
| 2571 | // If the brace was at the start of the line, we use that; |
| 2572 | // otherwise, check out the indentation of the line as |
| 2573 | // a whole and then add the "imaginary indent" to that. |
| 2574 | look = skipwhite(start); |
| 2575 | if (*look == '{') |
| 2576 | { |
| 2577 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 2578 | amount = col; |
| 2579 | if (*start == '{') |
| 2580 | start_brace = BRACE_IN_COL0; |
| 2581 | else |
| 2582 | start_brace = BRACE_AT_START; |
| 2583 | } |
| 2584 | else |
| 2585 | { |
| 2586 | // That opening brace might have been on a continuation |
| 2587 | // line. if so, find the start of the line. |
| 2588 | curwin->w_cursor.lnum = ourscope; |
| 2589 | |
| 2590 | // Position the cursor over the rightmost paren, so that |
| 2591 | // matching it will take us back to the start of the line. |
| 2592 | lnum = ourscope; |
| 2593 | if (find_last_paren(start, '(', ')') |
| 2594 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) |
| 2595 | != NULL) |
| 2596 | lnum = trypos->lnum; |
| 2597 | |
| 2598 | // It could have been something like |
| 2599 | // case 1: if (asdf && |
| 2600 | // ldfd) { |
| 2601 | // } |
| 2602 | if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) |
| 2603 | && cin_iscase(skipwhite(ml_get_curline()), FALSE)) |
| 2604 | amount = get_indent(); |
| 2605 | else if (curbuf->b_ind_js) |
| 2606 | amount = get_indent_lnum(lnum); |
| 2607 | else |
| 2608 | amount = skip_label(lnum, &l); |
| 2609 | |
| 2610 | start_brace = BRACE_AT_END; |
| 2611 | } |
| 2612 | |
| 2613 | // For Javascript check if the line starts with "key:". |
| 2614 | if (curbuf->b_ind_js) |
| 2615 | js_cur_has_key = cin_has_js_key(theline); |
| 2616 | |
| 2617 | // If we're looking at a closing brace, that's where |
| 2618 | // we want to be. otherwise, add the amount of room |
| 2619 | // that an indent is supposed to be. |
| 2620 | if (theline[0] == '}') |
| 2621 | { |
| 2622 | // they may want closing braces to line up with something |
| 2623 | // other than the open brace. indulge them, if so. |
| 2624 | amount += curbuf->b_ind_close_extra; |
| 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | // If we're looking at an "else", try to find an "if" |
| 2629 | // to match it with. |
| 2630 | // If we're looking at a "while", try to find a "do" |
| 2631 | // to match it with. |
| 2632 | lookfor = LOOKFOR_INITIAL; |
| 2633 | if (cin_iselse(theline)) |
| 2634 | lookfor = LOOKFOR_IF; |
| 2635 | else if (cin_iswhileofdo(theline, cur_curpos.lnum)) // XXX |
| 2636 | lookfor = LOOKFOR_DO; |
| 2637 | if (lookfor != LOOKFOR_INITIAL) |
| 2638 | { |
| 2639 | curwin->w_cursor.lnum = cur_curpos.lnum; |
| 2640 | if (find_match(lookfor, ourscope) == OK) |
| 2641 | { |
| 2642 | amount = get_indent(); // XXX |
| 2643 | goto theend; |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | // We get here if we are not on an "while-of-do" or "else" (or |
| 2648 | // failed to find a matching "if"). |
| 2649 | // Search backwards for something to line up with. |
| 2650 | // First set amount for when we don't find anything. |
| 2651 | |
| 2652 | // if the '{' is _really_ at the left margin, use the imaginary |
| 2653 | // location of a left-margin brace. Otherwise, correct the |
| 2654 | // location for b_ind_open_extra. |
| 2655 | |
| 2656 | if (start_brace == BRACE_IN_COL0) // '{' is in column 0 |
| 2657 | { |
| 2658 | amount = curbuf->b_ind_open_left_imag; |
| 2659 | lookfor_cpp_namespace = TRUE; |
| 2660 | } |
| 2661 | else if (start_brace == BRACE_AT_START && |
| 2662 | lookfor_cpp_namespace) // '{' is at start |
| 2663 | { |
| 2664 | |
| 2665 | lookfor_cpp_namespace = TRUE; |
| 2666 | } |
| 2667 | else |
| 2668 | { |
| 2669 | if (start_brace == BRACE_AT_END) // '{' is at end of line |
| 2670 | { |
| 2671 | amount += curbuf->b_ind_open_imag; |
| 2672 | |
| 2673 | l = skipwhite(ml_get_curline()); |
| 2674 | if (cin_is_cpp_namespace(l)) |
| 2675 | amount += curbuf->b_ind_cpp_namespace; |
| 2676 | else if (cin_is_cpp_extern_c(l)) |
| 2677 | amount += curbuf->b_ind_cpp_extern_c; |
| 2678 | } |
| 2679 | else |
| 2680 | { |
| 2681 | // Compensate for adding b_ind_open_extra later. |
| 2682 | amount -= curbuf->b_ind_open_extra; |
| 2683 | if (amount < 0) |
| 2684 | amount = 0; |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | lookfor_break = FALSE; |
| 2689 | |
| 2690 | if (cin_iscase(theline, FALSE)) // it's a switch() label |
| 2691 | { |
| 2692 | lookfor = LOOKFOR_CASE; // find a previous switch() label |
| 2693 | amount += curbuf->b_ind_case; |
| 2694 | } |
| 2695 | else if (cin_isscopedecl(theline)) // private:, ... |
| 2696 | { |
| 2697 | lookfor = LOOKFOR_SCOPEDECL; // class decl is this block |
| 2698 | amount += curbuf->b_ind_scopedecl; |
| 2699 | } |
| 2700 | else |
| 2701 | { |
| 2702 | if (curbuf->b_ind_case_break && cin_isbreak(theline)) |
| 2703 | // break; ... |
| 2704 | lookfor_break = TRUE; |
| 2705 | |
| 2706 | lookfor = LOOKFOR_INITIAL; |
| 2707 | // b_ind_level from start of block |
| 2708 | amount += curbuf->b_ind_level; |
| 2709 | } |
| 2710 | scope_amount = amount; |
| 2711 | whilelevel = 0; |
| 2712 | |
| 2713 | // Search backwards. If we find something we recognize, line up |
| 2714 | // with that. |
| 2715 | // |
| 2716 | // If we're looking at an open brace, indent |
| 2717 | // the usual amount relative to the conditional |
| 2718 | // that opens the block. |
| 2719 | curwin->w_cursor = cur_curpos; |
| 2720 | for (;;) |
| 2721 | { |
| 2722 | curwin->w_cursor.lnum--; |
| 2723 | curwin->w_cursor.col = 0; |
| 2724 | |
| 2725 | // If we went all the way back to the start of our scope, line |
| 2726 | // up with it. |
| 2727 | if (curwin->w_cursor.lnum <= ourscope) |
| 2728 | { |
| 2729 | // We reached end of scope: |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 2730 | // If looking for an enum or structure initialization |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2731 | // go further back: |
| 2732 | // If it is an initializer (enum xxx or xxx =), then |
| 2733 | // don't add ind_continuation, otherwise it is a variable |
| 2734 | // declaration: |
| 2735 | // int x, |
| 2736 | // here; <-- add ind_continuation |
| 2737 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 2738 | { |
| 2739 | if (curwin->w_cursor.lnum == 0 |
| 2740 | || curwin->w_cursor.lnum |
| 2741 | < ourscope - curbuf->b_ind_maxparen) |
| 2742 | { |
| 2743 | // nothing found (abuse curbuf->b_ind_maxparen as |
| 2744 | // limit) assume terminated line (i.e. a variable |
| 2745 | // initialization) |
| 2746 | if (cont_amount > 0) |
| 2747 | amount = cont_amount; |
| 2748 | else if (!curbuf->b_ind_js) |
| 2749 | amount += ind_continuation; |
| 2750 | break; |
| 2751 | } |
| 2752 | |
| 2753 | l = ml_get_curline(); |
| 2754 | |
| 2755 | // If we're in a comment or raw string now, skip to |
| 2756 | // the start of it. |
| 2757 | trypos = ind_find_start_CORS(NULL); |
| 2758 | if (trypos != NULL) |
| 2759 | { |
| 2760 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2761 | curwin->w_cursor.col = 0; |
| 2762 | continue; |
| 2763 | } |
| 2764 | |
| 2765 | // Skip preprocessor directives and blank lines. |
| 2766 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 2767 | &amount)) |
| 2768 | continue; |
| 2769 | |
| 2770 | if (cin_nocode(l)) |
| 2771 | continue; |
| 2772 | |
| 2773 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 2774 | |
| 2775 | // If we are at top level and the line looks like a |
| 2776 | // function declaration, we are done |
| 2777 | // (it's a variable declaration). |
| 2778 | if (start_brace != BRACE_IN_COL0 |
| 2779 | || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 2780 | { |
| 2781 | // if the line is terminated with another ',' |
| 2782 | // it is a continued variable initialization. |
| 2783 | // don't add extra indent. |
| 2784 | // TODO: does not work, if a function |
| 2785 | // declaration is split over multiple lines: |
| 2786 | // cin_isfuncdecl returns FALSE then. |
| 2787 | if (terminated == ',') |
| 2788 | break; |
| 2789 | |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 2790 | // if it is an enum declaration or an assignment, |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2791 | // we are done. |
| 2792 | if (terminated != ';' && cin_isinit()) |
| 2793 | break; |
| 2794 | |
| 2795 | // nothing useful found |
| 2796 | if (terminated == 0 || terminated == '{') |
| 2797 | continue; |
| 2798 | } |
| 2799 | |
| 2800 | if (terminated != ';') |
| 2801 | { |
| 2802 | // Skip parens and braces. Position the cursor |
| 2803 | // over the rightmost paren, so that matching it |
| 2804 | // will take us back to the start of the line. |
| 2805 | // XXX |
| 2806 | trypos = NULL; |
| 2807 | if (find_last_paren(l, '(', ')')) |
| 2808 | trypos = find_match_paren( |
| 2809 | curbuf->b_ind_maxparen); |
| 2810 | |
| 2811 | if (trypos == NULL && find_last_paren(l, '{', '}')) |
| 2812 | trypos = find_start_brace(); |
| 2813 | |
| 2814 | if (trypos != NULL) |
| 2815 | { |
| 2816 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2817 | curwin->w_cursor.col = 0; |
| 2818 | continue; |
| 2819 | } |
| 2820 | } |
| 2821 | |
| 2822 | // it's a variable declaration, add indentation |
| 2823 | // like in |
| 2824 | // int a, |
| 2825 | // b; |
| 2826 | if (cont_amount > 0) |
| 2827 | amount = cont_amount; |
| 2828 | else |
| 2829 | amount += ind_continuation; |
| 2830 | } |
| 2831 | else if (lookfor == LOOKFOR_UNTERM) |
| 2832 | { |
| 2833 | if (cont_amount > 0) |
| 2834 | amount = cont_amount; |
| 2835 | else |
| 2836 | amount += ind_continuation; |
| 2837 | } |
| 2838 | else |
| 2839 | { |
| 2840 | if (lookfor != LOOKFOR_TERM |
| 2841 | && lookfor != LOOKFOR_CPP_BASECLASS |
| 2842 | && lookfor != LOOKFOR_COMMA) |
| 2843 | { |
| 2844 | amount = scope_amount; |
| 2845 | if (theline[0] == '{') |
| 2846 | { |
| 2847 | amount += curbuf->b_ind_open_extra; |
| 2848 | added_to_amount = curbuf->b_ind_open_extra; |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | if (lookfor_cpp_namespace) |
| 2853 | { |
| 2854 | // Looking for C++ namespace, need to look further |
| 2855 | // back. |
| 2856 | if (curwin->w_cursor.lnum == ourscope) |
| 2857 | continue; |
| 2858 | |
| 2859 | if (curwin->w_cursor.lnum == 0 |
| 2860 | || curwin->w_cursor.lnum |
| 2861 | < ourscope - FIND_NAMESPACE_LIM) |
| 2862 | break; |
| 2863 | |
| 2864 | l = ml_get_curline(); |
| 2865 | |
| 2866 | // If we're in a comment or raw string now, skip |
| 2867 | // to the start of it. |
| 2868 | trypos = ind_find_start_CORS(NULL); |
| 2869 | if (trypos != NULL) |
| 2870 | { |
| 2871 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2872 | curwin->w_cursor.col = 0; |
| 2873 | continue; |
| 2874 | } |
| 2875 | |
| 2876 | // Skip preprocessor directives and blank lines. |
| 2877 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 2878 | &amount)) |
| 2879 | continue; |
| 2880 | |
| 2881 | // Finally the actual check for "namespace". |
| 2882 | if (cin_is_cpp_namespace(l)) |
| 2883 | { |
| 2884 | amount += curbuf->b_ind_cpp_namespace |
| 2885 | - added_to_amount; |
| 2886 | break; |
| 2887 | } |
| 2888 | else if (cin_is_cpp_extern_c(l)) |
| 2889 | { |
| 2890 | amount += curbuf->b_ind_cpp_extern_c |
| 2891 | - added_to_amount; |
| 2892 | break; |
| 2893 | } |
| 2894 | |
| 2895 | if (cin_nocode(l)) |
| 2896 | continue; |
| 2897 | } |
| 2898 | } |
| 2899 | break; |
| 2900 | } |
| 2901 | |
| 2902 | // If we're in a comment or raw string now, skip to the start |
| 2903 | // of it. XXX |
| 2904 | if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL) |
| 2905 | { |
| 2906 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 2907 | curwin->w_cursor.col = 0; |
| 2908 | continue; |
| 2909 | } |
| 2910 | |
| 2911 | l = ml_get_curline(); |
| 2912 | |
| 2913 | // If this is a switch() label, may line up relative to that. |
| 2914 | // If this is a C++ scope declaration, do the same. |
| 2915 | iscase = cin_iscase(l, FALSE); |
| 2916 | if (iscase || cin_isscopedecl(l)) |
| 2917 | { |
| 2918 | // we are only looking for cpp base class |
| 2919 | // declaration/initialization any longer |
| 2920 | if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 2921 | break; |
| 2922 | |
| 2923 | // When looking for a "do" we are not interested in |
| 2924 | // labels. |
| 2925 | if (whilelevel > 0) |
| 2926 | continue; |
| 2927 | |
| 2928 | // case xx: |
| 2929 | // c = 99 + <- this indent plus continuation |
| 2930 | //-> here; |
| 2931 | if (lookfor == LOOKFOR_UNTERM |
| 2932 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 2933 | { |
| 2934 | if (cont_amount > 0) |
| 2935 | amount = cont_amount; |
| 2936 | else |
| 2937 | amount += ind_continuation; |
| 2938 | break; |
| 2939 | } |
| 2940 | |
| 2941 | // case xx: <- line up with this case |
| 2942 | // x = 333; |
| 2943 | // case yy: |
| 2944 | if ( (iscase && lookfor == LOOKFOR_CASE) |
| 2945 | || (iscase && lookfor_break) |
| 2946 | || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) |
| 2947 | { |
| 2948 | // Check that this case label is not for another |
| 2949 | // switch() XXX |
| 2950 | if ((trypos = find_start_brace()) == NULL |
| 2951 | || trypos->lnum == ourscope) |
| 2952 | { |
| 2953 | amount = get_indent(); // XXX |
| 2954 | break; |
| 2955 | } |
| 2956 | continue; |
| 2957 | } |
| 2958 | |
| 2959 | n = get_indent_nolabel(curwin->w_cursor.lnum); // XXX |
| 2960 | |
| 2961 | // case xx: if (cond) <- line up with this if |
| 2962 | // y = y + 1; |
| 2963 | // -> s = 99; |
| 2964 | // |
| 2965 | // case xx: |
| 2966 | // if (cond) <- line up with this line |
| 2967 | // y = y + 1; |
| 2968 | // -> s = 99; |
| 2969 | if (lookfor == LOOKFOR_TERM) |
| 2970 | { |
| 2971 | if (n) |
| 2972 | amount = n; |
| 2973 | |
| 2974 | if (!lookfor_break) |
| 2975 | break; |
| 2976 | } |
| 2977 | |
| 2978 | // case xx: x = x + 1; <- line up with this x |
| 2979 | // -> y = y + 1; |
| 2980 | // |
| 2981 | // case xx: if (cond) <- line up with this if |
| 2982 | // -> y = y + 1; |
| 2983 | if (n) |
| 2984 | { |
| 2985 | amount = n; |
| 2986 | l = after_label(ml_get_curline()); |
| 2987 | if (l != NULL && cin_is_cinword(l)) |
| 2988 | { |
| 2989 | if (theline[0] == '{') |
| 2990 | amount += curbuf->b_ind_open_extra; |
| 2991 | else |
| 2992 | amount += curbuf->b_ind_level |
| 2993 | + curbuf->b_ind_no_brace; |
| 2994 | } |
| 2995 | break; |
| 2996 | } |
| 2997 | |
| 2998 | // Try to get the indent of a statement before the switch |
| 2999 | // label. If nothing is found, line up relative to the |
| 3000 | // switch label. |
| 3001 | // break; <- may line up with this line |
| 3002 | // case xx: |
| 3003 | // -> y = 1; |
| 3004 | scope_amount = get_indent() + (iscase // XXX |
| 3005 | ? curbuf->b_ind_case_code |
| 3006 | : curbuf->b_ind_scopedecl_code); |
| 3007 | lookfor = curbuf->b_ind_case_break |
| 3008 | ? LOOKFOR_NOBREAK : LOOKFOR_ANY; |
| 3009 | continue; |
| 3010 | } |
| 3011 | |
| 3012 | // Looking for a switch() label or C++ scope declaration, |
| 3013 | // ignore other lines, skip {}-blocks. |
| 3014 | if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) |
| 3015 | { |
| 3016 | if (find_last_paren(l, '{', '}') |
| 3017 | && (trypos = find_start_brace()) != NULL) |
| 3018 | { |
| 3019 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 3020 | curwin->w_cursor.col = 0; |
| 3021 | } |
| 3022 | continue; |
| 3023 | } |
| 3024 | |
| 3025 | // Ignore jump labels with nothing after them. |
| 3026 | if (!curbuf->b_ind_js && cin_islabel()) |
| 3027 | { |
| 3028 | l = after_label(ml_get_curline()); |
| 3029 | if (l == NULL || cin_nocode(l)) |
| 3030 | continue; |
| 3031 | } |
| 3032 | |
| 3033 | // Ignore #defines, #if, etc. |
| 3034 | // Ignore comment and empty lines. |
| 3035 | // (need to get the line again, cin_islabel() may have |
| 3036 | // unlocked it) |
| 3037 | l = ml_get_curline(); |
| 3038 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) |
| 3039 | || cin_nocode(l)) |
| 3040 | continue; |
| 3041 | |
| 3042 | // Are we at the start of a cpp base class declaration or |
| 3043 | // constructor initialization? XXX |
| 3044 | n = FALSE; |
| 3045 | if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) |
| 3046 | { |
| 3047 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 3048 | l = ml_get_curline(); |
| 3049 | } |
| 3050 | if (n) |
| 3051 | { |
| 3052 | if (lookfor == LOOKFOR_UNTERM) |
| 3053 | { |
| 3054 | if (cont_amount > 0) |
| 3055 | amount = cont_amount; |
| 3056 | else |
| 3057 | amount += ind_continuation; |
| 3058 | } |
| 3059 | else if (theline[0] == '{') |
| 3060 | { |
| 3061 | // Need to find start of the declaration. |
| 3062 | lookfor = LOOKFOR_UNTERM; |
| 3063 | ind_continuation = 0; |
| 3064 | continue; |
| 3065 | } |
| 3066 | else |
| 3067 | // XXX |
| 3068 | amount = get_baseclass_amount( |
| 3069 | cache_cpp_baseclass.lpos.col); |
| 3070 | break; |
| 3071 | } |
| 3072 | else if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 3073 | { |
| 3074 | // only look, whether there is a cpp base class |
| 3075 | // declaration or initialization before the opening brace. |
| 3076 | if (cin_isterminated(l, TRUE, FALSE)) |
| 3077 | break; |
| 3078 | else |
| 3079 | continue; |
| 3080 | } |
| 3081 | |
| 3082 | // What happens next depends on the line being terminated. |
| 3083 | // If terminated with a ',' only consider it terminating if |
| 3084 | // there is another unterminated statement behind, eg: |
| 3085 | // 123, |
| 3086 | // sizeof |
| 3087 | // here |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 3088 | // Otherwise check whether it is an enumeration or structure |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 3089 | // initialisation (not indented) or a variable declaration |
| 3090 | // (indented). |
| 3091 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 3092 | |
| 3093 | if (js_cur_has_key) |
| 3094 | { |
| 3095 | js_cur_has_key = 0; // only check the first line |
| 3096 | if (curbuf->b_ind_js && terminated == ',') |
| 3097 | { |
| 3098 | // For Javascript we might be inside an object: |
| 3099 | // key: something, <- align with this |
| 3100 | // key: something |
| 3101 | // or: |
| 3102 | // key: something + <- align with this |
| 3103 | // something, |
| 3104 | // key: something |
| 3105 | lookfor = LOOKFOR_JS_KEY; |
| 3106 | } |
| 3107 | } |
| 3108 | if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) |
| 3109 | { |
| 3110 | amount = get_indent(); |
| 3111 | break; |
| 3112 | } |
| 3113 | if (lookfor == LOOKFOR_COMMA) |
| 3114 | { |
| 3115 | if (tryposBrace != NULL && tryposBrace->lnum |
| 3116 | >= curwin->w_cursor.lnum) |
| 3117 | break; |
| 3118 | if (terminated == ',') |
| 3119 | // line below current line is the one that starts a |
| 3120 | // (possibly broken) line ending in a comma. |
| 3121 | break; |
| 3122 | else |
| 3123 | { |
| 3124 | amount = get_indent(); |
| 3125 | if (curwin->w_cursor.lnum - 1 == ourscope) |
| 3126 | // line above is start of the scope, thus current |
| 3127 | // line is the one that stars a (possibly broken) |
| 3128 | // line ending in a comma. |
| 3129 | break; |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | if (terminated == 0 || (lookfor != LOOKFOR_UNTERM |
| 3134 | && terminated == ',')) |
| 3135 | { |
| 3136 | if (lookfor != LOOKFOR_ENUM_OR_INIT && |
| 3137 | (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) |
| 3138 | amount += ind_continuation; |
| 3139 | // if we're in the middle of a paren thing, |
| 3140 | // go back to the line that starts it so |
| 3141 | // we can get the right prevailing indent |
| 3142 | // if ( foo && |
| 3143 | // bar ) |
| 3144 | |
| 3145 | // Position the cursor over the rightmost paren, so that |
| 3146 | // matching it will take us back to the start of the line. |
| 3147 | // Ignore a match before the start of the block. |
| 3148 | (void)find_last_paren(l, '(', ')'); |
| 3149 | trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); |
| 3150 | if (trypos != NULL && (trypos->lnum < tryposBrace->lnum |
| 3151 | || (trypos->lnum == tryposBrace->lnum |
| 3152 | && trypos->col < tryposBrace->col))) |
| 3153 | trypos = NULL; |
| 3154 | |
| 3155 | // If we are looking for ',', we also look for matching |
| 3156 | // braces. |
| 3157 | if (trypos == NULL && terminated == ',' |
| 3158 | && find_last_paren(l, '{', '}')) |
| 3159 | trypos = find_start_brace(); |
| 3160 | |
| 3161 | if (trypos != NULL) |
| 3162 | { |
| 3163 | // Check if we are on a case label now. This is |
| 3164 | // handled above. |
| 3165 | // case xx: if ( asdf && |
| 3166 | // asdf) |
| 3167 | curwin->w_cursor = *trypos; |
| 3168 | l = ml_get_curline(); |
| 3169 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
| 3170 | { |
| 3171 | ++curwin->w_cursor.lnum; |
| 3172 | curwin->w_cursor.col = 0; |
| 3173 | continue; |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | // Skip over continuation lines to find the one to get the |
| 3178 | // indent from |
| 3179 | // char *usethis = "bla{backslash} |
| 3180 | // bla", |
| 3181 | // here; |
| 3182 | if (terminated == ',') |
| 3183 | { |
| 3184 | while (curwin->w_cursor.lnum > 1) |
| 3185 | { |
| 3186 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 3187 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 3188 | break; |
| 3189 | --curwin->w_cursor.lnum; |
| 3190 | curwin->w_cursor.col = 0; |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | // Get indent and pointer to text for current line, |
| 3195 | // ignoring any jump label. XXX |
| 3196 | if (curbuf->b_ind_js) |
| 3197 | cur_amount = get_indent(); |
| 3198 | else |
| 3199 | cur_amount = skip_label(curwin->w_cursor.lnum, &l); |
| 3200 | // If this is just above the line we are indenting, and it |
| 3201 | // starts with a '{', line it up with this line. |
| 3202 | // while (not) |
| 3203 | // -> { |
| 3204 | // } |
| 3205 | if (terminated != ',' && lookfor != LOOKFOR_TERM |
| 3206 | && theline[0] == '{') |
| 3207 | { |
| 3208 | amount = cur_amount; |
| 3209 | // Only add b_ind_open_extra when the current line |
| 3210 | // doesn't start with a '{', which must have a match |
| 3211 | // in the same line (scope is the same). Probably: |
| 3212 | // { 1, 2 }, |
| 3213 | // -> { 3, 4 } |
| 3214 | if (*skipwhite(l) != '{') |
| 3215 | amount += curbuf->b_ind_open_extra; |
| 3216 | |
| 3217 | if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) |
| 3218 | { |
| 3219 | // have to look back, whether it is a cpp base |
| 3220 | // class declaration or initialization |
| 3221 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 3222 | continue; |
| 3223 | } |
| 3224 | break; |
| 3225 | } |
| 3226 | |
| 3227 | // Check if we are after an "if", "while", etc. |
| 3228 | // Also allow " } else". |
| 3229 | if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) |
| 3230 | { |
| 3231 | // Found an unterminated line after an if (), line up |
| 3232 | // with the last one. |
| 3233 | // if (cond) |
| 3234 | // 100 + |
| 3235 | // -> here; |
| 3236 | if (lookfor == LOOKFOR_UNTERM |
| 3237 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3238 | { |
| 3239 | if (cont_amount > 0) |
| 3240 | amount = cont_amount; |
| 3241 | else |
| 3242 | amount += ind_continuation; |
| 3243 | break; |
| 3244 | } |
| 3245 | |
| 3246 | // If this is just above the line we are indenting, we |
| 3247 | // are finished. |
| 3248 | // while (not) |
| 3249 | // -> here; |
| 3250 | // Otherwise this indent can be used when the line |
| 3251 | // before this is terminated. |
| 3252 | // yyy; |
| 3253 | // if (stat) |
| 3254 | // while (not) |
| 3255 | // xxx; |
| 3256 | // -> here; |
| 3257 | amount = cur_amount; |
| 3258 | if (theline[0] == '{') |
| 3259 | amount += curbuf->b_ind_open_extra; |
| 3260 | if (lookfor != LOOKFOR_TERM) |
| 3261 | { |
| 3262 | amount += curbuf->b_ind_level |
| 3263 | + curbuf->b_ind_no_brace; |
| 3264 | break; |
| 3265 | } |
| 3266 | |
| 3267 | // Special trick: when expecting the while () after a |
| 3268 | // do, line up with the while() |
| 3269 | // do |
| 3270 | // x = 1; |
| 3271 | // -> here |
| 3272 | l = skipwhite(ml_get_curline()); |
| 3273 | if (cin_isdo(l)) |
| 3274 | { |
| 3275 | if (whilelevel == 0) |
| 3276 | break; |
| 3277 | --whilelevel; |
| 3278 | } |
| 3279 | |
| 3280 | // When searching for a terminated line, don't use the |
| 3281 | // one between the "if" and the matching "else". |
| 3282 | // Need to use the scope of this "else". XXX |
| 3283 | // If whilelevel != 0 continue looking for a "do {". |
| 3284 | if (cin_iselse(l) && whilelevel == 0) |
| 3285 | { |
| 3286 | // If we're looking at "} else", let's make sure we |
| 3287 | // find the opening brace of the enclosing scope, |
| 3288 | // not the one from "if () {". |
| 3289 | if (*l == '}') |
| 3290 | curwin->w_cursor.col = |
| 3291 | (colnr_T)(l - ml_get_curline()) + 1; |
| 3292 | |
| 3293 | if ((trypos = find_start_brace()) == NULL |
| 3294 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 3295 | == FAIL) |
| 3296 | break; |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | // If we're below an unterminated line that is not an |
| 3301 | // "if" or something, we may line up with this line or |
| 3302 | // add something for a continuation line, depending on |
| 3303 | // the line before this one. |
| 3304 | else |
| 3305 | { |
| 3306 | // Found two unterminated lines on a row, line up with |
| 3307 | // the last one. |
| 3308 | // c = 99 + |
| 3309 | // 100 + |
| 3310 | // -> here; |
| 3311 | if (lookfor == LOOKFOR_UNTERM) |
| 3312 | { |
| 3313 | // When line ends in a comma add extra indent |
| 3314 | if (terminated == ',') |
| 3315 | amount += ind_continuation; |
| 3316 | break; |
| 3317 | } |
| 3318 | |
| 3319 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3320 | { |
| 3321 | // Found two lines ending in ',', lineup with the |
| 3322 | // lowest one, but check for cpp base class |
| 3323 | // declaration/initialization, if it is an |
| 3324 | // opening brace or we are looking just for |
| 3325 | // enumerations/initializations. |
| 3326 | if (terminated == ',') |
| 3327 | { |
| 3328 | if (curbuf->b_ind_cpp_baseclass == 0) |
| 3329 | break; |
| 3330 | |
| 3331 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 3332 | continue; |
| 3333 | } |
| 3334 | |
| 3335 | // Ignore unterminated lines in between, but |
| 3336 | // reduce indent. |
| 3337 | if (amount > cur_amount) |
| 3338 | amount = cur_amount; |
| 3339 | } |
| 3340 | else |
| 3341 | { |
| 3342 | // Found first unterminated line on a row, may |
| 3343 | // line up with this line, remember its indent |
| 3344 | // 100 + |
| 3345 | // -> here; |
| 3346 | l = ml_get_curline(); |
| 3347 | amount = cur_amount; |
| 3348 | |
| 3349 | n = (int)STRLEN(l); |
| 3350 | if (terminated == ',' && (*skipwhite(l) == ']' |
| 3351 | || (n >=2 && l[n - 2] == ']'))) |
| 3352 | break; |
| 3353 | |
| 3354 | // If previous line ends in ',', check whether we |
| 3355 | // are in an initialization or enum |
| 3356 | // struct xxx = |
| 3357 | // { |
| 3358 | // sizeof a, |
| 3359 | // 124 }; |
| 3360 | // or a normal possible continuation line. |
| 3361 | // but only, of no other statement has been found |
| 3362 | // yet. |
| 3363 | if (lookfor == LOOKFOR_INITIAL && terminated == ',') |
| 3364 | { |
| 3365 | if (curbuf->b_ind_js) |
| 3366 | { |
| 3367 | // Search for a line ending in a comma |
| 3368 | // and line up with the line below it |
| 3369 | // (could be the current line). |
| 3370 | // some = [ |
| 3371 | // 1, <- line up here |
| 3372 | // 2, |
| 3373 | // some = [ |
| 3374 | // 3 + <- line up here |
| 3375 | // 4 * |
| 3376 | // 5, |
| 3377 | // 6, |
| 3378 | if (cin_iscomment(skipwhite(l))) |
| 3379 | break; |
| 3380 | lookfor = LOOKFOR_COMMA; |
| 3381 | trypos = find_match_char('[', |
| 3382 | curbuf->b_ind_maxparen); |
| 3383 | if (trypos != NULL) |
| 3384 | { |
| 3385 | if (trypos->lnum |
| 3386 | == curwin->w_cursor.lnum - 1) |
| 3387 | { |
| 3388 | // Current line is first inside |
| 3389 | // [], line up with it. |
| 3390 | break; |
| 3391 | } |
| 3392 | ourscope = trypos->lnum; |
| 3393 | } |
| 3394 | } |
| 3395 | else |
| 3396 | { |
| 3397 | lookfor = LOOKFOR_ENUM_OR_INIT; |
| 3398 | cont_amount = cin_first_id_amount(); |
| 3399 | } |
| 3400 | } |
| 3401 | else |
| 3402 | { |
| 3403 | if (lookfor == LOOKFOR_INITIAL |
| 3404 | && *l != NUL |
| 3405 | && l[STRLEN(l) - 1] == '\\') |
| 3406 | // XXX |
| 3407 | cont_amount = cin_get_equal_amount( |
| 3408 | curwin->w_cursor.lnum); |
| 3409 | if (lookfor != LOOKFOR_TERM |
| 3410 | && lookfor != LOOKFOR_JS_KEY |
| 3411 | && lookfor != LOOKFOR_COMMA |
| 3412 | && raw_string_start != curwin->w_cursor.lnum) |
| 3413 | lookfor = LOOKFOR_UNTERM; |
| 3414 | } |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | // Check if we are after a while (cond); |
| 3420 | // If so: Ignore until the matching "do". |
| 3421 | else if (cin_iswhileofdo_end(terminated)) // XXX |
| 3422 | { |
| 3423 | // Found an unterminated line after a while ();, line up |
| 3424 | // with the last one. |
| 3425 | // while (cond); |
| 3426 | // 100 + <- line up with this one |
| 3427 | // -> here; |
| 3428 | if (lookfor == LOOKFOR_UNTERM |
| 3429 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3430 | { |
| 3431 | if (cont_amount > 0) |
| 3432 | amount = cont_amount; |
| 3433 | else |
| 3434 | amount += ind_continuation; |
| 3435 | break; |
| 3436 | } |
| 3437 | |
| 3438 | if (whilelevel == 0) |
| 3439 | { |
| 3440 | lookfor = LOOKFOR_TERM; |
| 3441 | amount = get_indent(); // XXX |
| 3442 | if (theline[0] == '{') |
| 3443 | amount += curbuf->b_ind_open_extra; |
| 3444 | } |
| 3445 | ++whilelevel; |
| 3446 | } |
| 3447 | |
| 3448 | // We are after a "normal" statement. |
| 3449 | // If we had another statement we can stop now and use the |
| 3450 | // indent of that other statement. |
| 3451 | // Otherwise the indent of the current statement may be used, |
| 3452 | // search backwards for the next "normal" statement. |
| 3453 | else |
| 3454 | { |
| 3455 | // Skip single break line, if before a switch label. It |
| 3456 | // may be lined up with the case label. |
| 3457 | if (lookfor == LOOKFOR_NOBREAK |
| 3458 | && cin_isbreak(skipwhite(ml_get_curline()))) |
| 3459 | { |
| 3460 | lookfor = LOOKFOR_ANY; |
| 3461 | continue; |
| 3462 | } |
| 3463 | |
| 3464 | // Handle "do {" line. |
| 3465 | if (whilelevel > 0) |
| 3466 | { |
| 3467 | l = cin_skipcomment(ml_get_curline()); |
| 3468 | if (cin_isdo(l)) |
| 3469 | { |
| 3470 | amount = get_indent(); // XXX |
| 3471 | --whilelevel; |
| 3472 | continue; |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | // Found a terminated line above an unterminated line. Add |
| 3477 | // the amount for a continuation line. |
| 3478 | // x = 1; |
| 3479 | // y = foo + |
| 3480 | // -> here; |
| 3481 | // or |
| 3482 | // int x = 1; |
| 3483 | // int foo, |
| 3484 | // -> here; |
| 3485 | if (lookfor == LOOKFOR_UNTERM |
| 3486 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 3487 | { |
| 3488 | if (cont_amount > 0) |
| 3489 | amount = cont_amount; |
| 3490 | else |
| 3491 | amount += ind_continuation; |
| 3492 | break; |
| 3493 | } |
| 3494 | |
| 3495 | // Found a terminated line above a terminated line or "if" |
| 3496 | // etc. line. Use the amount of the line below us. |
| 3497 | // x = 1; x = 1; |
| 3498 | // if (asdf) y = 2; |
| 3499 | // while (asdf) ->here; |
| 3500 | // here; |
| 3501 | // ->foo; |
| 3502 | if (lookfor == LOOKFOR_TERM) |
| 3503 | { |
| 3504 | if (!lookfor_break && whilelevel == 0) |
| 3505 | break; |
| 3506 | } |
| 3507 | |
| 3508 | // First line above the one we're indenting is terminated. |
| 3509 | // To know what needs to be done look further backward for |
| 3510 | // a terminated line. |
| 3511 | else |
| 3512 | { |
| 3513 | // position the cursor over the rightmost paren, so |
| 3514 | // that matching it will take us back to the start of |
| 3515 | // the line. Helps for: |
| 3516 | // func(asdr, |
| 3517 | // asdfasdf); |
| 3518 | // here; |
| 3519 | term_again: |
| 3520 | l = ml_get_curline(); |
| 3521 | if (find_last_paren(l, '(', ')') |
| 3522 | && (trypos = find_match_paren( |
| 3523 | curbuf->b_ind_maxparen)) != NULL) |
| 3524 | { |
| 3525 | // Check if we are on a case label now. This is |
| 3526 | // handled above. |
| 3527 | // case xx: if ( asdf && |
| 3528 | // asdf) |
| 3529 | curwin->w_cursor = *trypos; |
| 3530 | l = ml_get_curline(); |
| 3531 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
| 3532 | { |
| 3533 | ++curwin->w_cursor.lnum; |
| 3534 | curwin->w_cursor.col = 0; |
| 3535 | continue; |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | // When aligning with the case statement, don't align |
| 3540 | // with a statement after it. |
| 3541 | // case 1: { <-- don't use this { position |
| 3542 | // stat; |
| 3543 | // } |
| 3544 | // case 2: |
| 3545 | // stat; |
| 3546 | // } |
| 3547 | iscase = (curbuf->b_ind_keep_case_label |
| 3548 | && cin_iscase(l, FALSE)); |
| 3549 | |
| 3550 | // Get indent and pointer to text for current line, |
| 3551 | // ignoring any jump label. |
| 3552 | amount = skip_label(curwin->w_cursor.lnum, &l); |
| 3553 | |
| 3554 | if (theline[0] == '{') |
| 3555 | amount += curbuf->b_ind_open_extra; |
| 3556 | // See remark above: "Only add b_ind_open_extra.." |
| 3557 | l = skipwhite(l); |
| 3558 | if (*l == '{') |
| 3559 | amount -= curbuf->b_ind_open_extra; |
| 3560 | lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; |
| 3561 | |
| 3562 | // When a terminated line starts with "else" skip to |
| 3563 | // the matching "if": |
| 3564 | // else 3; |
| 3565 | // indent this; |
| 3566 | // Need to use the scope of this "else". XXX |
| 3567 | // If whilelevel != 0 continue looking for a "do {". |
| 3568 | if (lookfor == LOOKFOR_TERM |
| 3569 | && *l != '}' |
| 3570 | && cin_iselse(l) |
| 3571 | && whilelevel == 0) |
| 3572 | { |
| 3573 | if ((trypos = find_start_brace()) == NULL |
| 3574 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 3575 | == FAIL) |
| 3576 | break; |
| 3577 | continue; |
| 3578 | } |
| 3579 | |
| 3580 | // If we're at the end of a block, skip to the start of |
| 3581 | // that block. |
| 3582 | l = ml_get_curline(); |
| 3583 | if (find_last_paren(l, '{', '}') // XXX |
| 3584 | && (trypos = find_start_brace()) != NULL) |
| 3585 | { |
| 3586 | curwin->w_cursor = *trypos; |
| 3587 | // if not "else {" check for terminated again |
| 3588 | // but skip block for "} else {" |
| 3589 | l = cin_skipcomment(ml_get_curline()); |
| 3590 | if (*l == '}' || !cin_iselse(l)) |
| 3591 | goto term_again; |
| 3592 | ++curwin->w_cursor.lnum; |
| 3593 | curwin->w_cursor.col = 0; |
| 3594 | } |
| 3595 | } |
| 3596 | } |
| 3597 | } |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | // add extra indent for a comment |
| 3602 | if (cin_iscomment(theline)) |
| 3603 | amount += curbuf->b_ind_comment; |
| 3604 | |
| 3605 | // subtract extra left-shift for jump labels |
| 3606 | if (curbuf->b_ind_jump_label > 0 && original_line_islabel) |
| 3607 | amount -= curbuf->b_ind_jump_label; |
| 3608 | |
| 3609 | goto theend; |
| 3610 | } |
| 3611 | |
| 3612 | // ok -- we're not inside any sort of structure at all! |
| 3613 | // |
| 3614 | // This means we're at the top level, and everything should |
| 3615 | // basically just match where the previous line is, except |
| 3616 | // for the lines immediately following a function declaration, |
| 3617 | // which are K&R-style parameters and need to be indented. |
| 3618 | // |
| 3619 | // if our line starts with an open brace, forget about any |
| 3620 | // prevailing indent and make sure it looks like the start |
| 3621 | // of a function |
| 3622 | |
| 3623 | if (theline[0] == '{') |
| 3624 | { |
| 3625 | amount = curbuf->b_ind_first_open; |
| 3626 | goto theend; |
| 3627 | } |
| 3628 | |
| 3629 | // If the NEXT line is a function declaration, the current |
| 3630 | // line needs to be indented as a function type spec. |
| 3631 | // Don't do this if the current line looks like a comment or if the |
| 3632 | // current line is terminated, ie. ends in ';', or if the current line |
| 3633 | // contains { or }: "void f() {\n if (1)" |
| 3634 | if (cur_curpos.lnum < curbuf->b_ml.ml_line_count |
| 3635 | && !cin_nocode(theline) |
| 3636 | && vim_strchr(theline, '{') == NULL |
| 3637 | && vim_strchr(theline, '}') == NULL |
| 3638 | && !cin_ends_in(theline, (char_u *)":", NULL) |
| 3639 | && !cin_ends_in(theline, (char_u *)",", NULL) |
| 3640 | && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
| 3641 | cur_curpos.lnum + 1) |
| 3642 | && !cin_isterminated(theline, FALSE, TRUE)) |
| 3643 | { |
| 3644 | amount = curbuf->b_ind_func_type; |
| 3645 | goto theend; |
| 3646 | } |
| 3647 | |
| 3648 | // search backwards until we find something we recognize |
| 3649 | amount = 0; |
| 3650 | curwin->w_cursor = cur_curpos; |
| 3651 | while (curwin->w_cursor.lnum > 1) |
| 3652 | { |
| 3653 | curwin->w_cursor.lnum--; |
| 3654 | curwin->w_cursor.col = 0; |
| 3655 | |
| 3656 | l = ml_get_curline(); |
| 3657 | |
| 3658 | // If we're in a comment or raw string now, skip to the start |
| 3659 | // of it. XXX |
| 3660 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
| 3661 | { |
| 3662 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 3663 | curwin->w_cursor.col = 0; |
| 3664 | continue; |
| 3665 | } |
| 3666 | |
| 3667 | // Are we at the start of a cpp base class declaration or |
| 3668 | // constructor initialization? XXX |
| 3669 | n = FALSE; |
| 3670 | if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') |
| 3671 | { |
| 3672 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 3673 | l = ml_get_curline(); |
| 3674 | } |
| 3675 | if (n) |
| 3676 | { |
| 3677 | // XXX |
| 3678 | amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col); |
| 3679 | break; |
| 3680 | } |
| 3681 | |
| 3682 | // Skip preprocessor directives and blank lines. |
| 3683 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) |
| 3684 | continue; |
| 3685 | |
| 3686 | if (cin_nocode(l)) |
| 3687 | continue; |
| 3688 | |
| 3689 | // If the previous line ends in ',', use one level of |
| 3690 | // indentation: |
| 3691 | // int foo, |
| 3692 | // bar; |
| 3693 | // do this before checking for '}' in case of eg. |
| 3694 | // enum foobar |
| 3695 | // { |
| 3696 | // ... |
| 3697 | // } foo, |
| 3698 | // bar; |
| 3699 | n = 0; |
| 3700 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 3701 | || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) |
| 3702 | { |
| 3703 | // take us back to opening paren |
| 3704 | if (find_last_paren(l, '(', ')') |
| 3705 | && (trypos = find_match_paren( |
| 3706 | curbuf->b_ind_maxparen)) != NULL) |
| 3707 | curwin->w_cursor = *trypos; |
| 3708 | |
| 3709 | // For a line ending in ',' that is a continuation line go |
| 3710 | // back to the first line with a backslash: |
| 3711 | // char *foo = "bla{backslash} |
| 3712 | // bla", |
| 3713 | // here; |
| 3714 | while (n == 0 && curwin->w_cursor.lnum > 1) |
| 3715 | { |
| 3716 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 3717 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 3718 | break; |
| 3719 | --curwin->w_cursor.lnum; |
| 3720 | curwin->w_cursor.col = 0; |
| 3721 | } |
| 3722 | |
| 3723 | amount = get_indent(); // XXX |
| 3724 | |
| 3725 | if (amount == 0) |
| 3726 | amount = cin_first_id_amount(); |
| 3727 | if (amount == 0) |
| 3728 | amount = ind_continuation; |
| 3729 | break; |
| 3730 | } |
| 3731 | |
| 3732 | // If the line looks like a function declaration, and we're |
| 3733 | // not in a comment, put it the left margin. |
| 3734 | if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) // XXX |
| 3735 | break; |
| 3736 | l = ml_get_curline(); |
| 3737 | |
| 3738 | // Finding the closing '}' of a previous function. Put |
| 3739 | // current line at the left margin. For when 'cino' has "fs". |
| 3740 | if (*skipwhite(l) == '}') |
| 3741 | break; |
| 3742 | |
| 3743 | // (matching {) |
| 3744 | // If the previous line ends on '};' (maybe followed by |
| 3745 | // comments) align at column 0. For example: |
| 3746 | // char *string_array[] = { "foo", |
| 3747 | // / * x * / "b};ar" }; / * foobar * / |
| 3748 | if (cin_ends_in(l, (char_u *)"};", NULL)) |
| 3749 | break; |
| 3750 | |
| 3751 | // If the previous line ends on '[' we are probably in an |
| 3752 | // array constant: |
| 3753 | // something = [ |
| 3754 | // 234, <- extra indent |
| 3755 | if (cin_ends_in(l, (char_u *)"[", NULL)) |
| 3756 | { |
| 3757 | amount = get_indent() + ind_continuation; |
| 3758 | break; |
| 3759 | } |
| 3760 | |
| 3761 | // Find a line only has a semicolon that belongs to a previous |
| 3762 | // line ending in '}', e.g. before an #endif. Don't increase |
| 3763 | // indent then. |
| 3764 | if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) |
| 3765 | { |
| 3766 | pos_T curpos_save = curwin->w_cursor; |
| 3767 | |
| 3768 | while (curwin->w_cursor.lnum > 1) |
| 3769 | { |
| 3770 | look = ml_get(--curwin->w_cursor.lnum); |
| 3771 | if (!(cin_nocode(look) || cin_ispreproc_cont( |
| 3772 | &look, &curwin->w_cursor.lnum, &amount))) |
| 3773 | break; |
| 3774 | } |
| 3775 | if (curwin->w_cursor.lnum > 0 |
| 3776 | && cin_ends_in(look, (char_u *)"}", NULL)) |
| 3777 | break; |
| 3778 | |
| 3779 | curwin->w_cursor = curpos_save; |
| 3780 | } |
| 3781 | |
| 3782 | // If the PREVIOUS line is a function declaration, the current |
| 3783 | // line (and the ones that follow) needs to be indented as |
| 3784 | // parameters. |
| 3785 | if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 3786 | { |
| 3787 | amount = curbuf->b_ind_param; |
| 3788 | break; |
| 3789 | } |
| 3790 | |
| 3791 | // If the previous line ends in ';' and the line before the |
| 3792 | // previous line ends in ',' or '\', ident to column zero: |
| 3793 | // int foo, |
| 3794 | // bar; |
| 3795 | // indent_to_0 here; |
| 3796 | if (cin_ends_in(l, (char_u *)";", NULL)) |
| 3797 | { |
| 3798 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 3799 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 3800 | || (*l != NUL && l[STRLEN(l) - 1] == '\\')) |
| 3801 | break; |
| 3802 | l = ml_get_curline(); |
| 3803 | } |
| 3804 | |
| 3805 | // Doesn't look like anything interesting -- so just |
| 3806 | // use the indent of this line. |
| 3807 | // |
| 3808 | // Position the cursor over the rightmost paren, so that |
| 3809 | // matching it will take us back to the start of the line. |
| 3810 | find_last_paren(l, '(', ')'); |
| 3811 | |
| 3812 | if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 3813 | curwin->w_cursor = *trypos; |
| 3814 | amount = get_indent(); // XXX |
| 3815 | break; |
| 3816 | } |
| 3817 | |
| 3818 | // add extra indent for a comment |
| 3819 | if (cin_iscomment(theline)) |
| 3820 | amount += curbuf->b_ind_comment; |
| 3821 | |
| 3822 | // add extra indent if the previous line ended in a backslash: |
| 3823 | // "asdfasdf{backslash} |
| 3824 | // here"; |
| 3825 | // char *foo = "asdf{backslash} |
| 3826 | // here"; |
| 3827 | if (cur_curpos.lnum > 1) |
| 3828 | { |
| 3829 | l = ml_get(cur_curpos.lnum - 1); |
| 3830 | if (*l != NUL && l[STRLEN(l) - 1] == '\\') |
| 3831 | { |
| 3832 | cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); |
| 3833 | if (cur_amount > 0) |
| 3834 | amount = cur_amount; |
| 3835 | else if (cur_amount == 0) |
| 3836 | amount += ind_continuation; |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | theend: |
| 3841 | if (amount < 0) |
| 3842 | amount = 0; |
| 3843 | |
| 3844 | laterend: |
| 3845 | // put the cursor back where it belongs |
| 3846 | curwin->w_cursor = cur_curpos; |
| 3847 | |
| 3848 | vim_free(linecopy); |
| 3849 | |
| 3850 | return amount; |
| 3851 | } |
| 3852 | |
| 3853 | /* |
| 3854 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 3855 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 3856 | * when == '!': Only if key is preceded with '!' (don't insert) |
| 3857 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 3858 | * |
| 3859 | * "keytyped" can have a few special values: |
| 3860 | * KEY_OPEN_FORW |
| 3861 | * KEY_OPEN_BACK |
| 3862 | * KEY_COMPLETE just finished completion. |
| 3863 | * |
| 3864 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 3865 | */ |
| 3866 | int |
| 3867 | in_cinkeys( |
| 3868 | int keytyped, |
| 3869 | int when, |
| 3870 | int line_is_empty) |
| 3871 | { |
| 3872 | char_u *look; |
| 3873 | int try_match; |
| 3874 | int try_match_word; |
| 3875 | char_u *p; |
| 3876 | char_u *line; |
| 3877 | int icase; |
| 3878 | int i; |
| 3879 | |
| 3880 | if (keytyped == NUL) |
| 3881 | // Can happen with CTRL-Y and CTRL-E on a short line. |
| 3882 | return FALSE; |
| 3883 | |
| 3884 | #ifdef FEAT_EVAL |
| 3885 | if (*curbuf->b_p_inde != NUL) |
| 3886 | look = curbuf->b_p_indk; // 'indentexpr' set: use 'indentkeys' |
| 3887 | else |
| 3888 | #endif |
| 3889 | look = curbuf->b_p_cink; // 'indentexpr' empty: use 'cinkeys' |
| 3890 | while (*look) |
| 3891 | { |
| 3892 | // Find out if we want to try a match with this key, depending on |
| 3893 | // 'when' and a '*' or '!' before the key. |
| 3894 | switch (when) |
| 3895 | { |
| 3896 | case '*': try_match = (*look == '*'); break; |
| 3897 | case '!': try_match = (*look == '!'); break; |
| 3898 | default: try_match = (*look != '*'); break; |
| 3899 | } |
| 3900 | if (*look == '*' || *look == '!') |
| 3901 | ++look; |
| 3902 | |
| 3903 | // If there is a '0', only accept a match if the line is empty. |
| 3904 | // But may still match when typing last char of a word. |
| 3905 | if (*look == '0') |
| 3906 | { |
| 3907 | try_match_word = try_match; |
| 3908 | if (!line_is_empty) |
| 3909 | try_match = FALSE; |
| 3910 | ++look; |
| 3911 | } |
| 3912 | else |
| 3913 | try_match_word = FALSE; |
| 3914 | |
| 3915 | // does it look like a control character? |
| 3916 | if (*look == '^' |
| 3917 | #ifdef EBCDIC |
| 3918 | && (Ctrl_chr(look[1]) != 0) |
| 3919 | #else |
| 3920 | && look[1] >= '?' && look[1] <= '_' |
| 3921 | #endif |
| 3922 | ) |
| 3923 | { |
| 3924 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 3925 | return TRUE; |
| 3926 | look += 2; |
| 3927 | } |
| 3928 | // 'o' means "o" command, open forward. |
| 3929 | // 'O' means "O" command, open backward. |
| 3930 | else if (*look == 'o') |
| 3931 | { |
| 3932 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 3933 | return TRUE; |
| 3934 | ++look; |
| 3935 | } |
| 3936 | else if (*look == 'O') |
| 3937 | { |
| 3938 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 3939 | return TRUE; |
| 3940 | ++look; |
| 3941 | } |
| 3942 | |
| 3943 | // 'e' means to check for "else" at start of line and just before the |
| 3944 | // cursor. |
| 3945 | else if (*look == 'e') |
| 3946 | { |
| 3947 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 3948 | { |
| 3949 | p = ml_get_curline(); |
| 3950 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 3951 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 3952 | return TRUE; |
| 3953 | } |
| 3954 | ++look; |
| 3955 | } |
| 3956 | |
| 3957 | // ':' only causes an indent if it is at the end of a label or case |
| 3958 | // statement, or when it was before typing the ':' (to fix |
| 3959 | // class::method for C++). |
| 3960 | else if (*look == ':') |
| 3961 | { |
| 3962 | if (try_match && keytyped == ':') |
| 3963 | { |
| 3964 | p = ml_get_curline(); |
| 3965 | if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel()) |
| 3966 | return TRUE; |
| 3967 | // Need to get the line again after cin_islabel(). |
| 3968 | p = ml_get_curline(); |
| 3969 | if (curwin->w_cursor.col > 2 |
| 3970 | && p[curwin->w_cursor.col - 1] == ':' |
| 3971 | && p[curwin->w_cursor.col - 2] == ':') |
| 3972 | { |
| 3973 | p[curwin->w_cursor.col - 1] = ' '; |
| 3974 | i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
| 3975 | || cin_islabel()); |
| 3976 | p = ml_get_curline(); |
| 3977 | p[curwin->w_cursor.col - 1] = ':'; |
| 3978 | if (i) |
| 3979 | return TRUE; |
| 3980 | } |
| 3981 | } |
| 3982 | ++look; |
| 3983 | } |
| 3984 | |
| 3985 | |
| 3986 | // Is it a key in <>, maybe? |
| 3987 | else if (*look == '<') |
| 3988 | { |
| 3989 | if (try_match) |
| 3990 | { |
| 3991 | // make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 3992 | // <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 3993 | // >, *, : and ! keys if they really really want to. |
| 3994 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 3995 | && keytyped == look[1]) |
| 3996 | return TRUE; |
| 3997 | |
| 3998 | if (keytyped == get_special_key_code(look + 1)) |
| 3999 | return TRUE; |
| 4000 | } |
| 4001 | while (*look && *look != '>') |
| 4002 | look++; |
| 4003 | while (*look == '>') |
| 4004 | look++; |
| 4005 | } |
| 4006 | |
| 4007 | // Is it a word: "=word"? |
| 4008 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 4009 | { |
| 4010 | ++look; |
| 4011 | if (*look == '~') |
| 4012 | { |
| 4013 | icase = TRUE; |
| 4014 | ++look; |
| 4015 | } |
| 4016 | else |
| 4017 | icase = FALSE; |
| 4018 | p = vim_strchr(look, ','); |
| 4019 | if (p == NULL) |
| 4020 | p = look + STRLEN(look); |
| 4021 | if ((try_match || try_match_word) |
| 4022 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 4023 | { |
| 4024 | int match = FALSE; |
| 4025 | |
| 4026 | if (keytyped == KEY_COMPLETE) |
| 4027 | { |
| 4028 | char_u *s; |
| 4029 | |
| 4030 | // Just completed a word, check if it starts with "look". |
| 4031 | // search back for the start of a word. |
| 4032 | line = ml_get_curline(); |
| 4033 | if (has_mbyte) |
| 4034 | { |
| 4035 | char_u *n; |
| 4036 | |
| 4037 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 4038 | { |
| 4039 | n = mb_prevptr(line, s); |
| 4040 | if (!vim_iswordp(n)) |
| 4041 | break; |
| 4042 | } |
| 4043 | } |
| 4044 | else |
| 4045 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 4046 | if (!vim_iswordc(s[-1])) |
| 4047 | break; |
| 4048 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 4049 | && (icase |
| 4050 | ? MB_STRNICMP(s, look, p - look) |
| 4051 | : STRNCMP(s, look, p - look)) == 0) |
| 4052 | match = TRUE; |
| 4053 | } |
| 4054 | else |
| 4055 | // TODO: multi-byte |
| 4056 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 4057 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 4058 | { |
| 4059 | line = ml_get_cursor(); |
| 4060 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 4061 | || !vim_iswordc(line[-(p - look) - 1])) |
| 4062 | && (icase |
| 4063 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 4064 | : STRNCMP(line - (p - look), look, p - look)) |
| 4065 | == 0) |
| 4066 | match = TRUE; |
| 4067 | } |
| 4068 | if (match && try_match_word && !try_match) |
| 4069 | { |
| 4070 | // "0=word": Check if there are only blanks before the |
| 4071 | // word. |
| 4072 | if (getwhitecols_curline() != |
| 4073 | (int)(curwin->w_cursor.col - (p - look))) |
| 4074 | match = FALSE; |
| 4075 | } |
| 4076 | if (match) |
| 4077 | return TRUE; |
| 4078 | } |
| 4079 | look = p; |
| 4080 | } |
| 4081 | |
| 4082 | // ok, it's a boring generic character. |
| 4083 | else |
| 4084 | { |
| 4085 | if (try_match && *look == keytyped) |
| 4086 | return TRUE; |
| 4087 | if (*look != NUL) |
| 4088 | ++look; |
| 4089 | } |
| 4090 | |
| 4091 | // Skip over ", ". |
| 4092 | look = skip_to_option_part(look); |
| 4093 | } |
| 4094 | return FALSE; |
| 4095 | } |
| 4096 | |
| 4097 | /* |
| 4098 | * Do C or expression indenting on the current line. |
| 4099 | */ |
| 4100 | void |
| 4101 | do_c_expr_indent(void) |
| 4102 | { |
| 4103 | # ifdef FEAT_EVAL |
| 4104 | if (*curbuf->b_p_inde != NUL) |
| 4105 | fixthisline(get_expr_indent); |
| 4106 | else |
| 4107 | # endif |
| 4108 | fixthisline(get_c_indent); |
| 4109 | } |
| 4110 | #endif |
| 4111 | |
| 4112 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 4113 | /* |
| 4114 | * "cindent(lnum)" function |
| 4115 | */ |
| 4116 | void |
| 4117 | f_cindent(typval_T *argvars UNUSED, typval_T *rettv) |
| 4118 | { |
| 4119 | # ifdef FEAT_CINDENT |
| 4120 | pos_T pos; |
| 4121 | linenr_T lnum; |
| 4122 | |
| 4123 | pos = curwin->w_cursor; |
| 4124 | lnum = tv_get_lnum(argvars); |
| 4125 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 4126 | { |
| 4127 | curwin->w_cursor.lnum = lnum; |
| 4128 | rettv->vval.v_number = get_c_indent(); |
| 4129 | curwin->w_cursor = pos; |
| 4130 | } |
| 4131 | else |
| 4132 | # endif |
| 4133 | rettv->vval.v_number = -1; |
| 4134 | } |
| 4135 | #endif |