xref: /vim-8.2.3635/src/message.c (revision 2df6dcc5)
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(PROTO)
817 static void msg_end_prompt __ARGS((void));
818 
819 /*
820  * Call this after prompting the user.  This will avoid a hit-return message
821  * and a delay.
822  */
823     static void
824 msg_end_prompt()
825 {
826     need_wait_return = FALSE;
827     emsg_on_display = FALSE;
828     cmdline_row = msg_row;
829     msg_col = 0;
830     msg_clr_eos();
831 }
832 #endif
833 
834 /*
835  * wait for the user to hit a key (normally a return)
836  * if 'redraw' is TRUE, clear and redraw the screen
837  * if 'redraw' is FALSE, just redraw the screen
838  * if 'redraw' is -1, don't redraw at all
839  */
840     void
841 wait_return(redraw)
842     int		redraw;
843 {
844     int		c;
845     int		oldState;
846     int		tmpState;
847 #ifndef ORG_HITRETURN
848     int		had_got_int;
849 #endif
850 
851     if (redraw == TRUE)
852 	must_redraw = CLEAR;
853 
854     /* If using ":silent cmd", don't wait for a return.  Also don't set
855      * need_wait_return to do it later. */
856     if (msg_silent != 0)
857 	return;
858 
859 /*
860  * With the global command (and some others) we only need one return at the
861  * end. Adjust cmdline_row to avoid the next message overwriting the last one.
862  * When inside vgetc(), we can't wait for a typed character at all.
863  */
864     if (vgetc_busy)
865 	return;
866     if (no_wait_return)
867     {
868 	need_wait_return = TRUE;
869 	if (!exmode_active)
870 	    cmdline_row = msg_row;
871 	return;
872     }
873 
874     redir_off = TRUE;		/* don't redirect this message */
875     oldState = State;
876     if (quit_more)
877     {
878 	c = CAR;		/* just pretend CR was hit */
879 	quit_more = FALSE;
880 	got_int = FALSE;
881     }
882     else if (exmode_active)
883     {
884 	MSG_PUTS(" ");		/* make sure the cursor is on the right line */
885 	c = CAR;		/* no need for a return in ex mode */
886 	got_int = FALSE;
887     }
888     else
889     {
890 	/* Make sure the hit-return prompt is on screen when 'guioptions' was
891 	 * just changed. */
892 	screenalloc(FALSE);
893 
894 	State = HITRETURN;
895 #ifdef FEAT_MOUSE
896 	setmouse();
897 #endif
898 #ifdef USE_ON_FLY_SCROLL
899 	dont_scroll = TRUE;		/* disallow scrolling here */
900 #endif
901 	hit_return_msg();
902 
903 #ifdef ORG_HITRETURN
904 	do
905 	{
906 	    c = safe_vgetc();
907 	} while (vim_strchr((char_u *)"\r\n: ", c) == NULL);
908 	if (c == ':')			/* this can vi too (but not always!) */
909 	    stuffcharReadbuff(c);
910 #else
911 	do
912 	{
913 	    /* Remember "got_int", if it is set vgetc() probably returns a
914 	     * CTRL-C, but we need to loop then. */
915 	    had_got_int = got_int;
916 	    c = safe_vgetc();
917 	    if (!global_busy)
918 		got_int = FALSE;
919 #ifdef FEAT_CLIPBOARD
920 	    /* Strange way to allow copying (yanking) a modeless selection at
921 	     * the hit-enter prompt.  Use CTRL-Y, because the same is used in
922 	     * Cmdline-mode and it's harmless when there is no selection. */
923 	    if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
924 	    {
925 		clip_copy_modeless_selection(TRUE);
926 		c = K_IGNORE;
927 	    }
928 #endif
929 	} while ((had_got_int && c == Ctrl_C)
930 				|| c == K_IGNORE
931 #ifdef FEAT_GUI
932 				|| c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
933 #endif
934 #ifdef FEAT_MOUSE
935 				|| c == K_LEFTDRAG   || c == K_LEFTRELEASE
936 				|| c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
937 				|| c == K_RIGHTDRAG  || c == K_RIGHTRELEASE
938 				|| c == K_MOUSEDOWN  || c == K_MOUSEUP
939 				|| (!mouse_has(MOUSE_RETURN)
940 				    && mouse_row < msg_row
941 				    && (c == K_LEFTMOUSE
942 					|| c == K_MIDDLEMOUSE
943 					|| c == K_RIGHTMOUSE
944 					|| c == K_X1MOUSE
945 					|| c == K_X2MOUSE))
946 #endif
947 				);
948 	ui_breakcheck();
949 #ifdef FEAT_MOUSE
950 	/*
951 	 * Avoid that the mouse-up event causes visual mode to start.
952 	 */
953 	if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
954 					  || c == K_X1MOUSE || c == K_X2MOUSE)
955 	    (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
956 	else
957 #endif
958 	    if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
959 	{
960 	    stuffcharReadbuff(c);
961 	    do_redraw = TRUE;	    /* need a redraw even though there is
962 				       something in the stuff buffer */
963 	}
964 #endif
965     }
966     redir_off = FALSE;
967 
968     /*
969      * If the user hits ':', '?' or '/' we get a command line from the next
970      * line.
971      */
972     if (c == ':' || c == '?' || c == '/')
973     {
974 	if (!exmode_active)
975 	    cmdline_row = msg_row;
976 	skip_redraw = TRUE;	    /* skip redraw once */
977 	do_redraw = FALSE;
978     }
979 
980     /*
981      * If the window size changed set_shellsize() will redraw the screen.
982      * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
983      * typed.
984      */
985     tmpState = State;
986     State = oldState;		    /* restore State before set_shellsize */
987 #ifdef FEAT_MOUSE
988     setmouse();
989 #endif
990     msg_check();
991 
992 #if defined(UNIX) || defined(VMS)
993     /*
994      * When switching screens, we need to output an extra newline on exit.
995      */
996     if (swapping_screen() && !termcap_active)
997 	newline_on_exit = TRUE;
998 #endif
999 
1000     need_wait_return = FALSE;
1001     did_wait_return = TRUE;
1002     emsg_on_display = FALSE;	/* can delete error message now */
1003     lines_left = -1;		/* reset lines_left at next msg_start() */
1004     reset_last_sourcing();
1005     if (keep_msg != NULL && vim_strsize(keep_msg) >=
1006 				  (Rows - cmdline_row - 1) * Columns + sc_col)
1007     {
1008 	vim_free(keep_msg);
1009 	keep_msg = NULL;	    /* don't redisplay message, it's too long */
1010     }
1011 
1012     if (tmpState == SETWSIZE)	    /* got resize event while in vgetc() */
1013     {
1014 	starttermcap();		    /* start termcap before redrawing */
1015 	shell_resized();
1016     }
1017     else if (!skip_redraw
1018 	    && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1019     {
1020 	starttermcap();		    /* start termcap before redrawing */
1021 	redraw_later(VALID);
1022     }
1023 }
1024 
1025 /*
1026  * Write the hit-return prompt.
1027  */
1028     static void
1029 hit_return_msg()
1030 {
1031     if (msg_didout)		    /* start on a new line */
1032 	msg_putchar('\n');
1033     if (got_int)
1034 	MSG_PUTS(_("Interrupt: "));
1035 
1036 #ifdef ORG_HITRETURN
1037     MSG_PUTS_ATTR(_("Hit ENTER to continue"), hl_attr(HLF_R));
1038 #else
1039     MSG_PUTS_ATTR(_("Hit ENTER or type command to continue"), hl_attr(HLF_R));
1040 #endif
1041     if (!msg_use_printf())
1042 	msg_clr_eos();
1043 }
1044 
1045 /*
1046  * Set "keep_msg" to "s".  Free the old value and check for NULL pointer.
1047  */
1048     void
1049 set_keep_msg(s)
1050     char_u	*s;
1051 {
1052     vim_free(keep_msg);
1053     if (s != NULL && msg_silent == 0)
1054 	keep_msg = vim_strsave(s);
1055     else
1056 	keep_msg = NULL;
1057 }
1058 
1059 /*
1060  * Prepare for outputting characters in the command line.
1061  */
1062     void
1063 msg_start()
1064 {
1065     int		did_return = FALSE;
1066 
1067     vim_free(keep_msg);
1068     keep_msg = NULL;			/* don't display old message now */
1069     if (!msg_scroll && full_screen)	/* overwrite last message */
1070     {
1071 	msg_row = cmdline_row;
1072 	msg_col =
1073 #ifdef FEAT_RIGHTLEFT
1074 	    cmdmsg_rl ? Columns - 1 :
1075 #endif
1076 	    0;
1077     }
1078     else if (msg_didout)		    /* start message on next line */
1079     {
1080 	msg_putchar('\n');
1081 	did_return = TRUE;
1082 	if (exmode_active != EXMODE_NORMAL)
1083 	    cmdline_row = msg_row;
1084     }
1085     if (!msg_didany || lines_left < 0)
1086 	msg_starthere();
1087     if (msg_silent == 0)
1088     {
1089 	msg_didout = FALSE;		    /* no output on current line yet */
1090 	cursor_off();
1091     }
1092 
1093     /* when redirecting, may need to start a new line. */
1094     if (!did_return)
1095 	redir_write((char_u *)"\n", -1);
1096 }
1097 
1098 /*
1099  * Note that the current msg position is where messages start.
1100  */
1101     void
1102 msg_starthere()
1103 {
1104     lines_left = cmdline_row;
1105     msg_didany = FALSE;
1106 }
1107 
1108     void
1109 msg_putchar(c)
1110     int		c;
1111 {
1112     msg_putchar_attr(c, 0);
1113 }
1114 
1115     void
1116 msg_putchar_attr(c, attr)
1117     int		c;
1118     int		attr;
1119 {
1120 #ifdef FEAT_MBYTE
1121     char_u	buf[MB_MAXBYTES + 1];
1122 #else
1123     char_u	buf[4];
1124 #endif
1125 
1126     if (IS_SPECIAL(c))
1127     {
1128 	buf[0] = K_SPECIAL;
1129 	buf[1] = K_SECOND(c);
1130 	buf[2] = K_THIRD(c);
1131 	buf[3] = NUL;
1132     }
1133     else
1134     {
1135 #ifdef FEAT_MBYTE
1136 	buf[(*mb_char2bytes)(c, buf)] = NUL;
1137 #else
1138 	buf[0] = c;
1139 	buf[1] = NUL;
1140 #endif
1141     }
1142     msg_puts_attr(buf, attr);
1143 }
1144 
1145     void
1146 msg_outnum(n)
1147     long	n;
1148 {
1149     char_u	buf[20];
1150 
1151     sprintf((char *)buf, "%ld", n);
1152     msg_puts(buf);
1153 }
1154 
1155     void
1156 msg_home_replace(fname)
1157     char_u	*fname;
1158 {
1159     msg_home_replace_attr(fname, 0);
1160 }
1161 
1162 #if defined(FEAT_FIND_ID) || defined(PROTO)
1163     void
1164 msg_home_replace_hl(fname)
1165     char_u	*fname;
1166 {
1167     msg_home_replace_attr(fname, hl_attr(HLF_D));
1168 }
1169 #endif
1170 
1171     static void
1172 msg_home_replace_attr(fname, attr)
1173     char_u  *fname;
1174     int	    attr;
1175 {
1176     char_u	*name;
1177 
1178     name = home_replace_save(NULL, fname);
1179     if (name != NULL)
1180 	msg_outtrans_attr(name, attr);
1181     vim_free(name);
1182 }
1183 
1184 /*
1185  * Output 'len' characters in 'str' (including NULs) with translation
1186  * if 'len' is -1, output upto a NUL character.
1187  * Use attributes 'attr'.
1188  * Return the number of characters it takes on the screen.
1189  */
1190     int
1191 msg_outtrans(str)
1192     char_u	    *str;
1193 {
1194     return msg_outtrans_attr(str, 0);
1195 }
1196 
1197     int
1198 msg_outtrans_attr(str, attr)
1199     char_u	*str;
1200     int		attr;
1201 {
1202     return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1203 }
1204 
1205     int
1206 msg_outtrans_len(str, len)
1207     char_u	*str;
1208     int		len;
1209 {
1210     return msg_outtrans_len_attr(str, len, 0);
1211 }
1212 
1213 /*
1214  * Output one character at "p".  Return pointer to the next character.
1215  * Handles multi-byte characters.
1216  */
1217     char_u *
1218 msg_outtrans_one(p, attr)
1219     char_u	*p;
1220     int		attr;
1221 {
1222 #ifdef FEAT_MBYTE
1223     int		l;
1224 
1225     if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
1226     {
1227 	msg_outtrans_len_attr(p, l, attr);
1228 	return p + l;
1229     }
1230 #endif
1231     msg_puts_attr(transchar_byte(*p), attr);
1232     return p + 1;
1233 }
1234 
1235     int
1236 msg_outtrans_len_attr(msgstr, len, attr)
1237     char_u	*msgstr;
1238     int		len;
1239     int		attr;
1240 {
1241     int		retval = 0;
1242     char_u	*str = msgstr;
1243     char_u	*plain_start = msgstr;
1244     char_u	*s;
1245 #ifdef FEAT_MBYTE
1246     int		mb_l;
1247     int		c;
1248 #endif
1249 
1250     /* if MSG_HIST flag set, add message to history */
1251     if (attr & MSG_HIST)
1252     {
1253 	add_msg_hist(str, len, attr);
1254 	attr &= ~MSG_HIST;
1255     }
1256 
1257 #ifdef FEAT_MBYTE
1258     /* If the string starts with a composing character first draw a space on
1259      * which the composing char can be drawn. */
1260     if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1261 	msg_puts_attr((char_u *)" ", attr);
1262 #endif
1263 
1264     /*
1265      * Go over the string.  Special characters are translated and printed.
1266      * Normal characters are printed several at a time.
1267      */
1268     while (--len >= 0)
1269     {
1270 #ifdef FEAT_MBYTE
1271 	if (enc_utf8)
1272 	    /* Don't include composing chars after the end. */
1273 	    mb_l = utfc_ptr2len_check_len(str, len + 1);
1274 	else if (has_mbyte)
1275 	    mb_l = (*mb_ptr2len_check)(str);
1276 	else
1277 	    mb_l = 1;
1278 	if (has_mbyte && mb_l > 1)
1279 	{
1280 	    c = (*mb_ptr2char)(str);
1281 	    if (vim_isprintc(c))
1282 		/* printable multi-byte char: count the cells. */
1283 		retval += (*mb_ptr2cells)(str);
1284 	    else
1285 	    {
1286 		/* unprintable multi-byte char: print the printable chars so
1287 		 * far and the translation of the unprintable char. */
1288 		if (str > plain_start)
1289 		    msg_puts_attr_len(plain_start, (int)(str - plain_start),
1290 									attr);
1291 		plain_start = str + mb_l;
1292 		msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1293 		retval += char2cells(c);
1294 	    }
1295 	    len -= mb_l - 1;
1296 	    str += mb_l;
1297 	}
1298 	else
1299 #endif
1300 	{
1301 	    s = transchar_byte(*str);
1302 	    if (s[1] != NUL)
1303 	    {
1304 		/* unprintable char: print the printable chars so far and the
1305 		 * translation of the unprintable char. */
1306 		if (str > plain_start)
1307 		    msg_puts_attr_len(plain_start, (int)(str - plain_start),
1308 									attr);
1309 		plain_start = str + 1;
1310 		msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1311 	    }
1312 	    retval += ptr2cells(str);
1313 	    ++str;
1314 	}
1315     }
1316 
1317     if (str > plain_start)
1318 	/* print the printable chars at the end */
1319 	msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1320 
1321     return retval;
1322 }
1323 
1324 #if defined(FEAT_QUICKFIX) || defined(PROTO)
1325     void
1326 msg_make(arg)
1327     char_u  *arg;
1328 {
1329     int	    i;
1330     static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1331 
1332     arg = skipwhite(arg);
1333     for (i = 5; *arg && i >= 0; --i)
1334 	if (*arg++ != str[i])
1335 	    break;
1336     if (i < 0)
1337     {
1338 	msg_putchar('\n');
1339 	for (i = 0; rs[i]; ++i)
1340 	    msg_putchar(rs[i] - 3);
1341     }
1342 }
1343 #endif
1344 
1345 /*
1346  * Output the string 'str' upto a NUL character.
1347  * Return the number of characters it takes on the screen.
1348  *
1349  * If K_SPECIAL is encountered, then it is taken in conjunction with the
1350  * following character and shown as <F1>, <S-Up> etc.  Any other character
1351  * which is not printable shown in <> form.
1352  * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1353  * If a character is displayed in one of these special ways, is also
1354  * highlighted (its highlight name is '8' in the p_hl variable).
1355  * Otherwise characters are not highlighted.
1356  * This function is used to show mappings, where we want to see how to type
1357  * the character/string -- webb
1358  */
1359     int
1360 msg_outtrans_special(strstart, from)
1361     char_u	*strstart;
1362     int		from;	/* TRUE for lhs of a mapping */
1363 {
1364     char_u	*str = strstart;
1365     int		retval = 0;
1366     char_u	*string;
1367     int		attr;
1368     int		len;
1369 
1370     attr = hl_attr(HLF_8);
1371     while (*str != NUL)
1372     {
1373 	/* Leading and trailing spaces need to be displayed in <> form. */
1374 	if ((str == strstart || str[1] == NUL) && *str == ' ')
1375 	{
1376 	    string = (char_u *)"<Space>";
1377 	    ++str;
1378 	}
1379 	else
1380 	    string = str2special(&str, from);
1381 	len = vim_strsize(string);
1382 	/* Highlight special keys */
1383 	msg_puts_attr(string, len > 1
1384 #ifdef FEAT_MBYTE
1385 		&& (*mb_ptr2len_check)(string) <= 1
1386 #endif
1387 		? attr : 0);
1388 	retval += len;
1389     }
1390     return retval;
1391 }
1392 
1393 /*
1394  * Return the printable string for the key codes at "*sp".
1395  * Used for translating the lhs or rhs of a mapping to printable chars.
1396  * Advances "sp" to the next code.
1397  */
1398     char_u *
1399 str2special(sp, from)
1400     char_u	**sp;
1401     int		from;	/* TRUE for lhs of mapping */
1402 {
1403     int			c;
1404     static char_u	buf[7];
1405     char_u		*str = *sp;
1406     int			modifiers = 0;
1407     int			special = FALSE;
1408 
1409 #ifdef FEAT_MBYTE
1410     if (has_mbyte)
1411     {
1412 	char_u	*p;
1413 
1414 	/* Try to un-escape a multi-byte character.  Return the un-escaped
1415 	 * string if it is a multi-byte character. */
1416 	p = mb_unescape(sp);
1417 	if (p != NULL)
1418 	    return p;
1419     }
1420 #endif
1421 
1422     c = *str;
1423     if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1424     {
1425 	if (str[1] == KS_MODIFIER)
1426 	{
1427 	    modifiers = str[2];
1428 	    str += 3;
1429 	    c = *str;
1430 	}
1431 	if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1432 	{
1433 	    c = TO_SPECIAL(str[1], str[2]);
1434 	    str += 2;
1435 	    if (c == K_ZERO)	/* display <Nul> as ^@ */
1436 		c = NUL;
1437 	}
1438 	if (IS_SPECIAL(c) || modifiers)	/* special key */
1439 	    special = TRUE;
1440     }
1441     *sp = str + 1;
1442 
1443 #ifdef FEAT_MBYTE
1444     /* For multi-byte characters check for an illegal byte. */
1445     if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len_check)(str))
1446     {
1447 	transchar_nonprint(buf, c);
1448 	return buf;
1449     }
1450 #endif
1451 
1452     /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1453      * Use <Space> only for lhs of a mapping. */
1454     if (special || char2cells(c) > 1 || (from && c == ' '))
1455 	return get_special_key_name(c, modifiers);
1456     buf[0] = c;
1457     buf[1] = NUL;
1458     return buf;
1459 }
1460 
1461 /*
1462  * Translate a key sequence into special key names.
1463  */
1464     void
1465 str2specialbuf(sp, buf, len)
1466     char_u	*sp;
1467     char_u	*buf;
1468     int		len;
1469 {
1470     char_u	*s;
1471 
1472     *buf = NUL;
1473     while (*sp)
1474     {
1475 	s = str2special(&sp, FALSE);
1476 	if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1477 	    STRCAT(buf, s);
1478     }
1479 }
1480 
1481 /*
1482  * print line for :print or :list command
1483  */
1484     void
1485 msg_prt_line(s)
1486     char_u	*s;
1487 {
1488     int		c;
1489     int		col = 0;
1490     int		n_extra = 0;
1491     int		c_extra = 0;
1492     char_u	*p_extra = NULL;	    /* init to make SASC shut up */
1493     int		n;
1494     int		attr= 0;
1495     char_u	*trail = NULL;
1496 #ifdef FEAT_MBYTE
1497     int		l;
1498     char_u	buf[MB_MAXBYTES + 1];
1499 #endif
1500 
1501     /* find start of trailing whitespace */
1502     if (curwin->w_p_list && lcs_trail)
1503     {
1504 	trail = s + STRLEN(s);
1505 	while (trail > s && vim_iswhite(trail[-1]))
1506 	    --trail;
1507     }
1508 
1509     /* output a space for an empty line, otherwise the line will be
1510      * overwritten */
1511     if (*s == NUL && !(curwin->w_p_list && lcs_eol != NUL))
1512 	msg_putchar(' ');
1513 
1514     for (;;)
1515     {
1516 	if (n_extra)
1517 	{
1518 	    --n_extra;
1519 	    if (c_extra)
1520 		c = c_extra;
1521 	    else
1522 		c = *p_extra++;
1523 	}
1524 #ifdef FEAT_MBYTE
1525 	else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
1526 	{
1527 	    col += (*mb_ptr2cells)(s);
1528 	    mch_memmove(buf, s, (size_t)l);
1529 	    buf[l] = NUL;
1530 	    msg_puts_attr(buf, attr);
1531 	    s += l;
1532 	    continue;
1533 	}
1534 #endif
1535 	else
1536 	{
1537 	    attr = 0;
1538 	    c = *s++;
1539 	    if (c == TAB && (!curwin->w_p_list || lcs_tab1))
1540 	    {
1541 		/* tab amount depends on current column */
1542 		n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
1543 		if (!curwin->w_p_list)
1544 		{
1545 		    c = ' ';
1546 		    c_extra = ' ';
1547 		}
1548 		else
1549 		{
1550 		    c = lcs_tab1;
1551 		    c_extra = lcs_tab2;
1552 		    attr = hl_attr(HLF_8);
1553 		}
1554 	    }
1555 	    else if (c == NUL && curwin->w_p_list && lcs_eol != NUL)
1556 	    {
1557 		p_extra = (char_u *)"";
1558 		c_extra = NUL;
1559 		n_extra = 1;
1560 		c = lcs_eol;
1561 		attr = hl_attr(HLF_AT);
1562 		--s;
1563 	    }
1564 	    else if (c != NUL && (n = byte2cells(c)) > 1)
1565 	    {
1566 		n_extra = n - 1;
1567 		p_extra = transchar_byte(c);
1568 		c_extra = NUL;
1569 		c = *p_extra++;
1570 	    }
1571 	    else if (c == ' ' && trail != NULL && s > trail)
1572 	    {
1573 		c = lcs_trail;
1574 		attr = hl_attr(HLF_8);
1575 	    }
1576 	}
1577 
1578 	if (c == NUL)
1579 	    break;
1580 
1581 	msg_putchar_attr(c, attr);
1582 	col++;
1583     }
1584     msg_clr_eos();
1585 }
1586 
1587 #ifdef FEAT_MBYTE
1588 /*
1589  * Use screen_puts() to output one multi-byte character.
1590  * Return the pointer "s" advanced to the next character.
1591  */
1592     static char_u *
1593 screen_puts_mbyte(s, l, attr)
1594     char_u	*s;
1595     int		l;
1596     int		attr;
1597 {
1598     int		cw;
1599 
1600     msg_didout = TRUE;		/* remember that line is not empty */
1601     cw = (*mb_ptr2cells)(s);
1602     if (cw > 1 && (
1603 #ifdef FEAT_RIGHTLEFT
1604 		cmdmsg_rl ? msg_col <= 1 :
1605 #endif
1606 		msg_col == Columns - 1))
1607     {
1608 	/* Doesn't fit, print a highlighted '>' to fill it up. */
1609 	msg_screen_putchar('>', hl_attr(HLF_AT));
1610 	return s;
1611     }
1612 
1613     screen_puts_len(s, l, msg_row, msg_col, attr);
1614 #ifdef FEAT_RIGHTLEFT
1615     if (cmdmsg_rl)
1616     {
1617 	msg_col -= cw;
1618 	if (msg_col == 0)
1619 	{
1620 	    msg_col = Columns;
1621 	    ++msg_row;
1622 	}
1623     }
1624     else
1625 #endif
1626     {
1627 	msg_col += cw;
1628 	if (msg_col >= Columns)
1629 	{
1630 	    msg_col = 0;
1631 	    ++msg_row;
1632 	}
1633     }
1634     return s + l;
1635 }
1636 #endif
1637 
1638 /*
1639  * Output a string to the screen at position msg_row, msg_col.
1640  * Update msg_row and msg_col for the next message.
1641  */
1642     void
1643 msg_puts(s)
1644     char_u	*s;
1645 {
1646     msg_puts_attr(s, 0);
1647 }
1648 
1649     void
1650 msg_puts_title(s)
1651     char_u	*s;
1652 {
1653     msg_puts_attr(s, hl_attr(HLF_T));
1654 }
1655 
1656 #if defined(FEAT_CSCOPE) || defined(PROTO)
1657 /*
1658  * if printing a string will exceed the screen width, print "..." in the
1659  * middle.
1660  */
1661     void
1662 msg_puts_long(longstr)
1663     char_u	*longstr;
1664 {
1665     msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), 0);
1666 }
1667 #endif
1668 
1669 /*
1670  * Show a message in such a way that it always fits in the line.  Cut out a
1671  * part in the middle and replace it with "..." when necessary.
1672  * Does not handle multi-byte characters!
1673  */
1674     void
1675 msg_puts_long_attr(longstr, attr)
1676     char_u	*longstr;
1677     int		attr;
1678 {
1679     msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1680 }
1681 
1682     void
1683 msg_puts_long_len_attr(longstr, len, attr)
1684     char_u	*longstr;
1685     int		len;
1686     int		attr;
1687 {
1688     int		slen = len;
1689     int		room;
1690 
1691     room = Columns - msg_col;
1692     if (len > room && room >= 20)
1693     {
1694 	slen = (room - 3) / 2;
1695 	msg_outtrans_len_attr(longstr, slen, attr);
1696 	msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1697     }
1698     msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1699 }
1700 
1701 /*
1702  * Basic function for writing a message with highlight attributes.
1703  */
1704     void
1705 msg_puts_attr(s, attr)
1706     char_u	*s;
1707     int		attr;
1708 {
1709     msg_puts_attr_len(s, -1, attr);
1710 }
1711 
1712 /*
1713  * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1714  * When "maxlen" is -1 there is no maximum length.
1715  * When "maxlen" is >= 0 the message is not put in the history.
1716  */
1717     static void
1718 msg_puts_attr_len(str, maxlen, attr)
1719     char_u	*str;
1720     int		maxlen;
1721     int		attr;
1722 {
1723     int		oldState;
1724     char_u	*s = str;
1725     char_u	*p;
1726     char_u	buf[4];
1727     char_u	*t_s = str;	/* string from "t_s" to "s" is still todo */
1728     int		t_col = 0;	/* screen cells todo, 0 when "t_s" not used */
1729 #ifdef FEAT_MBYTE
1730     int		l;
1731     int		cw;
1732 #endif
1733     int		c;
1734 
1735     /*
1736      * If redirection is on, also write to the redirection file.
1737      */
1738     redir_write(s, maxlen);
1739 
1740     /*
1741      * Don't print anything when using ":silent cmd".
1742      */
1743     if (msg_silent != 0)
1744 	return;
1745 
1746     /* if MSG_HIST flag set, add message to history */
1747     if ((attr & MSG_HIST) && maxlen < 0)
1748     {
1749 	add_msg_hist(s, -1, attr);
1750 	attr &= ~MSG_HIST;
1751     }
1752 
1753     /*
1754      * When writing something to the screen after it has scrolled, requires a
1755      * wait-return prompt later.  Needed when scrolling, resetting
1756      * need_wait_return after some prompt, and then outputting something
1757      * without scrolling
1758      */
1759     if (msg_scrolled && !msg_scrolled_ign)
1760 	need_wait_return = TRUE;
1761     msg_didany = TRUE;		/* remember that something was outputted */
1762 
1763     /*
1764      * If there is no valid screen, use fprintf so we can see error messages.
1765      * If termcap is not active, we may be writing in an alternate console
1766      * window, cursor positioning may not work correctly (window size may be
1767      * different, e.g. for Win32 console) or we just don't know where the
1768      * cursor is.
1769      */
1770     if (msg_use_printf())
1771     {
1772 #ifdef WIN3264
1773 	if (!(silent_mode && p_verbose == 0))
1774 	    mch_settmode(TMODE_COOK);	/* handle '\r' and '\n' correctly */
1775 #endif
1776 	while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1777 	{
1778 	    if (!(silent_mode && p_verbose == 0))
1779 	    {
1780 		p = &buf[0];
1781 		/* NL --> CR NL translation (for Unix, not for "--version") */
1782 		/* NL --> CR translation (for Mac) */
1783 		if (*s == '\n' && !info_message)
1784 		    *p++ = '\r';
1785 #if defined(USE_CR) && !defined(MACOS_X_UNIX)
1786 		else
1787 #endif
1788 		    *p++ = *s;
1789 		*p = '\0';
1790 		if (info_message)	/* informative message, not an error */
1791 		    mch_msg((char *)buf);
1792 		else
1793 		    mch_errmsg((char *)buf);
1794 	    }
1795 
1796 	    /* primitive way to compute the current column */
1797 #ifdef FEAT_RIGHTLEFT
1798 	    if (cmdmsg_rl)
1799 	    {
1800 		if (*s == '\r' || *s == '\n')
1801 		    msg_col = Columns - 1;
1802 		else
1803 		    --msg_col;
1804 	    }
1805 	    else
1806 #endif
1807 	    {
1808 		if (*s == '\r' || *s == '\n')
1809 		    msg_col = 0;
1810 		else
1811 		    ++msg_col;
1812 	    }
1813 	    ++s;
1814 	}
1815 	msg_didout = TRUE;	    /* assume that line is not empty */
1816 
1817 #ifdef WIN3264
1818 	if (!(silent_mode && p_verbose == 0))
1819 	    mch_settmode(TMODE_RAW);
1820 #endif
1821 	return;
1822     }
1823 
1824     did_wait_return = FALSE;
1825     while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1826     {
1827 	/*
1828 	 * The screen is scrolled up when:
1829 	 * - When outputting a newline in the last row
1830 	 * - when outputting a character in the last column of the last row
1831 	 *   (some terminals scroll automatically, some don't. To avoid
1832 	 *   problems we scroll ourselves)
1833 	 */
1834 	if (msg_row >= Rows - 1
1835 		&& (*s == '\n'
1836 		    || (
1837 #ifdef FEAT_RIGHTLEFT
1838 		    cmdmsg_rl
1839 		    ? (
1840 			msg_col <= 1
1841 			|| (*s == TAB && msg_col <= 7)
1842 # ifdef FEAT_MBYTE
1843 			|| (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1844 # endif
1845 		      )
1846 		    :
1847 #endif
1848 		      (msg_col + t_col >= Columns - 1
1849 		       || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1850 # ifdef FEAT_MBYTE
1851 		       || (has_mbyte && (*mb_ptr2cells)(s) > 1
1852 					    && msg_col + t_col >= Columns - 2)
1853 # endif
1854 		      ))))
1855 	{
1856 	    if (t_col > 0)
1857 	    {
1858 		/* output postponed text */
1859 		t_puts(t_col, t_s, s, attr);
1860 		t_col = 0;
1861 	    }
1862 
1863 	    /* When no more prompt an no more room, truncate here */
1864 	    if (msg_no_more && lines_left == 0)
1865 		break;
1866 #ifdef FEAT_GUI
1867 	    /* Remove the cursor before scrolling, ScreenLines[] is going to
1868 	     * become invalid. */
1869 	    if (gui.in_use)
1870 		gui_undraw_cursor();
1871 #endif
1872 	    /* scrolling up always works */
1873 	    screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
1874 
1875 	    if (!can_clear((char_u *)" "))
1876 	    {
1877 		/* Scrolling up doesn't result in the right background.  Set
1878 		 * the background here.  It's not efficient, but avoids that
1879 		 * we have to do it all over the code. */
1880 		screen_fill((int)Rows - 1, (int)Rows, 0,
1881 						   (int)Columns, ' ', ' ', 0);
1882 
1883 		/* Also clear the last char of the last but one line if it was
1884 		 * not cleared before to avoid a scroll-up. */
1885 		if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1]
1886 							       == (sattr_T)-1)
1887 		    screen_fill((int)Rows - 2, (int)Rows - 1,
1888 				 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
1889 	    }
1890 
1891 	    msg_row = Rows - 2;
1892 	    if (msg_col >= Columns)	/* can happen after screen resize */
1893 		msg_col = Columns - 1;
1894 
1895 	    ++msg_scrolled;
1896 	    need_wait_return = TRUE;	/* may need wait_return in main() */
1897 	    if (must_redraw < VALID)
1898 		must_redraw = VALID;
1899 	    redraw_cmdline = TRUE;
1900 	    if (cmdline_row > 0 && !exmode_active)
1901 		--cmdline_row;
1902 
1903 	    /*
1904 	     * if screen is completely filled wait for a character
1905 	     */
1906 	    if (p_more && --lines_left == 0 && State != HITRETURN
1907 					    && !msg_no_more && !exmode_active)
1908 	    {
1909 		oldState = State;
1910 		State = ASKMORE;
1911 #ifdef FEAT_MOUSE
1912 		setmouse();
1913 #endif
1914 		msg_moremsg(FALSE);
1915 		for (;;)
1916 		{
1917 		    /*
1918 		     * Get a typed character directly from the user.
1919 		     */
1920 		    c = get_keystroke();
1921 
1922 #if defined(FEAT_MENU) && defined(FEAT_GUI)
1923 		    if (c == K_MENU)
1924 		    {
1925 			int idx = get_menu_index(current_menu, ASKMORE);
1926 
1927 			/* Used a menu.  If it starts with CTRL-Y, it must
1928 			 * be a "Copy" for the clipboard.  Otherwise
1929 			 * assume that we end */
1930 			if (idx == MENU_INDEX_INVALID)
1931 			    continue;
1932 			c = *current_menu->strings[idx];
1933 			if (c != NUL && current_menu->strings[idx][1] != NUL)
1934 			    ins_typebuf(current_menu->strings[idx] + 1,
1935 				    current_menu->noremap[idx], 0, TRUE,
1936 				    current_menu->silent[idx]);
1937 		    }
1938 #endif
1939 
1940 		    switch (c)
1941 		    {
1942 		    case BS:
1943 		    case 'k':
1944 		    case K_UP:
1945 			if (!more_back_used)
1946 			{
1947 			    msg_moremsg(TRUE);
1948 			    continue;
1949 			}
1950 			more_back = 1;
1951 			lines_left = 1;
1952 			break;
1953 		    case CAR:		/* one extra line */
1954 		    case NL:
1955 		    case 'j':
1956 		    case K_DOWN:
1957 			lines_left = 1;
1958 			break;
1959 		    case ':':		/* start new command line */
1960 #ifdef FEAT_CON_DIALOG
1961 			if (!confirm_msg_used)
1962 #endif
1963 			{
1964 			    /* Since got_int is set all typeahead will be
1965 			     * flushed, but we want to keep this ':', remember
1966 			     * that in a special way. */
1967 			    typeahead_noflush(':');
1968 			    cmdline_row = Rows - 1;   /* put ':' on this line */
1969 			    skip_redraw = TRUE;	      /* skip redraw once */
1970 			    need_wait_return = FALSE; /* don't wait in main() */
1971 			}
1972 			/*FALLTHROUGH*/
1973 		    case 'q':		/* quit */
1974 		    case Ctrl_C:
1975 		    case ESC:
1976 #ifdef FEAT_CON_DIALOG
1977 			if (confirm_msg_used)
1978 			{
1979 			    /* Jump to the choices of the dialog. */
1980 			    s = confirm_msg_tail;
1981 			    lines_left = Rows - 1;
1982 			}
1983 			else
1984 #endif
1985 			{
1986 			    got_int = TRUE;
1987 			    quit_more = TRUE;
1988 			}
1989 			break;
1990 		    case 'u':		/* Up half a page */
1991 		    case K_PAGEUP:
1992 			if (!more_back_used)
1993 			{
1994 			    msg_moremsg(TRUE);
1995 			    continue;
1996 			}
1997 			more_back = Rows / 2;
1998 			/*FALLTHROUGH*/
1999 		    case 'd':		/* Down half a page */
2000 			lines_left = Rows / 2;
2001 			break;
2002 		    case 'b':		/* one page back */
2003 			if (!more_back_used)
2004 			{
2005 			    msg_moremsg(TRUE);
2006 			    continue;
2007 			}
2008 			more_back = Rows - 1;
2009 			/*FALLTHROUGH*/
2010 		    case ' ':		/* one extra page */
2011 		    case K_PAGEDOWN:
2012 		    case K_LEFTMOUSE:
2013 			lines_left = Rows - 1;
2014 			break;
2015 
2016 #ifdef FEAT_CLIPBOARD
2017 		    case Ctrl_Y:
2018 			/* Strange way to allow copying (yanking) a modeless
2019 			 * selection at the more prompt.  Use CTRL-Y,
2020 			 * because the same is used in Cmdline-mode and at the
2021 			 * hit-enter prompt.  However, scrolling one line up
2022 			 * might be expected... */
2023 			if (clip_star.state == SELECT_DONE)
2024 			    clip_copy_modeless_selection(TRUE);
2025 			continue;
2026 #endif
2027 		    default:		/* no valid response */
2028 			msg_moremsg(TRUE);
2029 			continue;
2030 		    }
2031 		    break;
2032 		}
2033 
2034 		/* clear the --more-- message */
2035 		screen_fill((int)Rows - 1, (int)Rows,
2036 						0, (int)Columns, ' ', ' ', 0);
2037 		State = oldState;
2038 #ifdef FEAT_MOUSE
2039 		setmouse();
2040 #endif
2041 		if (quit_more)
2042 		{
2043 		    msg_row = Rows - 1;
2044 		    msg_col = 0;
2045 		    return;	    /* the string is not displayed! */
2046 		}
2047 #ifdef FEAT_RIGHTLEFT
2048 		if (cmdmsg_rl)
2049 		    msg_col = Columns - 1;
2050 #endif
2051 	    }
2052 	}
2053 
2054 	if (t_col > 0
2055 		&& (vim_strchr((char_u *)"\n\r\b\t", *s) != NULL
2056 		    || *s == BELL
2057 		    || msg_col + t_col >= Columns
2058 #ifdef FEAT_MBYTE
2059 		    || (has_mbyte && (*mb_ptr2cells)(s) > 1
2060 					    && msg_col + t_col >= Columns - 1)
2061 #endif
2062 		    ))
2063 	{
2064 	    /* output any postponed text */
2065 	    t_puts(t_col, t_s, s, attr);
2066 	    t_col = 0;
2067 	}
2068 
2069 	if (*s == '\n')		    /* go to next line */
2070 	{
2071 	    msg_didout = FALSE;	    /* remember that line is empty */
2072 	    msg_col = 0;
2073 	    if (++msg_row >= Rows)  /* safety check */
2074 		msg_row = Rows - 1;
2075 	}
2076 	else if (*s == '\r')	    /* go to column 0 */
2077 	{
2078 	    msg_col = 0;
2079 	}
2080 	else if (*s == '\b')	    /* go to previous char */
2081 	{
2082 	    if (msg_col)
2083 		--msg_col;
2084 	}
2085 	else if (*s == TAB)	    /* translate into spaces */
2086 	{
2087 	    do
2088 		msg_screen_putchar(' ', attr);
2089 	    while (msg_col & 7);
2090 	}
2091 	else if (*s == BELL)	    /* beep (from ":sh") */
2092 	    vim_beep();
2093 	else
2094 	{
2095 #ifdef FEAT_MBYTE
2096 	    if (has_mbyte)
2097 	    {
2098 		cw = (*mb_ptr2cells)(s);
2099 		if (enc_utf8 && maxlen >= 0)
2100 		    /* avoid including composing chars after the end */
2101 		    l = utfc_ptr2len_check_len(s, (int)((str + maxlen) - s));
2102 		else
2103 		    l = (*mb_ptr2len_check)(s);
2104 	    }
2105 	    else
2106 	    {
2107 		cw = 1;
2108 		l = 1;
2109 	    }
2110 #endif
2111 	    /* When drawing from right to left or when a double-wide character
2112 	     * doesn't fit, draw a single character here.  Otherwise collect
2113 	     * characters and draw them all at once later. */
2114 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2115 	    if (
2116 # ifdef FEAT_RIGHTLEFT
2117 		    cmdmsg_rl
2118 #  ifdef FEAT_MBYTE
2119 		    ||
2120 #  endif
2121 # endif
2122 # ifdef FEAT_MBYTE
2123 		    (cw > 1 && msg_col + t_col >= Columns - 1)
2124 # endif
2125 		    )
2126 	    {
2127 # ifdef FEAT_MBYTE
2128 		if (l > 1)
2129 		    s = screen_puts_mbyte(s, l, attr) - 1;
2130 		else
2131 # endif
2132 		    msg_screen_putchar(*s, attr);
2133 	    }
2134 	    else
2135 #endif
2136 	    {
2137 		/* postpone this character until later */
2138 		if (t_col == 0)
2139 		    t_s = s;
2140 #ifdef FEAT_MBYTE
2141 		t_col += cw;
2142 		s += l - 1;
2143 #else
2144 		++t_col;
2145 #endif
2146 	    }
2147 	}
2148 	++s;
2149     }
2150 
2151     /* output any postponed text */
2152     if (t_col > 0)
2153 	t_puts(t_col, t_s, s, attr);
2154 
2155     msg_check();
2156 }
2157 
2158 /*
2159  * Output any postponed text for msg_puts_attr_len().
2160  */
2161     static void
2162 t_puts(t_col, t_s, s, attr)
2163     int		t_col;
2164     char_u	*t_s;
2165     char_u	*s;
2166     int		attr;
2167 {
2168     /* output postponed text */
2169     msg_didout = TRUE;		/* remember that line is not empty */
2170     screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
2171     msg_col += t_col;
2172 #ifdef FEAT_MBYTE
2173     /* If the string starts with a composing character don't increment the
2174      * column position for it. */
2175     if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2176 	--msg_col;
2177 #endif
2178     if (msg_col >= Columns)
2179     {
2180 	msg_col = 0;
2181 	++msg_row;
2182     }
2183 }
2184 
2185 
2186 /*
2187  * Returns TRUE when messages should be printed with mch_errmsg().
2188  * This is used when there is no valid screen, so we can see error messages.
2189  * If termcap is not active, we may be writing in an alternate console
2190  * window, cursor positioning may not work correctly (window size may be
2191  * different, e.g. for Win32 console) or we just don't know where the
2192  * cursor is.
2193  */
2194     int
2195 msg_use_printf()
2196 {
2197     return (!msg_check_screen()
2198 #if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2199 	    || !termcap_active
2200 #endif
2201 	    || (swapping_screen() && !termcap_active)
2202 	       );
2203 }
2204 
2205 #if defined(USE_MCH_ERRMSG) || defined(PROTO)
2206 
2207 #ifdef mch_errmsg
2208 # undef mch_errmsg
2209 #endif
2210 #ifdef mch_msg
2211 # undef mch_msg
2212 #endif
2213 
2214 /*
2215  * Give an error message.  To be used when the screen hasn't been initialized
2216  * yet.  When stderr can't be used, collect error messages until the GUI has
2217  * started and they can be displayed in a message box.
2218  */
2219     void
2220 mch_errmsg(str)
2221     char	*str;
2222 {
2223     int		len;
2224 
2225 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2226     /* On Unix use stderr if it's a tty.
2227      * When not going to start the GUI also use stderr.
2228      * On Mac, when started from Finder, stderr is the console. */
2229     if (
2230 # ifdef UNIX
2231 #  ifdef MACOS_X_UNIX
2232 	    (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2233 #  else
2234 	    isatty(2)
2235 #  endif
2236 #  ifdef FEAT_GUI
2237 	    ||
2238 #  endif
2239 # endif
2240 # ifdef FEAT_GUI
2241 	    !(gui.in_use || gui.starting)
2242 # endif
2243 	    )
2244     {
2245 	fprintf(stderr, "%s", str);
2246 	return;
2247     }
2248 #endif
2249 
2250     /* avoid a delay for a message that isn't there */
2251     emsg_on_display = FALSE;
2252 
2253     len = (int)STRLEN(str) + 1;
2254     if (error_ga.ga_growsize == 0)
2255     {
2256 	error_ga.ga_growsize = 80;
2257 	error_ga.ga_itemsize = 1;
2258     }
2259     if (ga_grow(&error_ga, len) == OK)
2260     {
2261 	mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2262 							  (char_u *)str, len);
2263 #ifdef UNIX
2264 	/* remove CR characters, they are displayed */
2265 	{
2266 	    char_u	*p;
2267 
2268 	    p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2269 	    for (;;)
2270 	    {
2271 		p = vim_strchr(p, '\r');
2272 		if (p == NULL)
2273 		    break;
2274 		*p = ' ';
2275 	    }
2276 	}
2277 #endif
2278 	--len;		/* don't count the NUL at the end */
2279 	error_ga.ga_len += len;
2280 	error_ga.ga_room -= len;
2281     }
2282 }
2283 
2284 /*
2285  * Give a message.  To be used when the screen hasn't been initialized yet.
2286  * When there is no tty, collect messages until the GUI has started and they
2287  * can be displayed in a message box.
2288  */
2289     void
2290 mch_msg(str)
2291     char	*str;
2292 {
2293 #if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2294     /* On Unix use stdout if we have a tty.  This allows "vim -h | more" and
2295      * uses mch_errmsg() when started from the desktop.
2296      * When not going to start the GUI also use stdout.
2297      * On Mac, when started from Finder, stderr is the console. */
2298     if (
2299 #  ifdef UNIX
2300 #   ifdef MACOS_X_UNIX
2301 	    (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2302 #   else
2303 	    isatty(2)
2304 #    endif
2305 #   ifdef FEAT_GUI
2306 	    ||
2307 #   endif
2308 #  endif
2309 #  ifdef FEAT_GUI
2310 	    !(gui.in_use || gui.starting)
2311 #  endif
2312 	    )
2313     {
2314 	printf("%s", str);
2315 	return;
2316     }
2317 # endif
2318     mch_errmsg(str);
2319 }
2320 #endif /* USE_MCH_ERRMSG */
2321 
2322 /*
2323  * Put a character on the screen at the current message position and advance
2324  * to the next position.  Only for printable ASCII!
2325  */
2326     static void
2327 msg_screen_putchar(c, attr)
2328     int		c;
2329     int		attr;
2330 {
2331     msg_didout = TRUE;		/* remember that line is not empty */
2332     screen_putchar(c, msg_row, msg_col, attr);
2333 #ifdef FEAT_RIGHTLEFT
2334     if (cmdmsg_rl)
2335     {
2336 	if (--msg_col == 0)
2337 	{
2338 	    msg_col = Columns;
2339 	    ++msg_row;
2340 	}
2341     }
2342     else
2343 #endif
2344     {
2345 	if (++msg_col >= Columns)
2346 	{
2347 	    msg_col = 0;
2348 	    ++msg_row;
2349 	}
2350     }
2351 }
2352 
2353     void
2354 msg_moremsg(full)
2355     int	    full;
2356 {
2357     int	    attr;
2358 
2359     attr = hl_attr(HLF_M);
2360     screen_puts((char_u *)_("-- More --"), (int)Rows - 1, 0, attr);
2361     if (full)
2362 	screen_puts(more_back_used
2363 	    ? (char_u *)_(" (RET/BS: line, SPACE/b: page, d/u: half page, q: quit)")
2364 	    : (char_u *)_(" (RET: line, SPACE: page, d: half page, q: quit)"),
2365 	    (int)Rows - 1, 10, attr);
2366 }
2367 
2368 /*
2369  * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2370  * exmode_active.
2371  */
2372     void
2373 repeat_message()
2374 {
2375     if (State == ASKMORE)
2376     {
2377 	msg_moremsg(TRUE);	/* display --more-- message again */
2378 	msg_row = Rows - 1;
2379     }
2380 #ifdef FEAT_CON_DIALOG
2381     else if (State == CONFIRM)
2382     {
2383 	display_confirm_msg();	/* display ":confirm" message again */
2384 	msg_row = Rows - 1;
2385     }
2386 #endif
2387     else if (State == EXTERNCMD)
2388     {
2389 	windgoto(msg_row, msg_col); /* put cursor back */
2390     }
2391     else if (State == HITRETURN || State == SETWSIZE)
2392     {
2393 	hit_return_msg();
2394 	msg_row = Rows - 1;
2395     }
2396 }
2397 
2398 /*
2399  * msg_check_screen - check if the screen is initialized.
2400  * Also check msg_row and msg_col, if they are too big it may cause a crash.
2401  * While starting the GUI the terminal codes will be set for the GUI, but the
2402  * output goes to the terminal.  Don't use the terminal codes then.
2403  */
2404     static int
2405 msg_check_screen()
2406 {
2407     if (!full_screen || !screen_valid(FALSE))
2408 	return FALSE;
2409 
2410     if (msg_row >= Rows)
2411 	msg_row = Rows - 1;
2412     if (msg_col >= Columns)
2413 	msg_col = Columns - 1;
2414     return TRUE;
2415 }
2416 
2417 /*
2418  * Clear from current message position to end of screen.
2419  * Skip this when ":silent" was used, no need to clear for redirection.
2420  */
2421     void
2422 msg_clr_eos()
2423 {
2424     if (msg_silent == 0)
2425 	msg_clr_eos_force();
2426 }
2427 
2428 /*
2429  * Clear from current message position to end of screen.
2430  * Note: msg_col is not updated, so we remember the end of the message
2431  * for msg_check().
2432  */
2433     void
2434 msg_clr_eos_force()
2435 {
2436     if (msg_use_printf())
2437     {
2438 	if (full_screen)	/* only when termcap codes are valid */
2439 	{
2440 	    if (*T_CD)
2441 		out_str(T_CD);	/* clear to end of display */
2442 	    else if (*T_CE)
2443 		out_str(T_CE);	/* clear to end of line */
2444 	}
2445     }
2446     else
2447     {
2448 #ifdef FEAT_RIGHTLEFT
2449 	if (cmdmsg_rl)
2450 	{
2451 	    screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2452 	    screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2453 	}
2454 	else
2455 #endif
2456 	{
2457 	    screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2458 								 ' ', ' ', 0);
2459 	    screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2460 	}
2461     }
2462 }
2463 
2464 /*
2465  * Clear the command line.
2466  */
2467     void
2468 msg_clr_cmdline()
2469 {
2470     msg_row = cmdline_row;
2471     msg_col = 0;
2472     msg_clr_eos_force();
2473 }
2474 
2475 /*
2476  * end putting a message on the screen
2477  * call wait_return if the message does not fit in the available space
2478  * return TRUE if wait_return not called.
2479  */
2480     int
2481 msg_end()
2482 {
2483     /*
2484      * if the string is larger than the window,
2485      * or the ruler option is set and we run into it,
2486      * we have to redraw the window.
2487      * Do not do this if we are abandoning the file or editing the command line.
2488      */
2489     if (!exiting && need_wait_return && !(State & CMDLINE))
2490     {
2491 	wait_return(FALSE);
2492 	return FALSE;
2493     }
2494     out_flush();
2495     return TRUE;
2496 }
2497 
2498 /*
2499  * If the written message runs into the shown command or ruler, we have to
2500  * wait for hit-return and redraw the window later.
2501  */
2502     void
2503 msg_check()
2504 {
2505     if (msg_row == Rows - 1 && msg_col >= sc_col)
2506     {
2507 	need_wait_return = TRUE;
2508 	redraw_cmdline = TRUE;
2509     }
2510 }
2511 
2512 /*
2513  * May write a string to the redirection file.
2514  * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2515  */
2516     static void
2517 redir_write(str, maxlen)
2518     char_u	*str;
2519     int		maxlen;
2520 {
2521     char_u	*s = str;
2522     static int	cur_col = 0;
2523 
2524     if ((redir_fd != NULL
2525 #ifdef FEAT_EVAL
2526 			  || redir_reg
2527 #endif
2528 				       ) && !redir_off)
2529     {
2530 	/* If the string doesn't start with CR or NL, go to msg_col */
2531 	if (*s != '\n' && *s != '\r')
2532 	{
2533 	    while (cur_col < msg_col)
2534 	    {
2535 #ifdef FEAT_EVAL
2536 		if (redir_reg)
2537 		    write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
2538 		else if (redir_fd)
2539 #endif
2540 		    fputs(" ", redir_fd);
2541 		++cur_col;
2542 	    }
2543 	}
2544 
2545 #ifdef FEAT_EVAL
2546 	if (redir_reg)
2547 	    write_reg_contents(redir_reg, s, maxlen, TRUE);
2548 #endif
2549 
2550 	/* Adjust the current column */
2551 	while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2552 	{
2553 #ifdef FEAT_EVAL
2554 	    if (!redir_reg && redir_fd != NULL)
2555 #endif
2556 		putc(*s, redir_fd);
2557 	    if (*s == '\r' || *s == '\n')
2558 		cur_col = 0;
2559 	    else if (*s == '\t')
2560 		cur_col += (8 - cur_col % 8);
2561 	    else
2562 		++cur_col;
2563 	    ++s;
2564 	}
2565 
2566 	if (msg_silent != 0)	/* should update msg_col */
2567 	    msg_col = cur_col;
2568     }
2569 }
2570 
2571 /*
2572  * Give a warning message (for searching).
2573  * Use 'w' highlighting and may repeat the message after redrawing
2574  */
2575     void
2576 give_warning(message, hl)
2577     char_u  *message;
2578     int	    hl;
2579 {
2580     /* Don't do this for ":silent". */
2581     if (msg_silent != 0)
2582 	return;
2583 
2584     /* Don't want a hit-enter prompt here. */
2585     ++no_wait_return;
2586 
2587 #ifdef FEAT_EVAL
2588     set_vim_var_string(VV_WARNINGMSG, message, -1);
2589 #endif
2590     vim_free(keep_msg);
2591     keep_msg = NULL;
2592     if (hl)
2593 	keep_msg_attr = hl_attr(HLF_W);
2594     else
2595 	keep_msg_attr = 0;
2596     if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
2597 	set_keep_msg(message);
2598     msg_didout = FALSE;	    /* overwrite this message */
2599     msg_nowait = TRUE;	    /* don't wait for this message */
2600     msg_col = 0;
2601 
2602     --no_wait_return;
2603 }
2604 
2605 /*
2606  * Advance msg cursor to column "col".
2607  */
2608     void
2609 msg_advance(col)
2610     int	    col;
2611 {
2612     if (msg_silent != 0)	/* nothing to advance to */
2613     {
2614 	msg_col = col;		/* for redirection, may fill it up later */
2615 	return;
2616     }
2617     if (col >= Columns)		/* not enough room */
2618 	col = Columns - 1;
2619     while (msg_col < col)
2620 	msg_putchar(' ');
2621 }
2622 
2623 #if defined(FEAT_CON_DIALOG) || defined(PROTO)
2624 /*
2625  * Used for "confirm()" function, and the :confirm command prefix.
2626  * Versions which haven't got flexible dialogs yet, and console
2627  * versions, get this generic handler which uses the command line.
2628  *
2629  * type  = one of:
2630  *	   VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
2631  * title = title string (can be NULL for default)
2632  * (neither used in console dialogs at the moment)
2633  *
2634  * Format of the "buttons" string:
2635  * "Button1Name\nButton2Name\nButton3Name"
2636  * The first button should normally be the default/accept
2637  * The second button should be the 'Cancel' button
2638  * Other buttons- use your imagination!
2639  * A '&' in a button name becomes a shortcut, so each '&' should be before a
2640  * different letter.
2641  */
2642 /* ARGSUSED */
2643     int
2644 do_dialog(type, title, message, buttons, dfltbutton, textfield)
2645     int		type;
2646     char_u	*title;
2647     char_u	*message;
2648     char_u	*buttons;
2649     int		dfltbutton;
2650     char_u	*textfield;	/* IObuff for inputdialog(), NULL otherwise */
2651 {
2652     int		oldState;
2653     int		retval = 0;
2654     char_u	*hotkeys;
2655     int		c;
2656     int		i;
2657 
2658 #ifndef NO_CONSOLE
2659     /* Don't output anything in silent mode ("ex -s") */
2660     if (silent_mode)
2661 	return dfltbutton;   /* return default option */
2662 #endif
2663 
2664 #ifdef FEAT_GUI_DIALOG
2665     /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
2666     if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
2667     {
2668 	c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
2669 								   textfield);
2670 	msg_end_prompt();
2671 
2672 	/* Flush output to avoid that further messages and redrawing is done
2673 	 * in the wrong order. */
2674 	out_flush();
2675 	gui_mch_update();
2676 
2677 	return c;
2678     }
2679 #endif
2680 
2681     oldState = State;
2682     State = CONFIRM;
2683 #ifdef FEAT_MOUSE
2684     setmouse();
2685 #endif
2686 
2687     /*
2688      * Since we wait for a keypress, don't make the
2689      * user press RETURN as well afterwards.
2690      */
2691     ++no_wait_return;
2692     hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
2693 
2694     if (hotkeys != NULL)
2695     {
2696 	for (;;)
2697 	{
2698 	    /* Get a typed character directly from the user. */
2699 	    c = get_keystroke();
2700 	    switch (c)
2701 	    {
2702 	    case CAR:		/* User accepts default option */
2703 	    case NL:
2704 		retval = dfltbutton;
2705 		break;
2706 	    case Ctrl_C:	/* User aborts/cancels */
2707 	    case ESC:
2708 		retval = 0;
2709 		break;
2710 	    default:		/* Could be a hotkey? */
2711 		if (c < 0)	/* special keys are ignored here */
2712 		    continue;
2713 		/* Make the character lowercase, as chars in "hotkeys" are. */
2714 		c = MB_TOLOWER(c);
2715 		retval = 1;
2716 		for (i = 0; hotkeys[i]; ++i)
2717 		{
2718 #ifdef FEAT_MBYTE
2719 		    if (has_mbyte)
2720 		    {
2721 			if ((*mb_ptr2char)(hotkeys + i) == c)
2722 			    break;
2723 			i += (*mb_ptr2len_check)(hotkeys + i) - 1;
2724 		    }
2725 		    else
2726 #endif
2727 			if (hotkeys[i] == c)
2728 			    break;
2729 		    ++retval;
2730 		}
2731 		if (hotkeys[i])
2732 		    break;
2733 		/* No hotkey match, so keep waiting */
2734 		continue;
2735 	    }
2736 	    break;
2737 	}
2738 
2739 	vim_free(hotkeys);
2740     }
2741 
2742     State = oldState;
2743 #ifdef FEAT_MOUSE
2744     setmouse();
2745 #endif
2746     --no_wait_return;
2747     msg_end_prompt();
2748 
2749     return retval;
2750 }
2751 
2752 static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
2753 
2754 /*
2755  * Copy one character from "*from" to "*to", taking care of multi-byte
2756  * characters.  Return the length of the character in bytes.
2757  */
2758     static int
2759 copy_char(from, to, lowercase)
2760     char_u	*from;
2761     char_u	*to;
2762     int		lowercase;	/* make character lower case */
2763 {
2764 #ifdef FEAT_MBYTE
2765     int		len;
2766     int		c;
2767 
2768     if (has_mbyte)
2769     {
2770 	if (lowercase)
2771 	{
2772 	    c = MB_TOLOWER((*mb_ptr2char)(from));
2773 	    return (*mb_char2bytes)(c, to);
2774 	}
2775 	else
2776 	{
2777 	    len = (*mb_ptr2len_check)(from);
2778 	    mch_memmove(to, from, (size_t)len);
2779 	    return len;
2780 	}
2781     }
2782     else
2783 #endif
2784     {
2785 	if (lowercase)
2786 	    *to = (char_u)TOLOWER_LOC(*from);
2787 	else
2788 	    *to = *from;
2789 	return 1;
2790     }
2791 }
2792 
2793 /*
2794  * Format the dialog string, and display it at the bottom of
2795  * the screen. Return a string of hotkey chars (if defined) for
2796  * each 'button'. If a button has no hotkey defined, the first character of
2797  * the button is used.
2798  * The hotkeys can be multi-byte characters, but without combining chars.
2799  *
2800  * Returns an allocated string with hotkeys, or NULL for error.
2801  */
2802     static char_u *
2803 msg_show_console_dialog(message, buttons, dfltbutton)
2804     char_u	*message;
2805     char_u	*buttons;
2806     int		dfltbutton;
2807 {
2808     int		len = 0;
2809 #ifdef FEAT_MBYTE
2810 # define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
2811 #else
2812 # define HOTK_LEN 1
2813 #endif
2814     int		lenhotkey = HOTK_LEN;	/* count first button */
2815     char_u	*hotk = NULL;
2816     char_u	*msgp = NULL;
2817     char_u	*hotkp = NULL;
2818     char_u	*r;
2819     int		copy;
2820 #define HAS_HOTKEY_LEN 30
2821     char_u	has_hotkey[HAS_HOTKEY_LEN];
2822     int		first_hotkey = FALSE;	/* first char of button is hotkey */
2823     int		idx;
2824 
2825     has_hotkey[0] = FALSE;
2826 
2827     /*
2828      * First loop: compute the size of memory to allocate.
2829      * Second loop: copy to the allocated memory.
2830      */
2831     for (copy = 0; copy <= 1; ++copy)
2832     {
2833 	r = buttons;
2834 	idx = 0;
2835 	while (*r)
2836 	{
2837 	    if (*r == DLG_BUTTON_SEP)
2838 	    {
2839 		if (copy)
2840 		{
2841 		    *msgp++ = ',';
2842 		    *msgp++ = ' ';	    /* '\n' -> ', ' */
2843 
2844 		    /* advance to next hotkey and set default hotkey */
2845 #ifdef FEAT_MBYTE
2846 		    if (has_mbyte)
2847 			hotkp += (*mb_ptr2len_check)(hotkp);
2848 		    else
2849 #endif
2850 			++hotkp;
2851 		    (void)copy_char(r + 1, hotkp, TRUE);
2852 		    if (dfltbutton)
2853 			--dfltbutton;
2854 
2855 		    /* If no hotkey is specified first char is used. */
2856 		    if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
2857 			first_hotkey = TRUE;
2858 		}
2859 		else
2860 		{
2861 		    len += 3;		    /* '\n' -> ', '; 'x' -> '(x)' */
2862 		    lenhotkey += HOTK_LEN;  /* each button needs a hotkey */
2863 		    if (idx < HAS_HOTKEY_LEN - 1)
2864 			has_hotkey[++idx] = FALSE;
2865 		}
2866 	    }
2867 	    else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
2868 	    {
2869 		if (*r == DLG_HOTKEY_CHAR)
2870 		    ++r;
2871 		first_hotkey = FALSE;
2872 		if (copy)
2873 		{
2874 		    if (*r == DLG_HOTKEY_CHAR)		/* '&&a' -> '&a' */
2875 			*msgp++ = *r;
2876 		    else
2877 		    {
2878 			/* '&a' -> '[a]' */
2879 			*msgp++ = (dfltbutton == 1) ? '[' : '(';
2880 			msgp += copy_char(r, msgp, FALSE);
2881 			*msgp++ = (dfltbutton == 1) ? ']' : ')';
2882 
2883 			/* redefine hotkey */
2884 			(void)copy_char(r, hotkp, TRUE);
2885 		    }
2886 		}
2887 		else
2888 		{
2889 		    ++len;	    /* '&a' -> '[a]' */
2890 		    if (idx < HAS_HOTKEY_LEN - 1)
2891 			has_hotkey[idx] = TRUE;
2892 		}
2893 	    }
2894 	    else
2895 	    {
2896 		/* everything else copy literally */
2897 		if (copy)
2898 		    msgp += copy_char(r, msgp, FALSE);
2899 	    }
2900 
2901 	    /* advance to the next character */
2902 #ifdef FEAT_MBYTE
2903 	    if (has_mbyte)
2904 		r += (*mb_ptr2len_check)(r);
2905 	    else
2906 #endif
2907 		++r;
2908 	}
2909 
2910 	if (copy)
2911 	{
2912 	    *msgp++ = ':';
2913 	    *msgp++ = ' ';
2914 	    *msgp = NUL;
2915 #ifdef FEAT_MBYTE
2916 	    if (has_mbyte)
2917 		hotkp += (*mb_ptr2len_check)(hotkp);
2918 	    else
2919 #endif
2920 		++hotkp;
2921 	    *hotkp = NUL;
2922 	}
2923 	else
2924 	{
2925 	    len += STRLEN(message)
2926 		    + 2			/* for the NL's */
2927 		    + STRLEN(buttons)
2928 		    + 3;		/* for the ": " and NUL */
2929 	    lenhotkey++;		/* for the NUL */
2930 
2931 	    /* If no hotkey is specified first char is used. */
2932 	    if (!has_hotkey[0])
2933 	    {
2934 		first_hotkey = TRUE;
2935 		len += 2;		/* "x" -> "[x]" */
2936 	    }
2937 
2938 	    /*
2939 	     * Now allocate and load the strings
2940 	     */
2941 	    vim_free(confirm_msg);
2942 	    confirm_msg = alloc(len);
2943 	    if (confirm_msg == NULL)
2944 		return NULL;
2945 	    *confirm_msg = NUL;
2946 	    hotk = alloc(lenhotkey);
2947 	    if (hotk == NULL)
2948 		return NULL;
2949 
2950 	    *confirm_msg = '\n';
2951 	    STRCPY(confirm_msg + 1, message);
2952 
2953 	    msgp = confirm_msg + 1 + STRLEN(message);
2954 	    hotkp = hotk;
2955 
2956 	    /* define first default hotkey */
2957 	    (void)copy_char(buttons, hotkp, TRUE);
2958 
2959 	    /* Remember where the choices start, displaying starts here when
2960 	     * "hotkp" typed at the more prompt. */
2961 	    confirm_msg_tail = msgp;
2962 	    *msgp++ = '\n';
2963 	}
2964     }
2965 
2966     display_confirm_msg();
2967     return hotk;
2968 }
2969 
2970 /*
2971  * Display the ":confirm" message.  Also called when screen resized.
2972  */
2973     void
2974 display_confirm_msg()
2975 {
2976     /* avoid that 'q' at the more prompt truncates the message here */
2977     ++confirm_msg_used;
2978     if (confirm_msg != NULL)
2979 	msg_puts_attr(confirm_msg, hl_attr(HLF_M));
2980     --confirm_msg_used;
2981 }
2982 
2983 #endif /* FEAT_CON_DIALOG */
2984 
2985 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
2986 
2987     int
2988 vim_dialog_yesno(type, title, message, dflt)
2989     int		type;
2990     char_u	*title;
2991     char_u	*message;
2992     int		dflt;
2993 {
2994     if (do_dialog(type,
2995 		title == NULL ? (char_u *)_("Question") : title,
2996 		message,
2997 		(char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
2998 	return VIM_YES;
2999     return VIM_NO;
3000 }
3001 
3002     int
3003 vim_dialog_yesnocancel(type, title, message, dflt)
3004     int		type;
3005     char_u	*title;
3006     char_u	*message;
3007     int		dflt;
3008 {
3009     switch (do_dialog(type,
3010 		title == NULL ? (char_u *)_("Question") : title,
3011 		message,
3012 		(char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3013     {
3014 	case 1: return VIM_YES;
3015 	case 2: return VIM_NO;
3016     }
3017     return VIM_CANCEL;
3018 }
3019 
3020     int
3021 vim_dialog_yesnoallcancel(type, title, message, dflt)
3022     int		type;
3023     char_u	*title;
3024     char_u	*message;
3025     int		dflt;
3026 {
3027     switch (do_dialog(type,
3028 		title == NULL ? (char_u *)"Question" : title,
3029 		message,
3030 		(char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3031 								  dflt, NULL))
3032     {
3033 	case 1: return VIM_YES;
3034 	case 2: return VIM_NO;
3035 	case 3: return VIM_ALL;
3036 	case 4: return VIM_DISCARDALL;
3037     }
3038     return VIM_CANCEL;
3039 }
3040 
3041 #endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3042 
3043 #if defined(FEAT_BROWSE) || defined(PROTO)
3044 /*
3045  * Generic browse function.  Calls gui_mch_browse() when possible.
3046  * Later this may pop-up a non-GUI file selector (external command?).
3047  */
3048     char_u *
3049 do_browse(saving, title, dflt, ext, initdir, filter, buf)
3050     int		saving;		/* write action */
3051     char_u	*title;		/* title for the window */
3052     char_u	*dflt;		/* default file name (may include directory) */
3053     char_u	*ext;		/* extension added */
3054     char_u	*initdir;	/* initial directory, NULL for current dir or
3055 				   when using path from "dflt" */
3056     char_u	*filter;	/* file name filter */
3057     buf_T	*buf;		/* buffer to read/write for */
3058 {
3059     char_u		*fname;
3060     static char_u	*last_dir = NULL;    /* last used directory */
3061     char_u		*tofree = NULL;
3062     int			save_browse = cmdmod.browse;
3063 
3064     /* Must turn off browse to avoid that autocommands will get the
3065      * flag too!  */
3066     cmdmod.browse = FALSE;
3067 
3068     if (title == NULL)
3069     {
3070 	if (saving)
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 (mch_isdir(p_bsdir))
3101 	    initdir = p_bsdir;
3102 	/* When saving or 'browsedir' is "buffer", use buffer fname */
3103 	else if ((saving || *p_bsdir == 'b')
3104 		&& buf != NULL && buf->b_ffname != NULL)
3105 	{
3106 	    if (dflt == NULL || *dflt == NUL)
3107 		dflt = gettail(curbuf->b_ffname);
3108 	    tofree = vim_strsave(curbuf->b_ffname);
3109 	    if (tofree != NULL)
3110 	    {
3111 		initdir = tofree;
3112 		*gettail(initdir) = NUL;
3113 	    }
3114 	}
3115 	/* When 'browsedir' is "last", use dir from last browse */
3116 	else if (*p_bsdir == 'l')
3117 	    initdir = last_dir;
3118 	/* When 'browsedir is "current", use current directory.  This is the
3119 	 * default already, leave initdir empty. */
3120     }
3121 
3122 # ifdef FEAT_GUI
3123     if (gui.in_use)		/* when this changes, also adjust f_has()! */
3124     {
3125 	if (filter == NULL
3126 #  ifdef FEAT_EVAL
3127 		&& (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3128 		&& (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3129 #  endif
3130 	)
3131 	    filter = BROWSE_FILTER_DEFAULT;
3132 	fname = gui_mch_browse(saving, title, dflt, ext, initdir, filter);
3133 
3134 	/* We hang around in the dialog for a while, the user might do some
3135 	 * things to our files.  The Win32 dialog allows deleting or renaming
3136 	 * a file, check timestamps. */
3137 	need_check_timestamps = TRUE;
3138 	did_check_timestamps = FALSE;
3139     }
3140     else
3141 # endif
3142     {
3143 	/* TODO: non-GUI file selector here */
3144 	EMSG(_("E338: Sorry, no file browser in console mode"));
3145 	fname = NULL;
3146     }
3147 
3148     /* keep the directory for next time */
3149     if (fname != NULL)
3150     {
3151 	vim_free(last_dir);
3152 	last_dir = vim_strsave(fname);
3153 	if (last_dir != NULL)
3154 	{
3155 	    *gettail(last_dir) = NUL;
3156 	    if (*last_dir == NUL)
3157 	    {
3158 		/* filename only returned, must be in current dir */
3159 		vim_free(last_dir);
3160 		last_dir = alloc(MAXPATHL);
3161 		if (last_dir != NULL)
3162 		    mch_dirname(last_dir, MAXPATHL);
3163 	    }
3164 	}
3165     }
3166 
3167     vim_free(tofree);
3168     cmdmod.browse = save_browse;
3169 
3170     return fname;
3171 }
3172 #endif
3173