1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * ui.c: functions that handle the user interface. 12 * 1. Keyboard input stuff, and a bit of windowing stuff. These are called 13 * before the machine specific stuff (mch_*) so that we can call the GUI 14 * stuff instead if the GUI is running. 15 * 2. Input buffer stuff. 16 */ 17 18 #include "vim.h" 19 20 void 21 ui_write(char_u *s, int len) 22 { 23 #ifdef FEAT_GUI 24 if (gui.in_use && !gui.dying && !gui.starting) 25 { 26 gui_write(s, len); 27 if (p_wd) 28 gui_wait_for_chars(p_wd, typebuf.tb_change_cnt); 29 return; 30 } 31 #endif 32 #ifndef NO_CONSOLE 33 // Don't output anything in silent mode ("ex -s") unless 'verbose' set 34 if (!(silent_mode && p_verbose == 0)) 35 { 36 #if !defined(MSWIN) 37 char_u *tofree = NULL; 38 39 if (output_conv.vc_type != CONV_NONE) 40 { 41 // Convert characters from 'encoding' to 'termencoding'. 42 tofree = string_convert(&output_conv, s, &len); 43 if (tofree != NULL) 44 s = tofree; 45 } 46 #endif 47 48 mch_write(s, len); 49 50 # if !defined(MSWIN) 51 if (output_conv.vc_type != CONV_NONE) 52 vim_free(tofree); 53 # endif 54 } 55 #endif 56 } 57 58 #if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(MSWIN) 59 /* 60 * When executing an external program, there may be some typed characters that 61 * are not consumed by it. Give them back to ui_inchar() and they are stored 62 * here for the next call. 63 */ 64 static char_u *ta_str = NULL; 65 static int ta_off; // offset for next char to use when ta_str != NULL 66 static int ta_len; // length of ta_str when it's not NULL 67 68 void 69 ui_inchar_undo(char_u *s, int len) 70 { 71 char_u *new; 72 int newlen; 73 74 newlen = len; 75 if (ta_str != NULL) 76 newlen += ta_len - ta_off; 77 new = alloc(newlen); 78 if (new != NULL) 79 { 80 if (ta_str != NULL) 81 { 82 mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off)); 83 mch_memmove(new + ta_len - ta_off, s, (size_t)len); 84 vim_free(ta_str); 85 } 86 else 87 mch_memmove(new, s, (size_t)len); 88 ta_str = new; 89 ta_len = newlen; 90 ta_off = 0; 91 } 92 } 93 #endif 94 95 /* 96 * ui_inchar(): low level input function. 97 * Get characters from the keyboard. 98 * Return the number of characters that are available. 99 * If "wtime" == 0 do not wait for characters. 100 * If "wtime" == -1 wait forever for characters. 101 * If "wtime" > 0 wait "wtime" milliseconds for a character. 102 * 103 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into 104 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received 105 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL 106 * otherwise. 107 */ 108 int 109 ui_inchar( 110 char_u *buf, 111 int maxlen, 112 long wtime, // don't use "time", MIPS cannot handle it 113 int tb_change_cnt) 114 { 115 int retval = 0; 116 117 #if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS)) 118 /* 119 * Use the typeahead if there is any. 120 */ 121 if (ta_str != NULL) 122 { 123 if (maxlen >= ta_len - ta_off) 124 { 125 mch_memmove(buf, ta_str + ta_off, (size_t)ta_len); 126 VIM_CLEAR(ta_str); 127 return ta_len; 128 } 129 mch_memmove(buf, ta_str + ta_off, (size_t)maxlen); 130 ta_off += maxlen; 131 return maxlen; 132 } 133 #endif 134 135 #ifdef FEAT_PROFILE 136 if (do_profiling == PROF_YES && wtime != 0) 137 prof_inchar_enter(); 138 #endif 139 140 #ifdef NO_CONSOLE_INPUT 141 // Don't wait for character input when the window hasn't been opened yet. 142 // Do try reading, this works when redirecting stdin from a file. 143 // Must return something, otherwise we'll loop forever. If we run into 144 // this very often we probably got stuck, exit Vim. 145 if (no_console_input()) 146 { 147 static int count = 0; 148 149 # ifndef NO_CONSOLE 150 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt); 151 if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0) 152 goto theend; 153 # endif 154 if (wtime == -1 && ++count == 1000) 155 read_error_exit(); 156 buf[0] = CAR; 157 retval = 1; 158 goto theend; 159 } 160 #endif 161 162 // If we are going to wait for some time or block... 163 if (wtime == -1 || wtime > 100L) 164 { 165 // ... allow signals to kill us. 166 (void)vim_handle_signal(SIGNAL_UNBLOCK); 167 168 // ... there is no need for CTRL-C to interrupt something, don't let 169 // it set got_int when it was mapped. 170 if ((mapped_ctrl_c | curbuf->b_mapped_ctrl_c) & get_real_state()) 171 ctrl_c_interrupts = FALSE; 172 } 173 174 /* 175 * Here we call gui_inchar() or mch_inchar(), the GUI or machine-dependent 176 * input function. The functionality they implement is like this: 177 * 178 * while (not timed out) 179 * { 180 * handle-resize; 181 * parse-queued-messages; 182 * if (waited for 'updatetime') 183 * trigger-cursorhold; 184 * ui_wait_for_chars_or_timer() 185 * if (character available) 186 * break; 187 * } 188 * 189 * ui_wait_for_chars_or_timer() does: 190 * 191 * while (not timed out) 192 * { 193 * if (any-timer-triggered) 194 * invoke-timer-callback; 195 * wait-for-character(); 196 * if (character available) 197 * break; 198 * } 199 * 200 * wait-for-character() does: 201 * while (not timed out) 202 * { 203 * Wait for event; 204 * if (something on channel) 205 * read/write channel; 206 * else if (resized) 207 * handle_resize(); 208 * else if (system event) 209 * deal-with-system-event; 210 * else if (character available) 211 * break; 212 * } 213 * 214 */ 215 216 #ifdef FEAT_GUI 217 if (gui.in_use) 218 retval = gui_inchar(buf, maxlen, wtime, tb_change_cnt); 219 #endif 220 #ifndef NO_CONSOLE 221 # ifdef FEAT_GUI 222 else 223 # endif 224 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt); 225 #endif 226 227 if (wtime == -1 || wtime > 100L) 228 // block SIGHUP et al. 229 (void)vim_handle_signal(SIGNAL_BLOCK); 230 231 ctrl_c_interrupts = TRUE; 232 233 #ifdef NO_CONSOLE_INPUT 234 theend: 235 #endif 236 #ifdef FEAT_PROFILE 237 if (do_profiling == PROF_YES && wtime != 0) 238 prof_inchar_exit(); 239 #endif 240 return retval; 241 } 242 243 #if defined(UNIX) || defined(VMS) || defined(FEAT_GUI) || defined(PROTO) 244 /* 245 * Common code for mch_inchar() and gui_inchar(): Wait for a while or 246 * indefinitely until characters are available, dealing with timers and 247 * messages on channels. 248 * 249 * "buf" may be NULL if the available characters are not to be returned, only 250 * check if they are available. 251 * 252 * Return the number of characters that are available. 253 * If "wtime" == 0 do not wait for characters. 254 * If "wtime" == n wait a short time for characters. 255 * If "wtime" == -1 wait forever for characters. 256 */ 257 int 258 inchar_loop( 259 char_u *buf, 260 int maxlen, 261 long wtime, // don't use "time", MIPS cannot handle it 262 int tb_change_cnt, 263 int (*wait_func)(long wtime, int *interrupted, int ignore_input), 264 int (*resize_func)(int check_only)) 265 { 266 int len; 267 int interrupted = FALSE; 268 int did_call_wait_func = FALSE; 269 int did_start_blocking = FALSE; 270 long wait_time; 271 long elapsed_time = 0; 272 #ifdef ELAPSED_FUNC 273 elapsed_T start_tv; 274 275 ELAPSED_INIT(start_tv); 276 #endif 277 278 // repeat until we got a character or waited long enough 279 for (;;) 280 { 281 // Check if window changed size while we were busy, perhaps the ":set 282 // columns=99" command was used. 283 if (resize_func != NULL) 284 resize_func(FALSE); 285 286 #ifdef MESSAGE_QUEUE 287 // Only process messages when waiting. 288 if (wtime != 0) 289 { 290 parse_queued_messages(); 291 // If input was put directly in typeahead buffer bail out here. 292 if (typebuf_changed(tb_change_cnt)) 293 return 0; 294 } 295 #endif 296 if (wtime < 0 && did_start_blocking) 297 // blocking and already waited for p_ut 298 wait_time = -1; 299 else 300 { 301 if (wtime >= 0) 302 wait_time = wtime; 303 else 304 // going to block after p_ut 305 wait_time = p_ut; 306 #ifdef ELAPSED_FUNC 307 elapsed_time = ELAPSED_FUNC(start_tv); 308 #endif 309 wait_time -= elapsed_time; 310 311 // If the waiting time is now zero or less, we timed out. However, 312 // loop at least once to check for characters and events. Matters 313 // when "wtime" is zero. 314 if (wait_time <= 0 && did_call_wait_func) 315 { 316 if (wtime >= 0) 317 // no character available within "wtime" 318 return 0; 319 320 // No character available within 'updatetime'. 321 did_start_blocking = TRUE; 322 if (trigger_cursorhold() && maxlen >= 3 323 && !typebuf_changed(tb_change_cnt)) 324 { 325 // Put K_CURSORHOLD in the input buffer or return it. 326 if (buf == NULL) 327 { 328 char_u ibuf[3]; 329 330 ibuf[0] = CSI; 331 ibuf[1] = KS_EXTRA; 332 ibuf[2] = (int)KE_CURSORHOLD; 333 add_to_input_buf(ibuf, 3); 334 } 335 else 336 { 337 buf[0] = K_SPECIAL; 338 buf[1] = KS_EXTRA; 339 buf[2] = (int)KE_CURSORHOLD; 340 } 341 return 3; 342 } 343 344 // There is no character available within 'updatetime' seconds: 345 // flush all the swap files to disk. Also done when 346 // interrupted by SIGWINCH. 347 before_blocking(); 348 continue; 349 } 350 } 351 352 #ifdef FEAT_JOB_CHANNEL 353 if (wait_time < 0 || wait_time > 100L) 354 { 355 // Checking if a job ended requires polling. Do this at least 356 // every 100 msec. 357 if (has_pending_job()) 358 wait_time = 100L; 359 360 // If there is readahead then parse_queued_messages() timed out and 361 // we should call it again soon. 362 if (channel_any_readahead()) 363 wait_time = 10L; 364 } 365 #endif 366 #ifdef FEAT_BEVAL_GUI 367 if (p_beval && wait_time > 100L) 368 // The 'balloonexpr' may indirectly invoke a callback while waiting 369 // for a character, need to check often. 370 wait_time = 100L; 371 #endif 372 373 // Wait for a character to be typed or another event, such as the winch 374 // signal or an event on the monitored file descriptors. 375 did_call_wait_func = TRUE; 376 if (wait_func(wait_time, &interrupted, FALSE)) 377 { 378 // If input was put directly in typeahead buffer bail out here. 379 if (typebuf_changed(tb_change_cnt)) 380 return 0; 381 382 // We might have something to return now. 383 if (buf == NULL) 384 // "buf" is NULL, we were just waiting, not actually getting 385 // input. 386 return input_available(); 387 388 len = read_from_input_buf(buf, (long)maxlen); 389 if (len > 0) 390 return len; 391 continue; 392 } 393 // Timed out or interrupted with no character available. 394 395 #ifndef ELAPSED_FUNC 396 // estimate the elapsed time 397 elapsed_time += wait_time; 398 #endif 399 400 if ((resize_func != NULL && resize_func(TRUE)) 401 #if defined(FEAT_CLIENTSERVER) && defined(UNIX) 402 || server_waiting() 403 #endif 404 #ifdef MESSAGE_QUEUE 405 || interrupted 406 #endif 407 || wait_time > 0 408 || (wtime < 0 && !did_start_blocking)) 409 // no character available, but something to be done, keep going 410 continue; 411 412 // no character available or interrupted, return zero 413 break; 414 } 415 return 0; 416 } 417 #endif 418 419 #if defined(FEAT_TIMERS) || defined(PROTO) 420 /* 421 * Wait for a timer to fire or "wait_func" to return non-zero. 422 * Returns OK when something was read. 423 * Returns FAIL when it timed out or was interrupted. 424 */ 425 int 426 ui_wait_for_chars_or_timer( 427 long wtime, 428 int (*wait_func)(long wtime, int *interrupted, int ignore_input), 429 int *interrupted, 430 int ignore_input) 431 { 432 int due_time; 433 long remaining = wtime; 434 int tb_change_cnt = typebuf.tb_change_cnt; 435 # ifdef FEAT_JOB_CHANNEL 436 int brief_wait = FALSE; 437 # endif 438 439 // When waiting very briefly don't trigger timers. 440 if (wtime >= 0 && wtime < 10L) 441 return wait_func(wtime, NULL, ignore_input); 442 443 while (wtime < 0 || remaining > 0) 444 { 445 // Trigger timers and then get the time in wtime until the next one is 446 // due. Wait up to that time. 447 due_time = check_due_timer(); 448 if (typebuf.tb_change_cnt != tb_change_cnt) 449 { 450 // timer may have used feedkeys() 451 return FAIL; 452 } 453 if (due_time <= 0 || (wtime > 0 && due_time > remaining)) 454 due_time = remaining; 455 # if defined(FEAT_JOB_CHANNEL) || defined(FEAT_SOUND_CANBERRA) 456 if ((due_time < 0 || due_time > 10L) && ( 457 # if defined(FEAT_JOB_CHANNEL) 458 ( 459 # if defined(FEAT_GUI) 460 !gui.in_use && 461 # endif 462 (has_pending_job() || channel_any_readahead())) 463 # ifdef FEAT_SOUND_CANBERRA 464 || 465 # endif 466 # endif 467 # ifdef FEAT_SOUND_CANBERRA 468 has_any_sound_callback() 469 # endif 470 )) 471 { 472 // There is a pending job or channel, should return soon in order 473 // to handle them ASAP. Do check for input briefly. 474 due_time = 10L; 475 # ifdef FEAT_JOB_CHANNEL 476 brief_wait = TRUE; 477 # endif 478 } 479 # endif 480 if (wait_func(due_time, interrupted, ignore_input)) 481 return OK; 482 if ((interrupted != NULL && *interrupted) 483 # ifdef FEAT_JOB_CHANNEL 484 || brief_wait 485 # endif 486 ) 487 // Nothing available, but need to return so that side effects get 488 // handled, such as handling a message on a channel. 489 return FAIL; 490 if (wtime > 0) 491 remaining -= due_time; 492 } 493 return FAIL; 494 } 495 #endif 496 497 /* 498 * Return non-zero if a character is available. 499 */ 500 int 501 ui_char_avail(void) 502 { 503 #ifdef FEAT_GUI 504 if (gui.in_use) 505 { 506 gui_mch_update(); 507 return input_available(); 508 } 509 #endif 510 #ifndef NO_CONSOLE 511 # ifdef NO_CONSOLE_INPUT 512 if (no_console_input()) 513 return 0; 514 # endif 515 return mch_char_avail(); 516 #else 517 return 0; 518 #endif 519 } 520 521 /* 522 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we 523 * cancel the delay if a key is hit. 524 */ 525 void 526 ui_delay(long msec_arg, int ignoreinput) 527 { 528 long msec = msec_arg; 529 530 #ifdef FEAT_EVAL 531 if (ui_delay_for_testing > 0) 532 msec = ui_delay_for_testing; 533 #endif 534 #ifdef FEAT_JOB_CHANNEL 535 ch_log(NULL, "ui_delay(%ld)", msec); 536 #endif 537 #ifdef FEAT_GUI 538 if (gui.in_use && !ignoreinput) 539 gui_wait_for_chars(msec, typebuf.tb_change_cnt); 540 else 541 #endif 542 mch_delay(msec, ignoreinput); 543 } 544 545 /* 546 * If the machine has job control, use it to suspend the program, 547 * otherwise fake it by starting a new shell. 548 * When running the GUI iconify the window. 549 */ 550 void 551 ui_suspend(void) 552 { 553 #ifdef FEAT_GUI 554 if (gui.in_use) 555 { 556 gui_mch_iconify(); 557 return; 558 } 559 #endif 560 mch_suspend(); 561 } 562 563 #if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) 564 /* 565 * When the OS can't really suspend, call this function to start a shell. 566 * This is never called in the GUI. 567 */ 568 void 569 suspend_shell(void) 570 { 571 if (*p_sh == NUL) 572 emsg(_(e_shellempty)); 573 else 574 { 575 msg_puts(_("new shell started\n")); 576 do_shell(NULL, 0); 577 } 578 } 579 #endif 580 581 /* 582 * Try to get the current Vim shell size. Put the result in Rows and Columns. 583 * Use the new sizes as defaults for 'columns' and 'lines'. 584 * Return OK when size could be determined, FAIL otherwise. 585 */ 586 int 587 ui_get_shellsize(void) 588 { 589 int retval; 590 591 #ifdef FEAT_GUI 592 if (gui.in_use) 593 retval = gui_get_shellsize(); 594 else 595 #endif 596 retval = mch_get_shellsize(); 597 598 check_shellsize(); 599 600 // adjust the default for 'lines' and 'columns' 601 if (retval == OK) 602 { 603 set_number_default("lines", Rows); 604 set_number_default("columns", Columns); 605 } 606 return retval; 607 } 608 609 /* 610 * Set the size of the Vim shell according to Rows and Columns, if possible. 611 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the 612 * new size. If this is not possible, it will adjust Rows and Columns. 613 */ 614 void 615 ui_set_shellsize( 616 int mustset UNUSED) // set by the user 617 { 618 #ifdef FEAT_GUI 619 if (gui.in_use) 620 gui_set_shellsize(mustset, TRUE, RESIZE_BOTH); 621 else 622 #endif 623 mch_set_shellsize(); 624 } 625 626 /* 627 * Called when Rows and/or Columns changed. Adjust scroll region and mouse 628 * region. 629 */ 630 void 631 ui_new_shellsize(void) 632 { 633 if (full_screen && !exiting) 634 { 635 #ifdef FEAT_GUI 636 if (gui.in_use) 637 gui_new_shellsize(); 638 else 639 #endif 640 mch_new_shellsize(); 641 } 642 } 643 644 #if ((defined(FEAT_EVAL) || defined(FEAT_TERMINAL)) \ 645 && (defined(FEAT_GUI) \ 646 || defined(MSWIN) \ 647 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)))) \ 648 || defined(PROTO) 649 /* 650 * Get the window position in pixels, if possible. 651 * Return FAIL when not possible. 652 */ 653 int 654 ui_get_winpos(int *x, int *y, varnumber_T timeout UNUSED) 655 { 656 # ifdef FEAT_GUI 657 if (gui.in_use) 658 return gui_mch_get_winpos(x, y); 659 # endif 660 # if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) 661 return mch_get_winpos(x, y); 662 # else 663 # if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) 664 return term_get_winpos(x, y, timeout); 665 # else 666 return FAIL; 667 # endif 668 # endif 669 } 670 #endif 671 672 void 673 ui_breakcheck(void) 674 { 675 ui_breakcheck_force(FALSE); 676 } 677 678 /* 679 * When "force" is true also check when the terminal is not in raw mode. 680 * This is useful to read input on channels. 681 */ 682 void 683 ui_breakcheck_force(int force) 684 { 685 static int recursive = FALSE; 686 int save_updating_screen = updating_screen; 687 688 // We could be called recursively if stderr is redirected, calling 689 // fill_input_buf() calls settmode() when stdin isn't a tty. settmode() 690 // calls vgetorpeek() which calls ui_breakcheck() again. 691 if (recursive) 692 return; 693 recursive = TRUE; 694 695 // We do not want gui_resize_shell() to redraw the screen here. 696 ++updating_screen; 697 698 #ifdef FEAT_GUI 699 if (gui.in_use) 700 gui_mch_update(); 701 else 702 #endif 703 mch_breakcheck(force); 704 705 if (save_updating_screen) 706 updating_screen = TRUE; 707 else 708 after_updating_screen(FALSE); 709 710 recursive = FALSE; 711 } 712 713 ////////////////////////////////////////////////////////////////////////////// 714 // Functions that handle the input buffer. 715 // This is used for any GUI version, and the unix terminal version. 716 // 717 // For Unix, the input characters are buffered to be able to check for a 718 // CTRL-C. This should be done with signals, but I don't know how to do that 719 // in a portable way for a tty in RAW mode. 720 // 721 // For the client-server code in the console the received keys are put in the 722 // input buffer. 723 724 #if defined(USE_INPUT_BUF) || defined(PROTO) 725 726 /* 727 * Internal typeahead buffer. Includes extra space for long key code 728 * descriptions which would otherwise overflow. The buffer is considered full 729 * when only this extra space (or part of it) remains. 730 */ 731 #if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER) 732 /* 733 * NetBeans stuffs debugger commands into the input buffer. 734 * This requires a larger buffer... 735 * (Madsen) Go with this for remote input as well ... 736 */ 737 # define INBUFLEN 4096 738 #else 739 # define INBUFLEN 250 740 #endif 741 742 static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN]; 743 static int inbufcount = 0; // number of chars in inbuf[] 744 745 /* 746 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and 747 * trash_input_buf() are functions for manipulating the input buffer. These 748 * are used by the gui_* calls when a GUI is used to handle keyboard input. 749 */ 750 751 int 752 vim_is_input_buf_full(void) 753 { 754 return (inbufcount >= INBUFLEN); 755 } 756 757 int 758 vim_is_input_buf_empty(void) 759 { 760 return (inbufcount == 0); 761 } 762 763 #if defined(FEAT_OLE) || defined(PROTO) 764 int 765 vim_free_in_input_buf(void) 766 { 767 return (INBUFLEN - inbufcount); 768 } 769 #endif 770 771 #if defined(FEAT_GUI_GTK) || defined(PROTO) 772 int 773 vim_used_in_input_buf(void) 774 { 775 return inbufcount; 776 } 777 #endif 778 779 /* 780 * Return the current contents of the input buffer and make it empty. 781 * The returned pointer must be passed to set_input_buf() later. 782 */ 783 char_u * 784 get_input_buf(void) 785 { 786 garray_T *gap; 787 788 // We use a growarray to store the data pointer and the length. 789 gap = ALLOC_ONE(garray_T); 790 if (gap != NULL) 791 { 792 // Add one to avoid a zero size. 793 gap->ga_data = alloc(inbufcount + 1); 794 if (gap->ga_data != NULL) 795 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount); 796 gap->ga_len = inbufcount; 797 } 798 trash_input_buf(); 799 return (char_u *)gap; 800 } 801 802 /* 803 * Restore the input buffer with a pointer returned from get_input_buf(). 804 * The allocated memory is freed, this only works once! 805 */ 806 void 807 set_input_buf(char_u *p) 808 { 809 garray_T *gap = (garray_T *)p; 810 811 if (gap != NULL) 812 { 813 if (gap->ga_data != NULL) 814 { 815 mch_memmove(inbuf, gap->ga_data, gap->ga_len); 816 inbufcount = gap->ga_len; 817 vim_free(gap->ga_data); 818 } 819 vim_free(gap); 820 } 821 } 822 823 /* 824 * Add the given bytes to the input buffer 825 * Special keys start with CSI. A real CSI must have been translated to 826 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation. 827 */ 828 void 829 add_to_input_buf(char_u *s, int len) 830 { 831 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN) 832 return; // Shouldn't ever happen! 833 834 while (len--) 835 inbuf[inbufcount++] = *s++; 836 } 837 838 /* 839 * Add "str[len]" to the input buffer while escaping CSI bytes. 840 */ 841 void 842 add_to_input_buf_csi(char_u *str, int len) 843 { 844 int i; 845 char_u buf[2]; 846 847 for (i = 0; i < len; ++i) 848 { 849 add_to_input_buf(str + i, 1); 850 if (str[i] == CSI) 851 { 852 // Turn CSI into K_CSI. 853 buf[0] = KS_EXTRA; 854 buf[1] = (int)KE_CSI; 855 add_to_input_buf(buf, 2); 856 } 857 } 858 } 859 860 /* 861 * Remove everything from the input buffer. Called when ^C is found. 862 */ 863 void 864 trash_input_buf(void) 865 { 866 inbufcount = 0; 867 } 868 869 /* 870 * Read as much data from the input buffer as possible up to maxlen, and store 871 * it in buf. 872 */ 873 int 874 read_from_input_buf(char_u *buf, long maxlen) 875 { 876 if (inbufcount == 0) // if the buffer is empty, fill it 877 fill_input_buf(TRUE); 878 if (maxlen > inbufcount) 879 maxlen = inbufcount; 880 mch_memmove(buf, inbuf, (size_t)maxlen); 881 inbufcount -= maxlen; 882 if (inbufcount) 883 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount); 884 return (int)maxlen; 885 } 886 887 void 888 fill_input_buf(int exit_on_error UNUSED) 889 { 890 #if defined(UNIX) || defined(VMS) || defined(MACOS_X) 891 int len; 892 int try; 893 static int did_read_something = FALSE; 894 static char_u *rest = NULL; // unconverted rest of previous read 895 static int restlen = 0; 896 int unconverted; 897 #endif 898 899 #ifdef FEAT_GUI 900 if (gui.in_use 901 # ifdef NO_CONSOLE_INPUT 902 // Don't use the GUI input when the window hasn't been opened yet. 903 // We get here from ui_inchar() when we should try reading from stdin. 904 && !no_console_input() 905 # endif 906 ) 907 { 908 gui_mch_update(); 909 return; 910 } 911 #endif 912 #if defined(UNIX) || defined(VMS) || defined(MACOS_X) 913 if (vim_is_input_buf_full()) 914 return; 915 /* 916 * Fill_input_buf() is only called when we really need a character. 917 * If we can't get any, but there is some in the buffer, just return. 918 * If we can't get any, and there isn't any in the buffer, we give up and 919 * exit Vim. 920 */ 921 if (rest != NULL) 922 { 923 // Use remainder of previous call, starts with an invalid character 924 // that may become valid when reading more. 925 if (restlen > INBUFLEN - inbufcount) 926 unconverted = INBUFLEN - inbufcount; 927 else 928 unconverted = restlen; 929 mch_memmove(inbuf + inbufcount, rest, unconverted); 930 if (unconverted == restlen) 931 VIM_CLEAR(rest); 932 else 933 { 934 restlen -= unconverted; 935 mch_memmove(rest, rest + unconverted, restlen); 936 } 937 inbufcount += unconverted; 938 } 939 else 940 unconverted = 0; 941 942 len = 0; // to avoid gcc warning 943 for (try = 0; try < 100; ++try) 944 { 945 size_t readlen = (size_t)((INBUFLEN - inbufcount) 946 / input_conv.vc_factor); 947 # ifdef VMS 948 len = vms_read((char *)inbuf + inbufcount, readlen); 949 # else 950 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen); 951 # endif 952 953 if (len > 0 || got_int) 954 break; 955 /* 956 * If reading stdin results in an error, continue reading stderr. 957 * This helps when using "foo | xargs vim". 958 */ 959 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0) 960 { 961 int m = cur_tmode; 962 963 // We probably set the wrong file descriptor to raw mode. Switch 964 // back to cooked mode, use another descriptor and set the mode to 965 // what it was. 966 settmode(TMODE_COOK); 967 #ifdef HAVE_DUP 968 // Use stderr for stdin, also works for shell commands. 969 close(0); 970 vim_ignored = dup(2); 971 #else 972 read_cmd_fd = 2; // read from stderr instead of stdin 973 #endif 974 settmode(m); 975 } 976 if (!exit_on_error) 977 return; 978 } 979 if (len <= 0 && !got_int) 980 read_error_exit(); 981 if (len > 0) 982 did_read_something = TRUE; 983 if (got_int) 984 { 985 // Interrupted, pretend a CTRL-C was typed. 986 inbuf[0] = 3; 987 inbufcount = 1; 988 } 989 else 990 { 991 /* 992 * May perform conversion on the input characters. 993 * Include the unconverted rest of the previous call. 994 * If there is an incomplete char at the end it is kept for the next 995 * time, reading more bytes should make conversion possible. 996 * Don't do this in the unlikely event that the input buffer is too 997 * small ("rest" still contains more bytes). 998 */ 999 if (input_conv.vc_type != CONV_NONE) 1000 { 1001 inbufcount -= unconverted; 1002 len = convert_input_safe(inbuf + inbufcount, 1003 len + unconverted, INBUFLEN - inbufcount, 1004 rest == NULL ? &rest : NULL, &restlen); 1005 } 1006 while (len-- > 0) 1007 { 1008 /* 1009 * If a CTRL-C was typed, remove it from the buffer and set 1010 * got_int. Also recognize CTRL-C with modifyOtherKeys set. 1011 */ 1012 if (ctrl_c_interrupts && (inbuf[inbufcount] == 3 1013 || (len >= 9 && STRNCMP(inbuf + inbufcount, 1014 "\033[27;5;99~", 10) == 0))) 1015 { 1016 // remove everything typed before the CTRL-C 1017 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1)); 1018 inbufcount = 0; 1019 got_int = TRUE; 1020 } 1021 ++inbufcount; 1022 } 1023 } 1024 #endif // UNIX || VMS || MACOS_X 1025 } 1026 #endif // USE_INPUT_BUF 1027 1028 /* 1029 * Exit because of an input read error. 1030 */ 1031 void 1032 read_error_exit(void) 1033 { 1034 if (silent_mode) // Normal way to exit for "ex -s" 1035 getout(0); 1036 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n")); 1037 preserve_exit(); 1038 } 1039 1040 #if defined(CURSOR_SHAPE) || defined(PROTO) 1041 /* 1042 * May update the shape of the cursor. 1043 */ 1044 void 1045 ui_cursor_shape_forced(int forced) 1046 { 1047 # ifdef FEAT_GUI 1048 if (gui.in_use) 1049 gui_update_cursor_later(); 1050 else 1051 # endif 1052 term_cursor_mode(forced); 1053 1054 # ifdef MCH_CURSOR_SHAPE 1055 mch_update_cursor(); 1056 # endif 1057 1058 # ifdef FEAT_CONCEAL 1059 conceal_check_cursor_line(); 1060 # endif 1061 } 1062 1063 void 1064 ui_cursor_shape(void) 1065 { 1066 ui_cursor_shape_forced(FALSE); 1067 } 1068 #endif 1069 1070 /* 1071 * Check bounds for column number 1072 */ 1073 int 1074 check_col(int col) 1075 { 1076 if (col < 0) 1077 return 0; 1078 if (col >= (int)screen_Columns) 1079 return (int)screen_Columns - 1; 1080 return col; 1081 } 1082 1083 /* 1084 * Check bounds for row number 1085 */ 1086 int 1087 check_row(int row) 1088 { 1089 if (row < 0) 1090 return 0; 1091 if (row >= (int)screen_Rows) 1092 return (int)screen_Rows - 1; 1093 return row; 1094 } 1095 1096 #if defined(FEAT_GUI) || defined(MSWIN) || defined(PROTO) 1097 /* 1098 * Called when focus changed. Used for the GUI or for systems where this can 1099 * be done in the console (Win32). 1100 */ 1101 void 1102 ui_focus_change( 1103 int in_focus) // TRUE if focus gained. 1104 { 1105 static time_t last_time = (time_t)0; 1106 int need_redraw = FALSE; 1107 1108 // When activated: Check if any file was modified outside of Vim. 1109 // Only do this when not done within the last two seconds (could get 1110 // several events in a row). 1111 if (in_focus && last_time + 2 < time(NULL)) 1112 { 1113 need_redraw = check_timestamps( 1114 # ifdef FEAT_GUI 1115 gui.in_use 1116 # else 1117 FALSE 1118 # endif 1119 ); 1120 last_time = time(NULL); 1121 } 1122 1123 /* 1124 * Fire the focus gained/lost autocommand. 1125 */ 1126 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED 1127 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf); 1128 1129 if (need_redraw) 1130 { 1131 // Something was executed, make sure the cursor is put back where it 1132 // belongs. 1133 need_wait_return = FALSE; 1134 1135 if (State & CMDLINE) 1136 redrawcmdline(); 1137 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE 1138 || State == EXTERNCMD || State == CONFIRM || exmode_active) 1139 repeat_message(); 1140 else if ((State & NORMAL) || (State & INSERT)) 1141 { 1142 if (must_redraw != 0) 1143 update_screen(0); 1144 setcursor(); 1145 } 1146 cursor_on(); // redrawing may have switched it off 1147 out_flush_cursor(FALSE, TRUE); 1148 # ifdef FEAT_GUI 1149 if (gui.in_use) 1150 gui_update_scrollbars(FALSE); 1151 # endif 1152 } 1153 #ifdef FEAT_TITLE 1154 // File may have been changed from 'readonly' to 'noreadonly' 1155 if (need_maketitle) 1156 maketitle(); 1157 #endif 1158 } 1159 #endif 1160 1161 #if defined(HAVE_INPUT_METHOD) || defined(PROTO) 1162 /* 1163 * Save current Input Method status to specified place. 1164 */ 1165 void 1166 im_save_status(long *psave) 1167 { 1168 // Don't save when 'imdisable' is set or "xic" is NULL, IM is always 1169 // disabled then (but might start later). 1170 // Also don't save when inside a mapping, vgetc_im_active has not been set 1171 // then. 1172 // And don't save when the keys were stuffed (e.g., for a "." command). 1173 // And don't save when the GUI is running but our window doesn't have 1174 // input focus (e.g., when a find dialog is open). 1175 if (!p_imdisable && KeyTyped && !KeyStuffed 1176 # ifdef FEAT_XIM 1177 && xic != NULL 1178 # endif 1179 # ifdef FEAT_GUI 1180 && (!gui.in_use || gui.in_focus) 1181 # endif 1182 ) 1183 { 1184 // Do save when IM is on, or IM is off and saved status is on. 1185 if (vgetc_im_active) 1186 *psave = B_IMODE_IM; 1187 else if (*psave == B_IMODE_IM) 1188 *psave = B_IMODE_NONE; 1189 } 1190 } 1191 #endif 1192