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