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 1701 && (mb_ptr2char(s) == 160 1702 || mb_ptr2char(s) == 0x202f)) 1703 { 1704 mb_char2bytes(lcs_nbsp, buf); 1705 buf[(*mb_ptr2len)(buf)] = NUL; 1706 } 1707 else 1708 { 1709 mch_memmove(buf, s, (size_t)l); 1710 buf[l] = NUL; 1711 } 1712 msg_puts(buf); 1713 s += l; 1714 continue; 1715 } 1716 #endif 1717 else 1718 { 1719 attr = 0; 1720 c = *s++; 1721 if (c == TAB && (!list || lcs_tab1)) 1722 { 1723 /* tab amount depends on current column */ 1724 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1; 1725 if (!list) 1726 { 1727 c = ' '; 1728 c_extra = ' '; 1729 } 1730 else 1731 { 1732 c = lcs_tab1; 1733 c_extra = lcs_tab2; 1734 attr = hl_attr(HLF_8); 1735 } 1736 } 1737 else if (c == 160 && list && lcs_nbsp != NUL) 1738 { 1739 c = lcs_nbsp; 1740 attr = hl_attr(HLF_8); 1741 } 1742 else if (c == NUL && list && lcs_eol != NUL) 1743 { 1744 p_extra = (char_u *)""; 1745 c_extra = NUL; 1746 n_extra = 1; 1747 c = lcs_eol; 1748 attr = hl_attr(HLF_AT); 1749 --s; 1750 } 1751 else if (c != NUL && (n = byte2cells(c)) > 1) 1752 { 1753 n_extra = n - 1; 1754 p_extra = transchar_byte(c); 1755 c_extra = NUL; 1756 c = *p_extra++; 1757 /* Use special coloring to be able to distinguish <hex> from 1758 * the same in plain text. */ 1759 attr = hl_attr(HLF_8); 1760 } 1761 else if (c == ' ' && trail != NULL && s > trail) 1762 { 1763 c = lcs_trail; 1764 attr = hl_attr(HLF_8); 1765 } 1766 else if (c == ' ' && list && lcs_space != NUL) 1767 { 1768 c = lcs_space; 1769 attr = hl_attr(HLF_8); 1770 } 1771 } 1772 1773 if (c == NUL) 1774 break; 1775 1776 msg_putchar_attr(c, attr); 1777 col++; 1778 } 1779 msg_clr_eos(); 1780 } 1781 1782 #ifdef FEAT_MBYTE 1783 /* 1784 * Use screen_puts() to output one multi-byte character. 1785 * Return the pointer "s" advanced to the next character. 1786 */ 1787 static char_u * 1788 screen_puts_mbyte(s, l, attr) 1789 char_u *s; 1790 int l; 1791 int attr; 1792 { 1793 int cw; 1794 1795 msg_didout = TRUE; /* remember that line is not empty */ 1796 cw = (*mb_ptr2cells)(s); 1797 if (cw > 1 && ( 1798 #ifdef FEAT_RIGHTLEFT 1799 cmdmsg_rl ? msg_col <= 1 : 1800 #endif 1801 msg_col == Columns - 1)) 1802 { 1803 /* Doesn't fit, print a highlighted '>' to fill it up. */ 1804 msg_screen_putchar('>', hl_attr(HLF_AT)); 1805 return s; 1806 } 1807 1808 screen_puts_len(s, l, msg_row, msg_col, attr); 1809 #ifdef FEAT_RIGHTLEFT 1810 if (cmdmsg_rl) 1811 { 1812 msg_col -= cw; 1813 if (msg_col == 0) 1814 { 1815 msg_col = Columns; 1816 ++msg_row; 1817 } 1818 } 1819 else 1820 #endif 1821 { 1822 msg_col += cw; 1823 if (msg_col >= Columns) 1824 { 1825 msg_col = 0; 1826 ++msg_row; 1827 } 1828 } 1829 return s + l; 1830 } 1831 #endif 1832 1833 /* 1834 * Output a string to the screen at position msg_row, msg_col. 1835 * Update msg_row and msg_col for the next message. 1836 */ 1837 void 1838 msg_puts(s) 1839 char_u *s; 1840 { 1841 msg_puts_attr(s, 0); 1842 } 1843 1844 void 1845 msg_puts_title(s) 1846 char_u *s; 1847 { 1848 msg_puts_attr(s, hl_attr(HLF_T)); 1849 } 1850 1851 /* 1852 * Show a message in such a way that it always fits in the line. Cut out a 1853 * part in the middle and replace it with "..." when necessary. 1854 * Does not handle multi-byte characters! 1855 */ 1856 void 1857 msg_puts_long_attr(longstr, attr) 1858 char_u *longstr; 1859 int attr; 1860 { 1861 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr); 1862 } 1863 1864 void 1865 msg_puts_long_len_attr(longstr, len, attr) 1866 char_u *longstr; 1867 int len; 1868 int attr; 1869 { 1870 int slen = len; 1871 int room; 1872 1873 room = Columns - msg_col; 1874 if (len > room && room >= 20) 1875 { 1876 slen = (room - 3) / 2; 1877 msg_outtrans_len_attr(longstr, slen, attr); 1878 msg_puts_attr((char_u *)"...", hl_attr(HLF_8)); 1879 } 1880 msg_outtrans_len_attr(longstr + len - slen, slen, attr); 1881 } 1882 1883 /* 1884 * Basic function for writing a message with highlight attributes. 1885 */ 1886 void 1887 msg_puts_attr(s, attr) 1888 char_u *s; 1889 int attr; 1890 { 1891 msg_puts_attr_len(s, -1, attr); 1892 } 1893 1894 /* 1895 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes). 1896 * When "maxlen" is -1 there is no maximum length. 1897 * When "maxlen" is >= 0 the message is not put in the history. 1898 */ 1899 static void 1900 msg_puts_attr_len(str, maxlen, attr) 1901 char_u *str; 1902 int maxlen; 1903 int attr; 1904 { 1905 /* 1906 * If redirection is on, also write to the redirection file. 1907 */ 1908 redir_write(str, maxlen); 1909 1910 /* 1911 * Don't print anything when using ":silent cmd". 1912 */ 1913 if (msg_silent != 0) 1914 return; 1915 1916 /* if MSG_HIST flag set, add message to history */ 1917 if ((attr & MSG_HIST) && maxlen < 0) 1918 { 1919 add_msg_hist(str, -1, attr); 1920 attr &= ~MSG_HIST; 1921 } 1922 1923 /* 1924 * When writing something to the screen after it has scrolled, requires a 1925 * wait-return prompt later. Needed when scrolling, resetting 1926 * need_wait_return after some prompt, and then outputting something 1927 * without scrolling 1928 */ 1929 if (msg_scrolled != 0 && !msg_scrolled_ign) 1930 need_wait_return = TRUE; 1931 msg_didany = TRUE; /* remember that something was outputted */ 1932 1933 /* 1934 * If there is no valid screen, use fprintf so we can see error messages. 1935 * If termcap is not active, we may be writing in an alternate console 1936 * window, cursor positioning may not work correctly (window size may be 1937 * different, e.g. for Win32 console) or we just don't know where the 1938 * cursor is. 1939 */ 1940 if (msg_use_printf()) 1941 msg_puts_printf(str, maxlen); 1942 else 1943 msg_puts_display(str, maxlen, attr, FALSE); 1944 } 1945 1946 /* 1947 * The display part of msg_puts_attr_len(). 1948 * May be called recursively to display scroll-back text. 1949 */ 1950 static void 1951 msg_puts_display(str, maxlen, attr, recurse) 1952 char_u *str; 1953 int maxlen; 1954 int attr; 1955 int recurse; 1956 { 1957 char_u *s = str; 1958 char_u *t_s = str; /* string from "t_s" to "s" is still todo */ 1959 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */ 1960 #ifdef FEAT_MBYTE 1961 int l; 1962 int cw; 1963 #endif 1964 char_u *sb_str = str; 1965 int sb_col = msg_col; 1966 int wrap; 1967 int did_last_char; 1968 1969 did_wait_return = FALSE; 1970 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL) 1971 { 1972 /* 1973 * We are at the end of the screen line when: 1974 * - When outputting a newline. 1975 * - When outputting a character in the last column. 1976 */ 1977 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || ( 1978 #ifdef FEAT_RIGHTLEFT 1979 cmdmsg_rl 1980 ? ( 1981 msg_col <= 1 1982 || (*s == TAB && msg_col <= 7) 1983 # ifdef FEAT_MBYTE 1984 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2) 1985 # endif 1986 ) 1987 : 1988 #endif 1989 (msg_col + t_col >= Columns - 1 1990 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7)) 1991 # ifdef FEAT_MBYTE 1992 || (has_mbyte && (*mb_ptr2cells)(s) > 1 1993 && msg_col + t_col >= Columns - 2) 1994 # endif 1995 )))) 1996 { 1997 /* 1998 * The screen is scrolled up when at the last row (some terminals 1999 * scroll automatically, some don't. To avoid problems we scroll 2000 * ourselves). 2001 */ 2002 if (t_col > 0) 2003 /* output postponed text */ 2004 t_puts(&t_col, t_s, s, attr); 2005 2006 /* When no more prompt and no more room, truncate here */ 2007 if (msg_no_more && lines_left == 0) 2008 break; 2009 2010 /* Scroll the screen up one line. */ 2011 msg_scroll_up(); 2012 2013 msg_row = Rows - 2; 2014 if (msg_col >= Columns) /* can happen after screen resize */ 2015 msg_col = Columns - 1; 2016 2017 /* Display char in last column before showing more-prompt. */ 2018 if (*s >= ' ' 2019 #ifdef FEAT_RIGHTLEFT 2020 && !cmdmsg_rl 2021 #endif 2022 ) 2023 { 2024 #ifdef FEAT_MBYTE 2025 if (has_mbyte) 2026 { 2027 if (enc_utf8 && maxlen >= 0) 2028 /* avoid including composing chars after the end */ 2029 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s)); 2030 else 2031 l = (*mb_ptr2len)(s); 2032 s = screen_puts_mbyte(s, l, attr); 2033 } 2034 else 2035 #endif 2036 msg_screen_putchar(*s++, attr); 2037 did_last_char = TRUE; 2038 } 2039 else 2040 did_last_char = FALSE; 2041 2042 if (p_more) 2043 /* store text for scrolling back */ 2044 store_sb_text(&sb_str, s, attr, &sb_col, TRUE); 2045 2046 inc_msg_scrolled(); 2047 need_wait_return = TRUE; /* may need wait_return in main() */ 2048 if (must_redraw < VALID) 2049 must_redraw = VALID; 2050 redraw_cmdline = TRUE; 2051 if (cmdline_row > 0 && !exmode_active) 2052 --cmdline_row; 2053 2054 /* 2055 * If screen is completely filled and 'more' is set then wait 2056 * for a character. 2057 */ 2058 if (lines_left > 0) 2059 --lines_left; 2060 if (p_more && lines_left == 0 && State != HITRETURN 2061 && !msg_no_more && !exmode_active) 2062 { 2063 #ifdef FEAT_CON_DIALOG 2064 if (do_more_prompt(NUL)) 2065 s = confirm_msg_tail; 2066 #else 2067 (void)do_more_prompt(NUL); 2068 #endif 2069 if (quit_more) 2070 return; 2071 } 2072 2073 /* When we displayed a char in last column need to check if there 2074 * is still more. */ 2075 if (did_last_char) 2076 continue; 2077 } 2078 2079 wrap = *s == '\n' 2080 || msg_col + t_col >= Columns 2081 #ifdef FEAT_MBYTE 2082 || (has_mbyte && (*mb_ptr2cells)(s) > 1 2083 && msg_col + t_col >= Columns - 1) 2084 #endif 2085 ; 2086 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b' 2087 || *s == '\t' || *s == BELL)) 2088 /* output any postponed text */ 2089 t_puts(&t_col, t_s, s, attr); 2090 2091 if (wrap && p_more && !recurse) 2092 /* store text for scrolling back */ 2093 store_sb_text(&sb_str, s, attr, &sb_col, TRUE); 2094 2095 if (*s == '\n') /* go to next line */ 2096 { 2097 msg_didout = FALSE; /* remember that line is empty */ 2098 #ifdef FEAT_RIGHTLEFT 2099 if (cmdmsg_rl) 2100 msg_col = Columns - 1; 2101 else 2102 #endif 2103 msg_col = 0; 2104 if (++msg_row >= Rows) /* safety check */ 2105 msg_row = Rows - 1; 2106 } 2107 else if (*s == '\r') /* go to column 0 */ 2108 { 2109 msg_col = 0; 2110 } 2111 else if (*s == '\b') /* go to previous char */ 2112 { 2113 if (msg_col) 2114 --msg_col; 2115 } 2116 else if (*s == TAB) /* translate Tab into spaces */ 2117 { 2118 do 2119 msg_screen_putchar(' ', attr); 2120 while (msg_col & 7); 2121 } 2122 else if (*s == BELL) /* beep (from ":sh") */ 2123 vim_beep(BO_SH); 2124 else 2125 { 2126 #ifdef FEAT_MBYTE 2127 if (has_mbyte) 2128 { 2129 cw = (*mb_ptr2cells)(s); 2130 if (enc_utf8 && maxlen >= 0) 2131 /* avoid including composing chars after the end */ 2132 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s)); 2133 else 2134 l = (*mb_ptr2len)(s); 2135 } 2136 else 2137 { 2138 cw = 1; 2139 l = 1; 2140 } 2141 #endif 2142 /* When drawing from right to left or when a double-wide character 2143 * doesn't fit, draw a single character here. Otherwise collect 2144 * characters and draw them all at once later. */ 2145 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) 2146 if ( 2147 # ifdef FEAT_RIGHTLEFT 2148 cmdmsg_rl 2149 # ifdef FEAT_MBYTE 2150 || 2151 # endif 2152 # endif 2153 # ifdef FEAT_MBYTE 2154 (cw > 1 && msg_col + t_col >= Columns - 1) 2155 # endif 2156 ) 2157 { 2158 # ifdef FEAT_MBYTE 2159 if (l > 1) 2160 s = screen_puts_mbyte(s, l, attr) - 1; 2161 else 2162 # endif 2163 msg_screen_putchar(*s, attr); 2164 } 2165 else 2166 #endif 2167 { 2168 /* postpone this character until later */ 2169 if (t_col == 0) 2170 t_s = s; 2171 #ifdef FEAT_MBYTE 2172 t_col += cw; 2173 s += l - 1; 2174 #else 2175 ++t_col; 2176 #endif 2177 } 2178 } 2179 ++s; 2180 } 2181 2182 /* output any postponed text */ 2183 if (t_col > 0) 2184 t_puts(&t_col, t_s, s, attr); 2185 if (p_more && !recurse) 2186 store_sb_text(&sb_str, s, attr, &sb_col, FALSE); 2187 2188 msg_check(); 2189 } 2190 2191 /* 2192 * Scroll the screen up one line for displaying the next message line. 2193 */ 2194 static void 2195 msg_scroll_up() 2196 { 2197 #ifdef FEAT_GUI 2198 /* Remove the cursor before scrolling, ScreenLines[] is going 2199 * to become invalid. */ 2200 if (gui.in_use) 2201 gui_undraw_cursor(); 2202 #endif 2203 /* scrolling up always works */ 2204 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL); 2205 2206 if (!can_clear((char_u *)" ")) 2207 { 2208 /* Scrolling up doesn't result in the right background. Set the 2209 * background here. It's not efficient, but avoids that we have to do 2210 * it all over the code. */ 2211 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 2212 2213 /* Also clear the last char of the last but one line if it was not 2214 * cleared before to avoid a scroll-up. */ 2215 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1) 2216 screen_fill((int)Rows - 2, (int)Rows - 1, 2217 (int)Columns - 1, (int)Columns, ' ', ' ', 0); 2218 } 2219 } 2220 2221 /* 2222 * Increment "msg_scrolled". 2223 */ 2224 static void 2225 inc_msg_scrolled() 2226 { 2227 #ifdef FEAT_EVAL 2228 if (*get_vim_var_str(VV_SCROLLSTART) == NUL) 2229 { 2230 char_u *p = sourcing_name; 2231 char_u *tofree = NULL; 2232 int len; 2233 2234 /* v:scrollstart is empty, set it to the script/function name and line 2235 * number */ 2236 if (p == NULL) 2237 p = (char_u *)_("Unknown"); 2238 else 2239 { 2240 len = (int)STRLEN(p) + 40; 2241 tofree = alloc(len); 2242 if (tofree != NULL) 2243 { 2244 vim_snprintf((char *)tofree, len, _("%s line %ld"), 2245 p, (long)sourcing_lnum); 2246 p = tofree; 2247 } 2248 } 2249 set_vim_var_string(VV_SCROLLSTART, p, -1); 2250 vim_free(tofree); 2251 } 2252 #endif 2253 ++msg_scrolled; 2254 } 2255 2256 /* 2257 * To be able to scroll back at the "more" and "hit-enter" prompts we need to 2258 * store the displayed text and remember where screen lines start. 2259 */ 2260 typedef struct msgchunk_S msgchunk_T; 2261 struct msgchunk_S 2262 { 2263 msgchunk_T *sb_next; 2264 msgchunk_T *sb_prev; 2265 char sb_eol; /* TRUE when line ends after this text */ 2266 int sb_msg_col; /* column in which text starts */ 2267 int sb_attr; /* text attributes */ 2268 char_u sb_text[1]; /* text to be displayed, actually longer */ 2269 }; 2270 2271 static msgchunk_T *last_msgchunk = NULL; /* last displayed text */ 2272 2273 static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps)); 2274 static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp)); 2275 2276 static int do_clear_sb_text = FALSE; /* clear text on next msg */ 2277 2278 /* 2279 * Store part of a printed message for displaying when scrolling back. 2280 */ 2281 static void 2282 store_sb_text(sb_str, s, attr, sb_col, finish) 2283 char_u **sb_str; /* start of string */ 2284 char_u *s; /* just after string */ 2285 int attr; 2286 int *sb_col; 2287 int finish; /* line ends */ 2288 { 2289 msgchunk_T *mp; 2290 2291 if (do_clear_sb_text) 2292 { 2293 clear_sb_text(); 2294 do_clear_sb_text = FALSE; 2295 } 2296 2297 if (s > *sb_str) 2298 { 2299 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str))); 2300 if (mp != NULL) 2301 { 2302 mp->sb_eol = finish; 2303 mp->sb_msg_col = *sb_col; 2304 mp->sb_attr = attr; 2305 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str); 2306 2307 if (last_msgchunk == NULL) 2308 { 2309 last_msgchunk = mp; 2310 mp->sb_prev = NULL; 2311 } 2312 else 2313 { 2314 mp->sb_prev = last_msgchunk; 2315 last_msgchunk->sb_next = mp; 2316 last_msgchunk = mp; 2317 } 2318 mp->sb_next = NULL; 2319 } 2320 } 2321 else if (finish && last_msgchunk != NULL) 2322 last_msgchunk->sb_eol = TRUE; 2323 2324 *sb_str = s; 2325 *sb_col = 0; 2326 } 2327 2328 /* 2329 * Finished showing messages, clear the scroll-back text on the next message. 2330 */ 2331 void 2332 may_clear_sb_text() 2333 { 2334 do_clear_sb_text = TRUE; 2335 } 2336 2337 /* 2338 * Clear any text remembered for scrolling back. 2339 * Called when redrawing the screen. 2340 */ 2341 void 2342 clear_sb_text() 2343 { 2344 msgchunk_T *mp; 2345 2346 while (last_msgchunk != NULL) 2347 { 2348 mp = last_msgchunk->sb_prev; 2349 vim_free(last_msgchunk); 2350 last_msgchunk = mp; 2351 } 2352 } 2353 2354 /* 2355 * "g<" command. 2356 */ 2357 void 2358 show_sb_text() 2359 { 2360 msgchunk_T *mp; 2361 2362 /* Only show something if there is more than one line, otherwise it looks 2363 * weird, typing a command without output results in one line. */ 2364 mp = msg_sb_start(last_msgchunk); 2365 if (mp == NULL || mp->sb_prev == NULL) 2366 vim_beep(BO_MESS); 2367 else 2368 { 2369 do_more_prompt('G'); 2370 wait_return(FALSE); 2371 } 2372 } 2373 2374 /* 2375 * Move to the start of screen line in already displayed text. 2376 */ 2377 static msgchunk_T * 2378 msg_sb_start(mps) 2379 msgchunk_T *mps; 2380 { 2381 msgchunk_T *mp = mps; 2382 2383 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol) 2384 mp = mp->sb_prev; 2385 return mp; 2386 } 2387 2388 /* 2389 * Mark the last message chunk as finishing the line. 2390 */ 2391 void 2392 msg_sb_eol() 2393 { 2394 if (last_msgchunk != NULL) 2395 last_msgchunk->sb_eol = TRUE; 2396 } 2397 2398 /* 2399 * Display a screen line from previously displayed text at row "row". 2400 * Returns a pointer to the text for the next line (can be NULL). 2401 */ 2402 static msgchunk_T * 2403 disp_sb_line(row, smp) 2404 int row; 2405 msgchunk_T *smp; 2406 { 2407 msgchunk_T *mp = smp; 2408 char_u *p; 2409 2410 for (;;) 2411 { 2412 msg_row = row; 2413 msg_col = mp->sb_msg_col; 2414 p = mp->sb_text; 2415 if (*p == '\n') /* don't display the line break */ 2416 ++p; 2417 msg_puts_display(p, -1, mp->sb_attr, TRUE); 2418 if (mp->sb_eol || mp->sb_next == NULL) 2419 break; 2420 mp = mp->sb_next; 2421 } 2422 return mp->sb_next; 2423 } 2424 2425 /* 2426 * Output any postponed text for msg_puts_attr_len(). 2427 */ 2428 static void 2429 t_puts(t_col, t_s, s, attr) 2430 int *t_col; 2431 char_u *t_s; 2432 char_u *s; 2433 int attr; 2434 { 2435 /* output postponed text */ 2436 msg_didout = TRUE; /* remember that line is not empty */ 2437 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr); 2438 msg_col += *t_col; 2439 *t_col = 0; 2440 #ifdef FEAT_MBYTE 2441 /* If the string starts with a composing character don't increment the 2442 * column position for it. */ 2443 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s))) 2444 --msg_col; 2445 #endif 2446 if (msg_col >= Columns) 2447 { 2448 msg_col = 0; 2449 ++msg_row; 2450 } 2451 } 2452 2453 /* 2454 * Returns TRUE when messages should be printed with mch_errmsg(). 2455 * This is used when there is no valid screen, so we can see error messages. 2456 * If termcap is not active, we may be writing in an alternate console 2457 * window, cursor positioning may not work correctly (window size may be 2458 * different, e.g. for Win32 console) or we just don't know where the 2459 * cursor is. 2460 */ 2461 int 2462 msg_use_printf() 2463 { 2464 return (!msg_check_screen() 2465 #if defined(WIN3264) && !defined(FEAT_GUI_MSWIN) 2466 || !termcap_active 2467 #endif 2468 || (swapping_screen() && !termcap_active) 2469 ); 2470 } 2471 2472 /* 2473 * Print a message when there is no valid screen. 2474 */ 2475 static void 2476 msg_puts_printf(str, maxlen) 2477 char_u *str; 2478 int maxlen; 2479 { 2480 char_u *s = str; 2481 char_u buf[4]; 2482 char_u *p; 2483 2484 #ifdef WIN3264 2485 if (!(silent_mode && p_verbose == 0)) 2486 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */ 2487 #endif 2488 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen)) 2489 { 2490 if (!(silent_mode && p_verbose == 0)) 2491 { 2492 /* NL --> CR NL translation (for Unix, not for "--version") */ 2493 /* NL --> CR translation (for Mac) */ 2494 p = &buf[0]; 2495 if (*s == '\n' && !info_message) 2496 *p++ = '\r'; 2497 #if defined(USE_CR) && !defined(MACOS_X_UNIX) 2498 else 2499 #endif 2500 *p++ = *s; 2501 *p = '\0'; 2502 if (info_message) /* informative message, not an error */ 2503 mch_msg((char *)buf); 2504 else 2505 mch_errmsg((char *)buf); 2506 } 2507 2508 /* primitive way to compute the current column */ 2509 #ifdef FEAT_RIGHTLEFT 2510 if (cmdmsg_rl) 2511 { 2512 if (*s == '\r' || *s == '\n') 2513 msg_col = Columns - 1; 2514 else 2515 --msg_col; 2516 } 2517 else 2518 #endif 2519 { 2520 if (*s == '\r' || *s == '\n') 2521 msg_col = 0; 2522 else 2523 ++msg_col; 2524 } 2525 ++s; 2526 } 2527 msg_didout = TRUE; /* assume that line is not empty */ 2528 2529 #ifdef WIN3264 2530 if (!(silent_mode && p_verbose == 0)) 2531 mch_settmode(TMODE_RAW); 2532 #endif 2533 } 2534 2535 /* 2536 * Show the more-prompt and handle the user response. 2537 * This takes care of scrolling back and displaying previously displayed text. 2538 * When at hit-enter prompt "typed_char" is the already typed character, 2539 * otherwise it's NUL. 2540 * Returns TRUE when jumping ahead to "confirm_msg_tail". 2541 */ 2542 static int 2543 do_more_prompt(typed_char) 2544 int typed_char; 2545 { 2546 int used_typed_char = typed_char; 2547 int oldState = State; 2548 int c; 2549 #ifdef FEAT_CON_DIALOG 2550 int retval = FALSE; 2551 #endif 2552 int toscroll; 2553 msgchunk_T *mp_last = NULL; 2554 msgchunk_T *mp; 2555 int i; 2556 2557 if (typed_char == 'G') 2558 { 2559 /* "g<": Find first line on the last page. */ 2560 mp_last = msg_sb_start(last_msgchunk); 2561 for (i = 0; i < Rows - 2 && mp_last != NULL 2562 && mp_last->sb_prev != NULL; ++i) 2563 mp_last = msg_sb_start(mp_last->sb_prev); 2564 } 2565 2566 State = ASKMORE; 2567 #ifdef FEAT_MOUSE 2568 setmouse(); 2569 #endif 2570 if (typed_char == NUL) 2571 msg_moremsg(FALSE); 2572 for (;;) 2573 { 2574 /* 2575 * Get a typed character directly from the user. 2576 */ 2577 if (used_typed_char != NUL) 2578 { 2579 c = used_typed_char; /* was typed at hit-enter prompt */ 2580 used_typed_char = NUL; 2581 } 2582 else 2583 c = get_keystroke(); 2584 2585 #if defined(FEAT_MENU) && defined(FEAT_GUI) 2586 if (c == K_MENU) 2587 { 2588 int idx = get_menu_index(current_menu, ASKMORE); 2589 2590 /* Used a menu. If it starts with CTRL-Y, it must 2591 * be a "Copy" for the clipboard. Otherwise 2592 * assume that we end */ 2593 if (idx == MENU_INDEX_INVALID) 2594 continue; 2595 c = *current_menu->strings[idx]; 2596 if (c != NUL && current_menu->strings[idx][1] != NUL) 2597 ins_typebuf(current_menu->strings[idx] + 1, 2598 current_menu->noremap[idx], 0, TRUE, 2599 current_menu->silent[idx]); 2600 } 2601 #endif 2602 2603 toscroll = 0; 2604 switch (c) 2605 { 2606 case BS: /* scroll one line back */ 2607 case K_BS: 2608 case 'k': 2609 case K_UP: 2610 toscroll = -1; 2611 break; 2612 2613 case CAR: /* one extra line */ 2614 case NL: 2615 case 'j': 2616 case K_DOWN: 2617 toscroll = 1; 2618 break; 2619 2620 case 'u': /* Up half a page */ 2621 toscroll = -(Rows / 2); 2622 break; 2623 2624 case 'd': /* Down half a page */ 2625 toscroll = Rows / 2; 2626 break; 2627 2628 case 'b': /* one page back */ 2629 case K_PAGEUP: 2630 toscroll = -(Rows - 1); 2631 break; 2632 2633 case ' ': /* one extra page */ 2634 case 'f': 2635 case K_PAGEDOWN: 2636 case K_LEFTMOUSE: 2637 toscroll = Rows - 1; 2638 break; 2639 2640 case 'g': /* all the way back to the start */ 2641 toscroll = -999999; 2642 break; 2643 2644 case 'G': /* all the way to the end */ 2645 toscroll = 999999; 2646 lines_left = 999999; 2647 break; 2648 2649 case ':': /* start new command line */ 2650 #ifdef FEAT_CON_DIALOG 2651 if (!confirm_msg_used) 2652 #endif 2653 { 2654 /* Since got_int is set all typeahead will be flushed, but we 2655 * want to keep this ':', remember that in a special way. */ 2656 typeahead_noflush(':'); 2657 cmdline_row = Rows - 1; /* put ':' on this line */ 2658 skip_redraw = TRUE; /* skip redraw once */ 2659 need_wait_return = FALSE; /* don't wait in main() */ 2660 } 2661 /*FALLTHROUGH*/ 2662 case 'q': /* quit */ 2663 case Ctrl_C: 2664 case ESC: 2665 #ifdef FEAT_CON_DIALOG 2666 if (confirm_msg_used) 2667 { 2668 /* Jump to the choices of the dialog. */ 2669 retval = TRUE; 2670 } 2671 else 2672 #endif 2673 { 2674 got_int = TRUE; 2675 quit_more = TRUE; 2676 } 2677 /* When there is some more output (wrapping line) display that 2678 * without another prompt. */ 2679 lines_left = Rows - 1; 2680 break; 2681 2682 #ifdef FEAT_CLIPBOARD 2683 case Ctrl_Y: 2684 /* Strange way to allow copying (yanking) a modeless 2685 * selection at the more prompt. Use CTRL-Y, 2686 * because the same is used in Cmdline-mode and at the 2687 * hit-enter prompt. However, scrolling one line up 2688 * might be expected... */ 2689 if (clip_star.state == SELECT_DONE) 2690 clip_copy_modeless_selection(TRUE); 2691 continue; 2692 #endif 2693 default: /* no valid response */ 2694 msg_moremsg(TRUE); 2695 continue; 2696 } 2697 2698 if (toscroll != 0) 2699 { 2700 if (toscroll < 0) 2701 { 2702 /* go to start of last line */ 2703 if (mp_last == NULL) 2704 mp = msg_sb_start(last_msgchunk); 2705 else if (mp_last->sb_prev != NULL) 2706 mp = msg_sb_start(mp_last->sb_prev); 2707 else 2708 mp = NULL; 2709 2710 /* go to start of line at top of the screen */ 2711 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL; 2712 ++i) 2713 mp = msg_sb_start(mp->sb_prev); 2714 2715 if (mp != NULL && mp->sb_prev != NULL) 2716 { 2717 /* Find line to be displayed at top. */ 2718 for (i = 0; i > toscroll; --i) 2719 { 2720 if (mp == NULL || mp->sb_prev == NULL) 2721 break; 2722 mp = msg_sb_start(mp->sb_prev); 2723 if (mp_last == NULL) 2724 mp_last = msg_sb_start(last_msgchunk); 2725 else 2726 mp_last = msg_sb_start(mp_last->sb_prev); 2727 } 2728 2729 if (toscroll == -1 && screen_ins_lines(0, 0, 1, 2730 (int)Rows, NULL) == OK) 2731 { 2732 /* display line at top */ 2733 (void)disp_sb_line(0, mp); 2734 } 2735 else 2736 { 2737 /* redisplay all lines */ 2738 screenclear(); 2739 for (i = 0; mp != NULL && i < Rows - 1; ++i) 2740 { 2741 mp = disp_sb_line(i, mp); 2742 ++msg_scrolled; 2743 } 2744 } 2745 toscroll = 0; 2746 } 2747 } 2748 else 2749 { 2750 /* First display any text that we scrolled back. */ 2751 while (toscroll > 0 && mp_last != NULL) 2752 { 2753 /* scroll up, display line at bottom */ 2754 msg_scroll_up(); 2755 inc_msg_scrolled(); 2756 screen_fill((int)Rows - 2, (int)Rows - 1, 0, 2757 (int)Columns, ' ', ' ', 0); 2758 mp_last = disp_sb_line((int)Rows - 2, mp_last); 2759 --toscroll; 2760 } 2761 } 2762 2763 if (toscroll <= 0) 2764 { 2765 /* displayed the requested text, more prompt again */ 2766 screen_fill((int)Rows - 1, (int)Rows, 0, 2767 (int)Columns, ' ', ' ', 0); 2768 msg_moremsg(FALSE); 2769 continue; 2770 } 2771 2772 /* display more text, return to caller */ 2773 lines_left = toscroll; 2774 } 2775 2776 break; 2777 } 2778 2779 /* clear the --more-- message */ 2780 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 2781 State = oldState; 2782 #ifdef FEAT_MOUSE 2783 setmouse(); 2784 #endif 2785 if (quit_more) 2786 { 2787 msg_row = Rows - 1; 2788 msg_col = 0; 2789 } 2790 #ifdef FEAT_RIGHTLEFT 2791 else if (cmdmsg_rl) 2792 msg_col = Columns - 1; 2793 #endif 2794 2795 #ifdef FEAT_CON_DIALOG 2796 return retval; 2797 #else 2798 return FALSE; 2799 #endif 2800 } 2801 2802 #if defined(USE_MCH_ERRMSG) || defined(PROTO) 2803 2804 #ifdef mch_errmsg 2805 # undef mch_errmsg 2806 #endif 2807 #ifdef mch_msg 2808 # undef mch_msg 2809 #endif 2810 2811 /* 2812 * Give an error message. To be used when the screen hasn't been initialized 2813 * yet. When stderr can't be used, collect error messages until the GUI has 2814 * started and they can be displayed in a message box. 2815 */ 2816 void 2817 mch_errmsg(str) 2818 char *str; 2819 { 2820 int len; 2821 2822 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) 2823 /* On Unix use stderr if it's a tty. 2824 * When not going to start the GUI also use stderr. 2825 * On Mac, when started from Finder, stderr is the console. */ 2826 if ( 2827 # ifdef UNIX 2828 # ifdef MACOS_X_UNIX 2829 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0) 2830 # else 2831 isatty(2) 2832 # endif 2833 # ifdef FEAT_GUI 2834 || 2835 # endif 2836 # endif 2837 # ifdef FEAT_GUI 2838 !(gui.in_use || gui.starting) 2839 # endif 2840 ) 2841 { 2842 fprintf(stderr, "%s", str); 2843 return; 2844 } 2845 #endif 2846 2847 /* avoid a delay for a message that isn't there */ 2848 emsg_on_display = FALSE; 2849 2850 len = (int)STRLEN(str) + 1; 2851 if (error_ga.ga_growsize == 0) 2852 { 2853 error_ga.ga_growsize = 80; 2854 error_ga.ga_itemsize = 1; 2855 } 2856 if (ga_grow(&error_ga, len) == OK) 2857 { 2858 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len, 2859 (char_u *)str, len); 2860 #ifdef UNIX 2861 /* remove CR characters, they are displayed */ 2862 { 2863 char_u *p; 2864 2865 p = (char_u *)error_ga.ga_data + error_ga.ga_len; 2866 for (;;) 2867 { 2868 p = vim_strchr(p, '\r'); 2869 if (p == NULL) 2870 break; 2871 *p = ' '; 2872 } 2873 } 2874 #endif 2875 --len; /* don't count the NUL at the end */ 2876 error_ga.ga_len += len; 2877 } 2878 } 2879 2880 /* 2881 * Give a message. To be used when the screen hasn't been initialized yet. 2882 * When there is no tty, collect messages until the GUI has started and they 2883 * can be displayed in a message box. 2884 */ 2885 void 2886 mch_msg(str) 2887 char *str; 2888 { 2889 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) 2890 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and 2891 * uses mch_errmsg() when started from the desktop. 2892 * When not going to start the GUI also use stdout. 2893 * On Mac, when started from Finder, stderr is the console. */ 2894 if ( 2895 # ifdef UNIX 2896 # ifdef MACOS_X_UNIX 2897 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0) 2898 # else 2899 isatty(2) 2900 # endif 2901 # ifdef FEAT_GUI 2902 || 2903 # endif 2904 # endif 2905 # ifdef FEAT_GUI 2906 !(gui.in_use || gui.starting) 2907 # endif 2908 ) 2909 { 2910 printf("%s", str); 2911 return; 2912 } 2913 # endif 2914 mch_errmsg(str); 2915 } 2916 #endif /* USE_MCH_ERRMSG */ 2917 2918 /* 2919 * Put a character on the screen at the current message position and advance 2920 * to the next position. Only for printable ASCII! 2921 */ 2922 static void 2923 msg_screen_putchar(c, attr) 2924 int c; 2925 int attr; 2926 { 2927 msg_didout = TRUE; /* remember that line is not empty */ 2928 screen_putchar(c, msg_row, msg_col, attr); 2929 #ifdef FEAT_RIGHTLEFT 2930 if (cmdmsg_rl) 2931 { 2932 if (--msg_col == 0) 2933 { 2934 msg_col = Columns; 2935 ++msg_row; 2936 } 2937 } 2938 else 2939 #endif 2940 { 2941 if (++msg_col >= Columns) 2942 { 2943 msg_col = 0; 2944 ++msg_row; 2945 } 2946 } 2947 } 2948 2949 void 2950 msg_moremsg(full) 2951 int full; 2952 { 2953 int attr; 2954 char_u *s = (char_u *)_("-- More --"); 2955 2956 attr = hl_attr(HLF_M); 2957 screen_puts(s, (int)Rows - 1, 0, attr); 2958 if (full) 2959 screen_puts((char_u *) 2960 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "), 2961 (int)Rows - 1, vim_strsize(s), attr); 2962 } 2963 2964 /* 2965 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or 2966 * exmode_active. 2967 */ 2968 void 2969 repeat_message() 2970 { 2971 if (State == ASKMORE) 2972 { 2973 msg_moremsg(TRUE); /* display --more-- message again */ 2974 msg_row = Rows - 1; 2975 } 2976 #ifdef FEAT_CON_DIALOG 2977 else if (State == CONFIRM) 2978 { 2979 display_confirm_msg(); /* display ":confirm" message again */ 2980 msg_row = Rows - 1; 2981 } 2982 #endif 2983 else if (State == EXTERNCMD) 2984 { 2985 windgoto(msg_row, msg_col); /* put cursor back */ 2986 } 2987 else if (State == HITRETURN || State == SETWSIZE) 2988 { 2989 if (msg_row == Rows - 1) 2990 { 2991 /* Avoid drawing the "hit-enter" prompt below the previous one, 2992 * overwrite it. Esp. useful when regaining focus and a 2993 * FocusGained autocmd exists but didn't draw anything. */ 2994 msg_didout = FALSE; 2995 msg_col = 0; 2996 msg_clr_eos(); 2997 } 2998 hit_return_msg(); 2999 msg_row = Rows - 1; 3000 } 3001 } 3002 3003 /* 3004 * msg_check_screen - check if the screen is initialized. 3005 * Also check msg_row and msg_col, if they are too big it may cause a crash. 3006 * While starting the GUI the terminal codes will be set for the GUI, but the 3007 * output goes to the terminal. Don't use the terminal codes then. 3008 */ 3009 static int 3010 msg_check_screen() 3011 { 3012 if (!full_screen || !screen_valid(FALSE)) 3013 return FALSE; 3014 3015 if (msg_row >= Rows) 3016 msg_row = Rows - 1; 3017 if (msg_col >= Columns) 3018 msg_col = Columns - 1; 3019 return TRUE; 3020 } 3021 3022 /* 3023 * Clear from current message position to end of screen. 3024 * Skip this when ":silent" was used, no need to clear for redirection. 3025 */ 3026 void 3027 msg_clr_eos() 3028 { 3029 if (msg_silent == 0) 3030 msg_clr_eos_force(); 3031 } 3032 3033 /* 3034 * Clear from current message position to end of screen. 3035 * Note: msg_col is not updated, so we remember the end of the message 3036 * for msg_check(). 3037 */ 3038 void 3039 msg_clr_eos_force() 3040 { 3041 if (msg_use_printf()) 3042 { 3043 if (full_screen) /* only when termcap codes are valid */ 3044 { 3045 if (*T_CD) 3046 out_str(T_CD); /* clear to end of display */ 3047 else if (*T_CE) 3048 out_str(T_CE); /* clear to end of line */ 3049 } 3050 } 3051 else 3052 { 3053 #ifdef FEAT_RIGHTLEFT 3054 if (cmdmsg_rl) 3055 { 3056 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0); 3057 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3058 } 3059 else 3060 #endif 3061 { 3062 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns, 3063 ' ', ' ', 0); 3064 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0); 3065 } 3066 } 3067 } 3068 3069 /* 3070 * Clear the command line. 3071 */ 3072 void 3073 msg_clr_cmdline() 3074 { 3075 msg_row = cmdline_row; 3076 msg_col = 0; 3077 msg_clr_eos_force(); 3078 } 3079 3080 /* 3081 * end putting a message on the screen 3082 * call wait_return if the message does not fit in the available space 3083 * return TRUE if wait_return not called. 3084 */ 3085 int 3086 msg_end() 3087 { 3088 /* 3089 * If the string is larger than the window, 3090 * or the ruler option is set and we run into it, 3091 * we have to redraw the window. 3092 * Do not do this if we are abandoning the file or editing the command line. 3093 */ 3094 if (!exiting && need_wait_return && !(State & CMDLINE)) 3095 { 3096 wait_return(FALSE); 3097 return FALSE; 3098 } 3099 out_flush(); 3100 return TRUE; 3101 } 3102 3103 /* 3104 * If the written message runs into the shown command or ruler, we have to 3105 * wait for hit-return and redraw the window later. 3106 */ 3107 void 3108 msg_check() 3109 { 3110 if (msg_row == Rows - 1 && msg_col >= sc_col) 3111 { 3112 need_wait_return = TRUE; 3113 redraw_cmdline = TRUE; 3114 } 3115 } 3116 3117 /* 3118 * May write a string to the redirection file. 3119 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes. 3120 */ 3121 static void 3122 redir_write(str, maxlen) 3123 char_u *str; 3124 int maxlen; 3125 { 3126 char_u *s = str; 3127 static int cur_col = 0; 3128 3129 /* Don't do anything for displaying prompts and the like. */ 3130 if (redir_off) 3131 return; 3132 3133 /* If 'verbosefile' is set prepare for writing in that file. */ 3134 if (*p_vfile != NUL && verbose_fd == NULL) 3135 verbose_open(); 3136 3137 if (redirecting()) 3138 { 3139 /* If the string doesn't start with CR or NL, go to msg_col */ 3140 if (*s != '\n' && *s != '\r') 3141 { 3142 while (cur_col < msg_col) 3143 { 3144 #ifdef FEAT_EVAL 3145 if (redir_reg) 3146 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE); 3147 else if (redir_vname) 3148 var_redir_str((char_u *)" ", -1); 3149 else 3150 #endif 3151 if (redir_fd != NULL) 3152 fputs(" ", redir_fd); 3153 if (verbose_fd != NULL) 3154 fputs(" ", verbose_fd); 3155 ++cur_col; 3156 } 3157 } 3158 3159 #ifdef FEAT_EVAL 3160 if (redir_reg) 3161 write_reg_contents(redir_reg, s, maxlen, TRUE); 3162 if (redir_vname) 3163 var_redir_str(s, maxlen); 3164 #endif 3165 3166 /* Write and adjust the current column. */ 3167 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen)) 3168 { 3169 #ifdef FEAT_EVAL 3170 if (!redir_reg && !redir_vname) 3171 #endif 3172 if (redir_fd != NULL) 3173 putc(*s, redir_fd); 3174 if (verbose_fd != NULL) 3175 putc(*s, verbose_fd); 3176 if (*s == '\r' || *s == '\n') 3177 cur_col = 0; 3178 else if (*s == '\t') 3179 cur_col += (8 - cur_col % 8); 3180 else 3181 ++cur_col; 3182 ++s; 3183 } 3184 3185 if (msg_silent != 0) /* should update msg_col */ 3186 msg_col = cur_col; 3187 } 3188 } 3189 3190 int 3191 redirecting() 3192 { 3193 return redir_fd != NULL || *p_vfile != NUL 3194 #ifdef FEAT_EVAL 3195 || redir_reg || redir_vname 3196 #endif 3197 ; 3198 } 3199 3200 /* 3201 * Before giving verbose message. 3202 * Must always be called paired with verbose_leave()! 3203 */ 3204 void 3205 verbose_enter() 3206 { 3207 if (*p_vfile != NUL) 3208 ++msg_silent; 3209 } 3210 3211 /* 3212 * After giving verbose message. 3213 * Must always be called paired with verbose_enter()! 3214 */ 3215 void 3216 verbose_leave() 3217 { 3218 if (*p_vfile != NUL) 3219 if (--msg_silent < 0) 3220 msg_silent = 0; 3221 } 3222 3223 /* 3224 * Like verbose_enter() and set msg_scroll when displaying the message. 3225 */ 3226 void 3227 verbose_enter_scroll() 3228 { 3229 if (*p_vfile != NUL) 3230 ++msg_silent; 3231 else 3232 /* always scroll up, don't overwrite */ 3233 msg_scroll = TRUE; 3234 } 3235 3236 /* 3237 * Like verbose_leave() and set cmdline_row when displaying the message. 3238 */ 3239 void 3240 verbose_leave_scroll() 3241 { 3242 if (*p_vfile != NUL) 3243 { 3244 if (--msg_silent < 0) 3245 msg_silent = 0; 3246 } 3247 else 3248 cmdline_row = msg_row; 3249 } 3250 3251 /* 3252 * Called when 'verbosefile' is set: stop writing to the file. 3253 */ 3254 void 3255 verbose_stop() 3256 { 3257 if (verbose_fd != NULL) 3258 { 3259 fclose(verbose_fd); 3260 verbose_fd = NULL; 3261 } 3262 verbose_did_open = FALSE; 3263 } 3264 3265 /* 3266 * Open the file 'verbosefile'. 3267 * Return FAIL or OK. 3268 */ 3269 int 3270 verbose_open() 3271 { 3272 if (verbose_fd == NULL && !verbose_did_open) 3273 { 3274 /* Only give the error message once. */ 3275 verbose_did_open = TRUE; 3276 3277 verbose_fd = mch_fopen((char *)p_vfile, "a"); 3278 if (verbose_fd == NULL) 3279 { 3280 EMSG2(_(e_notopen), p_vfile); 3281 return FAIL; 3282 } 3283 } 3284 return OK; 3285 } 3286 3287 /* 3288 * Give a warning message (for searching). 3289 * Use 'w' highlighting and may repeat the message after redrawing 3290 */ 3291 void 3292 give_warning(message, hl) 3293 char_u *message; 3294 int hl; 3295 { 3296 /* Don't do this for ":silent". */ 3297 if (msg_silent != 0) 3298 return; 3299 3300 /* Don't want a hit-enter prompt here. */ 3301 ++no_wait_return; 3302 3303 #ifdef FEAT_EVAL 3304 set_vim_var_string(VV_WARNINGMSG, message, -1); 3305 #endif 3306 vim_free(keep_msg); 3307 keep_msg = NULL; 3308 if (hl) 3309 keep_msg_attr = hl_attr(HLF_W); 3310 else 3311 keep_msg_attr = 0; 3312 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0) 3313 set_keep_msg(message, keep_msg_attr); 3314 msg_didout = FALSE; /* overwrite this message */ 3315 msg_nowait = TRUE; /* don't wait for this message */ 3316 msg_col = 0; 3317 3318 --no_wait_return; 3319 } 3320 3321 /* 3322 * Advance msg cursor to column "col". 3323 */ 3324 void 3325 msg_advance(col) 3326 int col; 3327 { 3328 if (msg_silent != 0) /* nothing to advance to */ 3329 { 3330 msg_col = col; /* for redirection, may fill it up later */ 3331 return; 3332 } 3333 if (col >= Columns) /* not enough room */ 3334 col = Columns - 1; 3335 #ifdef FEAT_RIGHTLEFT 3336 if (cmdmsg_rl) 3337 while (msg_col > Columns - col) 3338 msg_putchar(' '); 3339 else 3340 #endif 3341 while (msg_col < col) 3342 msg_putchar(' '); 3343 } 3344 3345 #if defined(FEAT_CON_DIALOG) || defined(PROTO) 3346 /* 3347 * Used for "confirm()" function, and the :confirm command prefix. 3348 * Versions which haven't got flexible dialogs yet, and console 3349 * versions, get this generic handler which uses the command line. 3350 * 3351 * type = one of: 3352 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC 3353 * title = title string (can be NULL for default) 3354 * (neither used in console dialogs at the moment) 3355 * 3356 * Format of the "buttons" string: 3357 * "Button1Name\nButton2Name\nButton3Name" 3358 * The first button should normally be the default/accept 3359 * The second button should be the 'Cancel' button 3360 * Other buttons- use your imagination! 3361 * A '&' in a button name becomes a shortcut, so each '&' should be before a 3362 * different letter. 3363 */ 3364 int 3365 do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd) 3366 int type UNUSED; 3367 char_u *title UNUSED; 3368 char_u *message; 3369 char_u *buttons; 3370 int dfltbutton; 3371 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL 3372 otherwise */ 3373 int ex_cmd; /* when TRUE pressing : accepts default and starts 3374 Ex command */ 3375 { 3376 int oldState; 3377 int retval = 0; 3378 char_u *hotkeys; 3379 int c; 3380 int i; 3381 3382 #ifndef NO_CONSOLE 3383 /* Don't output anything in silent mode ("ex -s") */ 3384 if (silent_mode) 3385 return dfltbutton; /* return default option */ 3386 #endif 3387 3388 #ifdef FEAT_GUI_DIALOG 3389 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */ 3390 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL) 3391 { 3392 c = gui_mch_dialog(type, title, message, buttons, dfltbutton, 3393 textfield, ex_cmd); 3394 /* avoid a hit-enter prompt without clearing the cmdline */ 3395 need_wait_return = FALSE; 3396 emsg_on_display = FALSE; 3397 cmdline_row = msg_row; 3398 3399 /* Flush output to avoid that further messages and redrawing is done 3400 * in the wrong order. */ 3401 out_flush(); 3402 gui_mch_update(); 3403 3404 return c; 3405 } 3406 #endif 3407 3408 oldState = State; 3409 State = CONFIRM; 3410 #ifdef FEAT_MOUSE 3411 setmouse(); 3412 #endif 3413 3414 /* 3415 * Since we wait for a keypress, don't make the 3416 * user press RETURN as well afterwards. 3417 */ 3418 ++no_wait_return; 3419 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton); 3420 3421 if (hotkeys != NULL) 3422 { 3423 for (;;) 3424 { 3425 /* Get a typed character directly from the user. */ 3426 c = get_keystroke(); 3427 switch (c) 3428 { 3429 case CAR: /* User accepts default option */ 3430 case NL: 3431 retval = dfltbutton; 3432 break; 3433 case Ctrl_C: /* User aborts/cancels */ 3434 case ESC: 3435 retval = 0; 3436 break; 3437 default: /* Could be a hotkey? */ 3438 if (c < 0) /* special keys are ignored here */ 3439 continue; 3440 if (c == ':' && ex_cmd) 3441 { 3442 retval = dfltbutton; 3443 ins_char_typebuf(':'); 3444 break; 3445 } 3446 3447 /* Make the character lowercase, as chars in "hotkeys" are. */ 3448 c = MB_TOLOWER(c); 3449 retval = 1; 3450 for (i = 0; hotkeys[i]; ++i) 3451 { 3452 #ifdef FEAT_MBYTE 3453 if (has_mbyte) 3454 { 3455 if ((*mb_ptr2char)(hotkeys + i) == c) 3456 break; 3457 i += (*mb_ptr2len)(hotkeys + i) - 1; 3458 } 3459 else 3460 #endif 3461 if (hotkeys[i] == c) 3462 break; 3463 ++retval; 3464 } 3465 if (hotkeys[i]) 3466 break; 3467 /* No hotkey match, so keep waiting */ 3468 continue; 3469 } 3470 break; 3471 } 3472 3473 vim_free(hotkeys); 3474 } 3475 3476 State = oldState; 3477 #ifdef FEAT_MOUSE 3478 setmouse(); 3479 #endif 3480 --no_wait_return; 3481 msg_end_prompt(); 3482 3483 return retval; 3484 } 3485 3486 static int copy_char __ARGS((char_u *from, char_u *to, int lowercase)); 3487 3488 /* 3489 * Copy one character from "*from" to "*to", taking care of multi-byte 3490 * characters. Return the length of the character in bytes. 3491 */ 3492 static int 3493 copy_char(from, to, lowercase) 3494 char_u *from; 3495 char_u *to; 3496 int lowercase; /* make character lower case */ 3497 { 3498 #ifdef FEAT_MBYTE 3499 int len; 3500 int c; 3501 3502 if (has_mbyte) 3503 { 3504 if (lowercase) 3505 { 3506 c = MB_TOLOWER((*mb_ptr2char)(from)); 3507 return (*mb_char2bytes)(c, to); 3508 } 3509 else 3510 { 3511 len = (*mb_ptr2len)(from); 3512 mch_memmove(to, from, (size_t)len); 3513 return len; 3514 } 3515 } 3516 else 3517 #endif 3518 { 3519 if (lowercase) 3520 *to = (char_u)TOLOWER_LOC(*from); 3521 else 3522 *to = *from; 3523 return 1; 3524 } 3525 } 3526 3527 /* 3528 * Format the dialog string, and display it at the bottom of 3529 * the screen. Return a string of hotkey chars (if defined) for 3530 * each 'button'. If a button has no hotkey defined, the first character of 3531 * the button is used. 3532 * The hotkeys can be multi-byte characters, but without combining chars. 3533 * 3534 * Returns an allocated string with hotkeys, or NULL for error. 3535 */ 3536 static char_u * 3537 msg_show_console_dialog(message, buttons, dfltbutton) 3538 char_u *message; 3539 char_u *buttons; 3540 int dfltbutton; 3541 { 3542 int len = 0; 3543 #ifdef FEAT_MBYTE 3544 # define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1) 3545 #else 3546 # define HOTK_LEN 1 3547 #endif 3548 int lenhotkey = HOTK_LEN; /* count first button */ 3549 char_u *hotk = NULL; 3550 char_u *msgp = NULL; 3551 char_u *hotkp = NULL; 3552 char_u *r; 3553 int copy; 3554 #define HAS_HOTKEY_LEN 30 3555 char_u has_hotkey[HAS_HOTKEY_LEN]; 3556 int first_hotkey = FALSE; /* first char of button is hotkey */ 3557 int idx; 3558 3559 has_hotkey[0] = FALSE; 3560 3561 /* 3562 * First loop: compute the size of memory to allocate. 3563 * Second loop: copy to the allocated memory. 3564 */ 3565 for (copy = 0; copy <= 1; ++copy) 3566 { 3567 r = buttons; 3568 idx = 0; 3569 while (*r) 3570 { 3571 if (*r == DLG_BUTTON_SEP) 3572 { 3573 if (copy) 3574 { 3575 *msgp++ = ','; 3576 *msgp++ = ' '; /* '\n' -> ', ' */ 3577 3578 /* advance to next hotkey and set default hotkey */ 3579 #ifdef FEAT_MBYTE 3580 if (has_mbyte) 3581 hotkp += STRLEN(hotkp); 3582 else 3583 #endif 3584 ++hotkp; 3585 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL; 3586 if (dfltbutton) 3587 --dfltbutton; 3588 3589 /* If no hotkey is specified first char is used. */ 3590 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx]) 3591 first_hotkey = TRUE; 3592 } 3593 else 3594 { 3595 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */ 3596 lenhotkey += HOTK_LEN; /* each button needs a hotkey */ 3597 if (idx < HAS_HOTKEY_LEN - 1) 3598 has_hotkey[++idx] = FALSE; 3599 } 3600 } 3601 else if (*r == DLG_HOTKEY_CHAR || first_hotkey) 3602 { 3603 if (*r == DLG_HOTKEY_CHAR) 3604 ++r; 3605 first_hotkey = FALSE; 3606 if (copy) 3607 { 3608 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */ 3609 *msgp++ = *r; 3610 else 3611 { 3612 /* '&a' -> '[a]' */ 3613 *msgp++ = (dfltbutton == 1) ? '[' : '('; 3614 msgp += copy_char(r, msgp, FALSE); 3615 *msgp++ = (dfltbutton == 1) ? ']' : ')'; 3616 3617 /* redefine hotkey */ 3618 hotkp[copy_char(r, hotkp, TRUE)] = NUL; 3619 } 3620 } 3621 else 3622 { 3623 ++len; /* '&a' -> '[a]' */ 3624 if (idx < HAS_HOTKEY_LEN - 1) 3625 has_hotkey[idx] = TRUE; 3626 } 3627 } 3628 else 3629 { 3630 /* everything else copy literally */ 3631 if (copy) 3632 msgp += copy_char(r, msgp, FALSE); 3633 } 3634 3635 /* advance to the next character */ 3636 mb_ptr_adv(r); 3637 } 3638 3639 if (copy) 3640 { 3641 *msgp++ = ':'; 3642 *msgp++ = ' '; 3643 *msgp = NUL; 3644 } 3645 else 3646 { 3647 len += (int)(STRLEN(message) 3648 + 2 /* for the NL's */ 3649 + STRLEN(buttons) 3650 + 3); /* for the ": " and NUL */ 3651 lenhotkey++; /* for the NUL */ 3652 3653 /* If no hotkey is specified first char is used. */ 3654 if (!has_hotkey[0]) 3655 { 3656 first_hotkey = TRUE; 3657 len += 2; /* "x" -> "[x]" */ 3658 } 3659 3660 /* 3661 * Now allocate and load the strings 3662 */ 3663 vim_free(confirm_msg); 3664 confirm_msg = alloc(len); 3665 if (confirm_msg == NULL) 3666 return NULL; 3667 *confirm_msg = NUL; 3668 hotk = alloc(lenhotkey); 3669 if (hotk == NULL) 3670 return NULL; 3671 3672 *confirm_msg = '\n'; 3673 STRCPY(confirm_msg + 1, message); 3674 3675 msgp = confirm_msg + 1 + STRLEN(message); 3676 hotkp = hotk; 3677 3678 /* Define first default hotkey. Keep the hotkey string NUL 3679 * terminated to avoid reading past the end. */ 3680 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL; 3681 3682 /* Remember where the choices start, displaying starts here when 3683 * "hotkp" typed at the more prompt. */ 3684 confirm_msg_tail = msgp; 3685 *msgp++ = '\n'; 3686 } 3687 } 3688 3689 display_confirm_msg(); 3690 return hotk; 3691 } 3692 3693 /* 3694 * Display the ":confirm" message. Also called when screen resized. 3695 */ 3696 void 3697 display_confirm_msg() 3698 { 3699 /* avoid that 'q' at the more prompt truncates the message here */ 3700 ++confirm_msg_used; 3701 if (confirm_msg != NULL) 3702 msg_puts_attr(confirm_msg, hl_attr(HLF_M)); 3703 --confirm_msg_used; 3704 } 3705 3706 #endif /* FEAT_CON_DIALOG */ 3707 3708 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) 3709 3710 int 3711 vim_dialog_yesno(type, title, message, dflt) 3712 int type; 3713 char_u *title; 3714 char_u *message; 3715 int dflt; 3716 { 3717 if (do_dialog(type, 3718 title == NULL ? (char_u *)_("Question") : title, 3719 message, 3720 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1) 3721 return VIM_YES; 3722 return VIM_NO; 3723 } 3724 3725 int 3726 vim_dialog_yesnocancel(type, title, message, dflt) 3727 int type; 3728 char_u *title; 3729 char_u *message; 3730 int dflt; 3731 { 3732 switch (do_dialog(type, 3733 title == NULL ? (char_u *)_("Question") : title, 3734 message, 3735 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE)) 3736 { 3737 case 1: return VIM_YES; 3738 case 2: return VIM_NO; 3739 } 3740 return VIM_CANCEL; 3741 } 3742 3743 int 3744 vim_dialog_yesnoallcancel(type, title, message, dflt) 3745 int type; 3746 char_u *title; 3747 char_u *message; 3748 int dflt; 3749 { 3750 switch (do_dialog(type, 3751 title == NULL ? (char_u *)"Question" : title, 3752 message, 3753 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"), 3754 dflt, NULL, FALSE)) 3755 { 3756 case 1: return VIM_YES; 3757 case 2: return VIM_NO; 3758 case 3: return VIM_ALL; 3759 case 4: return VIM_DISCARDALL; 3760 } 3761 return VIM_CANCEL; 3762 } 3763 3764 #endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */ 3765 3766 #if defined(FEAT_BROWSE) || defined(PROTO) 3767 /* 3768 * Generic browse function. Calls gui_mch_browse() when possible. 3769 * Later this may pop-up a non-GUI file selector (external command?). 3770 */ 3771 char_u * 3772 do_browse(flags, title, dflt, ext, initdir, filter, buf) 3773 int flags; /* BROWSE_SAVE and BROWSE_DIR */ 3774 char_u *title; /* title for the window */ 3775 char_u *dflt; /* default file name (may include directory) */ 3776 char_u *ext; /* extension added */ 3777 char_u *initdir; /* initial directory, NULL for current dir or 3778 when using path from "dflt" */ 3779 char_u *filter; /* file name filter */ 3780 buf_T *buf; /* buffer to read/write for */ 3781 { 3782 char_u *fname; 3783 static char_u *last_dir = NULL; /* last used directory */ 3784 char_u *tofree = NULL; 3785 int save_browse = cmdmod.browse; 3786 3787 /* Must turn off browse to avoid that autocommands will get the 3788 * flag too! */ 3789 cmdmod.browse = FALSE; 3790 3791 if (title == NULL || *title == NUL) 3792 { 3793 if (flags & BROWSE_DIR) 3794 title = (char_u *)_("Select Directory dialog"); 3795 else if (flags & BROWSE_SAVE) 3796 title = (char_u *)_("Save File dialog"); 3797 else 3798 title = (char_u *)_("Open File dialog"); 3799 } 3800 3801 /* When no directory specified, use default file name, default dir, buffer 3802 * dir, last dir or current dir */ 3803 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL) 3804 { 3805 if (mch_isdir(dflt)) /* default file name is a directory */ 3806 { 3807 initdir = dflt; 3808 dflt = NULL; 3809 } 3810 else if (gettail(dflt) != dflt) /* default file name includes a path */ 3811 { 3812 tofree = vim_strsave(dflt); 3813 if (tofree != NULL) 3814 { 3815 initdir = tofree; 3816 *gettail(initdir) = NUL; 3817 dflt = gettail(dflt); 3818 } 3819 } 3820 } 3821 3822 if (initdir == NULL || *initdir == NUL) 3823 { 3824 /* When 'browsedir' is a directory, use it */ 3825 if (STRCMP(p_bsdir, "last") != 0 3826 && STRCMP(p_bsdir, "buffer") != 0 3827 && STRCMP(p_bsdir, "current") != 0 3828 && mch_isdir(p_bsdir)) 3829 initdir = p_bsdir; 3830 /* When saving or 'browsedir' is "buffer", use buffer fname */ 3831 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b') 3832 && buf != NULL && buf->b_ffname != NULL) 3833 { 3834 if (dflt == NULL || *dflt == NUL) 3835 dflt = gettail(curbuf->b_ffname); 3836 tofree = vim_strsave(curbuf->b_ffname); 3837 if (tofree != NULL) 3838 { 3839 initdir = tofree; 3840 *gettail(initdir) = NUL; 3841 } 3842 } 3843 /* When 'browsedir' is "last", use dir from last browse */ 3844 else if (*p_bsdir == 'l') 3845 initdir = last_dir; 3846 /* When 'browsedir is "current", use current directory. This is the 3847 * default already, leave initdir empty. */ 3848 } 3849 3850 # ifdef FEAT_GUI 3851 if (gui.in_use) /* when this changes, also adjust f_has()! */ 3852 { 3853 if (filter == NULL 3854 # ifdef FEAT_EVAL 3855 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL 3856 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL 3857 # endif 3858 ) 3859 filter = BROWSE_FILTER_DEFAULT; 3860 if (flags & BROWSE_DIR) 3861 { 3862 # if defined(FEAT_GUI_GTK) || defined(WIN3264) 3863 /* For systems that have a directory dialog. */ 3864 fname = gui_mch_browsedir(title, initdir); 3865 # else 3866 /* Generic solution for selecting a directory: select a file and 3867 * remove the file name. */ 3868 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)""); 3869 # endif 3870 # if !defined(FEAT_GUI_GTK) 3871 /* Win32 adds a dummy file name, others return an arbitrary file 3872 * name. GTK+ 2 returns only the directory, */ 3873 if (fname != NULL && *fname != NUL && !mch_isdir(fname)) 3874 { 3875 /* Remove the file name. */ 3876 char_u *tail = gettail_sep(fname); 3877 3878 if (tail == fname) 3879 *tail++ = '.'; /* use current dir */ 3880 *tail = NUL; 3881 } 3882 # endif 3883 } 3884 else 3885 fname = gui_mch_browse(flags & BROWSE_SAVE, 3886 title, dflt, ext, initdir, filter); 3887 3888 /* We hang around in the dialog for a while, the user might do some 3889 * things to our files. The Win32 dialog allows deleting or renaming 3890 * a file, check timestamps. */ 3891 need_check_timestamps = TRUE; 3892 did_check_timestamps = FALSE; 3893 } 3894 else 3895 # endif 3896 { 3897 /* TODO: non-GUI file selector here */ 3898 EMSG(_("E338: Sorry, no file browser in console mode")); 3899 fname = NULL; 3900 } 3901 3902 /* keep the directory for next time */ 3903 if (fname != NULL) 3904 { 3905 vim_free(last_dir); 3906 last_dir = vim_strsave(fname); 3907 if (last_dir != NULL && !(flags & BROWSE_DIR)) 3908 { 3909 *gettail(last_dir) = NUL; 3910 if (*last_dir == NUL) 3911 { 3912 /* filename only returned, must be in current dir */ 3913 vim_free(last_dir); 3914 last_dir = alloc(MAXPATHL); 3915 if (last_dir != NULL) 3916 mch_dirname(last_dir, MAXPATHL); 3917 } 3918 } 3919 } 3920 3921 vim_free(tofree); 3922 cmdmod.browse = save_browse; 3923 3924 return fname; 3925 } 3926 #endif 3927 3928 #if defined(HAVE_STDARG_H) && defined(FEAT_EVAL) 3929 static char *e_printf = N_("E766: Insufficient arguments for printf()"); 3930 3931 static long tv_nr __ARGS((typval_T *tvs, int *idxp)); 3932 static char *tv_str __ARGS((typval_T *tvs, int *idxp)); 3933 # ifdef FEAT_FLOAT 3934 static double tv_float __ARGS((typval_T *tvs, int *idxp)); 3935 # endif 3936 3937 /* 3938 * Get number argument from "idxp" entry in "tvs". First entry is 1. 3939 */ 3940 static long 3941 tv_nr(tvs, idxp) 3942 typval_T *tvs; 3943 int *idxp; 3944 { 3945 int idx = *idxp - 1; 3946 long n = 0; 3947 int err = FALSE; 3948 3949 if (tvs[idx].v_type == VAR_UNKNOWN) 3950 EMSG(_(e_printf)); 3951 else 3952 { 3953 ++*idxp; 3954 n = get_tv_number_chk(&tvs[idx], &err); 3955 if (err) 3956 n = 0; 3957 } 3958 return n; 3959 } 3960 3961 /* 3962 * Get string argument from "idxp" entry in "tvs". First entry is 1. 3963 * Returns NULL for an error. 3964 */ 3965 static char * 3966 tv_str(tvs, idxp) 3967 typval_T *tvs; 3968 int *idxp; 3969 { 3970 int idx = *idxp - 1; 3971 char *s = NULL; 3972 3973 if (tvs[idx].v_type == VAR_UNKNOWN) 3974 EMSG(_(e_printf)); 3975 else 3976 { 3977 ++*idxp; 3978 s = (char *)get_tv_string_chk(&tvs[idx]); 3979 } 3980 return s; 3981 } 3982 3983 # ifdef FEAT_FLOAT 3984 /* 3985 * Get float argument from "idxp" entry in "tvs". First entry is 1. 3986 */ 3987 static double 3988 tv_float(tvs, idxp) 3989 typval_T *tvs; 3990 int *idxp; 3991 { 3992 int idx = *idxp - 1; 3993 double f = 0; 3994 3995 if (tvs[idx].v_type == VAR_UNKNOWN) 3996 EMSG(_(e_printf)); 3997 else 3998 { 3999 ++*idxp; 4000 if (tvs[idx].v_type == VAR_FLOAT) 4001 f = tvs[idx].vval.v_float; 4002 else if (tvs[idx].v_type == VAR_NUMBER) 4003 f = tvs[idx].vval.v_number; 4004 else 4005 EMSG(_("E807: Expected Float argument for printf()")); 4006 } 4007 return f; 4008 } 4009 # endif 4010 #endif 4011 4012 /* 4013 * This code was included to provide a portable vsnprintf() and snprintf(). 4014 * Some systems may provide their own, but we always use this one for 4015 * consistency. 4016 * 4017 * This code is based on snprintf.c - a portable implementation of snprintf 4018 * by Mark Martinec <[email protected]>, Version 2.2, 2000-10-06. 4019 * Included with permission. It was heavily modified to fit in Vim. 4020 * The original code, including useful comments, can be found here: 4021 * http://www.ijs.si/software/snprintf/ 4022 * 4023 * This snprintf() only supports the following conversion specifiers: 4024 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below) 4025 * with flags: '-', '+', ' ', '0' and '#'. 4026 * An asterisk is supported for field width as well as precision. 4027 * 4028 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'. 4029 * 4030 * Length modifiers 'h' (short int) and 'l' (long int) are supported. 4031 * 'll' (long long int) is not supported. 4032 * 4033 * The locale is not used, the string is used as a byte string. This is only 4034 * relevant for double-byte encodings where the second byte may be '%'. 4035 * 4036 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL 4037 * pointer for resulting string argument if "str_m" is zero (as per ISO C99). 4038 * 4039 * The return value is the number of characters which would be generated 4040 * for the given input, excluding the trailing NUL. If this value 4041 * is greater or equal to "str_m", not all characters from the result 4042 * have been stored in str, output bytes beyond the ("str_m"-1) -th character 4043 * are discarded. If "str_m" is greater than zero it is guaranteed 4044 * the resulting string will be NUL-terminated. 4045 */ 4046 4047 /* 4048 * When va_list is not supported we only define vim_snprintf(). 4049 * 4050 * vim_vsnprintf() can be invoked with either "va_list" or a list of 4051 * "typval_T". When the latter is not used it must be NULL. 4052 */ 4053 4054 /* When generating prototypes all of this is skipped, cproto doesn't 4055 * understand this. */ 4056 #ifndef PROTO 4057 4058 # ifdef HAVE_STDARG_H 4059 /* Like vim_vsnprintf() but append to the string. */ 4060 int 4061 vim_snprintf_add(char *str, size_t str_m, char *fmt, ...) 4062 { 4063 va_list ap; 4064 int str_l; 4065 size_t len = STRLEN(str); 4066 size_t space; 4067 4068 if (str_m <= len) 4069 space = 0; 4070 else 4071 space = str_m - len; 4072 va_start(ap, fmt); 4073 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL); 4074 va_end(ap); 4075 return str_l; 4076 } 4077 # else 4078 /* Like vim_vsnprintf() but append to the string. */ 4079 int 4080 vim_snprintf_add(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) 4081 char *str; 4082 size_t str_m; 4083 char *fmt; 4084 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10; 4085 { 4086 size_t len = STRLEN(str); 4087 size_t space; 4088 4089 if (str_m <= len) 4090 space = 0; 4091 else 4092 space = str_m - len; 4093 return vim_vsnprintf(str + len, space, fmt, 4094 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); 4095 } 4096 # endif 4097 4098 # ifdef HAVE_STDARG_H 4099 int 4100 vim_snprintf(char *str, size_t str_m, char *fmt, ...) 4101 { 4102 va_list ap; 4103 int str_l; 4104 4105 va_start(ap, fmt); 4106 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL); 4107 va_end(ap); 4108 return str_l; 4109 } 4110 4111 int 4112 vim_vsnprintf(str, str_m, fmt, ap, tvs) 4113 # else 4114 /* clumsy way to work around missing va_list */ 4115 # 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) 4116 4117 /* VARARGS */ 4118 int 4119 #ifdef __BORLANDC__ 4120 _RTLENTRYF 4121 #endif 4122 vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) 4123 # endif 4124 char *str; 4125 size_t str_m; 4126 char *fmt; 4127 # ifdef HAVE_STDARG_H 4128 va_list ap; 4129 typval_T *tvs; 4130 # else 4131 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10; 4132 # endif 4133 { 4134 size_t str_l = 0; 4135 char *p = fmt; 4136 int arg_idx = 1; 4137 4138 if (p == NULL) 4139 p = ""; 4140 while (*p != NUL) 4141 { 4142 if (*p != '%') 4143 { 4144 char *q = strchr(p + 1, '%'); 4145 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p); 4146 4147 /* Copy up to the next '%' or NUL without any changes. */ 4148 if (str_l < str_m) 4149 { 4150 size_t avail = str_m - str_l; 4151 4152 mch_memmove(str + str_l, p, n > avail ? avail : n); 4153 } 4154 p += n; 4155 str_l += n; 4156 } 4157 else 4158 { 4159 size_t min_field_width = 0, precision = 0; 4160 int zero_padding = 0, precision_specified = 0, justify_left = 0; 4161 int alternate_form = 0, force_sign = 0; 4162 4163 /* If both the ' ' and '+' flags appear, the ' ' flag should be 4164 * ignored. */ 4165 int space_for_positive = 1; 4166 4167 /* allowed values: \0, h, l, L */ 4168 char length_modifier = '\0'; 4169 4170 /* temporary buffer for simple numeric->string conversion */ 4171 #ifdef FEAT_FLOAT 4172 # define TMP_LEN 350 /* On my system 1e308 is the biggest number possible. 4173 * That sounds reasonable to use as the maximum 4174 * printable. */ 4175 #else 4176 # define TMP_LEN 32 4177 #endif 4178 char tmp[TMP_LEN]; 4179 4180 /* string address in case of string argument */ 4181 char *str_arg; 4182 4183 /* natural field width of arg without padding and sign */ 4184 size_t str_arg_l; 4185 4186 /* unsigned char argument value - only defined for c conversion. 4187 * N.B. standard explicitly states the char argument for the c 4188 * conversion is unsigned */ 4189 unsigned char uchar_arg; 4190 4191 /* number of zeros to be inserted for numeric conversions as 4192 * required by the precision or minimal field width */ 4193 size_t number_of_zeros_to_pad = 0; 4194 4195 /* index into tmp where zero padding is to be inserted */ 4196 size_t zero_padding_insertion_ind = 0; 4197 4198 /* current conversion specifier character */ 4199 char fmt_spec = '\0'; 4200 4201 str_arg = NULL; 4202 p++; /* skip '%' */ 4203 4204 /* parse flags */ 4205 while (*p == '0' || *p == '-' || *p == '+' || *p == ' ' 4206 || *p == '#' || *p == '\'') 4207 { 4208 switch (*p) 4209 { 4210 case '0': zero_padding = 1; break; 4211 case '-': justify_left = 1; break; 4212 case '+': force_sign = 1; space_for_positive = 0; break; 4213 case ' ': force_sign = 1; 4214 /* If both the ' ' and '+' flags appear, the ' ' 4215 * flag should be ignored */ 4216 break; 4217 case '#': alternate_form = 1; break; 4218 case '\'': break; 4219 } 4220 p++; 4221 } 4222 /* If the '0' and '-' flags both appear, the '0' flag should be 4223 * ignored. */ 4224 4225 /* parse field width */ 4226 if (*p == '*') 4227 { 4228 int j; 4229 4230 p++; 4231 j = 4232 #ifndef HAVE_STDARG_H 4233 get_a_arg(arg_idx); 4234 #else 4235 # if defined(FEAT_EVAL) 4236 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4237 # endif 4238 va_arg(ap, int); 4239 #endif 4240 if (j >= 0) 4241 min_field_width = j; 4242 else 4243 { 4244 min_field_width = -j; 4245 justify_left = 1; 4246 } 4247 } 4248 else if (VIM_ISDIGIT((int)(*p))) 4249 { 4250 /* size_t could be wider than unsigned int; make sure we treat 4251 * argument like common implementations do */ 4252 unsigned int uj = *p++ - '0'; 4253 4254 while (VIM_ISDIGIT((int)(*p))) 4255 uj = 10 * uj + (unsigned int)(*p++ - '0'); 4256 min_field_width = uj; 4257 } 4258 4259 /* parse precision */ 4260 if (*p == '.') 4261 { 4262 p++; 4263 precision_specified = 1; 4264 if (*p == '*') 4265 { 4266 int j; 4267 4268 j = 4269 #ifndef HAVE_STDARG_H 4270 get_a_arg(arg_idx); 4271 #else 4272 # if defined(FEAT_EVAL) 4273 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4274 # endif 4275 va_arg(ap, int); 4276 #endif 4277 p++; 4278 if (j >= 0) 4279 precision = j; 4280 else 4281 { 4282 precision_specified = 0; 4283 precision = 0; 4284 } 4285 } 4286 else if (VIM_ISDIGIT((int)(*p))) 4287 { 4288 /* size_t could be wider than unsigned int; make sure we 4289 * treat argument like common implementations do */ 4290 unsigned int uj = *p++ - '0'; 4291 4292 while (VIM_ISDIGIT((int)(*p))) 4293 uj = 10 * uj + (unsigned int)(*p++ - '0'); 4294 precision = uj; 4295 } 4296 } 4297 4298 /* parse 'h', 'l' and 'll' length modifiers */ 4299 if (*p == 'h' || *p == 'l') 4300 { 4301 length_modifier = *p; 4302 p++; 4303 if (length_modifier == 'l' && *p == 'l') 4304 { 4305 /* double l = long long */ 4306 length_modifier = 'l'; /* treat it as a single 'l' */ 4307 p++; 4308 } 4309 } 4310 fmt_spec = *p; 4311 4312 /* common synonyms: */ 4313 switch (fmt_spec) 4314 { 4315 case 'i': fmt_spec = 'd'; break; 4316 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break; 4317 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break; 4318 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break; 4319 case 'F': fmt_spec = 'f'; break; 4320 default: break; 4321 } 4322 4323 /* get parameter value, do initial processing */ 4324 switch (fmt_spec) 4325 { 4326 /* '%' and 'c' behave similar to 's' regarding flags and field 4327 * widths */ 4328 case '%': 4329 case 'c': 4330 case 's': 4331 case 'S': 4332 length_modifier = '\0'; 4333 str_arg_l = 1; 4334 switch (fmt_spec) 4335 { 4336 case '%': 4337 str_arg = p; 4338 break; 4339 4340 case 'c': 4341 { 4342 int j; 4343 4344 j = 4345 #ifndef HAVE_STDARG_H 4346 get_a_arg(arg_idx); 4347 #else 4348 # if defined(FEAT_EVAL) 4349 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4350 # endif 4351 va_arg(ap, int); 4352 #endif 4353 /* standard demands unsigned char */ 4354 uchar_arg = (unsigned char)j; 4355 str_arg = (char *)&uchar_arg; 4356 break; 4357 } 4358 4359 case 's': 4360 case 'S': 4361 str_arg = 4362 #ifndef HAVE_STDARG_H 4363 (char *)get_a_arg(arg_idx); 4364 #else 4365 # if defined(FEAT_EVAL) 4366 tvs != NULL ? tv_str(tvs, &arg_idx) : 4367 # endif 4368 va_arg(ap, char *); 4369 #endif 4370 if (str_arg == NULL) 4371 { 4372 str_arg = "[NULL]"; 4373 str_arg_l = 6; 4374 } 4375 /* make sure not to address string beyond the specified 4376 * precision !!! */ 4377 else if (!precision_specified) 4378 str_arg_l = strlen(str_arg); 4379 /* truncate string if necessary as requested by precision */ 4380 else if (precision == 0) 4381 str_arg_l = 0; 4382 else 4383 { 4384 /* Don't put the #if inside memchr(), it can be a 4385 * macro. */ 4386 #if VIM_SIZEOF_INT <= 2 4387 char *q = memchr(str_arg, '\0', precision); 4388 #else 4389 /* memchr on HP does not like n > 2^31 !!! */ 4390 char *q = memchr(str_arg, '\0', 4391 precision <= (size_t)0x7fffffffL ? precision 4392 : (size_t)0x7fffffffL); 4393 #endif 4394 str_arg_l = (q == NULL) ? precision 4395 : (size_t)(q - str_arg); 4396 } 4397 #ifdef FEAT_MBYTE 4398 if (fmt_spec == 'S') 4399 { 4400 if (min_field_width != 0) 4401 min_field_width += STRLEN(str_arg) 4402 - mb_string2cells((char_u *)str_arg, -1); 4403 if (precision) 4404 { 4405 char_u *p1 = (char_u *)str_arg; 4406 size_t i; 4407 4408 for (i = 0; i < precision && *p1; i++) 4409 p1 += mb_ptr2len(p1); 4410 4411 str_arg_l = precision = p1 - (char_u *)str_arg; 4412 } 4413 } 4414 #endif 4415 break; 4416 4417 default: 4418 break; 4419 } 4420 break; 4421 4422 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p': 4423 { 4424 /* NOTE: the u, o, x, X and p conversion specifiers 4425 * imply the value is unsigned; d implies a signed 4426 * value */ 4427 4428 /* 0 if numeric argument is zero (or if pointer is 4429 * NULL for 'p'), +1 if greater than zero (or nonzero 4430 * for unsigned arguments), -1 if negative (unsigned 4431 * argument is never negative) */ 4432 int arg_sign = 0; 4433 4434 /* only defined for length modifier h, or for no 4435 * length modifiers */ 4436 int int_arg = 0; 4437 unsigned int uint_arg = 0; 4438 4439 /* only defined for length modifier l */ 4440 long int long_arg = 0; 4441 unsigned long int ulong_arg = 0; 4442 4443 /* pointer argument value -only defined for p 4444 * conversion */ 4445 void *ptr_arg = NULL; 4446 4447 if (fmt_spec == 'p') 4448 { 4449 length_modifier = '\0'; 4450 ptr_arg = 4451 #ifndef HAVE_STDARG_H 4452 (void *)get_a_arg(arg_idx); 4453 #else 4454 # if defined(FEAT_EVAL) 4455 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) : 4456 # endif 4457 va_arg(ap, void *); 4458 #endif 4459 if (ptr_arg != NULL) 4460 arg_sign = 1; 4461 } 4462 else if (fmt_spec == 'd') 4463 { 4464 /* signed */ 4465 switch (length_modifier) 4466 { 4467 case '\0': 4468 case 'h': 4469 /* char and short arguments are passed as int. */ 4470 int_arg = 4471 #ifndef HAVE_STDARG_H 4472 get_a_arg(arg_idx); 4473 #else 4474 # if defined(FEAT_EVAL) 4475 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4476 # endif 4477 va_arg(ap, int); 4478 #endif 4479 if (int_arg > 0) 4480 arg_sign = 1; 4481 else if (int_arg < 0) 4482 arg_sign = -1; 4483 break; 4484 case 'l': 4485 long_arg = 4486 #ifndef HAVE_STDARG_H 4487 get_a_arg(arg_idx); 4488 #else 4489 # if defined(FEAT_EVAL) 4490 tvs != NULL ? tv_nr(tvs, &arg_idx) : 4491 # endif 4492 va_arg(ap, long int); 4493 #endif 4494 if (long_arg > 0) 4495 arg_sign = 1; 4496 else if (long_arg < 0) 4497 arg_sign = -1; 4498 break; 4499 } 4500 } 4501 else 4502 { 4503 /* unsigned */ 4504 switch (length_modifier) 4505 { 4506 case '\0': 4507 case 'h': 4508 uint_arg = 4509 #ifndef HAVE_STDARG_H 4510 get_a_arg(arg_idx); 4511 #else 4512 # if defined(FEAT_EVAL) 4513 tvs != NULL ? (unsigned) 4514 tv_nr(tvs, &arg_idx) : 4515 # endif 4516 va_arg(ap, unsigned int); 4517 #endif 4518 if (uint_arg != 0) 4519 arg_sign = 1; 4520 break; 4521 case 'l': 4522 ulong_arg = 4523 #ifndef HAVE_STDARG_H 4524 get_a_arg(arg_idx); 4525 #else 4526 # if defined(FEAT_EVAL) 4527 tvs != NULL ? (unsigned long) 4528 tv_nr(tvs, &arg_idx) : 4529 # endif 4530 va_arg(ap, unsigned long int); 4531 #endif 4532 if (ulong_arg != 0) 4533 arg_sign = 1; 4534 break; 4535 } 4536 } 4537 4538 str_arg = tmp; 4539 str_arg_l = 0; 4540 4541 /* NOTE: 4542 * For d, i, u, o, x, and X conversions, if precision is 4543 * specified, the '0' flag should be ignored. This is so 4544 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux, 4545 * FreeBSD, NetBSD; but not with Perl. 4546 */ 4547 if (precision_specified) 4548 zero_padding = 0; 4549 if (fmt_spec == 'd') 4550 { 4551 if (force_sign && arg_sign >= 0) 4552 tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; 4553 /* leave negative numbers for sprintf to handle, to 4554 * avoid handling tricky cases like (short int)-32768 */ 4555 } 4556 else if (alternate_form) 4557 { 4558 if (arg_sign != 0 4559 && (fmt_spec == 'x' || fmt_spec == 'X') ) 4560 { 4561 tmp[str_arg_l++] = '0'; 4562 tmp[str_arg_l++] = fmt_spec; 4563 } 4564 /* alternate form should have no effect for p 4565 * conversion, but ... */ 4566 } 4567 4568 zero_padding_insertion_ind = str_arg_l; 4569 if (!precision_specified) 4570 precision = 1; /* default precision is 1 */ 4571 if (precision == 0 && arg_sign == 0) 4572 { 4573 /* When zero value is formatted with an explicit 4574 * precision 0, the resulting formatted string is 4575 * empty (d, i, u, o, x, X, p). */ 4576 } 4577 else 4578 { 4579 char f[5]; 4580 int f_l = 0; 4581 4582 /* construct a simple format string for sprintf */ 4583 f[f_l++] = '%'; 4584 if (!length_modifier) 4585 ; 4586 else if (length_modifier == '2') 4587 { 4588 f[f_l++] = 'l'; 4589 f[f_l++] = 'l'; 4590 } 4591 else 4592 f[f_l++] = length_modifier; 4593 f[f_l++] = fmt_spec; 4594 f[f_l++] = '\0'; 4595 4596 if (fmt_spec == 'p') 4597 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg); 4598 else if (fmt_spec == 'd') 4599 { 4600 /* signed */ 4601 switch (length_modifier) 4602 { 4603 case '\0': 4604 case 'h': str_arg_l += sprintf( 4605 tmp + str_arg_l, f, int_arg); 4606 break; 4607 case 'l': str_arg_l += sprintf( 4608 tmp + str_arg_l, f, long_arg); 4609 break; 4610 } 4611 } 4612 else 4613 { 4614 /* unsigned */ 4615 switch (length_modifier) 4616 { 4617 case '\0': 4618 case 'h': str_arg_l += sprintf( 4619 tmp + str_arg_l, f, uint_arg); 4620 break; 4621 case 'l': str_arg_l += sprintf( 4622 tmp + str_arg_l, f, ulong_arg); 4623 break; 4624 } 4625 } 4626 4627 /* include the optional minus sign and possible 4628 * "0x" in the region before the zero padding 4629 * insertion point */ 4630 if (zero_padding_insertion_ind < str_arg_l 4631 && tmp[zero_padding_insertion_ind] == '-') 4632 zero_padding_insertion_ind++; 4633 if (zero_padding_insertion_ind + 1 < str_arg_l 4634 && tmp[zero_padding_insertion_ind] == '0' 4635 && (tmp[zero_padding_insertion_ind + 1] == 'x' 4636 || tmp[zero_padding_insertion_ind + 1] == 'X')) 4637 zero_padding_insertion_ind += 2; 4638 } 4639 4640 { 4641 size_t num_of_digits = str_arg_l 4642 - zero_padding_insertion_ind; 4643 4644 if (alternate_form && fmt_spec == 'o' 4645 /* unless zero is already the first 4646 * character */ 4647 && !(zero_padding_insertion_ind < str_arg_l 4648 && tmp[zero_padding_insertion_ind] == '0')) 4649 { 4650 /* assure leading zero for alternate-form 4651 * octal numbers */ 4652 if (!precision_specified 4653 || precision < num_of_digits + 1) 4654 { 4655 /* precision is increased to force the 4656 * first character to be zero, except if a 4657 * zero value is formatted with an 4658 * explicit precision of zero */ 4659 precision = num_of_digits + 1; 4660 precision_specified = 1; 4661 } 4662 } 4663 /* zero padding to specified precision? */ 4664 if (num_of_digits < precision) 4665 number_of_zeros_to_pad = precision - num_of_digits; 4666 } 4667 /* zero padding to specified minimal field width? */ 4668 if (!justify_left && zero_padding) 4669 { 4670 int n = (int)(min_field_width - (str_arg_l 4671 + number_of_zeros_to_pad)); 4672 if (n > 0) 4673 number_of_zeros_to_pad += n; 4674 } 4675 break; 4676 } 4677 4678 #ifdef FEAT_FLOAT 4679 case 'f': 4680 case 'e': 4681 case 'E': 4682 case 'g': 4683 case 'G': 4684 { 4685 /* Floating point. */ 4686 double f; 4687 double abs_f; 4688 char format[40]; 4689 int l; 4690 int remove_trailing_zeroes = FALSE; 4691 4692 f = 4693 # ifndef HAVE_STDARG_H 4694 get_a_arg(arg_idx); 4695 # else 4696 # if defined(FEAT_EVAL) 4697 tvs != NULL ? tv_float(tvs, &arg_idx) : 4698 # endif 4699 va_arg(ap, double); 4700 # endif 4701 abs_f = f < 0 ? -f : f; 4702 4703 if (fmt_spec == 'g' || fmt_spec == 'G') 4704 { 4705 /* Would be nice to use %g directly, but it prints 4706 * "1.0" as "1", we don't want that. */ 4707 if ((abs_f >= 0.001 && abs_f < 10000000.0) 4708 || abs_f == 0.0) 4709 fmt_spec = 'f'; 4710 else 4711 fmt_spec = fmt_spec == 'g' ? 'e' : 'E'; 4712 remove_trailing_zeroes = TRUE; 4713 } 4714 4715 if (fmt_spec == 'f' && 4716 #ifdef VAX 4717 abs_f > 1.0e38 4718 #else 4719 abs_f > 1.0e307 4720 #endif 4721 ) 4722 { 4723 /* Avoid a buffer overflow */ 4724 strcpy(tmp, "inf"); 4725 str_arg_l = 3; 4726 } 4727 else 4728 { 4729 format[0] = '%'; 4730 l = 1; 4731 if (precision_specified) 4732 { 4733 size_t max_prec = TMP_LEN - 10; 4734 4735 /* Make sure we don't get more digits than we 4736 * have room for. */ 4737 if (fmt_spec == 'f' && abs_f > 1.0) 4738 max_prec -= (size_t)log10(abs_f); 4739 if (precision > max_prec) 4740 precision = max_prec; 4741 l += sprintf(format + 1, ".%d", (int)precision); 4742 } 4743 format[l] = fmt_spec; 4744 format[l + 1] = NUL; 4745 str_arg_l = sprintf(tmp, format, f); 4746 4747 if (remove_trailing_zeroes) 4748 { 4749 int i; 4750 char *tp; 4751 4752 /* Using %g or %G: remove superfluous zeroes. */ 4753 if (fmt_spec == 'f') 4754 tp = tmp + str_arg_l - 1; 4755 else 4756 { 4757 tp = (char *)vim_strchr((char_u *)tmp, 4758 fmt_spec == 'e' ? 'e' : 'E'); 4759 if (tp != NULL) 4760 { 4761 /* Remove superfluous '+' and leading 4762 * zeroes from the exponent. */ 4763 if (tp[1] == '+') 4764 { 4765 /* Change "1.0e+07" to "1.0e07" */ 4766 STRMOVE(tp + 1, tp + 2); 4767 --str_arg_l; 4768 } 4769 i = (tp[1] == '-') ? 2 : 1; 4770 while (tp[i] == '0') 4771 { 4772 /* Change "1.0e07" to "1.0e7" */ 4773 STRMOVE(tp + i, tp + i + 1); 4774 --str_arg_l; 4775 } 4776 --tp; 4777 } 4778 } 4779 4780 if (tp != NULL && !precision_specified) 4781 /* Remove trailing zeroes, but keep the one 4782 * just after a dot. */ 4783 while (tp > tmp + 2 && *tp == '0' 4784 && tp[-1] != '.') 4785 { 4786 STRMOVE(tp, tp + 1); 4787 --tp; 4788 --str_arg_l; 4789 } 4790 } 4791 else 4792 { 4793 char *tp; 4794 4795 /* Be consistent: some printf("%e") use 1.0e+12 4796 * and some 1.0e+012. Remove one zero in the last 4797 * case. */ 4798 tp = (char *)vim_strchr((char_u *)tmp, 4799 fmt_spec == 'e' ? 'e' : 'E'); 4800 if (tp != NULL && (tp[1] == '+' || tp[1] == '-') 4801 && tp[2] == '0' 4802 && vim_isdigit(tp[3]) 4803 && vim_isdigit(tp[4])) 4804 { 4805 STRMOVE(tp + 2, tp + 3); 4806 --str_arg_l; 4807 } 4808 } 4809 } 4810 str_arg = tmp; 4811 break; 4812 } 4813 #endif 4814 4815 default: 4816 /* unrecognized conversion specifier, keep format string 4817 * as-is */ 4818 zero_padding = 0; /* turn zero padding off for non-numeric 4819 conversion */ 4820 justify_left = 1; 4821 min_field_width = 0; /* reset flags */ 4822 4823 /* discard the unrecognized conversion, just keep * 4824 * the unrecognized conversion character */ 4825 str_arg = p; 4826 str_arg_l = 0; 4827 if (*p != NUL) 4828 str_arg_l++; /* include invalid conversion specifier 4829 unchanged if not at end-of-string */ 4830 break; 4831 } 4832 4833 if (*p != NUL) 4834 p++; /* step over the just processed conversion specifier */ 4835 4836 /* insert padding to the left as requested by min_field_width; 4837 * this does not include the zero padding in case of numerical 4838 * conversions*/ 4839 if (!justify_left) 4840 { 4841 /* left padding with blank or zero */ 4842 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad)); 4843 4844 if (pn > 0) 4845 { 4846 if (str_l < str_m) 4847 { 4848 size_t avail = str_m - str_l; 4849 4850 vim_memset(str + str_l, zero_padding ? '0' : ' ', 4851 (size_t)pn > avail ? avail 4852 : (size_t)pn); 4853 } 4854 str_l += pn; 4855 } 4856 } 4857 4858 /* zero padding as requested by the precision or by the minimal 4859 * field width for numeric conversions required? */ 4860 if (number_of_zeros_to_pad == 0) 4861 { 4862 /* will not copy first part of numeric right now, * 4863 * force it to be copied later in its entirety */ 4864 zero_padding_insertion_ind = 0; 4865 } 4866 else 4867 { 4868 /* insert first part of numerics (sign or '0x') before zero 4869 * padding */ 4870 int zn = (int)zero_padding_insertion_ind; 4871 4872 if (zn > 0) 4873 { 4874 if (str_l < str_m) 4875 { 4876 size_t avail = str_m - str_l; 4877 4878 mch_memmove(str + str_l, str_arg, 4879 (size_t)zn > avail ? avail 4880 : (size_t)zn); 4881 } 4882 str_l += zn; 4883 } 4884 4885 /* insert zero padding as requested by the precision or min 4886 * field width */ 4887 zn = (int)number_of_zeros_to_pad; 4888 if (zn > 0) 4889 { 4890 if (str_l < str_m) 4891 { 4892 size_t avail = str_m-str_l; 4893 4894 vim_memset(str + str_l, '0', 4895 (size_t)zn > avail ? avail 4896 : (size_t)zn); 4897 } 4898 str_l += zn; 4899 } 4900 } 4901 4902 /* insert formatted string 4903 * (or as-is conversion specifier for unknown conversions) */ 4904 { 4905 int sn = (int)(str_arg_l - zero_padding_insertion_ind); 4906 4907 if (sn > 0) 4908 { 4909 if (str_l < str_m) 4910 { 4911 size_t avail = str_m - str_l; 4912 4913 mch_memmove(str + str_l, 4914 str_arg + zero_padding_insertion_ind, 4915 (size_t)sn > avail ? avail : (size_t)sn); 4916 } 4917 str_l += sn; 4918 } 4919 } 4920 4921 /* insert right padding */ 4922 if (justify_left) 4923 { 4924 /* right blank padding to the field width */ 4925 int pn = (int)(min_field_width 4926 - (str_arg_l + number_of_zeros_to_pad)); 4927 4928 if (pn > 0) 4929 { 4930 if (str_l < str_m) 4931 { 4932 size_t avail = str_m - str_l; 4933 4934 vim_memset(str + str_l, ' ', 4935 (size_t)pn > avail ? avail 4936 : (size_t)pn); 4937 } 4938 str_l += pn; 4939 } 4940 } 4941 } 4942 } 4943 4944 if (str_m > 0) 4945 { 4946 /* make sure the string is nul-terminated even at the expense of 4947 * overwriting the last character (shouldn't happen, but just in case) 4948 * */ 4949 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0'; 4950 } 4951 4952 #ifdef HAVE_STDARG_H 4953 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN) 4954 EMSG(_("E767: Too many arguments to printf()")); 4955 #endif 4956 4957 /* Return the number of characters formatted (excluding trailing nul 4958 * character), that is, the number of characters that would have been 4959 * written to the buffer if it were large enough. */ 4960 return (int)str_l; 4961 } 4962 4963 #endif /* PROTO */ 4964