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