blob: 1c9fbe513c19ec6cb080a9a9b48f927de8ee013a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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() */
Bram Moolenaar44ca54a2016-08-27 15:41:32 +020015#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17#include "vim.h"
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static void add_msg_hist(char_u *s, int len, int attr);
20static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
23static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
31static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static int confirm_msg_used = FALSE; /* displaying confirm_msg */
36static char_u *confirm_msg = NULL; /* ":confirm" message */
37static char_u *confirm_msg_tail; /* tail of confirm_msg */
38#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010039#ifdef FEAT_JOB_CHANNEL
40static int emsg_to_channel_log = FALSE;
41#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43struct msg_hist
44{
45 struct msg_hist *next;
46 char_u *msg;
47 int attr;
48};
49
50static struct msg_hist *first_msg_hist = NULL;
51static struct msg_hist *last_msg_hist = NULL;
52static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020054static FILE *verbose_fd = NULL;
55static int verbose_did_open = FALSE;
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057/*
58 * When writing messages to the screen, there are many different situations.
59 * A number of variables is used to remember the current state:
60 * msg_didany TRUE when messages were written since the last time the
61 * user reacted to a prompt.
62 * Reset: After hitting a key for the hit-return prompt,
63 * hitting <CR> for the command line or input().
64 * Set: When any message is written to the screen.
65 * msg_didout TRUE when something was written to the current line.
66 * Reset: When advancing to the next line, when the current
67 * text can be overwritten.
68 * Set: When any message is written to the screen.
69 * msg_nowait No extra delay for the last drawn message.
70 * Used in normal_cmd() before the mode message is drawn.
71 * emsg_on_display There was an error message recently. Indicates that there
72 * should be a delay before redrawing.
73 * msg_scroll The next message should not overwrite the current one.
74 * msg_scrolled How many lines the screen has been scrolled (because of
75 * messages). Used in update_screen() to scroll the screen
76 * back. Incremented each time the screen scrolls a line.
77 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
78 * writes something without scrolling should not make
79 * need_wait_return to be set. This is a hack to make ":ts"
80 * work without an extra prompt.
81 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010082 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 * need_wait_return TRUE when the hit-return prompt is needed.
84 * Reset: After giving the hit-return prompt, when the user
85 * has answered some other prompt.
86 * Set: When the ruler or typeahead display is overwritten,
87 * scrolling the screen for some message.
88 * keep_msg Message to be displayed after redrawing the screen, in
89 * main_loop().
90 * This is an allocated string or NULL when not used.
91 */
92
93/*
94 * msg(s) - displays the string 's' on the status line
95 * When terminal not initialized (yet) mch_errmsg(..) is used.
96 * return TRUE if wait_return not called
97 */
98 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010099msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100{
101 return msg_attr_keep(s, 0, FALSE);
102}
103
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000104#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000105 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106/*
107 * Like msg() but keep it silent when 'verbosefile' is set.
108 */
109 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100110verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111{
112 int n;
113
114 verbose_enter();
115 n = msg_attr_keep(s, 0, FALSE);
116 verbose_leave();
117
118 return n;
119}
120#endif
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100123msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 return msg_attr_keep(s, attr, FALSE);
126}
127
128 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129msg_attr_keep(
130 char_u *s,
131 int attr,
132 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
134 static int entered = 0;
135 int retval;
136 char_u *buf = NULL;
137
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200138 /* Skip messages not matching ":filter pattern".
139 * Don't filter when there is an error. */
140 if (!emsg_on_display && message_filtered(s))
141 return TRUE;
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#ifdef FEAT_EVAL
144 if (attr == 0)
145 set_vim_var_string(VV_STATUSMSG, s, -1);
146#endif
147
148 /*
149 * It is possible that displaying a messages causes a problem (e.g.,
150 * when redrawing the window), which causes another message, etc.. To
151 * break this loop, limit the recursiveness to 3 levels.
152 */
153 if (entered >= 3)
154 return TRUE;
155 ++entered;
156
157 /* Add message to history (unless it's a repeated kept message or a
158 * truncated message) */
159 if (s != keep_msg
160 || (*s != '<'
161 && last_msg_hist != NULL
162 && last_msg_hist->msg != NULL
163 && STRCMP(s, last_msg_hist->msg)))
164 add_msg_hist(s, -1, attr);
165
Bram Moolenaar958dc692016-12-01 15:34:12 +0100166#ifdef FEAT_JOB_CHANNEL
167 if (emsg_to_channel_log)
Bram Moolenaar958dc692016-12-01 15:34:12 +0100168 /* Write message in the channel log. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200169 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 /* When displaying keep_msg, don't let msg_start() free it, caller must do
173 * that. */
174 if (s == keep_msg)
175 keep_msg = NULL;
176
177 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000178 msg_start();
179 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 if (buf != NULL)
181 s = buf;
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 msg_outtrans_attr(s, attr);
184 msg_clr_eos();
185 retval = msg_end();
186
187 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
188 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000189 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
191 vim_free(buf);
192 --entered;
193 return retval;
194}
195
196/*
197 * Truncate a string such that it can be printed without causing a scroll.
198 * Returns an allocated string or NULL when no truncating is done.
199 */
200 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100201msg_strtrunc(
202 char_u *s,
203 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
205 char_u *buf = NULL;
206 int len;
207 int room;
208
209 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000210 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
211 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {
213 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000214 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000215 /* Use all the columns. */
216 room = (int)(Rows - msg_row) * Columns - 1;
217 else
218 /* Use up to 'showcmd' column. */
219 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 if (len > room && room > 0)
221 {
222#ifdef FEAT_MBYTE
223 if (enc_utf8)
224 /* may have up to 18 bytes per cell (6 per char, up to two
225 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 else if (enc_dbcs == DBCS_JPNU)
228 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 else
231#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 len = room + 2;
233 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100235 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237 }
238 return buf;
239}
240
241/*
242 * Truncate a string "s" to "buf" with cell width "room".
243 * "s" and "buf" may be equal.
244 */
245 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100246trunc_string(
247 char_u *s,
248 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200249 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100250 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200252 size_t room = room_in - 3; /* "..." takes 3 chars */
253 size_t half;
254 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 int e;
256 int i;
257 int n;
258
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200259 if (room_in < 3)
260 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100264 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {
266 if (s[e] == NUL)
267 {
268 /* text fits without truncating! */
269 buf[e] = NUL;
270 return;
271 }
272 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200273 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 break;
275 len += n;
276 buf[e] = s[e];
277#ifdef FEAT_MBYTE
278 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000279 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100281 if (++e == buflen)
282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 buf[e] = s[e];
284 }
285#endif
286 }
287
288 /* Last part: End of the string. */
289 i = e;
290#ifdef FEAT_MBYTE
291 if (enc_dbcs != 0)
292 {
293 /* For DBCS going backwards in a string is slow, but
294 * computing the cell width isn't too slow: go forward
295 * until the rest fits. */
296 n = vim_strsize(s + i);
297 while (len + n > room)
298 {
299 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000300 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 }
302 }
303 else if (enc_utf8)
304 {
305 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000306 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 for (;;)
308 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000309 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200310 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200311 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200313 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 break;
315 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200316 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 }
318 }
319 else
320#endif
321 {
322 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
323 len += n;
324 }
325
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200326
327 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100328 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200329 /* text fits without truncating */
330 if (s != buf)
331 {
332 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200333 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200334 len = buflen - 1;
335 len = len - e + 1;
336 if (len < 1)
337 buf[e - 1] = NUL;
338 else
339 mch_memmove(buf + e, s + e, len);
340 }
341 }
342 else if (e + 3 < buflen)
343 {
344 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200346 len = STRLEN(s + i) + 1;
347 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100348 len = buflen - e - 3 - 1;
349 mch_memmove(buf + e + 3, s + i, len);
350 buf[e + 3 + len - 1] = NUL;
351 }
352 else
353 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200354 /* can't fit in the "...", just truncate it */
355 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357}
358
359/*
360 * Automatic prototype generation does not understand this function.
361 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
362 * shorter than IOSIZE!!!
363 */
364#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000366int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000367
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100369# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372smsg(char_u *s, ...)
373{
374 va_list arglist;
375
376 va_start(arglist, s);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +0200377 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 va_end(arglist);
379 return msg(IObuff);
380}
381
382 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100383# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386smsg_attr(int attr, char_u *s, ...)
387{
388 va_list arglist;
389
390 va_start(arglist, s);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +0200391 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 va_end(arglist);
393 return msg_attr(IObuff, attr);
394}
395
Bram Moolenaare0429682018-07-01 16:44:03 +0200396 int
397# ifdef __BORLANDC__
398_RTLENTRYF
399# endif
400smsg_attr_keep(int attr, char_u *s, ...)
401{
402 va_list arglist;
403
404 va_start(arglist, s);
405 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
406 va_end(arglist);
407 return msg_attr_keep(IObuff, attr, TRUE);
408}
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#endif
411
412/*
413 * Remember the last sourcing name/lnum used in an error message, so that it
414 * isn't printed each time when it didn't change.
415 */
416static int last_sourcing_lnum = 0;
417static char_u *last_sourcing_name = NULL;
418
419/*
420 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
421 * for the next error message;
422 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000423 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100424reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100426 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 last_sourcing_lnum = 0;
428}
429
430/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000431 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
432 */
433 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100434other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000435{
436 if (sourcing_name != NULL)
437 {
438 if (last_sourcing_name != NULL)
439 return STRCMP(sourcing_name, last_sourcing_name) != 0;
440 return TRUE;
441 }
442 return FALSE;
443}
444
445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 * Get the message about the source, as used for an error message.
447 * Returns an allocated string with room for one more character.
448 * Returns NULL when no message is to be given.
449 */
450 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100451get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 char_u *Buf, *p;
454
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000455 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
457 p = (char_u *)_("Error detected while processing %s:");
458 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
459 if (Buf != NULL)
460 sprintf((char *)Buf, (char *)p, sourcing_name);
461 return Buf;
462 }
463 return NULL;
464}
465
466/*
467 * Get the message about the source lnum, as used for an error message.
468 * Returns an allocated string with room for one more character.
469 * Returns NULL when no message is to be given.
470 */
471 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100472get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 char_u *Buf, *p;
475
476 /* lnum is 0 when executing a command from the command line
477 * argument, we don't want a line number then */
478 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000479 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 && sourcing_lnum != 0)
481 {
482 p = (char_u *)_("line %4ld:");
483 Buf = alloc((unsigned)(STRLEN(p) + 20));
484 if (Buf != NULL)
485 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
486 return Buf;
487 }
488 return NULL;
489}
490
491/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000492 * Display name and line number for the source of an error.
493 * Remember the file name and line number, so that for the next error the info
494 * is only displayed if it changed.
495 */
496 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100497msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000498{
499 char_u *p;
500
501 ++no_wait_return;
502 p = get_emsg_source();
503 if (p != NULL)
504 {
505 msg_attr(p, attr);
506 vim_free(p);
507 }
508 p = get_emsg_lnum();
509 if (p != NULL)
510 {
Bram Moolenaar8820b482017-03-16 17:23:31 +0100511 msg_attr(p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000512 vim_free(p);
513 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
514 }
515
516 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000517 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000518 {
519 vim_free(last_sourcing_name);
520 if (sourcing_name == NULL)
521 last_sourcing_name = NULL;
522 else
523 last_sourcing_name = vim_strsave(sourcing_name);
524 }
525 --no_wait_return;
526}
527
528/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000529 * Return TRUE if not giving error messages right now:
530 * If "emsg_off" is set: no error messages at the moment.
531 * If "msg" is in 'debug': do error message but without side effects.
532 * If "emsg_skip" is set: never do error messages.
533 */
534 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100535emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000536{
537 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
538 && vim_strchr(p_debug, 't') == NULL)
539#ifdef FEAT_EVAL
540 || emsg_skip > 0
541#endif
542 )
543 return TRUE;
544 return FALSE;
545}
546
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100547#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100548static garray_T ignore_error_list = GA_EMPTY;
549
550 void
551ignore_error_for_testing(char_u *error)
552{
553 if (ignore_error_list.ga_itemsize == 0)
554 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
555
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100556 if (STRCMP("RESET", error) == 0)
557 ga_clear_strings(&ignore_error_list);
558 else
559 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100560}
561
562 static int
563ignore_error(char_u *msg)
564{
565 int i;
566
567 for (i = 0; i < ignore_error_list.ga_len; ++i)
568 if (strstr((char *)msg,
569 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
570 return TRUE;
571 return FALSE;
572}
573#endif
574
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200575#if !defined(HAVE_STRERROR) || defined(PROTO)
576/*
577 * Replacement for perror() that behaves more or less like emsg() was called.
578 * v:errmsg will be set and called_emsg will be set.
579 */
580 void
581do_perror(char *msg)
582{
583 perror(msg);
584 ++emsg_silent;
585 emsg((char_u *)msg);
586 --emsg_silent;
587}
588#endif
589
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 * emsg() - display an error message
592 *
593 * Rings the bell, if appropriate, and calls message() to do the real work
594 * When terminal not initialized (yet) mch_errmsg(..) is used.
595 *
596 * return TRUE if wait_return not called
597 */
598 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100599emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100603 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_EVAL
605 int ignore = FALSE;
606 int severe;
607#endif
608
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100609 /* Skip this if not giving error messages at the moment. */
610 if (emsg_not_now())
611 return TRUE;
612
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100613#ifdef FEAT_EVAL
614 /* When testing some errors are turned into a normal message. */
615 if (ignore_error(s))
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +0100616 /* don't call msg() if it results in a dialog */
617 return msg_use_printf() ? FALSE : msg(s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 called_emsg = TRUE;
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#ifdef FEAT_EVAL
Bram Moolenaare723c422017-09-06 23:40:10 +0200623 /* If "emsg_severe" is TRUE: When an error exception is to be thrown,
624 * prefer this message over previous messages for the same command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 severe = emsg_severe;
626 emsg_severe = FALSE;
627#endif
628
Bram Moolenaar57657d82006-04-21 22:12:41 +0000629 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 {
631#ifdef FEAT_EVAL
632 /*
633 * Cause a throw of an error exception if appropriate. Don't display
634 * the error message in this case. (If no matching catch clause will
635 * be found, the message will be displayed later on.) "ignore" is set
636 * when the message should be ignored completely (used for the
637 * interrupt message).
638 */
639 if (cause_errthrow(s, severe, &ignore) == TRUE)
640 {
641 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100642 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 return TRUE;
644 }
645
646 /* set "v:errmsg", also when using ":silent! cmd" */
647 set_vim_var_string(VV_ERRMSG, s, -1);
648#endif
649
650 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000651 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 * But do write it to the redirection file.
653 */
654 if (emsg_silent != 0)
655 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200656 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200658 msg_start();
659 p = get_emsg_source();
660 if (p != NULL)
661 {
662 STRCAT(p, "\n");
663 redir_write(p, -1);
664 vim_free(p);
665 }
666 p = get_emsg_lnum();
667 if (p != NULL)
668 {
669 STRCAT(p, "\n");
670 redir_write(p, -1);
671 vim_free(p);
672 }
673 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100675#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200676 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return TRUE;
679 }
680
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100681 ex_exitval = 1;
682
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200683 /* Reset msg_silent, an error causes messages to be switched back on.
684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 msg_silent = 0;
686 cmd_silent = FALSE;
687
688 if (global_busy) /* break :global command */
689 ++global_busy;
690
691 if (p_eb)
692 beep_flush(); /* also includes flush_buffers() */
693 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200694 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100695 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200696#ifdef FEAT_EVAL
697 did_uncaught_emsg = TRUE;
698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
700
701 emsg_on_display = TRUE; /* remember there is an error message */
702 ++msg_scroll; /* don't overwrite a previous message */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100703 attr = HL_ATTR(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000704 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 need_wait_return = TRUE; /* needed in case emsg() is called after
706 * wait_return has reset need_wait_return
707 * and a redraw is expected because
708 * msg_scrolled is non-zero */
709
Bram Moolenaar958dc692016-12-01 15:34:12 +0100710#ifdef FEAT_JOB_CHANNEL
711 emsg_to_channel_log = TRUE;
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 /*
714 * Display name and line number for the source of the error.
715 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000716 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
718 /*
719 * Display the error message itself.
720 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000721 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar958dc692016-12-01 15:34:12 +0100722 r = msg_attr(s, attr);
723
724#ifdef FEAT_JOB_CHANNEL
725 emsg_to_channel_log = FALSE;
726#endif
727 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728}
729
Bram Moolenaar95f09602016-11-10 20:01:45 +0100730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731/*
732 * Print an error message with one "%s" and one string argument.
733 */
734 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100735emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 return emsg3(s, a1, NULL);
738}
739
Bram Moolenaar95f09602016-11-10 20:01:45 +0100740/*
741 * Print an error message with one or two "%s" and one or two string arguments.
742 * This is not in message.c to avoid a warning for prototypes.
743 */
744 int
745emsg3(char_u *s, char_u *a1, char_u *a2)
746{
747 if (emsg_not_now())
748 return TRUE; /* no error messages at the moment */
749 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
750 return emsg(IObuff);
751}
752
753/*
754 * Print an error message with one "%ld" and one long int argument.
755 * This is not in message.c to avoid a warning for prototypes.
756 */
757 int
758emsgn(char_u *s, long n)
759{
760 if (emsg_not_now())
761 return TRUE; /* no error messages at the moment */
762 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
763 return emsg(IObuff);
764}
765
766/*
767 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
768 * defined. It is used for internal errors only, so that they can be
769 * detected when fuzzing vim.
770 */
771 void
772iemsg(char_u *s)
773{
Bram Moolenaar191f18b2018-02-04 16:38:47 +0100774 emsg(s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100775#ifdef ABORT_ON_INTERNAL_ERROR
776 abort();
777#endif
778}
779
780
781/*
782 * Same as emsg2(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
783 * defined. It is used for internal errors only, so that they can be
784 * detected when fuzzing vim.
785 */
786 void
787iemsg2(char_u *s, char_u *a1)
788{
789 emsg2(s, a1);
790#ifdef ABORT_ON_INTERNAL_ERROR
791 abort();
792#endif
793}
794
795/*
796 * Same as emsgn(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
797 * defined. It is used for internal errors only, so that they can be
798 * detected when fuzzing vim.
799 */
800 void
801iemsgn(char_u *s, long n)
802{
803 emsgn(s, n);
804#ifdef ABORT_ON_INTERNAL_ERROR
805 abort();
806#endif
807}
808
809/*
810 * Give an "Internal error" message.
811 */
812 void
813internal_error(char *where)
814{
815 IEMSG2(_(e_intern2), where);
816}
817
Bram Moolenaarea424162005-06-16 21:51:00 +0000818/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaardf177f62005-02-22 08:39:57 +0000820 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100821emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000822{
823 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
824}
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826/*
827 * Like msg(), but truncate to a single line if p_shm contains 't', or when
828 * "force" is TRUE. This truncates in another way as for normal messages.
829 * Careful: The string may be changed by msg_may_trunc()!
830 * Returns a pointer to the printed message, if wait_return() not called.
831 */
832 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100833msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 int n;
836
837 /* Add message to history before truncating */
838 add_msg_hist(s, -1, attr);
839
840 s = msg_may_trunc(force, s);
841
842 msg_hist_off = TRUE;
843 n = msg_attr(s, attr);
844 msg_hist_off = FALSE;
845
846 if (n)
847 return s;
848 return NULL;
849}
850
851/*
852 * Check if message "s" should be truncated at the start (for filenames).
853 * Return a pointer to where the truncated message starts.
854 * Note: May change the message by replacing a character with '<'.
855 */
856 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100857msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858{
859 int n;
860 int room;
861
862 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
863 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
864 && (n = (int)STRLEN(s) - room) > 0)
865 {
866#ifdef FEAT_MBYTE
867 if (has_mbyte)
868 {
869 int size = vim_strsize(s);
870
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000871 /* There may be room anyway when there are multibyte chars. */
872 if (size <= room)
873 return s;
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 for (n = 0; size >= room; )
876 {
877 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000878 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880 --n;
881 }
882#endif
883 s += n;
884 *s = '<';
885 }
886 return s;
887}
888
889 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100890add_msg_hist(
891 char_u *s,
892 int len, /* -1 for undetermined length */
893 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
895 struct msg_hist *p;
896
897 if (msg_hist_off || msg_silent != 0)
898 return;
899
900 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000901 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000902 (void)delete_first_msg();
903
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 /* allocate an entry and add the message at the end of the history */
905 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
906 if (p != NULL)
907 {
908 if (len < 0)
909 len = (int)STRLEN(s);
910 /* remove leading and trailing newlines */
911 while (len > 0 && *s == '\n')
912 {
913 ++s;
914 --len;
915 }
916 while (len > 0 && s[len - 1] == '\n')
917 --len;
918 p->msg = vim_strnsave(s, len);
919 p->next = NULL;
920 p->attr = attr;
921 if (last_msg_hist != NULL)
922 last_msg_hist->next = p;
923 last_msg_hist = p;
924 if (first_msg_hist == NULL)
925 first_msg_hist = last_msg_hist;
926 ++msg_hist_len;
927 }
928}
929
930/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000931 * Delete the first (oldest) message from the history.
932 * Returns FAIL if there are no messages.
933 */
934 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100935delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000936{
937 struct msg_hist *p;
938
939 if (msg_hist_len <= 0)
940 return FAIL;
941 p = first_msg_hist;
942 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000943 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200944 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000945 vim_free(p->msg);
946 vim_free(p);
947 --msg_hist_len;
948 return OK;
949}
950
951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 * ":messages" command.
953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200955ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 struct msg_hist *p;
958 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200959 int c = 0;
960
961 if (STRCMP(eap->arg, "clear") == 0)
962 {
963 int keep = eap->addr_count == 0 ? 0 : eap->line2;
964
965 while (msg_hist_len > keep)
966 (void)delete_first_msg();
967 return;
968 }
969
970 if (*eap->arg != NUL)
971 {
972 EMSG(_(e_invarg));
973 return;
974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 msg_hist_off = TRUE;
977
Bram Moolenaar451f8492016-04-14 17:16:22 +0200978 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200979 if (eap->addr_count != 0)
980 {
981 /* Count total messages */
982 for (; p != NULL && !got_int; p = p->next)
983 c++;
984
985 c -= eap->line2;
986
987 /* Skip without number of messages specified */
988 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
989 p = p->next, c--);
990 }
991
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200992 if (p == first_msg_hist)
993 {
994 s = mch_getenv((char_u *)"LANG");
995 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +0200996 // The next comment is extracted by xgettext and put in po file for
997 // translators to read.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200998 msg_attr((char_u *)
Bram Moolenaar0c183192018-06-28 14:54:43 +0200999 // Translator: Please replace the name and email address
1000 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001001 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001002 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001003 }
1004
Bram Moolenaar451f8492016-04-14 17:16:22 +02001005 /* Display what was not skipped. */
1006 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (p->msg != NULL)
1008 msg_attr(p->msg, p->attr);
1009
1010 msg_hist_off = FALSE;
1011}
1012
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001013#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014/*
1015 * Call this after prompting the user. This will avoid a hit-return message
1016 * and a delay.
1017 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001018 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001019msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 need_wait_return = FALSE;
1022 emsg_on_display = FALSE;
1023 cmdline_row = msg_row;
1024 msg_col = 0;
1025 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001026 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028#endif
1029
1030/*
1031 * wait for the user to hit a key (normally a return)
1032 * if 'redraw' is TRUE, clear and redraw the screen
1033 * if 'redraw' is FALSE, just redraw the screen
1034 * if 'redraw' is -1, don't redraw at all
1035 */
1036 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001037wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 int c;
1040 int oldState;
1041 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001043 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001044 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 if (redraw == TRUE)
1047 must_redraw = CLEAR;
1048
1049 /* If using ":silent cmd", don't wait for a return. Also don't set
1050 * need_wait_return to do it later. */
1051 if (msg_silent != 0)
1052 return;
1053
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001054 /*
1055 * When inside vgetc(), we can't wait for a typed character at all.
1056 * With the global command (and some others) we only need one return at
1057 * the end. Adjust cmdline_row to avoid the next message overwriting the
1058 * last one.
1059 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001060 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001062 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (no_wait_return)
1064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (!exmode_active)
1066 cmdline_row = msg_row;
1067 return;
1068 }
1069
1070 redir_off = TRUE; /* don't redirect this message */
1071 oldState = State;
1072 if (quit_more)
1073 {
1074 c = CAR; /* just pretend CR was hit */
1075 quit_more = FALSE;
1076 got_int = FALSE;
1077 }
1078 else if (exmode_active)
1079 {
1080 MSG_PUTS(" "); /* make sure the cursor is on the right line */
1081 c = CAR; /* no need for a return in ex mode */
1082 got_int = FALSE;
1083 }
1084 else
1085 {
1086 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1087 * just changed. */
1088 screenalloc(FALSE);
1089
1090 State = HITRETURN;
1091#ifdef FEAT_MOUSE
1092 setmouse();
1093#endif
1094#ifdef USE_ON_FLY_SCROLL
1095 dont_scroll = TRUE; /* disallow scrolling here */
1096#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001097 cmdline_row = msg_row;
1098
Bram Moolenaarec38d692013-04-24 15:12:32 +02001099 /* Avoid the sequence that the user types ":" at the hit-return prompt
1100 * to start an Ex command, but the file-changed dialog gets in the
1101 * way. */
1102 if (need_check_timestamps)
1103 check_timestamps(FALSE);
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 hit_return_msg();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 do
1108 {
1109 /* Remember "got_int", if it is set vgetc() probably returns a
1110 * CTRL-C, but we need to loop then. */
1111 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001112
1113 /* Don't do mappings here, we put the character back in the
1114 * typeahead buffer. */
1115 ++no_mapping;
1116 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001117
1118 /* Temporarily disable Recording. If Recording is active, the
1119 * character will be recorded later, since it will be added to the
1120 * typebuf after the loop */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001121 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001122 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001123 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001124 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001126 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001128 --no_mapping;
1129 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001130 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001131 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133#ifdef FEAT_CLIPBOARD
1134 /* Strange way to allow copying (yanking) a modeless selection at
1135 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1136 * Cmdline-mode and it's harmless when there is no selection. */
1137 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1138 {
1139 clip_copy_modeless_selection(TRUE);
1140 c = K_IGNORE;
1141 }
1142#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001143
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001144 /*
1145 * Allow scrolling back in the messages.
1146 * Also accept scroll-down commands when messages fill the screen,
1147 * to avoid that typing one 'j' too many makes the messages
1148 * disappear.
1149 */
1150 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001151 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001152 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1153 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001154 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001155 if (msg_scrolled > Rows)
1156 /* scroll back to show older messages */
1157 do_more_prompt(c);
1158 else
1159 {
1160 msg_didout = FALSE;
1161 c = K_IGNORE;
1162 msg_col =
1163#ifdef FEAT_RIGHTLEFT
1164 cmdmsg_rl ? Columns - 1 :
1165#endif
1166 0;
1167 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001168 if (quit_more)
1169 {
1170 c = CAR; /* just pretend CR was hit */
1171 quit_more = FALSE;
1172 got_int = FALSE;
1173 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001174 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001175 {
1176 c = K_IGNORE;
1177 hit_return_msg();
1178 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001179 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001180 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001181 && (c == 'j' || c == 'd' || c == 'f'
1182 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001183 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 } while ((had_got_int && c == Ctrl_C)
1186 || c == K_IGNORE
1187#ifdef FEAT_GUI
1188 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1189#endif
1190#ifdef FEAT_MOUSE
1191 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1192 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1193 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001194 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001196 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 || (!mouse_has(MOUSE_RETURN)
1198 && mouse_row < msg_row
1199 && (c == K_LEFTMOUSE
1200 || c == K_MIDDLEMOUSE
1201 || c == K_RIGHTMOUSE
1202 || c == K_X1MOUSE
1203 || c == K_X2MOUSE))
1204#endif
1205 );
1206 ui_breakcheck();
1207#ifdef FEAT_MOUSE
1208 /*
1209 * Avoid that the mouse-up event causes visual mode to start.
1210 */
1211 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1212 || c == K_X1MOUSE || c == K_X2MOUSE)
1213 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1214 else
1215#endif
1216 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1217 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001218 /* Put the character back in the typeahead buffer. Don't use the
1219 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001220 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001222 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 redir_off = FALSE;
1226
1227 /*
1228 * If the user hits ':', '?' or '/' we get a command line from the next
1229 * line.
1230 */
1231 if (c == ':' || c == '?' || c == '/')
1232 {
1233 if (!exmode_active)
1234 cmdline_row = msg_row;
1235 skip_redraw = TRUE; /* skip redraw once */
1236 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001237#ifdef FEAT_TERMINAL
1238 skip_term_loop = TRUE;
1239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241
1242 /*
1243 * If the window size changed set_shellsize() will redraw the screen.
1244 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1245 * typed.
1246 */
1247 tmpState = State;
1248 State = oldState; /* restore State before set_shellsize */
1249#ifdef FEAT_MOUSE
1250 setmouse();
1251#endif
1252 msg_check();
1253
1254#if defined(UNIX) || defined(VMS)
1255 /*
1256 * When switching screens, we need to output an extra newline on exit.
1257 */
1258 if (swapping_screen() && !termcap_active)
1259 newline_on_exit = TRUE;
1260#endif
1261
1262 need_wait_return = FALSE;
1263 did_wait_return = TRUE;
1264 emsg_on_display = FALSE; /* can delete error message now */
1265 lines_left = -1; /* reset lines_left at next msg_start() */
1266 reset_last_sourcing();
1267 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1268 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001269 VIM_CLEAR(keep_msg); /* don't redisplay message, it's too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1272 {
1273 starttermcap(); /* start termcap before redrawing */
1274 shell_resized();
1275 }
1276 else if (!skip_redraw
1277 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1278 {
1279 starttermcap(); /* start termcap before redrawing */
1280 redraw_later(VALID);
1281 }
1282}
1283
1284/*
1285 * Write the hit-return prompt.
1286 */
1287 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001288hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001290 int save_p_more = p_more;
1291
1292 p_more = FALSE; /* don't want see this message when scrolling back */
1293 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 msg_putchar('\n');
1295 if (got_int)
1296 MSG_PUTS(_("Interrupt: "));
1297
Bram Moolenaar8820b482017-03-16 17:23:31 +01001298 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (!msg_use_printf())
1300 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001301 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302}
1303
1304/*
1305 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1306 */
1307 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001308set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 vim_free(keep_msg);
1311 if (s != NULL && msg_silent == 0)
1312 keep_msg = vim_strsave(s);
1313 else
1314 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001315 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001316 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317}
1318
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001319#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1320/*
1321 * If there currently is a message being displayed, set "keep_msg" to it, so
1322 * that it will be displayed again after redraw.
1323 */
1324 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001325set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001326{
1327 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1328 && (State & NORMAL))
1329 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1330}
1331#endif
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333/*
1334 * Prepare for outputting characters in the command line.
1335 */
1336 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001337msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339 int did_return = FALSE;
1340
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001341 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001342 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001343
1344#ifdef FEAT_EVAL
1345 if (need_clr_eos)
1346 {
1347 /* Halfway an ":echo" command and getting an (error) message: clear
1348 * any text from the command. */
1349 need_clr_eos = FALSE;
1350 msg_clr_eos();
1351 }
1352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (!msg_scroll && full_screen) /* overwrite last message */
1355 {
1356 msg_row = cmdline_row;
1357 msg_col =
1358#ifdef FEAT_RIGHTLEFT
1359 cmdmsg_rl ? Columns - 1 :
1360#endif
1361 0;
1362 }
1363 else if (msg_didout) /* start message on next line */
1364 {
1365 msg_putchar('\n');
1366 did_return = TRUE;
1367 if (exmode_active != EXMODE_NORMAL)
1368 cmdline_row = msg_row;
1369 }
1370 if (!msg_didany || lines_left < 0)
1371 msg_starthere();
1372 if (msg_silent == 0)
1373 {
1374 msg_didout = FALSE; /* no output on current line yet */
1375 cursor_off();
1376 }
1377
1378 /* when redirecting, may need to start a new line. */
1379 if (!did_return)
1380 redir_write((char_u *)"\n", -1);
1381}
1382
1383/*
1384 * Note that the current msg position is where messages start.
1385 */
1386 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001387msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 lines_left = cmdline_row;
1390 msg_didany = FALSE;
1391}
1392
1393 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001394msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 msg_putchar_attr(c, 0);
1397}
1398
1399 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001400msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402#ifdef FEAT_MBYTE
1403 char_u buf[MB_MAXBYTES + 1];
1404#else
1405 char_u buf[4];
1406#endif
1407
1408 if (IS_SPECIAL(c))
1409 {
1410 buf[0] = K_SPECIAL;
1411 buf[1] = K_SECOND(c);
1412 buf[2] = K_THIRD(c);
1413 buf[3] = NUL;
1414 }
1415 else
1416 {
1417#ifdef FEAT_MBYTE
1418 buf[(*mb_char2bytes)(c, buf)] = NUL;
1419#else
1420 buf[0] = c;
1421 buf[1] = NUL;
1422#endif
1423 }
1424 msg_puts_attr(buf, attr);
1425}
1426
1427 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001428msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430 char_u buf[20];
1431
1432 sprintf((char *)buf, "%ld", n);
1433 msg_puts(buf);
1434}
1435
1436 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001437msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438{
1439 msg_home_replace_attr(fname, 0);
1440}
1441
1442#if defined(FEAT_FIND_ID) || defined(PROTO)
1443 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001444msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001446 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447}
1448#endif
1449
1450 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001451msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 char_u *name;
1454
1455 name = home_replace_save(NULL, fname);
1456 if (name != NULL)
1457 msg_outtrans_attr(name, attr);
1458 vim_free(name);
1459}
1460
1461/*
1462 * Output 'len' characters in 'str' (including NULs) with translation
1463 * if 'len' is -1, output upto a NUL character.
1464 * Use attributes 'attr'.
1465 * Return the number of characters it takes on the screen.
1466 */
1467 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001468msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 return msg_outtrans_attr(str, 0);
1471}
1472
1473 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001474msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1477}
1478
1479 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001480msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
1482 return msg_outtrans_len_attr(str, len, 0);
1483}
1484
1485/*
1486 * Output one character at "p". Return pointer to the next character.
1487 * Handles multi-byte characters.
1488 */
1489 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001490msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
1492#ifdef FEAT_MBYTE
1493 int l;
1494
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001495 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 msg_outtrans_len_attr(p, l, attr);
1498 return p + l;
1499 }
1500#endif
1501 msg_puts_attr(transchar_byte(*p), attr);
1502 return p + 1;
1503}
1504
1505 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001506msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 int retval = 0;
1509 char_u *str = msgstr;
1510 char_u *plain_start = msgstr;
1511 char_u *s;
1512#ifdef FEAT_MBYTE
1513 int mb_l;
1514 int c;
1515#endif
1516
1517 /* if MSG_HIST flag set, add message to history */
1518 if (attr & MSG_HIST)
1519 {
1520 add_msg_hist(str, len, attr);
1521 attr &= ~MSG_HIST;
1522 }
1523
1524#ifdef FEAT_MBYTE
1525 /* If the string starts with a composing character first draw a space on
1526 * which the composing char can be drawn. */
1527 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1528 msg_puts_attr((char_u *)" ", attr);
1529#endif
1530
1531 /*
1532 * Go over the string. Special characters are translated and printed.
1533 * Normal characters are printed several at a time.
1534 */
1535 while (--len >= 0)
1536 {
1537#ifdef FEAT_MBYTE
1538 if (enc_utf8)
1539 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001540 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001542 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 else
1544 mb_l = 1;
1545 if (has_mbyte && mb_l > 1)
1546 {
1547 c = (*mb_ptr2char)(str);
1548 if (vim_isprintc(c))
1549 /* printable multi-byte char: count the cells. */
1550 retval += (*mb_ptr2cells)(str);
1551 else
1552 {
1553 /* unprintable multi-byte char: print the printable chars so
1554 * far and the translation of the unprintable char. */
1555 if (str > plain_start)
1556 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1557 attr);
1558 plain_start = str + mb_l;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001559 msg_puts_attr(transchar(c), attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 retval += char2cells(c);
1561 }
1562 len -= mb_l - 1;
1563 str += mb_l;
1564 }
1565 else
1566#endif
1567 {
1568 s = transchar_byte(*str);
1569 if (s[1] != NUL)
1570 {
1571 /* unprintable char: print the printable chars so far and the
1572 * translation of the unprintable char. */
1573 if (str > plain_start)
1574 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1575 attr);
1576 plain_start = str + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001577 msg_puts_attr(s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001578 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001580 else
1581 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 ++str;
1583 }
1584 }
1585
1586 if (str > plain_start)
1587 /* print the printable chars at the end */
1588 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1589
1590 return retval;
1591}
1592
1593#if defined(FEAT_QUICKFIX) || defined(PROTO)
1594 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001595msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
1597 int i;
1598 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1599
1600 arg = skipwhite(arg);
1601 for (i = 5; *arg && i >= 0; --i)
1602 if (*arg++ != str[i])
1603 break;
1604 if (i < 0)
1605 {
1606 msg_putchar('\n');
1607 for (i = 0; rs[i]; ++i)
1608 msg_putchar(rs[i] - 3);
1609 }
1610}
1611#endif
1612
1613/*
1614 * Output the string 'str' upto a NUL character.
1615 * Return the number of characters it takes on the screen.
1616 *
1617 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1618 * following character and shown as <F1>, <S-Up> etc. Any other character
1619 * which is not printable shown in <> form.
1620 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1621 * If a character is displayed in one of these special ways, is also
1622 * highlighted (its highlight name is '8' in the p_hl variable).
1623 * Otherwise characters are not highlighted.
1624 * This function is used to show mappings, where we want to see how to type
1625 * the character/string -- webb
1626 */
1627 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001628msg_outtrans_special(
1629 char_u *strstart,
1630 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 char_u *str = strstart;
1633 int retval = 0;
1634 char_u *string;
1635 int attr;
1636 int len;
1637
Bram Moolenaar8820b482017-03-16 17:23:31 +01001638 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 while (*str != NUL)
1640 {
1641 /* Leading and trailing spaces need to be displayed in <> form. */
1642 if ((str == strstart || str[1] == NUL) && *str == ' ')
1643 {
1644 string = (char_u *)"<Space>";
1645 ++str;
1646 }
1647 else
1648 string = str2special(&str, from);
1649 len = vim_strsize(string);
1650 /* Highlight special keys */
1651 msg_puts_attr(string, len > 1
1652#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001653 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
1655 ? attr : 0);
1656 retval += len;
1657 }
1658 return retval;
1659}
1660
Bram Moolenaarbd743252010-10-20 21:23:33 +02001661#if defined(FEAT_EVAL) || defined(PROTO)
1662/*
1663 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1664 * strings, in an allocated string.
1665 */
1666 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001667str2special_save(
1668 char_u *str,
1669 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001670{
1671 garray_T ga;
1672 char_u *p = str;
1673
1674 ga_init2(&ga, 1, 40);
1675 while (*p != NUL)
1676 ga_concat(&ga, str2special(&p, is_lhs));
1677 ga_append(&ga, NUL);
1678 return (char_u *)ga.ga_data;
1679}
1680#endif
1681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682/*
1683 * Return the printable string for the key codes at "*sp".
1684 * Used for translating the lhs or rhs of a mapping to printable chars.
1685 * Advances "sp" to the next code.
1686 */
1687 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001688str2special(
1689 char_u **sp,
1690 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 int c;
1693 static char_u buf[7];
1694 char_u *str = *sp;
1695 int modifiers = 0;
1696 int special = FALSE;
1697
1698#ifdef FEAT_MBYTE
1699 if (has_mbyte)
1700 {
1701 char_u *p;
1702
1703 /* Try to un-escape a multi-byte character. Return the un-escaped
1704 * string if it is a multi-byte character. */
1705 p = mb_unescape(sp);
1706 if (p != NULL)
1707 return p;
1708 }
1709#endif
1710
1711 c = *str;
1712 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1713 {
1714 if (str[1] == KS_MODIFIER)
1715 {
1716 modifiers = str[2];
1717 str += 3;
1718 c = *str;
1719 }
1720 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1721 {
1722 c = TO_SPECIAL(str[1], str[2]);
1723 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
1725 if (IS_SPECIAL(c) || modifiers) /* special key */
1726 special = TRUE;
1727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001730 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001732 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001733
1734 /* For multi-byte characters check for an illegal byte. */
1735 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1736 {
1737 transchar_nonprint(buf, c);
1738 *sp = str + 1;
1739 return buf;
1740 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001741 /* Since 'special' is TRUE the multi-byte character 'c' will be
1742 * processed by get_special_key_name() */
1743 c = (*mb_ptr2char)(str);
1744 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001746 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001748 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1751 * Use <Space> only for lhs of a mapping. */
1752 if (special || char2cells(c) > 1 || (from && c == ' '))
1753 return get_special_key_name(c, modifiers);
1754 buf[0] = c;
1755 buf[1] = NUL;
1756 return buf;
1757}
1758
1759/*
1760 * Translate a key sequence into special key names.
1761 */
1762 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001763str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 char_u *s;
1766
1767 *buf = NUL;
1768 while (*sp)
1769 {
1770 s = str2special(&sp, FALSE);
1771 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1772 STRCAT(buf, s);
1773 }
1774}
1775
1776/*
1777 * print line for :print or :list command
1778 */
1779 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001780msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 int c;
1783 int col = 0;
1784 int n_extra = 0;
1785 int c_extra = 0;
1786 char_u *p_extra = NULL; /* init to make SASC shut up */
1787 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001788 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 char_u *trail = NULL;
1790#ifdef FEAT_MBYTE
1791 int l;
1792 char_u buf[MB_MAXBYTES + 1];
1793#endif
1794
Bram Moolenaardf177f62005-02-22 08:39:57 +00001795 if (curwin->w_p_list)
1796 list = TRUE;
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001799 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
1801 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001802 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 --trail;
1804 }
1805
1806 /* output a space for an empty line, otherwise the line will be
1807 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001808 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 msg_putchar(' ');
1810
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001811 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001813 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
1815 --n_extra;
1816 if (c_extra)
1817 c = c_extra;
1818 else
1819 c = *p_extra++;
1820 }
1821#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001822 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001825 if (lcs_nbsp != NUL && list
1826 && (mb_ptr2char(s) == 160
1827 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001828 {
1829 mb_char2bytes(lcs_nbsp, buf);
1830 buf[(*mb_ptr2len)(buf)] = NUL;
1831 }
1832 else
1833 {
1834 mch_memmove(buf, s, (size_t)l);
1835 buf[l] = NUL;
1836 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001837 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 s += l;
1839 continue;
1840 }
1841#endif
1842 else
1843 {
1844 attr = 0;
1845 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001846 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001849#ifdef FEAT_VARTABS
1850 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1851 curbuf->b_p_vts_array) - 1;
1852#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001854#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001855 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 c = ' ';
1858 c_extra = ' ';
1859 }
1860 else
1861 {
1862 c = lcs_tab1;
1863 c_extra = lcs_tab2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001864 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001867 else if (c == 160 && list && lcs_nbsp != NUL)
1868 {
1869 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001870 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001871 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001872 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
1874 p_extra = (char_u *)"";
1875 c_extra = NUL;
1876 n_extra = 1;
1877 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001878 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 --s;
1880 }
1881 else if (c != NUL && (n = byte2cells(c)) > 1)
1882 {
1883 n_extra = n - 1;
1884 p_extra = transchar_byte(c);
1885 c_extra = NUL;
1886 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001887 /* Use special coloring to be able to distinguish <hex> from
1888 * the same in plain text. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001889 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
1891 else if (c == ' ' && trail != NULL && s > trail)
1892 {
1893 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001894 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001896 else if (c == ' ' && list && lcs_space != NUL)
1897 {
1898 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001899 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902
1903 if (c == NUL)
1904 break;
1905
1906 msg_putchar_attr(c, attr);
1907 col++;
1908 }
1909 msg_clr_eos();
1910}
1911
1912#ifdef FEAT_MBYTE
1913/*
1914 * Use screen_puts() to output one multi-byte character.
1915 * Return the pointer "s" advanced to the next character.
1916 */
1917 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001918screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 int cw;
1921
1922 msg_didout = TRUE; /* remember that line is not empty */
1923 cw = (*mb_ptr2cells)(s);
1924 if (cw > 1 && (
1925#ifdef FEAT_RIGHTLEFT
1926 cmdmsg_rl ? msg_col <= 1 :
1927#endif
1928 msg_col == Columns - 1))
1929 {
1930 /* Doesn't fit, print a highlighted '>' to fill it up. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001931 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 return s;
1933 }
1934
1935 screen_puts_len(s, l, msg_row, msg_col, attr);
1936#ifdef FEAT_RIGHTLEFT
1937 if (cmdmsg_rl)
1938 {
1939 msg_col -= cw;
1940 if (msg_col == 0)
1941 {
1942 msg_col = Columns;
1943 ++msg_row;
1944 }
1945 }
1946 else
1947#endif
1948 {
1949 msg_col += cw;
1950 if (msg_col >= Columns)
1951 {
1952 msg_col = 0;
1953 ++msg_row;
1954 }
1955 }
1956 return s + l;
1957}
1958#endif
1959
1960/*
1961 * Output a string to the screen at position msg_row, msg_col.
1962 * Update msg_row and msg_col for the next message.
1963 */
1964 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001965msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001967 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968}
1969
1970 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001971msg_puts_title(
1972 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001974 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975}
1976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977/*
1978 * Show a message in such a way that it always fits in the line. Cut out a
1979 * part in the middle and replace it with "..." when necessary.
1980 * Does not handle multi-byte characters!
1981 */
1982 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001983msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001985 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986}
1987
1988 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001989msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 int slen = len;
1992 int room;
1993
1994 room = Columns - msg_col;
1995 if (len > room && room >= 20)
1996 {
1997 slen = (room - 3) / 2;
1998 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01001999 msg_puts_attr((char_u *)"...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2002}
2003
2004/*
2005 * Basic function for writing a message with highlight attributes.
2006 */
2007 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002008msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 msg_puts_attr_len(s, -1, attr);
2011}
2012
2013/*
2014 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2015 * When "maxlen" is -1 there is no maximum length.
2016 * When "maxlen" is >= 0 the message is not put in the history.
2017 */
2018 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002019msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 /*
2022 * If redirection is on, also write to the redirection file.
2023 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002024 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 /*
2027 * Don't print anything when using ":silent cmd".
2028 */
2029 if (msg_silent != 0)
2030 return;
2031
2032 /* if MSG_HIST flag set, add message to history */
2033 if ((attr & MSG_HIST) && maxlen < 0)
2034 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002035 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 attr &= ~MSG_HIST;
2037 }
2038
2039 /*
2040 * When writing something to the screen after it has scrolled, requires a
2041 * wait-return prompt later. Needed when scrolling, resetting
2042 * need_wait_return after some prompt, and then outputting something
2043 * without scrolling
2044 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002045 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 need_wait_return = TRUE;
2047 msg_didany = TRUE; /* remember that something was outputted */
2048
2049 /*
2050 * If there is no valid screen, use fprintf so we can see error messages.
2051 * If termcap is not active, we may be writing in an alternate console
2052 * window, cursor positioning may not work correctly (window size may be
2053 * different, e.g. for Win32 console) or we just don't know where the
2054 * cursor is.
2055 */
2056 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002057 msg_puts_printf(str, maxlen);
2058 else
2059 msg_puts_display(str, maxlen, attr, FALSE);
2060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002062/*
2063 * The display part of msg_puts_attr_len().
2064 * May be called recursively to display scroll-back text.
2065 */
2066 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002067msg_puts_display(
2068 char_u *str,
2069 int maxlen,
2070 int attr,
2071 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002072{
2073 char_u *s = str;
2074 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2075 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
2076#ifdef FEAT_MBYTE
2077 int l;
2078 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002080 char_u *sb_str = str;
2081 int sb_col = msg_col;
2082 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002083 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002086 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
2088 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002089 * We are at the end of the screen line when:
2090 * - When outputting a newline.
2091 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002093 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094#ifdef FEAT_RIGHTLEFT
2095 cmdmsg_rl
2096 ? (
2097 msg_col <= 1
2098 || (*s == TAB && msg_col <= 7)
2099# ifdef FEAT_MBYTE
2100 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
2101# endif
2102 )
2103 :
2104#endif
2105 (msg_col + t_col >= Columns - 1
2106 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
2107# ifdef FEAT_MBYTE
2108 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2109 && msg_col + t_col >= Columns - 2)
2110# endif
2111 ))))
2112 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002113 /*
2114 * The screen is scrolled up when at the last row (some terminals
2115 * scroll automatically, some don't. To avoid problems we scroll
2116 * ourselves).
2117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002120 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002122 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (msg_no_more && lines_left == 0)
2124 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002126 /* Scroll the screen up one line. */
2127 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 msg_row = Rows - 2;
2130 if (msg_col >= Columns) /* can happen after screen resize */
2131 msg_col = Columns - 1;
2132
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002133 /* Display char in last column before showing more-prompt. */
2134 if (*s >= ' '
2135#ifdef FEAT_RIGHTLEFT
2136 && !cmdmsg_rl
2137#endif
2138 )
2139 {
2140#ifdef FEAT_MBYTE
2141 if (has_mbyte)
2142 {
2143 if (enc_utf8 && maxlen >= 0)
2144 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002145 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002146 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002147 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148 s = screen_puts_mbyte(s, l, attr);
2149 }
2150 else
2151#endif
2152 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002153 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002154 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002155 else
2156 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002157
2158 if (p_more)
2159 /* store text for scrolling back */
2160 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2161
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002162 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002163 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 redraw_cmdline = TRUE;
2165 if (cmdline_row > 0 && !exmode_active)
2166 --cmdline_row;
2167
2168 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002169 * If screen is completely filled and 'more' is set then wait
2170 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002172 if (lines_left > 0)
2173 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002174 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 && !msg_no_more && !exmode_active)
2176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002178 if (do_more_prompt(NUL))
2179 s = confirm_msg_tail;
2180#else
2181 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182#endif
2183 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002186
2187 /* When we displayed a char in last column need to check if there
2188 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002189 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002190 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 }
2192
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002193 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 || msg_col + t_col >= Columns
2195#ifdef FEAT_MBYTE
2196 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2197 && msg_col + t_col >= Columns - 1)
2198#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002199 ;
2200 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2201 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002203 t_puts(&t_col, t_s, s, attr);
2204
2205 if (wrap && p_more && !recurse)
2206 /* store text for scrolling back */
2207 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 if (*s == '\n') /* go to next line */
2210 {
2211 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002212#ifdef FEAT_RIGHTLEFT
2213 if (cmdmsg_rl)
2214 msg_col = Columns - 1;
2215 else
2216#endif
2217 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (++msg_row >= Rows) /* safety check */
2219 msg_row = Rows - 1;
2220 }
2221 else if (*s == '\r') /* go to column 0 */
2222 {
2223 msg_col = 0;
2224 }
2225 else if (*s == '\b') /* go to previous char */
2226 {
2227 if (msg_col)
2228 --msg_col;
2229 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002230 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 do
2233 msg_screen_putchar(' ', attr);
2234 while (msg_col & 7);
2235 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002236 else if (*s == BELL) /* beep (from ":sh") */
2237 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 else
2239 {
2240#ifdef FEAT_MBYTE
2241 if (has_mbyte)
2242 {
2243 cw = (*mb_ptr2cells)(s);
2244 if (enc_utf8 && maxlen >= 0)
2245 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002246 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002248 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250 else
2251 {
2252 cw = 1;
2253 l = 1;
2254 }
2255#endif
2256 /* When drawing from right to left or when a double-wide character
2257 * doesn't fit, draw a single character here. Otherwise collect
2258 * characters and draw them all at once later. */
2259#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2260 if (
2261# ifdef FEAT_RIGHTLEFT
2262 cmdmsg_rl
2263# ifdef FEAT_MBYTE
2264 ||
2265# endif
2266# endif
2267# ifdef FEAT_MBYTE
2268 (cw > 1 && msg_col + t_col >= Columns - 1)
2269# endif
2270 )
2271 {
2272# ifdef FEAT_MBYTE
2273 if (l > 1)
2274 s = screen_puts_mbyte(s, l, attr) - 1;
2275 else
2276# endif
2277 msg_screen_putchar(*s, attr);
2278 }
2279 else
2280#endif
2281 {
2282 /* postpone this character until later */
2283 if (t_col == 0)
2284 t_s = s;
2285#ifdef FEAT_MBYTE
2286 t_col += cw;
2287 s += l - 1;
2288#else
2289 ++t_col;
2290#endif
2291 }
2292 }
2293 ++s;
2294 }
2295
2296 /* output any postponed text */
2297 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002298 t_puts(&t_col, t_s, s, attr);
2299 if (p_more && !recurse)
2300 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
2302 msg_check();
2303}
2304
2305/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002306 * Return TRUE when ":filter pattern" was used and "msg" does not match
2307 * "pattern".
2308 */
2309 int
2310message_filtered(char_u *msg)
2311{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002312 int match;
2313
2314 if (cmdmod.filter_regmatch.regprog == NULL)
2315 return FALSE;
2316 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2317 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002318}
2319
2320/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002321 * Scroll the screen up one line for displaying the next message line.
2322 */
2323 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002324msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002325{
2326#ifdef FEAT_GUI
2327 /* Remove the cursor before scrolling, ScreenLines[] is going
2328 * to become invalid. */
2329 if (gui.in_use)
2330 gui_undraw_cursor();
2331#endif
2332 /* scrolling up always works */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002333 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002334 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002335 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002336
2337 if (!can_clear((char_u *)" "))
2338 {
2339 /* Scrolling up doesn't result in the right background. Set the
2340 * background here. It's not efficient, but avoids that we have to do
2341 * it all over the code. */
2342 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2343
2344 /* Also clear the last char of the last but one line if it was not
2345 * cleared before to avoid a scroll-up. */
2346 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2347 screen_fill((int)Rows - 2, (int)Rows - 1,
2348 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2349 }
2350}
2351
2352/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002353 * Increment "msg_scrolled".
2354 */
2355 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002356inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002357{
2358#ifdef FEAT_EVAL
2359 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2360 {
2361 char_u *p = sourcing_name;
2362 char_u *tofree = NULL;
2363 int len;
2364
2365 /* v:scrollstart is empty, set it to the script/function name and line
2366 * number */
2367 if (p == NULL)
2368 p = (char_u *)_("Unknown");
2369 else
2370 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002371 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002372 tofree = alloc(len);
2373 if (tofree != NULL)
2374 {
2375 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2376 p, (long)sourcing_lnum);
2377 p = tofree;
2378 }
2379 }
2380 set_vim_var_string(VV_SCROLLSTART, p, -1);
2381 vim_free(tofree);
2382 }
2383#endif
2384 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002385 if (must_redraw < VALID)
2386 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002387}
2388
2389/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002390 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2391 * store the displayed text and remember where screen lines start.
2392 */
2393typedef struct msgchunk_S msgchunk_T;
2394struct msgchunk_S
2395{
2396 msgchunk_T *sb_next;
2397 msgchunk_T *sb_prev;
2398 char sb_eol; /* TRUE when line ends after this text */
2399 int sb_msg_col; /* column in which text starts */
2400 int sb_attr; /* text attributes */
2401 char_u sb_text[1]; /* text to be displayed, actually longer */
2402};
2403
2404static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2405
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002406static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002407
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002408typedef enum {
2409 SB_CLEAR_NONE = 0,
2410 SB_CLEAR_ALL,
2411 SB_CLEAR_CMDLINE_BUSY,
2412 SB_CLEAR_CMDLINE_DONE
2413} sb_clear_T;
2414
2415/* When to clear text on next msg. */
2416static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002417
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002418/*
2419 * Store part of a printed message for displaying when scrolling back.
2420 */
2421 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002422store_sb_text(
2423 char_u **sb_str, /* start of string */
2424 char_u *s, /* just after string */
2425 int attr,
2426 int *sb_col,
2427 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002428{
2429 msgchunk_T *mp;
2430
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002431 if (do_clear_sb_text == SB_CLEAR_ALL
2432 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002433 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002434 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2435 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002436 }
2437
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002438 if (s > *sb_str)
2439 {
2440 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2441 if (mp != NULL)
2442 {
2443 mp->sb_eol = finish;
2444 mp->sb_msg_col = *sb_col;
2445 mp->sb_attr = attr;
2446 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2447
2448 if (last_msgchunk == NULL)
2449 {
2450 last_msgchunk = mp;
2451 mp->sb_prev = NULL;
2452 }
2453 else
2454 {
2455 mp->sb_prev = last_msgchunk;
2456 last_msgchunk->sb_next = mp;
2457 last_msgchunk = mp;
2458 }
2459 mp->sb_next = NULL;
2460 }
2461 }
2462 else if (finish && last_msgchunk != NULL)
2463 last_msgchunk->sb_eol = TRUE;
2464
2465 *sb_str = s;
2466 *sb_col = 0;
2467}
2468
2469/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002470 * Finished showing messages, clear the scroll-back text on the next message.
2471 */
2472 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002473may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002474{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002475 do_clear_sb_text = SB_CLEAR_ALL;
2476}
2477
2478/*
2479 * Starting to edit the command line, do not clear messages now.
2480 */
2481 void
2482sb_text_start_cmdline(void)
2483{
2484 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2485 msg_sb_eol();
2486}
2487
2488/*
2489 * Ending to edit the command line. Clear old lines but the last one later.
2490 */
2491 void
2492sb_text_end_cmdline(void)
2493{
2494 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002495}
2496
2497/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002498 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002499 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002500 * Called when redrawing the screen.
2501 */
2502 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002503clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002504{
2505 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002506 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002507
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002508 if (all)
2509 lastp = &last_msgchunk;
2510 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002511 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002512 if (last_msgchunk == NULL)
2513 return;
2514 lastp = &last_msgchunk->sb_prev;
2515 }
2516
2517 while (*lastp != NULL)
2518 {
2519 mp = (*lastp)->sb_prev;
2520 vim_free(*lastp);
2521 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002522 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002523}
2524
2525/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002526 * "g<" command.
2527 */
2528 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002529show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002530{
2531 msgchunk_T *mp;
2532
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002533 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002534 * weird, typing a command without output results in one line. */
2535 mp = msg_sb_start(last_msgchunk);
2536 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002537 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002538 else
2539 {
2540 do_more_prompt('G');
2541 wait_return(FALSE);
2542 }
2543}
2544
2545/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546 * Move to the start of screen line in already displayed text.
2547 */
2548 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002549msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002550{
2551 msgchunk_T *mp = mps;
2552
2553 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2554 mp = mp->sb_prev;
2555 return mp;
2556}
2557
2558/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002559 * Mark the last message chunk as finishing the line.
2560 */
2561 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002562msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002563{
2564 if (last_msgchunk != NULL)
2565 last_msgchunk->sb_eol = TRUE;
2566}
2567
2568/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002569 * Display a screen line from previously displayed text at row "row".
2570 * Returns a pointer to the text for the next line (can be NULL).
2571 */
2572 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002573disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002574{
2575 msgchunk_T *mp = smp;
2576 char_u *p;
2577
2578 for (;;)
2579 {
2580 msg_row = row;
2581 msg_col = mp->sb_msg_col;
2582 p = mp->sb_text;
2583 if (*p == '\n') /* don't display the line break */
2584 ++p;
2585 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2586 if (mp->sb_eol || mp->sb_next == NULL)
2587 break;
2588 mp = mp->sb_next;
2589 }
2590 return mp->sb_next;
2591}
2592
2593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * Output any postponed text for msg_puts_attr_len().
2595 */
2596 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002597t_puts(
2598 int *t_col,
2599 char_u *t_s,
2600 char_u *s,
2601 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 /* output postponed text */
2604 msg_didout = TRUE; /* remember that line is not empty */
2605 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 msg_col += *t_col;
2607 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608#ifdef FEAT_MBYTE
2609 /* If the string starts with a composing character don't increment the
2610 * column position for it. */
2611 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2612 --msg_col;
2613#endif
2614 if (msg_col >= Columns)
2615 {
2616 msg_col = 0;
2617 ++msg_row;
2618 }
2619}
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621/*
2622 * Returns TRUE when messages should be printed with mch_errmsg().
2623 * This is used when there is no valid screen, so we can see error messages.
2624 * If termcap is not active, we may be writing in an alternate console
2625 * window, cursor positioning may not work correctly (window size may be
2626 * different, e.g. for Win32 console) or we just don't know where the
2627 * cursor is.
2628 */
2629 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002630msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 return (!msg_check_screen()
2633#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2634 || !termcap_active
2635#endif
2636 || (swapping_screen() && !termcap_active)
2637 );
2638}
2639
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002640/*
2641 * Print a message when there is no valid screen.
2642 */
2643 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002644msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645{
2646 char_u *s = str;
2647 char_u buf[4];
2648 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002649#ifdef WIN3264
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002650# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2651 char_u *ccp = NULL;
2652
2653# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654 if (!(silent_mode && p_verbose == 0))
2655 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002656
2657# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2658 if (enc_codepage >= 0 && (int)GetConsoleCP() != enc_codepage)
2659 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02002660 int inlen = (int)STRLEN(str);
Bram Moolenaar1b66c002017-08-03 18:55:00 +02002661 int outlen;
2662 WCHAR *widestr = (WCHAR *)enc_to_utf16(str, &inlen);
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002663
2664 if (widestr != NULL)
2665 {
Bram Moolenaar1b66c002017-08-03 18:55:00 +02002666 WideCharToMultiByte_alloc(GetConsoleCP(), 0, widestr, inlen,
2667 (LPSTR *)&ccp, &outlen, 0, 0);
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002668 vim_free(widestr);
2669 s = str = ccp;
2670 }
2671 }
2672# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002673#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002674 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002675 {
2676 if (!(silent_mode && p_verbose == 0))
2677 {
2678 /* NL --> CR NL translation (for Unix, not for "--version") */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002679 p = &buf[0];
2680 if (*s == '\n' && !info_message)
2681 *p++ = '\r';
Bram Moolenaard0573012017-10-28 21:11:06 +02002682#if defined(USE_CR)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002683 else
2684#endif
2685 *p++ = *s;
2686 *p = '\0';
2687 if (info_message) /* informative message, not an error */
2688 mch_msg((char *)buf);
2689 else
2690 mch_errmsg((char *)buf);
2691 }
2692
2693 /* primitive way to compute the current column */
2694#ifdef FEAT_RIGHTLEFT
2695 if (cmdmsg_rl)
2696 {
2697 if (*s == '\r' || *s == '\n')
2698 msg_col = Columns - 1;
2699 else
2700 --msg_col;
2701 }
2702 else
2703#endif
2704 {
2705 if (*s == '\r' || *s == '\n')
2706 msg_col = 0;
2707 else
2708 ++msg_col;
2709 }
2710 ++s;
2711 }
2712 msg_didout = TRUE; /* assume that line is not empty */
2713
2714#ifdef WIN3264
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002715# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2716 vim_free(ccp);
2717# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718 if (!(silent_mode && p_verbose == 0))
2719 mch_settmode(TMODE_RAW);
2720#endif
2721}
2722
2723/*
2724 * Show the more-prompt and handle the user response.
2725 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002726 * When at hit-enter prompt "typed_char" is the already typed character,
2727 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2729 */
2730 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002731do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002732{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002733 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002734 int used_typed_char = typed_char;
2735 int oldState = State;
2736 int c;
2737#ifdef FEAT_CON_DIALOG
2738 int retval = FALSE;
2739#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002740 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002741 msgchunk_T *mp_last = NULL;
2742 msgchunk_T *mp;
2743 int i;
2744
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002745 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002746 * that case don't show another prompt. Also when at the hit-Enter prompt
2747 * and nothing was typed. */
2748 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002749 return FALSE;
2750 entered = TRUE;
2751
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002752 if (typed_char == 'G')
2753 {
2754 /* "g<": Find first line on the last page. */
2755 mp_last = msg_sb_start(last_msgchunk);
2756 for (i = 0; i < Rows - 2 && mp_last != NULL
2757 && mp_last->sb_prev != NULL; ++i)
2758 mp_last = msg_sb_start(mp_last->sb_prev);
2759 }
2760
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002761 State = ASKMORE;
2762#ifdef FEAT_MOUSE
2763 setmouse();
2764#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002765 if (typed_char == NUL)
2766 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002767 for (;;)
2768 {
2769 /*
2770 * Get a typed character directly from the user.
2771 */
2772 if (used_typed_char != NUL)
2773 {
2774 c = used_typed_char; /* was typed at hit-enter prompt */
2775 used_typed_char = NUL;
2776 }
2777 else
2778 c = get_keystroke();
2779
2780#if defined(FEAT_MENU) && defined(FEAT_GUI)
2781 if (c == K_MENU)
2782 {
2783 int idx = get_menu_index(current_menu, ASKMORE);
2784
2785 /* Used a menu. If it starts with CTRL-Y, it must
2786 * be a "Copy" for the clipboard. Otherwise
2787 * assume that we end */
2788 if (idx == MENU_INDEX_INVALID)
2789 continue;
2790 c = *current_menu->strings[idx];
2791 if (c != NUL && current_menu->strings[idx][1] != NUL)
2792 ins_typebuf(current_menu->strings[idx] + 1,
2793 current_menu->noremap[idx], 0, TRUE,
2794 current_menu->silent[idx]);
2795 }
2796#endif
2797
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002798 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 switch (c)
2800 {
2801 case BS: /* scroll one line back */
2802 case K_BS:
2803 case 'k':
2804 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002805 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002806 break;
2807
2808 case CAR: /* one extra line */
2809 case NL:
2810 case 'j':
2811 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002812 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 break;
2814
2815 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002816 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 break;
2818
2819 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002820 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821 break;
2822
2823 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002824 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002825 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002826 break;
2827
2828 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002829 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002830 case K_PAGEDOWN:
2831 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002832 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002833 break;
2834
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002835 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002836 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002837 break;
2838
2839 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002840 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002841 lines_left = 999999;
2842 break;
2843
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002844 case ':': /* start new command line */
2845#ifdef FEAT_CON_DIALOG
2846 if (!confirm_msg_used)
2847#endif
2848 {
2849 /* Since got_int is set all typeahead will be flushed, but we
2850 * want to keep this ':', remember that in a special way. */
2851 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002852#ifdef FEAT_TERMINAL
2853 skip_term_loop = TRUE;
2854#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002855 cmdline_row = Rows - 1; /* put ':' on this line */
2856 skip_redraw = TRUE; /* skip redraw once */
2857 need_wait_return = FALSE; /* don't wait in main() */
2858 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002859 /* FALLTHROUGH */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002860 case 'q': /* quit */
2861 case Ctrl_C:
2862 case ESC:
2863#ifdef FEAT_CON_DIALOG
2864 if (confirm_msg_used)
2865 {
2866 /* Jump to the choices of the dialog. */
2867 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002868 }
2869 else
2870#endif
2871 {
2872 got_int = TRUE;
2873 quit_more = TRUE;
2874 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002875 /* When there is some more output (wrapping line) display that
2876 * without another prompt. */
2877 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 break;
2879
2880#ifdef FEAT_CLIPBOARD
2881 case Ctrl_Y:
2882 /* Strange way to allow copying (yanking) a modeless
2883 * selection at the more prompt. Use CTRL-Y,
2884 * because the same is used in Cmdline-mode and at the
2885 * hit-enter prompt. However, scrolling one line up
2886 * might be expected... */
2887 if (clip_star.state == SELECT_DONE)
2888 clip_copy_modeless_selection(TRUE);
2889 continue;
2890#endif
2891 default: /* no valid response */
2892 msg_moremsg(TRUE);
2893 continue;
2894 }
2895
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002896 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002897 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002898 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002899 {
2900 /* go to start of last line */
2901 if (mp_last == NULL)
2902 mp = msg_sb_start(last_msgchunk);
2903 else if (mp_last->sb_prev != NULL)
2904 mp = msg_sb_start(mp_last->sb_prev);
2905 else
2906 mp = NULL;
2907
2908 /* go to start of line at top of the screen */
2909 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2910 ++i)
2911 mp = msg_sb_start(mp->sb_prev);
2912
2913 if (mp != NULL && mp->sb_prev != NULL)
2914 {
2915 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002916 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002917 {
2918 if (mp == NULL || mp->sb_prev == NULL)
2919 break;
2920 mp = msg_sb_start(mp->sb_prev);
2921 if (mp_last == NULL)
2922 mp_last = msg_sb_start(last_msgchunk);
2923 else
2924 mp_last = msg_sb_start(mp_last->sb_prev);
2925 }
2926
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002927 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002928 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002929 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002930 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002931 (void)disp_sb_line(0, mp);
2932 }
2933 else
2934 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002935 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002937 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2938 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002939 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002940 ++msg_scrolled;
2941 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002942 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002943 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 }
2945 }
2946 else
2947 {
2948 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002949 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002950 {
2951 /* scroll up, display line at bottom */
2952 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002953 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2955 (int)Columns, ' ', ' ', 0);
2956 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002957 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002958 }
2959 }
2960
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002961 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002962 {
2963 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002964 screen_fill((int)Rows - 1, (int)Rows, 0,
2965 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 msg_moremsg(FALSE);
2967 continue;
2968 }
2969
2970 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002971 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002972 }
2973
2974 break;
2975 }
2976
2977 /* clear the --more-- message */
2978 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2979 State = oldState;
2980#ifdef FEAT_MOUSE
2981 setmouse();
2982#endif
2983 if (quit_more)
2984 {
2985 msg_row = Rows - 1;
2986 msg_col = 0;
2987 }
2988#ifdef FEAT_RIGHTLEFT
2989 else if (cmdmsg_rl)
2990 msg_col = Columns - 1;
2991#endif
2992
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002993 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002994#ifdef FEAT_CON_DIALOG
2995 return retval;
2996#else
2997 return FALSE;
2998#endif
2999}
3000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3002
3003#ifdef mch_errmsg
3004# undef mch_errmsg
3005#endif
3006#ifdef mch_msg
3007# undef mch_msg
3008#endif
3009
3010/*
3011 * Give an error message. To be used when the screen hasn't been initialized
3012 * yet. When stderr can't be used, collect error messages until the GUI has
3013 * started and they can be displayed in a message box.
3014 */
3015 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003016mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 int len;
3019
3020#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3021 /* On Unix use stderr if it's a tty.
3022 * When not going to start the GUI also use stderr.
3023 * On Mac, when started from Finder, stderr is the console. */
3024 if (
3025# ifdef UNIX
Bram Moolenaard0573012017-10-28 21:11:06 +02003026# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3028# else
3029 isatty(2)
3030# endif
3031# ifdef FEAT_GUI
3032 ||
3033# endif
3034# endif
3035# ifdef FEAT_GUI
3036 !(gui.in_use || gui.starting)
3037# endif
3038 )
3039 {
3040 fprintf(stderr, "%s", str);
3041 return;
3042 }
3043#endif
3044
3045 /* avoid a delay for a message that isn't there */
3046 emsg_on_display = FALSE;
3047
3048 len = (int)STRLEN(str) + 1;
3049 if (error_ga.ga_growsize == 0)
3050 {
3051 error_ga.ga_growsize = 80;
3052 error_ga.ga_itemsize = 1;
3053 }
3054 if (ga_grow(&error_ga, len) == OK)
3055 {
3056 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3057 (char_u *)str, len);
3058#ifdef UNIX
3059 /* remove CR characters, they are displayed */
3060 {
3061 char_u *p;
3062
3063 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3064 for (;;)
3065 {
3066 p = vim_strchr(p, '\r');
3067 if (p == NULL)
3068 break;
3069 *p = ' ';
3070 }
3071 }
3072#endif
3073 --len; /* don't count the NUL at the end */
3074 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076}
3077
3078/*
3079 * Give a message. To be used when the screen hasn't been initialized yet.
3080 * When there is no tty, collect messages until the GUI has started and they
3081 * can be displayed in a message box.
3082 */
3083 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003084mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3087 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3088 * uses mch_errmsg() when started from the desktop.
3089 * When not going to start the GUI also use stdout.
3090 * On Mac, when started from Finder, stderr is the console. */
3091 if (
3092# ifdef UNIX
Bram Moolenaard0573012017-10-28 21:11:06 +02003093# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3095# else
3096 isatty(2)
3097# endif
3098# ifdef FEAT_GUI
3099 ||
3100# endif
3101# endif
3102# ifdef FEAT_GUI
3103 !(gui.in_use || gui.starting)
3104# endif
3105 )
3106 {
3107 printf("%s", str);
3108 return;
3109 }
3110# endif
3111 mch_errmsg(str);
3112}
3113#endif /* USE_MCH_ERRMSG */
3114
3115/*
3116 * Put a character on the screen at the current message position and advance
3117 * to the next position. Only for printable ASCII!
3118 */
3119 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003120msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 msg_didout = TRUE; /* remember that line is not empty */
3123 screen_putchar(c, msg_row, msg_col, attr);
3124#ifdef FEAT_RIGHTLEFT
3125 if (cmdmsg_rl)
3126 {
3127 if (--msg_col == 0)
3128 {
3129 msg_col = Columns;
3130 ++msg_row;
3131 }
3132 }
3133 else
3134#endif
3135 {
3136 if (++msg_col >= Columns)
3137 {
3138 msg_col = 0;
3139 ++msg_row;
3140 }
3141 }
3142}
3143
3144 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003145msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003147 int attr;
3148 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
Bram Moolenaar8820b482017-03-16 17:23:31 +01003150 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003151 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003153 screen_puts((char_u *)
3154 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3155 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158/*
3159 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3160 * exmode_active.
3161 */
3162 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003163repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165 if (State == ASKMORE)
3166 {
3167 msg_moremsg(TRUE); /* display --more-- message again */
3168 msg_row = Rows - 1;
3169 }
3170#ifdef FEAT_CON_DIALOG
3171 else if (State == CONFIRM)
3172 {
3173 display_confirm_msg(); /* display ":confirm" message again */
3174 msg_row = Rows - 1;
3175 }
3176#endif
3177 else if (State == EXTERNCMD)
3178 {
3179 windgoto(msg_row, msg_col); /* put cursor back */
3180 }
3181 else if (State == HITRETURN || State == SETWSIZE)
3182 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003183 if (msg_row == Rows - 1)
3184 {
3185 /* Avoid drawing the "hit-enter" prompt below the previous one,
3186 * overwrite it. Esp. useful when regaining focus and a
3187 * FocusGained autocmd exists but didn't draw anything. */
3188 msg_didout = FALSE;
3189 msg_col = 0;
3190 msg_clr_eos();
3191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 hit_return_msg();
3193 msg_row = Rows - 1;
3194 }
3195}
3196
3197/*
3198 * msg_check_screen - check if the screen is initialized.
3199 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3200 * While starting the GUI the terminal codes will be set for the GUI, but the
3201 * output goes to the terminal. Don't use the terminal codes then.
3202 */
3203 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003204msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
3206 if (!full_screen || !screen_valid(FALSE))
3207 return FALSE;
3208
3209 if (msg_row >= Rows)
3210 msg_row = Rows - 1;
3211 if (msg_col >= Columns)
3212 msg_col = Columns - 1;
3213 return TRUE;
3214}
3215
3216/*
3217 * Clear from current message position to end of screen.
3218 * Skip this when ":silent" was used, no need to clear for redirection.
3219 */
3220 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003221msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 if (msg_silent == 0)
3224 msg_clr_eos_force();
3225}
3226
3227/*
3228 * Clear from current message position to end of screen.
3229 * Note: msg_col is not updated, so we remember the end of the message
3230 * for msg_check().
3231 */
3232 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 if (msg_use_printf())
3236 {
3237 if (full_screen) /* only when termcap codes are valid */
3238 {
3239 if (*T_CD)
3240 out_str(T_CD); /* clear to end of display */
3241 else if (*T_CE)
3242 out_str(T_CE); /* clear to end of line */
3243 }
3244 }
3245 else
3246 {
3247#ifdef FEAT_RIGHTLEFT
3248 if (cmdmsg_rl)
3249 {
3250 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3251 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3252 }
3253 else
3254#endif
3255 {
3256 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3257 ' ', ' ', 0);
3258 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3259 }
3260 }
3261}
3262
3263/*
3264 * Clear the command line.
3265 */
3266 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003267msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 msg_row = cmdline_row;
3270 msg_col = 0;
3271 msg_clr_eos_force();
3272}
3273
3274/*
3275 * end putting a message on the screen
3276 * call wait_return if the message does not fit in the available space
3277 * return TRUE if wait_return not called.
3278 */
3279 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003280msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003283 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 * or the ruler option is set and we run into it,
3285 * we have to redraw the window.
3286 * Do not do this if we are abandoning the file or editing the command line.
3287 */
3288 if (!exiting && need_wait_return && !(State & CMDLINE))
3289 {
3290 wait_return(FALSE);
3291 return FALSE;
3292 }
3293 out_flush();
3294 return TRUE;
3295}
3296
3297/*
3298 * If the written message runs into the shown command or ruler, we have to
3299 * wait for hit-return and redraw the window later.
3300 */
3301 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003302msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304 if (msg_row == Rows - 1 && msg_col >= sc_col)
3305 {
3306 need_wait_return = TRUE;
3307 redraw_cmdline = TRUE;
3308 }
3309}
3310
3311/*
3312 * May write a string to the redirection file.
3313 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3314 */
3315 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003316redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 char_u *s = str;
3319 static int cur_col = 0;
3320
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003321 /* Don't do anything for displaying prompts and the like. */
3322 if (redir_off)
3323 return;
3324
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003325 /* If 'verbosefile' is set prepare for writing in that file. */
3326 if (*p_vfile != NUL && verbose_fd == NULL)
3327 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003328
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003329 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
3331 /* If the string doesn't start with CR or NL, go to msg_col */
3332 if (*s != '\n' && *s != '\r')
3333 {
3334 while (cur_col < msg_col)
3335 {
3336#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003337 if (redir_execute)
3338 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003339 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003341 else if (redir_vname)
3342 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003343 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003345 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003347 if (verbose_fd != NULL)
3348 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 ++cur_col;
3350 }
3351 }
3352
3353#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003354 if (redir_execute)
3355 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003356 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003358 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003359 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#endif
3361
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003362 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3364 {
3365#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003366 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003368 if (redir_fd != NULL)
3369 putc(*s, redir_fd);
3370 if (verbose_fd != NULL)
3371 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (*s == '\r' || *s == '\n')
3373 cur_col = 0;
3374 else if (*s == '\t')
3375 cur_col += (8 - cur_col % 8);
3376 else
3377 ++cur_col;
3378 ++s;
3379 }
3380
3381 if (msg_silent != 0) /* should update msg_col */
3382 msg_col = cur_col;
3383 }
3384}
3385
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003386 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003387redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003388{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003389 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003390#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003391 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003392#endif
3393 ;
3394}
3395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003397 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003398 * Must always be called paired with verbose_leave()!
3399 */
3400 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003401verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003402{
3403 if (*p_vfile != NUL)
3404 ++msg_silent;
3405}
3406
3407/*
3408 * After giving verbose message.
3409 * Must always be called paired with verbose_enter()!
3410 */
3411 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003412verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003413{
3414 if (*p_vfile != NUL)
3415 if (--msg_silent < 0)
3416 msg_silent = 0;
3417}
3418
3419/*
3420 * Like verbose_enter() and set msg_scroll when displaying the message.
3421 */
3422 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003423verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003424{
3425 if (*p_vfile != NUL)
3426 ++msg_silent;
3427 else
3428 /* always scroll up, don't overwrite */
3429 msg_scroll = TRUE;
3430}
3431
3432/*
3433 * Like verbose_leave() and set cmdline_row when displaying the message.
3434 */
3435 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003436verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003437{
3438 if (*p_vfile != NUL)
3439 {
3440 if (--msg_silent < 0)
3441 msg_silent = 0;
3442 }
3443 else
3444 cmdline_row = msg_row;
3445}
3446
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003447/*
3448 * Called when 'verbosefile' is set: stop writing to the file.
3449 */
3450 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003451verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003452{
3453 if (verbose_fd != NULL)
3454 {
3455 fclose(verbose_fd);
3456 verbose_fd = NULL;
3457 }
3458 verbose_did_open = FALSE;
3459}
3460
3461/*
3462 * Open the file 'verbosefile'.
3463 * Return FAIL or OK.
3464 */
3465 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003466verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003467{
3468 if (verbose_fd == NULL && !verbose_did_open)
3469 {
3470 /* Only give the error message once. */
3471 verbose_did_open = TRUE;
3472
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003473 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003474 if (verbose_fd == NULL)
3475 {
3476 EMSG2(_(e_notopen), p_vfile);
3477 return FAIL;
3478 }
3479 }
3480 return OK;
3481}
3482
3483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 * Give a warning message (for searching).
3485 * Use 'w' highlighting and may repeat the message after redrawing
3486 */
3487 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003488give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489{
3490 /* Don't do this for ":silent". */
3491 if (msg_silent != 0)
3492 return;
3493
3494 /* Don't want a hit-enter prompt here. */
3495 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497#ifdef FEAT_EVAL
3498 set_vim_var_string(VV_WARNINGMSG, message, -1);
3499#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003500 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003502 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 else
3504 keep_msg_attr = 0;
3505 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003506 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 msg_didout = FALSE; /* overwrite this message */
3508 msg_nowait = TRUE; /* don't wait for this message */
3509 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 --no_wait_return;
3512}
3513
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003514 void
3515give_warning2(char_u *message, char_u *a1, int hl)
3516{
3517 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3518 give_warning(IObuff, hl);
3519}
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521/*
3522 * Advance msg cursor to column "col".
3523 */
3524 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003525msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 if (msg_silent != 0) /* nothing to advance to */
3528 {
3529 msg_col = col; /* for redirection, may fill it up later */
3530 return;
3531 }
3532 if (col >= Columns) /* not enough room */
3533 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003534#ifdef FEAT_RIGHTLEFT
3535 if (cmdmsg_rl)
3536 while (msg_col > Columns - col)
3537 msg_putchar(' ');
3538 else
3539#endif
3540 while (msg_col < col)
3541 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
3543
3544#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3545/*
3546 * Used for "confirm()" function, and the :confirm command prefix.
3547 * Versions which haven't got flexible dialogs yet, and console
3548 * versions, get this generic handler which uses the command line.
3549 *
3550 * type = one of:
3551 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3552 * title = title string (can be NULL for default)
3553 * (neither used in console dialogs at the moment)
3554 *
3555 * Format of the "buttons" string:
3556 * "Button1Name\nButton2Name\nButton3Name"
3557 * The first button should normally be the default/accept
3558 * The second button should be the 'Cancel' button
3559 * Other buttons- use your imagination!
3560 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3561 * different letter.
3562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003564do_dialog(
3565 int type UNUSED,
3566 char_u *title UNUSED,
3567 char_u *message,
3568 char_u *buttons,
3569 int dfltbutton,
3570 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003571 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003572 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003573 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
3575 int oldState;
3576 int retval = 0;
3577 char_u *hotkeys;
3578 int c;
3579 int i;
3580
3581#ifndef NO_CONSOLE
3582 /* Don't output anything in silent mode ("ex -s") */
3583 if (silent_mode)
3584 return dfltbutton; /* return default option */
3585#endif
3586
3587#ifdef FEAT_GUI_DIALOG
3588 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3589 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3590 {
3591 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003592 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003593 /* avoid a hit-enter prompt without clearing the cmdline */
3594 need_wait_return = FALSE;
3595 emsg_on_display = FALSE;
3596 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
3598 /* Flush output to avoid that further messages and redrawing is done
3599 * in the wrong order. */
3600 out_flush();
3601 gui_mch_update();
3602
3603 return c;
3604 }
3605#endif
3606
3607 oldState = State;
3608 State = CONFIRM;
3609#ifdef FEAT_MOUSE
3610 setmouse();
3611#endif
3612
3613 /*
3614 * Since we wait for a keypress, don't make the
3615 * user press RETURN as well afterwards.
3616 */
3617 ++no_wait_return;
3618 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3619
3620 if (hotkeys != NULL)
3621 {
3622 for (;;)
3623 {
3624 /* Get a typed character directly from the user. */
3625 c = get_keystroke();
3626 switch (c)
3627 {
3628 case CAR: /* User accepts default option */
3629 case NL:
3630 retval = dfltbutton;
3631 break;
3632 case Ctrl_C: /* User aborts/cancels */
3633 case ESC:
3634 retval = 0;
3635 break;
3636 default: /* Could be a hotkey? */
3637 if (c < 0) /* special keys are ignored here */
3638 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003639 if (c == ':' && ex_cmd)
3640 {
3641 retval = dfltbutton;
3642 ins_char_typebuf(':');
3643 break;
3644 }
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 /* Make the character lowercase, as chars in "hotkeys" are. */
3647 c = MB_TOLOWER(c);
3648 retval = 1;
3649 for (i = 0; hotkeys[i]; ++i)
3650 {
3651#ifdef FEAT_MBYTE
3652 if (has_mbyte)
3653 {
3654 if ((*mb_ptr2char)(hotkeys + i) == c)
3655 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003656 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658 else
3659#endif
3660 if (hotkeys[i] == c)
3661 break;
3662 ++retval;
3663 }
3664 if (hotkeys[i])
3665 break;
3666 /* No hotkey match, so keep waiting */
3667 continue;
3668 }
3669 break;
3670 }
3671
3672 vim_free(hotkeys);
3673 }
3674
3675 State = oldState;
3676#ifdef FEAT_MOUSE
3677 setmouse();
3678#endif
3679 --no_wait_return;
3680 msg_end_prompt();
3681
3682 return retval;
3683}
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685/*
3686 * Copy one character from "*from" to "*to", taking care of multi-byte
3687 * characters. Return the length of the character in bytes.
3688 */
3689 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003690copy_char(
3691 char_u *from,
3692 char_u *to,
3693 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695#ifdef FEAT_MBYTE
3696 int len;
3697 int c;
3698
3699 if (has_mbyte)
3700 {
3701 if (lowercase)
3702 {
3703 c = MB_TOLOWER((*mb_ptr2char)(from));
3704 return (*mb_char2bytes)(c, to);
3705 }
3706 else
3707 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003708 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 mch_memmove(to, from, (size_t)len);
3710 return len;
3711 }
3712 }
3713 else
3714#endif
3715 {
3716 if (lowercase)
3717 *to = (char_u)TOLOWER_LOC(*from);
3718 else
3719 *to = *from;
3720 return 1;
3721 }
3722}
3723
3724/*
3725 * Format the dialog string, and display it at the bottom of
3726 * the screen. Return a string of hotkey chars (if defined) for
3727 * each 'button'. If a button has no hotkey defined, the first character of
3728 * the button is used.
3729 * The hotkeys can be multi-byte characters, but without combining chars.
3730 *
3731 * Returns an allocated string with hotkeys, or NULL for error.
3732 */
3733 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003734msg_show_console_dialog(
3735 char_u *message,
3736 char_u *buttons,
3737 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 int len = 0;
3740#ifdef FEAT_MBYTE
3741# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3742#else
3743# define HOTK_LEN 1
3744#endif
3745 int lenhotkey = HOTK_LEN; /* count first button */
3746 char_u *hotk = NULL;
3747 char_u *msgp = NULL;
3748 char_u *hotkp = NULL;
3749 char_u *r;
3750 int copy;
3751#define HAS_HOTKEY_LEN 30
3752 char_u has_hotkey[HAS_HOTKEY_LEN];
3753 int first_hotkey = FALSE; /* first char of button is hotkey */
3754 int idx;
3755
3756 has_hotkey[0] = FALSE;
3757
3758 /*
3759 * First loop: compute the size of memory to allocate.
3760 * Second loop: copy to the allocated memory.
3761 */
3762 for (copy = 0; copy <= 1; ++copy)
3763 {
3764 r = buttons;
3765 idx = 0;
3766 while (*r)
3767 {
3768 if (*r == DLG_BUTTON_SEP)
3769 {
3770 if (copy)
3771 {
3772 *msgp++ = ',';
3773 *msgp++ = ' '; /* '\n' -> ', ' */
3774
3775 /* advance to next hotkey and set default hotkey */
3776#ifdef FEAT_MBYTE
3777 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003778 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 else
3780#endif
3781 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003782 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 if (dfltbutton)
3784 --dfltbutton;
3785
3786 /* If no hotkey is specified first char is used. */
3787 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3788 first_hotkey = TRUE;
3789 }
3790 else
3791 {
3792 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3793 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3794 if (idx < HAS_HOTKEY_LEN - 1)
3795 has_hotkey[++idx] = FALSE;
3796 }
3797 }
3798 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3799 {
3800 if (*r == DLG_HOTKEY_CHAR)
3801 ++r;
3802 first_hotkey = FALSE;
3803 if (copy)
3804 {
3805 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3806 *msgp++ = *r;
3807 else
3808 {
3809 /* '&a' -> '[a]' */
3810 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3811 msgp += copy_char(r, msgp, FALSE);
3812 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3813
3814 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003815 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
3818 else
3819 {
3820 ++len; /* '&a' -> '[a]' */
3821 if (idx < HAS_HOTKEY_LEN - 1)
3822 has_hotkey[idx] = TRUE;
3823 }
3824 }
3825 else
3826 {
3827 /* everything else copy literally */
3828 if (copy)
3829 msgp += copy_char(r, msgp, FALSE);
3830 }
3831
3832 /* advance to the next character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003833 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835
3836 if (copy)
3837 {
3838 *msgp++ = ':';
3839 *msgp++ = ' ';
3840 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 else
3843 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003844 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003845 + 2 /* for the NL's */
3846 + STRLEN(buttons)
3847 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003848 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
3850 /* If no hotkey is specified first char is used. */
3851 if (!has_hotkey[0])
3852 {
3853 first_hotkey = TRUE;
3854 len += 2; /* "x" -> "[x]" */
3855 }
3856
3857 /*
3858 * Now allocate and load the strings
3859 */
3860 vim_free(confirm_msg);
3861 confirm_msg = alloc(len);
3862 if (confirm_msg == NULL)
3863 return NULL;
3864 *confirm_msg = NUL;
3865 hotk = alloc(lenhotkey);
3866 if (hotk == NULL)
3867 return NULL;
3868
3869 *confirm_msg = '\n';
3870 STRCPY(confirm_msg + 1, message);
3871
3872 msgp = confirm_msg + 1 + STRLEN(message);
3873 hotkp = hotk;
3874
Bram Moolenaar6a516062007-07-05 08:11:42 +00003875 /* Define first default hotkey. Keep the hotkey string NUL
3876 * terminated to avoid reading past the end. */
3877 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
3879 /* Remember where the choices start, displaying starts here when
3880 * "hotkp" typed at the more prompt. */
3881 confirm_msg_tail = msgp;
3882 *msgp++ = '\n';
3883 }
3884 }
3885
3886 display_confirm_msg();
3887 return hotk;
3888}
3889
3890/*
3891 * Display the ":confirm" message. Also called when screen resized.
3892 */
3893 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003894display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 /* avoid that 'q' at the more prompt truncates the message here */
3897 ++confirm_msg_used;
3898 if (confirm_msg != NULL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003899 msg_puts_attr(confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 --confirm_msg_used;
3901}
3902
3903#endif /* FEAT_CON_DIALOG */
3904
3905#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3906
3907 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003908vim_dialog_yesno(
3909 int type,
3910 char_u *title,
3911 char_u *message,
3912 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913{
3914 if (do_dialog(type,
3915 title == NULL ? (char_u *)_("Question") : title,
3916 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003917 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 return VIM_YES;
3919 return VIM_NO;
3920}
3921
3922 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003923vim_dialog_yesnocancel(
3924 int type,
3925 char_u *title,
3926 char_u *message,
3927 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928{
3929 switch (do_dialog(type,
3930 title == NULL ? (char_u *)_("Question") : title,
3931 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003932 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
3934 case 1: return VIM_YES;
3935 case 2: return VIM_NO;
3936 }
3937 return VIM_CANCEL;
3938}
3939
3940 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003941vim_dialog_yesnoallcancel(
3942 int type,
3943 char_u *title,
3944 char_u *message,
3945 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
3947 switch (do_dialog(type,
3948 title == NULL ? (char_u *)"Question" : title,
3949 message,
3950 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003951 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
3953 case 1: return VIM_YES;
3954 case 2: return VIM_NO;
3955 case 3: return VIM_ALL;
3956 case 4: return VIM_DISCARDALL;
3957 }
3958 return VIM_CANCEL;
3959}
3960
3961#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3962
3963#if defined(FEAT_BROWSE) || defined(PROTO)
3964/*
3965 * Generic browse function. Calls gui_mch_browse() when possible.
3966 * Later this may pop-up a non-GUI file selector (external command?).
3967 */
3968 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003969do_browse(
3970 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3971 char_u *title, /* title for the window */
3972 char_u *dflt, /* default file name (may include directory) */
3973 char_u *ext, /* extension added */
3974 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003976 char_u *filter, /* file name filter */
3977 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 char_u *fname;
3980 static char_u *last_dir = NULL; /* last used directory */
3981 char_u *tofree = NULL;
3982 int save_browse = cmdmod.browse;
3983
3984 /* Must turn off browse to avoid that autocommands will get the
3985 * flag too! */
3986 cmdmod.browse = FALSE;
3987
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003988 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003990 if (flags & BROWSE_DIR)
3991 title = (char_u *)_("Select Directory dialog");
3992 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 title = (char_u *)_("Save File dialog");
3994 else
3995 title = (char_u *)_("Open File dialog");
3996 }
3997
3998 /* When no directory specified, use default file name, default dir, buffer
3999 * dir, last dir or current dir */
4000 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
4001 {
4002 if (mch_isdir(dflt)) /* default file name is a directory */
4003 {
4004 initdir = dflt;
4005 dflt = NULL;
4006 }
4007 else if (gettail(dflt) != dflt) /* default file name includes a path */
4008 {
4009 tofree = vim_strsave(dflt);
4010 if (tofree != NULL)
4011 {
4012 initdir = tofree;
4013 *gettail(initdir) = NUL;
4014 dflt = gettail(dflt);
4015 }
4016 }
4017 }
4018
4019 if (initdir == NULL || *initdir == NUL)
4020 {
4021 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004022 if (STRCMP(p_bsdir, "last") != 0
4023 && STRCMP(p_bsdir, "buffer") != 0
4024 && STRCMP(p_bsdir, "current") != 0
4025 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 initdir = p_bsdir;
4027 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004028 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 && buf != NULL && buf->b_ffname != NULL)
4030 {
4031 if (dflt == NULL || *dflt == NUL)
4032 dflt = gettail(curbuf->b_ffname);
4033 tofree = vim_strsave(curbuf->b_ffname);
4034 if (tofree != NULL)
4035 {
4036 initdir = tofree;
4037 *gettail(initdir) = NUL;
4038 }
4039 }
4040 /* When 'browsedir' is "last", use dir from last browse */
4041 else if (*p_bsdir == 'l')
4042 initdir = last_dir;
4043 /* When 'browsedir is "current", use current directory. This is the
4044 * default already, leave initdir empty. */
4045 }
4046
4047# ifdef FEAT_GUI
4048 if (gui.in_use) /* when this changes, also adjust f_has()! */
4049 {
4050 if (filter == NULL
4051# ifdef FEAT_EVAL
4052 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
4053 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
4054# endif
4055 )
4056 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004057 if (flags & BROWSE_DIR)
4058 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004059# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004060 /* For systems that have a directory dialog. */
4061 fname = gui_mch_browsedir(title, initdir);
4062# else
4063 /* Generic solution for selecting a directory: select a file and
4064 * remove the file name. */
4065 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
4066# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004067# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004068 /* Win32 adds a dummy file name, others return an arbitrary file
4069 * name. GTK+ 2 returns only the directory, */
4070 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
4071 {
4072 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004073 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004074
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004075 if (tail == fname)
4076 *tail++ = '.'; /* use current dir */
4077 *tail = NUL;
4078 }
4079# endif
4080 }
4081 else
4082 fname = gui_mch_browse(flags & BROWSE_SAVE,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004083 title, dflt, ext, initdir, (char_u *)_(filter));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 /* We hang around in the dialog for a while, the user might do some
4086 * things to our files. The Win32 dialog allows deleting or renaming
4087 * a file, check timestamps. */
4088 need_check_timestamps = TRUE;
4089 did_check_timestamps = FALSE;
4090 }
4091 else
4092# endif
4093 {
4094 /* TODO: non-GUI file selector here */
4095 EMSG(_("E338: Sorry, no file browser in console mode"));
4096 fname = NULL;
4097 }
4098
4099 /* keep the directory for next time */
4100 if (fname != NULL)
4101 {
4102 vim_free(last_dir);
4103 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004104 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
4106 *gettail(last_dir) = NUL;
4107 if (*last_dir == NUL)
4108 {
4109 /* filename only returned, must be in current dir */
4110 vim_free(last_dir);
4111 last_dir = alloc(MAXPATHL);
4112 if (last_dir != NULL)
4113 mch_dirname(last_dir, MAXPATHL);
4114 }
4115 }
4116 }
4117
4118 vim_free(tofree);
4119 cmdmod.browse = save_browse;
4120
4121 return fname;
4122}
4123#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004124
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004125#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004126static char *e_printf = N_("E766: Insufficient arguments for printf()");
4127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004128/*
4129 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4130 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004131 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004132tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133{
4134 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004135 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 int err = FALSE;
4137
4138 if (tvs[idx].v_type == VAR_UNKNOWN)
4139 EMSG(_(e_printf));
4140 else
4141 {
4142 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004143 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 if (err)
4145 n = 0;
4146 }
4147 return n;
4148}
4149
4150/*
4151 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004152 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004153 * are not converted to a string.
4154 * If "tofree" is not NULL echo_string() is used. All types are converted to
4155 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004156 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 */
4158 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004159tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004160{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004161 int idx = *idxp - 1;
4162 char *s = NULL;
4163 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004164
4165 if (tvs[idx].v_type == VAR_UNKNOWN)
4166 EMSG(_(e_printf));
4167 else
4168 {
4169 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004170 if (tofree != NULL)
4171 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4172 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004173 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 }
4175 return s;
4176}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004177
4178# ifdef FEAT_FLOAT
4179/*
4180 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4181 */
4182 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004183tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004184{
4185 int idx = *idxp - 1;
4186 double f = 0;
4187
4188 if (tvs[idx].v_type == VAR_UNKNOWN)
4189 EMSG(_(e_printf));
4190 else
4191 {
4192 ++*idxp;
4193 if (tvs[idx].v_type == VAR_FLOAT)
4194 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004195 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004196 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004197 else
4198 EMSG(_("E807: Expected Float argument for printf()"));
4199 }
4200 return f;
4201}
4202# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004203#endif
4204
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004205#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004206/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004207 * Return the representation of infinity for printf() function:
4208 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4209 */
4210 static const char *
4211infinity_str(int positive,
4212 char fmt_spec,
4213 int force_sign,
4214 int space_for_positive)
4215{
4216 static const char *table[] =
4217 {
4218 "-inf", "inf", "+inf", " inf",
4219 "-INF", "INF", "+INF", " INF"
4220 };
4221 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4222
4223 if (ASCII_ISUPPER(fmt_spec))
4224 idx += 4;
4225 return table[idx];
4226}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004227#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004228
4229/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004231 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004232 * consistency.
4233 *
4234 * This code is based on snprintf.c - a portable implementation of snprintf
4235 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004236 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004237 * The original code, including useful comments, can be found here:
4238 * http://www.ijs.si/software/snprintf/
4239 *
4240 * This snprintf() only supports the following conversion specifiers:
4241 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4242 * with flags: '-', '+', ' ', '0' and '#'.
4243 * An asterisk is supported for field width as well as precision.
4244 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004245 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004246 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004247 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4248 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004249 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004250 * The locale is not used, the string is used as a byte string. This is only
4251 * relevant for double-byte encodings where the second byte may be '%'.
4252 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004253 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4254 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004255 *
4256 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004257 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004258 * is greater or equal to "str_m", not all characters from the result
4259 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4260 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004261 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004262 */
4263
4264/*
4265 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004267 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004268 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004269 */
4270
4271/* When generating prototypes all of this is skipped, cproto doesn't
4272 * understand this. */
4273#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004274
Bram Moolenaara800b422010-06-27 01:15:55 +02004275/* Like vim_vsnprintf() but append to the string. */
4276 int
4277vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4278{
4279 va_list ap;
4280 int str_l;
4281 size_t len = STRLEN(str);
4282 size_t space;
4283
4284 if (str_m <= len)
4285 space = 0;
4286 else
4287 space = str_m - len;
4288 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004289 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004290 va_end(ap);
4291 return str_l;
4292}
Bram Moolenaara800b422010-06-27 01:15:55 +02004293
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004294 int
4295vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4296{
4297 va_list ap;
4298 int str_l;
4299
4300 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004301 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004302 va_end(ap);
4303 return str_l;
4304}
4305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004306 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004307vim_vsnprintf(
4308 char *str,
4309 size_t str_m,
4310 char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004311 va_list ap)
4312{
4313 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4314}
4315
4316 int
4317vim_vsnprintf_typval(
4318 char *str,
4319 size_t str_m,
4320 char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004321 va_list ap,
4322 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004323{
4324 size_t str_l = 0;
4325 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004326 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004327
4328 if (p == NULL)
4329 p = "";
4330 while (*p != NUL)
4331 {
4332 if (*p != '%')
4333 {
4334 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004335 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004336
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004337 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004338 if (str_l < str_m)
4339 {
4340 size_t avail = str_m - str_l;
4341
4342 mch_memmove(str + str_l, p, n > avail ? avail : n);
4343 }
4344 p += n;
4345 str_l += n;
4346 }
4347 else
4348 {
4349 size_t min_field_width = 0, precision = 0;
4350 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4351 int alternate_form = 0, force_sign = 0;
4352
4353 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4354 * ignored. */
4355 int space_for_positive = 1;
4356
4357 /* allowed values: \0, h, l, L */
4358 char length_modifier = '\0';
4359
4360 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004361# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004362# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004363 * That sounds reasonable to use as the maximum
4364 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004365# elif defined(FEAT_NUM64)
4366# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004367# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004368# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004369# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004370 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004371
4372 /* string address in case of string argument */
4373 char *str_arg;
4374
4375 /* natural field width of arg without padding and sign */
4376 size_t str_arg_l;
4377
4378 /* unsigned char argument value - only defined for c conversion.
4379 * N.B. standard explicitly states the char argument for the c
4380 * conversion is unsigned */
4381 unsigned char uchar_arg;
4382
4383 /* number of zeros to be inserted for numeric conversions as
4384 * required by the precision or minimal field width */
4385 size_t number_of_zeros_to_pad = 0;
4386
4387 /* index into tmp where zero padding is to be inserted */
4388 size_t zero_padding_insertion_ind = 0;
4389
4390 /* current conversion specifier character */
4391 char fmt_spec = '\0';
4392
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004393 /* buffer for 's' and 'S' specs */
4394 char_u *tofree = NULL;
4395
4396
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004397 str_arg = NULL;
4398 p++; /* skip '%' */
4399
4400 /* parse flags */
4401 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4402 || *p == '#' || *p == '\'')
4403 {
4404 switch (*p)
4405 {
4406 case '0': zero_padding = 1; break;
4407 case '-': justify_left = 1; break;
4408 case '+': force_sign = 1; space_for_positive = 0; break;
4409 case ' ': force_sign = 1;
4410 /* If both the ' ' and '+' flags appear, the ' '
4411 * flag should be ignored */
4412 break;
4413 case '#': alternate_form = 1; break;
4414 case '\'': break;
4415 }
4416 p++;
4417 }
4418 /* If the '0' and '-' flags both appear, the '0' flag should be
4419 * ignored. */
4420
4421 /* parse field width */
4422 if (*p == '*')
4423 {
4424 int j;
4425
4426 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004429 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430# endif
4431 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432 if (j >= 0)
4433 min_field_width = j;
4434 else
4435 {
4436 min_field_width = -j;
4437 justify_left = 1;
4438 }
4439 }
4440 else if (VIM_ISDIGIT((int)(*p)))
4441 {
4442 /* size_t could be wider than unsigned int; make sure we treat
4443 * argument like common implementations do */
4444 unsigned int uj = *p++ - '0';
4445
4446 while (VIM_ISDIGIT((int)(*p)))
4447 uj = 10 * uj + (unsigned int)(*p++ - '0');
4448 min_field_width = uj;
4449 }
4450
4451 /* parse precision */
4452 if (*p == '.')
4453 {
4454 p++;
4455 precision_specified = 1;
4456 if (*p == '*')
4457 {
4458 int j;
4459
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004461# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004462 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463# endif
4464 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004465 p++;
4466 if (j >= 0)
4467 precision = j;
4468 else
4469 {
4470 precision_specified = 0;
4471 precision = 0;
4472 }
4473 }
4474 else if (VIM_ISDIGIT((int)(*p)))
4475 {
4476 /* size_t could be wider than unsigned int; make sure we
4477 * treat argument like common implementations do */
4478 unsigned int uj = *p++ - '0';
4479
4480 while (VIM_ISDIGIT((int)(*p)))
4481 uj = 10 * uj + (unsigned int)(*p++ - '0');
4482 precision = uj;
4483 }
4484 }
4485
4486 /* parse 'h', 'l' and 'll' length modifiers */
4487 if (*p == 'h' || *p == 'l')
4488 {
4489 length_modifier = *p;
4490 p++;
4491 if (length_modifier == 'l' && *p == 'l')
4492 {
4493 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004494# ifdef FEAT_NUM64
4495 length_modifier = 'L';
4496# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004497 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004498# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004499 p++;
4500 }
4501 }
4502 fmt_spec = *p;
4503
4504 /* common synonyms: */
4505 switch (fmt_spec)
4506 {
4507 case 'i': fmt_spec = 'd'; break;
4508 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4509 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4510 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4511 default: break;
4512 }
4513
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004514# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4515 switch (fmt_spec)
4516 {
4517 case 'd': case 'u': case 'o': case 'x': case 'X':
4518 if (tvs != NULL && length_modifier == '\0')
4519 length_modifier = 'L';
4520 }
4521# endif
4522
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004523 /* get parameter value, do initial processing */
4524 switch (fmt_spec)
4525 {
4526 /* '%' and 'c' behave similar to 's' regarding flags and field
4527 * widths */
4528 case '%':
4529 case 'c':
4530 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004531 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004533 str_arg_l = 1;
4534 switch (fmt_spec)
4535 {
4536 case '%':
4537 str_arg = p;
4538 break;
4539
4540 case 'c':
4541 {
4542 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543
4544 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004545# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004546 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547# endif
4548 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004549 /* standard demands unsigned char */
4550 uchar_arg = (unsigned char)j;
4551 str_arg = (char *)&uchar_arg;
4552 break;
4553 }
4554
4555 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004556 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004559 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004560# endif
4561 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004562 if (str_arg == NULL)
4563 {
4564 str_arg = "[NULL]";
4565 str_arg_l = 6;
4566 }
4567 /* make sure not to address string beyond the specified
4568 * precision !!! */
4569 else if (!precision_specified)
4570 str_arg_l = strlen(str_arg);
4571 /* truncate string if necessary as requested by precision */
4572 else if (precision == 0)
4573 str_arg_l = 0;
4574 else
4575 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004576 /* Don't put the #if inside memchr(), it can be a
4577 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004578# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004579 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004580# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004581 /* memchr on HP does not like n > 2^31 !!! */
4582 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004583 precision <= (size_t)0x7fffffffL ? precision
4584 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004585# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004586 str_arg_l = (q == NULL) ? precision
4587 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004588 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004589# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004590 if (fmt_spec == 'S')
4591 {
4592 if (min_field_width != 0)
4593 min_field_width += STRLEN(str_arg)
4594 - mb_string2cells((char_u *)str_arg, -1);
4595 if (precision)
4596 {
4597 char_u *p1 = (char_u *)str_arg;
4598 size_t i;
4599
4600 for (i = 0; i < precision && *p1; i++)
4601 p1 += mb_ptr2len(p1);
4602
4603 str_arg_l = precision = p1 - (char_u *)str_arg;
4604 }
4605 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004606# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004607 break;
4608
4609 default:
4610 break;
4611 }
4612 break;
4613
Bram Moolenaar91984b92016-08-16 21:58:41 +02004614 case 'd': case 'u':
4615 case 'b': case 'B':
4616 case 'o':
4617 case 'x': case 'X':
4618 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004619 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004620 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004621 * imply the value is unsigned; d implies a signed
4622 * value */
4623
4624 /* 0 if numeric argument is zero (or if pointer is
4625 * NULL for 'p'), +1 if greater than zero (or nonzero
4626 * for unsigned arguments), -1 if negative (unsigned
4627 * argument is never negative) */
4628 int arg_sign = 0;
4629
4630 /* only defined for length modifier h, or for no
4631 * length modifiers */
4632 int int_arg = 0;
4633 unsigned int uint_arg = 0;
4634
4635 /* only defined for length modifier l */
4636 long int long_arg = 0;
4637 unsigned long int ulong_arg = 0;
4638
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004639# ifdef FEAT_NUM64
4640 /* only defined for length modifier ll */
4641 varnumber_T llong_arg = 0;
4642 uvarnumber_T ullong_arg = 0;
4643# endif
4644
Bram Moolenaare9997822016-08-28 16:03:38 +02004645 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004646 uvarnumber_T bin_arg = 0;
4647
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004648 /* pointer argument value -only defined for p
4649 * conversion */
4650 void *ptr_arg = NULL;
4651
4652 if (fmt_spec == 'p')
4653 {
4654 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004655 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004657 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4658 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004659# endif
4660 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004661 if (ptr_arg != NULL)
4662 arg_sign = 1;
4663 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004664 else if (fmt_spec == 'b' || fmt_spec == 'B')
4665 {
4666 bin_arg =
4667# if defined(FEAT_EVAL)
4668 tvs != NULL ?
4669 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4670# endif
4671 va_arg(ap, uvarnumber_T);
4672 if (bin_arg != 0)
4673 arg_sign = 1;
4674 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004675 else if (fmt_spec == 'd')
4676 {
4677 /* signed */
4678 switch (length_modifier)
4679 {
4680 case '\0':
4681 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682 /* char and short arguments are passed as int. */
4683 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004685 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004686# endif
4687 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004688 if (int_arg > 0)
4689 arg_sign = 1;
4690 else if (int_arg < 0)
4691 arg_sign = -1;
4692 break;
4693 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004694 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004695# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004696 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004697# endif
4698 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004699 if (long_arg > 0)
4700 arg_sign = 1;
4701 else if (long_arg < 0)
4702 arg_sign = -1;
4703 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004704# ifdef FEAT_NUM64
4705 case 'L':
4706 llong_arg =
4707# if defined(FEAT_EVAL)
4708 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4709# endif
4710 va_arg(ap, varnumber_T);
4711 if (llong_arg > 0)
4712 arg_sign = 1;
4713 else if (llong_arg < 0)
4714 arg_sign = -1;
4715 break;
4716# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004717 }
4718 }
4719 else
4720 {
4721 /* unsigned */
4722 switch (length_modifier)
4723 {
4724 case '\0':
4725 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004727# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004728 tvs != NULL ? (unsigned)
4729 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004730# endif
4731 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004732 if (uint_arg != 0)
4733 arg_sign = 1;
4734 break;
4735 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004737# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004738 tvs != NULL ? (unsigned long)
4739 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004740# endif
4741 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004742 if (ulong_arg != 0)
4743 arg_sign = 1;
4744 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004745# ifdef FEAT_NUM64
4746 case 'L':
4747 ullong_arg =
4748# if defined(FEAT_EVAL)
4749 tvs != NULL ? (uvarnumber_T)
4750 tv_nr(tvs, &arg_idx) :
4751# endif
4752 va_arg(ap, uvarnumber_T);
4753 if (ullong_arg != 0)
4754 arg_sign = 1;
4755 break;
4756# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004757 }
4758 }
4759
4760 str_arg = tmp;
4761 str_arg_l = 0;
4762
4763 /* NOTE:
4764 * For d, i, u, o, x, and X conversions, if precision is
4765 * specified, the '0' flag should be ignored. This is so
4766 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4767 * FreeBSD, NetBSD; but not with Perl.
4768 */
4769 if (precision_specified)
4770 zero_padding = 0;
4771 if (fmt_spec == 'd')
4772 {
4773 if (force_sign && arg_sign >= 0)
4774 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4775 /* leave negative numbers for sprintf to handle, to
4776 * avoid handling tricky cases like (short int)-32768 */
4777 }
4778 else if (alternate_form)
4779 {
4780 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004781 && (fmt_spec == 'b' || fmt_spec == 'B'
4782 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004783 {
4784 tmp[str_arg_l++] = '0';
4785 tmp[str_arg_l++] = fmt_spec;
4786 }
4787 /* alternate form should have no effect for p
4788 * conversion, but ... */
4789 }
4790
4791 zero_padding_insertion_ind = str_arg_l;
4792 if (!precision_specified)
4793 precision = 1; /* default precision is 1 */
4794 if (precision == 0 && arg_sign == 0)
4795 {
4796 /* When zero value is formatted with an explicit
4797 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004798 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 }
4800 else
4801 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004802 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803 int f_l = 0;
4804
4805 /* construct a simple format string for sprintf */
4806 f[f_l++] = '%';
4807 if (!length_modifier)
4808 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004809 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004810 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004811# ifdef FEAT_NUM64
4812# ifdef WIN3264
4813 f[f_l++] = 'I';
4814 f[f_l++] = '6';
4815 f[f_l++] = '4';
4816# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004817 f[f_l++] = 'l';
4818 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004819# endif
4820# else
4821 f[f_l++] = 'l';
4822# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004823 }
4824 else
4825 f[f_l++] = length_modifier;
4826 f[f_l++] = fmt_spec;
4827 f[f_l++] = '\0';
4828
4829 if (fmt_spec == 'p')
4830 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004831 else if (fmt_spec == 'b' || fmt_spec == 'B')
4832 {
4833 char b[8 * sizeof(uvarnumber_T)];
4834 size_t b_l = 0;
4835 uvarnumber_T bn = bin_arg;
4836
4837 do
4838 {
4839 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4840 bn >>= 1;
4841 }
4842 while (bn != 0);
4843
4844 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4845 str_arg_l += b_l;
4846 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004847 else if (fmt_spec == 'd')
4848 {
4849 /* signed */
4850 switch (length_modifier)
4851 {
4852 case '\0':
4853 case 'h': str_arg_l += sprintf(
4854 tmp + str_arg_l, f, int_arg);
4855 break;
4856 case 'l': str_arg_l += sprintf(
4857 tmp + str_arg_l, f, long_arg);
4858 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004859# ifdef FEAT_NUM64
4860 case 'L': str_arg_l += sprintf(
4861 tmp + str_arg_l, f, llong_arg);
4862 break;
4863# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004864 }
4865 }
4866 else
4867 {
4868 /* unsigned */
4869 switch (length_modifier)
4870 {
4871 case '\0':
4872 case 'h': str_arg_l += sprintf(
4873 tmp + str_arg_l, f, uint_arg);
4874 break;
4875 case 'l': str_arg_l += sprintf(
4876 tmp + str_arg_l, f, ulong_arg);
4877 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004878# ifdef FEAT_NUM64
4879 case 'L': str_arg_l += sprintf(
4880 tmp + str_arg_l, f, ullong_arg);
4881 break;
4882# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004883 }
4884 }
4885
4886 /* include the optional minus sign and possible
4887 * "0x" in the region before the zero padding
4888 * insertion point */
4889 if (zero_padding_insertion_ind < str_arg_l
4890 && tmp[zero_padding_insertion_ind] == '-')
4891 zero_padding_insertion_ind++;
4892 if (zero_padding_insertion_ind + 1 < str_arg_l
4893 && tmp[zero_padding_insertion_ind] == '0'
4894 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4895 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4896 zero_padding_insertion_ind += 2;
4897 }
4898
4899 {
4900 size_t num_of_digits = str_arg_l
4901 - zero_padding_insertion_ind;
4902
4903 if (alternate_form && fmt_spec == 'o'
4904 /* unless zero is already the first
4905 * character */
4906 && !(zero_padding_insertion_ind < str_arg_l
4907 && tmp[zero_padding_insertion_ind] == '0'))
4908 {
4909 /* assure leading zero for alternate-form
4910 * octal numbers */
4911 if (!precision_specified
4912 || precision < num_of_digits + 1)
4913 {
4914 /* precision is increased to force the
4915 * first character to be zero, except if a
4916 * zero value is formatted with an
4917 * explicit precision of zero */
4918 precision = num_of_digits + 1;
4919 precision_specified = 1;
4920 }
4921 }
4922 /* zero padding to specified precision? */
4923 if (num_of_digits < precision)
4924 number_of_zeros_to_pad = precision - num_of_digits;
4925 }
4926 /* zero padding to specified minimal field width? */
4927 if (!justify_left && zero_padding)
4928 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004929 int n = (int)(min_field_width - (str_arg_l
4930 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004931 if (n > 0)
4932 number_of_zeros_to_pad += n;
4933 }
4934 break;
4935 }
4936
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004937# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004938 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004939 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004940 case 'e':
4941 case 'E':
4942 case 'g':
4943 case 'G':
4944 {
4945 /* Floating point. */
4946 double f;
4947 double abs_f;
4948 char format[40];
4949 int l;
4950 int remove_trailing_zeroes = FALSE;
4951
4952 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004953# if defined(FEAT_EVAL)
4954 tvs != NULL ? tv_float(tvs, &arg_idx) :
4955# endif
4956 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004957 abs_f = f < 0 ? -f : f;
4958
4959 if (fmt_spec == 'g' || fmt_spec == 'G')
4960 {
4961 /* Would be nice to use %g directly, but it prints
4962 * "1.0" as "1", we don't want that. */
4963 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4964 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004965 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004966 else
4967 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4968 remove_trailing_zeroes = TRUE;
4969 }
4970
Bram Moolenaar04186092016-08-29 21:55:35 +02004971 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004972# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004973 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004974# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004975 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004976# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004977 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004978 {
4979 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004980 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4981 force_sign, space_for_positive));
4982 str_arg_l = STRLEN(tmp);
4983 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004984 }
4985 else
4986 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004987 if (isnan(f))
4988 {
4989 /* Not a number: nan or NAN */
4990 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4991 : "nan");
4992 str_arg_l = 3;
4993 zero_padding = 0;
4994 }
4995 else if (isinf(f))
4996 {
4997 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4998 force_sign, space_for_positive));
4999 str_arg_l = STRLEN(tmp);
5000 zero_padding = 0;
5001 }
5002 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01005003 {
Bram Moolenaare9997822016-08-28 16:03:38 +02005004 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02005005 format[0] = '%';
5006 l = 1;
5007 if (force_sign)
5008 format[l++] = space_for_positive ? ' ' : '+';
5009 if (precision_specified)
5010 {
5011 size_t max_prec = TMP_LEN - 10;
5012
5013 /* Make sure we don't get more digits than we
5014 * have room for. */
5015 if ((fmt_spec == 'f' || fmt_spec == 'F')
5016 && abs_f > 1.0)
5017 max_prec -= (size_t)log10(abs_f);
5018 if (precision > max_prec)
5019 precision = max_prec;
5020 l += sprintf(format + l, ".%d", (int)precision);
5021 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02005022 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02005023 format[l + 1] = NUL;
5024
Bram Moolenaare9997822016-08-28 16:03:38 +02005025 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01005026 }
Bram Moolenaar99922372016-08-27 15:26:35 +02005027
Bram Moolenaara7241f52008-06-24 20:39:31 +00005028 if (remove_trailing_zeroes)
5029 {
5030 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005031 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005032
5033 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02005034 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005035 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005036 else
5037 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005038 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005039 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005040 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005041 {
5042 /* Remove superfluous '+' and leading
5043 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005044 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005045 {
5046 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005047 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005048 --str_arg_l;
5049 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005050 i = (tp[1] == '-') ? 2 : 1;
5051 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005052 {
5053 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005054 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005055 --str_arg_l;
5056 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005057 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005058 }
5059 }
5060
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005061 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005062 /* Remove trailing zeroes, but keep the one
5063 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005064 while (tp > tmp + 2 && *tp == '0'
5065 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005066 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005067 STRMOVE(tp, tp + 1);
5068 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005069 --str_arg_l;
5070 }
5071 }
5072 else
5073 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005074 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005075
5076 /* Be consistent: some printf("%e") use 1.0e+12
5077 * and some 1.0e+012. Remove one zero in the last
5078 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005079 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005080 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005081 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5082 && tp[2] == '0'
5083 && vim_isdigit(tp[3])
5084 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005085 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005086 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005087 --str_arg_l;
5088 }
5089 }
5090 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005091 if (zero_padding && min_field_width > str_arg_l
5092 && (tmp[0] == '-' || force_sign))
5093 {
5094 /* padding 0's should be inserted after the sign */
5095 number_of_zeros_to_pad = min_field_width - str_arg_l;
5096 zero_padding_insertion_ind = 1;
5097 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005098 str_arg = tmp;
5099 break;
5100 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005101# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005102
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103 default:
5104 /* unrecognized conversion specifier, keep format string
5105 * as-is */
5106 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005107 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005108 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005109 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005110
5111 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005112 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005113 str_arg = p;
5114 str_arg_l = 0;
5115 if (*p != NUL)
5116 str_arg_l++; /* include invalid conversion specifier
5117 unchanged if not at end-of-string */
5118 break;
5119 }
5120
5121 if (*p != NUL)
5122 p++; /* step over the just processed conversion specifier */
5123
5124 /* insert padding to the left as requested by min_field_width;
5125 * this does not include the zero padding in case of numerical
5126 * conversions*/
5127 if (!justify_left)
5128 {
5129 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005130 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005131
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005132 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005133 {
5134 if (str_l < str_m)
5135 {
5136 size_t avail = str_m - str_l;
5137
5138 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005139 (size_t)pn > avail ? avail
5140 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005141 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005142 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005143 }
5144 }
5145
5146 /* zero padding as requested by the precision or by the minimal
5147 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005148 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005149 {
5150 /* will not copy first part of numeric right now, *
5151 * force it to be copied later in its entirety */
5152 zero_padding_insertion_ind = 0;
5153 }
5154 else
5155 {
5156 /* insert first part of numerics (sign or '0x') before zero
5157 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005158 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005159
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005160 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005161 {
5162 if (str_l < str_m)
5163 {
5164 size_t avail = str_m - str_l;
5165
5166 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005167 (size_t)zn > avail ? avail
5168 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005169 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005170 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005171 }
5172
5173 /* insert zero padding as requested by the precision or min
5174 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005175 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005176 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005177 {
5178 if (str_l < str_m)
5179 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005180 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005181
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005182 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005183 (size_t)zn > avail ? avail
5184 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005185 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005186 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005187 }
5188 }
5189
5190 /* insert formatted string
5191 * (or as-is conversion specifier for unknown conversions) */
5192 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005193 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005194
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005195 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005196 {
5197 if (str_l < str_m)
5198 {
5199 size_t avail = str_m - str_l;
5200
5201 mch_memmove(str + str_l,
5202 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005203 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005204 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005205 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005206 }
5207 }
5208
5209 /* insert right padding */
5210 if (justify_left)
5211 {
5212 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005213 int pn = (int)(min_field_width
5214 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005215
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005216 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005217 {
5218 if (str_l < str_m)
5219 {
5220 size_t avail = str_m - str_l;
5221
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005222 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005223 (size_t)pn > avail ? avail
5224 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005225 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005226 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005227 }
5228 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005229 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005230 }
5231 }
5232
5233 if (str_m > 0)
5234 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005235 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005236 * overwriting the last character (shouldn't happen, but just in case)
5237 * */
5238 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5239 }
5240
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005241 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005243
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005244 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005245 * character), that is, the number of characters that would have been
5246 * written to the buffer if it were large enough. */
5247 return (int)str_l;
5248}
5249
5250#endif /* PROTO */