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