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 char_u *lead = NULL; 1835 int l; 1836 char_u buf[MB_MAXBYTES + 1]; 1837 1838 if (curwin->w_p_list) 1839 list = TRUE; 1840 1841 if (list) 1842 { 1843 // find start of trailing whitespace 1844 if (lcs_trail) 1845 { 1846 trail = s + STRLEN(s); 1847 while (trail > s && VIM_ISWHITE(trail[-1])) 1848 --trail; 1849 } 1850 // find end of leading whitespace 1851 if (lcs_lead) 1852 { 1853 lead = s; 1854 while (VIM_ISWHITE(lead[0])) 1855 lead++; 1856 // in a line full of spaces all of them are treated as trailing 1857 if (*lead == NUL) 1858 lead = NULL; 1859 } 1860 } 1861 1862 // output a space for an empty line, otherwise the line will be 1863 // overwritten 1864 if (*s == NUL && !(list && lcs_eol != NUL)) 1865 msg_putchar(' '); 1866 1867 while (!got_int) 1868 { 1869 if (n_extra > 0) 1870 { 1871 --n_extra; 1872 if (n_extra == 0 && c_final) 1873 c = c_final; 1874 else if (c_extra) 1875 c = c_extra; 1876 else 1877 c = *p_extra++; 1878 } 1879 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1) 1880 { 1881 col += (*mb_ptr2cells)(s); 1882 if (l >= MB_MAXBYTES) 1883 { 1884 STRCPY(buf, "?"); 1885 } 1886 else if (lcs_nbsp != NUL && list 1887 && (mb_ptr2char(s) == 160 1888 || mb_ptr2char(s) == 0x202f)) 1889 { 1890 mb_char2bytes(lcs_nbsp, buf); 1891 buf[(*mb_ptr2len)(buf)] = NUL; 1892 } 1893 else 1894 { 1895 mch_memmove(buf, s, (size_t)l); 1896 buf[l] = NUL; 1897 } 1898 msg_puts((char *)buf); 1899 s += l; 1900 continue; 1901 } 1902 else 1903 { 1904 attr = 0; 1905 c = *s++; 1906 if (c == TAB && (!list || lcs_tab1)) 1907 { 1908 // tab amount depends on current column 1909 #ifdef FEAT_VARTABS 1910 n_extra = tabstop_padding(col, curbuf->b_p_ts, 1911 curbuf->b_p_vts_array) - 1; 1912 #else 1913 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1; 1914 #endif 1915 if (!list) 1916 { 1917 c = ' '; 1918 c_extra = ' '; 1919 c_final = NUL; 1920 } 1921 else 1922 { 1923 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1; 1924 c_extra = lcs_tab2; 1925 c_final = lcs_tab3; 1926 attr = HL_ATTR(HLF_8); 1927 } 1928 } 1929 else if (c == 160 && list && lcs_nbsp != NUL) 1930 { 1931 c = lcs_nbsp; 1932 attr = HL_ATTR(HLF_8); 1933 } 1934 else if (c == NUL && list && lcs_eol != NUL) 1935 { 1936 p_extra = (char_u *)""; 1937 c_extra = NUL; 1938 c_final = NUL; 1939 n_extra = 1; 1940 c = lcs_eol; 1941 attr = HL_ATTR(HLF_AT); 1942 --s; 1943 } 1944 else if (c != NUL && (n = byte2cells(c)) > 1) 1945 { 1946 n_extra = n - 1; 1947 p_extra = transchar_byte(c); 1948 c_extra = NUL; 1949 c_final = NUL; 1950 c = *p_extra++; 1951 // Use special coloring to be able to distinguish <hex> from 1952 // the same in plain text. 1953 attr = HL_ATTR(HLF_8); 1954 } 1955 else if (c == ' ' && lead != NULL && s <= lead) 1956 { 1957 c = lcs_lead; 1958 attr = HL_ATTR(HLF_8); 1959 } 1960 else if (c == ' ' && trail != NULL && s > trail) 1961 { 1962 c = lcs_trail; 1963 attr = HL_ATTR(HLF_8); 1964 } 1965 else if (c == ' ' && list && lcs_space != NUL) 1966 { 1967 c = lcs_space; 1968 attr = HL_ATTR(HLF_8); 1969 } 1970 } 1971 1972 if (c == NUL) 1973 break; 1974 1975 msg_putchar_attr(c, attr); 1976 col++; 1977 } 1978 msg_clr_eos(); 1979 } 1980 1981 /* 1982 * Use screen_puts() to output one multi-byte character. 1983 * Return the pointer "s" advanced to the next character. 1984 */ 1985 static char_u * 1986 screen_puts_mbyte(char_u *s, int l, int attr) 1987 { 1988 int cw; 1989 1990 msg_didout = TRUE; // remember that line is not empty 1991 cw = (*mb_ptr2cells)(s); 1992 if (cw > 1 && ( 1993 #ifdef FEAT_RIGHTLEFT 1994 cmdmsg_rl ? msg_col <= 1 : 1995 #endif 1996 msg_col == Columns - 1)) 1997 { 1998 // Doesn't fit, print a highlighted '>' to fill it up. 1999 msg_screen_putchar('>', HL_ATTR(HLF_AT)); 2000 return s; 2001 } 2002 2003 screen_puts_len(s, l, msg_row, msg_col, attr); 2004 #ifdef FEAT_RIGHTLEFT 2005 if (cmdmsg_rl) 2006 { 2007 msg_col -= cw; 2008 if (msg_col == 0) 2009 { 2010 msg_col = Columns; 2011 ++msg_row; 2012 } 2013 } 2014 else 2015 #endif 2016 { 2017 msg_col += cw; 2018 if (msg_col >= Columns) 2019 { 2020 msg_col = 0; 2021 ++msg_row; 2022 } 2023 } 2024 return s + l; 2025 } 2026 2027 /* 2028 * Output a string to the screen at position msg_row, msg_col. 2029 * Update msg_row and msg_col for the next message. 2030 */ 2031 void 2032 msg_puts(char *s) 2033 { 2034 msg_puts_attr(s, 0); 2035 } 2036 2037 void 2038 msg_puts_title(char *s) 2039 { 2040 msg_puts_attr(s, HL_ATTR(HLF_T)); 2041 } 2042 2043 /* 2044 * Show a message in such a way that it always fits in the line. Cut out a 2045 * part in the middle and replace it with "..." when necessary. 2046 * Does not handle multi-byte characters! 2047 */ 2048 static void 2049 msg_outtrans_long_len_attr(char_u *longstr, int len, int attr) 2050 { 2051 int slen = len; 2052 int room; 2053 2054 room = Columns - msg_col; 2055 if (len > room && room >= 20) 2056 { 2057 slen = (room - 3) / 2; 2058 msg_outtrans_len_attr(longstr, slen, attr); 2059 msg_puts_attr("...", HL_ATTR(HLF_8)); 2060 } 2061 msg_outtrans_len_attr(longstr + len - slen, slen, attr); 2062 } 2063 2064 void 2065 msg_outtrans_long_attr(char_u *longstr, int attr) 2066 { 2067 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr); 2068 } 2069 2070 /* 2071 * Basic function for writing a message with highlight attributes. 2072 */ 2073 void 2074 msg_puts_attr(char *s, int attr) 2075 { 2076 msg_puts_attr_len(s, -1, attr); 2077 } 2078 2079 /* 2080 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes). 2081 * When "maxlen" is -1 there is no maximum length. 2082 * When "maxlen" is >= 0 the message is not put in the history. 2083 */ 2084 static void 2085 msg_puts_attr_len(char *str, int maxlen, int attr) 2086 { 2087 /* 2088 * If redirection is on, also write to the redirection file. 2089 */ 2090 redir_write((char_u *)str, maxlen); 2091 2092 /* 2093 * Don't print anything when using ":silent cmd". 2094 */ 2095 if (msg_silent != 0) 2096 return; 2097 2098 // if MSG_HIST flag set, add message to history 2099 if ((attr & MSG_HIST) && maxlen < 0) 2100 { 2101 add_msg_hist((char_u *)str, -1, attr); 2102 attr &= ~MSG_HIST; 2103 } 2104 2105 // When writing something to the screen after it has scrolled, requires a 2106 // wait-return prompt later. Needed when scrolling, resetting 2107 // need_wait_return after some prompt, and then outputting something 2108 // without scrolling 2109 // Not needed when only using CR to move the cursor. 2110 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0) 2111 need_wait_return = TRUE; 2112 msg_didany = TRUE; // remember that something was outputted 2113 2114 /* 2115 * If there is no valid screen, use fprintf so we can see error messages. 2116 * If termcap is not active, we may be writing in an alternate console 2117 * window, cursor positioning may not work correctly (window size may be 2118 * different, e.g. for Win32 console) or we just don't know where the 2119 * cursor is. 2120 */ 2121 if (msg_use_printf()) 2122 msg_puts_printf((char_u *)str, maxlen); 2123 else 2124 msg_puts_display((char_u *)str, maxlen, attr, FALSE); 2125 } 2126 2127 /* 2128 * The display part of msg_puts_attr_len(). 2129 * May be called recursively to display scroll-back text. 2130 */ 2131 static void 2132 msg_puts_display( 2133 char_u *str, 2134 int maxlen, 2135 int attr, 2136 int recurse) 2137 { 2138 char_u *s = str; 2139 char_u *t_s = str; // string from "t_s" to "s" is still todo 2140 int t_col = 0; // screen cells todo, 0 when "t_s" not used 2141 int l; 2142 int cw; 2143 char_u *sb_str = str; 2144 int sb_col = msg_col; 2145 int wrap; 2146 int did_last_char; 2147 2148 did_wait_return = FALSE; 2149 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL) 2150 { 2151 /* 2152 * We are at the end of the screen line when: 2153 * - When outputting a newline. 2154 * - When outputting a character in the last column. 2155 */ 2156 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || ( 2157 #ifdef FEAT_RIGHTLEFT 2158 cmdmsg_rl 2159 ? ( 2160 msg_col <= 1 2161 || (*s == TAB && msg_col <= 7) 2162 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)) 2163 : 2164 #endif 2165 ((*s != '\r' && msg_col + t_col >= Columns - 1) 2166 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7)) 2167 || (has_mbyte && (*mb_ptr2cells)(s) > 1 2168 && msg_col + t_col >= Columns - 2))))) 2169 { 2170 /* 2171 * The screen is scrolled up when at the last row (some terminals 2172 * scroll automatically, some don't. To avoid problems we scroll 2173 * ourselves). 2174 */ 2175 if (t_col > 0) 2176 // output postponed text 2177 t_puts(&t_col, t_s, s, attr); 2178 2179 // When no more prompt and no more room, truncate here 2180 if (msg_no_more && lines_left == 0) 2181 break; 2182 2183 // Scroll the screen up one line. 2184 msg_scroll_up(); 2185 2186 msg_row = Rows - 2; 2187 if (msg_col >= Columns) // can happen after screen resize 2188 msg_col = Columns - 1; 2189 2190 // Display char in last column before showing more-prompt. 2191 if (*s >= ' ' 2192 #ifdef FEAT_RIGHTLEFT 2193 && !cmdmsg_rl 2194 #endif 2195 ) 2196 { 2197 if (has_mbyte) 2198 { 2199 if (enc_utf8 && maxlen >= 0) 2200 // avoid including composing chars after the end 2201 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s)); 2202 else 2203 l = (*mb_ptr2len)(s); 2204 s = screen_puts_mbyte(s, l, attr); 2205 } 2206 else 2207 msg_screen_putchar(*s++, attr); 2208 did_last_char = TRUE; 2209 } 2210 else 2211 did_last_char = FALSE; 2212 2213 if (p_more) 2214 // store text for scrolling back 2215 store_sb_text(&sb_str, s, attr, &sb_col, TRUE); 2216 2217 inc_msg_scrolled(); 2218 need_wait_return = TRUE; // may need wait_return in main() 2219 redraw_cmdline = TRUE; 2220 if (cmdline_row > 0 && !exmode_active) 2221 --cmdline_row; 2222 2223 /* 2224 * If screen is completely filled and 'more' is set then wait 2225 * for a character. 2226 */ 2227 if (lines_left > 0) 2228 --lines_left; 2229 if (p_more && lines_left == 0 && State != HITRETURN 2230 && !msg_no_more && !exmode_active) 2231 { 2232 #ifdef FEAT_CON_DIALOG 2233 if (do_more_prompt(NUL)) 2234 s = confirm_msg_tail; 2235 #else 2236 (void)do_more_prompt(NUL); 2237 #endif 2238 if (quit_more) 2239 return; 2240 } 2241 2242 // When we displayed a char in last column need to check if there 2243 // is still more. 2244 if (did_last_char) 2245 continue; 2246 } 2247 2248 wrap = *s == '\n' 2249 || msg_col + t_col >= Columns 2250 || (has_mbyte && (*mb_ptr2cells)(s) > 1 2251 && msg_col + t_col >= Columns - 1); 2252 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b' 2253 || *s == '\t' || *s == BELL)) 2254 // output any postponed text 2255 t_puts(&t_col, t_s, s, attr); 2256 2257 if (wrap && p_more && !recurse) 2258 // store text for scrolling back 2259 store_sb_text(&sb_str, s, attr, &sb_col, TRUE); 2260 2261 if (*s == '\n') // go to next line 2262 { 2263 msg_didout = FALSE; // remember that line is empty 2264 #ifdef FEAT_RIGHTLEFT 2265 if (cmdmsg_rl) 2266 msg_col = Columns - 1; 2267 else 2268 #endif 2269 msg_col = 0; 2270 if (++msg_row >= Rows) // safety check 2271 msg_row = Rows - 1; 2272 } 2273 else if (*s == '\r') // go to column 0 2274 { 2275 msg_col = 0; 2276 } 2277 else if (*s == '\b') // go to previous char 2278 { 2279 if (msg_col) 2280 --msg_col; 2281 } 2282 else if (*s == TAB) // translate Tab into spaces 2283 { 2284 do 2285 msg_screen_putchar(' ', attr); 2286 while (msg_col & 7); 2287 } 2288 else if (*s == BELL) // beep (from ":sh") 2289 vim_beep(BO_SH); 2290 else 2291 { 2292 if (has_mbyte) 2293 { 2294 cw = (*mb_ptr2cells)(s); 2295 if (enc_utf8 && maxlen >= 0) 2296 // avoid including composing chars after the end 2297 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s)); 2298 else 2299 l = (*mb_ptr2len)(s); 2300 } 2301 else 2302 { 2303 cw = 1; 2304 l = 1; 2305 } 2306 2307 // When drawing from right to left or when a double-wide character 2308 // doesn't fit, draw a single character here. Otherwise collect 2309 // characters and draw them all at once later. 2310 if ( 2311 # ifdef FEAT_RIGHTLEFT 2312 cmdmsg_rl || 2313 # endif 2314 (cw > 1 && msg_col + t_col >= Columns - 1)) 2315 { 2316 if (l > 1) 2317 s = screen_puts_mbyte(s, l, attr) - 1; 2318 else 2319 msg_screen_putchar(*s, attr); 2320 } 2321 else 2322 { 2323 // postpone this character until later 2324 if (t_col == 0) 2325 t_s = s; 2326 t_col += cw; 2327 s += l - 1; 2328 } 2329 } 2330 ++s; 2331 } 2332 2333 // output any postponed text 2334 if (t_col > 0) 2335 t_puts(&t_col, t_s, s, attr); 2336 if (p_more && !recurse) 2337 store_sb_text(&sb_str, s, attr, &sb_col, FALSE); 2338 2339 msg_check(); 2340 } 2341 2342 /* 2343 * Return TRUE when ":filter pattern" was used and "msg" does not match 2344 * "pattern". 2345 */ 2346 int 2347 message_filtered(char_u *msg) 2348 { 2349 int match; 2350 2351 if (cmdmod.cmod_filter_regmatch.regprog == NULL) 2352 return FALSE; 2353 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0); 2354 return cmdmod.cmod_filter_force ? match : !match; 2355 } 2356 2357 /* 2358 * Scroll the screen up one line for displaying the next message line. 2359 */ 2360 static void 2361 msg_scroll_up(void) 2362 { 2363 #ifdef FEAT_GUI 2364 // Remove the cursor before scrolling, ScreenLines[] is going 2365 // to become invalid. 2366 if (gui.in_use) 2367 gui_undraw_cursor(); 2368 #endif 2369 // scrolling up always works 2370 mch_disable_flush(); 2371 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL); 2372 mch_enable_flush(); 2373 2374 if (!can_clear((char_u *)" ")) 2375 { 2376 // Scrolling up doesn't result in the right background. Set the 2377 // background here. It's not efficient, but avoids that we have to do 2378 // it all over the code. 2379 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 2380 2381 // Also clear the last char of the last but one line if it was not 2382 // cleared before to avoid a scroll-up. 2383 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1) 2384 screen_fill((int)Rows - 2, (int)Rows - 1, 2385 (int)Columns - 1, (int)Columns, ' ', ' ', 0); 2386 } 2387 } 2388 2389 /* 2390 * Increment "msg_scrolled". 2391 */ 2392 static void 2393 inc_msg_scrolled(void) 2394 { 2395 #ifdef FEAT_EVAL 2396 if (*get_vim_var_str(VV_SCROLLSTART) == NUL) 2397 { 2398 char_u *p = SOURCING_NAME; 2399 char_u *tofree = NULL; 2400 int len; 2401 2402 // v:scrollstart is empty, set it to the script/function name and line 2403 // number 2404 if (p == NULL) 2405 p = (char_u *)_("Unknown"); 2406 else 2407 { 2408 len = (int)STRLEN(p) + 40; 2409 tofree = alloc(len); 2410 if (tofree != NULL) 2411 { 2412 vim_snprintf((char *)tofree, len, _("%s line %ld"), 2413 p, (long)SOURCING_LNUM); 2414 p = tofree; 2415 } 2416 } 2417 set_vim_var_string(VV_SCROLLSTART, p, -1); 2418 vim_free(tofree); 2419 } 2420 #endif 2421 ++msg_scrolled; 2422 if (must_redraw < VALID) 2423 must_redraw = VALID; 2424 } 2425 2426 /* 2427 * To be able to scroll back at the "more" and "hit-enter" prompts we need to 2428 * store the displayed text and remember where screen lines start. 2429 */ 2430 typedef struct msgchunk_S msgchunk_T; 2431 struct msgchunk_S 2432 { 2433 msgchunk_T *sb_next; 2434 msgchunk_T *sb_prev; 2435 char sb_eol; // TRUE when line ends after this text 2436 int sb_msg_col; // column in which text starts 2437 int sb_attr; // text attributes 2438 char_u sb_text[1]; // text to be displayed, actually longer 2439 }; 2440 2441 static msgchunk_T *last_msgchunk = NULL; // last displayed text 2442 2443 static msgchunk_T *msg_sb_start(msgchunk_T *mps); 2444 2445 typedef enum { 2446 SB_CLEAR_NONE = 0, 2447 SB_CLEAR_ALL, 2448 SB_CLEAR_CMDLINE_BUSY, 2449 SB_CLEAR_CMDLINE_DONE 2450 } sb_clear_T; 2451 2452 // When to clear text on next msg. 2453 static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE; 2454 2455 /* 2456 * Store part of a printed message for displaying when scrolling back. 2457 */ 2458 static void 2459 store_sb_text( 2460 char_u **sb_str, // start of string 2461 char_u *s, // just after string 2462 int attr, 2463 int *sb_col, 2464 int finish) // line ends 2465 { 2466 msgchunk_T *mp; 2467 2468 if (do_clear_sb_text == SB_CLEAR_ALL 2469 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE) 2470 { 2471 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL); 2472 do_clear_sb_text = SB_CLEAR_NONE; 2473 } 2474 2475 if (s > *sb_str) 2476 { 2477 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str)); 2478 if (mp != NULL) 2479 { 2480 mp->sb_eol = finish; 2481 mp->sb_msg_col = *sb_col; 2482 mp->sb_attr = attr; 2483 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str); 2484 2485 if (last_msgchunk == NULL) 2486 { 2487 last_msgchunk = mp; 2488 mp->sb_prev = NULL; 2489 } 2490 else 2491 { 2492 mp->sb_prev = last_msgchunk; 2493 last_msgchunk->sb_next = mp; 2494 last_msgchunk = mp; 2495 } 2496 mp->sb_next = NULL; 2497 } 2498 } 2499 else if (finish && last_msgchunk != NULL) 2500 last_msgchunk->sb_eol = TRUE; 2501 2502 *sb_str = s; 2503 *sb_col = 0; 2504 } 2505 2506 /* 2507 * Finished showing messages, clear the scroll-back text on the next message. 2508 */ 2509 void 2510 may_clear_sb_text(void) 2511 { 2512 do_clear_sb_text = SB_CLEAR_ALL; 2513 } 2514 2515 /* 2516 * Starting to edit the command line, do not clear messages now. 2517 */ 2518 void 2519 sb_text_start_cmdline(void) 2520 { 2521 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY; 2522 msg_sb_eol(); 2523 } 2524 2525 /* 2526 * Ending to edit the command line. Clear old lines but the last one later. 2527 */ 2528 void 2529 sb_text_end_cmdline(void) 2530 { 2531 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE; 2532 } 2533 2534 /* 2535 * Clear any text remembered for scrolling back. 2536 * When "all" is FALSE keep the last line. 2537 * Called when redrawing the screen. 2538 */ 2539 void 2540 clear_sb_text(int all) 2541 { 2542 msgchunk_T *mp; 2543 msgchunk_T **lastp; 2544 2545 if (all) 2546 lastp = &last_msgchunk; 2547 else 2548 { 2549 if (last_msgchunk == NULL) 2550 return; 2551 lastp = &last_msgchunk->sb_prev; 2552 } 2553 2554 while (*lastp != NULL) 2555 { 2556 mp = (*lastp)->sb_prev; 2557 vim_free(*lastp); 2558 *lastp = mp; 2559 } 2560 } 2561 2562 /* 2563 * "g<" command. 2564 */ 2565 void 2566 show_sb_text(void) 2567 { 2568 msgchunk_T *mp; 2569 2570 // Only show something if there is more than one line, otherwise it looks 2571 // weird, typing a command without output results in one line. 2572 mp = msg_sb_start(last_msgchunk); 2573 if (mp == NULL || mp->sb_prev == NULL) 2574 vim_beep(BO_MESS); 2575 else 2576 { 2577 do_more_prompt('G'); 2578 wait_return(FALSE); 2579 } 2580 } 2581 2582 /* 2583 * Move to the start of screen line in already displayed text. 2584 */ 2585 static msgchunk_T * 2586 msg_sb_start(msgchunk_T *mps) 2587 { 2588 msgchunk_T *mp = mps; 2589 2590 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol) 2591 mp = mp->sb_prev; 2592 return mp; 2593 } 2594 2595 /* 2596 * Mark the last message chunk as finishing the line. 2597 */ 2598 void 2599 msg_sb_eol(void) 2600 { 2601 if (last_msgchunk != NULL) 2602 last_msgchunk->sb_eol = TRUE; 2603 } 2604 2605 /* 2606 * Display a screen line from previously displayed text at row "row". 2607 * Returns a pointer to the text for the next line (can be NULL). 2608 */ 2609 static msgchunk_T * 2610 disp_sb_line(int row, msgchunk_T *smp) 2611 { 2612 msgchunk_T *mp = smp; 2613 char_u *p; 2614 2615 for (;;) 2616 { 2617 msg_row = row; 2618 msg_col = mp->sb_msg_col; 2619 p = mp->sb_text; 2620 if (*p == '\n') // don't display the line break 2621 ++p; 2622 msg_puts_display(p, -1, mp->sb_attr, TRUE); 2623 if (mp->sb_eol || mp->sb_next == NULL) 2624 break; 2625 mp = mp->sb_next; 2626 } 2627 return mp->sb_next; 2628 } 2629 2630 /* 2631 * Output any postponed text for msg_puts_attr_len(). 2632 */ 2633 static void 2634 t_puts( 2635 int *t_col, 2636 char_u *t_s, 2637 char_u *s, 2638 int attr) 2639 { 2640 // output postponed text 2641 msg_didout = TRUE; // remember that line is not empty 2642 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr); 2643 msg_col += *t_col; 2644 *t_col = 0; 2645 // If the string starts with a composing character don't increment the 2646 // column position for it. 2647 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s))) 2648 --msg_col; 2649 if (msg_col >= Columns) 2650 { 2651 msg_col = 0; 2652 ++msg_row; 2653 } 2654 } 2655 2656 /* 2657 * Returns TRUE when messages should be printed with mch_errmsg(). 2658 * This is used when there is no valid screen, so we can see error messages. 2659 * If termcap is not active, we may be writing in an alternate console 2660 * window, cursor positioning may not work correctly (window size may be 2661 * different, e.g. for Win32 console) or we just don't know where the 2662 * cursor is. 2663 */ 2664 int 2665 msg_use_printf(void) 2666 { 2667 return (!msg_check_screen() 2668 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 2669 # ifdef VIMDLL 2670 || (!gui.in_use && !termcap_active) 2671 # else 2672 || !termcap_active 2673 # endif 2674 #endif 2675 || (swapping_screen() && !termcap_active) 2676 ); 2677 } 2678 2679 /* 2680 * Print a message when there is no valid screen. 2681 */ 2682 static void 2683 msg_puts_printf(char_u *str, int maxlen) 2684 { 2685 char_u *s = str; 2686 char_u *buf = NULL; 2687 char_u *p = s; 2688 2689 #ifdef MSWIN 2690 if (!(silent_mode && p_verbose == 0)) 2691 mch_settmode(TMODE_COOK); // handle CR and NL correctly 2692 #endif 2693 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL) 2694 { 2695 if (!(silent_mode && p_verbose == 0)) 2696 { 2697 // NL --> CR NL translation (for Unix, not for "--version") 2698 if (*s == NL) 2699 { 2700 int n = (int)(s - p); 2701 2702 buf = alloc(n + 3); 2703 if (buf != NULL) 2704 { 2705 memcpy(buf, p, n); 2706 if (!info_message) 2707 buf[n++] = CAR; 2708 buf[n++] = NL; 2709 buf[n++] = NUL; 2710 if (info_message) // informative message, not an error 2711 mch_msg((char *)buf); 2712 else 2713 mch_errmsg((char *)buf); 2714 vim_free(buf); 2715 } 2716 p = s + 1; 2717 } 2718 } 2719 2720 // primitive way to compute the current column 2721 #ifdef FEAT_RIGHTLEFT 2722 if (cmdmsg_rl) 2723 { 2724 if (*s == CAR || *s == NL) 2725 msg_col = Columns - 1; 2726 else 2727 --msg_col; 2728 } 2729 else 2730 #endif 2731 { 2732 if (*s == CAR || *s == NL) 2733 msg_col = 0; 2734 else 2735 ++msg_col; 2736 } 2737 ++s; 2738 } 2739 2740 if (*p != NUL && !(silent_mode && p_verbose == 0)) 2741 { 2742 int c = -1; 2743 2744 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen) 2745 { 2746 c = p[maxlen]; 2747 p[maxlen] = 0; 2748 } 2749 if (info_message) 2750 mch_msg((char *)p); 2751 else 2752 mch_errmsg((char *)p); 2753 if (c != -1) 2754 p[maxlen] = c; 2755 } 2756 2757 msg_didout = TRUE; // assume that line is not empty 2758 2759 #ifdef MSWIN 2760 if (!(silent_mode && p_verbose == 0)) 2761 mch_settmode(TMODE_RAW); 2762 #endif 2763 } 2764 2765 /* 2766 * Show the more-prompt and handle the user response. 2767 * This takes care of scrolling back and displaying previously displayed text. 2768 * When at hit-enter prompt "typed_char" is the already typed character, 2769 * otherwise it's NUL. 2770 * Returns TRUE when jumping ahead to "confirm_msg_tail". 2771 */ 2772 static int 2773 do_more_prompt(int typed_char) 2774 { 2775 static int entered = FALSE; 2776 int used_typed_char = typed_char; 2777 int oldState = State; 2778 int c; 2779 #ifdef FEAT_CON_DIALOG 2780 int retval = FALSE; 2781 #endif 2782 int toscroll; 2783 msgchunk_T *mp_last = NULL; 2784 msgchunk_T *mp; 2785 int i; 2786 2787 // We get called recursively when a timer callback outputs a message. In 2788 // that case don't show another prompt. Also when at the hit-Enter prompt 2789 // and nothing was typed. 2790 if (entered || (State == HITRETURN && typed_char == 0)) 2791 return FALSE; 2792 entered = TRUE; 2793 2794 if (typed_char == 'G') 2795 { 2796 // "g<": Find first line on the last page. 2797 mp_last = msg_sb_start(last_msgchunk); 2798 for (i = 0; i < Rows - 2 && mp_last != NULL 2799 && mp_last->sb_prev != NULL; ++i) 2800 mp_last = msg_sb_start(mp_last->sb_prev); 2801 } 2802 2803 State = ASKMORE; 2804 setmouse(); 2805 if (typed_char == NUL) 2806 msg_moremsg(FALSE); 2807 for (;;) 2808 { 2809 /* 2810 * Get a typed character directly from the user. 2811 */ 2812 if (used_typed_char != NUL) 2813 { 2814 c = used_typed_char; // was typed at hit-enter prompt 2815 used_typed_char = NUL; 2816 } 2817 else 2818 c = get_keystroke(); 2819 2820 #if defined(FEAT_MENU) && defined(FEAT_GUI) 2821 if (c == K_MENU) 2822 { 2823 int idx = get_menu_index(current_menu, ASKMORE); 2824 2825 // Used a menu. If it starts with CTRL-Y, it must 2826 // be a "Copy" for the clipboard. Otherwise 2827 // assume that we end 2828 if (idx == MENU_INDEX_INVALID) 2829 continue; 2830 c = *current_menu->strings[idx]; 2831 if (c != NUL && current_menu->strings[idx][1] != NUL) 2832 ins_typebuf(current_menu->strings[idx] + 1, 2833 current_menu->noremap[idx], 0, TRUE, 2834 current_menu->silent[idx]); 2835 } 2836 #endif 2837 2838 toscroll = 0; 2839 switch (c) 2840 { 2841 case BS: // scroll one line back 2842 case K_BS: 2843 case 'k': 2844 case K_UP: 2845 toscroll = -1; 2846 break; 2847 2848 case CAR: // one extra line 2849 case NL: 2850 case 'j': 2851 case K_DOWN: 2852 toscroll = 1; 2853 break; 2854 2855 case 'u': // Up half a page 2856 toscroll = -(Rows / 2); 2857 break; 2858 2859 case 'd': // Down half a page 2860 toscroll = Rows / 2; 2861 break; 2862 2863 case 'b': // one page back 2864 case K_PAGEUP: 2865 toscroll = -(Rows - 1); 2866 break; 2867 2868 case ' ': // one extra page 2869 case 'f': 2870 case K_PAGEDOWN: 2871 case K_LEFTMOUSE: 2872 toscroll = Rows - 1; 2873 break; 2874 2875 case 'g': // all the way back to the start 2876 toscroll = -999999; 2877 break; 2878 2879 case 'G': // all the way to the end 2880 toscroll = 999999; 2881 lines_left = 999999; 2882 break; 2883 2884 case ':': // start new command line 2885 #ifdef FEAT_CON_DIALOG 2886 if (!confirm_msg_used) 2887 #endif 2888 { 2889 // Since got_int is set all typeahead will be flushed, but we 2890 // want to keep this ':', remember that in a special way. 2891 typeahead_noflush(':'); 2892 #ifdef FEAT_TERMINAL 2893 skip_term_loop = TRUE; 2894 #endif 2895 cmdline_row = Rows - 1; // put ':' on this line 2896 skip_redraw = TRUE; // skip redraw once 2897 need_wait_return = FALSE; // don't wait in main() 2898 } 2899 // FALLTHROUGH 2900 case 'q': // quit 2901 case Ctrl_C: 2902 case ESC: 2903 #ifdef FEAT_CON_DIALOG 2904 if (confirm_msg_used) 2905 { 2906 // Jump to the choices of the dialog. 2907 retval = TRUE; 2908 } 2909 else 2910 #endif 2911 { 2912 got_int = TRUE; 2913 quit_more = TRUE; 2914 } 2915 // When there is some more output (wrapping line) display that 2916 // without another prompt. 2917 lines_left = Rows - 1; 2918 break; 2919 2920 #ifdef FEAT_CLIPBOARD 2921 case Ctrl_Y: 2922 // Strange way to allow copying (yanking) a modeless 2923 // selection at the more prompt. Use CTRL-Y, 2924 // because the same is used in Cmdline-mode and at the 2925 // hit-enter prompt. However, scrolling one line up 2926 // might be expected... 2927 if (clip_star.state == SELECT_DONE) 2928 clip_copy_modeless_selection(TRUE); 2929 continue; 2930 #endif 2931 default: // no valid response 2932 msg_moremsg(TRUE); 2933 continue; 2934 } 2935 2936 if (toscroll != 0) 2937 { 2938 if (toscroll < 0) 2939 { 2940 // go to start of last line 2941 if (mp_last == NULL) 2942 mp = msg_sb_start(last_msgchunk); 2943 else if (mp_last->sb_prev != NULL) 2944 mp = msg_sb_start(mp_last->sb_prev); 2945 else 2946 mp = NULL; 2947 2948 // go to start of line at top of the screen 2949 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL; 2950 ++i) 2951 mp = msg_sb_start(mp->sb_prev); 2952 2953 if (mp != NULL && mp->sb_prev != NULL) 2954 { 2955 // Find line to be displayed at top. 2956 for (i = 0; i > toscroll; --i) 2957 { 2958 if (mp == NULL || mp->sb_prev == NULL) 2959 break; 2960 mp = msg_sb_start(mp->sb_prev); 2961 if (mp_last == NULL) 2962 mp_last = msg_sb_start(last_msgchunk); 2963 else 2964 mp_last = msg_sb_start(mp_last->sb_prev); 2965 } 2966 2967 if (toscroll == -1 && screen_ins_lines(0, 0, 1, 2968 (int)Rows, 0, NULL) == OK) 2969 { 2970 // display line at top 2971 (void)disp_sb_line(0, mp); 2972 } 2973 else 2974 { 2975 // redisplay all lines 2976 screenclear(); 2977 for (i = 0; mp != NULL && i < Rows - 1; ++i) 2978 { 2979 mp = disp_sb_line(i, mp); 2980 ++msg_scrolled; 2981 } 2982 } 2983 toscroll = 0; 2984 } 2985 } 2986 else 2987 { 2988 // First display any text that we scrolled back. 2989 while (toscroll > 0 && mp_last != NULL) 2990 { 2991 // scroll up, display line at bottom 2992 msg_scroll_up(); 2993 inc_msg_scrolled(); 2994 screen_fill((int)Rows - 2, (int)Rows - 1, 0, 2995 (int)Columns, ' ', ' ', 0); 2996 mp_last = disp_sb_line((int)Rows - 2, mp_last); 2997 --toscroll; 2998 } 2999 } 3000 3001 if (toscroll <= 0) 3002 { 3003 // displayed the requested text, more prompt again 3004 screen_fill((int)Rows - 1, (int)Rows, 0, 3005 (int)Columns, ' ', ' ', 0); 3006 msg_moremsg(FALSE); 3007 continue; 3008 } 3009 3010 // display more text, return to caller 3011 lines_left = toscroll; 3012 } 3013 3014 break; 3015 } 3016 3017 // clear the --more-- message 3018 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3019 State = oldState; 3020 setmouse(); 3021 if (quit_more) 3022 { 3023 msg_row = Rows - 1; 3024 msg_col = 0; 3025 } 3026 #ifdef FEAT_RIGHTLEFT 3027 else if (cmdmsg_rl) 3028 msg_col = Columns - 1; 3029 #endif 3030 3031 entered = FALSE; 3032 #ifdef FEAT_CON_DIALOG 3033 return retval; 3034 #else 3035 return FALSE; 3036 #endif 3037 } 3038 3039 #if defined(USE_MCH_ERRMSG) || defined(PROTO) 3040 3041 #ifdef mch_errmsg 3042 # undef mch_errmsg 3043 #endif 3044 #ifdef mch_msg 3045 # undef mch_msg 3046 #endif 3047 3048 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3049 static void 3050 mch_errmsg_c(char *str) 3051 { 3052 int len = (int)STRLEN(str); 3053 DWORD nwrite = 0; 3054 DWORD mode = 0; 3055 HANDLE h = GetStdHandle(STD_ERROR_HANDLE); 3056 3057 if (GetConsoleMode(h, &mode) && enc_codepage >= 0 3058 && (int)GetConsoleCP() != enc_codepage) 3059 { 3060 WCHAR *w = enc_to_utf16((char_u *)str, &len); 3061 3062 WriteConsoleW(h, w, len, &nwrite, NULL); 3063 vim_free(w); 3064 } 3065 else 3066 { 3067 fprintf(stderr, "%s", str); 3068 } 3069 } 3070 #endif 3071 3072 /* 3073 * Give an error message. To be used when the screen hasn't been initialized 3074 * yet. When stderr can't be used, collect error messages until the GUI has 3075 * started and they can be displayed in a message box. 3076 */ 3077 void 3078 mch_errmsg(char *str) 3079 { 3080 #if !defined(MSWIN) || defined(FEAT_GUI_MSWIN) 3081 int len; 3082 #endif 3083 3084 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL) 3085 // On Unix use stderr if it's a tty. 3086 // When not going to start the GUI also use stderr. 3087 // On Mac, when started from Finder, stderr is the console. 3088 if ( 3089 # ifdef UNIX 3090 # ifdef MACOS_X 3091 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0) 3092 # else 3093 isatty(2) 3094 # endif 3095 # ifdef FEAT_GUI 3096 || 3097 # endif 3098 # endif 3099 # ifdef FEAT_GUI 3100 !(gui.in_use || gui.starting) 3101 # endif 3102 ) 3103 { 3104 fprintf(stderr, "%s", str); 3105 return; 3106 } 3107 #endif 3108 3109 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3110 # ifdef VIMDLL 3111 if (!(gui.in_use || gui.starting)) 3112 # endif 3113 { 3114 mch_errmsg_c(str); 3115 return; 3116 } 3117 #endif 3118 3119 #if !defined(MSWIN) || defined(FEAT_GUI_MSWIN) 3120 // avoid a delay for a message that isn't there 3121 emsg_on_display = FALSE; 3122 3123 len = (int)STRLEN(str) + 1; 3124 if (error_ga.ga_growsize == 0) 3125 { 3126 error_ga.ga_growsize = 80; 3127 error_ga.ga_itemsize = 1; 3128 } 3129 if (ga_grow(&error_ga, len) == OK) 3130 { 3131 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len, 3132 (char_u *)str, len); 3133 # ifdef UNIX 3134 // remove CR characters, they are displayed 3135 { 3136 char_u *p; 3137 3138 p = (char_u *)error_ga.ga_data + error_ga.ga_len; 3139 for (;;) 3140 { 3141 p = vim_strchr(p, '\r'); 3142 if (p == NULL) 3143 break; 3144 *p = ' '; 3145 } 3146 } 3147 # endif 3148 --len; // don't count the NUL at the end 3149 error_ga.ga_len += len; 3150 } 3151 #endif 3152 } 3153 3154 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3155 static void 3156 mch_msg_c(char *str) 3157 { 3158 int len = (int)STRLEN(str); 3159 DWORD nwrite = 0; 3160 DWORD mode; 3161 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); 3162 3163 3164 if (GetConsoleMode(h, &mode) && enc_codepage >= 0 3165 && (int)GetConsoleCP() != enc_codepage) 3166 { 3167 WCHAR *w = enc_to_utf16((char_u *)str, &len); 3168 3169 WriteConsoleW(h, w, len, &nwrite, NULL); 3170 vim_free(w); 3171 } 3172 else 3173 { 3174 printf("%s", str); 3175 } 3176 } 3177 #endif 3178 3179 /* 3180 * Give a message. To be used when the screen hasn't been initialized yet. 3181 * When there is no tty, collect messages until the GUI has started and they 3182 * can be displayed in a message box. 3183 */ 3184 void 3185 mch_msg(char *str) 3186 { 3187 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL) 3188 // On Unix use stdout if we have a tty. This allows "vim -h | more" and 3189 // uses mch_errmsg() when started from the desktop. 3190 // When not going to start the GUI also use stdout. 3191 // On Mac, when started from Finder, stderr is the console. 3192 if ( 3193 # ifdef UNIX 3194 # ifdef MACOS_X 3195 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0) 3196 # else 3197 isatty(2) 3198 # endif 3199 # ifdef FEAT_GUI 3200 || 3201 # endif 3202 # endif 3203 # ifdef FEAT_GUI 3204 !(gui.in_use || gui.starting) 3205 # endif 3206 ) 3207 { 3208 printf("%s", str); 3209 return; 3210 } 3211 #endif 3212 3213 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3214 # ifdef VIMDLL 3215 if (!(gui.in_use || gui.starting)) 3216 # endif 3217 { 3218 mch_msg_c(str); 3219 return; 3220 } 3221 #endif 3222 #if !defined(MSWIN) || defined(FEAT_GUI_MSWIN) 3223 mch_errmsg(str); 3224 #endif 3225 } 3226 #endif // USE_MCH_ERRMSG 3227 3228 /* 3229 * Put a character on the screen at the current message position and advance 3230 * to the next position. Only for printable ASCII! 3231 */ 3232 static void 3233 msg_screen_putchar(int c, int attr) 3234 { 3235 msg_didout = TRUE; // remember that line is not empty 3236 screen_putchar(c, msg_row, msg_col, attr); 3237 #ifdef FEAT_RIGHTLEFT 3238 if (cmdmsg_rl) 3239 { 3240 if (--msg_col == 0) 3241 { 3242 msg_col = Columns; 3243 ++msg_row; 3244 } 3245 } 3246 else 3247 #endif 3248 { 3249 if (++msg_col >= Columns) 3250 { 3251 msg_col = 0; 3252 ++msg_row; 3253 } 3254 } 3255 } 3256 3257 static void 3258 msg_moremsg(int full) 3259 { 3260 int attr; 3261 char_u *s = (char_u *)_("-- More --"); 3262 3263 attr = HL_ATTR(HLF_M); 3264 screen_puts(s, (int)Rows - 1, 0, attr); 3265 if (full) 3266 screen_puts((char_u *) 3267 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "), 3268 (int)Rows - 1, vim_strsize(s), attr); 3269 } 3270 3271 /* 3272 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or 3273 * exmode_active. 3274 */ 3275 void 3276 repeat_message(void) 3277 { 3278 if (State == ASKMORE) 3279 { 3280 msg_moremsg(TRUE); // display --more-- message again 3281 msg_row = Rows - 1; 3282 } 3283 #ifdef FEAT_CON_DIALOG 3284 else if (State == CONFIRM) 3285 { 3286 display_confirm_msg(); // display ":confirm" message again 3287 msg_row = Rows - 1; 3288 } 3289 #endif 3290 else if (State == EXTERNCMD) 3291 { 3292 windgoto(msg_row, msg_col); // put cursor back 3293 } 3294 else if (State == HITRETURN || State == SETWSIZE) 3295 { 3296 if (msg_row == Rows - 1) 3297 { 3298 // Avoid drawing the "hit-enter" prompt below the previous one, 3299 // overwrite it. Esp. useful when regaining focus and a 3300 // FocusGained autocmd exists but didn't draw anything. 3301 msg_didout = FALSE; 3302 msg_col = 0; 3303 msg_clr_eos(); 3304 } 3305 hit_return_msg(); 3306 msg_row = Rows - 1; 3307 } 3308 } 3309 3310 /* 3311 * msg_check_screen - check if the screen is initialized. 3312 * Also check msg_row and msg_col, if they are too big it may cause a crash. 3313 * While starting the GUI the terminal codes will be set for the GUI, but the 3314 * output goes to the terminal. Don't use the terminal codes then. 3315 */ 3316 static int 3317 msg_check_screen(void) 3318 { 3319 if (!full_screen || !screen_valid(FALSE)) 3320 return FALSE; 3321 3322 if (msg_row >= Rows) 3323 msg_row = Rows - 1; 3324 if (msg_col >= Columns) 3325 msg_col = Columns - 1; 3326 return TRUE; 3327 } 3328 3329 /* 3330 * Clear from current message position to end of screen. 3331 * Skip this when ":silent" was used, no need to clear for redirection. 3332 */ 3333 void 3334 msg_clr_eos(void) 3335 { 3336 if (msg_silent == 0) 3337 msg_clr_eos_force(); 3338 } 3339 3340 /* 3341 * Clear from current message position to end of screen. 3342 * Note: msg_col is not updated, so we remember the end of the message 3343 * for msg_check(). 3344 */ 3345 void 3346 msg_clr_eos_force(void) 3347 { 3348 if (msg_use_printf()) 3349 { 3350 if (full_screen) // only when termcap codes are valid 3351 { 3352 if (*T_CD) 3353 out_str(T_CD); // clear to end of display 3354 else if (*T_CE) 3355 out_str(T_CE); // clear to end of line 3356 } 3357 } 3358 else 3359 { 3360 #ifdef FEAT_RIGHTLEFT 3361 if (cmdmsg_rl) 3362 { 3363 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0); 3364 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3365 } 3366 else 3367 #endif 3368 { 3369 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns, 3370 ' ', ' ', 0); 3371 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3372 } 3373 } 3374 } 3375 3376 /* 3377 * Clear the command line. 3378 */ 3379 void 3380 msg_clr_cmdline(void) 3381 { 3382 msg_row = cmdline_row; 3383 msg_col = 0; 3384 msg_clr_eos_force(); 3385 } 3386 3387 /* 3388 * end putting a message on the screen 3389 * call wait_return if the message does not fit in the available space 3390 * return TRUE if wait_return not called. 3391 */ 3392 int 3393 msg_end(void) 3394 { 3395 /* 3396 * If the string is larger than the window, 3397 * or the ruler option is set and we run into it, 3398 * we have to redraw the window. 3399 * Do not do this if we are abandoning the file or editing the command line. 3400 */ 3401 if (!exiting && need_wait_return && !(State & CMDLINE)) 3402 { 3403 wait_return(FALSE); 3404 return FALSE; 3405 } 3406 out_flush(); 3407 return TRUE; 3408 } 3409 3410 /* 3411 * If the written message runs into the shown command or ruler, we have to 3412 * wait for hit-return and redraw the window later. 3413 */ 3414 void 3415 msg_check(void) 3416 { 3417 if (msg_row == Rows - 1 && msg_col >= sc_col) 3418 { 3419 need_wait_return = TRUE; 3420 redraw_cmdline = TRUE; 3421 } 3422 } 3423 3424 /* 3425 * May write a string to the redirection file. 3426 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes. 3427 */ 3428 static void 3429 redir_write(char_u *str, int maxlen) 3430 { 3431 char_u *s = str; 3432 static int cur_col = 0; 3433 3434 // Don't do anything for displaying prompts and the like. 3435 if (redir_off) 3436 return; 3437 3438 // If 'verbosefile' is set prepare for writing in that file. 3439 if (*p_vfile != NUL && verbose_fd == NULL) 3440 verbose_open(); 3441 3442 if (redirecting()) 3443 { 3444 // If the string doesn't start with CR or NL, go to msg_col 3445 if (*s != '\n' && *s != '\r') 3446 { 3447 while (cur_col < msg_col) 3448 { 3449 #ifdef FEAT_EVAL 3450 if (redir_execute) 3451 execute_redir_str((char_u *)" ", -1); 3452 else if (redir_reg) 3453 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE); 3454 else if (redir_vname) 3455 var_redir_str((char_u *)" ", -1); 3456 else 3457 #endif 3458 if (redir_fd != NULL) 3459 fputs(" ", redir_fd); 3460 if (verbose_fd != NULL) 3461 fputs(" ", verbose_fd); 3462 ++cur_col; 3463 } 3464 } 3465 3466 #ifdef FEAT_EVAL 3467 if (redir_execute) 3468 execute_redir_str(s, maxlen); 3469 else if (redir_reg) 3470 write_reg_contents(redir_reg, s, maxlen, TRUE); 3471 else if (redir_vname) 3472 var_redir_str(s, maxlen); 3473 #endif 3474 3475 // Write and adjust the current column. 3476 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen)) 3477 { 3478 #ifdef FEAT_EVAL 3479 if (!redir_reg && !redir_vname && !redir_execute) 3480 #endif 3481 if (redir_fd != NULL) 3482 putc(*s, redir_fd); 3483 if (verbose_fd != NULL) 3484 putc(*s, verbose_fd); 3485 if (*s == '\r' || *s == '\n') 3486 cur_col = 0; 3487 else if (*s == '\t') 3488 cur_col += (8 - cur_col % 8); 3489 else 3490 ++cur_col; 3491 ++s; 3492 } 3493 3494 if (msg_silent != 0) // should update msg_col 3495 msg_col = cur_col; 3496 } 3497 } 3498 3499 int 3500 redirecting(void) 3501 { 3502 return redir_fd != NULL || *p_vfile != NUL 3503 #ifdef FEAT_EVAL 3504 || redir_reg || redir_vname || redir_execute 3505 #endif 3506 ; 3507 } 3508 3509 /* 3510 * Before giving verbose message. 3511 * Must always be called paired with verbose_leave()! 3512 */ 3513 void 3514 verbose_enter(void) 3515 { 3516 if (*p_vfile != NUL) 3517 ++msg_silent; 3518 } 3519 3520 /* 3521 * After giving verbose message. 3522 * Must always be called paired with verbose_enter()! 3523 */ 3524 void 3525 verbose_leave(void) 3526 { 3527 if (*p_vfile != NUL) 3528 if (--msg_silent < 0) 3529 msg_silent = 0; 3530 } 3531 3532 /* 3533 * Like verbose_enter() and set msg_scroll when displaying the message. 3534 */ 3535 void 3536 verbose_enter_scroll(void) 3537 { 3538 if (*p_vfile != NUL) 3539 ++msg_silent; 3540 else 3541 // always scroll up, don't overwrite 3542 msg_scroll = TRUE; 3543 } 3544 3545 /* 3546 * Like verbose_leave() and set cmdline_row when displaying the message. 3547 */ 3548 void 3549 verbose_leave_scroll(void) 3550 { 3551 if (*p_vfile != NUL) 3552 { 3553 if (--msg_silent < 0) 3554 msg_silent = 0; 3555 } 3556 else 3557 cmdline_row = msg_row; 3558 } 3559 3560 /* 3561 * Called when 'verbosefile' is set: stop writing to the file. 3562 */ 3563 void 3564 verbose_stop(void) 3565 { 3566 if (verbose_fd != NULL) 3567 { 3568 fclose(verbose_fd); 3569 verbose_fd = NULL; 3570 } 3571 verbose_did_open = FALSE; 3572 } 3573 3574 /* 3575 * Open the file 'verbosefile'. 3576 * Return FAIL or OK. 3577 */ 3578 int 3579 verbose_open(void) 3580 { 3581 if (verbose_fd == NULL && !verbose_did_open) 3582 { 3583 // Only give the error message once. 3584 verbose_did_open = TRUE; 3585 3586 verbose_fd = mch_fopen((char *)p_vfile, "a"); 3587 if (verbose_fd == NULL) 3588 { 3589 semsg(_(e_notopen), p_vfile); 3590 return FAIL; 3591 } 3592 } 3593 return OK; 3594 } 3595 3596 /* 3597 * Give a warning message (for searching). 3598 * Use 'w' highlighting and may repeat the message after redrawing 3599 */ 3600 void 3601 give_warning(char_u *message, int hl) 3602 { 3603 // Don't do this for ":silent". 3604 if (msg_silent != 0) 3605 return; 3606 3607 // Don't want a hit-enter prompt here. 3608 ++no_wait_return; 3609 3610 #ifdef FEAT_EVAL 3611 set_vim_var_string(VV_WARNINGMSG, message, -1); 3612 #endif 3613 VIM_CLEAR(keep_msg); 3614 if (hl) 3615 keep_msg_attr = HL_ATTR(HLF_W); 3616 else 3617 keep_msg_attr = 0; 3618 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0) 3619 set_keep_msg(message, keep_msg_attr); 3620 msg_didout = FALSE; // overwrite this message 3621 msg_nowait = TRUE; // don't wait for this message 3622 msg_col = 0; 3623 3624 --no_wait_return; 3625 } 3626 3627 #if defined(FEAT_EVAL) || defined(PROTO) 3628 void 3629 give_warning2(char_u *message, char_u *a1, int hl) 3630 { 3631 if (IObuff == NULL) 3632 { 3633 // Very early in initialisation and already something wrong, just give 3634 // the raw message so the user at least gets a hint. 3635 give_warning((char_u *)message, hl); 3636 } 3637 else 3638 { 3639 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1); 3640 give_warning(IObuff, hl); 3641 } 3642 } 3643 #endif 3644 3645 /* 3646 * Advance msg cursor to column "col". 3647 */ 3648 void 3649 msg_advance(int col) 3650 { 3651 if (msg_silent != 0) // nothing to advance to 3652 { 3653 msg_col = col; // for redirection, may fill it up later 3654 return; 3655 } 3656 if (col >= Columns) // not enough room 3657 col = Columns - 1; 3658 #ifdef FEAT_RIGHTLEFT 3659 if (cmdmsg_rl) 3660 while (msg_col > Columns - col) 3661 msg_putchar(' '); 3662 else 3663 #endif 3664 while (msg_col < col) 3665 msg_putchar(' '); 3666 } 3667 3668 #if defined(FEAT_CON_DIALOG) || defined(PROTO) 3669 /* 3670 * Used for "confirm()" function, and the :confirm command prefix. 3671 * Versions which haven't got flexible dialogs yet, and console 3672 * versions, get this generic handler which uses the command line. 3673 * 3674 * type = one of: 3675 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC 3676 * title = title string (can be NULL for default) 3677 * (neither used in console dialogs at the moment) 3678 * 3679 * Format of the "buttons" string: 3680 * "Button1Name\nButton2Name\nButton3Name" 3681 * The first button should normally be the default/accept 3682 * The second button should be the 'Cancel' button 3683 * Other buttons- use your imagination! 3684 * A '&' in a button name becomes a shortcut, so each '&' should be before a 3685 * different letter. 3686 */ 3687 int 3688 do_dialog( 3689 int type UNUSED, 3690 char_u *title UNUSED, 3691 char_u *message, 3692 char_u *buttons, 3693 int dfltbutton, 3694 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL 3695 // otherwise 3696 int ex_cmd) // when TRUE pressing : accepts default and starts 3697 // Ex command 3698 { 3699 int oldState; 3700 int retval = 0; 3701 char_u *hotkeys; 3702 int c; 3703 int i; 3704 tmode_T save_tmode; 3705 3706 #ifndef NO_CONSOLE 3707 // Don't output anything in silent mode ("ex -s") 3708 if (silent_mode) 3709 return dfltbutton; // return default option 3710 #endif 3711 3712 #ifdef FEAT_GUI_DIALOG 3713 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog 3714 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL) 3715 { 3716 c = gui_mch_dialog(type, title, message, buttons, dfltbutton, 3717 textfield, ex_cmd); 3718 // avoid a hit-enter prompt without clearing the cmdline 3719 need_wait_return = FALSE; 3720 emsg_on_display = FALSE; 3721 cmdline_row = msg_row; 3722 3723 // Flush output to avoid that further messages and redrawing is done 3724 // in the wrong order. 3725 out_flush(); 3726 gui_mch_update(); 3727 3728 return c; 3729 } 3730 #endif 3731 3732 oldState = State; 3733 State = CONFIRM; 3734 setmouse(); 3735 3736 // Ensure raw mode here. 3737 save_tmode = cur_tmode; 3738 settmode(TMODE_RAW); 3739 3740 /* 3741 * Since we wait for a keypress, don't make the 3742 * user press RETURN as well afterwards. 3743 */ 3744 ++no_wait_return; 3745 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton); 3746 3747 if (hotkeys != NULL) 3748 { 3749 for (;;) 3750 { 3751 // Get a typed character directly from the user. 3752 c = get_keystroke(); 3753 switch (c) 3754 { 3755 case CAR: // User accepts default option 3756 case NL: 3757 retval = dfltbutton; 3758 break; 3759 case Ctrl_C: // User aborts/cancels 3760 case ESC: 3761 retval = 0; 3762 break; 3763 default: // Could be a hotkey? 3764 if (c < 0) // special keys are ignored here 3765 continue; 3766 if (c == ':' && ex_cmd) 3767 { 3768 retval = dfltbutton; 3769 ins_char_typebuf(':', 0); 3770 break; 3771 } 3772 3773 // Make the character lowercase, as chars in "hotkeys" are. 3774 c = MB_TOLOWER(c); 3775 retval = 1; 3776 for (i = 0; hotkeys[i]; ++i) 3777 { 3778 if (has_mbyte) 3779 { 3780 if ((*mb_ptr2char)(hotkeys + i) == c) 3781 break; 3782 i += (*mb_ptr2len)(hotkeys + i) - 1; 3783 } 3784 else 3785 if (hotkeys[i] == c) 3786 break; 3787 ++retval; 3788 } 3789 if (hotkeys[i]) 3790 break; 3791 // No hotkey match, so keep waiting 3792 continue; 3793 } 3794 break; 3795 } 3796 3797 vim_free(hotkeys); 3798 } 3799 3800 settmode(save_tmode); 3801 State = oldState; 3802 setmouse(); 3803 --no_wait_return; 3804 msg_end_prompt(); 3805 3806 return retval; 3807 } 3808 3809 /* 3810 * Copy one character from "*from" to "*to", taking care of multi-byte 3811 * characters. Return the length of the character in bytes. 3812 */ 3813 static int 3814 copy_char( 3815 char_u *from, 3816 char_u *to, 3817 int lowercase) // make character lower case 3818 { 3819 int len; 3820 int c; 3821 3822 if (has_mbyte) 3823 { 3824 if (lowercase) 3825 { 3826 c = MB_TOLOWER((*mb_ptr2char)(from)); 3827 return (*mb_char2bytes)(c, to); 3828 } 3829 else 3830 { 3831 len = (*mb_ptr2len)(from); 3832 mch_memmove(to, from, (size_t)len); 3833 return len; 3834 } 3835 } 3836 else 3837 { 3838 if (lowercase) 3839 *to = (char_u)TOLOWER_LOC(*from); 3840 else 3841 *to = *from; 3842 return 1; 3843 } 3844 } 3845 3846 /* 3847 * Format the dialog string, and display it at the bottom of 3848 * the screen. Return a string of hotkey chars (if defined) for 3849 * each 'button'. If a button has no hotkey defined, the first character of 3850 * the button is used. 3851 * The hotkeys can be multi-byte characters, but without combining chars. 3852 * 3853 * Returns an allocated string with hotkeys, or NULL for error. 3854 */ 3855 static char_u * 3856 msg_show_console_dialog( 3857 char_u *message, 3858 char_u *buttons, 3859 int dfltbutton) 3860 { 3861 int len = 0; 3862 #define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1) 3863 int lenhotkey = HOTK_LEN; // count first button 3864 char_u *hotk = NULL; 3865 char_u *msgp = NULL; 3866 char_u *hotkp = NULL; 3867 char_u *r; 3868 int copy; 3869 #define HAS_HOTKEY_LEN 30 3870 char_u has_hotkey[HAS_HOTKEY_LEN]; 3871 int first_hotkey = FALSE; // first char of button is hotkey 3872 int idx; 3873 3874 has_hotkey[0] = FALSE; 3875 3876 /* 3877 * First loop: compute the size of memory to allocate. 3878 * Second loop: copy to the allocated memory. 3879 */ 3880 for (copy = 0; copy <= 1; ++copy) 3881 { 3882 r = buttons; 3883 idx = 0; 3884 while (*r) 3885 { 3886 if (*r == DLG_BUTTON_SEP) 3887 { 3888 if (copy) 3889 { 3890 *msgp++ = ','; 3891 *msgp++ = ' '; // '\n' -> ', ' 3892 3893 // advance to next hotkey and set default hotkey 3894 if (has_mbyte) 3895 hotkp += STRLEN(hotkp); 3896 else 3897 ++hotkp; 3898 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL; 3899 if (dfltbutton) 3900 --dfltbutton; 3901 3902 // If no hotkey is specified first char is used. 3903 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx]) 3904 first_hotkey = TRUE; 3905 } 3906 else 3907 { 3908 len += 3; // '\n' -> ', '; 'x' -> '(x)' 3909 lenhotkey += HOTK_LEN; // each button needs a hotkey 3910 if (idx < HAS_HOTKEY_LEN - 1) 3911 has_hotkey[++idx] = FALSE; 3912 } 3913 } 3914 else if (*r == DLG_HOTKEY_CHAR || first_hotkey) 3915 { 3916 if (*r == DLG_HOTKEY_CHAR) 3917 ++r; 3918 first_hotkey = FALSE; 3919 if (copy) 3920 { 3921 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a' 3922 *msgp++ = *r; 3923 else 3924 { 3925 // '&a' -> '[a]' 3926 *msgp++ = (dfltbutton == 1) ? '[' : '('; 3927 msgp += copy_char(r, msgp, FALSE); 3928 *msgp++ = (dfltbutton == 1) ? ']' : ')'; 3929 3930 // redefine hotkey 3931 hotkp[copy_char(r, hotkp, TRUE)] = NUL; 3932 } 3933 } 3934 else 3935 { 3936 ++len; // '&a' -> '[a]' 3937 if (idx < HAS_HOTKEY_LEN - 1) 3938 has_hotkey[idx] = TRUE; 3939 } 3940 } 3941 else 3942 { 3943 // everything else copy literally 3944 if (copy) 3945 msgp += copy_char(r, msgp, FALSE); 3946 } 3947 3948 // advance to the next character 3949 MB_PTR_ADV(r); 3950 } 3951 3952 if (copy) 3953 { 3954 *msgp++ = ':'; 3955 *msgp++ = ' '; 3956 *msgp = NUL; 3957 } 3958 else 3959 { 3960 len += (int)(STRLEN(message) 3961 + 2 // for the NL's 3962 + STRLEN(buttons) 3963 + 3); // for the ": " and NUL 3964 lenhotkey++; // for the NUL 3965 3966 // If no hotkey is specified first char is used. 3967 if (!has_hotkey[0]) 3968 { 3969 first_hotkey = TRUE; 3970 len += 2; // "x" -> "[x]" 3971 } 3972 3973 /* 3974 * Now allocate and load the strings 3975 */ 3976 vim_free(confirm_msg); 3977 confirm_msg = alloc(len); 3978 if (confirm_msg == NULL) 3979 return NULL; 3980 *confirm_msg = NUL; 3981 hotk = alloc(lenhotkey); 3982 if (hotk == NULL) 3983 return NULL; 3984 3985 *confirm_msg = '\n'; 3986 STRCPY(confirm_msg + 1, message); 3987 3988 msgp = confirm_msg + 1 + STRLEN(message); 3989 hotkp = hotk; 3990 3991 // Define first default hotkey. Keep the hotkey string NUL 3992 // terminated to avoid reading past the end. 3993 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL; 3994 3995 // Remember where the choices start, displaying starts here when 3996 // "hotkp" typed at the more prompt. 3997 confirm_msg_tail = msgp; 3998 *msgp++ = '\n'; 3999 } 4000 } 4001 4002 display_confirm_msg(); 4003 return hotk; 4004 } 4005 4006 /* 4007 * Display the ":confirm" message. Also called when screen resized. 4008 */ 4009 static void 4010 display_confirm_msg(void) 4011 { 4012 // avoid that 'q' at the more prompt truncates the message here 4013 ++confirm_msg_used; 4014 if (confirm_msg != NULL) 4015 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M)); 4016 --confirm_msg_used; 4017 } 4018 4019 #endif // FEAT_CON_DIALOG 4020 4021 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) 4022 4023 int 4024 vim_dialog_yesno( 4025 int type, 4026 char_u *title, 4027 char_u *message, 4028 int dflt) 4029 { 4030 if (do_dialog(type, 4031 title == NULL ? (char_u *)_("Question") : title, 4032 message, 4033 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1) 4034 return VIM_YES; 4035 return VIM_NO; 4036 } 4037 4038 int 4039 vim_dialog_yesnocancel( 4040 int type, 4041 char_u *title, 4042 char_u *message, 4043 int dflt) 4044 { 4045 switch (do_dialog(type, 4046 title == NULL ? (char_u *)_("Question") : title, 4047 message, 4048 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE)) 4049 { 4050 case 1: return VIM_YES; 4051 case 2: return VIM_NO; 4052 } 4053 return VIM_CANCEL; 4054 } 4055 4056 int 4057 vim_dialog_yesnoallcancel( 4058 int type, 4059 char_u *title, 4060 char_u *message, 4061 int dflt) 4062 { 4063 switch (do_dialog(type, 4064 title == NULL ? (char_u *)"Question" : title, 4065 message, 4066 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"), 4067 dflt, NULL, FALSE)) 4068 { 4069 case 1: return VIM_YES; 4070 case 2: return VIM_NO; 4071 case 3: return VIM_ALL; 4072 case 4: return VIM_DISCARDALL; 4073 } 4074 return VIM_CANCEL; 4075 } 4076 4077 #endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG 4078 4079 #if defined(FEAT_EVAL) 4080 static char *e_printf = N_("E766: Insufficient arguments for printf()"); 4081 4082 /* 4083 * Get number argument from "idxp" entry in "tvs". First entry is 1. 4084 */ 4085 static varnumber_T 4086 tv_nr(typval_T *tvs, int *idxp) 4087 { 4088 int idx = *idxp - 1; 4089 varnumber_T n = 0; 4090 int err = FALSE; 4091 4092 if (tvs[idx].v_type == VAR_UNKNOWN) 4093 emsg(_(e_printf)); 4094 else 4095 { 4096 ++*idxp; 4097 n = tv_get_number_chk(&tvs[idx], &err); 4098 if (err) 4099 n = 0; 4100 } 4101 return n; 4102 } 4103 4104 /* 4105 * Get string argument from "idxp" entry in "tvs". First entry is 1. 4106 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List) 4107 * are not converted to a string. 4108 * If "tofree" is not NULL echo_string() is used. All types are converted to 4109 * a string with the same format as ":echo". The caller must free "*tofree". 4110 * Returns NULL for an error. 4111 */ 4112 static char * 4113 tv_str(typval_T *tvs, int *idxp, char_u **tofree) 4114 { 4115 int idx = *idxp - 1; 4116 char *s = NULL; 4117 static char_u numbuf[NUMBUFLEN]; 4118 4119 if (tvs[idx].v_type == VAR_UNKNOWN) 4120 emsg(_(e_printf)); 4121 else 4122 { 4123 ++*idxp; 4124 if (tofree != NULL) 4125 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID()); 4126 else 4127 s = (char *)tv_get_string_chk(&tvs[idx]); 4128 } 4129 return s; 4130 } 4131 4132 # ifdef FEAT_FLOAT 4133 /* 4134 * Get float argument from "idxp" entry in "tvs". First entry is 1. 4135 */ 4136 static double 4137 tv_float(typval_T *tvs, int *idxp) 4138 { 4139 int idx = *idxp - 1; 4140 double f = 0; 4141 4142 if (tvs[idx].v_type == VAR_UNKNOWN) 4143 emsg(_(e_printf)); 4144 else 4145 { 4146 ++*idxp; 4147 if (tvs[idx].v_type == VAR_FLOAT) 4148 f = tvs[idx].vval.v_float; 4149 else if (tvs[idx].v_type == VAR_NUMBER) 4150 f = (double)tvs[idx].vval.v_number; 4151 else 4152 emsg(_("E807: Expected Float argument for printf()")); 4153 } 4154 return f; 4155 } 4156 # endif 4157 #endif 4158 4159 #ifdef FEAT_FLOAT 4160 /* 4161 * Return the representation of infinity for printf() function: 4162 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF". 4163 */ 4164 static const char * 4165 infinity_str(int positive, 4166 char fmt_spec, 4167 int force_sign, 4168 int space_for_positive) 4169 { 4170 static const char *table[] = 4171 { 4172 "-inf", "inf", "+inf", " inf", 4173 "-INF", "INF", "+INF", " INF" 4174 }; 4175 int idx = positive * (1 + force_sign + force_sign * space_for_positive); 4176 4177 if (ASCII_ISUPPER(fmt_spec)) 4178 idx += 4; 4179 return table[idx]; 4180 } 4181 #endif 4182 4183 /* 4184 * This code was included to provide a portable vsnprintf() and snprintf(). 4185 * Some systems may provide their own, but we always use this one for 4186 * consistency. 4187 * 4188 * This code is based on snprintf.c - a portable implementation of snprintf 4189 * by Mark Martinec <[email protected]>, Version 2.2, 2000-10-06. 4190 * Included with permission. It was heavily modified to fit in Vim. 4191 * The original code, including useful comments, can be found here: 4192 * http://www.ijs.si/software/snprintf/ 4193 * 4194 * This snprintf() only supports the following conversion specifiers: 4195 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below) 4196 * with flags: '-', '+', ' ', '0' and '#'. 4197 * An asterisk is supported for field width as well as precision. 4198 * 4199 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'. 4200 * 4201 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int) 4202 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T. 4203 * 4204 * The locale is not used, the string is used as a byte string. This is only 4205 * relevant for double-byte encodings where the second byte may be '%'. 4206 * 4207 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL 4208 * pointer for resulting string argument if "str_m" is zero (as per ISO C99). 4209 * 4210 * The return value is the number of characters which would be generated 4211 * for the given input, excluding the trailing NUL. If this value 4212 * is greater or equal to "str_m", not all characters from the result 4213 * have been stored in str, output bytes beyond the ("str_m"-1) -th character 4214 * are discarded. If "str_m" is greater than zero it is guaranteed 4215 * the resulting string will be NUL-terminated. 4216 */ 4217 4218 /* 4219 * When va_list is not supported we only define vim_snprintf(). 4220 * 4221 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of 4222 * "typval_T". When the latter is not used it must be NULL. 4223 */ 4224 4225 // When generating prototypes all of this is skipped, cproto doesn't 4226 // understand this. 4227 #ifndef PROTO 4228 4229 // Like vim_vsnprintf() but append to the string. 4230 int 4231 vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...) 4232 { 4233 va_list ap; 4234 int str_l; 4235 size_t len = STRLEN(str); 4236 size_t space; 4237 4238 if (str_m <= len) 4239 space = 0; 4240 else 4241 space = str_m - len; 4242 va_start(ap, fmt); 4243 str_l = vim_vsnprintf(str + len, space, fmt, ap); 4244 va_end(ap); 4245 return str_l; 4246 } 4247 4248 int 4249 vim_snprintf(char *str, size_t str_m, const char *fmt, ...) 4250 { 4251 va_list ap; 4252 int str_l; 4253 4254 va_start(ap, fmt); 4255 str_l = vim_vsnprintf(str, str_m, fmt, ap); 4256 va_end(ap); 4257 return str_l; 4258 } 4259 4260 int 4261 vim_vsnprintf( 4262 char *str, 4263 size_t str_m, 4264 const char *fmt, 4265 va_list ap) 4266 { 4267 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL); 4268 } 4269 4270 int 4271 vim_vsnprintf_typval( 4272 char *str, 4273 size_t str_m, 4274 const char *fmt, 4275 va_list ap, 4276 typval_T *tvs) 4277 { 4278 size_t str_l = 0; 4279 const char *p = fmt; 4280 int arg_idx = 1; 4281 4282 if (p == NULL) 4283 p = ""; 4284 while (*p != NUL) 4285 { 4286 if (*p != '%') 4287 { 4288 char *q = strchr(p + 1, '%'); 4289 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p); 4290 4291 // Copy up to the next '%' or NUL without any changes. 4292 if (str_l < str_m) 4293 { 4294 size_t avail = str_m - str_l; 4295 4296 mch_memmove(str + str_l, p, n > avail ? avail : n); 4297 } 4298 p += n; 4299 str_l += n; 4300 } 4301 else 4302 { 4303 size_t min_field_width = 0, precision = 0; 4304 int zero_padding = 0, precision_specified = 0, justify_left = 0; 4305 int alternate_form = 0, force_sign = 0; 4306 4307 // If both the ' ' and '+' flags appear, the ' ' flag should be 4308 // ignored. 4309 int space_for_positive = 1; 4310 4311 // allowed values: \0, h, l, L 4312 char length_modifier = '\0'; 4313 4314 // temporary buffer for simple numeric->string conversion 4315 # if defined(FEAT_FLOAT) 4316 # define TMP_LEN 350 // On my system 1e308 is the biggest number possible. 4317 // That sounds reasonable to use as the maximum 4318 // printable. 4319 # else 4320 # define TMP_LEN 66 4321 # endif 4322 char tmp[TMP_LEN]; 4323 4324 // string address in case of string argument 4325 const char *str_arg = NULL; 4326 4327 // natural field width of arg without padding and sign 4328 size_t str_arg_l; 4329 4330 // unsigned char argument value - only defined for c conversion. 4331 // N.B. standard explicitly states the char argument for the c 4332 // conversion is unsigned 4333 unsigned char uchar_arg; 4334 4335 // number of zeros to be inserted for numeric conversions as 4336 // required by the precision or minimal field width 4337 size_t number_of_zeros_to_pad = 0; 4338 4339 // index into tmp where zero padding is to be inserted 4340 size_t zero_padding_insertion_ind = 0; 4341 4342 // current conversion specifier character 4343 char fmt_spec = '\0'; 4344 4345 // buffer for 's' and 'S' specs 4346 char_u *tofree = NULL; 4347 4348 4349 p++; // skip '%' 4350 4351 // parse flags 4352 while (*p == '0' || *p == '-' || *p == '+' || *p == ' ' 4353 || *p == '#' || *p == '\'') 4354 { 4355 switch (*p) 4356 { 4357 case '0': zero_padding = 1; break; 4358 case '-': justify_left = 1; break; 4359 case '+': force_sign = 1; space_for_positive = 0; break; 4360 case ' ': force_sign = 1; 4361 // If both the ' ' and '+' flags appear, the ' ' 4362 // flag should be ignored 4363 break; 4364 case '#': alternate_form = 1; break; 4365 case '\'': break; 4366 } 4367 p++; 4368 } 4369 // If the '0' and '-' flags both appear, the '0' flag should be 4370 // ignored. 4371 4372 // parse field width 4373 if (*p == '*') 4374 { 4375 int j; 4376 4377 p++; 4378 j = 4379 # if defined(FEAT_EVAL) 4380 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4381 # endif 4382 va_arg(ap, int); 4383 if (j >= 0) 4384 min_field_width = j; 4385 else 4386 { 4387 min_field_width = -j; 4388 justify_left = 1; 4389 } 4390 } 4391 else if (VIM_ISDIGIT((int)(*p))) 4392 { 4393 // size_t could be wider than unsigned int; make sure we treat 4394 // argument like common implementations do 4395 unsigned int uj = *p++ - '0'; 4396 4397 while (VIM_ISDIGIT((int)(*p))) 4398 uj = 10 * uj + (unsigned int)(*p++ - '0'); 4399 min_field_width = uj; 4400 } 4401 4402 // parse precision 4403 if (*p == '.') 4404 { 4405 p++; 4406 precision_specified = 1; 4407 if (*p == '*') 4408 { 4409 int j; 4410 4411 j = 4412 # if defined(FEAT_EVAL) 4413 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4414 # endif 4415 va_arg(ap, int); 4416 p++; 4417 if (j >= 0) 4418 precision = j; 4419 else 4420 { 4421 precision_specified = 0; 4422 precision = 0; 4423 } 4424 } 4425 else if (VIM_ISDIGIT((int)(*p))) 4426 { 4427 // size_t could be wider than unsigned int; make sure we 4428 // treat argument like common implementations do 4429 unsigned int uj = *p++ - '0'; 4430 4431 while (VIM_ISDIGIT((int)(*p))) 4432 uj = 10 * uj + (unsigned int)(*p++ - '0'); 4433 precision = uj; 4434 } 4435 } 4436 4437 // parse 'h', 'l' and 'll' length modifiers 4438 if (*p == 'h' || *p == 'l') 4439 { 4440 length_modifier = *p; 4441 p++; 4442 if (length_modifier == 'l' && *p == 'l') 4443 { 4444 // double l = __int64 / varnumber_T 4445 length_modifier = 'L'; 4446 p++; 4447 } 4448 } 4449 fmt_spec = *p; 4450 4451 // common synonyms: 4452 switch (fmt_spec) 4453 { 4454 case 'i': fmt_spec = 'd'; break; 4455 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break; 4456 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break; 4457 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break; 4458 default: break; 4459 } 4460 4461 # if defined(FEAT_EVAL) 4462 switch (fmt_spec) 4463 { 4464 case 'd': case 'u': case 'o': case 'x': case 'X': 4465 if (tvs != NULL && length_modifier == '\0') 4466 length_modifier = 'L'; 4467 } 4468 # endif 4469 4470 // get parameter value, do initial processing 4471 switch (fmt_spec) 4472 { 4473 // '%' and 'c' behave similar to 's' regarding flags and field 4474 // widths 4475 case '%': 4476 case 'c': 4477 case 's': 4478 case 'S': 4479 str_arg_l = 1; 4480 switch (fmt_spec) 4481 { 4482 case '%': 4483 str_arg = p; 4484 break; 4485 4486 case 'c': 4487 { 4488 int j; 4489 4490 j = 4491 # if defined(FEAT_EVAL) 4492 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4493 # endif 4494 va_arg(ap, int); 4495 // standard demands unsigned char 4496 uchar_arg = (unsigned char)j; 4497 str_arg = (char *)&uchar_arg; 4498 break; 4499 } 4500 4501 case 's': 4502 case 'S': 4503 str_arg = 4504 # if defined(FEAT_EVAL) 4505 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) : 4506 # endif 4507 va_arg(ap, char *); 4508 if (str_arg == NULL) 4509 { 4510 str_arg = "[NULL]"; 4511 str_arg_l = 6; 4512 } 4513 // make sure not to address string beyond the specified 4514 // precision !!! 4515 else if (!precision_specified) 4516 str_arg_l = strlen(str_arg); 4517 // truncate string if necessary as requested by precision 4518 else if (precision == 0) 4519 str_arg_l = 0; 4520 else 4521 { 4522 // Don't put the #if inside memchr(), it can be a 4523 // macro. 4524 // memchr on HP does not like n > 2^31 !!! 4525 char *q = memchr(str_arg, '\0', 4526 precision <= (size_t)0x7fffffffL ? precision 4527 : (size_t)0x7fffffffL); 4528 str_arg_l = (q == NULL) ? precision 4529 : (size_t)(q - str_arg); 4530 } 4531 if (fmt_spec == 'S') 4532 { 4533 if (min_field_width != 0) 4534 min_field_width += STRLEN(str_arg) 4535 - mb_string2cells((char_u *)str_arg, -1); 4536 if (precision) 4537 { 4538 char_u *p1; 4539 size_t i = 0; 4540 4541 for (p1 = (char_u *)str_arg; *p1; 4542 p1 += mb_ptr2len(p1)) 4543 { 4544 i += (size_t)mb_ptr2cells(p1); 4545 if (i > precision) 4546 break; 4547 } 4548 str_arg_l = precision = p1 - (char_u *)str_arg; 4549 } 4550 } 4551 break; 4552 4553 default: 4554 break; 4555 } 4556 break; 4557 4558 case 'd': case 'u': 4559 case 'b': case 'B': 4560 case 'o': 4561 case 'x': case 'X': 4562 case 'p': 4563 { 4564 // NOTE: the u, b, o, x, X and p conversion specifiers 4565 // imply the value is unsigned; d implies a signed 4566 // value 4567 4568 // 0 if numeric argument is zero (or if pointer is 4569 // NULL for 'p'), +1 if greater than zero (or nonzero 4570 // for unsigned arguments), -1 if negative (unsigned 4571 // argument is never negative) 4572 int arg_sign = 0; 4573 4574 // only set for length modifier h, or for no length 4575 // modifiers 4576 int int_arg = 0; 4577 unsigned int uint_arg = 0; 4578 4579 // only set for length modifier l 4580 long int long_arg = 0; 4581 unsigned long int ulong_arg = 0; 4582 4583 // only set for length modifier ll 4584 varnumber_T llong_arg = 0; 4585 uvarnumber_T ullong_arg = 0; 4586 4587 // only set for b conversion 4588 uvarnumber_T bin_arg = 0; 4589 4590 // pointer argument value -only defined for p 4591 // conversion 4592 void *ptr_arg = NULL; 4593 4594 if (fmt_spec == 'p') 4595 { 4596 length_modifier = '\0'; 4597 ptr_arg = 4598 # if defined(FEAT_EVAL) 4599 tvs != NULL ? (void *)tv_str(tvs, &arg_idx, 4600 NULL) : 4601 # endif 4602 va_arg(ap, void *); 4603 if (ptr_arg != NULL) 4604 arg_sign = 1; 4605 } 4606 else if (fmt_spec == 'b' || fmt_spec == 'B') 4607 { 4608 bin_arg = 4609 # if defined(FEAT_EVAL) 4610 tvs != NULL ? 4611 (uvarnumber_T)tv_nr(tvs, &arg_idx) : 4612 # endif 4613 va_arg(ap, uvarnumber_T); 4614 if (bin_arg != 0) 4615 arg_sign = 1; 4616 } 4617 else if (fmt_spec == 'd') 4618 { 4619 // signed 4620 switch (length_modifier) 4621 { 4622 case '\0': 4623 case 'h': 4624 // char and short arguments are passed as int. 4625 int_arg = 4626 # if defined(FEAT_EVAL) 4627 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4628 # endif 4629 va_arg(ap, int); 4630 if (int_arg > 0) 4631 arg_sign = 1; 4632 else if (int_arg < 0) 4633 arg_sign = -1; 4634 break; 4635 case 'l': 4636 long_arg = 4637 # if defined(FEAT_EVAL) 4638 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4639 # endif 4640 va_arg(ap, long int); 4641 if (long_arg > 0) 4642 arg_sign = 1; 4643 else if (long_arg < 0) 4644 arg_sign = -1; 4645 break; 4646 case 'L': 4647 llong_arg = 4648 # if defined(FEAT_EVAL) 4649 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4650 # endif 4651 va_arg(ap, varnumber_T); 4652 if (llong_arg > 0) 4653 arg_sign = 1; 4654 else if (llong_arg < 0) 4655 arg_sign = -1; 4656 break; 4657 } 4658 } 4659 else 4660 { 4661 // unsigned 4662 switch (length_modifier) 4663 { 4664 case '\0': 4665 case 'h': 4666 uint_arg = 4667 # if defined(FEAT_EVAL) 4668 tvs != NULL ? (unsigned) 4669 tv_nr(tvs, &arg_idx) : 4670 # endif 4671 va_arg(ap, unsigned int); 4672 if (uint_arg != 0) 4673 arg_sign = 1; 4674 break; 4675 case 'l': 4676 ulong_arg = 4677 # if defined(FEAT_EVAL) 4678 tvs != NULL ? (unsigned long) 4679 tv_nr(tvs, &arg_idx) : 4680 # endif 4681 va_arg(ap, unsigned long int); 4682 if (ulong_arg != 0) 4683 arg_sign = 1; 4684 break; 4685 case 'L': 4686 ullong_arg = 4687 # if defined(FEAT_EVAL) 4688 tvs != NULL ? (uvarnumber_T) 4689 tv_nr(tvs, &arg_idx) : 4690 # endif 4691 va_arg(ap, uvarnumber_T); 4692 if (ullong_arg != 0) 4693 arg_sign = 1; 4694 break; 4695 } 4696 } 4697 4698 str_arg = tmp; 4699 str_arg_l = 0; 4700 4701 // NOTE: 4702 // For d, i, u, o, x, and X conversions, if precision is 4703 // specified, the '0' flag should be ignored. This is so 4704 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux, 4705 // FreeBSD, NetBSD; but not with Perl. 4706 if (precision_specified) 4707 zero_padding = 0; 4708 if (fmt_spec == 'd') 4709 { 4710 if (force_sign && arg_sign >= 0) 4711 tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; 4712 // leave negative numbers for sprintf to handle, to 4713 // avoid handling tricky cases like (short int)-32768 4714 } 4715 else if (alternate_form) 4716 { 4717 if (arg_sign != 0 4718 && (fmt_spec == 'b' || fmt_spec == 'B' 4719 || fmt_spec == 'x' || fmt_spec == 'X') ) 4720 { 4721 tmp[str_arg_l++] = '0'; 4722 tmp[str_arg_l++] = fmt_spec; 4723 } 4724 // alternate form should have no effect for p 4725 // conversion, but ... 4726 } 4727 4728 zero_padding_insertion_ind = str_arg_l; 4729 if (!precision_specified) 4730 precision = 1; // default precision is 1 4731 if (precision == 0 && arg_sign == 0) 4732 { 4733 // When zero value is formatted with an explicit 4734 // precision 0, the resulting formatted string is 4735 // empty (d, i, u, b, B, o, x, X, p). 4736 } 4737 else 4738 { 4739 char f[6]; 4740 int f_l = 0; 4741 4742 // construct a simple format string for sprintf 4743 f[f_l++] = '%'; 4744 if (!length_modifier) 4745 ; 4746 else if (length_modifier == 'L') 4747 { 4748 # ifdef MSWIN 4749 f[f_l++] = 'I'; 4750 f[f_l++] = '6'; 4751 f[f_l++] = '4'; 4752 # else 4753 f[f_l++] = 'l'; 4754 f[f_l++] = 'l'; 4755 # endif 4756 } 4757 else 4758 f[f_l++] = length_modifier; 4759 f[f_l++] = fmt_spec; 4760 f[f_l++] = '\0'; 4761 4762 if (fmt_spec == 'p') 4763 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg); 4764 else if (fmt_spec == 'b' || fmt_spec == 'B') 4765 { 4766 char b[8 * sizeof(uvarnumber_T)]; 4767 size_t b_l = 0; 4768 uvarnumber_T bn = bin_arg; 4769 4770 do 4771 { 4772 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1); 4773 bn >>= 1; 4774 } 4775 while (bn != 0); 4776 4777 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l); 4778 str_arg_l += b_l; 4779 } 4780 else if (fmt_spec == 'd') 4781 { 4782 // signed 4783 switch (length_modifier) 4784 { 4785 case '\0': str_arg_l += sprintf( 4786 tmp + str_arg_l, f, 4787 int_arg); 4788 break; 4789 case 'h': str_arg_l += sprintf( 4790 tmp + str_arg_l, f, 4791 (short)int_arg); 4792 break; 4793 case 'l': str_arg_l += sprintf( 4794 tmp + str_arg_l, f, long_arg); 4795 break; 4796 case 'L': str_arg_l += sprintf( 4797 tmp + str_arg_l, f, llong_arg); 4798 break; 4799 } 4800 } 4801 else 4802 { 4803 // unsigned 4804 switch (length_modifier) 4805 { 4806 case '\0': str_arg_l += sprintf( 4807 tmp + str_arg_l, f, 4808 uint_arg); 4809 break; 4810 case 'h': str_arg_l += sprintf( 4811 tmp + str_arg_l, f, 4812 (unsigned short)uint_arg); 4813 break; 4814 case 'l': str_arg_l += sprintf( 4815 tmp + str_arg_l, f, ulong_arg); 4816 break; 4817 case 'L': str_arg_l += sprintf( 4818 tmp + str_arg_l, f, ullong_arg); 4819 break; 4820 } 4821 } 4822 4823 // include the optional minus sign and possible 4824 // "0x" in the region before the zero padding 4825 // insertion point 4826 if (zero_padding_insertion_ind < str_arg_l 4827 && tmp[zero_padding_insertion_ind] == '-') 4828 zero_padding_insertion_ind++; 4829 if (zero_padding_insertion_ind + 1 < str_arg_l 4830 && tmp[zero_padding_insertion_ind] == '0' 4831 && (tmp[zero_padding_insertion_ind + 1] == 'x' 4832 || tmp[zero_padding_insertion_ind + 1] == 'X')) 4833 zero_padding_insertion_ind += 2; 4834 } 4835 4836 { 4837 size_t num_of_digits = str_arg_l 4838 - zero_padding_insertion_ind; 4839 4840 if (alternate_form && fmt_spec == 'o' 4841 // unless zero is already the first 4842 // character 4843 && !(zero_padding_insertion_ind < str_arg_l 4844 && tmp[zero_padding_insertion_ind] == '0')) 4845 { 4846 // assure leading zero for alternate-form 4847 // octal numbers 4848 if (!precision_specified 4849 || precision < num_of_digits + 1) 4850 { 4851 // precision is increased to force the 4852 // first character to be zero, except if a 4853 // zero value is formatted with an 4854 // explicit precision of zero 4855 precision = num_of_digits + 1; 4856 } 4857 } 4858 // zero padding to specified precision? 4859 if (num_of_digits < precision) 4860 number_of_zeros_to_pad = precision - num_of_digits; 4861 } 4862 // zero padding to specified minimal field width? 4863 if (!justify_left && zero_padding) 4864 { 4865 int n = (int)(min_field_width - (str_arg_l 4866 + number_of_zeros_to_pad)); 4867 if (n > 0) 4868 number_of_zeros_to_pad += n; 4869 } 4870 break; 4871 } 4872 4873 # ifdef FEAT_FLOAT 4874 case 'f': 4875 case 'F': 4876 case 'e': 4877 case 'E': 4878 case 'g': 4879 case 'G': 4880 { 4881 // Floating point. 4882 double f; 4883 double abs_f; 4884 char format[40]; 4885 int l; 4886 int remove_trailing_zeroes = FALSE; 4887 4888 f = 4889 # if defined(FEAT_EVAL) 4890 tvs != NULL ? tv_float(tvs, &arg_idx) : 4891 # endif 4892 va_arg(ap, double); 4893 abs_f = f < 0 ? -f : f; 4894 4895 if (fmt_spec == 'g' || fmt_spec == 'G') 4896 { 4897 // Would be nice to use %g directly, but it prints 4898 // "1.0" as "1", we don't want that. 4899 if ((abs_f >= 0.001 && abs_f < 10000000.0) 4900 || abs_f == 0.0) 4901 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f'; 4902 else 4903 fmt_spec = fmt_spec == 'g' ? 'e' : 'E'; 4904 remove_trailing_zeroes = TRUE; 4905 } 4906 4907 if ((fmt_spec == 'f' || fmt_spec == 'F') && 4908 # ifdef VAX 4909 abs_f > 1.0e38 4910 # else 4911 abs_f > 1.0e307 4912 # endif 4913 ) 4914 { 4915 // Avoid a buffer overflow 4916 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, 4917 force_sign, space_for_positive)); 4918 str_arg_l = STRLEN(tmp); 4919 zero_padding = 0; 4920 } 4921 else 4922 { 4923 if (isnan(f)) 4924 { 4925 // Not a number: nan or NAN 4926 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN" 4927 : "nan"); 4928 str_arg_l = 3; 4929 zero_padding = 0; 4930 } 4931 else if (isinf(f)) 4932 { 4933 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, 4934 force_sign, space_for_positive)); 4935 str_arg_l = STRLEN(tmp); 4936 zero_padding = 0; 4937 } 4938 else 4939 { 4940 // Regular float number 4941 format[0] = '%'; 4942 l = 1; 4943 if (force_sign) 4944 format[l++] = space_for_positive ? ' ' : '+'; 4945 if (precision_specified) 4946 { 4947 size_t max_prec = TMP_LEN - 10; 4948 4949 // Make sure we don't get more digits than we 4950 // have room for. 4951 if ((fmt_spec == 'f' || fmt_spec == 'F') 4952 && abs_f > 1.0) 4953 max_prec -= (size_t)log10(abs_f); 4954 if (precision > max_prec) 4955 precision = max_prec; 4956 l += sprintf(format + l, ".%d", (int)precision); 4957 } 4958 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec; 4959 format[l + 1] = NUL; 4960 4961 str_arg_l = sprintf(tmp, format, f); 4962 } 4963 4964 if (remove_trailing_zeroes) 4965 { 4966 int i; 4967 char *tp; 4968 4969 // Using %g or %G: remove superfluous zeroes. 4970 if (fmt_spec == 'f' || fmt_spec == 'F') 4971 tp = tmp + str_arg_l - 1; 4972 else 4973 { 4974 tp = (char *)vim_strchr((char_u *)tmp, 4975 fmt_spec == 'e' ? 'e' : 'E'); 4976 if (tp != NULL) 4977 { 4978 // Remove superfluous '+' and leading 4979 // zeroes from the exponent. 4980 if (tp[1] == '+') 4981 { 4982 // Change "1.0e+07" to "1.0e07" 4983 STRMOVE(tp + 1, tp + 2); 4984 --str_arg_l; 4985 } 4986 i = (tp[1] == '-') ? 2 : 1; 4987 while (tp[i] == '0') 4988 { 4989 // Change "1.0e07" to "1.0e7" 4990 STRMOVE(tp + i, tp + i + 1); 4991 --str_arg_l; 4992 } 4993 --tp; 4994 } 4995 } 4996 4997 if (tp != NULL && !precision_specified) 4998 // Remove trailing zeroes, but keep the one 4999 // just after a dot. 5000 while (tp > tmp + 2 && *tp == '0' 5001 && tp[-1] != '.') 5002 { 5003 STRMOVE(tp, tp + 1); 5004 --tp; 5005 --str_arg_l; 5006 } 5007 } 5008 else 5009 { 5010 char *tp; 5011 5012 // Be consistent: some printf("%e") use 1.0e+12 5013 // and some 1.0e+012. Remove one zero in the last 5014 // case. 5015 tp = (char *)vim_strchr((char_u *)tmp, 5016 fmt_spec == 'e' ? 'e' : 'E'); 5017 if (tp != NULL && (tp[1] == '+' || tp[1] == '-') 5018 && tp[2] == '0' 5019 && vim_isdigit(tp[3]) 5020 && vim_isdigit(tp[4])) 5021 { 5022 STRMOVE(tp + 2, tp + 3); 5023 --str_arg_l; 5024 } 5025 } 5026 } 5027 if (zero_padding && min_field_width > str_arg_l 5028 && (tmp[0] == '-' || force_sign)) 5029 { 5030 // padding 0's should be inserted after the sign 5031 number_of_zeros_to_pad = min_field_width - str_arg_l; 5032 zero_padding_insertion_ind = 1; 5033 } 5034 str_arg = tmp; 5035 break; 5036 } 5037 # endif 5038 5039 default: 5040 // unrecognized conversion specifier, keep format string 5041 // as-is 5042 zero_padding = 0; // turn zero padding off for non-numeric 5043 // conversion 5044 justify_left = 1; 5045 min_field_width = 0; // reset flags 5046 5047 // discard the unrecognized conversion, just keep * 5048 // the unrecognized conversion character 5049 str_arg = p; 5050 str_arg_l = 0; 5051 if (*p != NUL) 5052 str_arg_l++; // include invalid conversion specifier 5053 // unchanged if not at end-of-string 5054 break; 5055 } 5056 5057 if (*p != NUL) 5058 p++; // step over the just processed conversion specifier 5059 5060 // insert padding to the left as requested by min_field_width; 5061 // this does not include the zero padding in case of numerical 5062 // conversions 5063 if (!justify_left) 5064 { 5065 // left padding with blank or zero 5066 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad)); 5067 5068 if (pn > 0) 5069 { 5070 if (str_l < str_m) 5071 { 5072 size_t avail = str_m - str_l; 5073 5074 vim_memset(str + str_l, zero_padding ? '0' : ' ', 5075 (size_t)pn > avail ? avail 5076 : (size_t)pn); 5077 } 5078 str_l += pn; 5079 } 5080 } 5081 5082 // zero padding as requested by the precision or by the minimal 5083 // field width for numeric conversions required? 5084 if (number_of_zeros_to_pad == 0) 5085 { 5086 // will not copy first part of numeric right now, * 5087 // force it to be copied later in its entirety 5088 zero_padding_insertion_ind = 0; 5089 } 5090 else 5091 { 5092 // insert first part of numerics (sign or '0x') before zero 5093 // padding 5094 int zn = (int)zero_padding_insertion_ind; 5095 5096 if (zn > 0) 5097 { 5098 if (str_l < str_m) 5099 { 5100 size_t avail = str_m - str_l; 5101 5102 mch_memmove(str + str_l, str_arg, 5103 (size_t)zn > avail ? avail 5104 : (size_t)zn); 5105 } 5106 str_l += zn; 5107 } 5108 5109 // insert zero padding as requested by the precision or min 5110 // field width 5111 zn = (int)number_of_zeros_to_pad; 5112 if (zn > 0) 5113 { 5114 if (str_l < str_m) 5115 { 5116 size_t avail = str_m - str_l; 5117 5118 vim_memset(str + str_l, '0', 5119 (size_t)zn > avail ? avail 5120 : (size_t)zn); 5121 } 5122 str_l += zn; 5123 } 5124 } 5125 5126 // insert formatted string 5127 // (or as-is conversion specifier for unknown conversions) 5128 { 5129 int sn = (int)(str_arg_l - zero_padding_insertion_ind); 5130 5131 if (sn > 0) 5132 { 5133 if (str_l < str_m) 5134 { 5135 size_t avail = str_m - str_l; 5136 5137 mch_memmove(str + str_l, 5138 str_arg + zero_padding_insertion_ind, 5139 (size_t)sn > avail ? avail : (size_t)sn); 5140 } 5141 str_l += sn; 5142 } 5143 } 5144 5145 // insert right padding 5146 if (justify_left) 5147 { 5148 // right blank padding to the field width 5149 int pn = (int)(min_field_width 5150 - (str_arg_l + number_of_zeros_to_pad)); 5151 5152 if (pn > 0) 5153 { 5154 if (str_l < str_m) 5155 { 5156 size_t avail = str_m - str_l; 5157 5158 vim_memset(str + str_l, ' ', 5159 (size_t)pn > avail ? avail 5160 : (size_t)pn); 5161 } 5162 str_l += pn; 5163 } 5164 } 5165 vim_free(tofree); 5166 } 5167 } 5168 5169 if (str_m > 0) 5170 { 5171 // make sure the string is nul-terminated even at the expense of 5172 // overwriting the last character (shouldn't happen, but just in case) 5173 // 5174 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0'; 5175 } 5176 5177 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN) 5178 emsg(_("E767: Too many arguments to printf()")); 5179 5180 // Return the number of characters formatted (excluding trailing nul 5181 // character), that is, the number of characters that would have been 5182 // written to the buffer if it were large enough. 5183 return (int)str_l; 5184 } 5185 5186 #endif // PROTO 5187