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