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