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