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