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 * message.c: functions for displaying messages on the command line 12 */ 13 14 #define MESSAGE_FILE // don't include prototype for smsg() 15 #define USING_FLOAT_STUFF 16 17 #include "vim.h" 18 19 static void add_msg_hist(char_u *s, int len, int attr); 20 static void hit_return_msg(void); 21 static void msg_home_replace_attr(char_u *fname, int attr); 22 static void msg_puts_attr_len(char *str, int maxlen, int attr); 23 static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse); 24 static void msg_scroll_up(void); 25 static void inc_msg_scrolled(void); 26 static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish); 27 static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr); 28 static void msg_puts_printf(char_u *str, int maxlen); 29 static int do_more_prompt(int typed_char); 30 static void msg_screen_putchar(int c, int attr); 31 static void msg_moremsg(int full); 32 static int msg_check_screen(void); 33 static void redir_write(char_u *s, int maxlen); 34 #ifdef FEAT_CON_DIALOG 35 static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton); 36 static int confirm_msg_used = FALSE; // displaying confirm_msg 37 static char_u *confirm_msg = NULL; // ":confirm" message 38 static char_u *confirm_msg_tail; // tail of confirm_msg 39 static void display_confirm_msg(void); 40 #endif 41 #ifdef FEAT_JOB_CHANNEL 42 static int emsg_to_channel_log = FALSE; 43 #endif 44 45 struct msg_hist 46 { 47 struct msg_hist *next; 48 char_u *msg; 49 int attr; 50 }; 51 52 static struct msg_hist *first_msg_hist = NULL; 53 static struct msg_hist *last_msg_hist = NULL; 54 static int msg_hist_len = 0; 55 56 static FILE *verbose_fd = NULL; 57 static int verbose_did_open = FALSE; 58 59 /* 60 * When writing messages to the screen, there are many different situations. 61 * A number of variables is used to remember the current state: 62 * msg_didany TRUE when messages were written since the last time the 63 * user reacted to a prompt. 64 * Reset: After hitting a key for the hit-return prompt, 65 * hitting <CR> for the command line or input(). 66 * Set: When any message is written to the screen. 67 * msg_didout TRUE when something was written to the current line. 68 * Reset: When advancing to the next line, when the current 69 * text can be overwritten. 70 * Set: When any message is written to the screen. 71 * msg_nowait No extra delay for the last drawn message. 72 * Used in normal_cmd() before the mode message is drawn. 73 * emsg_on_display There was an error message recently. Indicates that there 74 * should be a delay before redrawing. 75 * msg_scroll The next message should not overwrite the current one. 76 * msg_scrolled How many lines the screen has been scrolled (because of 77 * messages). Used in update_screen() to scroll the screen 78 * back. Incremented each time the screen scrolls a line. 79 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr() 80 * writes something without scrolling should not make 81 * need_wait_return to be set. This is a hack to make ":ts" 82 * work without an extra prompt. 83 * lines_left Number of lines available for messages before the 84 * more-prompt is to be given. -1 when not set. 85 * need_wait_return TRUE when the hit-return prompt is needed. 86 * Reset: After giving the hit-return prompt, when the user 87 * has answered some other prompt. 88 * Set: When the ruler or typeahead display is overwritten, 89 * scrolling the screen for some message. 90 * keep_msg Message to be displayed after redrawing the screen, in 91 * main_loop(). 92 * This is an allocated string or NULL when not used. 93 */ 94 95 /* 96 * msg(s) - displays the string 's' on the status line 97 * When terminal not initialized (yet) mch_errmsg(..) is used. 98 * return TRUE if wait_return not called 99 */ 100 int 101 msg(char *s) 102 { 103 return msg_attr_keep(s, 0, FALSE); 104 } 105 106 /* 107 * Like msg() but keep it silent when 'verbosefile' is set. 108 */ 109 int 110 verb_msg(char *s) 111 { 112 int n; 113 114 verbose_enter(); 115 n = msg_attr_keep(s, 0, FALSE); 116 verbose_leave(); 117 118 return n; 119 } 120 121 int 122 msg_attr(char *s, int attr) 123 { 124 return msg_attr_keep(s, attr, FALSE); 125 } 126 127 int 128 msg_attr_keep( 129 char *s, 130 int attr, 131 int keep) // TRUE: set keep_msg if it doesn't scroll 132 { 133 static int entered = 0; 134 int retval; 135 char_u *buf = NULL; 136 137 // Skip messages not matching ":filter pattern". 138 // Don't filter when there is an error. 139 if (!emsg_on_display && message_filtered((char_u *)s)) 140 return TRUE; 141 142 #ifdef FEAT_EVAL 143 if (attr == 0) 144 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1); 145 #endif 146 147 /* 148 * It is possible that displaying a messages causes a problem (e.g., 149 * when redrawing the window), which causes another message, etc.. To 150 * break this loop, limit the recursiveness to 3 levels. 151 */ 152 if (entered >= 3) 153 return TRUE; 154 ++entered; 155 156 // Add message to history (unless it's a repeated kept message or a 157 // truncated message) 158 if ((char_u *)s != keep_msg 159 || (*s != '<' 160 && last_msg_hist != NULL 161 && last_msg_hist->msg != NULL 162 && STRCMP(s, last_msg_hist->msg))) 163 add_msg_hist((char_u *)s, -1, attr); 164 165 #ifdef FEAT_JOB_CHANNEL 166 if (emsg_to_channel_log) 167 // Write message in the channel log. 168 ch_log(NULL, "ERROR: %s", (char *)s); 169 #endif 170 171 // Truncate the message if needed. 172 msg_start(); 173 buf = msg_strtrunc((char_u *)s, FALSE); 174 if (buf != NULL) 175 s = (char *)buf; 176 177 msg_outtrans_attr((char_u *)s, attr); 178 msg_clr_eos(); 179 retval = msg_end(); 180 181 if (keep && retval && vim_strsize((char_u *)s) 182 < (int)(Rows - cmdline_row - 1) * Columns + sc_col) 183 set_keep_msg((char_u *)s, 0); 184 185 vim_free(buf); 186 --entered; 187 return retval; 188 } 189 190 /* 191 * Truncate a string such that it can be printed without causing a scroll. 192 * Returns an allocated string or NULL when no truncating is done. 193 */ 194 char_u * 195 msg_strtrunc( 196 char_u *s, 197 int force) // always truncate 198 { 199 char_u *buf = NULL; 200 int len; 201 int room; 202 203 // May truncate message to avoid a hit-return prompt 204 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL) 205 && !exmode_active && msg_silent == 0) || force) 206 { 207 len = vim_strsize(s); 208 if (msg_scrolled != 0) 209 // Use all the columns. 210 room = (int)(Rows - msg_row) * Columns - 1; 211 else 212 // Use up to 'showcmd' column. 213 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; 214 if (len > room && room > 0) 215 { 216 if (enc_utf8) 217 // may have up to 18 bytes per cell (6 per char, up to two 218 // composing chars) 219 len = (room + 2) * 18; 220 else if (enc_dbcs == DBCS_JPNU) 221 // may have up to 2 bytes per cell for euc-jp 222 len = (room + 2) * 2; 223 else 224 len = room + 2; 225 buf = alloc(len); 226 if (buf != NULL) 227 trunc_string(s, buf, room, len); 228 } 229 } 230 return buf; 231 } 232 233 /* 234 * Truncate a string "s" to "buf" with cell width "room". 235 * "s" and "buf" may be equal. 236 */ 237 void 238 trunc_string( 239 char_u *s, 240 char_u *buf, 241 int room_in, 242 int buflen) 243 { 244 size_t room = room_in - 3; // "..." takes 3 chars 245 size_t half; 246 size_t len = 0; 247 int e; 248 int i; 249 int n; 250 251 if (room_in < 3) 252 room = 0; 253 half = room / 2; 254 255 // First part: Start of the string. 256 for (e = 0; len < half && e < buflen; ++e) 257 { 258 if (s[e] == NUL) 259 { 260 // text fits without truncating! 261 buf[e] = NUL; 262 return; 263 } 264 n = ptr2cells(s + e); 265 if (len + n > half) 266 break; 267 len += n; 268 buf[e] = s[e]; 269 if (has_mbyte) 270 for (n = (*mb_ptr2len)(s + e); --n > 0; ) 271 { 272 if (++e == buflen) 273 break; 274 buf[e] = s[e]; 275 } 276 } 277 278 // Last part: End of the string. 279 i = e; 280 if (enc_dbcs != 0) 281 { 282 // For DBCS going backwards in a string is slow, but 283 // computing the cell width isn't too slow: go forward 284 // until the rest fits. 285 n = vim_strsize(s + i); 286 while (len + n > room) 287 { 288 n -= ptr2cells(s + i); 289 i += (*mb_ptr2len)(s + i); 290 } 291 } 292 else if (enc_utf8) 293 { 294 // For UTF-8 we can go backwards easily. 295 half = i = (int)STRLEN(s); 296 for (;;) 297 { 298 do 299 half = half - utf_head_off(s, s + half - 1) - 1; 300 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half))); 301 n = ptr2cells(s + half); 302 if (len + n > room || half == 0) 303 break; 304 len += n; 305 i = (int)half; 306 } 307 } 308 else 309 { 310 for (i = (int)STRLEN(s); 311 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i) 312 len += n; 313 } 314 315 316 if (i <= e + 3) 317 { 318 // text fits without truncating 319 if (s != buf) 320 { 321 len = STRLEN(s); 322 if (len >= (size_t)buflen) 323 len = buflen - 1; 324 len = len - e + 1; 325 if (len < 1) 326 buf[e - 1] = NUL; 327 else 328 mch_memmove(buf + e, s + e, len); 329 } 330 } 331 else if (e + 3 < buflen) 332 { 333 // set the middle and copy the last part 334 mch_memmove(buf + e, "...", (size_t)3); 335 len = STRLEN(s + i) + 1; 336 if (len >= (size_t)buflen - e - 3) 337 len = buflen - e - 3 - 1; 338 mch_memmove(buf + e + 3, s + i, len); 339 buf[e + 3 + len - 1] = NUL; 340 } 341 else 342 { 343 // can't fit in the "...", just truncate it 344 buf[e - 1] = NUL; 345 } 346 } 347 348 /* 349 * Automatic prototype generation does not understand this function. 350 * Note: Caller of smsg() and smsg_attr() must check the resulting string is 351 * shorter than IOSIZE!!! 352 */ 353 #ifndef PROTO 354 355 int vim_snprintf(char *str, size_t str_m, const char *fmt, ...); 356 357 int 358 smsg(const char *s, ...) 359 { 360 if (IObuff == NULL) 361 { 362 // Very early in initialisation and already something wrong, just 363 // give the raw message so the user at least gets a hint. 364 return msg((char *)s); 365 } 366 else 367 { 368 va_list arglist; 369 370 va_start(arglist, s); 371 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist); 372 va_end(arglist); 373 return msg((char *)IObuff); 374 } 375 } 376 377 int 378 smsg_attr(int attr, const char *s, ...) 379 { 380 if (IObuff == NULL) 381 { 382 // Very early in initialisation and already something wrong, just 383 // give the raw message so the user at least gets a hint. 384 return msg_attr((char *)s, attr); 385 } 386 else 387 { 388 va_list arglist; 389 390 va_start(arglist, s); 391 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist); 392 va_end(arglist); 393 return msg_attr((char *)IObuff, attr); 394 } 395 } 396 397 int 398 smsg_attr_keep(int attr, const char *s, ...) 399 { 400 if (IObuff == NULL) 401 { 402 // Very early in initialisation and already something wrong, just 403 // give the raw message so the user at least gets a hint. 404 return msg_attr_keep((char *)s, attr, TRUE); 405 } 406 else 407 { 408 va_list arglist; 409 410 va_start(arglist, s); 411 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist); 412 va_end(arglist); 413 return msg_attr_keep((char *)IObuff, attr, TRUE); 414 } 415 } 416 417 #endif 418 419 /* 420 * Remember the last sourcing name/lnum used in an error message, so that it 421 * isn't printed each time when it didn't change. 422 */ 423 static int last_sourcing_lnum = 0; 424 static char_u *last_sourcing_name = NULL; 425 426 /* 427 * Reset the last used sourcing name/lnum. Makes sure it is displayed again 428 * for the next error message; 429 */ 430 void 431 reset_last_sourcing(void) 432 { 433 VIM_CLEAR(last_sourcing_name); 434 last_sourcing_lnum = 0; 435 } 436 437 /* 438 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name". 439 */ 440 static int 441 other_sourcing_name(void) 442 { 443 if (SOURCING_NAME != NULL) 444 { 445 if (last_sourcing_name != NULL) 446 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0; 447 return TRUE; 448 } 449 return FALSE; 450 } 451 452 /* 453 * Get the message about the source, as used for an error message. 454 * Returns an allocated string with room for one more character. 455 * Returns NULL when no message is to be given. 456 */ 457 static char_u * 458 get_emsg_source(void) 459 { 460 char_u *Buf, *p; 461 462 if (SOURCING_NAME != NULL && other_sourcing_name()) 463 { 464 char_u *sname = estack_sfile(ESTACK_NONE); 465 char_u *tofree = sname; 466 467 if (sname == NULL) 468 sname = SOURCING_NAME; 469 470 #ifdef FEAT_EVAL 471 if (estack_compiling) 472 p = (char_u *)_("Error detected while compiling %s:"); 473 else 474 #endif 475 p = (char_u *)_("Error detected while processing %s:"); 476 Buf = alloc(STRLEN(sname) + STRLEN(p)); 477 if (Buf != NULL) 478 sprintf((char *)Buf, (char *)p, sname); 479 vim_free(tofree); 480 return Buf; 481 } 482 return NULL; 483 } 484 485 /* 486 * Get the message about the source lnum, as used for an error message. 487 * Returns an allocated string with room for one more character. 488 * Returns NULL when no message is to be given. 489 */ 490 static char_u * 491 get_emsg_lnum(void) 492 { 493 char_u *Buf, *p; 494 495 // lnum is 0 when executing a command from the command line 496 // argument, we don't want a line number then 497 if (SOURCING_NAME != NULL 498 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum) 499 && SOURCING_LNUM != 0) 500 { 501 p = (char_u *)_("line %4ld:"); 502 Buf = alloc(STRLEN(p) + 20); 503 if (Buf != NULL) 504 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM); 505 return Buf; 506 } 507 return NULL; 508 } 509 510 /* 511 * Display name and line number for the source of an error. 512 * Remember the file name and line number, so that for the next error the info 513 * is only displayed if it changed. 514 */ 515 void 516 msg_source(int attr) 517 { 518 char_u *p; 519 520 ++no_wait_return; 521 p = get_emsg_source(); 522 if (p != NULL) 523 { 524 msg_attr((char *)p, attr); 525 vim_free(p); 526 } 527 p = get_emsg_lnum(); 528 if (p != NULL) 529 { 530 msg_attr((char *)p, HL_ATTR(HLF_N)); 531 vim_free(p); 532 last_sourcing_lnum = SOURCING_LNUM; // only once for each line 533 } 534 535 // remember the last sourcing name printed, also when it's empty 536 if (SOURCING_NAME == NULL || other_sourcing_name()) 537 { 538 vim_free(last_sourcing_name); 539 if (SOURCING_NAME == NULL) 540 last_sourcing_name = NULL; 541 else 542 last_sourcing_name = vim_strsave(SOURCING_NAME); 543 } 544 --no_wait_return; 545 } 546 547 /* 548 * Return TRUE if not giving error messages right now: 549 * If "emsg_off" is set: no error messages at the moment. 550 * If "msg" is in 'debug': do error message but without side effects. 551 * If "emsg_skip" is set: never do error messages. 552 */ 553 static int 554 emsg_not_now(void) 555 { 556 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL 557 && vim_strchr(p_debug, 't') == NULL) 558 #ifdef FEAT_EVAL 559 || emsg_skip > 0 560 #endif 561 ) 562 return TRUE; 563 return FALSE; 564 } 565 566 #if defined(FEAT_EVAL) || defined(PROTO) 567 static garray_T ignore_error_list = GA_EMPTY; 568 569 void 570 ignore_error_for_testing(char_u *error) 571 { 572 if (ignore_error_list.ga_itemsize == 0) 573 ga_init2(&ignore_error_list, sizeof(char_u *), 1); 574 575 if (STRCMP("RESET", error) == 0) 576 ga_clear_strings(&ignore_error_list); 577 else 578 ga_add_string(&ignore_error_list, error); 579 } 580 581 static int 582 ignore_error(char_u *msg) 583 { 584 int i; 585 586 for (i = 0; i < ignore_error_list.ga_len; ++i) 587 if (strstr((char *)msg, 588 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL) 589 return TRUE; 590 return FALSE; 591 } 592 #endif 593 594 #if !defined(HAVE_STRERROR) || defined(PROTO) 595 /* 596 * Replacement for perror() that behaves more or less like emsg() was called. 597 * v:errmsg will be set and called_emsg will be incremented. 598 */ 599 void 600 do_perror(char *msg) 601 { 602 perror(msg); 603 ++emsg_silent; 604 emsg(msg); 605 --emsg_silent; 606 } 607 #endif 608 609 /* 610 * emsg_core() - display an error message 611 * 612 * Rings the bell, if appropriate, and calls message() to do the real work 613 * When terminal not initialized (yet) mch_errmsg(..) is used. 614 * 615 * Return TRUE if wait_return not called. 616 * Note: caller must check 'emsg_not_now()' before calling this. 617 */ 618 static int 619 emsg_core(char_u *s) 620 { 621 int attr; 622 char_u *p; 623 int r; 624 #ifdef FEAT_EVAL 625 int ignore = FALSE; 626 int severe; 627 #endif 628 629 #ifdef FEAT_EVAL 630 // When testing some errors are turned into a normal message. 631 if (ignore_error(s)) 632 // don't call msg() if it results in a dialog 633 return msg_use_printf() ? FALSE : msg((char *)s); 634 #endif 635 636 ++called_emsg; 637 638 #ifdef FEAT_EVAL 639 // If "emsg_severe" is TRUE: When an error exception is to be thrown, 640 // prefer this message over previous messages for the same command. 641 severe = emsg_severe; 642 emsg_severe = FALSE; 643 #endif 644 645 if (!emsg_off || vim_strchr(p_debug, 't') != NULL) 646 { 647 #ifdef FEAT_EVAL 648 /* 649 * Cause a throw of an error exception if appropriate. Don't display 650 * the error message in this case. (If no matching catch clause will 651 * be found, the message will be displayed later on.) "ignore" is set 652 * when the message should be ignored completely (used for the 653 * interrupt message). 654 */ 655 if (cause_errthrow(s, severe, &ignore) == TRUE) 656 { 657 if (!ignore) 658 ++did_emsg; 659 return TRUE; 660 } 661 662 if (in_assert_fails && emsg_assert_fails_msg == NULL) 663 { 664 emsg_assert_fails_msg = vim_strsave(s); 665 emsg_assert_fails_lnum = SOURCING_LNUM; 666 vim_free(emsg_assert_fails_context); 667 emsg_assert_fails_context = vim_strsave( 668 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME); 669 } 670 671 // set "v:errmsg", also when using ":silent! cmd" 672 set_vim_var_string(VV_ERRMSG, s, -1); 673 #endif 674 675 /* 676 * When using ":silent! cmd" ignore error messages. 677 * But do write it to the redirection file. 678 */ 679 if (emsg_silent != 0) 680 { 681 if (emsg_noredir == 0) 682 { 683 msg_start(); 684 p = get_emsg_source(); 685 if (p != NULL) 686 { 687 STRCAT(p, "\n"); 688 redir_write(p, -1); 689 vim_free(p); 690 } 691 p = get_emsg_lnum(); 692 if (p != NULL) 693 { 694 STRCAT(p, "\n"); 695 redir_write(p, -1); 696 vim_free(p); 697 } 698 redir_write(s, -1); 699 } 700 #ifdef FEAT_EVAL 701 // Only increment did_emsg_def when :silent! wasn't used inside the 702 // :def function. 703 if (emsg_silent == emsg_silent_def) 704 ++did_emsg_def; 705 #endif 706 #ifdef FEAT_JOB_CHANNEL 707 ch_log(NULL, "ERROR silent: %s", (char *)s); 708 #endif 709 return TRUE; 710 } 711 712 ex_exitval = 1; 713 714 // Reset msg_silent, an error causes messages to be switched back on. 715 msg_silent = 0; 716 cmd_silent = FALSE; 717 718 if (global_busy) // break :global command 719 ++global_busy; 720 721 if (p_eb) 722 beep_flush(); // also includes flush_buffers() 723 else 724 flush_buffers(FLUSH_MINIMAL); // flush internal buffers 725 ++did_emsg; // flag for DoOneCmd() 726 #ifdef FEAT_EVAL 727 did_uncaught_emsg = TRUE; 728 #endif 729 } 730 731 emsg_on_display = TRUE; // remember there is an error message 732 ++msg_scroll; // don't overwrite a previous message 733 attr = HL_ATTR(HLF_E); // set highlight mode for error messages 734 if (msg_scrolled != 0) 735 need_wait_return = TRUE; // needed in case emsg() is called after 736 // wait_return has reset need_wait_return 737 // and a redraw is expected because 738 // msg_scrolled is non-zero 739 740 #ifdef FEAT_JOB_CHANNEL 741 emsg_to_channel_log = TRUE; 742 #endif 743 /* 744 * Display name and line number for the source of the error. 745 */ 746 msg_source(attr); 747 748 /* 749 * Display the error message itself. 750 */ 751 msg_nowait = FALSE; // wait for this msg 752 r = msg_attr((char *)s, attr); 753 754 #ifdef FEAT_JOB_CHANNEL 755 emsg_to_channel_log = FALSE; 756 #endif 757 return r; 758 } 759 760 /* 761 * Print an error message. 762 */ 763 int 764 emsg(char *s) 765 { 766 // Skip this if not giving error messages at the moment. 767 if (!emsg_not_now()) 768 return emsg_core((char_u *)s); 769 return TRUE; // no error messages at the moment 770 } 771 772 #ifndef PROTO // manual proto with __attribute__ 773 /* 774 * Print an error message with format string and variable arguments. 775 * Note: caller must not pass 'IObuff' as 1st argument. 776 */ 777 int 778 semsg(const char *s, ...) 779 { 780 // Skip this if not giving error messages at the moment. 781 if (!emsg_not_now()) 782 { 783 if (IObuff == NULL) 784 { 785 // Very early in initialisation and already something wrong, just 786 // give the raw message so the user at least gets a hint. 787 return emsg_core((char_u *)s); 788 } 789 else 790 { 791 va_list ap; 792 793 va_start(ap, s); 794 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap); 795 va_end(ap); 796 return emsg_core(IObuff); 797 } 798 } 799 return TRUE; // no error messages at the moment 800 } 801 #endif 802 803 /* 804 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is 805 * defined. It is used for internal errors only, so that they can be 806 * detected when fuzzing vim. 807 */ 808 void 809 iemsg(char *s) 810 { 811 if (!emsg_not_now()) 812 emsg_core((char_u *)s); 813 #ifdef ABORT_ON_INTERNAL_ERROR 814 abort(); 815 #endif 816 } 817 818 #ifndef PROTO // manual proto with __attribute__ 819 /* 820 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is 821 * defined. It is used for internal errors only, so that they can be 822 * detected when fuzzing vim. 823 * Note: caller must not pass 'IObuff' as 1st argument. 824 */ 825 void 826 siemsg(const char *s, ...) 827 { 828 if (!emsg_not_now()) 829 { 830 if (IObuff == NULL) 831 { 832 // Very early in initialisation and already something wrong, just 833 // give the raw message so the user at least gets a hint. 834 emsg_core((char_u *)s); 835 } 836 else 837 { 838 va_list ap; 839 840 va_start(ap, s); 841 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap); 842 va_end(ap); 843 emsg_core(IObuff); 844 } 845 } 846 # ifdef ABORT_ON_INTERNAL_ERROR 847 abort(); 848 # endif 849 } 850 #endif 851 852 /* 853 * Give an "Internal error" message. 854 */ 855 void 856 internal_error(char *where) 857 { 858 siemsg(_(e_intern2), where); 859 } 860 861 /* 862 * Like internal_error() but do not call abort(), to avoid tests using 863 * test_unknown() and test_void() causing Vim to exit. 864 */ 865 void 866 internal_error_no_abort(char *where) 867 { 868 semsg(_(e_intern2), where); 869 } 870 871 // emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. 872 873 void 874 emsg_invreg(int name) 875 { 876 semsg(_("E354: Invalid register name: '%s'"), transchar(name)); 877 } 878 879 /* 880 * Give an error message which contains %s for "name[len]". 881 */ 882 void 883 emsg_namelen(char *msg, char_u *name, int len) 884 { 885 char_u *copy = vim_strnsave((char_u *)name, len); 886 887 semsg(msg, copy == NULL ? "NULL" : (char *)copy); 888 vim_free(copy); 889 } 890 891 /* 892 * Like msg(), but truncate to a single line if p_shm contains 't', or when 893 * "force" is TRUE. This truncates in another way as for normal messages. 894 * Careful: The string may be changed by msg_may_trunc()! 895 * Returns a pointer to the printed message, if wait_return() not called. 896 */ 897 char * 898 msg_trunc_attr(char *s, int force, int attr) 899 { 900 int n; 901 char *ts; 902 903 // Add message to history before truncating 904 add_msg_hist((char_u *)s, -1, attr); 905 906 ts = (char *)msg_may_trunc(force, (char_u *)s); 907 908 msg_hist_off = TRUE; 909 n = msg_attr(ts, attr); 910 msg_hist_off = FALSE; 911 912 if (n) 913 return ts; 914 return NULL; 915 } 916 917 /* 918 * Check if message "s" should be truncated at the start (for filenames). 919 * Return a pointer to where the truncated message starts. 920 * Note: May change the message by replacing a character with '<'. 921 */ 922 char_u * 923 msg_may_trunc(int force, char_u *s) 924 { 925 int n; 926 int room; 927 928 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1; 929 if ((force || (shortmess(SHM_TRUNC) && !exmode_active)) 930 && (n = (int)STRLEN(s) - room) > 0) 931 { 932 if (has_mbyte) 933 { 934 int size = vim_strsize(s); 935 936 // There may be room anyway when there are multibyte chars. 937 if (size <= room) 938 return s; 939 940 for (n = 0; size >= room; ) 941 { 942 size -= (*mb_ptr2cells)(s + n); 943 n += (*mb_ptr2len)(s + n); 944 } 945 --n; 946 } 947 s += n; 948 *s = '<'; 949 } 950 return s; 951 } 952 953 static void 954 add_msg_hist( 955 char_u *s, 956 int len, // -1 for undetermined length 957 int attr) 958 { 959 struct msg_hist *p; 960 961 if (msg_hist_off || msg_silent != 0) 962 return; 963 964 // Don't let the message history get too big 965 while (msg_hist_len > MAX_MSG_HIST_LEN) 966 (void)delete_first_msg(); 967 968 // allocate an entry and add the message at the end of the history 969 p = ALLOC_ONE(struct msg_hist); 970 if (p != NULL) 971 { 972 if (len < 0) 973 len = (int)STRLEN(s); 974 // remove leading and trailing newlines 975 while (len > 0 && *s == '\n') 976 { 977 ++s; 978 --len; 979 } 980 while (len > 0 && s[len - 1] == '\n') 981 --len; 982 p->msg = vim_strnsave(s, len); 983 p->next = NULL; 984 p->attr = attr; 985 if (last_msg_hist != NULL) 986 last_msg_hist->next = p; 987 last_msg_hist = p; 988 if (first_msg_hist == NULL) 989 first_msg_hist = last_msg_hist; 990 ++msg_hist_len; 991 } 992 } 993 994 /* 995 * Delete the first (oldest) message from the history. 996 * Returns FAIL if there are no messages. 997 */ 998 int 999 delete_first_msg(void) 1000 { 1001 struct msg_hist *p; 1002 1003 if (msg_hist_len <= 0) 1004 return FAIL; 1005 p = first_msg_hist; 1006 first_msg_hist = p->next; 1007 if (first_msg_hist == NULL) 1008 last_msg_hist = NULL; // history is empty 1009 vim_free(p->msg); 1010 vim_free(p); 1011 --msg_hist_len; 1012 return OK; 1013 } 1014 1015 /* 1016 * ":messages" command. 1017 */ 1018 void 1019 ex_messages(exarg_T *eap) 1020 { 1021 struct msg_hist *p; 1022 char_u *s; 1023 int c = 0; 1024 1025 if (STRCMP(eap->arg, "clear") == 0) 1026 { 1027 int keep = eap->addr_count == 0 ? 0 : eap->line2; 1028 1029 while (msg_hist_len > keep) 1030 (void)delete_first_msg(); 1031 return; 1032 } 1033 1034 if (*eap->arg != NUL) 1035 { 1036 emsg(_(e_invarg)); 1037 return; 1038 } 1039 1040 msg_hist_off = TRUE; 1041 1042 p = first_msg_hist; 1043 if (eap->addr_count != 0) 1044 { 1045 // Count total messages 1046 for (; p != NULL && !got_int; p = p->next) 1047 c++; 1048 1049 c -= eap->line2; 1050 1051 // Skip without number of messages specified 1052 for (p = first_msg_hist; p != NULL && !got_int && c > 0; 1053 p = p->next, c--); 1054 } 1055 1056 if (p == first_msg_hist) 1057 { 1058 #ifdef FEAT_MULTI_LANG 1059 s = get_mess_lang(); 1060 #else 1061 s = mch_getenv((char_u *)"LANG"); 1062 #endif 1063 if (s != NULL && *s != NUL) 1064 // The next comment is extracted by xgettext and put in po file for 1065 // translators to read. 1066 msg_attr( 1067 // Translator: Please replace the name and email address 1068 // with the appropriate text for your translation. 1069 _("Messages maintainer: Bram Moolenaar <[email protected]>"), 1070 HL_ATTR(HLF_T)); 1071 } 1072 1073 // Display what was not skipped. 1074 for (; p != NULL && !got_int; p = p->next) 1075 if (p->msg != NULL) 1076 msg_attr((char *)p->msg, p->attr); 1077 1078 msg_hist_off = FALSE; 1079 } 1080 1081 #if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO) 1082 /* 1083 * Call this after prompting the user. This will avoid a hit-return message 1084 * and a delay. 1085 */ 1086 void 1087 msg_end_prompt(void) 1088 { 1089 need_wait_return = FALSE; 1090 emsg_on_display = FALSE; 1091 cmdline_row = msg_row; 1092 msg_col = 0; 1093 msg_clr_eos(); 1094 lines_left = -1; 1095 } 1096 #endif 1097 1098 /* 1099 * Wait for the user to hit a key (normally Enter). 1100 * If "redraw" is TRUE, clear and redraw the screen. 1101 * If "redraw" is FALSE, just redraw the screen. 1102 * If "redraw" is -1, don't redraw at all. 1103 */ 1104 void 1105 wait_return(int redraw) 1106 { 1107 int c; 1108 int oldState; 1109 int tmpState; 1110 int had_got_int; 1111 int save_reg_recording; 1112 FILE *save_scriptout; 1113 1114 if (redraw == TRUE) 1115 must_redraw = CLEAR; 1116 1117 // If using ":silent cmd", don't wait for a return. Also don't set 1118 // need_wait_return to do it later. 1119 if (msg_silent != 0) 1120 return; 1121 1122 /* 1123 * When inside vgetc(), we can't wait for a typed character at all. 1124 * With the global command (and some others) we only need one return at 1125 * the end. Adjust cmdline_row to avoid the next message overwriting the 1126 * last one. 1127 */ 1128 if (vgetc_busy > 0) 1129 return; 1130 need_wait_return = TRUE; 1131 if (no_wait_return) 1132 { 1133 if (!exmode_active) 1134 cmdline_row = msg_row; 1135 return; 1136 } 1137 1138 redir_off = TRUE; // don't redirect this message 1139 oldState = State; 1140 if (quit_more) 1141 { 1142 c = CAR; // just pretend CR was hit 1143 quit_more = FALSE; 1144 got_int = FALSE; 1145 } 1146 else if (exmode_active) 1147 { 1148 msg_puts(" "); // make sure the cursor is on the right line 1149 c = CAR; // no need for a return in ex mode 1150 got_int = FALSE; 1151 } 1152 else 1153 { 1154 // Make sure the hit-return prompt is on screen when 'guioptions' was 1155 // just changed. 1156 screenalloc(FALSE); 1157 1158 State = HITRETURN; 1159 setmouse(); 1160 #ifdef USE_ON_FLY_SCROLL 1161 dont_scroll = TRUE; // disallow scrolling here 1162 #endif 1163 cmdline_row = msg_row; 1164 1165 // Avoid the sequence that the user types ":" at the hit-return prompt 1166 // to start an Ex command, but the file-changed dialog gets in the 1167 // way. 1168 if (need_check_timestamps) 1169 check_timestamps(FALSE); 1170 1171 hit_return_msg(); 1172 1173 do 1174 { 1175 // Remember "got_int", if it is set vgetc() probably returns a 1176 // CTRL-C, but we need to loop then. 1177 had_got_int = got_int; 1178 1179 // Don't do mappings here, we put the character back in the 1180 // typeahead buffer. 1181 ++no_mapping; 1182 ++allow_keys; 1183 1184 // Temporarily disable Recording. If Recording is active, the 1185 // character will be recorded later, since it will be added to the 1186 // typebuf after the loop 1187 save_reg_recording = reg_recording; 1188 save_scriptout = scriptout; 1189 reg_recording = 0; 1190 scriptout = NULL; 1191 c = safe_vgetc(); 1192 if (had_got_int && !global_busy) 1193 got_int = FALSE; 1194 --no_mapping; 1195 --allow_keys; 1196 reg_recording = save_reg_recording; 1197 scriptout = save_scriptout; 1198 1199 #ifdef FEAT_CLIPBOARD 1200 // Strange way to allow copying (yanking) a modeless selection at 1201 // the hit-enter prompt. Use CTRL-Y, because the same is used in 1202 // Cmdline-mode and it's harmless when there is no selection. 1203 if (c == Ctrl_Y && clip_star.state == SELECT_DONE) 1204 { 1205 clip_copy_modeless_selection(TRUE); 1206 c = K_IGNORE; 1207 } 1208 #endif 1209 1210 /* 1211 * Allow scrolling back in the messages. 1212 * Also accept scroll-down commands when messages fill the screen, 1213 * to avoid that typing one 'j' too many makes the messages 1214 * disappear. 1215 */ 1216 if (p_more && !p_cp) 1217 { 1218 if (c == 'b' || c == 'k' || c == 'u' || c == 'g' 1219 || c == K_UP || c == K_PAGEUP) 1220 { 1221 if (msg_scrolled > Rows) 1222 // scroll back to show older messages 1223 do_more_prompt(c); 1224 else 1225 { 1226 msg_didout = FALSE; 1227 c = K_IGNORE; 1228 msg_col = 1229 #ifdef FEAT_RIGHTLEFT 1230 cmdmsg_rl ? Columns - 1 : 1231 #endif 1232 0; 1233 } 1234 if (quit_more) 1235 { 1236 c = CAR; // just pretend CR was hit 1237 quit_more = FALSE; 1238 got_int = FALSE; 1239 } 1240 else if (c != K_IGNORE) 1241 { 1242 c = K_IGNORE; 1243 hit_return_msg(); 1244 } 1245 } 1246 else if (msg_scrolled > Rows - 2 1247 && (c == 'j' || c == 'd' || c == 'f' 1248 || c == K_DOWN || c == K_PAGEDOWN)) 1249 c = K_IGNORE; 1250 } 1251 } while ((had_got_int && c == Ctrl_C) 1252 || c == K_IGNORE 1253 #ifdef FEAT_GUI 1254 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR 1255 #endif 1256 || c == K_LEFTDRAG || c == K_LEFTRELEASE 1257 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE 1258 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE 1259 || c == K_MOUSELEFT || c == K_MOUSERIGHT 1260 || c == K_MOUSEDOWN || c == K_MOUSEUP 1261 || c == K_MOUSEMOVE 1262 || (!mouse_has(MOUSE_RETURN) 1263 && mouse_row < msg_row 1264 && (c == K_LEFTMOUSE 1265 || c == K_MIDDLEMOUSE 1266 || c == K_RIGHTMOUSE 1267 || c == K_X1MOUSE 1268 || c == K_X2MOUSE)) 1269 ); 1270 ui_breakcheck(); 1271 /* 1272 * Avoid that the mouse-up event causes visual mode to start. 1273 */ 1274 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE 1275 || c == K_X1MOUSE || c == K_X2MOUSE) 1276 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0); 1277 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C) 1278 { 1279 // Put the character back in the typeahead buffer. Don't use the 1280 // stuff buffer, because lmaps wouldn't work. 1281 ins_char_typebuf(vgetc_char, vgetc_mod_mask); 1282 do_redraw = TRUE; // need a redraw even though there is 1283 // typeahead 1284 } 1285 } 1286 redir_off = FALSE; 1287 1288 /* 1289 * If the user hits ':', '?' or '/' we get a command line from the next 1290 * line. 1291 */ 1292 if (c == ':' || c == '?' || c == '/') 1293 { 1294 if (!exmode_active) 1295 cmdline_row = msg_row; 1296 skip_redraw = TRUE; // skip redraw once 1297 do_redraw = FALSE; 1298 #ifdef FEAT_TERMINAL 1299 skip_term_loop = TRUE; 1300 #endif 1301 } 1302 1303 /* 1304 * If the window size changed set_shellsize() will redraw the screen. 1305 * Otherwise the screen is only redrawn if 'redraw' is set and no ':' 1306 * typed. 1307 */ 1308 tmpState = State; 1309 State = oldState; // restore State before set_shellsize 1310 setmouse(); 1311 msg_check(); 1312 1313 #if defined(UNIX) || defined(VMS) 1314 /* 1315 * When switching screens, we need to output an extra newline on exit. 1316 */ 1317 if (swapping_screen() && !termcap_active) 1318 newline_on_exit = TRUE; 1319 #endif 1320 1321 need_wait_return = FALSE; 1322 did_wait_return = TRUE; 1323 emsg_on_display = FALSE; // can delete error message now 1324 lines_left = -1; // reset lines_left at next msg_start() 1325 reset_last_sourcing(); 1326 if (keep_msg != NULL && vim_strsize(keep_msg) >= 1327 (Rows - cmdline_row - 1) * Columns + sc_col) 1328 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long 1329 1330 if (tmpState == SETWSIZE) // got resize event while in vgetc() 1331 { 1332 starttermcap(); // start termcap before redrawing 1333 shell_resized(); 1334 } 1335 else if (!skip_redraw 1336 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1))) 1337 { 1338 starttermcap(); // start termcap before redrawing 1339 redraw_later(VALID); 1340 } 1341 } 1342 1343 /* 1344 * Write the hit-return prompt. 1345 */ 1346 static void 1347 hit_return_msg(void) 1348 { 1349 int save_p_more = p_more; 1350 1351 p_more = FALSE; // don't want see this message when scrolling back 1352 if (msg_didout) // start on a new line 1353 msg_putchar('\n'); 1354 if (got_int) 1355 msg_puts(_("Interrupt: ")); 1356 1357 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R)); 1358 if (!msg_use_printf()) 1359 msg_clr_eos(); 1360 p_more = save_p_more; 1361 } 1362 1363 /* 1364 * Set "keep_msg" to "s". Free the old value and check for NULL pointer. 1365 */ 1366 void 1367 set_keep_msg(char_u *s, int attr) 1368 { 1369 vim_free(keep_msg); 1370 if (s != NULL && msg_silent == 0) 1371 keep_msg = vim_strsave(s); 1372 else 1373 keep_msg = NULL; 1374 keep_msg_more = FALSE; 1375 keep_msg_attr = attr; 1376 } 1377 1378 #if defined(FEAT_TERMRESPONSE) || defined(PROTO) 1379 /* 1380 * If there currently is a message being displayed, set "keep_msg" to it, so 1381 * that it will be displayed again after redraw. 1382 */ 1383 void 1384 set_keep_msg_from_hist(void) 1385 { 1386 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0 1387 && (State & NORMAL)) 1388 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr); 1389 } 1390 #endif 1391 1392 /* 1393 * Prepare for outputting characters in the command line. 1394 */ 1395 void 1396 msg_start(void) 1397 { 1398 int did_return = FALSE; 1399 1400 if (!msg_silent) 1401 VIM_CLEAR(keep_msg); 1402 1403 #ifdef FEAT_EVAL 1404 if (need_clr_eos) 1405 { 1406 // Halfway an ":echo" command and getting an (error) message: clear 1407 // any text from the command. 1408 need_clr_eos = FALSE; 1409 msg_clr_eos(); 1410 } 1411 #endif 1412 1413 if (!msg_scroll && full_screen) // overwrite last message 1414 { 1415 msg_row = cmdline_row; 1416 msg_col = 1417 #ifdef FEAT_RIGHTLEFT 1418 cmdmsg_rl ? Columns - 1 : 1419 #endif 1420 0; 1421 } 1422 else if (msg_didout) // start message on next line 1423 { 1424 msg_putchar('\n'); 1425 did_return = TRUE; 1426 if (exmode_active != EXMODE_NORMAL) 1427 cmdline_row = msg_row; 1428 } 1429 if (!msg_didany || lines_left < 0) 1430 msg_starthere(); 1431 if (msg_silent == 0) 1432 { 1433 msg_didout = FALSE; // no output on current line yet 1434 cursor_off(); 1435 } 1436 1437 // when redirecting, may need to start a new line. 1438 if (!did_return) 1439 redir_write((char_u *)"\n", -1); 1440 } 1441 1442 /* 1443 * Note that the current msg position is where messages start. 1444 */ 1445 void 1446 msg_starthere(void) 1447 { 1448 lines_left = cmdline_row; 1449 msg_didany = FALSE; 1450 } 1451 1452 void 1453 msg_putchar(int c) 1454 { 1455 msg_putchar_attr(c, 0); 1456 } 1457 1458 void 1459 msg_putchar_attr(int c, int attr) 1460 { 1461 char_u buf[MB_MAXBYTES + 1]; 1462 1463 if (IS_SPECIAL(c)) 1464 { 1465 buf[0] = K_SPECIAL; 1466 buf[1] = K_SECOND(c); 1467 buf[2] = K_THIRD(c); 1468 buf[3] = NUL; 1469 } 1470 else 1471 buf[(*mb_char2bytes)(c, buf)] = NUL; 1472 msg_puts_attr((char *)buf, attr); 1473 } 1474 1475 void 1476 msg_outnum(long n) 1477 { 1478 char buf[20]; 1479 1480 sprintf(buf, "%ld", n); 1481 msg_puts(buf); 1482 } 1483 1484 void 1485 msg_home_replace(char_u *fname) 1486 { 1487 msg_home_replace_attr(fname, 0); 1488 } 1489 1490 #if defined(FEAT_FIND_ID) || defined(PROTO) 1491 void 1492 msg_home_replace_hl(char_u *fname) 1493 { 1494 msg_home_replace_attr(fname, HL_ATTR(HLF_D)); 1495 } 1496 #endif 1497 1498 static void 1499 msg_home_replace_attr(char_u *fname, int attr) 1500 { 1501 char_u *name; 1502 1503 name = home_replace_save(NULL, fname); 1504 if (name != NULL) 1505 msg_outtrans_attr(name, attr); 1506 vim_free(name); 1507 } 1508 1509 /* 1510 * Output 'len' characters in 'str' (including NULs) with translation 1511 * if 'len' is -1, output up to a NUL character. 1512 * Use attributes 'attr'. 1513 * Return the number of characters it takes on the screen. 1514 */ 1515 int 1516 msg_outtrans(char_u *str) 1517 { 1518 return msg_outtrans_attr(str, 0); 1519 } 1520 1521 int 1522 msg_outtrans_attr(char_u *str, int attr) 1523 { 1524 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr); 1525 } 1526 1527 int 1528 msg_outtrans_len(char_u *str, int len) 1529 { 1530 return msg_outtrans_len_attr(str, len, 0); 1531 } 1532 1533 /* 1534 * Output one character at "p". Return pointer to the next character. 1535 * Handles multi-byte characters. 1536 */ 1537 char_u * 1538 msg_outtrans_one(char_u *p, int attr) 1539 { 1540 int l; 1541 1542 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) 1543 { 1544 msg_outtrans_len_attr(p, l, attr); 1545 return p + l; 1546 } 1547 msg_puts_attr((char *)transchar_byte(*p), attr); 1548 return p + 1; 1549 } 1550 1551 int 1552 msg_outtrans_len_attr(char_u *msgstr, int len, int attr) 1553 { 1554 int retval = 0; 1555 char_u *str = msgstr; 1556 char_u *plain_start = msgstr; 1557 char_u *s; 1558 int mb_l; 1559 int c; 1560 int save_got_int = got_int; 1561 1562 // Only quit when got_int was set in here. 1563 got_int = FALSE; 1564 1565 // if MSG_HIST flag set, add message to history 1566 if (attr & MSG_HIST) 1567 { 1568 add_msg_hist(str, len, attr); 1569 attr &= ~MSG_HIST; 1570 } 1571 1572 // If the string starts with a composing character first draw a space on 1573 // which the composing char can be drawn. 1574 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr))) 1575 msg_puts_attr(" ", attr); 1576 1577 /* 1578 * Go over the string. Special characters are translated and printed. 1579 * Normal characters are printed several at a time. 1580 */ 1581 while (--len >= 0 && !got_int) 1582 { 1583 if (enc_utf8) 1584 // Don't include composing chars after the end. 1585 mb_l = utfc_ptr2len_len(str, len + 1); 1586 else if (has_mbyte) 1587 mb_l = (*mb_ptr2len)(str); 1588 else 1589 mb_l = 1; 1590 if (has_mbyte && mb_l > 1) 1591 { 1592 c = (*mb_ptr2char)(str); 1593 if (vim_isprintc(c)) 1594 // printable multi-byte char: count the cells. 1595 retval += (*mb_ptr2cells)(str); 1596 else 1597 { 1598 // unprintable multi-byte char: print the printable chars so 1599 // far and the translation of the unprintable char. 1600 if (str > plain_start) 1601 msg_puts_attr_len((char *)plain_start, 1602 (int)(str - plain_start), attr); 1603 plain_start = str + mb_l; 1604 msg_puts_attr((char *)transchar(c), 1605 attr == 0 ? HL_ATTR(HLF_8) : attr); 1606 retval += char2cells(c); 1607 } 1608 len -= mb_l - 1; 1609 str += mb_l; 1610 } 1611 else 1612 { 1613 s = transchar_byte(*str); 1614 if (s[1] != NUL) 1615 { 1616 // unprintable char: print the printable chars so far and the 1617 // translation of the unprintable char. 1618 if (str > plain_start) 1619 msg_puts_attr_len((char *)plain_start, 1620 (int)(str - plain_start), attr); 1621 plain_start = str + 1; 1622 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr); 1623 retval += (int)STRLEN(s); 1624 } 1625 else 1626 ++retval; 1627 ++str; 1628 } 1629 } 1630 1631 if (str > plain_start && !got_int) 1632 // print the printable chars at the end 1633 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr); 1634 1635 got_int |= save_got_int; 1636 1637 return retval; 1638 } 1639 1640 #if defined(FEAT_QUICKFIX) || defined(PROTO) 1641 void 1642 msg_make(char_u *arg) 1643 { 1644 int i; 1645 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB"; 1646 1647 arg = skipwhite(arg); 1648 for (i = 5; *arg && i >= 0; --i) 1649 if (*arg++ != str[i]) 1650 break; 1651 if (i < 0) 1652 { 1653 msg_putchar('\n'); 1654 for (i = 0; rs[i]; ++i) 1655 msg_putchar(rs[i] - 3); 1656 } 1657 } 1658 #endif 1659 1660 /* 1661 * Output the string 'str' up to a NUL character. 1662 * Return the number of characters it takes on the screen. 1663 * 1664 * If K_SPECIAL is encountered, then it is taken in conjunction with the 1665 * following character and shown as <F1>, <S-Up> etc. Any other character 1666 * which is not printable shown in <> form. 1667 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>. 1668 * If a character is displayed in one of these special ways, is also 1669 * highlighted (its highlight name is '8' in the p_hl variable). 1670 * Otherwise characters are not highlighted. 1671 * This function is used to show mappings, where we want to see how to type 1672 * the character/string -- webb 1673 */ 1674 int 1675 msg_outtrans_special( 1676 char_u *strstart, 1677 int from, // TRUE for lhs of a mapping 1678 int maxlen) // screen columns, 0 for unlimited 1679 { 1680 char_u *str = strstart; 1681 int retval = 0; 1682 char *text; 1683 int attr; 1684 int len; 1685 1686 attr = HL_ATTR(HLF_8); 1687 while (*str != NUL) 1688 { 1689 // Leading and trailing spaces need to be displayed in <> form. 1690 if ((str == strstart || str[1] == NUL) && *str == ' ') 1691 { 1692 text = "<Space>"; 1693 ++str; 1694 } 1695 else 1696 text = (char *)str2special(&str, from); 1697 len = vim_strsize((char_u *)text); 1698 if (maxlen > 0 && retval + len >= maxlen) 1699 break; 1700 // Highlight special keys 1701 msg_puts_attr(text, len > 1 1702 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0); 1703 retval += len; 1704 } 1705 return retval; 1706 } 1707 1708 #if defined(FEAT_EVAL) || defined(PROTO) 1709 /* 1710 * Return the lhs or rhs of a mapping, with the key codes turned into printable 1711 * strings, in an allocated string. 1712 */ 1713 char_u * 1714 str2special_save( 1715 char_u *str, 1716 int is_lhs) // TRUE for lhs, FALSE for rhs 1717 { 1718 garray_T ga; 1719 char_u *p = str; 1720 1721 ga_init2(&ga, 1, 40); 1722 while (*p != NUL) 1723 ga_concat(&ga, str2special(&p, is_lhs)); 1724 ga_append(&ga, NUL); 1725 return (char_u *)ga.ga_data; 1726 } 1727 #endif 1728 1729 /* 1730 * Return the printable string for the key codes at "*sp". 1731 * Used for translating the lhs or rhs of a mapping to printable chars. 1732 * Advances "sp" to the next code. 1733 */ 1734 char_u * 1735 str2special( 1736 char_u **sp, 1737 int from) // TRUE for lhs of mapping 1738 { 1739 int c; 1740 static char_u buf[7]; 1741 char_u *str = *sp; 1742 int modifiers = 0; 1743 int special = FALSE; 1744 1745 if (has_mbyte) 1746 { 1747 char_u *p; 1748 1749 // Try to un-escape a multi-byte character. Return the un-escaped 1750 // string if it is a multi-byte character. 1751 p = mb_unescape(sp); 1752 if (p != NULL) 1753 return p; 1754 } 1755 1756 c = *str; 1757 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL) 1758 { 1759 if (str[1] == KS_MODIFIER) 1760 { 1761 modifiers = str[2]; 1762 str += 3; 1763 c = *str; 1764 } 1765 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL) 1766 { 1767 c = TO_SPECIAL(str[1], str[2]); 1768 str += 2; 1769 } 1770 if (IS_SPECIAL(c) || modifiers) // special key 1771 special = TRUE; 1772 } 1773 1774 if (has_mbyte && !IS_SPECIAL(c)) 1775 { 1776 int len = (*mb_ptr2len)(str); 1777 1778 // For multi-byte characters check for an illegal byte. 1779 if (has_mbyte && MB_BYTE2LEN(*str) > len) 1780 { 1781 transchar_nonprint(curbuf, buf, c); 1782 *sp = str + 1; 1783 return buf; 1784 } 1785 // Since 'special' is TRUE the multi-byte character 'c' will be 1786 // processed by get_special_key_name() 1787 c = (*mb_ptr2char)(str); 1788 *sp = str + len; 1789 } 1790 else 1791 *sp = str + 1; 1792 1793 // Make unprintable characters in <> form, also <M-Space> and <Tab>. 1794 // Use <Space> only for lhs of a mapping. 1795 if (special || char2cells(c) > 1 || (from && c == ' ')) 1796 return get_special_key_name(c, modifiers); 1797 buf[0] = c; 1798 buf[1] = NUL; 1799 return buf; 1800 } 1801 1802 /* 1803 * Translate a key sequence into special key names. 1804 */ 1805 void 1806 str2specialbuf(char_u *sp, char_u *buf, int len) 1807 { 1808 char_u *s; 1809 1810 *buf = NUL; 1811 while (*sp) 1812 { 1813 s = str2special(&sp, FALSE); 1814 if ((int)(STRLEN(s) + STRLEN(buf)) < len) 1815 STRCAT(buf, s); 1816 } 1817 } 1818 1819 /* 1820 * print line for :print or :list command 1821 */ 1822 void 1823 msg_prt_line(char_u *s, int list) 1824 { 1825 int c; 1826 int col = 0; 1827 int n_extra = 0; 1828 int c_extra = 0; 1829 int c_final = 0; 1830 char_u *p_extra = NULL; // init to make SASC shut up 1831 int n; 1832 int attr = 0; 1833 char_u *trail = NULL; 1834 int l; 1835 char_u buf[MB_MAXBYTES + 1]; 1836 1837 if (curwin->w_p_list) 1838 list = TRUE; 1839 1840 // find start of trailing whitespace 1841 if (list && lcs_trail) 1842 { 1843 trail = s + STRLEN(s); 1844 while (trail > s && VIM_ISWHITE(trail[-1])) 1845 --trail; 1846 } 1847 1848 // output a space for an empty line, otherwise the line will be 1849 // overwritten 1850 if (*s == NUL && !(list && lcs_eol != NUL)) 1851 msg_putchar(' '); 1852 1853 while (!got_int) 1854 { 1855 if (n_extra > 0) 1856 { 1857 --n_extra; 1858 if (n_extra == 0 && c_final) 1859 c = c_final; 1860 else if (c_extra) 1861 c = c_extra; 1862 else 1863 c = *p_extra++; 1864 } 1865 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1) 1866 { 1867 col += (*mb_ptr2cells)(s); 1868 if (l >= MB_MAXBYTES) 1869 { 1870 STRCPY(buf, "?"); 1871 } 1872 else if (lcs_nbsp != NUL && list 1873 && (mb_ptr2char(s) == 160 1874 || mb_ptr2char(s) == 0x202f)) 1875 { 1876 mb_char2bytes(lcs_nbsp, buf); 1877 buf[(*mb_ptr2len)(buf)] = NUL; 1878 } 1879 else 1880 { 1881 mch_memmove(buf, s, (size_t)l); 1882 buf[l] = NUL; 1883 } 1884 msg_puts((char *)buf); 1885 s += l; 1886 continue; 1887 } 1888 else 1889 { 1890 attr = 0; 1891 c = *s++; 1892 if (c == TAB && (!list || lcs_tab1)) 1893 { 1894 // tab amount depends on current column 1895 #ifdef FEAT_VARTABS 1896 n_extra = tabstop_padding(col, curbuf->b_p_ts, 1897 curbuf->b_p_vts_array) - 1; 1898 #else 1899 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1; 1900 #endif 1901 if (!list) 1902 { 1903 c = ' '; 1904 c_extra = ' '; 1905 c_final = NUL; 1906 } 1907 else 1908 { 1909 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1; 1910 c_extra = lcs_tab2; 1911 c_final = lcs_tab3; 1912 attr = HL_ATTR(HLF_8); 1913 } 1914 } 1915 else if (c == 160 && list && lcs_nbsp != NUL) 1916 { 1917 c = lcs_nbsp; 1918 attr = HL_ATTR(HLF_8); 1919 } 1920 else if (c == NUL && list && lcs_eol != NUL) 1921 { 1922 p_extra = (char_u *)""; 1923 c_extra = NUL; 1924 c_final = NUL; 1925 n_extra = 1; 1926 c = lcs_eol; 1927 attr = HL_ATTR(HLF_AT); 1928 --s; 1929 } 1930 else if (c != NUL && (n = byte2cells(c)) > 1) 1931 { 1932 n_extra = n - 1; 1933 p_extra = transchar_byte(c); 1934 c_extra = NUL; 1935 c_final = NUL; 1936 c = *p_extra++; 1937 // Use special coloring to be able to distinguish <hex> from 1938 // the same in plain text. 1939 attr = HL_ATTR(HLF_8); 1940 } 1941 else if (c == ' ' && trail != NULL && s > trail) 1942 { 1943 c = lcs_trail; 1944 attr = HL_ATTR(HLF_8); 1945 } 1946 else if (c == ' ' && list && lcs_space != NUL) 1947 { 1948 c = lcs_space; 1949 attr = HL_ATTR(HLF_8); 1950 } 1951 } 1952 1953 if (c == NUL) 1954 break; 1955 1956 msg_putchar_attr(c, attr); 1957 col++; 1958 } 1959 msg_clr_eos(); 1960 } 1961 1962 /* 1963 * Use screen_puts() to output one multi-byte character. 1964 * Return the pointer "s" advanced to the next character. 1965 */ 1966 static char_u * 1967 screen_puts_mbyte(char_u *s, int l, int attr) 1968 { 1969 int cw; 1970 1971 msg_didout = TRUE; // remember that line is not empty 1972 cw = (*mb_ptr2cells)(s); 1973 if (cw > 1 && ( 1974 #ifdef FEAT_RIGHTLEFT 1975 cmdmsg_rl ? msg_col <= 1 : 1976 #endif 1977 msg_col == Columns - 1)) 1978 { 1979 // Doesn't fit, print a highlighted '>' to fill it up. 1980 msg_screen_putchar('>', HL_ATTR(HLF_AT)); 1981 return s; 1982 } 1983 1984 screen_puts_len(s, l, msg_row, msg_col, attr); 1985 #ifdef FEAT_RIGHTLEFT 1986 if (cmdmsg_rl) 1987 { 1988 msg_col -= cw; 1989 if (msg_col == 0) 1990 { 1991 msg_col = Columns; 1992 ++msg_row; 1993 } 1994 } 1995 else 1996 #endif 1997 { 1998 msg_col += cw; 1999 if (msg_col >= Columns) 2000 { 2001 msg_col = 0; 2002 ++msg_row; 2003 } 2004 } 2005 return s + l; 2006 } 2007 2008 /* 2009 * Output a string to the screen at position msg_row, msg_col. 2010 * Update msg_row and msg_col for the next message. 2011 */ 2012 void 2013 msg_puts(char *s) 2014 { 2015 msg_puts_attr(s, 0); 2016 } 2017 2018 void 2019 msg_puts_title(char *s) 2020 { 2021 msg_puts_attr(s, HL_ATTR(HLF_T)); 2022 } 2023 2024 /* 2025 * Show a message in such a way that it always fits in the line. Cut out a 2026 * part in the middle and replace it with "..." when necessary. 2027 * Does not handle multi-byte characters! 2028 */ 2029 static void 2030 msg_outtrans_long_len_attr(char_u *longstr, int len, int attr) 2031 { 2032 int slen = len; 2033 int room; 2034 2035 room = Columns - msg_col; 2036 if (len > room && room >= 20) 2037 { 2038 slen = (room - 3) / 2; 2039 msg_outtrans_len_attr(longstr, slen, attr); 2040 msg_puts_attr("...", HL_ATTR(HLF_8)); 2041 } 2042 msg_outtrans_len_attr(longstr + len - slen, slen, attr); 2043 } 2044 2045 void 2046 msg_outtrans_long_attr(char_u *longstr, int attr) 2047 { 2048 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr); 2049 } 2050 2051 /* 2052 * Basic function for writing a message with highlight attributes. 2053 */ 2054 void 2055 msg_puts_attr(char *s, int attr) 2056 { 2057 msg_puts_attr_len(s, -1, attr); 2058 } 2059 2060 /* 2061 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes). 2062 * When "maxlen" is -1 there is no maximum length. 2063 * When "maxlen" is >= 0 the message is not put in the history. 2064 */ 2065 static void 2066 msg_puts_attr_len(char *str, int maxlen, int attr) 2067 { 2068 /* 2069 * If redirection is on, also write to the redirection file. 2070 */ 2071 redir_write((char_u *)str, maxlen); 2072 2073 /* 2074 * Don't print anything when using ":silent cmd". 2075 */ 2076 if (msg_silent != 0) 2077 return; 2078 2079 // if MSG_HIST flag set, add message to history 2080 if ((attr & MSG_HIST) && maxlen < 0) 2081 { 2082 add_msg_hist((char_u *)str, -1, attr); 2083 attr &= ~MSG_HIST; 2084 } 2085 2086 // When writing something to the screen after it has scrolled, requires a 2087 // wait-return prompt later. Needed when scrolling, resetting 2088 // need_wait_return after some prompt, and then outputting something 2089 // without scrolling 2090 // Not needed when only using CR to move the cursor. 2091 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0) 2092 need_wait_return = TRUE; 2093 msg_didany = TRUE; // remember that something was outputted 2094 2095 /* 2096 * If there is no valid screen, use fprintf so we can see error messages. 2097 * If termcap is not active, we may be writing in an alternate console 2098 * window, cursor positioning may not work correctly (window size may be 2099 * different, e.g. for Win32 console) or we just don't know where the 2100 * cursor is. 2101 */ 2102 if (msg_use_printf()) 2103 msg_puts_printf((char_u *)str, maxlen); 2104 else 2105 msg_puts_display((char_u *)str, maxlen, attr, FALSE); 2106 } 2107 2108 /* 2109 * The display part of msg_puts_attr_len(). 2110 * May be called recursively to display scroll-back text. 2111 */ 2112 static void 2113 msg_puts_display( 2114 char_u *str, 2115 int maxlen, 2116 int attr, 2117 int recurse) 2118 { 2119 char_u *s = str; 2120 char_u *t_s = str; // string from "t_s" to "s" is still todo 2121 int t_col = 0; // screen cells todo, 0 when "t_s" not used 2122 int l; 2123 int cw; 2124 char_u *sb_str = str; 2125 int sb_col = msg_col; 2126 int wrap; 2127 int did_last_char; 2128 2129 did_wait_return = FALSE; 2130 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL) 2131 { 2132 /* 2133 * We are at the end of the screen line when: 2134 * - When outputting a newline. 2135 * - When outputting a character in the last column. 2136 */ 2137 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || ( 2138 #ifdef FEAT_RIGHTLEFT 2139 cmdmsg_rl 2140 ? ( 2141 msg_col <= 1 2142 || (*s == TAB && msg_col <= 7) 2143 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)) 2144 : 2145 #endif 2146 ((*s != '\r' && msg_col + t_col >= Columns - 1) 2147 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7)) 2148 || (has_mbyte && (*mb_ptr2cells)(s) > 1 2149 && msg_col + t_col >= Columns - 2))))) 2150 { 2151 /* 2152 * The screen is scrolled up when at the last row (some terminals 2153 * scroll automatically, some don't. To avoid problems we scroll 2154 * ourselves). 2155 */ 2156 if (t_col > 0) 2157 // output postponed text 2158 t_puts(&t_col, t_s, s, attr); 2159 2160 // When no more prompt and no more room, truncate here 2161 if (msg_no_more && lines_left == 0) 2162 break; 2163 2164 // Scroll the screen up one line. 2165 msg_scroll_up(); 2166 2167 msg_row = Rows - 2; 2168 if (msg_col >= Columns) // can happen after screen resize 2169 msg_col = Columns - 1; 2170 2171 // Display char in last column before showing more-prompt. 2172 if (*s >= ' ' 2173 #ifdef FEAT_RIGHTLEFT 2174 && !cmdmsg_rl 2175 #endif 2176 ) 2177 { 2178 if (has_mbyte) 2179 { 2180 if (enc_utf8 && maxlen >= 0) 2181 // avoid including composing chars after the end 2182 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s)); 2183 else 2184 l = (*mb_ptr2len)(s); 2185 s = screen_puts_mbyte(s, l, attr); 2186 } 2187 else 2188 msg_screen_putchar(*s++, attr); 2189 did_last_char = TRUE; 2190 } 2191 else 2192 did_last_char = FALSE; 2193 2194 if (p_more) 2195 // store text for scrolling back 2196 store_sb_text(&sb_str, s, attr, &sb_col, TRUE); 2197 2198 inc_msg_scrolled(); 2199 need_wait_return = TRUE; // may need wait_return in main() 2200 redraw_cmdline = TRUE; 2201 if (cmdline_row > 0 && !exmode_active) 2202 --cmdline_row; 2203 2204 /* 2205 * If screen is completely filled and 'more' is set then wait 2206 * for a character. 2207 */ 2208 if (lines_left > 0) 2209 --lines_left; 2210 if (p_more && lines_left == 0 && State != HITRETURN 2211 && !msg_no_more && !exmode_active) 2212 { 2213 #ifdef FEAT_CON_DIALOG 2214 if (do_more_prompt(NUL)) 2215 s = confirm_msg_tail; 2216 #else 2217 (void)do_more_prompt(NUL); 2218 #endif 2219 if (quit_more) 2220 return; 2221 } 2222 2223 // When we displayed a char in last column need to check if there 2224 // is still more. 2225 if (did_last_char) 2226 continue; 2227 } 2228 2229 wrap = *s == '\n' 2230 || msg_col + t_col >= Columns 2231 || (has_mbyte && (*mb_ptr2cells)(s) > 1 2232 && msg_col + t_col >= Columns - 1); 2233 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b' 2234 || *s == '\t' || *s == BELL)) 2235 // output any postponed text 2236 t_puts(&t_col, t_s, s, attr); 2237 2238 if (wrap && p_more && !recurse) 2239 // store text for scrolling back 2240 store_sb_text(&sb_str, s, attr, &sb_col, TRUE); 2241 2242 if (*s == '\n') // go to next line 2243 { 2244 msg_didout = FALSE; // remember that line is empty 2245 #ifdef FEAT_RIGHTLEFT 2246 if (cmdmsg_rl) 2247 msg_col = Columns - 1; 2248 else 2249 #endif 2250 msg_col = 0; 2251 if (++msg_row >= Rows) // safety check 2252 msg_row = Rows - 1; 2253 } 2254 else if (*s == '\r') // go to column 0 2255 { 2256 msg_col = 0; 2257 } 2258 else if (*s == '\b') // go to previous char 2259 { 2260 if (msg_col) 2261 --msg_col; 2262 } 2263 else if (*s == TAB) // translate Tab into spaces 2264 { 2265 do 2266 msg_screen_putchar(' ', attr); 2267 while (msg_col & 7); 2268 } 2269 else if (*s == BELL) // beep (from ":sh") 2270 vim_beep(BO_SH); 2271 else 2272 { 2273 if (has_mbyte) 2274 { 2275 cw = (*mb_ptr2cells)(s); 2276 if (enc_utf8 && maxlen >= 0) 2277 // avoid including composing chars after the end 2278 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s)); 2279 else 2280 l = (*mb_ptr2len)(s); 2281 } 2282 else 2283 { 2284 cw = 1; 2285 l = 1; 2286 } 2287 2288 // When drawing from right to left or when a double-wide character 2289 // doesn't fit, draw a single character here. Otherwise collect 2290 // characters and draw them all at once later. 2291 if ( 2292 # ifdef FEAT_RIGHTLEFT 2293 cmdmsg_rl || 2294 # endif 2295 (cw > 1 && msg_col + t_col >= Columns - 1)) 2296 { 2297 if (l > 1) 2298 s = screen_puts_mbyte(s, l, attr) - 1; 2299 else 2300 msg_screen_putchar(*s, attr); 2301 } 2302 else 2303 { 2304 // postpone this character until later 2305 if (t_col == 0) 2306 t_s = s; 2307 t_col += cw; 2308 s += l - 1; 2309 } 2310 } 2311 ++s; 2312 } 2313 2314 // output any postponed text 2315 if (t_col > 0) 2316 t_puts(&t_col, t_s, s, attr); 2317 if (p_more && !recurse) 2318 store_sb_text(&sb_str, s, attr, &sb_col, FALSE); 2319 2320 msg_check(); 2321 } 2322 2323 /* 2324 * Return TRUE when ":filter pattern" was used and "msg" does not match 2325 * "pattern". 2326 */ 2327 int 2328 message_filtered(char_u *msg) 2329 { 2330 int match; 2331 2332 if (cmdmod.cmod_filter_regmatch.regprog == NULL) 2333 return FALSE; 2334 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0); 2335 return cmdmod.cmod_filter_force ? match : !match; 2336 } 2337 2338 /* 2339 * Scroll the screen up one line for displaying the next message line. 2340 */ 2341 static void 2342 msg_scroll_up(void) 2343 { 2344 #ifdef FEAT_GUI 2345 // Remove the cursor before scrolling, ScreenLines[] is going 2346 // to become invalid. 2347 if (gui.in_use) 2348 gui_undraw_cursor(); 2349 #endif 2350 // scrolling up always works 2351 mch_disable_flush(); 2352 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL); 2353 mch_enable_flush(); 2354 2355 if (!can_clear((char_u *)" ")) 2356 { 2357 // Scrolling up doesn't result in the right background. Set the 2358 // background here. It's not efficient, but avoids that we have to do 2359 // it all over the code. 2360 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 2361 2362 // Also clear the last char of the last but one line if it was not 2363 // cleared before to avoid a scroll-up. 2364 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1) 2365 screen_fill((int)Rows - 2, (int)Rows - 1, 2366 (int)Columns - 1, (int)Columns, ' ', ' ', 0); 2367 } 2368 } 2369 2370 /* 2371 * Increment "msg_scrolled". 2372 */ 2373 static void 2374 inc_msg_scrolled(void) 2375 { 2376 #ifdef FEAT_EVAL 2377 if (*get_vim_var_str(VV_SCROLLSTART) == NUL) 2378 { 2379 char_u *p = SOURCING_NAME; 2380 char_u *tofree = NULL; 2381 int len; 2382 2383 // v:scrollstart is empty, set it to the script/function name and line 2384 // number 2385 if (p == NULL) 2386 p = (char_u *)_("Unknown"); 2387 else 2388 { 2389 len = (int)STRLEN(p) + 40; 2390 tofree = alloc(len); 2391 if (tofree != NULL) 2392 { 2393 vim_snprintf((char *)tofree, len, _("%s line %ld"), 2394 p, (long)SOURCING_LNUM); 2395 p = tofree; 2396 } 2397 } 2398 set_vim_var_string(VV_SCROLLSTART, p, -1); 2399 vim_free(tofree); 2400 } 2401 #endif 2402 ++msg_scrolled; 2403 if (must_redraw < VALID) 2404 must_redraw = VALID; 2405 } 2406 2407 /* 2408 * To be able to scroll back at the "more" and "hit-enter" prompts we need to 2409 * store the displayed text and remember where screen lines start. 2410 */ 2411 typedef struct msgchunk_S msgchunk_T; 2412 struct msgchunk_S 2413 { 2414 msgchunk_T *sb_next; 2415 msgchunk_T *sb_prev; 2416 char sb_eol; // TRUE when line ends after this text 2417 int sb_msg_col; // column in which text starts 2418 int sb_attr; // text attributes 2419 char_u sb_text[1]; // text to be displayed, actually longer 2420 }; 2421 2422 static msgchunk_T *last_msgchunk = NULL; // last displayed text 2423 2424 static msgchunk_T *msg_sb_start(msgchunk_T *mps); 2425 2426 typedef enum { 2427 SB_CLEAR_NONE = 0, 2428 SB_CLEAR_ALL, 2429 SB_CLEAR_CMDLINE_BUSY, 2430 SB_CLEAR_CMDLINE_DONE 2431 } sb_clear_T; 2432 2433 // When to clear text on next msg. 2434 static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE; 2435 2436 /* 2437 * Store part of a printed message for displaying when scrolling back. 2438 */ 2439 static void 2440 store_sb_text( 2441 char_u **sb_str, // start of string 2442 char_u *s, // just after string 2443 int attr, 2444 int *sb_col, 2445 int finish) // line ends 2446 { 2447 msgchunk_T *mp; 2448 2449 if (do_clear_sb_text == SB_CLEAR_ALL 2450 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE) 2451 { 2452 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL); 2453 do_clear_sb_text = SB_CLEAR_NONE; 2454 } 2455 2456 if (s > *sb_str) 2457 { 2458 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str)); 2459 if (mp != NULL) 2460 { 2461 mp->sb_eol = finish; 2462 mp->sb_msg_col = *sb_col; 2463 mp->sb_attr = attr; 2464 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str); 2465 2466 if (last_msgchunk == NULL) 2467 { 2468 last_msgchunk = mp; 2469 mp->sb_prev = NULL; 2470 } 2471 else 2472 { 2473 mp->sb_prev = last_msgchunk; 2474 last_msgchunk->sb_next = mp; 2475 last_msgchunk = mp; 2476 } 2477 mp->sb_next = NULL; 2478 } 2479 } 2480 else if (finish && last_msgchunk != NULL) 2481 last_msgchunk->sb_eol = TRUE; 2482 2483 *sb_str = s; 2484 *sb_col = 0; 2485 } 2486 2487 /* 2488 * Finished showing messages, clear the scroll-back text on the next message. 2489 */ 2490 void 2491 may_clear_sb_text(void) 2492 { 2493 do_clear_sb_text = SB_CLEAR_ALL; 2494 } 2495 2496 /* 2497 * Starting to edit the command line, do not clear messages now. 2498 */ 2499 void 2500 sb_text_start_cmdline(void) 2501 { 2502 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY; 2503 msg_sb_eol(); 2504 } 2505 2506 /* 2507 * Ending to edit the command line. Clear old lines but the last one later. 2508 */ 2509 void 2510 sb_text_end_cmdline(void) 2511 { 2512 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE; 2513 } 2514 2515 /* 2516 * Clear any text remembered for scrolling back. 2517 * When "all" is FALSE keep the last line. 2518 * Called when redrawing the screen. 2519 */ 2520 void 2521 clear_sb_text(int all) 2522 { 2523 msgchunk_T *mp; 2524 msgchunk_T **lastp; 2525 2526 if (all) 2527 lastp = &last_msgchunk; 2528 else 2529 { 2530 if (last_msgchunk == NULL) 2531 return; 2532 lastp = &last_msgchunk->sb_prev; 2533 } 2534 2535 while (*lastp != NULL) 2536 { 2537 mp = (*lastp)->sb_prev; 2538 vim_free(*lastp); 2539 *lastp = mp; 2540 } 2541 } 2542 2543 /* 2544 * "g<" command. 2545 */ 2546 void 2547 show_sb_text(void) 2548 { 2549 msgchunk_T *mp; 2550 2551 // Only show something if there is more than one line, otherwise it looks 2552 // weird, typing a command without output results in one line. 2553 mp = msg_sb_start(last_msgchunk); 2554 if (mp == NULL || mp->sb_prev == NULL) 2555 vim_beep(BO_MESS); 2556 else 2557 { 2558 do_more_prompt('G'); 2559 wait_return(FALSE); 2560 } 2561 } 2562 2563 /* 2564 * Move to the start of screen line in already displayed text. 2565 */ 2566 static msgchunk_T * 2567 msg_sb_start(msgchunk_T *mps) 2568 { 2569 msgchunk_T *mp = mps; 2570 2571 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol) 2572 mp = mp->sb_prev; 2573 return mp; 2574 } 2575 2576 /* 2577 * Mark the last message chunk as finishing the line. 2578 */ 2579 void 2580 msg_sb_eol(void) 2581 { 2582 if (last_msgchunk != NULL) 2583 last_msgchunk->sb_eol = TRUE; 2584 } 2585 2586 /* 2587 * Display a screen line from previously displayed text at row "row". 2588 * Returns a pointer to the text for the next line (can be NULL). 2589 */ 2590 static msgchunk_T * 2591 disp_sb_line(int row, msgchunk_T *smp) 2592 { 2593 msgchunk_T *mp = smp; 2594 char_u *p; 2595 2596 for (;;) 2597 { 2598 msg_row = row; 2599 msg_col = mp->sb_msg_col; 2600 p = mp->sb_text; 2601 if (*p == '\n') // don't display the line break 2602 ++p; 2603 msg_puts_display(p, -1, mp->sb_attr, TRUE); 2604 if (mp->sb_eol || mp->sb_next == NULL) 2605 break; 2606 mp = mp->sb_next; 2607 } 2608 return mp->sb_next; 2609 } 2610 2611 /* 2612 * Output any postponed text for msg_puts_attr_len(). 2613 */ 2614 static void 2615 t_puts( 2616 int *t_col, 2617 char_u *t_s, 2618 char_u *s, 2619 int attr) 2620 { 2621 // output postponed text 2622 msg_didout = TRUE; // remember that line is not empty 2623 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr); 2624 msg_col += *t_col; 2625 *t_col = 0; 2626 // If the string starts with a composing character don't increment the 2627 // column position for it. 2628 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s))) 2629 --msg_col; 2630 if (msg_col >= Columns) 2631 { 2632 msg_col = 0; 2633 ++msg_row; 2634 } 2635 } 2636 2637 /* 2638 * Returns TRUE when messages should be printed with mch_errmsg(). 2639 * This is used when there is no valid screen, so we can see error messages. 2640 * If termcap is not active, we may be writing in an alternate console 2641 * window, cursor positioning may not work correctly (window size may be 2642 * different, e.g. for Win32 console) or we just don't know where the 2643 * cursor is. 2644 */ 2645 int 2646 msg_use_printf(void) 2647 { 2648 return (!msg_check_screen() 2649 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 2650 # ifdef VIMDLL 2651 || (!gui.in_use && !termcap_active) 2652 # else 2653 || !termcap_active 2654 # endif 2655 #endif 2656 || (swapping_screen() && !termcap_active) 2657 ); 2658 } 2659 2660 /* 2661 * Print a message when there is no valid screen. 2662 */ 2663 static void 2664 msg_puts_printf(char_u *str, int maxlen) 2665 { 2666 char_u *s = str; 2667 char_u *buf = NULL; 2668 char_u *p = s; 2669 2670 #ifdef MSWIN 2671 if (!(silent_mode && p_verbose == 0)) 2672 mch_settmode(TMODE_COOK); // handle CR and NL correctly 2673 #endif 2674 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL) 2675 { 2676 if (!(silent_mode && p_verbose == 0)) 2677 { 2678 // NL --> CR NL translation (for Unix, not for "--version") 2679 if (*s == NL) 2680 { 2681 int n = (int)(s - p); 2682 2683 buf = alloc(n + 3); 2684 if (buf != NULL) 2685 { 2686 memcpy(buf, p, n); 2687 if (!info_message) 2688 buf[n++] = CAR; 2689 buf[n++] = NL; 2690 buf[n++] = NUL; 2691 if (info_message) // informative message, not an error 2692 mch_msg((char *)buf); 2693 else 2694 mch_errmsg((char *)buf); 2695 vim_free(buf); 2696 } 2697 p = s + 1; 2698 } 2699 } 2700 2701 // primitive way to compute the current column 2702 #ifdef FEAT_RIGHTLEFT 2703 if (cmdmsg_rl) 2704 { 2705 if (*s == CAR || *s == NL) 2706 msg_col = Columns - 1; 2707 else 2708 --msg_col; 2709 } 2710 else 2711 #endif 2712 { 2713 if (*s == CAR || *s == NL) 2714 msg_col = 0; 2715 else 2716 ++msg_col; 2717 } 2718 ++s; 2719 } 2720 2721 if (*p != NUL && !(silent_mode && p_verbose == 0)) 2722 { 2723 int c = -1; 2724 2725 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen) 2726 { 2727 c = p[maxlen]; 2728 p[maxlen] = 0; 2729 } 2730 if (info_message) 2731 mch_msg((char *)p); 2732 else 2733 mch_errmsg((char *)p); 2734 if (c != -1) 2735 p[maxlen] = c; 2736 } 2737 2738 msg_didout = TRUE; // assume that line is not empty 2739 2740 #ifdef MSWIN 2741 if (!(silent_mode && p_verbose == 0)) 2742 mch_settmode(TMODE_RAW); 2743 #endif 2744 } 2745 2746 /* 2747 * Show the more-prompt and handle the user response. 2748 * This takes care of scrolling back and displaying previously displayed text. 2749 * When at hit-enter prompt "typed_char" is the already typed character, 2750 * otherwise it's NUL. 2751 * Returns TRUE when jumping ahead to "confirm_msg_tail". 2752 */ 2753 static int 2754 do_more_prompt(int typed_char) 2755 { 2756 static int entered = FALSE; 2757 int used_typed_char = typed_char; 2758 int oldState = State; 2759 int c; 2760 #ifdef FEAT_CON_DIALOG 2761 int retval = FALSE; 2762 #endif 2763 int toscroll; 2764 msgchunk_T *mp_last = NULL; 2765 msgchunk_T *mp; 2766 int i; 2767 2768 // We get called recursively when a timer callback outputs a message. In 2769 // that case don't show another prompt. Also when at the hit-Enter prompt 2770 // and nothing was typed. 2771 if (entered || (State == HITRETURN && typed_char == 0)) 2772 return FALSE; 2773 entered = TRUE; 2774 2775 if (typed_char == 'G') 2776 { 2777 // "g<": Find first line on the last page. 2778 mp_last = msg_sb_start(last_msgchunk); 2779 for (i = 0; i < Rows - 2 && mp_last != NULL 2780 && mp_last->sb_prev != NULL; ++i) 2781 mp_last = msg_sb_start(mp_last->sb_prev); 2782 } 2783 2784 State = ASKMORE; 2785 setmouse(); 2786 if (typed_char == NUL) 2787 msg_moremsg(FALSE); 2788 for (;;) 2789 { 2790 /* 2791 * Get a typed character directly from the user. 2792 */ 2793 if (used_typed_char != NUL) 2794 { 2795 c = used_typed_char; // was typed at hit-enter prompt 2796 used_typed_char = NUL; 2797 } 2798 else 2799 c = get_keystroke(); 2800 2801 #if defined(FEAT_MENU) && defined(FEAT_GUI) 2802 if (c == K_MENU) 2803 { 2804 int idx = get_menu_index(current_menu, ASKMORE); 2805 2806 // Used a menu. If it starts with CTRL-Y, it must 2807 // be a "Copy" for the clipboard. Otherwise 2808 // assume that we end 2809 if (idx == MENU_INDEX_INVALID) 2810 continue; 2811 c = *current_menu->strings[idx]; 2812 if (c != NUL && current_menu->strings[idx][1] != NUL) 2813 ins_typebuf(current_menu->strings[idx] + 1, 2814 current_menu->noremap[idx], 0, TRUE, 2815 current_menu->silent[idx]); 2816 } 2817 #endif 2818 2819 toscroll = 0; 2820 switch (c) 2821 { 2822 case BS: // scroll one line back 2823 case K_BS: 2824 case 'k': 2825 case K_UP: 2826 toscroll = -1; 2827 break; 2828 2829 case CAR: // one extra line 2830 case NL: 2831 case 'j': 2832 case K_DOWN: 2833 toscroll = 1; 2834 break; 2835 2836 case 'u': // Up half a page 2837 toscroll = -(Rows / 2); 2838 break; 2839 2840 case 'd': // Down half a page 2841 toscroll = Rows / 2; 2842 break; 2843 2844 case 'b': // one page back 2845 case K_PAGEUP: 2846 toscroll = -(Rows - 1); 2847 break; 2848 2849 case ' ': // one extra page 2850 case 'f': 2851 case K_PAGEDOWN: 2852 case K_LEFTMOUSE: 2853 toscroll = Rows - 1; 2854 break; 2855 2856 case 'g': // all the way back to the start 2857 toscroll = -999999; 2858 break; 2859 2860 case 'G': // all the way to the end 2861 toscroll = 999999; 2862 lines_left = 999999; 2863 break; 2864 2865 case ':': // start new command line 2866 #ifdef FEAT_CON_DIALOG 2867 if (!confirm_msg_used) 2868 #endif 2869 { 2870 // Since got_int is set all typeahead will be flushed, but we 2871 // want to keep this ':', remember that in a special way. 2872 typeahead_noflush(':'); 2873 #ifdef FEAT_TERMINAL 2874 skip_term_loop = TRUE; 2875 #endif 2876 cmdline_row = Rows - 1; // put ':' on this line 2877 skip_redraw = TRUE; // skip redraw once 2878 need_wait_return = FALSE; // don't wait in main() 2879 } 2880 // FALLTHROUGH 2881 case 'q': // quit 2882 case Ctrl_C: 2883 case ESC: 2884 #ifdef FEAT_CON_DIALOG 2885 if (confirm_msg_used) 2886 { 2887 // Jump to the choices of the dialog. 2888 retval = TRUE; 2889 } 2890 else 2891 #endif 2892 { 2893 got_int = TRUE; 2894 quit_more = TRUE; 2895 } 2896 // When there is some more output (wrapping line) display that 2897 // without another prompt. 2898 lines_left = Rows - 1; 2899 break; 2900 2901 #ifdef FEAT_CLIPBOARD 2902 case Ctrl_Y: 2903 // Strange way to allow copying (yanking) a modeless 2904 // selection at the more prompt. Use CTRL-Y, 2905 // because the same is used in Cmdline-mode and at the 2906 // hit-enter prompt. However, scrolling one line up 2907 // might be expected... 2908 if (clip_star.state == SELECT_DONE) 2909 clip_copy_modeless_selection(TRUE); 2910 continue; 2911 #endif 2912 default: // no valid response 2913 msg_moremsg(TRUE); 2914 continue; 2915 } 2916 2917 if (toscroll != 0) 2918 { 2919 if (toscroll < 0) 2920 { 2921 // go to start of last line 2922 if (mp_last == NULL) 2923 mp = msg_sb_start(last_msgchunk); 2924 else if (mp_last->sb_prev != NULL) 2925 mp = msg_sb_start(mp_last->sb_prev); 2926 else 2927 mp = NULL; 2928 2929 // go to start of line at top of the screen 2930 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL; 2931 ++i) 2932 mp = msg_sb_start(mp->sb_prev); 2933 2934 if (mp != NULL && mp->sb_prev != NULL) 2935 { 2936 // Find line to be displayed at top. 2937 for (i = 0; i > toscroll; --i) 2938 { 2939 if (mp == NULL || mp->sb_prev == NULL) 2940 break; 2941 mp = msg_sb_start(mp->sb_prev); 2942 if (mp_last == NULL) 2943 mp_last = msg_sb_start(last_msgchunk); 2944 else 2945 mp_last = msg_sb_start(mp_last->sb_prev); 2946 } 2947 2948 if (toscroll == -1 && screen_ins_lines(0, 0, 1, 2949 (int)Rows, 0, NULL) == OK) 2950 { 2951 // display line at top 2952 (void)disp_sb_line(0, mp); 2953 } 2954 else 2955 { 2956 // redisplay all lines 2957 screenclear(); 2958 for (i = 0; mp != NULL && i < Rows - 1; ++i) 2959 { 2960 mp = disp_sb_line(i, mp); 2961 ++msg_scrolled; 2962 } 2963 } 2964 toscroll = 0; 2965 } 2966 } 2967 else 2968 { 2969 // First display any text that we scrolled back. 2970 while (toscroll > 0 && mp_last != NULL) 2971 { 2972 // scroll up, display line at bottom 2973 msg_scroll_up(); 2974 inc_msg_scrolled(); 2975 screen_fill((int)Rows - 2, (int)Rows - 1, 0, 2976 (int)Columns, ' ', ' ', 0); 2977 mp_last = disp_sb_line((int)Rows - 2, mp_last); 2978 --toscroll; 2979 } 2980 } 2981 2982 if (toscroll <= 0) 2983 { 2984 // displayed the requested text, more prompt again 2985 screen_fill((int)Rows - 1, (int)Rows, 0, 2986 (int)Columns, ' ', ' ', 0); 2987 msg_moremsg(FALSE); 2988 continue; 2989 } 2990 2991 // display more text, return to caller 2992 lines_left = toscroll; 2993 } 2994 2995 break; 2996 } 2997 2998 // clear the --more-- message 2999 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3000 State = oldState; 3001 setmouse(); 3002 if (quit_more) 3003 { 3004 msg_row = Rows - 1; 3005 msg_col = 0; 3006 } 3007 #ifdef FEAT_RIGHTLEFT 3008 else if (cmdmsg_rl) 3009 msg_col = Columns - 1; 3010 #endif 3011 3012 entered = FALSE; 3013 #ifdef FEAT_CON_DIALOG 3014 return retval; 3015 #else 3016 return FALSE; 3017 #endif 3018 } 3019 3020 #if defined(USE_MCH_ERRMSG) || defined(PROTO) 3021 3022 #ifdef mch_errmsg 3023 # undef mch_errmsg 3024 #endif 3025 #ifdef mch_msg 3026 # undef mch_msg 3027 #endif 3028 3029 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3030 static void 3031 mch_errmsg_c(char *str) 3032 { 3033 int len = (int)STRLEN(str); 3034 DWORD nwrite = 0; 3035 DWORD mode = 0; 3036 HANDLE h = GetStdHandle(STD_ERROR_HANDLE); 3037 3038 if (GetConsoleMode(h, &mode) && enc_codepage >= 0 3039 && (int)GetConsoleCP() != enc_codepage) 3040 { 3041 WCHAR *w = enc_to_utf16((char_u *)str, &len); 3042 3043 WriteConsoleW(h, w, len, &nwrite, NULL); 3044 vim_free(w); 3045 } 3046 else 3047 { 3048 fprintf(stderr, "%s", str); 3049 } 3050 } 3051 #endif 3052 3053 /* 3054 * Give an error message. To be used when the screen hasn't been initialized 3055 * yet. When stderr can't be used, collect error messages until the GUI has 3056 * started and they can be displayed in a message box. 3057 */ 3058 void 3059 mch_errmsg(char *str) 3060 { 3061 #if !defined(MSWIN) || defined(FEAT_GUI_MSWIN) 3062 int len; 3063 #endif 3064 3065 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL) 3066 // On Unix use stderr if it's a tty. 3067 // When not going to start the GUI also use stderr. 3068 // On Mac, when started from Finder, stderr is the console. 3069 if ( 3070 # ifdef UNIX 3071 # ifdef MACOS_X 3072 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0) 3073 # else 3074 isatty(2) 3075 # endif 3076 # ifdef FEAT_GUI 3077 || 3078 # endif 3079 # endif 3080 # ifdef FEAT_GUI 3081 !(gui.in_use || gui.starting) 3082 # endif 3083 ) 3084 { 3085 fprintf(stderr, "%s", str); 3086 return; 3087 } 3088 #endif 3089 3090 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3091 # ifdef VIMDLL 3092 if (!(gui.in_use || gui.starting)) 3093 # endif 3094 { 3095 mch_errmsg_c(str); 3096 return; 3097 } 3098 #endif 3099 3100 #if !defined(MSWIN) || defined(FEAT_GUI_MSWIN) 3101 // avoid a delay for a message that isn't there 3102 emsg_on_display = FALSE; 3103 3104 len = (int)STRLEN(str) + 1; 3105 if (error_ga.ga_growsize == 0) 3106 { 3107 error_ga.ga_growsize = 80; 3108 error_ga.ga_itemsize = 1; 3109 } 3110 if (ga_grow(&error_ga, len) == OK) 3111 { 3112 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len, 3113 (char_u *)str, len); 3114 # ifdef UNIX 3115 // remove CR characters, they are displayed 3116 { 3117 char_u *p; 3118 3119 p = (char_u *)error_ga.ga_data + error_ga.ga_len; 3120 for (;;) 3121 { 3122 p = vim_strchr(p, '\r'); 3123 if (p == NULL) 3124 break; 3125 *p = ' '; 3126 } 3127 } 3128 # endif 3129 --len; // don't count the NUL at the end 3130 error_ga.ga_len += len; 3131 } 3132 #endif 3133 } 3134 3135 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3136 static void 3137 mch_msg_c(char *str) 3138 { 3139 int len = (int)STRLEN(str); 3140 DWORD nwrite = 0; 3141 DWORD mode; 3142 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); 3143 3144 3145 if (GetConsoleMode(h, &mode) && enc_codepage >= 0 3146 && (int)GetConsoleCP() != enc_codepage) 3147 { 3148 WCHAR *w = enc_to_utf16((char_u *)str, &len); 3149 3150 WriteConsoleW(h, w, len, &nwrite, NULL); 3151 vim_free(w); 3152 } 3153 else 3154 { 3155 printf("%s", str); 3156 } 3157 } 3158 #endif 3159 3160 /* 3161 * Give a message. To be used when the screen hasn't been initialized yet. 3162 * When there is no tty, collect messages until the GUI has started and they 3163 * can be displayed in a message box. 3164 */ 3165 void 3166 mch_msg(char *str) 3167 { 3168 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL) 3169 // On Unix use stdout if we have a tty. This allows "vim -h | more" and 3170 // uses mch_errmsg() when started from the desktop. 3171 // When not going to start the GUI also use stdout. 3172 // On Mac, when started from Finder, stderr is the console. 3173 if ( 3174 # ifdef UNIX 3175 # ifdef MACOS_X 3176 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0) 3177 # else 3178 isatty(2) 3179 # endif 3180 # ifdef FEAT_GUI 3181 || 3182 # endif 3183 # endif 3184 # ifdef FEAT_GUI 3185 !(gui.in_use || gui.starting) 3186 # endif 3187 ) 3188 { 3189 printf("%s", str); 3190 return; 3191 } 3192 #endif 3193 3194 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3195 # ifdef VIMDLL 3196 if (!(gui.in_use || gui.starting)) 3197 # endif 3198 { 3199 mch_msg_c(str); 3200 return; 3201 } 3202 #endif 3203 #if !defined(MSWIN) || defined(FEAT_GUI_MSWIN) 3204 mch_errmsg(str); 3205 #endif 3206 } 3207 #endif // USE_MCH_ERRMSG 3208 3209 /* 3210 * Put a character on the screen at the current message position and advance 3211 * to the next position. Only for printable ASCII! 3212 */ 3213 static void 3214 msg_screen_putchar(int c, int attr) 3215 { 3216 msg_didout = TRUE; // remember that line is not empty 3217 screen_putchar(c, msg_row, msg_col, attr); 3218 #ifdef FEAT_RIGHTLEFT 3219 if (cmdmsg_rl) 3220 { 3221 if (--msg_col == 0) 3222 { 3223 msg_col = Columns; 3224 ++msg_row; 3225 } 3226 } 3227 else 3228 #endif 3229 { 3230 if (++msg_col >= Columns) 3231 { 3232 msg_col = 0; 3233 ++msg_row; 3234 } 3235 } 3236 } 3237 3238 static void 3239 msg_moremsg(int full) 3240 { 3241 int attr; 3242 char_u *s = (char_u *)_("-- More --"); 3243 3244 attr = HL_ATTR(HLF_M); 3245 screen_puts(s, (int)Rows - 1, 0, attr); 3246 if (full) 3247 screen_puts((char_u *) 3248 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "), 3249 (int)Rows - 1, vim_strsize(s), attr); 3250 } 3251 3252 /* 3253 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or 3254 * exmode_active. 3255 */ 3256 void 3257 repeat_message(void) 3258 { 3259 if (State == ASKMORE) 3260 { 3261 msg_moremsg(TRUE); // display --more-- message again 3262 msg_row = Rows - 1; 3263 } 3264 #ifdef FEAT_CON_DIALOG 3265 else if (State == CONFIRM) 3266 { 3267 display_confirm_msg(); // display ":confirm" message again 3268 msg_row = Rows - 1; 3269 } 3270 #endif 3271 else if (State == EXTERNCMD) 3272 { 3273 windgoto(msg_row, msg_col); // put cursor back 3274 } 3275 else if (State == HITRETURN || State == SETWSIZE) 3276 { 3277 if (msg_row == Rows - 1) 3278 { 3279 // Avoid drawing the "hit-enter" prompt below the previous one, 3280 // overwrite it. Esp. useful when regaining focus and a 3281 // FocusGained autocmd exists but didn't draw anything. 3282 msg_didout = FALSE; 3283 msg_col = 0; 3284 msg_clr_eos(); 3285 } 3286 hit_return_msg(); 3287 msg_row = Rows - 1; 3288 } 3289 } 3290 3291 /* 3292 * msg_check_screen - check if the screen is initialized. 3293 * Also check msg_row and msg_col, if they are too big it may cause a crash. 3294 * While starting the GUI the terminal codes will be set for the GUI, but the 3295 * output goes to the terminal. Don't use the terminal codes then. 3296 */ 3297 static int 3298 msg_check_screen(void) 3299 { 3300 if (!full_screen || !screen_valid(FALSE)) 3301 return FALSE; 3302 3303 if (msg_row >= Rows) 3304 msg_row = Rows - 1; 3305 if (msg_col >= Columns) 3306 msg_col = Columns - 1; 3307 return TRUE; 3308 } 3309 3310 /* 3311 * Clear from current message position to end of screen. 3312 * Skip this when ":silent" was used, no need to clear for redirection. 3313 */ 3314 void 3315 msg_clr_eos(void) 3316 { 3317 if (msg_silent == 0) 3318 msg_clr_eos_force(); 3319 } 3320 3321 /* 3322 * Clear from current message position to end of screen. 3323 * Note: msg_col is not updated, so we remember the end of the message 3324 * for msg_check(). 3325 */ 3326 void 3327 msg_clr_eos_force(void) 3328 { 3329 if (msg_use_printf()) 3330 { 3331 if (full_screen) // only when termcap codes are valid 3332 { 3333 if (*T_CD) 3334 out_str(T_CD); // clear to end of display 3335 else if (*T_CE) 3336 out_str(T_CE); // clear to end of line 3337 } 3338 } 3339 else 3340 { 3341 #ifdef FEAT_RIGHTLEFT 3342 if (cmdmsg_rl) 3343 { 3344 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0); 3345 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3346 } 3347 else 3348 #endif 3349 { 3350 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns, 3351 ' ', ' ', 0); 3352 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3353 } 3354 } 3355 } 3356 3357 /* 3358 * Clear the command line. 3359 */ 3360 void 3361 msg_clr_cmdline(void) 3362 { 3363 msg_row = cmdline_row; 3364 msg_col = 0; 3365 msg_clr_eos_force(); 3366 } 3367 3368 /* 3369 * end putting a message on the screen 3370 * call wait_return if the message does not fit in the available space 3371 * return TRUE if wait_return not called. 3372 */ 3373 int 3374 msg_end(void) 3375 { 3376 /* 3377 * If the string is larger than the window, 3378 * or the ruler option is set and we run into it, 3379 * we have to redraw the window. 3380 * Do not do this if we are abandoning the file or editing the command line. 3381 */ 3382 if (!exiting && need_wait_return && !(State & CMDLINE)) 3383 { 3384 wait_return(FALSE); 3385 return FALSE; 3386 } 3387 out_flush(); 3388 return TRUE; 3389 } 3390 3391 /* 3392 * If the written message runs into the shown command or ruler, we have to 3393 * wait for hit-return and redraw the window later. 3394 */ 3395 void 3396 msg_check(void) 3397 { 3398 if (msg_row == Rows - 1 && msg_col >= sc_col) 3399 { 3400 need_wait_return = TRUE; 3401 redraw_cmdline = TRUE; 3402 } 3403 } 3404 3405 /* 3406 * May write a string to the redirection file. 3407 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes. 3408 */ 3409 static void 3410 redir_write(char_u *str, int maxlen) 3411 { 3412 char_u *s = str; 3413 static int cur_col = 0; 3414 3415 // Don't do anything for displaying prompts and the like. 3416 if (redir_off) 3417 return; 3418 3419 // If 'verbosefile' is set prepare for writing in that file. 3420 if (*p_vfile != NUL && verbose_fd == NULL) 3421 verbose_open(); 3422 3423 if (redirecting()) 3424 { 3425 // If the string doesn't start with CR or NL, go to msg_col 3426 if (*s != '\n' && *s != '\r') 3427 { 3428 while (cur_col < msg_col) 3429 { 3430 #ifdef FEAT_EVAL 3431 if (redir_execute) 3432 execute_redir_str((char_u *)" ", -1); 3433 else if (redir_reg) 3434 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE); 3435 else if (redir_vname) 3436 var_redir_str((char_u *)" ", -1); 3437 else 3438 #endif 3439 if (redir_fd != NULL) 3440 fputs(" ", redir_fd); 3441 if (verbose_fd != NULL) 3442 fputs(" ", verbose_fd); 3443 ++cur_col; 3444 } 3445 } 3446 3447 #ifdef FEAT_EVAL 3448 if (redir_execute) 3449 execute_redir_str(s, maxlen); 3450 else if (redir_reg) 3451 write_reg_contents(redir_reg, s, maxlen, TRUE); 3452 else if (redir_vname) 3453 var_redir_str(s, maxlen); 3454 #endif 3455 3456 // Write and adjust the current column. 3457 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen)) 3458 { 3459 #ifdef FEAT_EVAL 3460 if (!redir_reg && !redir_vname && !redir_execute) 3461 #endif 3462 if (redir_fd != NULL) 3463 putc(*s, redir_fd); 3464 if (verbose_fd != NULL) 3465 putc(*s, verbose_fd); 3466 if (*s == '\r' || *s == '\n') 3467 cur_col = 0; 3468 else if (*s == '\t') 3469 cur_col += (8 - cur_col % 8); 3470 else 3471 ++cur_col; 3472 ++s; 3473 } 3474 3475 if (msg_silent != 0) // should update msg_col 3476 msg_col = cur_col; 3477 } 3478 } 3479 3480 int 3481 redirecting(void) 3482 { 3483 return redir_fd != NULL || *p_vfile != NUL 3484 #ifdef FEAT_EVAL 3485 || redir_reg || redir_vname || redir_execute 3486 #endif 3487 ; 3488 } 3489 3490 /* 3491 * Before giving verbose message. 3492 * Must always be called paired with verbose_leave()! 3493 */ 3494 void 3495 verbose_enter(void) 3496 { 3497 if (*p_vfile != NUL) 3498 ++msg_silent; 3499 } 3500 3501 /* 3502 * After giving verbose message. 3503 * Must always be called paired with verbose_enter()! 3504 */ 3505 void 3506 verbose_leave(void) 3507 { 3508 if (*p_vfile != NUL) 3509 if (--msg_silent < 0) 3510 msg_silent = 0; 3511 } 3512 3513 /* 3514 * Like verbose_enter() and set msg_scroll when displaying the message. 3515 */ 3516 void 3517 verbose_enter_scroll(void) 3518 { 3519 if (*p_vfile != NUL) 3520 ++msg_silent; 3521 else 3522 // always scroll up, don't overwrite 3523 msg_scroll = TRUE; 3524 } 3525 3526 /* 3527 * Like verbose_leave() and set cmdline_row when displaying the message. 3528 */ 3529 void 3530 verbose_leave_scroll(void) 3531 { 3532 if (*p_vfile != NUL) 3533 { 3534 if (--msg_silent < 0) 3535 msg_silent = 0; 3536 } 3537 else 3538 cmdline_row = msg_row; 3539 } 3540 3541 /* 3542 * Called when 'verbosefile' is set: stop writing to the file. 3543 */ 3544 void 3545 verbose_stop(void) 3546 { 3547 if (verbose_fd != NULL) 3548 { 3549 fclose(verbose_fd); 3550 verbose_fd = NULL; 3551 } 3552 verbose_did_open = FALSE; 3553 } 3554 3555 /* 3556 * Open the file 'verbosefile'. 3557 * Return FAIL or OK. 3558 */ 3559 int 3560 verbose_open(void) 3561 { 3562 if (verbose_fd == NULL && !verbose_did_open) 3563 { 3564 // Only give the error message once. 3565 verbose_did_open = TRUE; 3566 3567 verbose_fd = mch_fopen((char *)p_vfile, "a"); 3568 if (verbose_fd == NULL) 3569 { 3570 semsg(_(e_notopen), p_vfile); 3571 return FAIL; 3572 } 3573 } 3574 return OK; 3575 } 3576 3577 /* 3578 * Give a warning message (for searching). 3579 * Use 'w' highlighting and may repeat the message after redrawing 3580 */ 3581 void 3582 give_warning(char_u *message, int hl) 3583 { 3584 // Don't do this for ":silent". 3585 if (msg_silent != 0) 3586 return; 3587 3588 // Don't want a hit-enter prompt here. 3589 ++no_wait_return; 3590 3591 #ifdef FEAT_EVAL 3592 set_vim_var_string(VV_WARNINGMSG, message, -1); 3593 #endif 3594 VIM_CLEAR(keep_msg); 3595 if (hl) 3596 keep_msg_attr = HL_ATTR(HLF_W); 3597 else 3598 keep_msg_attr = 0; 3599 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0) 3600 set_keep_msg(message, keep_msg_attr); 3601 msg_didout = FALSE; // overwrite this message 3602 msg_nowait = TRUE; // don't wait for this message 3603 msg_col = 0; 3604 3605 --no_wait_return; 3606 } 3607 3608 #if defined(FEAT_EVAL) || defined(PROTO) 3609 void 3610 give_warning2(char_u *message, char_u *a1, int hl) 3611 { 3612 if (IObuff == NULL) 3613 { 3614 // Very early in initialisation and already something wrong, just give 3615 // the raw message so the user at least gets a hint. 3616 give_warning((char_u *)message, hl); 3617 } 3618 else 3619 { 3620 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1); 3621 give_warning(IObuff, hl); 3622 } 3623 } 3624 #endif 3625 3626 /* 3627 * Advance msg cursor to column "col". 3628 */ 3629 void 3630 msg_advance(int col) 3631 { 3632 if (msg_silent != 0) // nothing to advance to 3633 { 3634 msg_col = col; // for redirection, may fill it up later 3635 return; 3636 } 3637 if (col >= Columns) // not enough room 3638 col = Columns - 1; 3639 #ifdef FEAT_RIGHTLEFT 3640 if (cmdmsg_rl) 3641 while (msg_col > Columns - col) 3642 msg_putchar(' '); 3643 else 3644 #endif 3645 while (msg_col < col) 3646 msg_putchar(' '); 3647 } 3648 3649 #if defined(FEAT_CON_DIALOG) || defined(PROTO) 3650 /* 3651 * Used for "confirm()" function, and the :confirm command prefix. 3652 * Versions which haven't got flexible dialogs yet, and console 3653 * versions, get this generic handler which uses the command line. 3654 * 3655 * type = one of: 3656 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC 3657 * title = title string (can be NULL for default) 3658 * (neither used in console dialogs at the moment) 3659 * 3660 * Format of the "buttons" string: 3661 * "Button1Name\nButton2Name\nButton3Name" 3662 * The first button should normally be the default/accept 3663 * The second button should be the 'Cancel' button 3664 * Other buttons- use your imagination! 3665 * A '&' in a button name becomes a shortcut, so each '&' should be before a 3666 * different letter. 3667 */ 3668 int 3669 do_dialog( 3670 int type UNUSED, 3671 char_u *title UNUSED, 3672 char_u *message, 3673 char_u *buttons, 3674 int dfltbutton, 3675 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL 3676 // otherwise 3677 int ex_cmd) // when TRUE pressing : accepts default and starts 3678 // Ex command 3679 { 3680 int oldState; 3681 int retval = 0; 3682 char_u *hotkeys; 3683 int c; 3684 int i; 3685 tmode_T save_tmode; 3686 3687 #ifndef NO_CONSOLE 3688 // Don't output anything in silent mode ("ex -s") 3689 if (silent_mode) 3690 return dfltbutton; // return default option 3691 #endif 3692 3693 #ifdef FEAT_GUI_DIALOG 3694 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog 3695 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL) 3696 { 3697 c = gui_mch_dialog(type, title, message, buttons, dfltbutton, 3698 textfield, ex_cmd); 3699 // avoid a hit-enter prompt without clearing the cmdline 3700 need_wait_return = FALSE; 3701 emsg_on_display = FALSE; 3702 cmdline_row = msg_row; 3703 3704 // Flush output to avoid that further messages and redrawing is done 3705 // in the wrong order. 3706 out_flush(); 3707 gui_mch_update(); 3708 3709 return c; 3710 } 3711 #endif 3712 3713 oldState = State; 3714 State = CONFIRM; 3715 setmouse(); 3716 3717 // Ensure raw mode here. 3718 save_tmode = cur_tmode; 3719 settmode(TMODE_RAW); 3720 3721 /* 3722 * Since we wait for a keypress, don't make the 3723 * user press RETURN as well afterwards. 3724 */ 3725 ++no_wait_return; 3726 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton); 3727 3728 if (hotkeys != NULL) 3729 { 3730 for (;;) 3731 { 3732 // Get a typed character directly from the user. 3733 c = get_keystroke(); 3734 switch (c) 3735 { 3736 case CAR: // User accepts default option 3737 case NL: 3738 retval = dfltbutton; 3739 break; 3740 case Ctrl_C: // User aborts/cancels 3741 case ESC: 3742 retval = 0; 3743 break; 3744 default: // Could be a hotkey? 3745 if (c < 0) // special keys are ignored here 3746 continue; 3747 if (c == ':' && ex_cmd) 3748 { 3749 retval = dfltbutton; 3750 ins_char_typebuf(':', 0); 3751 break; 3752 } 3753 3754 // Make the character lowercase, as chars in "hotkeys" are. 3755 c = MB_TOLOWER(c); 3756 retval = 1; 3757 for (i = 0; hotkeys[i]; ++i) 3758 { 3759 if (has_mbyte) 3760 { 3761 if ((*mb_ptr2char)(hotkeys + i) == c) 3762 break; 3763 i += (*mb_ptr2len)(hotkeys + i) - 1; 3764 } 3765 else 3766 if (hotkeys[i] == c) 3767 break; 3768 ++retval; 3769 } 3770 if (hotkeys[i]) 3771 break; 3772 // No hotkey match, so keep waiting 3773 continue; 3774 } 3775 break; 3776 } 3777 3778 vim_free(hotkeys); 3779 } 3780 3781 settmode(save_tmode); 3782 State = oldState; 3783 setmouse(); 3784 --no_wait_return; 3785 msg_end_prompt(); 3786 3787 return retval; 3788 } 3789 3790 /* 3791 * Copy one character from "*from" to "*to", taking care of multi-byte 3792 * characters. Return the length of the character in bytes. 3793 */ 3794 static int 3795 copy_char( 3796 char_u *from, 3797 char_u *to, 3798 int lowercase) // make character lower case 3799 { 3800 int len; 3801 int c; 3802 3803 if (has_mbyte) 3804 { 3805 if (lowercase) 3806 { 3807 c = MB_TOLOWER((*mb_ptr2char)(from)); 3808 return (*mb_char2bytes)(c, to); 3809 } 3810 else 3811 { 3812 len = (*mb_ptr2len)(from); 3813 mch_memmove(to, from, (size_t)len); 3814 return len; 3815 } 3816 } 3817 else 3818 { 3819 if (lowercase) 3820 *to = (char_u)TOLOWER_LOC(*from); 3821 else 3822 *to = *from; 3823 return 1; 3824 } 3825 } 3826 3827 /* 3828 * Format the dialog string, and display it at the bottom of 3829 * the screen. Return a string of hotkey chars (if defined) for 3830 * each 'button'. If a button has no hotkey defined, the first character of 3831 * the button is used. 3832 * The hotkeys can be multi-byte characters, but without combining chars. 3833 * 3834 * Returns an allocated string with hotkeys, or NULL for error. 3835 */ 3836 static char_u * 3837 msg_show_console_dialog( 3838 char_u *message, 3839 char_u *buttons, 3840 int dfltbutton) 3841 { 3842 int len = 0; 3843 #define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1) 3844 int lenhotkey = HOTK_LEN; // count first button 3845 char_u *hotk = NULL; 3846 char_u *msgp = NULL; 3847 char_u *hotkp = NULL; 3848 char_u *r; 3849 int copy; 3850 #define HAS_HOTKEY_LEN 30 3851 char_u has_hotkey[HAS_HOTKEY_LEN]; 3852 int first_hotkey = FALSE; // first char of button is hotkey 3853 int idx; 3854 3855 has_hotkey[0] = FALSE; 3856 3857 /* 3858 * First loop: compute the size of memory to allocate. 3859 * Second loop: copy to the allocated memory. 3860 */ 3861 for (copy = 0; copy <= 1; ++copy) 3862 { 3863 r = buttons; 3864 idx = 0; 3865 while (*r) 3866 { 3867 if (*r == DLG_BUTTON_SEP) 3868 { 3869 if (copy) 3870 { 3871 *msgp++ = ','; 3872 *msgp++ = ' '; // '\n' -> ', ' 3873 3874 // advance to next hotkey and set default hotkey 3875 if (has_mbyte) 3876 hotkp += STRLEN(hotkp); 3877 else 3878 ++hotkp; 3879 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL; 3880 if (dfltbutton) 3881 --dfltbutton; 3882 3883 // If no hotkey is specified first char is used. 3884 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx]) 3885 first_hotkey = TRUE; 3886 } 3887 else 3888 { 3889 len += 3; // '\n' -> ', '; 'x' -> '(x)' 3890 lenhotkey += HOTK_LEN; // each button needs a hotkey 3891 if (idx < HAS_HOTKEY_LEN - 1) 3892 has_hotkey[++idx] = FALSE; 3893 } 3894 } 3895 else if (*r == DLG_HOTKEY_CHAR || first_hotkey) 3896 { 3897 if (*r == DLG_HOTKEY_CHAR) 3898 ++r; 3899 first_hotkey = FALSE; 3900 if (copy) 3901 { 3902 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a' 3903 *msgp++ = *r; 3904 else 3905 { 3906 // '&a' -> '[a]' 3907 *msgp++ = (dfltbutton == 1) ? '[' : '('; 3908 msgp += copy_char(r, msgp, FALSE); 3909 *msgp++ = (dfltbutton == 1) ? ']' : ')'; 3910 3911 // redefine hotkey 3912 hotkp[copy_char(r, hotkp, TRUE)] = NUL; 3913 } 3914 } 3915 else 3916 { 3917 ++len; // '&a' -> '[a]' 3918 if (idx < HAS_HOTKEY_LEN - 1) 3919 has_hotkey[idx] = TRUE; 3920 } 3921 } 3922 else 3923 { 3924 // everything else copy literally 3925 if (copy) 3926 msgp += copy_char(r, msgp, FALSE); 3927 } 3928 3929 // advance to the next character 3930 MB_PTR_ADV(r); 3931 } 3932 3933 if (copy) 3934 { 3935 *msgp++ = ':'; 3936 *msgp++ = ' '; 3937 *msgp = NUL; 3938 } 3939 else 3940 { 3941 len += (int)(STRLEN(message) 3942 + 2 // for the NL's 3943 + STRLEN(buttons) 3944 + 3); // for the ": " and NUL 3945 lenhotkey++; // for the NUL 3946 3947 // If no hotkey is specified first char is used. 3948 if (!has_hotkey[0]) 3949 { 3950 first_hotkey = TRUE; 3951 len += 2; // "x" -> "[x]" 3952 } 3953 3954 /* 3955 * Now allocate and load the strings 3956 */ 3957 vim_free(confirm_msg); 3958 confirm_msg = alloc(len); 3959 if (confirm_msg == NULL) 3960 return NULL; 3961 *confirm_msg = NUL; 3962 hotk = alloc(lenhotkey); 3963 if (hotk == NULL) 3964 return NULL; 3965 3966 *confirm_msg = '\n'; 3967 STRCPY(confirm_msg + 1, message); 3968 3969 msgp = confirm_msg + 1 + STRLEN(message); 3970 hotkp = hotk; 3971 3972 // Define first default hotkey. Keep the hotkey string NUL 3973 // terminated to avoid reading past the end. 3974 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL; 3975 3976 // Remember where the choices start, displaying starts here when 3977 // "hotkp" typed at the more prompt. 3978 confirm_msg_tail = msgp; 3979 *msgp++ = '\n'; 3980 } 3981 } 3982 3983 display_confirm_msg(); 3984 return hotk; 3985 } 3986 3987 /* 3988 * Display the ":confirm" message. Also called when screen resized. 3989 */ 3990 static void 3991 display_confirm_msg(void) 3992 { 3993 // avoid that 'q' at the more prompt truncates the message here 3994 ++confirm_msg_used; 3995 if (confirm_msg != NULL) 3996 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M)); 3997 --confirm_msg_used; 3998 } 3999 4000 #endif // FEAT_CON_DIALOG 4001 4002 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) 4003 4004 int 4005 vim_dialog_yesno( 4006 int type, 4007 char_u *title, 4008 char_u *message, 4009 int dflt) 4010 { 4011 if (do_dialog(type, 4012 title == NULL ? (char_u *)_("Question") : title, 4013 message, 4014 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1) 4015 return VIM_YES; 4016 return VIM_NO; 4017 } 4018 4019 int 4020 vim_dialog_yesnocancel( 4021 int type, 4022 char_u *title, 4023 char_u *message, 4024 int dflt) 4025 { 4026 switch (do_dialog(type, 4027 title == NULL ? (char_u *)_("Question") : title, 4028 message, 4029 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE)) 4030 { 4031 case 1: return VIM_YES; 4032 case 2: return VIM_NO; 4033 } 4034 return VIM_CANCEL; 4035 } 4036 4037 int 4038 vim_dialog_yesnoallcancel( 4039 int type, 4040 char_u *title, 4041 char_u *message, 4042 int dflt) 4043 { 4044 switch (do_dialog(type, 4045 title == NULL ? (char_u *)"Question" : title, 4046 message, 4047 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"), 4048 dflt, NULL, FALSE)) 4049 { 4050 case 1: return VIM_YES; 4051 case 2: return VIM_NO; 4052 case 3: return VIM_ALL; 4053 case 4: return VIM_DISCARDALL; 4054 } 4055 return VIM_CANCEL; 4056 } 4057 4058 #endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG 4059 4060 #if defined(FEAT_EVAL) 4061 static char *e_printf = N_("E766: Insufficient arguments for printf()"); 4062 4063 /* 4064 * Get number argument from "idxp" entry in "tvs". First entry is 1. 4065 */ 4066 static varnumber_T 4067 tv_nr(typval_T *tvs, int *idxp) 4068 { 4069 int idx = *idxp - 1; 4070 varnumber_T n = 0; 4071 int err = FALSE; 4072 4073 if (tvs[idx].v_type == VAR_UNKNOWN) 4074 emsg(_(e_printf)); 4075 else 4076 { 4077 ++*idxp; 4078 n = tv_get_number_chk(&tvs[idx], &err); 4079 if (err) 4080 n = 0; 4081 } 4082 return n; 4083 } 4084 4085 /* 4086 * Get string argument from "idxp" entry in "tvs". First entry is 1. 4087 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List) 4088 * are not converted to a string. 4089 * If "tofree" is not NULL echo_string() is used. All types are converted to 4090 * a string with the same format as ":echo". The caller must free "*tofree". 4091 * Returns NULL for an error. 4092 */ 4093 static char * 4094 tv_str(typval_T *tvs, int *idxp, char_u **tofree) 4095 { 4096 int idx = *idxp - 1; 4097 char *s = NULL; 4098 static char_u numbuf[NUMBUFLEN]; 4099 4100 if (tvs[idx].v_type == VAR_UNKNOWN) 4101 emsg(_(e_printf)); 4102 else 4103 { 4104 ++*idxp; 4105 if (tofree != NULL) 4106 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID()); 4107 else 4108 s = (char *)tv_get_string_chk(&tvs[idx]); 4109 } 4110 return s; 4111 } 4112 4113 # ifdef FEAT_FLOAT 4114 /* 4115 * Get float argument from "idxp" entry in "tvs". First entry is 1. 4116 */ 4117 static double 4118 tv_float(typval_T *tvs, int *idxp) 4119 { 4120 int idx = *idxp - 1; 4121 double f = 0; 4122 4123 if (tvs[idx].v_type == VAR_UNKNOWN) 4124 emsg(_(e_printf)); 4125 else 4126 { 4127 ++*idxp; 4128 if (tvs[idx].v_type == VAR_FLOAT) 4129 f = tvs[idx].vval.v_float; 4130 else if (tvs[idx].v_type == VAR_NUMBER) 4131 f = (double)tvs[idx].vval.v_number; 4132 else 4133 emsg(_("E807: Expected Float argument for printf()")); 4134 } 4135 return f; 4136 } 4137 # endif 4138 #endif 4139 4140 #ifdef FEAT_FLOAT 4141 /* 4142 * Return the representation of infinity for printf() function: 4143 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF". 4144 */ 4145 static const char * 4146 infinity_str(int positive, 4147 char fmt_spec, 4148 int force_sign, 4149 int space_for_positive) 4150 { 4151 static const char *table[] = 4152 { 4153 "-inf", "inf", "+inf", " inf", 4154 "-INF", "INF", "+INF", " INF" 4155 }; 4156 int idx = positive * (1 + force_sign + force_sign * space_for_positive); 4157 4158 if (ASCII_ISUPPER(fmt_spec)) 4159 idx += 4; 4160 return table[idx]; 4161 } 4162 #endif 4163 4164 /* 4165 * This code was included to provide a portable vsnprintf() and snprintf(). 4166 * Some systems may provide their own, but we always use this one for 4167 * consistency. 4168 * 4169 * This code is based on snprintf.c - a portable implementation of snprintf 4170 * by Mark Martinec <[email protected]>, Version 2.2, 2000-10-06. 4171 * Included with permission. It was heavily modified to fit in Vim. 4172 * The original code, including useful comments, can be found here: 4173 * http://www.ijs.si/software/snprintf/ 4174 * 4175 * This snprintf() only supports the following conversion specifiers: 4176 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below) 4177 * with flags: '-', '+', ' ', '0' and '#'. 4178 * An asterisk is supported for field width as well as precision. 4179 * 4180 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'. 4181 * 4182 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int) 4183 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T. 4184 * 4185 * The locale is not used, the string is used as a byte string. This is only 4186 * relevant for double-byte encodings where the second byte may be '%'. 4187 * 4188 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL 4189 * pointer for resulting string argument if "str_m" is zero (as per ISO C99). 4190 * 4191 * The return value is the number of characters which would be generated 4192 * for the given input, excluding the trailing NUL. If this value 4193 * is greater or equal to "str_m", not all characters from the result 4194 * have been stored in str, output bytes beyond the ("str_m"-1) -th character 4195 * are discarded. If "str_m" is greater than zero it is guaranteed 4196 * the resulting string will be NUL-terminated. 4197 */ 4198 4199 /* 4200 * When va_list is not supported we only define vim_snprintf(). 4201 * 4202 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of 4203 * "typval_T". When the latter is not used it must be NULL. 4204 */ 4205 4206 // When generating prototypes all of this is skipped, cproto doesn't 4207 // understand this. 4208 #ifndef PROTO 4209 4210 // Like vim_vsnprintf() but append to the string. 4211 int 4212 vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...) 4213 { 4214 va_list ap; 4215 int str_l; 4216 size_t len = STRLEN(str); 4217 size_t space; 4218 4219 if (str_m <= len) 4220 space = 0; 4221 else 4222 space = str_m - len; 4223 va_start(ap, fmt); 4224 str_l = vim_vsnprintf(str + len, space, fmt, ap); 4225 va_end(ap); 4226 return str_l; 4227 } 4228 4229 int 4230 vim_snprintf(char *str, size_t str_m, const char *fmt, ...) 4231 { 4232 va_list ap; 4233 int str_l; 4234 4235 va_start(ap, fmt); 4236 str_l = vim_vsnprintf(str, str_m, fmt, ap); 4237 va_end(ap); 4238 return str_l; 4239 } 4240 4241 int 4242 vim_vsnprintf( 4243 char *str, 4244 size_t str_m, 4245 const char *fmt, 4246 va_list ap) 4247 { 4248 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL); 4249 } 4250 4251 int 4252 vim_vsnprintf_typval( 4253 char *str, 4254 size_t str_m, 4255 const char *fmt, 4256 va_list ap, 4257 typval_T *tvs) 4258 { 4259 size_t str_l = 0; 4260 const char *p = fmt; 4261 int arg_idx = 1; 4262 4263 if (p == NULL) 4264 p = ""; 4265 while (*p != NUL) 4266 { 4267 if (*p != '%') 4268 { 4269 char *q = strchr(p + 1, '%'); 4270 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p); 4271 4272 // Copy up to the next '%' or NUL without any changes. 4273 if (str_l < str_m) 4274 { 4275 size_t avail = str_m - str_l; 4276 4277 mch_memmove(str + str_l, p, n > avail ? avail : n); 4278 } 4279 p += n; 4280 str_l += n; 4281 } 4282 else 4283 { 4284 size_t min_field_width = 0, precision = 0; 4285 int zero_padding = 0, precision_specified = 0, justify_left = 0; 4286 int alternate_form = 0, force_sign = 0; 4287 4288 // If both the ' ' and '+' flags appear, the ' ' flag should be 4289 // ignored. 4290 int space_for_positive = 1; 4291 4292 // allowed values: \0, h, l, L 4293 char length_modifier = '\0'; 4294 4295 // temporary buffer for simple numeric->string conversion 4296 # if defined(FEAT_FLOAT) 4297 # define TMP_LEN 350 // On my system 1e308 is the biggest number possible. 4298 // That sounds reasonable to use as the maximum 4299 // printable. 4300 # else 4301 # define TMP_LEN 66 4302 # endif 4303 char tmp[TMP_LEN]; 4304 4305 // string address in case of string argument 4306 const char *str_arg = NULL; 4307 4308 // natural field width of arg without padding and sign 4309 size_t str_arg_l; 4310 4311 // unsigned char argument value - only defined for c conversion. 4312 // N.B. standard explicitly states the char argument for the c 4313 // conversion is unsigned 4314 unsigned char uchar_arg; 4315 4316 // number of zeros to be inserted for numeric conversions as 4317 // required by the precision or minimal field width 4318 size_t number_of_zeros_to_pad = 0; 4319 4320 // index into tmp where zero padding is to be inserted 4321 size_t zero_padding_insertion_ind = 0; 4322 4323 // current conversion specifier character 4324 char fmt_spec = '\0'; 4325 4326 // buffer for 's' and 'S' specs 4327 char_u *tofree = NULL; 4328 4329 4330 p++; // skip '%' 4331 4332 // parse flags 4333 while (*p == '0' || *p == '-' || *p == '+' || *p == ' ' 4334 || *p == '#' || *p == '\'') 4335 { 4336 switch (*p) 4337 { 4338 case '0': zero_padding = 1; break; 4339 case '-': justify_left = 1; break; 4340 case '+': force_sign = 1; space_for_positive = 0; break; 4341 case ' ': force_sign = 1; 4342 // If both the ' ' and '+' flags appear, the ' ' 4343 // flag should be ignored 4344 break; 4345 case '#': alternate_form = 1; break; 4346 case '\'': break; 4347 } 4348 p++; 4349 } 4350 // If the '0' and '-' flags both appear, the '0' flag should be 4351 // ignored. 4352 4353 // parse field width 4354 if (*p == '*') 4355 { 4356 int j; 4357 4358 p++; 4359 j = 4360 # if defined(FEAT_EVAL) 4361 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4362 # endif 4363 va_arg(ap, int); 4364 if (j >= 0) 4365 min_field_width = j; 4366 else 4367 { 4368 min_field_width = -j; 4369 justify_left = 1; 4370 } 4371 } 4372 else if (VIM_ISDIGIT((int)(*p))) 4373 { 4374 // size_t could be wider than unsigned int; make sure we treat 4375 // argument like common implementations do 4376 unsigned int uj = *p++ - '0'; 4377 4378 while (VIM_ISDIGIT((int)(*p))) 4379 uj = 10 * uj + (unsigned int)(*p++ - '0'); 4380 min_field_width = uj; 4381 } 4382 4383 // parse precision 4384 if (*p == '.') 4385 { 4386 p++; 4387 precision_specified = 1; 4388 if (*p == '*') 4389 { 4390 int j; 4391 4392 j = 4393 # if defined(FEAT_EVAL) 4394 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4395 # endif 4396 va_arg(ap, int); 4397 p++; 4398 if (j >= 0) 4399 precision = j; 4400 else 4401 { 4402 precision_specified = 0; 4403 precision = 0; 4404 } 4405 } 4406 else if (VIM_ISDIGIT((int)(*p))) 4407 { 4408 // size_t could be wider than unsigned int; make sure we 4409 // treat argument like common implementations do 4410 unsigned int uj = *p++ - '0'; 4411 4412 while (VIM_ISDIGIT((int)(*p))) 4413 uj = 10 * uj + (unsigned int)(*p++ - '0'); 4414 precision = uj; 4415 } 4416 } 4417 4418 // parse 'h', 'l' and 'll' length modifiers 4419 if (*p == 'h' || *p == 'l') 4420 { 4421 length_modifier = *p; 4422 p++; 4423 if (length_modifier == 'l' && *p == 'l') 4424 { 4425 // double l = __int64 / varnumber_T 4426 length_modifier = 'L'; 4427 p++; 4428 } 4429 } 4430 fmt_spec = *p; 4431 4432 // common synonyms: 4433 switch (fmt_spec) 4434 { 4435 case 'i': fmt_spec = 'd'; break; 4436 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break; 4437 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break; 4438 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break; 4439 default: break; 4440 } 4441 4442 # if defined(FEAT_EVAL) 4443 switch (fmt_spec) 4444 { 4445 case 'd': case 'u': case 'o': case 'x': case 'X': 4446 if (tvs != NULL && length_modifier == '\0') 4447 length_modifier = 'L'; 4448 } 4449 # endif 4450 4451 // get parameter value, do initial processing 4452 switch (fmt_spec) 4453 { 4454 // '%' and 'c' behave similar to 's' regarding flags and field 4455 // widths 4456 case '%': 4457 case 'c': 4458 case 's': 4459 case 'S': 4460 str_arg_l = 1; 4461 switch (fmt_spec) 4462 { 4463 case '%': 4464 str_arg = p; 4465 break; 4466 4467 case 'c': 4468 { 4469 int j; 4470 4471 j = 4472 # if defined(FEAT_EVAL) 4473 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4474 # endif 4475 va_arg(ap, int); 4476 // standard demands unsigned char 4477 uchar_arg = (unsigned char)j; 4478 str_arg = (char *)&uchar_arg; 4479 break; 4480 } 4481 4482 case 's': 4483 case 'S': 4484 str_arg = 4485 # if defined(FEAT_EVAL) 4486 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) : 4487 # endif 4488 va_arg(ap, char *); 4489 if (str_arg == NULL) 4490 { 4491 str_arg = "[NULL]"; 4492 str_arg_l = 6; 4493 } 4494 // make sure not to address string beyond the specified 4495 // precision !!! 4496 else if (!precision_specified) 4497 str_arg_l = strlen(str_arg); 4498 // truncate string if necessary as requested by precision 4499 else if (precision == 0) 4500 str_arg_l = 0; 4501 else 4502 { 4503 // Don't put the #if inside memchr(), it can be a 4504 // macro. 4505 // memchr on HP does not like n > 2^31 !!! 4506 char *q = memchr(str_arg, '\0', 4507 precision <= (size_t)0x7fffffffL ? precision 4508 : (size_t)0x7fffffffL); 4509 str_arg_l = (q == NULL) ? precision 4510 : (size_t)(q - str_arg); 4511 } 4512 if (fmt_spec == 'S') 4513 { 4514 if (min_field_width != 0) 4515 min_field_width += STRLEN(str_arg) 4516 - mb_string2cells((char_u *)str_arg, -1); 4517 if (precision) 4518 { 4519 char_u *p1; 4520 size_t i = 0; 4521 4522 for (p1 = (char_u *)str_arg; *p1; 4523 p1 += mb_ptr2len(p1)) 4524 { 4525 i += (size_t)mb_ptr2cells(p1); 4526 if (i > precision) 4527 break; 4528 } 4529 str_arg_l = precision = p1 - (char_u *)str_arg; 4530 } 4531 } 4532 break; 4533 4534 default: 4535 break; 4536 } 4537 break; 4538 4539 case 'd': case 'u': 4540 case 'b': case 'B': 4541 case 'o': 4542 case 'x': case 'X': 4543 case 'p': 4544 { 4545 // NOTE: the u, b, o, x, X and p conversion specifiers 4546 // imply the value is unsigned; d implies a signed 4547 // value 4548 4549 // 0 if numeric argument is zero (or if pointer is 4550 // NULL for 'p'), +1 if greater than zero (or nonzero 4551 // for unsigned arguments), -1 if negative (unsigned 4552 // argument is never negative) 4553 int arg_sign = 0; 4554 4555 // only set for length modifier h, or for no length 4556 // modifiers 4557 int int_arg = 0; 4558 unsigned int uint_arg = 0; 4559 4560 // only set for length modifier l 4561 long int long_arg = 0; 4562 unsigned long int ulong_arg = 0; 4563 4564 // only set for length modifier ll 4565 varnumber_T llong_arg = 0; 4566 uvarnumber_T ullong_arg = 0; 4567 4568 // only set for b conversion 4569 uvarnumber_T bin_arg = 0; 4570 4571 // pointer argument value -only defined for p 4572 // conversion 4573 void *ptr_arg = NULL; 4574 4575 if (fmt_spec == 'p') 4576 { 4577 length_modifier = '\0'; 4578 ptr_arg = 4579 # if defined(FEAT_EVAL) 4580 tvs != NULL ? (void *)tv_str(tvs, &arg_idx, 4581 NULL) : 4582 # endif 4583 va_arg(ap, void *); 4584 if (ptr_arg != NULL) 4585 arg_sign = 1; 4586 } 4587 else if (fmt_spec == 'b' || fmt_spec == 'B') 4588 { 4589 bin_arg = 4590 # if defined(FEAT_EVAL) 4591 tvs != NULL ? 4592 (uvarnumber_T)tv_nr(tvs, &arg_idx) : 4593 # endif 4594 va_arg(ap, uvarnumber_T); 4595 if (bin_arg != 0) 4596 arg_sign = 1; 4597 } 4598 else if (fmt_spec == 'd') 4599 { 4600 // signed 4601 switch (length_modifier) 4602 { 4603 case '\0': 4604 case 'h': 4605 // char and short arguments are passed as int. 4606 int_arg = 4607 # if defined(FEAT_EVAL) 4608 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4609 # endif 4610 va_arg(ap, int); 4611 if (int_arg > 0) 4612 arg_sign = 1; 4613 else if (int_arg < 0) 4614 arg_sign = -1; 4615 break; 4616 case 'l': 4617 long_arg = 4618 # if defined(FEAT_EVAL) 4619 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4620 # endif 4621 va_arg(ap, long int); 4622 if (long_arg > 0) 4623 arg_sign = 1; 4624 else if (long_arg < 0) 4625 arg_sign = -1; 4626 break; 4627 case 'L': 4628 llong_arg = 4629 # if defined(FEAT_EVAL) 4630 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4631 # endif 4632 va_arg(ap, varnumber_T); 4633 if (llong_arg > 0) 4634 arg_sign = 1; 4635 else if (llong_arg < 0) 4636 arg_sign = -1; 4637 break; 4638 } 4639 } 4640 else 4641 { 4642 // unsigned 4643 switch (length_modifier) 4644 { 4645 case '\0': 4646 case 'h': 4647 uint_arg = 4648 # if defined(FEAT_EVAL) 4649 tvs != NULL ? (unsigned) 4650 tv_nr(tvs, &arg_idx) : 4651 # endif 4652 va_arg(ap, unsigned int); 4653 if (uint_arg != 0) 4654 arg_sign = 1; 4655 break; 4656 case 'l': 4657 ulong_arg = 4658 # if defined(FEAT_EVAL) 4659 tvs != NULL ? (unsigned long) 4660 tv_nr(tvs, &arg_idx) : 4661 # endif 4662 va_arg(ap, unsigned long int); 4663 if (ulong_arg != 0) 4664 arg_sign = 1; 4665 break; 4666 case 'L': 4667 ullong_arg = 4668 # if defined(FEAT_EVAL) 4669 tvs != NULL ? (uvarnumber_T) 4670 tv_nr(tvs, &arg_idx) : 4671 # endif 4672 va_arg(ap, uvarnumber_T); 4673 if (ullong_arg != 0) 4674 arg_sign = 1; 4675 break; 4676 } 4677 } 4678 4679 str_arg = tmp; 4680 str_arg_l = 0; 4681 4682 // NOTE: 4683 // For d, i, u, o, x, and X conversions, if precision is 4684 // specified, the '0' flag should be ignored. This is so 4685 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux, 4686 // FreeBSD, NetBSD; but not with Perl. 4687 if (precision_specified) 4688 zero_padding = 0; 4689 if (fmt_spec == 'd') 4690 { 4691 if (force_sign && arg_sign >= 0) 4692 tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; 4693 // leave negative numbers for sprintf to handle, to 4694 // avoid handling tricky cases like (short int)-32768 4695 } 4696 else if (alternate_form) 4697 { 4698 if (arg_sign != 0 4699 && (fmt_spec == 'b' || fmt_spec == 'B' 4700 || fmt_spec == 'x' || fmt_spec == 'X') ) 4701 { 4702 tmp[str_arg_l++] = '0'; 4703 tmp[str_arg_l++] = fmt_spec; 4704 } 4705 // alternate form should have no effect for p 4706 // conversion, but ... 4707 } 4708 4709 zero_padding_insertion_ind = str_arg_l; 4710 if (!precision_specified) 4711 precision = 1; // default precision is 1 4712 if (precision == 0 && arg_sign == 0) 4713 { 4714 // When zero value is formatted with an explicit 4715 // precision 0, the resulting formatted string is 4716 // empty (d, i, u, b, B, o, x, X, p). 4717 } 4718 else 4719 { 4720 char f[6]; 4721 int f_l = 0; 4722 4723 // construct a simple format string for sprintf 4724 f[f_l++] = '%'; 4725 if (!length_modifier) 4726 ; 4727 else if (length_modifier == 'L') 4728 { 4729 # ifdef MSWIN 4730 f[f_l++] = 'I'; 4731 f[f_l++] = '6'; 4732 f[f_l++] = '4'; 4733 # else 4734 f[f_l++] = 'l'; 4735 f[f_l++] = 'l'; 4736 # endif 4737 } 4738 else 4739 f[f_l++] = length_modifier; 4740 f[f_l++] = fmt_spec; 4741 f[f_l++] = '\0'; 4742 4743 if (fmt_spec == 'p') 4744 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg); 4745 else if (fmt_spec == 'b' || fmt_spec == 'B') 4746 { 4747 char b[8 * sizeof(uvarnumber_T)]; 4748 size_t b_l = 0; 4749 uvarnumber_T bn = bin_arg; 4750 4751 do 4752 { 4753 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1); 4754 bn >>= 1; 4755 } 4756 while (bn != 0); 4757 4758 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l); 4759 str_arg_l += b_l; 4760 } 4761 else if (fmt_spec == 'd') 4762 { 4763 // signed 4764 switch (length_modifier) 4765 { 4766 case '\0': str_arg_l += sprintf( 4767 tmp + str_arg_l, f, 4768 int_arg); 4769 break; 4770 case 'h': str_arg_l += sprintf( 4771 tmp + str_arg_l, f, 4772 (short)int_arg); 4773 break; 4774 case 'l': str_arg_l += sprintf( 4775 tmp + str_arg_l, f, long_arg); 4776 break; 4777 case 'L': str_arg_l += sprintf( 4778 tmp + str_arg_l, f, llong_arg); 4779 break; 4780 } 4781 } 4782 else 4783 { 4784 // unsigned 4785 switch (length_modifier) 4786 { 4787 case '\0': str_arg_l += sprintf( 4788 tmp + str_arg_l, f, 4789 uint_arg); 4790 break; 4791 case 'h': str_arg_l += sprintf( 4792 tmp + str_arg_l, f, 4793 (unsigned short)uint_arg); 4794 break; 4795 case 'l': str_arg_l += sprintf( 4796 tmp + str_arg_l, f, ulong_arg); 4797 break; 4798 case 'L': str_arg_l += sprintf( 4799 tmp + str_arg_l, f, ullong_arg); 4800 break; 4801 } 4802 } 4803 4804 // include the optional minus sign and possible 4805 // "0x" in the region before the zero padding 4806 // insertion point 4807 if (zero_padding_insertion_ind < str_arg_l 4808 && tmp[zero_padding_insertion_ind] == '-') 4809 zero_padding_insertion_ind++; 4810 if (zero_padding_insertion_ind + 1 < str_arg_l 4811 && tmp[zero_padding_insertion_ind] == '0' 4812 && (tmp[zero_padding_insertion_ind + 1] == 'x' 4813 || tmp[zero_padding_insertion_ind + 1] == 'X')) 4814 zero_padding_insertion_ind += 2; 4815 } 4816 4817 { 4818 size_t num_of_digits = str_arg_l 4819 - zero_padding_insertion_ind; 4820 4821 if (alternate_form && fmt_spec == 'o' 4822 // unless zero is already the first 4823 // character 4824 && !(zero_padding_insertion_ind < str_arg_l 4825 && tmp[zero_padding_insertion_ind] == '0')) 4826 { 4827 // assure leading zero for alternate-form 4828 // octal numbers 4829 if (!precision_specified 4830 || precision < num_of_digits + 1) 4831 { 4832 // precision is increased to force the 4833 // first character to be zero, except if a 4834 // zero value is formatted with an 4835 // explicit precision of zero 4836 precision = num_of_digits + 1; 4837 } 4838 } 4839 // zero padding to specified precision? 4840 if (num_of_digits < precision) 4841 number_of_zeros_to_pad = precision - num_of_digits; 4842 } 4843 // zero padding to specified minimal field width? 4844 if (!justify_left && zero_padding) 4845 { 4846 int n = (int)(min_field_width - (str_arg_l 4847 + number_of_zeros_to_pad)); 4848 if (n > 0) 4849 number_of_zeros_to_pad += n; 4850 } 4851 break; 4852 } 4853 4854 # ifdef FEAT_FLOAT 4855 case 'f': 4856 case 'F': 4857 case 'e': 4858 case 'E': 4859 case 'g': 4860 case 'G': 4861 { 4862 // Floating point. 4863 double f; 4864 double abs_f; 4865 char format[40]; 4866 int l; 4867 int remove_trailing_zeroes = FALSE; 4868 4869 f = 4870 # if defined(FEAT_EVAL) 4871 tvs != NULL ? tv_float(tvs, &arg_idx) : 4872 # endif 4873 va_arg(ap, double); 4874 abs_f = f < 0 ? -f : f; 4875 4876 if (fmt_spec == 'g' || fmt_spec == 'G') 4877 { 4878 // Would be nice to use %g directly, but it prints 4879 // "1.0" as "1", we don't want that. 4880 if ((abs_f >= 0.001 && abs_f < 10000000.0) 4881 || abs_f == 0.0) 4882 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f'; 4883 else 4884 fmt_spec = fmt_spec == 'g' ? 'e' : 'E'; 4885 remove_trailing_zeroes = TRUE; 4886 } 4887 4888 if ((fmt_spec == 'f' || fmt_spec == 'F') && 4889 # ifdef VAX 4890 abs_f > 1.0e38 4891 # else 4892 abs_f > 1.0e307 4893 # endif 4894 ) 4895 { 4896 // Avoid a buffer overflow 4897 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, 4898 force_sign, space_for_positive)); 4899 str_arg_l = STRLEN(tmp); 4900 zero_padding = 0; 4901 } 4902 else 4903 { 4904 if (isnan(f)) 4905 { 4906 // Not a number: nan or NAN 4907 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN" 4908 : "nan"); 4909 str_arg_l = 3; 4910 zero_padding = 0; 4911 } 4912 else if (isinf(f)) 4913 { 4914 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, 4915 force_sign, space_for_positive)); 4916 str_arg_l = STRLEN(tmp); 4917 zero_padding = 0; 4918 } 4919 else 4920 { 4921 // Regular float number 4922 format[0] = '%'; 4923 l = 1; 4924 if (force_sign) 4925 format[l++] = space_for_positive ? ' ' : '+'; 4926 if (precision_specified) 4927 { 4928 size_t max_prec = TMP_LEN - 10; 4929 4930 // Make sure we don't get more digits than we 4931 // have room for. 4932 if ((fmt_spec == 'f' || fmt_spec == 'F') 4933 && abs_f > 1.0) 4934 max_prec -= (size_t)log10(abs_f); 4935 if (precision > max_prec) 4936 precision = max_prec; 4937 l += sprintf(format + l, ".%d", (int)precision); 4938 } 4939 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec; 4940 format[l + 1] = NUL; 4941 4942 str_arg_l = sprintf(tmp, format, f); 4943 } 4944 4945 if (remove_trailing_zeroes) 4946 { 4947 int i; 4948 char *tp; 4949 4950 // Using %g or %G: remove superfluous zeroes. 4951 if (fmt_spec == 'f' || fmt_spec == 'F') 4952 tp = tmp + str_arg_l - 1; 4953 else 4954 { 4955 tp = (char *)vim_strchr((char_u *)tmp, 4956 fmt_spec == 'e' ? 'e' : 'E'); 4957 if (tp != NULL) 4958 { 4959 // Remove superfluous '+' and leading 4960 // zeroes from the exponent. 4961 if (tp[1] == '+') 4962 { 4963 // Change "1.0e+07" to "1.0e07" 4964 STRMOVE(tp + 1, tp + 2); 4965 --str_arg_l; 4966 } 4967 i = (tp[1] == '-') ? 2 : 1; 4968 while (tp[i] == '0') 4969 { 4970 // Change "1.0e07" to "1.0e7" 4971 STRMOVE(tp + i, tp + i + 1); 4972 --str_arg_l; 4973 } 4974 --tp; 4975 } 4976 } 4977 4978 if (tp != NULL && !precision_specified) 4979 // Remove trailing zeroes, but keep the one 4980 // just after a dot. 4981 while (tp > tmp + 2 && *tp == '0' 4982 && tp[-1] != '.') 4983 { 4984 STRMOVE(tp, tp + 1); 4985 --tp; 4986 --str_arg_l; 4987 } 4988 } 4989 else 4990 { 4991 char *tp; 4992 4993 // Be consistent: some printf("%e") use 1.0e+12 4994 // and some 1.0e+012. Remove one zero in the last 4995 // case. 4996 tp = (char *)vim_strchr((char_u *)tmp, 4997 fmt_spec == 'e' ? 'e' : 'E'); 4998 if (tp != NULL && (tp[1] == '+' || tp[1] == '-') 4999 && tp[2] == '0' 5000 && vim_isdigit(tp[3]) 5001 && vim_isdigit(tp[4])) 5002 { 5003 STRMOVE(tp + 2, tp + 3); 5004 --str_arg_l; 5005 } 5006 } 5007 } 5008 if (zero_padding && min_field_width > str_arg_l 5009 && (tmp[0] == '-' || force_sign)) 5010 { 5011 // padding 0's should be inserted after the sign 5012 number_of_zeros_to_pad = min_field_width - str_arg_l; 5013 zero_padding_insertion_ind = 1; 5014 } 5015 str_arg = tmp; 5016 break; 5017 } 5018 # endif 5019 5020 default: 5021 // unrecognized conversion specifier, keep format string 5022 // as-is 5023 zero_padding = 0; // turn zero padding off for non-numeric 5024 // conversion 5025 justify_left = 1; 5026 min_field_width = 0; // reset flags 5027 5028 // discard the unrecognized conversion, just keep * 5029 // the unrecognized conversion character 5030 str_arg = p; 5031 str_arg_l = 0; 5032 if (*p != NUL) 5033 str_arg_l++; // include invalid conversion specifier 5034 // unchanged if not at end-of-string 5035 break; 5036 } 5037 5038 if (*p != NUL) 5039 p++; // step over the just processed conversion specifier 5040 5041 // insert padding to the left as requested by min_field_width; 5042 // this does not include the zero padding in case of numerical 5043 // conversions 5044 if (!justify_left) 5045 { 5046 // left padding with blank or zero 5047 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad)); 5048 5049 if (pn > 0) 5050 { 5051 if (str_l < str_m) 5052 { 5053 size_t avail = str_m - str_l; 5054 5055 vim_memset(str + str_l, zero_padding ? '0' : ' ', 5056 (size_t)pn > avail ? avail 5057 : (size_t)pn); 5058 } 5059 str_l += pn; 5060 } 5061 } 5062 5063 // zero padding as requested by the precision or by the minimal 5064 // field width for numeric conversions required? 5065 if (number_of_zeros_to_pad == 0) 5066 { 5067 // will not copy first part of numeric right now, * 5068 // force it to be copied later in its entirety 5069 zero_padding_insertion_ind = 0; 5070 } 5071 else 5072 { 5073 // insert first part of numerics (sign or '0x') before zero 5074 // padding 5075 int zn = (int)zero_padding_insertion_ind; 5076 5077 if (zn > 0) 5078 { 5079 if (str_l < str_m) 5080 { 5081 size_t avail = str_m - str_l; 5082 5083 mch_memmove(str + str_l, str_arg, 5084 (size_t)zn > avail ? avail 5085 : (size_t)zn); 5086 } 5087 str_l += zn; 5088 } 5089 5090 // insert zero padding as requested by the precision or min 5091 // field width 5092 zn = (int)number_of_zeros_to_pad; 5093 if (zn > 0) 5094 { 5095 if (str_l < str_m) 5096 { 5097 size_t avail = str_m - str_l; 5098 5099 vim_memset(str + str_l, '0', 5100 (size_t)zn > avail ? avail 5101 : (size_t)zn); 5102 } 5103 str_l += zn; 5104 } 5105 } 5106 5107 // insert formatted string 5108 // (or as-is conversion specifier for unknown conversions) 5109 { 5110 int sn = (int)(str_arg_l - zero_padding_insertion_ind); 5111 5112 if (sn > 0) 5113 { 5114 if (str_l < str_m) 5115 { 5116 size_t avail = str_m - str_l; 5117 5118 mch_memmove(str + str_l, 5119 str_arg + zero_padding_insertion_ind, 5120 (size_t)sn > avail ? avail : (size_t)sn); 5121 } 5122 str_l += sn; 5123 } 5124 } 5125 5126 // insert right padding 5127 if (justify_left) 5128 { 5129 // right blank padding to the field width 5130 int pn = (int)(min_field_width 5131 - (str_arg_l + number_of_zeros_to_pad)); 5132 5133 if (pn > 0) 5134 { 5135 if (str_l < str_m) 5136 { 5137 size_t avail = str_m - str_l; 5138 5139 vim_memset(str + str_l, ' ', 5140 (size_t)pn > avail ? avail 5141 : (size_t)pn); 5142 } 5143 str_l += pn; 5144 } 5145 } 5146 vim_free(tofree); 5147 } 5148 } 5149 5150 if (str_m > 0) 5151 { 5152 // make sure the string is nul-terminated even at the expense of 5153 // overwriting the last character (shouldn't happen, but just in case) 5154 // 5155 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0'; 5156 } 5157 5158 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN) 5159 emsg(_("E767: Too many arguments to printf()")); 5160 5161 // Return the number of characters formatted (excluding trailing nul 5162 // character), that is, the number of characters that would have been 5163 // written to the buffer if it were large enough. 5164 return (int)str_l; 5165 } 5166 5167 #endif // PROTO 5168