Sync with newest CVS repository.
[MacVim/jjgod.git] / src / message.c
blobc0146c962a08bf58497a803a95aaaa94aac88c48
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * message.c: functions for displaying messages on the command line
14 #define MESSAGE_FILE /* don't include prototype for smsg() */
16 #include "vim.h"
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 inc_msg_scrolled __ARGS((void));
31 static void store_sb_text __ARGS((char_u **sb_str, char_u *s, int attr, int *sb_col, int finish));
32 static void t_puts __ARGS((int *t_col, char_u *t_s, char_u *s, int attr));
33 static void msg_puts_printf __ARGS((char_u *str, int maxlen));
34 static int do_more_prompt __ARGS((int typed_char));
35 static void msg_screen_putchar __ARGS((int c, int attr));
36 static int msg_check_screen __ARGS((void));
37 static void redir_write __ARGS((char_u *s, int maxlen));
38 static void verbose_write __ARGS((char_u *s, int maxlen));
39 #ifdef FEAT_CON_DIALOG
40 static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
41 static int confirm_msg_used = FALSE; /* displaying confirm_msg */
42 static char_u *confirm_msg = NULL; /* ":confirm" message */
43 static char_u *confirm_msg_tail; /* tail of confirm_msg */
44 #endif
46 struct msg_hist
48 struct msg_hist *next;
49 char_u *msg;
50 int attr;
53 static struct msg_hist *first_msg_hist = NULL;
54 static struct msg_hist *last_msg_hist = NULL;
55 static int msg_hist_len = 0;
56 static int msg_hist_off = FALSE; /* don't add messages to history */
59 * When writing messages to the screen, there are many different situations.
60 * A number of variables is used to remember the current state:
61 * msg_didany TRUE when messages were written since the last time the
62 * user reacted to a prompt.
63 * Reset: After hitting a key for the hit-return prompt,
64 * hitting <CR> for the command line or input().
65 * Set: When any message is written to the screen.
66 * msg_didout TRUE when something was written to the current line.
67 * Reset: When advancing to the next line, when the current
68 * text can be overwritten.
69 * Set: When any message is written to the screen.
70 * msg_nowait No extra delay for the last drawn message.
71 * Used in normal_cmd() before the mode message is drawn.
72 * emsg_on_display There was an error message recently. Indicates that there
73 * should be a delay before redrawing.
74 * msg_scroll The next message should not overwrite the current one.
75 * msg_scrolled How many lines the screen has been scrolled (because of
76 * messages). Used in update_screen() to scroll the screen
77 * back. Incremented each time the screen scrolls a line.
78 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
79 * writes something without scrolling should not make
80 * need_wait_return to be set. This is a hack to make ":ts"
81 * work without an extra prompt.
82 * lines_left Number of lines available for messages before the
83 * more-prompt is to be given.
84 * need_wait_return TRUE when the hit-return prompt is needed.
85 * Reset: After giving the hit-return prompt, when the user
86 * has answered some other prompt.
87 * Set: When the ruler or typeahead display is overwritten,
88 * scrolling the screen for some message.
89 * keep_msg Message to be displayed after redrawing the screen, in
90 * main_loop().
91 * This is an allocated string or NULL when not used.
95 * msg(s) - displays the string 's' on the status line
96 * When terminal not initialized (yet) mch_errmsg(..) is used.
97 * return TRUE if wait_return not called
99 int
100 msg(s)
101 char_u *s;
103 return msg_attr_keep(s, 0, FALSE);
106 #if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
107 || defined(PROTO)
109 * Like msg() but keep it silent when 'verbosefile' is set.
112 verb_msg(s)
113 char_u *s;
115 int n;
117 verbose_enter();
118 n = msg_attr_keep(s, 0, FALSE);
119 verbose_leave();
121 return n;
123 #endif
126 msg_attr(s, attr)
127 char_u *s;
128 int attr;
130 return msg_attr_keep(s, attr, FALSE);
134 msg_attr_keep(s, attr, keep)
135 char_u *s;
136 int attr;
137 int keep; /* TRUE: set keep_msg if it doesn't scroll */
139 static int entered = 0;
140 int retval;
141 char_u *buf = NULL;
143 #ifdef FEAT_EVAL
144 if (attr == 0)
145 set_vim_var_string(VV_STATUSMSG, s, -1);
146 #endif
149 * It is possible that displaying a messages causes a problem (e.g.,
150 * when redrawing the window), which causes another message, etc.. To
151 * break this loop, limit the recursiveness to 3 levels.
153 if (entered >= 3)
154 return TRUE;
155 ++entered;
157 /* Add message to history (unless it's a repeated kept message or a
158 * truncated message) */
159 if (s != keep_msg
160 || (*s != '<'
161 && last_msg_hist != NULL
162 && last_msg_hist->msg != NULL
163 && STRCMP(s, last_msg_hist->msg)))
164 add_msg_hist(s, -1, attr);
166 /* When displaying keep_msg, don't let msg_start() free it, caller must do
167 * that. */
168 if (s == keep_msg)
169 keep_msg = NULL;
171 /* Truncate the message if needed. */
172 msg_start();
173 buf = msg_strtrunc(s, FALSE);
174 if (buf != NULL)
175 s = buf;
177 msg_outtrans_attr(s, attr);
178 msg_clr_eos();
179 retval = msg_end();
181 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
182 * Columns + sc_col)
183 set_keep_msg(s, 0);
185 vim_free(buf);
186 --entered;
187 return retval;
191 * Truncate a string such that it can be printed without causing a scroll.
192 * Returns an allocated string or NULL when no truncating is done.
194 char_u *
195 msg_strtrunc(s, force)
196 char_u *s;
197 int force; /* always truncate */
199 char_u *buf = NULL;
200 int len;
201 int room;
203 /* May truncate message to avoid a hit-return prompt */
204 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
205 && !exmode_active && msg_silent == 0) || force)
207 len = vim_strsize(s);
208 if (msg_scrolled != 0)
209 /* Use all the columns. */
210 room = (int)(Rows - msg_row) * Columns - 1;
211 else
212 /* Use up to 'showcmd' column. */
213 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
214 if (len > room && room > 0)
216 #ifdef FEAT_MBYTE
217 if (enc_utf8)
218 /* may have up to 18 bytes per cell (6 per char, up to two
219 * composing chars) */
220 buf = alloc((room + 2) * 18);
221 else if (enc_dbcs == DBCS_JPNU)
222 /* may have up to 2 bytes per cell for euc-jp */
223 buf = alloc((room + 2) * 2);
224 else
225 #endif
226 buf = alloc(room + 2);
227 if (buf != NULL)
228 trunc_string(s, buf, room);
231 return buf;
235 * Truncate a string "s" to "buf" with cell width "room".
236 * "s" and "buf" may be equal.
238 void
239 trunc_string(s, buf, room)
240 char_u *s;
241 char_u *buf;
242 int room;
244 int half;
245 int len;
246 int e;
247 int i;
248 int n;
250 room -= 3;
251 half = room / 2;
252 len = 0;
254 /* First part: Start of the string. */
255 for (e = 0; len < half; ++e)
257 if (s[e] == NUL)
259 /* text fits without truncating! */
260 buf[e] = NUL;
261 return;
263 n = ptr2cells(s + e);
264 if (len + n >= half)
265 break;
266 len += n;
267 buf[e] = s[e];
268 #ifdef FEAT_MBYTE
269 if (has_mbyte)
270 for (n = (*mb_ptr2len)(s + e); --n > 0; )
272 ++e;
273 buf[e] = s[e];
275 #endif
278 /* Last part: End of the string. */
279 i = e;
280 #ifdef FEAT_MBYTE
281 if (enc_dbcs != 0)
283 /* For DBCS going backwards in a string is slow, but
284 * computing the cell width isn't too slow: go forward
285 * until the rest fits. */
286 n = vim_strsize(s + i);
287 while (len + n > room)
289 n -= ptr2cells(s + i);
290 i += (*mb_ptr2len)(s + i);
293 else if (enc_utf8)
295 /* For UTF-8 we can go backwards easily. */
296 half = i = (int)STRLEN(s);
297 for (;;)
300 half = half - (*mb_head_off)(s, s + half - 1) - 1;
301 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
302 n = ptr2cells(s + half);
303 if (len + n > room)
304 break;
305 len += n;
306 i = half;
309 else
310 #endif
312 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
313 len += n;
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);
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!!!
326 #ifndef PROTO
327 # ifndef HAVE_STDARG_H
330 #ifdef __BORLANDC__
331 _RTLENTRYF
332 #endif
333 smsg __ARGS((char_u *, long, long, long,
334 long, long, long, long, long, long, long));
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));
342 int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
343 long, long, long, long, long, long, long));
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.
351 /* VARARGS */
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;
360 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
363 /* VARARGS */
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;
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);
378 # else /* HAVE_STDARG_H */
380 int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
383 #ifdef __BORLANDC__
384 _RTLENTRYF
385 #endif
386 smsg(char_u *s, ...)
388 va_list arglist;
390 va_start(arglist, s);
391 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
392 va_end(arglist);
393 return msg(IObuff);
397 #ifdef __BORLANDC__
398 _RTLENTRYF
399 #endif
400 smsg_attr(int attr, char_u *s, ...)
402 va_list arglist;
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);
410 # endif /* HAVE_STDARG_H */
411 #endif
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.
417 static int last_sourcing_lnum = 0;
418 static char_u *last_sourcing_name = NULL;
421 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
422 * for the next error message;
424 void
425 reset_last_sourcing()
427 vim_free(last_sourcing_name);
428 last_sourcing_name = NULL;
429 last_sourcing_lnum = 0;
433 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
435 static int
436 other_sourcing_name()
438 if (sourcing_name != NULL)
440 if (last_sourcing_name != NULL)
441 return STRCMP(sourcing_name, last_sourcing_name) != 0;
442 return TRUE;
444 return FALSE;
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.
452 static char_u *
453 get_emsg_source()
455 char_u *Buf, *p;
457 if (sourcing_name != NULL && other_sourcing_name())
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;
465 return NULL;
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.
473 static char_u *
474 get_emsg_lnum()
476 char_u *Buf, *p;
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)
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;
490 return NULL;
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.
498 void
499 msg_source(attr)
500 int attr;
502 char_u *p;
504 ++no_wait_return;
505 p = get_emsg_source();
506 if (p != NULL)
508 msg_attr(p, attr);
509 vim_free(p);
511 p = get_emsg_lnum();
512 if (p != NULL)
514 msg_attr(p, hl_attr(HLF_N));
515 vim_free(p);
516 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
519 /* remember the last sourcing name printed, also when it's empty */
520 if (sourcing_name == NULL || other_sourcing_name())
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);
528 --no_wait_return;
532 * emsg() - display an error message
534 * Rings the bell, if appropriate, and calls message() to do the real work
535 * When terminal not initialized (yet) mch_errmsg(..) is used.
537 * return TRUE if wait_return not called
540 emsg(s)
541 char_u *s;
543 int attr;
544 char_u *p;
545 #ifdef FEAT_EVAL
546 int ignore = FALSE;
547 int severe;
548 #endif
550 called_emsg = TRUE;
551 ex_exitval = 1;
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.
557 #ifdef FEAT_EVAL
558 severe = emsg_severe;
559 emsg_severe = FALSE;
560 #endif
563 * If "emsg_off" is set: no error messages at the moment.
564 * If "msg" is in 'debug': do error message but without side effects.
565 * If "emsg_skip" is set: never do error messages.
567 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
568 && vim_strchr(p_debug, 't') == NULL)
569 #ifdef FEAT_EVAL
570 || emsg_skip > 0
571 #endif
573 return TRUE;
575 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
577 #ifdef FEAT_EVAL
579 * Cause a throw of an error exception if appropriate. Don't display
580 * the error message in this case. (If no matching catch clause will
581 * be found, the message will be displayed later on.) "ignore" is set
582 * when the message should be ignored completely (used for the
583 * interrupt message).
585 if (cause_errthrow(s, severe, &ignore) == TRUE)
587 if (!ignore)
588 did_emsg = TRUE;
589 return TRUE;
592 /* set "v:errmsg", also when using ":silent! cmd" */
593 set_vim_var_string(VV_ERRMSG, s, -1);
594 #endif
597 * When using ":silent! cmd" ignore error messsages.
598 * But do write it to the redirection file.
600 if (emsg_silent != 0)
602 msg_start();
603 p = get_emsg_source();
604 if (p != NULL)
606 STRCAT(p, "\n");
607 redir_write(p, -1);
608 vim_free(p);
610 p = get_emsg_lnum();
611 if (p != NULL)
613 STRCAT(p, "\n");
614 redir_write(p, -1);
615 vim_free(p);
617 redir_write(s, -1);
618 return TRUE;
621 /* Reset msg_silent, an error causes messages to be switched back on. */
622 msg_silent = 0;
623 cmd_silent = FALSE;
625 if (global_busy) /* break :global command */
626 ++global_busy;
628 if (p_eb)
629 beep_flush(); /* also includes flush_buffers() */
630 else
631 flush_buffers(FALSE); /* flush internal buffers */
632 did_emsg = TRUE; /* flag for DoOneCmd() */
635 emsg_on_display = TRUE; /* remember there is an error message */
636 ++msg_scroll; /* don't overwrite a previous message */
637 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
638 if (msg_scrolled != 0)
639 need_wait_return = TRUE; /* needed in case emsg() is called after
640 * wait_return has reset need_wait_return
641 * and a redraw is expected because
642 * msg_scrolled is non-zero */
645 * Display name and line number for the source of the error.
647 msg_source(attr);
650 * Display the error message itself.
652 msg_nowait = FALSE; /* wait for this msg */
653 return msg_attr(s, attr);
657 * Print an error message with one "%s" and one string argument.
660 emsg2(s, a1)
661 char_u *s, *a1;
663 return emsg3(s, a1, NULL);
666 /* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
668 void
669 emsg_invreg(name)
670 int name;
672 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
676 * Like msg(), but truncate to a single line if p_shm contains 't', or when
677 * "force" is TRUE. This truncates in another way as for normal messages.
678 * Careful: The string may be changed by msg_may_trunc()!
679 * Returns a pointer to the printed message, if wait_return() not called.
681 char_u *
682 msg_trunc_attr(s, force, attr)
683 char_u *s;
684 int force;
685 int attr;
687 int n;
689 /* Add message to history before truncating */
690 add_msg_hist(s, -1, attr);
692 s = msg_may_trunc(force, s);
694 msg_hist_off = TRUE;
695 n = msg_attr(s, attr);
696 msg_hist_off = FALSE;
698 if (n)
699 return s;
700 return NULL;
704 * Check if message "s" should be truncated at the start (for filenames).
705 * Return a pointer to where the truncated message starts.
706 * Note: May change the message by replacing a character with '<'.
708 char_u *
709 msg_may_trunc(force, s)
710 int force;
711 char_u *s;
713 int n;
714 int room;
716 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
717 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
718 && (n = (int)STRLEN(s) - room) > 0)
720 #ifdef FEAT_MBYTE
721 if (has_mbyte)
723 int size = vim_strsize(s);
725 /* There may be room anyway when there are multibyte chars. */
726 if (size <= room)
727 return s;
729 for (n = 0; size >= room; )
731 size -= (*mb_ptr2cells)(s + n);
732 n += (*mb_ptr2len)(s + n);
734 --n;
736 #endif
737 s += n;
738 *s = '<';
740 return s;
743 static void
744 add_msg_hist(s, len, attr)
745 char_u *s;
746 int len; /* -1 for undetermined length */
747 int attr;
749 struct msg_hist *p;
751 if (msg_hist_off || msg_silent != 0)
752 return;
754 /* Don't let the message history get too big */
755 while (msg_hist_len > MAX_MSG_HIST_LEN)
756 (void)delete_first_msg();
758 /* allocate an entry and add the message at the end of the history */
759 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
760 if (p != NULL)
762 if (len < 0)
763 len = (int)STRLEN(s);
764 /* remove leading and trailing newlines */
765 while (len > 0 && *s == '\n')
767 ++s;
768 --len;
770 while (len > 0 && s[len - 1] == '\n')
771 --len;
772 p->msg = vim_strnsave(s, len);
773 p->next = NULL;
774 p->attr = attr;
775 if (last_msg_hist != NULL)
776 last_msg_hist->next = p;
777 last_msg_hist = p;
778 if (first_msg_hist == NULL)
779 first_msg_hist = last_msg_hist;
780 ++msg_hist_len;
785 * Delete the first (oldest) message from the history.
786 * Returns FAIL if there are no messages.
789 delete_first_msg()
791 struct msg_hist *p;
793 if (msg_hist_len <= 0)
794 return FAIL;
795 p = first_msg_hist;
796 first_msg_hist = p->next;
797 vim_free(p->msg);
798 vim_free(p);
799 --msg_hist_len;
800 return OK;
804 * ":messages" command.
806 /*ARGSUSED*/
807 void
808 ex_messages(eap)
809 exarg_T *eap;
811 struct msg_hist *p;
812 char_u *s;
814 msg_hist_off = TRUE;
816 s = mch_getenv((char_u *)"LANG");
817 if (s != NULL && *s != NUL)
818 msg_attr((char_u *)
819 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
820 hl_attr(HLF_T));
822 for (p = first_msg_hist; p != NULL; p = p->next)
823 if (p->msg != NULL)
824 msg_attr(p->msg, p->attr);
826 msg_hist_off = FALSE;
829 #if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
831 * Call this after prompting the user. This will avoid a hit-return message
832 * and a delay.
834 void
835 msg_end_prompt()
837 need_wait_return = FALSE;
838 emsg_on_display = FALSE;
839 cmdline_row = msg_row;
840 msg_col = 0;
841 msg_clr_eos();
843 #endif
846 * wait for the user to hit a key (normally a return)
847 * if 'redraw' is TRUE, clear and redraw the screen
848 * if 'redraw' is FALSE, just redraw the screen
849 * if 'redraw' is -1, don't redraw at all
851 void
852 wait_return(redraw)
853 int redraw;
855 int c;
856 int oldState;
857 int tmpState;
858 int had_got_int;
860 if (redraw == TRUE)
861 must_redraw = CLEAR;
863 /* If using ":silent cmd", don't wait for a return. Also don't set
864 * need_wait_return to do it later. */
865 if (msg_silent != 0)
866 return;
869 * With the global command (and some others) we only need one return at the
870 * end. Adjust cmdline_row to avoid the next message overwriting the last one.
871 * When inside vgetc(), we can't wait for a typed character at all.
873 if (vgetc_busy > 0)
874 return;
875 if (no_wait_return)
877 need_wait_return = TRUE;
878 if (!exmode_active)
879 cmdline_row = msg_row;
880 return;
883 redir_off = TRUE; /* don't redirect this message */
884 oldState = State;
885 if (quit_more)
887 c = CAR; /* just pretend CR was hit */
888 quit_more = FALSE;
889 got_int = FALSE;
891 else if (exmode_active)
893 MSG_PUTS(" "); /* make sure the cursor is on the right line */
894 c = CAR; /* no need for a return in ex mode */
895 got_int = FALSE;
897 else
899 /* Make sure the hit-return prompt is on screen when 'guioptions' was
900 * just changed. */
901 screenalloc(FALSE);
903 State = HITRETURN;
904 #ifdef FEAT_MOUSE
905 setmouse();
906 #endif
907 #ifdef USE_ON_FLY_SCROLL
908 dont_scroll = TRUE; /* disallow scrolling here */
909 #endif
910 hit_return_msg();
914 /* Remember "got_int", if it is set vgetc() probably returns a
915 * CTRL-C, but we need to loop then. */
916 had_got_int = got_int;
918 /* Don't do mappings here, we put the character back in the
919 * typeahead buffer. */
920 ++no_mapping;
921 ++allow_keys;
922 c = safe_vgetc();
923 if (had_got_int && !global_busy)
924 got_int = FALSE;
925 --no_mapping;
926 --allow_keys;
928 #ifdef FEAT_CLIPBOARD
929 /* Strange way to allow copying (yanking) a modeless selection at
930 * the hit-enter prompt. Use CTRL-Y, because the same is used in
931 * Cmdline-mode and it's harmless when there is no selection. */
932 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
934 clip_copy_modeless_selection(TRUE);
935 c = K_IGNORE;
937 #endif
939 * Allow scrolling back in the messages.
940 * Also accept scroll-down commands when messages fill the screen,
941 * to avoid that typing one 'j' too many makes the messages
942 * disappear.
944 if (p_more && !p_cp)
946 if (c == 'b' || c == 'k' || c == 'u' || c == 'g' || c == K_UP)
948 /* scroll back to show older messages */
949 do_more_prompt(c);
950 if (quit_more)
952 c = CAR; /* just pretend CR was hit */
953 quit_more = FALSE;
954 got_int = FALSE;
956 else
958 c = K_IGNORE;
959 hit_return_msg();
962 else if (msg_scrolled > Rows - 2
963 && (c == 'j' || c == K_DOWN || c == 'd'))
964 c = K_IGNORE;
966 } while ((had_got_int && c == Ctrl_C)
967 || c == K_IGNORE
968 #ifdef FEAT_GUI
969 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
970 #endif
971 #ifdef FEAT_MOUSE
972 || c == K_LEFTDRAG || c == K_LEFTRELEASE
973 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
974 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
975 || c == K_MOUSEDOWN || c == K_MOUSEUP
976 || (!mouse_has(MOUSE_RETURN)
977 && mouse_row < msg_row
978 && (c == K_LEFTMOUSE
979 || c == K_MIDDLEMOUSE
980 || c == K_RIGHTMOUSE
981 || c == K_X1MOUSE
982 || c == K_X2MOUSE))
983 #endif
985 ui_breakcheck();
986 #ifdef FEAT_MOUSE
988 * Avoid that the mouse-up event causes visual mode to start.
990 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
991 || c == K_X1MOUSE || c == K_X2MOUSE)
992 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
993 else
994 #endif
995 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
997 char_u buf[2];
999 /* Put the character back in the typeahead buffer. Don't use the
1000 * stuff buffer, because lmaps wouldn't work. */
1001 buf[0] = c;
1002 buf[1] = NUL;
1003 ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
1004 do_redraw = TRUE; /* need a redraw even though there is
1005 typeahead */
1008 redir_off = FALSE;
1011 * If the user hits ':', '?' or '/' we get a command line from the next
1012 * line.
1014 if (c == ':' || c == '?' || c == '/')
1016 if (!exmode_active)
1017 cmdline_row = msg_row;
1018 skip_redraw = TRUE; /* skip redraw once */
1019 do_redraw = FALSE;
1023 * If the window size changed set_shellsize() will redraw the screen.
1024 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1025 * typed.
1027 tmpState = State;
1028 State = oldState; /* restore State before set_shellsize */
1029 #ifdef FEAT_MOUSE
1030 setmouse();
1031 #endif
1032 msg_check();
1034 #if defined(UNIX) || defined(VMS)
1036 * When switching screens, we need to output an extra newline on exit.
1038 if (swapping_screen() && !termcap_active)
1039 newline_on_exit = TRUE;
1040 #endif
1042 need_wait_return = FALSE;
1043 did_wait_return = TRUE;
1044 emsg_on_display = FALSE; /* can delete error message now */
1045 lines_left = -1; /* reset lines_left at next msg_start() */
1046 reset_last_sourcing();
1047 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1048 (Rows - cmdline_row - 1) * Columns + sc_col)
1050 vim_free(keep_msg);
1051 keep_msg = NULL; /* don't redisplay message, it's too long */
1054 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1056 starttermcap(); /* start termcap before redrawing */
1057 shell_resized();
1059 else if (!skip_redraw
1060 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1062 starttermcap(); /* start termcap before redrawing */
1063 redraw_later(VALID);
1068 * Write the hit-return prompt.
1070 static void
1071 hit_return_msg()
1073 int save_p_more = p_more;
1075 p_more = FALSE; /* don't want see this message when scrolling back */
1076 if (msg_didout) /* start on a new line */
1077 msg_putchar('\n');
1078 if (got_int)
1079 MSG_PUTS(_("Interrupt: "));
1081 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
1082 if (!msg_use_printf())
1083 msg_clr_eos();
1084 p_more = save_p_more;
1088 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1090 void
1091 set_keep_msg(s, attr)
1092 char_u *s;
1093 int attr;
1095 vim_free(keep_msg);
1096 if (s != NULL && msg_silent == 0)
1097 keep_msg = vim_strsave(s);
1098 else
1099 keep_msg = NULL;
1100 keep_msg_more = FALSE;
1101 keep_msg_attr = attr;
1104 #if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1106 * If there currently is a message being displayed, set "keep_msg" to it, so
1107 * that it will be displayed again after redraw.
1109 void
1110 set_keep_msg_from_hist()
1112 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1113 && (State & NORMAL))
1114 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1116 #endif
1119 * Prepare for outputting characters in the command line.
1121 void
1122 msg_start()
1124 int did_return = FALSE;
1126 vim_free(keep_msg);
1127 keep_msg = NULL; /* don't display old message now */
1128 if (!msg_scroll && full_screen) /* overwrite last message */
1130 msg_row = cmdline_row;
1131 msg_col =
1132 #ifdef FEAT_RIGHTLEFT
1133 cmdmsg_rl ? Columns - 1 :
1134 #endif
1137 else if (msg_didout) /* start message on next line */
1139 msg_putchar('\n');
1140 did_return = TRUE;
1141 if (exmode_active != EXMODE_NORMAL)
1142 cmdline_row = msg_row;
1144 if (!msg_didany || lines_left < 0)
1145 msg_starthere();
1146 if (msg_silent == 0)
1148 msg_didout = FALSE; /* no output on current line yet */
1149 cursor_off();
1152 /* when redirecting, may need to start a new line. */
1153 if (!did_return)
1154 redir_write((char_u *)"\n", -1);
1158 * Note that the current msg position is where messages start.
1160 void
1161 msg_starthere()
1163 lines_left = cmdline_row;
1164 msg_didany = FALSE;
1167 void
1168 msg_putchar(c)
1169 int c;
1171 msg_putchar_attr(c, 0);
1174 void
1175 msg_putchar_attr(c, attr)
1176 int c;
1177 int attr;
1179 #ifdef FEAT_MBYTE
1180 char_u buf[MB_MAXBYTES + 1];
1181 #else
1182 char_u buf[4];
1183 #endif
1185 if (IS_SPECIAL(c))
1187 buf[0] = K_SPECIAL;
1188 buf[1] = K_SECOND(c);
1189 buf[2] = K_THIRD(c);
1190 buf[3] = NUL;
1192 else
1194 #ifdef FEAT_MBYTE
1195 buf[(*mb_char2bytes)(c, buf)] = NUL;
1196 #else
1197 buf[0] = c;
1198 buf[1] = NUL;
1199 #endif
1201 msg_puts_attr(buf, attr);
1204 void
1205 msg_outnum(n)
1206 long n;
1208 char_u buf[20];
1210 sprintf((char *)buf, "%ld", n);
1211 msg_puts(buf);
1214 void
1215 msg_home_replace(fname)
1216 char_u *fname;
1218 msg_home_replace_attr(fname, 0);
1221 #if defined(FEAT_FIND_ID) || defined(PROTO)
1222 void
1223 msg_home_replace_hl(fname)
1224 char_u *fname;
1226 msg_home_replace_attr(fname, hl_attr(HLF_D));
1228 #endif
1230 static void
1231 msg_home_replace_attr(fname, attr)
1232 char_u *fname;
1233 int attr;
1235 char_u *name;
1237 name = home_replace_save(NULL, fname);
1238 if (name != NULL)
1239 msg_outtrans_attr(name, attr);
1240 vim_free(name);
1244 * Output 'len' characters in 'str' (including NULs) with translation
1245 * if 'len' is -1, output upto a NUL character.
1246 * Use attributes 'attr'.
1247 * Return the number of characters it takes on the screen.
1250 msg_outtrans(str)
1251 char_u *str;
1253 return msg_outtrans_attr(str, 0);
1257 msg_outtrans_attr(str, attr)
1258 char_u *str;
1259 int attr;
1261 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1265 msg_outtrans_len(str, len)
1266 char_u *str;
1267 int len;
1269 return msg_outtrans_len_attr(str, len, 0);
1273 * Output one character at "p". Return pointer to the next character.
1274 * Handles multi-byte characters.
1276 char_u *
1277 msg_outtrans_one(p, attr)
1278 char_u *p;
1279 int attr;
1281 #ifdef FEAT_MBYTE
1282 int l;
1284 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1286 msg_outtrans_len_attr(p, l, attr);
1287 return p + l;
1289 #endif
1290 msg_puts_attr(transchar_byte(*p), attr);
1291 return p + 1;
1295 msg_outtrans_len_attr(msgstr, len, attr)
1296 char_u *msgstr;
1297 int len;
1298 int attr;
1300 int retval = 0;
1301 char_u *str = msgstr;
1302 char_u *plain_start = msgstr;
1303 char_u *s;
1304 #ifdef FEAT_MBYTE
1305 int mb_l;
1306 int c;
1307 #endif
1309 /* if MSG_HIST flag set, add message to history */
1310 if (attr & MSG_HIST)
1312 add_msg_hist(str, len, attr);
1313 attr &= ~MSG_HIST;
1316 #ifdef FEAT_MBYTE
1317 /* If the string starts with a composing character first draw a space on
1318 * which the composing char can be drawn. */
1319 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1320 msg_puts_attr((char_u *)" ", attr);
1321 #endif
1324 * Go over the string. Special characters are translated and printed.
1325 * Normal characters are printed several at a time.
1327 while (--len >= 0)
1329 #ifdef FEAT_MBYTE
1330 if (enc_utf8)
1331 /* Don't include composing chars after the end. */
1332 mb_l = utfc_ptr2len_len(str, len + 1);
1333 else if (has_mbyte)
1334 mb_l = (*mb_ptr2len)(str);
1335 else
1336 mb_l = 1;
1337 if (has_mbyte && mb_l > 1)
1339 c = (*mb_ptr2char)(str);
1340 if (vim_isprintc(c))
1341 /* printable multi-byte char: count the cells. */
1342 retval += (*mb_ptr2cells)(str);
1343 else
1345 /* unprintable multi-byte char: print the printable chars so
1346 * far and the translation of the unprintable char. */
1347 if (str > plain_start)
1348 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1349 attr);
1350 plain_start = str + mb_l;
1351 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1352 retval += char2cells(c);
1354 len -= mb_l - 1;
1355 str += mb_l;
1357 else
1358 #endif
1360 s = transchar_byte(*str);
1361 if (s[1] != NUL)
1363 /* unprintable char: print the printable chars so far and the
1364 * translation of the unprintable char. */
1365 if (str > plain_start)
1366 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1367 attr);
1368 plain_start = str + 1;
1369 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1371 retval += ptr2cells(str);
1372 ++str;
1376 if (str > plain_start)
1377 /* print the printable chars at the end */
1378 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1380 return retval;
1383 #if defined(FEAT_QUICKFIX) || defined(PROTO)
1384 void
1385 msg_make(arg)
1386 char_u *arg;
1388 int i;
1389 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1391 arg = skipwhite(arg);
1392 for (i = 5; *arg && i >= 0; --i)
1393 if (*arg++ != str[i])
1394 break;
1395 if (i < 0)
1397 msg_putchar('\n');
1398 for (i = 0; rs[i]; ++i)
1399 msg_putchar(rs[i] - 3);
1402 #endif
1405 * Output the string 'str' upto a NUL character.
1406 * Return the number of characters it takes on the screen.
1408 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1409 * following character and shown as <F1>, <S-Up> etc. Any other character
1410 * which is not printable shown in <> form.
1411 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1412 * If a character is displayed in one of these special ways, is also
1413 * highlighted (its highlight name is '8' in the p_hl variable).
1414 * Otherwise characters are not highlighted.
1415 * This function is used to show mappings, where we want to see how to type
1416 * the character/string -- webb
1419 msg_outtrans_special(strstart, from)
1420 char_u *strstart;
1421 int from; /* TRUE for lhs of a mapping */
1423 char_u *str = strstart;
1424 int retval = 0;
1425 char_u *string;
1426 int attr;
1427 int len;
1429 attr = hl_attr(HLF_8);
1430 while (*str != NUL)
1432 /* Leading and trailing spaces need to be displayed in <> form. */
1433 if ((str == strstart || str[1] == NUL) && *str == ' ')
1435 string = (char_u *)"<Space>";
1436 ++str;
1438 else
1439 string = str2special(&str, from);
1440 len = vim_strsize(string);
1441 /* Highlight special keys */
1442 msg_puts_attr(string, len > 1
1443 #ifdef FEAT_MBYTE
1444 && (*mb_ptr2len)(string) <= 1
1445 #endif
1446 ? attr : 0);
1447 retval += len;
1449 return retval;
1453 * Return the printable string for the key codes at "*sp".
1454 * Used for translating the lhs or rhs of a mapping to printable chars.
1455 * Advances "sp" to the next code.
1457 char_u *
1458 str2special(sp, from)
1459 char_u **sp;
1460 int from; /* TRUE for lhs of mapping */
1462 int c;
1463 static char_u buf[7];
1464 char_u *str = *sp;
1465 int modifiers = 0;
1466 int special = FALSE;
1468 #ifdef FEAT_MBYTE
1469 if (has_mbyte)
1471 char_u *p;
1473 /* Try to un-escape a multi-byte character. Return the un-escaped
1474 * string if it is a multi-byte character. */
1475 p = mb_unescape(sp);
1476 if (p != NULL)
1477 return p;
1479 #endif
1481 c = *str;
1482 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1484 if (str[1] == KS_MODIFIER)
1486 modifiers = str[2];
1487 str += 3;
1488 c = *str;
1490 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1492 c = TO_SPECIAL(str[1], str[2]);
1493 str += 2;
1494 if (c == K_ZERO) /* display <Nul> as ^@ */
1495 c = NUL;
1497 if (IS_SPECIAL(c) || modifiers) /* special key */
1498 special = TRUE;
1500 *sp = str + 1;
1502 #ifdef FEAT_MBYTE
1503 /* For multi-byte characters check for an illegal byte. */
1504 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len)(str))
1506 transchar_nonprint(buf, c);
1507 return buf;
1509 #endif
1511 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1512 * Use <Space> only for lhs of a mapping. */
1513 if (special || char2cells(c) > 1 || (from && c == ' '))
1514 return get_special_key_name(c, modifiers);
1515 buf[0] = c;
1516 buf[1] = NUL;
1517 return buf;
1521 * Translate a key sequence into special key names.
1523 void
1524 str2specialbuf(sp, buf, len)
1525 char_u *sp;
1526 char_u *buf;
1527 int len;
1529 char_u *s;
1531 *buf = NUL;
1532 while (*sp)
1534 s = str2special(&sp, FALSE);
1535 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1536 STRCAT(buf, s);
1541 * print line for :print or :list command
1543 void
1544 msg_prt_line(s, list)
1545 char_u *s;
1546 int list;
1548 int c;
1549 int col = 0;
1550 int n_extra = 0;
1551 int c_extra = 0;
1552 char_u *p_extra = NULL; /* init to make SASC shut up */
1553 int n;
1554 int attr= 0;
1555 char_u *trail = NULL;
1556 #ifdef FEAT_MBYTE
1557 int l;
1558 char_u buf[MB_MAXBYTES + 1];
1559 #endif
1561 if (curwin->w_p_list)
1562 list = TRUE;
1564 /* find start of trailing whitespace */
1565 if (list && lcs_trail)
1567 trail = s + STRLEN(s);
1568 while (trail > s && vim_iswhite(trail[-1]))
1569 --trail;
1572 /* output a space for an empty line, otherwise the line will be
1573 * overwritten */
1574 if (*s == NUL && !(list && lcs_eol != NUL))
1575 msg_putchar(' ');
1577 while (!got_int)
1579 if (n_extra)
1581 --n_extra;
1582 if (c_extra)
1583 c = c_extra;
1584 else
1585 c = *p_extra++;
1587 #ifdef FEAT_MBYTE
1588 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
1590 col += (*mb_ptr2cells)(s);
1591 mch_memmove(buf, s, (size_t)l);
1592 buf[l] = NUL;
1593 msg_puts_attr(buf, attr);
1594 s += l;
1595 continue;
1597 #endif
1598 else
1600 attr = 0;
1601 c = *s++;
1602 if (c == TAB && (!list || lcs_tab1))
1604 /* tab amount depends on current column */
1605 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
1606 if (!list)
1608 c = ' ';
1609 c_extra = ' ';
1611 else
1613 c = lcs_tab1;
1614 c_extra = lcs_tab2;
1615 attr = hl_attr(HLF_8);
1618 else if (c == NUL && list && lcs_eol != NUL)
1620 p_extra = (char_u *)"";
1621 c_extra = NUL;
1622 n_extra = 1;
1623 c = lcs_eol;
1624 attr = hl_attr(HLF_AT);
1625 --s;
1627 else if (c != NUL && (n = byte2cells(c)) > 1)
1629 n_extra = n - 1;
1630 p_extra = transchar_byte(c);
1631 c_extra = NUL;
1632 c = *p_extra++;
1634 else if (c == ' ' && trail != NULL && s > trail)
1636 c = lcs_trail;
1637 attr = hl_attr(HLF_8);
1641 if (c == NUL)
1642 break;
1644 msg_putchar_attr(c, attr);
1645 col++;
1647 msg_clr_eos();
1650 #ifdef FEAT_MBYTE
1652 * Use screen_puts() to output one multi-byte character.
1653 * Return the pointer "s" advanced to the next character.
1655 static char_u *
1656 screen_puts_mbyte(s, l, attr)
1657 char_u *s;
1658 int l;
1659 int attr;
1661 int cw;
1663 msg_didout = TRUE; /* remember that line is not empty */
1664 cw = (*mb_ptr2cells)(s);
1665 if (cw > 1 && (
1666 #ifdef FEAT_RIGHTLEFT
1667 cmdmsg_rl ? msg_col <= 1 :
1668 #endif
1669 msg_col == Columns - 1))
1671 /* Doesn't fit, print a highlighted '>' to fill it up. */
1672 msg_screen_putchar('>', hl_attr(HLF_AT));
1673 return s;
1676 screen_puts_len(s, l, msg_row, msg_col, attr);
1677 #ifdef FEAT_RIGHTLEFT
1678 if (cmdmsg_rl)
1680 msg_col -= cw;
1681 if (msg_col == 0)
1683 msg_col = Columns;
1684 ++msg_row;
1687 else
1688 #endif
1690 msg_col += cw;
1691 if (msg_col >= Columns)
1693 msg_col = 0;
1694 ++msg_row;
1697 return s + l;
1699 #endif
1702 * Output a string to the screen at position msg_row, msg_col.
1703 * Update msg_row and msg_col for the next message.
1705 void
1706 msg_puts(s)
1707 char_u *s;
1709 msg_puts_attr(s, 0);
1712 void
1713 msg_puts_title(s)
1714 char_u *s;
1716 msg_puts_attr(s, hl_attr(HLF_T));
1720 * Show a message in such a way that it always fits in the line. Cut out a
1721 * part in the middle and replace it with "..." when necessary.
1722 * Does not handle multi-byte characters!
1724 void
1725 msg_puts_long_attr(longstr, attr)
1726 char_u *longstr;
1727 int attr;
1729 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
1732 void
1733 msg_puts_long_len_attr(longstr, len, attr)
1734 char_u *longstr;
1735 int len;
1736 int attr;
1738 int slen = len;
1739 int room;
1741 room = Columns - msg_col;
1742 if (len > room && room >= 20)
1744 slen = (room - 3) / 2;
1745 msg_outtrans_len_attr(longstr, slen, attr);
1746 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1748 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1752 * Basic function for writing a message with highlight attributes.
1754 void
1755 msg_puts_attr(s, attr)
1756 char_u *s;
1757 int attr;
1759 msg_puts_attr_len(s, -1, attr);
1763 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1764 * When "maxlen" is -1 there is no maximum length.
1765 * When "maxlen" is >= 0 the message is not put in the history.
1767 static void
1768 msg_puts_attr_len(str, maxlen, attr)
1769 char_u *str;
1770 int maxlen;
1771 int attr;
1774 * If redirection is on, also write to the redirection file.
1776 redir_write(str, maxlen);
1779 * Don't print anything when using ":silent cmd".
1781 if (msg_silent != 0)
1782 return;
1784 /* if MSG_HIST flag set, add message to history */
1785 if ((attr & MSG_HIST) && maxlen < 0)
1787 add_msg_hist(str, -1, attr);
1788 attr &= ~MSG_HIST;
1792 * When writing something to the screen after it has scrolled, requires a
1793 * wait-return prompt later. Needed when scrolling, resetting
1794 * need_wait_return after some prompt, and then outputting something
1795 * without scrolling
1797 if (msg_scrolled != 0 && !msg_scrolled_ign)
1798 need_wait_return = TRUE;
1799 msg_didany = TRUE; /* remember that something was outputted */
1802 * If there is no valid screen, use fprintf so we can see error messages.
1803 * If termcap is not active, we may be writing in an alternate console
1804 * window, cursor positioning may not work correctly (window size may be
1805 * different, e.g. for Win32 console) or we just don't know where the
1806 * cursor is.
1808 if (msg_use_printf())
1809 msg_puts_printf(str, maxlen);
1810 else
1811 msg_puts_display(str, maxlen, attr, FALSE);
1815 * The display part of msg_puts_attr_len().
1816 * May be called recursively to display scroll-back text.
1818 static void
1819 msg_puts_display(str, maxlen, attr, recurse)
1820 char_u *str;
1821 int maxlen;
1822 int attr;
1823 int recurse;
1825 char_u *s = str;
1826 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1827 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1828 #ifdef FEAT_MBYTE
1829 int l;
1830 int cw;
1831 #endif
1832 char_u *sb_str = str;
1833 int sb_col = msg_col;
1834 int wrap;
1836 did_wait_return = FALSE;
1837 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1840 * We are at the end of the screen line when:
1841 * - When outputting a newline.
1842 * - When outputting a character in the last column.
1844 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
1845 #ifdef FEAT_RIGHTLEFT
1846 cmdmsg_rl
1848 msg_col <= 1
1849 || (*s == TAB && msg_col <= 7)
1850 # ifdef FEAT_MBYTE
1851 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1852 # endif
1855 #endif
1856 (msg_col + t_col >= Columns - 1
1857 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1858 # ifdef FEAT_MBYTE
1859 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1860 && msg_col + t_col >= Columns - 2)
1861 # endif
1862 ))))
1865 * The screen is scrolled up when at the last row (some terminals
1866 * scroll automatically, some don't. To avoid problems we scroll
1867 * ourselves).
1869 if (t_col > 0)
1870 /* output postponed text */
1871 t_puts(&t_col, t_s, s, attr);
1873 /* When no more prompt an no more room, truncate here */
1874 if (msg_no_more && lines_left == 0)
1875 break;
1877 /* Scroll the screen up one line. */
1878 msg_scroll_up();
1880 msg_row = Rows - 2;
1881 if (msg_col >= Columns) /* can happen after screen resize */
1882 msg_col = Columns - 1;
1884 /* Display char in last column before showing more-prompt. */
1885 if (*s >= ' '
1886 #ifdef FEAT_RIGHTLEFT
1887 && !cmdmsg_rl
1888 #endif
1891 #ifdef FEAT_MBYTE
1892 if (has_mbyte)
1894 if (enc_utf8 && maxlen >= 0)
1895 /* avoid including composing chars after the end */
1896 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
1897 else
1898 l = (*mb_ptr2len)(s);
1899 s = screen_puts_mbyte(s, l, attr);
1901 else
1902 #endif
1903 msg_screen_putchar(*s++, attr);
1906 if (p_more)
1907 /* store text for scrolling back */
1908 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1910 inc_msg_scrolled();
1911 need_wait_return = TRUE; /* may need wait_return in main() */
1912 if (must_redraw < VALID)
1913 must_redraw = VALID;
1914 redraw_cmdline = TRUE;
1915 if (cmdline_row > 0 && !exmode_active)
1916 --cmdline_row;
1919 * If screen is completely filled and 'more' is set then wait
1920 * for a character.
1922 if (p_more && --lines_left == 0 && State != HITRETURN
1923 && !msg_no_more && !exmode_active)
1925 #ifdef FEAT_CON_DIALOG
1926 if (do_more_prompt(NUL))
1927 s = confirm_msg_tail;
1928 #else
1929 (void)do_more_prompt(NUL);
1930 #endif
1931 if (quit_more)
1932 return;
1935 /* When we displayed a char in last column need to check if there
1936 * is still more. */
1937 if (*s >= ' '
1938 #ifdef FEAT_RIGHTLEFT
1939 && !cmdmsg_rl
1940 #endif
1942 continue;
1945 wrap = *s == '\n'
1946 || msg_col + t_col >= Columns
1947 #ifdef FEAT_MBYTE
1948 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1949 && msg_col + t_col >= Columns - 1)
1950 #endif
1952 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
1953 || *s == '\t' || *s == BELL))
1954 /* output any postponed text */
1955 t_puts(&t_col, t_s, s, attr);
1957 if (wrap && p_more && !recurse)
1958 /* store text for scrolling back */
1959 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1961 if (*s == '\n') /* go to next line */
1963 msg_didout = FALSE; /* remember that line is empty */
1964 #ifdef FEAT_RIGHTLEFT
1965 if (cmdmsg_rl)
1966 msg_col = Columns - 1;
1967 else
1968 #endif
1969 msg_col = 0;
1970 if (++msg_row >= Rows) /* safety check */
1971 msg_row = Rows - 1;
1973 else if (*s == '\r') /* go to column 0 */
1975 msg_col = 0;
1977 else if (*s == '\b') /* go to previous char */
1979 if (msg_col)
1980 --msg_col;
1982 else if (*s == TAB) /* translate Tab into spaces */
1985 msg_screen_putchar(' ', attr);
1986 while (msg_col & 7);
1988 else if (*s == BELL) /* beep (from ":sh") */
1989 vim_beep();
1990 else
1992 #ifdef FEAT_MBYTE
1993 if (has_mbyte)
1995 cw = (*mb_ptr2cells)(s);
1996 if (enc_utf8 && maxlen >= 0)
1997 /* avoid including composing chars after the end */
1998 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
1999 else
2000 l = (*mb_ptr2len)(s);
2002 else
2004 cw = 1;
2005 l = 1;
2007 #endif
2008 /* When drawing from right to left or when a double-wide character
2009 * doesn't fit, draw a single character here. Otherwise collect
2010 * characters and draw them all at once later. */
2011 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2012 if (
2013 # ifdef FEAT_RIGHTLEFT
2014 cmdmsg_rl
2015 # ifdef FEAT_MBYTE
2017 # endif
2018 # endif
2019 # ifdef FEAT_MBYTE
2020 (cw > 1 && msg_col + t_col >= Columns - 1)
2021 # endif
2024 # ifdef FEAT_MBYTE
2025 if (l > 1)
2026 s = screen_puts_mbyte(s, l, attr) - 1;
2027 else
2028 # endif
2029 msg_screen_putchar(*s, attr);
2031 else
2032 #endif
2034 /* postpone this character until later */
2035 if (t_col == 0)
2036 t_s = s;
2037 #ifdef FEAT_MBYTE
2038 t_col += cw;
2039 s += l - 1;
2040 #else
2041 ++t_col;
2042 #endif
2045 ++s;
2048 /* output any postponed text */
2049 if (t_col > 0)
2050 t_puts(&t_col, t_s, s, attr);
2051 if (p_more && !recurse)
2052 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
2054 msg_check();
2058 * Scroll the screen up one line for displaying the next message line.
2060 static void
2061 msg_scroll_up()
2063 #ifdef FEAT_GUI
2064 /* Remove the cursor before scrolling, ScreenLines[] is going
2065 * to become invalid. */
2066 if (gui.in_use)
2067 gui_undraw_cursor();
2068 #endif
2069 /* scrolling up always works */
2070 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2072 if (!can_clear((char_u *)" "))
2074 /* Scrolling up doesn't result in the right background. Set the
2075 * background here. It's not efficient, but avoids that we have to do
2076 * it all over the code. */
2077 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2079 /* Also clear the last char of the last but one line if it was not
2080 * cleared before to avoid a scroll-up. */
2081 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2082 screen_fill((int)Rows - 2, (int)Rows - 1,
2083 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2088 * Increment "msg_scrolled".
2090 static void
2091 inc_msg_scrolled()
2093 #ifdef FEAT_EVAL
2094 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2096 char_u *p = sourcing_name;
2097 char_u *tofree = NULL;
2098 int len;
2100 /* v:scrollstart is empty, set it to the script/function name and line
2101 * number */
2102 if (p == NULL)
2103 p = (char_u *)_("Unknown");
2104 else
2106 len = (int)STRLEN(p) + 40;
2107 tofree = alloc(len);
2108 if (tofree != NULL)
2110 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2111 p, (long)sourcing_lnum);
2112 p = tofree;
2115 set_vim_var_string(VV_SCROLLSTART, p, -1);
2116 vim_free(tofree);
2118 #endif
2119 ++msg_scrolled;
2123 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2124 * store the displayed text and remember where screen lines start.
2126 typedef struct msgchunk_S msgchunk_T;
2127 struct msgchunk_S
2129 msgchunk_T *sb_next;
2130 msgchunk_T *sb_prev;
2131 char sb_eol; /* TRUE when line ends after this text */
2132 int sb_msg_col; /* column in which text starts */
2133 int sb_attr; /* text attributes */
2134 char_u sb_text[1]; /* text to be displayed, actually longer */
2137 static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2139 static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2140 static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2142 static int do_clear_sb_text = FALSE; /* clear text on next msg */
2145 * Store part of a printed message for displaying when scrolling back.
2147 static void
2148 store_sb_text(sb_str, s, attr, sb_col, finish)
2149 char_u **sb_str; /* start of string */
2150 char_u *s; /* just after string */
2151 int attr;
2152 int *sb_col;
2153 int finish; /* line ends */
2155 msgchunk_T *mp;
2157 if (do_clear_sb_text)
2159 clear_sb_text();
2160 do_clear_sb_text = FALSE;
2163 if (s > *sb_str)
2165 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2166 if (mp != NULL)
2168 mp->sb_eol = finish;
2169 mp->sb_msg_col = *sb_col;
2170 mp->sb_attr = attr;
2171 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2173 if (last_msgchunk == NULL)
2175 last_msgchunk = mp;
2176 mp->sb_prev = NULL;
2178 else
2180 mp->sb_prev = last_msgchunk;
2181 last_msgchunk->sb_next = mp;
2182 last_msgchunk = mp;
2184 mp->sb_next = NULL;
2187 else if (finish && last_msgchunk != NULL)
2188 last_msgchunk->sb_eol = TRUE;
2190 *sb_str = s;
2191 *sb_col = 0;
2195 * Finished showing messages, clear the scroll-back text on the next message.
2197 void
2198 may_clear_sb_text()
2200 do_clear_sb_text = TRUE;
2204 * Clear any text remembered for scrolling back.
2205 * Called when redrawing the screen.
2207 void
2208 clear_sb_text()
2210 msgchunk_T *mp;
2212 while (last_msgchunk != NULL)
2214 mp = last_msgchunk->sb_prev;
2215 vim_free(last_msgchunk);
2216 last_msgchunk = mp;
2221 * "g<" command.
2223 void
2224 show_sb_text()
2226 msgchunk_T *mp;
2228 /* Only show somethign if there is more than one line, otherwise it looks
2229 * weird, typing a command without output results in one line. */
2230 mp = msg_sb_start(last_msgchunk);
2231 if (mp == NULL || mp->sb_prev == NULL)
2232 vim_beep();
2233 else
2235 do_more_prompt('G');
2236 wait_return(FALSE);
2241 * Move to the start of screen line in already displayed text.
2243 static msgchunk_T *
2244 msg_sb_start(mps)
2245 msgchunk_T *mps;
2247 msgchunk_T *mp = mps;
2249 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2250 mp = mp->sb_prev;
2251 return mp;
2255 * Display a screen line from previously displayed text at row "row".
2256 * Returns a pointer to the text for the next line (can be NULL).
2258 static msgchunk_T *
2259 disp_sb_line(row, smp)
2260 int row;
2261 msgchunk_T *smp;
2263 msgchunk_T *mp = smp;
2264 char_u *p;
2266 for (;;)
2268 msg_row = row;
2269 msg_col = mp->sb_msg_col;
2270 p = mp->sb_text;
2271 if (*p == '\n') /* don't display the line break */
2272 ++p;
2273 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2274 if (mp->sb_eol || mp->sb_next == NULL)
2275 break;
2276 mp = mp->sb_next;
2278 return mp->sb_next;
2282 * Output any postponed text for msg_puts_attr_len().
2284 static void
2285 t_puts(t_col, t_s, s, attr)
2286 int *t_col;
2287 char_u *t_s;
2288 char_u *s;
2289 int attr;
2291 /* output postponed text */
2292 msg_didout = TRUE; /* remember that line is not empty */
2293 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
2294 msg_col += *t_col;
2295 *t_col = 0;
2296 #ifdef FEAT_MBYTE
2297 /* If the string starts with a composing character don't increment the
2298 * column position for it. */
2299 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2300 --msg_col;
2301 #endif
2302 if (msg_col >= Columns)
2304 msg_col = 0;
2305 ++msg_row;
2310 * Returns TRUE when messages should be printed with mch_errmsg().
2311 * This is used when there is no valid screen, so we can see error messages.
2312 * If termcap is not active, we may be writing in an alternate console
2313 * window, cursor positioning may not work correctly (window size may be
2314 * different, e.g. for Win32 console) or we just don't know where the
2315 * cursor is.
2318 msg_use_printf()
2320 return (!msg_check_screen()
2321 #if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2322 || !termcap_active
2323 #endif
2324 || (swapping_screen() && !termcap_active)
2329 * Print a message when there is no valid screen.
2331 static void
2332 msg_puts_printf(str, maxlen)
2333 char_u *str;
2334 int maxlen;
2336 char_u *s = str;
2337 char_u buf[4];
2338 char_u *p;
2340 #ifdef WIN3264
2341 if (!(silent_mode && p_verbose == 0))
2342 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2343 #endif
2344 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2346 if (!(silent_mode && p_verbose == 0))
2348 /* NL --> CR NL translation (for Unix, not for "--version") */
2349 /* NL --> CR translation (for Mac) */
2350 p = &buf[0];
2351 if (*s == '\n' && !info_message)
2352 *p++ = '\r';
2353 #if defined(USE_CR) && !defined(MACOS_X_UNIX)
2354 else
2355 #endif
2356 *p++ = *s;
2357 *p = '\0';
2358 if (info_message) /* informative message, not an error */
2359 mch_msg((char *)buf);
2360 else
2361 mch_errmsg((char *)buf);
2364 /* primitive way to compute the current column */
2365 #ifdef FEAT_RIGHTLEFT
2366 if (cmdmsg_rl)
2368 if (*s == '\r' || *s == '\n')
2369 msg_col = Columns - 1;
2370 else
2371 --msg_col;
2373 else
2374 #endif
2376 if (*s == '\r' || *s == '\n')
2377 msg_col = 0;
2378 else
2379 ++msg_col;
2381 ++s;
2383 msg_didout = TRUE; /* assume that line is not empty */
2385 #ifdef WIN3264
2386 if (!(silent_mode && p_verbose == 0))
2387 mch_settmode(TMODE_RAW);
2388 #endif
2392 * Show the more-prompt and handle the user response.
2393 * This takes care of scrolling back and displaying previously displayed text.
2394 * When at hit-enter prompt "typed_char" is the already typed character,
2395 * otherwise it's NUL.
2396 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2398 static int
2399 do_more_prompt(typed_char)
2400 int typed_char;
2402 int used_typed_char = typed_char;
2403 int oldState = State;
2404 int c;
2405 #ifdef FEAT_CON_DIALOG
2406 int retval = FALSE;
2407 #endif
2408 int scroll;
2409 msgchunk_T *mp_last = NULL;
2410 msgchunk_T *mp;
2411 int i;
2413 if (typed_char == 'G')
2415 /* "g<": Find first line on the last page. */
2416 mp_last = msg_sb_start(last_msgchunk);
2417 for (i = 0; i < Rows - 2 && mp_last != NULL
2418 && mp_last->sb_prev != NULL; ++i)
2419 mp_last = msg_sb_start(mp_last->sb_prev);
2422 State = ASKMORE;
2423 #ifdef FEAT_MOUSE
2424 setmouse();
2425 #endif
2426 if (typed_char == NUL)
2427 msg_moremsg(FALSE);
2428 for (;;)
2431 * Get a typed character directly from the user.
2433 if (used_typed_char != NUL)
2435 c = used_typed_char; /* was typed at hit-enter prompt */
2436 used_typed_char = NUL;
2438 else
2439 c = get_keystroke();
2441 #if defined(FEAT_MENU) && defined(FEAT_GUI)
2442 if (c == K_MENU)
2444 int idx = get_menu_index(current_menu, ASKMORE);
2446 /* Used a menu. If it starts with CTRL-Y, it must
2447 * be a "Copy" for the clipboard. Otherwise
2448 * assume that we end */
2449 if (idx == MENU_INDEX_INVALID)
2450 continue;
2451 c = *current_menu->strings[idx];
2452 if (c != NUL && current_menu->strings[idx][1] != NUL)
2453 ins_typebuf(current_menu->strings[idx] + 1,
2454 current_menu->noremap[idx], 0, TRUE,
2455 current_menu->silent[idx]);
2457 #endif
2459 scroll = 0;
2460 switch (c)
2462 case BS: /* scroll one line back */
2463 case K_BS:
2464 case 'k':
2465 case K_UP:
2466 scroll = -1;
2467 break;
2469 case CAR: /* one extra line */
2470 case NL:
2471 case 'j':
2472 case K_DOWN:
2473 scroll = 1;
2474 break;
2476 case 'u': /* Up half a page */
2477 case K_PAGEUP:
2478 scroll = -(Rows / 2);
2479 break;
2481 case 'd': /* Down half a page */
2482 scroll = Rows / 2;
2483 break;
2485 case 'b': /* one page back */
2486 scroll = -(Rows - 1);
2487 break;
2489 case ' ': /* one extra page */
2490 case K_PAGEDOWN:
2491 case K_LEFTMOUSE:
2492 scroll = Rows - 1;
2493 break;
2495 case 'g': /* all the way back to the start */
2496 scroll = -999999;
2497 break;
2499 case 'G': /* all the way to the end */
2500 scroll = 999999;
2501 lines_left = 999999;
2502 break;
2504 case ':': /* start new command line */
2505 #ifdef FEAT_CON_DIALOG
2506 if (!confirm_msg_used)
2507 #endif
2509 /* Since got_int is set all typeahead will be flushed, but we
2510 * want to keep this ':', remember that in a special way. */
2511 typeahead_noflush(':');
2512 cmdline_row = Rows - 1; /* put ':' on this line */
2513 skip_redraw = TRUE; /* skip redraw once */
2514 need_wait_return = FALSE; /* don't wait in main() */
2516 /*FALLTHROUGH*/
2517 case 'q': /* quit */
2518 case Ctrl_C:
2519 case ESC:
2520 #ifdef FEAT_CON_DIALOG
2521 if (confirm_msg_used)
2523 /* Jump to the choices of the dialog. */
2524 retval = TRUE;
2525 lines_left = Rows - 1;
2527 else
2528 #endif
2530 got_int = TRUE;
2531 quit_more = TRUE;
2533 break;
2535 #ifdef FEAT_CLIPBOARD
2536 case Ctrl_Y:
2537 /* Strange way to allow copying (yanking) a modeless
2538 * selection at the more prompt. Use CTRL-Y,
2539 * because the same is used in Cmdline-mode and at the
2540 * hit-enter prompt. However, scrolling one line up
2541 * might be expected... */
2542 if (clip_star.state == SELECT_DONE)
2543 clip_copy_modeless_selection(TRUE);
2544 continue;
2545 #endif
2546 default: /* no valid response */
2547 msg_moremsg(TRUE);
2548 continue;
2551 if (scroll != 0)
2553 if (scroll < 0)
2555 /* go to start of last line */
2556 if (mp_last == NULL)
2557 mp = msg_sb_start(last_msgchunk);
2558 else if (mp_last->sb_prev != NULL)
2559 mp = msg_sb_start(mp_last->sb_prev);
2560 else
2561 mp = NULL;
2563 /* go to start of line at top of the screen */
2564 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2565 ++i)
2566 mp = msg_sb_start(mp->sb_prev);
2568 if (mp != NULL && mp->sb_prev != NULL)
2570 /* Find line to be displayed at top. */
2571 for (i = 0; i > scroll; --i)
2573 if (mp == NULL || mp->sb_prev == NULL)
2574 break;
2575 mp = msg_sb_start(mp->sb_prev);
2576 if (mp_last == NULL)
2577 mp_last = msg_sb_start(last_msgchunk);
2578 else
2579 mp_last = msg_sb_start(mp_last->sb_prev);
2582 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2583 (int)Rows, NULL) == OK)
2585 /* display line at top */
2586 (void)disp_sb_line(0, mp);
2588 else
2590 /* redisplay all lines */
2591 screenclear();
2592 for (i = 0; i < Rows - 1; ++i)
2593 mp = disp_sb_line(i, mp);
2595 scroll = 0;
2598 else
2600 /* First display any text that we scrolled back. */
2601 while (scroll > 0 && mp_last != NULL)
2603 /* scroll up, display line at bottom */
2604 msg_scroll_up();
2605 inc_msg_scrolled();
2606 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2607 (int)Columns, ' ', ' ', 0);
2608 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2609 --scroll;
2613 if (scroll < 0 || (scroll == 0 && mp_last != NULL))
2615 /* displayed the requested text, more prompt again */
2616 screen_fill((int)Rows - 1, (int)Rows, 0,
2617 (int)Columns, ' ', ' ', 0);
2618 msg_moremsg(FALSE);
2619 continue;
2622 /* display more text, return to caller */
2623 lines_left = scroll;
2626 break;
2629 /* clear the --more-- message */
2630 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2631 State = oldState;
2632 #ifdef FEAT_MOUSE
2633 setmouse();
2634 #endif
2635 if (quit_more)
2637 msg_row = Rows - 1;
2638 msg_col = 0;
2640 #ifdef FEAT_RIGHTLEFT
2641 else if (cmdmsg_rl)
2642 msg_col = Columns - 1;
2643 #endif
2645 #ifdef FEAT_CON_DIALOG
2646 return retval;
2647 #else
2648 return FALSE;
2649 #endif
2652 #if defined(USE_MCH_ERRMSG) || defined(PROTO)
2654 #ifdef mch_errmsg
2655 # undef mch_errmsg
2656 #endif
2657 #ifdef mch_msg
2658 # undef mch_msg
2659 #endif
2662 * Give an error message. To be used when the screen hasn't been initialized
2663 * yet. When stderr can't be used, collect error messages until the GUI has
2664 * started and they can be displayed in a message box.
2666 void
2667 mch_errmsg(str)
2668 char *str;
2670 int len;
2672 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2673 /* On Unix use stderr if it's a tty.
2674 * When not going to start the GUI also use stderr.
2675 * On Mac, when started from Finder, stderr is the console. */
2676 if (
2677 # ifdef UNIX
2678 # ifdef MACOS_X_UNIX
2679 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2680 # else
2681 isatty(2)
2682 # endif
2683 # ifdef FEAT_GUI
2685 # endif
2686 # endif
2687 # ifdef FEAT_GUI
2688 !(gui.in_use || gui.starting)
2689 # endif
2692 fprintf(stderr, "%s", str);
2693 return;
2695 #endif
2697 /* avoid a delay for a message that isn't there */
2698 emsg_on_display = FALSE;
2700 len = (int)STRLEN(str) + 1;
2701 if (error_ga.ga_growsize == 0)
2703 error_ga.ga_growsize = 80;
2704 error_ga.ga_itemsize = 1;
2706 if (ga_grow(&error_ga, len) == OK)
2708 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2709 (char_u *)str, len);
2710 #ifdef UNIX
2711 /* remove CR characters, they are displayed */
2713 char_u *p;
2715 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2716 for (;;)
2718 p = vim_strchr(p, '\r');
2719 if (p == NULL)
2720 break;
2721 *p = ' ';
2724 #endif
2725 --len; /* don't count the NUL at the end */
2726 error_ga.ga_len += len;
2731 * Give a message. To be used when the screen hasn't been initialized yet.
2732 * When there is no tty, collect messages until the GUI has started and they
2733 * can be displayed in a message box.
2735 void
2736 mch_msg(str)
2737 char *str;
2739 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2740 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2741 * uses mch_errmsg() when started from the desktop.
2742 * When not going to start the GUI also use stdout.
2743 * On Mac, when started from Finder, stderr is the console. */
2744 if (
2745 # ifdef UNIX
2746 # ifdef MACOS_X_UNIX
2747 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2748 # else
2749 isatty(2)
2750 # endif
2751 # ifdef FEAT_GUI
2753 # endif
2754 # endif
2755 # ifdef FEAT_GUI
2756 !(gui.in_use || gui.starting)
2757 # endif
2760 printf("%s", str);
2761 return;
2763 # endif
2764 mch_errmsg(str);
2766 #endif /* USE_MCH_ERRMSG */
2769 * Put a character on the screen at the current message position and advance
2770 * to the next position. Only for printable ASCII!
2772 static void
2773 msg_screen_putchar(c, attr)
2774 int c;
2775 int attr;
2777 msg_didout = TRUE; /* remember that line is not empty */
2778 screen_putchar(c, msg_row, msg_col, attr);
2779 #ifdef FEAT_RIGHTLEFT
2780 if (cmdmsg_rl)
2782 if (--msg_col == 0)
2784 msg_col = Columns;
2785 ++msg_row;
2788 else
2789 #endif
2791 if (++msg_col >= Columns)
2793 msg_col = 0;
2794 ++msg_row;
2799 void
2800 msg_moremsg(full)
2801 int full;
2803 int attr;
2804 char_u *s = (char_u *)_("-- More --");
2806 attr = hl_attr(HLF_M);
2807 screen_puts(s, (int)Rows - 1, 0, attr);
2808 if (full)
2809 screen_puts((char_u *)
2810 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2811 (int)Rows - 1, vim_strsize(s), attr);
2815 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2816 * exmode_active.
2818 void
2819 repeat_message()
2821 if (State == ASKMORE)
2823 msg_moremsg(TRUE); /* display --more-- message again */
2824 msg_row = Rows - 1;
2826 #ifdef FEAT_CON_DIALOG
2827 else if (State == CONFIRM)
2829 display_confirm_msg(); /* display ":confirm" message again */
2830 msg_row = Rows - 1;
2832 #endif
2833 else if (State == EXTERNCMD)
2835 windgoto(msg_row, msg_col); /* put cursor back */
2837 else if (State == HITRETURN || State == SETWSIZE)
2839 hit_return_msg();
2840 msg_row = Rows - 1;
2845 * msg_check_screen - check if the screen is initialized.
2846 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2847 * While starting the GUI the terminal codes will be set for the GUI, but the
2848 * output goes to the terminal. Don't use the terminal codes then.
2850 static int
2851 msg_check_screen()
2853 if (!full_screen || !screen_valid(FALSE))
2854 return FALSE;
2856 if (msg_row >= Rows)
2857 msg_row = Rows - 1;
2858 if (msg_col >= Columns)
2859 msg_col = Columns - 1;
2860 return TRUE;
2864 * Clear from current message position to end of screen.
2865 * Skip this when ":silent" was used, no need to clear for redirection.
2867 void
2868 msg_clr_eos()
2870 if (msg_silent == 0)
2871 msg_clr_eos_force();
2875 * Clear from current message position to end of screen.
2876 * Note: msg_col is not updated, so we remember the end of the message
2877 * for msg_check().
2879 void
2880 msg_clr_eos_force()
2882 if (msg_use_printf())
2884 if (full_screen) /* only when termcap codes are valid */
2886 if (*T_CD)
2887 out_str(T_CD); /* clear to end of display */
2888 else if (*T_CE)
2889 out_str(T_CE); /* clear to end of line */
2892 else
2894 #ifdef FEAT_RIGHTLEFT
2895 if (cmdmsg_rl)
2897 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2898 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2900 else
2901 #endif
2903 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2904 ' ', ' ', 0);
2905 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2911 * Clear the command line.
2913 void
2914 msg_clr_cmdline()
2916 msg_row = cmdline_row;
2917 msg_col = 0;
2918 msg_clr_eos_force();
2922 * end putting a message on the screen
2923 * call wait_return if the message does not fit in the available space
2924 * return TRUE if wait_return not called.
2927 msg_end()
2930 * if the string is larger than the window,
2931 * or the ruler option is set and we run into it,
2932 * we have to redraw the window.
2933 * Do not do this if we are abandoning the file or editing the command line.
2935 if (!exiting && need_wait_return && !(State & CMDLINE))
2937 wait_return(FALSE);
2938 return FALSE;
2940 out_flush();
2941 return TRUE;
2945 * If the written message runs into the shown command or ruler, we have to
2946 * wait for hit-return and redraw the window later.
2948 void
2949 msg_check()
2951 if (msg_row == Rows - 1 && msg_col >= sc_col)
2953 need_wait_return = TRUE;
2954 redraw_cmdline = TRUE;
2959 * May write a string to the redirection file.
2960 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2962 static void
2963 redir_write(str, maxlen)
2964 char_u *str;
2965 int maxlen;
2967 char_u *s = str;
2968 static int cur_col = 0;
2970 /* Don't do anything for displaying prompts and the like. */
2971 if (redir_off)
2972 return;
2975 * If 'verbosefile' is set write message in that file.
2976 * Must come before the rest because of updating "msg_col".
2978 if (*p_vfile != NUL)
2979 verbose_write(s, maxlen);
2981 if (redir_fd != NULL
2982 #ifdef FEAT_EVAL
2983 || redir_reg || redir_vname
2984 #endif
2987 /* If the string doesn't start with CR or NL, go to msg_col */
2988 if (*s != '\n' && *s != '\r')
2990 while (cur_col < msg_col)
2992 #ifdef FEAT_EVAL
2993 if (redir_reg)
2994 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
2995 else if (redir_vname)
2996 var_redir_str((char_u *)" ", -1);
2997 else if (redir_fd)
2998 #endif
2999 fputs(" ", redir_fd);
3000 ++cur_col;
3004 #ifdef FEAT_EVAL
3005 if (redir_reg)
3006 write_reg_contents(redir_reg, s, maxlen, TRUE);
3007 if (redir_vname)
3008 var_redir_str(s, maxlen);
3009 #endif
3011 /* Adjust the current column */
3012 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3014 #ifdef FEAT_EVAL
3015 if (!redir_reg && !redir_vname && redir_fd != NULL)
3016 #endif
3017 putc(*s, redir_fd);
3018 if (*s == '\r' || *s == '\n')
3019 cur_col = 0;
3020 else if (*s == '\t')
3021 cur_col += (8 - cur_col % 8);
3022 else
3023 ++cur_col;
3024 ++s;
3027 if (msg_silent != 0) /* should update msg_col */
3028 msg_col = cur_col;
3033 * Before giving verbose messsage.
3034 * Must always be called paired with verbose_leave()!
3036 void
3037 verbose_enter()
3039 if (*p_vfile != NUL)
3040 ++msg_silent;
3044 * After giving verbose message.
3045 * Must always be called paired with verbose_enter()!
3047 void
3048 verbose_leave()
3050 if (*p_vfile != NUL)
3051 if (--msg_silent < 0)
3052 msg_silent = 0;
3056 * Like verbose_enter() and set msg_scroll when displaying the message.
3058 void
3059 verbose_enter_scroll()
3061 if (*p_vfile != NUL)
3062 ++msg_silent;
3063 else
3064 /* always scroll up, don't overwrite */
3065 msg_scroll = TRUE;
3069 * Like verbose_leave() and set cmdline_row when displaying the message.
3071 void
3072 verbose_leave_scroll()
3074 if (*p_vfile != NUL)
3076 if (--msg_silent < 0)
3077 msg_silent = 0;
3079 else
3080 cmdline_row = msg_row;
3083 static FILE *verbose_fd = NULL;
3084 static int verbose_did_open = FALSE;
3087 * Called when 'verbosefile' is set: stop writing to the file.
3089 void
3090 verbose_stop()
3092 if (verbose_fd != NULL)
3094 fclose(verbose_fd);
3095 verbose_fd = NULL;
3097 verbose_did_open = FALSE;
3101 * Open the file 'verbosefile'.
3102 * Return FAIL or OK.
3105 verbose_open()
3107 if (verbose_fd == NULL && !verbose_did_open)
3109 /* Only give the error message once. */
3110 verbose_did_open = TRUE;
3112 verbose_fd = mch_fopen((char *)p_vfile, "a");
3113 if (verbose_fd == NULL)
3115 EMSG2(_(e_notopen), p_vfile);
3116 return FAIL;
3119 return OK;
3123 * Write a string to 'verbosefile'.
3124 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3126 static void
3127 verbose_write(str, maxlen)
3128 char_u *str;
3129 int maxlen;
3131 char_u *s = str;
3132 static int cur_col = 0;
3134 /* Open the file when called the first time. */
3135 if (verbose_fd == NULL)
3136 verbose_open();
3138 if (verbose_fd != NULL)
3140 /* If the string doesn't start with CR or NL, go to msg_col */
3141 if (*s != '\n' && *s != '\r')
3143 while (cur_col < msg_col)
3145 fputs(" ", verbose_fd);
3146 ++cur_col;
3150 /* Adjust the current column */
3151 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3153 putc(*s, verbose_fd);
3154 if (*s == '\r' || *s == '\n')
3155 cur_col = 0;
3156 else if (*s == '\t')
3157 cur_col += (8 - cur_col % 8);
3158 else
3159 ++cur_col;
3160 ++s;
3166 * Give a warning message (for searching).
3167 * Use 'w' highlighting and may repeat the message after redrawing
3169 void
3170 give_warning(message, hl)
3171 char_u *message;
3172 int hl;
3174 /* Don't do this for ":silent". */
3175 if (msg_silent != 0)
3176 return;
3178 /* Don't want a hit-enter prompt here. */
3179 ++no_wait_return;
3181 #ifdef FEAT_EVAL
3182 set_vim_var_string(VV_WARNINGMSG, message, -1);
3183 #endif
3184 vim_free(keep_msg);
3185 keep_msg = NULL;
3186 if (hl)
3187 keep_msg_attr = hl_attr(HLF_W);
3188 else
3189 keep_msg_attr = 0;
3190 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
3191 set_keep_msg(message, keep_msg_attr);
3192 msg_didout = FALSE; /* overwrite this message */
3193 msg_nowait = TRUE; /* don't wait for this message */
3194 msg_col = 0;
3196 --no_wait_return;
3200 * Advance msg cursor to column "col".
3202 void
3203 msg_advance(col)
3204 int col;
3206 if (msg_silent != 0) /* nothing to advance to */
3208 msg_col = col; /* for redirection, may fill it up later */
3209 return;
3211 if (col >= Columns) /* not enough room */
3212 col = Columns - 1;
3213 #ifdef FEAT_RIGHTLEFT
3214 if (cmdmsg_rl)
3215 while (msg_col > Columns - col)
3216 msg_putchar(' ');
3217 else
3218 #endif
3219 while (msg_col < col)
3220 msg_putchar(' ');
3223 #if defined(FEAT_CON_DIALOG) || defined(PROTO)
3225 * Used for "confirm()" function, and the :confirm command prefix.
3226 * Versions which haven't got flexible dialogs yet, and console
3227 * versions, get this generic handler which uses the command line.
3229 * type = one of:
3230 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3231 * title = title string (can be NULL for default)
3232 * (neither used in console dialogs at the moment)
3234 * Format of the "buttons" string:
3235 * "Button1Name\nButton2Name\nButton3Name"
3236 * The first button should normally be the default/accept
3237 * The second button should be the 'Cancel' button
3238 * Other buttons- use your imagination!
3239 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3240 * different letter.
3242 /* ARGSUSED */
3244 do_dialog(type, title, message, buttons, dfltbutton, textfield)
3245 int type;
3246 char_u *title;
3247 char_u *message;
3248 char_u *buttons;
3249 int dfltbutton;
3250 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
3252 int oldState;
3253 int retval = 0;
3254 char_u *hotkeys;
3255 int c;
3256 int i;
3258 #ifndef NO_CONSOLE
3259 /* Don't output anything in silent mode ("ex -s") */
3260 if (silent_mode)
3261 return dfltbutton; /* return default option */
3262 #endif
3264 #ifdef FEAT_GUI_DIALOG
3265 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3266 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3268 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
3269 textfield);
3270 msg_end_prompt();
3272 /* Flush output to avoid that further messages and redrawing is done
3273 * in the wrong order. */
3274 out_flush();
3275 gui_mch_update();
3277 return c;
3279 #endif
3281 oldState = State;
3282 State = CONFIRM;
3283 #ifdef FEAT_MOUSE
3284 setmouse();
3285 #endif
3288 * Since we wait for a keypress, don't make the
3289 * user press RETURN as well afterwards.
3291 ++no_wait_return;
3292 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3294 if (hotkeys != NULL)
3296 for (;;)
3298 /* Get a typed character directly from the user. */
3299 c = get_keystroke();
3300 switch (c)
3302 case CAR: /* User accepts default option */
3303 case NL:
3304 retval = dfltbutton;
3305 break;
3306 case Ctrl_C: /* User aborts/cancels */
3307 case ESC:
3308 retval = 0;
3309 break;
3310 default: /* Could be a hotkey? */
3311 if (c < 0) /* special keys are ignored here */
3312 continue;
3313 /* Make the character lowercase, as chars in "hotkeys" are. */
3314 c = MB_TOLOWER(c);
3315 retval = 1;
3316 for (i = 0; hotkeys[i]; ++i)
3318 #ifdef FEAT_MBYTE
3319 if (has_mbyte)
3321 if ((*mb_ptr2char)(hotkeys + i) == c)
3322 break;
3323 i += (*mb_ptr2len)(hotkeys + i) - 1;
3325 else
3326 #endif
3327 if (hotkeys[i] == c)
3328 break;
3329 ++retval;
3331 if (hotkeys[i])
3332 break;
3333 /* No hotkey match, so keep waiting */
3334 continue;
3336 break;
3339 vim_free(hotkeys);
3342 State = oldState;
3343 #ifdef FEAT_MOUSE
3344 setmouse();
3345 #endif
3346 --no_wait_return;
3347 msg_end_prompt();
3349 return retval;
3352 static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3355 * Copy one character from "*from" to "*to", taking care of multi-byte
3356 * characters. Return the length of the character in bytes.
3358 static int
3359 copy_char(from, to, lowercase)
3360 char_u *from;
3361 char_u *to;
3362 int lowercase; /* make character lower case */
3364 #ifdef FEAT_MBYTE
3365 int len;
3366 int c;
3368 if (has_mbyte)
3370 if (lowercase)
3372 c = MB_TOLOWER((*mb_ptr2char)(from));
3373 return (*mb_char2bytes)(c, to);
3375 else
3377 len = (*mb_ptr2len)(from);
3378 mch_memmove(to, from, (size_t)len);
3379 return len;
3382 else
3383 #endif
3385 if (lowercase)
3386 *to = (char_u)TOLOWER_LOC(*from);
3387 else
3388 *to = *from;
3389 return 1;
3394 * Format the dialog string, and display it at the bottom of
3395 * the screen. Return a string of hotkey chars (if defined) for
3396 * each 'button'. If a button has no hotkey defined, the first character of
3397 * the button is used.
3398 * The hotkeys can be multi-byte characters, but without combining chars.
3400 * Returns an allocated string with hotkeys, or NULL for error.
3402 static char_u *
3403 msg_show_console_dialog(message, buttons, dfltbutton)
3404 char_u *message;
3405 char_u *buttons;
3406 int dfltbutton;
3408 int len = 0;
3409 #ifdef FEAT_MBYTE
3410 # define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3411 #else
3412 # define HOTK_LEN 1
3413 #endif
3414 int lenhotkey = HOTK_LEN; /* count first button */
3415 char_u *hotk = NULL;
3416 char_u *msgp = NULL;
3417 char_u *hotkp = NULL;
3418 char_u *r;
3419 int copy;
3420 #define HAS_HOTKEY_LEN 30
3421 char_u has_hotkey[HAS_HOTKEY_LEN];
3422 int first_hotkey = FALSE; /* first char of button is hotkey */
3423 int idx;
3425 has_hotkey[0] = FALSE;
3428 * First loop: compute the size of memory to allocate.
3429 * Second loop: copy to the allocated memory.
3431 for (copy = 0; copy <= 1; ++copy)
3433 r = buttons;
3434 idx = 0;
3435 while (*r)
3437 if (*r == DLG_BUTTON_SEP)
3439 if (copy)
3441 *msgp++ = ',';
3442 *msgp++ = ' '; /* '\n' -> ', ' */
3444 /* advance to next hotkey and set default hotkey */
3445 #ifdef FEAT_MBYTE
3446 if (has_mbyte)
3447 hotkp += (*mb_ptr2len)(hotkp);
3448 else
3449 #endif
3450 ++hotkp;
3451 (void)copy_char(r + 1, hotkp, TRUE);
3452 if (dfltbutton)
3453 --dfltbutton;
3455 /* If no hotkey is specified first char is used. */
3456 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3457 first_hotkey = TRUE;
3459 else
3461 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3462 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3463 if (idx < HAS_HOTKEY_LEN - 1)
3464 has_hotkey[++idx] = FALSE;
3467 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3469 if (*r == DLG_HOTKEY_CHAR)
3470 ++r;
3471 first_hotkey = FALSE;
3472 if (copy)
3474 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3475 *msgp++ = *r;
3476 else
3478 /* '&a' -> '[a]' */
3479 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3480 msgp += copy_char(r, msgp, FALSE);
3481 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3483 /* redefine hotkey */
3484 (void)copy_char(r, hotkp, TRUE);
3487 else
3489 ++len; /* '&a' -> '[a]' */
3490 if (idx < HAS_HOTKEY_LEN - 1)
3491 has_hotkey[idx] = TRUE;
3494 else
3496 /* everything else copy literally */
3497 if (copy)
3498 msgp += copy_char(r, msgp, FALSE);
3501 /* advance to the next character */
3502 mb_ptr_adv(r);
3505 if (copy)
3507 *msgp++ = ':';
3508 *msgp++ = ' ';
3509 *msgp = NUL;
3510 mb_ptr_adv(hotkp);
3511 *hotkp = NUL;
3513 else
3515 len += (int)(STRLEN(message)
3516 + 2 /* for the NL's */
3517 + STRLEN(buttons)
3518 + 3); /* for the ": " and NUL */
3519 lenhotkey++; /* for the NUL */
3521 /* If no hotkey is specified first char is used. */
3522 if (!has_hotkey[0])
3524 first_hotkey = TRUE;
3525 len += 2; /* "x" -> "[x]" */
3529 * Now allocate and load the strings
3531 vim_free(confirm_msg);
3532 confirm_msg = alloc(len);
3533 if (confirm_msg == NULL)
3534 return NULL;
3535 *confirm_msg = NUL;
3536 hotk = alloc(lenhotkey);
3537 if (hotk == NULL)
3538 return NULL;
3540 *confirm_msg = '\n';
3541 STRCPY(confirm_msg + 1, message);
3543 msgp = confirm_msg + 1 + STRLEN(message);
3544 hotkp = hotk;
3546 /* define first default hotkey */
3547 (void)copy_char(buttons, hotkp, TRUE);
3549 /* Remember where the choices start, displaying starts here when
3550 * "hotkp" typed at the more prompt. */
3551 confirm_msg_tail = msgp;
3552 *msgp++ = '\n';
3556 display_confirm_msg();
3557 return hotk;
3561 * Display the ":confirm" message. Also called when screen resized.
3563 void
3564 display_confirm_msg()
3566 /* avoid that 'q' at the more prompt truncates the message here */
3567 ++confirm_msg_used;
3568 if (confirm_msg != NULL)
3569 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3570 --confirm_msg_used;
3573 #endif /* FEAT_CON_DIALOG */
3575 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3578 vim_dialog_yesno(type, title, message, dflt)
3579 int type;
3580 char_u *title;
3581 char_u *message;
3582 int dflt;
3584 if (do_dialog(type,
3585 title == NULL ? (char_u *)_("Question") : title,
3586 message,
3587 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3588 return VIM_YES;
3589 return VIM_NO;
3593 vim_dialog_yesnocancel(type, title, message, dflt)
3594 int type;
3595 char_u *title;
3596 char_u *message;
3597 int dflt;
3599 switch (do_dialog(type,
3600 title == NULL ? (char_u *)_("Question") : title,
3601 message,
3602 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3604 case 1: return VIM_YES;
3605 case 2: return VIM_NO;
3607 return VIM_CANCEL;
3611 vim_dialog_yesnoallcancel(type, title, message, dflt)
3612 int type;
3613 char_u *title;
3614 char_u *message;
3615 int dflt;
3617 switch (do_dialog(type,
3618 title == NULL ? (char_u *)"Question" : title,
3619 message,
3620 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3621 dflt, NULL))
3623 case 1: return VIM_YES;
3624 case 2: return VIM_NO;
3625 case 3: return VIM_ALL;
3626 case 4: return VIM_DISCARDALL;
3628 return VIM_CANCEL;
3631 #endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3633 #if defined(FEAT_BROWSE) || defined(PROTO)
3635 * Generic browse function. Calls gui_mch_browse() when possible.
3636 * Later this may pop-up a non-GUI file selector (external command?).
3638 char_u *
3639 do_browse(flags, title, dflt, ext, initdir, filter, buf)
3640 int flags; /* BROWSE_SAVE and BROWSE_DIR */
3641 char_u *title; /* title for the window */
3642 char_u *dflt; /* default file name (may include directory) */
3643 char_u *ext; /* extension added */
3644 char_u *initdir; /* initial directory, NULL for current dir or
3645 when using path from "dflt" */
3646 char_u *filter; /* file name filter */
3647 buf_T *buf; /* buffer to read/write for */
3649 char_u *fname;
3650 static char_u *last_dir = NULL; /* last used directory */
3651 char_u *tofree = NULL;
3652 int save_browse = cmdmod.browse;
3654 /* Must turn off browse to avoid that autocommands will get the
3655 * flag too! */
3656 cmdmod.browse = FALSE;
3658 if (title == NULL || *title == NUL)
3660 if (flags & BROWSE_DIR)
3661 title = (char_u *)_("Select Directory dialog");
3662 else if (flags & BROWSE_SAVE)
3663 title = (char_u *)_("Save File dialog");
3664 else
3665 title = (char_u *)_("Open File dialog");
3668 /* When no directory specified, use default file name, default dir, buffer
3669 * dir, last dir or current dir */
3670 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3672 if (mch_isdir(dflt)) /* default file name is a directory */
3674 initdir = dflt;
3675 dflt = NULL;
3677 else if (gettail(dflt) != dflt) /* default file name includes a path */
3679 tofree = vim_strsave(dflt);
3680 if (tofree != NULL)
3682 initdir = tofree;
3683 *gettail(initdir) = NUL;
3684 dflt = gettail(dflt);
3689 if (initdir == NULL || *initdir == NUL)
3691 /* When 'browsedir' is a directory, use it */
3692 if (STRCMP(p_bsdir, "last") != 0
3693 && STRCMP(p_bsdir, "buffer") != 0
3694 && STRCMP(p_bsdir, "current") != 0
3695 && mch_isdir(p_bsdir))
3696 initdir = p_bsdir;
3697 /* When saving or 'browsedir' is "buffer", use buffer fname */
3698 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
3699 && buf != NULL && buf->b_ffname != NULL)
3701 if (dflt == NULL || *dflt == NUL)
3702 dflt = gettail(curbuf->b_ffname);
3703 tofree = vim_strsave(curbuf->b_ffname);
3704 if (tofree != NULL)
3706 initdir = tofree;
3707 *gettail(initdir) = NUL;
3710 /* When 'browsedir' is "last", use dir from last browse */
3711 else if (*p_bsdir == 'l')
3712 initdir = last_dir;
3713 /* When 'browsedir is "current", use current directory. This is the
3714 * default already, leave initdir empty. */
3717 # ifdef FEAT_GUI
3718 if (gui.in_use) /* when this changes, also adjust f_has()! */
3720 if (filter == NULL
3721 # ifdef FEAT_EVAL
3722 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3723 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3724 # endif
3726 filter = BROWSE_FILTER_DEFAULT;
3727 if (flags & BROWSE_DIR)
3729 # if defined(HAVE_GTK2) || defined(WIN3264)
3730 /* For systems that have a directory dialog. */
3731 fname = gui_mch_browsedir(title, initdir);
3732 # else
3733 /* Generic solution for selecting a directory: select a file and
3734 * remove the file name. */
3735 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3736 # endif
3737 # if !defined(HAVE_GTK2)
3738 /* Win32 adds a dummy file name, others return an arbitrary file
3739 * name. GTK+ 2 returns only the directory, */
3740 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3742 /* Remove the file name. */
3743 char_u *tail = gettail_sep(fname);
3745 if (tail == fname)
3746 *tail++ = '.'; /* use current dir */
3747 *tail = NUL;
3749 # endif
3751 else
3752 fname = gui_mch_browse(flags & BROWSE_SAVE,
3753 title, dflt, ext, initdir, filter);
3755 /* We hang around in the dialog for a while, the user might do some
3756 * things to our files. The Win32 dialog allows deleting or renaming
3757 * a file, check timestamps. */
3758 need_check_timestamps = TRUE;
3759 did_check_timestamps = FALSE;
3761 else
3762 # endif
3764 /* TODO: non-GUI file selector here */
3765 EMSG(_("E338: Sorry, no file browser in console mode"));
3766 fname = NULL;
3769 /* keep the directory for next time */
3770 if (fname != NULL)
3772 vim_free(last_dir);
3773 last_dir = vim_strsave(fname);
3774 if (last_dir != NULL && !(flags & BROWSE_DIR))
3776 *gettail(last_dir) = NUL;
3777 if (*last_dir == NUL)
3779 /* filename only returned, must be in current dir */
3780 vim_free(last_dir);
3781 last_dir = alloc(MAXPATHL);
3782 if (last_dir != NULL)
3783 mch_dirname(last_dir, MAXPATHL);
3788 vim_free(tofree);
3789 cmdmod.browse = save_browse;
3791 return fname;
3793 #endif
3795 #if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3796 static char *e_printf = N_("E766: Insufficient arguments for printf()");
3798 static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3799 static char *tv_str __ARGS((typval_T *tvs, int *idxp));
3802 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3804 static long
3805 tv_nr(tvs, idxp)
3806 typval_T *tvs;
3807 int *idxp;
3809 int idx = *idxp - 1;
3810 long n = 0;
3811 int err = FALSE;
3813 if (tvs[idx].v_type == VAR_UNKNOWN)
3814 EMSG(_(e_printf));
3815 else
3817 ++*idxp;
3818 n = get_tv_number_chk(&tvs[idx], &err);
3819 if (err)
3820 n = 0;
3822 return n;
3826 * Get string argument from "idxp" entry in "tvs". First entry is 1.
3827 * Returns NULL for an error.
3829 static char *
3830 tv_str(tvs, idxp)
3831 typval_T *tvs;
3832 int *idxp;
3834 int idx = *idxp - 1;
3835 char *s = NULL;
3837 if (tvs[idx].v_type == VAR_UNKNOWN)
3838 EMSG(_(e_printf));
3839 else
3841 ++*idxp;
3842 s = (char *)get_tv_string_chk(&tvs[idx]);
3844 return s;
3846 #endif
3849 * This code was included to provide a portable vsnprintf() and snprintf().
3850 * Some systems may provide their own, but we always use this one for
3851 * consistency.
3853 * This code is based on snprintf.c - a portable implementation of snprintf
3854 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
3855 * Included with permission. It was heavely modified to fit in Vim.
3856 * The original code, including useful comments, can be found here:
3857 * http://www.ijs.si/software/snprintf/
3859 * This snprintf() only supports the following conversion specifiers:
3860 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3861 * with flags: '-', '+', ' ', '0' and '#'.
3862 * An asterisk is supported for field width as well as precision.
3864 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3865 * 'll' (long long int) is not supported.
3867 * The locale is not used, the string is used as a byte string. This is only
3868 * relevant for double-byte encodings where the second byte may be '%'.
3870 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3871 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
3873 * The return value is the number of characters which would be generated
3874 * for the given input, excluding the trailing null. If this value
3875 * is greater or equal to "str_m", not all characters from the result
3876 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3877 * are discarded. If "str_m" is greater than zero it is guaranteed
3878 * the resulting string will be null-terminated.
3882 * When va_list is not supported we only define vim_snprintf().
3884 * vim_vsnprintf() can be invoked with either "va_list" or a list of
3885 * "typval_T". When the latter is not used it must be NULL.
3888 /* When generating prototypes all of this is skipped, cproto doesn't
3889 * understand this. */
3890 #ifndef PROTO
3891 # ifdef HAVE_STDARG_H
3893 vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3895 va_list ap;
3896 int str_l;
3898 va_start(ap, fmt);
3899 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
3900 va_end(ap);
3901 return str_l;
3905 vim_vsnprintf(str, str_m, fmt, ap, tvs)
3906 # else
3907 /* clumsy way to work around missing va_list */
3908 # 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)
3910 /* VARARGS */
3912 #ifdef __BORLANDC__
3913 _RTLENTRYF
3914 #endif
3915 vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
3916 # endif
3917 char *str;
3918 size_t str_m;
3919 char *fmt;
3920 # ifdef HAVE_STDARG_H
3921 va_list ap;
3922 typval_T *tvs;
3923 # else
3924 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
3925 # endif
3927 size_t str_l = 0;
3928 char *p = fmt;
3929 int arg_idx = 1;
3931 if (p == NULL)
3932 p = "";
3933 while (*p != NUL)
3935 if (*p != '%')
3937 char *q = strchr(p + 1, '%');
3938 size_t n = (q == NULL) ? STRLEN(p) : (q - p);
3940 /* Copy up to the next '%' or NUL without any changes. */
3941 if (str_l < str_m)
3943 size_t avail = str_m - str_l;
3945 mch_memmove(str + str_l, p, n > avail ? avail : n);
3947 p += n;
3948 str_l += n;
3950 else
3952 size_t min_field_width = 0, precision = 0;
3953 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3954 int alternate_form = 0, force_sign = 0;
3956 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3957 * ignored. */
3958 int space_for_positive = 1;
3960 /* allowed values: \0, h, l, L */
3961 char length_modifier = '\0';
3963 /* temporary buffer for simple numeric->string conversion */
3964 char tmp[32];
3966 /* string address in case of string argument */
3967 char *str_arg;
3969 /* natural field width of arg without padding and sign */
3970 size_t str_arg_l;
3972 /* unsigned char argument value - only defined for c conversion.
3973 * N.B. standard explicitly states the char argument for the c
3974 * conversion is unsigned */
3975 unsigned char uchar_arg;
3977 /* number of zeros to be inserted for numeric conversions as
3978 * required by the precision or minimal field width */
3979 size_t number_of_zeros_to_pad = 0;
3981 /* index into tmp where zero padding is to be inserted */
3982 size_t zero_padding_insertion_ind = 0;
3984 /* current conversion specifier character */
3985 char fmt_spec = '\0';
3987 str_arg = NULL;
3988 p++; /* skip '%' */
3990 /* parse flags */
3991 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
3992 || *p == '#' || *p == '\'')
3994 switch (*p)
3996 case '0': zero_padding = 1; break;
3997 case '-': justify_left = 1; break;
3998 case '+': force_sign = 1; space_for_positive = 0; break;
3999 case ' ': force_sign = 1;
4000 /* If both the ' ' and '+' flags appear, the ' '
4001 * flag should be ignored */
4002 break;
4003 case '#': alternate_form = 1; break;
4004 case '\'': break;
4006 p++;
4008 /* If the '0' and '-' flags both appear, the '0' flag should be
4009 * ignored. */
4011 /* parse field width */
4012 if (*p == '*')
4014 int j;
4016 p++;
4018 #ifndef HAVE_STDARG_H
4019 get_a_arg(arg_idx);
4020 #else
4021 # if defined(FEAT_EVAL)
4022 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4023 # endif
4024 va_arg(ap, int);
4025 #endif
4026 if (j >= 0)
4027 min_field_width = j;
4028 else
4030 min_field_width = -j;
4031 justify_left = 1;
4034 else if (VIM_ISDIGIT((int)(*p)))
4036 /* size_t could be wider than unsigned int; make sure we treat
4037 * argument like common implementations do */
4038 unsigned int uj = *p++ - '0';
4040 while (VIM_ISDIGIT((int)(*p)))
4041 uj = 10 * uj + (unsigned int)(*p++ - '0');
4042 min_field_width = uj;
4045 /* parse precision */
4046 if (*p == '.')
4048 p++;
4049 precision_specified = 1;
4050 if (*p == '*')
4052 int 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 p++;
4064 if (j >= 0)
4065 precision = j;
4066 else
4068 precision_specified = 0;
4069 precision = 0;
4072 else if (VIM_ISDIGIT((int)(*p)))
4074 /* size_t could be wider than unsigned int; make sure we
4075 * treat argument like common implementations do */
4076 unsigned int uj = *p++ - '0';
4078 while (VIM_ISDIGIT((int)(*p)))
4079 uj = 10 * uj + (unsigned int)(*p++ - '0');
4080 precision = uj;
4084 /* parse 'h', 'l' and 'll' length modifiers */
4085 if (*p == 'h' || *p == 'l')
4087 length_modifier = *p;
4088 p++;
4089 if (length_modifier == 'l' && *p == 'l')
4091 /* double l = long long */
4092 length_modifier = 'l'; /* treat it as a single 'l' */
4093 p++;
4096 fmt_spec = *p;
4098 /* common synonyms: */
4099 switch (fmt_spec)
4101 case 'i': fmt_spec = 'd'; break;
4102 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4103 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4104 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4105 default: break;
4108 /* get parameter value, do initial processing */
4109 switch (fmt_spec)
4111 /* '%' and 'c' behave similar to 's' regarding flags and field
4112 * widths */
4113 case '%':
4114 case 'c':
4115 case 's':
4116 length_modifier = '\0';
4117 zero_padding = 0; /* turn zero padding off for string
4118 conversions */
4119 str_arg_l = 1;
4120 switch (fmt_spec)
4122 case '%':
4123 str_arg = p;
4124 break;
4126 case 'c':
4128 int j;
4131 #ifndef HAVE_STDARG_H
4132 get_a_arg(arg_idx);
4133 #else
4134 # if defined(FEAT_EVAL)
4135 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4136 # endif
4137 va_arg(ap, int);
4138 #endif
4139 /* standard demands unsigned char */
4140 uchar_arg = (unsigned char)j;
4141 str_arg = (char *)&uchar_arg;
4142 break;
4145 case 's':
4146 str_arg =
4147 #ifndef HAVE_STDARG_H
4148 (char *)get_a_arg(arg_idx);
4149 #else
4150 # if defined(FEAT_EVAL)
4151 tvs != NULL ? tv_str(tvs, &arg_idx) :
4152 # endif
4153 va_arg(ap, char *);
4154 #endif
4155 if (str_arg == NULL)
4157 str_arg = "[NULL]";
4158 str_arg_l = 6;
4160 /* make sure not to address string beyond the specified
4161 * precision !!! */
4162 else if (!precision_specified)
4163 str_arg_l = strlen(str_arg);
4164 /* truncate string if necessary as requested by precision */
4165 else if (precision == 0)
4166 str_arg_l = 0;
4167 else
4169 /* memchr on HP does not like n > 2^31 !!! */
4170 char *q = memchr(str_arg, '\0',
4171 #if SIZEOF_INT <= 2
4172 precision
4173 #else
4174 precision <= (size_t)0x7fffffffL ? precision
4175 : (size_t)0x7fffffffL
4176 #endif
4178 str_arg_l = (q == NULL) ? precision : q - str_arg;
4180 break;
4182 default:
4183 break;
4185 break;
4187 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4189 /* NOTE: the u, o, x, X and p conversion specifiers
4190 * imply the value is unsigned; d implies a signed
4191 * value */
4193 /* 0 if numeric argument is zero (or if pointer is
4194 * NULL for 'p'), +1 if greater than zero (or nonzero
4195 * for unsigned arguments), -1 if negative (unsigned
4196 * argument is never negative) */
4197 int arg_sign = 0;
4199 /* only defined for length modifier h, or for no
4200 * length modifiers */
4201 int int_arg = 0;
4202 unsigned int uint_arg = 0;
4204 /* only defined for length modifier l */
4205 long int long_arg = 0;
4206 unsigned long int ulong_arg = 0;
4208 /* pointer argument value -only defined for p
4209 * conversion */
4210 void *ptr_arg = NULL;
4212 if (fmt_spec == 'p')
4214 length_modifier = '\0';
4215 ptr_arg =
4216 #ifndef HAVE_STDARG_H
4217 (void *)get_a_arg(arg_idx);
4218 #else
4219 # if defined(FEAT_EVAL)
4220 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
4221 # endif
4222 va_arg(ap, void *);
4223 #endif
4224 if (ptr_arg != NULL)
4225 arg_sign = 1;
4227 else if (fmt_spec == 'd')
4229 /* signed */
4230 switch (length_modifier)
4232 case '\0':
4233 case 'h':
4234 /* char and short arguments are passed as int. */
4235 int_arg =
4236 #ifndef HAVE_STDARG_H
4237 get_a_arg(arg_idx);
4238 #else
4239 # if defined(FEAT_EVAL)
4240 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4241 # endif
4242 va_arg(ap, int);
4243 #endif
4244 if (int_arg > 0)
4245 arg_sign = 1;
4246 else if (int_arg < 0)
4247 arg_sign = -1;
4248 break;
4249 case 'l':
4250 long_arg =
4251 #ifndef HAVE_STDARG_H
4252 get_a_arg(arg_idx);
4253 #else
4254 # if defined(FEAT_EVAL)
4255 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4256 # endif
4257 va_arg(ap, long int);
4258 #endif
4259 if (long_arg > 0)
4260 arg_sign = 1;
4261 else if (long_arg < 0)
4262 arg_sign = -1;
4263 break;
4266 else
4268 /* unsigned */
4269 switch (length_modifier)
4271 case '\0':
4272 case 'h':
4273 uint_arg =
4274 #ifndef HAVE_STDARG_H
4275 get_a_arg(arg_idx);
4276 #else
4277 # if defined(FEAT_EVAL)
4278 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4279 # endif
4280 va_arg(ap, unsigned int);
4281 #endif
4282 if (uint_arg != 0)
4283 arg_sign = 1;
4284 break;
4285 case 'l':
4286 ulong_arg =
4287 #ifndef HAVE_STDARG_H
4288 get_a_arg(arg_idx);
4289 #else
4290 # if defined(FEAT_EVAL)
4291 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4292 # endif
4293 va_arg(ap, unsigned long int);
4294 #endif
4295 if (ulong_arg != 0)
4296 arg_sign = 1;
4297 break;
4301 str_arg = tmp;
4302 str_arg_l = 0;
4304 /* NOTE:
4305 * For d, i, u, o, x, and X conversions, if precision is
4306 * specified, the '0' flag should be ignored. This is so
4307 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4308 * FreeBSD, NetBSD; but not with Perl.
4310 if (precision_specified)
4311 zero_padding = 0;
4312 if (fmt_spec == 'd')
4314 if (force_sign && arg_sign >= 0)
4315 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4316 /* leave negative numbers for sprintf to handle, to
4317 * avoid handling tricky cases like (short int)-32768 */
4319 else if (alternate_form)
4321 if (arg_sign != 0
4322 && (fmt_spec == 'x' || fmt_spec == 'X') )
4324 tmp[str_arg_l++] = '0';
4325 tmp[str_arg_l++] = fmt_spec;
4327 /* alternate form should have no effect for p
4328 * conversion, but ... */
4331 zero_padding_insertion_ind = str_arg_l;
4332 if (!precision_specified)
4333 precision = 1; /* default precision is 1 */
4334 if (precision == 0 && arg_sign == 0)
4336 /* When zero value is formatted with an explicit
4337 * precision 0, the resulting formatted string is
4338 * empty (d, i, u, o, x, X, p). */
4340 else
4342 char f[5];
4343 int f_l = 0;
4345 /* construct a simple format string for sprintf */
4346 f[f_l++] = '%';
4347 if (!length_modifier)
4349 else if (length_modifier == '2')
4351 f[f_l++] = 'l';
4352 f[f_l++] = 'l';
4354 else
4355 f[f_l++] = length_modifier;
4356 f[f_l++] = fmt_spec;
4357 f[f_l++] = '\0';
4359 if (fmt_spec == 'p')
4360 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4361 else if (fmt_spec == 'd')
4363 /* signed */
4364 switch (length_modifier)
4366 case '\0':
4367 case 'h': str_arg_l += sprintf(
4368 tmp + str_arg_l, f, int_arg);
4369 break;
4370 case 'l': str_arg_l += sprintf(
4371 tmp + str_arg_l, f, long_arg);
4372 break;
4375 else
4377 /* unsigned */
4378 switch (length_modifier)
4380 case '\0':
4381 case 'h': str_arg_l += sprintf(
4382 tmp + str_arg_l, f, uint_arg);
4383 break;
4384 case 'l': str_arg_l += sprintf(
4385 tmp + str_arg_l, f, ulong_arg);
4386 break;
4390 /* include the optional minus sign and possible
4391 * "0x" in the region before the zero padding
4392 * insertion point */
4393 if (zero_padding_insertion_ind < str_arg_l
4394 && tmp[zero_padding_insertion_ind] == '-')
4395 zero_padding_insertion_ind++;
4396 if (zero_padding_insertion_ind + 1 < str_arg_l
4397 && tmp[zero_padding_insertion_ind] == '0'
4398 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4399 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4400 zero_padding_insertion_ind += 2;
4404 size_t num_of_digits = str_arg_l
4405 - zero_padding_insertion_ind;
4407 if (alternate_form && fmt_spec == 'o'
4408 /* unless zero is already the first
4409 * character */
4410 && !(zero_padding_insertion_ind < str_arg_l
4411 && tmp[zero_padding_insertion_ind] == '0'))
4413 /* assure leading zero for alternate-form
4414 * octal numbers */
4415 if (!precision_specified
4416 || precision < num_of_digits + 1)
4418 /* precision is increased to force the
4419 * first character to be zero, except if a
4420 * zero value is formatted with an
4421 * explicit precision of zero */
4422 precision = num_of_digits + 1;
4423 precision_specified = 1;
4426 /* zero padding to specified precision? */
4427 if (num_of_digits < precision)
4428 number_of_zeros_to_pad = precision - num_of_digits;
4430 /* zero padding to specified minimal field width? */
4431 if (!justify_left && zero_padding)
4433 int n = (int)(min_field_width - (str_arg_l
4434 + number_of_zeros_to_pad));
4435 if (n > 0)
4436 number_of_zeros_to_pad += n;
4438 break;
4441 default:
4442 /* unrecognized conversion specifier, keep format string
4443 * as-is */
4444 zero_padding = 0; /* turn zero padding off for non-numeric
4445 convers. */
4446 justify_left = 1;
4447 min_field_width = 0; /* reset flags */
4449 /* discard the unrecognized conversion, just keep *
4450 * the unrecognized conversion character */
4451 str_arg = p;
4452 str_arg_l = 0;
4453 if (*p != NUL)
4454 str_arg_l++; /* include invalid conversion specifier
4455 unchanged if not at end-of-string */
4456 break;
4459 if (*p != NUL)
4460 p++; /* step over the just processed conversion specifier */
4462 /* insert padding to the left as requested by min_field_width;
4463 * this does not include the zero padding in case of numerical
4464 * conversions*/
4465 if (!justify_left)
4467 /* left padding with blank or zero */
4468 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
4470 if (pn > 0)
4472 if (str_l < str_m)
4474 size_t avail = str_m - str_l;
4476 vim_memset(str + str_l, zero_padding ? '0' : ' ',
4477 (size_t)pn > avail ? avail : pn);
4479 str_l += pn;
4483 /* zero padding as requested by the precision or by the minimal
4484 * field width for numeric conversions required? */
4485 if (number_of_zeros_to_pad == 0)
4487 /* will not copy first part of numeric right now, *
4488 * force it to be copied later in its entirety */
4489 zero_padding_insertion_ind = 0;
4491 else
4493 /* insert first part of numerics (sign or '0x') before zero
4494 * padding */
4495 int zn = (int)zero_padding_insertion_ind;
4497 if (zn > 0)
4499 if (str_l < str_m)
4501 size_t avail = str_m - str_l;
4503 mch_memmove(str + str_l, str_arg,
4504 (size_t)zn > avail ? avail : zn);
4506 str_l += zn;
4509 /* insert zero padding as requested by the precision or min
4510 * field width */
4511 zn = (int)number_of_zeros_to_pad;
4512 if (zn > 0)
4514 if (str_l < str_m)
4516 size_t avail = str_m-str_l;
4518 vim_memset(str + str_l, '0',
4519 (size_t)zn > avail ? avail : zn);
4521 str_l += zn;
4525 /* insert formatted string
4526 * (or as-is conversion specifier for unknown conversions) */
4528 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
4530 if (sn > 0)
4532 if (str_l < str_m)
4534 size_t avail = str_m - str_l;
4536 mch_memmove(str + str_l,
4537 str_arg + zero_padding_insertion_ind,
4538 (size_t)sn > avail ? avail : sn);
4540 str_l += sn;
4544 /* insert right padding */
4545 if (justify_left)
4547 /* right blank padding to the field width */
4548 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
4550 if (pn > 0)
4552 if (str_l < str_m)
4554 size_t avail = str_m - str_l;
4556 vim_memset(str + str_l, ' ',
4557 (size_t)pn > avail ? avail : pn);
4559 str_l += pn;
4565 if (str_m > 0)
4567 /* make sure the string is nul-terminated even at the expense of
4568 * overwriting the last character (shouldn't happen, but just in case)
4569 * */
4570 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4573 #ifdef HAVE_STDARG_H
4574 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
4575 EMSG(_("E767: Too many arguments to printf()"));
4576 #endif
4578 /* Return the number of characters formatted (excluding trailing nul
4579 * character), that is, the number of characters that would have been
4580 * written to the buffer if it were large enough. */
4581 return (int)str_l;
4584 #endif /* PROTO */