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