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