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