xref: /vim-8.2.3635/src/message.c (revision 7171abea)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * message.c: functions for displaying messages on the command line
12  */
13 
14 #define MESSAGE_FILE		/* don't include prototype for smsg() */
15 
16 #include "vim.h"
17 
18 #ifdef HAVE_STDARG_H
19 # include <stdarg.h>
20 #endif
21 
22 static void reset_last_sourcing __ARGS((void));
23 static int other_sourcing_name __ARGS((void));
24 static char_u *get_emsg_source __ARGS((void));
25 static char_u *get_emsg_lnum __ARGS((void));
26 static void add_msg_hist __ARGS((char_u *s, int len, int attr));
27 static void hit_return_msg __ARGS((void));
28 static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
29 #ifdef FEAT_MBYTE
30 static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
31 #endif
32 static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
33 static void t_puts __ARGS((int t_col, char_u *t_s, char_u *s, int attr));
34 static void msg_screen_putchar __ARGS((int c, int attr));
35 static int  msg_check_screen __ARGS((void));
36 static void redir_write __ARGS((char_u *s, int maxlen));
37 #ifdef FEAT_CON_DIALOG
38 static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
39 static int	confirm_msg_used = FALSE;	/* displaying confirm_msg */
40 static char_u	*confirm_msg = NULL;		/* ":confirm" message */
41 static char_u	*confirm_msg_tail;		/* tail of confirm_msg */
42 #endif
43 
44 struct msg_hist
45 {
46     struct msg_hist	*next;
47     char_u		*msg;
48     int			attr;
49 };
50 
51 static struct msg_hist *first_msg_hist = NULL;
52 static struct msg_hist *last_msg_hist = NULL;
53 static int msg_hist_len = 0;
54 static int msg_hist_off = FALSE;	/* don't add messages to history */
55 
56 /*
57  * When writing messages to the screen, there are many different situations.
58  * A number of variables is used to remember the current state:
59  * msg_didany	    TRUE when messages were written since the last time the
60  *		    user reacted to a prompt.
61  *		    Reset: After hitting a key for the hit-return prompt,
62  *		    hitting <CR> for the command line or input().
63  *		    Set: When any message is written to the screen.
64  * msg_didout	    TRUE when something was written to the current line.
65  *		    Reset: When advancing to the next line, when the current
66  *		    text can be overwritten.
67  *		    Set: When any message is written to the screen.
68  * msg_nowait	    No extra delay for the last drawn message.
69  *		    Used in normal_cmd() before the mode message is drawn.
70  * emsg_on_display  There was an error message recently.  Indicates that there
71  *		    should be a delay before redrawing.
72  * msg_scroll	    The next message should not overwrite the current one.
73  * msg_scrolled	    How many lines the screen has been scrolled (because of
74  *		    messages).  Used in update_screen() to scroll the screen
75  *		    back.  Incremented each time the screen scrolls a line.
76  * msg_scrolled_ign  TRUE when msg_scrolled is non-zero and msg_puts_attr()
77  *		    writes something without scrolling should not make
78  *		    need_wait_return to be set.  This is a hack to make ":ts"
79  *		    work without an extra prompt.
80  * lines_left	    Number of lines available for messages before the
81  *		    more-prompt is to be given.
82  * need_wait_return TRUE when the hit-return prompt is needed.
83  *		    Reset: After giving the hit-return prompt, when the user
84  *		    has answered some other prompt.
85  *		    Set: When the ruler or typeahead display is overwritten,
86  *		    scrolling the screen for some message.
87  * keep_msg	    Message to be displayed after redrawing the screen, in
88  *		    main_loop().
89  *		    This is an allocated string or NULL when not used.
90  */
91 
92 /*
93  * msg(s) - displays the string 's' on the status line
94  * When terminal not initialized (yet) mch_errmsg(..) is used.
95  * return TRUE if wait_return not called
96  */
97     int
98 msg(s)
99     char_u	*s;
100 {
101     return msg_attr_keep(s, 0, FALSE);
102 }
103 
104     int
105 msg_attr(s, attr)
106     char_u	*s;
107     int		attr;
108 {
109     return msg_attr_keep(s, attr, FALSE);
110 }
111 
112     int
113 msg_attr_keep(s, attr, keep)
114     char_u	*s;
115     int		attr;
116     int		keep;	    /* TRUE: set keep_msg if it doesn't scroll */
117 {
118     static int	entered = 0;
119     int		retval;
120     char_u	*buf = NULL;
121 
122 #ifdef FEAT_EVAL
123     if (attr == 0)
124 	set_vim_var_string(VV_STATUSMSG, s, -1);
125 #endif
126 
127     /*
128      * It is possible that displaying a messages causes a problem (e.g.,
129      * when redrawing the window), which causes another message, etc..	To
130      * break this loop, limit the recursiveness to 3 levels.
131      */
132     if (entered >= 3)
133 	return TRUE;
134     ++entered;
135 
136     /* Add message to history (unless it's a repeated kept message or a
137      * truncated message) */
138     if (s != keep_msg
139 	    || (*s != '<'
140 		&& last_msg_hist != NULL
141 		&& last_msg_hist->msg != NULL
142 		&& STRCMP(s, last_msg_hist->msg)))
143 	add_msg_hist(s, -1, attr);
144 
145     /* When displaying keep_msg, don't let msg_start() free it, caller must do
146      * that. */
147     if (s == keep_msg)
148 	keep_msg = NULL;
149 
150     /* Truncate the message if needed. */
151     buf = msg_strtrunc(s);
152     if (buf != NULL)
153 	s = buf;
154 
155     msg_start();
156     msg_outtrans_attr(s, attr);
157     msg_clr_eos();
158     retval = msg_end();
159 
160     if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
161 							   * Columns + sc_col)
162     {
163 	set_keep_msg(s);
164 	keep_msg_attr = 0;
165     }
166 
167     vim_free(buf);
168     --entered;
169     return retval;
170 }
171 
172 /*
173  * Truncate a string such that it can be printed without causing a scroll.
174  * Returns an allocated string or NULL when no truncating is done.
175  */
176     char_u *
177 msg_strtrunc(s)
178     char_u	*s;
179 {
180     char_u	*buf = NULL;
181     int		len;
182     int		room;
183 
184     /* May truncate message to avoid a hit-return prompt */
185     if (!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
186 							    && !exmode_active)
187     {
188 	len = vim_strsize(s);
189 	room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
190 	if (len > room && room > 0)
191 	{
192 #ifdef FEAT_MBYTE
193 	    if (enc_utf8)
194 		/* may have up to 18 bytes per cell (6 per char, up to two
195 		 * composing chars) */
196 		buf = alloc((room + 2) * 18);
197 	    else if (enc_dbcs == DBCS_JPNU)
198 		/* may have up to 2 bytes per cell for euc-jp */
199 		buf = alloc((room + 2) * 2);
200 	    else
201 #endif
202 		buf = alloc(room + 2);
203 	    if (buf != NULL)
204 		trunc_string(s, buf, room);
205 	}
206     }
207     return buf;
208 }
209 
210 /*
211  * Truncate a string "s" to "buf" with cell width "room".
212  * "s" and "buf" may be equal.
213  */
214     void
215 trunc_string(s, buf, room)
216     char_u	*s;
217     char_u	*buf;
218     int		room;
219 {
220     int		half;
221     int		len;
222     int		e;
223     int		i;
224     int		n;
225 
226     room -= 3;
227     half = room / 2;
228     len = 0;
229 
230     /* First part: Start of the string. */
231     for (e = 0; len < half; ++e)
232     {
233 	if (s[e] == NUL)
234 	{
235 	    /* text fits without truncating! */
236 	    buf[e] = NUL;
237 	    return;
238 	}
239 	n = ptr2cells(s + e);
240 	if (len + n >= half)
241 	    break;
242 	len += n;
243 	buf[e] = s[e];
244 #ifdef FEAT_MBYTE
245 	if (has_mbyte)
246 	    for (n = (*mb_ptr2len_check)(s + e); --n > 0; )
247 	    {
248 		++e;
249 		buf[e] = s[e];
250 	    }
251 #endif
252     }
253 
254     /* Last part: End of the string. */
255     i = e;
256 #ifdef FEAT_MBYTE
257     if (enc_dbcs != 0)
258     {
259 	/* For DBCS going backwards in a string is slow, but
260 	 * computing the cell width isn't too slow: go forward
261 	 * until the rest fits. */
262 	n = vim_strsize(s + i);
263 	while (len + n > room)
264 	{
265 	    n -= ptr2cells(s + i);
266 	    i += (*mb_ptr2len_check)(s + i);
267 	}
268     }
269     else if (enc_utf8)
270     {
271 	/* For UTF-8 we can go backwards easily. */
272 	i = (int)STRLEN(s);
273 	for (;;)
274 	{
275 	    half = i - (*mb_head_off)(s, s + i - 1) - 1;
276 	    n = ptr2cells(s + half);
277 	    if (len + n > room)
278 		break;
279 	    len += n;
280 	    i = half;
281 	}
282     }
283     else
284 #endif
285     {
286 	for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
287 	    len += n;
288     }
289 
290     /* Set the middle and copy the last part. */
291     mch_memmove(buf + e, "...", (size_t)3);
292     mch_memmove(buf + e + 3, s + i, STRLEN(s + i) + 1);
293 }
294 
295 /*
296  * Automatic prototype generation does not understand this function.
297  * Note: Caller of smgs() and smsg_attr() must check the resulting string is
298  * shorter than IOSIZE!!!
299  */
300 #ifndef PROTO
301 # ifndef HAVE_STDARG_H
302 
303 int
304 #ifdef __BORLANDC__
305 _RTLENTRYF
306 #endif
307 smsg __ARGS((char_u *, long, long, long,
308 			long, long, long, long, long, long, long));
309 int
310 #ifdef __BORLANDC__
311 _RTLENTRYF
312 #endif
313 smsg_attr __ARGS((int, char_u *, long, long, long,
314 			long, long, long, long, long, long, long));
315 
316 /* VARARGS */
317     int
318 #ifdef __BORLANDC__
319 _RTLENTRYF
320 #endif
321 smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
322     char_u	*s;
323     long	a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
324 {
325     return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
326 }
327 
328 /* VARARGS */
329     int
330 #ifdef __BORLANDC__
331 _RTLENTRYF
332 #endif
333 smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
334     int		attr;
335     char_u	*s;
336     long	a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
337 {
338     sprintf((char *)IObuff, (char *)s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
339     return msg_attr(IObuff, attr);
340 }
341 
342 # else /* HAVE_STDARG_H */
343 
344     int
345 #ifdef __BORLANDC__
346 _RTLENTRYF
347 #endif
348 smsg(char_u *s, ...)
349 {
350     va_list arglist;
351 
352     va_start(arglist, s);
353 # ifdef HAVE_VSNPRINTF
354     vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
355 # else
356     vsprintf((char *)IObuff, (char *)s, arglist);
357 # endif
358     va_end(arglist);
359     return msg(IObuff);
360 }
361 
362     int
363 #ifdef __BORLANDC__
364 _RTLENTRYF
365 #endif
366 smsg_attr(int attr, char_u *s, ...)
367 {
368     va_list arglist;
369 
370     va_start(arglist, s);
371 # ifdef HAVE_VSNPRINTF
372     vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
373 # else
374     vsprintf((char *)IObuff, (char *)s, arglist);
375 # endif
376     va_end(arglist);
377     return msg_attr(IObuff, attr);
378 }
379 
380 # endif /* HAVE_STDARG_H */
381 #endif
382 
383 /*
384  * Remember the last sourcing name/lnum used in an error message, so that it
385  * isn't printed each time when it didn't change.
386  */
387 static int	last_sourcing_lnum = 0;
388 static char_u   *last_sourcing_name = NULL;
389 
390 /*
391  * Reset the last used sourcing name/lnum.  Makes sure it is displayed again
392  * for the next error message;
393  */
394     static void
395 reset_last_sourcing()
396 {
397     vim_free(last_sourcing_name);
398     last_sourcing_name = NULL;
399     last_sourcing_lnum = 0;
400 }
401 
402 /*
403  * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
404  */
405     static int
406 other_sourcing_name()
407 {
408     if (sourcing_name != NULL)
409     {
410 	if (last_sourcing_name != NULL)
411 	    return STRCMP(sourcing_name, last_sourcing_name) != 0;
412 	return TRUE;
413     }
414     return FALSE;
415 }
416 
417 /*
418  * Get the message about the source, as used for an error message.
419  * Returns an allocated string with room for one more character.
420  * Returns NULL when no message is to be given.
421  */
422     static char_u *
423 get_emsg_source()
424 {
425     char_u	*Buf, *p;
426 
427     if (sourcing_name != NULL && other_sourcing_name())
428     {
429 	p = (char_u *)_("Error detected while processing %s:");
430 	Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
431 	if (Buf != NULL)
432 	    sprintf((char *)Buf, (char *)p, sourcing_name);
433 	return Buf;
434     }
435     return NULL;
436 }
437 
438 /*
439  * Get the message about the source lnum, as used for an error message.
440  * Returns an allocated string with room for one more character.
441  * Returns NULL when no message is to be given.
442  */
443     static char_u *
444 get_emsg_lnum()
445 {
446     char_u	*Buf, *p;
447 
448     /* lnum is 0 when executing a command from the command line
449      * argument, we don't want a line number then */
450     if (sourcing_name != NULL
451 	    && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
452 	    && sourcing_lnum != 0)
453     {
454 	p = (char_u *)_("line %4ld:");
455 	Buf = alloc((unsigned)(STRLEN(p) + 20));
456 	if (Buf != NULL)
457 	    sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
458 	return Buf;
459     }
460     return NULL;
461 }
462 
463 /*
464  * Display name and line number for the source of an error.
465  * Remember the file name and line number, so that for the next error the info
466  * is only displayed if it changed.
467  */
468     void
469 msg_source(attr)
470     int		attr;
471 {
472     char_u	*p;
473 
474     ++no_wait_return;
475     p = get_emsg_source();
476     if (p != NULL)
477     {
478 	msg_attr(p, attr);
479 	vim_free(p);
480     }
481     p = get_emsg_lnum();
482     if (p != NULL)
483     {
484 	msg_attr(p, hl_attr(HLF_N));
485 	vim_free(p);
486 	last_sourcing_lnum = sourcing_lnum;  /* only once for each line */
487     }
488 
489     /* remember the last sourcing name printed, also when it's empty */
490     if (sourcing_name == NULL || other_sourcing_name)
491     {
492 	vim_free(last_sourcing_name);
493 	if (sourcing_name == NULL)
494 	    last_sourcing_name = NULL;
495 	else
496 	    last_sourcing_name = vim_strsave(sourcing_name);
497     }
498     --no_wait_return;
499 }
500 
501 /*
502  * emsg() - display an error message
503  *
504  * Rings the bell, if appropriate, and calls message() to do the real work
505  * When terminal not initialized (yet) mch_errmsg(..) is used.
506  *
507  * return TRUE if wait_return not called
508  */
509     int
510 emsg(s)
511     char_u	*s;
512 {
513     int		attr;
514     char_u	*p;
515 #ifdef FEAT_EVAL
516     int		ignore = FALSE;
517     int		severe;
518 #endif
519 
520     called_emsg = TRUE;
521 
522     /*
523      * If "emsg_severe" is TRUE: When an error exception is to be thrown, prefer
524      * this message over previous messages for the same command.
525      */
526 #ifdef FEAT_EVAL
527     severe = emsg_severe;
528     emsg_severe = FALSE;
529 #endif
530 
531     /*
532      * If "emsg_off" is set: no error messages at the moment.
533      * If 'debug' is set: do error message anyway, but without side effects.
534      * If "emsg_skip" is set: never do error messages.
535      */
536     if ((emsg_off > 0 && *p_debug == NUL)
537 #ifdef FEAT_EVAL
538 	    || emsg_skip > 0
539 #endif
540 	    )
541 	return TRUE;
542 
543     if (!emsg_off)
544     {
545 #ifdef FEAT_EVAL
546 	/*
547 	 * Cause a throw of an error exception if appropriate.  Don't display
548 	 * the error message in this case.  (If no matching catch clause will
549 	 * be found, the message will be displayed later on.)  "ignore" is set
550 	 * when the message should be ignored completely (used for the
551 	 * interrupt message).
552 	 */
553 	if (cause_errthrow(s, severe, &ignore) == TRUE)
554 	{
555 	    if (!ignore)
556 		did_emsg = TRUE;
557 	    return TRUE;
558 	}
559 
560 	/* set "v:errmsg", also when using ":silent! cmd" */
561 	set_vim_var_string(VV_ERRMSG, s, -1);
562 #endif
563 
564 	/*
565 	 * When using ":silent! cmd" ignore error messsages.
566 	 * But do write it to the redirection file.
567 	 */
568 	if (emsg_silent != 0)
569 	{
570 	    msg_start();
571 	    p = get_emsg_source();
572 	    if (p != NULL)
573 	    {
574 		STRCAT(p, "\n");
575 		redir_write(p, -1);
576 		vim_free(p);
577 	    }
578 	    p = get_emsg_lnum();
579 	    if (p != NULL)
580 	    {
581 		STRCAT(p, "\n");
582 		redir_write(p, -1);
583 		vim_free(p);
584 	    }
585 	    redir_write(s, -1);
586 	    return TRUE;
587 	}
588 
589 	/* Reset msg_silent, an error causes messages to be switched back on. */
590 	msg_silent = 0;
591 	cmd_silent = FALSE;
592 
593 	if (global_busy)		/* break :global command */
594 	    ++global_busy;
595 
596 	if (p_eb)
597 	    beep_flush();		/* also includes flush_buffers() */
598 	else
599 	    flush_buffers(FALSE);	/* flush internal buffers */
600 	did_emsg = TRUE;		/* flag for DoOneCmd() */
601     }
602 
603     emsg_on_display = TRUE;	/* remember there is an error message */
604     ++msg_scroll;		/* don't overwrite a previous message */
605     attr = hl_attr(HLF_E);	/* set highlight mode for error messages */
606     if (msg_scrolled)
607 	need_wait_return = TRUE;    /* needed in case emsg() is called after
608 				     * wait_return has reset need_wait_return
609 				     * and a redraw is expected because
610 				     * msg_scrolled is non-zero */
611 
612     /*
613      * Display name and line number for the source of the error.
614      */
615     msg_source(attr);
616 
617     /*
618      * Display the error message itself.
619      */
620     msg_nowait = FALSE;			/* wait for this msg */
621     return msg_attr(s, attr);
622 }
623 
624 /*
625  * Print an error message with one "%s" and one string argument.
626  */
627     int
628 emsg2(s, a1)
629     char_u *s, *a1;
630 {
631     return emsg3(s, a1, NULL);
632 }
633 
634 /*
635  * Print an error message with one or two "%s" and one or two string arguments.
636  */
637     int
638 emsg3(s, a1, a2)
639     char_u *s, *a1, *a2;
640 {
641     if ((emsg_off > 0 && *p_debug == NUL)
642 #ifdef FEAT_EVAL
643 	    || emsg_skip > 0
644 #endif
645 	    )
646 	return TRUE;		/* no error messages at the moment */
647 
648     /* Check for NULL strings (just in case) */
649     if (a1 == NULL)
650 	a1 = (char_u *)"[NULL]";
651     if (a2 == NULL)
652 	a2 = (char_u *)"[NULL]";
653 
654     /* Check for very long strings (can happen with ":help ^A<CR>"). */
655     if (STRLEN(s) + STRLEN(a1) + STRLEN(a2) >= (size_t)IOSIZE)
656 	a1 = a2 = (char_u *)_("[string too long]");
657 
658     sprintf((char *)IObuff, (char *)s, (char *)a1, (char *)a2);
659     return emsg(IObuff);
660 }
661 
662 /*
663  * Print an error message with one "%ld" and one long int argument.
664  */
665     int
666 emsgn(s, n)
667     char_u	*s;
668     long	n;
669 {
670     if ((emsg_off > 0 && *p_debug == NUL)
671 #ifdef FEAT_EVAL
672 	    || emsg_skip > 0
673 #endif
674 	    )
675 	return TRUE;		/* no error messages at the moment */
676     sprintf((char *)IObuff, (char *)s, n);
677     return emsg(IObuff);
678 }
679 
680 /*
681  * Like msg(), but truncate to a single line if p_shm contains 't', or when
682  * "force" is TRUE.  This truncates in another way as for normal messages.
683  * Careful: The string may be changed by msg_may_trunc()!
684  * Returns a pointer to the printed message, if wait_return() not called.
685  */
686     char_u *
687 msg_trunc_attr(s, force, attr)
688     char_u	*s;
689     int		force;
690     int		attr;
691 {
692     int		n;
693 
694     /* Add message to history before truncating */
695     add_msg_hist(s, -1, attr);
696 
697     s = msg_may_trunc(force, s);
698 
699     msg_hist_off = TRUE;
700     n = msg_attr(s, attr);
701     msg_hist_off = FALSE;
702 
703     if (n)
704 	return s;
705     return NULL;
706 }
707 
708 /*
709  * Check if message "s" should be truncated at the start (for filenames).
710  * Return a pointer to where the truncated message starts.
711  * Note: May change the message by replacing a character with '<'.
712  */
713     char_u *
714 msg_may_trunc(force, s)
715     int		force;
716     char_u	*s;
717 {
718     int		n;
719     int		room;
720 
721     room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
722     if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
723 	    && (n = (int)STRLEN(s) - room) > 0)
724     {
725 #ifdef FEAT_MBYTE
726 	if (has_mbyte)
727 	{
728 	    int	size = vim_strsize(s);
729 
730 	    for (n = 0; size >= room; )
731 	    {
732 		size -= (*mb_ptr2cells)(s + n);
733 		n += (*mb_ptr2len_check)(s + n);
734 	    }
735 	    --n;
736 	}
737 #endif
738 	s += n;
739 	*s = '<';
740     }
741     return s;
742 }
743 
744     static void
745 add_msg_hist(s, len, attr)
746     char_u	*s;
747     int		len;		/* -1 for undetermined length */
748     int		attr;
749 {
750     struct msg_hist *p;
751 
752     if (msg_hist_off || msg_silent != 0)
753 	return;
754 
755     /* Don't let the message history get too big */
756     while (msg_hist_len > 20)
757     {
758 	p = first_msg_hist;
759 	first_msg_hist = p->next;
760 	vim_free(p->msg);
761 	vim_free(p);
762 	--msg_hist_len;
763     }
764     /* allocate an entry and add the message at the end of the history */
765     p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
766     if (p != NULL)
767     {
768 	if (len < 0)
769 	    len = (int)STRLEN(s);
770 	/* remove leading and trailing newlines */
771 	while (len > 0 && *s == '\n')
772 	{
773 	    ++s;
774 	    --len;
775 	}
776 	while (len > 0 && s[len - 1] == '\n')
777 	    --len;
778 	p->msg = vim_strnsave(s, len);
779 	p->next = NULL;
780 	p->attr = attr;
781 	if (last_msg_hist != NULL)
782 	    last_msg_hist->next = p;
783 	last_msg_hist = p;
784 	if (first_msg_hist == NULL)
785 	    first_msg_hist = last_msg_hist;
786 	++msg_hist_len;
787     }
788 }
789 
790 /*
791  * ":messages" command.
792  */
793 /*ARGSUSED*/
794     void
795 ex_messages(eap)
796     exarg_T	*eap;
797 {
798     struct msg_hist *p;
799     char_u	    *s;
800 
801     msg_hist_off = TRUE;
802 
803     s = mch_getenv((char_u *)"LANG");
804     if (s != NULL && *s != NUL)
805 	msg_attr((char_u *)
806 		_("Messages maintainer: Bram Moolenaar <[email protected]>"),
807 		hl_attr(HLF_T));
808 
809     for (p = first_msg_hist; p != NULL; p = p->next)
810 	if (p->msg != NULL)
811 	    msg_attr(p->msg, p->attr);
812 
813     msg_hist_off = FALSE;
814 }
815 
816 #if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
817 /*
818  * Call this after prompting the user.  This will avoid a hit-return message
819  * and a delay.
820  */
821     void
822 msg_end_prompt()
823 {
824     need_wait_return = FALSE;
825     emsg_on_display = FALSE;
826     cmdline_row = msg_row;
827     msg_col = 0;
828     msg_clr_eos();
829 }
830 #endif
831 
832 /*
833  * wait for the user to hit a key (normally a return)
834  * if 'redraw' is TRUE, clear and redraw the screen
835  * if 'redraw' is FALSE, just redraw the screen
836  * if 'redraw' is -1, don't redraw at all
837  */
838     void
839 wait_return(redraw)
840     int		redraw;
841 {
842     int		c;
843     int		oldState;
844     int		tmpState;
845 #ifndef ORG_HITRETURN
846     int		had_got_int;
847 #endif
848 
849     if (redraw == TRUE)
850 	must_redraw = CLEAR;
851 
852     /* If using ":silent cmd", don't wait for a return.  Also don't set
853      * need_wait_return to do it later. */
854     if (msg_silent != 0)
855 	return;
856 
857 /*
858  * With the global command (and some others) we only need one return at the
859  * end. Adjust cmdline_row to avoid the next message overwriting the last one.
860  * When inside vgetc(), we can't wait for a typed character at all.
861  */
862     if (vgetc_busy)
863 	return;
864     if (no_wait_return)
865     {
866 	need_wait_return = TRUE;
867 	if (!exmode_active)
868 	    cmdline_row = msg_row;
869 	return;
870     }
871 
872     redir_off = TRUE;		/* don't redirect this message */
873     oldState = State;
874     if (quit_more)
875     {
876 	c = CAR;		/* just pretend CR was hit */
877 	quit_more = FALSE;
878 	got_int = FALSE;
879     }
880     else if (exmode_active)
881     {
882 	MSG_PUTS(" ");		/* make sure the cursor is on the right line */
883 	c = CAR;		/* no need for a return in ex mode */
884 	got_int = FALSE;
885     }
886     else
887     {
888 	/* Make sure the hit-return prompt is on screen when 'guioptions' was
889 	 * just changed. */
890 	screenalloc(FALSE);
891 
892 	State = HITRETURN;
893 #ifdef FEAT_MOUSE
894 	setmouse();
895 #endif
896 #ifdef USE_ON_FLY_SCROLL
897 	dont_scroll = TRUE;		/* disallow scrolling here */
898 #endif
899 	hit_return_msg();
900 
901 #ifdef ORG_HITRETURN
902 	do
903 	{
904 	    c = safe_vgetc();
905 	} while (vim_strchr((char_u *)"\r\n: ", c) == NULL);
906 	if (c == ':')			/* this can vi too (but not always!) */
907 	    stuffcharReadbuff(c);
908 #else
909 	do
910 	{
911 	    /* Remember "got_int", if it is set vgetc() probably returns a
912 	     * CTRL-C, but we need to loop then. */
913 	    had_got_int = got_int;
914 	    c = safe_vgetc();
915 	    if (!global_busy)
916 		got_int = FALSE;
917 #ifdef FEAT_CLIPBOARD
918 	    /* Strange way to allow copying (yanking) a modeless selection at
919 	     * the hit-enter prompt.  Use CTRL-Y, because the same is used in
920 	     * Cmdline-mode and it's harmless when there is no selection. */
921 	    if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
922 	    {
923 		clip_copy_modeless_selection(TRUE);
924 		c = K_IGNORE;
925 	    }
926 #endif
927 	} while ((had_got_int && c == Ctrl_C)
928 				|| c == K_IGNORE
929 #ifdef FEAT_GUI
930 				|| c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
931 #endif
932 #ifdef FEAT_MOUSE
933 				|| c == K_LEFTDRAG   || c == K_LEFTRELEASE
934 				|| c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
935 				|| c == K_RIGHTDRAG  || c == K_RIGHTRELEASE
936 				|| c == K_MOUSEDOWN  || c == K_MOUSEUP
937 				|| (!mouse_has(MOUSE_RETURN)
938 				    && mouse_row < msg_row
939 				    && (c == K_LEFTMOUSE
940 					|| c == K_MIDDLEMOUSE
941 					|| c == K_RIGHTMOUSE
942 					|| c == K_X1MOUSE
943 					|| c == K_X2MOUSE))
944 #endif
945 				);
946 	ui_breakcheck();
947 #ifdef FEAT_MOUSE
948 	/*
949 	 * Avoid that the mouse-up event causes visual mode to start.
950 	 */
951 	if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
952 					  || c == K_X1MOUSE || c == K_X2MOUSE)
953 	    (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
954 	else
955 #endif
956 	    if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
957 	{
958 	    stuffcharReadbuff(c);
959 	    do_redraw = TRUE;	    /* need a redraw even though there is
960 				       something in the stuff buffer */
961 	}
962 #endif
963     }
964     redir_off = FALSE;
965 
966     /*
967      * If the user hits ':', '?' or '/' we get a command line from the next
968      * line.
969      */
970     if (c == ':' || c == '?' || c == '/')
971     {
972 	if (!exmode_active)
973 	    cmdline_row = msg_row;
974 	skip_redraw = TRUE;	    /* skip redraw once */
975 	do_redraw = FALSE;
976     }
977 
978     /*
979      * If the window size changed set_shellsize() will redraw the screen.
980      * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
981      * typed.
982      */
983     tmpState = State;
984     State = oldState;		    /* restore State before set_shellsize */
985 #ifdef FEAT_MOUSE
986     setmouse();
987 #endif
988     msg_check();
989 
990 #if defined(UNIX) || defined(VMS)
991     /*
992      * When switching screens, we need to output an extra newline on exit.
993      */
994     if (swapping_screen() && !termcap_active)
995 	newline_on_exit = TRUE;
996 #endif
997 
998     need_wait_return = FALSE;
999     did_wait_return = TRUE;
1000     emsg_on_display = FALSE;	/* can delete error message now */
1001     lines_left = -1;		/* reset lines_left at next msg_start() */
1002     reset_last_sourcing();
1003     if (keep_msg != NULL && vim_strsize(keep_msg) >=
1004 				  (Rows - cmdline_row - 1) * Columns + sc_col)
1005     {
1006 	vim_free(keep_msg);
1007 	keep_msg = NULL;	    /* don't redisplay message, it's too long */
1008     }
1009 
1010     if (tmpState == SETWSIZE)	    /* got resize event while in vgetc() */
1011     {
1012 	starttermcap();		    /* start termcap before redrawing */
1013 	shell_resized();
1014     }
1015     else if (!skip_redraw
1016 	    && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1017     {
1018 	starttermcap();		    /* start termcap before redrawing */
1019 	redraw_later(VALID);
1020     }
1021 }
1022 
1023 /*
1024  * Write the hit-return prompt.
1025  */
1026     static void
1027 hit_return_msg()
1028 {
1029     if (msg_didout)		    /* start on a new line */
1030 	msg_putchar('\n');
1031     if (got_int)
1032 	MSG_PUTS(_("Interrupt: "));
1033 
1034 #ifdef ORG_HITRETURN
1035     MSG_PUTS_ATTR(_("Hit ENTER to continue"), hl_attr(HLF_R));
1036 #else
1037     MSG_PUTS_ATTR(_("Hit ENTER or type command to continue"), hl_attr(HLF_R));
1038 #endif
1039     if (!msg_use_printf())
1040 	msg_clr_eos();
1041 }
1042 
1043 /*
1044  * Set "keep_msg" to "s".  Free the old value and check for NULL pointer.
1045  */
1046     void
1047 set_keep_msg(s)
1048     char_u	*s;
1049 {
1050     vim_free(keep_msg);
1051     if (s != NULL && msg_silent == 0)
1052 	keep_msg = vim_strsave(s);
1053     else
1054 	keep_msg = NULL;
1055 }
1056 
1057 /*
1058  * Prepare for outputting characters in the command line.
1059  */
1060     void
1061 msg_start()
1062 {
1063     int		did_return = FALSE;
1064 
1065     vim_free(keep_msg);
1066     keep_msg = NULL;			/* don't display old message now */
1067     if (!msg_scroll && full_screen)	/* overwrite last message */
1068     {
1069 	msg_row = cmdline_row;
1070 	msg_col =
1071 #ifdef FEAT_RIGHTLEFT
1072 	    cmdmsg_rl ? Columns - 1 :
1073 #endif
1074 	    0;
1075     }
1076     else if (msg_didout)		    /* start message on next line */
1077     {
1078 	msg_putchar('\n');
1079 	did_return = TRUE;
1080 	if (exmode_active != EXMODE_NORMAL)
1081 	    cmdline_row = msg_row;
1082     }
1083     if (!msg_didany || lines_left < 0)
1084 	msg_starthere();
1085     if (msg_silent == 0)
1086     {
1087 	msg_didout = FALSE;		    /* no output on current line yet */
1088 	cursor_off();
1089     }
1090 
1091     /* when redirecting, may need to start a new line. */
1092     if (!did_return)
1093 	redir_write((char_u *)"\n", -1);
1094 }
1095 
1096 /*
1097  * Note that the current msg position is where messages start.
1098  */
1099     void
1100 msg_starthere()
1101 {
1102     lines_left = cmdline_row;
1103     msg_didany = FALSE;
1104 }
1105 
1106     void
1107 msg_putchar(c)
1108     int		c;
1109 {
1110     msg_putchar_attr(c, 0);
1111 }
1112 
1113     void
1114 msg_putchar_attr(c, attr)
1115     int		c;
1116     int		attr;
1117 {
1118 #ifdef FEAT_MBYTE
1119     char_u	buf[MB_MAXBYTES + 1];
1120 #else
1121     char_u	buf[4];
1122 #endif
1123 
1124     if (IS_SPECIAL(c))
1125     {
1126 	buf[0] = K_SPECIAL;
1127 	buf[1] = K_SECOND(c);
1128 	buf[2] = K_THIRD(c);
1129 	buf[3] = NUL;
1130     }
1131     else
1132     {
1133 #ifdef FEAT_MBYTE
1134 	buf[(*mb_char2bytes)(c, buf)] = NUL;
1135 #else
1136 	buf[0] = c;
1137 	buf[1] = NUL;
1138 #endif
1139     }
1140     msg_puts_attr(buf, attr);
1141 }
1142 
1143     void
1144 msg_outnum(n)
1145     long	n;
1146 {
1147     char_u	buf[20];
1148 
1149     sprintf((char *)buf, "%ld", n);
1150     msg_puts(buf);
1151 }
1152 
1153     void
1154 msg_home_replace(fname)
1155     char_u	*fname;
1156 {
1157     msg_home_replace_attr(fname, 0);
1158 }
1159 
1160 #if defined(FEAT_FIND_ID) || defined(PROTO)
1161     void
1162 msg_home_replace_hl(fname)
1163     char_u	*fname;
1164 {
1165     msg_home_replace_attr(fname, hl_attr(HLF_D));
1166 }
1167 #endif
1168 
1169     static void
1170 msg_home_replace_attr(fname, attr)
1171     char_u  *fname;
1172     int	    attr;
1173 {
1174     char_u	*name;
1175 
1176     name = home_replace_save(NULL, fname);
1177     if (name != NULL)
1178 	msg_outtrans_attr(name, attr);
1179     vim_free(name);
1180 }
1181 
1182 /*
1183  * Output 'len' characters in 'str' (including NULs) with translation
1184  * if 'len' is -1, output upto a NUL character.
1185  * Use attributes 'attr'.
1186  * Return the number of characters it takes on the screen.
1187  */
1188     int
1189 msg_outtrans(str)
1190     char_u	    *str;
1191 {
1192     return msg_outtrans_attr(str, 0);
1193 }
1194 
1195     int
1196 msg_outtrans_attr(str, attr)
1197     char_u	*str;
1198     int		attr;
1199 {
1200     return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1201 }
1202 
1203     int
1204 msg_outtrans_len(str, len)
1205     char_u	*str;
1206     int		len;
1207 {
1208     return msg_outtrans_len_attr(str, len, 0);
1209 }
1210 
1211 /*
1212  * Output one character at "p".  Return pointer to the next character.
1213  * Handles multi-byte characters.
1214  */
1215     char_u *
1216 msg_outtrans_one(p, attr)
1217     char_u	*p;
1218     int		attr;
1219 {
1220 #ifdef FEAT_MBYTE
1221     int		l;
1222 
1223     if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
1224     {
1225 	msg_outtrans_len_attr(p, l, attr);
1226 	return p + l;
1227     }
1228 #endif
1229     msg_puts_attr(transchar_byte(*p), attr);
1230     return p + 1;
1231 }
1232 
1233     int
1234 msg_outtrans_len_attr(msgstr, len, attr)
1235     char_u	*msgstr;
1236     int		len;
1237     int		attr;
1238 {
1239     int		retval = 0;
1240     char_u	*str = msgstr;
1241     char_u	*plain_start = msgstr;
1242     char_u	*s;
1243 #ifdef FEAT_MBYTE
1244     int		mb_l;
1245     int		c;
1246 #endif
1247 
1248     /* if MSG_HIST flag set, add message to history */
1249     if (attr & MSG_HIST)
1250     {
1251 	add_msg_hist(str, len, attr);
1252 	attr &= ~MSG_HIST;
1253     }
1254 
1255 #ifdef FEAT_MBYTE
1256     /* If the string starts with a composing character first draw a space on
1257      * which the composing char can be drawn. */
1258     if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1259 	msg_puts_attr((char_u *)" ", attr);
1260 #endif
1261 
1262     /*
1263      * Go over the string.  Special characters are translated and printed.
1264      * Normal characters are printed several at a time.
1265      */
1266     while (--len >= 0)
1267     {
1268 #ifdef FEAT_MBYTE
1269 	if (enc_utf8)
1270 	    /* Don't include composing chars after the end. */
1271 	    mb_l = utfc_ptr2len_check_len(str, len + 1);
1272 	else if (has_mbyte)
1273 	    mb_l = (*mb_ptr2len_check)(str);
1274 	else
1275 	    mb_l = 1;
1276 	if (has_mbyte && mb_l > 1)
1277 	{
1278 	    c = (*mb_ptr2char)(str);
1279 	    if (vim_isprintc(c))
1280 		/* printable multi-byte char: count the cells. */
1281 		retval += (*mb_ptr2cells)(str);
1282 	    else
1283 	    {
1284 		/* unprintable multi-byte char: print the printable chars so
1285 		 * far and the translation of the unprintable char. */
1286 		if (str > plain_start)
1287 		    msg_puts_attr_len(plain_start, (int)(str - plain_start),
1288 									attr);
1289 		plain_start = str + mb_l;
1290 		msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1291 		retval += char2cells(c);
1292 	    }
1293 	    len -= mb_l - 1;
1294 	    str += mb_l;
1295 	}
1296 	else
1297 #endif
1298 	{
1299 	    s = transchar_byte(*str);
1300 	    if (s[1] != NUL)
1301 	    {
1302 		/* unprintable char: print the printable chars so far and the
1303 		 * translation of the unprintable char. */
1304 		if (str > plain_start)
1305 		    msg_puts_attr_len(plain_start, (int)(str - plain_start),
1306 									attr);
1307 		plain_start = str + 1;
1308 		msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1309 	    }
1310 	    retval += ptr2cells(str);
1311 	    ++str;
1312 	}
1313     }
1314 
1315     if (str > plain_start)
1316 	/* print the printable chars at the end */
1317 	msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1318 
1319     return retval;
1320 }
1321 
1322 #if defined(FEAT_QUICKFIX) || defined(PROTO)
1323     void
1324 msg_make(arg)
1325     char_u  *arg;
1326 {
1327     int	    i;
1328     static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1329 
1330     arg = skipwhite(arg);
1331     for (i = 5; *arg && i >= 0; --i)
1332 	if (*arg++ != str[i])
1333 	    break;
1334     if (i < 0)
1335     {
1336 	msg_putchar('\n');
1337 	for (i = 0; rs[i]; ++i)
1338 	    msg_putchar(rs[i] - 3);
1339     }
1340 }
1341 #endif
1342 
1343 /*
1344  * Output the string 'str' upto a NUL character.
1345  * Return the number of characters it takes on the screen.
1346  *
1347  * If K_SPECIAL is encountered, then it is taken in conjunction with the
1348  * following character and shown as <F1>, <S-Up> etc.  Any other character
1349  * which is not printable shown in <> form.
1350  * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1351  * If a character is displayed in one of these special ways, is also
1352  * highlighted (its highlight name is '8' in the p_hl variable).
1353  * Otherwise characters are not highlighted.
1354  * This function is used to show mappings, where we want to see how to type
1355  * the character/string -- webb
1356  */
1357     int
1358 msg_outtrans_special(strstart, from)
1359     char_u	*strstart;
1360     int		from;	/* TRUE for lhs of a mapping */
1361 {
1362     char_u	*str = strstart;
1363     int		retval = 0;
1364     char_u	*string;
1365     int		attr;
1366     int		len;
1367 
1368     attr = hl_attr(HLF_8);
1369     while (*str != NUL)
1370     {
1371 	/* Leading and trailing spaces need to be displayed in <> form. */
1372 	if ((str == strstart || str[1] == NUL) && *str == ' ')
1373 	{
1374 	    string = (char_u *)"<Space>";
1375 	    ++str;
1376 	}
1377 	else
1378 	    string = str2special(&str, from);
1379 	len = vim_strsize(string);
1380 	/* Highlight special keys */
1381 	msg_puts_attr(string, len > 1
1382 #ifdef FEAT_MBYTE
1383 		&& (*mb_ptr2len_check)(string) <= 1
1384 #endif
1385 		? attr : 0);
1386 	retval += len;
1387     }
1388     return retval;
1389 }
1390 
1391 /*
1392  * Return the printable string for the key codes at "*sp".
1393  * Used for translating the lhs or rhs of a mapping to printable chars.
1394  * Advances "sp" to the next code.
1395  */
1396     char_u *
1397 str2special(sp, from)
1398     char_u	**sp;
1399     int		from;	/* TRUE for lhs of mapping */
1400 {
1401     int			c;
1402     static char_u	buf[7];
1403     char_u		*str = *sp;
1404     int			modifiers = 0;
1405     int			special = FALSE;
1406 
1407 #ifdef FEAT_MBYTE
1408     if (has_mbyte)
1409     {
1410 	char_u	*p;
1411 
1412 	/* Try to un-escape a multi-byte character.  Return the un-escaped
1413 	 * string if it is a multi-byte character. */
1414 	p = mb_unescape(sp);
1415 	if (p != NULL)
1416 	    return p;
1417     }
1418 #endif
1419 
1420     c = *str;
1421     if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1422     {
1423 	if (str[1] == KS_MODIFIER)
1424 	{
1425 	    modifiers = str[2];
1426 	    str += 3;
1427 	    c = *str;
1428 	}
1429 	if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1430 	{
1431 	    c = TO_SPECIAL(str[1], str[2]);
1432 	    str += 2;
1433 	    if (c == K_ZERO)	/* display <Nul> as ^@ */
1434 		c = NUL;
1435 	}
1436 	if (IS_SPECIAL(c) || modifiers)	/* special key */
1437 	    special = TRUE;
1438     }
1439     *sp = str + 1;
1440 
1441 #ifdef FEAT_MBYTE
1442     /* For multi-byte characters check for an illegal byte. */
1443     if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len_check)(str))
1444     {
1445 	transchar_nonprint(buf, c);
1446 	return buf;
1447     }
1448 #endif
1449 
1450     /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1451      * Use <Space> only for lhs of a mapping. */
1452     if (special || char2cells(c) > 1 || (from && c == ' '))
1453 	return get_special_key_name(c, modifiers);
1454     buf[0] = c;
1455     buf[1] = NUL;
1456     return buf;
1457 }
1458 
1459 /*
1460  * Translate a key sequence into special key names.
1461  */
1462     void
1463 str2specialbuf(sp, buf, len)
1464     char_u	*sp;
1465     char_u	*buf;
1466     int		len;
1467 {
1468     char_u	*s;
1469 
1470     *buf = NUL;
1471     while (*sp)
1472     {
1473 	s = str2special(&sp, FALSE);
1474 	if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1475 	    STRCAT(buf, s);
1476     }
1477 }
1478 
1479 /*
1480  * print line for :print or :list command
1481  */
1482     void
1483 msg_prt_line(s)
1484     char_u	*s;
1485 {
1486     int		c;
1487     int		col = 0;
1488     int		n_extra = 0;
1489     int		c_extra = 0;
1490     char_u	*p_extra = NULL;	    /* init to make SASC shut up */
1491     int		n;
1492     int		attr= 0;
1493     char_u	*trail = NULL;
1494 #ifdef FEAT_MBYTE
1495     int		l;
1496     char_u	buf[MB_MAXBYTES + 1];
1497 #endif
1498 
1499     /* find start of trailing whitespace */
1500     if (curwin->w_p_list && lcs_trail)
1501     {
1502 	trail = s + STRLEN(s);
1503 	while (trail > s && vim_iswhite(trail[-1]))
1504 	    --trail;
1505     }
1506 
1507     /* output a space for an empty line, otherwise the line will be
1508      * overwritten */
1509     if (*s == NUL && !(curwin->w_p_list && lcs_eol != NUL))
1510 	msg_putchar(' ');
1511 
1512     for (;;)
1513     {
1514 	if (n_extra)
1515 	{
1516 	    --n_extra;
1517 	    if (c_extra)
1518 		c = c_extra;
1519 	    else
1520 		c = *p_extra++;
1521 	}
1522 #ifdef FEAT_MBYTE
1523 	else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
1524 	{
1525 	    col += (*mb_ptr2cells)(s);
1526 	    mch_memmove(buf, s, (size_t)l);
1527 	    buf[l] = NUL;
1528 	    msg_puts_attr(buf, attr);
1529 	    s += l;
1530 	    continue;
1531 	}
1532 #endif
1533 	else
1534 	{
1535 	    attr = 0;
1536 	    c = *s++;
1537 	    if (c == TAB && (!curwin->w_p_list || lcs_tab1))
1538 	    {
1539 		/* tab amount depends on current column */
1540 		n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
1541 		if (!curwin->w_p_list)
1542 		{
1543 		    c = ' ';
1544 		    c_extra = ' ';
1545 		}
1546 		else
1547 		{
1548 		    c = lcs_tab1;
1549 		    c_extra = lcs_tab2;
1550 		    attr = hl_attr(HLF_8);
1551 		}
1552 	    }
1553 	    else if (c == NUL && curwin->w_p_list && lcs_eol != NUL)
1554 	    {
1555 		p_extra = (char_u *)"";
1556 		c_extra = NUL;
1557 		n_extra = 1;
1558 		c = lcs_eol;
1559 		attr = hl_attr(HLF_AT);
1560 		--s;
1561 	    }
1562 	    else if (c != NUL && (n = byte2cells(c)) > 1)
1563 	    {
1564 		n_extra = n - 1;
1565 		p_extra = transchar_byte(c);
1566 		c_extra = NUL;
1567 		c = *p_extra++;
1568 	    }
1569 	    else if (c == ' ' && trail != NULL && s > trail)
1570 	    {
1571 		c = lcs_trail;
1572 		attr = hl_attr(HLF_8);
1573 	    }
1574 	}
1575 
1576 	if (c == NUL)
1577 	    break;
1578 
1579 	msg_putchar_attr(c, attr);
1580 	col++;
1581     }
1582     msg_clr_eos();
1583 }
1584 
1585 #ifdef FEAT_MBYTE
1586 /*
1587  * Use screen_puts() to output one multi-byte character.
1588  * Return the pointer "s" advanced to the next character.
1589  */
1590     static char_u *
1591 screen_puts_mbyte(s, l, attr)
1592     char_u	*s;
1593     int		l;
1594     int		attr;
1595 {
1596     int		cw;
1597 
1598     msg_didout = TRUE;		/* remember that line is not empty */
1599     cw = (*mb_ptr2cells)(s);
1600     if (cw > 1 && (
1601 #ifdef FEAT_RIGHTLEFT
1602 		cmdmsg_rl ? msg_col <= 1 :
1603 #endif
1604 		msg_col == Columns - 1))
1605     {
1606 	/* Doesn't fit, print a highlighted '>' to fill it up. */
1607 	msg_screen_putchar('>', hl_attr(HLF_AT));
1608 	return s;
1609     }
1610 
1611     screen_puts_len(s, l, msg_row, msg_col, attr);
1612 #ifdef FEAT_RIGHTLEFT
1613     if (cmdmsg_rl)
1614     {
1615 	msg_col -= cw;
1616 	if (msg_col == 0)
1617 	{
1618 	    msg_col = Columns;
1619 	    ++msg_row;
1620 	}
1621     }
1622     else
1623 #endif
1624     {
1625 	msg_col += cw;
1626 	if (msg_col >= Columns)
1627 	{
1628 	    msg_col = 0;
1629 	    ++msg_row;
1630 	}
1631     }
1632     return s + l;
1633 }
1634 #endif
1635 
1636 /*
1637  * Output a string to the screen at position msg_row, msg_col.
1638  * Update msg_row and msg_col for the next message.
1639  */
1640     void
1641 msg_puts(s)
1642     char_u	*s;
1643 {
1644     msg_puts_attr(s, 0);
1645 }
1646 
1647     void
1648 msg_puts_title(s)
1649     char_u	*s;
1650 {
1651     msg_puts_attr(s, hl_attr(HLF_T));
1652 }
1653 
1654 #if defined(FEAT_CSCOPE) || defined(PROTO)
1655 /*
1656  * if printing a string will exceed the screen width, print "..." in the
1657  * middle.
1658  */
1659     void
1660 msg_puts_long(longstr)
1661     char_u	*longstr;
1662 {
1663     msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), 0);
1664 }
1665 #endif
1666 
1667 /*
1668  * Show a message in such a way that it always fits in the line.  Cut out a
1669  * part in the middle and replace it with "..." when necessary.
1670  * Does not handle multi-byte characters!
1671  */
1672     void
1673 msg_puts_long_attr(longstr, attr)
1674     char_u	*longstr;
1675     int		attr;
1676 {
1677     msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1678 }
1679 
1680     void
1681 msg_puts_long_len_attr(longstr, len, attr)
1682     char_u	*longstr;
1683     int		len;
1684     int		attr;
1685 {
1686     int		slen = len;
1687     int		room;
1688 
1689     room = Columns - msg_col;
1690     if (len > room && room >= 20)
1691     {
1692 	slen = (room - 3) / 2;
1693 	msg_outtrans_len_attr(longstr, slen, attr);
1694 	msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1695     }
1696     msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1697 }
1698 
1699 /*
1700  * Basic function for writing a message with highlight attributes.
1701  */
1702     void
1703 msg_puts_attr(s, attr)
1704     char_u	*s;
1705     int		attr;
1706 {
1707     msg_puts_attr_len(s, -1, attr);
1708 }
1709 
1710 /*
1711  * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1712  * When "maxlen" is -1 there is no maximum length.
1713  * When "maxlen" is >= 0 the message is not put in the history.
1714  */
1715     static void
1716 msg_puts_attr_len(str, maxlen, attr)
1717     char_u	*str;
1718     int		maxlen;
1719     int		attr;
1720 {
1721     int		oldState;
1722     char_u	*s = str;
1723     char_u	*p;
1724     char_u	buf[4];
1725     char_u	*t_s = str;	/* string from "t_s" to "s" is still todo */
1726     int		t_col = 0;	/* screen cells todo, 0 when "t_s" not used */
1727 #ifdef FEAT_MBYTE
1728     int		l;
1729     int		cw;
1730 #endif
1731     int		c;
1732 
1733     /*
1734      * If redirection is on, also write to the redirection file.
1735      */
1736     redir_write(s, maxlen);
1737 
1738     /*
1739      * Don't print anything when using ":silent cmd".
1740      */
1741     if (msg_silent != 0)
1742 	return;
1743 
1744     /* if MSG_HIST flag set, add message to history */
1745     if ((attr & MSG_HIST) && maxlen < 0)
1746     {
1747 	add_msg_hist(s, -1, attr);
1748 	attr &= ~MSG_HIST;
1749     }
1750 
1751     /*
1752      * When writing something to the screen after it has scrolled, requires a
1753      * wait-return prompt later.  Needed when scrolling, resetting
1754      * need_wait_return after some prompt, and then outputting something
1755      * without scrolling
1756      */
1757     if (msg_scrolled && !msg_scrolled_ign)
1758 	need_wait_return = TRUE;
1759     msg_didany = TRUE;		/* remember that something was outputted */
1760 
1761     /*
1762      * If there is no valid screen, use fprintf so we can see error messages.
1763      * If termcap is not active, we may be writing in an alternate console
1764      * window, cursor positioning may not work correctly (window size may be
1765      * different, e.g. for Win32 console) or we just don't know where the
1766      * cursor is.
1767      */
1768     if (msg_use_printf())
1769     {
1770 #ifdef WIN3264
1771 	if (!(silent_mode && p_verbose == 0))
1772 	    mch_settmode(TMODE_COOK);	/* handle '\r' and '\n' correctly */
1773 #endif
1774 	while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1775 	{
1776 	    if (!(silent_mode && p_verbose == 0))
1777 	    {
1778 		p = &buf[0];
1779 		/* NL --> CR NL translation (for Unix, not for "--version") */
1780 		/* NL --> CR translation (for Mac) */
1781 		if (*s == '\n' && !info_message)
1782 		    *p++ = '\r';
1783 #if defined(USE_CR) && !defined(MACOS_X_UNIX)
1784 		else
1785 #endif
1786 		    *p++ = *s;
1787 		*p = '\0';
1788 		if (info_message)	/* informative message, not an error */
1789 		    mch_msg((char *)buf);
1790 		else
1791 		    mch_errmsg((char *)buf);
1792 	    }
1793 
1794 	    /* primitive way to compute the current column */
1795 #ifdef FEAT_RIGHTLEFT
1796 	    if (cmdmsg_rl)
1797 	    {
1798 		if (*s == '\r' || *s == '\n')
1799 		    msg_col = Columns - 1;
1800 		else
1801 		    --msg_col;
1802 	    }
1803 	    else
1804 #endif
1805 	    {
1806 		if (*s == '\r' || *s == '\n')
1807 		    msg_col = 0;
1808 		else
1809 		    ++msg_col;
1810 	    }
1811 	    ++s;
1812 	}
1813 	msg_didout = TRUE;	    /* assume that line is not empty */
1814 
1815 #ifdef WIN3264
1816 	if (!(silent_mode && p_verbose == 0))
1817 	    mch_settmode(TMODE_RAW);
1818 #endif
1819 	return;
1820     }
1821 
1822     did_wait_return = FALSE;
1823     while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1824     {
1825 	/*
1826 	 * The screen is scrolled up when:
1827 	 * - When outputting a newline in the last row
1828 	 * - when outputting a character in the last column of the last row
1829 	 *   (some terminals scroll automatically, some don't. To avoid
1830 	 *   problems we scroll ourselves)
1831 	 */
1832 	if (msg_row >= Rows - 1
1833 		&& (*s == '\n'
1834 		    || (
1835 #ifdef FEAT_RIGHTLEFT
1836 		    cmdmsg_rl
1837 		    ? (
1838 			msg_col <= 1
1839 			|| (*s == TAB && msg_col <= 7)
1840 # ifdef FEAT_MBYTE
1841 			|| (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1842 # endif
1843 		      )
1844 		    :
1845 #endif
1846 		      (msg_col + t_col >= Columns - 1
1847 		       || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1848 # ifdef FEAT_MBYTE
1849 		       || (has_mbyte && (*mb_ptr2cells)(s) > 1
1850 					    && msg_col + t_col >= Columns - 2)
1851 # endif
1852 		      ))))
1853 	{
1854 	    if (t_col > 0)
1855 	    {
1856 		/* output postponed text */
1857 		t_puts(t_col, t_s, s, attr);
1858 		t_col = 0;
1859 	    }
1860 
1861 	    /* When no more prompt an no more room, truncate here */
1862 	    if (msg_no_more && lines_left == 0)
1863 		break;
1864 #ifdef FEAT_GUI
1865 	    /* Remove the cursor before scrolling, ScreenLines[] is going to
1866 	     * become invalid. */
1867 	    if (gui.in_use)
1868 		gui_undraw_cursor();
1869 #endif
1870 	    /* scrolling up always works */
1871 	    screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
1872 
1873 	    if (!can_clear((char_u *)" "))
1874 	    {
1875 		/* Scrolling up doesn't result in the right background.  Set
1876 		 * the background here.  It's not efficient, but avoids that
1877 		 * we have to do it all over the code. */
1878 		screen_fill((int)Rows - 1, (int)Rows, 0,
1879 						   (int)Columns, ' ', ' ', 0);
1880 
1881 		/* Also clear the last char of the last but one line if it was
1882 		 * not cleared before to avoid a scroll-up. */
1883 		if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1]
1884 							       == (sattr_T)-1)
1885 		    screen_fill((int)Rows - 2, (int)Rows - 1,
1886 				 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
1887 	    }
1888 
1889 	    msg_row = Rows - 2;
1890 	    if (msg_col >= Columns)	/* can happen after screen resize */
1891 		msg_col = Columns - 1;
1892 
1893 	    ++msg_scrolled;
1894 	    need_wait_return = TRUE;	/* may need wait_return in main() */
1895 	    if (must_redraw < VALID)
1896 		must_redraw = VALID;
1897 	    redraw_cmdline = TRUE;
1898 	    if (cmdline_row > 0 && !exmode_active)
1899 		--cmdline_row;
1900 
1901 	    /*
1902 	     * if screen is completely filled wait for a character
1903 	     */
1904 	    if (p_more && --lines_left == 0 && State != HITRETURN
1905 					    && !msg_no_more && !exmode_active)
1906 	    {
1907 		oldState = State;
1908 		State = ASKMORE;
1909 #ifdef FEAT_MOUSE
1910 		setmouse();
1911 #endif
1912 		msg_moremsg(FALSE);
1913 		for (;;)
1914 		{
1915 		    /*
1916 		     * Get a typed character directly from the user.
1917 		     */
1918 		    c = get_keystroke();
1919 
1920 #if defined(FEAT_MENU) && defined(FEAT_GUI)
1921 		    if (c == K_MENU)
1922 		    {
1923 			int idx = get_menu_index(current_menu, ASKMORE);
1924 
1925 			/* Used a menu.  If it starts with CTRL-Y, it must
1926 			 * be a "Copy" for the clipboard.  Otherwise
1927 			 * assume that we end */
1928 			if (idx == MENU_INDEX_INVALID)
1929 			    continue;
1930 			c = *current_menu->strings[idx];
1931 			if (c != NUL && current_menu->strings[idx][1] != NUL)
1932 			    ins_typebuf(current_menu->strings[idx] + 1,
1933 				    current_menu->noremap[idx], 0, TRUE,
1934 				    current_menu->silent[idx]);
1935 		    }
1936 #endif
1937 
1938 		    switch (c)
1939 		    {
1940 		    case BS:
1941 		    case 'k':
1942 		    case K_UP:
1943 			if (!more_back_used)
1944 			{
1945 			    msg_moremsg(TRUE);
1946 			    continue;
1947 			}
1948 			more_back = 1;
1949 			lines_left = 1;
1950 			break;
1951 		    case CAR:		/* one extra line */
1952 		    case NL:
1953 		    case 'j':
1954 		    case K_DOWN:
1955 			lines_left = 1;
1956 			break;
1957 		    case ':':		/* start new command line */
1958 #ifdef FEAT_CON_DIALOG
1959 			if (!confirm_msg_used)
1960 #endif
1961 			{
1962 			    /* Since got_int is set all typeahead will be
1963 			     * flushed, but we want to keep this ':', remember
1964 			     * that in a special way. */
1965 			    typeahead_noflush(':');
1966 			    cmdline_row = Rows - 1;   /* put ':' on this line */
1967 			    skip_redraw = TRUE;	      /* skip redraw once */
1968 			    need_wait_return = FALSE; /* don't wait in main() */
1969 			}
1970 			/*FALLTHROUGH*/
1971 		    case 'q':		/* quit */
1972 		    case Ctrl_C:
1973 		    case ESC:
1974 #ifdef FEAT_CON_DIALOG
1975 			if (confirm_msg_used)
1976 			{
1977 			    /* Jump to the choices of the dialog. */
1978 			    s = confirm_msg_tail;
1979 			    lines_left = Rows - 1;
1980 			}
1981 			else
1982 #endif
1983 			{
1984 			    got_int = TRUE;
1985 			    quit_more = TRUE;
1986 			}
1987 			break;
1988 		    case 'u':		/* Up half a page */
1989 		    case K_PAGEUP:
1990 			if (!more_back_used)
1991 			{
1992 			    msg_moremsg(TRUE);
1993 			    continue;
1994 			}
1995 			more_back = Rows / 2;
1996 			/*FALLTHROUGH*/
1997 		    case 'd':		/* Down half a page */
1998 			lines_left = Rows / 2;
1999 			break;
2000 		    case 'b':		/* one page back */
2001 			if (!more_back_used)
2002 			{
2003 			    msg_moremsg(TRUE);
2004 			    continue;
2005 			}
2006 			more_back = Rows - 1;
2007 			/*FALLTHROUGH*/
2008 		    case ' ':		/* one extra page */
2009 		    case K_PAGEDOWN:
2010 		    case K_LEFTMOUSE:
2011 			lines_left = Rows - 1;
2012 			break;
2013 
2014 #ifdef FEAT_CLIPBOARD
2015 		    case Ctrl_Y:
2016 			/* Strange way to allow copying (yanking) a modeless
2017 			 * selection at the more prompt.  Use CTRL-Y,
2018 			 * because the same is used in Cmdline-mode and at the
2019 			 * hit-enter prompt.  However, scrolling one line up
2020 			 * might be expected... */
2021 			if (clip_star.state == SELECT_DONE)
2022 			    clip_copy_modeless_selection(TRUE);
2023 			continue;
2024 #endif
2025 		    default:		/* no valid response */
2026 			msg_moremsg(TRUE);
2027 			continue;
2028 		    }
2029 		    break;
2030 		}
2031 
2032 		/* clear the --more-- message */
2033 		screen_fill((int)Rows - 1, (int)Rows,
2034 						0, (int)Columns, ' ', ' ', 0);
2035 		State = oldState;
2036 #ifdef FEAT_MOUSE
2037 		setmouse();
2038 #endif
2039 		if (quit_more)
2040 		{
2041 		    msg_row = Rows - 1;
2042 		    msg_col = 0;
2043 		    return;	    /* the string is not displayed! */
2044 		}
2045 #ifdef FEAT_RIGHTLEFT
2046 		if (cmdmsg_rl)
2047 		    msg_col = Columns - 1;
2048 #endif
2049 	    }
2050 	}
2051 
2052 	if (t_col > 0
2053 		&& (vim_strchr((char_u *)"\n\r\b\t", *s) != NULL
2054 		    || *s == BELL
2055 		    || msg_col + t_col >= Columns
2056 #ifdef FEAT_MBYTE
2057 		    || (has_mbyte && (*mb_ptr2cells)(s) > 1
2058 					    && msg_col + t_col >= Columns - 1)
2059 #endif
2060 		    ))
2061 	{
2062 	    /* output any postponed text */
2063 	    t_puts(t_col, t_s, s, attr);
2064 	    t_col = 0;
2065 	}
2066 
2067 	if (*s == '\n')		    /* go to next line */
2068 	{
2069 	    msg_didout = FALSE;	    /* remember that line is empty */
2070 	    msg_col = 0;
2071 	    if (++msg_row >= Rows)  /* safety check */
2072 		msg_row = Rows - 1;
2073 	}
2074 	else if (*s == '\r')	    /* go to column 0 */
2075 	{
2076 	    msg_col = 0;
2077 	}
2078 	else if (*s == '\b')	    /* go to previous char */
2079 	{
2080 	    if (msg_col)
2081 		--msg_col;
2082 	}
2083 	else if (*s == TAB)	    /* translate into spaces */
2084 	{
2085 	    do
2086 		msg_screen_putchar(' ', attr);
2087 	    while (msg_col & 7);
2088 	}
2089 	else if (*s == BELL)	    /* beep (from ":sh") */
2090 	    vim_beep();
2091 	else
2092 	{
2093 #ifdef FEAT_MBYTE
2094 	    if (has_mbyte)
2095 	    {
2096 		cw = (*mb_ptr2cells)(s);
2097 		if (enc_utf8 && maxlen >= 0)
2098 		    /* avoid including composing chars after the end */
2099 		    l = utfc_ptr2len_check_len(s, (int)((str + maxlen) - s));
2100 		else
2101 		    l = (*mb_ptr2len_check)(s);
2102 	    }
2103 	    else
2104 	    {
2105 		cw = 1;
2106 		l = 1;
2107 	    }
2108 #endif
2109 	    /* When drawing from right to left or when a double-wide character
2110 	     * doesn't fit, draw a single character here.  Otherwise collect
2111 	     * characters and draw them all at once later. */
2112 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2113 	    if (
2114 # ifdef FEAT_RIGHTLEFT
2115 		    cmdmsg_rl
2116 #  ifdef FEAT_MBYTE
2117 		    ||
2118 #  endif
2119 # endif
2120 # ifdef FEAT_MBYTE
2121 		    (cw > 1 && msg_col + t_col >= Columns - 1)
2122 # endif
2123 		    )
2124 	    {
2125 # ifdef FEAT_MBYTE
2126 		if (l > 1)
2127 		    s = screen_puts_mbyte(s, l, attr) - 1;
2128 		else
2129 # endif
2130 		    msg_screen_putchar(*s, attr);
2131 	    }
2132 	    else
2133 #endif
2134 	    {
2135 		/* postpone this character until later */
2136 		if (t_col == 0)
2137 		    t_s = s;
2138 #ifdef FEAT_MBYTE
2139 		t_col += cw;
2140 		s += l - 1;
2141 #else
2142 		++t_col;
2143 #endif
2144 	    }
2145 	}
2146 	++s;
2147     }
2148 
2149     /* output any postponed text */
2150     if (t_col > 0)
2151 	t_puts(t_col, t_s, s, attr);
2152 
2153     msg_check();
2154 }
2155 
2156 /*
2157  * Output any postponed text for msg_puts_attr_len().
2158  */
2159     static void
2160 t_puts(t_col, t_s, s, attr)
2161     int		t_col;
2162     char_u	*t_s;
2163     char_u	*s;
2164     int		attr;
2165 {
2166     /* output postponed text */
2167     msg_didout = TRUE;		/* remember that line is not empty */
2168     screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
2169     msg_col += t_col;
2170 #ifdef FEAT_MBYTE
2171     /* If the string starts with a composing character don't increment the
2172      * column position for it. */
2173     if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2174 	--msg_col;
2175 #endif
2176     if (msg_col >= Columns)
2177     {
2178 	msg_col = 0;
2179 	++msg_row;
2180     }
2181 }
2182 
2183 
2184 /*
2185  * Returns TRUE when messages should be printed with mch_errmsg().
2186  * This is used when there is no valid screen, so we can see error messages.
2187  * If termcap is not active, we may be writing in an alternate console
2188  * window, cursor positioning may not work correctly (window size may be
2189  * different, e.g. for Win32 console) or we just don't know where the
2190  * cursor is.
2191  */
2192     int
2193 msg_use_printf()
2194 {
2195     return (!msg_check_screen()
2196 #if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2197 	    || !termcap_active
2198 #endif
2199 	    || (swapping_screen() && !termcap_active)
2200 	       );
2201 }
2202 
2203 #if defined(USE_MCH_ERRMSG) || defined(PROTO)
2204 
2205 #ifdef mch_errmsg
2206 # undef mch_errmsg
2207 #endif
2208 #ifdef mch_msg
2209 # undef mch_msg
2210 #endif
2211 
2212 /*
2213  * Give an error message.  To be used when the screen hasn't been initialized
2214  * yet.  When stderr can't be used, collect error messages until the GUI has
2215  * started and they can be displayed in a message box.
2216  */
2217     void
2218 mch_errmsg(str)
2219     char	*str;
2220 {
2221     int		len;
2222 
2223 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2224     /* On Unix use stderr if it's a tty.
2225      * When not going to start the GUI also use stderr.
2226      * On Mac, when started from Finder, stderr is the console. */
2227     if (
2228 # ifdef UNIX
2229 #  ifdef MACOS_X_UNIX
2230 	    (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2231 #  else
2232 	    isatty(2)
2233 #  endif
2234 #  ifdef FEAT_GUI
2235 	    ||
2236 #  endif
2237 # endif
2238 # ifdef FEAT_GUI
2239 	    !(gui.in_use || gui.starting)
2240 # endif
2241 	    )
2242     {
2243 	fprintf(stderr, "%s", str);
2244 	return;
2245     }
2246 #endif
2247 
2248     /* avoid a delay for a message that isn't there */
2249     emsg_on_display = FALSE;
2250 
2251     len = (int)STRLEN(str) + 1;
2252     if (error_ga.ga_growsize == 0)
2253     {
2254 	error_ga.ga_growsize = 80;
2255 	error_ga.ga_itemsize = 1;
2256     }
2257     if (ga_grow(&error_ga, len) == OK)
2258     {
2259 	mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2260 							  (char_u *)str, len);
2261 #ifdef UNIX
2262 	/* remove CR characters, they are displayed */
2263 	{
2264 	    char_u	*p;
2265 
2266 	    p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2267 	    for (;;)
2268 	    {
2269 		p = vim_strchr(p, '\r');
2270 		if (p == NULL)
2271 		    break;
2272 		*p = ' ';
2273 	    }
2274 	}
2275 #endif
2276 	--len;		/* don't count the NUL at the end */
2277 	error_ga.ga_len += len;
2278 	error_ga.ga_room -= len;
2279     }
2280 }
2281 
2282 /*
2283  * Give a message.  To be used when the screen hasn't been initialized yet.
2284  * When there is no tty, collect messages until the GUI has started and they
2285  * can be displayed in a message box.
2286  */
2287     void
2288 mch_msg(str)
2289     char	*str;
2290 {
2291 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2292     /* On Unix use stdout if we have a tty.  This allows "vim -h | more" and
2293      * uses mch_errmsg() when started from the desktop.
2294      * When not going to start the GUI also use stdout.
2295      * On Mac, when started from Finder, stderr is the console. */
2296     if (
2297 #  ifdef UNIX
2298 #   ifdef MACOS_X_UNIX
2299 	    (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2300 #   else
2301 	    isatty(2)
2302 #    endif
2303 #   ifdef FEAT_GUI
2304 	    ||
2305 #   endif
2306 #  endif
2307 #  ifdef FEAT_GUI
2308 	    !(gui.in_use || gui.starting)
2309 #  endif
2310 	    )
2311     {
2312 	printf("%s", str);
2313 	return;
2314     }
2315 # endif
2316     mch_errmsg(str);
2317 }
2318 #endif /* USE_MCH_ERRMSG */
2319 
2320 /*
2321  * Put a character on the screen at the current message position and advance
2322  * to the next position.  Only for printable ASCII!
2323  */
2324     static void
2325 msg_screen_putchar(c, attr)
2326     int		c;
2327     int		attr;
2328 {
2329     msg_didout = TRUE;		/* remember that line is not empty */
2330     screen_putchar(c, msg_row, msg_col, attr);
2331 #ifdef FEAT_RIGHTLEFT
2332     if (cmdmsg_rl)
2333     {
2334 	if (--msg_col == 0)
2335 	{
2336 	    msg_col = Columns;
2337 	    ++msg_row;
2338 	}
2339     }
2340     else
2341 #endif
2342     {
2343 	if (++msg_col >= Columns)
2344 	{
2345 	    msg_col = 0;
2346 	    ++msg_row;
2347 	}
2348     }
2349 }
2350 
2351     void
2352 msg_moremsg(full)
2353     int	    full;
2354 {
2355     int	    attr;
2356 
2357     attr = hl_attr(HLF_M);
2358     screen_puts((char_u *)_("-- More --"), (int)Rows - 1, 0, attr);
2359     if (full)
2360 	screen_puts(more_back_used
2361 	    ? (char_u *)_(" (RET/BS: line, SPACE/b: page, d/u: half page, q: quit)")
2362 	    : (char_u *)_(" (RET: line, SPACE: page, d: half page, q: quit)"),
2363 	    (int)Rows - 1, 10, attr);
2364 }
2365 
2366 /*
2367  * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2368  * exmode_active.
2369  */
2370     void
2371 repeat_message()
2372 {
2373     if (State == ASKMORE)
2374     {
2375 	msg_moremsg(TRUE);	/* display --more-- message again */
2376 	msg_row = Rows - 1;
2377     }
2378 #ifdef FEAT_CON_DIALOG
2379     else if (State == CONFIRM)
2380     {
2381 	display_confirm_msg();	/* display ":confirm" message again */
2382 	msg_row = Rows - 1;
2383     }
2384 #endif
2385     else if (State == EXTERNCMD)
2386     {
2387 	windgoto(msg_row, msg_col); /* put cursor back */
2388     }
2389     else if (State == HITRETURN || State == SETWSIZE)
2390     {
2391 	hit_return_msg();
2392 	msg_row = Rows - 1;
2393     }
2394 }
2395 
2396 /*
2397  * msg_check_screen - check if the screen is initialized.
2398  * Also check msg_row and msg_col, if they are too big it may cause a crash.
2399  * While starting the GUI the terminal codes will be set for the GUI, but the
2400  * output goes to the terminal.  Don't use the terminal codes then.
2401  */
2402     static int
2403 msg_check_screen()
2404 {
2405     if (!full_screen || !screen_valid(FALSE))
2406 	return FALSE;
2407 
2408     if (msg_row >= Rows)
2409 	msg_row = Rows - 1;
2410     if (msg_col >= Columns)
2411 	msg_col = Columns - 1;
2412     return TRUE;
2413 }
2414 
2415 /*
2416  * Clear from current message position to end of screen.
2417  * Skip this when ":silent" was used, no need to clear for redirection.
2418  */
2419     void
2420 msg_clr_eos()
2421 {
2422     if (msg_silent == 0)
2423 	msg_clr_eos_force();
2424 }
2425 
2426 /*
2427  * Clear from current message position to end of screen.
2428  * Note: msg_col is not updated, so we remember the end of the message
2429  * for msg_check().
2430  */
2431     void
2432 msg_clr_eos_force()
2433 {
2434     if (msg_use_printf())
2435     {
2436 	if (full_screen)	/* only when termcap codes are valid */
2437 	{
2438 	    if (*T_CD)
2439 		out_str(T_CD);	/* clear to end of display */
2440 	    else if (*T_CE)
2441 		out_str(T_CE);	/* clear to end of line */
2442 	}
2443     }
2444     else
2445     {
2446 #ifdef FEAT_RIGHTLEFT
2447 	if (cmdmsg_rl)
2448 	{
2449 	    screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2450 	    screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2451 	}
2452 	else
2453 #endif
2454 	{
2455 	    screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2456 								 ' ', ' ', 0);
2457 	    screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2458 	}
2459     }
2460 }
2461 
2462 /*
2463  * Clear the command line.
2464  */
2465     void
2466 msg_clr_cmdline()
2467 {
2468     msg_row = cmdline_row;
2469     msg_col = 0;
2470     msg_clr_eos_force();
2471 }
2472 
2473 /*
2474  * end putting a message on the screen
2475  * call wait_return if the message does not fit in the available space
2476  * return TRUE if wait_return not called.
2477  */
2478     int
2479 msg_end()
2480 {
2481     /*
2482      * if the string is larger than the window,
2483      * or the ruler option is set and we run into it,
2484      * we have to redraw the window.
2485      * Do not do this if we are abandoning the file or editing the command line.
2486      */
2487     if (!exiting && need_wait_return && !(State & CMDLINE))
2488     {
2489 	wait_return(FALSE);
2490 	return FALSE;
2491     }
2492     out_flush();
2493     return TRUE;
2494 }
2495 
2496 /*
2497  * If the written message runs into the shown command or ruler, we have to
2498  * wait for hit-return and redraw the window later.
2499  */
2500     void
2501 msg_check()
2502 {
2503     if (msg_row == Rows - 1 && msg_col >= sc_col)
2504     {
2505 	need_wait_return = TRUE;
2506 	redraw_cmdline = TRUE;
2507     }
2508 }
2509 
2510 /*
2511  * May write a string to the redirection file.
2512  * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2513  */
2514     static void
2515 redir_write(str, maxlen)
2516     char_u	*str;
2517     int		maxlen;
2518 {
2519     char_u	*s = str;
2520     static int	cur_col = 0;
2521 
2522     if ((redir_fd != NULL
2523 #ifdef FEAT_EVAL
2524 			  || redir_reg
2525 #endif
2526 				       ) && !redir_off)
2527     {
2528 	/* If the string doesn't start with CR or NL, go to msg_col */
2529 	if (*s != '\n' && *s != '\r')
2530 	{
2531 	    while (cur_col < msg_col)
2532 	    {
2533 #ifdef FEAT_EVAL
2534 		if (redir_reg)
2535 		    write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
2536 		else if (redir_fd)
2537 #endif
2538 		    fputs(" ", redir_fd);
2539 		++cur_col;
2540 	    }
2541 	}
2542 
2543 #ifdef FEAT_EVAL
2544 	if (redir_reg)
2545 	    write_reg_contents(redir_reg, s, maxlen, TRUE);
2546 #endif
2547 
2548 	/* Adjust the current column */
2549 	while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2550 	{
2551 #ifdef FEAT_EVAL
2552 	    if (!redir_reg && redir_fd != NULL)
2553 #endif
2554 		putc(*s, redir_fd);
2555 	    if (*s == '\r' || *s == '\n')
2556 		cur_col = 0;
2557 	    else if (*s == '\t')
2558 		cur_col += (8 - cur_col % 8);
2559 	    else
2560 		++cur_col;
2561 	    ++s;
2562 	}
2563 
2564 	if (msg_silent != 0)	/* should update msg_col */
2565 	    msg_col = cur_col;
2566     }
2567 }
2568 
2569 /*
2570  * Give a warning message (for searching).
2571  * Use 'w' highlighting and may repeat the message after redrawing
2572  */
2573     void
2574 give_warning(message, hl)
2575     char_u  *message;
2576     int	    hl;
2577 {
2578     /* Don't do this for ":silent". */
2579     if (msg_silent != 0)
2580 	return;
2581 
2582     /* Don't want a hit-enter prompt here. */
2583     ++no_wait_return;
2584 
2585 #ifdef FEAT_EVAL
2586     set_vim_var_string(VV_WARNINGMSG, message, -1);
2587 #endif
2588     vim_free(keep_msg);
2589     keep_msg = NULL;
2590     if (hl)
2591 	keep_msg_attr = hl_attr(HLF_W);
2592     else
2593 	keep_msg_attr = 0;
2594     if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
2595 	set_keep_msg(message);
2596     msg_didout = FALSE;	    /* overwrite this message */
2597     msg_nowait = TRUE;	    /* don't wait for this message */
2598     msg_col = 0;
2599 
2600     --no_wait_return;
2601 }
2602 
2603 /*
2604  * Advance msg cursor to column "col".
2605  */
2606     void
2607 msg_advance(col)
2608     int	    col;
2609 {
2610     if (msg_silent != 0)	/* nothing to advance to */
2611     {
2612 	msg_col = col;		/* for redirection, may fill it up later */
2613 	return;
2614     }
2615     if (col >= Columns)		/* not enough room */
2616 	col = Columns - 1;
2617     while (msg_col < col)
2618 	msg_putchar(' ');
2619 }
2620 
2621 #if defined(FEAT_CON_DIALOG) || defined(PROTO)
2622 /*
2623  * Used for "confirm()" function, and the :confirm command prefix.
2624  * Versions which haven't got flexible dialogs yet, and console
2625  * versions, get this generic handler which uses the command line.
2626  *
2627  * type  = one of:
2628  *	   VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
2629  * title = title string (can be NULL for default)
2630  * (neither used in console dialogs at the moment)
2631  *
2632  * Format of the "buttons" string:
2633  * "Button1Name\nButton2Name\nButton3Name"
2634  * The first button should normally be the default/accept
2635  * The second button should be the 'Cancel' button
2636  * Other buttons- use your imagination!
2637  * A '&' in a button name becomes a shortcut, so each '&' should be before a
2638  * different letter.
2639  */
2640 /* ARGSUSED */
2641     int
2642 do_dialog(type, title, message, buttons, dfltbutton, textfield)
2643     int		type;
2644     char_u	*title;
2645     char_u	*message;
2646     char_u	*buttons;
2647     int		dfltbutton;
2648     char_u	*textfield;	/* IObuff for inputdialog(), NULL otherwise */
2649 {
2650     int		oldState;
2651     int		retval = 0;
2652     char_u	*hotkeys;
2653     int		c;
2654     int		i;
2655 
2656 #ifndef NO_CONSOLE
2657     /* Don't output anything in silent mode ("ex -s") */
2658     if (silent_mode)
2659 	return dfltbutton;   /* return default option */
2660 #endif
2661 
2662 #ifdef FEAT_GUI_DIALOG
2663     /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
2664     if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
2665     {
2666 	c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
2667 								   textfield);
2668 	msg_end_prompt();
2669 
2670 	/* Flush output to avoid that further messages and redrawing is done
2671 	 * in the wrong order. */
2672 	out_flush();
2673 	gui_mch_update();
2674 
2675 	return c;
2676     }
2677 #endif
2678 
2679     oldState = State;
2680     State = CONFIRM;
2681 #ifdef FEAT_MOUSE
2682     setmouse();
2683 #endif
2684 
2685     /*
2686      * Since we wait for a keypress, don't make the
2687      * user press RETURN as well afterwards.
2688      */
2689     ++no_wait_return;
2690     hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
2691 
2692     if (hotkeys != NULL)
2693     {
2694 	for (;;)
2695 	{
2696 	    /* Get a typed character directly from the user. */
2697 	    c = get_keystroke();
2698 	    switch (c)
2699 	    {
2700 	    case CAR:		/* User accepts default option */
2701 	    case NL:
2702 		retval = dfltbutton;
2703 		break;
2704 	    case Ctrl_C:	/* User aborts/cancels */
2705 	    case ESC:
2706 		retval = 0;
2707 		break;
2708 	    default:		/* Could be a hotkey? */
2709 		if (c < 0)	/* special keys are ignored here */
2710 		    continue;
2711 		/* Make the character lowercase, as chars in "hotkeys" are. */
2712 		c = MB_TOLOWER(c);
2713 		retval = 1;
2714 		for (i = 0; hotkeys[i]; ++i)
2715 		{
2716 #ifdef FEAT_MBYTE
2717 		    if (has_mbyte)
2718 		    {
2719 			if ((*mb_ptr2char)(hotkeys + i) == c)
2720 			    break;
2721 			i += (*mb_ptr2len_check)(hotkeys + i) - 1;
2722 		    }
2723 		    else
2724 #endif
2725 			if (hotkeys[i] == c)
2726 			    break;
2727 		    ++retval;
2728 		}
2729 		if (hotkeys[i])
2730 		    break;
2731 		/* No hotkey match, so keep waiting */
2732 		continue;
2733 	    }
2734 	    break;
2735 	}
2736 
2737 	vim_free(hotkeys);
2738     }
2739 
2740     State = oldState;
2741 #ifdef FEAT_MOUSE
2742     setmouse();
2743 #endif
2744     --no_wait_return;
2745     msg_end_prompt();
2746 
2747     return retval;
2748 }
2749 
2750 static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
2751 
2752 /*
2753  * Copy one character from "*from" to "*to", taking care of multi-byte
2754  * characters.  Return the length of the character in bytes.
2755  */
2756     static int
2757 copy_char(from, to, lowercase)
2758     char_u	*from;
2759     char_u	*to;
2760     int		lowercase;	/* make character lower case */
2761 {
2762 #ifdef FEAT_MBYTE
2763     int		len;
2764     int		c;
2765 
2766     if (has_mbyte)
2767     {
2768 	if (lowercase)
2769 	{
2770 	    c = MB_TOLOWER((*mb_ptr2char)(from));
2771 	    return (*mb_char2bytes)(c, to);
2772 	}
2773 	else
2774 	{
2775 	    len = (*mb_ptr2len_check)(from);
2776 	    mch_memmove(to, from, (size_t)len);
2777 	    return len;
2778 	}
2779     }
2780     else
2781 #endif
2782     {
2783 	if (lowercase)
2784 	    *to = (char_u)TOLOWER_LOC(*from);
2785 	else
2786 	    *to = *from;
2787 	return 1;
2788     }
2789 }
2790 
2791 /*
2792  * Format the dialog string, and display it at the bottom of
2793  * the screen. Return a string of hotkey chars (if defined) for
2794  * each 'button'. If a button has no hotkey defined, the first character of
2795  * the button is used.
2796  * The hotkeys can be multi-byte characters, but without combining chars.
2797  *
2798  * Returns an allocated string with hotkeys, or NULL for error.
2799  */
2800     static char_u *
2801 msg_show_console_dialog(message, buttons, dfltbutton)
2802     char_u	*message;
2803     char_u	*buttons;
2804     int		dfltbutton;
2805 {
2806     int		len = 0;
2807 #ifdef FEAT_MBYTE
2808 # define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
2809 #else
2810 # define HOTK_LEN 1
2811 #endif
2812     int		lenhotkey = HOTK_LEN;	/* count first button */
2813     char_u	*hotk = NULL;
2814     char_u	*msgp = NULL;
2815     char_u	*hotkp = NULL;
2816     char_u	*r;
2817     int		copy;
2818 #define HAS_HOTKEY_LEN 30
2819     char_u	has_hotkey[HAS_HOTKEY_LEN];
2820     int		first_hotkey = FALSE;	/* first char of button is hotkey */
2821     int		idx;
2822 
2823     has_hotkey[0] = FALSE;
2824 
2825     /*
2826      * First loop: compute the size of memory to allocate.
2827      * Second loop: copy to the allocated memory.
2828      */
2829     for (copy = 0; copy <= 1; ++copy)
2830     {
2831 	r = buttons;
2832 	idx = 0;
2833 	while (*r)
2834 	{
2835 	    if (*r == DLG_BUTTON_SEP)
2836 	    {
2837 		if (copy)
2838 		{
2839 		    *msgp++ = ',';
2840 		    *msgp++ = ' ';	    /* '\n' -> ', ' */
2841 
2842 		    /* advance to next hotkey and set default hotkey */
2843 #ifdef FEAT_MBYTE
2844 		    if (has_mbyte)
2845 			hotkp += (*mb_ptr2len_check)(hotkp);
2846 		    else
2847 #endif
2848 			++hotkp;
2849 		    (void)copy_char(r + 1, hotkp, TRUE);
2850 		    if (dfltbutton)
2851 			--dfltbutton;
2852 
2853 		    /* If no hotkey is specified first char is used. */
2854 		    if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
2855 			first_hotkey = TRUE;
2856 		}
2857 		else
2858 		{
2859 		    len += 3;		    /* '\n' -> ', '; 'x' -> '(x)' */
2860 		    lenhotkey += HOTK_LEN;  /* each button needs a hotkey */
2861 		    if (idx < HAS_HOTKEY_LEN - 1)
2862 			has_hotkey[++idx] = FALSE;
2863 		}
2864 	    }
2865 	    else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
2866 	    {
2867 		if (*r == DLG_HOTKEY_CHAR)
2868 		    ++r;
2869 		first_hotkey = FALSE;
2870 		if (copy)
2871 		{
2872 		    if (*r == DLG_HOTKEY_CHAR)		/* '&&a' -> '&a' */
2873 			*msgp++ = *r;
2874 		    else
2875 		    {
2876 			/* '&a' -> '[a]' */
2877 			*msgp++ = (dfltbutton == 1) ? '[' : '(';
2878 			msgp += copy_char(r, msgp, FALSE);
2879 			*msgp++ = (dfltbutton == 1) ? ']' : ')';
2880 
2881 			/* redefine hotkey */
2882 			(void)copy_char(r, hotkp, TRUE);
2883 		    }
2884 		}
2885 		else
2886 		{
2887 		    ++len;	    /* '&a' -> '[a]' */
2888 		    if (idx < HAS_HOTKEY_LEN - 1)
2889 			has_hotkey[idx] = TRUE;
2890 		}
2891 	    }
2892 	    else
2893 	    {
2894 		/* everything else copy literally */
2895 		if (copy)
2896 		    msgp += copy_char(r, msgp, FALSE);
2897 	    }
2898 
2899 	    /* advance to the next character */
2900 #ifdef FEAT_MBYTE
2901 	    if (has_mbyte)
2902 		r += (*mb_ptr2len_check)(r);
2903 	    else
2904 #endif
2905 		++r;
2906 	}
2907 
2908 	if (copy)
2909 	{
2910 	    *msgp++ = ':';
2911 	    *msgp++ = ' ';
2912 	    *msgp = NUL;
2913 #ifdef FEAT_MBYTE
2914 	    if (has_mbyte)
2915 		hotkp += (*mb_ptr2len_check)(hotkp);
2916 	    else
2917 #endif
2918 		++hotkp;
2919 	    *hotkp = NUL;
2920 	}
2921 	else
2922 	{
2923 	    len += STRLEN(message)
2924 		    + 2			/* for the NL's */
2925 		    + STRLEN(buttons)
2926 		    + 3;		/* for the ": " and NUL */
2927 	    lenhotkey++;		/* for the NUL */
2928 
2929 	    /* If no hotkey is specified first char is used. */
2930 	    if (!has_hotkey[0])
2931 	    {
2932 		first_hotkey = TRUE;
2933 		len += 2;		/* "x" -> "[x]" */
2934 	    }
2935 
2936 	    /*
2937 	     * Now allocate and load the strings
2938 	     */
2939 	    vim_free(confirm_msg);
2940 	    confirm_msg = alloc(len);
2941 	    if (confirm_msg == NULL)
2942 		return NULL;
2943 	    *confirm_msg = NUL;
2944 	    hotk = alloc(lenhotkey);
2945 	    if (hotk == NULL)
2946 		return NULL;
2947 
2948 	    *confirm_msg = '\n';
2949 	    STRCPY(confirm_msg + 1, message);
2950 
2951 	    msgp = confirm_msg + 1 + STRLEN(message);
2952 	    hotkp = hotk;
2953 
2954 	    /* define first default hotkey */
2955 	    (void)copy_char(buttons, hotkp, TRUE);
2956 
2957 	    /* Remember where the choices start, displaying starts here when
2958 	     * "hotkp" typed at the more prompt. */
2959 	    confirm_msg_tail = msgp;
2960 	    *msgp++ = '\n';
2961 	}
2962     }
2963 
2964     display_confirm_msg();
2965     return hotk;
2966 }
2967 
2968 /*
2969  * Display the ":confirm" message.  Also called when screen resized.
2970  */
2971     void
2972 display_confirm_msg()
2973 {
2974     /* avoid that 'q' at the more prompt truncates the message here */
2975     ++confirm_msg_used;
2976     if (confirm_msg != NULL)
2977 	msg_puts_attr(confirm_msg, hl_attr(HLF_M));
2978     --confirm_msg_used;
2979 }
2980 
2981 #endif /* FEAT_CON_DIALOG */
2982 
2983 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
2984 
2985     int
2986 vim_dialog_yesno(type, title, message, dflt)
2987     int		type;
2988     char_u	*title;
2989     char_u	*message;
2990     int		dflt;
2991 {
2992     if (do_dialog(type,
2993 		title == NULL ? (char_u *)_("Question") : title,
2994 		message,
2995 		(char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
2996 	return VIM_YES;
2997     return VIM_NO;
2998 }
2999 
3000     int
3001 vim_dialog_yesnocancel(type, title, message, dflt)
3002     int		type;
3003     char_u	*title;
3004     char_u	*message;
3005     int		dflt;
3006 {
3007     switch (do_dialog(type,
3008 		title == NULL ? (char_u *)_("Question") : title,
3009 		message,
3010 		(char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3011     {
3012 	case 1: return VIM_YES;
3013 	case 2: return VIM_NO;
3014     }
3015     return VIM_CANCEL;
3016 }
3017 
3018     int
3019 vim_dialog_yesnoallcancel(type, title, message, dflt)
3020     int		type;
3021     char_u	*title;
3022     char_u	*message;
3023     int		dflt;
3024 {
3025     switch (do_dialog(type,
3026 		title == NULL ? (char_u *)"Question" : title,
3027 		message,
3028 		(char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3029 								  dflt, NULL))
3030     {
3031 	case 1: return VIM_YES;
3032 	case 2: return VIM_NO;
3033 	case 3: return VIM_ALL;
3034 	case 4: return VIM_DISCARDALL;
3035     }
3036     return VIM_CANCEL;
3037 }
3038 
3039 #endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3040 
3041 #if defined(FEAT_BROWSE) || defined(PROTO)
3042 /*
3043  * Generic browse function.  Calls gui_mch_browse() when possible.
3044  * Later this may pop-up a non-GUI file selector (external command?).
3045  */
3046     char_u *
3047 do_browse(flags, title, dflt, ext, initdir, filter, buf)
3048     int		flags;		/* BROWSE_SAVE and BROWSE_DIR */
3049     char_u	*title;		/* title for the window */
3050     char_u	*dflt;		/* default file name (may include directory) */
3051     char_u	*ext;		/* extension added */
3052     char_u	*initdir;	/* initial directory, NULL for current dir or
3053 				   when using path from "dflt" */
3054     char_u	*filter;	/* file name filter */
3055     buf_T	*buf;		/* buffer to read/write for */
3056 {
3057     char_u		*fname;
3058     static char_u	*last_dir = NULL;    /* last used directory */
3059     char_u		*tofree = NULL;
3060     int			save_browse = cmdmod.browse;
3061 
3062     /* Must turn off browse to avoid that autocommands will get the
3063      * flag too!  */
3064     cmdmod.browse = FALSE;
3065 
3066     if (title == NULL || *title == NUL)
3067     {
3068 	if (flags & BROWSE_DIR)
3069 	    title = (char_u *)_("Select Directory dialog");
3070 	else if (flags & BROWSE_SAVE)
3071 	    title = (char_u *)_("Save File dialog");
3072 	else
3073 	    title = (char_u *)_("Open File dialog");
3074     }
3075 
3076     /* When no directory specified, use default file name, default dir, buffer
3077      * dir, last dir or current dir */
3078     if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3079     {
3080 	if (mch_isdir(dflt))		/* default file name is a directory */
3081 	{
3082 	    initdir = dflt;
3083 	    dflt = NULL;
3084 	}
3085 	else if (gettail(dflt) != dflt)	/* default file name includes a path */
3086 	{
3087 	    tofree = vim_strsave(dflt);
3088 	    if (tofree != NULL)
3089 	    {
3090 		initdir = tofree;
3091 		*gettail(initdir) = NUL;
3092 		dflt = gettail(dflt);
3093 	    }
3094 	}
3095     }
3096 
3097     if (initdir == NULL || *initdir == NUL)
3098     {
3099 	/* When 'browsedir' is a directory, use it */
3100 	if (STRCMP(p_bsdir, "last") != 0
3101 		&& STRCMP(p_bsdir, "buffer") != 0
3102 		&& STRCMP(p_bsdir, "current") != 0
3103 		&& mch_isdir(p_bsdir))
3104 	    initdir = p_bsdir;
3105 	/* When saving or 'browsedir' is "buffer", use buffer fname */
3106 	else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
3107 		&& buf != NULL && buf->b_ffname != NULL)
3108 	{
3109 	    if (dflt == NULL || *dflt == NUL)
3110 		dflt = gettail(curbuf->b_ffname);
3111 	    tofree = vim_strsave(curbuf->b_ffname);
3112 	    if (tofree != NULL)
3113 	    {
3114 		initdir = tofree;
3115 		*gettail(initdir) = NUL;
3116 	    }
3117 	}
3118 	/* When 'browsedir' is "last", use dir from last browse */
3119 	else if (*p_bsdir == 'l')
3120 	    initdir = last_dir;
3121 	/* When 'browsedir is "current", use current directory.  This is the
3122 	 * default already, leave initdir empty. */
3123     }
3124 
3125 # ifdef FEAT_GUI
3126     if (gui.in_use)		/* when this changes, also adjust f_has()! */
3127     {
3128 	if (filter == NULL
3129 #  ifdef FEAT_EVAL
3130 		&& (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3131 		&& (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3132 #  endif
3133 	)
3134 	    filter = BROWSE_FILTER_DEFAULT;
3135 	if (flags & BROWSE_DIR)
3136 	{
3137 #  if defined(HAVE_GTK2) || defined(WIN3264)
3138 	    /* For systems that have a directory dialog. */
3139 	    fname = gui_mch_browsedir(title, initdir);
3140 #  else
3141 	    /* Generic solution for selecting a directory: select a file and
3142 	     * remove the file name. */
3143 	    fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3144 #  endif
3145 #  if !defined(HAVE_GTK2)
3146 	    /* Win32 adds a dummy file name, others return an arbitrary file
3147 	     * name.  GTK+ 2 returns only the directory, */
3148 	    if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3149 	    {
3150 		/* Remove the file name. */
3151 		char_u	    *s = get_past_head(fname);
3152 		char_u	    *tail = gettail(fname);
3153 
3154 		while (tail > s && vim_ispathsep(tail[-1]))
3155 		    --tail;
3156 		if (tail == fname)
3157 		    *tail++ = '.';	/* use current dir */
3158 		*tail = NUL;
3159 	    }
3160 #  endif
3161 	}
3162 	else
3163 	    fname = gui_mch_browse(flags & BROWSE_SAVE,
3164 					   title, dflt, ext, initdir, filter);
3165 
3166 	/* We hang around in the dialog for a while, the user might do some
3167 	 * things to our files.  The Win32 dialog allows deleting or renaming
3168 	 * a file, check timestamps. */
3169 	need_check_timestamps = TRUE;
3170 	did_check_timestamps = FALSE;
3171     }
3172     else
3173 # endif
3174     {
3175 	/* TODO: non-GUI file selector here */
3176 	EMSG(_("E338: Sorry, no file browser in console mode"));
3177 	fname = NULL;
3178     }
3179 
3180     /* keep the directory for next time */
3181     if (fname != NULL)
3182     {
3183 	vim_free(last_dir);
3184 	last_dir = vim_strsave(fname);
3185 	if (last_dir != NULL && !(flags & BROWSE_DIR))
3186 	{
3187 	    *gettail(last_dir) = NUL;
3188 	    if (*last_dir == NUL)
3189 	    {
3190 		/* filename only returned, must be in current dir */
3191 		vim_free(last_dir);
3192 		last_dir = alloc(MAXPATHL);
3193 		if (last_dir != NULL)
3194 		    mch_dirname(last_dir, MAXPATHL);
3195 	    }
3196 	}
3197     }
3198 
3199     vim_free(tofree);
3200     cmdmod.browse = save_browse;
3201 
3202     return fname;
3203 }
3204 #endif
3205