blob: 211403384a63178e5da4d42bf010b3902b411cb5 [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 int other_sourcing_name(void);
20static char_u *get_emsg_source(void);
21static char_u *get_emsg_lnum(void);
22static void add_msg_hist(char_u *s, int len, int attr);
23static void hit_return_msg(void);
24static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
29static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
30static void msg_scroll_up(void);
31static void inc_msg_scrolled(void);
32static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
33static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
34static void msg_puts_printf(char_u *str, int maxlen);
35static int do_more_prompt(int typed_char);
36static void msg_screen_putchar(int c, int attr);
37static int msg_check_screen(void);
38static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041static int confirm_msg_used = FALSE; /* displaying confirm_msg */
42static char_u *confirm_msg = NULL; /* ":confirm" message */
43static char_u *confirm_msg_tail; /* tail of confirm_msg */
44#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010045#ifdef FEAT_JOB_CHANNEL
46static int emsg_to_channel_log = FALSE;
47#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49struct msg_hist
50{
51 struct msg_hist *next;
52 char_u *msg;
53 int attr;
54};
55
56static struct msg_hist *first_msg_hist = NULL;
57static struct msg_hist *last_msg_hist = NULL;
58static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020060static FILE *verbose_fd = NULL;
61static int verbose_did_open = FALSE;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063/*
64 * When writing messages to the screen, there are many different situations.
65 * A number of variables is used to remember the current state:
66 * msg_didany TRUE when messages were written since the last time the
67 * user reacted to a prompt.
68 * Reset: After hitting a key for the hit-return prompt,
69 * hitting <CR> for the command line or input().
70 * Set: When any message is written to the screen.
71 * msg_didout TRUE when something was written to the current line.
72 * Reset: When advancing to the next line, when the current
73 * text can be overwritten.
74 * Set: When any message is written to the screen.
75 * msg_nowait No extra delay for the last drawn message.
76 * Used in normal_cmd() before the mode message is drawn.
77 * emsg_on_display There was an error message recently. Indicates that there
78 * should be a delay before redrawing.
79 * msg_scroll The next message should not overwrite the current one.
80 * msg_scrolled How many lines the screen has been scrolled (because of
81 * messages). Used in update_screen() to scroll the screen
82 * back. Incremented each time the screen scrolls a line.
83 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
84 * writes something without scrolling should not make
85 * need_wait_return to be set. This is a hack to make ":ts"
86 * work without an extra prompt.
87 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010088 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 * need_wait_return TRUE when the hit-return prompt is needed.
90 * Reset: After giving the hit-return prompt, when the user
91 * has answered some other prompt.
92 * Set: When the ruler or typeahead display is overwritten,
93 * scrolling the screen for some message.
94 * keep_msg Message to be displayed after redrawing the screen, in
95 * main_loop().
96 * This is an allocated string or NULL when not used.
97 */
98
99/*
100 * msg(s) - displays the string 's' on the status line
101 * When terminal not initialized (yet) mch_errmsg(..) is used.
102 * return TRUE if wait_return not called
103 */
104 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100105msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106{
107 return msg_attr_keep(s, 0, FALSE);
108}
109
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000111 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000112/*
113 * Like msg() but keep it silent when 'verbosefile' is set.
114 */
115 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100116verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000117{
118 int n;
119
120 verbose_enter();
121 n = msg_attr_keep(s, 0, FALSE);
122 verbose_leave();
123
124 return n;
125}
126#endif
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
131 return msg_attr_keep(s, attr, FALSE);
132}
133
134 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100135msg_attr_keep(
136 char_u *s,
137 int attr,
138 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 static int entered = 0;
141 int retval;
142 char_u *buf = NULL;
143
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200144 /* Skip messages not matching ":filter pattern".
145 * Don't filter when there is an error. */
146 if (!emsg_on_display && message_filtered(s))
147 return TRUE;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#ifdef FEAT_EVAL
150 if (attr == 0)
151 set_vim_var_string(VV_STATUSMSG, s, -1);
152#endif
153
154 /*
155 * It is possible that displaying a messages causes a problem (e.g.,
156 * when redrawing the window), which causes another message, etc.. To
157 * break this loop, limit the recursiveness to 3 levels.
158 */
159 if (entered >= 3)
160 return TRUE;
161 ++entered;
162
163 /* Add message to history (unless it's a repeated kept message or a
164 * truncated message) */
165 if (s != keep_msg
166 || (*s != '<'
167 && last_msg_hist != NULL
168 && last_msg_hist->msg != NULL
169 && STRCMP(s, last_msg_hist->msg)))
170 add_msg_hist(s, -1, attr);
171
Bram Moolenaar958dc692016-12-01 15:34:12 +0100172#ifdef FEAT_JOB_CHANNEL
173 if (emsg_to_channel_log)
Bram Moolenaar958dc692016-12-01 15:34:12 +0100174 /* Write message in the channel log. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200175 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100176#endif
177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 /* When displaying keep_msg, don't let msg_start() free it, caller must do
179 * that. */
180 if (s == keep_msg)
181 keep_msg = NULL;
182
183 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000184 msg_start();
185 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 if (buf != NULL)
187 s = buf;
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 msg_outtrans_attr(s, attr);
190 msg_clr_eos();
191 retval = msg_end();
192
193 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
194 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000195 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197 vim_free(buf);
198 --entered;
199 return retval;
200}
201
202/*
203 * Truncate a string such that it can be printed without causing a scroll.
204 * Returns an allocated string or NULL when no truncating is done.
205 */
206 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100207msg_strtrunc(
208 char_u *s,
209 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210{
211 char_u *buf = NULL;
212 int len;
213 int room;
214
215 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000216 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
217 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {
219 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000220 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000221 /* Use all the columns. */
222 room = (int)(Rows - msg_row) * Columns - 1;
223 else
224 /* Use up to 'showcmd' column. */
225 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (len > room && room > 0)
227 {
228#ifdef FEAT_MBYTE
229 if (enc_utf8)
230 /* may have up to 18 bytes per cell (6 per char, up to two
231 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 else if (enc_dbcs == DBCS_JPNU)
234 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100235 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 else
237#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100238 len = room + 2;
239 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100241 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 }
243 }
244 return buf;
245}
246
247/*
248 * Truncate a string "s" to "buf" with cell width "room".
249 * "s" and "buf" may be equal.
250 */
251 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100252trunc_string(
253 char_u *s,
254 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200255 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100256 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200258 size_t room = room_in - 3; /* "..." takes 3 chars */
259 size_t half;
260 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 int e;
262 int i;
263 int n;
264
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200265 if (room_in < 3)
266 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100270 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
272 if (s[e] == NUL)
273 {
274 /* text fits without truncating! */
275 buf[e] = NUL;
276 return;
277 }
278 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200279 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 break;
281 len += n;
282 buf[e] = s[e];
283#ifdef FEAT_MBYTE
284 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000285 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100287 if (++e == buflen)
288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 buf[e] = s[e];
290 }
291#endif
292 }
293
294 /* Last part: End of the string. */
295 i = e;
296#ifdef FEAT_MBYTE
297 if (enc_dbcs != 0)
298 {
299 /* For DBCS going backwards in a string is slow, but
300 * computing the cell width isn't too slow: go forward
301 * until the rest fits. */
302 n = vim_strsize(s + i);
303 while (len + n > room)
304 {
305 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000306 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 }
308 }
309 else if (enc_utf8)
310 {
311 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000312 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 for (;;)
314 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000315 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200316 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200317 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200319 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 break;
321 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200322 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 }
324 }
325 else
326#endif
327 {
328 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
329 len += n;
330 }
331
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200332
333 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100334 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200335 /* text fits without truncating */
336 if (s != buf)
337 {
338 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200339 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200340 len = buflen - 1;
341 len = len - e + 1;
342 if (len < 1)
343 buf[e - 1] = NUL;
344 else
345 mch_memmove(buf + e, s + e, len);
346 }
347 }
348 else if (e + 3 < buflen)
349 {
350 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100351 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200352 len = STRLEN(s + i) + 1;
353 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100354 len = buflen - e - 3 - 1;
355 mch_memmove(buf + e + 3, s + i, len);
356 buf[e + 3 + len - 1] = NUL;
357 }
358 else
359 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200360 /* can't fit in the "...", just truncate it */
361 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363}
364
365/*
366 * Automatic prototype generation does not understand this function.
367 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
368 * shorter than IOSIZE!!!
369 */
370#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000372int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100375# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378smsg(char_u *s, ...)
379{
380 va_list arglist;
381
382 va_start(arglist, s);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +0200383 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 va_end(arglist);
385 return msg(IObuff);
386}
387
388 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100389# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392smsg_attr(int attr, char_u *s, ...)
393{
394 va_list arglist;
395
396 va_start(arglist, s);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +0200397 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 va_end(arglist);
399 return msg_attr(IObuff, attr);
400}
401
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#endif
403
404/*
405 * Remember the last sourcing name/lnum used in an error message, so that it
406 * isn't printed each time when it didn't change.
407 */
408static int last_sourcing_lnum = 0;
409static char_u *last_sourcing_name = NULL;
410
411/*
412 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
413 * for the next error message;
414 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000415 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100416reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
418 vim_free(last_sourcing_name);
419 last_sourcing_name = NULL;
420 last_sourcing_lnum = 0;
421}
422
423/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000424 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
425 */
426 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100427other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000428{
429 if (sourcing_name != NULL)
430 {
431 if (last_sourcing_name != NULL)
432 return STRCMP(sourcing_name, last_sourcing_name) != 0;
433 return TRUE;
434 }
435 return FALSE;
436}
437
438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 * Get the message about the source, as used for an error message.
440 * Returns an allocated string with room for one more character.
441 * Returns NULL when no message is to be given.
442 */
443 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100444get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
446 char_u *Buf, *p;
447
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000448 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
450 p = (char_u *)_("Error detected while processing %s:");
451 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
452 if (Buf != NULL)
453 sprintf((char *)Buf, (char *)p, sourcing_name);
454 return Buf;
455 }
456 return NULL;
457}
458
459/*
460 * Get the message about the source lnum, as used for an error message.
461 * Returns an allocated string with room for one more character.
462 * Returns NULL when no message is to be given.
463 */
464 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100465get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 char_u *Buf, *p;
468
469 /* lnum is 0 when executing a command from the command line
470 * argument, we don't want a line number then */
471 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000472 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 && sourcing_lnum != 0)
474 {
475 p = (char_u *)_("line %4ld:");
476 Buf = alloc((unsigned)(STRLEN(p) + 20));
477 if (Buf != NULL)
478 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
479 return Buf;
480 }
481 return NULL;
482}
483
484/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000485 * Display name and line number for the source of an error.
486 * Remember the file name and line number, so that for the next error the info
487 * is only displayed if it changed.
488 */
489 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100490msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000491{
492 char_u *p;
493
494 ++no_wait_return;
495 p = get_emsg_source();
496 if (p != NULL)
497 {
498 msg_attr(p, attr);
499 vim_free(p);
500 }
501 p = get_emsg_lnum();
502 if (p != NULL)
503 {
Bram Moolenaar8820b482017-03-16 17:23:31 +0100504 msg_attr(p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000505 vim_free(p);
506 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
507 }
508
509 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000510 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000511 {
512 vim_free(last_sourcing_name);
513 if (sourcing_name == NULL)
514 last_sourcing_name = NULL;
515 else
516 last_sourcing_name = vim_strsave(sourcing_name);
517 }
518 --no_wait_return;
519}
520
521/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000522 * Return TRUE if not giving error messages right now:
523 * If "emsg_off" is set: no error messages at the moment.
524 * If "msg" is in 'debug': do error message but without side effects.
525 * If "emsg_skip" is set: never do error messages.
526 */
527 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100528emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000529{
530 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
531 && vim_strchr(p_debug, 't') == NULL)
532#ifdef FEAT_EVAL
533 || emsg_skip > 0
534#endif
535 )
536 return TRUE;
537 return FALSE;
538}
539
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100540#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100541static garray_T ignore_error_list = GA_EMPTY;
542
543 void
544ignore_error_for_testing(char_u *error)
545{
546 if (ignore_error_list.ga_itemsize == 0)
547 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
548
549 ga_add_string(&ignore_error_list, error);
550}
551
552 static int
553ignore_error(char_u *msg)
554{
555 int i;
556
557 for (i = 0; i < ignore_error_list.ga_len; ++i)
558 if (strstr((char *)msg,
559 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
560 return TRUE;
561 return FALSE;
562}
563#endif
564
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200565#if !defined(HAVE_STRERROR) || defined(PROTO)
566/*
567 * Replacement for perror() that behaves more or less like emsg() was called.
568 * v:errmsg will be set and called_emsg will be set.
569 */
570 void
571do_perror(char *msg)
572{
573 perror(msg);
574 ++emsg_silent;
575 emsg((char_u *)msg);
576 --emsg_silent;
577}
578#endif
579
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 * emsg() - display an error message
582 *
583 * Rings the bell, if appropriate, and calls message() to do the real work
584 * When terminal not initialized (yet) mch_errmsg(..) is used.
585 *
586 * return TRUE if wait_return not called
587 */
588 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100589emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100593 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594#ifdef FEAT_EVAL
595 int ignore = FALSE;
596 int severe;
597#endif
598
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100599 /* Skip this if not giving error messages at the moment. */
600 if (emsg_not_now())
601 return TRUE;
602
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100603#ifdef FEAT_EVAL
604 /* When testing some errors are turned into a normal message. */
605 if (ignore_error(s))
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +0100606 /* don't call msg() if it results in a dialog */
607 return msg_use_printf() ? FALSE : msg(s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100608#endif
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 called_emsg = TRUE;
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef FEAT_EVAL
Bram Moolenaare723c422017-09-06 23:40:10 +0200613 /* If "emsg_severe" is TRUE: When an error exception is to be thrown,
614 * prefer this message over previous messages for the same command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 severe = emsg_severe;
616 emsg_severe = FALSE;
617#endif
618
Bram Moolenaar57657d82006-04-21 22:12:41 +0000619 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {
621#ifdef FEAT_EVAL
622 /*
623 * Cause a throw of an error exception if appropriate. Don't display
624 * the error message in this case. (If no matching catch clause will
625 * be found, the message will be displayed later on.) "ignore" is set
626 * when the message should be ignored completely (used for the
627 * interrupt message).
628 */
629 if (cause_errthrow(s, severe, &ignore) == TRUE)
630 {
631 if (!ignore)
632 did_emsg = TRUE;
633 return TRUE;
634 }
635
636 /* set "v:errmsg", also when using ":silent! cmd" */
637 set_vim_var_string(VV_ERRMSG, s, -1);
638#endif
639
640 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000641 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 * But do write it to the redirection file.
643 */
644 if (emsg_silent != 0)
645 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200646 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200648 msg_start();
649 p = get_emsg_source();
650 if (p != NULL)
651 {
652 STRCAT(p, "\n");
653 redir_write(p, -1);
654 vim_free(p);
655 }
656 p = get_emsg_lnum();
657 if (p != NULL)
658 {
659 STRCAT(p, "\n");
660 redir_write(p, -1);
661 vim_free(p);
662 }
663 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100665#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200666 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 return TRUE;
669 }
670
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100671 ex_exitval = 1;
672
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200673 /* Reset msg_silent, an error causes messages to be switched back on.
674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 msg_silent = 0;
676 cmd_silent = FALSE;
677
678 if (global_busy) /* break :global command */
679 ++global_busy;
680
681 if (p_eb)
682 beep_flush(); /* also includes flush_buffers() */
683 else
684 flush_buffers(FALSE); /* flush internal buffers */
685 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaare723c422017-09-06 23:40:10 +0200686#ifdef FEAT_EVAL
687 did_uncaught_emsg = TRUE;
688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690
691 emsg_on_display = TRUE; /* remember there is an error message */
692 ++msg_scroll; /* don't overwrite a previous message */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100693 attr = HL_ATTR(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000694 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 need_wait_return = TRUE; /* needed in case emsg() is called after
696 * wait_return has reset need_wait_return
697 * and a redraw is expected because
698 * msg_scrolled is non-zero */
699
Bram Moolenaar958dc692016-12-01 15:34:12 +0100700#ifdef FEAT_JOB_CHANNEL
701 emsg_to_channel_log = TRUE;
702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 /*
704 * Display name and line number for the source of the error.
705 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000706 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
708 /*
709 * Display the error message itself.
710 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000711 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar958dc692016-12-01 15:34:12 +0100712 r = msg_attr(s, attr);
713
714#ifdef FEAT_JOB_CHANNEL
715 emsg_to_channel_log = FALSE;
716#endif
717 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718}
719
Bram Moolenaar95f09602016-11-10 20:01:45 +0100720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * Print an error message with one "%s" and one string argument.
723 */
724 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100725emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
727 return emsg3(s, a1, NULL);
728}
729
Bram Moolenaar95f09602016-11-10 20:01:45 +0100730/*
731 * Print an error message with one or two "%s" and one or two string arguments.
732 * This is not in message.c to avoid a warning for prototypes.
733 */
734 int
735emsg3(char_u *s, char_u *a1, char_u *a2)
736{
737 if (emsg_not_now())
738 return TRUE; /* no error messages at the moment */
739 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
740 return emsg(IObuff);
741}
742
743/*
744 * Print an error message with one "%ld" and one long int argument.
745 * This is not in message.c to avoid a warning for prototypes.
746 */
747 int
748emsgn(char_u *s, long n)
749{
750 if (emsg_not_now())
751 return TRUE; /* no error messages at the moment */
752 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
753 return emsg(IObuff);
754}
755
756/*
757 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
758 * defined. It is used for internal errors only, so that they can be
759 * detected when fuzzing vim.
760 */
761 void
762iemsg(char_u *s)
763{
764 msg(s);
765#ifdef ABORT_ON_INTERNAL_ERROR
766 abort();
767#endif
768}
769
770
771/*
772 * Same as emsg2(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
773 * defined. It is used for internal errors only, so that they can be
774 * detected when fuzzing vim.
775 */
776 void
777iemsg2(char_u *s, char_u *a1)
778{
779 emsg2(s, a1);
780#ifdef ABORT_ON_INTERNAL_ERROR
781 abort();
782#endif
783}
784
785/*
786 * Same as emsgn(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
787 * defined. It is used for internal errors only, so that they can be
788 * detected when fuzzing vim.
789 */
790 void
791iemsgn(char_u *s, long n)
792{
793 emsgn(s, n);
794#ifdef ABORT_ON_INTERNAL_ERROR
795 abort();
796#endif
797}
798
799/*
800 * Give an "Internal error" message.
801 */
802 void
803internal_error(char *where)
804{
805 IEMSG2(_(e_intern2), where);
806}
807
Bram Moolenaarea424162005-06-16 21:51:00 +0000808/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaardf177f62005-02-22 08:39:57 +0000810 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100811emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000812{
813 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
814}
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Like msg(), but truncate to a single line if p_shm contains 't', or when
818 * "force" is TRUE. This truncates in another way as for normal messages.
819 * Careful: The string may be changed by msg_may_trunc()!
820 * Returns a pointer to the printed message, if wait_return() not called.
821 */
822 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100823msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 int n;
826
827 /* Add message to history before truncating */
828 add_msg_hist(s, -1, attr);
829
830 s = msg_may_trunc(force, s);
831
832 msg_hist_off = TRUE;
833 n = msg_attr(s, attr);
834 msg_hist_off = FALSE;
835
836 if (n)
837 return s;
838 return NULL;
839}
840
841/*
842 * Check if message "s" should be truncated at the start (for filenames).
843 * Return a pointer to where the truncated message starts.
844 * Note: May change the message by replacing a character with '<'.
845 */
846 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100847msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849 int n;
850 int room;
851
852 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
853 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
854 && (n = (int)STRLEN(s) - room) > 0)
855 {
856#ifdef FEAT_MBYTE
857 if (has_mbyte)
858 {
859 int size = vim_strsize(s);
860
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000861 /* There may be room anyway when there are multibyte chars. */
862 if (size <= room)
863 return s;
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 for (n = 0; size >= room; )
866 {
867 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000868 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 --n;
871 }
872#endif
873 s += n;
874 *s = '<';
875 }
876 return s;
877}
878
879 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100880add_msg_hist(
881 char_u *s,
882 int len, /* -1 for undetermined length */
883 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 struct msg_hist *p;
886
887 if (msg_hist_off || msg_silent != 0)
888 return;
889
890 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000891 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000892 (void)delete_first_msg();
893
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 /* allocate an entry and add the message at the end of the history */
895 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
896 if (p != NULL)
897 {
898 if (len < 0)
899 len = (int)STRLEN(s);
900 /* remove leading and trailing newlines */
901 while (len > 0 && *s == '\n')
902 {
903 ++s;
904 --len;
905 }
906 while (len > 0 && s[len - 1] == '\n')
907 --len;
908 p->msg = vim_strnsave(s, len);
909 p->next = NULL;
910 p->attr = attr;
911 if (last_msg_hist != NULL)
912 last_msg_hist->next = p;
913 last_msg_hist = p;
914 if (first_msg_hist == NULL)
915 first_msg_hist = last_msg_hist;
916 ++msg_hist_len;
917 }
918}
919
920/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000921 * Delete the first (oldest) message from the history.
922 * Returns FAIL if there are no messages.
923 */
924 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100925delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000926{
927 struct msg_hist *p;
928
929 if (msg_hist_len <= 0)
930 return FAIL;
931 p = first_msg_hist;
932 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000933 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200934 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000935 vim_free(p->msg);
936 vim_free(p);
937 --msg_hist_len;
938 return OK;
939}
940
941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 * ":messages" command.
943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200945ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 struct msg_hist *p;
948 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200949 int c = 0;
950
951 if (STRCMP(eap->arg, "clear") == 0)
952 {
953 int keep = eap->addr_count == 0 ? 0 : eap->line2;
954
955 while (msg_hist_len > keep)
956 (void)delete_first_msg();
957 return;
958 }
959
960 if (*eap->arg != NUL)
961 {
962 EMSG(_(e_invarg));
963 return;
964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 msg_hist_off = TRUE;
967
Bram Moolenaar451f8492016-04-14 17:16:22 +0200968 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200969 if (eap->addr_count != 0)
970 {
971 /* Count total messages */
972 for (; p != NULL && !got_int; p = p->next)
973 c++;
974
975 c -= eap->line2;
976
977 /* Skip without number of messages specified */
978 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
979 p = p->next, c--);
980 }
981
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200982 if (p == first_msg_hist)
983 {
984 s = mch_getenv((char_u *)"LANG");
985 if (s != NULL && *s != NUL)
986 msg_attr((char_u *)
987 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +0100988 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200989 }
990
Bram Moolenaar451f8492016-04-14 17:16:22 +0200991 /* Display what was not skipped. */
992 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (p->msg != NULL)
994 msg_attr(p->msg, p->attr);
995
996 msg_hist_off = FALSE;
997}
998
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000999#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000/*
1001 * Call this after prompting the user. This will avoid a hit-return message
1002 * and a delay.
1003 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001004 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001005msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 need_wait_return = FALSE;
1008 emsg_on_display = FALSE;
1009 cmdline_row = msg_row;
1010 msg_col = 0;
1011 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001012 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014#endif
1015
1016/*
1017 * wait for the user to hit a key (normally a return)
1018 * if 'redraw' is TRUE, clear and redraw the screen
1019 * if 'redraw' is FALSE, just redraw the screen
1020 * if 'redraw' is -1, don't redraw at all
1021 */
1022 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001023wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 int c;
1026 int oldState;
1027 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001029 int save_Recording;
1030 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 if (redraw == TRUE)
1033 must_redraw = CLEAR;
1034
1035 /* If using ":silent cmd", don't wait for a return. Also don't set
1036 * need_wait_return to do it later. */
1037 if (msg_silent != 0)
1038 return;
1039
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001040 /*
1041 * When inside vgetc(), we can't wait for a typed character at all.
1042 * With the global command (and some others) we only need one return at
1043 * the end. Adjust cmdline_row to avoid the next message overwriting the
1044 * last one.
1045 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001046 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001048 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (no_wait_return)
1050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (!exmode_active)
1052 cmdline_row = msg_row;
1053 return;
1054 }
1055
1056 redir_off = TRUE; /* don't redirect this message */
1057 oldState = State;
1058 if (quit_more)
1059 {
1060 c = CAR; /* just pretend CR was hit */
1061 quit_more = FALSE;
1062 got_int = FALSE;
1063 }
1064 else if (exmode_active)
1065 {
1066 MSG_PUTS(" "); /* make sure the cursor is on the right line */
1067 c = CAR; /* no need for a return in ex mode */
1068 got_int = FALSE;
1069 }
1070 else
1071 {
1072 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1073 * just changed. */
1074 screenalloc(FALSE);
1075
1076 State = HITRETURN;
1077#ifdef FEAT_MOUSE
1078 setmouse();
1079#endif
1080#ifdef USE_ON_FLY_SCROLL
1081 dont_scroll = TRUE; /* disallow scrolling here */
1082#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001083 cmdline_row = msg_row;
1084
Bram Moolenaarec38d692013-04-24 15:12:32 +02001085 /* Avoid the sequence that the user types ":" at the hit-return prompt
1086 * to start an Ex command, but the file-changed dialog gets in the
1087 * way. */
1088 if (need_check_timestamps)
1089 check_timestamps(FALSE);
1090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 hit_return_msg();
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 do
1094 {
1095 /* Remember "got_int", if it is set vgetc() probably returns a
1096 * CTRL-C, but we need to loop then. */
1097 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001098
1099 /* Don't do mappings here, we put the character back in the
1100 * typeahead buffer. */
1101 ++no_mapping;
1102 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001103
1104 /* Temporarily disable Recording. If Recording is active, the
1105 * character will be recorded later, since it will be added to the
1106 * typebuf after the loop */
1107 save_Recording = Recording;
1108 save_scriptout = scriptout;
1109 Recording = FALSE;
1110 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001112 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001114 --no_mapping;
1115 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001116 Recording = save_Recording;
1117 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#ifdef FEAT_CLIPBOARD
1120 /* Strange way to allow copying (yanking) a modeless selection at
1121 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1122 * Cmdline-mode and it's harmless when there is no selection. */
1123 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1124 {
1125 clip_copy_modeless_selection(TRUE);
1126 c = K_IGNORE;
1127 }
1128#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001129
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001130 /*
1131 * Allow scrolling back in the messages.
1132 * Also accept scroll-down commands when messages fill the screen,
1133 * to avoid that typing one 'j' too many makes the messages
1134 * disappear.
1135 */
1136 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001137 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001138 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1139 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001140 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001141 if (msg_scrolled > Rows)
1142 /* scroll back to show older messages */
1143 do_more_prompt(c);
1144 else
1145 {
1146 msg_didout = FALSE;
1147 c = K_IGNORE;
1148 msg_col =
1149#ifdef FEAT_RIGHTLEFT
1150 cmdmsg_rl ? Columns - 1 :
1151#endif
1152 0;
1153 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001154 if (quit_more)
1155 {
1156 c = CAR; /* just pretend CR was hit */
1157 quit_more = FALSE;
1158 got_int = FALSE;
1159 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001160 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001161 {
1162 c = K_IGNORE;
1163 hit_return_msg();
1164 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001165 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001166 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001167 && (c == 'j' || c == 'd' || c == 'f'
1168 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001169 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 } while ((had_got_int && c == Ctrl_C)
1172 || c == K_IGNORE
1173#ifdef FEAT_GUI
1174 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1175#endif
1176#ifdef FEAT_MOUSE
1177 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1178 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1179 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001180 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001182 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 || (!mouse_has(MOUSE_RETURN)
1184 && mouse_row < msg_row
1185 && (c == K_LEFTMOUSE
1186 || c == K_MIDDLEMOUSE
1187 || c == K_RIGHTMOUSE
1188 || c == K_X1MOUSE
1189 || c == K_X2MOUSE))
1190#endif
1191 );
1192 ui_breakcheck();
1193#ifdef FEAT_MOUSE
1194 /*
1195 * Avoid that the mouse-up event causes visual mode to start.
1196 */
1197 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1198 || c == K_X1MOUSE || c == K_X2MOUSE)
1199 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1200 else
1201#endif
1202 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1203 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001204 /* Put the character back in the typeahead buffer. Don't use the
1205 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001206 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001208 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211 redir_off = FALSE;
1212
1213 /*
1214 * If the user hits ':', '?' or '/' we get a command line from the next
1215 * line.
1216 */
1217 if (c == ':' || c == '?' || c == '/')
1218 {
1219 if (!exmode_active)
1220 cmdline_row = msg_row;
1221 skip_redraw = TRUE; /* skip redraw once */
1222 do_redraw = FALSE;
1223 }
1224
1225 /*
1226 * If the window size changed set_shellsize() will redraw the screen.
1227 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1228 * typed.
1229 */
1230 tmpState = State;
1231 State = oldState; /* restore State before set_shellsize */
1232#ifdef FEAT_MOUSE
1233 setmouse();
1234#endif
1235 msg_check();
1236
1237#if defined(UNIX) || defined(VMS)
1238 /*
1239 * When switching screens, we need to output an extra newline on exit.
1240 */
1241 if (swapping_screen() && !termcap_active)
1242 newline_on_exit = TRUE;
1243#endif
1244
1245 need_wait_return = FALSE;
1246 did_wait_return = TRUE;
1247 emsg_on_display = FALSE; /* can delete error message now */
1248 lines_left = -1; /* reset lines_left at next msg_start() */
1249 reset_last_sourcing();
1250 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1251 (Rows - cmdline_row - 1) * Columns + sc_col)
1252 {
1253 vim_free(keep_msg);
1254 keep_msg = NULL; /* don't redisplay message, it's too long */
1255 }
1256
1257 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1258 {
1259 starttermcap(); /* start termcap before redrawing */
1260 shell_resized();
1261 }
1262 else if (!skip_redraw
1263 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1264 {
1265 starttermcap(); /* start termcap before redrawing */
1266 redraw_later(VALID);
1267 }
1268}
1269
1270/*
1271 * Write the hit-return prompt.
1272 */
1273 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001274hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001276 int save_p_more = p_more;
1277
1278 p_more = FALSE; /* don't want see this message when scrolling back */
1279 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 msg_putchar('\n');
1281 if (got_int)
1282 MSG_PUTS(_("Interrupt: "));
1283
Bram Moolenaar8820b482017-03-16 17:23:31 +01001284 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (!msg_use_printf())
1286 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001287 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288}
1289
1290/*
1291 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1292 */
1293 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001294set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 vim_free(keep_msg);
1297 if (s != NULL && msg_silent == 0)
1298 keep_msg = vim_strsave(s);
1299 else
1300 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001301 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001302 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001305#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1306/*
1307 * If there currently is a message being displayed, set "keep_msg" to it, so
1308 * that it will be displayed again after redraw.
1309 */
1310 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001311set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001312{
1313 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1314 && (State & NORMAL))
1315 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1316}
1317#endif
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319/*
1320 * Prepare for outputting characters in the command line.
1321 */
1322 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001323msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
1325 int did_return = FALSE;
1326
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001327 if (!msg_silent)
1328 {
1329 vim_free(keep_msg);
1330 keep_msg = NULL; /* don't display old message now */
1331 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001332
1333#ifdef FEAT_EVAL
1334 if (need_clr_eos)
1335 {
1336 /* Halfway an ":echo" command and getting an (error) message: clear
1337 * any text from the command. */
1338 need_clr_eos = FALSE;
1339 msg_clr_eos();
1340 }
1341#endif
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (!msg_scroll && full_screen) /* overwrite last message */
1344 {
1345 msg_row = cmdline_row;
1346 msg_col =
1347#ifdef FEAT_RIGHTLEFT
1348 cmdmsg_rl ? Columns - 1 :
1349#endif
1350 0;
1351 }
1352 else if (msg_didout) /* start message on next line */
1353 {
1354 msg_putchar('\n');
1355 did_return = TRUE;
1356 if (exmode_active != EXMODE_NORMAL)
1357 cmdline_row = msg_row;
1358 }
1359 if (!msg_didany || lines_left < 0)
1360 msg_starthere();
1361 if (msg_silent == 0)
1362 {
1363 msg_didout = FALSE; /* no output on current line yet */
1364 cursor_off();
1365 }
1366
1367 /* when redirecting, may need to start a new line. */
1368 if (!did_return)
1369 redir_write((char_u *)"\n", -1);
1370}
1371
1372/*
1373 * Note that the current msg position is where messages start.
1374 */
1375 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001376msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 lines_left = cmdline_row;
1379 msg_didany = FALSE;
1380}
1381
1382 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001383msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 msg_putchar_attr(c, 0);
1386}
1387
1388 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001389msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391#ifdef FEAT_MBYTE
1392 char_u buf[MB_MAXBYTES + 1];
1393#else
1394 char_u buf[4];
1395#endif
1396
1397 if (IS_SPECIAL(c))
1398 {
1399 buf[0] = K_SPECIAL;
1400 buf[1] = K_SECOND(c);
1401 buf[2] = K_THIRD(c);
1402 buf[3] = NUL;
1403 }
1404 else
1405 {
1406#ifdef FEAT_MBYTE
1407 buf[(*mb_char2bytes)(c, buf)] = NUL;
1408#else
1409 buf[0] = c;
1410 buf[1] = NUL;
1411#endif
1412 }
1413 msg_puts_attr(buf, attr);
1414}
1415
1416 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001417msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 char_u buf[20];
1420
1421 sprintf((char *)buf, "%ld", n);
1422 msg_puts(buf);
1423}
1424
1425 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001426msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 msg_home_replace_attr(fname, 0);
1429}
1430
1431#if defined(FEAT_FIND_ID) || defined(PROTO)
1432 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001433msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001435 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436}
1437#endif
1438
1439 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001440msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 char_u *name;
1443
1444 name = home_replace_save(NULL, fname);
1445 if (name != NULL)
1446 msg_outtrans_attr(name, attr);
1447 vim_free(name);
1448}
1449
1450/*
1451 * Output 'len' characters in 'str' (including NULs) with translation
1452 * if 'len' is -1, output upto a NUL character.
1453 * Use attributes 'attr'.
1454 * Return the number of characters it takes on the screen.
1455 */
1456 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001457msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 return msg_outtrans_attr(str, 0);
1460}
1461
1462 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001463msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1466}
1467
1468 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001469msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 return msg_outtrans_len_attr(str, len, 0);
1472}
1473
1474/*
1475 * Output one character at "p". Return pointer to the next character.
1476 * Handles multi-byte characters.
1477 */
1478 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001479msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481#ifdef FEAT_MBYTE
1482 int l;
1483
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001484 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 msg_outtrans_len_attr(p, l, attr);
1487 return p + l;
1488 }
1489#endif
1490 msg_puts_attr(transchar_byte(*p), attr);
1491 return p + 1;
1492}
1493
1494 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001495msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 int retval = 0;
1498 char_u *str = msgstr;
1499 char_u *plain_start = msgstr;
1500 char_u *s;
1501#ifdef FEAT_MBYTE
1502 int mb_l;
1503 int c;
1504#endif
1505
1506 /* if MSG_HIST flag set, add message to history */
1507 if (attr & MSG_HIST)
1508 {
1509 add_msg_hist(str, len, attr);
1510 attr &= ~MSG_HIST;
1511 }
1512
1513#ifdef FEAT_MBYTE
1514 /* If the string starts with a composing character first draw a space on
1515 * which the composing char can be drawn. */
1516 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1517 msg_puts_attr((char_u *)" ", attr);
1518#endif
1519
1520 /*
1521 * Go over the string. Special characters are translated and printed.
1522 * Normal characters are printed several at a time.
1523 */
1524 while (--len >= 0)
1525 {
1526#ifdef FEAT_MBYTE
1527 if (enc_utf8)
1528 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001529 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001531 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 else
1533 mb_l = 1;
1534 if (has_mbyte && mb_l > 1)
1535 {
1536 c = (*mb_ptr2char)(str);
1537 if (vim_isprintc(c))
1538 /* printable multi-byte char: count the cells. */
1539 retval += (*mb_ptr2cells)(str);
1540 else
1541 {
1542 /* unprintable multi-byte char: print the printable chars so
1543 * far and the translation of the unprintable char. */
1544 if (str > plain_start)
1545 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1546 attr);
1547 plain_start = str + mb_l;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001548 msg_puts_attr(transchar(c), attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 retval += char2cells(c);
1550 }
1551 len -= mb_l - 1;
1552 str += mb_l;
1553 }
1554 else
1555#endif
1556 {
1557 s = transchar_byte(*str);
1558 if (s[1] != NUL)
1559 {
1560 /* unprintable char: print the printable chars so far and the
1561 * translation of the unprintable char. */
1562 if (str > plain_start)
1563 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1564 attr);
1565 plain_start = str + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001566 msg_puts_attr(s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001567 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001569 else
1570 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 ++str;
1572 }
1573 }
1574
1575 if (str > plain_start)
1576 /* print the printable chars at the end */
1577 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1578
1579 return retval;
1580}
1581
1582#if defined(FEAT_QUICKFIX) || defined(PROTO)
1583 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001584msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 int i;
1587 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1588
1589 arg = skipwhite(arg);
1590 for (i = 5; *arg && i >= 0; --i)
1591 if (*arg++ != str[i])
1592 break;
1593 if (i < 0)
1594 {
1595 msg_putchar('\n');
1596 for (i = 0; rs[i]; ++i)
1597 msg_putchar(rs[i] - 3);
1598 }
1599}
1600#endif
1601
1602/*
1603 * Output the string 'str' upto a NUL character.
1604 * Return the number of characters it takes on the screen.
1605 *
1606 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1607 * following character and shown as <F1>, <S-Up> etc. Any other character
1608 * which is not printable shown in <> form.
1609 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1610 * If a character is displayed in one of these special ways, is also
1611 * highlighted (its highlight name is '8' in the p_hl variable).
1612 * Otherwise characters are not highlighted.
1613 * This function is used to show mappings, where we want to see how to type
1614 * the character/string -- webb
1615 */
1616 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001617msg_outtrans_special(
1618 char_u *strstart,
1619 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 char_u *str = strstart;
1622 int retval = 0;
1623 char_u *string;
1624 int attr;
1625 int len;
1626
Bram Moolenaar8820b482017-03-16 17:23:31 +01001627 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 while (*str != NUL)
1629 {
1630 /* Leading and trailing spaces need to be displayed in <> form. */
1631 if ((str == strstart || str[1] == NUL) && *str == ' ')
1632 {
1633 string = (char_u *)"<Space>";
1634 ++str;
1635 }
1636 else
1637 string = str2special(&str, from);
1638 len = vim_strsize(string);
1639 /* Highlight special keys */
1640 msg_puts_attr(string, len > 1
1641#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001642 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643#endif
1644 ? attr : 0);
1645 retval += len;
1646 }
1647 return retval;
1648}
1649
Bram Moolenaarbd743252010-10-20 21:23:33 +02001650#if defined(FEAT_EVAL) || defined(PROTO)
1651/*
1652 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1653 * strings, in an allocated string.
1654 */
1655 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001656str2special_save(
1657 char_u *str,
1658 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001659{
1660 garray_T ga;
1661 char_u *p = str;
1662
1663 ga_init2(&ga, 1, 40);
1664 while (*p != NUL)
1665 ga_concat(&ga, str2special(&p, is_lhs));
1666 ga_append(&ga, NUL);
1667 return (char_u *)ga.ga_data;
1668}
1669#endif
1670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671/*
1672 * Return the printable string for the key codes at "*sp".
1673 * Used for translating the lhs or rhs of a mapping to printable chars.
1674 * Advances "sp" to the next code.
1675 */
1676 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001677str2special(
1678 char_u **sp,
1679 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
1681 int c;
1682 static char_u buf[7];
1683 char_u *str = *sp;
1684 int modifiers = 0;
1685 int special = FALSE;
1686
1687#ifdef FEAT_MBYTE
1688 if (has_mbyte)
1689 {
1690 char_u *p;
1691
1692 /* Try to un-escape a multi-byte character. Return the un-escaped
1693 * string if it is a multi-byte character. */
1694 p = mb_unescape(sp);
1695 if (p != NULL)
1696 return p;
1697 }
1698#endif
1699
1700 c = *str;
1701 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1702 {
1703 if (str[1] == KS_MODIFIER)
1704 {
1705 modifiers = str[2];
1706 str += 3;
1707 c = *str;
1708 }
1709 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1710 {
1711 c = TO_SPECIAL(str[1], str[2]);
1712 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001713 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 c = NUL;
1715 }
1716 if (IS_SPECIAL(c) || modifiers) /* special key */
1717 special = TRUE;
1718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001721 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001723 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001724
1725 /* For multi-byte characters check for an illegal byte. */
1726 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1727 {
1728 transchar_nonprint(buf, c);
1729 *sp = str + 1;
1730 return buf;
1731 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001732 /* Since 'special' is TRUE the multi-byte character 'c' will be
1733 * processed by get_special_key_name() */
1734 c = (*mb_ptr2char)(str);
1735 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001737 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001739 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
1741 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1742 * Use <Space> only for lhs of a mapping. */
1743 if (special || char2cells(c) > 1 || (from && c == ' '))
1744 return get_special_key_name(c, modifiers);
1745 buf[0] = c;
1746 buf[1] = NUL;
1747 return buf;
1748}
1749
1750/*
1751 * Translate a key sequence into special key names.
1752 */
1753 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001754str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
1756 char_u *s;
1757
1758 *buf = NUL;
1759 while (*sp)
1760 {
1761 s = str2special(&sp, FALSE);
1762 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1763 STRCAT(buf, s);
1764 }
1765}
1766
1767/*
1768 * print line for :print or :list command
1769 */
1770 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001771msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
1773 int c;
1774 int col = 0;
1775 int n_extra = 0;
1776 int c_extra = 0;
1777 char_u *p_extra = NULL; /* init to make SASC shut up */
1778 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001779 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 char_u *trail = NULL;
1781#ifdef FEAT_MBYTE
1782 int l;
1783 char_u buf[MB_MAXBYTES + 1];
1784#endif
1785
Bram Moolenaardf177f62005-02-22 08:39:57 +00001786 if (curwin->w_p_list)
1787 list = TRUE;
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001790 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001793 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 --trail;
1795 }
1796
1797 /* output a space for an empty line, otherwise the line will be
1798 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001799 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 msg_putchar(' ');
1801
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001802 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001804 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
1806 --n_extra;
1807 if (c_extra)
1808 c = c_extra;
1809 else
1810 c = *p_extra++;
1811 }
1812#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001813 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
1815 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001816 if (lcs_nbsp != NUL && list
1817 && (mb_ptr2char(s) == 160
1818 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001819 {
1820 mb_char2bytes(lcs_nbsp, buf);
1821 buf[(*mb_ptr2len)(buf)] = NUL;
1822 }
1823 else
1824 {
1825 mch_memmove(buf, s, (size_t)l);
1826 buf[l] = NUL;
1827 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001828 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 s += l;
1830 continue;
1831 }
1832#endif
1833 else
1834 {
1835 attr = 0;
1836 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001837 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 /* tab amount depends on current column */
1840 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001841 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
1843 c = ' ';
1844 c_extra = ' ';
1845 }
1846 else
1847 {
1848 c = lcs_tab1;
1849 c_extra = lcs_tab2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001850 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001853 else if (c == 160 && list && lcs_nbsp != NUL)
1854 {
1855 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001856 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001857 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001858 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
1860 p_extra = (char_u *)"";
1861 c_extra = NUL;
1862 n_extra = 1;
1863 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001864 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 --s;
1866 }
1867 else if (c != NUL && (n = byte2cells(c)) > 1)
1868 {
1869 n_extra = n - 1;
1870 p_extra = transchar_byte(c);
1871 c_extra = NUL;
1872 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001873 /* Use special coloring to be able to distinguish <hex> from
1874 * the same in plain text. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001875 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 else if (c == ' ' && trail != NULL && s > trail)
1878 {
1879 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001880 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001882 else if (c == ' ' && list && lcs_space != NUL)
1883 {
1884 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001885 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888
1889 if (c == NUL)
1890 break;
1891
1892 msg_putchar_attr(c, attr);
1893 col++;
1894 }
1895 msg_clr_eos();
1896}
1897
1898#ifdef FEAT_MBYTE
1899/*
1900 * Use screen_puts() to output one multi-byte character.
1901 * Return the pointer "s" advanced to the next character.
1902 */
1903 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001904screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
1906 int cw;
1907
1908 msg_didout = TRUE; /* remember that line is not empty */
1909 cw = (*mb_ptr2cells)(s);
1910 if (cw > 1 && (
1911#ifdef FEAT_RIGHTLEFT
1912 cmdmsg_rl ? msg_col <= 1 :
1913#endif
1914 msg_col == Columns - 1))
1915 {
1916 /* Doesn't fit, print a highlighted '>' to fill it up. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001917 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 return s;
1919 }
1920
1921 screen_puts_len(s, l, msg_row, msg_col, attr);
1922#ifdef FEAT_RIGHTLEFT
1923 if (cmdmsg_rl)
1924 {
1925 msg_col -= cw;
1926 if (msg_col == 0)
1927 {
1928 msg_col = Columns;
1929 ++msg_row;
1930 }
1931 }
1932 else
1933#endif
1934 {
1935 msg_col += cw;
1936 if (msg_col >= Columns)
1937 {
1938 msg_col = 0;
1939 ++msg_row;
1940 }
1941 }
1942 return s + l;
1943}
1944#endif
1945
1946/*
1947 * Output a string to the screen at position msg_row, msg_col.
1948 * Update msg_row and msg_col for the next message.
1949 */
1950 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001951msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001953 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954}
1955
1956 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001957msg_puts_title(
1958 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001960 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961}
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963/*
1964 * Show a message in such a way that it always fits in the line. Cut out a
1965 * part in the middle and replace it with "..." when necessary.
1966 * Does not handle multi-byte characters!
1967 */
1968 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001969msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001971 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972}
1973
1974 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001975msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 int slen = len;
1978 int room;
1979
1980 room = Columns - msg_col;
1981 if (len > room && room >= 20)
1982 {
1983 slen = (room - 3) / 2;
1984 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01001985 msg_puts_attr((char_u *)"...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1988}
1989
1990/*
1991 * Basic function for writing a message with highlight attributes.
1992 */
1993 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001994msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 msg_puts_attr_len(s, -1, attr);
1997}
1998
1999/*
2000 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2001 * When "maxlen" is -1 there is no maximum length.
2002 * When "maxlen" is >= 0 the message is not put in the history.
2003 */
2004 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002005msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 /*
2008 * If redirection is on, also write to the redirection file.
2009 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002010 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 /*
2013 * Don't print anything when using ":silent cmd".
2014 */
2015 if (msg_silent != 0)
2016 return;
2017
2018 /* if MSG_HIST flag set, add message to history */
2019 if ((attr & MSG_HIST) && maxlen < 0)
2020 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002021 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 attr &= ~MSG_HIST;
2023 }
2024
2025 /*
2026 * When writing something to the screen after it has scrolled, requires a
2027 * wait-return prompt later. Needed when scrolling, resetting
2028 * need_wait_return after some prompt, and then outputting something
2029 * without scrolling
2030 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002031 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 need_wait_return = TRUE;
2033 msg_didany = TRUE; /* remember that something was outputted */
2034
2035 /*
2036 * If there is no valid screen, use fprintf so we can see error messages.
2037 * If termcap is not active, we may be writing in an alternate console
2038 * window, cursor positioning may not work correctly (window size may be
2039 * different, e.g. for Win32 console) or we just don't know where the
2040 * cursor is.
2041 */
2042 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002043 msg_puts_printf(str, maxlen);
2044 else
2045 msg_puts_display(str, maxlen, attr, FALSE);
2046}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002048/*
2049 * The display part of msg_puts_attr_len().
2050 * May be called recursively to display scroll-back text.
2051 */
2052 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002053msg_puts_display(
2054 char_u *str,
2055 int maxlen,
2056 int attr,
2057 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002058{
2059 char_u *s = str;
2060 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2061 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
2062#ifdef FEAT_MBYTE
2063 int l;
2064 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002066 char_u *sb_str = str;
2067 int sb_col = msg_col;
2068 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002069 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
2071 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002072 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
2074 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002075 * We are at the end of the screen line when:
2076 * - When outputting a newline.
2077 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002079 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef FEAT_RIGHTLEFT
2081 cmdmsg_rl
2082 ? (
2083 msg_col <= 1
2084 || (*s == TAB && msg_col <= 7)
2085# ifdef FEAT_MBYTE
2086 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
2087# endif
2088 )
2089 :
2090#endif
2091 (msg_col + t_col >= Columns - 1
2092 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
2093# ifdef FEAT_MBYTE
2094 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2095 && msg_col + t_col >= Columns - 2)
2096# endif
2097 ))))
2098 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002099 /*
2100 * The screen is scrolled up when at the last row (some terminals
2101 * scroll automatically, some don't. To avoid problems we scroll
2102 * ourselves).
2103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002106 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002108 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (msg_no_more && lines_left == 0)
2110 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002112 /* Scroll the screen up one line. */
2113 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
2115 msg_row = Rows - 2;
2116 if (msg_col >= Columns) /* can happen after screen resize */
2117 msg_col = Columns - 1;
2118
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002119 /* Display char in last column before showing more-prompt. */
2120 if (*s >= ' '
2121#ifdef FEAT_RIGHTLEFT
2122 && !cmdmsg_rl
2123#endif
2124 )
2125 {
2126#ifdef FEAT_MBYTE
2127 if (has_mbyte)
2128 {
2129 if (enc_utf8 && maxlen >= 0)
2130 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002131 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002132 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002133 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002134 s = screen_puts_mbyte(s, l, attr);
2135 }
2136 else
2137#endif
2138 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002139 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002140 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002141 else
2142 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002143
2144 if (p_more)
2145 /* store text for scrolling back */
2146 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2147
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002148 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002149 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 redraw_cmdline = TRUE;
2151 if (cmdline_row > 0 && !exmode_active)
2152 --cmdline_row;
2153
2154 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002155 * If screen is completely filled and 'more' is set then wait
2156 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002158 if (lines_left > 0)
2159 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002160 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 && !msg_no_more && !exmode_active)
2162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002164 if (do_more_prompt(NUL))
2165 s = confirm_msg_tail;
2166#else
2167 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#endif
2169 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002170 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002172
2173 /* When we displayed a char in last column need to check if there
2174 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002175 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002176 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002179 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 || msg_col + t_col >= Columns
2181#ifdef FEAT_MBYTE
2182 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2183 && msg_col + t_col >= Columns - 1)
2184#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002185 ;
2186 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2187 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002189 t_puts(&t_col, t_s, s, attr);
2190
2191 if (wrap && p_more && !recurse)
2192 /* store text for scrolling back */
2193 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 if (*s == '\n') /* go to next line */
2196 {
2197 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002198#ifdef FEAT_RIGHTLEFT
2199 if (cmdmsg_rl)
2200 msg_col = Columns - 1;
2201 else
2202#endif
2203 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (++msg_row >= Rows) /* safety check */
2205 msg_row = Rows - 1;
2206 }
2207 else if (*s == '\r') /* go to column 0 */
2208 {
2209 msg_col = 0;
2210 }
2211 else if (*s == '\b') /* go to previous char */
2212 {
2213 if (msg_col)
2214 --msg_col;
2215 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002216 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 do
2219 msg_screen_putchar(' ', attr);
2220 while (msg_col & 7);
2221 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002222 else if (*s == BELL) /* beep (from ":sh") */
2223 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 else
2225 {
2226#ifdef FEAT_MBYTE
2227 if (has_mbyte)
2228 {
2229 cw = (*mb_ptr2cells)(s);
2230 if (enc_utf8 && maxlen >= 0)
2231 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002232 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002234 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
2236 else
2237 {
2238 cw = 1;
2239 l = 1;
2240 }
2241#endif
2242 /* When drawing from right to left or when a double-wide character
2243 * doesn't fit, draw a single character here. Otherwise collect
2244 * characters and draw them all at once later. */
2245#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2246 if (
2247# ifdef FEAT_RIGHTLEFT
2248 cmdmsg_rl
2249# ifdef FEAT_MBYTE
2250 ||
2251# endif
2252# endif
2253# ifdef FEAT_MBYTE
2254 (cw > 1 && msg_col + t_col >= Columns - 1)
2255# endif
2256 )
2257 {
2258# ifdef FEAT_MBYTE
2259 if (l > 1)
2260 s = screen_puts_mbyte(s, l, attr) - 1;
2261 else
2262# endif
2263 msg_screen_putchar(*s, attr);
2264 }
2265 else
2266#endif
2267 {
2268 /* postpone this character until later */
2269 if (t_col == 0)
2270 t_s = s;
2271#ifdef FEAT_MBYTE
2272 t_col += cw;
2273 s += l - 1;
2274#else
2275 ++t_col;
2276#endif
2277 }
2278 }
2279 ++s;
2280 }
2281
2282 /* output any postponed text */
2283 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002284 t_puts(&t_col, t_s, s, attr);
2285 if (p_more && !recurse)
2286 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288 msg_check();
2289}
2290
2291/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002292 * Return TRUE when ":filter pattern" was used and "msg" does not match
2293 * "pattern".
2294 */
2295 int
2296message_filtered(char_u *msg)
2297{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002298 int match;
2299
2300 if (cmdmod.filter_regmatch.regprog == NULL)
2301 return FALSE;
2302 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2303 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002304}
2305
2306/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002307 * Scroll the screen up one line for displaying the next message line.
2308 */
2309 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002310msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002311{
2312#ifdef FEAT_GUI
2313 /* Remove the cursor before scrolling, ScreenLines[] is going
2314 * to become invalid. */
2315 if (gui.in_use)
2316 gui_undraw_cursor();
2317#endif
2318 /* scrolling up always works */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002319 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002320 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002321 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002322
2323 if (!can_clear((char_u *)" "))
2324 {
2325 /* Scrolling up doesn't result in the right background. Set the
2326 * background here. It's not efficient, but avoids that we have to do
2327 * it all over the code. */
2328 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2329
2330 /* Also clear the last char of the last but one line if it was not
2331 * cleared before to avoid a scroll-up. */
2332 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2333 screen_fill((int)Rows - 2, (int)Rows - 1,
2334 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2335 }
2336}
2337
2338/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002339 * Increment "msg_scrolled".
2340 */
2341 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002342inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002343{
2344#ifdef FEAT_EVAL
2345 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2346 {
2347 char_u *p = sourcing_name;
2348 char_u *tofree = NULL;
2349 int len;
2350
2351 /* v:scrollstart is empty, set it to the script/function name and line
2352 * number */
2353 if (p == NULL)
2354 p = (char_u *)_("Unknown");
2355 else
2356 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002357 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002358 tofree = alloc(len);
2359 if (tofree != NULL)
2360 {
2361 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2362 p, (long)sourcing_lnum);
2363 p = tofree;
2364 }
2365 }
2366 set_vim_var_string(VV_SCROLLSTART, p, -1);
2367 vim_free(tofree);
2368 }
2369#endif
2370 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002371 if (must_redraw < VALID)
2372 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002373}
2374
2375/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002376 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2377 * store the displayed text and remember where screen lines start.
2378 */
2379typedef struct msgchunk_S msgchunk_T;
2380struct msgchunk_S
2381{
2382 msgchunk_T *sb_next;
2383 msgchunk_T *sb_prev;
2384 char sb_eol; /* TRUE when line ends after this text */
2385 int sb_msg_col; /* column in which text starts */
2386 int sb_attr; /* text attributes */
2387 char_u sb_text[1]; /* text to be displayed, actually longer */
2388};
2389
2390static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2391
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002392static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2393static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002394
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002395typedef enum {
2396 SB_CLEAR_NONE = 0,
2397 SB_CLEAR_ALL,
2398 SB_CLEAR_CMDLINE_BUSY,
2399 SB_CLEAR_CMDLINE_DONE
2400} sb_clear_T;
2401
2402/* When to clear text on next msg. */
2403static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002404
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002405/*
2406 * Store part of a printed message for displaying when scrolling back.
2407 */
2408 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002409store_sb_text(
2410 char_u **sb_str, /* start of string */
2411 char_u *s, /* just after string */
2412 int attr,
2413 int *sb_col,
2414 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002415{
2416 msgchunk_T *mp;
2417
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002418 if (do_clear_sb_text == SB_CLEAR_ALL
2419 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002420 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002421 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2422 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002423 }
2424
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002425 if (s > *sb_str)
2426 {
2427 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2428 if (mp != NULL)
2429 {
2430 mp->sb_eol = finish;
2431 mp->sb_msg_col = *sb_col;
2432 mp->sb_attr = attr;
2433 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2434
2435 if (last_msgchunk == NULL)
2436 {
2437 last_msgchunk = mp;
2438 mp->sb_prev = NULL;
2439 }
2440 else
2441 {
2442 mp->sb_prev = last_msgchunk;
2443 last_msgchunk->sb_next = mp;
2444 last_msgchunk = mp;
2445 }
2446 mp->sb_next = NULL;
2447 }
2448 }
2449 else if (finish && last_msgchunk != NULL)
2450 last_msgchunk->sb_eol = TRUE;
2451
2452 *sb_str = s;
2453 *sb_col = 0;
2454}
2455
2456/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002457 * Finished showing messages, clear the scroll-back text on the next message.
2458 */
2459 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002460may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002461{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002462 do_clear_sb_text = SB_CLEAR_ALL;
2463}
2464
2465/*
2466 * Starting to edit the command line, do not clear messages now.
2467 */
2468 void
2469sb_text_start_cmdline(void)
2470{
2471 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2472 msg_sb_eol();
2473}
2474
2475/*
2476 * Ending to edit the command line. Clear old lines but the last one later.
2477 */
2478 void
2479sb_text_end_cmdline(void)
2480{
2481 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002482}
2483
2484/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002485 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002486 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002487 * Called when redrawing the screen.
2488 */
2489 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002490clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002491{
2492 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002493 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002494
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002495 if (all)
2496 lastp = &last_msgchunk;
2497 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002498 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002499 if (last_msgchunk == NULL)
2500 return;
2501 lastp = &last_msgchunk->sb_prev;
2502 }
2503
2504 while (*lastp != NULL)
2505 {
2506 mp = (*lastp)->sb_prev;
2507 vim_free(*lastp);
2508 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002509 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510}
2511
2512/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002513 * "g<" command.
2514 */
2515 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002516show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002517{
2518 msgchunk_T *mp;
2519
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002520 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002521 * weird, typing a command without output results in one line. */
2522 mp = msg_sb_start(last_msgchunk);
2523 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002524 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002525 else
2526 {
2527 do_more_prompt('G');
2528 wait_return(FALSE);
2529 }
2530}
2531
2532/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002533 * Move to the start of screen line in already displayed text.
2534 */
2535 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002536msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002537{
2538 msgchunk_T *mp = mps;
2539
2540 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2541 mp = mp->sb_prev;
2542 return mp;
2543}
2544
2545/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002546 * Mark the last message chunk as finishing the line.
2547 */
2548 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002549msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002550{
2551 if (last_msgchunk != NULL)
2552 last_msgchunk->sb_eol = TRUE;
2553}
2554
2555/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002556 * Display a screen line from previously displayed text at row "row".
2557 * Returns a pointer to the text for the next line (can be NULL).
2558 */
2559 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002560disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002561{
2562 msgchunk_T *mp = smp;
2563 char_u *p;
2564
2565 for (;;)
2566 {
2567 msg_row = row;
2568 msg_col = mp->sb_msg_col;
2569 p = mp->sb_text;
2570 if (*p == '\n') /* don't display the line break */
2571 ++p;
2572 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2573 if (mp->sb_eol || mp->sb_next == NULL)
2574 break;
2575 mp = mp->sb_next;
2576 }
2577 return mp->sb_next;
2578}
2579
2580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 * Output any postponed text for msg_puts_attr_len().
2582 */
2583 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002584t_puts(
2585 int *t_col,
2586 char_u *t_s,
2587 char_u *s,
2588 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
2590 /* output postponed text */
2591 msg_didout = TRUE; /* remember that line is not empty */
2592 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002593 msg_col += *t_col;
2594 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595#ifdef FEAT_MBYTE
2596 /* If the string starts with a composing character don't increment the
2597 * column position for it. */
2598 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2599 --msg_col;
2600#endif
2601 if (msg_col >= Columns)
2602 {
2603 msg_col = 0;
2604 ++msg_row;
2605 }
2606}
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/*
2609 * Returns TRUE when messages should be printed with mch_errmsg().
2610 * This is used when there is no valid screen, so we can see error messages.
2611 * If termcap is not active, we may be writing in an alternate console
2612 * window, cursor positioning may not work correctly (window size may be
2613 * different, e.g. for Win32 console) or we just don't know where the
2614 * cursor is.
2615 */
2616 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002617msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 return (!msg_check_screen()
2620#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2621 || !termcap_active
2622#endif
2623 || (swapping_screen() && !termcap_active)
2624 );
2625}
2626
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002627/*
2628 * Print a message when there is no valid screen.
2629 */
2630 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002631msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002632{
2633 char_u *s = str;
2634 char_u buf[4];
2635 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002636#ifdef WIN3264
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002637# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2638 char_u *ccp = NULL;
2639
2640# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002641 if (!(silent_mode && p_verbose == 0))
2642 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002643
2644# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2645 if (enc_codepage >= 0 && (int)GetConsoleCP() != enc_codepage)
2646 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02002647 int inlen = (int)STRLEN(str);
Bram Moolenaar1b66c002017-08-03 18:55:00 +02002648 int outlen;
2649 WCHAR *widestr = (WCHAR *)enc_to_utf16(str, &inlen);
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002650
2651 if (widestr != NULL)
2652 {
Bram Moolenaar1b66c002017-08-03 18:55:00 +02002653 WideCharToMultiByte_alloc(GetConsoleCP(), 0, widestr, inlen,
2654 (LPSTR *)&ccp, &outlen, 0, 0);
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002655 vim_free(widestr);
2656 s = str = ccp;
2657 }
2658 }
2659# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002660#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002661 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002662 {
2663 if (!(silent_mode && p_verbose == 0))
2664 {
2665 /* NL --> CR NL translation (for Unix, not for "--version") */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002666 p = &buf[0];
2667 if (*s == '\n' && !info_message)
2668 *p++ = '\r';
Bram Moolenaard0573012017-10-28 21:11:06 +02002669#if defined(USE_CR)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002670 else
2671#endif
2672 *p++ = *s;
2673 *p = '\0';
2674 if (info_message) /* informative message, not an error */
2675 mch_msg((char *)buf);
2676 else
2677 mch_errmsg((char *)buf);
2678 }
2679
2680 /* primitive way to compute the current column */
2681#ifdef FEAT_RIGHTLEFT
2682 if (cmdmsg_rl)
2683 {
2684 if (*s == '\r' || *s == '\n')
2685 msg_col = Columns - 1;
2686 else
2687 --msg_col;
2688 }
2689 else
2690#endif
2691 {
2692 if (*s == '\r' || *s == '\n')
2693 msg_col = 0;
2694 else
2695 ++msg_col;
2696 }
2697 ++s;
2698 }
2699 msg_didout = TRUE; /* assume that line is not empty */
2700
2701#ifdef WIN3264
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002702# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2703 vim_free(ccp);
2704# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002705 if (!(silent_mode && p_verbose == 0))
2706 mch_settmode(TMODE_RAW);
2707#endif
2708}
2709
2710/*
2711 * Show the more-prompt and handle the user response.
2712 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002713 * When at hit-enter prompt "typed_char" is the already typed character,
2714 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002715 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2716 */
2717 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002718do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002719{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002720 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 int used_typed_char = typed_char;
2722 int oldState = State;
2723 int c;
2724#ifdef FEAT_CON_DIALOG
2725 int retval = FALSE;
2726#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002727 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728 msgchunk_T *mp_last = NULL;
2729 msgchunk_T *mp;
2730 int i;
2731
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002732 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002733 * that case don't show another prompt. Also when at the hit-Enter prompt
2734 * and nothing was typed. */
2735 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002736 return FALSE;
2737 entered = TRUE;
2738
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002739 if (typed_char == 'G')
2740 {
2741 /* "g<": Find first line on the last page. */
2742 mp_last = msg_sb_start(last_msgchunk);
2743 for (i = 0; i < Rows - 2 && mp_last != NULL
2744 && mp_last->sb_prev != NULL; ++i)
2745 mp_last = msg_sb_start(mp_last->sb_prev);
2746 }
2747
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002748 State = ASKMORE;
2749#ifdef FEAT_MOUSE
2750 setmouse();
2751#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002752 if (typed_char == NUL)
2753 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002754 for (;;)
2755 {
2756 /*
2757 * Get a typed character directly from the user.
2758 */
2759 if (used_typed_char != NUL)
2760 {
2761 c = used_typed_char; /* was typed at hit-enter prompt */
2762 used_typed_char = NUL;
2763 }
2764 else
2765 c = get_keystroke();
2766
2767#if defined(FEAT_MENU) && defined(FEAT_GUI)
2768 if (c == K_MENU)
2769 {
2770 int idx = get_menu_index(current_menu, ASKMORE);
2771
2772 /* Used a menu. If it starts with CTRL-Y, it must
2773 * be a "Copy" for the clipboard. Otherwise
2774 * assume that we end */
2775 if (idx == MENU_INDEX_INVALID)
2776 continue;
2777 c = *current_menu->strings[idx];
2778 if (c != NUL && current_menu->strings[idx][1] != NUL)
2779 ins_typebuf(current_menu->strings[idx] + 1,
2780 current_menu->noremap[idx], 0, TRUE,
2781 current_menu->silent[idx]);
2782 }
2783#endif
2784
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002785 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002786 switch (c)
2787 {
2788 case BS: /* scroll one line back */
2789 case K_BS:
2790 case 'k':
2791 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002792 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 break;
2794
2795 case CAR: /* one extra line */
2796 case NL:
2797 case 'j':
2798 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002799 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002800 break;
2801
2802 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002803 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002804 break;
2805
2806 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002807 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002808 break;
2809
2810 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002811 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002812 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 break;
2814
2815 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002816 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 case K_PAGEDOWN:
2818 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002819 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002820 break;
2821
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002822 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002823 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002824 break;
2825
2826 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002827 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002828 lines_left = 999999;
2829 break;
2830
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002831 case ':': /* start new command line */
2832#ifdef FEAT_CON_DIALOG
2833 if (!confirm_msg_used)
2834#endif
2835 {
2836 /* Since got_int is set all typeahead will be flushed, but we
2837 * want to keep this ':', remember that in a special way. */
2838 typeahead_noflush(':');
2839 cmdline_row = Rows - 1; /* put ':' on this line */
2840 skip_redraw = TRUE; /* skip redraw once */
2841 need_wait_return = FALSE; /* don't wait in main() */
2842 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002843 /* FALLTHROUGH */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002844 case 'q': /* quit */
2845 case Ctrl_C:
2846 case ESC:
2847#ifdef FEAT_CON_DIALOG
2848 if (confirm_msg_used)
2849 {
2850 /* Jump to the choices of the dialog. */
2851 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002852 }
2853 else
2854#endif
2855 {
2856 got_int = TRUE;
2857 quit_more = TRUE;
2858 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002859 /* When there is some more output (wrapping line) display that
2860 * without another prompt. */
2861 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002862 break;
2863
2864#ifdef FEAT_CLIPBOARD
2865 case Ctrl_Y:
2866 /* Strange way to allow copying (yanking) a modeless
2867 * selection at the more prompt. Use CTRL-Y,
2868 * because the same is used in Cmdline-mode and at the
2869 * hit-enter prompt. However, scrolling one line up
2870 * might be expected... */
2871 if (clip_star.state == SELECT_DONE)
2872 clip_copy_modeless_selection(TRUE);
2873 continue;
2874#endif
2875 default: /* no valid response */
2876 msg_moremsg(TRUE);
2877 continue;
2878 }
2879
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002880 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002881 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002882 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 {
2884 /* go to start of last line */
2885 if (mp_last == NULL)
2886 mp = msg_sb_start(last_msgchunk);
2887 else if (mp_last->sb_prev != NULL)
2888 mp = msg_sb_start(mp_last->sb_prev);
2889 else
2890 mp = NULL;
2891
2892 /* go to start of line at top of the screen */
2893 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2894 ++i)
2895 mp = msg_sb_start(mp->sb_prev);
2896
2897 if (mp != NULL && mp->sb_prev != NULL)
2898 {
2899 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002900 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002901 {
2902 if (mp == NULL || mp->sb_prev == NULL)
2903 break;
2904 mp = msg_sb_start(mp->sb_prev);
2905 if (mp_last == NULL)
2906 mp_last = msg_sb_start(last_msgchunk);
2907 else
2908 mp_last = msg_sb_start(mp_last->sb_prev);
2909 }
2910
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002911 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002912 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002914 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002915 (void)disp_sb_line(0, mp);
2916 }
2917 else
2918 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002919 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002920 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002921 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2922 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002923 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002924 ++msg_scrolled;
2925 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002926 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002927 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928 }
2929 }
2930 else
2931 {
2932 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002933 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002934 {
2935 /* scroll up, display line at bottom */
2936 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002937 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2939 (int)Columns, ' ', ' ', 0);
2940 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002941 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002942 }
2943 }
2944
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002945 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002946 {
2947 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002948 screen_fill((int)Rows - 1, (int)Rows, 0,
2949 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002950 msg_moremsg(FALSE);
2951 continue;
2952 }
2953
2954 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002955 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002956 }
2957
2958 break;
2959 }
2960
2961 /* clear the --more-- message */
2962 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2963 State = oldState;
2964#ifdef FEAT_MOUSE
2965 setmouse();
2966#endif
2967 if (quit_more)
2968 {
2969 msg_row = Rows - 1;
2970 msg_col = 0;
2971 }
2972#ifdef FEAT_RIGHTLEFT
2973 else if (cmdmsg_rl)
2974 msg_col = Columns - 1;
2975#endif
2976
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002977 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978#ifdef FEAT_CON_DIALOG
2979 return retval;
2980#else
2981 return FALSE;
2982#endif
2983}
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2986
2987#ifdef mch_errmsg
2988# undef mch_errmsg
2989#endif
2990#ifdef mch_msg
2991# undef mch_msg
2992#endif
2993
2994/*
2995 * Give an error message. To be used when the screen hasn't been initialized
2996 * yet. When stderr can't be used, collect error messages until the GUI has
2997 * started and they can be displayed in a message box.
2998 */
2999 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003000mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 int len;
3003
3004#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3005 /* On Unix use stderr if it's a tty.
3006 * When not going to start the GUI also use stderr.
3007 * On Mac, when started from Finder, stderr is the console. */
3008 if (
3009# ifdef UNIX
Bram Moolenaard0573012017-10-28 21:11:06 +02003010# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3012# else
3013 isatty(2)
3014# endif
3015# ifdef FEAT_GUI
3016 ||
3017# endif
3018# endif
3019# ifdef FEAT_GUI
3020 !(gui.in_use || gui.starting)
3021# endif
3022 )
3023 {
3024 fprintf(stderr, "%s", str);
3025 return;
3026 }
3027#endif
3028
3029 /* avoid a delay for a message that isn't there */
3030 emsg_on_display = FALSE;
3031
3032 len = (int)STRLEN(str) + 1;
3033 if (error_ga.ga_growsize == 0)
3034 {
3035 error_ga.ga_growsize = 80;
3036 error_ga.ga_itemsize = 1;
3037 }
3038 if (ga_grow(&error_ga, len) == OK)
3039 {
3040 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3041 (char_u *)str, len);
3042#ifdef UNIX
3043 /* remove CR characters, they are displayed */
3044 {
3045 char_u *p;
3046
3047 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3048 for (;;)
3049 {
3050 p = vim_strchr(p, '\r');
3051 if (p == NULL)
3052 break;
3053 *p = ' ';
3054 }
3055 }
3056#endif
3057 --len; /* don't count the NUL at the end */
3058 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 }
3060}
3061
3062/*
3063 * Give a message. To be used when the screen hasn't been initialized yet.
3064 * When there is no tty, collect messages until the GUI has started and they
3065 * can be displayed in a message box.
3066 */
3067 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003068mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3071 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3072 * uses mch_errmsg() when started from the desktop.
3073 * When not going to start the GUI also use stdout.
3074 * On Mac, when started from Finder, stderr is the console. */
3075 if (
3076# ifdef UNIX
Bram Moolenaard0573012017-10-28 21:11:06 +02003077# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3079# else
3080 isatty(2)
3081# endif
3082# ifdef FEAT_GUI
3083 ||
3084# endif
3085# endif
3086# ifdef FEAT_GUI
3087 !(gui.in_use || gui.starting)
3088# endif
3089 )
3090 {
3091 printf("%s", str);
3092 return;
3093 }
3094# endif
3095 mch_errmsg(str);
3096}
3097#endif /* USE_MCH_ERRMSG */
3098
3099/*
3100 * Put a character on the screen at the current message position and advance
3101 * to the next position. Only for printable ASCII!
3102 */
3103 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003104msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 msg_didout = TRUE; /* remember that line is not empty */
3107 screen_putchar(c, msg_row, msg_col, attr);
3108#ifdef FEAT_RIGHTLEFT
3109 if (cmdmsg_rl)
3110 {
3111 if (--msg_col == 0)
3112 {
3113 msg_col = Columns;
3114 ++msg_row;
3115 }
3116 }
3117 else
3118#endif
3119 {
3120 if (++msg_col >= Columns)
3121 {
3122 msg_col = 0;
3123 ++msg_row;
3124 }
3125 }
3126}
3127
3128 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003129msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003131 int attr;
3132 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
Bram Moolenaar8820b482017-03-16 17:23:31 +01003134 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003135 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003137 screen_puts((char_u *)
3138 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3139 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141
3142/*
3143 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3144 * exmode_active.
3145 */
3146 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003147repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 if (State == ASKMORE)
3150 {
3151 msg_moremsg(TRUE); /* display --more-- message again */
3152 msg_row = Rows - 1;
3153 }
3154#ifdef FEAT_CON_DIALOG
3155 else if (State == CONFIRM)
3156 {
3157 display_confirm_msg(); /* display ":confirm" message again */
3158 msg_row = Rows - 1;
3159 }
3160#endif
3161 else if (State == EXTERNCMD)
3162 {
3163 windgoto(msg_row, msg_col); /* put cursor back */
3164 }
3165 else if (State == HITRETURN || State == SETWSIZE)
3166 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003167 if (msg_row == Rows - 1)
3168 {
3169 /* Avoid drawing the "hit-enter" prompt below the previous one,
3170 * overwrite it. Esp. useful when regaining focus and a
3171 * FocusGained autocmd exists but didn't draw anything. */
3172 msg_didout = FALSE;
3173 msg_col = 0;
3174 msg_clr_eos();
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 hit_return_msg();
3177 msg_row = Rows - 1;
3178 }
3179}
3180
3181/*
3182 * msg_check_screen - check if the screen is initialized.
3183 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3184 * While starting the GUI the terminal codes will be set for the GUI, but the
3185 * output goes to the terminal. Don't use the terminal codes then.
3186 */
3187 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003188msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 if (!full_screen || !screen_valid(FALSE))
3191 return FALSE;
3192
3193 if (msg_row >= Rows)
3194 msg_row = Rows - 1;
3195 if (msg_col >= Columns)
3196 msg_col = Columns - 1;
3197 return TRUE;
3198}
3199
3200/*
3201 * Clear from current message position to end of screen.
3202 * Skip this when ":silent" was used, no need to clear for redirection.
3203 */
3204 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003205msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 if (msg_silent == 0)
3208 msg_clr_eos_force();
3209}
3210
3211/*
3212 * Clear from current message position to end of screen.
3213 * Note: msg_col is not updated, so we remember the end of the message
3214 * for msg_check().
3215 */
3216 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003217msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 if (msg_use_printf())
3220 {
3221 if (full_screen) /* only when termcap codes are valid */
3222 {
3223 if (*T_CD)
3224 out_str(T_CD); /* clear to end of display */
3225 else if (*T_CE)
3226 out_str(T_CE); /* clear to end of line */
3227 }
3228 }
3229 else
3230 {
3231#ifdef FEAT_RIGHTLEFT
3232 if (cmdmsg_rl)
3233 {
3234 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3235 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3236 }
3237 else
3238#endif
3239 {
3240 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3241 ' ', ' ', 0);
3242 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3243 }
3244 }
3245}
3246
3247/*
3248 * Clear the command line.
3249 */
3250 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003251msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
3253 msg_row = cmdline_row;
3254 msg_col = 0;
3255 msg_clr_eos_force();
3256}
3257
3258/*
3259 * end putting a message on the screen
3260 * call wait_return if the message does not fit in the available space
3261 * return TRUE if wait_return not called.
3262 */
3263 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003264msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003267 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * or the ruler option is set and we run into it,
3269 * we have to redraw the window.
3270 * Do not do this if we are abandoning the file or editing the command line.
3271 */
3272 if (!exiting && need_wait_return && !(State & CMDLINE))
3273 {
3274 wait_return(FALSE);
3275 return FALSE;
3276 }
3277 out_flush();
3278 return TRUE;
3279}
3280
3281/*
3282 * If the written message runs into the shown command or ruler, we have to
3283 * wait for hit-return and redraw the window later.
3284 */
3285 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003286msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 if (msg_row == Rows - 1 && msg_col >= sc_col)
3289 {
3290 need_wait_return = TRUE;
3291 redraw_cmdline = TRUE;
3292 }
3293}
3294
3295/*
3296 * May write a string to the redirection file.
3297 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3298 */
3299 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003300redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 char_u *s = str;
3303 static int cur_col = 0;
3304
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003305 /* Don't do anything for displaying prompts and the like. */
3306 if (redir_off)
3307 return;
3308
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003309 /* If 'verbosefile' is set prepare for writing in that file. */
3310 if (*p_vfile != NUL && verbose_fd == NULL)
3311 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003312
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003313 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
3315 /* If the string doesn't start with CR or NL, go to msg_col */
3316 if (*s != '\n' && *s != '\r')
3317 {
3318 while (cur_col < msg_col)
3319 {
3320#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003321 if (redir_execute)
3322 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003323 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003325 else if (redir_vname)
3326 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003329 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003331 if (verbose_fd != NULL)
3332 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 ++cur_col;
3334 }
3335 }
3336
3337#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003338 if (redir_execute)
3339 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003340 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003342 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003343 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#endif
3345
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003346 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3348 {
3349#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003350 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003352 if (redir_fd != NULL)
3353 putc(*s, redir_fd);
3354 if (verbose_fd != NULL)
3355 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (*s == '\r' || *s == '\n')
3357 cur_col = 0;
3358 else if (*s == '\t')
3359 cur_col += (8 - cur_col % 8);
3360 else
3361 ++cur_col;
3362 ++s;
3363 }
3364
3365 if (msg_silent != 0) /* should update msg_col */
3366 msg_col = cur_col;
3367 }
3368}
3369
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003370 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003371redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003372{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003373 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003374#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003375 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003376#endif
3377 ;
3378}
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003381 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003382 * Must always be called paired with verbose_leave()!
3383 */
3384 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003385verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003386{
3387 if (*p_vfile != NUL)
3388 ++msg_silent;
3389}
3390
3391/*
3392 * After giving verbose message.
3393 * Must always be called paired with verbose_enter()!
3394 */
3395 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003396verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003397{
3398 if (*p_vfile != NUL)
3399 if (--msg_silent < 0)
3400 msg_silent = 0;
3401}
3402
3403/*
3404 * Like verbose_enter() and set msg_scroll when displaying the message.
3405 */
3406 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003407verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003408{
3409 if (*p_vfile != NUL)
3410 ++msg_silent;
3411 else
3412 /* always scroll up, don't overwrite */
3413 msg_scroll = TRUE;
3414}
3415
3416/*
3417 * Like verbose_leave() and set cmdline_row when displaying the message.
3418 */
3419 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003420verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003421{
3422 if (*p_vfile != NUL)
3423 {
3424 if (--msg_silent < 0)
3425 msg_silent = 0;
3426 }
3427 else
3428 cmdline_row = msg_row;
3429}
3430
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003431/*
3432 * Called when 'verbosefile' is set: stop writing to the file.
3433 */
3434 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003435verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003436{
3437 if (verbose_fd != NULL)
3438 {
3439 fclose(verbose_fd);
3440 verbose_fd = NULL;
3441 }
3442 verbose_did_open = FALSE;
3443}
3444
3445/*
3446 * Open the file 'verbosefile'.
3447 * Return FAIL or OK.
3448 */
3449 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003450verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003451{
3452 if (verbose_fd == NULL && !verbose_did_open)
3453 {
3454 /* Only give the error message once. */
3455 verbose_did_open = TRUE;
3456
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003457 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003458 if (verbose_fd == NULL)
3459 {
3460 EMSG2(_(e_notopen), p_vfile);
3461 return FAIL;
3462 }
3463 }
3464 return OK;
3465}
3466
3467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 * Give a warning message (for searching).
3469 * Use 'w' highlighting and may repeat the message after redrawing
3470 */
3471 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003472give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 /* Don't do this for ":silent". */
3475 if (msg_silent != 0)
3476 return;
3477
3478 /* Don't want a hit-enter prompt here. */
3479 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#ifdef FEAT_EVAL
3482 set_vim_var_string(VV_WARNINGMSG, message, -1);
3483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 vim_free(keep_msg);
3485 keep_msg = NULL;
3486 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003487 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else
3489 keep_msg_attr = 0;
3490 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003491 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 msg_didout = FALSE; /* overwrite this message */
3493 msg_nowait = TRUE; /* don't wait for this message */
3494 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 --no_wait_return;
3497}
3498
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003499 void
3500give_warning2(char_u *message, char_u *a1, int hl)
3501{
3502 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3503 give_warning(IObuff, hl);
3504}
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506/*
3507 * Advance msg cursor to column "col".
3508 */
3509 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003510msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
3512 if (msg_silent != 0) /* nothing to advance to */
3513 {
3514 msg_col = col; /* for redirection, may fill it up later */
3515 return;
3516 }
3517 if (col >= Columns) /* not enough room */
3518 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003519#ifdef FEAT_RIGHTLEFT
3520 if (cmdmsg_rl)
3521 while (msg_col > Columns - col)
3522 msg_putchar(' ');
3523 else
3524#endif
3525 while (msg_col < col)
3526 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527}
3528
3529#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3530/*
3531 * Used for "confirm()" function, and the :confirm command prefix.
3532 * Versions which haven't got flexible dialogs yet, and console
3533 * versions, get this generic handler which uses the command line.
3534 *
3535 * type = one of:
3536 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3537 * title = title string (can be NULL for default)
3538 * (neither used in console dialogs at the moment)
3539 *
3540 * Format of the "buttons" string:
3541 * "Button1Name\nButton2Name\nButton3Name"
3542 * The first button should normally be the default/accept
3543 * The second button should be the 'Cancel' button
3544 * Other buttons- use your imagination!
3545 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3546 * different letter.
3547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003549do_dialog(
3550 int type UNUSED,
3551 char_u *title UNUSED,
3552 char_u *message,
3553 char_u *buttons,
3554 int dfltbutton,
3555 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003556 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003557 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003558 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560 int oldState;
3561 int retval = 0;
3562 char_u *hotkeys;
3563 int c;
3564 int i;
3565
3566#ifndef NO_CONSOLE
3567 /* Don't output anything in silent mode ("ex -s") */
3568 if (silent_mode)
3569 return dfltbutton; /* return default option */
3570#endif
3571
3572#ifdef FEAT_GUI_DIALOG
3573 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3574 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3575 {
3576 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003577 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003578 /* avoid a hit-enter prompt without clearing the cmdline */
3579 need_wait_return = FALSE;
3580 emsg_on_display = FALSE;
3581 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /* Flush output to avoid that further messages and redrawing is done
3584 * in the wrong order. */
3585 out_flush();
3586 gui_mch_update();
3587
3588 return c;
3589 }
3590#endif
3591
3592 oldState = State;
3593 State = CONFIRM;
3594#ifdef FEAT_MOUSE
3595 setmouse();
3596#endif
3597
3598 /*
3599 * Since we wait for a keypress, don't make the
3600 * user press RETURN as well afterwards.
3601 */
3602 ++no_wait_return;
3603 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3604
3605 if (hotkeys != NULL)
3606 {
3607 for (;;)
3608 {
3609 /* Get a typed character directly from the user. */
3610 c = get_keystroke();
3611 switch (c)
3612 {
3613 case CAR: /* User accepts default option */
3614 case NL:
3615 retval = dfltbutton;
3616 break;
3617 case Ctrl_C: /* User aborts/cancels */
3618 case ESC:
3619 retval = 0;
3620 break;
3621 default: /* Could be a hotkey? */
3622 if (c < 0) /* special keys are ignored here */
3623 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003624 if (c == ':' && ex_cmd)
3625 {
3626 retval = dfltbutton;
3627 ins_char_typebuf(':');
3628 break;
3629 }
3630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 /* Make the character lowercase, as chars in "hotkeys" are. */
3632 c = MB_TOLOWER(c);
3633 retval = 1;
3634 for (i = 0; hotkeys[i]; ++i)
3635 {
3636#ifdef FEAT_MBYTE
3637 if (has_mbyte)
3638 {
3639 if ((*mb_ptr2char)(hotkeys + i) == c)
3640 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003641 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643 else
3644#endif
3645 if (hotkeys[i] == c)
3646 break;
3647 ++retval;
3648 }
3649 if (hotkeys[i])
3650 break;
3651 /* No hotkey match, so keep waiting */
3652 continue;
3653 }
3654 break;
3655 }
3656
3657 vim_free(hotkeys);
3658 }
3659
3660 State = oldState;
3661#ifdef FEAT_MOUSE
3662 setmouse();
3663#endif
3664 --no_wait_return;
3665 msg_end_prompt();
3666
3667 return retval;
3668}
3669
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003670static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
3672/*
3673 * Copy one character from "*from" to "*to", taking care of multi-byte
3674 * characters. Return the length of the character in bytes.
3675 */
3676 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003677copy_char(
3678 char_u *from,
3679 char_u *to,
3680 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682#ifdef FEAT_MBYTE
3683 int len;
3684 int c;
3685
3686 if (has_mbyte)
3687 {
3688 if (lowercase)
3689 {
3690 c = MB_TOLOWER((*mb_ptr2char)(from));
3691 return (*mb_char2bytes)(c, to);
3692 }
3693 else
3694 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003695 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 mch_memmove(to, from, (size_t)len);
3697 return len;
3698 }
3699 }
3700 else
3701#endif
3702 {
3703 if (lowercase)
3704 *to = (char_u)TOLOWER_LOC(*from);
3705 else
3706 *to = *from;
3707 return 1;
3708 }
3709}
3710
3711/*
3712 * Format the dialog string, and display it at the bottom of
3713 * the screen. Return a string of hotkey chars (if defined) for
3714 * each 'button'. If a button has no hotkey defined, the first character of
3715 * the button is used.
3716 * The hotkeys can be multi-byte characters, but without combining chars.
3717 *
3718 * Returns an allocated string with hotkeys, or NULL for error.
3719 */
3720 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003721msg_show_console_dialog(
3722 char_u *message,
3723 char_u *buttons,
3724 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 int len = 0;
3727#ifdef FEAT_MBYTE
3728# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3729#else
3730# define HOTK_LEN 1
3731#endif
3732 int lenhotkey = HOTK_LEN; /* count first button */
3733 char_u *hotk = NULL;
3734 char_u *msgp = NULL;
3735 char_u *hotkp = NULL;
3736 char_u *r;
3737 int copy;
3738#define HAS_HOTKEY_LEN 30
3739 char_u has_hotkey[HAS_HOTKEY_LEN];
3740 int first_hotkey = FALSE; /* first char of button is hotkey */
3741 int idx;
3742
3743 has_hotkey[0] = FALSE;
3744
3745 /*
3746 * First loop: compute the size of memory to allocate.
3747 * Second loop: copy to the allocated memory.
3748 */
3749 for (copy = 0; copy <= 1; ++copy)
3750 {
3751 r = buttons;
3752 idx = 0;
3753 while (*r)
3754 {
3755 if (*r == DLG_BUTTON_SEP)
3756 {
3757 if (copy)
3758 {
3759 *msgp++ = ',';
3760 *msgp++ = ' '; /* '\n' -> ', ' */
3761
3762 /* advance to next hotkey and set default hotkey */
3763#ifdef FEAT_MBYTE
3764 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003765 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 else
3767#endif
3768 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003769 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (dfltbutton)
3771 --dfltbutton;
3772
3773 /* If no hotkey is specified first char is used. */
3774 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3775 first_hotkey = TRUE;
3776 }
3777 else
3778 {
3779 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3780 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3781 if (idx < HAS_HOTKEY_LEN - 1)
3782 has_hotkey[++idx] = FALSE;
3783 }
3784 }
3785 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3786 {
3787 if (*r == DLG_HOTKEY_CHAR)
3788 ++r;
3789 first_hotkey = FALSE;
3790 if (copy)
3791 {
3792 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3793 *msgp++ = *r;
3794 else
3795 {
3796 /* '&a' -> '[a]' */
3797 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3798 msgp += copy_char(r, msgp, FALSE);
3799 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3800
3801 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003802 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 }
3805 else
3806 {
3807 ++len; /* '&a' -> '[a]' */
3808 if (idx < HAS_HOTKEY_LEN - 1)
3809 has_hotkey[idx] = TRUE;
3810 }
3811 }
3812 else
3813 {
3814 /* everything else copy literally */
3815 if (copy)
3816 msgp += copy_char(r, msgp, FALSE);
3817 }
3818
3819 /* advance to the next character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003820 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822
3823 if (copy)
3824 {
3825 *msgp++ = ':';
3826 *msgp++ = ' ';
3827 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829 else
3830 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003831 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003832 + 2 /* for the NL's */
3833 + STRLEN(buttons)
3834 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003835 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 /* If no hotkey is specified first char is used. */
3838 if (!has_hotkey[0])
3839 {
3840 first_hotkey = TRUE;
3841 len += 2; /* "x" -> "[x]" */
3842 }
3843
3844 /*
3845 * Now allocate and load the strings
3846 */
3847 vim_free(confirm_msg);
3848 confirm_msg = alloc(len);
3849 if (confirm_msg == NULL)
3850 return NULL;
3851 *confirm_msg = NUL;
3852 hotk = alloc(lenhotkey);
3853 if (hotk == NULL)
3854 return NULL;
3855
3856 *confirm_msg = '\n';
3857 STRCPY(confirm_msg + 1, message);
3858
3859 msgp = confirm_msg + 1 + STRLEN(message);
3860 hotkp = hotk;
3861
Bram Moolenaar6a516062007-07-05 08:11:42 +00003862 /* Define first default hotkey. Keep the hotkey string NUL
3863 * terminated to avoid reading past the end. */
3864 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 /* Remember where the choices start, displaying starts here when
3867 * "hotkp" typed at the more prompt. */
3868 confirm_msg_tail = msgp;
3869 *msgp++ = '\n';
3870 }
3871 }
3872
3873 display_confirm_msg();
3874 return hotk;
3875}
3876
3877/*
3878 * Display the ":confirm" message. Also called when screen resized.
3879 */
3880 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003881display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
3883 /* avoid that 'q' at the more prompt truncates the message here */
3884 ++confirm_msg_used;
3885 if (confirm_msg != NULL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003886 msg_puts_attr(confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 --confirm_msg_used;
3888}
3889
3890#endif /* FEAT_CON_DIALOG */
3891
3892#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3893
3894 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003895vim_dialog_yesno(
3896 int type,
3897 char_u *title,
3898 char_u *message,
3899 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 if (do_dialog(type,
3902 title == NULL ? (char_u *)_("Question") : title,
3903 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003904 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 return VIM_YES;
3906 return VIM_NO;
3907}
3908
3909 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003910vim_dialog_yesnocancel(
3911 int type,
3912 char_u *title,
3913 char_u *message,
3914 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 switch (do_dialog(type,
3917 title == NULL ? (char_u *)_("Question") : title,
3918 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003919 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
3921 case 1: return VIM_YES;
3922 case 2: return VIM_NO;
3923 }
3924 return VIM_CANCEL;
3925}
3926
3927 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003928vim_dialog_yesnoallcancel(
3929 int type,
3930 char_u *title,
3931 char_u *message,
3932 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 switch (do_dialog(type,
3935 title == NULL ? (char_u *)"Question" : title,
3936 message,
3937 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003938 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
3940 case 1: return VIM_YES;
3941 case 2: return VIM_NO;
3942 case 3: return VIM_ALL;
3943 case 4: return VIM_DISCARDALL;
3944 }
3945 return VIM_CANCEL;
3946}
3947
3948#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3949
3950#if defined(FEAT_BROWSE) || defined(PROTO)
3951/*
3952 * Generic browse function. Calls gui_mch_browse() when possible.
3953 * Later this may pop-up a non-GUI file selector (external command?).
3954 */
3955 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003956do_browse(
3957 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3958 char_u *title, /* title for the window */
3959 char_u *dflt, /* default file name (may include directory) */
3960 char_u *ext, /* extension added */
3961 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003963 char_u *filter, /* file name filter */
3964 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
3966 char_u *fname;
3967 static char_u *last_dir = NULL; /* last used directory */
3968 char_u *tofree = NULL;
3969 int save_browse = cmdmod.browse;
3970
3971 /* Must turn off browse to avoid that autocommands will get the
3972 * flag too! */
3973 cmdmod.browse = FALSE;
3974
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003975 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003977 if (flags & BROWSE_DIR)
3978 title = (char_u *)_("Select Directory dialog");
3979 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 title = (char_u *)_("Save File dialog");
3981 else
3982 title = (char_u *)_("Open File dialog");
3983 }
3984
3985 /* When no directory specified, use default file name, default dir, buffer
3986 * dir, last dir or current dir */
3987 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3988 {
3989 if (mch_isdir(dflt)) /* default file name is a directory */
3990 {
3991 initdir = dflt;
3992 dflt = NULL;
3993 }
3994 else if (gettail(dflt) != dflt) /* default file name includes a path */
3995 {
3996 tofree = vim_strsave(dflt);
3997 if (tofree != NULL)
3998 {
3999 initdir = tofree;
4000 *gettail(initdir) = NUL;
4001 dflt = gettail(dflt);
4002 }
4003 }
4004 }
4005
4006 if (initdir == NULL || *initdir == NUL)
4007 {
4008 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004009 if (STRCMP(p_bsdir, "last") != 0
4010 && STRCMP(p_bsdir, "buffer") != 0
4011 && STRCMP(p_bsdir, "current") != 0
4012 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 initdir = p_bsdir;
4014 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004015 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 && buf != NULL && buf->b_ffname != NULL)
4017 {
4018 if (dflt == NULL || *dflt == NUL)
4019 dflt = gettail(curbuf->b_ffname);
4020 tofree = vim_strsave(curbuf->b_ffname);
4021 if (tofree != NULL)
4022 {
4023 initdir = tofree;
4024 *gettail(initdir) = NUL;
4025 }
4026 }
4027 /* When 'browsedir' is "last", use dir from last browse */
4028 else if (*p_bsdir == 'l')
4029 initdir = last_dir;
4030 /* When 'browsedir is "current", use current directory. This is the
4031 * default already, leave initdir empty. */
4032 }
4033
4034# ifdef FEAT_GUI
4035 if (gui.in_use) /* when this changes, also adjust f_has()! */
4036 {
4037 if (filter == NULL
4038# ifdef FEAT_EVAL
4039 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
4040 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
4041# endif
4042 )
4043 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004044 if (flags & BROWSE_DIR)
4045 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004046# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004047 /* For systems that have a directory dialog. */
4048 fname = gui_mch_browsedir(title, initdir);
4049# else
4050 /* Generic solution for selecting a directory: select a file and
4051 * remove the file name. */
4052 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
4053# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004054# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004055 /* Win32 adds a dummy file name, others return an arbitrary file
4056 * name. GTK+ 2 returns only the directory, */
4057 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
4058 {
4059 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004060 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004061
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004062 if (tail == fname)
4063 *tail++ = '.'; /* use current dir */
4064 *tail = NUL;
4065 }
4066# endif
4067 }
4068 else
4069 fname = gui_mch_browse(flags & BROWSE_SAVE,
4070 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 /* We hang around in the dialog for a while, the user might do some
4073 * things to our files. The Win32 dialog allows deleting or renaming
4074 * a file, check timestamps. */
4075 need_check_timestamps = TRUE;
4076 did_check_timestamps = FALSE;
4077 }
4078 else
4079# endif
4080 {
4081 /* TODO: non-GUI file selector here */
4082 EMSG(_("E338: Sorry, no file browser in console mode"));
4083 fname = NULL;
4084 }
4085
4086 /* keep the directory for next time */
4087 if (fname != NULL)
4088 {
4089 vim_free(last_dir);
4090 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004091 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 *gettail(last_dir) = NUL;
4094 if (*last_dir == NUL)
4095 {
4096 /* filename only returned, must be in current dir */
4097 vim_free(last_dir);
4098 last_dir = alloc(MAXPATHL);
4099 if (last_dir != NULL)
4100 mch_dirname(last_dir, MAXPATHL);
4101 }
4102 }
4103 }
4104
4105 vim_free(tofree);
4106 cmdmod.browse = save_browse;
4107
4108 return fname;
4109}
4110#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004111
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004112#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004113static char *e_printf = N_("E766: Insufficient arguments for printf()");
4114
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004115static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004116static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004117# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004118static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004119# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120
4121/*
4122 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4123 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004124 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004125tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004126{
4127 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004128 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129 int err = FALSE;
4130
4131 if (tvs[idx].v_type == VAR_UNKNOWN)
4132 EMSG(_(e_printf));
4133 else
4134 {
4135 ++*idxp;
4136 n = get_tv_number_chk(&tvs[idx], &err);
4137 if (err)
4138 n = 0;
4139 }
4140 return n;
4141}
4142
4143/*
4144 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004145 * If "tofree" is NULL get_tv_string_chk() is used. Some types (e.g. List)
4146 * are not converted to a string.
4147 * If "tofree" is not NULL echo_string() is used. All types are converted to
4148 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004149 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 */
4151 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004152tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004153{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004154 int idx = *idxp - 1;
4155 char *s = NULL;
4156 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157
4158 if (tvs[idx].v_type == VAR_UNKNOWN)
4159 EMSG(_(e_printf));
4160 else
4161 {
4162 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004163 if (tofree != NULL)
4164 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4165 else
4166 s = (char *)get_tv_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004167 }
4168 return s;
4169}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004170
4171# ifdef FEAT_FLOAT
4172/*
4173 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4174 */
4175 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004176tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004177{
4178 int idx = *idxp - 1;
4179 double f = 0;
4180
4181 if (tvs[idx].v_type == VAR_UNKNOWN)
4182 EMSG(_(e_printf));
4183 else
4184 {
4185 ++*idxp;
4186 if (tvs[idx].v_type == VAR_FLOAT)
4187 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004188 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004189 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004190 else
4191 EMSG(_("E807: Expected Float argument for printf()"));
4192 }
4193 return f;
4194}
4195# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196#endif
4197
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004198#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004199/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004200 * Return the representation of infinity for printf() function:
4201 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4202 */
4203 static const char *
4204infinity_str(int positive,
4205 char fmt_spec,
4206 int force_sign,
4207 int space_for_positive)
4208{
4209 static const char *table[] =
4210 {
4211 "-inf", "inf", "+inf", " inf",
4212 "-INF", "INF", "+INF", " INF"
4213 };
4214 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4215
4216 if (ASCII_ISUPPER(fmt_spec))
4217 idx += 4;
4218 return table[idx];
4219}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004220#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004221
4222/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004224 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004225 * consistency.
4226 *
4227 * This code is based on snprintf.c - a portable implementation of snprintf
4228 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004229 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230 * The original code, including useful comments, can be found here:
4231 * http://www.ijs.si/software/snprintf/
4232 *
4233 * This snprintf() only supports the following conversion specifiers:
4234 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4235 * with flags: '-', '+', ' ', '0' and '#'.
4236 * An asterisk is supported for field width as well as precision.
4237 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004238 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004239 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004240 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4241 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004242 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004243 * The locale is not used, the string is used as a byte string. This is only
4244 * relevant for double-byte encodings where the second byte may be '%'.
4245 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004246 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4247 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004248 *
4249 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004250 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004251 * is greater or equal to "str_m", not all characters from the result
4252 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4253 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004254 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004255 */
4256
4257/*
4258 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004260 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004261 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004262 */
4263
4264/* When generating prototypes all of this is skipped, cproto doesn't
4265 * understand this. */
4266#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004267
Bram Moolenaara800b422010-06-27 01:15:55 +02004268/* Like vim_vsnprintf() but append to the string. */
4269 int
4270vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4271{
4272 va_list ap;
4273 int str_l;
4274 size_t len = STRLEN(str);
4275 size_t space;
4276
4277 if (str_m <= len)
4278 space = 0;
4279 else
4280 space = str_m - len;
4281 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004282 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004283 va_end(ap);
4284 return str_l;
4285}
Bram Moolenaara800b422010-06-27 01:15:55 +02004286
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004287 int
4288vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4289{
4290 va_list ap;
4291 int str_l;
4292
4293 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004294 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004295 va_end(ap);
4296 return str_l;
4297}
4298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004300vim_vsnprintf(
4301 char *str,
4302 size_t str_m,
4303 char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004304 va_list ap)
4305{
4306 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4307}
4308
4309 int
4310vim_vsnprintf_typval(
4311 char *str,
4312 size_t str_m,
4313 char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004314 va_list ap,
4315 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004316{
4317 size_t str_l = 0;
4318 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004319 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004320
4321 if (p == NULL)
4322 p = "";
4323 while (*p != NUL)
4324 {
4325 if (*p != '%')
4326 {
4327 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004328 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004329
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004330 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004331 if (str_l < str_m)
4332 {
4333 size_t avail = str_m - str_l;
4334
4335 mch_memmove(str + str_l, p, n > avail ? avail : n);
4336 }
4337 p += n;
4338 str_l += n;
4339 }
4340 else
4341 {
4342 size_t min_field_width = 0, precision = 0;
4343 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4344 int alternate_form = 0, force_sign = 0;
4345
4346 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4347 * ignored. */
4348 int space_for_positive = 1;
4349
4350 /* allowed values: \0, h, l, L */
4351 char length_modifier = '\0';
4352
4353 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004354# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004355# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004356 * That sounds reasonable to use as the maximum
4357 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004358# elif defined(FEAT_NUM64)
4359# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004360# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004361# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004362# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004363 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004364
4365 /* string address in case of string argument */
4366 char *str_arg;
4367
4368 /* natural field width of arg without padding and sign */
4369 size_t str_arg_l;
4370
4371 /* unsigned char argument value - only defined for c conversion.
4372 * N.B. standard explicitly states the char argument for the c
4373 * conversion is unsigned */
4374 unsigned char uchar_arg;
4375
4376 /* number of zeros to be inserted for numeric conversions as
4377 * required by the precision or minimal field width */
4378 size_t number_of_zeros_to_pad = 0;
4379
4380 /* index into tmp where zero padding is to be inserted */
4381 size_t zero_padding_insertion_ind = 0;
4382
4383 /* current conversion specifier character */
4384 char fmt_spec = '\0';
4385
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004386 /* buffer for 's' and 'S' specs */
4387 char_u *tofree = NULL;
4388
4389
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004390 str_arg = NULL;
4391 p++; /* skip '%' */
4392
4393 /* parse flags */
4394 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4395 || *p == '#' || *p == '\'')
4396 {
4397 switch (*p)
4398 {
4399 case '0': zero_padding = 1; break;
4400 case '-': justify_left = 1; break;
4401 case '+': force_sign = 1; space_for_positive = 0; break;
4402 case ' ': force_sign = 1;
4403 /* If both the ' ' and '+' flags appear, the ' '
4404 * flag should be ignored */
4405 break;
4406 case '#': alternate_form = 1; break;
4407 case '\'': break;
4408 }
4409 p++;
4410 }
4411 /* If the '0' and '-' flags both appear, the '0' flag should be
4412 * ignored. */
4413
4414 /* parse field width */
4415 if (*p == '*')
4416 {
4417 int j;
4418
4419 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004422 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423# endif
4424 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004425 if (j >= 0)
4426 min_field_width = j;
4427 else
4428 {
4429 min_field_width = -j;
4430 justify_left = 1;
4431 }
4432 }
4433 else if (VIM_ISDIGIT((int)(*p)))
4434 {
4435 /* size_t could be wider than unsigned int; make sure we treat
4436 * argument like common implementations do */
4437 unsigned int uj = *p++ - '0';
4438
4439 while (VIM_ISDIGIT((int)(*p)))
4440 uj = 10 * uj + (unsigned int)(*p++ - '0');
4441 min_field_width = uj;
4442 }
4443
4444 /* parse precision */
4445 if (*p == '.')
4446 {
4447 p++;
4448 precision_specified = 1;
4449 if (*p == '*')
4450 {
4451 int j;
4452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004455 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456# endif
4457 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004458 p++;
4459 if (j >= 0)
4460 precision = j;
4461 else
4462 {
4463 precision_specified = 0;
4464 precision = 0;
4465 }
4466 }
4467 else if (VIM_ISDIGIT((int)(*p)))
4468 {
4469 /* size_t could be wider than unsigned int; make sure we
4470 * treat argument like common implementations do */
4471 unsigned int uj = *p++ - '0';
4472
4473 while (VIM_ISDIGIT((int)(*p)))
4474 uj = 10 * uj + (unsigned int)(*p++ - '0');
4475 precision = uj;
4476 }
4477 }
4478
4479 /* parse 'h', 'l' and 'll' length modifiers */
4480 if (*p == 'h' || *p == 'l')
4481 {
4482 length_modifier = *p;
4483 p++;
4484 if (length_modifier == 'l' && *p == 'l')
4485 {
4486 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004487# ifdef FEAT_NUM64
4488 length_modifier = 'L';
4489# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004490 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004491# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492 p++;
4493 }
4494 }
4495 fmt_spec = *p;
4496
4497 /* common synonyms: */
4498 switch (fmt_spec)
4499 {
4500 case 'i': fmt_spec = 'd'; break;
4501 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4502 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4503 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4504 default: break;
4505 }
4506
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004507# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4508 switch (fmt_spec)
4509 {
4510 case 'd': case 'u': case 'o': case 'x': case 'X':
4511 if (tvs != NULL && length_modifier == '\0')
4512 length_modifier = 'L';
4513 }
4514# endif
4515
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004516 /* get parameter value, do initial processing */
4517 switch (fmt_spec)
4518 {
4519 /* '%' and 'c' behave similar to 's' regarding flags and field
4520 * widths */
4521 case '%':
4522 case 'c':
4523 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004524 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004525 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004526 str_arg_l = 1;
4527 switch (fmt_spec)
4528 {
4529 case '%':
4530 str_arg = p;
4531 break;
4532
4533 case 'c':
4534 {
4535 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536
4537 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004539 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540# endif
4541 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004542 /* standard demands unsigned char */
4543 uchar_arg = (unsigned char)j;
4544 str_arg = (char *)&uchar_arg;
4545 break;
4546 }
4547
4548 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004549 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004552 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553# endif
4554 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004555 if (str_arg == NULL)
4556 {
4557 str_arg = "[NULL]";
4558 str_arg_l = 6;
4559 }
4560 /* make sure not to address string beyond the specified
4561 * precision !!! */
4562 else if (!precision_specified)
4563 str_arg_l = strlen(str_arg);
4564 /* truncate string if necessary as requested by precision */
4565 else if (precision == 0)
4566 str_arg_l = 0;
4567 else
4568 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004569 /* Don't put the #if inside memchr(), it can be a
4570 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004571# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004572 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004573# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004574 /* memchr on HP does not like n > 2^31 !!! */
4575 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004576 precision <= (size_t)0x7fffffffL ? precision
4577 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004578# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004579 str_arg_l = (q == NULL) ? precision
4580 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004581 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004582# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004583 if (fmt_spec == 'S')
4584 {
4585 if (min_field_width != 0)
4586 min_field_width += STRLEN(str_arg)
4587 - mb_string2cells((char_u *)str_arg, -1);
4588 if (precision)
4589 {
4590 char_u *p1 = (char_u *)str_arg;
4591 size_t i;
4592
4593 for (i = 0; i < precision && *p1; i++)
4594 p1 += mb_ptr2len(p1);
4595
4596 str_arg_l = precision = p1 - (char_u *)str_arg;
4597 }
4598 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004599# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004600 break;
4601
4602 default:
4603 break;
4604 }
4605 break;
4606
Bram Moolenaar91984b92016-08-16 21:58:41 +02004607 case 'd': case 'u':
4608 case 'b': case 'B':
4609 case 'o':
4610 case 'x': case 'X':
4611 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004612 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004613 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004614 * imply the value is unsigned; d implies a signed
4615 * value */
4616
4617 /* 0 if numeric argument is zero (or if pointer is
4618 * NULL for 'p'), +1 if greater than zero (or nonzero
4619 * for unsigned arguments), -1 if negative (unsigned
4620 * argument is never negative) */
4621 int arg_sign = 0;
4622
4623 /* only defined for length modifier h, or for no
4624 * length modifiers */
4625 int int_arg = 0;
4626 unsigned int uint_arg = 0;
4627
4628 /* only defined for length modifier l */
4629 long int long_arg = 0;
4630 unsigned long int ulong_arg = 0;
4631
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004632# ifdef FEAT_NUM64
4633 /* only defined for length modifier ll */
4634 varnumber_T llong_arg = 0;
4635 uvarnumber_T ullong_arg = 0;
4636# endif
4637
Bram Moolenaare9997822016-08-28 16:03:38 +02004638 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004639 uvarnumber_T bin_arg = 0;
4640
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004641 /* pointer argument value -only defined for p
4642 * conversion */
4643 void *ptr_arg = NULL;
4644
4645 if (fmt_spec == 'p')
4646 {
4647 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004650 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4651 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652# endif
4653 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004654 if (ptr_arg != NULL)
4655 arg_sign = 1;
4656 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004657 else if (fmt_spec == 'b' || fmt_spec == 'B')
4658 {
4659 bin_arg =
4660# if defined(FEAT_EVAL)
4661 tvs != NULL ?
4662 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4663# endif
4664 va_arg(ap, uvarnumber_T);
4665 if (bin_arg != 0)
4666 arg_sign = 1;
4667 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004668 else if (fmt_spec == 'd')
4669 {
4670 /* signed */
4671 switch (length_modifier)
4672 {
4673 case '\0':
4674 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004675 /* char and short arguments are passed as int. */
4676 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004677# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004678 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679# endif
4680 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004681 if (int_arg > 0)
4682 arg_sign = 1;
4683 else if (int_arg < 0)
4684 arg_sign = -1;
4685 break;
4686 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004688# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004689 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690# endif
4691 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004692 if (long_arg > 0)
4693 arg_sign = 1;
4694 else if (long_arg < 0)
4695 arg_sign = -1;
4696 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004697# ifdef FEAT_NUM64
4698 case 'L':
4699 llong_arg =
4700# if defined(FEAT_EVAL)
4701 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4702# endif
4703 va_arg(ap, varnumber_T);
4704 if (llong_arg > 0)
4705 arg_sign = 1;
4706 else if (llong_arg < 0)
4707 arg_sign = -1;
4708 break;
4709# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004710 }
4711 }
4712 else
4713 {
4714 /* unsigned */
4715 switch (length_modifier)
4716 {
4717 case '\0':
4718 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004719 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004720# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004721 tvs != NULL ? (unsigned)
4722 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004723# endif
4724 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004725 if (uint_arg != 0)
4726 arg_sign = 1;
4727 break;
4728 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004729 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004730# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004731 tvs != NULL ? (unsigned long)
4732 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004733# endif
4734 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004735 if (ulong_arg != 0)
4736 arg_sign = 1;
4737 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004738# ifdef FEAT_NUM64
4739 case 'L':
4740 ullong_arg =
4741# if defined(FEAT_EVAL)
4742 tvs != NULL ? (uvarnumber_T)
4743 tv_nr(tvs, &arg_idx) :
4744# endif
4745 va_arg(ap, uvarnumber_T);
4746 if (ullong_arg != 0)
4747 arg_sign = 1;
4748 break;
4749# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004750 }
4751 }
4752
4753 str_arg = tmp;
4754 str_arg_l = 0;
4755
4756 /* NOTE:
4757 * For d, i, u, o, x, and X conversions, if precision is
4758 * specified, the '0' flag should be ignored. This is so
4759 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4760 * FreeBSD, NetBSD; but not with Perl.
4761 */
4762 if (precision_specified)
4763 zero_padding = 0;
4764 if (fmt_spec == 'd')
4765 {
4766 if (force_sign && arg_sign >= 0)
4767 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4768 /* leave negative numbers for sprintf to handle, to
4769 * avoid handling tricky cases like (short int)-32768 */
4770 }
4771 else if (alternate_form)
4772 {
4773 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004774 && (fmt_spec == 'b' || fmt_spec == 'B'
4775 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004776 {
4777 tmp[str_arg_l++] = '0';
4778 tmp[str_arg_l++] = fmt_spec;
4779 }
4780 /* alternate form should have no effect for p
4781 * conversion, but ... */
4782 }
4783
4784 zero_padding_insertion_ind = str_arg_l;
4785 if (!precision_specified)
4786 precision = 1; /* default precision is 1 */
4787 if (precision == 0 && arg_sign == 0)
4788 {
4789 /* When zero value is formatted with an explicit
4790 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004791 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004792 }
4793 else
4794 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004795 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004796 int f_l = 0;
4797
4798 /* construct a simple format string for sprintf */
4799 f[f_l++] = '%';
4800 if (!length_modifier)
4801 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004802 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004804# ifdef FEAT_NUM64
4805# ifdef WIN3264
4806 f[f_l++] = 'I';
4807 f[f_l++] = '6';
4808 f[f_l++] = '4';
4809# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004810 f[f_l++] = 'l';
4811 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004812# endif
4813# else
4814 f[f_l++] = 'l';
4815# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004816 }
4817 else
4818 f[f_l++] = length_modifier;
4819 f[f_l++] = fmt_spec;
4820 f[f_l++] = '\0';
4821
4822 if (fmt_spec == 'p')
4823 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004824 else if (fmt_spec == 'b' || fmt_spec == 'B')
4825 {
4826 char b[8 * sizeof(uvarnumber_T)];
4827 size_t b_l = 0;
4828 uvarnumber_T bn = bin_arg;
4829
4830 do
4831 {
4832 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4833 bn >>= 1;
4834 }
4835 while (bn != 0);
4836
4837 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4838 str_arg_l += b_l;
4839 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004840 else if (fmt_spec == 'd')
4841 {
4842 /* signed */
4843 switch (length_modifier)
4844 {
4845 case '\0':
4846 case 'h': str_arg_l += sprintf(
4847 tmp + str_arg_l, f, int_arg);
4848 break;
4849 case 'l': str_arg_l += sprintf(
4850 tmp + str_arg_l, f, long_arg);
4851 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004852# ifdef FEAT_NUM64
4853 case 'L': str_arg_l += sprintf(
4854 tmp + str_arg_l, f, llong_arg);
4855 break;
4856# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004857 }
4858 }
4859 else
4860 {
4861 /* unsigned */
4862 switch (length_modifier)
4863 {
4864 case '\0':
4865 case 'h': str_arg_l += sprintf(
4866 tmp + str_arg_l, f, uint_arg);
4867 break;
4868 case 'l': str_arg_l += sprintf(
4869 tmp + str_arg_l, f, ulong_arg);
4870 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004871# ifdef FEAT_NUM64
4872 case 'L': str_arg_l += sprintf(
4873 tmp + str_arg_l, f, ullong_arg);
4874 break;
4875# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004876 }
4877 }
4878
4879 /* include the optional minus sign and possible
4880 * "0x" in the region before the zero padding
4881 * insertion point */
4882 if (zero_padding_insertion_ind < str_arg_l
4883 && tmp[zero_padding_insertion_ind] == '-')
4884 zero_padding_insertion_ind++;
4885 if (zero_padding_insertion_ind + 1 < str_arg_l
4886 && tmp[zero_padding_insertion_ind] == '0'
4887 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4888 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4889 zero_padding_insertion_ind += 2;
4890 }
4891
4892 {
4893 size_t num_of_digits = str_arg_l
4894 - zero_padding_insertion_ind;
4895
4896 if (alternate_form && fmt_spec == 'o'
4897 /* unless zero is already the first
4898 * character */
4899 && !(zero_padding_insertion_ind < str_arg_l
4900 && tmp[zero_padding_insertion_ind] == '0'))
4901 {
4902 /* assure leading zero for alternate-form
4903 * octal numbers */
4904 if (!precision_specified
4905 || precision < num_of_digits + 1)
4906 {
4907 /* precision is increased to force the
4908 * first character to be zero, except if a
4909 * zero value is formatted with an
4910 * explicit precision of zero */
4911 precision = num_of_digits + 1;
4912 precision_specified = 1;
4913 }
4914 }
4915 /* zero padding to specified precision? */
4916 if (num_of_digits < precision)
4917 number_of_zeros_to_pad = precision - num_of_digits;
4918 }
4919 /* zero padding to specified minimal field width? */
4920 if (!justify_left && zero_padding)
4921 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004922 int n = (int)(min_field_width - (str_arg_l
4923 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004924 if (n > 0)
4925 number_of_zeros_to_pad += n;
4926 }
4927 break;
4928 }
4929
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004930# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004931 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004932 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004933 case 'e':
4934 case 'E':
4935 case 'g':
4936 case 'G':
4937 {
4938 /* Floating point. */
4939 double f;
4940 double abs_f;
4941 char format[40];
4942 int l;
4943 int remove_trailing_zeroes = FALSE;
4944
4945 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004946# if defined(FEAT_EVAL)
4947 tvs != NULL ? tv_float(tvs, &arg_idx) :
4948# endif
4949 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004950 abs_f = f < 0 ? -f : f;
4951
4952 if (fmt_spec == 'g' || fmt_spec == 'G')
4953 {
4954 /* Would be nice to use %g directly, but it prints
4955 * "1.0" as "1", we don't want that. */
4956 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4957 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004958 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004959 else
4960 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4961 remove_trailing_zeroes = TRUE;
4962 }
4963
Bram Moolenaar04186092016-08-29 21:55:35 +02004964 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004965# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004966 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004967# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004968 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004969# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004970 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004971 {
4972 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004973 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4974 force_sign, space_for_positive));
4975 str_arg_l = STRLEN(tmp);
4976 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004977 }
4978 else
4979 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004980 if (isnan(f))
4981 {
4982 /* Not a number: nan or NAN */
4983 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4984 : "nan");
4985 str_arg_l = 3;
4986 zero_padding = 0;
4987 }
4988 else if (isinf(f))
4989 {
4990 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4991 force_sign, space_for_positive));
4992 str_arg_l = STRLEN(tmp);
4993 zero_padding = 0;
4994 }
4995 else
Bram Moolenaar04186092016-08-29 21:55:35 +02004996 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004997 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004998 format[0] = '%';
4999 l = 1;
5000 if (force_sign)
5001 format[l++] = space_for_positive ? ' ' : '+';
5002 if (precision_specified)
5003 {
5004 size_t max_prec = TMP_LEN - 10;
5005
5006 /* Make sure we don't get more digits than we
5007 * have room for. */
5008 if ((fmt_spec == 'f' || fmt_spec == 'F')
5009 && abs_f > 1.0)
5010 max_prec -= (size_t)log10(abs_f);
5011 if (precision > max_prec)
5012 precision = max_prec;
5013 l += sprintf(format + l, ".%d", (int)precision);
5014 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02005015 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02005016 format[l + 1] = NUL;
5017
Bram Moolenaare9997822016-08-28 16:03:38 +02005018 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar04186092016-08-29 21:55:35 +02005019 }
Bram Moolenaar99922372016-08-27 15:26:35 +02005020
Bram Moolenaara7241f52008-06-24 20:39:31 +00005021 if (remove_trailing_zeroes)
5022 {
5023 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005024 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005025
5026 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02005027 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005028 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005029 else
5030 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005031 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005032 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005033 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005034 {
5035 /* Remove superfluous '+' and leading
5036 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005037 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005038 {
5039 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005040 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005041 --str_arg_l;
5042 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005043 i = (tp[1] == '-') ? 2 : 1;
5044 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005045 {
5046 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005047 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005048 --str_arg_l;
5049 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005050 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005051 }
5052 }
5053
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005054 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005055 /* Remove trailing zeroes, but keep the one
5056 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005057 while (tp > tmp + 2 && *tp == '0'
5058 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005059 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005060 STRMOVE(tp, tp + 1);
5061 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005062 --str_arg_l;
5063 }
5064 }
5065 else
5066 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005067 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005068
5069 /* Be consistent: some printf("%e") use 1.0e+12
5070 * and some 1.0e+012. Remove one zero in the last
5071 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005072 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005073 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005074 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5075 && tp[2] == '0'
5076 && vim_isdigit(tp[3])
5077 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005078 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005079 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005080 --str_arg_l;
5081 }
5082 }
5083 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005084 if (zero_padding && min_field_width > str_arg_l
5085 && (tmp[0] == '-' || force_sign))
5086 {
5087 /* padding 0's should be inserted after the sign */
5088 number_of_zeros_to_pad = min_field_width - str_arg_l;
5089 zero_padding_insertion_ind = 1;
5090 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005091 str_arg = tmp;
5092 break;
5093 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005094# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005095
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005096 default:
5097 /* unrecognized conversion specifier, keep format string
5098 * as-is */
5099 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005100 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005101 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005102 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103
5104 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005105 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005106 str_arg = p;
5107 str_arg_l = 0;
5108 if (*p != NUL)
5109 str_arg_l++; /* include invalid conversion specifier
5110 unchanged if not at end-of-string */
5111 break;
5112 }
5113
5114 if (*p != NUL)
5115 p++; /* step over the just processed conversion specifier */
5116
5117 /* insert padding to the left as requested by min_field_width;
5118 * this does not include the zero padding in case of numerical
5119 * conversions*/
5120 if (!justify_left)
5121 {
5122 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005123 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005124
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005125 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005126 {
5127 if (str_l < str_m)
5128 {
5129 size_t avail = str_m - str_l;
5130
5131 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005132 (size_t)pn > avail ? avail
5133 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005134 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005135 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005136 }
5137 }
5138
5139 /* zero padding as requested by the precision or by the minimal
5140 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005141 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005142 {
5143 /* will not copy first part of numeric right now, *
5144 * force it to be copied later in its entirety */
5145 zero_padding_insertion_ind = 0;
5146 }
5147 else
5148 {
5149 /* insert first part of numerics (sign or '0x') before zero
5150 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005151 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005152
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005153 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005154 {
5155 if (str_l < str_m)
5156 {
5157 size_t avail = str_m - str_l;
5158
5159 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005160 (size_t)zn > avail ? avail
5161 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005162 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005163 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005164 }
5165
5166 /* insert zero padding as requested by the precision or min
5167 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005168 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005169 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005170 {
5171 if (str_l < str_m)
5172 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005173 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005174
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005175 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005176 (size_t)zn > avail ? avail
5177 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005178 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005179 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005180 }
5181 }
5182
5183 /* insert formatted string
5184 * (or as-is conversion specifier for unknown conversions) */
5185 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005186 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005187
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005188 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005189 {
5190 if (str_l < str_m)
5191 {
5192 size_t avail = str_m - str_l;
5193
5194 mch_memmove(str + str_l,
5195 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005196 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005197 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005198 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005199 }
5200 }
5201
5202 /* insert right padding */
5203 if (justify_left)
5204 {
5205 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005206 int pn = (int)(min_field_width
5207 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005208
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005209 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005210 {
5211 if (str_l < str_m)
5212 {
5213 size_t avail = str_m - str_l;
5214
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005215 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005216 (size_t)pn > avail ? avail
5217 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005218 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005219 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005220 }
5221 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005222 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005223 }
5224 }
5225
5226 if (str_m > 0)
5227 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005228 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005229 * overwriting the last character (shouldn't happen, but just in case)
5230 * */
5231 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5232 }
5233
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005234 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005237 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005238 * character), that is, the number of characters that would have been
5239 * written to the buffer if it were large enough. */
5240 return (int)str_l;
5241}
5242
5243#endif /* PROTO */