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_source(hl_attr(HLF_W)); 2774 MSG_PUTS_ATTR(_("W10: Warning: Changing a readonly file"), 2775 hl_attr(HLF_W) | MSG_HIST); 2776 msg_clr_eos(); 2777 (void)msg_end(); 2778 if (msg_silent == 0 && !silent_mode) 2779 { 2780 out_flush(); 2781 ui_delay(1000L, TRUE); /* give the user time to think about it */ 2782 } 2783 curbuf->b_did_warn = TRUE; 2784 redraw_cmdline = FALSE; /* don't redraw and erase the message */ 2785 if (msg_row < Rows - 1) 2786 showmode(); 2787 } 2788 } 2789 2790 /* 2791 * Ask for a reply from the user, a 'y' or a 'n'. 2792 * No other characters are accepted, the message is repeated until a valid 2793 * reply is entered or CTRL-C is hit. 2794 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters 2795 * from any buffers but directly from the user. 2796 * 2797 * return the 'y' or 'n' 2798 */ 2799 int 2800 ask_yesno(str, direct) 2801 char_u *str; 2802 int direct; 2803 { 2804 int r = ' '; 2805 int save_State = State; 2806 2807 if (exiting) /* put terminal in raw mode for this question */ 2808 settmode(TMODE_RAW); 2809 ++no_wait_return; 2810 #ifdef USE_ON_FLY_SCROLL 2811 dont_scroll = TRUE; /* disallow scrolling here */ 2812 #endif 2813 State = CONFIRM; /* mouse behaves like with :confirm */ 2814 #ifdef FEAT_MOUSE 2815 setmouse(); /* disables mouse for xterm */ 2816 #endif 2817 ++no_mapping; 2818 ++allow_keys; /* no mapping here, but recognize keys */ 2819 2820 while (r != 'y' && r != 'n') 2821 { 2822 /* same highlighting as for wait_return */ 2823 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str); 2824 if (direct) 2825 r = get_keystroke(); 2826 else 2827 r = safe_vgetc(); 2828 if (r == Ctrl_C || r == ESC) 2829 r = 'n'; 2830 msg_putchar(r); /* show what you typed */ 2831 out_flush(); 2832 } 2833 --no_wait_return; 2834 State = save_State; 2835 #ifdef FEAT_MOUSE 2836 setmouse(); 2837 #endif 2838 --no_mapping; 2839 --allow_keys; 2840 2841 return r; 2842 } 2843 2844 /* 2845 * Get a key stroke directly from the user. 2846 * Ignores mouse clicks and scrollbar events, except a click for the left 2847 * button (used at the more prompt). 2848 * Doesn't use vgetc(), because it syncs undo and eats mapped characters. 2849 * Disadvantage: typeahead is ignored. 2850 * Translates the interrupt character for unix to ESC. 2851 */ 2852 int 2853 get_keystroke() 2854 { 2855 #define CBUFLEN 151 2856 char_u buf[CBUFLEN]; 2857 int len = 0; 2858 int n; 2859 int save_mapped_ctrl_c = mapped_ctrl_c; 2860 2861 mapped_ctrl_c = FALSE; /* mappings are not used here */ 2862 for (;;) 2863 { 2864 cursor_on(); 2865 out_flush(); 2866 2867 /* First time: blocking wait. Second time: wait up to 100ms for a 2868 * terminal code to complete. Leave some room for check_termcode() to 2869 * insert a key code into (max 5 chars plus NUL). And 2870 * fix_input_buffer() can triple the number of bytes. */ 2871 n = ui_inchar(buf + len, (CBUFLEN - 6 - len) / 3, 2872 len == 0 ? -1L : 100L, 0); 2873 if (n > 0) 2874 { 2875 /* Replace zero and CSI by a special key code. */ 2876 n = fix_input_buffer(buf + len, n, FALSE); 2877 len += n; 2878 } 2879 2880 /* incomplete termcode: get more characters */ 2881 if ((n = check_termcode(1, buf, len)) < 0) 2882 continue; 2883 /* found a termcode: adjust length */ 2884 if (n > 0) 2885 len = n; 2886 if (len == 0) /* nothing typed yet */ 2887 continue; 2888 2889 /* Handle modifier and/or special key code. */ 2890 n = buf[0]; 2891 if (n == K_SPECIAL) 2892 { 2893 n = TO_SPECIAL(buf[1], buf[2]); 2894 if (buf[1] == KS_MODIFIER 2895 || n == K_IGNORE 2896 #ifdef FEAT_MOUSE 2897 || n == K_LEFTMOUSE_NM 2898 || n == K_LEFTDRAG 2899 || n == K_LEFTRELEASE 2900 || n == K_LEFTRELEASE_NM 2901 || n == K_MIDDLEMOUSE 2902 || n == K_MIDDLEDRAG 2903 || n == K_MIDDLERELEASE 2904 || n == K_RIGHTMOUSE 2905 || n == K_RIGHTDRAG 2906 || n == K_RIGHTRELEASE 2907 || n == K_MOUSEDOWN 2908 || n == K_MOUSEUP 2909 || n == K_X1MOUSE 2910 || n == K_X1DRAG 2911 || n == K_X1RELEASE 2912 || n == K_X2MOUSE 2913 || n == K_X2DRAG 2914 || n == K_X2RELEASE 2915 # ifdef FEAT_GUI 2916 || n == K_VER_SCROLLBAR 2917 || n == K_HOR_SCROLLBAR 2918 # endif 2919 #endif 2920 ) 2921 { 2922 if (buf[1] == KS_MODIFIER) 2923 mod_mask = buf[2]; 2924 len -= 3; 2925 if (len > 0) 2926 mch_memmove(buf, buf + 3, (size_t)len); 2927 continue; 2928 } 2929 } 2930 #ifdef FEAT_MBYTE 2931 if (has_mbyte) 2932 { 2933 if (MB_BYTE2LEN(n) > len) 2934 continue; /* more bytes to get */ 2935 buf[len >= CBUFLEN ? CBUFLEN - 1 : len] = NUL; 2936 n = (*mb_ptr2char)(buf); 2937 } 2938 #endif 2939 #ifdef UNIX 2940 if (n == intr_char) 2941 n = ESC; 2942 #endif 2943 break; 2944 } 2945 2946 mapped_ctrl_c = save_mapped_ctrl_c; 2947 return n; 2948 } 2949 2950 /* 2951 * get a number from the user 2952 */ 2953 int 2954 get_number(colon) 2955 int colon; /* allow colon to abort */ 2956 { 2957 int n = 0; 2958 int c; 2959 2960 /* When not printing messages, the user won't know what to type, return a 2961 * zero (as if CR was hit). */ 2962 if (msg_silent != 0) 2963 return 0; 2964 2965 #ifdef USE_ON_FLY_SCROLL 2966 dont_scroll = TRUE; /* disallow scrolling here */ 2967 #endif 2968 ++no_mapping; 2969 ++allow_keys; /* no mapping here, but recognize keys */ 2970 for (;;) 2971 { 2972 windgoto(msg_row, msg_col); 2973 c = safe_vgetc(); 2974 if (VIM_ISDIGIT(c)) 2975 { 2976 n = n * 10 + c - '0'; 2977 msg_putchar(c); 2978 } 2979 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) 2980 { 2981 n /= 10; 2982 MSG_PUTS("\b \b"); 2983 } 2984 else if (n == 0 && c == ':' && colon) 2985 { 2986 stuffcharReadbuff(':'); 2987 if (!exmode_active) 2988 cmdline_row = msg_row; 2989 skip_redraw = TRUE; /* skip redraw once */ 2990 do_redraw = FALSE; 2991 break; 2992 } 2993 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) 2994 break; 2995 } 2996 --no_mapping; 2997 --allow_keys; 2998 return n; 2999 } 3000 3001 void 3002 msgmore(n) 3003 long n; 3004 { 3005 long pn; 3006 3007 if (global_busy /* no messages now, wait until global is finished */ 3008 || keep_msg != NULL /* there is a message already, skip this one */ 3009 || !messaging()) /* 'lazyredraw' set, don't do messages now */ 3010 return; 3011 3012 if (n > 0) 3013 pn = n; 3014 else 3015 pn = -n; 3016 3017 if (pn > p_report) 3018 { 3019 if (pn == 1) 3020 { 3021 if (n > 0) 3022 STRCPY(msg_buf, _("1 more line")); 3023 else 3024 STRCPY(msg_buf, _("1 line less")); 3025 } 3026 else 3027 { 3028 if (n > 0) 3029 sprintf((char *)msg_buf, _("%ld more lines"), pn); 3030 else 3031 sprintf((char *)msg_buf, _("%ld fewer lines"), pn); 3032 } 3033 if (got_int) 3034 STRCAT(msg_buf, _(" (Interrupted)")); 3035 if (msg(msg_buf)) 3036 { 3037 set_keep_msg(msg_buf); 3038 keep_msg_attr = 0; 3039 } 3040 } 3041 } 3042 3043 /* 3044 * flush map and typeahead buffers and give a warning for an error 3045 */ 3046 void 3047 beep_flush() 3048 { 3049 if (emsg_silent == 0) 3050 { 3051 flush_buffers(FALSE); 3052 vim_beep(); 3053 } 3054 } 3055 3056 /* 3057 * give a warning for an error 3058 */ 3059 void 3060 vim_beep() 3061 { 3062 if (emsg_silent == 0) 3063 { 3064 if (p_vb 3065 #ifdef FEAT_GUI 3066 /* While the GUI is starting up the termcap is set for the GUI 3067 * but the output still goes to a terminal. */ 3068 && !(gui.in_use && gui.starting) 3069 #endif 3070 ) 3071 { 3072 out_str(T_VB); 3073 } 3074 else 3075 { 3076 #ifdef MSDOS 3077 /* 3078 * The number of beeps outputted is reduced to avoid having to wait 3079 * for all the beeps to finish. This is only a problem on systems 3080 * where the beeps don't overlap. 3081 */ 3082 if (beep_count == 0 || beep_count == 10) 3083 { 3084 out_char(BELL); 3085 beep_count = 1; 3086 } 3087 else 3088 ++beep_count; 3089 #else 3090 out_char(BELL); 3091 #endif 3092 } 3093 } 3094 } 3095 3096 /* 3097 * To get the "real" home directory: 3098 * - get value of $HOME 3099 * For Unix: 3100 * - go to that directory 3101 * - do mch_dirname() to get the real name of that directory. 3102 * This also works with mounts and links. 3103 * Don't do this for MS-DOS, it will change the "current dir" for a drive. 3104 */ 3105 static char_u *homedir = NULL; 3106 3107 void 3108 init_homedir() 3109 { 3110 char_u *var; 3111 3112 #ifdef VMS 3113 var = mch_getenv((char_u *)"SYS$LOGIN"); 3114 #else 3115 var = mch_getenv((char_u *)"HOME"); 3116 #endif 3117 3118 if (var != NULL && *var == NUL) /* empty is same as not set */ 3119 var = NULL; 3120 3121 #ifdef WIN3264 3122 /* 3123 * Weird but true: $HOME may contain an indirect reference to another 3124 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set 3125 * when $HOME is being set. 3126 */ 3127 if (var != NULL && *var == '%') 3128 { 3129 char_u *p; 3130 char_u *exp; 3131 3132 p = vim_strchr(var + 1, '%'); 3133 if (p != NULL) 3134 { 3135 STRNCPY(NameBuff, var + 1, p - (var + 1)); 3136 NameBuff[p - (var + 1)] = NUL; 3137 exp = mch_getenv(NameBuff); 3138 if (exp != NULL && *exp != NUL 3139 && STRLEN(exp) + STRLEN(p) < MAXPATHL) 3140 { 3141 sprintf((char *)NameBuff, "%s%s", exp, p + 1); 3142 var = NameBuff; 3143 /* Also set $HOME, it's needed for _viminfo. */ 3144 vim_setenv((char_u *)"HOME", NameBuff); 3145 } 3146 } 3147 } 3148 3149 /* 3150 * Typically, $HOME is not defined on Windows, unless the user has 3151 * specifically defined it for Vim's sake. However, on Windows NT 3152 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for 3153 * each user. Try constructing $HOME from these. 3154 */ 3155 if (var == NULL) 3156 { 3157 char_u *homedrive, *homepath; 3158 3159 homedrive = mch_getenv((char_u *)"HOMEDRIVE"); 3160 homepath = mch_getenv((char_u *)"HOMEPATH"); 3161 if (homedrive != NULL && homepath != NULL 3162 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) 3163 { 3164 sprintf((char *)NameBuff, "%s%s", homedrive, homepath); 3165 if (NameBuff[0] != NUL) 3166 { 3167 var = NameBuff; 3168 /* Also set $HOME, it's needed for _viminfo. */ 3169 vim_setenv((char_u *)"HOME", NameBuff); 3170 } 3171 } 3172 } 3173 #endif 3174 3175 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) 3176 /* 3177 * Default home dir is C:/ 3178 * Best assumption we can make in such a situation. 3179 */ 3180 if (var == NULL) 3181 var = "C:/"; 3182 #endif 3183 if (var != NULL) 3184 { 3185 #ifdef UNIX 3186 /* 3187 * Change to the directory and get the actual path. This resolves 3188 * links. Don't do it when we can't return. 3189 */ 3190 if (mch_dirname(NameBuff, MAXPATHL) == OK 3191 && mch_chdir((char *)NameBuff) == 0) 3192 { 3193 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) 3194 var = IObuff; 3195 if (mch_chdir((char *)NameBuff) != 0) 3196 EMSG(_(e_prev_dir)); 3197 } 3198 #endif 3199 homedir = vim_strsave(var); 3200 } 3201 } 3202 3203 /* 3204 * Expand environment variable with path name. 3205 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. 3206 * Skips over "\ ", "\~" and "\$". 3207 * If anything fails no expansion is done and dst equals src. 3208 */ 3209 void 3210 expand_env(src, dst, dstlen) 3211 char_u *src; /* input string e.g. "$HOME/vim.hlp" */ 3212 char_u *dst; /* where to put the result */ 3213 int dstlen; /* maximum length of the result */ 3214 { 3215 expand_env_esc(src, dst, dstlen, FALSE); 3216 } 3217 3218 void 3219 expand_env_esc(src, dst, dstlen, esc) 3220 char_u *src; /* input string e.g. "$HOME/vim.hlp" */ 3221 char_u *dst; /* where to put the result */ 3222 int dstlen; /* maximum length of the result */ 3223 int esc; /* escape spaces in expanded variables */ 3224 { 3225 char_u *tail; 3226 int c; 3227 char_u *var; 3228 int copy_char; 3229 int mustfree; /* var was allocated, need to free it later */ 3230 int at_start = TRUE; /* at start of a name */ 3231 3232 src = skipwhite(src); 3233 --dstlen; /* leave one char space for "\," */ 3234 while (*src && dstlen > 0) 3235 { 3236 copy_char = TRUE; 3237 if (*src == '$' 3238 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) 3239 || *src == '%' 3240 #endif 3241 || (*src == '~' && at_start)) 3242 { 3243 mustfree = FALSE; 3244 3245 /* 3246 * The variable name is copied into dst temporarily, because it may 3247 * be a string in read-only memory and a NUL needs to be appended. 3248 */ 3249 if (*src != '~') /* environment var */ 3250 { 3251 tail = src + 1; 3252 var = dst; 3253 c = dstlen - 1; 3254 3255 #ifdef UNIX 3256 /* Unix has ${var-name} type environment vars */ 3257 if (*tail == '{' && !vim_isIDc('{')) 3258 { 3259 tail++; /* ignore '{' */ 3260 while (c-- > 0 && *tail && *tail != '}') 3261 *var++ = *tail++; 3262 } 3263 else 3264 #endif 3265 { 3266 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) 3267 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) 3268 || (*src == '%' && *tail != '%') 3269 #endif 3270 )) 3271 { 3272 #ifdef OS2 /* env vars only in uppercase */ 3273 *var++ = TOUPPER_LOC(*tail); 3274 tail++; /* toupper() may be a macro! */ 3275 #else 3276 *var++ = *tail++; 3277 #endif 3278 } 3279 } 3280 3281 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) 3282 # ifdef UNIX 3283 if (src[1] == '{' && *tail != '}') 3284 # else 3285 if (*src == '%' && *tail != '%') 3286 # endif 3287 var = NULL; 3288 else 3289 { 3290 # ifdef UNIX 3291 if (src[1] == '{') 3292 # else 3293 if (*src == '%') 3294 #endif 3295 ++tail; 3296 #endif 3297 *var = NUL; 3298 var = vim_getenv(dst, &mustfree); 3299 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) 3300 } 3301 #endif 3302 } 3303 /* home directory */ 3304 else if ( src[1] == NUL 3305 || vim_ispathsep(src[1]) 3306 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) 3307 { 3308 var = homedir; 3309 tail = src + 1; 3310 } 3311 else /* user directory */ 3312 { 3313 #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) 3314 /* 3315 * Copy ~user to dst[], so we can put a NUL after it. 3316 */ 3317 tail = src; 3318 var = dst; 3319 c = dstlen - 1; 3320 while ( c-- > 0 3321 && *tail 3322 && vim_isfilec(*tail) 3323 && !vim_ispathsep(*tail)) 3324 *var++ = *tail++; 3325 *var = NUL; 3326 # ifdef UNIX 3327 /* 3328 * If the system supports getpwnam(), use it. 3329 * Otherwise, or if getpwnam() fails, the shell is used to 3330 * expand ~user. This is slower and may fail if the shell 3331 * does not support ~user (old versions of /bin/sh). 3332 */ 3333 # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) 3334 { 3335 struct passwd *pw; 3336 3337 pw = getpwnam((char *)dst + 1); 3338 if (pw != NULL) 3339 var = (char_u *)pw->pw_dir; 3340 else 3341 var = NULL; 3342 } 3343 if (var == NULL) 3344 # endif 3345 { 3346 expand_T xpc; 3347 3348 ExpandInit(&xpc); 3349 xpc.xp_context = EXPAND_FILES; 3350 var = ExpandOne(&xpc, dst, NULL, 3351 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); 3352 ExpandCleanup(&xpc); 3353 mustfree = TRUE; 3354 } 3355 3356 # else /* !UNIX, thus VMS */ 3357 /* 3358 * USER_HOME is a comma-separated list of 3359 * directories to search for the user account in. 3360 */ 3361 { 3362 char_u test[MAXPATHL], paths[MAXPATHL]; 3363 char_u *path, *next_path, *ptr; 3364 struct stat st; 3365 3366 STRCPY(paths, USER_HOME); 3367 next_path = paths; 3368 while (*next_path) 3369 { 3370 for (path = next_path; *next_path && *next_path != ','; 3371 next_path++); 3372 if (*next_path) 3373 *next_path++ = NUL; 3374 STRCPY(test, path); 3375 STRCAT(test, "/"); 3376 STRCAT(test, dst + 1); 3377 if (mch_stat(test, &st) == 0) 3378 { 3379 var = alloc(STRLEN(test) + 1); 3380 STRCPY(var, test); 3381 mustfree = TRUE; 3382 break; 3383 } 3384 } 3385 } 3386 # endif /* UNIX */ 3387 #else 3388 /* cannot expand user's home directory, so don't try */ 3389 var = NULL; 3390 tail = (char_u *)""; /* for gcc */ 3391 #endif /* UNIX || VMS */ 3392 } 3393 3394 #ifdef BACKSLASH_IN_FILENAME 3395 /* If 'shellslash' is set change backslashes to forward slashes. 3396 * Can't use slash_adjust(), p_ssl may be set temporarily. */ 3397 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) 3398 { 3399 char_u *p = vim_strsave(var); 3400 3401 if (p != NULL) 3402 { 3403 if (mustfree) 3404 vim_free(var); 3405 var = p; 3406 mustfree = TRUE; 3407 forward_slash(var); 3408 } 3409 } 3410 #endif 3411 3412 /* If "var" contains white space, escape it with a backslash. 3413 * Required for ":e ~/tt" when $HOME includes a space. */ 3414 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) 3415 { 3416 char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); 3417 3418 if (p != NULL) 3419 { 3420 if (mustfree) 3421 vim_free(var); 3422 var = p; 3423 mustfree = TRUE; 3424 } 3425 } 3426 3427 if (var != NULL && *var != NUL 3428 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) 3429 { 3430 STRCPY(dst, var); 3431 dstlen -= (int)STRLEN(var); 3432 dst += STRLEN(var); 3433 /* if var[] ends in a path separator and tail[] starts 3434 * with it, skip a character */ 3435 if (*var != NUL && vim_ispathsep(dst[-1]) 3436 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) 3437 && dst[-1] != ':' 3438 #endif 3439 && vim_ispathsep(*tail)) 3440 ++tail; 3441 src = tail; 3442 copy_char = FALSE; 3443 } 3444 if (mustfree) 3445 vim_free(var); 3446 } 3447 3448 if (copy_char) /* copy at least one char */ 3449 { 3450 /* 3451 * Recogize the start of a new name, for '~'. 3452 */ 3453 at_start = FALSE; 3454 if (src[0] == '\\' && src[1] != NUL) 3455 { 3456 *dst++ = *src++; 3457 --dstlen; 3458 } 3459 else if (src[0] == ' ' || src[0] == ',') 3460 at_start = TRUE; 3461 *dst++ = *src++; 3462 --dstlen; 3463 } 3464 } 3465 *dst = NUL; 3466 } 3467 3468 /* 3469 * Vim's version of getenv(). 3470 * Special handling of $HOME, $VIM and $VIMRUNTIME. 3471 */ 3472 char_u * 3473 vim_getenv(name, mustfree) 3474 char_u *name; 3475 int *mustfree; /* set to TRUE when returned is allocated */ 3476 { 3477 char_u *p; 3478 char_u *pend; 3479 int vimruntime; 3480 3481 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) 3482 /* use "C:/" when $HOME is not set */ 3483 if (STRCMP(name, "HOME") == 0) 3484 return homedir; 3485 #endif 3486 3487 p = mch_getenv(name); 3488 if (p != NULL && *p == NUL) /* empty is the same as not set */ 3489 p = NULL; 3490 3491 if (p != NULL) 3492 return p; 3493 3494 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); 3495 if (!vimruntime && STRCMP(name, "VIM") != 0) 3496 return NULL; 3497 3498 /* 3499 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. 3500 * Don't do this when default_vimruntime_dir is non-empty. 3501 */ 3502 if (vimruntime 3503 #ifdef HAVE_PATHDEF 3504 && *default_vimruntime_dir == NUL 3505 #endif 3506 ) 3507 { 3508 p = mch_getenv((char_u *)"VIM"); 3509 if (p != NULL && *p == NUL) /* empty is the same as not set */ 3510 p = NULL; 3511 if (p != NULL) 3512 { 3513 p = vim_version_dir(p); 3514 if (p != NULL) 3515 *mustfree = TRUE; 3516 else 3517 p = mch_getenv((char_u *)"VIM"); 3518 } 3519 } 3520 3521 /* 3522 * When expanding $VIM or $VIMRUNTIME fails, try using: 3523 * - the directory name from 'helpfile' (unless it contains '$') 3524 * - the executable name from argv[0] 3525 */ 3526 if (p == NULL) 3527 { 3528 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) 3529 p = p_hf; 3530 #ifdef USE_EXE_NAME 3531 /* 3532 * Use the name of the executable, obtained from argv[0]. 3533 */ 3534 else 3535 p = exe_name; 3536 #endif 3537 if (p != NULL) 3538 { 3539 /* remove the file name */ 3540 pend = gettail(p); 3541 3542 /* remove "doc/" from 'helpfile', if present */ 3543 if (p == p_hf) 3544 pend = remove_tail(p, pend, (char_u *)"doc"); 3545 3546 #ifdef USE_EXE_NAME 3547 # ifdef MACOS_X 3548 /* remove "build/..." from exe_name, if present */ 3549 if (p == exe_name) 3550 { 3551 char_u *pend1; 3552 char_u *pend2; 3553 3554 pend1 = remove_tail(p, pend, (char_u *)"Contents/MacOS"); 3555 pend2 = remove_tail_with_ext(p, pend1, (char_u *)".app"); 3556 pend = remove_tail(p, pend2, (char_u *)"build"); 3557 /* When runnig from project builder get rid of the 3558 * build/???.app, otherwise keep the ???.app */ 3559 if (pend2 == pend) 3560 pend = pend1; 3561 } 3562 # endif 3563 /* remove "src/" from exe_name, if present */ 3564 if (p == exe_name) 3565 pend = remove_tail(p, pend, (char_u *)"src"); 3566 #endif 3567 3568 /* for $VIM, remove "runtime/" or "vim54/", if present */ 3569 if (!vimruntime) 3570 { 3571 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); 3572 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); 3573 } 3574 3575 /* remove trailing path separator */ 3576 #ifndef MACOS_CLASSIC 3577 /* With MacOS path (with colons) the final colon is required */ 3578 /* to avoid confusion between absoulute and relative path */ 3579 if (pend > p && vim_ispathsep(*(pend - 1))) 3580 --pend; 3581 #endif 3582 3583 /* check that the result is a directory name */ 3584 p = vim_strnsave(p, (int)(pend - p)); 3585 3586 if (p != NULL && !mch_isdir(p)) 3587 { 3588 vim_free(p); 3589 p = NULL; 3590 } 3591 else 3592 { 3593 #ifdef USE_EXE_NAME 3594 /* may add "/vim54" or "/runtime" if it exists */ 3595 if (vimruntime && (pend = vim_version_dir(p)) != NULL) 3596 { 3597 vim_free(p); 3598 p = pend; 3599 } 3600 #endif 3601 *mustfree = TRUE; 3602 } 3603 } 3604 } 3605 3606 #ifdef HAVE_PATHDEF 3607 /* When there is a pathdef.c file we can use default_vim_dir and 3608 * default_vimruntime_dir */ 3609 if (p == NULL) 3610 { 3611 /* Only use default_vimruntime_dir when it is not empty */ 3612 if (vimruntime && *default_vimruntime_dir != NUL) 3613 { 3614 p = default_vimruntime_dir; 3615 *mustfree = FALSE; 3616 } 3617 else if (*default_vim_dir != NUL) 3618 { 3619 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) 3620 *mustfree = TRUE; 3621 else 3622 { 3623 p = default_vim_dir; 3624 *mustfree = FALSE; 3625 } 3626 } 3627 } 3628 #endif 3629 3630 /* 3631 * Set the environment variable, so that the new value can be found fast 3632 * next time, and others can also use it (e.g. Perl). 3633 */ 3634 if (p != NULL) 3635 { 3636 if (vimruntime) 3637 { 3638 vim_setenv((char_u *)"VIMRUNTIME", p); 3639 didset_vimruntime = TRUE; 3640 #ifdef FEAT_GETTEXT 3641 { 3642 char_u *buf = alloc((unsigned int)STRLEN(p) + 6); 3643 3644 if (buf != NULL) 3645 { 3646 STRCPY(buf, p); 3647 STRCAT(buf, "/lang"); 3648 bindtextdomain(VIMPACKAGE, (char *)buf); 3649 vim_free(buf); 3650 } 3651 } 3652 #endif 3653 } 3654 else 3655 { 3656 vim_setenv((char_u *)"VIM", p); 3657 didset_vim = TRUE; 3658 } 3659 } 3660 return p; 3661 } 3662 3663 /* 3664 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. 3665 * Return NULL if not, return its name in allocated memory otherwise. 3666 */ 3667 static char_u * 3668 vim_version_dir(vimdir) 3669 char_u *vimdir; 3670 { 3671 char_u *p; 3672 3673 if (vimdir == NULL || *vimdir == NUL) 3674 return NULL; 3675 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); 3676 if (p != NULL && mch_isdir(p)) 3677 return p; 3678 vim_free(p); 3679 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); 3680 if (p != NULL && mch_isdir(p)) 3681 return p; 3682 vim_free(p); 3683 return NULL; 3684 } 3685 3686 /* 3687 * If the string between "p" and "pend" ends in "name/", return "pend" minus 3688 * the length of "name/". Otherwise return "pend". 3689 */ 3690 static char_u * 3691 remove_tail(p, pend, name) 3692 char_u *p; 3693 char_u *pend; 3694 char_u *name; 3695 { 3696 int len = (int)STRLEN(name) + 1; 3697 char_u *newend = pend - len; 3698 3699 if (newend >= p 3700 && fnamencmp(newend, name, len - 1) == 0 3701 && (newend == p || vim_ispathsep(*(newend - 1)))) 3702 return newend; 3703 return pend; 3704 } 3705 3706 #if defined(USE_EXE_NAME) && defined(MACOS_X) 3707 /* 3708 * If the string between "p" and "pend" ends in "???.ext/", return "pend" 3709 * minus the length of "???.ext/". Otherwise return "pend". 3710 */ 3711 static char_u * 3712 remove_tail_with_ext(p, pend, ext) 3713 char_u *p; 3714 char_u *pend; 3715 char_u *ext; 3716 { 3717 int len = (int)STRLEN(ext) + 1; 3718 char_u *newend = pend - len; 3719 3720 if (newend >= p && fnamencmp(newend, ext, len - 1) == 0) 3721 while (newend != p && !vim_ispathsep(*(newend - 1))) 3722 --newend; 3723 if (newend == p || vim_ispathsep(*(newend - 1))) 3724 return newend; 3725 return pend; 3726 } 3727 #endif 3728 3729 /* 3730 * Call expand_env() and store the result in an allocated string. 3731 * This is not very memory efficient, this expects the result to be freed 3732 * again soon. 3733 */ 3734 char_u * 3735 expand_env_save(src) 3736 char_u *src; 3737 { 3738 char_u *p; 3739 3740 p = alloc(MAXPATHL); 3741 if (p != NULL) 3742 expand_env(src, p, MAXPATHL); 3743 return p; 3744 } 3745 3746 /* 3747 * Our portable version of setenv. 3748 */ 3749 void 3750 vim_setenv(name, val) 3751 char_u *name; 3752 char_u *val; 3753 { 3754 #ifdef HAVE_SETENV 3755 mch_setenv((char *)name, (char *)val, 1); 3756 #else 3757 char_u *envbuf; 3758 3759 /* 3760 * Putenv does not copy the string, it has to remain 3761 * valid. The allocated memory will never be freed. 3762 */ 3763 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); 3764 if (envbuf != NULL) 3765 { 3766 sprintf((char *)envbuf, "%s=%s", name, val); 3767 putenv((char *)envbuf); 3768 } 3769 #endif 3770 } 3771 3772 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 3773 /* 3774 * Function given to ExpandGeneric() to obtain an environment variable name. 3775 */ 3776 /*ARGSUSED*/ 3777 char_u * 3778 get_env_name(xp, idx) 3779 expand_T *xp; 3780 int idx; 3781 { 3782 # if defined(AMIGA) || defined(__MRC__) || defined(__SC__) 3783 /* 3784 * No environ[] on the Amiga and on the Mac (using MPW). 3785 */ 3786 return NULL; 3787 # else 3788 # ifndef __WIN32__ 3789 /* Borland C++ 5.2 has this in a header file. */ 3790 extern char **environ; 3791 # endif 3792 static char_u name[100]; 3793 char_u *str; 3794 int n; 3795 3796 str = (char_u *)environ[idx]; 3797 if (str == NULL) 3798 return NULL; 3799 3800 for (n = 0; n < 99; ++n) 3801 { 3802 if (str[n] == '=' || str[n] == NUL) 3803 break; 3804 name[n] = str[n]; 3805 } 3806 name[n] = NUL; 3807 return name; 3808 # endif 3809 } 3810 #endif 3811 3812 /* 3813 * Replace home directory by "~" in each space or comma separated file name in 3814 * 'src'. 3815 * If anything fails (except when out of space) dst equals src. 3816 */ 3817 void 3818 home_replace(buf, src, dst, dstlen, one) 3819 buf_T *buf; /* when not NULL, check for help files */ 3820 char_u *src; /* input file name */ 3821 char_u *dst; /* where to put the result */ 3822 int dstlen; /* maximum length of the result */ 3823 int one; /* if TRUE, only replace one file name, include 3824 spaces and commas in the file name. */ 3825 { 3826 size_t dirlen = 0, envlen = 0; 3827 size_t len; 3828 char_u *homedir_env; 3829 char_u *p; 3830 3831 if (src == NULL) 3832 { 3833 *dst = NUL; 3834 return; 3835 } 3836 3837 /* 3838 * If the file is a help file, remove the path completely. 3839 */ 3840 if (buf != NULL && buf->b_help) 3841 { 3842 STRCPY(dst, gettail(src)); 3843 return; 3844 } 3845 3846 /* 3847 * We check both the value of the $HOME environment variable and the 3848 * "real" home directory. 3849 */ 3850 if (homedir != NULL) 3851 dirlen = STRLEN(homedir); 3852 3853 #ifdef VMS 3854 homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); 3855 #else 3856 homedir_env = mch_getenv((char_u *)"HOME"); 3857 #endif 3858 3859 if (homedir_env != NULL && *homedir_env == NUL) 3860 homedir_env = NULL; 3861 if (homedir_env != NULL) 3862 envlen = STRLEN(homedir_env); 3863 3864 if (!one) 3865 src = skipwhite(src); 3866 while (*src && dstlen > 0) 3867 { 3868 /* 3869 * Here we are at the beginning of a file name. 3870 * First, check to see if the beginning of the file name matches 3871 * $HOME or the "real" home directory. Check that there is a '/' 3872 * after the match (so that if e.g. the file is "/home/pieter/bla", 3873 * and the home directory is "/home/piet", the file does not end up 3874 * as "~er/bla" (which would seem to indicate the file "bla" in user 3875 * er's home directory)). 3876 */ 3877 p = homedir; 3878 len = dirlen; 3879 for (;;) 3880 { 3881 if ( len 3882 && fnamencmp(src, p, len) == 0 3883 && (vim_ispathsep(src[len]) 3884 || (!one && (src[len] == ',' || src[len] == ' ')) 3885 || src[len] == NUL)) 3886 { 3887 src += len; 3888 if (--dstlen > 0) 3889 *dst++ = '~'; 3890 3891 /* 3892 * If it's just the home directory, add "/". 3893 */ 3894 if (!vim_ispathsep(src[0]) && --dstlen > 0) 3895 *dst++ = '/'; 3896 break; 3897 } 3898 if (p == homedir_env) 3899 break; 3900 p = homedir_env; 3901 len = envlen; 3902 } 3903 3904 /* if (!one) skip to separator: space or comma */ 3905 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) 3906 *dst++ = *src++; 3907 /* skip separator */ 3908 while ((*src == ' ' || *src == ',') && --dstlen > 0) 3909 *dst++ = *src++; 3910 } 3911 /* if (dstlen == 0) out of space, what to do??? */ 3912 3913 *dst = NUL; 3914 } 3915 3916 /* 3917 * Like home_replace, store the replaced string in allocated memory. 3918 * When something fails, NULL is returned. 3919 */ 3920 char_u * 3921 home_replace_save(buf, src) 3922 buf_T *buf; /* when not NULL, check for help files */ 3923 char_u *src; /* input file name */ 3924 { 3925 char_u *dst; 3926 unsigned len; 3927 3928 len = 3; /* space for "~/" and trailing NUL */ 3929 if (src != NULL) /* just in case */ 3930 len += (unsigned)STRLEN(src); 3931 dst = alloc(len); 3932 if (dst != NULL) 3933 home_replace(buf, src, dst, len, TRUE); 3934 return dst; 3935 } 3936 3937 /* 3938 * Compare two file names and return: 3939 * FPC_SAME if they both exist and are the same file. 3940 * FPC_SAMEX if they both don't exist and have the same file name. 3941 * FPC_DIFF if they both exist and are different files. 3942 * FPC_NOTX if they both don't exist. 3943 * FPC_DIFFX if one of them doesn't exist. 3944 * For the first name environment variables are expanded 3945 */ 3946 int 3947 fullpathcmp(s1, s2, checkname) 3948 char_u *s1, *s2; 3949 int checkname; /* when both don't exist, check file names */ 3950 { 3951 #ifdef UNIX 3952 char_u exp1[MAXPATHL]; 3953 char_u full1[MAXPATHL]; 3954 char_u full2[MAXPATHL]; 3955 struct stat st1, st2; 3956 int r1, r2; 3957 3958 expand_env(s1, exp1, MAXPATHL); 3959 r1 = mch_stat((char *)exp1, &st1); 3960 r2 = mch_stat((char *)s2, &st2); 3961 if (r1 != 0 && r2 != 0) 3962 { 3963 /* if mch_stat() doesn't work, may compare the names */ 3964 if (checkname) 3965 { 3966 if (fnamecmp(exp1, s2) == 0) 3967 return FPC_SAMEX; 3968 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); 3969 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); 3970 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) 3971 return FPC_SAMEX; 3972 } 3973 return FPC_NOTX; 3974 } 3975 if (r1 != 0 || r2 != 0) 3976 return FPC_DIFFX; 3977 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) 3978 return FPC_SAME; 3979 return FPC_DIFF; 3980 #else 3981 char_u *exp1; /* expanded s1 */ 3982 char_u *full1; /* full path of s1 */ 3983 char_u *full2; /* full path of s2 */ 3984 int retval = FPC_DIFF; 3985 int r1, r2; 3986 3987 /* allocate one buffer to store three paths (alloc()/free() is slow!) */ 3988 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) 3989 { 3990 full1 = exp1 + MAXPATHL; 3991 full2 = full1 + MAXPATHL; 3992 3993 expand_env(s1, exp1, MAXPATHL); 3994 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); 3995 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); 3996 3997 /* If vim_FullName() fails, the file probably doesn't exist. */ 3998 if (r1 != OK && r2 != OK) 3999 { 4000 if (checkname && fnamecmp(exp1, s2) == 0) 4001 retval = FPC_SAMEX; 4002 else 4003 retval = FPC_NOTX; 4004 } 4005 else if (r1 != OK || r2 != OK) 4006 retval = FPC_DIFFX; 4007 else if (fnamecmp(full1, full2)) 4008 retval = FPC_DIFF; 4009 else 4010 retval = FPC_SAME; 4011 vim_free(exp1); 4012 } 4013 return retval; 4014 #endif 4015 } 4016 4017 /* 4018 * Get the tail of a path: the file name. 4019 * Fail safe: never returns NULL. 4020 */ 4021 char_u * 4022 gettail(fname) 4023 char_u *fname; 4024 { 4025 char_u *p1, *p2; 4026 4027 if (fname == NULL) 4028 return (char_u *)""; 4029 for (p1 = p2 = fname; *p2; ) /* find last part of path */ 4030 { 4031 if (vim_ispathsep(*p2)) 4032 p1 = p2 + 1; 4033 #ifdef FEAT_MBYTE 4034 if (has_mbyte) 4035 p2 += (*mb_ptr2len_check)(p2); 4036 else 4037 #endif 4038 ++p2; 4039 } 4040 return p1; 4041 } 4042 4043 /* 4044 * get the next path component (just after the next path separator). 4045 */ 4046 char_u * 4047 getnextcomp(fname) 4048 char_u *fname; 4049 { 4050 while (*fname && !vim_ispathsep(*fname)) 4051 ++fname; 4052 if (*fname) 4053 ++fname; 4054 return fname; 4055 } 4056 4057 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ 4058 || defined(FEAT_SESSION) || defined(MSWIN) \ 4059 || ((defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)) \ 4060 && (defined(FEAT_WINDOWS) || defined(FEAT_DND))) \ 4061 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \ 4062 || defined(PROTO) 4063 /* 4064 * Get a pointer to one character past the head of a path name. 4065 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. 4066 * If there is no head, path is returned. 4067 */ 4068 char_u * 4069 get_past_head(path) 4070 char_u *path; 4071 { 4072 char_u *retval; 4073 4074 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) 4075 /* may skip "c:" */ 4076 if (isalpha(path[0]) && path[1] == ':') 4077 retval = path + 2; 4078 else 4079 retval = path; 4080 #else 4081 # if defined(AMIGA) 4082 /* may skip "label:" */ 4083 retval = vim_strchr(path, ':'); 4084 if (retval == NULL) 4085 retval = path; 4086 # else /* Unix */ 4087 retval = path; 4088 # endif 4089 #endif 4090 4091 while (vim_ispathsep(*retval)) 4092 ++retval; 4093 4094 return retval; 4095 } 4096 #endif 4097 4098 /* 4099 * return TRUE if 'c' is a path separator. 4100 */ 4101 int 4102 vim_ispathsep(c) 4103 int c; 4104 { 4105 #ifdef RISCOS 4106 return (c == '.' || c == ':'); 4107 #else 4108 # ifdef UNIX 4109 return (c == '/'); /* UNIX has ':' inside file names */ 4110 # else 4111 # ifdef BACKSLASH_IN_FILENAME 4112 return (c == ':' || c == '/' || c == '\\'); 4113 # else 4114 # ifdef VMS 4115 /* server"user passwd"::device:[full.path.name]fname.extension;version" */ 4116 return (c == ':' || c == '[' || c == ']' || c == '/' 4117 || c == '<' || c == '>' || c == '"' ); 4118 # else 4119 # ifdef COLON_AS_PATHSEP 4120 return (c == ':'); 4121 # else /* Amiga */ 4122 return (c == ':' || c == '/'); 4123 # endif 4124 # endif /* VMS */ 4125 # endif 4126 # endif 4127 #endif /* RISC OS */ 4128 } 4129 4130 #if defined(FEAT_SEARCHPATH) || defined(PROTO) 4131 /* 4132 * return TRUE if 'c' is a path list separator. 4133 */ 4134 int 4135 vim_ispathlistsep(c) 4136 int c; 4137 { 4138 #ifdef UNIX 4139 return (c == ':'); 4140 #else 4141 return (c == ';'); /* might not be rigth for every system... */ 4142 #endif 4143 } 4144 #endif 4145 4146 #if (defined(CASE_INSENSITIVE_FILENAME) && defined(BACKSLASH_IN_FILENAME)) \ 4147 || defined(PROTO) 4148 /* 4149 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally. 4150 */ 4151 int 4152 vim_fnamecmp(x, y) 4153 char_u *x, *y; 4154 { 4155 return vim_fnamencmp(x, y, MAXPATHL); 4156 } 4157 4158 int 4159 vim_fnamencmp(x, y, len) 4160 char_u *x, *y; 4161 size_t len; 4162 { 4163 while (len > 0 && *x && *y) 4164 { 4165 if (TOLOWER_LOC(*x) != TOLOWER_LOC(*y) 4166 && !(*x == '/' && *y == '\\') 4167 && !(*x == '\\' && *y == '/')) 4168 break; 4169 ++x; 4170 ++y; 4171 --len; 4172 } 4173 if (len == 0) 4174 return 0; 4175 return (*x - *y); 4176 } 4177 #endif 4178 4179 /* 4180 * Concatenate file names fname1 and fname2 into allocated memory. 4181 * Only add a '/' or '\\' when 'sep' is TRUE and it is neccesary. 4182 */ 4183 char_u * 4184 concat_fnames(fname1, fname2, sep) 4185 char_u *fname1; 4186 char_u *fname2; 4187 int sep; 4188 { 4189 char_u *dest; 4190 4191 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); 4192 if (dest != NULL) 4193 { 4194 STRCPY(dest, fname1); 4195 if (sep) 4196 add_pathsep(dest); 4197 STRCAT(dest, fname2); 4198 } 4199 return dest; 4200 } 4201 4202 /* 4203 * Add a path separator to a file name, unless it already ends in a path 4204 * separator. 4205 */ 4206 void 4207 add_pathsep(p) 4208 char_u *p; 4209 { 4210 if (*p != NUL && !vim_ispathsep(*(p + STRLEN(p) - 1))) 4211 STRCAT(p, PATHSEPSTR); 4212 } 4213 4214 /* 4215 * FullName_save - Make an allocated copy of a full file name. 4216 * Returns NULL when out of memory. 4217 */ 4218 char_u * 4219 FullName_save(fname, force) 4220 char_u *fname; 4221 int force; /* force expansion, even when it already looks 4222 like a full path name */ 4223 { 4224 char_u *buf; 4225 char_u *new_fname = NULL; 4226 4227 if (fname == NULL) 4228 return NULL; 4229 4230 buf = alloc((unsigned)MAXPATHL); 4231 if (buf != NULL) 4232 { 4233 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) 4234 new_fname = vim_strsave(buf); 4235 else 4236 new_fname = vim_strsave(fname); 4237 vim_free(buf); 4238 } 4239 return new_fname; 4240 } 4241 4242 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) 4243 4244 static char_u *skip_string __ARGS((char_u *p)); 4245 4246 /* 4247 * Find the start of a comment, not knowing if we are in a comment right now. 4248 * Search starts at w_cursor.lnum and goes backwards. 4249 */ 4250 pos_T * 4251 find_start_comment(ind_maxcomment) /* XXX */ 4252 int ind_maxcomment; 4253 { 4254 pos_T *pos; 4255 char_u *line; 4256 char_u *p; 4257 4258 if ((pos = findmatchlimit(NULL, '*', FM_BACKWARD, ind_maxcomment)) == NULL) 4259 return NULL; 4260 4261 /* 4262 * Check if the comment start we found is inside a string. 4263 */ 4264 line = ml_get(pos->lnum); 4265 for (p = line; *p && (unsigned)(p - line) < pos->col; ++p) 4266 p = skip_string(p); 4267 if ((unsigned)(p - line) > pos->col) 4268 return NULL; 4269 return pos; 4270 } 4271 4272 /* 4273 * Skip to the end of a "string" and a 'c' character. 4274 * If there is no string or character, return argument unmodified. 4275 */ 4276 static char_u * 4277 skip_string(p) 4278 char_u *p; 4279 { 4280 int i; 4281 4282 /* 4283 * We loop, because strings may be concatenated: "date""time". 4284 */ 4285 for ( ; ; ++p) 4286 { 4287 if (p[0] == '\'') /* 'c' or '\n' or '\000' */ 4288 { 4289 if (!p[1]) /* ' at end of line */ 4290 break; 4291 i = 2; 4292 if (p[1] == '\\') /* '\n' or '\000' */ 4293 { 4294 ++i; 4295 while (vim_isdigit(p[i - 1])) /* '\000' */ 4296 ++i; 4297 } 4298 if (p[i] == '\'') /* check for trailing ' */ 4299 { 4300 p += i; 4301 continue; 4302 } 4303 } 4304 else if (p[0] == '"') /* start of string */ 4305 { 4306 for (++p; p[0]; ++p) 4307 { 4308 if (p[0] == '\\' && p[1] != NUL) 4309 ++p; 4310 else if (p[0] == '"') /* end of string */ 4311 break; 4312 } 4313 if (p[0] == '"') 4314 continue; 4315 } 4316 break; /* no string found */ 4317 } 4318 if (!*p) 4319 --p; /* backup from NUL */ 4320 return p; 4321 } 4322 #endif /* FEAT_CINDENT || FEAT_SYN_HL */ 4323 4324 #if defined(FEAT_CINDENT) || defined(PROTO) 4325 4326 /* 4327 * Do C or expression indenting on the current line. 4328 */ 4329 void 4330 do_c_expr_indent() 4331 { 4332 # ifdef FEAT_EVAL 4333 if (*curbuf->b_p_inde != NUL) 4334 fixthisline(get_expr_indent); 4335 else 4336 # endif 4337 fixthisline(get_c_indent); 4338 } 4339 4340 /* 4341 * Functions for C-indenting. 4342 * Most of this originally comes from Eric Fischer. 4343 */ 4344 /* 4345 * Below "XXX" means that this function may unlock the current line. 4346 */ 4347 4348 static char_u *cin_skipcomment __ARGS((char_u *)); 4349 static int cin_nocode __ARGS((char_u *)); 4350 static pos_T *find_line_comment __ARGS((void)); 4351 static int cin_islabel_skip __ARGS((char_u **)); 4352 static int cin_isdefault __ARGS((char_u *)); 4353 static char_u *after_label __ARGS((char_u *l)); 4354 static int get_indent_nolabel __ARGS((linenr_T lnum)); 4355 static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment)); 4356 static int cin_first_id_amount __ARGS((void)); 4357 static int cin_get_equal_amount __ARGS((linenr_T lnum)); 4358 static int cin_ispreproc __ARGS((char_u *)); 4359 static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump)); 4360 static int cin_iscomment __ARGS((char_u *)); 4361 static int cin_islinecomment __ARGS((char_u *)); 4362 static int cin_isterminated __ARGS((char_u *, int, int)); 4363 static int cin_isinit __ARGS((void)); 4364 static int cin_isfuncdecl __ARGS((char_u **, linenr_T)); 4365 static int cin_isif __ARGS((char_u *)); 4366 static int cin_iselse __ARGS((char_u *)); 4367 static int cin_isdo __ARGS((char_u *)); 4368 static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int)); 4369 static int cin_isbreak __ARGS((char_u *)); 4370 static int cin_is_cpp_baseclass __ARGS((char_u *line, colnr_T *col)); 4371 static int cin_ends_in __ARGS((char_u *, char_u *, char_u *)); 4372 static int cin_skip2pos __ARGS((pos_T *trypos)); 4373 static pos_T *find_start_brace __ARGS((int)); 4374 static pos_T *find_match_paren __ARGS((int, int)); 4375 static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos)); 4376 static int find_last_paren __ARGS((char_u *l, int start, int end)); 4377 static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment)); 4378 4379 /* 4380 * Skip over white space and C comments within the line. 4381 */ 4382 static char_u * 4383 cin_skipcomment(s) 4384 char_u *s; 4385 { 4386 while (*s) 4387 { 4388 s = skipwhite(s); 4389 if (*s != '/') 4390 break; 4391 ++s; 4392 if (*s == '/') /* slash-slash comment continues till eol */ 4393 { 4394 s += STRLEN(s); 4395 break; 4396 } 4397 if (*s != '*') 4398 break; 4399 for (++s; *s; ++s) /* skip slash-star comment */ 4400 if (s[0] == '*' && s[1] == '/') 4401 { 4402 s += 2; 4403 break; 4404 } 4405 } 4406 return s; 4407 } 4408 4409 /* 4410 * Return TRUE if there there is no code at *s. White space and comments are 4411 * not considered code. 4412 */ 4413 static int 4414 cin_nocode(s) 4415 char_u *s; 4416 { 4417 return *cin_skipcomment(s) == NUL; 4418 } 4419 4420 /* 4421 * Check previous lines for a "//" line comment, skipping over blank lines. 4422 */ 4423 static pos_T * 4424 find_line_comment() /* XXX */ 4425 { 4426 static pos_T pos; 4427 char_u *line; 4428 char_u *p; 4429 4430 pos = curwin->w_cursor; 4431 while (--pos.lnum > 0) 4432 { 4433 line = ml_get(pos.lnum); 4434 p = skipwhite(line); 4435 if (cin_islinecomment(p)) 4436 { 4437 pos.col = (int)(p - line); 4438 return &pos; 4439 } 4440 if (*p != NUL) 4441 break; 4442 } 4443 return NULL; 4444 } 4445 4446 /* 4447 * Check if string matches "label:"; move to character after ':' if true. 4448 */ 4449 static int 4450 cin_islabel_skip(s) 4451 char_u **s; 4452 { 4453 if (!vim_isIDc(**s)) /* need at least one ID character */ 4454 return FALSE; 4455 4456 while (vim_isIDc(**s)) 4457 (*s)++; 4458 4459 *s = cin_skipcomment(*s); 4460 4461 /* "::" is not a label, it's C++ */ 4462 return (**s == ':' && *++*s != ':'); 4463 } 4464 4465 /* 4466 * Recognize a label: "label:". 4467 * Note: curwin->w_cursor must be where we are looking for the label. 4468 */ 4469 int 4470 cin_islabel(ind_maxcomment) /* XXX */ 4471 int ind_maxcomment; 4472 { 4473 char_u *s; 4474 4475 s = cin_skipcomment(ml_get_curline()); 4476 4477 /* 4478 * Exclude "default" from labels, since it should be indented 4479 * like a switch label. Same for C++ scope declarations. 4480 */ 4481 if (cin_isdefault(s)) 4482 return FALSE; 4483 if (cin_isscopedecl(s)) 4484 return FALSE; 4485 4486 if (cin_islabel_skip(&s)) 4487 { 4488 /* 4489 * Only accept a label if the previous line is terminated or is a case 4490 * label. 4491 */ 4492 pos_T cursor_save; 4493 pos_T *trypos; 4494 char_u *line; 4495 4496 cursor_save = curwin->w_cursor; 4497 while (curwin->w_cursor.lnum > 1) 4498 { 4499 --curwin->w_cursor.lnum; 4500 4501 /* 4502 * If we're in a comment now, skip to the start of the comment. 4503 */ 4504 curwin->w_cursor.col = 0; 4505 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ 4506 curwin->w_cursor = *trypos; 4507 4508 line = ml_get_curline(); 4509 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ 4510 continue; 4511 if (*(line = cin_skipcomment(line)) == NUL) 4512 continue; 4513 4514 curwin->w_cursor = cursor_save; 4515 if (cin_isterminated(line, TRUE, FALSE) 4516 || cin_isscopedecl(line) 4517 || cin_iscase(line) 4518 || (cin_islabel_skip(&line) && cin_nocode(line))) 4519 return TRUE; 4520 return FALSE; 4521 } 4522 curwin->w_cursor = cursor_save; 4523 return TRUE; /* label at start of file??? */ 4524 } 4525 return FALSE; 4526 } 4527 4528 /* 4529 * Recognize structure initialization and enumerations. 4530 * Q&D-Implementation: 4531 * check for "=" at end or "[typedef] enum" at beginning of line. 4532 */ 4533 static int 4534 cin_isinit(void) 4535 { 4536 char_u *s; 4537 4538 s = cin_skipcomment(ml_get_curline()); 4539 4540 if (STRNCMP(s, "typedef", 7) == 0 && !vim_isIDc(s[7])) 4541 s = cin_skipcomment(s + 7); 4542 4543 if (STRNCMP(s, "enum", 4) == 0 && !vim_isIDc(s[4])) 4544 return TRUE; 4545 4546 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) 4547 return TRUE; 4548 4549 return FALSE; 4550 } 4551 4552 /* 4553 * Recognize a switch label: "case .*:" or "default:". 4554 */ 4555 int 4556 cin_iscase(s) 4557 char_u *s; 4558 { 4559 s = cin_skipcomment(s); 4560 if (STRNCMP(s, "case", 4) == 0 && !vim_isIDc(s[4])) 4561 { 4562 for (s += 4; *s; ++s) 4563 { 4564 s = cin_skipcomment(s); 4565 if (*s == ':') 4566 { 4567 if (s[1] == ':') /* skip over "::" for C++ */ 4568 ++s; 4569 else 4570 return TRUE; 4571 } 4572 if (*s == '\'' && s[1] && s[2] == '\'') 4573 s += 2; /* skip over '.' */ 4574 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) 4575 return FALSE; /* stop at comment */ 4576 else if (*s == '"') 4577 return FALSE; /* stop at string */ 4578 } 4579 return FALSE; 4580 } 4581 4582 if (cin_isdefault(s)) 4583 return TRUE; 4584 return FALSE; 4585 } 4586 4587 /* 4588 * Recognize a "default" switch label. 4589 */ 4590 static int 4591 cin_isdefault(s) 4592 char_u *s; 4593 { 4594 return (STRNCMP(s, "default", 7) == 0 4595 && *(s = cin_skipcomment(s + 7)) == ':' 4596 && s[1] != ':'); 4597 } 4598 4599 /* 4600 * Recognize a "public/private/proctected" scope declaration label. 4601 */ 4602 int 4603 cin_isscopedecl(s) 4604 char_u *s; 4605 { 4606 int i; 4607 4608 s = cin_skipcomment(s); 4609 if (STRNCMP(s, "public", 6) == 0) 4610 i = 6; 4611 else if (STRNCMP(s, "protected", 9) == 0) 4612 i = 9; 4613 else if (STRNCMP(s, "private", 7) == 0) 4614 i = 7; 4615 else 4616 return FALSE; 4617 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); 4618 } 4619 4620 /* 4621 * Return a pointer to the first non-empty non-comment character after a ':'. 4622 * Return NULL if not found. 4623 * case 234: a = b; 4624 * ^ 4625 */ 4626 static char_u * 4627 after_label(l) 4628 char_u *l; 4629 { 4630 for ( ; *l; ++l) 4631 { 4632 if (*l == ':') 4633 { 4634 if (l[1] == ':') /* skip over "::" for C++ */ 4635 ++l; 4636 else if (!cin_iscase(l + 1)) 4637 break; 4638 } 4639 else if (*l == '\'' && l[1] && l[2] == '\'') 4640 l += 2; /* skip over 'x' */ 4641 } 4642 if (*l == NUL) 4643 return NULL; 4644 l = cin_skipcomment(l + 1); 4645 if (*l == NUL) 4646 return NULL; 4647 return l; 4648 } 4649 4650 /* 4651 * Get indent of line "lnum", skipping a label. 4652 * Return 0 if there is nothing after the label. 4653 */ 4654 static int 4655 get_indent_nolabel(lnum) /* XXX */ 4656 linenr_T lnum; 4657 { 4658 char_u *l; 4659 pos_T fp; 4660 colnr_T col; 4661 char_u *p; 4662 4663 l = ml_get(lnum); 4664 p = after_label(l); 4665 if (p == NULL) 4666 return 0; 4667 4668 fp.col = (colnr_T)(p - l); 4669 fp.lnum = lnum; 4670 getvcol(curwin, &fp, &col, NULL, NULL); 4671 return (int)col; 4672 } 4673 4674 /* 4675 * Find indent for line "lnum", ignoring any case or jump label. 4676 * Also return a pointer to the text (after the label). 4677 * label: if (asdf && asdfasdf) 4678 * ^ 4679 */ 4680 static int 4681 skip_label(lnum, pp, ind_maxcomment) 4682 linenr_T lnum; 4683 char_u **pp; 4684 int ind_maxcomment; 4685 { 4686 char_u *l; 4687 int amount; 4688 pos_T cursor_save; 4689 4690 cursor_save = curwin->w_cursor; 4691 curwin->w_cursor.lnum = lnum; 4692 l = ml_get_curline(); 4693 /* XXX */ 4694 if (cin_iscase(l) || cin_isscopedecl(l) || cin_islabel(ind_maxcomment)) 4695 { 4696 amount = get_indent_nolabel(lnum); 4697 l = after_label(ml_get_curline()); 4698 if (l == NULL) /* just in case */ 4699 l = ml_get_curline(); 4700 } 4701 else 4702 { 4703 amount = get_indent(); 4704 l = ml_get_curline(); 4705 } 4706 *pp = l; 4707 4708 curwin->w_cursor = cursor_save; 4709 return amount; 4710 } 4711 4712 /* 4713 * Return the indent of the first variable name after a type in a declaration. 4714 * int a, indent of "a" 4715 * static struct foo b, indent of "b" 4716 * enum bla c, indent of "c" 4717 * Returns zero when it doesn't look like a declaration. 4718 */ 4719 static int 4720 cin_first_id_amount() 4721 { 4722 char_u *line, *p, *s; 4723 int len; 4724 pos_T fp; 4725 colnr_T col; 4726 4727 line = ml_get_curline(); 4728 p = skipwhite(line); 4729 len = skiptowhite(p) - p; 4730 if (len == 6 && STRNCMP(p, "static", 6) == 0) 4731 { 4732 p = skipwhite(p + 6); 4733 len = skiptowhite(p) - p; 4734 } 4735 if (len == 6 && STRNCMP(p, "struct", 6) == 0) 4736 p = skipwhite(p + 6); 4737 else if (len == 4 && STRNCMP(p, "enum", 4) == 0) 4738 p = skipwhite(p + 4); 4739 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) 4740 || (len == 6 && STRNCMP(p, "signed", 6) == 0)) 4741 { 4742 s = skipwhite(p + len); 4743 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3])) 4744 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4])) 4745 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5])) 4746 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4]))) 4747 p = s; 4748 } 4749 for (len = 0; vim_isIDc(p[len]); ++len) 4750 ; 4751 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p)) 4752 return 0; 4753 4754 p = skipwhite(p + len); 4755 fp.lnum = curwin->w_cursor.lnum; 4756 fp.col = (colnr_T)(p - line); 4757 getvcol(curwin, &fp, &col, NULL, NULL); 4758 return (int)col; 4759 } 4760 4761 /* 4762 * Return the indent of the first non-blank after an equal sign. 4763 * char *foo = "here"; 4764 * Return zero if no (useful) equal sign found. 4765 * Return -1 if the line above "lnum" ends in a backslash. 4766 * foo = "asdf\ 4767 * asdf\ 4768 * here"; 4769 */ 4770 static int 4771 cin_get_equal_amount(lnum) 4772 linenr_T lnum; 4773 { 4774 char_u *line; 4775 char_u *s; 4776 colnr_T col; 4777 pos_T fp; 4778 4779 if (lnum > 1) 4780 { 4781 line = ml_get(lnum - 1); 4782 if (*line != NUL && line[STRLEN(line) - 1] == '\\') 4783 return -1; 4784 } 4785 4786 line = s = ml_get(lnum); 4787 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) 4788 { 4789 if (cin_iscomment(s)) /* ignore comments */ 4790 s = cin_skipcomment(s); 4791 else 4792 ++s; 4793 } 4794 if (*s != '=') 4795 return 0; 4796 4797 s = skipwhite(s + 1); 4798 if (cin_nocode(s)) 4799 return 0; 4800 4801 if (*s == '"') /* nice alignment for continued strings */ 4802 ++s; 4803 4804 fp.lnum = lnum; 4805 fp.col = (colnr_T)(s - line); 4806 getvcol(curwin, &fp, &col, NULL, NULL); 4807 return (int)col; 4808 } 4809 4810 /* 4811 * Recognize a preprocessor statement: Any line that starts with '#'. 4812 */ 4813 static int 4814 cin_ispreproc(s) 4815 char_u *s; 4816 { 4817 s = skipwhite(s); 4818 if (*s == '#') 4819 return TRUE; 4820 return FALSE; 4821 } 4822 4823 /* 4824 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a 4825 * continuation line of a preprocessor statement. Decrease "*lnump" to the 4826 * start and return the line in "*pp". 4827 */ 4828 static int 4829 cin_ispreproc_cont(pp, lnump) 4830 char_u **pp; 4831 linenr_T *lnump; 4832 { 4833 char_u *line = *pp; 4834 linenr_T lnum = *lnump; 4835 int retval = FALSE; 4836 4837 while (1) 4838 { 4839 if (cin_ispreproc(line)) 4840 { 4841 retval = TRUE; 4842 *lnump = lnum; 4843 break; 4844 } 4845 if (lnum == 1) 4846 break; 4847 line = ml_get(--lnum); 4848 if (*line == NUL || line[STRLEN(line) - 1] != '\\') 4849 break; 4850 } 4851 4852 if (lnum != *lnump) 4853 *pp = ml_get(*lnump); 4854 return retval; 4855 } 4856 4857 /* 4858 * Recognize the start of a C or C++ comment. 4859 */ 4860 static int 4861 cin_iscomment(p) 4862 char_u *p; 4863 { 4864 return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); 4865 } 4866 4867 /* 4868 * Recognize the start of a "//" comment. 4869 */ 4870 static int 4871 cin_islinecomment(p) 4872 char_u *p; 4873 { 4874 return (p[0] == '/' && p[1] == '/'); 4875 } 4876 4877 /* 4878 * Recognize a line that starts with '{' or '}', or ends with ';', '{' or '}'. 4879 * Don't consider "} else" a terminated line. 4880 * Return the character terminating the line (ending char's have precedence if 4881 * both apply in order to determine initializations). 4882 */ 4883 static int 4884 cin_isterminated(s, incl_open, incl_comma) 4885 char_u *s; 4886 int incl_open; /* include '{' at the end as terminator */ 4887 int incl_comma; /* recognize a trailing comma */ 4888 { 4889 char_u found_start = 0; 4890 4891 s = cin_skipcomment(s); 4892 4893 if (*s == '{' || (*s == '}' && !cin_iselse(s))) 4894 found_start = *s; 4895 4896 while (*s) 4897 { 4898 /* skip over comments, "" strings and 'c'haracters */ 4899 s = skip_string(cin_skipcomment(s)); 4900 if ((*s == ';' || (incl_open && *s == '{') || *s == '}' 4901 || (incl_comma && *s == ',')) 4902 && cin_nocode(s + 1)) 4903 return *s; 4904 4905 if (*s) 4906 s++; 4907 } 4908 return found_start; 4909 } 4910 4911 /* 4912 * Recognize the basic picture of a function declaration -- it needs to 4913 * have an open paren somewhere and a close paren at the end of the line and 4914 * no semicolons anywhere. 4915 * When a line ends in a comma we continue looking in the next line. 4916 * "sp" points to a string with the line. When looking at other lines it must 4917 * be restored to the line. When it's NULL fetch lines here. 4918 * "lnum" is where we start looking. 4919 */ 4920 static int 4921 cin_isfuncdecl(sp, first_lnum) 4922 char_u **sp; 4923 linenr_T first_lnum; 4924 { 4925 char_u *s; 4926 linenr_T lnum = first_lnum; 4927 int retval = FALSE; 4928 4929 if (sp == NULL) 4930 s = ml_get(lnum); 4931 else 4932 s = *sp; 4933 4934 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') 4935 { 4936 if (cin_iscomment(s)) /* ignore comments */ 4937 s = cin_skipcomment(s); 4938 else 4939 ++s; 4940 } 4941 if (*s != '(') 4942 return FALSE; /* ';', ' or " before any () or no '(' */ 4943 4944 while (*s && *s != ';' && *s != '\'' && *s != '"') 4945 { 4946 if (*s == ')' && cin_nocode(s + 1)) 4947 { 4948 /* ')' at the end: may have found a match 4949 * Check for he previous line not to end in a backslash: 4950 * #if defined(x) && \ 4951 * defined(y) 4952 */ 4953 lnum = first_lnum - 1; 4954 s = ml_get(lnum); 4955 if (*s == NUL || s[STRLEN(s) - 1] != '\\') 4956 retval = TRUE; 4957 goto done; 4958 } 4959 if (*s == ',' && cin_nocode(s + 1)) 4960 { 4961 /* ',' at the end: continue looking in the next line */ 4962 if (lnum >= curbuf->b_ml.ml_line_count) 4963 break; 4964 4965 s = ml_get(++lnum); 4966 } 4967 else if (cin_iscomment(s)) /* ignore comments */ 4968 s = cin_skipcomment(s); 4969 else 4970 ++s; 4971 } 4972 4973 done: 4974 if (lnum != first_lnum && sp != NULL) 4975 *sp = ml_get(first_lnum); 4976 4977 return retval; 4978 } 4979 4980 static int 4981 cin_isif(p) 4982 char_u *p; 4983 { 4984 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); 4985 } 4986 4987 static int 4988 cin_iselse(p) 4989 char_u *p; 4990 { 4991 if (*p == '}') /* accept "} else" */ 4992 p = cin_skipcomment(p + 1); 4993 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); 4994 } 4995 4996 static int 4997 cin_isdo(p) 4998 char_u *p; 4999 { 5000 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); 5001 } 5002 5003 /* 5004 * Check if this is a "while" that should have a matching "do". 5005 * We only accept a "while (condition) ;", with only white space between the 5006 * ')' and ';'. The condition may be spread over several lines. 5007 */ 5008 static int 5009 cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */ 5010 char_u *p; 5011 linenr_T lnum; 5012 int ind_maxparen; 5013 { 5014 pos_T cursor_save; 5015 pos_T *trypos; 5016 int retval = FALSE; 5017 5018 p = cin_skipcomment(p); 5019 if (*p == '}') /* accept "} while (cond);" */ 5020 p = cin_skipcomment(p + 1); 5021 if (STRNCMP(p, "while", 5) == 0 && !vim_isIDc(p[5])) 5022 { 5023 cursor_save = curwin->w_cursor; 5024 curwin->w_cursor.lnum = lnum; 5025 curwin->w_cursor.col = 0; 5026 p = ml_get_curline(); 5027 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ 5028 { 5029 ++p; 5030 ++curwin->w_cursor.col; 5031 } 5032 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL 5033 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') 5034 retval = TRUE; 5035 curwin->w_cursor = cursor_save; 5036 } 5037 return retval; 5038 } 5039 5040 static int 5041 cin_isbreak(p) 5042 char_u *p; 5043 { 5044 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); 5045 } 5046 5047 /* Find the position of a C++ base-class declaration or 5048 * constructor-initialization. eg: 5049 * 5050 * class MyClass : 5051 * baseClass <-- here 5052 * class MyClass : public baseClass, 5053 * anotherBaseClass <-- here (should probably lineup ??) 5054 * MyClass::MyClass(...) : 5055 * baseClass(...) <-- here (constructor-initialization) 5056 */ 5057 static int 5058 cin_is_cpp_baseclass(line, col) 5059 char_u *line; 5060 colnr_T *col; 5061 { 5062 char_u *s; 5063 int class_or_struct, lookfor_ctor_init, cpp_base_class; 5064 5065 *col = 0; 5066 5067 s = cin_skipcomment(line); 5068 if (*s == NUL) 5069 return FALSE; 5070 5071 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; 5072 5073 while(*s != NUL) 5074 { 5075 if (s[0] == ':') 5076 { 5077 if (s[1] == ':') 5078 { 5079 /* skip double colon. It can't be a constructor 5080 * initialization any more */ 5081 lookfor_ctor_init = FALSE; 5082 s = cin_skipcomment(s + 2); 5083 } 5084 else if (lookfor_ctor_init || class_or_struct) 5085 { 5086 /* we have something found, that looks like the start of 5087 * cpp-base-class-declaration or contructor-initialization */ 5088 cpp_base_class = TRUE; 5089 lookfor_ctor_init = class_or_struct = FALSE; 5090 *col = 0; 5091 s = cin_skipcomment(s + 1); 5092 } 5093 else 5094 s = cin_skipcomment(s + 1); 5095 } 5096 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) 5097 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) 5098 { 5099 class_or_struct = TRUE; 5100 lookfor_ctor_init = FALSE; 5101 5102 if (*s == 'c') 5103 s = cin_skipcomment(s + 5); 5104 else 5105 s = cin_skipcomment(s + 6); 5106 } 5107 else 5108 { 5109 if (s[0] == '{' || s[0] == '}' || s[0] == ';') 5110 { 5111 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; 5112 } 5113 else if (s[0] == ')') 5114 { 5115 /* Constructor-initialization is assumed if we come across 5116 * something like "):" */ 5117 class_or_struct = FALSE; 5118 lookfor_ctor_init = TRUE; 5119 } 5120 else if (!vim_isIDc(s[0])) 5121 { 5122 /* if it is not an identifier, we are wrong */ 5123 class_or_struct = FALSE; 5124 lookfor_ctor_init = FALSE; 5125 } 5126 else if (*col == 0) 5127 { 5128 /* it can't be a constructor-initialization any more */ 5129 lookfor_ctor_init = FALSE; 5130 5131 /* the first statement starts here: lineup with this one... */ 5132 if (cpp_base_class && *col == 0) 5133 *col = (colnr_T)(s - line); 5134 } 5135 5136 s = cin_skipcomment(s + 1); 5137 } 5138 } 5139 5140 return cpp_base_class; 5141 } 5142 5143 /* 5144 * Return TRUE if string "s" ends with the string "find", possibly followed by 5145 * white space and comments. Skip strings and comments. 5146 * Ignore "ignore" after "find" if it's not NULL. 5147 */ 5148 static int 5149 cin_ends_in(s, find, ignore) 5150 char_u *s; 5151 char_u *find; 5152 char_u *ignore; 5153 { 5154 char_u *p = s; 5155 char_u *r; 5156 int len = (int)STRLEN(find); 5157 5158 while (*p != NUL) 5159 { 5160 p = cin_skipcomment(p); 5161 if (STRNCMP(p, find, len) == 0) 5162 { 5163 r = skipwhite(p + len); 5164 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) 5165 r = skipwhite(r + STRLEN(ignore)); 5166 if (cin_nocode(r)) 5167 return TRUE; 5168 } 5169 if (*p != NUL) 5170 ++p; 5171 } 5172 return FALSE; 5173 } 5174 5175 /* 5176 * Skip strings, chars and comments until at or past "trypos". 5177 * Return the column found. 5178 */ 5179 static int 5180 cin_skip2pos(trypos) 5181 pos_T *trypos; 5182 { 5183 char_u *line; 5184 char_u *p; 5185 5186 p = line = ml_get(trypos->lnum); 5187 while (*p && (colnr_T)(p - line) < trypos->col) 5188 { 5189 if (cin_iscomment(p)) 5190 p = cin_skipcomment(p); 5191 else 5192 { 5193 p = skip_string(p); 5194 ++p; 5195 } 5196 } 5197 return (int)(p - line); 5198 } 5199 5200 /* 5201 * Find the '{' at the start of the block we are in. 5202 * Return NULL if no match found. 5203 * Ignore a '{' that is in a comment, makes indenting the next three lines 5204 * work. */ 5205 /* foo() */ 5206 /* { */ 5207 /* } */ 5208 5209 static pos_T * 5210 find_start_brace(ind_maxcomment) /* XXX */ 5211 int ind_maxcomment; 5212 { 5213 pos_T cursor_save; 5214 pos_T *trypos; 5215 pos_T *pos; 5216 static pos_T pos_copy; 5217 5218 cursor_save = curwin->w_cursor; 5219 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) 5220 { 5221 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ 5222 trypos = &pos_copy; 5223 curwin->w_cursor = *trypos; 5224 pos = NULL; 5225 /* ignore the { if it's in a // comment */ 5226 if ((colnr_T)cin_skip2pos(trypos) == trypos->col 5227 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */ 5228 break; 5229 if (pos != NULL) 5230 curwin->w_cursor.lnum = pos->lnum; 5231 } 5232 curwin->w_cursor = cursor_save; 5233 return trypos; 5234 } 5235 5236 /* 5237 * Find the matching '(', failing if it is in a comment. 5238 * Return NULL of no match found. 5239 */ 5240 static pos_T * 5241 find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */ 5242 int ind_maxparen; 5243 int ind_maxcomment; 5244 { 5245 pos_T cursor_save; 5246 pos_T *trypos; 5247 static pos_T pos_copy; 5248 5249 cursor_save = curwin->w_cursor; 5250 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL) 5251 { 5252 /* check if the ( is in a // comment */ 5253 if ((colnr_T)cin_skip2pos(trypos) > trypos->col) 5254 trypos = NULL; 5255 else 5256 { 5257 pos_copy = *trypos; /* copy trypos, findmatch will change it */ 5258 trypos = &pos_copy; 5259 curwin->w_cursor = *trypos; 5260 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */ 5261 trypos = NULL; 5262 } 5263 } 5264 curwin->w_cursor = cursor_save; 5265 return trypos; 5266 } 5267 5268 /* 5269 * Return ind_maxparen corrected for the difference in line number between the 5270 * cursor position and "startpos". This makes sure that searching for a 5271 * matching paren above the cursor line doesn't find a match because of 5272 * looking a few lines further. 5273 */ 5274 static int 5275 corr_ind_maxparen(ind_maxparen, startpos) 5276 int ind_maxparen; 5277 pos_T *startpos; 5278 { 5279 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; 5280 5281 if (n > 0 && n < ind_maxparen / 2) 5282 return ind_maxparen - (int)n; 5283 return ind_maxparen; 5284 } 5285 5286 /* 5287 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in 5288 * line "l". 5289 */ 5290 static int 5291 find_last_paren(l, start, end) 5292 char_u *l; 5293 int start, end; 5294 { 5295 int i; 5296 int retval = FALSE; 5297 int open_count = 0; 5298 5299 curwin->w_cursor.col = 0; /* default is start of line */ 5300 5301 for (i = 0; l[i]; i++) 5302 { 5303 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ 5304 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ 5305 if (l[i] == start) 5306 ++open_count; 5307 else if (l[i] == end) 5308 { 5309 if (open_count > 0) 5310 --open_count; 5311 else 5312 { 5313 curwin->w_cursor.col = i; 5314 retval = TRUE; 5315 } 5316 } 5317 } 5318 return retval; 5319 } 5320 5321 int 5322 get_c_indent() 5323 { 5324 /* 5325 * spaces from a block's opening brace the prevailing indent for that 5326 * block should be 5327 */ 5328 int ind_level = curbuf->b_p_sw; 5329 5330 /* 5331 * spaces from the edge of the line an open brace that's at the end of a 5332 * line is imagined to be. 5333 */ 5334 int ind_open_imag = 0; 5335 5336 /* 5337 * spaces from the prevailing indent for a line that is not precededof by 5338 * an opening brace. 5339 */ 5340 int ind_no_brace = 0; 5341 5342 /* 5343 * column where the first { of a function should be located } 5344 */ 5345 int ind_first_open = 0; 5346 5347 /* 5348 * spaces from the prevailing indent a leftmost open brace should be 5349 * located 5350 */ 5351 int ind_open_extra = 0; 5352 5353 /* 5354 * spaces from the matching open brace (real location for one at the left 5355 * edge; imaginary location from one that ends a line) the matching close 5356 * brace should be located 5357 */ 5358 int ind_close_extra = 0; 5359 5360 /* 5361 * spaces from the edge of the line an open brace sitting in the leftmost 5362 * column is imagined to be 5363 */ 5364 int ind_open_left_imag = 0; 5365 5366 /* 5367 * spaces from the switch() indent a "case xx" label should be located 5368 */ 5369 int ind_case = curbuf->b_p_sw; 5370 5371 /* 5372 * spaces from the "case xx:" code after a switch() should be located 5373 */ 5374 int ind_case_code = curbuf->b_p_sw; 5375 5376 /* 5377 * lineup break at end of case in switch() with case label 5378 */ 5379 int ind_case_break = 0; 5380 5381 /* 5382 * spaces from the class declaration indent a scope declaration label 5383 * should be located 5384 */ 5385 int ind_scopedecl = curbuf->b_p_sw; 5386 5387 /* 5388 * spaces from the scope declaration label code should be located 5389 */ 5390 int ind_scopedecl_code = curbuf->b_p_sw; 5391 5392 /* 5393 * amount K&R-style parameters should be indented 5394 */ 5395 int ind_param = curbuf->b_p_sw; 5396 5397 /* 5398 * amount a function type spec should be indented 5399 */ 5400 int ind_func_type = curbuf->b_p_sw; 5401 5402 /* 5403 * amount a cpp base class declaration or constructor initialization 5404 * should be indented 5405 */ 5406 int ind_cpp_baseclass = curbuf->b_p_sw; 5407 5408 /* 5409 * additional spaces beyond the prevailing indent a continuation line 5410 * should be located 5411 */ 5412 int ind_continuation = curbuf->b_p_sw; 5413 5414 /* 5415 * spaces from the indent of the line with an unclosed parentheses 5416 */ 5417 int ind_unclosed = curbuf->b_p_sw * 2; 5418 5419 /* 5420 * spaces from the indent of the line with an unclosed parentheses, which 5421 * itself is also unclosed 5422 */ 5423 int ind_unclosed2 = curbuf->b_p_sw; 5424 5425 /* 5426 * suppress ignoring spaces from the indent of a line starting with an 5427 * unclosed parentheses. 5428 */ 5429 int ind_unclosed_noignore = 0; 5430 5431 /* 5432 * If the opening paren is the last nonwhite character on the line, and 5433 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer 5434 * context (for very long lines). 5435 */ 5436 int ind_unclosed_wrapped = 0; 5437 5438 /* 5439 * suppress ignoring white space when lining up with the character after 5440 * an unclosed parentheses. 5441 */ 5442 int ind_unclosed_whiteok = 0; 5443 5444 /* 5445 * indent a closing parentheses under the line start of the matching 5446 * opening parentheses. 5447 */ 5448 int ind_matching_paren = 0; 5449 5450 /* 5451 * Extra indent for comments. 5452 */ 5453 int ind_comment = 0; 5454 5455 /* 5456 * spaces from the comment opener when there is nothing after it. 5457 */ 5458 int ind_in_comment = 3; 5459 5460 /* 5461 * boolean: if non-zero, use ind_in_comment even if there is something 5462 * after the comment opener. 5463 */ 5464 int ind_in_comment2 = 0; 5465 5466 /* 5467 * max lines to search for an open paren 5468 */ 5469 int ind_maxparen = 20; 5470 5471 /* 5472 * max lines to search for an open comment 5473 */ 5474 int ind_maxcomment = 70; 5475 5476 /* 5477 * handle braces for java code 5478 */ 5479 int ind_java = 0; 5480 5481 /* 5482 * handle blocked cases correctly 5483 */ 5484 int ind_keep_case_label = 0; 5485 5486 pos_T cur_curpos; 5487 int amount; 5488 int scope_amount; 5489 int cur_amount; 5490 colnr_T col; 5491 char_u *theline; 5492 char_u *linecopy; 5493 pos_T *trypos; 5494 pos_T *tryposBrace = NULL; 5495 pos_T our_paren_pos; 5496 char_u *start; 5497 int start_brace; 5498 #define BRACE_IN_COL0 1 /* '{' is in comumn 0 */ 5499 #define BRACE_AT_START 2 /* '{' is at start of line */ 5500 #define BRACE_AT_END 3 /* '{' is at end of line */ 5501 linenr_T ourscope; 5502 char_u *l; 5503 char_u *look; 5504 char_u terminated; 5505 int lookfor; 5506 #define LOOKFOR_INITIAL 0 5507 #define LOOKFOR_IF 1 5508 #define LOOKFOR_DO 2 5509 #define LOOKFOR_CASE 3 5510 #define LOOKFOR_ANY 4 5511 #define LOOKFOR_TERM 5 5512 #define LOOKFOR_UNTERM 6 5513 #define LOOKFOR_SCOPEDECL 7 5514 #define LOOKFOR_NOBREAK 8 5515 #define LOOKFOR_CPP_BASECLASS 9 5516 #define LOOKFOR_ENUM_OR_INIT 10 5517 5518 int whilelevel; 5519 linenr_T lnum; 5520 char_u *options; 5521 int fraction = 0; /* init for GCC */ 5522 int divider; 5523 int n; 5524 int iscase; 5525 int lookfor_break; 5526 int cont_amount = 0; /* amount for continuation line */ 5527 5528 for (options = curbuf->b_p_cino; *options; ) 5529 { 5530 l = options++; 5531 if (*options == '-') 5532 ++options; 5533 n = getdigits(&options); 5534 divider = 0; 5535 if (*options == '.') /* ".5s" means a fraction */ 5536 { 5537 fraction = atol((char *)++options); 5538 while (VIM_ISDIGIT(*options)) 5539 { 5540 ++options; 5541 if (divider) 5542 divider *= 10; 5543 else 5544 divider = 10; 5545 } 5546 } 5547 if (*options == 's') /* "2s" means two times 'shiftwidth' */ 5548 { 5549 if (n == 0 && fraction == 0) 5550 n = curbuf->b_p_sw; /* just "s" is one 'shiftwidth' */ 5551 else 5552 { 5553 n *= curbuf->b_p_sw; 5554 if (divider) 5555 n += (curbuf->b_p_sw * fraction + divider / 2) / divider; 5556 } 5557 ++options; 5558 } 5559 if (l[1] == '-') 5560 n = -n; 5561 /* When adding an entry here, also update the default 'cinoptions' in 5562 * change.txt, and add explanation for it! */ 5563 switch (*l) 5564 { 5565 case '>': ind_level = n; break; 5566 case 'e': ind_open_imag = n; break; 5567 case 'n': ind_no_brace = n; break; 5568 case 'f': ind_first_open = n; break; 5569 case '{': ind_open_extra = n; break; 5570 case '}': ind_close_extra = n; break; 5571 case '^': ind_open_left_imag = n; break; 5572 case ':': ind_case = n; break; 5573 case '=': ind_case_code = n; break; 5574 case 'b': ind_case_break = n; break; 5575 case 'p': ind_param = n; break; 5576 case 't': ind_func_type = n; break; 5577 case '/': ind_comment = n; break; 5578 case 'c': ind_in_comment = n; break; 5579 case 'C': ind_in_comment2 = n; break; 5580 case 'i': ind_cpp_baseclass = n; break; 5581 case '+': ind_continuation = n; break; 5582 case '(': ind_unclosed = n; break; 5583 case 'u': ind_unclosed2 = n; break; 5584 case 'U': ind_unclosed_noignore = n; break; 5585 case 'W': ind_unclosed_wrapped = n; break; 5586 case 'w': ind_unclosed_whiteok = n; break; 5587 case 'm': ind_matching_paren = n; break; 5588 case ')': ind_maxparen = n; break; 5589 case '*': ind_maxcomment = n; break; 5590 case 'g': ind_scopedecl = n; break; 5591 case 'h': ind_scopedecl_code = n; break; 5592 case 'j': ind_java = n; break; 5593 case 'l': ind_keep_case_label = n; break; 5594 } 5595 } 5596 5597 /* remember where the cursor was when we started */ 5598 cur_curpos = curwin->w_cursor; 5599 5600 /* Get a copy of the current contents of the line. 5601 * This is required, because only the most recent line obtained with 5602 * ml_get is valid! */ 5603 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); 5604 if (linecopy == NULL) 5605 return 0; 5606 5607 /* 5608 * In insert mode and the cursor is on a ')' truncate the line at the 5609 * cursor position. We don't want to line up with the matching '(' when 5610 * inserting new stuff. 5611 * For unknown reasons the cursor might be past the end of the line, thus 5612 * check for that. 5613 */ 5614 if ((State & INSERT) 5615 && curwin->w_cursor.col < STRLEN(linecopy) 5616 && linecopy[curwin->w_cursor.col] == ')') 5617 linecopy[curwin->w_cursor.col] = NUL; 5618 5619 theline = skipwhite(linecopy); 5620 5621 /* move the cursor to the start of the line */ 5622 5623 curwin->w_cursor.col = 0; 5624 5625 /* 5626 * #defines and so on always go at the left when included in 'cinkeys'. 5627 */ 5628 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) 5629 { 5630 amount = 0; 5631 } 5632 5633 /* 5634 * Is it a non-case label? Then that goes at the left margin too. 5635 */ 5636 else if (cin_islabel(ind_maxcomment)) /* XXX */ 5637 { 5638 amount = 0; 5639 } 5640 5641 /* 5642 * If we're inside a "//" comment and there is a "//" comment in a 5643 * previous line, lineup with that one. 5644 */ 5645 else if (cin_islinecomment(theline) 5646 && (trypos = find_line_comment()) != NULL) /* XXX */ 5647 { 5648 /* find how indented the line beginning the comment is */ 5649 getvcol(curwin, trypos, &col, NULL, NULL); 5650 amount = col; 5651 } 5652 5653 /* 5654 * If we're inside a comment and not looking at the start of the 5655 * comment, try using the 'comments' option. 5656 */ 5657 else if (!cin_iscomment(theline) 5658 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ 5659 { 5660 int lead_start_len = 2; 5661 int lead_middle_len = 1; 5662 char_u lead_start[COM_MAX_LEN]; /* start-comment string */ 5663 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ 5664 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ 5665 char_u *p; 5666 int start_align = 0; 5667 int start_off = 0; 5668 int done = FALSE; 5669 5670 /* find how indented the line beginning the comment is */ 5671 getvcol(curwin, trypos, &col, NULL, NULL); 5672 amount = col; 5673 5674 p = curbuf->b_p_com; 5675 while (*p != NUL) 5676 { 5677 int align = 0; 5678 int off = 0; 5679 int what = 0; 5680 5681 while (*p != NUL && *p != ':') 5682 { 5683 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) 5684 what = *p++; 5685 else if (*p == COM_LEFT || *p == COM_RIGHT) 5686 align = *p++; 5687 else if (VIM_ISDIGIT(*p) || *p == '-') 5688 off = getdigits(&p); 5689 else 5690 ++p; 5691 } 5692 5693 if (*p == ':') 5694 ++p; 5695 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); 5696 if (what == COM_START) 5697 { 5698 STRCPY(lead_start, lead_end); 5699 lead_start_len = (int)STRLEN(lead_start); 5700 start_off = off; 5701 start_align = align; 5702 } 5703 else if (what == COM_MIDDLE) 5704 { 5705 STRCPY(lead_middle, lead_end); 5706 lead_middle_len = (int)STRLEN(lead_middle); 5707 } 5708 else if (what == COM_END) 5709 { 5710 /* If our line starts with the middle comment string, line it 5711 * up with the comment opener per the 'comments' option. */ 5712 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 5713 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) 5714 { 5715 done = TRUE; 5716 if (curwin->w_cursor.lnum > 1) 5717 { 5718 /* If the start comment string matches in the previous 5719 * line, use the indent of that line pluss offset. If 5720 * the middle comment string matches in the previous 5721 * line, use the indent of that line. XXX */ 5722 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); 5723 if (STRNCMP(look, lead_start, lead_start_len) == 0) 5724 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); 5725 else if (STRNCMP(look, lead_middle, 5726 lead_middle_len) == 0) 5727 { 5728 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); 5729 break; 5730 } 5731 /* If the start comment string doesn't match with the 5732 * start of the comment, skip this entry. XXX */ 5733 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col, 5734 lead_start, lead_start_len) != 0) 5735 continue; 5736 } 5737 if (start_off != 0) 5738 amount += start_off; 5739 else if (start_align == COM_RIGHT) 5740 amount += lead_start_len - lead_middle_len; 5741 break; 5742 } 5743 5744 /* If our line starts with the end comment string, line it up 5745 * with the middle comment */ 5746 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 5747 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) 5748 { 5749 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); 5750 /* XXX */ 5751 if (off != 0) 5752 amount += off; 5753 else if (align == COM_RIGHT) 5754 amount += lead_start_len - lead_middle_len; 5755 done = TRUE; 5756 break; 5757 } 5758 } 5759 } 5760 5761 /* If our line starts with an asterisk, line up with the 5762 * asterisk in the comment opener; otherwise, line up 5763 * with the first character of the comment text. 5764 */ 5765 if (done) 5766 ; 5767 else if (theline[0] == '*') 5768 amount += 1; 5769 else 5770 { 5771 /* 5772 * If we are more than one line away from the comment opener, take 5773 * the indent of the previous non-empty line. If 'cino' has "CO" 5774 * and we are just below the comment opener and there are any 5775 * white characters after it line up with the text after it; 5776 * otherwise, add the amount specified by "c" in 'cino' 5777 */ 5778 amount = -1; 5779 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum) 5780 { 5781 if (linewhite(lnum)) /* skip blank lines */ 5782 continue; 5783 amount = get_indent_lnum(lnum); /* XXX */ 5784 break; 5785 } 5786 if (amount == -1) /* use the comment opener */ 5787 { 5788 if (!ind_in_comment2) 5789 { 5790 start = ml_get(trypos->lnum); 5791 look = start + trypos->col + 2; /* skip / and * */ 5792 if (*look != NUL) /* if something after it */ 5793 trypos->col = (colnr_T)(skipwhite(look) - start); 5794 } 5795 getvcol(curwin, trypos, &col, NULL, NULL); 5796 amount = col; 5797 if (ind_in_comment2 || *look == NUL) 5798 amount += ind_in_comment; 5799 } 5800 } 5801 } 5802 5803 /* 5804 * Are we inside parentheses or braces? 5805 */ /* XXX */ 5806 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL 5807 && ind_java == 0) 5808 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL 5809 || trypos != NULL) 5810 { 5811 if (trypos != NULL && tryposBrace != NULL) 5812 { 5813 /* Both an unmatched '(' and '{' is found. Use the one which is 5814 * closer to the current cursor position, set the other to NULL. */ 5815 if (trypos->lnum != tryposBrace->lnum 5816 ? trypos->lnum < tryposBrace->lnum 5817 : trypos->col < tryposBrace->col) 5818 trypos = NULL; 5819 else 5820 tryposBrace = NULL; 5821 } 5822 5823 if (trypos != NULL) 5824 { 5825 /* 5826 * If the matching paren is more than one line away, use the indent of 5827 * a previous non-empty line that matches the same paren. 5828 */ 5829 amount = -1; 5830 cur_amount = MAXCOL; 5831 our_paren_pos = *trypos; 5832 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) 5833 { 5834 l = skipwhite(ml_get(lnum)); 5835 if (cin_nocode(l)) /* skip comment lines */ 5836 continue; 5837 if (cin_ispreproc_cont(&l, &lnum)) /* ignore #defines, #if, etc. */ 5838 continue; 5839 curwin->w_cursor.lnum = lnum; 5840 5841 /* Skip a comment. XXX */ 5842 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) 5843 { 5844 lnum = trypos->lnum + 1; 5845 continue; 5846 } 5847 5848 /* XXX */ 5849 if ((trypos = find_match_paren( 5850 corr_ind_maxparen(ind_maxparen, &cur_curpos), 5851 ind_maxcomment)) != NULL 5852 && trypos->lnum == our_paren_pos.lnum 5853 && trypos->col == our_paren_pos.col) 5854 { 5855 amount = get_indent_lnum(lnum); /* XXX */ 5856 5857 if (theline[0] == ')') 5858 { 5859 if (our_paren_pos.lnum != lnum && cur_amount > amount) 5860 cur_amount = amount; 5861 amount = -1; 5862 } 5863 break; 5864 } 5865 } 5866 5867 /* 5868 * Line up with line where the matching paren is. XXX 5869 * If the line starts with a '(' or the indent for unclosed 5870 * parentheses is zero, line up with the unclosed parentheses. 5871 */ 5872 if (amount == -1) 5873 { 5874 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment); 5875 if (theline[0] == ')' || ind_unclosed == 0 5876 || (!ind_unclosed_noignore && *skipwhite(look) == '(')) 5877 { 5878 /* 5879 * If we're looking at a close paren, line up right there; 5880 * otherwise, line up with the next (non-white) character. 5881 * When ind_unclosed_wrapped is set and the matching paren is 5882 * the last nonwhite character of the line, use either the 5883 * indent of the current line or the indentation of the next 5884 * outer paren and add ind_unclosed_wrapped (for very long 5885 * lines). 5886 */ 5887 if (theline[0] != ')') 5888 { 5889 cur_amount = MAXCOL; 5890 l = ml_get(our_paren_pos.lnum); 5891 if (ind_unclosed_wrapped 5892 && cin_ends_in(l, (char_u *)"(", NULL)) 5893 { 5894 /* look for opening unmatched paren, indent one level 5895 * for each additional level */ 5896 n = 1; 5897 for (col = 0; col < our_paren_pos.col; ++col) 5898 { 5899 switch (l[col]) 5900 { 5901 case '(': 5902 case '{': ++n; 5903 break; 5904 5905 case ')': 5906 case '}': if (n > 1) 5907 --n; 5908 break; 5909 } 5910 } 5911 5912 our_paren_pos.col = 0; 5913 amount += n * ind_unclosed_wrapped; 5914 } 5915 else if (ind_unclosed_whiteok) 5916 our_paren_pos.col++; 5917 else 5918 { 5919 col = our_paren_pos.col + 1; 5920 while (vim_iswhite(l[col])) 5921 col++; 5922 if (l[col] != NUL) /* In case of trailing space */ 5923 our_paren_pos.col = col; 5924 else 5925 our_paren_pos.col++; 5926 } 5927 } 5928 5929 /* 5930 * Find how indented the paren is, or the character after it 5931 * if we did the above "if". 5932 */ 5933 if (our_paren_pos.col > 0) 5934 { 5935 getvcol(curwin, &our_paren_pos, &col, NULL, NULL); 5936 if (cur_amount > (int)col) 5937 cur_amount = col; 5938 } 5939 } 5940 5941 if (theline[0] == ')' && ind_matching_paren) 5942 { 5943 /* Line up with the start of the matching paren line. */ 5944 } 5945 else if (ind_unclosed == 0 || (!ind_unclosed_noignore 5946 && *skipwhite(look) == '(')) 5947 { 5948 if (cur_amount != MAXCOL) 5949 amount = cur_amount; 5950 } 5951 else 5952 { 5953 /* add ind_unclosed2 for each '(' before our matching one */ 5954 col = our_paren_pos.col; 5955 while (our_paren_pos.col > 0) 5956 { 5957 --our_paren_pos.col; 5958 switch (*ml_get_pos(&our_paren_pos)) 5959 { 5960 case '(': amount += ind_unclosed2; 5961 col = our_paren_pos.col; 5962 break; 5963 case ')': amount -= ind_unclosed2; 5964 col = MAXCOL; 5965 break; 5966 } 5967 } 5968 5969 /* Use ind_unclosed once, when the first '(' is not inside 5970 * braces */ 5971 if (col == MAXCOL) 5972 amount += ind_unclosed; 5973 else 5974 { 5975 curwin->w_cursor.lnum = our_paren_pos.lnum; 5976 curwin->w_cursor.col = col; 5977 if ((trypos = find_match_paren(ind_maxparen, 5978 ind_maxcomment)) != NULL) 5979 amount += ind_unclosed2; 5980 else 5981 amount += ind_unclosed; 5982 } 5983 /* 5984 * For a line starting with ')' use the minimum of the two 5985 * positions, to avoid giving it more indent than the previous 5986 * lines: 5987 * func_long_name( if (x 5988 * arg && yy 5989 * ) ^ not here ) ^ not here 5990 */ 5991 if (cur_amount < amount) 5992 amount = cur_amount; 5993 } 5994 } 5995 5996 /* add extra indent for a comment */ 5997 if (cin_iscomment(theline)) 5998 amount += ind_comment; 5999 } 6000 6001 /* 6002 * Are we at least inside braces, then? 6003 */ 6004 else 6005 { 6006 trypos = tryposBrace; 6007 6008 ourscope = trypos->lnum; 6009 start = ml_get(ourscope); 6010 6011 /* 6012 * Now figure out how indented the line is in general. 6013 * If the brace was at the start of the line, we use that; 6014 * otherwise, check out the indentation of the line as 6015 * a whole and then add the "imaginary indent" to that. 6016 */ 6017 look = skipwhite(start); 6018 if (*look == '{') 6019 { 6020 getvcol(curwin, trypos, &col, NULL, NULL); 6021 amount = col; 6022 if (*start == '{') 6023 start_brace = BRACE_IN_COL0; 6024 else 6025 start_brace = BRACE_AT_START; 6026 } 6027 else 6028 { 6029 /* 6030 * that opening brace might have been on a continuation 6031 * line. if so, find the start of the line. 6032 */ 6033 curwin->w_cursor.lnum = ourscope; 6034 6035 /* 6036 * position the cursor over the rightmost paren, so that 6037 * matching it will take us back to the start of the line. 6038 */ 6039 lnum = ourscope; 6040 if (find_last_paren(start, '(', ')') 6041 && (trypos = find_match_paren(ind_maxparen, 6042 ind_maxcomment)) != NULL) 6043 lnum = trypos->lnum; 6044 6045 /* 6046 * It could have been something like 6047 * case 1: if (asdf && 6048 * ldfd) { 6049 * } 6050 */ 6051 if (ind_keep_case_label && cin_iscase(skipwhite(ml_get_curline()))) 6052 amount = get_indent(); 6053 else 6054 amount = skip_label(lnum, &l, ind_maxcomment); 6055 6056 start_brace = BRACE_AT_END; 6057 } 6058 6059 /* 6060 * if we're looking at a closing brace, that's where 6061 * we want to be. otherwise, add the amount of room 6062 * that an indent is supposed to be. 6063 */ 6064 if (theline[0] == '}') 6065 { 6066 /* 6067 * they may want closing braces to line up with something 6068 * other than the open brace. indulge them, if so. 6069 */ 6070 amount += ind_close_extra; 6071 } 6072 else 6073 { 6074 /* 6075 * If we're looking at an "else", try to find an "if" 6076 * to match it with. 6077 * If we're looking at a "while", try to find a "do" 6078 * to match it with. 6079 */ 6080 lookfor = LOOKFOR_INITIAL; 6081 if (cin_iselse(theline)) 6082 lookfor = LOOKFOR_IF; 6083 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen)) 6084 /* XXX */ 6085 lookfor = LOOKFOR_DO; 6086 if (lookfor != LOOKFOR_INITIAL) 6087 { 6088 curwin->w_cursor.lnum = cur_curpos.lnum; 6089 if (find_match(lookfor, ourscope, ind_maxparen, 6090 ind_maxcomment) == OK) 6091 { 6092 amount = get_indent(); /* XXX */ 6093 goto theend; 6094 } 6095 } 6096 6097 /* 6098 * We get here if we are not on an "while-of-do" or "else" (or 6099 * failed to find a matching "if"). 6100 * Search backwards for something to line up with. 6101 * First set amount for when we don't find anything. 6102 */ 6103 6104 /* 6105 * if the '{' is _really_ at the left margin, use the imaginary 6106 * location of a left-margin brace. Otherwise, correct the 6107 * location for ind_open_extra. 6108 */ 6109 6110 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ 6111 { 6112 amount = ind_open_left_imag; 6113 } 6114 else 6115 { 6116 if (start_brace == BRACE_AT_END) /* '{' is at end of line */ 6117 amount += ind_open_imag; 6118 else 6119 { 6120 /* Compensate for adding ind_open_extra later. */ 6121 amount -= ind_open_extra; 6122 if (amount < 0) 6123 amount = 0; 6124 } 6125 } 6126 6127 lookfor_break = FALSE; 6128 6129 if (cin_iscase(theline)) /* it's a switch() label */ 6130 { 6131 lookfor = LOOKFOR_CASE; /* find a previous switch() label */ 6132 amount += ind_case; 6133 } 6134 else if (cin_isscopedecl(theline)) /* private:, ... */ 6135 { 6136 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ 6137 amount += ind_scopedecl; 6138 } 6139 else 6140 { 6141 if (ind_case_break && cin_isbreak(theline)) /* break; ... */ 6142 lookfor_break = TRUE; 6143 6144 lookfor = LOOKFOR_INITIAL; 6145 amount += ind_level; /* ind_level from start of block */ 6146 } 6147 scope_amount = amount; 6148 whilelevel = 0; 6149 6150 /* 6151 * Search backwards. If we find something we recognize, line up 6152 * with that. 6153 * 6154 * if we're looking at an open brace, indent 6155 * the usual amount relative to the conditional 6156 * that opens the block. 6157 */ 6158 curwin->w_cursor = cur_curpos; 6159 for (;;) 6160 { 6161 curwin->w_cursor.lnum--; 6162 curwin->w_cursor.col = 0; 6163 6164 /* 6165 * If we went all the way back to the start of our scope, line 6166 * up with it. 6167 */ 6168 if (curwin->w_cursor.lnum <= ourscope) 6169 { 6170 /* we reached end of scope: 6171 * if looking for a enum or structure initialization 6172 * go further back: 6173 * if it is an initializer (enum xxx or xxx =), then 6174 * don't add ind_continuation, otherwise it is a variable 6175 * declaration: 6176 * int x, 6177 * here; <-- add ind_continuation 6178 */ 6179 if (lookfor == LOOKFOR_ENUM_OR_INIT) 6180 { 6181 if (curwin->w_cursor.lnum == 0 6182 || curwin->w_cursor.lnum 6183 < ourscope - ind_maxparen) 6184 { 6185 /* nothing found (abuse ind_maxparen as limit) 6186 * assume terminated line (i.e. a variable 6187 * initialization) */ 6188 if (cont_amount > 0) 6189 amount = cont_amount; 6190 else 6191 amount += ind_continuation; 6192 break; 6193 } 6194 6195 l = ml_get_curline(); 6196 6197 /* 6198 * If we're in a comment now, skip to the start of the 6199 * comment. 6200 */ 6201 trypos = find_start_comment(ind_maxcomment); 6202 if (trypos != NULL) 6203 { 6204 curwin->w_cursor.lnum = trypos->lnum + 1; 6205 continue; 6206 } 6207 6208 /* 6209 * Skip preprocessor directives and blank lines. 6210 */ 6211 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) 6212 continue; 6213 6214 if (cin_nocode(l)) 6215 continue; 6216 6217 terminated = cin_isterminated(l, FALSE, TRUE); 6218 6219 /* 6220 * If we are at top level and the line looks like a 6221 * function declaration, we are done 6222 * (it's a variable declaration). 6223 */ 6224 if (start_brace != BRACE_IN_COL0 6225 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum)) 6226 { 6227 /* if the line is terminated with another ',' 6228 * it is a continued variable initialization. 6229 * don't add extra indent. 6230 * TODO: does not work, if a function 6231 * declaration is split over multiple lines: 6232 * cin_isfuncdecl returns FALSE then. 6233 */ 6234 if (terminated == ',') 6235 break; 6236 6237 /* if it es a enum declaration or an assignment, 6238 * we are done. 6239 */ 6240 if (terminated != ';' && cin_isinit()) 6241 break; 6242 6243 /* nothing useful found */ 6244 if (terminated == 0 || terminated == '{') 6245 continue; 6246 } 6247 6248 if (terminated != ';') 6249 { 6250 /* Skip parens and braces. Position the cursor 6251 * over the rightmost paren, so that matching it 6252 * will take us back to the start of the line. 6253 */ /* XXX */ 6254 trypos = NULL; 6255 if (find_last_paren(l, '(', ')')) 6256 trypos = find_match_paren(ind_maxparen, 6257 ind_maxcomment); 6258 6259 if (trypos == NULL && find_last_paren(l, '{', '}')) 6260 trypos = find_start_brace(ind_maxcomment); 6261 6262 if (trypos != NULL) 6263 { 6264 curwin->w_cursor.lnum = trypos->lnum + 1; 6265 continue; 6266 } 6267 } 6268 6269 /* it's a variable declaration, add indentation 6270 * like in 6271 * int a, 6272 * b; 6273 */ 6274 if (cont_amount > 0) 6275 amount = cont_amount; 6276 else 6277 amount += ind_continuation; 6278 } 6279 else if (lookfor == LOOKFOR_UNTERM) 6280 { 6281 if (cont_amount > 0) 6282 amount = cont_amount; 6283 else 6284 amount += ind_continuation; 6285 } 6286 else if (lookfor != LOOKFOR_TERM 6287 && lookfor != LOOKFOR_CPP_BASECLASS) 6288 { 6289 amount = scope_amount; 6290 if (theline[0] == '{') 6291 amount += ind_open_extra; 6292 } 6293 break; 6294 } 6295 6296 /* 6297 * If we're in a comment now, skip to the start of the comment. 6298 */ /* XXX */ 6299 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) 6300 { 6301 curwin->w_cursor.lnum = trypos->lnum + 1; 6302 continue; 6303 } 6304 6305 l = ml_get_curline(); 6306 6307 /* 6308 * If this is a switch() label, may line up relative to that. 6309 * if this is a C++ scope declaration, do the same. 6310 */ 6311 iscase = cin_iscase(l); 6312 if (iscase || cin_isscopedecl(l)) 6313 { 6314 /* we are only looking for cpp base class 6315 * declaration/initialization any longer */ 6316 if (lookfor == LOOKFOR_CPP_BASECLASS) 6317 break; 6318 6319 /* When looking for a "do" we are not interested in 6320 * labels. */ 6321 if (whilelevel > 0) 6322 continue; 6323 6324 /* 6325 * case xx: 6326 * c = 99 + <- this indent plus continuation 6327 *-> here; 6328 */ 6329 if (lookfor == LOOKFOR_UNTERM 6330 || lookfor == LOOKFOR_ENUM_OR_INIT) 6331 { 6332 if (cont_amount > 0) 6333 amount = cont_amount; 6334 else 6335 amount += ind_continuation; 6336 break; 6337 } 6338 6339 /* 6340 * case xx: <- line up with this case 6341 * x = 333; 6342 * case yy: 6343 */ 6344 if ( (iscase && lookfor == LOOKFOR_CASE) 6345 || (iscase && lookfor_break) 6346 || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) 6347 { 6348 /* 6349 * Check that this case label is not for another 6350 * switch() 6351 */ /* XXX */ 6352 if ((trypos = find_start_brace(ind_maxcomment)) == 6353 NULL || trypos->lnum == ourscope) 6354 { 6355 amount = get_indent(); /* XXX */ 6356 break; 6357 } 6358 continue; 6359 } 6360 6361 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ 6362 6363 /* 6364 * case xx: if (cond) <- line up with this if 6365 * y = y + 1; 6366 * -> s = 99; 6367 * 6368 * case xx: 6369 * if (cond) <- line up with this line 6370 * y = y + 1; 6371 * -> s = 99; 6372 */ 6373 if (lookfor == LOOKFOR_TERM) 6374 { 6375 if (n) 6376 amount = n; 6377 6378 if (!lookfor_break) 6379 break; 6380 } 6381 6382 /* 6383 * case xx: x = x + 1; <- line up with this x 6384 * -> y = y + 1; 6385 * 6386 * case xx: if (cond) <- line up with this if 6387 * -> y = y + 1; 6388 */ 6389 if (n) 6390 { 6391 amount = n; 6392 l = after_label(ml_get_curline()); 6393 if (l != NULL && cin_is_cinword(l)) 6394 amount += ind_level + ind_no_brace; 6395 break; 6396 } 6397 6398 /* 6399 * Try to get the indent of a statement before the switch 6400 * label. If nothing is found, line up relative to the 6401 * switch label. 6402 * break; <- may line up with this line 6403 * case xx: 6404 * -> y = 1; 6405 */ 6406 scope_amount = get_indent() + (iscase /* XXX */ 6407 ? ind_case_code : ind_scopedecl_code); 6408 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY; 6409 continue; 6410 } 6411 6412 /* 6413 * Looking for a switch() label or C++ scope declaration, 6414 * ignore other lines, skip {}-blocks. 6415 */ 6416 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) 6417 { 6418 if (find_last_paren(l, '{', '}') && (trypos = 6419 find_start_brace(ind_maxcomment)) != NULL) 6420 curwin->w_cursor.lnum = trypos->lnum + 1; 6421 continue; 6422 } 6423 6424 /* 6425 * Ignore jump labels with nothing after them. 6426 */ 6427 if (cin_islabel(ind_maxcomment)) 6428 { 6429 l = after_label(ml_get_curline()); 6430 if (l == NULL || cin_nocode(l)) 6431 continue; 6432 } 6433 6434 /* 6435 * Ignore #defines, #if, etc. 6436 * Ignore comment and empty lines. 6437 * (need to get the line again, cin_islabel() may have 6438 * unlocked it) 6439 */ 6440 l = ml_get_curline(); 6441 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum) 6442 || cin_nocode(l)) 6443 continue; 6444 6445 /* 6446 * Are we at the start of a cpp base class declaration or 6447 * constructor initialization? 6448 */ /* XXX */ 6449 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass 6450 && cin_is_cpp_baseclass(l, &col)) 6451 { 6452 if (lookfor == LOOKFOR_UNTERM) 6453 { 6454 if (cont_amount > 0) 6455 amount = cont_amount; 6456 else 6457 amount += ind_continuation; 6458 } 6459 else if (col == 0 || theline[0] == '{') 6460 { 6461 amount = get_indent(); 6462 if (find_last_paren(l, '(', ')') 6463 && (trypos = find_match_paren(ind_maxparen, 6464 ind_maxcomment)) != NULL) 6465 amount = get_indent_lnum(trypos->lnum); /* XXX */ 6466 if (theline[0] != '{') 6467 amount += ind_cpp_baseclass; 6468 } 6469 else 6470 { 6471 curwin->w_cursor.col = col; 6472 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL); 6473 amount = (int)col; 6474 } 6475 break; 6476 } 6477 else if (lookfor == LOOKFOR_CPP_BASECLASS) 6478 { 6479 /* only look, whether there is a cpp base class 6480 * declaration or initialization before the opening brace. */ 6481 if (cin_isterminated(l, TRUE, FALSE)) 6482 break; 6483 else 6484 continue; 6485 } 6486 6487 /* 6488 * What happens next depends on the line being terminated. 6489 * If terminated with a ',' only consider it terminating if 6490 * there is anoter unterminated statement behind, eg: 6491 * 123, 6492 * sizeof 6493 * here 6494 * Otherwise check whether it is a enumeration or structure 6495 * initialisation (not indented) or a variable declaration 6496 * (indented). 6497 */ 6498 terminated = cin_isterminated(l, FALSE, TRUE); 6499 6500 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM 6501 && terminated == ',')) 6502 { 6503 /* 6504 * if we're in the middle of a paren thing, 6505 * go back to the line that starts it so 6506 * we can get the right prevailing indent 6507 * if ( foo && 6508 * bar ) 6509 */ 6510 /* 6511 * position the cursor over the rightmost paren, so that 6512 * matching it will take us back to the start of the line. 6513 */ 6514 (void)find_last_paren(l, '(', ')'); 6515 trypos = find_match_paren( 6516 corr_ind_maxparen(ind_maxparen, &cur_curpos), 6517 ind_maxcomment); 6518 6519 /* 6520 * If we are looking for ',', we also look for matching 6521 * braces. 6522 */ 6523 if (trypos == NULL && find_last_paren(l, '{', '}')) 6524 trypos = find_start_brace(ind_maxcomment); 6525 6526 if (trypos != NULL) 6527 { 6528 /* 6529 * Check if we are on a case label now. This is 6530 * handled above. 6531 * case xx: if ( asdf && 6532 * asdf) 6533 */ 6534 curwin->w_cursor.lnum = trypos->lnum; 6535 l = ml_get_curline(); 6536 if (cin_iscase(l) || cin_isscopedecl(l)) 6537 { 6538 ++curwin->w_cursor.lnum; 6539 continue; 6540 } 6541 } 6542 6543 /* 6544 * Skip over continuation lines to find the one to get the 6545 * indent from 6546 * char *usethis = "bla\ 6547 * bla", 6548 * here; 6549 */ 6550 if (terminated == ',') 6551 { 6552 while (curwin->w_cursor.lnum > 1) 6553 { 6554 l = ml_get(curwin->w_cursor.lnum - 1); 6555 if (*l == NUL || l[STRLEN(l) - 1] != '\\') 6556 break; 6557 --curwin->w_cursor.lnum; 6558 } 6559 } 6560 6561 /* 6562 * Get indent and pointer to text for current line, 6563 * ignoring any jump label. XXX 6564 */ 6565 cur_amount = skip_label(curwin->w_cursor.lnum, 6566 &l, ind_maxcomment); 6567 6568 /* 6569 * If this is just above the line we are indenting, and it 6570 * starts with a '{', line it up with this line. 6571 * while (not) 6572 * -> { 6573 * } 6574 */ 6575 if (terminated != ',' && lookfor != LOOKFOR_TERM 6576 && theline[0] == '{') 6577 { 6578 amount = cur_amount; 6579 /* 6580 * Only add ind_open_extra when the current line 6581 * doesn't start with a '{', which must have a match 6582 * in the same line (scope is the same). Probably: 6583 * { 1, 2 }, 6584 * -> { 3, 4 } 6585 */ 6586 if (*skipwhite(l) != '{') 6587 amount += ind_open_extra; 6588 6589 if (ind_cpp_baseclass) 6590 { 6591 /* have to look back, whether it is a cpp base 6592 * class declaration or initialization */ 6593 lookfor = LOOKFOR_CPP_BASECLASS; 6594 continue; 6595 } 6596 break; 6597 } 6598 6599 /* 6600 * Check if we are after an "if", "while", etc. 6601 * Also allow " } else". 6602 */ 6603 if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) 6604 { 6605 /* 6606 * Found an unterminated line after an if (), line up 6607 * with the last one. 6608 * if (cond) 6609 * 100 + 6610 * -> here; 6611 */ 6612 if (lookfor == LOOKFOR_UNTERM 6613 || lookfor == LOOKFOR_ENUM_OR_INIT) 6614 { 6615 if (cont_amount > 0) 6616 amount = cont_amount; 6617 else 6618 amount += ind_continuation; 6619 break; 6620 } 6621 6622 /* 6623 * If this is just above the line we are indenting, we 6624 * are finished. 6625 * while (not) 6626 * -> here; 6627 * Otherwise this indent can be used when the line 6628 * before this is terminated. 6629 * yyy; 6630 * if (stat) 6631 * while (not) 6632 * xxx; 6633 * -> here; 6634 */ 6635 amount = cur_amount; 6636 if (theline[0] == '{') 6637 amount += ind_open_extra; 6638 if (lookfor != LOOKFOR_TERM) 6639 { 6640 amount += ind_level + ind_no_brace; 6641 break; 6642 } 6643 6644 /* 6645 * Special trick: when expecting the while () after a 6646 * do, line up with the while() 6647 * do 6648 * x = 1; 6649 * -> here 6650 */ 6651 l = skipwhite(ml_get_curline()); 6652 if (cin_isdo(l)) 6653 { 6654 if (whilelevel == 0) 6655 break; 6656 --whilelevel; 6657 } 6658 6659 /* 6660 * When searching for a terminated line, don't use the 6661 * one between the "if" and the "else". 6662 * Need to use the scope of this "else". XXX 6663 * If whilelevel != 0 continue looking for a "do {". 6664 */ 6665 if (cin_iselse(l) 6666 && whilelevel == 0 6667 && ((trypos = find_start_brace(ind_maxcomment)) 6668 == NULL 6669 || find_match(LOOKFOR_IF, trypos->lnum, 6670 ind_maxparen, ind_maxcomment) == FAIL)) 6671 break; 6672 } 6673 6674 /* 6675 * If we're below an unterminated line that is not an 6676 * "if" or something, we may line up with this line or 6677 * add someting for a continuation line, depending on 6678 * the line before this one. 6679 */ 6680 else 6681 { 6682 /* 6683 * Found two unterminated lines on a row, line up with 6684 * the last one. 6685 * c = 99 + 6686 * 100 + 6687 * -> here; 6688 */ 6689 if (lookfor == LOOKFOR_UNTERM) 6690 { 6691 /* When line ends in a comma add extra indent */ 6692 if (terminated == ',') 6693 amount += ind_continuation; 6694 break; 6695 } 6696 6697 if (lookfor == LOOKFOR_ENUM_OR_INIT) 6698 { 6699 /* Found two lines ending in ',', lineup with the 6700 * lowest one, but check for cpp base class 6701 * declaration/initialization, if it is an 6702 * opening brace or we are looking just for 6703 * enumerations/initializations. */ 6704 if (terminated == ',') 6705 { 6706 if (ind_cpp_baseclass == 0) 6707 break; 6708 6709 lookfor = LOOKFOR_CPP_BASECLASS; 6710 continue; 6711 } 6712 6713 /* Ignore unterminated lines in between, but 6714 * reduce indent. */ 6715 if (amount > cur_amount) 6716 amount = cur_amount; 6717 } 6718 else 6719 { 6720 /* 6721 * Found first unterminated line on a row, may 6722 * line up with this line, remember its indent 6723 * 100 + 6724 * -> here; 6725 */ 6726 amount = cur_amount; 6727 6728 /* 6729 * If previous line ends in ',', check whether we 6730 * are in an initialization or enum 6731 * struct xxx = 6732 * { 6733 * sizeof a, 6734 * 124 }; 6735 * or a normal possible continuation line. 6736 * but only, of no other statement has been found 6737 * yet. 6738 */ 6739 if (lookfor == LOOKFOR_INITIAL && terminated == ',') 6740 { 6741 lookfor = LOOKFOR_ENUM_OR_INIT; 6742 cont_amount = cin_first_id_amount(); 6743 } 6744 else 6745 { 6746 if (lookfor == LOOKFOR_INITIAL 6747 && *l != NUL 6748 && l[STRLEN(l) - 1] == '\\') 6749 /* XXX */ 6750 cont_amount = cin_get_equal_amount( 6751 curwin->w_cursor.lnum); 6752 if (lookfor != LOOKFOR_TERM) 6753 lookfor = LOOKFOR_UNTERM; 6754 } 6755 } 6756 } 6757 } 6758 6759 /* 6760 * Check if we are after a while (cond); 6761 * If so: Ignore until the matching "do". 6762 */ 6763 /* XXX */ 6764 else if (cin_iswhileofdo(l, 6765 curwin->w_cursor.lnum, ind_maxparen)) 6766 { 6767 /* 6768 * Found an unterminated line after a while ();, line up 6769 * with the last one. 6770 * while (cond); 6771 * 100 + <- line up with this one 6772 * -> here; 6773 */ 6774 if (lookfor == LOOKFOR_UNTERM 6775 || lookfor == LOOKFOR_ENUM_OR_INIT) 6776 { 6777 if (cont_amount > 0) 6778 amount = cont_amount; 6779 else 6780 amount += ind_continuation; 6781 break; 6782 } 6783 6784 if (whilelevel == 0) 6785 { 6786 lookfor = LOOKFOR_TERM; 6787 amount = get_indent(); /* XXX */ 6788 if (theline[0] == '{') 6789 amount += ind_open_extra; 6790 } 6791 ++whilelevel; 6792 } 6793 6794 /* 6795 * We are after a "normal" statement. 6796 * If we had another statement we can stop now and use the 6797 * indent of that other statement. 6798 * Otherwise the indent of the current statement may be used, 6799 * search backwards for the next "normal" statement. 6800 */ 6801 else 6802 { 6803 /* 6804 * Skip single break line, if before a switch label. It 6805 * may be lined up with the case label. 6806 */ 6807 if (lookfor == LOOKFOR_NOBREAK 6808 && cin_isbreak(skipwhite(ml_get_curline()))) 6809 { 6810 lookfor = LOOKFOR_ANY; 6811 continue; 6812 } 6813 6814 /* 6815 * Handle "do {" line. 6816 */ 6817 if (whilelevel > 0) 6818 { 6819 l = cin_skipcomment(ml_get_curline()); 6820 if (cin_isdo(l)) 6821 { 6822 amount = get_indent(); /* XXX */ 6823 --whilelevel; 6824 continue; 6825 } 6826 } 6827 6828 /* 6829 * Found a terminated line above an unterminated line. Add 6830 * the amount for a continuation line. 6831 * x = 1; 6832 * y = foo + 6833 * -> here; 6834 * or 6835 * int x = 1; 6836 * int foo, 6837 * -> here; 6838 */ 6839 if (lookfor == LOOKFOR_UNTERM 6840 || lookfor == LOOKFOR_ENUM_OR_INIT) 6841 { 6842 if (cont_amount > 0) 6843 amount = cont_amount; 6844 else 6845 amount += ind_continuation; 6846 break; 6847 } 6848 6849 /* 6850 * Found a terminated line above a terminated line or "if" 6851 * etc. line. Use the amount of the line below us. 6852 * x = 1; x = 1; 6853 * if (asdf) y = 2; 6854 * while (asdf) ->here; 6855 * here; 6856 * ->foo; 6857 */ 6858 if (lookfor == LOOKFOR_TERM) 6859 { 6860 if (!lookfor_break && whilelevel == 0) 6861 break; 6862 } 6863 6864 /* 6865 * First line above the one we're indenting is terminated. 6866 * To know what needs to be done look further backward for 6867 * a terminated line. 6868 */ 6869 else 6870 { 6871 /* 6872 * position the cursor over the rightmost paren, so 6873 * that matching it will take us back to the start of 6874 * the line. Helps for: 6875 * func(asdr, 6876 * asdfasdf); 6877 * here; 6878 */ 6879 term_again: 6880 l = ml_get_curline(); 6881 if (find_last_paren(l, '(', ')') 6882 && (trypos = find_match_paren(ind_maxparen, 6883 ind_maxcomment)) != NULL) 6884 { 6885 /* 6886 * Check if we are on a case label now. This is 6887 * handled above. 6888 * case xx: if ( asdf && 6889 * asdf) 6890 */ 6891 curwin->w_cursor.lnum = trypos->lnum; 6892 l = ml_get_curline(); 6893 if (cin_iscase(l) || cin_isscopedecl(l)) 6894 { 6895 ++curwin->w_cursor.lnum; 6896 continue; 6897 } 6898 } 6899 6900 /* When aligning with the case statement, don't align 6901 * with a statement after it. 6902 * case 1: { <-- don't use this { position 6903 * stat; 6904 * } 6905 * case 2: 6906 * stat; 6907 * } 6908 */ 6909 iscase = (ind_keep_case_label && cin_iscase(l)); 6910 6911 /* 6912 * Get indent and pointer to text for current line, 6913 * ignoring any jump label. 6914 */ 6915 amount = skip_label(curwin->w_cursor.lnum, 6916 &l, ind_maxcomment); 6917 6918 if (theline[0] == '{') 6919 amount += ind_open_extra; 6920 /* See remark above: "Only add ind_open_extra.." */ 6921 if (*skipwhite(l) == '{') 6922 amount -= ind_open_extra; 6923 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; 6924 6925 /* 6926 * If we're at the end of a block, skip to the start of 6927 * that block. 6928 */ 6929 curwin->w_cursor.col = 0; 6930 if (*cin_skipcomment(l) == '}' 6931 && (trypos = find_start_brace(ind_maxcomment)) 6932 != NULL) /* XXX */ 6933 { 6934 curwin->w_cursor.lnum = trypos->lnum; 6935 /* if not "else {" check for terminated again */ 6936 /* but skip block for "} else {" */ 6937 l = cin_skipcomment(ml_get_curline()); 6938 if (*l == '}' || !cin_iselse(l)) 6939 goto term_again; 6940 ++curwin->w_cursor.lnum; 6941 } 6942 } 6943 } 6944 } 6945 } 6946 } 6947 6948 /* add extra indent for a comment */ 6949 if (cin_iscomment(theline)) 6950 amount += ind_comment; 6951 } 6952 6953 /* 6954 * ok -- we're not inside any sort of structure at all! 6955 * 6956 * this means we're at the top level, and everything should 6957 * basically just match where the previous line is, except 6958 * for the lines immediately following a function declaration, 6959 * which are K&R-style parameters and need to be indented. 6960 */ 6961 else 6962 { 6963 /* 6964 * if our line starts with an open brace, forget about any 6965 * prevailing indent and make sure it looks like the start 6966 * of a function 6967 */ 6968 6969 if (theline[0] == '{') 6970 { 6971 amount = ind_first_open; 6972 } 6973 6974 /* 6975 * If the NEXT line is a function declaration, the current 6976 * line needs to be indented as a function type spec. 6977 * Don't do this if the current line looks like a comment 6978 * or if the current line is terminated, ie. ends in ';'. 6979 */ 6980 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count 6981 && !cin_nocode(theline) 6982 && !cin_ends_in(theline, (char_u *)":", NULL) 6983 && !cin_ends_in(theline, (char_u *)",", NULL) 6984 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1) 6985 && !cin_isterminated(theline, FALSE, TRUE)) 6986 { 6987 amount = ind_func_type; 6988 } 6989 else 6990 { 6991 amount = 0; 6992 curwin->w_cursor = cur_curpos; 6993 6994 /* search backwards until we find something we recognize */ 6995 6996 while (curwin->w_cursor.lnum > 1) 6997 { 6998 curwin->w_cursor.lnum--; 6999 curwin->w_cursor.col = 0; 7000 7001 l = ml_get_curline(); 7002 7003 /* 7004 * If we're in a comment now, skip to the start of the comment. 7005 */ /* XXX */ 7006 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) 7007 { 7008 curwin->w_cursor.lnum = trypos->lnum + 1; 7009 continue; 7010 } 7011 7012 /* 7013 * Are we at the start of a cpp base class declaration or constructor 7014 * initialization? 7015 */ /* XXX */ 7016 if (ind_cpp_baseclass != 0 && theline[0] != '{' 7017 && cin_is_cpp_baseclass(l, &col)) 7018 { 7019 if (col == 0) 7020 { 7021 amount = get_indent() + ind_cpp_baseclass; /* XXX */ 7022 if (find_last_paren(l, '(', ')') 7023 && (trypos = find_match_paren(ind_maxparen, 7024 ind_maxcomment)) != NULL) 7025 amount = get_indent_lnum(trypos->lnum) 7026 + ind_cpp_baseclass; /* XXX */ 7027 } 7028 else 7029 { 7030 curwin->w_cursor.col = col; 7031 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL); 7032 amount = (int)col; 7033 } 7034 break; 7035 } 7036 7037 /* 7038 * Skip preprocessor directives and blank lines. 7039 */ 7040 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) 7041 continue; 7042 7043 if (cin_nocode(l)) 7044 continue; 7045 7046 /* 7047 * If the previous line ends in ',', use one level of 7048 * indentation: 7049 * int foo, 7050 * bar; 7051 * do this before checking for '}' in case of eg. 7052 * enum foobar 7053 * { 7054 * ... 7055 * } foo, 7056 * bar; 7057 */ 7058 n = 0; 7059 if (cin_ends_in(l, (char_u *)",", NULL) 7060 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) 7061 { 7062 /* take us back to opening paren */ 7063 if (find_last_paren(l, '(', ')') 7064 && (trypos = find_match_paren(ind_maxparen, 7065 ind_maxcomment)) != NULL) 7066 curwin->w_cursor.lnum = trypos->lnum; 7067 7068 /* For a line ending in ',' that is a continuation line go 7069 * back to the first line with a backslash: 7070 * char *foo = "bla\ 7071 * bla", 7072 * here; 7073 */ 7074 while (n == 0 && curwin->w_cursor.lnum > 1) 7075 { 7076 l = ml_get(curwin->w_cursor.lnum - 1); 7077 if (*l == NUL || l[STRLEN(l) - 1] != '\\') 7078 break; 7079 --curwin->w_cursor.lnum; 7080 } 7081 7082 amount = get_indent(); /* XXX */ 7083 7084 if (amount == 0) 7085 amount = cin_first_id_amount(); 7086 if (amount == 0) 7087 amount = ind_continuation; 7088 break; 7089 } 7090 7091 /* 7092 * If the line looks like a function declaration, and we're 7093 * not in a comment, put it the left margin. 7094 */ 7095 if (cin_isfuncdecl(NULL, cur_curpos.lnum)) /* XXX */ 7096 break; 7097 l = ml_get_curline(); 7098 7099 /* 7100 * Finding the closing '}' of a previous function. Put 7101 * current line at the left margin. For when 'cino' has "fs". 7102 */ 7103 if (*skipwhite(l) == '}') 7104 break; 7105 7106 /* (matching {) 7107 * If the previous line ends on '};' (maybe followed by 7108 * comments) align at column 0. For example: 7109 * char *string_array[] = { "foo", 7110 * / * x * / "b};ar" }; / * foobar * / 7111 */ 7112 if (cin_ends_in(l, (char_u *)"};", NULL)) 7113 break; 7114 7115 /* 7116 * If the PREVIOUS line is a function declaration, the current 7117 * line (and the ones that follow) needs to be indented as 7118 * parameters. 7119 */ 7120 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum)) 7121 { 7122 amount = ind_param; 7123 break; 7124 } 7125 7126 /* 7127 * If the previous line ends in ';' and the line before the 7128 * previous line ends in ',' or '\', ident to column zero: 7129 * int foo, 7130 * bar; 7131 * indent_to_0 here; 7132 */ 7133 if (cin_ends_in(l, (char_u*)";", NULL)) 7134 { 7135 l = ml_get(curwin->w_cursor.lnum - 1); 7136 if (cin_ends_in(l, (char_u *)",", NULL) 7137 || (*l != NUL && l[STRLEN(l) - 1] == '\\')) 7138 break; 7139 l = ml_get_curline(); 7140 } 7141 7142 /* 7143 * Doesn't look like anything interesting -- so just 7144 * use the indent of this line. 7145 * 7146 * Position the cursor over the rightmost paren, so that 7147 * matching it will take us back to the start of the line. 7148 */ 7149 find_last_paren(l, '(', ')'); 7150 7151 if ((trypos = find_match_paren(ind_maxparen, 7152 ind_maxcomment)) != NULL) 7153 curwin->w_cursor.lnum = trypos->lnum; 7154 amount = get_indent(); /* XXX */ 7155 break; 7156 } 7157 7158 /* add extra indent for a comment */ 7159 if (cin_iscomment(theline)) 7160 amount += ind_comment; 7161 7162 /* add extra indent if the previous line ended in a backslash: 7163 * "asdfasdf\ 7164 * here"; 7165 * char *foo = "asdf\ 7166 * here"; 7167 */ 7168 if (cur_curpos.lnum > 1) 7169 { 7170 l = ml_get(cur_curpos.lnum - 1); 7171 if (*l != NUL && l[STRLEN(l) - 1] == '\\') 7172 { 7173 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); 7174 if (cur_amount > 0) 7175 amount = cur_amount; 7176 else if (cur_amount == 0) 7177 amount += ind_continuation; 7178 } 7179 } 7180 } 7181 } 7182 7183 theend: 7184 /* put the cursor back where it belongs */ 7185 curwin->w_cursor = cur_curpos; 7186 7187 vim_free(linecopy); 7188 7189 if (amount < 0) 7190 return 0; 7191 return amount; 7192 } 7193 7194 static int 7195 find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment) 7196 int lookfor; 7197 linenr_T ourscope; 7198 int ind_maxparen; 7199 int ind_maxcomment; 7200 { 7201 char_u *look; 7202 pos_T *theirscope; 7203 char_u *mightbeif; 7204 int elselevel; 7205 int whilelevel; 7206 7207 if (lookfor == LOOKFOR_IF) 7208 { 7209 elselevel = 1; 7210 whilelevel = 0; 7211 } 7212 else 7213 { 7214 elselevel = 0; 7215 whilelevel = 1; 7216 } 7217 7218 curwin->w_cursor.col = 0; 7219 7220 while (curwin->w_cursor.lnum > ourscope + 1) 7221 { 7222 curwin->w_cursor.lnum--; 7223 curwin->w_cursor.col = 0; 7224 7225 look = cin_skipcomment(ml_get_curline()); 7226 if (cin_iselse(look) 7227 || cin_isif(look) 7228 || cin_isdo(look) /* XXX */ 7229 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) 7230 { 7231 /* 7232 * if we've gone outside the braces entirely, 7233 * we must be out of scope... 7234 */ 7235 theirscope = find_start_brace(ind_maxcomment); /* XXX */ 7236 if (theirscope == NULL) 7237 break; 7238 7239 /* 7240 * and if the brace enclosing this is further 7241 * back than the one enclosing the else, we're 7242 * out of luck too. 7243 */ 7244 if (theirscope->lnum < ourscope) 7245 break; 7246 7247 /* 7248 * and if they're enclosed in a *deeper* brace, 7249 * then we can ignore it because it's in a 7250 * different scope... 7251 */ 7252 if (theirscope->lnum > ourscope) 7253 continue; 7254 7255 /* 7256 * if it was an "else" (that's not an "else if") 7257 * then we need to go back to another if, so 7258 * increment elselevel 7259 */ 7260 look = cin_skipcomment(ml_get_curline()); 7261 if (cin_iselse(look)) 7262 { 7263 mightbeif = cin_skipcomment(look + 4); 7264 if (!cin_isif(mightbeif)) 7265 ++elselevel; 7266 continue; 7267 } 7268 7269 /* 7270 * if it was a "while" then we need to go back to 7271 * another "do", so increment whilelevel. XXX 7272 */ 7273 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) 7274 { 7275 ++whilelevel; 7276 continue; 7277 } 7278 7279 /* If it's an "if" decrement elselevel */ 7280 look = cin_skipcomment(ml_get_curline()); 7281 if (cin_isif(look)) 7282 { 7283 elselevel--; 7284 /* 7285 * When looking for an "if" ignore "while"s that 7286 * get in the way. 7287 */ 7288 if (elselevel == 0 && lookfor == LOOKFOR_IF) 7289 whilelevel = 0; 7290 } 7291 7292 /* If it's a "do" decrement whilelevel */ 7293 if (cin_isdo(look)) 7294 whilelevel--; 7295 7296 /* 7297 * if we've used up all the elses, then 7298 * this must be the if that we want! 7299 * match the indent level of that if. 7300 */ 7301 if (elselevel <= 0 && whilelevel <= 0) 7302 { 7303 return OK; 7304 } 7305 } 7306 } 7307 return FAIL; 7308 } 7309 7310 # if defined(FEAT_EVAL) || defined(PROTO) 7311 /* 7312 * Get indent level from 'indentexpr'. 7313 */ 7314 int 7315 get_expr_indent() 7316 { 7317 int indent; 7318 pos_T pos; 7319 int save_State; 7320 7321 pos = curwin->w_cursor; 7322 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); 7323 ++sandbox; 7324 indent = eval_to_number(curbuf->b_p_inde); 7325 --sandbox; 7326 7327 /* Restore the cursor position so that 'indentexpr' doesn't need to. 7328 * Pretend to be in Insert mode, allow cursor past end of line for "o" 7329 * command. */ 7330 save_State = State; 7331 State = INSERT; 7332 curwin->w_cursor = pos; 7333 check_cursor(); 7334 State = save_State; 7335 7336 /* If there is an error, just keep the current indent. */ 7337 if (indent < 0) 7338 indent = get_indent(); 7339 7340 return indent; 7341 } 7342 # endif 7343 7344 #endif /* FEAT_CINDENT */ 7345 7346 #if defined(FEAT_LISP) || defined(PROTO) 7347 7348 static int lisp_match __ARGS((char_u *p)); 7349 7350 static int 7351 lisp_match(p) 7352 char_u *p; 7353 { 7354 char_u buf[LSIZE]; 7355 int len; 7356 char_u *word = p_lispwords; 7357 7358 while (*word != NUL) 7359 { 7360 (void)copy_option_part(&word, buf, LSIZE, ","); 7361 len = (int)STRLEN(buf); 7362 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') 7363 return TRUE; 7364 } 7365 return FALSE; 7366 } 7367 7368 /* 7369 * When 'p' is present in 'cpoptions, a Vi compatible method is used. 7370 * The incompatible newer method is quite a bit better at indenting 7371 * code in lisp-like languages than the traditional one; it's still 7372 * mostly heuristics however -- Dirk van Deun, [email protected] 7373 * 7374 * TODO: 7375 * Findmatch() should be adapted for lisp, also to make showmatch 7376 * work correctly: now (v5.3) it seems all C/C++ oriented: 7377 * - it does not recognize the #\( and #\) notations as character literals 7378 * - it doesn't know about comments starting with a semicolon 7379 * - it incorrectly interprets '(' as a character literal 7380 * All this messes up get_lisp_indent in some rare cases. 7381 * Update from Sergey Khorev: 7382 * I tried to fix the first two issues. 7383 */ 7384 int 7385 get_lisp_indent() 7386 { 7387 pos_T *pos, realpos, paren; 7388 int amount; 7389 char_u *that; 7390 colnr_T col; 7391 colnr_T firsttry; 7392 int parencount, quotecount; 7393 int vi_lisp; 7394 7395 /* Set vi_lisp to use the vi-compatible method */ 7396 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); 7397 7398 realpos = curwin->w_cursor; 7399 curwin->w_cursor.col = 0; 7400 7401 if ((pos = findmatch(NULL, '(')) == NULL) 7402 pos = findmatch(NULL, '['); 7403 else 7404 { 7405 paren = *pos; 7406 pos = findmatch(NULL, '['); 7407 if (pos == NULL || ltp(pos, &paren)) 7408 pos = &paren; 7409 } 7410 if (pos != NULL) 7411 { 7412 /* Extra trick: Take the indent of the first previous non-white 7413 * line that is at the same () level. */ 7414 amount = -1; 7415 parencount = 0; 7416 7417 while (--curwin->w_cursor.lnum >= pos->lnum) 7418 { 7419 if (linewhite(curwin->w_cursor.lnum)) 7420 continue; 7421 for (that = ml_get_curline(); *that != NUL; ++that) 7422 { 7423 if (*that == ';') 7424 { 7425 while (*(that + 1) != NUL) 7426 ++that; 7427 continue; 7428 } 7429 if (*that == '\\') 7430 { 7431 if (*(that + 1) != NUL) 7432 ++that; 7433 continue; 7434 } 7435 if (*that == '"' && *(that + 1) != NUL) 7436 { 7437 that++; 7438 while (*that && (*that != '"' || *(that - 1) == '\\')) 7439 ++that; 7440 } 7441 if (*that == '(' || *that == '[') 7442 ++parencount; 7443 else if (*that == ')' || *that == ']') 7444 --parencount; 7445 } 7446 if (parencount == 0) 7447 { 7448 amount = get_indent(); 7449 break; 7450 } 7451 } 7452 7453 if (amount == -1) 7454 { 7455 curwin->w_cursor.lnum = pos->lnum; 7456 curwin->w_cursor.col = pos->col; 7457 col = pos->col; 7458 7459 that = ml_get_curline(); 7460 7461 if (vi_lisp && get_indent() == 0) 7462 amount = 2; 7463 else 7464 { 7465 amount = 0; 7466 while (*that && col) 7467 { 7468 amount += lbr_chartabsize_adv(&that, (colnr_T)amount); 7469 col--; 7470 } 7471 7472 /* 7473 * Some keywords require "body" indenting rules (the 7474 * non-standard-lisp ones are Scheme special forms): 7475 * 7476 * (let ((a 1)) instead (let ((a 1)) 7477 * (...)) of (...)) 7478 */ 7479 7480 if (!vi_lisp && (*that == '(' || *that == '[') 7481 && lisp_match(that + 1)) 7482 amount += 2; 7483 else 7484 { 7485 that++; 7486 amount++; 7487 firsttry = amount; 7488 7489 while (vim_iswhite(*that)) 7490 { 7491 amount += lbr_chartabsize(that, (colnr_T)amount); 7492 ++that; 7493 } 7494 7495 if (*that && *that != ';') /* not a comment line */ 7496 { 7497 /* test *that != '(' to accomodate first let/do 7498 * argument if it is more than one line */ 7499 if (!vi_lisp && *that != '(' && *that != '[') 7500 firsttry++; 7501 7502 parencount = 0; 7503 quotecount = 0; 7504 7505 if (vi_lisp 7506 || (*that != '"' 7507 && *that != '\'' 7508 && *that != '#' 7509 && (*that < '0' || *that > '9'))) 7510 { 7511 while (*that 7512 && (!vim_iswhite(*that) 7513 || quotecount 7514 || parencount) 7515 && (!((*that == '(' || *that == '[') 7516 && !quotecount 7517 && !parencount 7518 && vi_lisp))) 7519 { 7520 if (*that == '"') 7521 quotecount = !quotecount; 7522 if ((*that == '(' || *that == '[') 7523 && !quotecount) 7524 ++parencount; 7525 if ((*that == ')' || *that == ']') 7526 && !quotecount) 7527 --parencount; 7528 if (*that == '\\' && *(that+1) != NUL) 7529 amount += lbr_chartabsize_adv(&that, 7530 (colnr_T)amount); 7531 amount += lbr_chartabsize_adv(&that, 7532 (colnr_T)amount); 7533 } 7534 } 7535 while (vim_iswhite(*that)) 7536 { 7537 amount += lbr_chartabsize(that, (colnr_T)amount); 7538 that++; 7539 } 7540 if (!*that || *that == ';') 7541 amount = firsttry; 7542 } 7543 } 7544 } 7545 } 7546 } 7547 else 7548 amount = 0; /* no matching '(' or '[' found, use zero indent */ 7549 7550 curwin->w_cursor = realpos; 7551 7552 return amount; 7553 } 7554 #endif /* FEAT_LISP */ 7555 7556 void 7557 prepare_to_exit() 7558 { 7559 #ifdef FEAT_GUI 7560 if (gui.in_use) 7561 { 7562 gui.dying = TRUE; 7563 out_trash(); /* trash any pending output */ 7564 } 7565 else 7566 #endif 7567 { 7568 windgoto((int)Rows - 1, 0); 7569 7570 /* 7571 * Switch terminal mode back now, so messages end up on the "normal" 7572 * screen (if there are two screens). 7573 */ 7574 settmode(TMODE_COOK); 7575 #ifdef WIN3264 7576 if (can_end_termcap_mode(FALSE) == TRUE) 7577 #endif 7578 stoptermcap(); 7579 out_flush(); 7580 } 7581 } 7582 7583 /* 7584 * Preserve files and exit. 7585 * When called IObuff must contain a message. 7586 */ 7587 void 7588 preserve_exit() 7589 { 7590 buf_T *buf; 7591 7592 prepare_to_exit(); 7593 7594 out_str(IObuff); 7595 screen_start(); /* don't know where cursor is now */ 7596 out_flush(); 7597 7598 ml_close_notmod(); /* close all not-modified buffers */ 7599 7600 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 7601 { 7602 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) 7603 { 7604 OUT_STR(_("Vim: preserving files...\n")); 7605 screen_start(); /* don't know where cursor is now */ 7606 out_flush(); 7607 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ 7608 break; 7609 } 7610 } 7611 7612 ml_close_all(FALSE); /* close all memfiles, without deleting */ 7613 7614 OUT_STR(_("Vim: Finished.\n")); 7615 7616 getout(1); 7617 } 7618 7619 /* 7620 * return TRUE if "fname" exists. 7621 */ 7622 int 7623 vim_fexists(fname) 7624 char_u *fname; 7625 { 7626 struct stat st; 7627 7628 if (mch_stat((char *)fname, &st)) 7629 return FALSE; 7630 return TRUE; 7631 } 7632 7633 /* 7634 * Check for CTRL-C pressed, but only once in a while. 7635 * Should be used instead of ui_breakcheck() for functions that check for 7636 * each line in the file. Calling ui_breakcheck() each time takes too much 7637 * time, because it can be a system call. 7638 */ 7639 7640 #ifndef BREAKCHECK_SKIP 7641 # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ 7642 # define BREAKCHECK_SKIP 200 7643 # else 7644 # define BREAKCHECK_SKIP 32 7645 # endif 7646 #endif 7647 7648 static int breakcheck_count = 0; 7649 7650 void 7651 line_breakcheck() 7652 { 7653 if (++breakcheck_count >= BREAKCHECK_SKIP) 7654 { 7655 breakcheck_count = 0; 7656 ui_breakcheck(); 7657 } 7658 } 7659 7660 /* 7661 * Like line_breakcheck() but check 10 times less often. 7662 */ 7663 void 7664 fast_breakcheck() 7665 { 7666 if (++breakcheck_count >= BREAKCHECK_SKIP * 10) 7667 { 7668 breakcheck_count = 0; 7669 ui_breakcheck(); 7670 } 7671 } 7672 7673 /* 7674 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching 7675 * 'wildignore'. 7676 */ 7677 int 7678 expand_wildcards(num_pat, pat, num_file, file, flags) 7679 int num_pat; /* number of input patterns */ 7680 char_u **pat; /* array of input patterns */ 7681 int *num_file; /* resulting number of files */ 7682 char_u ***file; /* array of resulting files */ 7683 int flags; /* EW_DIR, etc. */ 7684 { 7685 int retval; 7686 int i, j; 7687 char_u *p; 7688 int non_suf_match; /* number without matching suffix */ 7689 7690 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags); 7691 7692 /* When keeping all matches, return here */ 7693 if (flags & EW_KEEPALL) 7694 return retval; 7695 7696 #ifdef FEAT_WILDIGN 7697 /* 7698 * Remove names that match 'wildignore'. 7699 */ 7700 if (*p_wig) 7701 { 7702 char_u *ffname; 7703 7704 /* check all files in (*file)[] */ 7705 for (i = 0; i < *num_file; ++i) 7706 { 7707 ffname = FullName_save((*file)[i], FALSE); 7708 if (ffname == NULL) /* out of memory */ 7709 break; 7710 # ifdef VMS 7711 vms_remove_version(ffname); 7712 # endif 7713 if (match_file_list(p_wig, (*file)[i], ffname)) 7714 { 7715 /* remove this matching file from the list */ 7716 vim_free((*file)[i]); 7717 for (j = i; j + 1 < *num_file; ++j) 7718 (*file)[j] = (*file)[j + 1]; 7719 --*num_file; 7720 --i; 7721 } 7722 vim_free(ffname); 7723 } 7724 } 7725 #endif 7726 7727 /* 7728 * Move the names where 'suffixes' match to the end. 7729 */ 7730 if (*num_file > 1) 7731 { 7732 non_suf_match = 0; 7733 for (i = 0; i < *num_file; ++i) 7734 { 7735 if (!match_suffix((*file)[i])) 7736 { 7737 /* 7738 * Move the name without matching suffix to the front 7739 * of the list. 7740 */ 7741 p = (*file)[i]; 7742 for (j = i; j > non_suf_match; --j) 7743 (*file)[j] = (*file)[j - 1]; 7744 (*file)[non_suf_match++] = p; 7745 } 7746 } 7747 } 7748 7749 return retval; 7750 } 7751 7752 /* 7753 * Return TRUE if "fname" matches with an entry in 'suffixes'. 7754 */ 7755 int 7756 match_suffix(fname) 7757 char_u *fname; 7758 { 7759 int fnamelen, setsuflen; 7760 char_u *setsuf; 7761 #define MAXSUFLEN 30 /* maximum length of a file suffix */ 7762 char_u suf_buf[MAXSUFLEN]; 7763 7764 fnamelen = (int)STRLEN(fname); 7765 setsuflen = 0; 7766 for (setsuf = p_su; *setsuf; ) 7767 { 7768 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); 7769 if (fnamelen >= setsuflen 7770 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, 7771 (size_t)setsuflen) == 0) 7772 break; 7773 setsuflen = 0; 7774 } 7775 return (setsuflen != 0); 7776 } 7777 7778 #if !defined(NO_EXPANDPATH) || defined(PROTO) 7779 7780 # ifdef VIM_BACKTICK 7781 static int vim_backtick __ARGS((char_u *p)); 7782 static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags)); 7783 # endif 7784 7785 # if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264) 7786 /* 7787 * File name expansion code for MS-DOS, Win16 and Win32. It's here because 7788 * it's shared between these systems. 7789 */ 7790 # if defined(DJGPP) || defined(PROTO) 7791 # define _cdecl /* DJGPP doesn't have this */ 7792 # else 7793 # ifdef __BORLANDC__ 7794 # define _cdecl _RTLENTRYF 7795 # endif 7796 # endif 7797 7798 /* 7799 * comparison function for qsort in dos_expandpath() 7800 */ 7801 static int _cdecl 7802 pstrcmp(const void *a, const void *b) 7803 { 7804 return (pathcmp(*(char **)a, *(char **)b)); 7805 } 7806 7807 # ifndef WIN3264 7808 static void 7809 namelowcpy( 7810 char_u *d, 7811 char_u *s) 7812 { 7813 # ifdef DJGPP 7814 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */ 7815 while (*s) 7816 *d++ = *s++; 7817 else 7818 # endif 7819 while (*s) 7820 *d++ = TOLOWER_LOC(*s++); 7821 *d = NUL; 7822 } 7823 # endif 7824 7825 /* 7826 * Recursively build up a list of files in "gap" matching the first wildcard 7827 * in `path'. Called by expand_wildcards(). 7828 * Return the number of matches found. 7829 * "path" has backslashes before chars that are not to be expanded, starting 7830 * at "path[wildoff]". 7831 */ 7832 static int 7833 dos_expandpath( 7834 garray_T *gap, 7835 char_u *path, 7836 int wildoff, 7837 int flags) /* EW_* flags */ 7838 { 7839 char_u *buf; 7840 char_u *path_end; 7841 char_u *p, *s, *e; 7842 int start_len = gap->ga_len; 7843 int ok; 7844 #ifdef WIN3264 7845 WIN32_FIND_DATA fb; 7846 HANDLE hFind = (HANDLE)0; 7847 # ifdef FEAT_MBYTE 7848 WIN32_FIND_DATAW wfb; 7849 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ 7850 # endif 7851 #else 7852 struct ffblk fb; 7853 #endif 7854 int matches; 7855 int starts_with_dot; 7856 int len; 7857 char_u *pat; 7858 regmatch_T regmatch; 7859 char_u *matchname; 7860 7861 /* make room for file name */ 7862 buf = alloc((unsigned int)STRLEN(path) + BASENAMELEN + 5); 7863 if (buf == NULL) 7864 return 0; 7865 7866 /* 7867 * Find the first part in the path name that contains a wildcard or a ~1. 7868 * Copy it into buf, including the preceding characters. 7869 */ 7870 p = buf; 7871 s = buf; 7872 e = NULL; 7873 path_end = path; 7874 while (*path_end != NUL) 7875 { 7876 /* May ignore a wildcard that has a backslash before it; it will 7877 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ 7878 if (path_end >= path + wildoff && rem_backslash(path_end)) 7879 *p++ = *path_end++; 7880 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') 7881 { 7882 if (e != NULL) 7883 break; 7884 s = p + 1; 7885 } 7886 else if (path_end >= path + wildoff 7887 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) 7888 e = p; 7889 #ifdef FEAT_MBYTE 7890 if (has_mbyte) 7891 { 7892 len = (*mb_ptr2len_check)(path_end); 7893 STRNCPY(p, path_end, len); 7894 p += len; 7895 path_end += len; 7896 } 7897 else 7898 #endif 7899 *p++ = *path_end++; 7900 } 7901 e = p; 7902 *e = NUL; 7903 7904 /* now we have one wildcard component between s and e */ 7905 /* Remove backslashes between "wildoff" and the start of the wildcard 7906 * component. */ 7907 for (p = buf + wildoff; p < s; ++p) 7908 if (rem_backslash(p)) 7909 { 7910 STRCPY(p, p + 1); 7911 --e; 7912 --s; 7913 } 7914 7915 starts_with_dot = (*s == '.'); 7916 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); 7917 if (pat == NULL) 7918 { 7919 vim_free(buf); 7920 return 0; 7921 } 7922 7923 /* compile the regexp into a program */ 7924 regmatch.rm_ic = TRUE; /* Always ignore case */ 7925 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); 7926 vim_free(pat); 7927 7928 if (regmatch.regprog == NULL) 7929 { 7930 vim_free(buf); 7931 return 0; 7932 } 7933 7934 /* remember the pattern or file name being looked for */ 7935 matchname = vim_strsave(s); 7936 7937 /* Scan all files in the directory with "dir/ *.*" */ 7938 STRCPY(s, "*.*"); 7939 #ifdef WIN3264 7940 # ifdef FEAT_MBYTE 7941 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) 7942 { 7943 /* The active codepage differs from 'encoding'. Attempt using the 7944 * wide function. If it fails because it is not implemented fall back 7945 * to the non-wide version (for Windows 98) */ 7946 wn = enc_to_ucs2(buf, NULL); 7947 if (wn != NULL) 7948 { 7949 hFind = FindFirstFileW(wn, &wfb); 7950 if (hFind == INVALID_HANDLE_VALUE 7951 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) 7952 { 7953 vim_free(wn); 7954 wn = NULL; 7955 } 7956 } 7957 } 7958 7959 if (wn == NULL) 7960 # endif 7961 hFind = FindFirstFile(buf, &fb); 7962 ok = (hFind != INVALID_HANDLE_VALUE); 7963 #else 7964 /* If we are expanding wildcards we try both files and directories */ 7965 ok = (findfirst((char *)buf, &fb, 7966 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); 7967 #endif 7968 7969 while (ok) 7970 { 7971 #ifdef WIN3264 7972 # ifdef FEAT_MBYTE 7973 if (wn != NULL) 7974 p = ucs2_to_enc(wfb.cFileName, NULL); /* p is allocated here */ 7975 else 7976 # endif 7977 p = (char_u *)fb.cFileName; 7978 #else 7979 p = (char_u *)fb.ff_name; 7980 #endif 7981 /* Ignore entries starting with a dot, unless when asked for. Accept 7982 * all entries found with "matchname". */ 7983 if ((p[0] != '.' || starts_with_dot) 7984 && (matchname == NULL 7985 || vim_regexec(®match, p, (colnr_T)0))) 7986 { 7987 #ifdef WIN3264 7988 STRCPY(s, p); 7989 #else 7990 namelowcpy(s, p); 7991 #endif 7992 len = (int)STRLEN(buf); 7993 STRCPY(buf + len, path_end); 7994 if (mch_has_exp_wildcard(path_end)) 7995 { 7996 /* need to expand another component of the path */ 7997 /* remove backslashes for the remaining components only */ 7998 (void)dos_expandpath(gap, buf, len + 1, flags); 7999 } 8000 else 8001 { 8002 /* no more wildcards, check if there is a match */ 8003 /* remove backslashes for the remaining components only */ 8004 if (*path_end != 0) 8005 backslash_halve(buf + len + 1); 8006 if (mch_getperm(buf) >= 0) /* add existing file */ 8007 addfile(gap, buf, flags); 8008 } 8009 } 8010 8011 #ifdef WIN3264 8012 # ifdef FEAT_MBYTE 8013 if (wn != NULL) 8014 { 8015 vim_free(p); 8016 ok = FindNextFileW(hFind, &wfb); 8017 } 8018 else 8019 # endif 8020 ok = FindNextFile(hFind, &fb); 8021 #else 8022 ok = (findnext(&fb) == 0); 8023 #endif 8024 8025 /* If no more matches and no match was used, try expanding the name 8026 * itself. Finds the long name of a short filename. */ 8027 if (!ok && matchname != NULL && gap->ga_len == start_len) 8028 { 8029 STRCPY(s, matchname); 8030 #ifdef WIN3264 8031 FindClose(hFind); 8032 # ifdef FEAT_MBYTE 8033 if (wn != NULL) 8034 { 8035 vim_free(wn); 8036 wn = enc_to_ucs2(buf, NULL); 8037 if (wn != NULL) 8038 hFind = FindFirstFileW(wn, &wfb); 8039 } 8040 if (wn == NULL) 8041 # endif 8042 hFind = FindFirstFile(buf, &fb); 8043 ok = (hFind != INVALID_HANDLE_VALUE); 8044 #else 8045 ok = (findfirst((char *)buf, &fb, 8046 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); 8047 #endif 8048 vim_free(matchname); 8049 matchname = NULL; 8050 } 8051 } 8052 8053 #ifdef WIN3264 8054 FindClose(hFind); 8055 # ifdef FEAT_MBYTE 8056 vim_free(wn); 8057 # endif 8058 #endif 8059 vim_free(buf); 8060 vim_free(regmatch.regprog); 8061 vim_free(matchname); 8062 8063 matches = gap->ga_len - start_len; 8064 if (matches > 0) 8065 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, 8066 sizeof(char_u *), pstrcmp); 8067 return matches; 8068 } 8069 8070 int 8071 mch_expandpath( 8072 garray_T *gap, 8073 char_u *path, 8074 int flags) /* EW_* flags */ 8075 { 8076 return dos_expandpath(gap, path, 0, flags); 8077 } 8078 # endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */ 8079 8080 /* 8081 * Generic wildcard expansion code. 8082 * 8083 * Characters in "pat" that should not be expanded must be preceded with a 8084 * backslash. E.g., "/path\ with\ spaces/my\*star*" 8085 * 8086 * Return FAIL when no single file was found. In this case "num_file" is not 8087 * set, and "file" may contain an error message. 8088 * Return OK when some files found. "num_file" is set to the number of 8089 * matches, "file" to the array of matches. Call FreeWild() later. 8090 */ 8091 int 8092 gen_expand_wildcards(num_pat, pat, num_file, file, flags) 8093 int num_pat; /* number of input patterns */ 8094 char_u **pat; /* array of input patterns */ 8095 int *num_file; /* resulting number of files */ 8096 char_u ***file; /* array of resulting files */ 8097 int flags; /* EW_* flags */ 8098 { 8099 int i; 8100 garray_T ga; 8101 char_u *p; 8102 static int recursive = FALSE; 8103 int add_pat; 8104 8105 /* 8106 * expand_env() is called to expand things like "~user". If this fails, 8107 * it calls ExpandOne(), which brings us back here. In this case, always 8108 * call the machine specific expansion function, if possible. Otherwise, 8109 * return FAIL. 8110 */ 8111 if (recursive) 8112 #ifdef SPECIAL_WILDCHAR 8113 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); 8114 #else 8115 return FAIL; 8116 #endif 8117 8118 #ifdef SPECIAL_WILDCHAR 8119 /* 8120 * If there are any special wildcard characters which we cannot handle 8121 * here, call machine specific function for all the expansion. This 8122 * avoids starting the shell for each argument separately. 8123 * For `=expr` do use the internal function. 8124 */ 8125 for (i = 0; i < num_pat; i++) 8126 { 8127 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL 8128 # ifdef VIM_BACKTICK 8129 && !(vim_backtick(pat[i]) && pat[i][1] == '=') 8130 # endif 8131 ) 8132 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); 8133 } 8134 #endif 8135 8136 recursive = TRUE; 8137 8138 /* 8139 * The matching file names are stored in a growarray. Init it empty. 8140 */ 8141 ga_init2(&ga, (int)sizeof(char_u *), 30); 8142 8143 for (i = 0; i < num_pat; ++i) 8144 { 8145 add_pat = -1; 8146 p = pat[i]; 8147 8148 #ifdef VIM_BACKTICK 8149 if (vim_backtick(p)) 8150 add_pat = expand_backtick(&ga, p, flags); 8151 else 8152 #endif 8153 { 8154 /* 8155 * First expand environment variables, "~/" and "~user/". 8156 */ 8157 if (vim_strpbrk(p, (char_u *)"$~") != NULL) 8158 { 8159 p = expand_env_save(p); 8160 if (p == NULL) 8161 p = pat[i]; 8162 #ifdef UNIX 8163 /* 8164 * On Unix, if expand_env() can't expand an environment 8165 * variable, use the shell to do that. Discard previously 8166 * found file names and start all over again. 8167 */ 8168 else if (vim_strpbrk(p, (char_u *)"$~") != NULL) 8169 { 8170 vim_free(p); 8171 ga_clear(&ga); 8172 i = mch_expand_wildcards(num_pat, pat, num_file, file, 8173 flags); 8174 recursive = FALSE; 8175 return i; 8176 } 8177 #endif 8178 } 8179 8180 /* 8181 * If there are wildcards: Expand file names and add each match to 8182 * the list. If there is no match, and EW_NOTFOUND is given, add 8183 * the pattern. 8184 * If there are no wildcards: Add the file name if it exists or 8185 * when EW_NOTFOUND is given. 8186 */ 8187 if (mch_has_exp_wildcard(p)) 8188 add_pat = mch_expandpath(&ga, p, flags); 8189 } 8190 8191 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) 8192 { 8193 char_u *t = backslash_halve_save(p); 8194 8195 #if defined(MACOS_CLASSIC) 8196 slash_to_colon(t); 8197 #endif 8198 /* When EW_NOTFOUND is used, always add files and dirs. Makes 8199 * "vim c:/" work. */ 8200 if (flags & EW_NOTFOUND) 8201 addfile(&ga, t, flags | EW_DIR | EW_FILE); 8202 else if (mch_getperm(t) >= 0) 8203 addfile(&ga, t, flags); 8204 vim_free(t); 8205 } 8206 8207 if (p != pat[i]) 8208 vim_free(p); 8209 } 8210 8211 *num_file = ga.ga_len; 8212 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; 8213 8214 recursive = FALSE; 8215 8216 return (ga.ga_data != NULL) ? OK : FAIL; 8217 } 8218 8219 # ifdef VIM_BACKTICK 8220 8221 /* 8222 * Return TRUE if we can expand this backtick thing here. 8223 */ 8224 static int 8225 vim_backtick(p) 8226 char_u *p; 8227 { 8228 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); 8229 } 8230 8231 /* 8232 * Expand an item in `backticks` by executing it as a command. 8233 * Currently only works when pat[] starts and ends with a `. 8234 * Returns number of file names found. 8235 */ 8236 static int 8237 expand_backtick(gap, pat, flags) 8238 garray_T *gap; 8239 char_u *pat; 8240 int flags; /* EW_* flags */ 8241 { 8242 char_u *p; 8243 char_u *cmd; 8244 char_u *buffer; 8245 int cnt = 0; 8246 int i; 8247 8248 /* Create the command: lop off the backticks. */ 8249 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); 8250 if (cmd == NULL) 8251 return 0; 8252 8253 #ifdef FEAT_EVAL 8254 if (*cmd == '=') /* `={expr}`: Expand expression */ 8255 buffer = eval_to_string(cmd + 1, &p); 8256 else 8257 #endif 8258 buffer = get_cmd_output(cmd, (flags & EW_SILENT) ? SHELL_SILENT : 0); 8259 vim_free(cmd); 8260 if (buffer == NULL) 8261 return 0; 8262 8263 cmd = buffer; 8264 while (*cmd != NUL) 8265 { 8266 cmd = skipwhite(cmd); /* skip over white space */ 8267 p = cmd; 8268 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ 8269 ++p; 8270 /* add an entry if it is not empty */ 8271 if (p > cmd) 8272 { 8273 i = *p; 8274 *p = NUL; 8275 addfile(gap, cmd, flags); 8276 *p = i; 8277 ++cnt; 8278 } 8279 cmd = p; 8280 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) 8281 ++cmd; 8282 } 8283 8284 vim_free(buffer); 8285 return cnt; 8286 } 8287 # endif /* VIM_BACKTICK */ 8288 8289 /* 8290 * Add a file to a file list. Accepted flags: 8291 * EW_DIR add directories 8292 * EW_FILE add files 8293 * EW_NOTFOUND add even when it doesn't exist 8294 * EW_ADDSLASH add slash after directory name 8295 */ 8296 void 8297 addfile(gap, f, flags) 8298 garray_T *gap; 8299 char_u *f; /* filename */ 8300 int flags; 8301 { 8302 char_u *p; 8303 int isdir; 8304 8305 /* if the file/dir doesn't exist, may not add it */ 8306 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0) 8307 return; 8308 8309 #ifdef FNAME_ILLEGAL 8310 /* if the file/dir contains illegal characters, don't add it */ 8311 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) 8312 return; 8313 #endif 8314 8315 isdir = mch_isdir(f); 8316 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) 8317 return; 8318 8319 /* Make room for another item in the file list. */ 8320 if (ga_grow(gap, 1) == FAIL) 8321 return; 8322 8323 p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); 8324 if (p == NULL) 8325 return; 8326 8327 STRCPY(p, f); 8328 #ifdef BACKSLASH_IN_FILENAME 8329 slash_adjust(p); 8330 #endif 8331 /* 8332 * Append a slash or backslash after directory names if none is present. 8333 */ 8334 #ifndef DONT_ADD_PATHSEP_TO_DIR 8335 if (isdir && (flags & EW_ADDSLASH)) 8336 add_pathsep(p); 8337 #endif 8338 ((char_u **)gap->ga_data)[gap->ga_len++] = p; 8339 --gap->ga_room; 8340 } 8341 #endif /* !NO_EXPANDPATH */ 8342 8343 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) 8344 8345 #ifndef SEEK_SET 8346 # define SEEK_SET 0 8347 #endif 8348 #ifndef SEEK_END 8349 # define SEEK_END 2 8350 #endif 8351 8352 /* 8353 * Get the stdout of an external command. 8354 * Returns an allocated string, or NULL for error. 8355 */ 8356 char_u * 8357 get_cmd_output(cmd, flags) 8358 char_u *cmd; 8359 int flags; /* can be SHELL_SILENT */ 8360 { 8361 char_u *tempname; 8362 char_u *command; 8363 char_u *buffer = NULL; 8364 int len; 8365 int i = 0; 8366 FILE *fd; 8367 8368 if (check_restricted() || check_secure()) 8369 return NULL; 8370 8371 /* get a name for the temp file */ 8372 if ((tempname = vim_tempname('o')) == NULL) 8373 { 8374 EMSG(_(e_notmp)); 8375 return NULL; 8376 } 8377 8378 /* Add the redirection stuff */ 8379 command = make_filter_cmd(cmd, NULL, tempname); 8380 if (command == NULL) 8381 goto done; 8382 8383 /* 8384 * Call the shell to execute the command (errors are ignored). 8385 * Don't check timestamps here. 8386 */ 8387 ++no_check_timestamps; 8388 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); 8389 --no_check_timestamps; 8390 8391 vim_free(command); 8392 8393 /* 8394 * read the names from the file into memory 8395 */ 8396 # ifdef VMS 8397 /* created temporary file is not allways readable as binary */ 8398 fd = mch_fopen((char *)tempname, "r"); 8399 # else 8400 fd = mch_fopen((char *)tempname, READBIN); 8401 # endif 8402 8403 if (fd == NULL) 8404 { 8405 EMSG2(_(e_notopen), tempname); 8406 goto done; 8407 } 8408 8409 fseek(fd, 0L, SEEK_END); 8410 len = ftell(fd); /* get size of temp file */ 8411 fseek(fd, 0L, SEEK_SET); 8412 8413 buffer = alloc(len + 1); 8414 if (buffer != NULL) 8415 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); 8416 fclose(fd); 8417 mch_remove(tempname); 8418 if (buffer == NULL) 8419 goto done; 8420 #ifdef VMS 8421 len = i; /* VMS doesn't give us what we asked for... */ 8422 #endif 8423 if (i != len) 8424 { 8425 EMSG2(_(e_notread), tempname); 8426 vim_free(buffer); 8427 buffer = NULL; 8428 } 8429 else 8430 buffer[len] = '\0'; /* make sure the buffer is terminated */ 8431 8432 done: 8433 vim_free(tempname); 8434 return buffer; 8435 } 8436 #endif 8437 8438 /* 8439 * Free the list of files returned by expand_wildcards() or other expansion 8440 * functions. 8441 */ 8442 void 8443 FreeWild(count, files) 8444 int count; 8445 char_u **files; 8446 { 8447 if (files == NULL || count <= 0) 8448 return; 8449 #if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */ 8450 /* 8451 * Is this still OK for when other functions than expand_wildcards() have 8452 * been used??? 8453 */ 8454 _fnexplodefree((char **)files); 8455 #else 8456 while (count--) 8457 vim_free(files[count]); 8458 vim_free(files); 8459 #endif 8460 } 8461 8462 /* 8463 * return TRUE when need to go to Insert mode because of 'insertmode'. 8464 * Don't do this when still processing a command or a mapping. 8465 * Don't do this when inside a ":normal" command. 8466 */ 8467 int 8468 goto_im() 8469 { 8470 return (p_im && stuff_empty() && typebuf_typed()); 8471 } 8472