1 /* vi:set ts=8 sts=4 sw=4: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * misc1.c: functions that didn't seem to fit elsewhere 12 */ 13 14 #include "vim.h" 15 #include "version.h" 16 17 #ifdef HAVE_FCNTL_H 18 # include <fcntl.h> /* for chdir() */ 19 #endif 20 21 static char_u *vim_version_dir __ARGS((char_u *vimdir)); 22 static char_u *remove_tail __ARGS((char_u *p, char_u *pend, char_u *name)); 23 #if defined(USE_EXE_NAME) && defined(MACOS_X) 24 static char_u *remove_tail_with_ext __ARGS((char_u *p, char_u *pend, char_u *ext)); 25 #endif 26 static int get_indent_str __ARGS((char_u *ptr, int ts)); 27 static int copy_indent __ARGS((int size, char_u *src)); 28 29 /* 30 * Count the size (in window cells) of the indent in the current line. 31 */ 32 int 33 get_indent() 34 { 35 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts); 36 } 37 38 /* 39 * Count the size (in window cells) of the indent in line "lnum". 40 */ 41 int 42 get_indent_lnum(lnum) 43 linenr_T lnum; 44 { 45 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts); 46 } 47 48 #if defined(FEAT_FOLDING) || defined(PROTO) 49 /* 50 * Count the size (in window cells) of the indent in line "lnum" of buffer 51 * "buf". 52 */ 53 int 54 get_indent_buf(buf, lnum) 55 buf_T *buf; 56 linenr_T lnum; 57 { 58 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts); 59 } 60 #endif 61 62 /* 63 * count the size (in window cells) of the indent in line "ptr", with 64 * 'tabstop' at "ts" 65 */ 66 static int 67 get_indent_str(ptr, ts) 68 char_u *ptr; 69 int ts; 70 { 71 int count = 0; 72 73 for ( ; *ptr; ++ptr) 74 { 75 if (*ptr == TAB) /* count a tab for what it is worth */ 76 count += ts - (count % ts); 77 else if (*ptr == ' ') 78 ++count; /* count a space for one */ 79 else 80 break; 81 } 82 return (count); 83 } 84 85 /* 86 * Set the indent of the current line. 87 * Leaves the cursor on the first non-blank in the line. 88 * Caller must take care of undo. 89 * "flags": 90 * SIN_CHANGED: call changed_bytes() if the line was changed. 91 * SIN_INSERT: insert the indent in front of the line. 92 * SIN_UNDO: save line for undo before changing it. 93 * Returns TRUE if the line was changed. 94 */ 95 int 96 set_indent(size, flags) 97 int size; 98 int flags; 99 { 100 char_u *p; 101 char_u *newline; 102 char_u *oldline; 103 char_u *s; 104 int todo; 105 int ind_len; 106 int line_len; 107 int doit = FALSE; 108 int ind_done; 109 int tab_pad; 110 111 /* 112 * First check if there is anything to do and compute the number of 113 * characters needed for the indent. 114 */ 115 todo = size; 116 ind_len = 0; 117 p = oldline = ml_get_curline(); 118 119 /* Calculate the buffer size for the new indent, and check to see if it 120 * isn't already set */ 121 122 /* if 'expandtab' isn't set: use TABs */ 123 if (!curbuf->b_p_et) 124 { 125 /* If 'preserveindent' is set then reuse as much as possible of 126 * the existing indent structure for the new indent */ 127 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) 128 { 129 ind_done = 0; 130 131 /* count as many characters as we can use */ 132 while (todo > 0 && vim_iswhite(*p)) 133 { 134 if (*p == TAB) 135 { 136 tab_pad = (int)curbuf->b_p_ts 137 - (ind_done % (int)curbuf->b_p_ts); 138 /* stop if this tab will overshoot the target */ 139 if (todo < tab_pad) 140 break; 141 todo -= tab_pad; 142 ++ind_len; 143 ind_done += tab_pad; 144 } 145 else 146 { 147 --todo; 148 ++ind_len; 149 ++ind_done; 150 } 151 ++p; 152 } 153 154 /* Fill to next tabstop with a tab, if possible */ 155 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); 156 if (todo >= tab_pad) 157 { 158 doit = TRUE; 159 todo -= tab_pad; 160 ++ind_len; 161 /* ind_done += tab_pad; */ 162 } 163 } 164 165 /* count tabs required for indent */ 166 while (todo >= (int)curbuf->b_p_ts) 167 { 168 if (*p != TAB) 169 doit = TRUE; 170 else 171 ++p; 172 todo -= (int)curbuf->b_p_ts; 173 ++ind_len; 174 /* ind_done += (int)curbuf->b_p_ts; */ 175 } 176 } 177 /* count spaces required for indent */ 178 while (todo > 0) 179 { 180 if (*p != ' ') 181 doit = TRUE; 182 else 183 ++p; 184 --todo; 185 ++ind_len; 186 /* ++ind_done; */ 187 } 188 189 /* Return if the indent is OK already. */ 190 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT)) 191 return FALSE; 192 193 /* Allocate memory for the new line. */ 194 if (flags & SIN_INSERT) 195 p = oldline; 196 else 197 p = skipwhite(p); 198 line_len = (int)STRLEN(p) + 1; 199 newline = alloc(ind_len + line_len); 200 if (newline == NULL) 201 return FALSE; 202 203 /* Put the characters in the new line. */ 204 s = newline; 205 todo = size; 206 /* if 'expandtab' isn't set: use TABs */ 207 if (!curbuf->b_p_et) 208 { 209 /* If 'preserveindent' is set then reuse as much as possible of 210 * the existing indent structure for the new indent */ 211 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) 212 { 213 p = oldline; 214 ind_done = 0; 215 216 while (todo > 0 && vim_iswhite(*p)) 217 { 218 if (*p == TAB) 219 { 220 tab_pad = (int)curbuf->b_p_ts 221 - (ind_done % (int)curbuf->b_p_ts); 222 /* stop if this tab will overshoot the target */ 223 if (todo < tab_pad) 224 break; 225 todo -= tab_pad; 226 ind_done += tab_pad; 227 } 228 else 229 { 230 --todo; 231 ++ind_done; 232 } 233 *s++ = *p++; 234 } 235 236 /* Fill to next tabstop with a tab, if possible */ 237 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); 238 if (todo >= tab_pad) 239 { 240 *s++ = TAB; 241 todo -= tab_pad; 242 } 243 244 p = skipwhite(p); 245 } 246 247 while (todo >= (int)curbuf->b_p_ts) 248 { 249 *s++ = TAB; 250 todo -= (int)curbuf->b_p_ts; 251 } 252 } 253 while (todo > 0) 254 { 255 *s++ = ' '; 256 --todo; 257 } 258 mch_memmove(s, p, (size_t)line_len); 259 260 /* Replace the line (unless undo fails). */ 261 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) 262 { 263 ml_replace(curwin->w_cursor.lnum, newline, FALSE); 264 if (flags & SIN_CHANGED) 265 changed_bytes(curwin->w_cursor.lnum, 0); 266 /* Correct saved cursor position if it's after the indent. */ 267 if (saved_cursor.lnum == curwin->w_cursor.lnum 268 && saved_cursor.col >= (colnr_T)(p - oldline)) 269 saved_cursor.col += ind_len - (p - oldline); 270 } 271 else 272 vim_free(newline); 273 274 curwin->w_cursor.col = ind_len; 275 return TRUE; 276 } 277 278 /* 279 * Copy the indent from ptr to the current line (and fill to size) 280 * Leaves the cursor on the first non-blank in the line. 281 * Returns TRUE if the line was changed. 282 */ 283 static int 284 copy_indent(size, src) 285 int size; 286 char_u *src; 287 { 288 char_u *p = NULL; 289 char_u *line = NULL; 290 char_u *s; 291 int todo; 292 int ind_len; 293 int line_len = 0; 294 int tab_pad; 295 int ind_done; 296 int round; 297 298 /* Round 1: compute the number of characters needed for the indent 299 * Round 2: copy the characters. */ 300 for (round = 1; round <= 2; ++round) 301 { 302 todo = size; 303 ind_len = 0; 304 ind_done = 0; 305 s = src; 306 307 /* Count/copy the usable portion of the source line */ 308 while (todo > 0 && vim_iswhite(*s)) 309 { 310 if (*s == TAB) 311 { 312 tab_pad = (int)curbuf->b_p_ts 313 - (ind_done % (int)curbuf->b_p_ts); 314 /* Stop if this tab will overshoot the target */ 315 if (todo < tab_pad) 316 break; 317 todo -= tab_pad; 318 ind_done += tab_pad; 319 } 320 else 321 { 322 --todo; 323 ++ind_done; 324 } 325 ++ind_len; 326 if (round == 2) 327 *p++ = *s; 328 ++s; 329 } 330 331 /* Fill to next tabstop with a tab, if possible */ 332 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); 333 if (todo >= tab_pad) 334 { 335 todo -= tab_pad; 336 ++ind_len; 337 if (round == 2) 338 *p++ = TAB; 339 } 340 341 /* Add tabs required for indent */ 342 while (todo >= (int)curbuf->b_p_ts) 343 { 344 todo -= (int)curbuf->b_p_ts; 345 ++ind_len; 346 if (round == 2) 347 *p++ = TAB; 348 } 349 350 /* Count/add spaces required for indent */ 351 while (todo > 0) 352 { 353 --todo; 354 ++ind_len; 355 if (round == 2) 356 *p++ = ' '; 357 } 358 359 if (round == 1) 360 { 361 /* Allocate memory for the result: the copied indent, new indent 362 * and the rest of the line. */ 363 line_len = (int)STRLEN(ml_get_curline()) + 1; 364 line = alloc(ind_len + line_len); 365 if (line == NULL) 366 return FALSE; 367 p = line; 368 } 369 } 370 371 /* Append the original line */ 372 mch_memmove(p, ml_get_curline(), (size_t)line_len); 373 374 /* Replace the line */ 375 ml_replace(curwin->w_cursor.lnum, line, FALSE); 376 377 /* Put the cursor after the indent. */ 378 curwin->w_cursor.col = ind_len; 379 return TRUE; 380 } 381 382 /* 383 * Return the indent of the current line after a number. Return -1 if no 384 * number was found. Used for 'n' in 'formatoptions': numbered list. 385 */ 386 int 387 get_number_indent(lnum) 388 linenr_T lnum; 389 { 390 char_u *line; 391 char_u *p; 392 colnr_T col; 393 pos_T pos; 394 395 if (lnum > curbuf->b_ml.ml_line_count) 396 return -1; 397 line = ml_get(lnum); 398 p = skipwhite(line); 399 if (!VIM_ISDIGIT(*p)) 400 return -1; 401 p = skipdigits(p); 402 if (vim_strchr((char_u *)":.)]}\t ", *p) == NULL) 403 return -1; 404 p = skipwhite(p + 1); 405 if (*p == NUL) 406 return -1; 407 pos.lnum = lnum; 408 pos.col = (colnr_T)(p - line); 409 getvcol(curwin, &pos, &col, NULL, NULL); 410 return (int)col; 411 } 412 413 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) 414 415 static int cin_is_cinword __ARGS((char_u *line)); 416 417 /* 418 * Return TRUE if the string "line" starts with a word from 'cinwords'. 419 */ 420 static int 421 cin_is_cinword(line) 422 char_u *line; 423 { 424 char_u *cinw; 425 char_u *cinw_buf; 426 int cinw_len; 427 int retval = FALSE; 428 int len; 429 430 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; 431 cinw_buf = alloc((unsigned)cinw_len); 432 if (cinw_buf != NULL) 433 { 434 line = skipwhite(line); 435 for (cinw = curbuf->b_p_cinw; *cinw; ) 436 { 437 len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); 438 if (STRNCMP(line, cinw_buf, len) == 0 439 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) 440 { 441 retval = TRUE; 442 break; 443 } 444 } 445 vim_free(cinw_buf); 446 } 447 return retval; 448 } 449 #endif 450 451 /* 452 * open_line: Add a new line below or above the current line. 453 * 454 * For VREPLACE mode, we only add a new line when we get to the end of the 455 * file, otherwise we just start replacing the next line. 456 * 457 * Caller must take care of undo. Since VREPLACE may affect any number of 458 * lines however, it may call u_save_cursor() again when starting to change a 459 * new line. 460 * "flags": OPENLINE_DELSPACES delete spaces after cursor 461 * OPENLINE_DO_COM format comments 462 * OPENLINE_KEEPTRAIL keep trailing spaces 463 * OPENLINE_MARKFIX adjust mark positions after the line break 464 * 465 * Return TRUE for success, FALSE for failure 466 */ 467 int 468 open_line(dir, flags, old_indent) 469 int dir; /* FORWARD or BACKWARD */ 470 int flags; 471 int old_indent; /* indent for after ^^D in Insert mode */ 472 { 473 char_u *saved_line; /* copy of the original line */ 474 char_u *next_line = NULL; /* copy of the next line */ 475 char_u *p_extra = NULL; /* what goes to next line */ 476 int less_cols = 0; /* less columns for mark in new line */ 477 int less_cols_off = 0; /* columns to skip for mark adjust */ 478 pos_T old_cursor; /* old cursor position */ 479 int newcol = 0; /* new cursor column */ 480 int newindent = 0; /* auto-indent of the new line */ 481 int n; 482 int trunc_line = FALSE; /* truncate current line afterwards */ 483 int retval = FALSE; /* return value, default is FAIL */ 484 #ifdef FEAT_COMMENTS 485 int extra_len = 0; /* length of p_extra string */ 486 int lead_len; /* length of comment leader */ 487 char_u *lead_flags; /* position in 'comments' for comment leader */ 488 char_u *leader = NULL; /* copy of comment leader */ 489 #endif 490 char_u *allocated = NULL; /* allocated memory */ 491 #if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \ 492 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS) 493 char_u *p; 494 #endif 495 int saved_char = NUL; /* init for GCC */ 496 #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) 497 pos_T *pos; 498 #endif 499 #ifdef FEAT_SMARTINDENT 500 int do_si = (!p_paste && curbuf->b_p_si 501 # ifdef FEAT_CINDENT 502 && !curbuf->b_p_cin 503 # endif 504 ); 505 int no_si = FALSE; /* reset did_si afterwards */ 506 int first_char = NUL; /* init for GCC */ 507 #endif 508 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) 509 int vreplace_mode; 510 #endif 511 int did_append; /* appended a new line */ 512 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ 513 514 /* 515 * make a copy of the current line so we can mess with it 516 */ 517 saved_line = vim_strsave(ml_get_curline()); 518 if (saved_line == NULL) /* out of memory! */ 519 return FALSE; 520 521 #ifdef FEAT_VREPLACE 522 if (State & VREPLACE_FLAG) 523 { 524 /* 525 * With VREPLACE we make a copy of the next line, which we will be 526 * starting to replace. First make the new line empty and let vim play 527 * with the indenting and comment leader to its heart's content. Then 528 * we grab what it ended up putting on the new line, put back the 529 * original line, and call ins_char() to put each new character onto 530 * the line, replacing what was there before and pushing the right 531 * stuff onto the replace stack. -- webb. 532 */ 533 if (curwin->w_cursor.lnum < orig_line_count) 534 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); 535 else 536 next_line = vim_strsave((char_u *)""); 537 if (next_line == NULL) /* out of memory! */ 538 goto theend; 539 540 /* 541 * In VREPLACE mode, a NL replaces the rest of the line, and starts 542 * replacing the next line, so push all of the characters left on the 543 * line onto the replace stack. We'll push any other characters that 544 * might be replaced at the start of the next line (due to autoindent 545 * etc) a bit later. 546 */ 547 replace_push(NUL); /* Call twice because BS over NL expects it */ 548 replace_push(NUL); 549 p = saved_line + curwin->w_cursor.col; 550 while (*p != NUL) 551 replace_push(*p++); 552 saved_line[curwin->w_cursor.col] = NUL; 553 } 554 #endif 555 556 if ((State & INSERT) 557 #ifdef FEAT_VREPLACE 558 && !(State & VREPLACE_FLAG) 559 #endif 560 ) 561 { 562 p_extra = saved_line + curwin->w_cursor.col; 563 #ifdef FEAT_SMARTINDENT 564 if (do_si) /* need first char after new line break */ 565 { 566 p = skipwhite(p_extra); 567 first_char = *p; 568 } 569 #endif 570 #ifdef FEAT_COMMENTS 571 extra_len = (int)STRLEN(p_extra); 572 #endif 573 saved_char = *p_extra; 574 *p_extra = NUL; 575 } 576 577 u_clearline(); /* cannot do "U" command when adding lines */ 578 #ifdef FEAT_SMARTINDENT 579 did_si = FALSE; 580 #endif 581 ai_col = 0; 582 583 /* 584 * If we just did an auto-indent, then we didn't type anything on 585 * the prior line, and it should be truncated. Do this even if 'ai' is not 586 * set because automatically inserting a comment leader also sets did_ai. 587 */ 588 if (dir == FORWARD && did_ai) 589 trunc_line = TRUE; 590 591 /* 592 * If 'autoindent' and/or 'smartindent' is set, try to figure out what 593 * indent to use for the new line. 594 */ 595 if (curbuf->b_p_ai 596 #ifdef FEAT_SMARTINDENT 597 || do_si 598 #endif 599 ) 600 { 601 /* 602 * count white space on current line 603 */ 604 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts); 605 if (newindent == 0) 606 newindent = old_indent; /* for ^^D command in insert mode */ 607 608 #ifdef FEAT_SMARTINDENT 609 /* 610 * Do smart indenting. 611 * In insert/replace mode (only when dir == FORWARD) 612 * we may move some text to the next line. If it starts with '{' 613 * don't add an indent. Fixes inserting a NL before '{' in line 614 * "if (condition) {" 615 */ 616 if (!trunc_line && do_si && *saved_line != NUL 617 && (p_extra == NULL || first_char != '{')) 618 { 619 char_u *ptr; 620 char_u last_char; 621 622 old_cursor = curwin->w_cursor; 623 ptr = saved_line; 624 # ifdef FEAT_COMMENTS 625 if (flags & OPENLINE_DO_COM) 626 lead_len = get_leader_len(ptr, NULL, FALSE); 627 else 628 lead_len = 0; 629 # endif 630 if (dir == FORWARD) 631 { 632 /* 633 * Skip preprocessor directives, unless they are 634 * recognised as comments. 635 */ 636 if ( 637 # ifdef FEAT_COMMENTS 638 lead_len == 0 && 639 # endif 640 ptr[0] == '#') 641 { 642 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) 643 ptr = ml_get(--curwin->w_cursor.lnum); 644 newindent = get_indent(); 645 } 646 # ifdef FEAT_COMMENTS 647 if (flags & OPENLINE_DO_COM) 648 lead_len = get_leader_len(ptr, NULL, FALSE); 649 else 650 lead_len = 0; 651 if (lead_len > 0) 652 { 653 /* 654 * This case gets the following right: 655 * \* 656 * * A comment (read '\' as '/'). 657 * *\ 658 * #define IN_THE_WAY 659 * This should line up here; 660 */ 661 p = skipwhite(ptr); 662 if (p[0] == '/' && p[1] == '*') 663 p++; 664 if (p[0] == '*') 665 { 666 for (p++; *p; p++) 667 { 668 if (p[0] == '/' && p[-1] == '*') 669 { 670 /* 671 * End of C comment, indent should line up 672 * with the line containing the start of 673 * the comment 674 */ 675 curwin->w_cursor.col = (colnr_T)(p - ptr); 676 if ((pos = findmatch(NULL, NUL)) != NULL) 677 { 678 curwin->w_cursor.lnum = pos->lnum; 679 newindent = get_indent(); 680 } 681 } 682 } 683 } 684 } 685 else /* Not a comment line */ 686 # endif 687 { 688 /* Find last non-blank in line */ 689 p = ptr + STRLEN(ptr) - 1; 690 while (p > ptr && vim_iswhite(*p)) 691 --p; 692 last_char = *p; 693 694 /* 695 * find the character just before the '{' or ';' 696 */ 697 if (last_char == '{' || last_char == ';') 698 { 699 if (p > ptr) 700 --p; 701 while (p > ptr && vim_iswhite(*p)) 702 --p; 703 } 704 /* 705 * Try to catch lines that are split over multiple 706 * lines. eg: 707 * if (condition && 708 * condition) { 709 * Should line up here! 710 * } 711 */ 712 if (*p == ')') 713 { 714 curwin->w_cursor.col = (colnr_T)(p - ptr); 715 if ((pos = findmatch(NULL, '(')) != NULL) 716 { 717 curwin->w_cursor.lnum = pos->lnum; 718 newindent = get_indent(); 719 ptr = ml_get_curline(); 720 } 721 } 722 /* 723 * If last character is '{' do indent, without 724 * checking for "if" and the like. 725 */ 726 if (last_char == '{') 727 { 728 did_si = TRUE; /* do indent */ 729 no_si = TRUE; /* don't delete it when '{' typed */ 730 } 731 /* 732 * Look for "if" and the like, use 'cinwords'. 733 * Don't do this if the previous line ended in ';' or 734 * '}'. 735 */ 736 else if (last_char != ';' && last_char != '}' 737 && cin_is_cinword(ptr)) 738 did_si = TRUE; 739 } 740 } 741 else /* dir == BACKWARD */ 742 { 743 /* 744 * Skip preprocessor directives, unless they are 745 * recognised as comments. 746 */ 747 if ( 748 # ifdef FEAT_COMMENTS 749 lead_len == 0 && 750 # endif 751 ptr[0] == '#') 752 { 753 int was_backslashed = FALSE; 754 755 while ((ptr[0] == '#' || was_backslashed) && 756 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 757 { 758 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') 759 was_backslashed = TRUE; 760 else 761 was_backslashed = FALSE; 762 ptr = ml_get(++curwin->w_cursor.lnum); 763 } 764 if (was_backslashed) 765 newindent = 0; /* Got to end of file */ 766 else 767 newindent = get_indent(); 768 } 769 p = skipwhite(ptr); 770 if (*p == '}') /* if line starts with '}': do indent */ 771 did_si = TRUE; 772 else /* can delete indent when '{' typed */ 773 can_si_back = TRUE; 774 } 775 curwin->w_cursor = old_cursor; 776 } 777 if (do_si) 778 can_si = TRUE; 779 #endif /* FEAT_SMARTINDENT */ 780 781 did_ai = TRUE; 782 } 783 784 #ifdef FEAT_COMMENTS 785 /* 786 * Find out if the current line starts with a comment leader. 787 * This may then be inserted in front of the new line. 788 */ 789 end_comment_pending = NUL; 790 if (flags & OPENLINE_DO_COM) 791 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD); 792 else 793 lead_len = 0; 794 if (lead_len > 0) 795 { 796 char_u *lead_repl = NULL; /* replaces comment leader */ 797 int lead_repl_len = 0; /* length of *lead_repl */ 798 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ 799 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ 800 char_u *comment_end = NULL; /* where lead_end has been found */ 801 int extra_space = FALSE; /* append extra space */ 802 int current_flag; 803 int require_blank = FALSE; /* requires blank after middle */ 804 char_u *p2; 805 806 /* 807 * If the comment leader has the start, middle or end flag, it may not 808 * be used or may be replaced with the middle leader. 809 */ 810 for (p = lead_flags; *p && *p != ':'; ++p) 811 { 812 if (*p == COM_BLANK) 813 { 814 require_blank = TRUE; 815 continue; 816 } 817 if (*p == COM_START || *p == COM_MIDDLE) 818 { 819 current_flag = *p; 820 if (*p == COM_START) 821 { 822 /* 823 * Doing "O" on a start of comment does not insert leader. 824 */ 825 if (dir == BACKWARD) 826 { 827 lead_len = 0; 828 break; 829 } 830 831 /* find start of middle part */ 832 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); 833 require_blank = FALSE; 834 } 835 836 /* 837 * Isolate the strings of the middle and end leader. 838 */ 839 while (*p && p[-1] != ':') /* find end of middle flags */ 840 { 841 if (*p == COM_BLANK) 842 require_blank = TRUE; 843 ++p; 844 } 845 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); 846 847 while (*p && p[-1] != ':') /* find end of end flags */ 848 { 849 /* Check whether we allow automatic ending of comments */ 850 if (*p == COM_AUTO_END) 851 end_comment_pending = -1; /* means we want to set it */ 852 ++p; 853 } 854 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); 855 856 if (end_comment_pending == -1) /* we can set it now */ 857 end_comment_pending = lead_end[n - 1]; 858 859 /* 860 * If the end of the comment is in the same line, don't use 861 * the comment leader. 862 */ 863 if (dir == FORWARD) 864 { 865 for (p = saved_line + lead_len; *p; ++p) 866 if (STRNCMP(p, lead_end, n) == 0) 867 { 868 comment_end = p; 869 lead_len = 0; 870 break; 871 } 872 } 873 874 /* 875 * Doing "o" on a start of comment inserts the middle leader. 876 */ 877 if (lead_len > 0) 878 { 879 if (current_flag == COM_START) 880 { 881 lead_repl = lead_middle; 882 lead_repl_len = (int)STRLEN(lead_middle); 883 } 884 885 /* 886 * If we have hit RETURN immediately after the start 887 * comment leader, then put a space after the middle 888 * comment leader on the next line. 889 */ 890 if (!vim_iswhite(saved_line[lead_len - 1]) 891 && ((p_extra != NULL 892 && (int)curwin->w_cursor.col == lead_len) 893 || (p_extra == NULL 894 && saved_line[lead_len] == NUL) 895 || require_blank)) 896 extra_space = TRUE; 897 } 898 break; 899 } 900 if (*p == COM_END) 901 { 902 /* 903 * Doing "o" on the end of a comment does not insert leader. 904 * Remember where the end is, might want to use it to find the 905 * start (for C-comments). 906 */ 907 if (dir == FORWARD) 908 { 909 comment_end = skipwhite(saved_line); 910 lead_len = 0; 911 break; 912 } 913 914 /* 915 * Doing "O" on the end of a comment inserts the middle leader. 916 * Find the string for the middle leader, searching backwards. 917 */ 918 while (p > curbuf->b_p_com && *p != ',') 919 --p; 920 for (lead_repl = p; lead_repl > curbuf->b_p_com 921 && lead_repl[-1] != ':'; --lead_repl) 922 ; 923 lead_repl_len = (int)(p - lead_repl); 924 925 /* We can probably always add an extra space when doing "O" on 926 * the comment-end */ 927 extra_space = TRUE; 928 929 /* Check whether we allow automatic ending of comments */ 930 for (p2 = p; *p2 && *p2 != ':'; p2++) 931 { 932 if (*p2 == COM_AUTO_END) 933 end_comment_pending = -1; /* means we want to set it */ 934 } 935 if (end_comment_pending == -1) 936 { 937 /* Find last character in end-comment string */ 938 while (*p2 && *p2 != ',') 939 p2++; 940 end_comment_pending = p2[-1]; 941 } 942 break; 943 } 944 if (*p == COM_FIRST) 945 { 946 /* 947 * Comment leader for first line only: Don't repeat leader 948 * when using "O", blank out leader when using "o". 949 */ 950 if (dir == BACKWARD) 951 lead_len = 0; 952 else 953 { 954 lead_repl = (char_u *)""; 955 lead_repl_len = 0; 956 } 957 break; 958 } 959 } 960 if (lead_len) 961 { 962 /* allocate buffer (may concatenate p_exta later) */ 963 leader = alloc(lead_len + lead_repl_len + extra_space + 964 extra_len + 1); 965 allocated = leader; /* remember to free it later */ 966 967 if (leader == NULL) 968 lead_len = 0; 969 else 970 { 971 STRNCPY(leader, saved_line, lead_len); 972 leader[lead_len] = NUL; 973 974 /* 975 * Replace leader with lead_repl, right or left adjusted 976 */ 977 if (lead_repl != NULL) 978 { 979 int c = 0; 980 int off = 0; 981 982 for (p = lead_flags; *p && *p != ':'; ++p) 983 { 984 if (*p == COM_RIGHT || *p == COM_LEFT) 985 c = *p; 986 else if (VIM_ISDIGIT(*p) || *p == '-') 987 off = getdigits(&p); 988 } 989 if (c == COM_RIGHT) /* right adjusted leader */ 990 { 991 /* find last non-white in the leader to line up with */ 992 for (p = leader + lead_len - 1; p > leader 993 && vim_iswhite(*p); --p) 994 ; 995 ++p; 996 997 #ifdef FEAT_MBYTE 998 /* Compute the length of the replaced characters in 999 * screen characters, not bytes. */ 1000 { 1001 int repl_size = vim_strnsize(lead_repl, 1002 lead_repl_len); 1003 int old_size = 0; 1004 char_u *endp = p; 1005 int l; 1006 1007 while (old_size < repl_size && p > leader) 1008 { 1009 --p; 1010 p -= mb_head_off(leader, p); 1011 old_size += ptr2cells(p); 1012 } 1013 l = lead_repl_len - (endp - p); 1014 if (l != 0) 1015 mch_memmove(endp + l, endp, 1016 (size_t)((leader + lead_len) - endp)); 1017 lead_len += l; 1018 } 1019 #else 1020 if (p < leader + lead_repl_len) 1021 p = leader; 1022 else 1023 p -= lead_repl_len; 1024 #endif 1025 mch_memmove(p, lead_repl, (size_t)lead_repl_len); 1026 if (p + lead_repl_len > leader + lead_len) 1027 p[lead_repl_len] = NUL; 1028 1029 /* blank-out any other chars from the old leader. */ 1030 while (--p >= leader) 1031 { 1032 #ifdef FEAT_MBYTE 1033 int l = mb_head_off(leader, p); 1034 1035 if (l > 1) 1036 { 1037 p -= l; 1038 if (ptr2cells(p) > 1) 1039 { 1040 p[1] = ' '; 1041 --l; 1042 } 1043 mch_memmove(p + 1, p + l + 1, 1044 (size_t)((leader + lead_len) - (p + l + 1))); 1045 lead_len -= l; 1046 *p = ' '; 1047 } 1048 else 1049 #endif 1050 if (!vim_iswhite(*p)) 1051 *p = ' '; 1052 } 1053 } 1054 else /* left adjusted leader */ 1055 { 1056 p = skipwhite(leader); 1057 #ifdef FEAT_MBYTE 1058 /* Compute the length of the replaced characters in 1059 * screen characters, not bytes. Move the part that is 1060 * not to be overwritten. */ 1061 { 1062 int repl_size = vim_strnsize(lead_repl, 1063 lead_repl_len); 1064 int i; 1065 int l; 1066 1067 for (i = 0; p[i] != NUL && i < lead_len; i += l) 1068 { 1069 l = mb_ptr2len_check(p + i); 1070 if (vim_strnsize(p, i + l) > repl_size) 1071 break; 1072 } 1073 if (i != lead_repl_len) 1074 { 1075 mch_memmove(p + lead_repl_len, p + i, 1076 (size_t)(lead_len - i - (leader - p))); 1077 lead_len += lead_repl_len - i; 1078 } 1079 } 1080 #endif 1081 mch_memmove(p, lead_repl, (size_t)lead_repl_len); 1082 1083 /* Replace any remaining non-white chars in the old 1084 * leader by spaces. Keep Tabs, the indent must 1085 * remain the same. */ 1086 for (p += lead_repl_len; p < leader + lead_len; ++p) 1087 if (!vim_iswhite(*p)) 1088 { 1089 /* Don't put a space before a TAB. */ 1090 if (p + 1 < leader + lead_len && p[1] == TAB) 1091 { 1092 --lead_len; 1093 mch_memmove(p, p + 1, 1094 (leader + lead_len) - p); 1095 } 1096 else 1097 { 1098 #ifdef FEAT_MBYTE 1099 int l = mb_ptr2len_check(p); 1100 1101 if (l > 1) 1102 { 1103 if (ptr2cells(p) > 1) 1104 { 1105 /* Replace a double-wide char with 1106 * two spaces */ 1107 --l; 1108 *p++ = ' '; 1109 } 1110 mch_memmove(p + 1, p + l, 1111 (leader + lead_len) - p); 1112 lead_len -= l - 1; 1113 } 1114 #endif 1115 *p = ' '; 1116 } 1117 } 1118 *p = NUL; 1119 } 1120 1121 /* Recompute the indent, it may have changed. */ 1122 if (curbuf->b_p_ai 1123 #ifdef FEAT_SMARTINDENT 1124 || do_si 1125 #endif 1126 ) 1127 newindent = get_indent_str(leader, (int)curbuf->b_p_ts); 1128 1129 /* Add the indent offset */ 1130 if (newindent + off < 0) 1131 { 1132 off = -newindent; 1133 newindent = 0; 1134 } 1135 else 1136 newindent += off; 1137 1138 /* Correct trailing spaces for the shift, so that 1139 * alignment remains equal. */ 1140 while (off > 0 && lead_len > 0 1141 && leader[lead_len - 1] == ' ') 1142 { 1143 /* Don't do it when there is a tab before the space */ 1144 if (vim_strchr(skipwhite(leader), '\t') != NULL) 1145 break; 1146 --lead_len; 1147 --off; 1148 } 1149 1150 /* If the leader ends in white space, don't add an 1151 * extra space */ 1152 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1])) 1153 extra_space = FALSE; 1154 leader[lead_len] = NUL; 1155 } 1156 1157 if (extra_space) 1158 { 1159 leader[lead_len++] = ' '; 1160 leader[lead_len] = NUL; 1161 } 1162 1163 newcol = lead_len; 1164 1165 /* 1166 * if a new indent will be set below, remove the indent that 1167 * is in the comment leader 1168 */ 1169 if (newindent 1170 #ifdef FEAT_SMARTINDENT 1171 || did_si 1172 #endif 1173 ) 1174 { 1175 while (lead_len && vim_iswhite(*leader)) 1176 { 1177 --lead_len; 1178 --newcol; 1179 ++leader; 1180 } 1181 } 1182 1183 } 1184 #ifdef FEAT_SMARTINDENT 1185 did_si = can_si = FALSE; 1186 #endif 1187 } 1188 else if (comment_end != NULL) 1189 { 1190 /* 1191 * We have finished a comment, so we don't use the leader. 1192 * If this was a C-comment and 'ai' or 'si' is set do a normal 1193 * indent to align with the line containing the start of the 1194 * comment. 1195 */ 1196 if (comment_end[0] == '*' && comment_end[1] == '/' && 1197 (curbuf->b_p_ai 1198 #ifdef FEAT_SMARTINDENT 1199 || do_si 1200 #endif 1201 )) 1202 { 1203 old_cursor = curwin->w_cursor; 1204 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); 1205 if ((pos = findmatch(NULL, NUL)) != NULL) 1206 { 1207 curwin->w_cursor.lnum = pos->lnum; 1208 newindent = get_indent(); 1209 } 1210 curwin->w_cursor = old_cursor; 1211 } 1212 } 1213 } 1214 #endif 1215 1216 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ 1217 if (p_extra != NULL) 1218 { 1219 *p_extra = saved_char; /* restore char that NUL replaced */ 1220 1221 /* 1222 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first 1223 * non-blank. 1224 * 1225 * When in REPLACE mode, put the deleted blanks on the replace stack, 1226 * preceded by a NUL, so they can be put back when a BS is entered. 1227 */ 1228 if (REPLACE_NORMAL(State)) 1229 replace_push(NUL); /* end of extra blanks */ 1230 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) 1231 { 1232 while ((*p_extra == ' ' || *p_extra == '\t') 1233 #ifdef FEAT_MBYTE 1234 && (!enc_utf8 1235 || !utf_iscomposing(utf_ptr2char(p_extra + 1))) 1236 #endif 1237 ) 1238 { 1239 if (REPLACE_NORMAL(State)) 1240 replace_push(*p_extra); 1241 ++p_extra; 1242 ++less_cols_off; 1243 } 1244 } 1245 if (*p_extra != NUL) 1246 did_ai = FALSE; /* append some text, don't truncate now */ 1247 1248 /* columns for marks adjusted for removed columns */ 1249 less_cols = (int)(p_extra - saved_line); 1250 } 1251 1252 if (p_extra == NULL) 1253 p_extra = (char_u *)""; /* append empty line */ 1254 1255 #ifdef FEAT_COMMENTS 1256 /* concatenate leader and p_extra, if there is a leader */ 1257 if (lead_len) 1258 { 1259 STRCAT(leader, p_extra); 1260 p_extra = leader; 1261 did_ai = TRUE; /* So truncating blanks works with comments */ 1262 less_cols -= lead_len; 1263 } 1264 else 1265 end_comment_pending = NUL; /* turns out there was no leader */ 1266 #endif 1267 1268 old_cursor = curwin->w_cursor; 1269 if (dir == BACKWARD) 1270 --curwin->w_cursor.lnum; 1271 #ifdef FEAT_VREPLACE 1272 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) 1273 #endif 1274 { 1275 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) 1276 == FAIL) 1277 goto theend; 1278 /* Postpone calling changed_lines(), because it would mess up folding 1279 * with markers. */ 1280 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); 1281 did_append = TRUE; 1282 } 1283 #ifdef FEAT_VREPLACE 1284 else 1285 { 1286 /* 1287 * In VREPLACE mode we are starting to replace the next line. 1288 */ 1289 curwin->w_cursor.lnum++; 1290 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) 1291 { 1292 /* In case we NL to a new line, BS to the previous one, and NL 1293 * again, we don't want to save the new line for undo twice. 1294 */ 1295 (void)u_save_cursor(); /* errors are ignored! */ 1296 vr_lines_changed++; 1297 } 1298 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); 1299 changed_bytes(curwin->w_cursor.lnum, 0); 1300 curwin->w_cursor.lnum--; 1301 did_append = FALSE; 1302 } 1303 #endif 1304 1305 if (newindent 1306 #ifdef FEAT_SMARTINDENT 1307 || did_si 1308 #endif 1309 ) 1310 { 1311 ++curwin->w_cursor.lnum; 1312 #ifdef FEAT_SMARTINDENT 1313 if (did_si) 1314 { 1315 if (p_sr) 1316 newindent -= newindent % (int)curbuf->b_p_sw; 1317 newindent += (int)curbuf->b_p_sw; 1318 } 1319 #endif 1320 /* Copy the indent only if expand tab is disabled */ 1321 if (curbuf->b_p_ci && !curbuf->b_p_et) 1322 { 1323 (void)copy_indent(newindent, saved_line); 1324 1325 /* 1326 * Set the 'preserveindent' option so that any further screwing 1327 * with the line doesn't entirely destroy our efforts to preserve 1328 * it. It gets restored at the function end. 1329 */ 1330 curbuf->b_p_pi = TRUE; 1331 } 1332 else 1333 (void)set_indent(newindent, SIN_INSERT); 1334 less_cols -= curwin->w_cursor.col; 1335 1336 ai_col = curwin->w_cursor.col; 1337 1338 /* 1339 * In REPLACE mode, for each character in the new indent, there must 1340 * be a NUL on the replace stack, for when it is deleted with BS 1341 */ 1342 if (REPLACE_NORMAL(State)) 1343 for (n = 0; n < (int)curwin->w_cursor.col; ++n) 1344 replace_push(NUL); 1345 newcol += curwin->w_cursor.col; 1346 #ifdef FEAT_SMARTINDENT 1347 if (no_si) 1348 did_si = FALSE; 1349 #endif 1350 } 1351 1352 #ifdef FEAT_COMMENTS 1353 /* 1354 * In REPLACE mode, for each character in the extra leader, there must be 1355 * a NUL on the replace stack, for when it is deleted with BS. 1356 */ 1357 if (REPLACE_NORMAL(State)) 1358 while (lead_len-- > 0) 1359 replace_push(NUL); 1360 #endif 1361 1362 curwin->w_cursor = old_cursor; 1363 1364 if (dir == FORWARD) 1365 { 1366 if (trunc_line || (State & INSERT)) 1367 { 1368 /* truncate current line at cursor */ 1369 saved_line[curwin->w_cursor.col] = NUL; 1370 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ 1371 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) 1372 truncate_spaces(saved_line); 1373 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); 1374 saved_line = NULL; 1375 if (did_append) 1376 { 1377 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, 1378 curwin->w_cursor.lnum + 1, 1L); 1379 did_append = FALSE; 1380 1381 /* Move marks after the line break to the new line. */ 1382 if (flags & OPENLINE_MARKFIX) 1383 mark_col_adjust(curwin->w_cursor.lnum, 1384 curwin->w_cursor.col + less_cols_off, 1385 1L, (long)-less_cols); 1386 } 1387 else 1388 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); 1389 } 1390 1391 /* 1392 * Put the cursor on the new line. Careful: the scrollup() above may 1393 * have moved w_cursor, we must use old_cursor. 1394 */ 1395 curwin->w_cursor.lnum = old_cursor.lnum + 1; 1396 } 1397 if (did_append) 1398 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); 1399 1400 curwin->w_cursor.col = newcol; 1401 #ifdef FEAT_VIRTUALEDIT 1402 curwin->w_cursor.coladd = 0; 1403 #endif 1404 1405 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) 1406 /* 1407 * In VREPLACE mode, we are handling the replace stack ourselves, so stop 1408 * fixthisline() from doing it (via change_indent()) by telling it we're in 1409 * normal INSERT mode. 1410 */ 1411 if (State & VREPLACE_FLAG) 1412 { 1413 vreplace_mode = State; /* So we know to put things right later */ 1414 State = INSERT; 1415 } 1416 else 1417 vreplace_mode = 0; 1418 #endif 1419 #ifdef FEAT_LISP 1420 /* 1421 * May do lisp indenting. 1422 */ 1423 if (!p_paste 1424 # ifdef FEAT_COMMENTS 1425 && leader == NULL 1426 # endif 1427 && curbuf->b_p_lisp 1428 && curbuf->b_p_ai) 1429 { 1430 fixthisline(get_lisp_indent); 1431 p = ml_get_curline(); 1432 ai_col = (colnr_T)(skipwhite(p) - p); 1433 } 1434 #endif 1435 #ifdef FEAT_CINDENT 1436 /* 1437 * May do indenting after opening a new line. 1438 */ 1439 if (!p_paste 1440 && (curbuf->b_p_cin 1441 # ifdef FEAT_EVAL 1442 || *curbuf->b_p_inde != NUL 1443 # endif 1444 ) 1445 && in_cinkeys(dir == FORWARD 1446 ? KEY_OPEN_FORW 1447 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) 1448 { 1449 do_c_expr_indent(); 1450 p = ml_get_curline(); 1451 ai_col = (colnr_T)(skipwhite(p) - p); 1452 } 1453 #endif 1454 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) 1455 if (vreplace_mode != 0) 1456 State = vreplace_mode; 1457 #endif 1458 1459 #ifdef FEAT_VREPLACE 1460 /* 1461 * Finally, VREPLACE gets the stuff on the new line, then puts back the 1462 * original line, and inserts the new stuff char by char, pushing old stuff 1463 * onto the replace stack (via ins_char()). 1464 */ 1465 if (State & VREPLACE_FLAG) 1466 { 1467 /* Put new line in p_extra */ 1468 p_extra = vim_strsave(ml_get_curline()); 1469 if (p_extra == NULL) 1470 goto theend; 1471 1472 /* Put back original line */ 1473 ml_replace(curwin->w_cursor.lnum, next_line, FALSE); 1474 1475 /* Insert new stuff into line again */ 1476 curwin->w_cursor.col = 0; 1477 #ifdef FEAT_VIRTUALEDIT 1478 curwin->w_cursor.coladd = 0; 1479 #endif 1480 ins_bytes(p_extra); /* will call changed_bytes() */ 1481 vim_free(p_extra); 1482 next_line = NULL; 1483 } 1484 #endif 1485 1486 retval = TRUE; /* success! */ 1487 theend: 1488 curbuf->b_p_pi = saved_pi; 1489 vim_free(saved_line); 1490 vim_free(next_line); 1491 vim_free(allocated); 1492 return retval; 1493 } 1494 1495 #if defined(FEAT_COMMENTS) || defined(PROTO) 1496 /* 1497 * get_leader_len() returns the length of the prefix of the given string 1498 * which introduces a comment. If this string is not a comment then 0 is 1499 * returned. 1500 * When "flags" is not NULL, it is set to point to the flags of the recognized 1501 * comment leader. 1502 * "backward" must be true for the "O" command. 1503 */ 1504 int 1505 get_leader_len(line, flags, backward) 1506 char_u *line; 1507 char_u **flags; 1508 int backward; 1509 { 1510 int i, j; 1511 int got_com = FALSE; 1512 int found_one; 1513 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ 1514 char_u *string; /* pointer to comment string */ 1515 char_u *list; 1516 1517 i = 0; 1518 while (vim_iswhite(line[i])) /* leading white space is ignored */ 1519 ++i; 1520 1521 /* 1522 * Repeat to match several nested comment strings. 1523 */ 1524 while (line[i]) 1525 { 1526 /* 1527 * scan through the 'comments' option for a match 1528 */ 1529 found_one = FALSE; 1530 for (list = curbuf->b_p_com; *list; ) 1531 { 1532 /* 1533 * Get one option part into part_buf[]. Advance list to next one. 1534 * put string at start of string. 1535 */ 1536 if (!got_com && flags != NULL) /* remember where flags started */ 1537 *flags = list; 1538 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); 1539 string = vim_strchr(part_buf, ':'); 1540 if (string == NULL) /* missing ':', ignore this part */ 1541 continue; 1542 *string++ = NUL; /* isolate flags from string */ 1543 1544 /* 1545 * When already found a nested comment, only accept further 1546 * nested comments. 1547 */ 1548 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) 1549 continue; 1550 1551 /* When 'O' flag used don't use for "O" command */ 1552 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) 1553 continue; 1554 1555 /* 1556 * Line contents and string must match. 1557 * When string starts with white space, must have some white space 1558 * (but the amount does not need to match, there might be a mix of 1559 * TABs and spaces). 1560 */ 1561 if (vim_iswhite(string[0])) 1562 { 1563 if (i == 0 || !vim_iswhite(line[i - 1])) 1564 continue; 1565 while (vim_iswhite(string[0])) 1566 ++string; 1567 } 1568 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) 1569 ; 1570 if (string[j] != NUL) 1571 continue; 1572 1573 /* 1574 * When 'b' flag used, there must be white space or an 1575 * end-of-line after the string in the line. 1576 */ 1577 if (vim_strchr(part_buf, COM_BLANK) != NULL 1578 && !vim_iswhite(line[i + j]) && line[i + j] != NUL) 1579 continue; 1580 1581 /* 1582 * We have found a match, stop searching. 1583 */ 1584 i += j; 1585 got_com = TRUE; 1586 found_one = TRUE; 1587 break; 1588 } 1589 1590 /* 1591 * No match found, stop scanning. 1592 */ 1593 if (!found_one) 1594 break; 1595 1596 /* 1597 * Include any trailing white space. 1598 */ 1599 while (vim_iswhite(line[i])) 1600 ++i; 1601 1602 /* 1603 * If this comment doesn't nest, stop here. 1604 */ 1605 if (vim_strchr(part_buf, COM_NEST) == NULL) 1606 break; 1607 } 1608 return (got_com ? i : 0); 1609 } 1610 #endif 1611 1612 /* 1613 * Return the number of window lines occupied by buffer line "lnum". 1614 */ 1615 int 1616 plines(lnum) 1617 linenr_T lnum; 1618 { 1619 return plines_win(curwin, lnum, TRUE); 1620 } 1621 1622 int 1623 plines_win(wp, lnum, winheight) 1624 win_T *wp; 1625 linenr_T lnum; 1626 int winheight; /* when TRUE limit to window height */ 1627 { 1628 #if defined(FEAT_DIFF) || defined(PROTO) 1629 /* Check for filler lines above this buffer line. When folded the result 1630 * is one line anyway. */ 1631 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); 1632 } 1633 1634 int 1635 plines_nofill(lnum) 1636 linenr_T lnum; 1637 { 1638 return plines_win_nofill(curwin, lnum, TRUE); 1639 } 1640 1641 int 1642 plines_win_nofill(wp, lnum, winheight) 1643 win_T *wp; 1644 linenr_T lnum; 1645 int winheight; /* when TRUE limit to window height */ 1646 { 1647 #endif 1648 int lines; 1649 1650 if (!wp->w_p_wrap) 1651 return 1; 1652 1653 #ifdef FEAT_VERTSPLIT 1654 if (wp->w_width == 0) 1655 return 1; 1656 #endif 1657 1658 #ifdef FEAT_FOLDING 1659 /* A folded lines is handled just like an empty line. */ 1660 /* NOTE: Caller must handle lines that are MAYBE folded. */ 1661 if (lineFolded(wp, lnum) == TRUE) 1662 return 1; 1663 #endif 1664 1665 lines = plines_win_nofold(wp, lnum); 1666 if (winheight > 0 && lines > wp->w_height) 1667 return (int)wp->w_height; 1668 return lines; 1669 } 1670 1671 /* 1672 * Return number of window lines physical line "lnum" will occupy in window 1673 * "wp". Does not care about folding, 'wrap' or 'diff'. 1674 */ 1675 int 1676 plines_win_nofold(wp, lnum) 1677 win_T *wp; 1678 linenr_T lnum; 1679 { 1680 char_u *s; 1681 long col; 1682 int width; 1683 1684 s = ml_get_buf(wp->w_buffer, lnum, FALSE); 1685 if (*s == NUL) /* empty line */ 1686 return 1; 1687 col = win_linetabsize(wp, s, (colnr_T)MAXCOL); 1688 1689 /* 1690 * If list mode is on, then the '$' at the end of the line may take up one 1691 * extra column. 1692 */ 1693 if (wp->w_p_list && lcs_eol != NUL) 1694 col += 1; 1695 1696 /* 1697 * Add column offset for 'number' and 'foldcolumn'. 1698 */ 1699 width = W_WIDTH(wp) - win_col_off(wp); 1700 if (width <= 0) 1701 return 32000; 1702 if (col <= width) 1703 return 1; 1704 col -= width; 1705 width += win_col_off2(wp); 1706 return (col + (width - 1)) / width + 1; 1707 } 1708 1709 /* 1710 * Like plines_win(), but only reports the number of physical screen lines 1711 * used from the start of the line to the given column number. 1712 */ 1713 int 1714 plines_win_col(wp, lnum, column) 1715 win_T *wp; 1716 linenr_T lnum; 1717 long column; 1718 { 1719 long col; 1720 char_u *s; 1721 int lines = 0; 1722 int width; 1723 1724 #ifdef FEAT_DIFF 1725 /* Check for filler lines above this buffer line. When folded the result 1726 * is one line anyway. */ 1727 lines = diff_check_fill(wp, lnum); 1728 #endif 1729 1730 if (!wp->w_p_wrap) 1731 return lines + 1; 1732 1733 #ifdef FEAT_VERTSPLIT 1734 if (wp->w_width == 0) 1735 return lines + 1; 1736 #endif 1737 1738 s = ml_get_buf(wp->w_buffer, lnum, FALSE); 1739 1740 col = 0; 1741 while (*s != NUL && --column >= 0) 1742 { 1743 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL); 1744 #ifdef FEAT_MBYTE 1745 if (has_mbyte) 1746 s += (*mb_ptr2len_check)(s); 1747 else 1748 #endif 1749 ++s; 1750 } 1751 1752 /* 1753 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in 1754 * INSERT mode, then col must be adjusted so that it represents the last 1755 * screen position of the TAB. This only fixes an error when the TAB wraps 1756 * from one screen line to the next (when 'columns' is not a multiple of 1757 * 'ts') -- webb. 1758 */ 1759 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) 1760 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1; 1761 1762 /* 1763 * Add column offset for 'number', 'foldcolumn', etc. 1764 */ 1765 width = W_WIDTH(wp) - win_col_off(wp); 1766 if (width > 0) 1767 { 1768 lines += 1; 1769 if (col >= width) 1770 lines += (col - width) / (width + win_col_off2(wp)); 1771 if (lines <= wp->w_height) 1772 return lines; 1773 } 1774 return (int)(wp->w_height); /* maximum length */ 1775 } 1776 1777 int 1778 plines_m_win(wp, first, last) 1779 win_T *wp; 1780 linenr_T first, last; 1781 { 1782 int count = 0; 1783 1784 while (first <= last) 1785 { 1786 #ifdef FEAT_FOLDING 1787 int x; 1788 1789 /* Check if there are any really folded lines, but also included lines 1790 * that are maybe folded. */ 1791 x = foldedCount(wp, first, NULL); 1792 if (x > 0) 1793 { 1794 ++count; /* count 1 for "+-- folded" line */ 1795 first += x; 1796 } 1797 else 1798 #endif 1799 { 1800 #ifdef FEAT_DIFF 1801 if (first == wp->w_topline) 1802 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; 1803 else 1804 #endif 1805 count += plines_win(wp, first, TRUE); 1806 ++first; 1807 } 1808 } 1809 return (count); 1810 } 1811 1812 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO) 1813 /* 1814 * Insert string "p" at the cursor position. Stops at a NUL byte. 1815 * Handles Replace mode and multi-byte characters. 1816 */ 1817 void 1818 ins_bytes(p) 1819 char_u *p; 1820 { 1821 ins_bytes_len(p, (int)STRLEN(p)); 1822 } 1823 #endif 1824 1825 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ 1826 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO) 1827 /* 1828 * Insert string "p" with length "len" at the cursor position. 1829 * Handles Replace mode and multi-byte characters. 1830 */ 1831 void 1832 ins_bytes_len(p, len) 1833 char_u *p; 1834 int len; 1835 { 1836 int i; 1837 # ifdef FEAT_MBYTE 1838 int n; 1839 1840 for (i = 0; i < len; i += n) 1841 { 1842 n = (*mb_ptr2len_check)(p + i); 1843 ins_char_bytes(p + i, n); 1844 } 1845 # else 1846 for (i = 0; i < len; ++i) 1847 ins_char(p[i]); 1848 # endif 1849 } 1850 #endif 1851 1852 /* 1853 * Insert or replace a single character at the cursor position. 1854 * When in REPLACE or VREPLACE mode, replace any existing character. 1855 * Caller must have prepared for undo. 1856 * For multi-byte characters we get the whole character, the caller must 1857 * convert bytes to a character. 1858 */ 1859 void 1860 ins_char(c) 1861 int c; 1862 { 1863 #if defined(FEAT_MBYTE) || defined(PROTO) 1864 char_u buf[MB_MAXBYTES]; 1865 int n; 1866 1867 n = (*mb_char2bytes)(c, buf); 1868 1869 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. 1870 * Happens for CTRL-Vu9900. */ 1871 if (buf[0] == 0) 1872 buf[0] = '\n'; 1873 1874 ins_char_bytes(buf, n); 1875 } 1876 1877 void 1878 ins_char_bytes(buf, charlen) 1879 char_u *buf; 1880 int charlen; 1881 { 1882 int c = buf[0]; 1883 int l, j; 1884 #endif 1885 int newlen; /* nr of bytes inserted */ 1886 int oldlen; /* nr of bytes deleted (0 when not replacing) */ 1887 char_u *p; 1888 char_u *newp; 1889 char_u *oldp; 1890 int linelen; /* length of old line including NUL */ 1891 colnr_T col; 1892 linenr_T lnum = curwin->w_cursor.lnum; 1893 int i; 1894 1895 #ifdef FEAT_VIRTUALEDIT 1896 /* Break tabs if needed. */ 1897 if (virtual_active() && curwin->w_cursor.coladd > 0) 1898 coladvance_force(getviscol()); 1899 #endif 1900 1901 col = curwin->w_cursor.col; 1902 oldp = ml_get(lnum); 1903 linelen = (int)STRLEN(oldp) + 1; 1904 1905 /* The lengths default to the values for when not replacing. */ 1906 oldlen = 0; 1907 #ifdef FEAT_MBYTE 1908 newlen = charlen; 1909 #else 1910 newlen = 1; 1911 #endif 1912 1913 if (State & REPLACE_FLAG) 1914 { 1915 #ifdef FEAT_VREPLACE 1916 if (State & VREPLACE_FLAG) 1917 { 1918 colnr_T new_vcol = 0; /* init for GCC */ 1919 colnr_T vcol; 1920 int old_list; 1921 #ifndef FEAT_MBYTE 1922 char_u buf[2]; 1923 #endif 1924 1925 /* 1926 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. 1927 * Returns the old value of list, so when finished, 1928 * curwin->w_p_list should be set back to this. 1929 */ 1930 old_list = curwin->w_p_list; 1931 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) 1932 curwin->w_p_list = FALSE; 1933 1934 /* 1935 * In virtual replace mode each character may replace one or more 1936 * characters (zero if it's a TAB). Count the number of bytes to 1937 * be deleted to make room for the new character, counting screen 1938 * cells. May result in adding spaces to fill a gap. 1939 */ 1940 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); 1941 #ifndef FEAT_MBYTE 1942 buf[0] = c; 1943 buf[1] = NUL; 1944 #endif 1945 new_vcol = vcol + chartabsize(buf, vcol); 1946 while (oldp[col + oldlen] != NUL && vcol < new_vcol) 1947 { 1948 vcol += chartabsize(oldp + col + oldlen, vcol); 1949 /* Don't need to remove a TAB that takes us to the right 1950 * position. */ 1951 if (vcol > new_vcol && oldp[col + oldlen] == TAB) 1952 break; 1953 #ifdef FEAT_MBYTE 1954 oldlen += (*mb_ptr2len_check)(oldp + col + oldlen); 1955 #else 1956 ++oldlen; 1957 #endif 1958 /* Deleted a bit too much, insert spaces. */ 1959 if (vcol > new_vcol) 1960 newlen += vcol - new_vcol; 1961 } 1962 curwin->w_p_list = old_list; 1963 } 1964 else 1965 #endif 1966 if (oldp[col] != NUL) 1967 { 1968 /* normal replace */ 1969 #ifdef FEAT_MBYTE 1970 oldlen = (*mb_ptr2len_check)(oldp + col); 1971 #else 1972 oldlen = 1; 1973 #endif 1974 } 1975 1976 1977 /* Push the replaced bytes onto the replace stack, so that they can be 1978 * put back when BS is used. The bytes of a multi-byte character are 1979 * done the other way around, so that the first byte is popped off 1980 * first (it tells the byte length of the character). */ 1981 replace_push(NUL); 1982 for (i = 0; i < oldlen; ++i) 1983 { 1984 #ifdef FEAT_MBYTE 1985 l = (*mb_ptr2len_check)(oldp + col + i) - 1; 1986 for (j = l; j >= 0; --j) 1987 replace_push(oldp[col + i + j]); 1988 i += l; 1989 #else 1990 replace_push(oldp[col + i]); 1991 #endif 1992 } 1993 } 1994 1995 newp = alloc_check((unsigned)(linelen + newlen - oldlen)); 1996 if (newp == NULL) 1997 return; 1998 1999 /* Copy bytes before the cursor. */ 2000 if (col > 0) 2001 mch_memmove(newp, oldp, (size_t)col); 2002 2003 /* Copy bytes after the changed character(s). */ 2004 p = newp + col; 2005 mch_memmove(p + newlen, oldp + col + oldlen, 2006 (size_t)(linelen - col - oldlen)); 2007 2008 /* Insert or overwrite the new character. */ 2009 #ifdef FEAT_MBYTE 2010 mch_memmove(p, buf, charlen); 2011 i = charlen; 2012 #else 2013 *p = c; 2014 i = 1; 2015 #endif 2016 2017 /* Fill with spaces when necessary. */ 2018 while (i < newlen) 2019 p[i++] = ' '; 2020 2021 /* Replace the line in the buffer. */ 2022 ml_replace(lnum, newp, FALSE); 2023 2024 /* mark the buffer as changed and prepare for displaying */ 2025 changed_bytes(lnum, col); 2026 2027 /* 2028 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly 2029 * show the match for right parens and braces. 2030 */ 2031 if (p_sm && (State & INSERT) 2032 && msg_silent == 0 2033 #ifdef FEAT_MBYTE 2034 && charlen == 1 2035 #endif 2036 ) 2037 showmatch(c); 2038 2039 #ifdef FEAT_RIGHTLEFT 2040 if (!p_ri || (State & REPLACE_FLAG)) 2041 #endif 2042 { 2043 /* Normal insert: move cursor right */ 2044 #ifdef FEAT_MBYTE 2045 curwin->w_cursor.col += charlen; 2046 #else 2047 ++curwin->w_cursor.col; 2048 #endif 2049 } 2050 /* 2051 * TODO: should try to update w_row here, to avoid recomputing it later. 2052 */ 2053 } 2054 2055 /* 2056 * Insert a string at the cursor position. 2057 * Note: Does NOT handle Replace mode. 2058 * Caller must have prepared for undo. 2059 */ 2060 void 2061 ins_str(s) 2062 char_u *s; 2063 { 2064 char_u *oldp, *newp; 2065 int newlen = (int)STRLEN(s); 2066 int oldlen; 2067 colnr_T col; 2068 linenr_T lnum = curwin->w_cursor.lnum; 2069 2070 #ifdef FEAT_VIRTUALEDIT 2071 if (virtual_active() && curwin->w_cursor.coladd > 0) 2072 coladvance_force(getviscol()); 2073 #endif 2074 2075 col = curwin->w_cursor.col; 2076 oldp = ml_get(lnum); 2077 oldlen = (int)STRLEN(oldp); 2078 2079 newp = alloc_check((unsigned)(oldlen + newlen + 1)); 2080 if (newp == NULL) 2081 return; 2082 if (col > 0) 2083 mch_memmove(newp, oldp, (size_t)col); 2084 mch_memmove(newp + col, s, (size_t)newlen); 2085 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); 2086 ml_replace(lnum, newp, FALSE); 2087 changed_bytes(lnum, col); 2088 curwin->w_cursor.col += newlen; 2089 } 2090 2091 /* 2092 * Delete one character under the cursor. 2093 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. 2094 * Caller must have prepared for undo. 2095 * 2096 * return FAIL for failure, OK otherwise 2097 */ 2098 int 2099 del_char(fixpos) 2100 int fixpos; 2101 { 2102 #ifdef FEAT_MBYTE 2103 if (has_mbyte) 2104 { 2105 /* Make sure the cursor is at the start of a character. */ 2106 mb_adjust_cursor(); 2107 if (*ml_get_cursor() == NUL) 2108 return FAIL; 2109 return del_chars(1L, fixpos); 2110 } 2111 #endif 2112 return del_bytes(1L, fixpos); 2113 } 2114 2115 #if defined(FEAT_MBYTE) || defined(PROTO) 2116 /* 2117 * Like del_bytes(), but delete characters instead of bytes. 2118 */ 2119 int 2120 del_chars(count, fixpos) 2121 long count; 2122 int fixpos; 2123 { 2124 long bytes = 0; 2125 long i; 2126 char_u *p; 2127 int l; 2128 2129 p = ml_get_cursor(); 2130 for (i = 0; i < count && *p != NUL; ++i) 2131 { 2132 l = (*mb_ptr2len_check)(p); 2133 bytes += l; 2134 p += l; 2135 } 2136 return del_bytes(bytes, fixpos); 2137 } 2138 #endif 2139 2140 /* 2141 * Delete "count" bytes under the cursor. 2142 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. 2143 * Caller must have prepared for undo. 2144 * 2145 * return FAIL for failure, OK otherwise 2146 */ 2147 int 2148 del_bytes(count, fixpos) 2149 long count; 2150 int fixpos; 2151 { 2152 char_u *oldp, *newp; 2153 colnr_T oldlen; 2154 linenr_T lnum = curwin->w_cursor.lnum; 2155 colnr_T col = curwin->w_cursor.col; 2156 int was_alloced; 2157 long movelen; 2158 2159 oldp = ml_get(lnum); 2160 oldlen = (int)STRLEN(oldp); 2161 2162 /* 2163 * Can't do anything when the cursor is on the NUL after the line. 2164 */ 2165 if (col >= oldlen) 2166 return FAIL; 2167 2168 #ifdef FEAT_MBYTE 2169 /* If 'delcombine' is set and deleting (less than) one character, only 2170 * delete the last combining character. */ 2171 if (p_deco && enc_utf8 && utfc_ptr2len_check(oldp + col) >= count) 2172 { 2173 int c1, c2; 2174 int n; 2175 2176 (void)utfc_ptr2char(oldp + col, &c1, &c2); 2177 if (c1 != NUL) 2178 { 2179 /* Find the last composing char, there can be several. */ 2180 n = col; 2181 do 2182 { 2183 col = n; 2184 count = utf_ptr2len_check(oldp + n); 2185 n += count; 2186 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); 2187 fixpos = 0; 2188 } 2189 } 2190 #endif 2191 2192 /* 2193 * When count is too big, reduce it. 2194 */ 2195 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ 2196 if (movelen <= 1) 2197 { 2198 /* 2199 * If we just took off the last character of a non-blank line, and 2200 * fixpos is TRUE, we don't want to end up positioned at the NUL. 2201 */ 2202 if (col > 0 && fixpos) 2203 { 2204 --curwin->w_cursor.col; 2205 #ifdef FEAT_VIRTUALEDIT 2206 curwin->w_cursor.coladd = 0; 2207 #endif 2208 #ifdef FEAT_MBYTE 2209 if (has_mbyte) 2210 curwin->w_cursor.col -= 2211 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); 2212 #endif 2213 } 2214 count = oldlen - col; 2215 movelen = 1; 2216 } 2217 2218 /* 2219 * If the old line has been allocated the deletion can be done in the 2220 * existing line. Otherwise a new line has to be allocated 2221 */ 2222 was_alloced = ml_line_alloced(); /* check if oldp was allocated */ 2223 #ifdef FEAT_NETBEANS_INTG 2224 if (was_alloced && usingNetbeans) 2225 netbeans_removed(curbuf, lnum, col, count); 2226 /* else is handled by ml_replace() */ 2227 #endif 2228 if (was_alloced) 2229 newp = oldp; /* use same allocated memory */ 2230 else 2231 { /* need to allocate a new line */ 2232 newp = alloc((unsigned)(oldlen + 1 - count)); 2233 if (newp == NULL) 2234 return FAIL; 2235 mch_memmove(newp, oldp, (size_t)col); 2236 } 2237 mch_memmove(newp + col, oldp + col + count, (size_t)movelen); 2238 if (!was_alloced) 2239 ml_replace(lnum, newp, FALSE); 2240 2241 /* mark the buffer as changed and prepare for displaying */ 2242 changed_bytes(lnum, curwin->w_cursor.col); 2243 2244 return OK; 2245 } 2246 2247 /* 2248 * Delete from cursor to end of line. 2249 * Caller must have prepared for undo. 2250 * 2251 * return FAIL for failure, OK otherwise 2252 */ 2253 int 2254 truncate_line(fixpos) 2255 int fixpos; /* if TRUE fix the cursor position when done */ 2256 { 2257 char_u *newp; 2258 linenr_T lnum = curwin->w_cursor.lnum; 2259 colnr_T col = curwin->w_cursor.col; 2260 2261 if (col == 0) 2262 newp = vim_strsave((char_u *)""); 2263 else 2264 newp = vim_strnsave(ml_get(lnum), col); 2265 2266 if (newp == NULL) 2267 return FAIL; 2268 2269 ml_replace(lnum, newp, FALSE); 2270 2271 /* mark the buffer as changed and prepare for displaying */ 2272 changed_bytes(lnum, curwin->w_cursor.col); 2273 2274 /* 2275 * If "fixpos" is TRUE we don't want to end up positioned at the NUL. 2276 */ 2277 if (fixpos && curwin->w_cursor.col > 0) 2278 --curwin->w_cursor.col; 2279 2280 return OK; 2281 } 2282 2283 /* 2284 * Delete "nlines" lines at the cursor. 2285 * Saves the lines for undo first if "undo" is TRUE. 2286 */ 2287 void 2288 del_lines(nlines, undo) 2289 long nlines; /* number of lines to delete */ 2290 int undo; /* if TRUE, prepare for undo */ 2291 { 2292 long n; 2293 2294 if (nlines <= 0) 2295 return; 2296 2297 /* save the deleted lines for undo */ 2298 if (undo && u_savedel(curwin->w_cursor.lnum, nlines) == FAIL) 2299 return; 2300 2301 for (n = 0; n < nlines; ) 2302 { 2303 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ 2304 break; 2305 2306 ml_delete(curwin->w_cursor.lnum, TRUE); 2307 ++n; 2308 2309 /* If we delete the last line in the file, stop */ 2310 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) 2311 break; 2312 } 2313 /* adjust marks, mark the buffer as changed and prepare for displaying */ 2314 deleted_lines_mark(curwin->w_cursor.lnum, n); 2315 2316 curwin->w_cursor.col = 0; 2317 check_cursor_lnum(); 2318 } 2319 2320 int 2321 gchar_pos(pos) 2322 pos_T *pos; 2323 { 2324 char_u *ptr = ml_get_pos(pos); 2325 2326 #ifdef FEAT_MBYTE 2327 if (has_mbyte) 2328 return (*mb_ptr2char)(ptr); 2329 #endif 2330 return (int)*ptr; 2331 } 2332 2333 int 2334 gchar_cursor() 2335 { 2336 #ifdef FEAT_MBYTE 2337 if (has_mbyte) 2338 return (*mb_ptr2char)(ml_get_cursor()); 2339 #endif 2340 return (int)*ml_get_cursor(); 2341 } 2342 2343 /* 2344 * Write a character at the current cursor position. 2345 * It is directly written into the block. 2346 */ 2347 void 2348 pchar_cursor(c) 2349 int c; 2350 { 2351 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) 2352 + curwin->w_cursor.col) = c; 2353 } 2354 2355 #if 0 /* not used */ 2356 /* 2357 * Put *pos at end of current buffer 2358 */ 2359 void 2360 goto_endofbuf(pos) 2361 pos_T *pos; 2362 { 2363 char_u *p; 2364 2365 pos->lnum = curbuf->b_ml.ml_line_count; 2366 pos->col = 0; 2367 p = ml_get(pos->lnum); 2368 while (*p++) 2369 ++pos->col; 2370 } 2371 #endif 2372 2373 /* 2374 * When extra == 0: Return TRUE if the cursor is before or on the first 2375 * non-blank in the line. 2376 * When extra == 1: Return TRUE if the cursor is before the first non-blank in 2377 * the line. 2378 */ 2379 int 2380 inindent(extra) 2381 int extra; 2382 { 2383 char_u *ptr; 2384 colnr_T col; 2385 2386 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col) 2387 ++ptr; 2388 if (col >= curwin->w_cursor.col + extra) 2389 return TRUE; 2390 else 2391 return FALSE; 2392 } 2393 2394 /* 2395 * Skip to next part of an option argument: Skip space and comma. 2396 */ 2397 char_u * 2398 skip_to_option_part(p) 2399 char_u *p; 2400 { 2401 if (*p == ',') 2402 ++p; 2403 while (*p == ' ') 2404 ++p; 2405 return p; 2406 } 2407 2408 /* 2409 * changed() is called when something in the current buffer is changed. 2410 * 2411 * Most often called through changed_bytes() and changed_lines(), which also 2412 * mark the area of the display to be redrawn. 2413 */ 2414 void 2415 changed() 2416 { 2417 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) 2418 /* The text of the preediting area is inserted, but this doesn't 2419 * mean a change of the buffer yet. That is delayed until the 2420 * text is committed. (this means preedit becomes empty) */ 2421 if (im_is_preediting() && !xim_changed_while_preediting) 2422 return; 2423 xim_changed_while_preediting = FALSE; 2424 #endif 2425 2426 if (!curbuf->b_changed) 2427 { 2428 int save_msg_scroll = msg_scroll; 2429 2430 change_warning(0); 2431 /* Create a swap file if that is wanted. 2432 * Don't do this for "nofile" and "nowrite" buffer types. */ 2433 if (curbuf->b_may_swap 2434 #ifdef FEAT_QUICKFIX 2435 && !bt_dontwrite(curbuf) 2436 #endif 2437 ) 2438 { 2439 ml_open_file(curbuf); 2440 2441 /* The ml_open_file() can cause an ATTENTION message. 2442 * Wait two seconds, to make sure the user reads this unexpected 2443 * message. Since we could be anywhere, call wait_return() now, 2444 * and don't let the emsg() set msg_scroll. */ 2445 if (need_wait_return && emsg_silent == 0) 2446 { 2447 out_flush(); 2448 ui_delay(2000L, TRUE); 2449 wait_return(TRUE); 2450 msg_scroll = save_msg_scroll; 2451 } 2452 } 2453 curbuf->b_changed = TRUE; 2454 ml_setdirty(curbuf, TRUE); 2455 #ifdef FEAT_WINDOWS 2456 check_status(curbuf); 2457 #endif 2458 #ifdef FEAT_TITLE 2459 need_maketitle = TRUE; /* set window title later */ 2460 #endif 2461 } 2462 ++curbuf->b_changedtick; 2463 ++global_changedtick; 2464 } 2465 2466 static void changedOneline __ARGS((linenr_T lnum)); 2467 static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra)); 2468 2469 /* 2470 * Changed bytes within a single line for the current buffer. 2471 * - marks the windows on this buffer to be redisplayed 2472 * - marks the buffer changed by calling changed() 2473 * - invalidates cached values 2474 */ 2475 void 2476 changed_bytes(lnum, col) 2477 linenr_T lnum; 2478 colnr_T col; 2479 { 2480 changedOneline(lnum); 2481 changed_common(lnum, col, lnum + 1, 0L); 2482 } 2483 2484 static void 2485 changedOneline(lnum) 2486 linenr_T lnum; 2487 { 2488 if (curbuf->b_mod_set) 2489 { 2490 /* find the maximum area that must be redisplayed */ 2491 if (lnum < curbuf->b_mod_top) 2492 curbuf->b_mod_top = lnum; 2493 else if (lnum >= curbuf->b_mod_bot) 2494 curbuf->b_mod_bot = lnum + 1; 2495 } 2496 else 2497 { 2498 /* set the area that must be redisplayed to one line */ 2499 curbuf->b_mod_set = TRUE; 2500 curbuf->b_mod_top = lnum; 2501 curbuf->b_mod_bot = lnum + 1; 2502 curbuf->b_mod_xlines = 0; 2503 } 2504 } 2505 2506 /* 2507 * Appended "count" lines below line "lnum" in the current buffer. 2508 * Must be called AFTER the change and after mark_adjust(). 2509 * Takes care of marking the buffer to be redrawn and sets the changed flag. 2510 */ 2511 void 2512 appended_lines(lnum, count) 2513 linenr_T lnum; 2514 long count; 2515 { 2516 changed_lines(lnum + 1, 0, lnum + 1, count); 2517 } 2518 2519 /* 2520 * Like appended_lines(), but adjust marks first. 2521 */ 2522 void 2523 appended_lines_mark(lnum, count) 2524 linenr_T lnum; 2525 long count; 2526 { 2527 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); 2528 changed_lines(lnum + 1, 0, lnum + 1, count); 2529 } 2530 2531 /* 2532 * Deleted "count" lines at line "lnum" in the current buffer. 2533 * Must be called AFTER the change and after mark_adjust(). 2534 * Takes care of marking the buffer to be redrawn and sets the changed flag. 2535 */ 2536 void 2537 deleted_lines(lnum, count) 2538 linenr_T lnum; 2539 long count; 2540 { 2541 changed_lines(lnum, 0, lnum + count, -count); 2542 } 2543 2544 /* 2545 * Like deleted_lines(), but adjust marks first. 2546 */ 2547 void 2548 deleted_lines_mark(lnum, count) 2549 linenr_T lnum; 2550 long count; 2551 { 2552 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); 2553 changed_lines(lnum, 0, lnum + count, -count); 2554 } 2555 2556 /* 2557 * Changed lines for the current buffer. 2558 * Must be called AFTER the change and after mark_adjust(). 2559 * - mark the buffer changed by calling changed() 2560 * - mark the windows on this buffer to be redisplayed 2561 * - invalidate cached values 2562 * "lnum" is the first line that needs displaying, "lnume" the first line 2563 * below the changed lines (BEFORE the change). 2564 * When only inserting lines, "lnum" and "lnume" are equal. 2565 * Takes care of calling changed() and updating b_mod_*. 2566 */ 2567 void 2568 changed_lines(lnum, col, lnume, xtra) 2569 linenr_T lnum; /* first line with change */ 2570 colnr_T col; /* column in first line with change */ 2571 linenr_T lnume; /* line below last changed line */ 2572 long xtra; /* number of extra lines (negative when deleting) */ 2573 { 2574 if (curbuf->b_mod_set) 2575 { 2576 /* find the maximum area that must be redisplayed */ 2577 if (lnum < curbuf->b_mod_top) 2578 curbuf->b_mod_top = lnum; 2579 if (lnum < curbuf->b_mod_bot) 2580 { 2581 /* adjust old bot position for xtra lines */ 2582 curbuf->b_mod_bot += xtra; 2583 if (curbuf->b_mod_bot < lnum) 2584 curbuf->b_mod_bot = lnum; 2585 } 2586 if (lnume + xtra > curbuf->b_mod_bot) 2587 curbuf->b_mod_bot = lnume + xtra; 2588 curbuf->b_mod_xlines += xtra; 2589 } 2590 else 2591 { 2592 /* set the area that must be redisplayed */ 2593 curbuf->b_mod_set = TRUE; 2594 curbuf->b_mod_top = lnum; 2595 curbuf->b_mod_bot = lnume + xtra; 2596 curbuf->b_mod_xlines = xtra; 2597 } 2598 2599 changed_common(lnum, col, lnume, xtra); 2600 } 2601 2602 static void 2603 changed_common(lnum, col, lnume, xtra) 2604 linenr_T lnum; 2605 colnr_T col; 2606 linenr_T lnume; 2607 long xtra; 2608 { 2609 win_T *wp; 2610 int i; 2611 #ifdef FEAT_JUMPLIST 2612 int cols; 2613 pos_T *p; 2614 int add; 2615 #endif 2616 2617 /* mark the buffer as modified */ 2618 changed(); 2619 2620 /* set the '. mark */ 2621 if (!cmdmod.keepjumps) 2622 { 2623 curbuf->b_last_change.lnum = lnum; 2624 curbuf->b_last_change.col = col; 2625 2626 #ifdef FEAT_JUMPLIST 2627 /* Create a new entry if a new undo-able change was started or we 2628 * don't have an entry yet. */ 2629 if (curbuf->b_new_change || curbuf->b_changelistlen == 0) 2630 { 2631 if (curbuf->b_changelistlen == 0) 2632 add = TRUE; 2633 else 2634 { 2635 /* Don't create a new entry when the line number is the same 2636 * as the last one and the column is not too far away. Avoids 2637 * creating many entries for typing "xxxxx". */ 2638 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; 2639 if (p->lnum != lnum) 2640 add = TRUE; 2641 else 2642 { 2643 cols = comp_textwidth(FALSE); 2644 if (cols == 0) 2645 cols = 79; 2646 add = (p->col + cols < col || col + cols < p->col); 2647 } 2648 } 2649 if (add) 2650 { 2651 /* This is the first of a new sequence of undo-able changes 2652 * and it's at some distance of the last change. Use a new 2653 * position in the changelist. */ 2654 curbuf->b_new_change = FALSE; 2655 2656 if (curbuf->b_changelistlen == JUMPLISTSIZE) 2657 { 2658 /* changelist is full: remove oldest entry */ 2659 curbuf->b_changelistlen = JUMPLISTSIZE - 1; 2660 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, 2661 sizeof(pos_T) * (JUMPLISTSIZE - 1)); 2662 FOR_ALL_WINDOWS(wp) 2663 { 2664 /* Correct position in changelist for other windows on 2665 * this buffer. */ 2666 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) 2667 --wp->w_changelistidx; 2668 } 2669 } 2670 FOR_ALL_WINDOWS(wp) 2671 { 2672 /* For other windows, if the position in the changelist is 2673 * at the end it stays at the end. */ 2674 if (wp->w_buffer == curbuf 2675 && wp->w_changelistidx == curbuf->b_changelistlen) 2676 ++wp->w_changelistidx; 2677 } 2678 ++curbuf->b_changelistlen; 2679 } 2680 } 2681 curbuf->b_changelist[curbuf->b_changelistlen - 1] = 2682 curbuf->b_last_change; 2683 /* The current window is always after the last change, so that "g," 2684 * takes you back to it. */ 2685 curwin->w_changelistidx = curbuf->b_changelistlen; 2686 #endif 2687 } 2688 2689 FOR_ALL_WINDOWS(wp) 2690 { 2691 if (wp->w_buffer == curbuf) 2692 { 2693 /* Mark this window to be redrawn later. */ 2694 if (wp->w_redr_type < VALID) 2695 wp->w_redr_type = VALID; 2696 2697 /* Check if a change in the buffer has invalidated the cached 2698 * values for the cursor. */ 2699 #ifdef FEAT_FOLDING 2700 /* 2701 * Update the folds for this window. Can't postpone this, because 2702 * a following operator might work on the whole fold: ">>dd". 2703 */ 2704 foldUpdate(wp, lnum, lnume + xtra - 1); 2705 2706 /* The change may cause lines above or below the change to become 2707 * included in a fold. Set lnum/lnume to the first/last line that 2708 * might be displayed differently. 2709 * Set w_cline_folded here as an efficient way to update it when 2710 * inserting lines just above a closed fold. */ 2711 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); 2712 if (wp->w_cursor.lnum == lnum) 2713 wp->w_cline_folded = i; 2714 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); 2715 if (wp->w_cursor.lnum == lnume) 2716 wp->w_cline_folded = i; 2717 2718 /* If the changed line is in a range of previously folded lines, 2719 * compare with the first line in that range. */ 2720 if (wp->w_cursor.lnum <= lnum) 2721 { 2722 i = find_wl_entry(wp, lnum); 2723 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) 2724 changed_line_abv_curs_win(wp); 2725 } 2726 #endif 2727 2728 if (wp->w_cursor.lnum > lnum) 2729 changed_line_abv_curs_win(wp); 2730 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) 2731 changed_cline_bef_curs_win(wp); 2732 if (wp->w_botline >= lnum) 2733 { 2734 /* Assume that botline doesn't change (inserted lines make 2735 * other lines scroll down below botline). */ 2736 approximate_botline_win(wp); 2737 } 2738 2739 /* Check if any w_lines[] entries have become invalid. 2740 * For entries below the change: Correct the lnums for 2741 * inserted/deleted lines. Makes it possible to stop displaying 2742 * after the change. */ 2743 for (i = 0; i < wp->w_lines_valid; ++i) 2744 if (wp->w_lines[i].wl_valid) 2745 { 2746 if (wp->w_lines[i].wl_lnum >= lnum) 2747 { 2748 if (wp->w_lines[i].wl_lnum < lnume) 2749 { 2750 /* line included in change */ 2751 wp->w_lines[i].wl_valid = FALSE; 2752 } 2753 else if (xtra != 0) 2754 { 2755 /* line below change */ 2756 wp->w_lines[i].wl_lnum += xtra; 2757 #ifdef FEAT_FOLDING 2758 wp->w_lines[i].wl_lastlnum += xtra; 2759 #endif 2760 } 2761 } 2762 #ifdef FEAT_FOLDING 2763 else if (wp->w_lines[i].wl_lastlnum >= lnum) 2764 { 2765 /* change somewhere inside this range of folded lines, 2766 * may need to be redrawn */ 2767 wp->w_lines[i].wl_valid = FALSE; 2768 } 2769 #endif 2770 } 2771 } 2772 } 2773 2774 /* Call update_screen() later, which checks out what needs to be redrawn, 2775 * since it notices b_mod_set and then uses b_mod_*. */ 2776 if (must_redraw < VALID) 2777 must_redraw = VALID; 2778 } 2779 2780 /* 2781 * unchanged() is called when the changed flag must be reset for buffer 'buf' 2782 */ 2783 void 2784 unchanged(buf, ff) 2785 buf_T *buf; 2786 int ff; /* also reset 'fileformat' */ 2787 { 2788 if (buf->b_changed || (ff && file_ff_differs(buf))) 2789 { 2790 buf->b_changed = 0; 2791 ml_setdirty(buf, FALSE); 2792 if (ff) 2793 save_file_ff(buf); 2794 #ifdef FEAT_WINDOWS 2795 check_status(buf); 2796 #endif 2797 #ifdef FEAT_TITLE 2798 need_maketitle = TRUE; /* set window title later */ 2799 #endif 2800 } 2801 ++buf->b_changedtick; 2802 ++global_changedtick; 2803 #ifdef FEAT_NETBEANS_INTG 2804 netbeans_unmodified(buf); 2805 #endif 2806 } 2807 2808 #if defined(FEAT_WINDOWS) || defined(PROTO) 2809 /* 2810 * check_status: called when the status bars for the buffer 'buf' 2811 * need to be updated 2812 */ 2813 void 2814 check_status(buf) 2815 buf_T *buf; 2816 { 2817 win_T *wp; 2818 2819 for (wp = firstwin; wp != NULL; wp = wp->w_next) 2820 if (wp->w_buffer == buf && wp->w_status_height) 2821 { 2822 wp->w_redr_status = TRUE; 2823 if (must_redraw < VALID) 2824 must_redraw = VALID; 2825 } 2826 } 2827 #endif 2828 2829 /* 2830 * If the file is readonly, give a warning message with the first change. 2831 * Don't do this for autocommands. 2832 * Don't use emsg(), because it flushes the macro buffer. 2833 * If we have undone all changes b_changed will be FALSE, but b_did_warn 2834 * will be TRUE. 2835 */ 2836 void 2837 change_warning(col) 2838 int col; /* column for message; non-zero when in insert 2839 mode and 'showmode' is on */ 2840 { 2841 if (curbuf->b_did_warn == FALSE 2842 && curbufIsChanged() == 0 2843 #ifdef FEAT_AUTOCMD 2844 && !autocmd_busy 2845 #endif 2846 && curbuf->b_p_ro) 2847 { 2848 #ifdef FEAT_AUTOCMD 2849 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); 2850 if (!curbuf->b_p_ro) 2851 return; 2852 #endif 2853 /* 2854 * Do what msg() does, but with a column offset if the warning should 2855 * be after the mode message. 2856 */ 2857 msg_start(); 2858 if (msg_row == Rows - 1) 2859 msg_col = col; 2860 msg_source(hl_attr(HLF_W)); 2861 MSG_PUTS_ATTR(_("W10: Warning: Changing a readonly file"), 2862 hl_attr(HLF_W) | MSG_HIST); 2863 msg_clr_eos(); 2864 (void)msg_end(); 2865 if (msg_silent == 0 && !silent_mode) 2866 { 2867 out_flush(); 2868 ui_delay(1000L, TRUE); /* give the user time to think about it */ 2869 } 2870 curbuf->b_did_warn = TRUE; 2871 redraw_cmdline = FALSE; /* don't redraw and erase the message */ 2872 if (msg_row < Rows - 1) 2873 showmode(); 2874 } 2875 } 2876 2877 /* 2878 * Ask for a reply from the user, a 'y' or a 'n'. 2879 * No other characters are accepted, the message is repeated until a valid 2880 * reply is entered or CTRL-C is hit. 2881 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters 2882 * from any buffers but directly from the user. 2883 * 2884 * return the 'y' or 'n' 2885 */ 2886 int 2887 ask_yesno(str, direct) 2888 char_u *str; 2889 int direct; 2890 { 2891 int r = ' '; 2892 int save_State = State; 2893 2894 if (exiting) /* put terminal in raw mode for this question */ 2895 settmode(TMODE_RAW); 2896 ++no_wait_return; 2897 #ifdef USE_ON_FLY_SCROLL 2898 dont_scroll = TRUE; /* disallow scrolling here */ 2899 #endif 2900 State = CONFIRM; /* mouse behaves like with :confirm */ 2901 #ifdef FEAT_MOUSE 2902 setmouse(); /* disables mouse for xterm */ 2903 #endif 2904 ++no_mapping; 2905 ++allow_keys; /* no mapping here, but recognize keys */ 2906 2907 while (r != 'y' && r != 'n') 2908 { 2909 /* same highlighting as for wait_return */ 2910 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str); 2911 if (direct) 2912 r = get_keystroke(); 2913 else 2914 r = safe_vgetc(); 2915 if (r == Ctrl_C || r == ESC) 2916 r = 'n'; 2917 msg_putchar(r); /* show what you typed */ 2918 out_flush(); 2919 } 2920 --no_wait_return; 2921 State = save_State; 2922 #ifdef FEAT_MOUSE 2923 setmouse(); 2924 #endif 2925 --no_mapping; 2926 --allow_keys; 2927 2928 return r; 2929 } 2930 2931 /* 2932 * Get a key stroke directly from the user. 2933 * Ignores mouse clicks and scrollbar events, except a click for the left 2934 * button (used at the more prompt). 2935 * Doesn't use vgetc(), because it syncs undo and eats mapped characters. 2936 * Disadvantage: typeahead is ignored. 2937 * Translates the interrupt character for unix to ESC. 2938 */ 2939 int 2940 get_keystroke() 2941 { 2942 #define CBUFLEN 151 2943 char_u buf[CBUFLEN]; 2944 int len = 0; 2945 int n; 2946 int save_mapped_ctrl_c = mapped_ctrl_c; 2947 2948 mapped_ctrl_c = FALSE; /* mappings are not used here */ 2949 for (;;) 2950 { 2951 cursor_on(); 2952 out_flush(); 2953 2954 /* First time: blocking wait. Second time: wait up to 100ms for a 2955 * terminal code to complete. Leave some room for check_termcode() to 2956 * insert a key code into (max 5 chars plus NUL). And 2957 * fix_input_buffer() can triple the number of bytes. */ 2958 n = ui_inchar(buf + len, (CBUFLEN - 6 - len) / 3, 2959 len == 0 ? -1L : 100L, 0); 2960 if (n > 0) 2961 { 2962 /* Replace zero and CSI by a special key code. */ 2963 n = fix_input_buffer(buf + len, n, FALSE); 2964 len += n; 2965 } 2966 2967 /* incomplete termcode: get more characters */ 2968 if ((n = check_termcode(1, buf, len)) < 0) 2969 continue; 2970 /* found a termcode: adjust length */ 2971 if (n > 0) 2972 len = n; 2973 if (len == 0) /* nothing typed yet */ 2974 continue; 2975 2976 /* Handle modifier and/or special key code. */ 2977 n = buf[0]; 2978 if (n == K_SPECIAL) 2979 { 2980 n = TO_SPECIAL(buf[1], buf[2]); 2981 if (buf[1] == KS_MODIFIER 2982 || n == K_IGNORE 2983 #ifdef FEAT_MOUSE 2984 || n == K_LEFTMOUSE_NM 2985 || n == K_LEFTDRAG 2986 || n == K_LEFTRELEASE 2987 || n == K_LEFTRELEASE_NM 2988 || n == K_MIDDLEMOUSE 2989 || n == K_MIDDLEDRAG 2990 || n == K_MIDDLERELEASE 2991 || n == K_RIGHTMOUSE 2992 || n == K_RIGHTDRAG 2993 || n == K_RIGHTRELEASE 2994 || n == K_MOUSEDOWN 2995 || n == K_MOUSEUP 2996 || n == K_X1MOUSE 2997 || n == K_X1DRAG 2998 || n == K_X1RELEASE 2999 || n == K_X2MOUSE 3000 || n == K_X2DRAG 3001 || n == K_X2RELEASE 3002 # ifdef FEAT_GUI 3003 || n == K_VER_SCROLLBAR 3004 || n == K_HOR_SCROLLBAR 3005 # endif 3006 #endif 3007 ) 3008 { 3009 if (buf[1] == KS_MODIFIER) 3010 mod_mask = buf[2]; 3011 len -= 3; 3012 if (len > 0) 3013 mch_memmove(buf, buf + 3, (size_t)len); 3014 continue; 3015 } 3016 } 3017 #ifdef FEAT_MBYTE 3018 if (has_mbyte) 3019 { 3020 if (MB_BYTE2LEN(n) > len) 3021 continue; /* more bytes to get */ 3022 buf[len >= CBUFLEN ? CBUFLEN - 1 : len] = NUL; 3023 n = (*mb_ptr2char)(buf); 3024 } 3025 #endif 3026 #ifdef UNIX 3027 if (n == intr_char) 3028 n = ESC; 3029 #endif 3030 break; 3031 } 3032 3033 mapped_ctrl_c = save_mapped_ctrl_c; 3034 return n; 3035 } 3036 3037 /* 3038 * get a number from the user 3039 */ 3040 int 3041 get_number(colon) 3042 int colon; /* allow colon to abort */ 3043 { 3044 int n = 0; 3045 int c; 3046 3047 /* When not printing messages, the user won't know what to type, return a 3048 * zero (as if CR was hit). */ 3049 if (msg_silent != 0) 3050 return 0; 3051 3052 #ifdef USE_ON_FLY_SCROLL 3053 dont_scroll = TRUE; /* disallow scrolling here */ 3054 #endif 3055 ++no_mapping; 3056 ++allow_keys; /* no mapping here, but recognize keys */ 3057 for (;;) 3058 { 3059 windgoto(msg_row, msg_col); 3060 c = safe_vgetc(); 3061 if (VIM_ISDIGIT(c)) 3062 { 3063 n = n * 10 + c - '0'; 3064 msg_putchar(c); 3065 } 3066 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) 3067 { 3068 n /= 10; 3069 MSG_PUTS("\b \b"); 3070 } 3071 else if (n == 0 && c == ':' && colon) 3072 { 3073 stuffcharReadbuff(':'); 3074 if (!exmode_active) 3075 cmdline_row = msg_row; 3076 skip_redraw = TRUE; /* skip redraw once */ 3077 do_redraw = FALSE; 3078 break; 3079 } 3080 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) 3081 break; 3082 } 3083 --no_mapping; 3084 --allow_keys; 3085 return n; 3086 } 3087 3088 void 3089 msgmore(n) 3090 long n; 3091 { 3092 long pn; 3093 3094 if (global_busy /* no messages now, wait until global is finished */ 3095 || keep_msg != NULL /* there is a message already, skip this one */ 3096 || !messaging()) /* 'lazyredraw' set, don't do messages now */ 3097 return; 3098 3099 if (n > 0) 3100 pn = n; 3101 else 3102 pn = -n; 3103 3104 if (pn > p_report) 3105 { 3106 if (pn == 1) 3107 { 3108 if (n > 0) 3109 STRCPY(msg_buf, _("1 more line")); 3110 else 3111 STRCPY(msg_buf, _("1 line less")); 3112 } 3113 else 3114 { 3115 if (n > 0) 3116 sprintf((char *)msg_buf, _("%ld more lines"), pn); 3117 else 3118 sprintf((char *)msg_buf, _("%ld fewer lines"), pn); 3119 } 3120 if (got_int) 3121 STRCAT(msg_buf, _(" (Interrupted)")); 3122 if (msg(msg_buf)) 3123 { 3124 set_keep_msg(msg_buf); 3125 keep_msg_attr = 0; 3126 } 3127 } 3128 } 3129 3130 /* 3131 * flush map and typeahead buffers and give a warning for an error 3132 */ 3133 void 3134 beep_flush() 3135 { 3136 if (emsg_silent == 0) 3137 { 3138 flush_buffers(FALSE); 3139 vim_beep(); 3140 } 3141 } 3142 3143 /* 3144 * give a warning for an error 3145 */ 3146 void 3147 vim_beep() 3148 { 3149 if (emsg_silent == 0) 3150 { 3151 if (p_vb 3152 #ifdef FEAT_GUI 3153 /* While the GUI is starting up the termcap is set for the GUI 3154 * but the output still goes to a terminal. */ 3155 && !(gui.in_use && gui.starting) 3156 #endif 3157 ) 3158 { 3159 out_str(T_VB); 3160 } 3161 else 3162 { 3163 #ifdef MSDOS 3164 /* 3165 * The number of beeps outputted is reduced to avoid having to wait 3166 * for all the beeps to finish. This is only a problem on systems 3167 * where the beeps don't overlap. 3168 */ 3169 if (beep_count == 0 || beep_count == 10) 3170 { 3171 out_char(BELL); 3172 beep_count = 1; 3173 } 3174 else 3175 ++beep_count; 3176 #else 3177 out_char(BELL); 3178 #endif 3179 } 3180 } 3181 } 3182 3183 /* 3184 * To get the "real" home directory: 3185 * - get value of $HOME 3186 * For Unix: 3187 * - go to that directory 3188 * - do mch_dirname() to get the real name of that directory. 3189 * This also works with mounts and links. 3190 * Don't do this for MS-DOS, it will change the "current dir" for a drive. 3191 */ 3192 static char_u *homedir = NULL; 3193 3194 void 3195 init_homedir() 3196 { 3197 char_u *var; 3198 3199 #ifdef VMS 3200 var = mch_getenv((char_u *)"SYS$LOGIN"); 3201 #else 3202 var = mch_getenv((char_u *)"HOME"); 3203 #endif 3204 3205 if (var != NULL && *var == NUL) /* empty is same as not set */ 3206 var = NULL; 3207 3208 #ifdef WIN3264 3209 /* 3210 * Weird but true: $HOME may contain an indirect reference to another 3211 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set 3212 * when $HOME is being set. 3213 */ 3214 if (var != NULL && *var == '%') 3215 { 3216 char_u *p; 3217 char_u *exp; 3218 3219 p = vim_strchr(var + 1, '%'); 3220 if (p != NULL) 3221 { 3222 STRNCPY(NameBuff, var + 1, p - (var + 1)); 3223 NameBuff[p - (var + 1)] = NUL; 3224 exp = mch_getenv(NameBuff); 3225 if (exp != NULL && *exp != NUL 3226 && STRLEN(exp) + STRLEN(p) < MAXPATHL) 3227 { 3228 sprintf((char *)NameBuff, "%s%s", exp, p + 1); 3229 var = NameBuff; 3230 /* Also set $HOME, it's needed for _viminfo. */ 3231 vim_setenv((char_u *)"HOME", NameBuff); 3232 } 3233 } 3234 } 3235 3236 /* 3237 * Typically, $HOME is not defined on Windows, unless the user has 3238 * specifically defined it for Vim's sake. However, on Windows NT 3239 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for 3240 * each user. Try constructing $HOME from these. 3241 */ 3242 if (var == NULL) 3243 { 3244 char_u *homedrive, *homepath; 3245 3246 homedrive = mch_getenv((char_u *)"HOMEDRIVE"); 3247 homepath = mch_getenv((char_u *)"HOMEPATH"); 3248 if (homedrive != NULL && homepath != NULL 3249 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) 3250 { 3251 sprintf((char *)NameBuff, "%s%s", homedrive, homepath); 3252 if (NameBuff[0] != NUL) 3253 { 3254 var = NameBuff; 3255 /* Also set $HOME, it's needed for _viminfo. */ 3256 vim_setenv((char_u *)"HOME", NameBuff); 3257 } 3258 } 3259 } 3260 #endif 3261 3262 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) 3263 /* 3264 * Default home dir is C:/ 3265 * Best assumption we can make in such a situation. 3266 */ 3267 if (var == NULL) 3268 var = "C:/"; 3269 #endif 3270 if (var != NULL) 3271 { 3272 #ifdef UNIX 3273 /* 3274 * Change to the directory and get the actual path. This resolves 3275 * links. Don't do it when we can't return. 3276 */ 3277 if (mch_dirname(NameBuff, MAXPATHL) == OK 3278 && mch_chdir((char *)NameBuff) == 0) 3279 { 3280 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) 3281 var = IObuff; 3282 if (mch_chdir((char *)NameBuff) != 0) 3283 EMSG(_(e_prev_dir)); 3284 } 3285 #endif 3286 homedir = vim_strsave(var); 3287 } 3288 } 3289 3290 /* 3291 * Expand environment variable with path name. 3292 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. 3293 * Skips over "\ ", "\~" and "\$". 3294 * If anything fails no expansion is done and dst equals src. 3295 */ 3296 void 3297 expand_env(src, dst, dstlen) 3298 char_u *src; /* input string e.g. "$HOME/vim.hlp" */ 3299 char_u *dst; /* where to put the result */ 3300 int dstlen; /* maximum length of the result */ 3301 { 3302 expand_env_esc(src, dst, dstlen, FALSE); 3303 } 3304 3305 void 3306 expand_env_esc(src, dst, dstlen, esc) 3307 char_u *src; /* input string e.g. "$HOME/vim.hlp" */ 3308 char_u *dst; /* where to put the result */ 3309 int dstlen; /* maximum length of the result */ 3310 int esc; /* escape spaces in expanded variables */ 3311 { 3312 char_u *tail; 3313 int c; 3314 char_u *var; 3315 int copy_char; 3316 int mustfree; /* var was allocated, need to free it later */ 3317 int at_start = TRUE; /* at start of a name */ 3318 3319 src = skipwhite(src); 3320 --dstlen; /* leave one char space for "\," */ 3321 while (*src && dstlen > 0) 3322 { 3323 copy_char = TRUE; 3324 if ((*src == '$' 3325 #ifdef VMS 3326 && at_start 3327 #endif 3328 ) 3329 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) 3330 || *src == '%' 3331 #endif 3332 || (*src == '~' && at_start)) 3333 { 3334 mustfree = FALSE; 3335 3336 /* 3337 * The variable name is copied into dst temporarily, because it may 3338 * be a string in read-only memory and a NUL needs to be appended. 3339 */ 3340 if (*src != '~') /* environment var */ 3341 { 3342 tail = src + 1; 3343 var = dst; 3344 c = dstlen - 1; 3345 3346 #ifdef UNIX 3347 /* Unix has ${var-name} type environment vars */ 3348 if (*tail == '{' && !vim_isIDc('{')) 3349 { 3350 tail++; /* ignore '{' */ 3351 while (c-- > 0 && *tail && *tail != '}') 3352 *var++ = *tail++; 3353 } 3354 else 3355 #endif 3356 { 3357 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) 3358 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) 3359 || (*src == '%' && *tail != '%') 3360 #endif 3361 )) 3362 { 3363 #ifdef OS2 /* env vars only in uppercase */ 3364 *var++ = TOUPPER_LOC(*tail); 3365 tail++; /* toupper() may be a macro! */ 3366 #else 3367 *var++ = *tail++; 3368 #endif 3369 } 3370 } 3371 3372 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) 3373 # ifdef UNIX 3374 if (src[1] == '{' && *tail != '}') 3375 # else 3376 if (*src == '%' && *tail != '%') 3377 # endif 3378 var = NULL; 3379 else 3380 { 3381 # ifdef UNIX 3382 if (src[1] == '{') 3383 # else 3384 if (*src == '%') 3385 #endif 3386 ++tail; 3387 #endif 3388 *var = NUL; 3389 var = vim_getenv(dst, &mustfree); 3390 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) 3391 } 3392 #endif 3393 } 3394 /* home directory */ 3395 else if ( src[1] == NUL 3396 || vim_ispathsep(src[1]) 3397 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) 3398 { 3399 var = homedir; 3400 tail = src + 1; 3401 } 3402 else /* user directory */ 3403 { 3404 #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) 3405 /* 3406 * Copy ~user to dst[], so we can put a NUL after it. 3407 */ 3408 tail = src; 3409 var = dst; 3410 c = dstlen - 1; 3411 while ( c-- > 0 3412 && *tail 3413 && vim_isfilec(*tail) 3414 && !vim_ispathsep(*tail)) 3415 *var++ = *tail++; 3416 *var = NUL; 3417 # ifdef UNIX 3418 /* 3419 * If the system supports getpwnam(), use it. 3420 * Otherwise, or if getpwnam() fails, the shell is used to 3421 * expand ~user. This is slower and may fail if the shell 3422 * does not support ~user (old versions of /bin/sh). 3423 */ 3424 # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) 3425 { 3426 struct passwd *pw; 3427 3428 pw = getpwnam((char *)dst + 1); 3429 if (pw != NULL) 3430 var = (char_u *)pw->pw_dir; 3431 else 3432 var = NULL; 3433 } 3434 if (var == NULL) 3435 # endif 3436 { 3437 expand_T xpc; 3438 3439 ExpandInit(&xpc); 3440 xpc.xp_context = EXPAND_FILES; 3441 var = ExpandOne(&xpc, dst, NULL, 3442 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); 3443 ExpandCleanup(&xpc); 3444 mustfree = TRUE; 3445 } 3446 3447 # else /* !UNIX, thus VMS */ 3448 /* 3449 * USER_HOME is a comma-separated list of 3450 * directories to search for the user account in. 3451 */ 3452 { 3453 char_u test[MAXPATHL], paths[MAXPATHL]; 3454 char_u *path, *next_path, *ptr; 3455 struct stat st; 3456 3457 STRCPY(paths, USER_HOME); 3458 next_path = paths; 3459 while (*next_path) 3460 { 3461 for (path = next_path; *next_path && *next_path != ','; 3462 next_path++); 3463 if (*next_path) 3464 *next_path++ = NUL; 3465 STRCPY(test, path); 3466 STRCAT(test, "/"); 3467 STRCAT(test, dst + 1); 3468 if (mch_stat(test, &st) == 0) 3469 { 3470 var = alloc(STRLEN(test) + 1); 3471 STRCPY(var, test); 3472 mustfree = TRUE; 3473 break; 3474 } 3475 } 3476 } 3477 # endif /* UNIX */ 3478 #else 3479 /* cannot expand user's home directory, so don't try */ 3480 var = NULL; 3481 tail = (char_u *)""; /* for gcc */ 3482 #endif /* UNIX || VMS */ 3483 } 3484 3485 #ifdef BACKSLASH_IN_FILENAME 3486 /* If 'shellslash' is set change backslashes to forward slashes. 3487 * Can't use slash_adjust(), p_ssl may be set temporarily. */ 3488 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) 3489 { 3490 char_u *p = vim_strsave(var); 3491 3492 if (p != NULL) 3493 { 3494 if (mustfree) 3495 vim_free(var); 3496 var = p; 3497 mustfree = TRUE; 3498 forward_slash(var); 3499 } 3500 } 3501 #endif 3502 3503 /* If "var" contains white space, escape it with a backslash. 3504 * Required for ":e ~/tt" when $HOME includes a space. */ 3505 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) 3506 { 3507 char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); 3508 3509 if (p != NULL) 3510 { 3511 if (mustfree) 3512 vim_free(var); 3513 var = p; 3514 mustfree = TRUE; 3515 } 3516 } 3517 3518 if (var != NULL && *var != NUL 3519 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) 3520 { 3521 STRCPY(dst, var); 3522 dstlen -= (int)STRLEN(var); 3523 dst += STRLEN(var); 3524 /* if var[] ends in a path separator and tail[] starts 3525 * with it, skip a character */ 3526 if (*var != NUL && vim_ispathsep(dst[-1]) 3527 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) 3528 && dst[-1] != ':' 3529 #endif 3530 && vim_ispathsep(*tail)) 3531 ++tail; 3532 src = tail; 3533 copy_char = FALSE; 3534 } 3535 if (mustfree) 3536 vim_free(var); 3537 } 3538 3539 if (copy_char) /* copy at least one char */ 3540 { 3541 /* 3542 * Recogize the start of a new name, for '~'. 3543 */ 3544 at_start = FALSE; 3545 if (src[0] == '\\' && src[1] != NUL) 3546 { 3547 *dst++ = *src++; 3548 --dstlen; 3549 } 3550 else if (src[0] == ' ' || src[0] == ',') 3551 at_start = TRUE; 3552 *dst++ = *src++; 3553 --dstlen; 3554 } 3555 } 3556 *dst = NUL; 3557 } 3558 3559 /* 3560 * Vim's version of getenv(). 3561 * Special handling of $HOME, $VIM and $VIMRUNTIME. 3562 */ 3563 char_u * 3564 vim_getenv(name, mustfree) 3565 char_u *name; 3566 int *mustfree; /* set to TRUE when returned is allocated */ 3567 { 3568 char_u *p; 3569 char_u *pend; 3570 int vimruntime; 3571 3572 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) 3573 /* use "C:/" when $HOME is not set */ 3574 if (STRCMP(name, "HOME") == 0) 3575 return homedir; 3576 #endif 3577 3578 p = mch_getenv(name); 3579 if (p != NULL && *p == NUL) /* empty is the same as not set */ 3580 p = NULL; 3581 3582 if (p != NULL) 3583 return p; 3584 3585 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); 3586 if (!vimruntime && STRCMP(name, "VIM") != 0) 3587 return NULL; 3588 3589 /* 3590 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. 3591 * Don't do this when default_vimruntime_dir is non-empty. 3592 */ 3593 if (vimruntime 3594 #ifdef HAVE_PATHDEF 3595 && *default_vimruntime_dir == NUL 3596 #endif 3597 ) 3598 { 3599 p = mch_getenv((char_u *)"VIM"); 3600 if (p != NULL && *p == NUL) /* empty is the same as not set */ 3601 p = NULL; 3602 if (p != NULL) 3603 { 3604 p = vim_version_dir(p); 3605 if (p != NULL) 3606 *mustfree = TRUE; 3607 else 3608 p = mch_getenv((char_u *)"VIM"); 3609 } 3610 } 3611 3612 /* 3613 * When expanding $VIM or $VIMRUNTIME fails, try using: 3614 * - the directory name from 'helpfile' (unless it contains '$') 3615 * - the executable name from argv[0] 3616 */ 3617 if (p == NULL) 3618 { 3619 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) 3620 p = p_hf; 3621 #ifdef USE_EXE_NAME 3622 /* 3623 * Use the name of the executable, obtained from argv[0]. 3624 */ 3625 else 3626 p = exe_name; 3627 #endif 3628 if (p != NULL) 3629 { 3630 /* remove the file name */ 3631 pend = gettail(p); 3632 3633 /* remove "doc/" from 'helpfile', if present */ 3634 if (p == p_hf) 3635 pend = remove_tail(p, pend, (char_u *)"doc"); 3636 3637 #ifdef USE_EXE_NAME 3638 # ifdef MACOS_X 3639 /* remove "build/..." from exe_name, if present */ 3640 if (p == exe_name) 3641 { 3642 char_u *pend1; 3643 char_u *pend2; 3644 3645 pend1 = remove_tail(p, pend, (char_u *)"Contents/MacOS"); 3646 pend2 = remove_tail_with_ext(p, pend1, (char_u *)".app"); 3647 pend = remove_tail(p, pend2, (char_u *)"build"); 3648 /* When runnig from project builder get rid of the 3649 * build/???.app, otherwise keep the ???.app */ 3650 if (pend2 == pend) 3651 pend = pend1; 3652 } 3653 # endif 3654 /* remove "src/" from exe_name, if present */ 3655 if (p == exe_name) 3656 pend = remove_tail(p, pend, (char_u *)"src"); 3657 #endif 3658 3659 /* for $VIM, remove "runtime/" or "vim54/", if present */ 3660 if (!vimruntime) 3661 { 3662 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); 3663 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); 3664 } 3665 3666 /* remove trailing path separator */ 3667 #ifndef MACOS_CLASSIC 3668 /* With MacOS path (with colons) the final colon is required */ 3669 /* to avoid confusion between absoulute and relative path */ 3670 if (pend > p && vim_ispathsep(*(pend - 1))) 3671 --pend; 3672 #endif 3673 3674 /* check that the result is a directory name */ 3675 p = vim_strnsave(p, (int)(pend - p)); 3676 3677 if (p != NULL && !mch_isdir(p)) 3678 { 3679 vim_free(p); 3680 p = NULL; 3681 } 3682 else 3683 { 3684 #ifdef USE_EXE_NAME 3685 /* may add "/vim54" or "/runtime" if it exists */ 3686 if (vimruntime && (pend = vim_version_dir(p)) != NULL) 3687 { 3688 vim_free(p); 3689 p = pend; 3690 } 3691 #endif 3692 *mustfree = TRUE; 3693 } 3694 } 3695 } 3696 3697 #ifdef HAVE_PATHDEF 3698 /* When there is a pathdef.c file we can use default_vim_dir and 3699 * default_vimruntime_dir */ 3700 if (p == NULL) 3701 { 3702 /* Only use default_vimruntime_dir when it is not empty */ 3703 if (vimruntime && *default_vimruntime_dir != NUL) 3704 { 3705 p = default_vimruntime_dir; 3706 *mustfree = FALSE; 3707 } 3708 else if (*default_vim_dir != NUL) 3709 { 3710 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) 3711 *mustfree = TRUE; 3712 else 3713 { 3714 p = default_vim_dir; 3715 *mustfree = FALSE; 3716 } 3717 } 3718 } 3719 #endif 3720 3721 /* 3722 * Set the environment variable, so that the new value can be found fast 3723 * next time, and others can also use it (e.g. Perl). 3724 */ 3725 if (p != NULL) 3726 { 3727 if (vimruntime) 3728 { 3729 vim_setenv((char_u *)"VIMRUNTIME", p); 3730 didset_vimruntime = TRUE; 3731 #ifdef FEAT_GETTEXT 3732 { 3733 char_u *buf = alloc((unsigned int)STRLEN(p) + 6); 3734 3735 if (buf != NULL) 3736 { 3737 STRCPY(buf, p); 3738 STRCAT(buf, "/lang"); 3739 bindtextdomain(VIMPACKAGE, (char *)buf); 3740 vim_free(buf); 3741 } 3742 } 3743 #endif 3744 } 3745 else 3746 { 3747 vim_setenv((char_u *)"VIM", p); 3748 didset_vim = TRUE; 3749 } 3750 } 3751 return p; 3752 } 3753 3754 /* 3755 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. 3756 * Return NULL if not, return its name in allocated memory otherwise. 3757 */ 3758 static char_u * 3759 vim_version_dir(vimdir) 3760 char_u *vimdir; 3761 { 3762 char_u *p; 3763 3764 if (vimdir == NULL || *vimdir == NUL) 3765 return NULL; 3766 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); 3767 if (p != NULL && mch_isdir(p)) 3768 return p; 3769 vim_free(p); 3770 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); 3771 if (p != NULL && mch_isdir(p)) 3772 return p; 3773 vim_free(p); 3774 return NULL; 3775 } 3776 3777 /* 3778 * If the string between "p" and "pend" ends in "name/", return "pend" minus 3779 * the length of "name/". Otherwise return "pend". 3780 */ 3781 static char_u * 3782 remove_tail(p, pend, name) 3783 char_u *p; 3784 char_u *pend; 3785 char_u *name; 3786 { 3787 int len = (int)STRLEN(name) + 1; 3788 char_u *newend = pend - len; 3789 3790 if (newend >= p 3791 && fnamencmp(newend, name, len - 1) == 0 3792 && (newend == p || vim_ispathsep(*(newend - 1)))) 3793 return newend; 3794 return pend; 3795 } 3796 3797 #if defined(USE_EXE_NAME) && defined(MACOS_X) 3798 /* 3799 * If the string between "p" and "pend" ends in "???.ext/", return "pend" 3800 * minus the length of "???.ext/". Otherwise return "pend". 3801 */ 3802 static char_u * 3803 remove_tail_with_ext(p, pend, ext) 3804 char_u *p; 3805 char_u *pend; 3806 char_u *ext; 3807 { 3808 int len = (int)STRLEN(ext) + 1; 3809 char_u *newend = pend - len; 3810 3811 if (newend >= p && fnamencmp(newend, ext, len - 1) == 0) 3812 while (newend != p && !vim_ispathsep(*(newend - 1))) 3813 --newend; 3814 if (newend == p || vim_ispathsep(*(newend - 1))) 3815 return newend; 3816 return pend; 3817 } 3818 #endif 3819 3820 /* 3821 * Call expand_env() and store the result in an allocated string. 3822 * This is not very memory efficient, this expects the result to be freed 3823 * again soon. 3824 */ 3825 char_u * 3826 expand_env_save(src) 3827 char_u *src; 3828 { 3829 char_u *p; 3830 3831 p = alloc(MAXPATHL); 3832 if (p != NULL) 3833 expand_env(src, p, MAXPATHL); 3834 return p; 3835 } 3836 3837 /* 3838 * Our portable version of setenv. 3839 */ 3840 void 3841 vim_setenv(name, val) 3842 char_u *name; 3843 char_u *val; 3844 { 3845 #ifdef HAVE_SETENV 3846 mch_setenv((char *)name, (char *)val, 1); 3847 #else 3848 char_u *envbuf; 3849 3850 /* 3851 * Putenv does not copy the string, it has to remain 3852 * valid. The allocated memory will never be freed. 3853 */ 3854 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); 3855 if (envbuf != NULL) 3856 { 3857 sprintf((char *)envbuf, "%s=%s", name, val); 3858 putenv((char *)envbuf); 3859 } 3860 #endif 3861 } 3862 3863 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 3864 /* 3865 * Function given to ExpandGeneric() to obtain an environment variable name. 3866 */ 3867 /*ARGSUSED*/ 3868 char_u * 3869 get_env_name(xp, idx) 3870 expand_T *xp; 3871 int idx; 3872 { 3873 # if defined(AMIGA) || defined(__MRC__) || defined(__SC__) 3874 /* 3875 * No environ[] on the Amiga and on the Mac (using MPW). 3876 */ 3877 return NULL; 3878 # else 3879 # ifndef __WIN32__ 3880 /* Borland C++ 5.2 has this in a header file. */ 3881 extern char **environ; 3882 # endif 3883 # define ENVNAMELEN 100 3884 static char_u name[ENVNAMELEN]; 3885 char_u *str; 3886 int n; 3887 3888 str = (char_u *)environ[idx]; 3889 if (str == NULL) 3890 return NULL; 3891 3892 for (n = 0; n < ENVNAMELEN - 1; ++n) 3893 { 3894 if (str[n] == '=' || str[n] == NUL) 3895 break; 3896 name[n] = str[n]; 3897 } 3898 name[n] = NUL; 3899 return name; 3900 # endif 3901 } 3902 #endif 3903 3904 /* 3905 * Replace home directory by "~" in each space or comma separated file name in 3906 * 'src'. 3907 * If anything fails (except when out of space) dst equals src. 3908 */ 3909 void 3910 home_replace(buf, src, dst, dstlen, one) 3911 buf_T *buf; /* when not NULL, check for help files */ 3912 char_u *src; /* input file name */ 3913 char_u *dst; /* where to put the result */ 3914 int dstlen; /* maximum length of the result */ 3915 int one; /* if TRUE, only replace one file name, include 3916 spaces and commas in the file name. */ 3917 { 3918 size_t dirlen = 0, envlen = 0; 3919 size_t len; 3920 char_u *homedir_env; 3921 char_u *p; 3922 3923 if (src == NULL) 3924 { 3925 *dst = NUL; 3926 return; 3927 } 3928 3929 /* 3930 * If the file is a help file, remove the path completely. 3931 */ 3932 if (buf != NULL && buf->b_help) 3933 { 3934 STRCPY(dst, gettail(src)); 3935 return; 3936 } 3937 3938 /* 3939 * We check both the value of the $HOME environment variable and the 3940 * "real" home directory. 3941 */ 3942 if (homedir != NULL) 3943 dirlen = STRLEN(homedir); 3944 3945 #ifdef VMS 3946 homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); 3947 #else 3948 homedir_env = mch_getenv((char_u *)"HOME"); 3949 #endif 3950 3951 if (homedir_env != NULL && *homedir_env == NUL) 3952 homedir_env = NULL; 3953 if (homedir_env != NULL) 3954 envlen = STRLEN(homedir_env); 3955 3956 if (!one) 3957 src = skipwhite(src); 3958 while (*src && dstlen > 0) 3959 { 3960 /* 3961 * Here we are at the beginning of a file name. 3962 * First, check to see if the beginning of the file name matches 3963 * $HOME or the "real" home directory. Check that there is a '/' 3964 * after the match (so that if e.g. the file is "/home/pieter/bla", 3965 * and the home directory is "/home/piet", the file does not end up 3966 * as "~er/bla" (which would seem to indicate the file "bla" in user 3967 * er's home directory)). 3968 */ 3969 p = homedir; 3970 len = dirlen; 3971 for (;;) 3972 { 3973 if ( len 3974 && fnamencmp(src, p, len) == 0 3975 && (vim_ispathsep(src[len]) 3976 || (!one && (src[len] == ',' || src[len] == ' ')) 3977 || src[len] == NUL)) 3978 { 3979 src += len; 3980 if (--dstlen > 0) 3981 *dst++ = '~'; 3982 3983 /* 3984 * If it's just the home directory, add "/". 3985 */ 3986 if (!vim_ispathsep(src[0]) && --dstlen > 0) 3987 *dst++ = '/'; 3988 break; 3989 } 3990 if (p == homedir_env) 3991 break; 3992 p = homedir_env; 3993 len = envlen; 3994 } 3995 3996 /* if (!one) skip to separator: space or comma */ 3997 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) 3998 *dst++ = *src++; 3999 /* skip separator */ 4000 while ((*src == ' ' || *src == ',') && --dstlen > 0) 4001 *dst++ = *src++; 4002 } 4003 /* if (dstlen == 0) out of space, what to do??? */ 4004 4005 *dst = NUL; 4006 } 4007 4008 /* 4009 * Like home_replace, store the replaced string in allocated memory. 4010 * When something fails, NULL is returned. 4011 */ 4012 char_u * 4013 home_replace_save(buf, src) 4014 buf_T *buf; /* when not NULL, check for help files */ 4015 char_u *src; /* input file name */ 4016 { 4017 char_u *dst; 4018 unsigned len; 4019 4020 len = 3; /* space for "~/" and trailing NUL */ 4021 if (src != NULL) /* just in case */ 4022 len += (unsigned)STRLEN(src); 4023 dst = alloc(len); 4024 if (dst != NULL) 4025 home_replace(buf, src, dst, len, TRUE); 4026 return dst; 4027 } 4028 4029 /* 4030 * Compare two file names and return: 4031 * FPC_SAME if they both exist and are the same file. 4032 * FPC_SAMEX if they both don't exist and have the same file name. 4033 * FPC_DIFF if they both exist and are different files. 4034 * FPC_NOTX if they both don't exist. 4035 * FPC_DIFFX if one of them doesn't exist. 4036 * For the first name environment variables are expanded 4037 */ 4038 int 4039 fullpathcmp(s1, s2, checkname) 4040 char_u *s1, *s2; 4041 int checkname; /* when both don't exist, check file names */ 4042 { 4043 #ifdef UNIX 4044 char_u exp1[MAXPATHL]; 4045 char_u full1[MAXPATHL]; 4046 char_u full2[MAXPATHL]; 4047 struct stat st1, st2; 4048 int r1, r2; 4049 4050 expand_env(s1, exp1, MAXPATHL); 4051 r1 = mch_stat((char *)exp1, &st1); 4052 r2 = mch_stat((char *)s2, &st2); 4053 if (r1 != 0 && r2 != 0) 4054 { 4055 /* if mch_stat() doesn't work, may compare the names */ 4056 if (checkname) 4057 { 4058 if (fnamecmp(exp1, s2) == 0) 4059 return FPC_SAMEX; 4060 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); 4061 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); 4062 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) 4063 return FPC_SAMEX; 4064 } 4065 return FPC_NOTX; 4066 } 4067 if (r1 != 0 || r2 != 0) 4068 return FPC_DIFFX; 4069 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) 4070 return FPC_SAME; 4071 return FPC_DIFF; 4072 #else 4073 char_u *exp1; /* expanded s1 */ 4074 char_u *full1; /* full path of s1 */ 4075 char_u *full2; /* full path of s2 */ 4076 int retval = FPC_DIFF; 4077 int r1, r2; 4078 4079 /* allocate one buffer to store three paths (alloc()/free() is slow!) */ 4080 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) 4081 { 4082 full1 = exp1 + MAXPATHL; 4083 full2 = full1 + MAXPATHL; 4084 4085 expand_env(s1, exp1, MAXPATHL); 4086 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); 4087 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); 4088 4089 /* If vim_FullName() fails, the file probably doesn't exist. */ 4090 if (r1 != OK && r2 != OK) 4091 { 4092 if (checkname && fnamecmp(exp1, s2) == 0) 4093 retval = FPC_SAMEX; 4094 else 4095 retval = FPC_NOTX; 4096 } 4097 else if (r1 != OK || r2 != OK) 4098 retval = FPC_DIFFX; 4099 else if (fnamecmp(full1, full2)) 4100 retval = FPC_DIFF; 4101 else 4102 retval = FPC_SAME; 4103 vim_free(exp1); 4104 } 4105 return retval; 4106 #endif 4107 } 4108 4109 /* 4110 * Get the tail of a path: the file name. 4111 * Fail safe: never returns NULL. 4112 */ 4113 char_u * 4114 gettail(fname) 4115 char_u *fname; 4116 { 4117 char_u *p1, *p2; 4118 4119 if (fname == NULL) 4120 return (char_u *)""; 4121 for (p1 = p2 = fname; *p2; ) /* find last part of path */ 4122 { 4123 if (vim_ispathsep(*p2)) 4124 p1 = p2 + 1; 4125 #ifdef FEAT_MBYTE 4126 if (has_mbyte) 4127 p2 += (*mb_ptr2len_check)(p2); 4128 else 4129 #endif 4130 ++p2; 4131 } 4132 return p1; 4133 } 4134 4135 /* 4136 * get the next path component (just after the next path separator). 4137 */ 4138 char_u * 4139 getnextcomp(fname) 4140 char_u *fname; 4141 { 4142 while (*fname && !vim_ispathsep(*fname)) 4143 ++fname; 4144 if (*fname) 4145 ++fname; 4146 return fname; 4147 } 4148 4149 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ 4150 || defined(FEAT_SESSION) || defined(MSWIN) \ 4151 || ((defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)) \ 4152 && (defined(FEAT_WINDOWS) || defined(FEAT_DND))) \ 4153 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \ 4154 || defined(PROTO) 4155 /* 4156 * Get a pointer to one character past the head of a path name. 4157 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. 4158 * If there is no head, path is returned. 4159 */ 4160 char_u * 4161 get_past_head(path) 4162 char_u *path; 4163 { 4164 char_u *retval; 4165 4166 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) 4167 /* may skip "c:" */ 4168 if (isalpha(path[0]) && path[1] == ':') 4169 retval = path + 2; 4170 else 4171 retval = path; 4172 #else 4173 # if defined(AMIGA) 4174 /* may skip "label:" */ 4175 retval = vim_strchr(path, ':'); 4176 if (retval == NULL) 4177 retval = path; 4178 # else /* Unix */ 4179 retval = path; 4180 # endif 4181 #endif 4182 4183 while (vim_ispathsep(*retval)) 4184 ++retval; 4185 4186 return retval; 4187 } 4188 #endif 4189 4190 /* 4191 * return TRUE if 'c' is a path separator. 4192 */ 4193 int 4194 vim_ispathsep(c) 4195 int c; 4196 { 4197 #ifdef RISCOS 4198 return (c == '.' || c == ':'); 4199 #else 4200 # ifdef UNIX 4201 return (c == '/'); /* UNIX has ':' inside file names */ 4202 # else 4203 # ifdef BACKSLASH_IN_FILENAME 4204 return (c == ':' || c == '/' || c == '\\'); 4205 # else 4206 # ifdef VMS 4207 /* server"user passwd"::device:[full.path.name]fname.extension;version" */ 4208 return (c == ':' || c == '[' || c == ']' || c == '/' 4209 || c == '<' || c == '>' || c == '"' ); 4210 # else 4211 # ifdef COLON_AS_PATHSEP 4212 return (c == ':'); 4213 # else /* Amiga */ 4214 return (c == ':' || c == '/'); 4215 # endif 4216 # endif /* VMS */ 4217 # endif 4218 # endif 4219 #endif /* RISC OS */ 4220 } 4221 4222 #if defined(FEAT_SEARCHPATH) || defined(PROTO) 4223 /* 4224 * return TRUE if 'c' is a path list separator. 4225 */ 4226 int 4227 vim_ispathlistsep(c) 4228 int c; 4229 { 4230 #ifdef UNIX 4231 return (c == ':'); 4232 #else 4233 return (c == ';'); /* might not be rigth for every system... */ 4234 #endif 4235 } 4236 #endif 4237 4238 #if (defined(CASE_INSENSITIVE_FILENAME) && defined(BACKSLASH_IN_FILENAME)) \ 4239 || defined(PROTO) 4240 /* 4241 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally. 4242 */ 4243 int 4244 vim_fnamecmp(x, y) 4245 char_u *x, *y; 4246 { 4247 return vim_fnamencmp(x, y, MAXPATHL); 4248 } 4249 4250 int 4251 vim_fnamencmp(x, y, len) 4252 char_u *x, *y; 4253 size_t len; 4254 { 4255 while (len > 0 && *x && *y) 4256 { 4257 if (TOLOWER_LOC(*x) != TOLOWER_LOC(*y) 4258 && !(*x == '/' && *y == '\\') 4259 && !(*x == '\\' && *y == '/')) 4260 break; 4261 ++x; 4262 ++y; 4263 --len; 4264 } 4265 if (len == 0) 4266 return 0; 4267 return (*x - *y); 4268 } 4269 #endif 4270 4271 /* 4272 * Concatenate file names fname1 and fname2 into allocated memory. 4273 * Only add a '/' or '\\' when 'sep' is TRUE and it is neccesary. 4274 */ 4275 char_u * 4276 concat_fnames(fname1, fname2, sep) 4277 char_u *fname1; 4278 char_u *fname2; 4279 int sep; 4280 { 4281 char_u *dest; 4282 4283 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); 4284 if (dest != NULL) 4285 { 4286 STRCPY(dest, fname1); 4287 if (sep) 4288 add_pathsep(dest); 4289 STRCAT(dest, fname2); 4290 } 4291 return dest; 4292 } 4293 4294 /* 4295 * Add a path separator to a file name, unless it already ends in a path 4296 * separator. 4297 */ 4298 void 4299 add_pathsep(p) 4300 char_u *p; 4301 { 4302 if (*p != NUL && !vim_ispathsep(*(p + STRLEN(p) - 1))) 4303 STRCAT(p, PATHSEPSTR); 4304 } 4305 4306 /* 4307 * FullName_save - Make an allocated copy of a full file name. 4308 * Returns NULL when out of memory. 4309 */ 4310 char_u * 4311 FullName_save(fname, force) 4312 char_u *fname; 4313 int force; /* force expansion, even when it already looks 4314 like a full path name */ 4315 { 4316 char_u *buf; 4317 char_u *new_fname = NULL; 4318 4319 if (fname == NULL) 4320 return NULL; 4321 4322 buf = alloc((unsigned)MAXPATHL); 4323 if (buf != NULL) 4324 { 4325 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) 4326 new_fname = vim_strsave(buf); 4327 else 4328 new_fname = vim_strsave(fname); 4329 vim_free(buf); 4330 } 4331 return new_fname; 4332 } 4333 4334 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) 4335 4336 static char_u *skip_string __ARGS((char_u *p)); 4337 4338 /* 4339 * Find the start of a comment, not knowing if we are in a comment right now. 4340 * Search starts at w_cursor.lnum and goes backwards. 4341 */ 4342 pos_T * 4343 find_start_comment(ind_maxcomment) /* XXX */ 4344 int ind_maxcomment; 4345 { 4346 pos_T *pos; 4347 char_u *line; 4348 char_u *p; 4349 4350 if ((pos = findmatchlimit(NULL, '*', FM_BACKWARD, ind_maxcomment)) == NULL) 4351 return NULL; 4352 4353 /* 4354 * Check if the comment start we found is inside a string. 4355 */ 4356 line = ml_get(pos->lnum); 4357 for (p = line; *p && (unsigned)(p - line) < pos->col; ++p) 4358 p = skip_string(p); 4359 if ((unsigned)(p - line) > pos->col) 4360 return NULL; 4361 return pos; 4362 } 4363 4364 /* 4365 * Skip to the end of a "string" and a 'c' character. 4366 * If there is no string or character, return argument unmodified. 4367 */ 4368 static char_u * 4369 skip_string(p) 4370 char_u *p; 4371 { 4372 int i; 4373 4374 /* 4375 * We loop, because strings may be concatenated: "date""time". 4376 */ 4377 for ( ; ; ++p) 4378 { 4379 if (p[0] == '\'') /* 'c' or '\n' or '\000' */ 4380 { 4381 if (!p[1]) /* ' at end of line */ 4382 break; 4383 i = 2; 4384 if (p[1] == '\\') /* '\n' or '\000' */ 4385 { 4386 ++i; 4387 while (vim_isdigit(p[i - 1])) /* '\000' */ 4388 ++i; 4389 } 4390 if (p[i] == '\'') /* check for trailing ' */ 4391 { 4392 p += i; 4393 continue; 4394 } 4395 } 4396 else if (p[0] == '"') /* start of string */ 4397 { 4398 for (++p; p[0]; ++p) 4399 { 4400 if (p[0] == '\\' && p[1] != NUL) 4401 ++p; 4402 else if (p[0] == '"') /* end of string */ 4403 break; 4404 } 4405 if (p[0] == '"') 4406 continue; 4407 } 4408 break; /* no string found */ 4409 } 4410 if (!*p) 4411 --p; /* backup from NUL */ 4412 return p; 4413 } 4414 #endif /* FEAT_CINDENT || FEAT_SYN_HL */ 4415 4416 #if defined(FEAT_CINDENT) || defined(PROTO) 4417 4418 /* 4419 * Do C or expression indenting on the current line. 4420 */ 4421 void 4422 do_c_expr_indent() 4423 { 4424 # ifdef FEAT_EVAL 4425 if (*curbuf->b_p_inde != NUL) 4426 fixthisline(get_expr_indent); 4427 else 4428 # endif 4429 fixthisline(get_c_indent); 4430 } 4431 4432 /* 4433 * Functions for C-indenting. 4434 * Most of this originally comes from Eric Fischer. 4435 */ 4436 /* 4437 * Below "XXX" means that this function may unlock the current line. 4438 */ 4439 4440 static char_u *cin_skipcomment __ARGS((char_u *)); 4441 static int cin_nocode __ARGS((char_u *)); 4442 static pos_T *find_line_comment __ARGS((void)); 4443 static int cin_islabel_skip __ARGS((char_u **)); 4444 static int cin_isdefault __ARGS((char_u *)); 4445 static char_u *after_label __ARGS((char_u *l)); 4446 static int get_indent_nolabel __ARGS((linenr_T lnum)); 4447 static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment)); 4448 static int cin_first_id_amount __ARGS((void)); 4449 static int cin_get_equal_amount __ARGS((linenr_T lnum)); 4450 static int cin_ispreproc __ARGS((char_u *)); 4451 static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump)); 4452 static int cin_iscomment __ARGS((char_u *)); 4453 static int cin_islinecomment __ARGS((char_u *)); 4454 static int cin_isterminated __ARGS((char_u *, int, int)); 4455 static int cin_isinit __ARGS((void)); 4456 static int cin_isfuncdecl __ARGS((char_u **, linenr_T)); 4457 static int cin_isif __ARGS((char_u *)); 4458 static int cin_iselse __ARGS((char_u *)); 4459 static int cin_isdo __ARGS((char_u *)); 4460 static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int)); 4461 static int cin_isbreak __ARGS((char_u *)); 4462 static int cin_is_cpp_baseclass __ARGS((char_u *line, colnr_T *col)); 4463 static int cin_ends_in __ARGS((char_u *, char_u *, char_u *)); 4464 static int cin_skip2pos __ARGS((pos_T *trypos)); 4465 static pos_T *find_start_brace __ARGS((int)); 4466 static pos_T *find_match_paren __ARGS((int, int)); 4467 static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos)); 4468 static int find_last_paren __ARGS((char_u *l, int start, int end)); 4469 static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment)); 4470 4471 /* 4472 * Skip over white space and C comments within the line. 4473 */ 4474 static char_u * 4475 cin_skipcomment(s) 4476 char_u *s; 4477 { 4478 while (*s) 4479 { 4480 s = skipwhite(s); 4481 if (*s != '/') 4482 break; 4483 ++s; 4484 if (*s == '/') /* slash-slash comment continues till eol */ 4485 { 4486 s += STRLEN(s); 4487 break; 4488 } 4489 if (*s != '*') 4490 break; 4491 for (++s; *s; ++s) /* skip slash-star comment */ 4492 if (s[0] == '*' && s[1] == '/') 4493 { 4494 s += 2; 4495 break; 4496 } 4497 } 4498 return s; 4499 } 4500 4501 /* 4502 * Return TRUE if there there is no code at *s. White space and comments are 4503 * not considered code. 4504 */ 4505 static int 4506 cin_nocode(s) 4507 char_u *s; 4508 { 4509 return *cin_skipcomment(s) == NUL; 4510 } 4511 4512 /* 4513 * Check previous lines for a "//" line comment, skipping over blank lines. 4514 */ 4515 static pos_T * 4516 find_line_comment() /* XXX */ 4517 { 4518 static pos_T pos; 4519 char_u *line; 4520 char_u *p; 4521 4522 pos = curwin->w_cursor; 4523 while (--pos.lnum > 0) 4524 { 4525 line = ml_get(pos.lnum); 4526 p = skipwhite(line); 4527 if (cin_islinecomment(p)) 4528 { 4529 pos.col = (int)(p - line); 4530 return &pos; 4531 } 4532 if (*p != NUL) 4533 break; 4534 } 4535 return NULL; 4536 } 4537 4538 /* 4539 * Check if string matches "label:"; move to character after ':' if true. 4540 */ 4541 static int 4542 cin_islabel_skip(s) 4543 char_u **s; 4544 { 4545 if (!vim_isIDc(**s)) /* need at least one ID character */ 4546 return FALSE; 4547 4548 while (vim_isIDc(**s)) 4549 (*s)++; 4550 4551 *s = cin_skipcomment(*s); 4552 4553 /* "::" is not a label, it's C++ */ 4554 return (**s == ':' && *++*s != ':'); 4555 } 4556 4557 /* 4558 * Recognize a label: "label:". 4559 * Note: curwin->w_cursor must be where we are looking for the label. 4560 */ 4561 int 4562 cin_islabel(ind_maxcomment) /* XXX */ 4563 int ind_maxcomment; 4564 { 4565 char_u *s; 4566 4567 s = cin_skipcomment(ml_get_curline()); 4568 4569 /* 4570 * Exclude "default" from labels, since it should be indented 4571 * like a switch label. Same for C++ scope declarations. 4572 */ 4573 if (cin_isdefault(s)) 4574 return FALSE; 4575 if (cin_isscopedecl(s)) 4576 return FALSE; 4577 4578 if (cin_islabel_skip(&s)) 4579 { 4580 /* 4581 * Only accept a label if the previous line is terminated or is a case 4582 * label. 4583 */ 4584 pos_T cursor_save; 4585 pos_T *trypos; 4586 char_u *line; 4587 4588 cursor_save = curwin->w_cursor; 4589 while (curwin->w_cursor.lnum > 1) 4590 { 4591 --curwin->w_cursor.lnum; 4592 4593 /* 4594 * If we're in a comment now, skip to the start of the comment. 4595 */ 4596 curwin->w_cursor.col = 0; 4597 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ 4598 curwin->w_cursor = *trypos; 4599 4600 line = ml_get_curline(); 4601 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ 4602 continue; 4603 if (*(line = cin_skipcomment(line)) == NUL) 4604 continue; 4605 4606 curwin->w_cursor = cursor_save; 4607 if (cin_isterminated(line, TRUE, FALSE) 4608 || cin_isscopedecl(line) 4609 || cin_iscase(line) 4610 || (cin_islabel_skip(&line) && cin_nocode(line))) 4611 return TRUE; 4612 return FALSE; 4613 } 4614 curwin->w_cursor = cursor_save; 4615 return TRUE; /* label at start of file??? */ 4616 } 4617 return FALSE; 4618 } 4619 4620 /* 4621 * Recognize structure initialization and enumerations. 4622 * Q&D-Implementation: 4623 * check for "=" at end or "[typedef] enum" at beginning of line. 4624 */ 4625 static int 4626 cin_isinit(void) 4627 { 4628 char_u *s; 4629 4630 s = cin_skipcomment(ml_get_curline()); 4631 4632 if (STRNCMP(s, "typedef", 7) == 0 && !vim_isIDc(s[7])) 4633 s = cin_skipcomment(s + 7); 4634 4635 if (STRNCMP(s, "enum", 4) == 0 && !vim_isIDc(s[4])) 4636 return TRUE; 4637 4638 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) 4639 return TRUE; 4640 4641 return FALSE; 4642 } 4643 4644 /* 4645 * Recognize a switch label: "case .*:" or "default:". 4646 */ 4647 int 4648 cin_iscase(s) 4649 char_u *s; 4650 { 4651 s = cin_skipcomment(s); 4652 if (STRNCMP(s, "case", 4) == 0 && !vim_isIDc(s[4])) 4653 { 4654 for (s += 4; *s; ++s) 4655 { 4656 s = cin_skipcomment(s); 4657 if (*s == ':') 4658 { 4659 if (s[1] == ':') /* skip over "::" for C++ */ 4660 ++s; 4661 else 4662 return TRUE; 4663 } 4664 if (*s == '\'' && s[1] && s[2] == '\'') 4665 s += 2; /* skip over '.' */ 4666 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) 4667 return FALSE; /* stop at comment */ 4668 else if (*s == '"') 4669 return FALSE; /* stop at string */ 4670 } 4671 return FALSE; 4672 } 4673 4674 if (cin_isdefault(s)) 4675 return TRUE; 4676 return FALSE; 4677 } 4678 4679 /* 4680 * Recognize a "default" switch label. 4681 */ 4682 static int 4683 cin_isdefault(s) 4684 char_u *s; 4685 { 4686 return (STRNCMP(s, "default", 7) == 0 4687 && *(s = cin_skipcomment(s + 7)) == ':' 4688 && s[1] != ':'); 4689 } 4690 4691 /* 4692 * Recognize a "public/private/proctected" scope declaration label. 4693 */ 4694 int 4695 cin_isscopedecl(s) 4696 char_u *s; 4697 { 4698 int i; 4699 4700 s = cin_skipcomment(s); 4701 if (STRNCMP(s, "public", 6) == 0) 4702 i = 6; 4703 else if (STRNCMP(s, "protected", 9) == 0) 4704 i = 9; 4705 else if (STRNCMP(s, "private", 7) == 0) 4706 i = 7; 4707 else 4708 return FALSE; 4709 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); 4710 } 4711 4712 /* 4713 * Return a pointer to the first non-empty non-comment character after a ':'. 4714 * Return NULL if not found. 4715 * case 234: a = b; 4716 * ^ 4717 */ 4718 static char_u * 4719 after_label(l) 4720 char_u *l; 4721 { 4722 for ( ; *l; ++l) 4723 { 4724 if (*l == ':') 4725 { 4726 if (l[1] == ':') /* skip over "::" for C++ */ 4727 ++l; 4728 else if (!cin_iscase(l + 1)) 4729 break; 4730 } 4731 else if (*l == '\'' && l[1] && l[2] == '\'') 4732 l += 2; /* skip over 'x' */ 4733 } 4734 if (*l == NUL) 4735 return NULL; 4736 l = cin_skipcomment(l + 1); 4737 if (*l == NUL) 4738 return NULL; 4739 return l; 4740 } 4741 4742 /* 4743 * Get indent of line "lnum", skipping a label. 4744 * Return 0 if there is nothing after the label. 4745 */ 4746 static int 4747 get_indent_nolabel(lnum) /* XXX */ 4748 linenr_T lnum; 4749 { 4750 char_u *l; 4751 pos_T fp; 4752 colnr_T col; 4753 char_u *p; 4754 4755 l = ml_get(lnum); 4756 p = after_label(l); 4757 if (p == NULL) 4758 return 0; 4759 4760 fp.col = (colnr_T)(p - l); 4761 fp.lnum = lnum; 4762 getvcol(curwin, &fp, &col, NULL, NULL); 4763 return (int)col; 4764 } 4765 4766 /* 4767 * Find indent for line "lnum", ignoring any case or jump label. 4768 * Also return a pointer to the text (after the label). 4769 * label: if (asdf && asdfasdf) 4770 * ^ 4771 */ 4772 static int 4773 skip_label(lnum, pp, ind_maxcomment) 4774 linenr_T lnum; 4775 char_u **pp; 4776 int ind_maxcomment; 4777 { 4778 char_u *l; 4779 int amount; 4780 pos_T cursor_save; 4781 4782 cursor_save = curwin->w_cursor; 4783 curwin->w_cursor.lnum = lnum; 4784 l = ml_get_curline(); 4785 /* XXX */ 4786 if (cin_iscase(l) || cin_isscopedecl(l) || cin_islabel(ind_maxcomment)) 4787 { 4788 amount = get_indent_nolabel(lnum); 4789 l = after_label(ml_get_curline()); 4790 if (l == NULL) /* just in case */ 4791 l = ml_get_curline(); 4792 } 4793 else 4794 { 4795 amount = get_indent(); 4796 l = ml_get_curline(); 4797 } 4798 *pp = l; 4799 4800 curwin->w_cursor = cursor_save; 4801 return amount; 4802 } 4803 4804 /* 4805 * Return the indent of the first variable name after a type in a declaration. 4806 * int a, indent of "a" 4807 * static struct foo b, indent of "b" 4808 * enum bla c, indent of "c" 4809 * Returns zero when it doesn't look like a declaration. 4810 */ 4811 static int 4812 cin_first_id_amount() 4813 { 4814 char_u *line, *p, *s; 4815 int len; 4816 pos_T fp; 4817 colnr_T col; 4818 4819 line = ml_get_curline(); 4820 p = skipwhite(line); 4821 len = skiptowhite(p) - p; 4822 if (len == 6 && STRNCMP(p, "static", 6) == 0) 4823 { 4824 p = skipwhite(p + 6); 4825 len = skiptowhite(p) - p; 4826 } 4827 if (len == 6 && STRNCMP(p, "struct", 6) == 0) 4828 p = skipwhite(p + 6); 4829 else if (len == 4 && STRNCMP(p, "enum", 4) == 0) 4830 p = skipwhite(p + 4); 4831 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) 4832 || (len == 6 && STRNCMP(p, "signed", 6) == 0)) 4833 { 4834 s = skipwhite(p + len); 4835 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3])) 4836 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4])) 4837 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5])) 4838 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4]))) 4839 p = s; 4840 } 4841 for (len = 0; vim_isIDc(p[len]); ++len) 4842 ; 4843 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p)) 4844 return 0; 4845 4846 p = skipwhite(p + len); 4847 fp.lnum = curwin->w_cursor.lnum; 4848 fp.col = (colnr_T)(p - line); 4849 getvcol(curwin, &fp, &col, NULL, NULL); 4850 return (int)col; 4851 } 4852 4853 /* 4854 * Return the indent of the first non-blank after an equal sign. 4855 * char *foo = "here"; 4856 * Return zero if no (useful) equal sign found. 4857 * Return -1 if the line above "lnum" ends in a backslash. 4858 * foo = "asdf\ 4859 * asdf\ 4860 * here"; 4861 */ 4862 static int 4863 cin_get_equal_amount(lnum) 4864 linenr_T lnum; 4865 { 4866 char_u *line; 4867 char_u *s; 4868 colnr_T col; 4869 pos_T fp; 4870 4871 if (lnum > 1) 4872 { 4873 line = ml_get(lnum - 1); 4874 if (*line != NUL && line[STRLEN(line) - 1] == '\\') 4875 return -1; 4876 } 4877 4878 line = s = ml_get(lnum); 4879 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) 4880 { 4881 if (cin_iscomment(s)) /* ignore comments */ 4882 s = cin_skipcomment(s); 4883 else 4884 ++s; 4885 } 4886 if (*s != '=') 4887 return 0; 4888 4889 s = skipwhite(s + 1); 4890 if (cin_nocode(s)) 4891 return 0; 4892 4893 if (*s == '"') /* nice alignment for continued strings */ 4894 ++s; 4895 4896 fp.lnum = lnum; 4897 fp.col = (colnr_T)(s - line); 4898 getvcol(curwin, &fp, &col, NULL, NULL); 4899 return (int)col; 4900 } 4901 4902 /* 4903 * Recognize a preprocessor statement: Any line that starts with '#'. 4904 */ 4905 static int 4906 cin_ispreproc(s) 4907 char_u *s; 4908 { 4909 s = skipwhite(s); 4910 if (*s == '#') 4911 return TRUE; 4912 return FALSE; 4913 } 4914 4915 /* 4916 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a 4917 * continuation line of a preprocessor statement. Decrease "*lnump" to the 4918 * start and return the line in "*pp". 4919 */ 4920 static int 4921 cin_ispreproc_cont(pp, lnump) 4922 char_u **pp; 4923 linenr_T *lnump; 4924 { 4925 char_u *line = *pp; 4926 linenr_T lnum = *lnump; 4927 int retval = FALSE; 4928 4929 while (1) 4930 { 4931 if (cin_ispreproc(line)) 4932 { 4933 retval = TRUE; 4934 *lnump = lnum; 4935 break; 4936 } 4937 if (lnum == 1) 4938 break; 4939 line = ml_get(--lnum); 4940 if (*line == NUL || line[STRLEN(line) - 1] != '\\') 4941 break; 4942 } 4943 4944 if (lnum != *lnump) 4945 *pp = ml_get(*lnump); 4946 return retval; 4947 } 4948 4949 /* 4950 * Recognize the start of a C or C++ comment. 4951 */ 4952 static int 4953 cin_iscomment(p) 4954 char_u *p; 4955 { 4956 return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); 4957 } 4958 4959 /* 4960 * Recognize the start of a "//" comment. 4961 */ 4962 static int 4963 cin_islinecomment(p) 4964 char_u *p; 4965 { 4966 return (p[0] == '/' && p[1] == '/'); 4967 } 4968 4969 /* 4970 * Recognize a line that starts with '{' or '}', or ends with ';', '{' or '}'. 4971 * Don't consider "} else" a terminated line. 4972 * Return the character terminating the line (ending char's have precedence if 4973 * both apply in order to determine initializations). 4974 */ 4975 static int 4976 cin_isterminated(s, incl_open, incl_comma) 4977 char_u *s; 4978 int incl_open; /* include '{' at the end as terminator */ 4979 int incl_comma; /* recognize a trailing comma */ 4980 { 4981 char_u found_start = 0; 4982 4983 s = cin_skipcomment(s); 4984 4985 if (*s == '{' || (*s == '}' && !cin_iselse(s))) 4986 found_start = *s; 4987 4988 while (*s) 4989 { 4990 /* skip over comments, "" strings and 'c'haracters */ 4991 s = skip_string(cin_skipcomment(s)); 4992 if ((*s == ';' || (incl_open && *s == '{') || *s == '}' 4993 || (incl_comma && *s == ',')) 4994 && cin_nocode(s + 1)) 4995 return *s; 4996 4997 if (*s) 4998 s++; 4999 } 5000 return found_start; 5001 } 5002 5003 /* 5004 * Recognize the basic picture of a function declaration -- it needs to 5005 * have an open paren somewhere and a close paren at the end of the line and 5006 * no semicolons anywhere. 5007 * When a line ends in a comma we continue looking in the next line. 5008 * "sp" points to a string with the line. When looking at other lines it must 5009 * be restored to the line. When it's NULL fetch lines here. 5010 * "lnum" is where we start looking. 5011 */ 5012 static int 5013 cin_isfuncdecl(sp, first_lnum) 5014 char_u **sp; 5015 linenr_T first_lnum; 5016 { 5017 char_u *s; 5018 linenr_T lnum = first_lnum; 5019 int retval = FALSE; 5020 5021 if (sp == NULL) 5022 s = ml_get(lnum); 5023 else 5024 s = *sp; 5025 5026 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') 5027 { 5028 if (cin_iscomment(s)) /* ignore comments */ 5029 s = cin_skipcomment(s); 5030 else 5031 ++s; 5032 } 5033 if (*s != '(') 5034 return FALSE; /* ';', ' or " before any () or no '(' */ 5035 5036 while (*s && *s != ';' && *s != '\'' && *s != '"') 5037 { 5038 if (*s == ')' && cin_nocode(s + 1)) 5039 { 5040 /* ')' at the end: may have found a match 5041 * Check for he previous line not to end in a backslash: 5042 * #if defined(x) && \ 5043 * defined(y) 5044 */ 5045 lnum = first_lnum - 1; 5046 s = ml_get(lnum); 5047 if (*s == NUL || s[STRLEN(s) - 1] != '\\') 5048 retval = TRUE; 5049 goto done; 5050 } 5051 if (*s == ',' && cin_nocode(s + 1)) 5052 { 5053 /* ',' at the end: continue looking in the next line */ 5054 if (lnum >= curbuf->b_ml.ml_line_count) 5055 break; 5056 5057 s = ml_get(++lnum); 5058 } 5059 else if (cin_iscomment(s)) /* ignore comments */ 5060 s = cin_skipcomment(s); 5061 else 5062 ++s; 5063 } 5064 5065 done: 5066 if (lnum != first_lnum && sp != NULL) 5067 *sp = ml_get(first_lnum); 5068 5069 return retval; 5070 } 5071 5072 static int 5073 cin_isif(p) 5074 char_u *p; 5075 { 5076 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); 5077 } 5078 5079 static int 5080 cin_iselse(p) 5081 char_u *p; 5082 { 5083 if (*p == '}') /* accept "} else" */ 5084 p = cin_skipcomment(p + 1); 5085 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); 5086 } 5087 5088 static int 5089 cin_isdo(p) 5090 char_u *p; 5091 { 5092 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); 5093 } 5094 5095 /* 5096 * Check if this is a "while" that should have a matching "do". 5097 * We only accept a "while (condition) ;", with only white space between the 5098 * ')' and ';'. The condition may be spread over several lines. 5099 */ 5100 static int 5101 cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */ 5102 char_u *p; 5103 linenr_T lnum; 5104 int ind_maxparen; 5105 { 5106 pos_T cursor_save; 5107 pos_T *trypos; 5108 int retval = FALSE; 5109 5110 p = cin_skipcomment(p); 5111 if (*p == '}') /* accept "} while (cond);" */ 5112 p = cin_skipcomment(p + 1); 5113 if (STRNCMP(p, "while", 5) == 0 && !vim_isIDc(p[5])) 5114 { 5115 cursor_save = curwin->w_cursor; 5116 curwin->w_cursor.lnum = lnum; 5117 curwin->w_cursor.col = 0; 5118 p = ml_get_curline(); 5119 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ 5120 { 5121 ++p; 5122 ++curwin->w_cursor.col; 5123 } 5124 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL 5125 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') 5126 retval = TRUE; 5127 curwin->w_cursor = cursor_save; 5128 } 5129 return retval; 5130 } 5131 5132 static int 5133 cin_isbreak(p) 5134 char_u *p; 5135 { 5136 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); 5137 } 5138 5139 /* Find the position of a C++ base-class declaration or 5140 * constructor-initialization. eg: 5141 * 5142 * class MyClass : 5143 * baseClass <-- here 5144 * class MyClass : public baseClass, 5145 * anotherBaseClass <-- here (should probably lineup ??) 5146 * MyClass::MyClass(...) : 5147 * baseClass(...) <-- here (constructor-initialization) 5148 */ 5149 static int 5150 cin_is_cpp_baseclass(line, col) 5151 char_u *line; 5152 colnr_T *col; 5153 { 5154 char_u *s; 5155 int class_or_struct, lookfor_ctor_init, cpp_base_class; 5156 5157 *col = 0; 5158 5159 s = skipwhite(line); 5160 if (*s == '#') /* skip #define FOO x ? (x) : x */ 5161 return FALSE; 5162 s = cin_skipcomment(s); 5163 if (*s == NUL) 5164 return FALSE; 5165 5166 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; 5167 5168 while(*s != NUL) 5169 { 5170 if (s[0] == ':') 5171 { 5172 if (s[1] == ':') 5173 { 5174 /* skip double colon. It can't be a constructor 5175 * initialization any more */ 5176 lookfor_ctor_init = FALSE; 5177 s = cin_skipcomment(s + 2); 5178 } 5179 else if (lookfor_ctor_init || class_or_struct) 5180 { 5181 /* we have something found, that looks like the start of 5182 * cpp-base-class-declaration or contructor-initialization */ 5183 cpp_base_class = TRUE; 5184 lookfor_ctor_init = class_or_struct = FALSE; 5185 *col = 0; 5186 s = cin_skipcomment(s + 1); 5187 } 5188 else 5189 s = cin_skipcomment(s + 1); 5190 } 5191 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) 5192 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) 5193 { 5194 class_or_struct = TRUE; 5195 lookfor_ctor_init = FALSE; 5196 5197 if (*s == 'c') 5198 s = cin_skipcomment(s + 5); 5199 else 5200 s = cin_skipcomment(s + 6); 5201 } 5202 else 5203 { 5204 if (s[0] == '{' || s[0] == '}' || s[0] == ';') 5205 { 5206 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; 5207 } 5208 else if (s[0] == ')') 5209 { 5210 /* Constructor-initialization is assumed if we come across 5211 * something like "):" */ 5212 class_or_struct = FALSE; 5213 lookfor_ctor_init = TRUE; 5214 } 5215 else if (!vim_isIDc(s[0])) 5216 { 5217 /* if it is not an identifier, we are wrong */ 5218 class_or_struct = FALSE; 5219 lookfor_ctor_init = FALSE; 5220 } 5221 else if (*col == 0) 5222 { 5223 /* it can't be a constructor-initialization any more */ 5224 lookfor_ctor_init = FALSE; 5225 5226 /* the first statement starts here: lineup with this one... */ 5227 if (cpp_base_class && *col == 0) 5228 *col = (colnr_T)(s - line); 5229 } 5230 5231 s = cin_skipcomment(s + 1); 5232 } 5233 } 5234 5235 return cpp_base_class; 5236 } 5237 5238 /* 5239 * Return TRUE if string "s" ends with the string "find", possibly followed by 5240 * white space and comments. Skip strings and comments. 5241 * Ignore "ignore" after "find" if it's not NULL. 5242 */ 5243 static int 5244 cin_ends_in(s, find, ignore) 5245 char_u *s; 5246 char_u *find; 5247 char_u *ignore; 5248 { 5249 char_u *p = s; 5250 char_u *r; 5251 int len = (int)STRLEN(find); 5252 5253 while (*p != NUL) 5254 { 5255 p = cin_skipcomment(p); 5256 if (STRNCMP(p, find, len) == 0) 5257 { 5258 r = skipwhite(p + len); 5259 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) 5260 r = skipwhite(r + STRLEN(ignore)); 5261 if (cin_nocode(r)) 5262 return TRUE; 5263 } 5264 if (*p != NUL) 5265 ++p; 5266 } 5267 return FALSE; 5268 } 5269 5270 /* 5271 * Skip strings, chars and comments until at or past "trypos". 5272 * Return the column found. 5273 */ 5274 static int 5275 cin_skip2pos(trypos) 5276 pos_T *trypos; 5277 { 5278 char_u *line; 5279 char_u *p; 5280 5281 p = line = ml_get(trypos->lnum); 5282 while (*p && (colnr_T)(p - line) < trypos->col) 5283 { 5284 if (cin_iscomment(p)) 5285 p = cin_skipcomment(p); 5286 else 5287 { 5288 p = skip_string(p); 5289 ++p; 5290 } 5291 } 5292 return (int)(p - line); 5293 } 5294 5295 /* 5296 * Find the '{' at the start of the block we are in. 5297 * Return NULL if no match found. 5298 * Ignore a '{' that is in a comment, makes indenting the next three lines 5299 * work. */ 5300 /* foo() */ 5301 /* { */ 5302 /* } */ 5303 5304 static pos_T * 5305 find_start_brace(ind_maxcomment) /* XXX */ 5306 int ind_maxcomment; 5307 { 5308 pos_T cursor_save; 5309 pos_T *trypos; 5310 pos_T *pos; 5311 static pos_T pos_copy; 5312 5313 cursor_save = curwin->w_cursor; 5314 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) 5315 { 5316 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ 5317 trypos = &pos_copy; 5318 curwin->w_cursor = *trypos; 5319 pos = NULL; 5320 /* ignore the { if it's in a // comment */ 5321 if ((colnr_T)cin_skip2pos(trypos) == trypos->col 5322 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */ 5323 break; 5324 if (pos != NULL) 5325 curwin->w_cursor.lnum = pos->lnum; 5326 } 5327 curwin->w_cursor = cursor_save; 5328 return trypos; 5329 } 5330 5331 /* 5332 * Find the matching '(', failing if it is in a comment. 5333 * Return NULL of no match found. 5334 */ 5335 static pos_T * 5336 find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */ 5337 int ind_maxparen; 5338 int ind_maxcomment; 5339 { 5340 pos_T cursor_save; 5341 pos_T *trypos; 5342 static pos_T pos_copy; 5343 5344 cursor_save = curwin->w_cursor; 5345 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL) 5346 { 5347 /* check if the ( is in a // comment */ 5348 if ((colnr_T)cin_skip2pos(trypos) > trypos->col) 5349 trypos = NULL; 5350 else 5351 { 5352 pos_copy = *trypos; /* copy trypos, findmatch will change it */ 5353 trypos = &pos_copy; 5354 curwin->w_cursor = *trypos; 5355 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */ 5356 trypos = NULL; 5357 } 5358 } 5359 curwin->w_cursor = cursor_save; 5360 return trypos; 5361 } 5362 5363 /* 5364 * Return ind_maxparen corrected for the difference in line number between the 5365 * cursor position and "startpos". This makes sure that searching for a 5366 * matching paren above the cursor line doesn't find a match because of 5367 * looking a few lines further. 5368 */ 5369 static int 5370 corr_ind_maxparen(ind_maxparen, startpos) 5371 int ind_maxparen; 5372 pos_T *startpos; 5373 { 5374 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; 5375 5376 if (n > 0 && n < ind_maxparen / 2) 5377 return ind_maxparen - (int)n; 5378 return ind_maxparen; 5379 } 5380 5381 /* 5382 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in 5383 * line "l". 5384 */ 5385 static int 5386 find_last_paren(l, start, end) 5387 char_u *l; 5388 int start, end; 5389 { 5390 int i; 5391 int retval = FALSE; 5392 int open_count = 0; 5393 5394 curwin->w_cursor.col = 0; /* default is start of line */ 5395 5396 for (i = 0; l[i]; i++) 5397 { 5398 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ 5399 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ 5400 if (l[i] == start) 5401 ++open_count; 5402 else if (l[i] == end) 5403 { 5404 if (open_count > 0) 5405 --open_count; 5406 else 5407 { 5408 curwin->w_cursor.col = i; 5409 retval = TRUE; 5410 } 5411 } 5412 } 5413 return retval; 5414 } 5415 5416 int 5417 get_c_indent() 5418 { 5419 /* 5420 * spaces from a block's opening brace the prevailing indent for that 5421 * block should be 5422 */ 5423 int ind_level = curbuf->b_p_sw; 5424 5425 /* 5426 * spaces from the edge of the line an open brace that's at the end of a 5427 * line is imagined to be. 5428 */ 5429 int ind_open_imag = 0; 5430 5431 /* 5432 * spaces from the prevailing indent for a line that is not precededof by 5433 * an opening brace. 5434 */ 5435 int ind_no_brace = 0; 5436 5437 /* 5438 * column where the first { of a function should be located } 5439 */ 5440 int ind_first_open = 0; 5441 5442 /* 5443 * spaces from the prevailing indent a leftmost open brace should be 5444 * located 5445 */ 5446 int ind_open_extra = 0; 5447 5448 /* 5449 * spaces from the matching open brace (real location for one at the left 5450 * edge; imaginary location from one that ends a line) the matching close 5451 * brace should be located 5452 */ 5453 int ind_close_extra = 0; 5454 5455 /* 5456 * spaces from the edge of the line an open brace sitting in the leftmost 5457 * column is imagined to be 5458 */ 5459 int ind_open_left_imag = 0; 5460 5461 /* 5462 * spaces from the switch() indent a "case xx" label should be located 5463 */ 5464 int ind_case = curbuf->b_p_sw; 5465 5466 /* 5467 * spaces from the "case xx:" code after a switch() should be located 5468 */ 5469 int ind_case_code = curbuf->b_p_sw; 5470 5471 /* 5472 * lineup break at end of case in switch() with case label 5473 */ 5474 int ind_case_break = 0; 5475 5476 /* 5477 * spaces from the class declaration indent a scope declaration label 5478 * should be located 5479 */ 5480 int ind_scopedecl = curbuf->b_p_sw; 5481 5482 /* 5483 * spaces from the scope declaration label code should be located 5484 */ 5485 int ind_scopedecl_code = curbuf->b_p_sw; 5486 5487 /* 5488 * amount K&R-style parameters should be indented 5489 */ 5490 int ind_param = curbuf->b_p_sw; 5491 5492 /* 5493 * amount a function type spec should be indented 5494 */ 5495 int ind_func_type = curbuf->b_p_sw; 5496 5497 /* 5498 * amount a cpp base class declaration or constructor initialization 5499 * should be indented 5500 */ 5501 int ind_cpp_baseclass = curbuf->b_p_sw; 5502 5503 /* 5504 * additional spaces beyond the prevailing indent a continuation line 5505 * should be located 5506 */ 5507 int ind_continuation = curbuf->b_p_sw; 5508 5509 /* 5510 * spaces from the indent of the line with an unclosed parentheses 5511 */ 5512 int ind_unclosed = curbuf->b_p_sw * 2; 5513 5514 /* 5515 * spaces from the indent of the line with an unclosed parentheses, which 5516 * itself is also unclosed 5517 */ 5518 int ind_unclosed2 = curbuf->b_p_sw; 5519 5520 /* 5521 * suppress ignoring spaces from the indent of a line starting with an 5522 * unclosed parentheses. 5523 */ 5524 int ind_unclosed_noignore = 0; 5525 5526 /* 5527 * If the opening paren is the last nonwhite character on the line, and 5528 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer 5529 * context (for very long lines). 5530 */ 5531 int ind_unclosed_wrapped = 0; 5532 5533 /* 5534 * suppress ignoring white space when lining up with the character after 5535 * an unclosed parentheses. 5536 */ 5537 int ind_unclosed_whiteok = 0; 5538 5539 /* 5540 * indent a closing parentheses under the line start of the matching 5541 * opening parentheses. 5542 */ 5543 int ind_matching_paren = 0; 5544 5545 /* 5546 * Extra indent for comments. 5547 */ 5548 int ind_comment = 0; 5549 5550 /* 5551 * spaces from the comment opener when there is nothing after it. 5552 */ 5553 int ind_in_comment = 3; 5554 5555 /* 5556 * boolean: if non-zero, use ind_in_comment even if there is something 5557 * after the comment opener. 5558 */ 5559 int ind_in_comment2 = 0; 5560 5561 /* 5562 * max lines to search for an open paren 5563 */ 5564 int ind_maxparen = 20; 5565 5566 /* 5567 * max lines to search for an open comment 5568 */ 5569 int ind_maxcomment = 70; 5570 5571 /* 5572 * handle braces for java code 5573 */ 5574 int ind_java = 0; 5575 5576 /* 5577 * handle blocked cases correctly 5578 */ 5579 int ind_keep_case_label = 0; 5580 5581 pos_T cur_curpos; 5582 int amount; 5583 int scope_amount; 5584 int cur_amount; 5585 colnr_T col; 5586 char_u *theline; 5587 char_u *linecopy; 5588 pos_T *trypos; 5589 pos_T *tryposBrace = NULL; 5590 pos_T our_paren_pos; 5591 char_u *start; 5592 int start_brace; 5593 #define BRACE_IN_COL0 1 /* '{' is in comumn 0 */ 5594 #define BRACE_AT_START 2 /* '{' is at start of line */ 5595 #define BRACE_AT_END 3 /* '{' is at end of line */ 5596 linenr_T ourscope; 5597 char_u *l; 5598 char_u *look; 5599 char_u terminated; 5600 int lookfor; 5601 #define LOOKFOR_INITIAL 0 5602 #define LOOKFOR_IF 1 5603 #define LOOKFOR_DO 2 5604 #define LOOKFOR_CASE 3 5605 #define LOOKFOR_ANY 4 5606 #define LOOKFOR_TERM 5 5607 #define LOOKFOR_UNTERM 6 5608 #define LOOKFOR_SCOPEDECL 7 5609 #define LOOKFOR_NOBREAK 8 5610 #define LOOKFOR_CPP_BASECLASS 9 5611 #define LOOKFOR_ENUM_OR_INIT 10 5612 5613 int whilelevel; 5614 linenr_T lnum; 5615 char_u *options; 5616 int fraction = 0; /* init for GCC */ 5617 int divider; 5618 int n; 5619 int iscase; 5620 int lookfor_break; 5621 int cont_amount = 0; /* amount for continuation line */ 5622 5623 for (options = curbuf->b_p_cino; *options; ) 5624 { 5625 l = options++; 5626 if (*options == '-') 5627 ++options; 5628 n = getdigits(&options); 5629 divider = 0; 5630 if (*options == '.') /* ".5s" means a fraction */ 5631 { 5632 fraction = atol((char *)++options); 5633 while (VIM_ISDIGIT(*options)) 5634 { 5635 ++options; 5636 if (divider) 5637 divider *= 10; 5638 else 5639 divider = 10; 5640 } 5641 } 5642 if (*options == 's') /* "2s" means two times 'shiftwidth' */ 5643 { 5644 if (n == 0 && fraction == 0) 5645 n = curbuf->b_p_sw; /* just "s" is one 'shiftwidth' */ 5646 else 5647 { 5648 n *= curbuf->b_p_sw; 5649 if (divider) 5650 n += (curbuf->b_p_sw * fraction + divider / 2) / divider; 5651 } 5652 ++options; 5653 } 5654 if (l[1] == '-') 5655 n = -n; 5656 /* When adding an entry here, also update the default 'cinoptions' in 5657 * change.txt, and add explanation for it! */ 5658 switch (*l) 5659 { 5660 case '>': ind_level = n; break; 5661 case 'e': ind_open_imag = n; break; 5662 case 'n': ind_no_brace = n; break; 5663 case 'f': ind_first_open = n; break; 5664 case '{': ind_open_extra = n; break; 5665 case '}': ind_close_extra = n; break; 5666 case '^': ind_open_left_imag = n; break; 5667 case ':': ind_case = n; break; 5668 case '=': ind_case_code = n; break; 5669 case 'b': ind_case_break = n; break; 5670 case 'p': ind_param = n; break; 5671 case 't': ind_func_type = n; break; 5672 case '/': ind_comment = n; break; 5673 case 'c': ind_in_comment = n; break; 5674 case 'C': ind_in_comment2 = n; break; 5675 case 'i': ind_cpp_baseclass = n; break; 5676 case '+': ind_continuation = n; break; 5677 case '(': ind_unclosed = n; break; 5678 case 'u': ind_unclosed2 = n; break; 5679 case 'U': ind_unclosed_noignore = n; break; 5680 case 'W': ind_unclosed_wrapped = n; break; 5681 case 'w': ind_unclosed_whiteok = n; break; 5682 case 'm': ind_matching_paren = n; break; 5683 case ')': ind_maxparen = n; break; 5684 case '*': ind_maxcomment = n; break; 5685 case 'g': ind_scopedecl = n; break; 5686 case 'h': ind_scopedecl_code = n; break; 5687 case 'j': ind_java = n; break; 5688 case 'l': ind_keep_case_label = n; break; 5689 } 5690 } 5691 5692 /* remember where the cursor was when we started */ 5693 cur_curpos = curwin->w_cursor; 5694 5695 /* Get a copy of the current contents of the line. 5696 * This is required, because only the most recent line obtained with 5697 * ml_get is valid! */ 5698 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); 5699 if (linecopy == NULL) 5700 return 0; 5701 5702 /* 5703 * In insert mode and the cursor is on a ')' truncate the line at the 5704 * cursor position. We don't want to line up with the matching '(' when 5705 * inserting new stuff. 5706 * For unknown reasons the cursor might be past the end of the line, thus 5707 * check for that. 5708 */ 5709 if ((State & INSERT) 5710 && curwin->w_cursor.col < STRLEN(linecopy) 5711 && linecopy[curwin->w_cursor.col] == ')') 5712 linecopy[curwin->w_cursor.col] = NUL; 5713 5714 theline = skipwhite(linecopy); 5715 5716 /* move the cursor to the start of the line */ 5717 5718 curwin->w_cursor.col = 0; 5719 5720 /* 5721 * #defines and so on always go at the left when included in 'cinkeys'. 5722 */ 5723 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) 5724 { 5725 amount = 0; 5726 } 5727 5728 /* 5729 * Is it a non-case label? Then that goes at the left margin too. 5730 */ 5731 else if (cin_islabel(ind_maxcomment)) /* XXX */ 5732 { 5733 amount = 0; 5734 } 5735 5736 /* 5737 * If we're inside a "//" comment and there is a "//" comment in a 5738 * previous line, lineup with that one. 5739 */ 5740 else if (cin_islinecomment(theline) 5741 && (trypos = find_line_comment()) != NULL) /* XXX */ 5742 { 5743 /* find how indented the line beginning the comment is */ 5744 getvcol(curwin, trypos, &col, NULL, NULL); 5745 amount = col; 5746 } 5747 5748 /* 5749 * If we're inside a comment and not looking at the start of the 5750 * comment, try using the 'comments' option. 5751 */ 5752 else if (!cin_iscomment(theline) 5753 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ 5754 { 5755 int lead_start_len = 2; 5756 int lead_middle_len = 1; 5757 char_u lead_start[COM_MAX_LEN]; /* start-comment string */ 5758 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ 5759 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ 5760 char_u *p; 5761 int start_align = 0; 5762 int start_off = 0; 5763 int done = FALSE; 5764 5765 /* find how indented the line beginning the comment is */ 5766 getvcol(curwin, trypos, &col, NULL, NULL); 5767 amount = col; 5768 5769 p = curbuf->b_p_com; 5770 while (*p != NUL) 5771 { 5772 int align = 0; 5773 int off = 0; 5774 int what = 0; 5775 5776 while (*p != NUL && *p != ':') 5777 { 5778 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) 5779 what = *p++; 5780 else if (*p == COM_LEFT || *p == COM_RIGHT) 5781 align = *p++; 5782 else if (VIM_ISDIGIT(*p) || *p == '-') 5783 off = getdigits(&p); 5784 else 5785 ++p; 5786 } 5787 5788 if (*p == ':') 5789 ++p; 5790 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); 5791 if (what == COM_START) 5792 { 5793 STRCPY(lead_start, lead_end); 5794 lead_start_len = (int)STRLEN(lead_start); 5795 start_off = off; 5796 start_align = align; 5797 } 5798 else if (what == COM_MIDDLE) 5799 { 5800 STRCPY(lead_middle, lead_end); 5801 lead_middle_len = (int)STRLEN(lead_middle); 5802 } 5803 else if (what == COM_END) 5804 { 5805 /* If our line starts with the middle comment string, line it 5806 * up with the comment opener per the 'comments' option. */ 5807 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 5808 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) 5809 { 5810 done = TRUE; 5811 if (curwin->w_cursor.lnum > 1) 5812 { 5813 /* If the start comment string matches in the previous 5814 * line, use the indent of that line pluss offset. If 5815 * the middle comment string matches in the previous 5816 * line, use the indent of that line. XXX */ 5817 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); 5818 if (STRNCMP(look, lead_start, lead_start_len) == 0) 5819 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); 5820 else if (STRNCMP(look, lead_middle, 5821 lead_middle_len) == 0) 5822 { 5823 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); 5824 break; 5825 } 5826 /* If the start comment string doesn't match with the 5827 * start of the comment, skip this entry. XXX */ 5828 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col, 5829 lead_start, lead_start_len) != 0) 5830 continue; 5831 } 5832 if (start_off != 0) 5833 amount += start_off; 5834 else if (start_align == COM_RIGHT) 5835 amount += vim_strsize(lead_start) 5836 - vim_strsize(lead_middle); 5837 break; 5838 } 5839 5840 /* If our line starts with the end comment string, line it up 5841 * with the middle comment */ 5842 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 5843 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) 5844 { 5845 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); 5846 /* XXX */ 5847 if (off != 0) 5848 amount += off; 5849 else if (align == COM_RIGHT) 5850 amount += vim_strsize(lead_start) 5851 - vim_strsize(lead_middle); 5852 done = TRUE; 5853 break; 5854 } 5855 } 5856 } 5857 5858 /* If our line starts with an asterisk, line up with the 5859 * asterisk in the comment opener; otherwise, line up 5860 * with the first character of the comment text. 5861 */ 5862 if (done) 5863 ; 5864 else if (theline[0] == '*') 5865 amount += 1; 5866 else 5867 { 5868 /* 5869 * If we are more than one line away from the comment opener, take 5870 * the indent of the previous non-empty line. If 'cino' has "CO" 5871 * and we are just below the comment opener and there are any 5872 * white characters after it line up with the text after it; 5873 * otherwise, add the amount specified by "c" in 'cino' 5874 */ 5875 amount = -1; 5876 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum) 5877 { 5878 if (linewhite(lnum)) /* skip blank lines */ 5879 continue; 5880 amount = get_indent_lnum(lnum); /* XXX */ 5881 break; 5882 } 5883 if (amount == -1) /* use the comment opener */ 5884 { 5885 if (!ind_in_comment2) 5886 { 5887 start = ml_get(trypos->lnum); 5888 look = start + trypos->col + 2; /* skip / and * */ 5889 if (*look != NUL) /* if something after it */ 5890 trypos->col = (colnr_T)(skipwhite(look) - start); 5891 } 5892 getvcol(curwin, trypos, &col, NULL, NULL); 5893 amount = col; 5894 if (ind_in_comment2 || *look == NUL) 5895 amount += ind_in_comment; 5896 } 5897 } 5898 } 5899 5900 /* 5901 * Are we inside parentheses or braces? 5902 */ /* XXX */ 5903 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL 5904 && ind_java == 0) 5905 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL 5906 || trypos != NULL) 5907 { 5908 if (trypos != NULL && tryposBrace != NULL) 5909 { 5910 /* Both an unmatched '(' and '{' is found. Use the one which is 5911 * closer to the current cursor position, set the other to NULL. */ 5912 if (trypos->lnum != tryposBrace->lnum 5913 ? trypos->lnum < tryposBrace->lnum 5914 : trypos->col < tryposBrace->col) 5915 trypos = NULL; 5916 else 5917 tryposBrace = NULL; 5918 } 5919 5920 if (trypos != NULL) 5921 { 5922 /* 5923 * If the matching paren is more than one line away, use the indent of 5924 * a previous non-empty line that matches the same paren. 5925 */ 5926 amount = -1; 5927 cur_amount = MAXCOL; 5928 our_paren_pos = *trypos; 5929 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) 5930 { 5931 l = skipwhite(ml_get(lnum)); 5932 if (cin_nocode(l)) /* skip comment lines */ 5933 continue; 5934 if (cin_ispreproc_cont(&l, &lnum)) /* ignore #defines, #if, etc. */ 5935 continue; 5936 curwin->w_cursor.lnum = lnum; 5937 5938 /* Skip a comment. XXX */ 5939 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) 5940 { 5941 lnum = trypos->lnum + 1; 5942 continue; 5943 } 5944 5945 /* XXX */ 5946 if ((trypos = find_match_paren( 5947 corr_ind_maxparen(ind_maxparen, &cur_curpos), 5948 ind_maxcomment)) != NULL 5949 && trypos->lnum == our_paren_pos.lnum 5950 && trypos->col == our_paren_pos.col) 5951 { 5952 amount = get_indent_lnum(lnum); /* XXX */ 5953 5954 if (theline[0] == ')') 5955 { 5956 if (our_paren_pos.lnum != lnum && cur_amount > amount) 5957 cur_amount = amount; 5958 amount = -1; 5959 } 5960 break; 5961 } 5962 } 5963 5964 /* 5965 * Line up with line where the matching paren is. XXX 5966 * If the line starts with a '(' or the indent for unclosed 5967 * parentheses is zero, line up with the unclosed parentheses. 5968 */ 5969 if (amount == -1) 5970 { 5971 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment); 5972 if (theline[0] == ')' || ind_unclosed == 0 5973 || (!ind_unclosed_noignore && *skipwhite(look) == '(')) 5974 { 5975 /* 5976 * If we're looking at a close paren, line up right there; 5977 * otherwise, line up with the next (non-white) character. 5978 * When ind_unclosed_wrapped is set and the matching paren is 5979 * the last nonwhite character of the line, use either the 5980 * indent of the current line or the indentation of the next 5981 * outer paren and add ind_unclosed_wrapped (for very long 5982 * lines). 5983 */ 5984 if (theline[0] != ')') 5985 { 5986 cur_amount = MAXCOL; 5987 l = ml_get(our_paren_pos.lnum); 5988 if (ind_unclosed_wrapped 5989 && cin_ends_in(l, (char_u *)"(", NULL)) 5990 { 5991 /* look for opening unmatched paren, indent one level 5992 * for each additional level */ 5993 n = 1; 5994 for (col = 0; col < our_paren_pos.col; ++col) 5995 { 5996 switch (l[col]) 5997 { 5998 case '(': 5999 case '{': ++n; 6000 break; 6001 6002 case ')': 6003 case '}': if (n > 1) 6004 --n; 6005 break; 6006 } 6007 } 6008 6009 our_paren_pos.col = 0; 6010 amount += n * ind_unclosed_wrapped; 6011 } 6012 else if (ind_unclosed_whiteok) 6013 our_paren_pos.col++; 6014 else 6015 { 6016 col = our_paren_pos.col + 1; 6017 while (vim_iswhite(l[col])) 6018 col++; 6019 if (l[col] != NUL) /* In case of trailing space */ 6020 our_paren_pos.col = col; 6021 else 6022 our_paren_pos.col++; 6023 } 6024 } 6025 6026 /* 6027 * Find how indented the paren is, or the character after it 6028 * if we did the above "if". 6029 */ 6030 if (our_paren_pos.col > 0) 6031 { 6032 getvcol(curwin, &our_paren_pos, &col, NULL, NULL); 6033 if (cur_amount > (int)col) 6034 cur_amount = col; 6035 } 6036 } 6037 6038 if (theline[0] == ')' && ind_matching_paren) 6039 { 6040 /* Line up with the start of the matching paren line. */ 6041 } 6042 else if (ind_unclosed == 0 || (!ind_unclosed_noignore 6043 && *skipwhite(look) == '(')) 6044 { 6045 if (cur_amount != MAXCOL) 6046 amount = cur_amount; 6047 } 6048 else 6049 { 6050 /* add ind_unclosed2 for each '(' before our matching one */ 6051 col = our_paren_pos.col; 6052 while (our_paren_pos.col > 0) 6053 { 6054 --our_paren_pos.col; 6055 switch (*ml_get_pos(&our_paren_pos)) 6056 { 6057 case '(': amount += ind_unclosed2; 6058 col = our_paren_pos.col; 6059 break; 6060 case ')': amount -= ind_unclosed2; 6061 col = MAXCOL; 6062 break; 6063 } 6064 } 6065 6066 /* Use ind_unclosed once, when the first '(' is not inside 6067 * braces */ 6068 if (col == MAXCOL) 6069 amount += ind_unclosed; 6070 else 6071 { 6072 curwin->w_cursor.lnum = our_paren_pos.lnum; 6073 curwin->w_cursor.col = col; 6074 if ((trypos = find_match_paren(ind_maxparen, 6075 ind_maxcomment)) != NULL) 6076 amount += ind_unclosed2; 6077 else 6078 amount += ind_unclosed; 6079 } 6080 /* 6081 * For a line starting with ')' use the minimum of the two 6082 * positions, to avoid giving it more indent than the previous 6083 * lines: 6084 * func_long_name( if (x 6085 * arg && yy 6086 * ) ^ not here ) ^ not here 6087 */ 6088 if (cur_amount < amount) 6089 amount = cur_amount; 6090 } 6091 } 6092 6093 /* add extra indent for a comment */ 6094 if (cin_iscomment(theline)) 6095 amount += ind_comment; 6096 } 6097 6098 /* 6099 * Are we at least inside braces, then? 6100 */ 6101 else 6102 { 6103 trypos = tryposBrace; 6104 6105 ourscope = trypos->lnum; 6106 start = ml_get(ourscope); 6107 6108 /* 6109 * Now figure out how indented the line is in general. 6110 * If the brace was at the start of the line, we use that; 6111 * otherwise, check out the indentation of the line as 6112 * a whole and then add the "imaginary indent" to that. 6113 */ 6114 look = skipwhite(start); 6115 if (*look == '{') 6116 { 6117 getvcol(curwin, trypos, &col, NULL, NULL); 6118 amount = col; 6119 if (*start == '{') 6120 start_brace = BRACE_IN_COL0; 6121 else 6122 start_brace = BRACE_AT_START; 6123 } 6124 else 6125 { 6126 /* 6127 * that opening brace might have been on a continuation 6128 * line. if so, find the start of the line. 6129 */ 6130 curwin->w_cursor.lnum = ourscope; 6131 6132 /* 6133 * position the cursor over the rightmost paren, so that 6134 * matching it will take us back to the start of the line. 6135 */ 6136 lnum = ourscope; 6137 if (find_last_paren(start, '(', ')') 6138 && (trypos = find_match_paren(ind_maxparen, 6139 ind_maxcomment)) != NULL) 6140 lnum = trypos->lnum; 6141 6142 /* 6143 * It could have been something like 6144 * case 1: if (asdf && 6145 * ldfd) { 6146 * } 6147 */ 6148 if (ind_keep_case_label && cin_iscase(skipwhite(ml_get_curline()))) 6149 amount = get_indent(); 6150 else 6151 amount = skip_label(lnum, &l, ind_maxcomment); 6152 6153 start_brace = BRACE_AT_END; 6154 } 6155 6156 /* 6157 * if we're looking at a closing brace, that's where 6158 * we want to be. otherwise, add the amount of room 6159 * that an indent is supposed to be. 6160 */ 6161 if (theline[0] == '}') 6162 { 6163 /* 6164 * they may want closing braces to line up with something 6165 * other than the open brace. indulge them, if so. 6166 */ 6167 amount += ind_close_extra; 6168 } 6169 else 6170 { 6171 /* 6172 * If we're looking at an "else", try to find an "if" 6173 * to match it with. 6174 * If we're looking at a "while", try to find a "do" 6175 * to match it with. 6176 */ 6177 lookfor = LOOKFOR_INITIAL; 6178 if (cin_iselse(theline)) 6179 lookfor = LOOKFOR_IF; 6180 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen)) 6181 /* XXX */ 6182 lookfor = LOOKFOR_DO; 6183 if (lookfor != LOOKFOR_INITIAL) 6184 { 6185 curwin->w_cursor.lnum = cur_curpos.lnum; 6186 if (find_match(lookfor, ourscope, ind_maxparen, 6187 ind_maxcomment) == OK) 6188 { 6189 amount = get_indent(); /* XXX */ 6190 goto theend; 6191 } 6192 } 6193 6194 /* 6195 * We get here if we are not on an "while-of-do" or "else" (or 6196 * failed to find a matching "if"). 6197 * Search backwards for something to line up with. 6198 * First set amount for when we don't find anything. 6199 */ 6200 6201 /* 6202 * if the '{' is _really_ at the left margin, use the imaginary 6203 * location of a left-margin brace. Otherwise, correct the 6204 * location for ind_open_extra. 6205 */ 6206 6207 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ 6208 { 6209 amount = ind_open_left_imag; 6210 } 6211 else 6212 { 6213 if (start_brace == BRACE_AT_END) /* '{' is at end of line */ 6214 amount += ind_open_imag; 6215 else 6216 { 6217 /* Compensate for adding ind_open_extra later. */ 6218 amount -= ind_open_extra; 6219 if (amount < 0) 6220 amount = 0; 6221 } 6222 } 6223 6224 lookfor_break = FALSE; 6225 6226 if (cin_iscase(theline)) /* it's a switch() label */ 6227 { 6228 lookfor = LOOKFOR_CASE; /* find a previous switch() label */ 6229 amount += ind_case; 6230 } 6231 else if (cin_isscopedecl(theline)) /* private:, ... */ 6232 { 6233 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ 6234 amount += ind_scopedecl; 6235 } 6236 else 6237 { 6238 if (ind_case_break && cin_isbreak(theline)) /* break; ... */ 6239 lookfor_break = TRUE; 6240 6241 lookfor = LOOKFOR_INITIAL; 6242 amount += ind_level; /* ind_level from start of block */ 6243 } 6244 scope_amount = amount; 6245 whilelevel = 0; 6246 6247 /* 6248 * Search backwards. If we find something we recognize, line up 6249 * with that. 6250 * 6251 * if we're looking at an open brace, indent 6252 * the usual amount relative to the conditional 6253 * that opens the block. 6254 */ 6255 curwin->w_cursor = cur_curpos; 6256 for (;;) 6257 { 6258 curwin->w_cursor.lnum--; 6259 curwin->w_cursor.col = 0; 6260 6261 /* 6262 * If we went all the way back to the start of our scope, line 6263 * up with it. 6264 */ 6265 if (curwin->w_cursor.lnum <= ourscope) 6266 { 6267 /* we reached end of scope: 6268 * if looking for a enum or structure initialization 6269 * go further back: 6270 * if it is an initializer (enum xxx or xxx =), then 6271 * don't add ind_continuation, otherwise it is a variable 6272 * declaration: 6273 * int x, 6274 * here; <-- add ind_continuation 6275 */ 6276 if (lookfor == LOOKFOR_ENUM_OR_INIT) 6277 { 6278 if (curwin->w_cursor.lnum == 0 6279 || curwin->w_cursor.lnum 6280 < ourscope - ind_maxparen) 6281 { 6282 /* nothing found (abuse ind_maxparen as limit) 6283 * assume terminated line (i.e. a variable 6284 * initialization) */ 6285 if (cont_amount > 0) 6286 amount = cont_amount; 6287 else 6288 amount += ind_continuation; 6289 break; 6290 } 6291 6292 l = ml_get_curline(); 6293 6294 /* 6295 * If we're in a comment now, skip to the start of the 6296 * comment. 6297 */ 6298 trypos = find_start_comment(ind_maxcomment); 6299 if (trypos != NULL) 6300 { 6301 curwin->w_cursor.lnum = trypos->lnum + 1; 6302 continue; 6303 } 6304 6305 /* 6306 * Skip preprocessor directives and blank lines. 6307 */ 6308 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) 6309 continue; 6310 6311 if (cin_nocode(l)) 6312 continue; 6313 6314 terminated = cin_isterminated(l, FALSE, TRUE); 6315 6316 /* 6317 * If we are at top level and the line looks like a 6318 * function declaration, we are done 6319 * (it's a variable declaration). 6320 */ 6321 if (start_brace != BRACE_IN_COL0 6322 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum)) 6323 { 6324 /* if the line is terminated with another ',' 6325 * it is a continued variable initialization. 6326 * don't add extra indent. 6327 * TODO: does not work, if a function 6328 * declaration is split over multiple lines: 6329 * cin_isfuncdecl returns FALSE then. 6330 */ 6331 if (terminated == ',') 6332 break; 6333 6334 /* if it es a enum declaration or an assignment, 6335 * we are done. 6336 */ 6337 if (terminated != ';' && cin_isinit()) 6338 break; 6339 6340 /* nothing useful found */ 6341 if (terminated == 0 || terminated == '{') 6342 continue; 6343 } 6344 6345 if (terminated != ';') 6346 { 6347 /* Skip parens and braces. Position the cursor 6348 * over the rightmost paren, so that matching it 6349 * will take us back to the start of the line. 6350 */ /* XXX */ 6351 trypos = NULL; 6352 if (find_last_paren(l, '(', ')')) 6353 trypos = find_match_paren(ind_maxparen, 6354 ind_maxcomment); 6355 6356 if (trypos == NULL && find_last_paren(l, '{', '}')) 6357 trypos = find_start_brace(ind_maxcomment); 6358 6359 if (trypos != NULL) 6360 { 6361 curwin->w_cursor.lnum = trypos->lnum + 1; 6362 continue; 6363 } 6364 } 6365 6366 /* it's a variable declaration, add indentation 6367 * like in 6368 * int a, 6369 * b; 6370 */ 6371 if (cont_amount > 0) 6372 amount = cont_amount; 6373 else 6374 amount += ind_continuation; 6375 } 6376 else if (lookfor == LOOKFOR_UNTERM) 6377 { 6378 if (cont_amount > 0) 6379 amount = cont_amount; 6380 else 6381 amount += ind_continuation; 6382 } 6383 else if (lookfor != LOOKFOR_TERM 6384 && lookfor != LOOKFOR_CPP_BASECLASS) 6385 { 6386 amount = scope_amount; 6387 if (theline[0] == '{') 6388 amount += ind_open_extra; 6389 } 6390 break; 6391 } 6392 6393 /* 6394 * If we're in a comment now, skip to the start of the comment. 6395 */ /* XXX */ 6396 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) 6397 { 6398 curwin->w_cursor.lnum = trypos->lnum + 1; 6399 continue; 6400 } 6401 6402 l = ml_get_curline(); 6403 6404 /* 6405 * If this is a switch() label, may line up relative to that. 6406 * if this is a C++ scope declaration, do the same. 6407 */ 6408 iscase = cin_iscase(l); 6409 if (iscase || cin_isscopedecl(l)) 6410 { 6411 /* we are only looking for cpp base class 6412 * declaration/initialization any longer */ 6413 if (lookfor == LOOKFOR_CPP_BASECLASS) 6414 break; 6415 6416 /* When looking for a "do" we are not interested in 6417 * labels. */ 6418 if (whilelevel > 0) 6419 continue; 6420 6421 /* 6422 * case xx: 6423 * c = 99 + <- this indent plus continuation 6424 *-> here; 6425 */ 6426 if (lookfor == LOOKFOR_UNTERM 6427 || lookfor == LOOKFOR_ENUM_OR_INIT) 6428 { 6429 if (cont_amount > 0) 6430 amount = cont_amount; 6431 else 6432 amount += ind_continuation; 6433 break; 6434 } 6435 6436 /* 6437 * case xx: <- line up with this case 6438 * x = 333; 6439 * case yy: 6440 */ 6441 if ( (iscase && lookfor == LOOKFOR_CASE) 6442 || (iscase && lookfor_break) 6443 || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) 6444 { 6445 /* 6446 * Check that this case label is not for another 6447 * switch() 6448 */ /* XXX */ 6449 if ((trypos = find_start_brace(ind_maxcomment)) == 6450 NULL || trypos->lnum == ourscope) 6451 { 6452 amount = get_indent(); /* XXX */ 6453 break; 6454 } 6455 continue; 6456 } 6457 6458 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ 6459 6460 /* 6461 * case xx: if (cond) <- line up with this if 6462 * y = y + 1; 6463 * -> s = 99; 6464 * 6465 * case xx: 6466 * if (cond) <- line up with this line 6467 * y = y + 1; 6468 * -> s = 99; 6469 */ 6470 if (lookfor == LOOKFOR_TERM) 6471 { 6472 if (n) 6473 amount = n; 6474 6475 if (!lookfor_break) 6476 break; 6477 } 6478 6479 /* 6480 * case xx: x = x + 1; <- line up with this x 6481 * -> y = y + 1; 6482 * 6483 * case xx: if (cond) <- line up with this if 6484 * -> y = y + 1; 6485 */ 6486 if (n) 6487 { 6488 amount = n; 6489 l = after_label(ml_get_curline()); 6490 if (l != NULL && cin_is_cinword(l)) 6491 amount += ind_level + ind_no_brace; 6492 break; 6493 } 6494 6495 /* 6496 * Try to get the indent of a statement before the switch 6497 * label. If nothing is found, line up relative to the 6498 * switch label. 6499 * break; <- may line up with this line 6500 * case xx: 6501 * -> y = 1; 6502 */ 6503 scope_amount = get_indent() + (iscase /* XXX */ 6504 ? ind_case_code : ind_scopedecl_code); 6505 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY; 6506 continue; 6507 } 6508 6509 /* 6510 * Looking for a switch() label or C++ scope declaration, 6511 * ignore other lines, skip {}-blocks. 6512 */ 6513 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) 6514 { 6515 if (find_last_paren(l, '{', '}') && (trypos = 6516 find_start_brace(ind_maxcomment)) != NULL) 6517 curwin->w_cursor.lnum = trypos->lnum + 1; 6518 continue; 6519 } 6520 6521 /* 6522 * Ignore jump labels with nothing after them. 6523 */ 6524 if (cin_islabel(ind_maxcomment)) 6525 { 6526 l = after_label(ml_get_curline()); 6527 if (l == NULL || cin_nocode(l)) 6528 continue; 6529 } 6530 6531 /* 6532 * Ignore #defines, #if, etc. 6533 * Ignore comment and empty lines. 6534 * (need to get the line again, cin_islabel() may have 6535 * unlocked it) 6536 */ 6537 l = ml_get_curline(); 6538 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum) 6539 || cin_nocode(l)) 6540 continue; 6541 6542 /* 6543 * Are we at the start of a cpp base class declaration or 6544 * constructor initialization? 6545 */ /* XXX */ 6546 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass 6547 && cin_is_cpp_baseclass(l, &col)) 6548 { 6549 if (lookfor == LOOKFOR_UNTERM) 6550 { 6551 if (cont_amount > 0) 6552 amount = cont_amount; 6553 else 6554 amount += ind_continuation; 6555 } 6556 else if (col == 0 || theline[0] == '{') 6557 { 6558 amount = get_indent(); 6559 if (find_last_paren(l, '(', ')') 6560 && (trypos = find_match_paren(ind_maxparen, 6561 ind_maxcomment)) != NULL) 6562 amount = get_indent_lnum(trypos->lnum); /* XXX */ 6563 if (theline[0] != '{') 6564 amount += ind_cpp_baseclass; 6565 } 6566 else 6567 { 6568 curwin->w_cursor.col = col; 6569 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL); 6570 amount = (int)col; 6571 } 6572 break; 6573 } 6574 else if (lookfor == LOOKFOR_CPP_BASECLASS) 6575 { 6576 /* only look, whether there is a cpp base class 6577 * declaration or initialization before the opening brace. */ 6578 if (cin_isterminated(l, TRUE, FALSE)) 6579 break; 6580 else 6581 continue; 6582 } 6583 6584 /* 6585 * What happens next depends on the line being terminated. 6586 * If terminated with a ',' only consider it terminating if 6587 * there is anoter unterminated statement behind, eg: 6588 * 123, 6589 * sizeof 6590 * here 6591 * Otherwise check whether it is a enumeration or structure 6592 * initialisation (not indented) or a variable declaration 6593 * (indented). 6594 */ 6595 terminated = cin_isterminated(l, FALSE, TRUE); 6596 6597 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM 6598 && terminated == ',')) 6599 { 6600 /* 6601 * if we're in the middle of a paren thing, 6602 * go back to the line that starts it so 6603 * we can get the right prevailing indent 6604 * if ( foo && 6605 * bar ) 6606 */ 6607 /* 6608 * position the cursor over the rightmost paren, so that 6609 * matching it will take us back to the start of the line. 6610 */ 6611 (void)find_last_paren(l, '(', ')'); 6612 trypos = find_match_paren( 6613 corr_ind_maxparen(ind_maxparen, &cur_curpos), 6614 ind_maxcomment); 6615 6616 /* 6617 * If we are looking for ',', we also look for matching 6618 * braces. 6619 */ 6620 if (trypos == NULL && find_last_paren(l, '{', '}')) 6621 trypos = find_start_brace(ind_maxcomment); 6622 6623 if (trypos != NULL) 6624 { 6625 /* 6626 * Check if we are on a case label now. This is 6627 * handled above. 6628 * case xx: if ( asdf && 6629 * asdf) 6630 */ 6631 curwin->w_cursor.lnum = trypos->lnum; 6632 l = ml_get_curline(); 6633 if (cin_iscase(l) || cin_isscopedecl(l)) 6634 { 6635 ++curwin->w_cursor.lnum; 6636 continue; 6637 } 6638 } 6639 6640 /* 6641 * Skip over continuation lines to find the one to get the 6642 * indent from 6643 * char *usethis = "bla\ 6644 * bla", 6645 * here; 6646 */ 6647 if (terminated == ',') 6648 { 6649 while (curwin->w_cursor.lnum > 1) 6650 { 6651 l = ml_get(curwin->w_cursor.lnum - 1); 6652 if (*l == NUL || l[STRLEN(l) - 1] != '\\') 6653 break; 6654 --curwin->w_cursor.lnum; 6655 } 6656 } 6657 6658 /* 6659 * Get indent and pointer to text for current line, 6660 * ignoring any jump label. XXX 6661 */ 6662 cur_amount = skip_label(curwin->w_cursor.lnum, 6663 &l, ind_maxcomment); 6664 6665 /* 6666 * If this is just above the line we are indenting, and it 6667 * starts with a '{', line it up with this line. 6668 * while (not) 6669 * -> { 6670 * } 6671 */ 6672 if (terminated != ',' && lookfor != LOOKFOR_TERM 6673 && theline[0] == '{') 6674 { 6675 amount = cur_amount; 6676 /* 6677 * Only add ind_open_extra when the current line 6678 * doesn't start with a '{', which must have a match 6679 * in the same line (scope is the same). Probably: 6680 * { 1, 2 }, 6681 * -> { 3, 4 } 6682 */ 6683 if (*skipwhite(l) != '{') 6684 amount += ind_open_extra; 6685 6686 if (ind_cpp_baseclass) 6687 { 6688 /* have to look back, whether it is a cpp base 6689 * class declaration or initialization */ 6690 lookfor = LOOKFOR_CPP_BASECLASS; 6691 continue; 6692 } 6693 break; 6694 } 6695 6696 /* 6697 * Check if we are after an "if", "while", etc. 6698 * Also allow " } else". 6699 */ 6700 if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) 6701 { 6702 /* 6703 * Found an unterminated line after an if (), line up 6704 * with the last one. 6705 * if (cond) 6706 * 100 + 6707 * -> here; 6708 */ 6709 if (lookfor == LOOKFOR_UNTERM 6710 || lookfor == LOOKFOR_ENUM_OR_INIT) 6711 { 6712 if (cont_amount > 0) 6713 amount = cont_amount; 6714 else 6715 amount += ind_continuation; 6716 break; 6717 } 6718 6719 /* 6720 * If this is just above the line we are indenting, we 6721 * are finished. 6722 * while (not) 6723 * -> here; 6724 * Otherwise this indent can be used when the line 6725 * before this is terminated. 6726 * yyy; 6727 * if (stat) 6728 * while (not) 6729 * xxx; 6730 * -> here; 6731 */ 6732 amount = cur_amount; 6733 if (theline[0] == '{') 6734 amount += ind_open_extra; 6735 if (lookfor != LOOKFOR_TERM) 6736 { 6737 amount += ind_level + ind_no_brace; 6738 break; 6739 } 6740 6741 /* 6742 * Special trick: when expecting the while () after a 6743 * do, line up with the while() 6744 * do 6745 * x = 1; 6746 * -> here 6747 */ 6748 l = skipwhite(ml_get_curline()); 6749 if (cin_isdo(l)) 6750 { 6751 if (whilelevel == 0) 6752 break; 6753 --whilelevel; 6754 } 6755 6756 /* 6757 * When searching for a terminated line, don't use the 6758 * one between the "if" and the "else". 6759 * Need to use the scope of this "else". XXX 6760 * If whilelevel != 0 continue looking for a "do {". 6761 */ 6762 if (cin_iselse(l) 6763 && whilelevel == 0 6764 && ((trypos = find_start_brace(ind_maxcomment)) 6765 == NULL 6766 || find_match(LOOKFOR_IF, trypos->lnum, 6767 ind_maxparen, ind_maxcomment) == FAIL)) 6768 break; 6769 } 6770 6771 /* 6772 * If we're below an unterminated line that is not an 6773 * "if" or something, we may line up with this line or 6774 * add someting for a continuation line, depending on 6775 * the line before this one. 6776 */ 6777 else 6778 { 6779 /* 6780 * Found two unterminated lines on a row, line up with 6781 * the last one. 6782 * c = 99 + 6783 * 100 + 6784 * -> here; 6785 */ 6786 if (lookfor == LOOKFOR_UNTERM) 6787 { 6788 /* When line ends in a comma add extra indent */ 6789 if (terminated == ',') 6790 amount += ind_continuation; 6791 break; 6792 } 6793 6794 if (lookfor == LOOKFOR_ENUM_OR_INIT) 6795 { 6796 /* Found two lines ending in ',', lineup with the 6797 * lowest one, but check for cpp base class 6798 * declaration/initialization, if it is an 6799 * opening brace or we are looking just for 6800 * enumerations/initializations. */ 6801 if (terminated == ',') 6802 { 6803 if (ind_cpp_baseclass == 0) 6804 break; 6805 6806 lookfor = LOOKFOR_CPP_BASECLASS; 6807 continue; 6808 } 6809 6810 /* Ignore unterminated lines in between, but 6811 * reduce indent. */ 6812 if (amount > cur_amount) 6813 amount = cur_amount; 6814 } 6815 else 6816 { 6817 /* 6818 * Found first unterminated line on a row, may 6819 * line up with this line, remember its indent 6820 * 100 + 6821 * -> here; 6822 */ 6823 amount = cur_amount; 6824 6825 /* 6826 * If previous line ends in ',', check whether we 6827 * are in an initialization or enum 6828 * struct xxx = 6829 * { 6830 * sizeof a, 6831 * 124 }; 6832 * or a normal possible continuation line. 6833 * but only, of no other statement has been found 6834 * yet. 6835 */ 6836 if (lookfor == LOOKFOR_INITIAL && terminated == ',') 6837 { 6838 lookfor = LOOKFOR_ENUM_OR_INIT; 6839 cont_amount = cin_first_id_amount(); 6840 } 6841 else 6842 { 6843 if (lookfor == LOOKFOR_INITIAL 6844 && *l != NUL 6845 && l[STRLEN(l) - 1] == '\\') 6846 /* XXX */ 6847 cont_amount = cin_get_equal_amount( 6848 curwin->w_cursor.lnum); 6849 if (lookfor != LOOKFOR_TERM) 6850 lookfor = LOOKFOR_UNTERM; 6851 } 6852 } 6853 } 6854 } 6855 6856 /* 6857 * Check if we are after a while (cond); 6858 * If so: Ignore until the matching "do". 6859 */ 6860 /* XXX */ 6861 else if (cin_iswhileofdo(l, 6862 curwin->w_cursor.lnum, ind_maxparen)) 6863 { 6864 /* 6865 * Found an unterminated line after a while ();, line up 6866 * with the last one. 6867 * while (cond); 6868 * 100 + <- line up with this one 6869 * -> here; 6870 */ 6871 if (lookfor == LOOKFOR_UNTERM 6872 || lookfor == LOOKFOR_ENUM_OR_INIT) 6873 { 6874 if (cont_amount > 0) 6875 amount = cont_amount; 6876 else 6877 amount += ind_continuation; 6878 break; 6879 } 6880 6881 if (whilelevel == 0) 6882 { 6883 lookfor = LOOKFOR_TERM; 6884 amount = get_indent(); /* XXX */ 6885 if (theline[0] == '{') 6886 amount += ind_open_extra; 6887 } 6888 ++whilelevel; 6889 } 6890 6891 /* 6892 * We are after a "normal" statement. 6893 * If we had another statement we can stop now and use the 6894 * indent of that other statement. 6895 * Otherwise the indent of the current statement may be used, 6896 * search backwards for the next "normal" statement. 6897 */ 6898 else 6899 { 6900 /* 6901 * Skip single break line, if before a switch label. It 6902 * may be lined up with the case label. 6903 */ 6904 if (lookfor == LOOKFOR_NOBREAK 6905 && cin_isbreak(skipwhite(ml_get_curline()))) 6906 { 6907 lookfor = LOOKFOR_ANY; 6908 continue; 6909 } 6910 6911 /* 6912 * Handle "do {" line. 6913 */ 6914 if (whilelevel > 0) 6915 { 6916 l = cin_skipcomment(ml_get_curline()); 6917 if (cin_isdo(l)) 6918 { 6919 amount = get_indent(); /* XXX */ 6920 --whilelevel; 6921 continue; 6922 } 6923 } 6924 6925 /* 6926 * Found a terminated line above an unterminated line. Add 6927 * the amount for a continuation line. 6928 * x = 1; 6929 * y = foo + 6930 * -> here; 6931 * or 6932 * int x = 1; 6933 * int foo, 6934 * -> here; 6935 */ 6936 if (lookfor == LOOKFOR_UNTERM 6937 || lookfor == LOOKFOR_ENUM_OR_INIT) 6938 { 6939 if (cont_amount > 0) 6940 amount = cont_amount; 6941 else 6942 amount += ind_continuation; 6943 break; 6944 } 6945 6946 /* 6947 * Found a terminated line above a terminated line or "if" 6948 * etc. line. Use the amount of the line below us. 6949 * x = 1; x = 1; 6950 * if (asdf) y = 2; 6951 * while (asdf) ->here; 6952 * here; 6953 * ->foo; 6954 */ 6955 if (lookfor == LOOKFOR_TERM) 6956 { 6957 if (!lookfor_break && whilelevel == 0) 6958 break; 6959 } 6960 6961 /* 6962 * First line above the one we're indenting is terminated. 6963 * To know what needs to be done look further backward for 6964 * a terminated line. 6965 */ 6966 else 6967 { 6968 /* 6969 * position the cursor over the rightmost paren, so 6970 * that matching it will take us back to the start of 6971 * the line. Helps for: 6972 * func(asdr, 6973 * asdfasdf); 6974 * here; 6975 */ 6976 term_again: 6977 l = ml_get_curline(); 6978 if (find_last_paren(l, '(', ')') 6979 && (trypos = find_match_paren(ind_maxparen, 6980 ind_maxcomment)) != NULL) 6981 { 6982 /* 6983 * Check if we are on a case label now. This is 6984 * handled above. 6985 * case xx: if ( asdf && 6986 * asdf) 6987 */ 6988 curwin->w_cursor.lnum = trypos->lnum; 6989 l = ml_get_curline(); 6990 if (cin_iscase(l) || cin_isscopedecl(l)) 6991 { 6992 ++curwin->w_cursor.lnum; 6993 continue; 6994 } 6995 } 6996 6997 /* When aligning with the case statement, don't align 6998 * with a statement after it. 6999 * case 1: { <-- don't use this { position 7000 * stat; 7001 * } 7002 * case 2: 7003 * stat; 7004 * } 7005 */ 7006 iscase = (ind_keep_case_label && cin_iscase(l)); 7007 7008 /* 7009 * Get indent and pointer to text for current line, 7010 * ignoring any jump label. 7011 */ 7012 amount = skip_label(curwin->w_cursor.lnum, 7013 &l, ind_maxcomment); 7014 7015 if (theline[0] == '{') 7016 amount += ind_open_extra; 7017 /* See remark above: "Only add ind_open_extra.." */ 7018 if (*skipwhite(l) == '{') 7019 amount -= ind_open_extra; 7020 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; 7021 7022 /* 7023 * If we're at the end of a block, skip to the start of 7024 * that block. 7025 */ 7026 curwin->w_cursor.col = 0; 7027 if (*cin_skipcomment(l) == '}' 7028 && (trypos = find_start_brace(ind_maxcomment)) 7029 != NULL) /* XXX */ 7030 { 7031 curwin->w_cursor.lnum = trypos->lnum; 7032 /* if not "else {" check for terminated again */ 7033 /* but skip block for "} else {" */ 7034 l = cin_skipcomment(ml_get_curline()); 7035 if (*l == '}' || !cin_iselse(l)) 7036 goto term_again; 7037 ++curwin->w_cursor.lnum; 7038 } 7039 } 7040 } 7041 } 7042 } 7043 } 7044 7045 /* add extra indent for a comment */ 7046 if (cin_iscomment(theline)) 7047 amount += ind_comment; 7048 } 7049 7050 /* 7051 * ok -- we're not inside any sort of structure at all! 7052 * 7053 * this means we're at the top level, and everything should 7054 * basically just match where the previous line is, except 7055 * for the lines immediately following a function declaration, 7056 * which are K&R-style parameters and need to be indented. 7057 */ 7058 else 7059 { 7060 /* 7061 * if our line starts with an open brace, forget about any 7062 * prevailing indent and make sure it looks like the start 7063 * of a function 7064 */ 7065 7066 if (theline[0] == '{') 7067 { 7068 amount = ind_first_open; 7069 } 7070 7071 /* 7072 * If the NEXT line is a function declaration, the current 7073 * line needs to be indented as a function type spec. 7074 * Don't do this if the current line looks like a comment 7075 * or if the current line is terminated, ie. ends in ';'. 7076 */ 7077 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count 7078 && !cin_nocode(theline) 7079 && !cin_ends_in(theline, (char_u *)":", NULL) 7080 && !cin_ends_in(theline, (char_u *)",", NULL) 7081 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1) 7082 && !cin_isterminated(theline, FALSE, TRUE)) 7083 { 7084 amount = ind_func_type; 7085 } 7086 else 7087 { 7088 amount = 0; 7089 curwin->w_cursor = cur_curpos; 7090 7091 /* search backwards until we find something we recognize */ 7092 7093 while (curwin->w_cursor.lnum > 1) 7094 { 7095 curwin->w_cursor.lnum--; 7096 curwin->w_cursor.col = 0; 7097 7098 l = ml_get_curline(); 7099 7100 /* 7101 * If we're in a comment now, skip to the start of the comment. 7102 */ /* XXX */ 7103 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) 7104 { 7105 curwin->w_cursor.lnum = trypos->lnum + 1; 7106 continue; 7107 } 7108 7109 /* 7110 * Are we at the start of a cpp base class declaration or constructor 7111 * initialization? 7112 */ /* XXX */ 7113 if (ind_cpp_baseclass != 0 && theline[0] != '{' 7114 && cin_is_cpp_baseclass(l, &col)) 7115 { 7116 if (col == 0) 7117 { 7118 amount = get_indent() + ind_cpp_baseclass; /* XXX */ 7119 if (find_last_paren(l, '(', ')') 7120 && (trypos = find_match_paren(ind_maxparen, 7121 ind_maxcomment)) != NULL) 7122 amount = get_indent_lnum(trypos->lnum) 7123 + ind_cpp_baseclass; /* XXX */ 7124 } 7125 else 7126 { 7127 curwin->w_cursor.col = col; 7128 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL); 7129 amount = (int)col; 7130 } 7131 break; 7132 } 7133 7134 /* 7135 * Skip preprocessor directives and blank lines. 7136 */ 7137 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) 7138 continue; 7139 7140 if (cin_nocode(l)) 7141 continue; 7142 7143 /* 7144 * If the previous line ends in ',', use one level of 7145 * indentation: 7146 * int foo, 7147 * bar; 7148 * do this before checking for '}' in case of eg. 7149 * enum foobar 7150 * { 7151 * ... 7152 * } foo, 7153 * bar; 7154 */ 7155 n = 0; 7156 if (cin_ends_in(l, (char_u *)",", NULL) 7157 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) 7158 { 7159 /* take us back to opening paren */ 7160 if (find_last_paren(l, '(', ')') 7161 && (trypos = find_match_paren(ind_maxparen, 7162 ind_maxcomment)) != NULL) 7163 curwin->w_cursor.lnum = trypos->lnum; 7164 7165 /* For a line ending in ',' that is a continuation line go 7166 * back to the first line with a backslash: 7167 * char *foo = "bla\ 7168 * bla", 7169 * here; 7170 */ 7171 while (n == 0 && curwin->w_cursor.lnum > 1) 7172 { 7173 l = ml_get(curwin->w_cursor.lnum - 1); 7174 if (*l == NUL || l[STRLEN(l) - 1] != '\\') 7175 break; 7176 --curwin->w_cursor.lnum; 7177 } 7178 7179 amount = get_indent(); /* XXX */ 7180 7181 if (amount == 0) 7182 amount = cin_first_id_amount(); 7183 if (amount == 0) 7184 amount = ind_continuation; 7185 break; 7186 } 7187 7188 /* 7189 * If the line looks like a function declaration, and we're 7190 * not in a comment, put it the left margin. 7191 */ 7192 if (cin_isfuncdecl(NULL, cur_curpos.lnum)) /* XXX */ 7193 break; 7194 l = ml_get_curline(); 7195 7196 /* 7197 * Finding the closing '}' of a previous function. Put 7198 * current line at the left margin. For when 'cino' has "fs". 7199 */ 7200 if (*skipwhite(l) == '}') 7201 break; 7202 7203 /* (matching {) 7204 * If the previous line ends on '};' (maybe followed by 7205 * comments) align at column 0. For example: 7206 * char *string_array[] = { "foo", 7207 * / * x * / "b};ar" }; / * foobar * / 7208 */ 7209 if (cin_ends_in(l, (char_u *)"};", NULL)) 7210 break; 7211 7212 /* 7213 * If the PREVIOUS line is a function declaration, the current 7214 * line (and the ones that follow) needs to be indented as 7215 * parameters. 7216 */ 7217 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum)) 7218 { 7219 amount = ind_param; 7220 break; 7221 } 7222 7223 /* 7224 * If the previous line ends in ';' and the line before the 7225 * previous line ends in ',' or '\', ident to column zero: 7226 * int foo, 7227 * bar; 7228 * indent_to_0 here; 7229 */ 7230 if (cin_ends_in(l, (char_u*)";", NULL)) 7231 { 7232 l = ml_get(curwin->w_cursor.lnum - 1); 7233 if (cin_ends_in(l, (char_u *)",", NULL) 7234 || (*l != NUL && l[STRLEN(l) - 1] == '\\')) 7235 break; 7236 l = ml_get_curline(); 7237 } 7238 7239 /* 7240 * Doesn't look like anything interesting -- so just 7241 * use the indent of this line. 7242 * 7243 * Position the cursor over the rightmost paren, so that 7244 * matching it will take us back to the start of the line. 7245 */ 7246 find_last_paren(l, '(', ')'); 7247 7248 if ((trypos = find_match_paren(ind_maxparen, 7249 ind_maxcomment)) != NULL) 7250 curwin->w_cursor.lnum = trypos->lnum; 7251 amount = get_indent(); /* XXX */ 7252 break; 7253 } 7254 7255 /* add extra indent for a comment */ 7256 if (cin_iscomment(theline)) 7257 amount += ind_comment; 7258 7259 /* add extra indent if the previous line ended in a backslash: 7260 * "asdfasdf\ 7261 * here"; 7262 * char *foo = "asdf\ 7263 * here"; 7264 */ 7265 if (cur_curpos.lnum > 1) 7266 { 7267 l = ml_get(cur_curpos.lnum - 1); 7268 if (*l != NUL && l[STRLEN(l) - 1] == '\\') 7269 { 7270 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); 7271 if (cur_amount > 0) 7272 amount = cur_amount; 7273 else if (cur_amount == 0) 7274 amount += ind_continuation; 7275 } 7276 } 7277 } 7278 } 7279 7280 theend: 7281 /* put the cursor back where it belongs */ 7282 curwin->w_cursor = cur_curpos; 7283 7284 vim_free(linecopy); 7285 7286 if (amount < 0) 7287 return 0; 7288 return amount; 7289 } 7290 7291 static int 7292 find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment) 7293 int lookfor; 7294 linenr_T ourscope; 7295 int ind_maxparen; 7296 int ind_maxcomment; 7297 { 7298 char_u *look; 7299 pos_T *theirscope; 7300 char_u *mightbeif; 7301 int elselevel; 7302 int whilelevel; 7303 7304 if (lookfor == LOOKFOR_IF) 7305 { 7306 elselevel = 1; 7307 whilelevel = 0; 7308 } 7309 else 7310 { 7311 elselevel = 0; 7312 whilelevel = 1; 7313 } 7314 7315 curwin->w_cursor.col = 0; 7316 7317 while (curwin->w_cursor.lnum > ourscope + 1) 7318 { 7319 curwin->w_cursor.lnum--; 7320 curwin->w_cursor.col = 0; 7321 7322 look = cin_skipcomment(ml_get_curline()); 7323 if (cin_iselse(look) 7324 || cin_isif(look) 7325 || cin_isdo(look) /* XXX */ 7326 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) 7327 { 7328 /* 7329 * if we've gone outside the braces entirely, 7330 * we must be out of scope... 7331 */ 7332 theirscope = find_start_brace(ind_maxcomment); /* XXX */ 7333 if (theirscope == NULL) 7334 break; 7335 7336 /* 7337 * and if the brace enclosing this is further 7338 * back than the one enclosing the else, we're 7339 * out of luck too. 7340 */ 7341 if (theirscope->lnum < ourscope) 7342 break; 7343 7344 /* 7345 * and if they're enclosed in a *deeper* brace, 7346 * then we can ignore it because it's in a 7347 * different scope... 7348 */ 7349 if (theirscope->lnum > ourscope) 7350 continue; 7351 7352 /* 7353 * if it was an "else" (that's not an "else if") 7354 * then we need to go back to another if, so 7355 * increment elselevel 7356 */ 7357 look = cin_skipcomment(ml_get_curline()); 7358 if (cin_iselse(look)) 7359 { 7360 mightbeif = cin_skipcomment(look + 4); 7361 if (!cin_isif(mightbeif)) 7362 ++elselevel; 7363 continue; 7364 } 7365 7366 /* 7367 * if it was a "while" then we need to go back to 7368 * another "do", so increment whilelevel. XXX 7369 */ 7370 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) 7371 { 7372 ++whilelevel; 7373 continue; 7374 } 7375 7376 /* If it's an "if" decrement elselevel */ 7377 look = cin_skipcomment(ml_get_curline()); 7378 if (cin_isif(look)) 7379 { 7380 elselevel--; 7381 /* 7382 * When looking for an "if" ignore "while"s that 7383 * get in the way. 7384 */ 7385 if (elselevel == 0 && lookfor == LOOKFOR_IF) 7386 whilelevel = 0; 7387 } 7388 7389 /* If it's a "do" decrement whilelevel */ 7390 if (cin_isdo(look)) 7391 whilelevel--; 7392 7393 /* 7394 * if we've used up all the elses, then 7395 * this must be the if that we want! 7396 * match the indent level of that if. 7397 */ 7398 if (elselevel <= 0 && whilelevel <= 0) 7399 { 7400 return OK; 7401 } 7402 } 7403 } 7404 return FAIL; 7405 } 7406 7407 # if defined(FEAT_EVAL) || defined(PROTO) 7408 /* 7409 * Get indent level from 'indentexpr'. 7410 */ 7411 int 7412 get_expr_indent() 7413 { 7414 int indent; 7415 pos_T pos; 7416 int save_State; 7417 7418 pos = curwin->w_cursor; 7419 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); 7420 ++sandbox; 7421 indent = eval_to_number(curbuf->b_p_inde); 7422 --sandbox; 7423 7424 /* Restore the cursor position so that 'indentexpr' doesn't need to. 7425 * Pretend to be in Insert mode, allow cursor past end of line for "o" 7426 * command. */ 7427 save_State = State; 7428 State = INSERT; 7429 curwin->w_cursor = pos; 7430 check_cursor(); 7431 State = save_State; 7432 7433 /* If there is an error, just keep the current indent. */ 7434 if (indent < 0) 7435 indent = get_indent(); 7436 7437 return indent; 7438 } 7439 # endif 7440 7441 #endif /* FEAT_CINDENT */ 7442 7443 #if defined(FEAT_LISP) || defined(PROTO) 7444 7445 static int lisp_match __ARGS((char_u *p)); 7446 7447 static int 7448 lisp_match(p) 7449 char_u *p; 7450 { 7451 char_u buf[LSIZE]; 7452 int len; 7453 char_u *word = p_lispwords; 7454 7455 while (*word != NUL) 7456 { 7457 (void)copy_option_part(&word, buf, LSIZE, ","); 7458 len = (int)STRLEN(buf); 7459 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') 7460 return TRUE; 7461 } 7462 return FALSE; 7463 } 7464 7465 /* 7466 * When 'p' is present in 'cpoptions, a Vi compatible method is used. 7467 * The incompatible newer method is quite a bit better at indenting 7468 * code in lisp-like languages than the traditional one; it's still 7469 * mostly heuristics however -- Dirk van Deun, [email protected] 7470 * 7471 * TODO: 7472 * Findmatch() should be adapted for lisp, also to make showmatch 7473 * work correctly: now (v5.3) it seems all C/C++ oriented: 7474 * - it does not recognize the #\( and #\) notations as character literals 7475 * - it doesn't know about comments starting with a semicolon 7476 * - it incorrectly interprets '(' as a character literal 7477 * All this messes up get_lisp_indent in some rare cases. 7478 * Update from Sergey Khorev: 7479 * I tried to fix the first two issues. 7480 */ 7481 int 7482 get_lisp_indent() 7483 { 7484 pos_T *pos, realpos, paren; 7485 int amount; 7486 char_u *that; 7487 colnr_T col; 7488 colnr_T firsttry; 7489 int parencount, quotecount; 7490 int vi_lisp; 7491 7492 /* Set vi_lisp to use the vi-compatible method */ 7493 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); 7494 7495 realpos = curwin->w_cursor; 7496 curwin->w_cursor.col = 0; 7497 7498 if ((pos = findmatch(NULL, '(')) == NULL) 7499 pos = findmatch(NULL, '['); 7500 else 7501 { 7502 paren = *pos; 7503 pos = findmatch(NULL, '['); 7504 if (pos == NULL || ltp(pos, &paren)) 7505 pos = &paren; 7506 } 7507 if (pos != NULL) 7508 { 7509 /* Extra trick: Take the indent of the first previous non-white 7510 * line that is at the same () level. */ 7511 amount = -1; 7512 parencount = 0; 7513 7514 while (--curwin->w_cursor.lnum >= pos->lnum) 7515 { 7516 if (linewhite(curwin->w_cursor.lnum)) 7517 continue; 7518 for (that = ml_get_curline(); *that != NUL; ++that) 7519 { 7520 if (*that == ';') 7521 { 7522 while (*(that + 1) != NUL) 7523 ++that; 7524 continue; 7525 } 7526 if (*that == '\\') 7527 { 7528 if (*(that + 1) != NUL) 7529 ++that; 7530 continue; 7531 } 7532 if (*that == '"' && *(that + 1) != NUL) 7533 { 7534 that++; 7535 while (*that && (*that != '"' || *(that - 1) == '\\')) 7536 ++that; 7537 } 7538 if (*that == '(' || *that == '[') 7539 ++parencount; 7540 else if (*that == ')' || *that == ']') 7541 --parencount; 7542 } 7543 if (parencount == 0) 7544 { 7545 amount = get_indent(); 7546 break; 7547 } 7548 } 7549 7550 if (amount == -1) 7551 { 7552 curwin->w_cursor.lnum = pos->lnum; 7553 curwin->w_cursor.col = pos->col; 7554 col = pos->col; 7555 7556 that = ml_get_curline(); 7557 7558 if (vi_lisp && get_indent() == 0) 7559 amount = 2; 7560 else 7561 { 7562 amount = 0; 7563 while (*that && col) 7564 { 7565 amount += lbr_chartabsize_adv(&that, (colnr_T)amount); 7566 col--; 7567 } 7568 7569 /* 7570 * Some keywords require "body" indenting rules (the 7571 * non-standard-lisp ones are Scheme special forms): 7572 * 7573 * (let ((a 1)) instead (let ((a 1)) 7574 * (...)) of (...)) 7575 */ 7576 7577 if (!vi_lisp && (*that == '(' || *that == '[') 7578 && lisp_match(that + 1)) 7579 amount += 2; 7580 else 7581 { 7582 that++; 7583 amount++; 7584 firsttry = amount; 7585 7586 while (vim_iswhite(*that)) 7587 { 7588 amount += lbr_chartabsize(that, (colnr_T)amount); 7589 ++that; 7590 } 7591 7592 if (*that && *that != ';') /* not a comment line */ 7593 { 7594 /* test *that != '(' to accomodate first let/do 7595 * argument if it is more than one line */ 7596 if (!vi_lisp && *that != '(' && *that != '[') 7597 firsttry++; 7598 7599 parencount = 0; 7600 quotecount = 0; 7601 7602 if (vi_lisp 7603 || (*that != '"' 7604 && *that != '\'' 7605 && *that != '#' 7606 && (*that < '0' || *that > '9'))) 7607 { 7608 while (*that 7609 && (!vim_iswhite(*that) 7610 || quotecount 7611 || parencount) 7612 && (!((*that == '(' || *that == '[') 7613 && !quotecount 7614 && !parencount 7615 && vi_lisp))) 7616 { 7617 if (*that == '"') 7618 quotecount = !quotecount; 7619 if ((*that == '(' || *that == '[') 7620 && !quotecount) 7621 ++parencount; 7622 if ((*that == ')' || *that == ']') 7623 && !quotecount) 7624 --parencount; 7625 if (*that == '\\' && *(that+1) != NUL) 7626 amount += lbr_chartabsize_adv(&that, 7627 (colnr_T)amount); 7628 amount += lbr_chartabsize_adv(&that, 7629 (colnr_T)amount); 7630 } 7631 } 7632 while (vim_iswhite(*that)) 7633 { 7634 amount += lbr_chartabsize(that, (colnr_T)amount); 7635 that++; 7636 } 7637 if (!*that || *that == ';') 7638 amount = firsttry; 7639 } 7640 } 7641 } 7642 } 7643 } 7644 else 7645 amount = 0; /* no matching '(' or '[' found, use zero indent */ 7646 7647 curwin->w_cursor = realpos; 7648 7649 return amount; 7650 } 7651 #endif /* FEAT_LISP */ 7652 7653 void 7654 prepare_to_exit() 7655 { 7656 #ifdef FEAT_GUI 7657 if (gui.in_use) 7658 { 7659 gui.dying = TRUE; 7660 out_trash(); /* trash any pending output */ 7661 } 7662 else 7663 #endif 7664 { 7665 windgoto((int)Rows - 1, 0); 7666 7667 /* 7668 * Switch terminal mode back now, so messages end up on the "normal" 7669 * screen (if there are two screens). 7670 */ 7671 settmode(TMODE_COOK); 7672 #ifdef WIN3264 7673 if (can_end_termcap_mode(FALSE) == TRUE) 7674 #endif 7675 stoptermcap(); 7676 out_flush(); 7677 } 7678 } 7679 7680 /* 7681 * Preserve files and exit. 7682 * When called IObuff must contain a message. 7683 */ 7684 void 7685 preserve_exit() 7686 { 7687 buf_T *buf; 7688 7689 prepare_to_exit(); 7690 7691 out_str(IObuff); 7692 screen_start(); /* don't know where cursor is now */ 7693 out_flush(); 7694 7695 ml_close_notmod(); /* close all not-modified buffers */ 7696 7697 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 7698 { 7699 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) 7700 { 7701 OUT_STR(_("Vim: preserving files...\n")); 7702 screen_start(); /* don't know where cursor is now */ 7703 out_flush(); 7704 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ 7705 break; 7706 } 7707 } 7708 7709 ml_close_all(FALSE); /* close all memfiles, without deleting */ 7710 7711 OUT_STR(_("Vim: Finished.\n")); 7712 7713 getout(1); 7714 } 7715 7716 /* 7717 * return TRUE if "fname" exists. 7718 */ 7719 int 7720 vim_fexists(fname) 7721 char_u *fname; 7722 { 7723 struct stat st; 7724 7725 if (mch_stat((char *)fname, &st)) 7726 return FALSE; 7727 return TRUE; 7728 } 7729 7730 /* 7731 * Check for CTRL-C pressed, but only once in a while. 7732 * Should be used instead of ui_breakcheck() for functions that check for 7733 * each line in the file. Calling ui_breakcheck() each time takes too much 7734 * time, because it can be a system call. 7735 */ 7736 7737 #ifndef BREAKCHECK_SKIP 7738 # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ 7739 # define BREAKCHECK_SKIP 200 7740 # else 7741 # define BREAKCHECK_SKIP 32 7742 # endif 7743 #endif 7744 7745 static int breakcheck_count = 0; 7746 7747 void 7748 line_breakcheck() 7749 { 7750 if (++breakcheck_count >= BREAKCHECK_SKIP) 7751 { 7752 breakcheck_count = 0; 7753 ui_breakcheck(); 7754 } 7755 } 7756 7757 /* 7758 * Like line_breakcheck() but check 10 times less often. 7759 */ 7760 void 7761 fast_breakcheck() 7762 { 7763 if (++breakcheck_count >= BREAKCHECK_SKIP * 10) 7764 { 7765 breakcheck_count = 0; 7766 ui_breakcheck(); 7767 } 7768 } 7769 7770 /* 7771 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching 7772 * 'wildignore'. 7773 */ 7774 int 7775 expand_wildcards(num_pat, pat, num_file, file, flags) 7776 int num_pat; /* number of input patterns */ 7777 char_u **pat; /* array of input patterns */ 7778 int *num_file; /* resulting number of files */ 7779 char_u ***file; /* array of resulting files */ 7780 int flags; /* EW_DIR, etc. */ 7781 { 7782 int retval; 7783 int i, j; 7784 char_u *p; 7785 int non_suf_match; /* number without matching suffix */ 7786 7787 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags); 7788 7789 /* When keeping all matches, return here */ 7790 if (flags & EW_KEEPALL) 7791 return retval; 7792 7793 #ifdef FEAT_WILDIGN 7794 /* 7795 * Remove names that match 'wildignore'. 7796 */ 7797 if (*p_wig) 7798 { 7799 char_u *ffname; 7800 7801 /* check all files in (*file)[] */ 7802 for (i = 0; i < *num_file; ++i) 7803 { 7804 ffname = FullName_save((*file)[i], FALSE); 7805 if (ffname == NULL) /* out of memory */ 7806 break; 7807 # ifdef VMS 7808 vms_remove_version(ffname); 7809 # endif 7810 if (match_file_list(p_wig, (*file)[i], ffname)) 7811 { 7812 /* remove this matching file from the list */ 7813 vim_free((*file)[i]); 7814 for (j = i; j + 1 < *num_file; ++j) 7815 (*file)[j] = (*file)[j + 1]; 7816 --*num_file; 7817 --i; 7818 } 7819 vim_free(ffname); 7820 } 7821 } 7822 #endif 7823 7824 /* 7825 * Move the names where 'suffixes' match to the end. 7826 */ 7827 if (*num_file > 1) 7828 { 7829 non_suf_match = 0; 7830 for (i = 0; i < *num_file; ++i) 7831 { 7832 if (!match_suffix((*file)[i])) 7833 { 7834 /* 7835 * Move the name without matching suffix to the front 7836 * of the list. 7837 */ 7838 p = (*file)[i]; 7839 for (j = i; j > non_suf_match; --j) 7840 (*file)[j] = (*file)[j - 1]; 7841 (*file)[non_suf_match++] = p; 7842 } 7843 } 7844 } 7845 7846 return retval; 7847 } 7848 7849 /* 7850 * Return TRUE if "fname" matches with an entry in 'suffixes'. 7851 */ 7852 int 7853 match_suffix(fname) 7854 char_u *fname; 7855 { 7856 int fnamelen, setsuflen; 7857 char_u *setsuf; 7858 #define MAXSUFLEN 30 /* maximum length of a file suffix */ 7859 char_u suf_buf[MAXSUFLEN]; 7860 7861 fnamelen = (int)STRLEN(fname); 7862 setsuflen = 0; 7863 for (setsuf = p_su; *setsuf; ) 7864 { 7865 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); 7866 if (fnamelen >= setsuflen 7867 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, 7868 (size_t)setsuflen) == 0) 7869 break; 7870 setsuflen = 0; 7871 } 7872 return (setsuflen != 0); 7873 } 7874 7875 #if !defined(NO_EXPANDPATH) || defined(PROTO) 7876 7877 # ifdef VIM_BACKTICK 7878 static int vim_backtick __ARGS((char_u *p)); 7879 static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags)); 7880 # endif 7881 7882 # if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264) 7883 /* 7884 * File name expansion code for MS-DOS, Win16 and Win32. It's here because 7885 * it's shared between these systems. 7886 */ 7887 # if defined(DJGPP) || defined(PROTO) 7888 # define _cdecl /* DJGPP doesn't have this */ 7889 # else 7890 # ifdef __BORLANDC__ 7891 # define _cdecl _RTLENTRYF 7892 # endif 7893 # endif 7894 7895 /* 7896 * comparison function for qsort in dos_expandpath() 7897 */ 7898 static int _cdecl 7899 pstrcmp(const void *a, const void *b) 7900 { 7901 return (pathcmp(*(char **)a, *(char **)b)); 7902 } 7903 7904 # ifndef WIN3264 7905 static void 7906 namelowcpy( 7907 char_u *d, 7908 char_u *s) 7909 { 7910 # ifdef DJGPP 7911 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */ 7912 while (*s) 7913 *d++ = *s++; 7914 else 7915 # endif 7916 while (*s) 7917 *d++ = TOLOWER_LOC(*s++); 7918 *d = NUL; 7919 } 7920 # endif 7921 7922 /* 7923 * Recursively build up a list of files in "gap" matching the first wildcard 7924 * in `path'. Called by expand_wildcards(). 7925 * Return the number of matches found. 7926 * "path" has backslashes before chars that are not to be expanded, starting 7927 * at "path[wildoff]". 7928 */ 7929 static int 7930 dos_expandpath( 7931 garray_T *gap, 7932 char_u *path, 7933 int wildoff, 7934 int flags) /* EW_* flags */ 7935 { 7936 char_u *buf; 7937 char_u *path_end; 7938 char_u *p, *s, *e; 7939 int start_len = gap->ga_len; 7940 int ok; 7941 #ifdef WIN3264 7942 WIN32_FIND_DATA fb; 7943 HANDLE hFind = (HANDLE)0; 7944 # ifdef FEAT_MBYTE 7945 WIN32_FIND_DATAW wfb; 7946 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ 7947 # endif 7948 #else 7949 struct ffblk fb; 7950 #endif 7951 int matches; 7952 int starts_with_dot; 7953 int len; 7954 char_u *pat; 7955 regmatch_T regmatch; 7956 char_u *matchname; 7957 7958 /* make room for file name */ 7959 buf = alloc((unsigned int)STRLEN(path) + BASENAMELEN + 5); 7960 if (buf == NULL) 7961 return 0; 7962 7963 /* 7964 * Find the first part in the path name that contains a wildcard or a ~1. 7965 * Copy it into buf, including the preceding characters. 7966 */ 7967 p = buf; 7968 s = buf; 7969 e = NULL; 7970 path_end = path; 7971 while (*path_end != NUL) 7972 { 7973 /* May ignore a wildcard that has a backslash before it; it will 7974 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ 7975 if (path_end >= path + wildoff && rem_backslash(path_end)) 7976 *p++ = *path_end++; 7977 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') 7978 { 7979 if (e != NULL) 7980 break; 7981 s = p + 1; 7982 } 7983 else if (path_end >= path + wildoff 7984 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) 7985 e = p; 7986 #ifdef FEAT_MBYTE 7987 if (has_mbyte) 7988 { 7989 len = (*mb_ptr2len_check)(path_end); 7990 STRNCPY(p, path_end, len); 7991 p += len; 7992 path_end += len; 7993 } 7994 else 7995 #endif 7996 *p++ = *path_end++; 7997 } 7998 e = p; 7999 *e = NUL; 8000 8001 /* now we have one wildcard component between s and e */ 8002 /* Remove backslashes between "wildoff" and the start of the wildcard 8003 * component. */ 8004 for (p = buf + wildoff; p < s; ++p) 8005 if (rem_backslash(p)) 8006 { 8007 STRCPY(p, p + 1); 8008 --e; 8009 --s; 8010 } 8011 8012 starts_with_dot = (*s == '.'); 8013 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); 8014 if (pat == NULL) 8015 { 8016 vim_free(buf); 8017 return 0; 8018 } 8019 8020 /* compile the regexp into a program */ 8021 regmatch.rm_ic = TRUE; /* Always ignore case */ 8022 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); 8023 vim_free(pat); 8024 8025 if (regmatch.regprog == NULL) 8026 { 8027 vim_free(buf); 8028 return 0; 8029 } 8030 8031 /* remember the pattern or file name being looked for */ 8032 matchname = vim_strsave(s); 8033 8034 /* Scan all files in the directory with "dir/ *.*" */ 8035 STRCPY(s, "*.*"); 8036 #ifdef WIN3264 8037 # ifdef FEAT_MBYTE 8038 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) 8039 { 8040 /* The active codepage differs from 'encoding'. Attempt using the 8041 * wide function. If it fails because it is not implemented fall back 8042 * to the non-wide version (for Windows 98) */ 8043 wn = enc_to_ucs2(buf, NULL); 8044 if (wn != NULL) 8045 { 8046 hFind = FindFirstFileW(wn, &wfb); 8047 if (hFind == INVALID_HANDLE_VALUE 8048 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) 8049 { 8050 vim_free(wn); 8051 wn = NULL; 8052 } 8053 } 8054 } 8055 8056 if (wn == NULL) 8057 # endif 8058 hFind = FindFirstFile(buf, &fb); 8059 ok = (hFind != INVALID_HANDLE_VALUE); 8060 #else 8061 /* If we are expanding wildcards we try both files and directories */ 8062 ok = (findfirst((char *)buf, &fb, 8063 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); 8064 #endif 8065 8066 while (ok) 8067 { 8068 #ifdef WIN3264 8069 # ifdef FEAT_MBYTE 8070 if (wn != NULL) 8071 p = ucs2_to_enc(wfb.cFileName, NULL); /* p is allocated here */ 8072 else 8073 # endif 8074 p = (char_u *)fb.cFileName; 8075 #else 8076 p = (char_u *)fb.ff_name; 8077 #endif 8078 /* Ignore entries starting with a dot, unless when asked for. Accept 8079 * all entries found with "matchname". */ 8080 if ((p[0] != '.' || starts_with_dot) 8081 && (matchname == NULL 8082 || vim_regexec(®match, p, (colnr_T)0))) 8083 { 8084 #ifdef WIN3264 8085 STRCPY(s, p); 8086 #else 8087 namelowcpy(s, p); 8088 #endif 8089 len = (int)STRLEN(buf); 8090 STRCPY(buf + len, path_end); 8091 if (mch_has_exp_wildcard(path_end)) 8092 { 8093 /* need to expand another component of the path */ 8094 /* remove backslashes for the remaining components only */ 8095 (void)dos_expandpath(gap, buf, len + 1, flags); 8096 } 8097 else 8098 { 8099 /* no more wildcards, check if there is a match */ 8100 /* remove backslashes for the remaining components only */ 8101 if (*path_end != 0) 8102 backslash_halve(buf + len + 1); 8103 if (mch_getperm(buf) >= 0) /* add existing file */ 8104 addfile(gap, buf, flags); 8105 } 8106 } 8107 8108 #ifdef WIN3264 8109 # ifdef FEAT_MBYTE 8110 if (wn != NULL) 8111 { 8112 vim_free(p); 8113 ok = FindNextFileW(hFind, &wfb); 8114 } 8115 else 8116 # endif 8117 ok = FindNextFile(hFind, &fb); 8118 #else 8119 ok = (findnext(&fb) == 0); 8120 #endif 8121 8122 /* If no more matches and no match was used, try expanding the name 8123 * itself. Finds the long name of a short filename. */ 8124 if (!ok && matchname != NULL && gap->ga_len == start_len) 8125 { 8126 STRCPY(s, matchname); 8127 #ifdef WIN3264 8128 FindClose(hFind); 8129 # ifdef FEAT_MBYTE 8130 if (wn != NULL) 8131 { 8132 vim_free(wn); 8133 wn = enc_to_ucs2(buf, NULL); 8134 if (wn != NULL) 8135 hFind = FindFirstFileW(wn, &wfb); 8136 } 8137 if (wn == NULL) 8138 # endif 8139 hFind = FindFirstFile(buf, &fb); 8140 ok = (hFind != INVALID_HANDLE_VALUE); 8141 #else 8142 ok = (findfirst((char *)buf, &fb, 8143 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); 8144 #endif 8145 vim_free(matchname); 8146 matchname = NULL; 8147 } 8148 } 8149 8150 #ifdef WIN3264 8151 FindClose(hFind); 8152 # ifdef FEAT_MBYTE 8153 vim_free(wn); 8154 # endif 8155 #endif 8156 vim_free(buf); 8157 vim_free(regmatch.regprog); 8158 vim_free(matchname); 8159 8160 matches = gap->ga_len - start_len; 8161 if (matches > 0) 8162 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, 8163 sizeof(char_u *), pstrcmp); 8164 return matches; 8165 } 8166 8167 int 8168 mch_expandpath( 8169 garray_T *gap, 8170 char_u *path, 8171 int flags) /* EW_* flags */ 8172 { 8173 return dos_expandpath(gap, path, 0, flags); 8174 } 8175 # endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */ 8176 8177 /* 8178 * Generic wildcard expansion code. 8179 * 8180 * Characters in "pat" that should not be expanded must be preceded with a 8181 * backslash. E.g., "/path\ with\ spaces/my\*star*" 8182 * 8183 * Return FAIL when no single file was found. In this case "num_file" is not 8184 * set, and "file" may contain an error message. 8185 * Return OK when some files found. "num_file" is set to the number of 8186 * matches, "file" to the array of matches. Call FreeWild() later. 8187 */ 8188 int 8189 gen_expand_wildcards(num_pat, pat, num_file, file, flags) 8190 int num_pat; /* number of input patterns */ 8191 char_u **pat; /* array of input patterns */ 8192 int *num_file; /* resulting number of files */ 8193 char_u ***file; /* array of resulting files */ 8194 int flags; /* EW_* flags */ 8195 { 8196 int i; 8197 garray_T ga; 8198 char_u *p; 8199 static int recursive = FALSE; 8200 int add_pat; 8201 8202 /* 8203 * expand_env() is called to expand things like "~user". If this fails, 8204 * it calls ExpandOne(), which brings us back here. In this case, always 8205 * call the machine specific expansion function, if possible. Otherwise, 8206 * return FAIL. 8207 */ 8208 if (recursive) 8209 #ifdef SPECIAL_WILDCHAR 8210 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); 8211 #else 8212 return FAIL; 8213 #endif 8214 8215 #ifdef SPECIAL_WILDCHAR 8216 /* 8217 * If there are any special wildcard characters which we cannot handle 8218 * here, call machine specific function for all the expansion. This 8219 * avoids starting the shell for each argument separately. 8220 * For `=expr` do use the internal function. 8221 */ 8222 for (i = 0; i < num_pat; i++) 8223 { 8224 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL 8225 # ifdef VIM_BACKTICK 8226 && !(vim_backtick(pat[i]) && pat[i][1] == '=') 8227 # endif 8228 ) 8229 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); 8230 } 8231 #endif 8232 8233 recursive = TRUE; 8234 8235 /* 8236 * The matching file names are stored in a growarray. Init it empty. 8237 */ 8238 ga_init2(&ga, (int)sizeof(char_u *), 30); 8239 8240 for (i = 0; i < num_pat; ++i) 8241 { 8242 add_pat = -1; 8243 p = pat[i]; 8244 8245 #ifdef VIM_BACKTICK 8246 if (vim_backtick(p)) 8247 add_pat = expand_backtick(&ga, p, flags); 8248 else 8249 #endif 8250 { 8251 /* 8252 * First expand environment variables, "~/" and "~user/". 8253 */ 8254 if (vim_strpbrk(p, (char_u *)"$~") != NULL) 8255 { 8256 p = expand_env_save(p); 8257 if (p == NULL) 8258 p = pat[i]; 8259 #ifdef UNIX 8260 /* 8261 * On Unix, if expand_env() can't expand an environment 8262 * variable, use the shell to do that. Discard previously 8263 * found file names and start all over again. 8264 */ 8265 else if (vim_strpbrk(p, (char_u *)"$~") != NULL) 8266 { 8267 vim_free(p); 8268 ga_clear(&ga); 8269 i = mch_expand_wildcards(num_pat, pat, num_file, file, 8270 flags); 8271 recursive = FALSE; 8272 return i; 8273 } 8274 #endif 8275 } 8276 8277 /* 8278 * If there are wildcards: Expand file names and add each match to 8279 * the list. If there is no match, and EW_NOTFOUND is given, add 8280 * the pattern. 8281 * If there are no wildcards: Add the file name if it exists or 8282 * when EW_NOTFOUND is given. 8283 */ 8284 if (mch_has_exp_wildcard(p)) 8285 add_pat = mch_expandpath(&ga, p, flags); 8286 } 8287 8288 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) 8289 { 8290 char_u *t = backslash_halve_save(p); 8291 8292 #if defined(MACOS_CLASSIC) 8293 slash_to_colon(t); 8294 #endif 8295 /* When EW_NOTFOUND is used, always add files and dirs. Makes 8296 * "vim c:/" work. */ 8297 if (flags & EW_NOTFOUND) 8298 addfile(&ga, t, flags | EW_DIR | EW_FILE); 8299 else if (mch_getperm(t) >= 0) 8300 addfile(&ga, t, flags); 8301 vim_free(t); 8302 } 8303 8304 if (p != pat[i]) 8305 vim_free(p); 8306 } 8307 8308 *num_file = ga.ga_len; 8309 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; 8310 8311 recursive = FALSE; 8312 8313 return (ga.ga_data != NULL) ? OK : FAIL; 8314 } 8315 8316 # ifdef VIM_BACKTICK 8317 8318 /* 8319 * Return TRUE if we can expand this backtick thing here. 8320 */ 8321 static int 8322 vim_backtick(p) 8323 char_u *p; 8324 { 8325 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); 8326 } 8327 8328 /* 8329 * Expand an item in `backticks` by executing it as a command. 8330 * Currently only works when pat[] starts and ends with a `. 8331 * Returns number of file names found. 8332 */ 8333 static int 8334 expand_backtick(gap, pat, flags) 8335 garray_T *gap; 8336 char_u *pat; 8337 int flags; /* EW_* flags */ 8338 { 8339 char_u *p; 8340 char_u *cmd; 8341 char_u *buffer; 8342 int cnt = 0; 8343 int i; 8344 8345 /* Create the command: lop off the backticks. */ 8346 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); 8347 if (cmd == NULL) 8348 return 0; 8349 8350 #ifdef FEAT_EVAL 8351 if (*cmd == '=') /* `={expr}`: Expand expression */ 8352 buffer = eval_to_string(cmd + 1, &p); 8353 else 8354 #endif 8355 buffer = get_cmd_output(cmd, NULL, 8356 (flags & EW_SILENT) ? SHELL_SILENT : 0); 8357 vim_free(cmd); 8358 if (buffer == NULL) 8359 return 0; 8360 8361 cmd = buffer; 8362 while (*cmd != NUL) 8363 { 8364 cmd = skipwhite(cmd); /* skip over white space */ 8365 p = cmd; 8366 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ 8367 ++p; 8368 /* add an entry if it is not empty */ 8369 if (p > cmd) 8370 { 8371 i = *p; 8372 *p = NUL; 8373 addfile(gap, cmd, flags); 8374 *p = i; 8375 ++cnt; 8376 } 8377 cmd = p; 8378 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) 8379 ++cmd; 8380 } 8381 8382 vim_free(buffer); 8383 return cnt; 8384 } 8385 # endif /* VIM_BACKTICK */ 8386 8387 /* 8388 * Add a file to a file list. Accepted flags: 8389 * EW_DIR add directories 8390 * EW_FILE add files 8391 * EW_NOTFOUND add even when it doesn't exist 8392 * EW_ADDSLASH add slash after directory name 8393 */ 8394 void 8395 addfile(gap, f, flags) 8396 garray_T *gap; 8397 char_u *f; /* filename */ 8398 int flags; 8399 { 8400 char_u *p; 8401 int isdir; 8402 8403 /* if the file/dir doesn't exist, may not add it */ 8404 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0) 8405 return; 8406 8407 #ifdef FNAME_ILLEGAL 8408 /* if the file/dir contains illegal characters, don't add it */ 8409 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) 8410 return; 8411 #endif 8412 8413 isdir = mch_isdir(f); 8414 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) 8415 return; 8416 8417 /* Make room for another item in the file list. */ 8418 if (ga_grow(gap, 1) == FAIL) 8419 return; 8420 8421 p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); 8422 if (p == NULL) 8423 return; 8424 8425 STRCPY(p, f); 8426 #ifdef BACKSLASH_IN_FILENAME 8427 slash_adjust(p); 8428 #endif 8429 /* 8430 * Append a slash or backslash after directory names if none is present. 8431 */ 8432 #ifndef DONT_ADD_PATHSEP_TO_DIR 8433 if (isdir && (flags & EW_ADDSLASH)) 8434 add_pathsep(p); 8435 #endif 8436 ((char_u **)gap->ga_data)[gap->ga_len++] = p; 8437 --gap->ga_room; 8438 } 8439 #endif /* !NO_EXPANDPATH */ 8440 8441 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) 8442 8443 #ifndef SEEK_SET 8444 # define SEEK_SET 0 8445 #endif 8446 #ifndef SEEK_END 8447 # define SEEK_END 2 8448 #endif 8449 8450 /* 8451 * Get the stdout of an external command. 8452 * Returns an allocated string, or NULL for error. 8453 */ 8454 char_u * 8455 get_cmd_output(cmd, infile, flags) 8456 char_u *cmd; 8457 char_u *infile; /* optional input file name */ 8458 int flags; /* can be SHELL_SILENT */ 8459 { 8460 char_u *tempname; 8461 char_u *command; 8462 char_u *buffer = NULL; 8463 int len; 8464 int i = 0; 8465 FILE *fd; 8466 8467 if (check_restricted() || check_secure()) 8468 return NULL; 8469 8470 /* get a name for the temp file */ 8471 if ((tempname = vim_tempname('o')) == NULL) 8472 { 8473 EMSG(_(e_notmp)); 8474 return NULL; 8475 } 8476 8477 /* Add the redirection stuff */ 8478 command = make_filter_cmd(cmd, infile, tempname); 8479 if (command == NULL) 8480 goto done; 8481 8482 /* 8483 * Call the shell to execute the command (errors are ignored). 8484 * Don't check timestamps here. 8485 */ 8486 ++no_check_timestamps; 8487 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); 8488 --no_check_timestamps; 8489 8490 vim_free(command); 8491 8492 /* 8493 * read the names from the file into memory 8494 */ 8495 # ifdef VMS 8496 /* created temporary file is not allways readable as binary */ 8497 fd = mch_fopen((char *)tempname, "r"); 8498 # else 8499 fd = mch_fopen((char *)tempname, READBIN); 8500 # endif 8501 8502 if (fd == NULL) 8503 { 8504 EMSG2(_(e_notopen), tempname); 8505 goto done; 8506 } 8507 8508 fseek(fd, 0L, SEEK_END); 8509 len = ftell(fd); /* get size of temp file */ 8510 fseek(fd, 0L, SEEK_SET); 8511 8512 buffer = alloc(len + 1); 8513 if (buffer != NULL) 8514 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); 8515 fclose(fd); 8516 mch_remove(tempname); 8517 if (buffer == NULL) 8518 goto done; 8519 #ifdef VMS 8520 len = i; /* VMS doesn't give us what we asked for... */ 8521 #endif 8522 if (i != len) 8523 { 8524 EMSG2(_(e_notread), tempname); 8525 vim_free(buffer); 8526 buffer = NULL; 8527 } 8528 else 8529 buffer[len] = '\0'; /* make sure the buffer is terminated */ 8530 8531 done: 8532 vim_free(tempname); 8533 return buffer; 8534 } 8535 #endif 8536 8537 /* 8538 * Free the list of files returned by expand_wildcards() or other expansion 8539 * functions. 8540 */ 8541 void 8542 FreeWild(count, files) 8543 int count; 8544 char_u **files; 8545 { 8546 if (files == NULL || count <= 0) 8547 return; 8548 #if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */ 8549 /* 8550 * Is this still OK for when other functions than expand_wildcards() have 8551 * been used??? 8552 */ 8553 _fnexplodefree((char **)files); 8554 #else 8555 while (count--) 8556 vim_free(files[count]); 8557 vim_free(files); 8558 #endif 8559 } 8560 8561 /* 8562 * return TRUE when need to go to Insert mode because of 'insertmode'. 8563 * Don't do this when still processing a command or a mapping. 8564 * Don't do this when inside a ":normal" command. 8565 */ 8566 int 8567 goto_im() 8568 { 8569 return (p_im && stuff_empty() && typebuf_typed()); 8570 } 8571