blob: 2e637a7904b33a6e9c9e7307ea5cd90465a7662b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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
Bram Moolenaara7241f52008-06-24 20:39:31 +000018#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
19# include <math.h>
20#endif
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int other_sourcing_name(void);
23static char_u *get_emsg_source(void);
24static char_u *get_emsg_lnum(void);
25static void add_msg_hist(char_u *s, int len, int attr);
26static void hit_return_msg(void);
27static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010029static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
32static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
33static void msg_scroll_up(void);
34static void inc_msg_scrolled(void);
35static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
36static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
37static void msg_puts_printf(char_u *str, int maxlen);
38static int do_more_prompt(int typed_char);
39static void msg_screen_putchar(int c, int attr);
40static int msg_check_screen(void);
41static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static int confirm_msg_used = FALSE; /* displaying confirm_msg */
45static char_u *confirm_msg = NULL; /* ":confirm" message */
46static char_u *confirm_msg_tail; /* tail of confirm_msg */
47#endif
48
49struct msg_hist
50{
51 struct msg_hist *next;
52 char_u *msg;
53 int attr;
54};
55
56static struct msg_hist *first_msg_hist = NULL;
57static struct msg_hist *last_msg_hist = NULL;
58static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020060static FILE *verbose_fd = NULL;
61static int verbose_did_open = FALSE;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063/*
64 * When writing messages to the screen, there are many different situations.
65 * A number of variables is used to remember the current state:
66 * msg_didany TRUE when messages were written since the last time the
67 * user reacted to a prompt.
68 * Reset: After hitting a key for the hit-return prompt,
69 * hitting <CR> for the command line or input().
70 * Set: When any message is written to the screen.
71 * msg_didout TRUE when something was written to the current line.
72 * Reset: When advancing to the next line, when the current
73 * text can be overwritten.
74 * Set: When any message is written to the screen.
75 * msg_nowait No extra delay for the last drawn message.
76 * Used in normal_cmd() before the mode message is drawn.
77 * emsg_on_display There was an error message recently. Indicates that there
78 * should be a delay before redrawing.
79 * msg_scroll The next message should not overwrite the current one.
80 * msg_scrolled How many lines the screen has been scrolled (because of
81 * messages). Used in update_screen() to scroll the screen
82 * back. Incremented each time the screen scrolls a line.
83 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
84 * writes something without scrolling should not make
85 * need_wait_return to be set. This is a hack to make ":ts"
86 * work without an extra prompt.
87 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010088 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 * need_wait_return TRUE when the hit-return prompt is needed.
90 * Reset: After giving the hit-return prompt, when the user
91 * has answered some other prompt.
92 * Set: When the ruler or typeahead display is overwritten,
93 * scrolling the screen for some message.
94 * keep_msg Message to be displayed after redrawing the screen, in
95 * main_loop().
96 * This is an allocated string or NULL when not used.
97 */
98
99/*
100 * msg(s) - displays the string 's' on the status line
101 * When terminal not initialized (yet) mch_errmsg(..) is used.
102 * return TRUE if wait_return not called
103 */
104 int
105msg(s)
106 char_u *s;
107{
108 return msg_attr_keep(s, 0, FALSE);
109}
110
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000112 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000113/*
114 * Like msg() but keep it silent when 'verbosefile' is set.
115 */
116 int
117verb_msg(s)
118 char_u *s;
119{
120 int n;
121
122 verbose_enter();
123 n = msg_attr_keep(s, 0, FALSE);
124 verbose_leave();
125
126 return n;
127}
128#endif
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 int
131msg_attr(s, attr)
132 char_u *s;
133 int attr;
134{
135 return msg_attr_keep(s, attr, FALSE);
136}
137
138 int
139msg_attr_keep(s, attr, keep)
140 char_u *s;
141 int attr;
142 int keep; /* TRUE: set keep_msg if it doesn't scroll */
143{
144 static int entered = 0;
145 int retval;
146 char_u *buf = NULL;
147
148#ifdef FEAT_EVAL
149 if (attr == 0)
150 set_vim_var_string(VV_STATUSMSG, s, -1);
151#endif
152
153 /*
154 * It is possible that displaying a messages causes a problem (e.g.,
155 * when redrawing the window), which causes another message, etc.. To
156 * break this loop, limit the recursiveness to 3 levels.
157 */
158 if (entered >= 3)
159 return TRUE;
160 ++entered;
161
162 /* Add message to history (unless it's a repeated kept message or a
163 * truncated message) */
164 if (s != keep_msg
165 || (*s != '<'
166 && last_msg_hist != NULL
167 && last_msg_hist->msg != NULL
168 && STRCMP(s, last_msg_hist->msg)))
169 add_msg_hist(s, -1, attr);
170
171 /* When displaying keep_msg, don't let msg_start() free it, caller must do
172 * that. */
173 if (s == keep_msg)
174 keep_msg = NULL;
175
176 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000177 msg_start();
178 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (buf != NULL)
180 s = buf;
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 msg_outtrans_attr(s, attr);
183 msg_clr_eos();
184 retval = msg_end();
185
186 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
187 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000188 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190 vim_free(buf);
191 --entered;
192 return retval;
193}
194
195/*
196 * Truncate a string such that it can be printed without causing a scroll.
197 * Returns an allocated string or NULL when no truncating is done.
198 */
199 char_u *
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000200msg_strtrunc(s, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u *s;
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000202 int force; /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 char_u *buf = NULL;
205 int len;
206 int room;
207
208 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000209 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
210 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000213 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000214 /* Use all the columns. */
215 room = (int)(Rows - msg_row) * Columns - 1;
216 else
217 /* Use up to 'showcmd' column. */
218 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (len > room && room > 0)
220 {
221#ifdef FEAT_MBYTE
222 if (enc_utf8)
223 /* may have up to 18 bytes per cell (6 per char, up to two
224 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100225 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 else if (enc_dbcs == DBCS_JPNU)
227 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100228 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 else
230#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100231 len = room + 2;
232 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100234 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 }
236 }
237 return buf;
238}
239
240/*
241 * Truncate a string "s" to "buf" with cell width "room".
242 * "s" and "buf" may be equal.
243 */
244 void
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100245trunc_string(s, buf, room, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 char_u *s;
247 char_u *buf;
248 int room;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100249 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 int half;
252 int len;
253 int e;
254 int i;
255 int n;
256
257 room -= 3;
258 half = room / 2;
259 len = 0;
260
261 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100262 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {
264 if (s[e] == NUL)
265 {
266 /* text fits without truncating! */
267 buf[e] = NUL;
268 return;
269 }
270 n = ptr2cells(s + e);
271 if (len + n >= half)
272 break;
273 len += n;
274 buf[e] = s[e];
275#ifdef FEAT_MBYTE
276 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000277 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100279 if (++e == buflen)
280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 buf[e] = s[e];
282 }
283#endif
284 }
285
286 /* Last part: End of the string. */
287 i = e;
288#ifdef FEAT_MBYTE
289 if (enc_dbcs != 0)
290 {
291 /* For DBCS going backwards in a string is slow, but
292 * computing the cell width isn't too slow: go forward
293 * until the rest fits. */
294 n = vim_strsize(s + i);
295 while (len + n > room)
296 {
297 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000298 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 }
300 }
301 else if (enc_utf8)
302 {
303 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000304 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 for (;;)
306 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307 do
308 half = half - (*mb_head_off)(s, s + half - 1) - 1;
309 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 n = ptr2cells(s + half);
311 if (len + n > room)
312 break;
313 len += n;
314 i = half;
315 }
316 }
317 else
318#endif
319 {
320 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
321 len += n;
322 }
323
324 /* Set the middle and copy the last part. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100325 if (e + 3 < buflen)
326 {
327 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaar38f5f952012-01-26 13:01:59 +0100328 len = (int)STRLEN(s + i) + 1;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100329 if (len >= buflen - e - 3)
330 len = buflen - e - 3 - 1;
331 mch_memmove(buf + e + 3, s + i, len);
332 buf[e + 3 + len - 1] = NUL;
333 }
334 else
335 {
Bram Moolenaar33c1b192012-01-23 20:48:40 +0100336 buf[e - 1] = NUL; /* make sure it is truncated */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338}
339
340/*
341 * Automatic prototype generation does not understand this function.
342 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
343 * shorter than IOSIZE!!!
344 */
345#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000347int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100350# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353smsg(char_u *s, ...)
354{
355 va_list arglist;
356
357 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000358 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 va_end(arglist);
360 return msg(IObuff);
361}
362
363 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100364# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367smsg_attr(int attr, char_u *s, ...)
368{
369 va_list arglist;
370
371 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000372 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 va_end(arglist);
374 return msg_attr(IObuff, attr);
375}
376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#endif
378
379/*
380 * Remember the last sourcing name/lnum used in an error message, so that it
381 * isn't printed each time when it didn't change.
382 */
383static int last_sourcing_lnum = 0;
384static char_u *last_sourcing_name = NULL;
385
386/*
387 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
388 * for the next error message;
389 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000390 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391reset_last_sourcing()
392{
393 vim_free(last_sourcing_name);
394 last_sourcing_name = NULL;
395 last_sourcing_lnum = 0;
396}
397
398/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000399 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
400 */
401 static int
402other_sourcing_name()
403{
404 if (sourcing_name != NULL)
405 {
406 if (last_sourcing_name != NULL)
407 return STRCMP(sourcing_name, last_sourcing_name) != 0;
408 return TRUE;
409 }
410 return FALSE;
411}
412
413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 * Get the message about the source, as used for an error message.
415 * Returns an allocated string with room for one more character.
416 * Returns NULL when no message is to be given.
417 */
418 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420{
421 char_u *Buf, *p;
422
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000423 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {
425 p = (char_u *)_("Error detected while processing %s:");
426 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
427 if (Buf != NULL)
428 sprintf((char *)Buf, (char *)p, sourcing_name);
429 return Buf;
430 }
431 return NULL;
432}
433
434/*
435 * Get the message about the source lnum, as used for an error message.
436 * Returns an allocated string with room for one more character.
437 * Returns NULL when no message is to be given.
438 */
439 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000440get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
442 char_u *Buf, *p;
443
444 /* lnum is 0 when executing a command from the command line
445 * argument, we don't want a line number then */
446 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000447 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 && sourcing_lnum != 0)
449 {
450 p = (char_u *)_("line %4ld:");
451 Buf = alloc((unsigned)(STRLEN(p) + 20));
452 if (Buf != NULL)
453 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
454 return Buf;
455 }
456 return NULL;
457}
458
459/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000460 * Display name and line number for the source of an error.
461 * Remember the file name and line number, so that for the next error the info
462 * is only displayed if it changed.
463 */
464 void
465msg_source(attr)
466 int attr;
467{
468 char_u *p;
469
470 ++no_wait_return;
471 p = get_emsg_source();
472 if (p != NULL)
473 {
474 msg_attr(p, attr);
475 vim_free(p);
476 }
477 p = get_emsg_lnum();
478 if (p != NULL)
479 {
480 msg_attr(p, hl_attr(HLF_N));
481 vim_free(p);
482 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
483 }
484
485 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000486 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000487 {
488 vim_free(last_sourcing_name);
489 if (sourcing_name == NULL)
490 last_sourcing_name = NULL;
491 else
492 last_sourcing_name = vim_strsave(sourcing_name);
493 }
494 --no_wait_return;
495}
496
497/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000498 * Return TRUE if not giving error messages right now:
499 * If "emsg_off" is set: no error messages at the moment.
500 * If "msg" is in 'debug': do error message but without side effects.
501 * If "emsg_skip" is set: never do error messages.
502 */
503 int
504emsg_not_now()
505{
506 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
507 && vim_strchr(p_debug, 't') == NULL)
508#ifdef FEAT_EVAL
509 || emsg_skip > 0
510#endif
511 )
512 return TRUE;
513 return FALSE;
514}
515
516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * emsg() - display an error message
518 *
519 * Rings the bell, if appropriate, and calls message() to do the real work
520 * When terminal not initialized (yet) mch_errmsg(..) is used.
521 *
522 * return TRUE if wait_return not called
523 */
524 int
525emsg(s)
526 char_u *s;
527{
528 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 char_u *p;
530#ifdef FEAT_EVAL
531 int ignore = FALSE;
532 int severe;
533#endif
534
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100535 /* Skip this if not giving error messages at the moment. */
536 if (emsg_not_now())
537 return TRUE;
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000540 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000543 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
544 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 */
546#ifdef FEAT_EVAL
547 severe = emsg_severe;
548 emsg_severe = FALSE;
549#endif
550
Bram Moolenaar57657d82006-04-21 22:12:41 +0000551 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {
553#ifdef FEAT_EVAL
554 /*
555 * Cause a throw of an error exception if appropriate. Don't display
556 * the error message in this case. (If no matching catch clause will
557 * be found, the message will be displayed later on.) "ignore" is set
558 * when the message should be ignored completely (used for the
559 * interrupt message).
560 */
561 if (cause_errthrow(s, severe, &ignore) == TRUE)
562 {
563 if (!ignore)
564 did_emsg = TRUE;
565 return TRUE;
566 }
567
568 /* set "v:errmsg", also when using ":silent! cmd" */
569 set_vim_var_string(VV_ERRMSG, s, -1);
570#endif
571
572 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000573 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 * But do write it to the redirection file.
575 */
576 if (emsg_silent != 0)
577 {
578 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000579 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (p != NULL)
581 {
582 STRCAT(p, "\n");
583 redir_write(p, -1);
584 vim_free(p);
585 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000586 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (p != NULL)
588 {
589 STRCAT(p, "\n");
590 redir_write(p, -1);
591 vim_free(p);
592 }
593 redir_write(s, -1);
594 return TRUE;
595 }
596
597 /* Reset msg_silent, an error causes messages to be switched back on. */
598 msg_silent = 0;
599 cmd_silent = FALSE;
600
601 if (global_busy) /* break :global command */
602 ++global_busy;
603
604 if (p_eb)
605 beep_flush(); /* also includes flush_buffers() */
606 else
607 flush_buffers(FALSE); /* flush internal buffers */
608 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610
611 emsg_on_display = TRUE; /* remember there is an error message */
612 ++msg_scroll; /* don't overwrite a previous message */
613 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000614 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 need_wait_return = TRUE; /* needed in case emsg() is called after
616 * wait_return has reset need_wait_return
617 * and a redraw is expected because
618 * msg_scrolled is non-zero */
619
620 /*
621 * Display name and line number for the source of the error.
622 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000623 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /*
626 * Display the error message itself.
627 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000628 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 return msg_attr(s, attr);
630}
631
632/*
633 * Print an error message with one "%s" and one string argument.
634 */
635 int
636emsg2(s, a1)
637 char_u *s, *a1;
638{
639 return emsg3(s, a1, NULL);
640}
641
Bram Moolenaarea424162005-06-16 21:51:00 +0000642/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaardf177f62005-02-22 08:39:57 +0000644 void
645emsg_invreg(name)
646 int name;
647{
648 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
649}
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651/*
652 * Like msg(), but truncate to a single line if p_shm contains 't', or when
653 * "force" is TRUE. This truncates in another way as for normal messages.
654 * Careful: The string may be changed by msg_may_trunc()!
655 * Returns a pointer to the printed message, if wait_return() not called.
656 */
657 char_u *
658msg_trunc_attr(s, force, attr)
659 char_u *s;
660 int force;
661 int attr;
662{
663 int n;
664
665 /* Add message to history before truncating */
666 add_msg_hist(s, -1, attr);
667
668 s = msg_may_trunc(force, s);
669
670 msg_hist_off = TRUE;
671 n = msg_attr(s, attr);
672 msg_hist_off = FALSE;
673
674 if (n)
675 return s;
676 return NULL;
677}
678
679/*
680 * Check if message "s" should be truncated at the start (for filenames).
681 * Return a pointer to where the truncated message starts.
682 * Note: May change the message by replacing a character with '<'.
683 */
684 char_u *
685msg_may_trunc(force, s)
686 int force;
687 char_u *s;
688{
689 int n;
690 int room;
691
692 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
693 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
694 && (n = (int)STRLEN(s) - room) > 0)
695 {
696#ifdef FEAT_MBYTE
697 if (has_mbyte)
698 {
699 int size = vim_strsize(s);
700
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000701 /* There may be room anyway when there are multibyte chars. */
702 if (size <= room)
703 return s;
704
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 for (n = 0; size >= room; )
706 {
707 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000708 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710 --n;
711 }
712#endif
713 s += n;
714 *s = '<';
715 }
716 return s;
717}
718
719 static void
720add_msg_hist(s, len, attr)
721 char_u *s;
722 int len; /* -1 for undetermined length */
723 int attr;
724{
725 struct msg_hist *p;
726
727 if (msg_hist_off || msg_silent != 0)
728 return;
729
730 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000731 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000732 (void)delete_first_msg();
733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 /* allocate an entry and add the message at the end of the history */
735 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
736 if (p != NULL)
737 {
738 if (len < 0)
739 len = (int)STRLEN(s);
740 /* remove leading and trailing newlines */
741 while (len > 0 && *s == '\n')
742 {
743 ++s;
744 --len;
745 }
746 while (len > 0 && s[len - 1] == '\n')
747 --len;
748 p->msg = vim_strnsave(s, len);
749 p->next = NULL;
750 p->attr = attr;
751 if (last_msg_hist != NULL)
752 last_msg_hist->next = p;
753 last_msg_hist = p;
754 if (first_msg_hist == NULL)
755 first_msg_hist = last_msg_hist;
756 ++msg_hist_len;
757 }
758}
759
760/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000761 * Delete the first (oldest) message from the history.
762 * Returns FAIL if there are no messages.
763 */
764 int
765delete_first_msg()
766{
767 struct msg_hist *p;
768
769 if (msg_hist_len <= 0)
770 return FAIL;
771 p = first_msg_hist;
772 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000773 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200774 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000775 vim_free(p->msg);
776 vim_free(p);
777 --msg_hist_len;
778 return OK;
779}
780
781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * ":messages" command.
783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 void
785ex_messages(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000786 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 struct msg_hist *p;
789 char_u *s;
790
791 msg_hist_off = TRUE;
792
793 s = mch_getenv((char_u *)"LANG");
794 if (s != NULL && *s != NUL)
795 msg_attr((char_u *)
796 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
797 hl_attr(HLF_T));
798
Bram Moolenaar5c2e0f22007-09-13 20:05:18 +0000799 for (p = first_msg_hist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (p->msg != NULL)
801 msg_attr(p->msg, p->attr);
802
803 msg_hist_off = FALSE;
804}
805
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000806#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807/*
808 * Call this after prompting the user. This will avoid a hit-return message
809 * and a delay.
810 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000811 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812msg_end_prompt()
813{
814 need_wait_return = FALSE;
815 emsg_on_display = FALSE;
816 cmdline_row = msg_row;
817 msg_col = 0;
818 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100819 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821#endif
822
823/*
824 * wait for the user to hit a key (normally a return)
825 * if 'redraw' is TRUE, clear and redraw the screen
826 * if 'redraw' is FALSE, just redraw the screen
827 * if 'redraw' is -1, don't redraw at all
828 */
829 void
830wait_return(redraw)
831 int redraw;
832{
833 int c;
834 int oldState;
835 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100837 int save_Recording;
838 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
840 if (redraw == TRUE)
841 must_redraw = CLEAR;
842
843 /* If using ":silent cmd", don't wait for a return. Also don't set
844 * need_wait_return to do it later. */
845 if (msg_silent != 0)
846 return;
847
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100848 /*
849 * When inside vgetc(), we can't wait for a typed character at all.
850 * With the global command (and some others) we only need one return at
851 * the end. Adjust cmdline_row to avoid the next message overwriting the
852 * last one.
853 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000854 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100856 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (no_wait_return)
858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (!exmode_active)
860 cmdline_row = msg_row;
861 return;
862 }
863
864 redir_off = TRUE; /* don't redirect this message */
865 oldState = State;
866 if (quit_more)
867 {
868 c = CAR; /* just pretend CR was hit */
869 quit_more = FALSE;
870 got_int = FALSE;
871 }
872 else if (exmode_active)
873 {
874 MSG_PUTS(" "); /* make sure the cursor is on the right line */
875 c = CAR; /* no need for a return in ex mode */
876 got_int = FALSE;
877 }
878 else
879 {
880 /* Make sure the hit-return prompt is on screen when 'guioptions' was
881 * just changed. */
882 screenalloc(FALSE);
883
884 State = HITRETURN;
885#ifdef FEAT_MOUSE
886 setmouse();
887#endif
888#ifdef USE_ON_FLY_SCROLL
889 dont_scroll = TRUE; /* disallow scrolling here */
890#endif
Bram Moolenaarec38d692013-04-24 15:12:32 +0200891 /* Avoid the sequence that the user types ":" at the hit-return prompt
892 * to start an Ex command, but the file-changed dialog gets in the
893 * way. */
894 if (need_check_timestamps)
895 check_timestamps(FALSE);
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 hit_return_msg();
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 do
900 {
901 /* Remember "got_int", if it is set vgetc() probably returns a
902 * CTRL-C, but we need to loop then. */
903 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000904
905 /* Don't do mappings here, we put the character back in the
906 * typeahead buffer. */
907 ++no_mapping;
908 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100909
910 /* Temporarily disable Recording. If Recording is active, the
911 * character will be recorded later, since it will be added to the
912 * typebuf after the loop */
913 save_Recording = Recording;
914 save_scriptout = scriptout;
915 Recording = FALSE;
916 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000918 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000920 --no_mapping;
921 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100922 Recording = save_Recording;
923 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000924
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#ifdef FEAT_CLIPBOARD
926 /* Strange way to allow copying (yanking) a modeless selection at
927 * the hit-enter prompt. Use CTRL-Y, because the same is used in
928 * Cmdline-mode and it's harmless when there is no selection. */
929 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
930 {
931 clip_copy_modeless_selection(TRUE);
932 c = K_IGNORE;
933 }
934#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000935
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000936 /*
937 * Allow scrolling back in the messages.
938 * Also accept scroll-down commands when messages fill the screen,
939 * to avoid that typing one 'j' too many makes the messages
940 * disappear.
941 */
942 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000943 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100944 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
945 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000946 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100947 if (msg_scrolled > Rows)
948 /* scroll back to show older messages */
949 do_more_prompt(c);
950 else
951 {
952 msg_didout = FALSE;
953 c = K_IGNORE;
954 msg_col =
955#ifdef FEAT_RIGHTLEFT
956 cmdmsg_rl ? Columns - 1 :
957#endif
958 0;
959 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000960 if (quit_more)
961 {
962 c = CAR; /* just pretend CR was hit */
963 quit_more = FALSE;
964 got_int = FALSE;
965 }
Bram Moolenaarb0912962013-08-09 20:38:26 +0200966 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000967 {
968 c = K_IGNORE;
969 hit_return_msg();
970 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000971 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000972 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100973 && (c == 'j' || c == 'd' || c == 'f'
974 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000975 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 } while ((had_got_int && c == Ctrl_C)
978 || c == K_IGNORE
979#ifdef FEAT_GUI
980 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
981#endif
982#ifdef FEAT_MOUSE
983 || c == K_LEFTDRAG || c == K_LEFTRELEASE
984 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
985 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200986 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 || c == K_MOUSEDOWN || c == K_MOUSEUP
988 || (!mouse_has(MOUSE_RETURN)
989 && mouse_row < msg_row
990 && (c == K_LEFTMOUSE
991 || c == K_MIDDLEMOUSE
992 || c == K_RIGHTMOUSE
993 || c == K_X1MOUSE
994 || c == K_X2MOUSE))
995#endif
996 );
997 ui_breakcheck();
998#ifdef FEAT_MOUSE
999 /*
1000 * Avoid that the mouse-up event causes visual mode to start.
1001 */
1002 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1003 || c == K_X1MOUSE || c == K_X2MOUSE)
1004 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1005 else
1006#endif
1007 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1008 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001009 /* Put the character back in the typeahead buffer. Don't use the
1010 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001011 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001013 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 redir_off = FALSE;
1017
1018 /*
1019 * If the user hits ':', '?' or '/' we get a command line from the next
1020 * line.
1021 */
1022 if (c == ':' || c == '?' || c == '/')
1023 {
1024 if (!exmode_active)
1025 cmdline_row = msg_row;
1026 skip_redraw = TRUE; /* skip redraw once */
1027 do_redraw = FALSE;
1028 }
1029
1030 /*
1031 * If the window size changed set_shellsize() will redraw the screen.
1032 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1033 * typed.
1034 */
1035 tmpState = State;
1036 State = oldState; /* restore State before set_shellsize */
1037#ifdef FEAT_MOUSE
1038 setmouse();
1039#endif
1040 msg_check();
1041
1042#if defined(UNIX) || defined(VMS)
1043 /*
1044 * When switching screens, we need to output an extra newline on exit.
1045 */
1046 if (swapping_screen() && !termcap_active)
1047 newline_on_exit = TRUE;
1048#endif
1049
1050 need_wait_return = FALSE;
1051 did_wait_return = TRUE;
1052 emsg_on_display = FALSE; /* can delete error message now */
1053 lines_left = -1; /* reset lines_left at next msg_start() */
1054 reset_last_sourcing();
1055 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1056 (Rows - cmdline_row - 1) * Columns + sc_col)
1057 {
1058 vim_free(keep_msg);
1059 keep_msg = NULL; /* don't redisplay message, it's too long */
1060 }
1061
1062 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1063 {
1064 starttermcap(); /* start termcap before redrawing */
1065 shell_resized();
1066 }
1067 else if (!skip_redraw
1068 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1069 {
1070 starttermcap(); /* start termcap before redrawing */
1071 redraw_later(VALID);
1072 }
1073}
1074
1075/*
1076 * Write the hit-return prompt.
1077 */
1078 static void
1079hit_return_msg()
1080{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001081 int save_p_more = p_more;
1082
1083 p_more = FALSE; /* don't want see this message when scrolling back */
1084 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 msg_putchar('\n');
1086 if (got_int)
1087 MSG_PUTS(_("Interrupt: "));
1088
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001089 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (!msg_use_printf())
1091 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001092 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093}
1094
1095/*
1096 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1097 */
1098 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001099set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001101 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 vim_free(keep_msg);
1104 if (s != NULL && msg_silent == 0)
1105 keep_msg = vim_strsave(s);
1106 else
1107 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001108 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001109 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110}
1111
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001112#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1113/*
1114 * If there currently is a message being displayed, set "keep_msg" to it, so
1115 * that it will be displayed again after redraw.
1116 */
1117 void
1118set_keep_msg_from_hist()
1119{
1120 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1121 && (State & NORMAL))
1122 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1123}
1124#endif
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126/*
1127 * Prepare for outputting characters in the command line.
1128 */
1129 void
1130msg_start()
1131{
1132 int did_return = FALSE;
1133
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001134 if (!msg_silent)
1135 {
1136 vim_free(keep_msg);
1137 keep_msg = NULL; /* don't display old message now */
1138 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001139
1140#ifdef FEAT_EVAL
1141 if (need_clr_eos)
1142 {
1143 /* Halfway an ":echo" command and getting an (error) message: clear
1144 * any text from the command. */
1145 need_clr_eos = FALSE;
1146 msg_clr_eos();
1147 }
1148#endif
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (!msg_scroll && full_screen) /* overwrite last message */
1151 {
1152 msg_row = cmdline_row;
1153 msg_col =
1154#ifdef FEAT_RIGHTLEFT
1155 cmdmsg_rl ? Columns - 1 :
1156#endif
1157 0;
1158 }
1159 else if (msg_didout) /* start message on next line */
1160 {
1161 msg_putchar('\n');
1162 did_return = TRUE;
1163 if (exmode_active != EXMODE_NORMAL)
1164 cmdline_row = msg_row;
1165 }
1166 if (!msg_didany || lines_left < 0)
1167 msg_starthere();
1168 if (msg_silent == 0)
1169 {
1170 msg_didout = FALSE; /* no output on current line yet */
1171 cursor_off();
1172 }
1173
1174 /* when redirecting, may need to start a new line. */
1175 if (!did_return)
1176 redir_write((char_u *)"\n", -1);
1177}
1178
1179/*
1180 * Note that the current msg position is where messages start.
1181 */
1182 void
1183msg_starthere()
1184{
1185 lines_left = cmdline_row;
1186 msg_didany = FALSE;
1187}
1188
1189 void
1190msg_putchar(c)
1191 int c;
1192{
1193 msg_putchar_attr(c, 0);
1194}
1195
1196 void
1197msg_putchar_attr(c, attr)
1198 int c;
1199 int attr;
1200{
1201#ifdef FEAT_MBYTE
1202 char_u buf[MB_MAXBYTES + 1];
1203#else
1204 char_u buf[4];
1205#endif
1206
1207 if (IS_SPECIAL(c))
1208 {
1209 buf[0] = K_SPECIAL;
1210 buf[1] = K_SECOND(c);
1211 buf[2] = K_THIRD(c);
1212 buf[3] = NUL;
1213 }
1214 else
1215 {
1216#ifdef FEAT_MBYTE
1217 buf[(*mb_char2bytes)(c, buf)] = NUL;
1218#else
1219 buf[0] = c;
1220 buf[1] = NUL;
1221#endif
1222 }
1223 msg_puts_attr(buf, attr);
1224}
1225
1226 void
1227msg_outnum(n)
1228 long n;
1229{
1230 char_u buf[20];
1231
1232 sprintf((char *)buf, "%ld", n);
1233 msg_puts(buf);
1234}
1235
1236 void
1237msg_home_replace(fname)
1238 char_u *fname;
1239{
1240 msg_home_replace_attr(fname, 0);
1241}
1242
1243#if defined(FEAT_FIND_ID) || defined(PROTO)
1244 void
1245msg_home_replace_hl(fname)
1246 char_u *fname;
1247{
1248 msg_home_replace_attr(fname, hl_attr(HLF_D));
1249}
1250#endif
1251
1252 static void
1253msg_home_replace_attr(fname, attr)
1254 char_u *fname;
1255 int attr;
1256{
1257 char_u *name;
1258
1259 name = home_replace_save(NULL, fname);
1260 if (name != NULL)
1261 msg_outtrans_attr(name, attr);
1262 vim_free(name);
1263}
1264
1265/*
1266 * Output 'len' characters in 'str' (including NULs) with translation
1267 * if 'len' is -1, output upto a NUL character.
1268 * Use attributes 'attr'.
1269 * Return the number of characters it takes on the screen.
1270 */
1271 int
1272msg_outtrans(str)
1273 char_u *str;
1274{
1275 return msg_outtrans_attr(str, 0);
1276}
1277
1278 int
1279msg_outtrans_attr(str, attr)
1280 char_u *str;
1281 int attr;
1282{
1283 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1284}
1285
1286 int
1287msg_outtrans_len(str, len)
1288 char_u *str;
1289 int len;
1290{
1291 return msg_outtrans_len_attr(str, len, 0);
1292}
1293
1294/*
1295 * Output one character at "p". Return pointer to the next character.
1296 * Handles multi-byte characters.
1297 */
1298 char_u *
1299msg_outtrans_one(p, attr)
1300 char_u *p;
1301 int attr;
1302{
1303#ifdef FEAT_MBYTE
1304 int l;
1305
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001306 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
1308 msg_outtrans_len_attr(p, l, attr);
1309 return p + l;
1310 }
1311#endif
1312 msg_puts_attr(transchar_byte(*p), attr);
1313 return p + 1;
1314}
1315
1316 int
1317msg_outtrans_len_attr(msgstr, len, attr)
1318 char_u *msgstr;
1319 int len;
1320 int attr;
1321{
1322 int retval = 0;
1323 char_u *str = msgstr;
1324 char_u *plain_start = msgstr;
1325 char_u *s;
1326#ifdef FEAT_MBYTE
1327 int mb_l;
1328 int c;
1329#endif
1330
1331 /* if MSG_HIST flag set, add message to history */
1332 if (attr & MSG_HIST)
1333 {
1334 add_msg_hist(str, len, attr);
1335 attr &= ~MSG_HIST;
1336 }
1337
1338#ifdef FEAT_MBYTE
1339 /* If the string starts with a composing character first draw a space on
1340 * which the composing char can be drawn. */
1341 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1342 msg_puts_attr((char_u *)" ", attr);
1343#endif
1344
1345 /*
1346 * Go over the string. Special characters are translated and printed.
1347 * Normal characters are printed several at a time.
1348 */
1349 while (--len >= 0)
1350 {
1351#ifdef FEAT_MBYTE
1352 if (enc_utf8)
1353 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001354 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001356 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 else
1358 mb_l = 1;
1359 if (has_mbyte && mb_l > 1)
1360 {
1361 c = (*mb_ptr2char)(str);
1362 if (vim_isprintc(c))
1363 /* printable multi-byte char: count the cells. */
1364 retval += (*mb_ptr2cells)(str);
1365 else
1366 {
1367 /* unprintable multi-byte char: print the printable chars so
1368 * far and the translation of the unprintable char. */
1369 if (str > plain_start)
1370 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1371 attr);
1372 plain_start = str + mb_l;
1373 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1374 retval += char2cells(c);
1375 }
1376 len -= mb_l - 1;
1377 str += mb_l;
1378 }
1379 else
1380#endif
1381 {
1382 s = transchar_byte(*str);
1383 if (s[1] != NUL)
1384 {
1385 /* unprintable char: print the printable chars so far and the
1386 * translation of the unprintable char. */
1387 if (str > plain_start)
1388 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1389 attr);
1390 plain_start = str + 1;
1391 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001392 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001394 else
1395 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ++str;
1397 }
1398 }
1399
1400 if (str > plain_start)
1401 /* print the printable chars at the end */
1402 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1403
1404 return retval;
1405}
1406
1407#if defined(FEAT_QUICKFIX) || defined(PROTO)
1408 void
1409msg_make(arg)
1410 char_u *arg;
1411{
1412 int i;
1413 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1414
1415 arg = skipwhite(arg);
1416 for (i = 5; *arg && i >= 0; --i)
1417 if (*arg++ != str[i])
1418 break;
1419 if (i < 0)
1420 {
1421 msg_putchar('\n');
1422 for (i = 0; rs[i]; ++i)
1423 msg_putchar(rs[i] - 3);
1424 }
1425}
1426#endif
1427
1428/*
1429 * Output the string 'str' upto a NUL character.
1430 * Return the number of characters it takes on the screen.
1431 *
1432 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1433 * following character and shown as <F1>, <S-Up> etc. Any other character
1434 * which is not printable shown in <> form.
1435 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1436 * If a character is displayed in one of these special ways, is also
1437 * highlighted (its highlight name is '8' in the p_hl variable).
1438 * Otherwise characters are not highlighted.
1439 * This function is used to show mappings, where we want to see how to type
1440 * the character/string -- webb
1441 */
1442 int
1443msg_outtrans_special(strstart, from)
1444 char_u *strstart;
1445 int from; /* TRUE for lhs of a mapping */
1446{
1447 char_u *str = strstart;
1448 int retval = 0;
1449 char_u *string;
1450 int attr;
1451 int len;
1452
1453 attr = hl_attr(HLF_8);
1454 while (*str != NUL)
1455 {
1456 /* Leading and trailing spaces need to be displayed in <> form. */
1457 if ((str == strstart || str[1] == NUL) && *str == ' ')
1458 {
1459 string = (char_u *)"<Space>";
1460 ++str;
1461 }
1462 else
1463 string = str2special(&str, from);
1464 len = vim_strsize(string);
1465 /* Highlight special keys */
1466 msg_puts_attr(string, len > 1
1467#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001468 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#endif
1470 ? attr : 0);
1471 retval += len;
1472 }
1473 return retval;
1474}
1475
Bram Moolenaarbd743252010-10-20 21:23:33 +02001476#if defined(FEAT_EVAL) || defined(PROTO)
1477/*
1478 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1479 * strings, in an allocated string.
1480 */
1481 char_u *
1482str2special_save(str, is_lhs)
1483 char_u *str;
1484 int is_lhs; /* TRUE for lhs, FALSE for rhs */
1485{
1486 garray_T ga;
1487 char_u *p = str;
1488
1489 ga_init2(&ga, 1, 40);
1490 while (*p != NUL)
1491 ga_concat(&ga, str2special(&p, is_lhs));
1492 ga_append(&ga, NUL);
1493 return (char_u *)ga.ga_data;
1494}
1495#endif
1496
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497/*
1498 * Return the printable string for the key codes at "*sp".
1499 * Used for translating the lhs or rhs of a mapping to printable chars.
1500 * Advances "sp" to the next code.
1501 */
1502 char_u *
1503str2special(sp, from)
1504 char_u **sp;
1505 int from; /* TRUE for lhs of mapping */
1506{
1507 int c;
1508 static char_u buf[7];
1509 char_u *str = *sp;
1510 int modifiers = 0;
1511 int special = FALSE;
1512
1513#ifdef FEAT_MBYTE
1514 if (has_mbyte)
1515 {
1516 char_u *p;
1517
1518 /* Try to un-escape a multi-byte character. Return the un-escaped
1519 * string if it is a multi-byte character. */
1520 p = mb_unescape(sp);
1521 if (p != NULL)
1522 return p;
1523 }
1524#endif
1525
1526 c = *str;
1527 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1528 {
1529 if (str[1] == KS_MODIFIER)
1530 {
1531 modifiers = str[2];
1532 str += 3;
1533 c = *str;
1534 }
1535 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1536 {
1537 c = TO_SPECIAL(str[1], str[2]);
1538 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001539 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 c = NUL;
1541 }
1542 if (IS_SPECIAL(c) || modifiers) /* special key */
1543 special = TRUE;
1544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001547 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001549 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001550
1551 /* For multi-byte characters check for an illegal byte. */
1552 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1553 {
1554 transchar_nonprint(buf, c);
1555 *sp = str + 1;
1556 return buf;
1557 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001558 /* Since 'special' is TRUE the multi-byte character 'c' will be
1559 * processed by get_special_key_name() */
1560 c = (*mb_ptr2char)(str);
1561 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001565 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1568 * Use <Space> only for lhs of a mapping. */
1569 if (special || char2cells(c) > 1 || (from && c == ' '))
1570 return get_special_key_name(c, modifiers);
1571 buf[0] = c;
1572 buf[1] = NUL;
1573 return buf;
1574}
1575
1576/*
1577 * Translate a key sequence into special key names.
1578 */
1579 void
1580str2specialbuf(sp, buf, len)
1581 char_u *sp;
1582 char_u *buf;
1583 int len;
1584{
1585 char_u *s;
1586
1587 *buf = NUL;
1588 while (*sp)
1589 {
1590 s = str2special(&sp, FALSE);
1591 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1592 STRCAT(buf, s);
1593 }
1594}
1595
1596/*
1597 * print line for :print or :list command
1598 */
1599 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001600msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001602 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 int c;
1605 int col = 0;
1606 int n_extra = 0;
1607 int c_extra = 0;
1608 char_u *p_extra = NULL; /* init to make SASC shut up */
1609 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001610 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 char_u *trail = NULL;
1612#ifdef FEAT_MBYTE
1613 int l;
1614 char_u buf[MB_MAXBYTES + 1];
1615#endif
1616
Bram Moolenaardf177f62005-02-22 08:39:57 +00001617 if (curwin->w_p_list)
1618 list = TRUE;
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001621 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 trail = s + STRLEN(s);
1624 while (trail > s && vim_iswhite(trail[-1]))
1625 --trail;
1626 }
1627
1628 /* output a space for an empty line, otherwise the line will be
1629 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001630 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 msg_putchar(' ');
1632
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001633 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001635 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 {
1637 --n_extra;
1638 if (c_extra)
1639 c = c_extra;
1640 else
1641 c = *p_extra++;
1642 }
1643#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001644 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001647 if (lcs_nbsp != NUL && list
1648 && (mb_ptr2char(s) == 160
1649 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001650 {
1651 mb_char2bytes(lcs_nbsp, buf);
1652 buf[(*mb_ptr2len)(buf)] = NUL;
1653 }
1654 else
1655 {
1656 mch_memmove(buf, s, (size_t)l);
1657 buf[l] = NUL;
1658 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001659 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 s += l;
1661 continue;
1662 }
1663#endif
1664 else
1665 {
1666 attr = 0;
1667 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001668 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
1670 /* tab amount depends on current column */
1671 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001672 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 c = ' ';
1675 c_extra = ' ';
1676 }
1677 else
1678 {
1679 c = lcs_tab1;
1680 c_extra = lcs_tab2;
1681 attr = hl_attr(HLF_8);
1682 }
1683 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001684 else if (c == 160 && list && lcs_nbsp != NUL)
1685 {
1686 c = lcs_nbsp;
1687 attr = hl_attr(HLF_8);
1688 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001689 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 p_extra = (char_u *)"";
1692 c_extra = NUL;
1693 n_extra = 1;
1694 c = lcs_eol;
1695 attr = hl_attr(HLF_AT);
1696 --s;
1697 }
1698 else if (c != NUL && (n = byte2cells(c)) > 1)
1699 {
1700 n_extra = n - 1;
1701 p_extra = transchar_byte(c);
1702 c_extra = NUL;
1703 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001704 /* Use special coloring to be able to distinguish <hex> from
1705 * the same in plain text. */
1706 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708 else if (c == ' ' && trail != NULL && s > trail)
1709 {
1710 c = lcs_trail;
1711 attr = hl_attr(HLF_8);
1712 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001713 else if (c == ' ' && list && lcs_space != NUL)
1714 {
1715 c = lcs_space;
1716 attr = hl_attr(HLF_8);
1717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719
1720 if (c == NUL)
1721 break;
1722
1723 msg_putchar_attr(c, attr);
1724 col++;
1725 }
1726 msg_clr_eos();
1727}
1728
1729#ifdef FEAT_MBYTE
1730/*
1731 * Use screen_puts() to output one multi-byte character.
1732 * Return the pointer "s" advanced to the next character.
1733 */
1734 static char_u *
1735screen_puts_mbyte(s, l, attr)
1736 char_u *s;
1737 int l;
1738 int attr;
1739{
1740 int cw;
1741
1742 msg_didout = TRUE; /* remember that line is not empty */
1743 cw = (*mb_ptr2cells)(s);
1744 if (cw > 1 && (
1745#ifdef FEAT_RIGHTLEFT
1746 cmdmsg_rl ? msg_col <= 1 :
1747#endif
1748 msg_col == Columns - 1))
1749 {
1750 /* Doesn't fit, print a highlighted '>' to fill it up. */
1751 msg_screen_putchar('>', hl_attr(HLF_AT));
1752 return s;
1753 }
1754
1755 screen_puts_len(s, l, msg_row, msg_col, attr);
1756#ifdef FEAT_RIGHTLEFT
1757 if (cmdmsg_rl)
1758 {
1759 msg_col -= cw;
1760 if (msg_col == 0)
1761 {
1762 msg_col = Columns;
1763 ++msg_row;
1764 }
1765 }
1766 else
1767#endif
1768 {
1769 msg_col += cw;
1770 if (msg_col >= Columns)
1771 {
1772 msg_col = 0;
1773 ++msg_row;
1774 }
1775 }
1776 return s + l;
1777}
1778#endif
1779
1780/*
1781 * Output a string to the screen at position msg_row, msg_col.
1782 * Update msg_row and msg_col for the next message.
1783 */
1784 void
1785msg_puts(s)
1786 char_u *s;
1787{
1788 msg_puts_attr(s, 0);
1789}
1790
1791 void
1792msg_puts_title(s)
1793 char_u *s;
1794{
1795 msg_puts_attr(s, hl_attr(HLF_T));
1796}
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798/*
1799 * Show a message in such a way that it always fits in the line. Cut out a
1800 * part in the middle and replace it with "..." when necessary.
1801 * Does not handle multi-byte characters!
1802 */
1803 void
1804msg_puts_long_attr(longstr, attr)
1805 char_u *longstr;
1806 int attr;
1807{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001808 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809}
1810
1811 void
1812msg_puts_long_len_attr(longstr, len, attr)
1813 char_u *longstr;
1814 int len;
1815 int attr;
1816{
1817 int slen = len;
1818 int room;
1819
1820 room = Columns - msg_col;
1821 if (len > room && room >= 20)
1822 {
1823 slen = (room - 3) / 2;
1824 msg_outtrans_len_attr(longstr, slen, attr);
1825 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1826 }
1827 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1828}
1829
1830/*
1831 * Basic function for writing a message with highlight attributes.
1832 */
1833 void
1834msg_puts_attr(s, attr)
1835 char_u *s;
1836 int attr;
1837{
1838 msg_puts_attr_len(s, -1, attr);
1839}
1840
1841/*
1842 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1843 * When "maxlen" is -1 there is no maximum length.
1844 * When "maxlen" is >= 0 the message is not put in the history.
1845 */
1846 static void
1847msg_puts_attr_len(str, maxlen, attr)
1848 char_u *str;
1849 int maxlen;
1850 int attr;
1851{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 /*
1853 * If redirection is on, also write to the redirection file.
1854 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001855 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857 /*
1858 * Don't print anything when using ":silent cmd".
1859 */
1860 if (msg_silent != 0)
1861 return;
1862
1863 /* if MSG_HIST flag set, add message to history */
1864 if ((attr & MSG_HIST) && maxlen < 0)
1865 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001866 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 attr &= ~MSG_HIST;
1868 }
1869
1870 /*
1871 * When writing something to the screen after it has scrolled, requires a
1872 * wait-return prompt later. Needed when scrolling, resetting
1873 * need_wait_return after some prompt, and then outputting something
1874 * without scrolling
1875 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001876 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 need_wait_return = TRUE;
1878 msg_didany = TRUE; /* remember that something was outputted */
1879
1880 /*
1881 * If there is no valid screen, use fprintf so we can see error messages.
1882 * If termcap is not active, we may be writing in an alternate console
1883 * window, cursor positioning may not work correctly (window size may be
1884 * different, e.g. for Win32 console) or we just don't know where the
1885 * cursor is.
1886 */
1887 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001888 msg_puts_printf(str, maxlen);
1889 else
1890 msg_puts_display(str, maxlen, attr, FALSE);
1891}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001893/*
1894 * The display part of msg_puts_attr_len().
1895 * May be called recursively to display scroll-back text.
1896 */
1897 static void
1898msg_puts_display(str, maxlen, attr, recurse)
1899 char_u *str;
1900 int maxlen;
1901 int attr;
1902 int recurse;
1903{
1904 char_u *s = str;
1905 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1906 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1907#ifdef FEAT_MBYTE
1908 int l;
1909 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001911 char_u *sb_str = str;
1912 int sb_col = msg_col;
1913 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001914 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001917 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001920 * We are at the end of the screen line when:
1921 * - When outputting a newline.
1922 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001924 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_RIGHTLEFT
1926 cmdmsg_rl
1927 ? (
1928 msg_col <= 1
1929 || (*s == TAB && msg_col <= 7)
1930# ifdef FEAT_MBYTE
1931 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1932# endif
1933 )
1934 :
1935#endif
1936 (msg_col + t_col >= Columns - 1
1937 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1938# ifdef FEAT_MBYTE
1939 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1940 && msg_col + t_col >= Columns - 2)
1941# endif
1942 ))))
1943 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001944 /*
1945 * The screen is scrolled up when at the last row (some terminals
1946 * scroll automatically, some don't. To avoid problems we scroll
1947 * ourselves).
1948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001951 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001953 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (msg_no_more && lines_left == 0)
1955 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001957 /* Scroll the screen up one line. */
1958 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 msg_row = Rows - 2;
1961 if (msg_col >= Columns) /* can happen after screen resize */
1962 msg_col = Columns - 1;
1963
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001964 /* Display char in last column before showing more-prompt. */
1965 if (*s >= ' '
1966#ifdef FEAT_RIGHTLEFT
1967 && !cmdmsg_rl
1968#endif
1969 )
1970 {
1971#ifdef FEAT_MBYTE
1972 if (has_mbyte)
1973 {
1974 if (enc_utf8 && maxlen >= 0)
1975 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001976 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001978 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001979 s = screen_puts_mbyte(s, l, attr);
1980 }
1981 else
1982#endif
1983 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001984 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001985 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001986 else
1987 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001988
1989 if (p_more)
1990 /* store text for scrolling back */
1991 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1992
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001993 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001994 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (must_redraw < VALID)
1996 must_redraw = VALID;
1997 redraw_cmdline = TRUE;
1998 if (cmdline_row > 0 && !exmode_active)
1999 --cmdline_row;
2000
2001 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002002 * If screen is completely filled and 'more' is set then wait
2003 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002005 if (lines_left > 0)
2006 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002007 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 && !msg_no_more && !exmode_active)
2009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002011 if (do_more_prompt(NUL))
2012 s = confirm_msg_tail;
2013#else
2014 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#endif
2016 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002017 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002019
2020 /* When we displayed a char in last column need to check if there
2021 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002022 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002023 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002026 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 || msg_col + t_col >= Columns
2028#ifdef FEAT_MBYTE
2029 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2030 && msg_col + t_col >= Columns - 1)
2031#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002032 ;
2033 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2034 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002036 t_puts(&t_col, t_s, s, attr);
2037
2038 if (wrap && p_more && !recurse)
2039 /* store text for scrolling back */
2040 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 if (*s == '\n') /* go to next line */
2043 {
2044 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002045#ifdef FEAT_RIGHTLEFT
2046 if (cmdmsg_rl)
2047 msg_col = Columns - 1;
2048 else
2049#endif
2050 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (++msg_row >= Rows) /* safety check */
2052 msg_row = Rows - 1;
2053 }
2054 else if (*s == '\r') /* go to column 0 */
2055 {
2056 msg_col = 0;
2057 }
2058 else if (*s == '\b') /* go to previous char */
2059 {
2060 if (msg_col)
2061 --msg_col;
2062 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002063 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 do
2066 msg_screen_putchar(' ', attr);
2067 while (msg_col & 7);
2068 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002069 else if (*s == BELL) /* beep (from ":sh") */
2070 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 else
2072 {
2073#ifdef FEAT_MBYTE
2074 if (has_mbyte)
2075 {
2076 cw = (*mb_ptr2cells)(s);
2077 if (enc_utf8 && maxlen >= 0)
2078 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002079 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002081 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 else
2084 {
2085 cw = 1;
2086 l = 1;
2087 }
2088#endif
2089 /* When drawing from right to left or when a double-wide character
2090 * doesn't fit, draw a single character here. Otherwise collect
2091 * characters and draw them all at once later. */
2092#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2093 if (
2094# ifdef FEAT_RIGHTLEFT
2095 cmdmsg_rl
2096# ifdef FEAT_MBYTE
2097 ||
2098# endif
2099# endif
2100# ifdef FEAT_MBYTE
2101 (cw > 1 && msg_col + t_col >= Columns - 1)
2102# endif
2103 )
2104 {
2105# ifdef FEAT_MBYTE
2106 if (l > 1)
2107 s = screen_puts_mbyte(s, l, attr) - 1;
2108 else
2109# endif
2110 msg_screen_putchar(*s, attr);
2111 }
2112 else
2113#endif
2114 {
2115 /* postpone this character until later */
2116 if (t_col == 0)
2117 t_s = s;
2118#ifdef FEAT_MBYTE
2119 t_col += cw;
2120 s += l - 1;
2121#else
2122 ++t_col;
2123#endif
2124 }
2125 }
2126 ++s;
2127 }
2128
2129 /* output any postponed text */
2130 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002131 t_puts(&t_col, t_s, s, attr);
2132 if (p_more && !recurse)
2133 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 msg_check();
2136}
2137
2138/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002139 * Scroll the screen up one line for displaying the next message line.
2140 */
2141 static void
2142msg_scroll_up()
2143{
2144#ifdef FEAT_GUI
2145 /* Remove the cursor before scrolling, ScreenLines[] is going
2146 * to become invalid. */
2147 if (gui.in_use)
2148 gui_undraw_cursor();
2149#endif
2150 /* scrolling up always works */
2151 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2152
2153 if (!can_clear((char_u *)" "))
2154 {
2155 /* Scrolling up doesn't result in the right background. Set the
2156 * background here. It's not efficient, but avoids that we have to do
2157 * it all over the code. */
2158 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2159
2160 /* Also clear the last char of the last but one line if it was not
2161 * cleared before to avoid a scroll-up. */
2162 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2163 screen_fill((int)Rows - 2, (int)Rows - 1,
2164 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2165 }
2166}
2167
2168/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002169 * Increment "msg_scrolled".
2170 */
2171 static void
2172inc_msg_scrolled()
2173{
2174#ifdef FEAT_EVAL
2175 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2176 {
2177 char_u *p = sourcing_name;
2178 char_u *tofree = NULL;
2179 int len;
2180
2181 /* v:scrollstart is empty, set it to the script/function name and line
2182 * number */
2183 if (p == NULL)
2184 p = (char_u *)_("Unknown");
2185 else
2186 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002187 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002188 tofree = alloc(len);
2189 if (tofree != NULL)
2190 {
2191 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2192 p, (long)sourcing_lnum);
2193 p = tofree;
2194 }
2195 }
2196 set_vim_var_string(VV_SCROLLSTART, p, -1);
2197 vim_free(tofree);
2198 }
2199#endif
2200 ++msg_scrolled;
2201}
2202
2203/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002204 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2205 * store the displayed text and remember where screen lines start.
2206 */
2207typedef struct msgchunk_S msgchunk_T;
2208struct msgchunk_S
2209{
2210 msgchunk_T *sb_next;
2211 msgchunk_T *sb_prev;
2212 char sb_eol; /* TRUE when line ends after this text */
2213 int sb_msg_col; /* column in which text starts */
2214 int sb_attr; /* text attributes */
2215 char_u sb_text[1]; /* text to be displayed, actually longer */
2216};
2217
2218static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2219
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002220static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2221static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002222
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002223static int do_clear_sb_text = FALSE; /* clear text on next msg */
2224
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002225/*
2226 * Store part of a printed message for displaying when scrolling back.
2227 */
2228 static void
2229store_sb_text(sb_str, s, attr, sb_col, finish)
2230 char_u **sb_str; /* start of string */
2231 char_u *s; /* just after string */
2232 int attr;
2233 int *sb_col;
2234 int finish; /* line ends */
2235{
2236 msgchunk_T *mp;
2237
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002238 if (do_clear_sb_text)
2239 {
2240 clear_sb_text();
2241 do_clear_sb_text = FALSE;
2242 }
2243
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002244 if (s > *sb_str)
2245 {
2246 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2247 if (mp != NULL)
2248 {
2249 mp->sb_eol = finish;
2250 mp->sb_msg_col = *sb_col;
2251 mp->sb_attr = attr;
2252 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2253
2254 if (last_msgchunk == NULL)
2255 {
2256 last_msgchunk = mp;
2257 mp->sb_prev = NULL;
2258 }
2259 else
2260 {
2261 mp->sb_prev = last_msgchunk;
2262 last_msgchunk->sb_next = mp;
2263 last_msgchunk = mp;
2264 }
2265 mp->sb_next = NULL;
2266 }
2267 }
2268 else if (finish && last_msgchunk != NULL)
2269 last_msgchunk->sb_eol = TRUE;
2270
2271 *sb_str = s;
2272 *sb_col = 0;
2273}
2274
2275/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002276 * Finished showing messages, clear the scroll-back text on the next message.
2277 */
2278 void
2279may_clear_sb_text()
2280{
2281 do_clear_sb_text = TRUE;
2282}
2283
2284/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002285 * Clear any text remembered for scrolling back.
2286 * Called when redrawing the screen.
2287 */
2288 void
2289clear_sb_text()
2290{
2291 msgchunk_T *mp;
2292
2293 while (last_msgchunk != NULL)
2294 {
2295 mp = last_msgchunk->sb_prev;
2296 vim_free(last_msgchunk);
2297 last_msgchunk = mp;
2298 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002299}
2300
2301/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002302 * "g<" command.
2303 */
2304 void
2305show_sb_text()
2306{
2307 msgchunk_T *mp;
2308
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002309 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002310 * weird, typing a command without output results in one line. */
2311 mp = msg_sb_start(last_msgchunk);
2312 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002313 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002314 else
2315 {
2316 do_more_prompt('G');
2317 wait_return(FALSE);
2318 }
2319}
2320
2321/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002322 * Move to the start of screen line in already displayed text.
2323 */
2324 static msgchunk_T *
2325msg_sb_start(mps)
2326 msgchunk_T *mps;
2327{
2328 msgchunk_T *mp = mps;
2329
2330 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2331 mp = mp->sb_prev;
2332 return mp;
2333}
2334
2335/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002336 * Mark the last message chunk as finishing the line.
2337 */
2338 void
2339msg_sb_eol()
2340{
2341 if (last_msgchunk != NULL)
2342 last_msgchunk->sb_eol = TRUE;
2343}
2344
2345/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002346 * Display a screen line from previously displayed text at row "row".
2347 * Returns a pointer to the text for the next line (can be NULL).
2348 */
2349 static msgchunk_T *
2350disp_sb_line(row, smp)
2351 int row;
2352 msgchunk_T *smp;
2353{
2354 msgchunk_T *mp = smp;
2355 char_u *p;
2356
2357 for (;;)
2358 {
2359 msg_row = row;
2360 msg_col = mp->sb_msg_col;
2361 p = mp->sb_text;
2362 if (*p == '\n') /* don't display the line break */
2363 ++p;
2364 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2365 if (mp->sb_eol || mp->sb_next == NULL)
2366 break;
2367 mp = mp->sb_next;
2368 }
2369 return mp->sb_next;
2370}
2371
2372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * Output any postponed text for msg_puts_attr_len().
2374 */
2375 static void
2376t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002377 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 char_u *t_s;
2379 char_u *s;
2380 int attr;
2381{
2382 /* output postponed text */
2383 msg_didout = TRUE; /* remember that line is not empty */
2384 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002385 msg_col += *t_col;
2386 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#ifdef FEAT_MBYTE
2388 /* If the string starts with a composing character don't increment the
2389 * column position for it. */
2390 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2391 --msg_col;
2392#endif
2393 if (msg_col >= Columns)
2394 {
2395 msg_col = 0;
2396 ++msg_row;
2397 }
2398}
2399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400/*
2401 * Returns TRUE when messages should be printed with mch_errmsg().
2402 * This is used when there is no valid screen, so we can see error messages.
2403 * If termcap is not active, we may be writing in an alternate console
2404 * window, cursor positioning may not work correctly (window size may be
2405 * different, e.g. for Win32 console) or we just don't know where the
2406 * cursor is.
2407 */
2408 int
2409msg_use_printf()
2410{
2411 return (!msg_check_screen()
2412#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2413 || !termcap_active
2414#endif
2415 || (swapping_screen() && !termcap_active)
2416 );
2417}
2418
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002419/*
2420 * Print a message when there is no valid screen.
2421 */
2422 static void
2423msg_puts_printf(str, maxlen)
2424 char_u *str;
2425 int maxlen;
2426{
2427 char_u *s = str;
2428 char_u buf[4];
2429 char_u *p;
2430
2431#ifdef WIN3264
2432 if (!(silent_mode && p_verbose == 0))
2433 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2434#endif
2435 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2436 {
2437 if (!(silent_mode && p_verbose == 0))
2438 {
2439 /* NL --> CR NL translation (for Unix, not for "--version") */
2440 /* NL --> CR translation (for Mac) */
2441 p = &buf[0];
2442 if (*s == '\n' && !info_message)
2443 *p++ = '\r';
2444#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2445 else
2446#endif
2447 *p++ = *s;
2448 *p = '\0';
2449 if (info_message) /* informative message, not an error */
2450 mch_msg((char *)buf);
2451 else
2452 mch_errmsg((char *)buf);
2453 }
2454
2455 /* primitive way to compute the current column */
2456#ifdef FEAT_RIGHTLEFT
2457 if (cmdmsg_rl)
2458 {
2459 if (*s == '\r' || *s == '\n')
2460 msg_col = Columns - 1;
2461 else
2462 --msg_col;
2463 }
2464 else
2465#endif
2466 {
2467 if (*s == '\r' || *s == '\n')
2468 msg_col = 0;
2469 else
2470 ++msg_col;
2471 }
2472 ++s;
2473 }
2474 msg_didout = TRUE; /* assume that line is not empty */
2475
2476#ifdef WIN3264
2477 if (!(silent_mode && p_verbose == 0))
2478 mch_settmode(TMODE_RAW);
2479#endif
2480}
2481
2482/*
2483 * Show the more-prompt and handle the user response.
2484 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002485 * When at hit-enter prompt "typed_char" is the already typed character,
2486 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002487 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2488 */
2489 static int
2490do_more_prompt(typed_char)
2491 int typed_char;
2492{
2493 int used_typed_char = typed_char;
2494 int oldState = State;
2495 int c;
2496#ifdef FEAT_CON_DIALOG
2497 int retval = FALSE;
2498#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002499 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002500 msgchunk_T *mp_last = NULL;
2501 msgchunk_T *mp;
2502 int i;
2503
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002504 if (typed_char == 'G')
2505 {
2506 /* "g<": Find first line on the last page. */
2507 mp_last = msg_sb_start(last_msgchunk);
2508 for (i = 0; i < Rows - 2 && mp_last != NULL
2509 && mp_last->sb_prev != NULL; ++i)
2510 mp_last = msg_sb_start(mp_last->sb_prev);
2511 }
2512
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002513 State = ASKMORE;
2514#ifdef FEAT_MOUSE
2515 setmouse();
2516#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002517 if (typed_char == NUL)
2518 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 for (;;)
2520 {
2521 /*
2522 * Get a typed character directly from the user.
2523 */
2524 if (used_typed_char != NUL)
2525 {
2526 c = used_typed_char; /* was typed at hit-enter prompt */
2527 used_typed_char = NUL;
2528 }
2529 else
2530 c = get_keystroke();
2531
2532#if defined(FEAT_MENU) && defined(FEAT_GUI)
2533 if (c == K_MENU)
2534 {
2535 int idx = get_menu_index(current_menu, ASKMORE);
2536
2537 /* Used a menu. If it starts with CTRL-Y, it must
2538 * be a "Copy" for the clipboard. Otherwise
2539 * assume that we end */
2540 if (idx == MENU_INDEX_INVALID)
2541 continue;
2542 c = *current_menu->strings[idx];
2543 if (c != NUL && current_menu->strings[idx][1] != NUL)
2544 ins_typebuf(current_menu->strings[idx] + 1,
2545 current_menu->noremap[idx], 0, TRUE,
2546 current_menu->silent[idx]);
2547 }
2548#endif
2549
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002550 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002551 switch (c)
2552 {
2553 case BS: /* scroll one line back */
2554 case K_BS:
2555 case 'k':
2556 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002557 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002558 break;
2559
2560 case CAR: /* one extra line */
2561 case NL:
2562 case 'j':
2563 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002564 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002565 break;
2566
2567 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002568 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002569 break;
2570
2571 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002572 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002573 break;
2574
2575 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002576 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002577 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002578 break;
2579
2580 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002581 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002582 case K_PAGEDOWN:
2583 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002584 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002585 break;
2586
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002587 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002588 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002589 break;
2590
2591 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002592 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002593 lines_left = 999999;
2594 break;
2595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596 case ':': /* start new command line */
2597#ifdef FEAT_CON_DIALOG
2598 if (!confirm_msg_used)
2599#endif
2600 {
2601 /* Since got_int is set all typeahead will be flushed, but we
2602 * want to keep this ':', remember that in a special way. */
2603 typeahead_noflush(':');
2604 cmdline_row = Rows - 1; /* put ':' on this line */
2605 skip_redraw = TRUE; /* skip redraw once */
2606 need_wait_return = FALSE; /* don't wait in main() */
2607 }
2608 /*FALLTHROUGH*/
2609 case 'q': /* quit */
2610 case Ctrl_C:
2611 case ESC:
2612#ifdef FEAT_CON_DIALOG
2613 if (confirm_msg_used)
2614 {
2615 /* Jump to the choices of the dialog. */
2616 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002617 }
2618 else
2619#endif
2620 {
2621 got_int = TRUE;
2622 quit_more = TRUE;
2623 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002624 /* When there is some more output (wrapping line) display that
2625 * without another prompt. */
2626 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002627 break;
2628
2629#ifdef FEAT_CLIPBOARD
2630 case Ctrl_Y:
2631 /* Strange way to allow copying (yanking) a modeless
2632 * selection at the more prompt. Use CTRL-Y,
2633 * because the same is used in Cmdline-mode and at the
2634 * hit-enter prompt. However, scrolling one line up
2635 * might be expected... */
2636 if (clip_star.state == SELECT_DONE)
2637 clip_copy_modeless_selection(TRUE);
2638 continue;
2639#endif
2640 default: /* no valid response */
2641 msg_moremsg(TRUE);
2642 continue;
2643 }
2644
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002645 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002646 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002647 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002648 {
2649 /* go to start of last line */
2650 if (mp_last == NULL)
2651 mp = msg_sb_start(last_msgchunk);
2652 else if (mp_last->sb_prev != NULL)
2653 mp = msg_sb_start(mp_last->sb_prev);
2654 else
2655 mp = NULL;
2656
2657 /* go to start of line at top of the screen */
2658 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2659 ++i)
2660 mp = msg_sb_start(mp->sb_prev);
2661
2662 if (mp != NULL && mp->sb_prev != NULL)
2663 {
2664 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002665 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002666 {
2667 if (mp == NULL || mp->sb_prev == NULL)
2668 break;
2669 mp = msg_sb_start(mp->sb_prev);
2670 if (mp_last == NULL)
2671 mp_last = msg_sb_start(last_msgchunk);
2672 else
2673 mp_last = msg_sb_start(mp_last->sb_prev);
2674 }
2675
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002676 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002677 (int)Rows, NULL) == OK)
2678 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002679 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002680 (void)disp_sb_line(0, mp);
2681 }
2682 else
2683 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002684 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002685 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002686 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2687 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002688 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002689 ++msg_scrolled;
2690 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002691 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002692 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693 }
2694 }
2695 else
2696 {
2697 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002698 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002699 {
2700 /* scroll up, display line at bottom */
2701 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002702 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002703 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2704 (int)Columns, ' ', ' ', 0);
2705 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002706 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002707 }
2708 }
2709
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002710 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002711 {
2712 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002713 screen_fill((int)Rows - 1, (int)Rows, 0,
2714 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002715 msg_moremsg(FALSE);
2716 continue;
2717 }
2718
2719 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002720 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 }
2722
2723 break;
2724 }
2725
2726 /* clear the --more-- message */
2727 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2728 State = oldState;
2729#ifdef FEAT_MOUSE
2730 setmouse();
2731#endif
2732 if (quit_more)
2733 {
2734 msg_row = Rows - 1;
2735 msg_col = 0;
2736 }
2737#ifdef FEAT_RIGHTLEFT
2738 else if (cmdmsg_rl)
2739 msg_col = Columns - 1;
2740#endif
2741
2742#ifdef FEAT_CON_DIALOG
2743 return retval;
2744#else
2745 return FALSE;
2746#endif
2747}
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2750
2751#ifdef mch_errmsg
2752# undef mch_errmsg
2753#endif
2754#ifdef mch_msg
2755# undef mch_msg
2756#endif
2757
2758/*
2759 * Give an error message. To be used when the screen hasn't been initialized
2760 * yet. When stderr can't be used, collect error messages until the GUI has
2761 * started and they can be displayed in a message box.
2762 */
2763 void
2764mch_errmsg(str)
2765 char *str;
2766{
2767 int len;
2768
2769#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2770 /* On Unix use stderr if it's a tty.
2771 * When not going to start the GUI also use stderr.
2772 * On Mac, when started from Finder, stderr is the console. */
2773 if (
2774# ifdef UNIX
2775# ifdef MACOS_X_UNIX
2776 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2777# else
2778 isatty(2)
2779# endif
2780# ifdef FEAT_GUI
2781 ||
2782# endif
2783# endif
2784# ifdef FEAT_GUI
2785 !(gui.in_use || gui.starting)
2786# endif
2787 )
2788 {
2789 fprintf(stderr, "%s", str);
2790 return;
2791 }
2792#endif
2793
2794 /* avoid a delay for a message that isn't there */
2795 emsg_on_display = FALSE;
2796
2797 len = (int)STRLEN(str) + 1;
2798 if (error_ga.ga_growsize == 0)
2799 {
2800 error_ga.ga_growsize = 80;
2801 error_ga.ga_itemsize = 1;
2802 }
2803 if (ga_grow(&error_ga, len) == OK)
2804 {
2805 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2806 (char_u *)str, len);
2807#ifdef UNIX
2808 /* remove CR characters, they are displayed */
2809 {
2810 char_u *p;
2811
2812 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2813 for (;;)
2814 {
2815 p = vim_strchr(p, '\r');
2816 if (p == NULL)
2817 break;
2818 *p = ' ';
2819 }
2820 }
2821#endif
2822 --len; /* don't count the NUL at the end */
2823 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825}
2826
2827/*
2828 * Give a message. To be used when the screen hasn't been initialized yet.
2829 * When there is no tty, collect messages until the GUI has started and they
2830 * can be displayed in a message box.
2831 */
2832 void
2833mch_msg(str)
2834 char *str;
2835{
2836#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2837 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2838 * uses mch_errmsg() when started from the desktop.
2839 * When not going to start the GUI also use stdout.
2840 * On Mac, when started from Finder, stderr is the console. */
2841 if (
2842# ifdef UNIX
2843# ifdef MACOS_X_UNIX
2844 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2845# else
2846 isatty(2)
2847# endif
2848# ifdef FEAT_GUI
2849 ||
2850# endif
2851# endif
2852# ifdef FEAT_GUI
2853 !(gui.in_use || gui.starting)
2854# endif
2855 )
2856 {
2857 printf("%s", str);
2858 return;
2859 }
2860# endif
2861 mch_errmsg(str);
2862}
2863#endif /* USE_MCH_ERRMSG */
2864
2865/*
2866 * Put a character on the screen at the current message position and advance
2867 * to the next position. Only for printable ASCII!
2868 */
2869 static void
2870msg_screen_putchar(c, attr)
2871 int c;
2872 int attr;
2873{
2874 msg_didout = TRUE; /* remember that line is not empty */
2875 screen_putchar(c, msg_row, msg_col, attr);
2876#ifdef FEAT_RIGHTLEFT
2877 if (cmdmsg_rl)
2878 {
2879 if (--msg_col == 0)
2880 {
2881 msg_col = Columns;
2882 ++msg_row;
2883 }
2884 }
2885 else
2886#endif
2887 {
2888 if (++msg_col >= Columns)
2889 {
2890 msg_col = 0;
2891 ++msg_row;
2892 }
2893 }
2894}
2895
2896 void
2897msg_moremsg(full)
2898 int full;
2899{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 int attr;
2901 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002904 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002906 screen_puts((char_u *)
2907 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2908 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909}
2910
2911/*
2912 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2913 * exmode_active.
2914 */
2915 void
2916repeat_message()
2917{
2918 if (State == ASKMORE)
2919 {
2920 msg_moremsg(TRUE); /* display --more-- message again */
2921 msg_row = Rows - 1;
2922 }
2923#ifdef FEAT_CON_DIALOG
2924 else if (State == CONFIRM)
2925 {
2926 display_confirm_msg(); /* display ":confirm" message again */
2927 msg_row = Rows - 1;
2928 }
2929#endif
2930 else if (State == EXTERNCMD)
2931 {
2932 windgoto(msg_row, msg_col); /* put cursor back */
2933 }
2934 else if (State == HITRETURN || State == SETWSIZE)
2935 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002936 if (msg_row == Rows - 1)
2937 {
2938 /* Avoid drawing the "hit-enter" prompt below the previous one,
2939 * overwrite it. Esp. useful when regaining focus and a
2940 * FocusGained autocmd exists but didn't draw anything. */
2941 msg_didout = FALSE;
2942 msg_col = 0;
2943 msg_clr_eos();
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 hit_return_msg();
2946 msg_row = Rows - 1;
2947 }
2948}
2949
2950/*
2951 * msg_check_screen - check if the screen is initialized.
2952 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2953 * While starting the GUI the terminal codes will be set for the GUI, but the
2954 * output goes to the terminal. Don't use the terminal codes then.
2955 */
2956 static int
2957msg_check_screen()
2958{
2959 if (!full_screen || !screen_valid(FALSE))
2960 return FALSE;
2961
2962 if (msg_row >= Rows)
2963 msg_row = Rows - 1;
2964 if (msg_col >= Columns)
2965 msg_col = Columns - 1;
2966 return TRUE;
2967}
2968
2969/*
2970 * Clear from current message position to end of screen.
2971 * Skip this when ":silent" was used, no need to clear for redirection.
2972 */
2973 void
2974msg_clr_eos()
2975{
2976 if (msg_silent == 0)
2977 msg_clr_eos_force();
2978}
2979
2980/*
2981 * Clear from current message position to end of screen.
2982 * Note: msg_col is not updated, so we remember the end of the message
2983 * for msg_check().
2984 */
2985 void
2986msg_clr_eos_force()
2987{
2988 if (msg_use_printf())
2989 {
2990 if (full_screen) /* only when termcap codes are valid */
2991 {
2992 if (*T_CD)
2993 out_str(T_CD); /* clear to end of display */
2994 else if (*T_CE)
2995 out_str(T_CE); /* clear to end of line */
2996 }
2997 }
2998 else
2999 {
3000#ifdef FEAT_RIGHTLEFT
3001 if (cmdmsg_rl)
3002 {
3003 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3004 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3005 }
3006 else
3007#endif
3008 {
3009 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3010 ' ', ' ', 0);
3011 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3012 }
3013 }
3014}
3015
3016/*
3017 * Clear the command line.
3018 */
3019 void
3020msg_clr_cmdline()
3021{
3022 msg_row = cmdline_row;
3023 msg_col = 0;
3024 msg_clr_eos_force();
3025}
3026
3027/*
3028 * end putting a message on the screen
3029 * call wait_return if the message does not fit in the available space
3030 * return TRUE if wait_return not called.
3031 */
3032 int
3033msg_end()
3034{
3035 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003036 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 * or the ruler option is set and we run into it,
3038 * we have to redraw the window.
3039 * Do not do this if we are abandoning the file or editing the command line.
3040 */
3041 if (!exiting && need_wait_return && !(State & CMDLINE))
3042 {
3043 wait_return(FALSE);
3044 return FALSE;
3045 }
3046 out_flush();
3047 return TRUE;
3048}
3049
3050/*
3051 * If the written message runs into the shown command or ruler, we have to
3052 * wait for hit-return and redraw the window later.
3053 */
3054 void
3055msg_check()
3056{
3057 if (msg_row == Rows - 1 && msg_col >= sc_col)
3058 {
3059 need_wait_return = TRUE;
3060 redraw_cmdline = TRUE;
3061 }
3062}
3063
3064/*
3065 * May write a string to the redirection file.
3066 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3067 */
3068 static void
3069redir_write(str, maxlen)
3070 char_u *str;
3071 int maxlen;
3072{
3073 char_u *s = str;
3074 static int cur_col = 0;
3075
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003076 /* Don't do anything for displaying prompts and the like. */
3077 if (redir_off)
3078 return;
3079
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003080 /* If 'verbosefile' is set prepare for writing in that file. */
3081 if (*p_vfile != NUL && verbose_fd == NULL)
3082 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003083
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003084 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
3086 /* If the string doesn't start with CR or NL, go to msg_col */
3087 if (*s != '\n' && *s != '\r')
3088 {
3089 while (cur_col < msg_col)
3090 {
3091#ifdef FEAT_EVAL
3092 if (redir_reg)
3093 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003094 else if (redir_vname)
3095 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003096 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003098 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003100 if (verbose_fd != NULL)
3101 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 ++cur_col;
3103 }
3104 }
3105
3106#ifdef FEAT_EVAL
3107 if (redir_reg)
3108 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003109 if (redir_vname)
3110 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111#endif
3112
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003113 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3115 {
3116#ifdef FEAT_EVAL
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003117 if (!redir_reg && !redir_vname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003119 if (redir_fd != NULL)
3120 putc(*s, redir_fd);
3121 if (verbose_fd != NULL)
3122 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (*s == '\r' || *s == '\n')
3124 cur_col = 0;
3125 else if (*s == '\t')
3126 cur_col += (8 - cur_col % 8);
3127 else
3128 ++cur_col;
3129 ++s;
3130 }
3131
3132 if (msg_silent != 0) /* should update msg_col */
3133 msg_col = cur_col;
3134 }
3135}
3136
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003137 int
3138redirecting()
3139{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003140 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003141#ifdef FEAT_EVAL
3142 || redir_reg || redir_vname
3143#endif
3144 ;
3145}
3146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003148 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003149 * Must always be called paired with verbose_leave()!
3150 */
3151 void
3152verbose_enter()
3153{
3154 if (*p_vfile != NUL)
3155 ++msg_silent;
3156}
3157
3158/*
3159 * After giving verbose message.
3160 * Must always be called paired with verbose_enter()!
3161 */
3162 void
3163verbose_leave()
3164{
3165 if (*p_vfile != NUL)
3166 if (--msg_silent < 0)
3167 msg_silent = 0;
3168}
3169
3170/*
3171 * Like verbose_enter() and set msg_scroll when displaying the message.
3172 */
3173 void
3174verbose_enter_scroll()
3175{
3176 if (*p_vfile != NUL)
3177 ++msg_silent;
3178 else
3179 /* always scroll up, don't overwrite */
3180 msg_scroll = TRUE;
3181}
3182
3183/*
3184 * Like verbose_leave() and set cmdline_row when displaying the message.
3185 */
3186 void
3187verbose_leave_scroll()
3188{
3189 if (*p_vfile != NUL)
3190 {
3191 if (--msg_silent < 0)
3192 msg_silent = 0;
3193 }
3194 else
3195 cmdline_row = msg_row;
3196}
3197
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003198/*
3199 * Called when 'verbosefile' is set: stop writing to the file.
3200 */
3201 void
3202verbose_stop()
3203{
3204 if (verbose_fd != NULL)
3205 {
3206 fclose(verbose_fd);
3207 verbose_fd = NULL;
3208 }
3209 verbose_did_open = FALSE;
3210}
3211
3212/*
3213 * Open the file 'verbosefile'.
3214 * Return FAIL or OK.
3215 */
3216 int
3217verbose_open()
3218{
3219 if (verbose_fd == NULL && !verbose_did_open)
3220 {
3221 /* Only give the error message once. */
3222 verbose_did_open = TRUE;
3223
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003224 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003225 if (verbose_fd == NULL)
3226 {
3227 EMSG2(_(e_notopen), p_vfile);
3228 return FAIL;
3229 }
3230 }
3231 return OK;
3232}
3233
3234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * Give a warning message (for searching).
3236 * Use 'w' highlighting and may repeat the message after redrawing
3237 */
3238 void
3239give_warning(message, hl)
3240 char_u *message;
3241 int hl;
3242{
3243 /* Don't do this for ":silent". */
3244 if (msg_silent != 0)
3245 return;
3246
3247 /* Don't want a hit-enter prompt here. */
3248 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#ifdef FEAT_EVAL
3251 set_vim_var_string(VV_WARNINGMSG, message, -1);
3252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 vim_free(keep_msg);
3254 keep_msg = NULL;
3255 if (hl)
3256 keep_msg_attr = hl_attr(HLF_W);
3257 else
3258 keep_msg_attr = 0;
3259 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003260 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 msg_didout = FALSE; /* overwrite this message */
3262 msg_nowait = TRUE; /* don't wait for this message */
3263 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 --no_wait_return;
3266}
3267
3268/*
3269 * Advance msg cursor to column "col".
3270 */
3271 void
3272msg_advance(col)
3273 int col;
3274{
3275 if (msg_silent != 0) /* nothing to advance to */
3276 {
3277 msg_col = col; /* for redirection, may fill it up later */
3278 return;
3279 }
3280 if (col >= Columns) /* not enough room */
3281 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003282#ifdef FEAT_RIGHTLEFT
3283 if (cmdmsg_rl)
3284 while (msg_col > Columns - col)
3285 msg_putchar(' ');
3286 else
3287#endif
3288 while (msg_col < col)
3289 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290}
3291
3292#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3293/*
3294 * Used for "confirm()" function, and the :confirm command prefix.
3295 * Versions which haven't got flexible dialogs yet, and console
3296 * versions, get this generic handler which uses the command line.
3297 *
3298 * type = one of:
3299 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3300 * title = title string (can be NULL for default)
3301 * (neither used in console dialogs at the moment)
3302 *
3303 * Format of the "buttons" string:
3304 * "Button1Name\nButton2Name\nButton3Name"
3305 * The first button should normally be the default/accept
3306 * The second button should be the 'Cancel' button
3307 * Other buttons- use your imagination!
3308 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3309 * different letter.
3310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 int
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003312do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003313 int type UNUSED;
3314 char_u *title UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 char_u *message;
3316 char_u *buttons;
3317 int dfltbutton;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003318 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL
3319 otherwise */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003320 int ex_cmd; /* when TRUE pressing : accepts default and starts
3321 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 int oldState;
3324 int retval = 0;
3325 char_u *hotkeys;
3326 int c;
3327 int i;
3328
3329#ifndef NO_CONSOLE
3330 /* Don't output anything in silent mode ("ex -s") */
3331 if (silent_mode)
3332 return dfltbutton; /* return default option */
3333#endif
3334
3335#ifdef FEAT_GUI_DIALOG
3336 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3337 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3338 {
3339 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003340 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003341 /* avoid a hit-enter prompt without clearing the cmdline */
3342 need_wait_return = FALSE;
3343 emsg_on_display = FALSE;
3344 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
3346 /* Flush output to avoid that further messages and redrawing is done
3347 * in the wrong order. */
3348 out_flush();
3349 gui_mch_update();
3350
3351 return c;
3352 }
3353#endif
3354
3355 oldState = State;
3356 State = CONFIRM;
3357#ifdef FEAT_MOUSE
3358 setmouse();
3359#endif
3360
3361 /*
3362 * Since we wait for a keypress, don't make the
3363 * user press RETURN as well afterwards.
3364 */
3365 ++no_wait_return;
3366 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3367
3368 if (hotkeys != NULL)
3369 {
3370 for (;;)
3371 {
3372 /* Get a typed character directly from the user. */
3373 c = get_keystroke();
3374 switch (c)
3375 {
3376 case CAR: /* User accepts default option */
3377 case NL:
3378 retval = dfltbutton;
3379 break;
3380 case Ctrl_C: /* User aborts/cancels */
3381 case ESC:
3382 retval = 0;
3383 break;
3384 default: /* Could be a hotkey? */
3385 if (c < 0) /* special keys are ignored here */
3386 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003387 if (c == ':' && ex_cmd)
3388 {
3389 retval = dfltbutton;
3390 ins_char_typebuf(':');
3391 break;
3392 }
3393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 /* Make the character lowercase, as chars in "hotkeys" are. */
3395 c = MB_TOLOWER(c);
3396 retval = 1;
3397 for (i = 0; hotkeys[i]; ++i)
3398 {
3399#ifdef FEAT_MBYTE
3400 if (has_mbyte)
3401 {
3402 if ((*mb_ptr2char)(hotkeys + i) == c)
3403 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003404 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 else
3407#endif
3408 if (hotkeys[i] == c)
3409 break;
3410 ++retval;
3411 }
3412 if (hotkeys[i])
3413 break;
3414 /* No hotkey match, so keep waiting */
3415 continue;
3416 }
3417 break;
3418 }
3419
3420 vim_free(hotkeys);
3421 }
3422
3423 State = oldState;
3424#ifdef FEAT_MOUSE
3425 setmouse();
3426#endif
3427 --no_wait_return;
3428 msg_end_prompt();
3429
3430 return retval;
3431}
3432
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003433static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435/*
3436 * Copy one character from "*from" to "*to", taking care of multi-byte
3437 * characters. Return the length of the character in bytes.
3438 */
3439 static int
3440copy_char(from, to, lowercase)
3441 char_u *from;
3442 char_u *to;
3443 int lowercase; /* make character lower case */
3444{
3445#ifdef FEAT_MBYTE
3446 int len;
3447 int c;
3448
3449 if (has_mbyte)
3450 {
3451 if (lowercase)
3452 {
3453 c = MB_TOLOWER((*mb_ptr2char)(from));
3454 return (*mb_char2bytes)(c, to);
3455 }
3456 else
3457 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003458 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 mch_memmove(to, from, (size_t)len);
3460 return len;
3461 }
3462 }
3463 else
3464#endif
3465 {
3466 if (lowercase)
3467 *to = (char_u)TOLOWER_LOC(*from);
3468 else
3469 *to = *from;
3470 return 1;
3471 }
3472}
3473
3474/*
3475 * Format the dialog string, and display it at the bottom of
3476 * the screen. Return a string of hotkey chars (if defined) for
3477 * each 'button'. If a button has no hotkey defined, the first character of
3478 * the button is used.
3479 * The hotkeys can be multi-byte characters, but without combining chars.
3480 *
3481 * Returns an allocated string with hotkeys, or NULL for error.
3482 */
3483 static char_u *
3484msg_show_console_dialog(message, buttons, dfltbutton)
3485 char_u *message;
3486 char_u *buttons;
3487 int dfltbutton;
3488{
3489 int len = 0;
3490#ifdef FEAT_MBYTE
3491# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3492#else
3493# define HOTK_LEN 1
3494#endif
3495 int lenhotkey = HOTK_LEN; /* count first button */
3496 char_u *hotk = NULL;
3497 char_u *msgp = NULL;
3498 char_u *hotkp = NULL;
3499 char_u *r;
3500 int copy;
3501#define HAS_HOTKEY_LEN 30
3502 char_u has_hotkey[HAS_HOTKEY_LEN];
3503 int first_hotkey = FALSE; /* first char of button is hotkey */
3504 int idx;
3505
3506 has_hotkey[0] = FALSE;
3507
3508 /*
3509 * First loop: compute the size of memory to allocate.
3510 * Second loop: copy to the allocated memory.
3511 */
3512 for (copy = 0; copy <= 1; ++copy)
3513 {
3514 r = buttons;
3515 idx = 0;
3516 while (*r)
3517 {
3518 if (*r == DLG_BUTTON_SEP)
3519 {
3520 if (copy)
3521 {
3522 *msgp++ = ',';
3523 *msgp++ = ' '; /* '\n' -> ', ' */
3524
3525 /* advance to next hotkey and set default hotkey */
3526#ifdef FEAT_MBYTE
3527 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003528 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 else
3530#endif
3531 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003532 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (dfltbutton)
3534 --dfltbutton;
3535
3536 /* If no hotkey is specified first char is used. */
3537 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3538 first_hotkey = TRUE;
3539 }
3540 else
3541 {
3542 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3543 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3544 if (idx < HAS_HOTKEY_LEN - 1)
3545 has_hotkey[++idx] = FALSE;
3546 }
3547 }
3548 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3549 {
3550 if (*r == DLG_HOTKEY_CHAR)
3551 ++r;
3552 first_hotkey = FALSE;
3553 if (copy)
3554 {
3555 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3556 *msgp++ = *r;
3557 else
3558 {
3559 /* '&a' -> '[a]' */
3560 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3561 msgp += copy_char(r, msgp, FALSE);
3562 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3563
3564 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003565 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 }
3568 else
3569 {
3570 ++len; /* '&a' -> '[a]' */
3571 if (idx < HAS_HOTKEY_LEN - 1)
3572 has_hotkey[idx] = TRUE;
3573 }
3574 }
3575 else
3576 {
3577 /* everything else copy literally */
3578 if (copy)
3579 msgp += copy_char(r, msgp, FALSE);
3580 }
3581
3582 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003583 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585
3586 if (copy)
3587 {
3588 *msgp++ = ':';
3589 *msgp++ = ' ';
3590 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592 else
3593 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003595 + 2 /* for the NL's */
3596 + STRLEN(buttons)
3597 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003598 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600 /* If no hotkey is specified first char is used. */
3601 if (!has_hotkey[0])
3602 {
3603 first_hotkey = TRUE;
3604 len += 2; /* "x" -> "[x]" */
3605 }
3606
3607 /*
3608 * Now allocate and load the strings
3609 */
3610 vim_free(confirm_msg);
3611 confirm_msg = alloc(len);
3612 if (confirm_msg == NULL)
3613 return NULL;
3614 *confirm_msg = NUL;
3615 hotk = alloc(lenhotkey);
3616 if (hotk == NULL)
3617 return NULL;
3618
3619 *confirm_msg = '\n';
3620 STRCPY(confirm_msg + 1, message);
3621
3622 msgp = confirm_msg + 1 + STRLEN(message);
3623 hotkp = hotk;
3624
Bram Moolenaar6a516062007-07-05 08:11:42 +00003625 /* Define first default hotkey. Keep the hotkey string NUL
3626 * terminated to avoid reading past the end. */
3627 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629 /* Remember where the choices start, displaying starts here when
3630 * "hotkp" typed at the more prompt. */
3631 confirm_msg_tail = msgp;
3632 *msgp++ = '\n';
3633 }
3634 }
3635
3636 display_confirm_msg();
3637 return hotk;
3638}
3639
3640/*
3641 * Display the ":confirm" message. Also called when screen resized.
3642 */
3643 void
3644display_confirm_msg()
3645{
3646 /* avoid that 'q' at the more prompt truncates the message here */
3647 ++confirm_msg_used;
3648 if (confirm_msg != NULL)
3649 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3650 --confirm_msg_used;
3651}
3652
3653#endif /* FEAT_CON_DIALOG */
3654
3655#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3656
3657 int
3658vim_dialog_yesno(type, title, message, dflt)
3659 int type;
3660 char_u *title;
3661 char_u *message;
3662 int dflt;
3663{
3664 if (do_dialog(type,
3665 title == NULL ? (char_u *)_("Question") : title,
3666 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003667 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 return VIM_YES;
3669 return VIM_NO;
3670}
3671
3672 int
3673vim_dialog_yesnocancel(type, title, message, dflt)
3674 int type;
3675 char_u *title;
3676 char_u *message;
3677 int dflt;
3678{
3679 switch (do_dialog(type,
3680 title == NULL ? (char_u *)_("Question") : title,
3681 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003682 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
3684 case 1: return VIM_YES;
3685 case 2: return VIM_NO;
3686 }
3687 return VIM_CANCEL;
3688}
3689
3690 int
3691vim_dialog_yesnoallcancel(type, title, message, dflt)
3692 int type;
3693 char_u *title;
3694 char_u *message;
3695 int dflt;
3696{
3697 switch (do_dialog(type,
3698 title == NULL ? (char_u *)"Question" : title,
3699 message,
3700 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003701 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
3703 case 1: return VIM_YES;
3704 case 2: return VIM_NO;
3705 case 3: return VIM_ALL;
3706 case 4: return VIM_DISCARDALL;
3707 }
3708 return VIM_CANCEL;
3709}
3710
3711#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3712
3713#if defined(FEAT_BROWSE) || defined(PROTO)
3714/*
3715 * Generic browse function. Calls gui_mch_browse() when possible.
3716 * Later this may pop-up a non-GUI file selector (external command?).
3717 */
3718 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003719do_browse(flags, title, dflt, ext, initdir, filter, buf)
3720 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 char_u *title; /* title for the window */
3722 char_u *dflt; /* default file name (may include directory) */
3723 char_u *ext; /* extension added */
3724 char_u *initdir; /* initial directory, NULL for current dir or
3725 when using path from "dflt" */
3726 char_u *filter; /* file name filter */
3727 buf_T *buf; /* buffer to read/write for */
3728{
3729 char_u *fname;
3730 static char_u *last_dir = NULL; /* last used directory */
3731 char_u *tofree = NULL;
3732 int save_browse = cmdmod.browse;
3733
3734 /* Must turn off browse to avoid that autocommands will get the
3735 * flag too! */
3736 cmdmod.browse = FALSE;
3737
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003738 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003740 if (flags & BROWSE_DIR)
3741 title = (char_u *)_("Select Directory dialog");
3742 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 title = (char_u *)_("Save File dialog");
3744 else
3745 title = (char_u *)_("Open File dialog");
3746 }
3747
3748 /* When no directory specified, use default file name, default dir, buffer
3749 * dir, last dir or current dir */
3750 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3751 {
3752 if (mch_isdir(dflt)) /* default file name is a directory */
3753 {
3754 initdir = dflt;
3755 dflt = NULL;
3756 }
3757 else if (gettail(dflt) != dflt) /* default file name includes a path */
3758 {
3759 tofree = vim_strsave(dflt);
3760 if (tofree != NULL)
3761 {
3762 initdir = tofree;
3763 *gettail(initdir) = NUL;
3764 dflt = gettail(dflt);
3765 }
3766 }
3767 }
3768
3769 if (initdir == NULL || *initdir == NUL)
3770 {
3771 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003772 if (STRCMP(p_bsdir, "last") != 0
3773 && STRCMP(p_bsdir, "buffer") != 0
3774 && STRCMP(p_bsdir, "current") != 0
3775 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 initdir = p_bsdir;
3777 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003778 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 && buf != NULL && buf->b_ffname != NULL)
3780 {
3781 if (dflt == NULL || *dflt == NUL)
3782 dflt = gettail(curbuf->b_ffname);
3783 tofree = vim_strsave(curbuf->b_ffname);
3784 if (tofree != NULL)
3785 {
3786 initdir = tofree;
3787 *gettail(initdir) = NUL;
3788 }
3789 }
3790 /* When 'browsedir' is "last", use dir from last browse */
3791 else if (*p_bsdir == 'l')
3792 initdir = last_dir;
3793 /* When 'browsedir is "current", use current directory. This is the
3794 * default already, leave initdir empty. */
3795 }
3796
3797# ifdef FEAT_GUI
3798 if (gui.in_use) /* when this changes, also adjust f_has()! */
3799 {
3800 if (filter == NULL
3801# ifdef FEAT_EVAL
3802 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3803 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3804# endif
3805 )
3806 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003807 if (flags & BROWSE_DIR)
3808 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003809# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003810 /* For systems that have a directory dialog. */
3811 fname = gui_mch_browsedir(title, initdir);
3812# else
3813 /* Generic solution for selecting a directory: select a file and
3814 * remove the file name. */
3815 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3816# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003817# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003818 /* Win32 adds a dummy file name, others return an arbitrary file
3819 * name. GTK+ 2 returns only the directory, */
3820 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3821 {
3822 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003823 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003824
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003825 if (tail == fname)
3826 *tail++ = '.'; /* use current dir */
3827 *tail = NUL;
3828 }
3829# endif
3830 }
3831 else
3832 fname = gui_mch_browse(flags & BROWSE_SAVE,
3833 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 /* We hang around in the dialog for a while, the user might do some
3836 * things to our files. The Win32 dialog allows deleting or renaming
3837 * a file, check timestamps. */
3838 need_check_timestamps = TRUE;
3839 did_check_timestamps = FALSE;
3840 }
3841 else
3842# endif
3843 {
3844 /* TODO: non-GUI file selector here */
3845 EMSG(_("E338: Sorry, no file browser in console mode"));
3846 fname = NULL;
3847 }
3848
3849 /* keep the directory for next time */
3850 if (fname != NULL)
3851 {
3852 vim_free(last_dir);
3853 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003854 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856 *gettail(last_dir) = NUL;
3857 if (*last_dir == NUL)
3858 {
3859 /* filename only returned, must be in current dir */
3860 vim_free(last_dir);
3861 last_dir = alloc(MAXPATHL);
3862 if (last_dir != NULL)
3863 mch_dirname(last_dir, MAXPATHL);
3864 }
3865 }
3866 }
3867
3868 vim_free(tofree);
3869 cmdmod.browse = save_browse;
3870
3871 return fname;
3872}
3873#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003874
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003875#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003876static char *e_printf = N_("E766: Insufficient arguments for printf()");
3877
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003878static long tv_nr(typval_T *tvs, int *idxp);
3879static char *tv_str(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003880# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003881static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003882# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003883
3884/*
3885 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3886 */
3887 static long
3888tv_nr(tvs, idxp)
3889 typval_T *tvs;
3890 int *idxp;
3891{
3892 int idx = *idxp - 1;
3893 long n = 0;
3894 int err = FALSE;
3895
3896 if (tvs[idx].v_type == VAR_UNKNOWN)
3897 EMSG(_(e_printf));
3898 else
3899 {
3900 ++*idxp;
3901 n = get_tv_number_chk(&tvs[idx], &err);
3902 if (err)
3903 n = 0;
3904 }
3905 return n;
3906}
3907
3908/*
3909 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003910 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003911 */
3912 static char *
3913tv_str(tvs, idxp)
3914 typval_T *tvs;
3915 int *idxp;
3916{
3917 int idx = *idxp - 1;
3918 char *s = NULL;
3919
3920 if (tvs[idx].v_type == VAR_UNKNOWN)
3921 EMSG(_(e_printf));
3922 else
3923 {
3924 ++*idxp;
3925 s = (char *)get_tv_string_chk(&tvs[idx]);
3926 }
3927 return s;
3928}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003929
3930# ifdef FEAT_FLOAT
3931/*
3932 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3933 */
3934 static double
3935tv_float(tvs, idxp)
3936 typval_T *tvs;
3937 int *idxp;
3938{
3939 int idx = *idxp - 1;
3940 double f = 0;
3941
3942 if (tvs[idx].v_type == VAR_UNKNOWN)
3943 EMSG(_(e_printf));
3944 else
3945 {
3946 ++*idxp;
3947 if (tvs[idx].v_type == VAR_FLOAT)
3948 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003949 else if (tvs[idx].v_type == VAR_NUMBER)
3950 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003951 else
3952 EMSG(_("E807: Expected Float argument for printf()"));
3953 }
3954 return f;
3955}
3956# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003957#endif
3958
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003959/*
3960 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003961 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003962 * consistency.
3963 *
3964 * This code is based on snprintf.c - a portable implementation of snprintf
3965 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003966 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003967 * The original code, including useful comments, can be found here:
3968 * http://www.ijs.si/software/snprintf/
3969 *
3970 * This snprintf() only supports the following conversion specifiers:
3971 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3972 * with flags: '-', '+', ' ', '0' and '#'.
3973 * An asterisk is supported for field width as well as precision.
3974 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003975 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3976 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003977 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3978 * 'll' (long long int) is not supported.
3979 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003980 * The locale is not used, the string is used as a byte string. This is only
3981 * relevant for double-byte encodings where the second byte may be '%'.
3982 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003983 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3984 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003985 *
3986 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003987 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003988 * is greater or equal to "str_m", not all characters from the result
3989 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3990 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003991 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003992 */
3993
3994/*
3995 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003996 *
3997 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003998 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003999 */
4000
4001/* When generating prototypes all of this is skipped, cproto doesn't
4002 * understand this. */
4003#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004004
Bram Moolenaara800b422010-06-27 01:15:55 +02004005/* Like vim_vsnprintf() but append to the string. */
4006 int
4007vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4008{
4009 va_list ap;
4010 int str_l;
4011 size_t len = STRLEN(str);
4012 size_t space;
4013
4014 if (str_m <= len)
4015 space = 0;
4016 else
4017 space = str_m - len;
4018 va_start(ap, fmt);
4019 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4020 va_end(ap);
4021 return str_l;
4022}
Bram Moolenaara800b422010-06-27 01:15:55 +02004023
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004024 int
4025vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4026{
4027 va_list ap;
4028 int str_l;
4029
4030 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004031 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004032 va_end(ap);
4033 return str_l;
4034}
4035
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004036 int
4037vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004038 char *str;
4039 size_t str_m;
4040 char *fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004041 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004042 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004043{
4044 size_t str_l = 0;
4045 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004046 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004047
4048 if (p == NULL)
4049 p = "";
4050 while (*p != NUL)
4051 {
4052 if (*p != '%')
4053 {
4054 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004055 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004056
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004057 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004058 if (str_l < str_m)
4059 {
4060 size_t avail = str_m - str_l;
4061
4062 mch_memmove(str + str_l, p, n > avail ? avail : n);
4063 }
4064 p += n;
4065 str_l += n;
4066 }
4067 else
4068 {
4069 size_t min_field_width = 0, precision = 0;
4070 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4071 int alternate_form = 0, force_sign = 0;
4072
4073 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4074 * ignored. */
4075 int space_for_positive = 1;
4076
4077 /* allowed values: \0, h, l, L */
4078 char length_modifier = '\0';
4079
4080 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004081# ifdef FEAT_FLOAT
4082# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004083 * That sounds reasonable to use as the maximum
4084 * printable. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004085# else
4086# define TMP_LEN 32
4087# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004088 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004089
4090 /* string address in case of string argument */
4091 char *str_arg;
4092
4093 /* natural field width of arg without padding and sign */
4094 size_t str_arg_l;
4095
4096 /* unsigned char argument value - only defined for c conversion.
4097 * N.B. standard explicitly states the char argument for the c
4098 * conversion is unsigned */
4099 unsigned char uchar_arg;
4100
4101 /* number of zeros to be inserted for numeric conversions as
4102 * required by the precision or minimal field width */
4103 size_t number_of_zeros_to_pad = 0;
4104
4105 /* index into tmp where zero padding is to be inserted */
4106 size_t zero_padding_insertion_ind = 0;
4107
4108 /* current conversion specifier character */
4109 char fmt_spec = '\0';
4110
4111 str_arg = NULL;
4112 p++; /* skip '%' */
4113
4114 /* parse flags */
4115 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4116 || *p == '#' || *p == '\'')
4117 {
4118 switch (*p)
4119 {
4120 case '0': zero_padding = 1; break;
4121 case '-': justify_left = 1; break;
4122 case '+': force_sign = 1; space_for_positive = 0; break;
4123 case ' ': force_sign = 1;
4124 /* If both the ' ' and '+' flags appear, the ' '
4125 * flag should be ignored */
4126 break;
4127 case '#': alternate_form = 1; break;
4128 case '\'': break;
4129 }
4130 p++;
4131 }
4132 /* If the '0' and '-' flags both appear, the '0' flag should be
4133 * ignored. */
4134
4135 /* parse field width */
4136 if (*p == '*')
4137 {
4138 int j;
4139
4140 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004142# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004143 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144# endif
4145 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004146 if (j >= 0)
4147 min_field_width = j;
4148 else
4149 {
4150 min_field_width = -j;
4151 justify_left = 1;
4152 }
4153 }
4154 else if (VIM_ISDIGIT((int)(*p)))
4155 {
4156 /* size_t could be wider than unsigned int; make sure we treat
4157 * argument like common implementations do */
4158 unsigned int uj = *p++ - '0';
4159
4160 while (VIM_ISDIGIT((int)(*p)))
4161 uj = 10 * uj + (unsigned int)(*p++ - '0');
4162 min_field_width = uj;
4163 }
4164
4165 /* parse precision */
4166 if (*p == '.')
4167 {
4168 p++;
4169 precision_specified = 1;
4170 if (*p == '*')
4171 {
4172 int j;
4173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004175# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004176 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177# endif
4178 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004179 p++;
4180 if (j >= 0)
4181 precision = j;
4182 else
4183 {
4184 precision_specified = 0;
4185 precision = 0;
4186 }
4187 }
4188 else if (VIM_ISDIGIT((int)(*p)))
4189 {
4190 /* size_t could be wider than unsigned int; make sure we
4191 * treat argument like common implementations do */
4192 unsigned int uj = *p++ - '0';
4193
4194 while (VIM_ISDIGIT((int)(*p)))
4195 uj = 10 * uj + (unsigned int)(*p++ - '0');
4196 precision = uj;
4197 }
4198 }
4199
4200 /* parse 'h', 'l' and 'll' length modifiers */
4201 if (*p == 'h' || *p == 'l')
4202 {
4203 length_modifier = *p;
4204 p++;
4205 if (length_modifier == 'l' && *p == 'l')
4206 {
4207 /* double l = long long */
4208 length_modifier = 'l'; /* treat it as a single 'l' */
4209 p++;
4210 }
4211 }
4212 fmt_spec = *p;
4213
4214 /* common synonyms: */
4215 switch (fmt_spec)
4216 {
4217 case 'i': fmt_spec = 'd'; break;
4218 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4219 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4220 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004221 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004222 default: break;
4223 }
4224
4225 /* get parameter value, do initial processing */
4226 switch (fmt_spec)
4227 {
4228 /* '%' and 'c' behave similar to 's' regarding flags and field
4229 * widths */
4230 case '%':
4231 case 'c':
4232 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004233 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004234 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004235 str_arg_l = 1;
4236 switch (fmt_spec)
4237 {
4238 case '%':
4239 str_arg = p;
4240 break;
4241
4242 case 'c':
4243 {
4244 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245
4246 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004247# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004248 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249# endif
4250 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004251 /* standard demands unsigned char */
4252 uchar_arg = (unsigned char)j;
4253 str_arg = (char *)&uchar_arg;
4254 break;
4255 }
4256
4257 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004258 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004261 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262# endif
4263 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004264 if (str_arg == NULL)
4265 {
4266 str_arg = "[NULL]";
4267 str_arg_l = 6;
4268 }
4269 /* make sure not to address string beyond the specified
4270 * precision !!! */
4271 else if (!precision_specified)
4272 str_arg_l = strlen(str_arg);
4273 /* truncate string if necessary as requested by precision */
4274 else if (precision == 0)
4275 str_arg_l = 0;
4276 else
4277 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004278 /* Don't put the #if inside memchr(), it can be a
4279 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004280# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004281 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004282# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283 /* memchr on HP does not like n > 2^31 !!! */
4284 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004285 precision <= (size_t)0x7fffffffL ? precision
4286 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004287# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004288 str_arg_l = (q == NULL) ? precision
4289 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004290 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004291# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004292 if (fmt_spec == 'S')
4293 {
4294 if (min_field_width != 0)
4295 min_field_width += STRLEN(str_arg)
4296 - mb_string2cells((char_u *)str_arg, -1);
4297 if (precision)
4298 {
4299 char_u *p1 = (char_u *)str_arg;
4300 size_t i;
4301
4302 for (i = 0; i < precision && *p1; i++)
4303 p1 += mb_ptr2len(p1);
4304
4305 str_arg_l = precision = p1 - (char_u *)str_arg;
4306 }
4307 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004308# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004309 break;
4310
4311 default:
4312 break;
4313 }
4314 break;
4315
4316 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4317 {
4318 /* NOTE: the u, o, x, X and p conversion specifiers
4319 * imply the value is unsigned; d implies a signed
4320 * value */
4321
4322 /* 0 if numeric argument is zero (or if pointer is
4323 * NULL for 'p'), +1 if greater than zero (or nonzero
4324 * for unsigned arguments), -1 if negative (unsigned
4325 * argument is never negative) */
4326 int arg_sign = 0;
4327
4328 /* only defined for length modifier h, or for no
4329 * length modifiers */
4330 int int_arg = 0;
4331 unsigned int uint_arg = 0;
4332
4333 /* only defined for length modifier l */
4334 long int long_arg = 0;
4335 unsigned long int ulong_arg = 0;
4336
4337 /* pointer argument value -only defined for p
4338 * conversion */
4339 void *ptr_arg = NULL;
4340
4341 if (fmt_spec == 'p')
4342 {
4343 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004345# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004346 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347# endif
4348 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004349 if (ptr_arg != NULL)
4350 arg_sign = 1;
4351 }
4352 else if (fmt_spec == 'd')
4353 {
4354 /* signed */
4355 switch (length_modifier)
4356 {
4357 case '\0':
4358 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359 /* char and short arguments are passed as int. */
4360 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004362 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363# endif
4364 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004365 if (int_arg > 0)
4366 arg_sign = 1;
4367 else if (int_arg < 0)
4368 arg_sign = -1;
4369 break;
4370 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004373 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374# endif
4375 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004376 if (long_arg > 0)
4377 arg_sign = 1;
4378 else if (long_arg < 0)
4379 arg_sign = -1;
4380 break;
4381 }
4382 }
4383 else
4384 {
4385 /* unsigned */
4386 switch (length_modifier)
4387 {
4388 case '\0':
4389 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004392 tvs != NULL ? (unsigned)
4393 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394# endif
4395 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004396 if (uint_arg != 0)
4397 arg_sign = 1;
4398 break;
4399 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004402 tvs != NULL ? (unsigned long)
4403 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004404# endif
4405 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004406 if (ulong_arg != 0)
4407 arg_sign = 1;
4408 break;
4409 }
4410 }
4411
4412 str_arg = tmp;
4413 str_arg_l = 0;
4414
4415 /* NOTE:
4416 * For d, i, u, o, x, and X conversions, if precision is
4417 * specified, the '0' flag should be ignored. This is so
4418 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4419 * FreeBSD, NetBSD; but not with Perl.
4420 */
4421 if (precision_specified)
4422 zero_padding = 0;
4423 if (fmt_spec == 'd')
4424 {
4425 if (force_sign && arg_sign >= 0)
4426 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4427 /* leave negative numbers for sprintf to handle, to
4428 * avoid handling tricky cases like (short int)-32768 */
4429 }
4430 else if (alternate_form)
4431 {
4432 if (arg_sign != 0
4433 && (fmt_spec == 'x' || fmt_spec == 'X') )
4434 {
4435 tmp[str_arg_l++] = '0';
4436 tmp[str_arg_l++] = fmt_spec;
4437 }
4438 /* alternate form should have no effect for p
4439 * conversion, but ... */
4440 }
4441
4442 zero_padding_insertion_ind = str_arg_l;
4443 if (!precision_specified)
4444 precision = 1; /* default precision is 1 */
4445 if (precision == 0 && arg_sign == 0)
4446 {
4447 /* When zero value is formatted with an explicit
4448 * precision 0, the resulting formatted string is
4449 * empty (d, i, u, o, x, X, p). */
4450 }
4451 else
4452 {
4453 char f[5];
4454 int f_l = 0;
4455
4456 /* construct a simple format string for sprintf */
4457 f[f_l++] = '%';
4458 if (!length_modifier)
4459 ;
4460 else if (length_modifier == '2')
4461 {
4462 f[f_l++] = 'l';
4463 f[f_l++] = 'l';
4464 }
4465 else
4466 f[f_l++] = length_modifier;
4467 f[f_l++] = fmt_spec;
4468 f[f_l++] = '\0';
4469
4470 if (fmt_spec == 'p')
4471 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4472 else if (fmt_spec == 'd')
4473 {
4474 /* signed */
4475 switch (length_modifier)
4476 {
4477 case '\0':
4478 case 'h': str_arg_l += sprintf(
4479 tmp + str_arg_l, f, int_arg);
4480 break;
4481 case 'l': str_arg_l += sprintf(
4482 tmp + str_arg_l, f, long_arg);
4483 break;
4484 }
4485 }
4486 else
4487 {
4488 /* unsigned */
4489 switch (length_modifier)
4490 {
4491 case '\0':
4492 case 'h': str_arg_l += sprintf(
4493 tmp + str_arg_l, f, uint_arg);
4494 break;
4495 case 'l': str_arg_l += sprintf(
4496 tmp + str_arg_l, f, ulong_arg);
4497 break;
4498 }
4499 }
4500
4501 /* include the optional minus sign and possible
4502 * "0x" in the region before the zero padding
4503 * insertion point */
4504 if (zero_padding_insertion_ind < str_arg_l
4505 && tmp[zero_padding_insertion_ind] == '-')
4506 zero_padding_insertion_ind++;
4507 if (zero_padding_insertion_ind + 1 < str_arg_l
4508 && tmp[zero_padding_insertion_ind] == '0'
4509 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4510 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4511 zero_padding_insertion_ind += 2;
4512 }
4513
4514 {
4515 size_t num_of_digits = str_arg_l
4516 - zero_padding_insertion_ind;
4517
4518 if (alternate_form && fmt_spec == 'o'
4519 /* unless zero is already the first
4520 * character */
4521 && !(zero_padding_insertion_ind < str_arg_l
4522 && tmp[zero_padding_insertion_ind] == '0'))
4523 {
4524 /* assure leading zero for alternate-form
4525 * octal numbers */
4526 if (!precision_specified
4527 || precision < num_of_digits + 1)
4528 {
4529 /* precision is increased to force the
4530 * first character to be zero, except if a
4531 * zero value is formatted with an
4532 * explicit precision of zero */
4533 precision = num_of_digits + 1;
4534 precision_specified = 1;
4535 }
4536 }
4537 /* zero padding to specified precision? */
4538 if (num_of_digits < precision)
4539 number_of_zeros_to_pad = precision - num_of_digits;
4540 }
4541 /* zero padding to specified minimal field width? */
4542 if (!justify_left && zero_padding)
4543 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004544 int n = (int)(min_field_width - (str_arg_l
4545 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004546 if (n > 0)
4547 number_of_zeros_to_pad += n;
4548 }
4549 break;
4550 }
4551
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004552# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004553 case 'f':
4554 case 'e':
4555 case 'E':
4556 case 'g':
4557 case 'G':
4558 {
4559 /* Floating point. */
4560 double f;
4561 double abs_f;
4562 char format[40];
4563 int l;
4564 int remove_trailing_zeroes = FALSE;
4565
4566 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004567# if defined(FEAT_EVAL)
4568 tvs != NULL ? tv_float(tvs, &arg_idx) :
4569# endif
4570 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004571 abs_f = f < 0 ? -f : f;
4572
4573 if (fmt_spec == 'g' || fmt_spec == 'G')
4574 {
4575 /* Would be nice to use %g directly, but it prints
4576 * "1.0" as "1", we don't want that. */
4577 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4578 || abs_f == 0.0)
4579 fmt_spec = 'f';
4580 else
4581 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4582 remove_trailing_zeroes = TRUE;
4583 }
4584
Bram Moolenaarc9372132009-01-13 15:38:37 +00004585 if (fmt_spec == 'f' &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004586# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004587 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004588# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004589 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004590# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004591 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004592 {
4593 /* Avoid a buffer overflow */
4594 strcpy(tmp, "inf");
4595 str_arg_l = 3;
4596 }
4597 else
4598 {
4599 format[0] = '%';
4600 l = 1;
4601 if (precision_specified)
4602 {
4603 size_t max_prec = TMP_LEN - 10;
4604
4605 /* Make sure we don't get more digits than we
4606 * have room for. */
4607 if (fmt_spec == 'f' && abs_f > 1.0)
4608 max_prec -= (size_t)log10(abs_f);
4609 if (precision > max_prec)
4610 precision = max_prec;
4611 l += sprintf(format + 1, ".%d", (int)precision);
4612 }
4613 format[l] = fmt_spec;
4614 format[l + 1] = NUL;
4615 str_arg_l = sprintf(tmp, format, f);
4616
4617 if (remove_trailing_zeroes)
4618 {
4619 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004620 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004621
4622 /* Using %g or %G: remove superfluous zeroes. */
4623 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004624 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004625 else
4626 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004627 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004628 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004629 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004630 {
4631 /* Remove superfluous '+' and leading
4632 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004633 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004634 {
4635 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004636 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004637 --str_arg_l;
4638 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004639 i = (tp[1] == '-') ? 2 : 1;
4640 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004641 {
4642 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004643 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004644 --str_arg_l;
4645 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004646 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004647 }
4648 }
4649
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004650 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004651 /* Remove trailing zeroes, but keep the one
4652 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004653 while (tp > tmp + 2 && *tp == '0'
4654 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004655 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004656 STRMOVE(tp, tp + 1);
4657 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004658 --str_arg_l;
4659 }
4660 }
4661 else
4662 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004663 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004664
4665 /* Be consistent: some printf("%e") use 1.0e+12
4666 * and some 1.0e+012. Remove one zero in the last
4667 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004668 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004669 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004670 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4671 && tp[2] == '0'
4672 && vim_isdigit(tp[3])
4673 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004674 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004675 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004676 --str_arg_l;
4677 }
4678 }
4679 }
4680 str_arg = tmp;
4681 break;
4682 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004683# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004684
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004685 default:
4686 /* unrecognized conversion specifier, keep format string
4687 * as-is */
4688 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004689 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004690 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004691 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004692
4693 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004694 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004695 str_arg = p;
4696 str_arg_l = 0;
4697 if (*p != NUL)
4698 str_arg_l++; /* include invalid conversion specifier
4699 unchanged if not at end-of-string */
4700 break;
4701 }
4702
4703 if (*p != NUL)
4704 p++; /* step over the just processed conversion specifier */
4705
4706 /* insert padding to the left as requested by min_field_width;
4707 * this does not include the zero padding in case of numerical
4708 * conversions*/
4709 if (!justify_left)
4710 {
4711 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004712 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004713
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004714 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004715 {
4716 if (str_l < str_m)
4717 {
4718 size_t avail = str_m - str_l;
4719
4720 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004721 (size_t)pn > avail ? avail
4722 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004723 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004724 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004725 }
4726 }
4727
4728 /* zero padding as requested by the precision or by the minimal
4729 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004730 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004731 {
4732 /* will not copy first part of numeric right now, *
4733 * force it to be copied later in its entirety */
4734 zero_padding_insertion_ind = 0;
4735 }
4736 else
4737 {
4738 /* insert first part of numerics (sign or '0x') before zero
4739 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004740 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004741
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004742 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 {
4744 if (str_l < str_m)
4745 {
4746 size_t avail = str_m - str_l;
4747
4748 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004749 (size_t)zn > avail ? avail
4750 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004751 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004752 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004753 }
4754
4755 /* insert zero padding as requested by the precision or min
4756 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004757 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004758 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004759 {
4760 if (str_l < str_m)
4761 {
4762 size_t avail = str_m-str_l;
4763
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004764 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004765 (size_t)zn > avail ? avail
4766 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004767 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004768 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004769 }
4770 }
4771
4772 /* insert formatted string
4773 * (or as-is conversion specifier for unknown conversions) */
4774 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004775 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004776
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004777 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004778 {
4779 if (str_l < str_m)
4780 {
4781 size_t avail = str_m - str_l;
4782
4783 mch_memmove(str + str_l,
4784 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004785 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004786 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004787 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004788 }
4789 }
4790
4791 /* insert right padding */
4792 if (justify_left)
4793 {
4794 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004795 int pn = (int)(min_field_width
4796 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004797
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004798 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 {
4800 if (str_l < str_m)
4801 {
4802 size_t avail = str_m - str_l;
4803
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004804 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004805 (size_t)pn > avail ? avail
4806 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004807 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004808 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004809 }
4810 }
4811 }
4812 }
4813
4814 if (str_m > 0)
4815 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004816 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004817 * overwriting the last character (shouldn't happen, but just in case)
4818 * */
4819 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4820 }
4821
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004822 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004824
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004825 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004826 * character), that is, the number of characters that would have been
4827 * written to the buffer if it were large enough. */
4828 return (int)str_l;
4829}
4830
4831#endif /* PROTO */