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