blob: 7c256585dcf903bdddeaf89b5da9ad43963e408c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
14#define MESSAGE_FILE /* don't include prototype for smsg() */
Bram Moolenaar44ca54a2016-08-27 15:41:32 +020015#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17#include "vim.h"
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static void add_msg_hist(char_u *s, int len, int attr);
20static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010022static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
31static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static int confirm_msg_used = FALSE; /* displaying confirm_msg */
36static char_u *confirm_msg = NULL; /* ":confirm" message */
37static char_u *confirm_msg_tail; /* tail of confirm_msg */
38#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010039#ifdef FEAT_JOB_CHANNEL
40static int emsg_to_channel_log = FALSE;
41#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43struct msg_hist
44{
45 struct msg_hist *next;
46 char_u *msg;
47 int attr;
48};
49
50static struct msg_hist *first_msg_hist = NULL;
51static struct msg_hist *last_msg_hist = NULL;
52static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020054static FILE *verbose_fd = NULL;
55static int verbose_did_open = FALSE;
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057/*
58 * When writing messages to the screen, there are many different situations.
59 * A number of variables is used to remember the current state:
60 * msg_didany TRUE when messages were written since the last time the
61 * user reacted to a prompt.
62 * Reset: After hitting a key for the hit-return prompt,
63 * hitting <CR> for the command line or input().
64 * Set: When any message is written to the screen.
65 * msg_didout TRUE when something was written to the current line.
66 * Reset: When advancing to the next line, when the current
67 * text can be overwritten.
68 * Set: When any message is written to the screen.
69 * msg_nowait No extra delay for the last drawn message.
70 * Used in normal_cmd() before the mode message is drawn.
71 * emsg_on_display There was an error message recently. Indicates that there
72 * should be a delay before redrawing.
73 * msg_scroll The next message should not overwrite the current one.
74 * msg_scrolled How many lines the screen has been scrolled (because of
75 * messages). Used in update_screen() to scroll the screen
76 * back. Incremented each time the screen scrolls a line.
77 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
78 * writes something without scrolling should not make
79 * need_wait_return to be set. This is a hack to make ":ts"
80 * work without an extra prompt.
81 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010082 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 * need_wait_return TRUE when the hit-return prompt is needed.
84 * Reset: After giving the hit-return prompt, when the user
85 * has answered some other prompt.
86 * Set: When the ruler or typeahead display is overwritten,
87 * scrolling the screen for some message.
88 * keep_msg Message to be displayed after redrawing the screen, in
89 * main_loop().
90 * This is an allocated string or NULL when not used.
91 */
92
93/*
94 * msg(s) - displays the string 's' on the status line
95 * When terminal not initialized (yet) mch_errmsg(..) is used.
96 * return TRUE if wait_return not called
97 */
98 int
Bram Moolenaar32526b32019-01-19 17:43:09 +010099msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100{
101 return msg_attr_keep(s, 0, FALSE);
102}
103
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000104/*
105 * Like msg() but keep it silent when 'verbosefile' is set.
106 */
107 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100108verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000109{
110 int n;
111
112 verbose_enter();
113 n = msg_attr_keep(s, 0, FALSE);
114 verbose_leave();
115
116 return n;
117}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100120msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121{
122 return msg_attr_keep(s, attr, FALSE);
123}
124
125 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100126msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100127 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100128 int attr,
129 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
131 static int entered = 0;
132 int retval;
133 char_u *buf = NULL;
134
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200135 /* Skip messages not matching ":filter pattern".
136 * Don't filter when there is an error. */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100137 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200138 return TRUE;
139
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_EVAL
141 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100142 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#endif
144
145 /*
146 * It is possible that displaying a messages causes a problem (e.g.,
147 * when redrawing the window), which causes another message, etc.. To
148 * break this loop, limit the recursiveness to 3 levels.
149 */
150 if (entered >= 3)
151 return TRUE;
152 ++entered;
153
154 /* Add message to history (unless it's a repeated kept message or a
155 * truncated message) */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100156 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 || (*s != '<'
158 && last_msg_hist != NULL
159 && last_msg_hist->msg != NULL
160 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100161 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Bram Moolenaar958dc692016-12-01 15:34:12 +0100163#ifdef FEAT_JOB_CHANNEL
164 if (emsg_to_channel_log)
Bram Moolenaar958dc692016-12-01 15:34:12 +0100165 /* Write message in the channel log. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200166 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* When displaying keep_msg, don't let msg_start() free it, caller must do
170 * that. */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100171 if ((char_u *)s == keep_msg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 keep_msg = NULL;
173
174 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000175 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100176 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100178 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 msg_clr_eos();
182 retval = msg_end();
183
Bram Moolenaar32526b32019-01-19 17:43:09 +0100184 if (keep && retval && vim_strsize((char_u *)s)
185 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
186 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188 vim_free(buf);
189 --entered;
190 return retval;
191}
192
193/*
194 * Truncate a string such that it can be printed without causing a scroll.
195 * Returns an allocated string or NULL when no truncating is done.
196 */
197 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100198msg_strtrunc(
199 char_u *s,
200 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 char_u *buf = NULL;
203 int len;
204 int room;
205
206 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000207 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
208 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000211 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000212 /* Use all the columns. */
213 room = (int)(Rows - msg_row) * Columns - 1;
214 else
215 /* Use up to 'showcmd' column. */
216 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 if (len > room && room > 0)
218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (enc_utf8)
220 /* may have up to 18 bytes per cell (6 per char, up to two
221 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100222 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 else if (enc_dbcs == DBCS_JPNU)
224 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100225 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 len = room + 2;
228 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100230 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
232 }
233 return buf;
234}
235
236/*
237 * Truncate a string "s" to "buf" with cell width "room".
238 * "s" and "buf" may be equal.
239 */
240 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100241trunc_string(
242 char_u *s,
243 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200244 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100245 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200247 size_t room = room_in - 3; /* "..." takes 3 chars */
248 size_t half;
249 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 int e;
251 int i;
252 int n;
253
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200254 if (room_in < 3)
255 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100259 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 {
261 if (s[e] == NUL)
262 {
263 /* text fits without truncating! */
264 buf[e] = NUL;
265 return;
266 }
267 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200268 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 break;
270 len += n;
271 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000273 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100275 if (++e == buflen)
276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 buf[e] = s[e];
278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 }
280
281 /* Last part: End of the string. */
282 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 if (enc_dbcs != 0)
284 {
285 /* For DBCS going backwards in a string is slow, but
286 * computing the cell width isn't too slow: go forward
287 * until the rest fits. */
288 n = vim_strsize(s + i);
289 while (len + n > room)
290 {
291 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000292 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 }
294 }
295 else if (enc_utf8)
296 {
297 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000298 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 for (;;)
300 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000301 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200302 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200303 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200305 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 break;
307 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200308 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 }
310 }
311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 {
313 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
314 len += n;
315 }
316
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200317
318 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100319 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200320 /* text fits without truncating */
321 if (s != buf)
322 {
323 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200324 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200325 len = buflen - 1;
326 len = len - e + 1;
327 if (len < 1)
328 buf[e - 1] = NUL;
329 else
330 mch_memmove(buf + e, s + e, len);
331 }
332 }
333 else if (e + 3 < buflen)
334 {
335 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100336 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200337 len = STRLEN(s + i) + 1;
338 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100339 len = buflen - e - 3 - 1;
340 mch_memmove(buf + e + 3, s + i, len);
341 buf[e + 3 + len - 1] = NUL;
342 }
343 else
344 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200345 /* can't fit in the "...", just truncate it */
346 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348}
349
350/*
351 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100352 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 * shorter than IOSIZE!!!
354 */
355#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100357int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100360# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100362# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100363smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364{
365 va_list arglist;
366
367 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100368 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100370 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371}
372
373 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100374# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100376# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
379 va_list arglist;
380
381 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100382 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100384 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385}
386
Bram Moolenaare0429682018-07-01 16:44:03 +0200387 int
388# ifdef __BORLANDC__
389_RTLENTRYF
390# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100391smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200392{
393 va_list arglist;
394
395 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100396 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaare0429682018-07-01 16:44:03 +0200397 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100398 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200399}
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#endif
402
403/*
404 * Remember the last sourcing name/lnum used in an error message, so that it
405 * isn't printed each time when it didn't change.
406 */
407static int last_sourcing_lnum = 0;
408static char_u *last_sourcing_name = NULL;
409
410/*
411 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
412 * for the next error message;
413 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000414 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100415reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100417 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 last_sourcing_lnum = 0;
419}
420
421/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000422 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
423 */
424 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100425other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000426{
427 if (sourcing_name != NULL)
428 {
429 if (last_sourcing_name != NULL)
430 return STRCMP(sourcing_name, last_sourcing_name) != 0;
431 return TRUE;
432 }
433 return FALSE;
434}
435
436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 * Get the message about the source, as used for an error message.
438 * Returns an allocated string with room for one more character.
439 * Returns NULL when no message is to be given.
440 */
441 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100442get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443{
444 char_u *Buf, *p;
445
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000446 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {
448 p = (char_u *)_("Error detected while processing %s:");
449 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
450 if (Buf != NULL)
451 sprintf((char *)Buf, (char *)p, sourcing_name);
452 return Buf;
453 }
454 return NULL;
455}
456
457/*
458 * Get the message about the source lnum, as used for an error message.
459 * Returns an allocated string with room for one more character.
460 * Returns NULL when no message is to be given.
461 */
462 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100463get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464{
465 char_u *Buf, *p;
466
467 /* lnum is 0 when executing a command from the command line
468 * argument, we don't want a line number then */
469 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000470 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 && sourcing_lnum != 0)
472 {
473 p = (char_u *)_("line %4ld:");
474 Buf = alloc((unsigned)(STRLEN(p) + 20));
475 if (Buf != NULL)
476 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
477 return Buf;
478 }
479 return NULL;
480}
481
482/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000483 * Display name and line number for the source of an error.
484 * Remember the file name and line number, so that for the next error the info
485 * is only displayed if it changed.
486 */
487 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100488msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000489{
490 char_u *p;
491
492 ++no_wait_return;
493 p = get_emsg_source();
494 if (p != NULL)
495 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100496 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000497 vim_free(p);
498 }
499 p = get_emsg_lnum();
500 if (p != NULL)
501 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100502 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000503 vim_free(p);
504 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
505 }
506
507 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000508 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000509 {
510 vim_free(last_sourcing_name);
511 if (sourcing_name == NULL)
512 last_sourcing_name = NULL;
513 else
514 last_sourcing_name = vim_strsave(sourcing_name);
515 }
516 --no_wait_return;
517}
518
519/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000520 * Return TRUE if not giving error messages right now:
521 * If "emsg_off" is set: no error messages at the moment.
522 * If "msg" is in 'debug': do error message but without side effects.
523 * If "emsg_skip" is set: never do error messages.
524 */
525 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100526emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000527{
528 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
529 && vim_strchr(p_debug, 't') == NULL)
530#ifdef FEAT_EVAL
531 || emsg_skip > 0
532#endif
533 )
534 return TRUE;
535 return FALSE;
536}
537
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100538#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100539static garray_T ignore_error_list = GA_EMPTY;
540
541 void
542ignore_error_for_testing(char_u *error)
543{
544 if (ignore_error_list.ga_itemsize == 0)
545 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
546
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100547 if (STRCMP("RESET", error) == 0)
548 ga_clear_strings(&ignore_error_list);
549 else
550 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100551}
552
553 static int
554ignore_error(char_u *msg)
555{
556 int i;
557
558 for (i = 0; i < ignore_error_list.ga_len; ++i)
559 if (strstr((char *)msg,
560 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
561 return TRUE;
562 return FALSE;
563}
564#endif
565
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200566#if !defined(HAVE_STRERROR) || defined(PROTO)
567/*
568 * Replacement for perror() that behaves more or less like emsg() was called.
569 * v:errmsg will be set and called_emsg will be set.
570 */
571 void
572do_perror(char *msg)
573{
574 perror(msg);
575 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100576 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200577 --emsg_silent;
578}
579#endif
580
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000581/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100582 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 *
584 * Rings the bell, if appropriate, and calls message() to do the real work
585 * When terminal not initialized (yet) mch_errmsg(..) is used.
586 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100587 * Return TRUE if wait_return not called.
588 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100590 static int
591emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100595 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#ifdef FEAT_EVAL
597 int ignore = FALSE;
598 int severe;
599#endif
600
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100601#ifdef FEAT_EVAL
602 /* When testing some errors are turned into a normal message. */
603 if (ignore_error(s))
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +0100604 /* don't call msg() if it results in a dialog */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100605 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100606#endif
607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 called_emsg = TRUE;
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#ifdef FEAT_EVAL
Bram Moolenaare723c422017-09-06 23:40:10 +0200611 /* If "emsg_severe" is TRUE: When an error exception is to be thrown,
612 * prefer this message over previous messages for the same command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 severe = emsg_severe;
614 emsg_severe = FALSE;
615#endif
616
Bram Moolenaar57657d82006-04-21 22:12:41 +0000617 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 {
619#ifdef FEAT_EVAL
620 /*
621 * Cause a throw of an error exception if appropriate. Don't display
622 * the error message in this case. (If no matching catch clause will
623 * be found, the message will be displayed later on.) "ignore" is set
624 * when the message should be ignored completely (used for the
625 * interrupt message).
626 */
627 if (cause_errthrow(s, severe, &ignore) == TRUE)
628 {
629 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100630 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 return TRUE;
632 }
633
634 /* set "v:errmsg", also when using ":silent! cmd" */
635 set_vim_var_string(VV_ERRMSG, s, -1);
636#endif
637
638 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000639 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 * But do write it to the redirection file.
641 */
642 if (emsg_silent != 0)
643 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200644 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200646 msg_start();
647 p = get_emsg_source();
648 if (p != NULL)
649 {
650 STRCAT(p, "\n");
651 redir_write(p, -1);
652 vim_free(p);
653 }
654 p = get_emsg_lnum();
655 if (p != NULL)
656 {
657 STRCAT(p, "\n");
658 redir_write(p, -1);
659 vim_free(p);
660 }
661 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100663#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200664 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return TRUE;
667 }
668
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100669 ex_exitval = 1;
670
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200671 /* Reset msg_silent, an error causes messages to be switched back on.
672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 msg_silent = 0;
674 cmd_silent = FALSE;
675
676 if (global_busy) /* break :global command */
677 ++global_busy;
678
679 if (p_eb)
680 beep_flush(); /* also includes flush_buffers() */
681 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200682 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100683 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200684#ifdef FEAT_EVAL
685 did_uncaught_emsg = TRUE;
686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688
689 emsg_on_display = TRUE; /* remember there is an error message */
690 ++msg_scroll; /* don't overwrite a previous message */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100691 attr = HL_ATTR(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000692 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 need_wait_return = TRUE; /* needed in case emsg() is called after
694 * wait_return has reset need_wait_return
695 * and a redraw is expected because
696 * msg_scrolled is non-zero */
697
Bram Moolenaar958dc692016-12-01 15:34:12 +0100698#ifdef FEAT_JOB_CHANNEL
699 emsg_to_channel_log = TRUE;
700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 /*
702 * Display name and line number for the source of the error.
703 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000704 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
706 /*
707 * Display the error message itself.
708 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000709 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100710 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100711
712#ifdef FEAT_JOB_CHANNEL
713 emsg_to_channel_log = FALSE;
714#endif
715 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716}
717
718/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100719 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 */
721 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100722emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100724 /* Skip this if not giving error messages at the moment. */
725 if (!emsg_not_now())
726 return emsg_core((char_u *)s);
727 return TRUE; /* no error messages at the moment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728}
729
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100730#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100731/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100732 * Print an error message with format string and variable arguments.
733 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100734 */
735 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100736semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100737{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100738 /* Skip this if not giving error messages at the moment. */
739 if (!emsg_not_now())
740 {
741 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100742
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100743 va_start(ap, s);
744 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
745 va_end(ap);
746 return emsg_core(IObuff);
747 }
748 return TRUE; /* no error messages at the moment */
Bram Moolenaar95f09602016-11-10 20:01:45 +0100749}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100750#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100751
752/*
753 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
754 * defined. It is used for internal errors only, so that they can be
755 * detected when fuzzing vim.
756 */
757 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100758iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100759{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100760 if (!emsg_not_now())
761 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100762#ifdef ABORT_ON_INTERNAL_ERROR
763 abort();
764#endif
765}
766
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100767#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100768/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100769 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100770 * defined. It is used for internal errors only, so that they can be
771 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100773 */
774 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100776{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100777 if (!emsg_not_now())
778 {
779 va_list ap;
780
781 va_start(ap, s);
782 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
783 va_end(ap);
784 emsg_core(IObuff);
785 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100786# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100787 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100788# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100789}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100790#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100791
792/*
793 * Give an "Internal error" message.
794 */
795 void
796internal_error(char *where)
797{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799}
800
Bram Moolenaarea424162005-06-16 21:51:00 +0000801/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaardf177f62005-02-22 08:39:57 +0000803 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100804emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000805{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000807}
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809/*
810 * Like msg(), but truncate to a single line if p_shm contains 't', or when
811 * "force" is TRUE. This truncates in another way as for normal messages.
812 * Careful: The string may be changed by msg_may_trunc()!
813 * Returns a pointer to the printed message, if wait_return() not called.
814 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100815 char *
816msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100819 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821 /* Add message to history before truncating */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100822 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaar32526b32019-01-19 17:43:09 +0100824 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100827 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 msg_hist_off = FALSE;
829
830 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100831 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 return NULL;
833}
834
835/*
836 * Check if message "s" should be truncated at the start (for filenames).
837 * Return a pointer to where the truncated message starts.
838 * Note: May change the message by replacing a character with '<'.
839 */
840 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100841msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 int n;
844 int room;
845
846 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
847 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
848 && (n = (int)STRLEN(s) - room) > 0)
849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (has_mbyte)
851 {
852 int size = vim_strsize(s);
853
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000854 /* There may be room anyway when there are multibyte chars. */
855 if (size <= room)
856 return s;
857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 for (n = 0; size >= room; )
859 {
860 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000861 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 --n;
864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 s += n;
866 *s = '<';
867 }
868 return s;
869}
870
871 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100872add_msg_hist(
873 char_u *s,
874 int len, /* -1 for undetermined length */
875 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
877 struct msg_hist *p;
878
879 if (msg_hist_off || msg_silent != 0)
880 return;
881
882 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000883 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000884 (void)delete_first_msg();
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 /* allocate an entry and add the message at the end of the history */
887 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
888 if (p != NULL)
889 {
890 if (len < 0)
891 len = (int)STRLEN(s);
892 /* remove leading and trailing newlines */
893 while (len > 0 && *s == '\n')
894 {
895 ++s;
896 --len;
897 }
898 while (len > 0 && s[len - 1] == '\n')
899 --len;
900 p->msg = vim_strnsave(s, len);
901 p->next = NULL;
902 p->attr = attr;
903 if (last_msg_hist != NULL)
904 last_msg_hist->next = p;
905 last_msg_hist = p;
906 if (first_msg_hist == NULL)
907 first_msg_hist = last_msg_hist;
908 ++msg_hist_len;
909 }
910}
911
912/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000913 * Delete the first (oldest) message from the history.
914 * Returns FAIL if there are no messages.
915 */
916 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100917delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000918{
919 struct msg_hist *p;
920
921 if (msg_hist_len <= 0)
922 return FAIL;
923 p = first_msg_hist;
924 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000925 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200926 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000927 vim_free(p->msg);
928 vim_free(p);
929 --msg_hist_len;
930 return OK;
931}
932
933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * ":messages" command.
935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200937ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 struct msg_hist *p;
940 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200941 int c = 0;
942
943 if (STRCMP(eap->arg, "clear") == 0)
944 {
945 int keep = eap->addr_count == 0 ? 0 : eap->line2;
946
947 while (msg_hist_len > keep)
948 (void)delete_first_msg();
949 return;
950 }
951
952 if (*eap->arg != NUL)
953 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100954 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +0200955 return;
956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 msg_hist_off = TRUE;
959
Bram Moolenaar451f8492016-04-14 17:16:22 +0200960 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200961 if (eap->addr_count != 0)
962 {
963 /* Count total messages */
964 for (; p != NULL && !got_int; p = p->next)
965 c++;
966
967 c -= eap->line2;
968
969 /* Skip without number of messages specified */
970 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
971 p = p->next, c--);
972 }
973
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200974 if (p == first_msg_hist)
975 {
976 s = mch_getenv((char_u *)"LANG");
977 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +0200978 // The next comment is extracted by xgettext and put in po file for
979 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100980 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +0200981 // Translator: Please replace the name and email address
982 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200983 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +0100984 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200985 }
986
Bram Moolenaar451f8492016-04-14 17:16:22 +0200987 /* Display what was not skipped. */
988 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100990 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 msg_hist_off = FALSE;
993}
994
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000995#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996/*
997 * Call this after prompting the user. This will avoid a hit-return message
998 * and a delay.
999 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001000 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001001msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 need_wait_return = FALSE;
1004 emsg_on_display = FALSE;
1005 cmdline_row = msg_row;
1006 msg_col = 0;
1007 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001008 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010#endif
1011
1012/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001013 * Wait for the user to hit a key (normally Enter).
1014 * If "redraw" is TRUE, clear and redraw the screen.
1015 * If "redraw" is FALSE, just redraw the screen.
1016 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
1018 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001019wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 int c;
1022 int oldState;
1023 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001025 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001026 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
1028 if (redraw == TRUE)
1029 must_redraw = CLEAR;
1030
1031 /* If using ":silent cmd", don't wait for a return. Also don't set
1032 * need_wait_return to do it later. */
1033 if (msg_silent != 0)
1034 return;
1035
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001036 /*
1037 * When inside vgetc(), we can't wait for a typed character at all.
1038 * With the global command (and some others) we only need one return at
1039 * the end. Adjust cmdline_row to avoid the next message overwriting the
1040 * last one.
1041 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001042 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001044 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (no_wait_return)
1046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (!exmode_active)
1048 cmdline_row = msg_row;
1049 return;
1050 }
1051
1052 redir_off = TRUE; /* don't redirect this message */
1053 oldState = State;
1054 if (quit_more)
1055 {
1056 c = CAR; /* just pretend CR was hit */
1057 quit_more = FALSE;
1058 got_int = FALSE;
1059 }
1060 else if (exmode_active)
1061 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001062 msg_puts(" "); /* make sure the cursor is on the right line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 c = CAR; /* no need for a return in ex mode */
1064 got_int = FALSE;
1065 }
1066 else
1067 {
1068 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1069 * just changed. */
1070 screenalloc(FALSE);
1071
1072 State = HITRETURN;
1073#ifdef FEAT_MOUSE
1074 setmouse();
1075#endif
1076#ifdef USE_ON_FLY_SCROLL
1077 dont_scroll = TRUE; /* disallow scrolling here */
1078#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001079 cmdline_row = msg_row;
1080
Bram Moolenaarec38d692013-04-24 15:12:32 +02001081 /* Avoid the sequence that the user types ":" at the hit-return prompt
1082 * to start an Ex command, but the file-changed dialog gets in the
1083 * way. */
1084 if (need_check_timestamps)
1085 check_timestamps(FALSE);
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 hit_return_msg();
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 do
1090 {
1091 /* Remember "got_int", if it is set vgetc() probably returns a
1092 * CTRL-C, but we need to loop then. */
1093 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001094
1095 /* Don't do mappings here, we put the character back in the
1096 * typeahead buffer. */
1097 ++no_mapping;
1098 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001099
1100 /* Temporarily disable Recording. If Recording is active, the
1101 * character will be recorded later, since it will be added to the
1102 * typebuf after the loop */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001103 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001104 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001105 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001106 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001108 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001110 --no_mapping;
1111 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001112 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001113 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#ifdef FEAT_CLIPBOARD
1116 /* Strange way to allow copying (yanking) a modeless selection at
1117 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1118 * Cmdline-mode and it's harmless when there is no selection. */
1119 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1120 {
1121 clip_copy_modeless_selection(TRUE);
1122 c = K_IGNORE;
1123 }
1124#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001125
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001126 /*
1127 * Allow scrolling back in the messages.
1128 * Also accept scroll-down commands when messages fill the screen,
1129 * to avoid that typing one 'j' too many makes the messages
1130 * disappear.
1131 */
1132 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001133 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001134 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1135 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001136 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001137 if (msg_scrolled > Rows)
1138 /* scroll back to show older messages */
1139 do_more_prompt(c);
1140 else
1141 {
1142 msg_didout = FALSE;
1143 c = K_IGNORE;
1144 msg_col =
1145#ifdef FEAT_RIGHTLEFT
1146 cmdmsg_rl ? Columns - 1 :
1147#endif
1148 0;
1149 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001150 if (quit_more)
1151 {
1152 c = CAR; /* just pretend CR was hit */
1153 quit_more = FALSE;
1154 got_int = FALSE;
1155 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001156 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001157 {
1158 c = K_IGNORE;
1159 hit_return_msg();
1160 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001161 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001162 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001163 && (c == 'j' || c == 'd' || c == 'f'
1164 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001165 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 } while ((had_got_int && c == Ctrl_C)
1168 || c == K_IGNORE
1169#ifdef FEAT_GUI
1170 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1171#endif
1172#ifdef FEAT_MOUSE
1173 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1174 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1175 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001176 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001178 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 || (!mouse_has(MOUSE_RETURN)
1180 && mouse_row < msg_row
1181 && (c == K_LEFTMOUSE
1182 || c == K_MIDDLEMOUSE
1183 || c == K_RIGHTMOUSE
1184 || c == K_X1MOUSE
1185 || c == K_X2MOUSE))
1186#endif
1187 );
1188 ui_breakcheck();
1189#ifdef FEAT_MOUSE
1190 /*
1191 * Avoid that the mouse-up event causes visual mode to start.
1192 */
1193 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1194 || c == K_X1MOUSE || c == K_X2MOUSE)
1195 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1196 else
1197#endif
1198 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1199 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001200 /* Put the character back in the typeahead buffer. Don't use the
1201 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001202 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001204 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 redir_off = FALSE;
1208
1209 /*
1210 * If the user hits ':', '?' or '/' we get a command line from the next
1211 * line.
1212 */
1213 if (c == ':' || c == '?' || c == '/')
1214 {
1215 if (!exmode_active)
1216 cmdline_row = msg_row;
1217 skip_redraw = TRUE; /* skip redraw once */
1218 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001219#ifdef FEAT_TERMINAL
1220 skip_term_loop = TRUE;
1221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223
1224 /*
1225 * If the window size changed set_shellsize() will redraw the screen.
1226 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1227 * typed.
1228 */
1229 tmpState = State;
1230 State = oldState; /* restore State before set_shellsize */
1231#ifdef FEAT_MOUSE
1232 setmouse();
1233#endif
1234 msg_check();
1235
1236#if defined(UNIX) || defined(VMS)
1237 /*
1238 * When switching screens, we need to output an extra newline on exit.
1239 */
1240 if (swapping_screen() && !termcap_active)
1241 newline_on_exit = TRUE;
1242#endif
1243
1244 need_wait_return = FALSE;
1245 did_wait_return = TRUE;
1246 emsg_on_display = FALSE; /* can delete error message now */
1247 lines_left = -1; /* reset lines_left at next msg_start() */
1248 reset_last_sourcing();
1249 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1250 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001251 VIM_CLEAR(keep_msg); /* don't redisplay message, it's too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1254 {
1255 starttermcap(); /* start termcap before redrawing */
1256 shell_resized();
1257 }
1258 else if (!skip_redraw
1259 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1260 {
1261 starttermcap(); /* start termcap before redrawing */
1262 redraw_later(VALID);
1263 }
1264}
1265
1266/*
1267 * Write the hit-return prompt.
1268 */
1269 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001270hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001272 int save_p_more = p_more;
1273
1274 p_more = FALSE; /* don't want see this message when scrolling back */
1275 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 msg_putchar('\n');
1277 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001278 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar32526b32019-01-19 17:43:09 +01001280 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (!msg_use_printf())
1282 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001283 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284}
1285
1286/*
1287 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1288 */
1289 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001290set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 vim_free(keep_msg);
1293 if (s != NULL && msg_silent == 0)
1294 keep_msg = vim_strsave(s);
1295 else
1296 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001297 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001298 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299}
1300
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001301#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1302/*
1303 * If there currently is a message being displayed, set "keep_msg" to it, so
1304 * that it will be displayed again after redraw.
1305 */
1306 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001307set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001308{
1309 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1310 && (State & NORMAL))
1311 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1312}
1313#endif
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315/*
1316 * Prepare for outputting characters in the command line.
1317 */
1318 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001319msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 int did_return = FALSE;
1322
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001323 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001324 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001325
1326#ifdef FEAT_EVAL
1327 if (need_clr_eos)
1328 {
1329 /* Halfway an ":echo" command and getting an (error) message: clear
1330 * any text from the command. */
1331 need_clr_eos = FALSE;
1332 msg_clr_eos();
1333 }
1334#endif
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (!msg_scroll && full_screen) /* overwrite last message */
1337 {
1338 msg_row = cmdline_row;
1339 msg_col =
1340#ifdef FEAT_RIGHTLEFT
1341 cmdmsg_rl ? Columns - 1 :
1342#endif
1343 0;
1344 }
1345 else if (msg_didout) /* start message on next line */
1346 {
1347 msg_putchar('\n');
1348 did_return = TRUE;
1349 if (exmode_active != EXMODE_NORMAL)
1350 cmdline_row = msg_row;
1351 }
1352 if (!msg_didany || lines_left < 0)
1353 msg_starthere();
1354 if (msg_silent == 0)
1355 {
1356 msg_didout = FALSE; /* no output on current line yet */
1357 cursor_off();
1358 }
1359
1360 /* when redirecting, may need to start a new line. */
1361 if (!did_return)
1362 redir_write((char_u *)"\n", -1);
1363}
1364
1365/*
1366 * Note that the current msg position is where messages start.
1367 */
1368 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001369msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 lines_left = cmdline_row;
1372 msg_didany = FALSE;
1373}
1374
1375 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001376msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 msg_putchar_attr(c, 0);
1379}
1380
1381 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001382msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001384 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386 if (IS_SPECIAL(c))
1387 {
1388 buf[0] = K_SPECIAL;
1389 buf[1] = K_SECOND(c);
1390 buf[2] = K_THIRD(c);
1391 buf[3] = NUL;
1392 }
1393 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001394 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001395 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397
1398 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001399msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001401 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
Bram Moolenaar32526b32019-01-19 17:43:09 +01001403 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 msg_puts(buf);
1405}
1406
1407 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001408msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 msg_home_replace_attr(fname, 0);
1411}
1412
1413#if defined(FEAT_FIND_ID) || defined(PROTO)
1414 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001415msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001417 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418}
1419#endif
1420
1421 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001422msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 char_u *name;
1425
1426 name = home_replace_save(NULL, fname);
1427 if (name != NULL)
1428 msg_outtrans_attr(name, attr);
1429 vim_free(name);
1430}
1431
1432/*
1433 * Output 'len' characters in 'str' (including NULs) with translation
1434 * if 'len' is -1, output upto a NUL character.
1435 * Use attributes 'attr'.
1436 * Return the number of characters it takes on the screen.
1437 */
1438 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001439msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 return msg_outtrans_attr(str, 0);
1442}
1443
1444 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001445msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1448}
1449
1450 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001451msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 return msg_outtrans_len_attr(str, len, 0);
1454}
1455
1456/*
1457 * Output one character at "p". Return pointer to the next character.
1458 * Handles multi-byte characters.
1459 */
1460 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001461msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 int l;
1464
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001465 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
1467 msg_outtrans_len_attr(p, l, attr);
1468 return p + l;
1469 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001470 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return p + 1;
1472}
1473
1474 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001475msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 int retval = 0;
1478 char_u *str = msgstr;
1479 char_u *plain_start = msgstr;
1480 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 int mb_l;
1482 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 /* if MSG_HIST flag set, add message to history */
1485 if (attr & MSG_HIST)
1486 {
1487 add_msg_hist(str, len, attr);
1488 attr &= ~MSG_HIST;
1489 }
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 /* If the string starts with a composing character first draw a space on
1492 * which the composing char can be drawn. */
1493 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001494 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 /*
1497 * Go over the string. Special characters are translated and printed.
1498 * Normal characters are printed several at a time.
1499 */
1500 while (--len >= 0)
1501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 if (enc_utf8)
1503 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001504 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001506 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 else
1508 mb_l = 1;
1509 if (has_mbyte && mb_l > 1)
1510 {
1511 c = (*mb_ptr2char)(str);
1512 if (vim_isprintc(c))
1513 /* printable multi-byte char: count the cells. */
1514 retval += (*mb_ptr2cells)(str);
1515 else
1516 {
1517 /* unprintable multi-byte char: print the printable chars so
1518 * far and the translation of the unprintable char. */
1519 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001520 msg_puts_attr_len((char *)plain_start,
1521 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001523 msg_puts_attr((char *)transchar(c),
1524 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 retval += char2cells(c);
1526 }
1527 len -= mb_l - 1;
1528 str += mb_l;
1529 }
1530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 s = transchar_byte(*str);
1533 if (s[1] != NUL)
1534 {
1535 /* unprintable char: print the printable chars so far and the
1536 * translation of the unprintable char. */
1537 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001538 msg_puts_attr_len((char *)plain_start,
1539 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001541 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001542 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001544 else
1545 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 ++str;
1547 }
1548 }
1549
1550 if (str > plain_start)
1551 /* print the printable chars at the end */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001552 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554 return retval;
1555}
1556
1557#if defined(FEAT_QUICKFIX) || defined(PROTO)
1558 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001559msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 int i;
1562 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1563
1564 arg = skipwhite(arg);
1565 for (i = 5; *arg && i >= 0; --i)
1566 if (*arg++ != str[i])
1567 break;
1568 if (i < 0)
1569 {
1570 msg_putchar('\n');
1571 for (i = 0; rs[i]; ++i)
1572 msg_putchar(rs[i] - 3);
1573 }
1574}
1575#endif
1576
1577/*
1578 * Output the string 'str' upto a NUL character.
1579 * Return the number of characters it takes on the screen.
1580 *
1581 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1582 * following character and shown as <F1>, <S-Up> etc. Any other character
1583 * which is not printable shown in <> form.
1584 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1585 * If a character is displayed in one of these special ways, is also
1586 * highlighted (its highlight name is '8' in the p_hl variable).
1587 * Otherwise characters are not highlighted.
1588 * This function is used to show mappings, where we want to see how to type
1589 * the character/string -- webb
1590 */
1591 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001592msg_outtrans_special(
1593 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001594 int from, // TRUE for lhs of a mapping
1595 int maxlen) // screen columns, 0 for unlimeted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
1597 char_u *str = strstart;
1598 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001599 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 int attr;
1601 int len;
1602
Bram Moolenaar8820b482017-03-16 17:23:31 +01001603 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 while (*str != NUL)
1605 {
1606 /* Leading and trailing spaces need to be displayed in <> form. */
1607 if ((str == strstart || str[1] == NUL) && *str == ' ')
1608 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001609 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 ++str;
1611 }
1612 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001613 text = (char *)str2special(&str, from);
1614 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001615 if (maxlen > 0 && retval + len >= maxlen)
1616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 /* Highlight special keys */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001618 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001619 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 retval += len;
1621 }
1622 return retval;
1623}
1624
Bram Moolenaarbd743252010-10-20 21:23:33 +02001625#if defined(FEAT_EVAL) || defined(PROTO)
1626/*
1627 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1628 * strings, in an allocated string.
1629 */
1630 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001631str2special_save(
1632 char_u *str,
1633 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001634{
1635 garray_T ga;
1636 char_u *p = str;
1637
1638 ga_init2(&ga, 1, 40);
1639 while (*p != NUL)
1640 ga_concat(&ga, str2special(&p, is_lhs));
1641 ga_append(&ga, NUL);
1642 return (char_u *)ga.ga_data;
1643}
1644#endif
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646/*
1647 * Return the printable string for the key codes at "*sp".
1648 * Used for translating the lhs or rhs of a mapping to printable chars.
1649 * Advances "sp" to the next code.
1650 */
1651 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001652str2special(
1653 char_u **sp,
1654 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 int c;
1657 static char_u buf[7];
1658 char_u *str = *sp;
1659 int modifiers = 0;
1660 int special = FALSE;
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (has_mbyte)
1663 {
1664 char_u *p;
1665
1666 /* Try to un-escape a multi-byte character. Return the un-escaped
1667 * string if it is a multi-byte character. */
1668 p = mb_unescape(sp);
1669 if (p != NULL)
1670 return p;
1671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
1673 c = *str;
1674 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1675 {
1676 if (str[1] == KS_MODIFIER)
1677 {
1678 modifiers = str[2];
1679 str += 3;
1680 c = *str;
1681 }
1682 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1683 {
1684 c = TO_SPECIAL(str[1], str[2]);
1685 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 }
1687 if (IS_SPECIAL(c) || modifiers) /* special key */
1688 special = TRUE;
1689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001691 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001693 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001694
1695 /* For multi-byte characters check for an illegal byte. */
1696 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1697 {
1698 transchar_nonprint(buf, c);
1699 *sp = str + 1;
1700 return buf;
1701 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001702 /* Since 'special' is TRUE the multi-byte character 'c' will be
1703 * processed by get_special_key_name() */
1704 c = (*mb_ptr2char)(str);
1705 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001707 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001708 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
1710 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1711 * Use <Space> only for lhs of a mapping. */
1712 if (special || char2cells(c) > 1 || (from && c == ' '))
1713 return get_special_key_name(c, modifiers);
1714 buf[0] = c;
1715 buf[1] = NUL;
1716 return buf;
1717}
1718
1719/*
1720 * Translate a key sequence into special key names.
1721 */
1722 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001723str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 char_u *s;
1726
1727 *buf = NUL;
1728 while (*sp)
1729 {
1730 s = str2special(&sp, FALSE);
1731 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1732 STRCAT(buf, s);
1733 }
1734}
1735
1736/*
1737 * print line for :print or :list command
1738 */
1739 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001740msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 int c;
1743 int col = 0;
1744 int n_extra = 0;
1745 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001746 int c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 char_u *p_extra = NULL; /* init to make SASC shut up */
1748 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001749 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 int l;
1752 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaardf177f62005-02-22 08:39:57 +00001754 if (curwin->w_p_list)
1755 list = TRUE;
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001758 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001761 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --trail;
1763 }
1764
1765 /* output a space for an empty line, otherwise the line will be
1766 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001767 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 msg_putchar(' ');
1769
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001770 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001772 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
1774 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001775 if (n_extra == 0 && c_final)
1776 c = c_final;
1777 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 c = c_extra;
1779 else
1780 c = *p_extra++;
1781 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001782 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
1784 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001785 if (lcs_nbsp != NUL && list
1786 && (mb_ptr2char(s) == 160
1787 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001788 {
1789 mb_char2bytes(lcs_nbsp, buf);
1790 buf[(*mb_ptr2len)(buf)] = NUL;
1791 }
1792 else
1793 {
1794 mch_memmove(buf, s, (size_t)l);
1795 buf[l] = NUL;
1796 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001797 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 s += l;
1799 continue;
1800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 else
1802 {
1803 attr = 0;
1804 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001805 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001808#ifdef FEAT_VARTABS
1809 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1810 curbuf->b_p_vts_array) - 1;
1811#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001813#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001814 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816 c = ' ';
1817 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001818 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 else
1821 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001822 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001824 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001825 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001828 else if (c == 160 && list && lcs_nbsp != NUL)
1829 {
1830 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001831 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001832 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001833 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
1835 p_extra = (char_u *)"";
1836 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001837 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 n_extra = 1;
1839 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001840 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 --s;
1842 }
1843 else if (c != NUL && (n = byte2cells(c)) > 1)
1844 {
1845 n_extra = n - 1;
1846 p_extra = transchar_byte(c);
1847 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001848 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001850 /* Use special coloring to be able to distinguish <hex> from
1851 * the same in plain text. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001852 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854 else if (c == ' ' && trail != NULL && s > trail)
1855 {
1856 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001857 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001859 else if (c == ' ' && list && lcs_space != NUL)
1860 {
1861 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001862 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 }
1865
1866 if (c == NUL)
1867 break;
1868
1869 msg_putchar_attr(c, attr);
1870 col++;
1871 }
1872 msg_clr_eos();
1873}
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875/*
1876 * Use screen_puts() to output one multi-byte character.
1877 * Return the pointer "s" advanced to the next character.
1878 */
1879 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001880screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
1882 int cw;
1883
1884 msg_didout = TRUE; /* remember that line is not empty */
1885 cw = (*mb_ptr2cells)(s);
1886 if (cw > 1 && (
1887#ifdef FEAT_RIGHTLEFT
1888 cmdmsg_rl ? msg_col <= 1 :
1889#endif
1890 msg_col == Columns - 1))
1891 {
1892 /* Doesn't fit, print a highlighted '>' to fill it up. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001893 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 return s;
1895 }
1896
1897 screen_puts_len(s, l, msg_row, msg_col, attr);
1898#ifdef FEAT_RIGHTLEFT
1899 if (cmdmsg_rl)
1900 {
1901 msg_col -= cw;
1902 if (msg_col == 0)
1903 {
1904 msg_col = Columns;
1905 ++msg_row;
1906 }
1907 }
1908 else
1909#endif
1910 {
1911 msg_col += cw;
1912 if (msg_col >= Columns)
1913 {
1914 msg_col = 0;
1915 ++msg_row;
1916 }
1917 }
1918 return s + l;
1919}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921/*
1922 * Output a string to the screen at position msg_row, msg_col.
1923 * Update msg_row and msg_col for the next message.
1924 */
1925 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001926msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001928 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929}
1930
1931 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001932msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001934 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935}
1936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937/*
1938 * Show a message in such a way that it always fits in the line. Cut out a
1939 * part in the middle and replace it with "..." when necessary.
1940 * Does not handle multi-byte characters!
1941 */
1942 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001943msg_outtrans_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001945 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946}
1947
1948 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001949msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
1951 int slen = len;
1952 int room;
1953
1954 room = Columns - msg_col;
1955 if (len > room && room >= 20)
1956 {
1957 slen = (room - 3) / 2;
1958 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001959 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1962}
1963
1964/*
1965 * Basic function for writing a message with highlight attributes.
1966 */
1967 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001968msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 msg_puts_attr_len(s, -1, attr);
1971}
1972
1973/*
1974 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1975 * When "maxlen" is -1 there is no maximum length.
1976 * When "maxlen" is >= 0 the message is not put in the history.
1977 */
1978 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001979msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 /*
1982 * If redirection is on, also write to the redirection file.
1983 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001984 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
1986 /*
1987 * Don't print anything when using ":silent cmd".
1988 */
1989 if (msg_silent != 0)
1990 return;
1991
1992 /* if MSG_HIST flag set, add message to history */
1993 if ((attr & MSG_HIST) && maxlen < 0)
1994 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001995 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 attr &= ~MSG_HIST;
1997 }
1998
1999 /*
2000 * When writing something to the screen after it has scrolled, requires a
2001 * wait-return prompt later. Needed when scrolling, resetting
2002 * need_wait_return after some prompt, and then outputting something
2003 * without scrolling
2004 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002005 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 need_wait_return = TRUE;
2007 msg_didany = TRUE; /* remember that something was outputted */
2008
2009 /*
2010 * If there is no valid screen, use fprintf so we can see error messages.
2011 * If termcap is not active, we may be writing in an alternate console
2012 * window, cursor positioning may not work correctly (window size may be
2013 * different, e.g. for Win32 console) or we just don't know where the
2014 * cursor is.
2015 */
2016 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002017 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002018 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002019 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002020}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002022/*
2023 * The display part of msg_puts_attr_len().
2024 * May be called recursively to display scroll-back text.
2025 */
2026 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002027msg_puts_display(
2028 char_u *str,
2029 int maxlen,
2030 int attr,
2031 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002032{
2033 char_u *s = str;
2034 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2035 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002036 int l;
2037 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002038 char_u *sb_str = str;
2039 int sb_col = msg_col;
2040 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002041 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002044 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
2046 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002047 * We are at the end of the screen line when:
2048 * - When outputting a newline.
2049 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002051 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#ifdef FEAT_RIGHTLEFT
2053 cmdmsg_rl
2054 ? (
2055 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002056 || (*s == TAB && msg_col <= 7)
2057 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 :
2059#endif
2060 (msg_col + t_col >= Columns - 1
2061 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002063 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002065 /*
2066 * The screen is scrolled up when at the last row (some terminals
2067 * scroll automatically, some don't. To avoid problems we scroll
2068 * ourselves).
2069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002072 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002074 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (msg_no_more && lines_left == 0)
2076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002078 /* Scroll the screen up one line. */
2079 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 msg_row = Rows - 2;
2082 if (msg_col >= Columns) /* can happen after screen resize */
2083 msg_col = Columns - 1;
2084
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002085 /* Display char in last column before showing more-prompt. */
2086 if (*s >= ' '
2087#ifdef FEAT_RIGHTLEFT
2088 && !cmdmsg_rl
2089#endif
2090 )
2091 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002092 if (has_mbyte)
2093 {
2094 if (enc_utf8 && maxlen >= 0)
2095 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002096 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002097 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002098 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002099 s = screen_puts_mbyte(s, l, attr);
2100 }
2101 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002102 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002103 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002104 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002105 else
2106 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002107
2108 if (p_more)
2109 /* store text for scrolling back */
2110 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2111
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002112 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002113 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 redraw_cmdline = TRUE;
2115 if (cmdline_row > 0 && !exmode_active)
2116 --cmdline_row;
2117
2118 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002119 * If screen is completely filled and 'more' is set then wait
2120 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002122 if (lines_left > 0)
2123 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002124 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 && !msg_no_more && !exmode_active)
2126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002128 if (do_more_prompt(NUL))
2129 s = confirm_msg_tail;
2130#else
2131 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002134 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002136
2137 /* When we displayed a char in last column need to check if there
2138 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002139 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002140 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002143 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002146 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002147 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2148 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002150 t_puts(&t_col, t_s, s, attr);
2151
2152 if (wrap && p_more && !recurse)
2153 /* store text for scrolling back */
2154 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 if (*s == '\n') /* go to next line */
2157 {
2158 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002159#ifdef FEAT_RIGHTLEFT
2160 if (cmdmsg_rl)
2161 msg_col = Columns - 1;
2162 else
2163#endif
2164 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (++msg_row >= Rows) /* safety check */
2166 msg_row = Rows - 1;
2167 }
2168 else if (*s == '\r') /* go to column 0 */
2169 {
2170 msg_col = 0;
2171 }
2172 else if (*s == '\b') /* go to previous char */
2173 {
2174 if (msg_col)
2175 --msg_col;
2176 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002177 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 do
2180 msg_screen_putchar(' ', attr);
2181 while (msg_col & 7);
2182 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002183 else if (*s == BELL) /* beep (from ":sh") */
2184 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 else
2186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (has_mbyte)
2188 {
2189 cw = (*mb_ptr2cells)(s);
2190 if (enc_utf8 && maxlen >= 0)
2191 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002192 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002194 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
2196 else
2197 {
2198 cw = 1;
2199 l = 1;
2200 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 /* When drawing from right to left or when a double-wide character
2203 * doesn't fit, draw a single character here. Otherwise collect
2204 * characters and draw them all at once later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (
2206# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002207 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002209 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (l > 1)
2212 s = screen_puts_mbyte(s, l, attr) - 1;
2213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 msg_screen_putchar(*s, attr);
2215 }
2216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 /* postpone this character until later */
2219 if (t_col == 0)
2220 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 t_col += cw;
2222 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224 }
2225 ++s;
2226 }
2227
2228 /* output any postponed text */
2229 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002230 t_puts(&t_col, t_s, s, attr);
2231 if (p_more && !recurse)
2232 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234 msg_check();
2235}
2236
2237/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002238 * Return TRUE when ":filter pattern" was used and "msg" does not match
2239 * "pattern".
2240 */
2241 int
2242message_filtered(char_u *msg)
2243{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002244 int match;
2245
2246 if (cmdmod.filter_regmatch.regprog == NULL)
2247 return FALSE;
2248 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2249 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002250}
2251
2252/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002253 * Scroll the screen up one line for displaying the next message line.
2254 */
2255 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002256msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002257{
2258#ifdef FEAT_GUI
2259 /* Remove the cursor before scrolling, ScreenLines[] is going
2260 * to become invalid. */
2261 if (gui.in_use)
2262 gui_undraw_cursor();
2263#endif
2264 /* scrolling up always works */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002265 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002266 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002267 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002268
2269 if (!can_clear((char_u *)" "))
2270 {
2271 /* Scrolling up doesn't result in the right background. Set the
2272 * background here. It's not efficient, but avoids that we have to do
2273 * it all over the code. */
2274 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2275
2276 /* Also clear the last char of the last but one line if it was not
2277 * cleared before to avoid a scroll-up. */
2278 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2279 screen_fill((int)Rows - 2, (int)Rows - 1,
2280 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2281 }
2282}
2283
2284/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002285 * Increment "msg_scrolled".
2286 */
2287 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002288inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002289{
2290#ifdef FEAT_EVAL
2291 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2292 {
2293 char_u *p = sourcing_name;
2294 char_u *tofree = NULL;
2295 int len;
2296
2297 /* v:scrollstart is empty, set it to the script/function name and line
2298 * number */
2299 if (p == NULL)
2300 p = (char_u *)_("Unknown");
2301 else
2302 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002303 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002304 tofree = alloc(len);
2305 if (tofree != NULL)
2306 {
2307 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2308 p, (long)sourcing_lnum);
2309 p = tofree;
2310 }
2311 }
2312 set_vim_var_string(VV_SCROLLSTART, p, -1);
2313 vim_free(tofree);
2314 }
2315#endif
2316 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002317 if (must_redraw < VALID)
2318 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002319}
2320
2321/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002322 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2323 * store the displayed text and remember where screen lines start.
2324 */
2325typedef struct msgchunk_S msgchunk_T;
2326struct msgchunk_S
2327{
2328 msgchunk_T *sb_next;
2329 msgchunk_T *sb_prev;
2330 char sb_eol; /* TRUE when line ends after this text */
2331 int sb_msg_col; /* column in which text starts */
2332 int sb_attr; /* text attributes */
2333 char_u sb_text[1]; /* text to be displayed, actually longer */
2334};
2335
2336static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2337
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002338static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002339
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002340typedef enum {
2341 SB_CLEAR_NONE = 0,
2342 SB_CLEAR_ALL,
2343 SB_CLEAR_CMDLINE_BUSY,
2344 SB_CLEAR_CMDLINE_DONE
2345} sb_clear_T;
2346
2347/* When to clear text on next msg. */
2348static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002349
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002350/*
2351 * Store part of a printed message for displaying when scrolling back.
2352 */
2353 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002354store_sb_text(
2355 char_u **sb_str, /* start of string */
2356 char_u *s, /* just after string */
2357 int attr,
2358 int *sb_col,
2359 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002360{
2361 msgchunk_T *mp;
2362
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002363 if (do_clear_sb_text == SB_CLEAR_ALL
2364 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002365 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002366 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2367 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002368 }
2369
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002370 if (s > *sb_str)
2371 {
2372 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2373 if (mp != NULL)
2374 {
2375 mp->sb_eol = finish;
2376 mp->sb_msg_col = *sb_col;
2377 mp->sb_attr = attr;
2378 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2379
2380 if (last_msgchunk == NULL)
2381 {
2382 last_msgchunk = mp;
2383 mp->sb_prev = NULL;
2384 }
2385 else
2386 {
2387 mp->sb_prev = last_msgchunk;
2388 last_msgchunk->sb_next = mp;
2389 last_msgchunk = mp;
2390 }
2391 mp->sb_next = NULL;
2392 }
2393 }
2394 else if (finish && last_msgchunk != NULL)
2395 last_msgchunk->sb_eol = TRUE;
2396
2397 *sb_str = s;
2398 *sb_col = 0;
2399}
2400
2401/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002402 * Finished showing messages, clear the scroll-back text on the next message.
2403 */
2404 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002405may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002406{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002407 do_clear_sb_text = SB_CLEAR_ALL;
2408}
2409
2410/*
2411 * Starting to edit the command line, do not clear messages now.
2412 */
2413 void
2414sb_text_start_cmdline(void)
2415{
2416 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2417 msg_sb_eol();
2418}
2419
2420/*
2421 * Ending to edit the command line. Clear old lines but the last one later.
2422 */
2423 void
2424sb_text_end_cmdline(void)
2425{
2426 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002427}
2428
2429/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002430 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002431 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002432 * Called when redrawing the screen.
2433 */
2434 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002435clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436{
2437 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002438 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002439
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002440 if (all)
2441 lastp = &last_msgchunk;
2442 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002443 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002444 if (last_msgchunk == NULL)
2445 return;
2446 lastp = &last_msgchunk->sb_prev;
2447 }
2448
2449 while (*lastp != NULL)
2450 {
2451 mp = (*lastp)->sb_prev;
2452 vim_free(*lastp);
2453 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002454 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002455}
2456
2457/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002458 * "g<" command.
2459 */
2460 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002461show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002462{
2463 msgchunk_T *mp;
2464
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002465 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002466 * weird, typing a command without output results in one line. */
2467 mp = msg_sb_start(last_msgchunk);
2468 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002469 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002470 else
2471 {
2472 do_more_prompt('G');
2473 wait_return(FALSE);
2474 }
2475}
2476
2477/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002478 * Move to the start of screen line in already displayed text.
2479 */
2480 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002481msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002482{
2483 msgchunk_T *mp = mps;
2484
2485 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2486 mp = mp->sb_prev;
2487 return mp;
2488}
2489
2490/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002491 * Mark the last message chunk as finishing the line.
2492 */
2493 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002494msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002495{
2496 if (last_msgchunk != NULL)
2497 last_msgchunk->sb_eol = TRUE;
2498}
2499
2500/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002501 * Display a screen line from previously displayed text at row "row".
2502 * Returns a pointer to the text for the next line (can be NULL).
2503 */
2504 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002505disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506{
2507 msgchunk_T *mp = smp;
2508 char_u *p;
2509
2510 for (;;)
2511 {
2512 msg_row = row;
2513 msg_col = mp->sb_msg_col;
2514 p = mp->sb_text;
2515 if (*p == '\n') /* don't display the line break */
2516 ++p;
2517 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2518 if (mp->sb_eol || mp->sb_next == NULL)
2519 break;
2520 mp = mp->sb_next;
2521 }
2522 return mp->sb_next;
2523}
2524
2525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 * Output any postponed text for msg_puts_attr_len().
2527 */
2528 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002529t_puts(
2530 int *t_col,
2531 char_u *t_s,
2532 char_u *s,
2533 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 /* output postponed text */
2536 msg_didout = TRUE; /* remember that line is not empty */
2537 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002538 msg_col += *t_col;
2539 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 /* If the string starts with a composing character don't increment the
2541 * column position for it. */
2542 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2543 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (msg_col >= Columns)
2545 {
2546 msg_col = 0;
2547 ++msg_row;
2548 }
2549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
2552 * Returns TRUE when messages should be printed with mch_errmsg().
2553 * This is used when there is no valid screen, so we can see error messages.
2554 * If termcap is not active, we may be writing in an alternate console
2555 * window, cursor positioning may not work correctly (window size may be
2556 * different, e.g. for Win32 console) or we just don't know where the
2557 * cursor is.
2558 */
2559 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002560msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002563#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2564# ifdef VIMDLL
2565 || (!gui.in_use && !termcap_active)
2566# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#endif
2570 || (swapping_screen() && !termcap_active)
2571 );
2572}
2573
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002574/*
2575 * Print a message when there is no valid screen.
2576 */
2577 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002578msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002579{
2580 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002581 char_u *buf = NULL;
2582 char_u *p = s;
2583
Bram Moolenaar4f974752019-02-17 17:44:42 +01002584#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002585 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002586 mch_settmode(TMODE_COOK); /* handle CR and NL correctly */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002588 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002589 {
2590 if (!(silent_mode && p_verbose == 0))
2591 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002592 // NL --> CR NL translation (for Unix, not for "--version")
2593 if (*s == NL)
2594 {
2595 int n = (int)(s - p);
2596
2597 buf = alloc(n + 3);
2598 memcpy(buf, p, n);
2599 if (!info_message)
2600 buf[n++] = CAR;
Bram Moolenaar00590742019-02-15 21:06:09 +01002601 buf[n++] = NL;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002602 buf[n++] = NUL;
2603 if (info_message) // informative message, not an error
2604 mch_msg((char *)buf);
2605 else
2606 mch_errmsg((char *)buf);
2607 vim_free(buf);
2608 p = s + 1;
2609 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002610 }
2611
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002612 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002613#ifdef FEAT_RIGHTLEFT
2614 if (cmdmsg_rl)
2615 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002616 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002617 msg_col = Columns - 1;
2618 else
2619 --msg_col;
2620 }
2621 else
2622#endif
2623 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002624 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002625 msg_col = 0;
2626 else
2627 ++msg_col;
2628 }
2629 ++s;
2630 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002631
2632 if (*p != NUL && !(silent_mode && p_verbose == 0))
2633 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002634 int c = -1;
2635
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002636 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002637 {
2638 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002639 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002640 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002641 if (info_message)
2642 mch_msg((char *)p);
2643 else
2644 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002645 if (c != -1)
2646 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002647 }
2648
2649 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002650
Bram Moolenaar4f974752019-02-17 17:44:42 +01002651#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002652 if (!(silent_mode && p_verbose == 0))
2653 mch_settmode(TMODE_RAW);
2654#endif
2655}
2656
2657/*
2658 * Show the more-prompt and handle the user response.
2659 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002660 * When at hit-enter prompt "typed_char" is the already typed character,
2661 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002662 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2663 */
2664 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002665do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002666{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002667 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002668 int used_typed_char = typed_char;
2669 int oldState = State;
2670 int c;
2671#ifdef FEAT_CON_DIALOG
2672 int retval = FALSE;
2673#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002674 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002675 msgchunk_T *mp_last = NULL;
2676 msgchunk_T *mp;
2677 int i;
2678
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002679 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002680 * that case don't show another prompt. Also when at the hit-Enter prompt
2681 * and nothing was typed. */
2682 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002683 return FALSE;
2684 entered = TRUE;
2685
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002686 if (typed_char == 'G')
2687 {
2688 /* "g<": Find first line on the last page. */
2689 mp_last = msg_sb_start(last_msgchunk);
2690 for (i = 0; i < Rows - 2 && mp_last != NULL
2691 && mp_last->sb_prev != NULL; ++i)
2692 mp_last = msg_sb_start(mp_last->sb_prev);
2693 }
2694
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002695 State = ASKMORE;
2696#ifdef FEAT_MOUSE
2697 setmouse();
2698#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002699 if (typed_char == NUL)
2700 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002701 for (;;)
2702 {
2703 /*
2704 * Get a typed character directly from the user.
2705 */
2706 if (used_typed_char != NUL)
2707 {
2708 c = used_typed_char; /* was typed at hit-enter prompt */
2709 used_typed_char = NUL;
2710 }
2711 else
2712 c = get_keystroke();
2713
2714#if defined(FEAT_MENU) && defined(FEAT_GUI)
2715 if (c == K_MENU)
2716 {
2717 int idx = get_menu_index(current_menu, ASKMORE);
2718
2719 /* Used a menu. If it starts with CTRL-Y, it must
2720 * be a "Copy" for the clipboard. Otherwise
2721 * assume that we end */
2722 if (idx == MENU_INDEX_INVALID)
2723 continue;
2724 c = *current_menu->strings[idx];
2725 if (c != NUL && current_menu->strings[idx][1] != NUL)
2726 ins_typebuf(current_menu->strings[idx] + 1,
2727 current_menu->noremap[idx], 0, TRUE,
2728 current_menu->silent[idx]);
2729 }
2730#endif
2731
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002732 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733 switch (c)
2734 {
2735 case BS: /* scroll one line back */
2736 case K_BS:
2737 case 'k':
2738 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002739 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 break;
2741
2742 case CAR: /* one extra line */
2743 case NL:
2744 case 'j':
2745 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002746 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002747 break;
2748
2749 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002750 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002751 break;
2752
2753 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002754 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755 break;
2756
2757 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002758 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002759 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002760 break;
2761
2762 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002763 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764 case K_PAGEDOWN:
2765 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002766 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002767 break;
2768
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002769 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002770 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002771 break;
2772
2773 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002774 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002775 lines_left = 999999;
2776 break;
2777
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002778 case ':': /* start new command line */
2779#ifdef FEAT_CON_DIALOG
2780 if (!confirm_msg_used)
2781#endif
2782 {
2783 /* Since got_int is set all typeahead will be flushed, but we
2784 * want to keep this ':', remember that in a special way. */
2785 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002786#ifdef FEAT_TERMINAL
2787 skip_term_loop = TRUE;
2788#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002789 cmdline_row = Rows - 1; /* put ':' on this line */
2790 skip_redraw = TRUE; /* skip redraw once */
2791 need_wait_return = FALSE; /* don't wait in main() */
2792 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002793 /* FALLTHROUGH */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002794 case 'q': /* quit */
2795 case Ctrl_C:
2796 case ESC:
2797#ifdef FEAT_CON_DIALOG
2798 if (confirm_msg_used)
2799 {
2800 /* Jump to the choices of the dialog. */
2801 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 }
2803 else
2804#endif
2805 {
2806 got_int = TRUE;
2807 quit_more = TRUE;
2808 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002809 /* When there is some more output (wrapping line) display that
2810 * without another prompt. */
2811 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002812 break;
2813
2814#ifdef FEAT_CLIPBOARD
2815 case Ctrl_Y:
2816 /* Strange way to allow copying (yanking) a modeless
2817 * selection at the more prompt. Use CTRL-Y,
2818 * because the same is used in Cmdline-mode and at the
2819 * hit-enter prompt. However, scrolling one line up
2820 * might be expected... */
2821 if (clip_star.state == SELECT_DONE)
2822 clip_copy_modeless_selection(TRUE);
2823 continue;
2824#endif
2825 default: /* no valid response */
2826 msg_moremsg(TRUE);
2827 continue;
2828 }
2829
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002830 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002831 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002832 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002833 {
2834 /* go to start of last line */
2835 if (mp_last == NULL)
2836 mp = msg_sb_start(last_msgchunk);
2837 else if (mp_last->sb_prev != NULL)
2838 mp = msg_sb_start(mp_last->sb_prev);
2839 else
2840 mp = NULL;
2841
2842 /* go to start of line at top of the screen */
2843 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2844 ++i)
2845 mp = msg_sb_start(mp->sb_prev);
2846
2847 if (mp != NULL && mp->sb_prev != NULL)
2848 {
2849 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002850 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 {
2852 if (mp == NULL || mp->sb_prev == NULL)
2853 break;
2854 mp = msg_sb_start(mp->sb_prev);
2855 if (mp_last == NULL)
2856 mp_last = msg_sb_start(last_msgchunk);
2857 else
2858 mp_last = msg_sb_start(mp_last->sb_prev);
2859 }
2860
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002861 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002862 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002863 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002864 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002865 (void)disp_sb_line(0, mp);
2866 }
2867 else
2868 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002869 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002870 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002871 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2872 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002874 ++msg_scrolled;
2875 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002876 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002877 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 }
2879 }
2880 else
2881 {
2882 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002883 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002884 {
2885 /* scroll up, display line at bottom */
2886 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002887 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002888 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2889 (int)Columns, ' ', ' ', 0);
2890 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002891 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002892 }
2893 }
2894
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002895 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 {
2897 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002898 screen_fill((int)Rows - 1, (int)Rows, 0,
2899 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 msg_moremsg(FALSE);
2901 continue;
2902 }
2903
2904 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002905 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002906 }
2907
2908 break;
2909 }
2910
2911 /* clear the --more-- message */
2912 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2913 State = oldState;
2914#ifdef FEAT_MOUSE
2915 setmouse();
2916#endif
2917 if (quit_more)
2918 {
2919 msg_row = Rows - 1;
2920 msg_col = 0;
2921 }
2922#ifdef FEAT_RIGHTLEFT
2923 else if (cmdmsg_rl)
2924 msg_col = Columns - 1;
2925#endif
2926
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002927 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928#ifdef FEAT_CON_DIALOG
2929 return retval;
2930#else
2931 return FALSE;
2932#endif
2933}
2934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2936
2937#ifdef mch_errmsg
2938# undef mch_errmsg
2939#endif
2940#ifdef mch_msg
2941# undef mch_msg
2942#endif
2943
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002944#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2945 static void
2946mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01002948 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002949 DWORD nwrite = 0;
2950 DWORD mode = 0;
2951 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
2952
2953 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
2954 && (int)GetConsoleCP() != enc_codepage)
2955 {
2956 WCHAR *w = enc_to_utf16((char_u *)str, &len);
2957
2958 WriteConsoleW(h, w, len, &nwrite, NULL);
2959 vim_free(w);
2960 }
2961 else
2962 {
2963 fprintf(stderr, "%s", str);
2964 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002965}
2966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002968/*
2969 * Give an error message. To be used when the screen hasn't been initialized
2970 * yet. When stderr can't be used, collect error messages until the GUI has
2971 * started and they can be displayed in a message box.
2972 */
2973 void
2974mch_errmsg(char *str)
2975{
2976#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
2977 int len;
2978#endif
2979
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02002980#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 /* On Unix use stderr if it's a tty.
2982 * When not going to start the GUI also use stderr.
2983 * On Mac, when started from Finder, stderr is the console. */
2984 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002985# ifdef UNIX
2986# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002988# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 isatty(2)
2990# endif
2991# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002992 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002993# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002994# endif
2995# ifdef FEAT_GUI
2996 !(gui.in_use || gui.starting)
2997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 )
2999 {
3000 fprintf(stderr, "%s", str);
3001 return;
3002 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003005#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3006# ifdef VIMDLL
3007 if (!(gui.in_use || gui.starting))
3008# endif
3009 {
3010 mch_errmsg_c(str);
3011 return;
3012 }
3013#endif
3014
3015#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 /* avoid a delay for a message that isn't there */
3017 emsg_on_display = FALSE;
3018
3019 len = (int)STRLEN(str) + 1;
3020 if (error_ga.ga_growsize == 0)
3021 {
3022 error_ga.ga_growsize = 80;
3023 error_ga.ga_itemsize = 1;
3024 }
3025 if (ga_grow(&error_ga, len) == OK)
3026 {
3027 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3028 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003029# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 /* remove CR characters, they are displayed */
3031 {
3032 char_u *p;
3033
3034 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3035 for (;;)
3036 {
3037 p = vim_strchr(p, '\r');
3038 if (p == NULL)
3039 break;
3040 *p = ' ';
3041 }
3042 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 --len; /* don't count the NUL at the end */
3045 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048}
3049
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003050#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3051 static void
3052mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003054 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003055 DWORD nwrite = 0;
3056 DWORD mode;
3057 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3058
3059
3060 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3061 && (int)GetConsoleCP() != enc_codepage)
3062 {
3063 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3064
3065 WriteConsoleW(h, w, len, &nwrite, NULL);
3066 vim_free(w);
3067 }
3068 else
3069 {
3070 printf("%s", str);
3071 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003072}
3073#endif
3074
3075/*
3076 * Give a message. To be used when the screen hasn't been initialized yet.
3077 * When there is no tty, collect messages until the GUI has started and they
3078 * can be displayed in a message box.
3079 */
3080 void
3081mch_msg(char *str)
3082{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003083#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3085 * uses mch_errmsg() when started from the desktop.
3086 * When not going to start the GUI also use stdout.
3087 * On Mac, when started from Finder, stderr is the console. */
3088 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003089# ifdef UNIX
3090# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003092# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 isatty(2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003096 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003098# endif
3099# ifdef FEAT_GUI
3100 !(gui.in_use || gui.starting)
3101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 )
3103 {
3104 printf("%s", str);
3105 return;
3106 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003107#endif
3108
3109#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3110# ifdef VIMDLL
3111 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003113 {
3114 mch_msg_c(str);
3115 return;
3116 }
3117#endif
3118#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121}
3122#endif /* USE_MCH_ERRMSG */
3123
3124/*
3125 * Put a character on the screen at the current message position and advance
3126 * to the next position. Only for printable ASCII!
3127 */
3128 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003129msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 msg_didout = TRUE; /* remember that line is not empty */
3132 screen_putchar(c, msg_row, msg_col, attr);
3133#ifdef FEAT_RIGHTLEFT
3134 if (cmdmsg_rl)
3135 {
3136 if (--msg_col == 0)
3137 {
3138 msg_col = Columns;
3139 ++msg_row;
3140 }
3141 }
3142 else
3143#endif
3144 {
3145 if (++msg_col >= Columns)
3146 {
3147 msg_col = 0;
3148 ++msg_row;
3149 }
3150 }
3151}
3152
3153 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003154msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003156 int attr;
3157 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar8820b482017-03-16 17:23:31 +01003159 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003160 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003162 screen_puts((char_u *)
3163 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3164 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165}
3166
3167/*
3168 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3169 * exmode_active.
3170 */
3171 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003172repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 if (State == ASKMORE)
3175 {
3176 msg_moremsg(TRUE); /* display --more-- message again */
3177 msg_row = Rows - 1;
3178 }
3179#ifdef FEAT_CON_DIALOG
3180 else if (State == CONFIRM)
3181 {
3182 display_confirm_msg(); /* display ":confirm" message again */
3183 msg_row = Rows - 1;
3184 }
3185#endif
3186 else if (State == EXTERNCMD)
3187 {
3188 windgoto(msg_row, msg_col); /* put cursor back */
3189 }
3190 else if (State == HITRETURN || State == SETWSIZE)
3191 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003192 if (msg_row == Rows - 1)
3193 {
3194 /* Avoid drawing the "hit-enter" prompt below the previous one,
3195 * overwrite it. Esp. useful when regaining focus and a
3196 * FocusGained autocmd exists but didn't draw anything. */
3197 msg_didout = FALSE;
3198 msg_col = 0;
3199 msg_clr_eos();
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 hit_return_msg();
3202 msg_row = Rows - 1;
3203 }
3204}
3205
3206/*
3207 * msg_check_screen - check if the screen is initialized.
3208 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3209 * While starting the GUI the terminal codes will be set for the GUI, but the
3210 * output goes to the terminal. Don't use the terminal codes then.
3211 */
3212 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003213msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 if (!full_screen || !screen_valid(FALSE))
3216 return FALSE;
3217
3218 if (msg_row >= Rows)
3219 msg_row = Rows - 1;
3220 if (msg_col >= Columns)
3221 msg_col = Columns - 1;
3222 return TRUE;
3223}
3224
3225/*
3226 * Clear from current message position to end of screen.
3227 * Skip this when ":silent" was used, no need to clear for redirection.
3228 */
3229 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003230msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
3232 if (msg_silent == 0)
3233 msg_clr_eos_force();
3234}
3235
3236/*
3237 * Clear from current message position to end of screen.
3238 * Note: msg_col is not updated, so we remember the end of the message
3239 * for msg_check().
3240 */
3241 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003242msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
3244 if (msg_use_printf())
3245 {
3246 if (full_screen) /* only when termcap codes are valid */
3247 {
3248 if (*T_CD)
3249 out_str(T_CD); /* clear to end of display */
3250 else if (*T_CE)
3251 out_str(T_CE); /* clear to end of line */
3252 }
3253 }
3254 else
3255 {
3256#ifdef FEAT_RIGHTLEFT
3257 if (cmdmsg_rl)
3258 {
3259 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3260 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3261 }
3262 else
3263#endif
3264 {
3265 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3266 ' ', ' ', 0);
3267 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3268 }
3269 }
3270}
3271
3272/*
3273 * Clear the command line.
3274 */
3275 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003276msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 msg_row = cmdline_row;
3279 msg_col = 0;
3280 msg_clr_eos_force();
3281}
3282
3283/*
3284 * end putting a message on the screen
3285 * call wait_return if the message does not fit in the available space
3286 * return TRUE if wait_return not called.
3287 */
3288 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003289msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003292 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 * or the ruler option is set and we run into it,
3294 * we have to redraw the window.
3295 * Do not do this if we are abandoning the file or editing the command line.
3296 */
3297 if (!exiting && need_wait_return && !(State & CMDLINE))
3298 {
3299 wait_return(FALSE);
3300 return FALSE;
3301 }
3302 out_flush();
3303 return TRUE;
3304}
3305
3306/*
3307 * If the written message runs into the shown command or ruler, we have to
3308 * wait for hit-return and redraw the window later.
3309 */
3310 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003311msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 if (msg_row == Rows - 1 && msg_col >= sc_col)
3314 {
3315 need_wait_return = TRUE;
3316 redraw_cmdline = TRUE;
3317 }
3318}
3319
3320/*
3321 * May write a string to the redirection file.
3322 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3323 */
3324 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003325redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 char_u *s = str;
3328 static int cur_col = 0;
3329
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003330 /* Don't do anything for displaying prompts and the like. */
3331 if (redir_off)
3332 return;
3333
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003334 /* If 'verbosefile' is set prepare for writing in that file. */
3335 if (*p_vfile != NUL && verbose_fd == NULL)
3336 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003337
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003338 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
3340 /* If the string doesn't start with CR or NL, go to msg_col */
3341 if (*s != '\n' && *s != '\r')
3342 {
3343 while (cur_col < msg_col)
3344 {
3345#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003346 if (redir_execute)
3347 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003348 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003350 else if (redir_vname)
3351 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003354 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003356 if (verbose_fd != NULL)
3357 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 ++cur_col;
3359 }
3360 }
3361
3362#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003363 if (redir_execute)
3364 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003365 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003367 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003368 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
3370
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003371 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3373 {
3374#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003375 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003377 if (redir_fd != NULL)
3378 putc(*s, redir_fd);
3379 if (verbose_fd != NULL)
3380 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (*s == '\r' || *s == '\n')
3382 cur_col = 0;
3383 else if (*s == '\t')
3384 cur_col += (8 - cur_col % 8);
3385 else
3386 ++cur_col;
3387 ++s;
3388 }
3389
3390 if (msg_silent != 0) /* should update msg_col */
3391 msg_col = cur_col;
3392 }
3393}
3394
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003395 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003396redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003397{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003398 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003399#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003400 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003401#endif
3402 ;
3403}
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003406 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003407 * Must always be called paired with verbose_leave()!
3408 */
3409 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003410verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003411{
3412 if (*p_vfile != NUL)
3413 ++msg_silent;
3414}
3415
3416/*
3417 * After giving verbose message.
3418 * Must always be called paired with verbose_enter()!
3419 */
3420 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003421verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003422{
3423 if (*p_vfile != NUL)
3424 if (--msg_silent < 0)
3425 msg_silent = 0;
3426}
3427
3428/*
3429 * Like verbose_enter() and set msg_scroll when displaying the message.
3430 */
3431 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003432verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003433{
3434 if (*p_vfile != NUL)
3435 ++msg_silent;
3436 else
3437 /* always scroll up, don't overwrite */
3438 msg_scroll = TRUE;
3439}
3440
3441/*
3442 * Like verbose_leave() and set cmdline_row when displaying the message.
3443 */
3444 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003445verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003446{
3447 if (*p_vfile != NUL)
3448 {
3449 if (--msg_silent < 0)
3450 msg_silent = 0;
3451 }
3452 else
3453 cmdline_row = msg_row;
3454}
3455
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003456/*
3457 * Called when 'verbosefile' is set: stop writing to the file.
3458 */
3459 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003460verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003461{
3462 if (verbose_fd != NULL)
3463 {
3464 fclose(verbose_fd);
3465 verbose_fd = NULL;
3466 }
3467 verbose_did_open = FALSE;
3468}
3469
3470/*
3471 * Open the file 'verbosefile'.
3472 * Return FAIL or OK.
3473 */
3474 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003475verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003476{
3477 if (verbose_fd == NULL && !verbose_did_open)
3478 {
3479 /* Only give the error message once. */
3480 verbose_did_open = TRUE;
3481
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003482 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003483 if (verbose_fd == NULL)
3484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003486 return FAIL;
3487 }
3488 }
3489 return OK;
3490}
3491
3492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 * Give a warning message (for searching).
3494 * Use 'w' highlighting and may repeat the message after redrawing
3495 */
3496 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003497give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 /* Don't do this for ":silent". */
3500 if (msg_silent != 0)
3501 return;
3502
3503 /* Don't want a hit-enter prompt here. */
3504 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506#ifdef FEAT_EVAL
3507 set_vim_var_string(VV_WARNINGMSG, message, -1);
3508#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003509 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003511 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 else
3513 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003514 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003515 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 msg_didout = FALSE; /* overwrite this message */
3517 msg_nowait = TRUE; /* don't wait for this message */
3518 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 --no_wait_return;
3521}
3522
Bram Moolenaar113e1072019-01-20 15:30:40 +01003523#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003524 void
3525give_warning2(char_u *message, char_u *a1, int hl)
3526{
3527 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3528 give_warning(IObuff, hl);
3529}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003530#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532/*
3533 * Advance msg cursor to column "col".
3534 */
3535 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003536msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
3538 if (msg_silent != 0) /* nothing to advance to */
3539 {
3540 msg_col = col; /* for redirection, may fill it up later */
3541 return;
3542 }
3543 if (col >= Columns) /* not enough room */
3544 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003545#ifdef FEAT_RIGHTLEFT
3546 if (cmdmsg_rl)
3547 while (msg_col > Columns - col)
3548 msg_putchar(' ');
3549 else
3550#endif
3551 while (msg_col < col)
3552 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553}
3554
3555#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3556/*
3557 * Used for "confirm()" function, and the :confirm command prefix.
3558 * Versions which haven't got flexible dialogs yet, and console
3559 * versions, get this generic handler which uses the command line.
3560 *
3561 * type = one of:
3562 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3563 * title = title string (can be NULL for default)
3564 * (neither used in console dialogs at the moment)
3565 *
3566 * Format of the "buttons" string:
3567 * "Button1Name\nButton2Name\nButton3Name"
3568 * The first button should normally be the default/accept
3569 * The second button should be the 'Cancel' button
3570 * Other buttons- use your imagination!
3571 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3572 * different letter.
3573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003575do_dialog(
3576 int type UNUSED,
3577 char_u *title UNUSED,
3578 char_u *message,
3579 char_u *buttons,
3580 int dfltbutton,
3581 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003582 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003583 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003584 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
3586 int oldState;
3587 int retval = 0;
3588 char_u *hotkeys;
3589 int c;
3590 int i;
3591
3592#ifndef NO_CONSOLE
3593 /* Don't output anything in silent mode ("ex -s") */
3594 if (silent_mode)
3595 return dfltbutton; /* return default option */
3596#endif
3597
3598#ifdef FEAT_GUI_DIALOG
3599 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3600 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3601 {
3602 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003603 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003604 /* avoid a hit-enter prompt without clearing the cmdline */
3605 need_wait_return = FALSE;
3606 emsg_on_display = FALSE;
3607 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 /* Flush output to avoid that further messages and redrawing is done
3610 * in the wrong order. */
3611 out_flush();
3612 gui_mch_update();
3613
3614 return c;
3615 }
3616#endif
3617
3618 oldState = State;
3619 State = CONFIRM;
3620#ifdef FEAT_MOUSE
3621 setmouse();
3622#endif
3623
3624 /*
3625 * Since we wait for a keypress, don't make the
3626 * user press RETURN as well afterwards.
3627 */
3628 ++no_wait_return;
3629 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3630
3631 if (hotkeys != NULL)
3632 {
3633 for (;;)
3634 {
3635 /* Get a typed character directly from the user. */
3636 c = get_keystroke();
3637 switch (c)
3638 {
3639 case CAR: /* User accepts default option */
3640 case NL:
3641 retval = dfltbutton;
3642 break;
3643 case Ctrl_C: /* User aborts/cancels */
3644 case ESC:
3645 retval = 0;
3646 break;
3647 default: /* Could be a hotkey? */
3648 if (c < 0) /* special keys are ignored here */
3649 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003650 if (c == ':' && ex_cmd)
3651 {
3652 retval = dfltbutton;
3653 ins_char_typebuf(':');
3654 break;
3655 }
3656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 /* Make the character lowercase, as chars in "hotkeys" are. */
3658 c = MB_TOLOWER(c);
3659 retval = 1;
3660 for (i = 0; hotkeys[i]; ++i)
3661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (has_mbyte)
3663 {
3664 if ((*mb_ptr2char)(hotkeys + i) == c)
3665 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003666 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (hotkeys[i] == c)
3670 break;
3671 ++retval;
3672 }
3673 if (hotkeys[i])
3674 break;
3675 /* No hotkey match, so keep waiting */
3676 continue;
3677 }
3678 break;
3679 }
3680
3681 vim_free(hotkeys);
3682 }
3683
3684 State = oldState;
3685#ifdef FEAT_MOUSE
3686 setmouse();
3687#endif
3688 --no_wait_return;
3689 msg_end_prompt();
3690
3691 return retval;
3692}
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
3695 * Copy one character from "*from" to "*to", taking care of multi-byte
3696 * characters. Return the length of the character in bytes.
3697 */
3698 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003699copy_char(
3700 char_u *from,
3701 char_u *to,
3702 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 int len;
3705 int c;
3706
3707 if (has_mbyte)
3708 {
3709 if (lowercase)
3710 {
3711 c = MB_TOLOWER((*mb_ptr2char)(from));
3712 return (*mb_char2bytes)(c, to);
3713 }
3714 else
3715 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003716 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 mch_memmove(to, from, (size_t)len);
3718 return len;
3719 }
3720 }
3721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
3723 if (lowercase)
3724 *to = (char_u)TOLOWER_LOC(*from);
3725 else
3726 *to = *from;
3727 return 1;
3728 }
3729}
3730
3731/*
3732 * Format the dialog string, and display it at the bottom of
3733 * the screen. Return a string of hotkey chars (if defined) for
3734 * each 'button'. If a button has no hotkey defined, the first character of
3735 * the button is used.
3736 * The hotkeys can be multi-byte characters, but without combining chars.
3737 *
3738 * Returns an allocated string with hotkeys, or NULL for error.
3739 */
3740 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003741msg_show_console_dialog(
3742 char_u *message,
3743 char_u *buttons,
3744 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
3746 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003747#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 int lenhotkey = HOTK_LEN; /* count first button */
3749 char_u *hotk = NULL;
3750 char_u *msgp = NULL;
3751 char_u *hotkp = NULL;
3752 char_u *r;
3753 int copy;
3754#define HAS_HOTKEY_LEN 30
3755 char_u has_hotkey[HAS_HOTKEY_LEN];
3756 int first_hotkey = FALSE; /* first char of button is hotkey */
3757 int idx;
3758
3759 has_hotkey[0] = FALSE;
3760
3761 /*
3762 * First loop: compute the size of memory to allocate.
3763 * Second loop: copy to the allocated memory.
3764 */
3765 for (copy = 0; copy <= 1; ++copy)
3766 {
3767 r = buttons;
3768 idx = 0;
3769 while (*r)
3770 {
3771 if (*r == DLG_BUTTON_SEP)
3772 {
3773 if (copy)
3774 {
3775 *msgp++ = ',';
3776 *msgp++ = ' '; /* '\n' -> ', ' */
3777
3778 /* advance to next hotkey and set default hotkey */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003780 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003783 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 if (dfltbutton)
3785 --dfltbutton;
3786
3787 /* If no hotkey is specified first char is used. */
3788 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3789 first_hotkey = TRUE;
3790 }
3791 else
3792 {
3793 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3794 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3795 if (idx < HAS_HOTKEY_LEN - 1)
3796 has_hotkey[++idx] = FALSE;
3797 }
3798 }
3799 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3800 {
3801 if (*r == DLG_HOTKEY_CHAR)
3802 ++r;
3803 first_hotkey = FALSE;
3804 if (copy)
3805 {
3806 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3807 *msgp++ = *r;
3808 else
3809 {
3810 /* '&a' -> '[a]' */
3811 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3812 msgp += copy_char(r, msgp, FALSE);
3813 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3814
3815 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003816 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818 }
3819 else
3820 {
3821 ++len; /* '&a' -> '[a]' */
3822 if (idx < HAS_HOTKEY_LEN - 1)
3823 has_hotkey[idx] = TRUE;
3824 }
3825 }
3826 else
3827 {
3828 /* everything else copy literally */
3829 if (copy)
3830 msgp += copy_char(r, msgp, FALSE);
3831 }
3832
3833 /* advance to the next character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003834 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836
3837 if (copy)
3838 {
3839 *msgp++ = ':';
3840 *msgp++ = ' ';
3841 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 else
3844 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003846 + 2 /* for the NL's */
3847 + STRLEN(buttons)
3848 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003849 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851 /* If no hotkey is specified first char is used. */
3852 if (!has_hotkey[0])
3853 {
3854 first_hotkey = TRUE;
3855 len += 2; /* "x" -> "[x]" */
3856 }
3857
3858 /*
3859 * Now allocate and load the strings
3860 */
3861 vim_free(confirm_msg);
3862 confirm_msg = alloc(len);
3863 if (confirm_msg == NULL)
3864 return NULL;
3865 *confirm_msg = NUL;
3866 hotk = alloc(lenhotkey);
3867 if (hotk == NULL)
3868 return NULL;
3869
3870 *confirm_msg = '\n';
3871 STRCPY(confirm_msg + 1, message);
3872
3873 msgp = confirm_msg + 1 + STRLEN(message);
3874 hotkp = hotk;
3875
Bram Moolenaar6a516062007-07-05 08:11:42 +00003876 /* Define first default hotkey. Keep the hotkey string NUL
3877 * terminated to avoid reading past the end. */
3878 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 /* Remember where the choices start, displaying starts here when
3881 * "hotkp" typed at the more prompt. */
3882 confirm_msg_tail = msgp;
3883 *msgp++ = '\n';
3884 }
3885 }
3886
3887 display_confirm_msg();
3888 return hotk;
3889}
3890
3891/*
3892 * Display the ":confirm" message. Also called when screen resized.
3893 */
3894 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003895display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
3897 /* avoid that 'q' at the more prompt truncates the message here */
3898 ++confirm_msg_used;
3899 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003900 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 --confirm_msg_used;
3902}
3903
3904#endif /* FEAT_CON_DIALOG */
3905
3906#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3907
3908 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003909vim_dialog_yesno(
3910 int type,
3911 char_u *title,
3912 char_u *message,
3913 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
3915 if (do_dialog(type,
3916 title == NULL ? (char_u *)_("Question") : title,
3917 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003918 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 return VIM_YES;
3920 return VIM_NO;
3921}
3922
3923 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003924vim_dialog_yesnocancel(
3925 int type,
3926 char_u *title,
3927 char_u *message,
3928 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 switch (do_dialog(type,
3931 title == NULL ? (char_u *)_("Question") : title,
3932 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003933 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
3935 case 1: return VIM_YES;
3936 case 2: return VIM_NO;
3937 }
3938 return VIM_CANCEL;
3939}
3940
3941 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003942vim_dialog_yesnoallcancel(
3943 int type,
3944 char_u *title,
3945 char_u *message,
3946 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 switch (do_dialog(type,
3949 title == NULL ? (char_u *)"Question" : title,
3950 message,
3951 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003952 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
3954 case 1: return VIM_YES;
3955 case 2: return VIM_NO;
3956 case 3: return VIM_ALL;
3957 case 4: return VIM_DISCARDALL;
3958 }
3959 return VIM_CANCEL;
3960}
3961
3962#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3963
3964#if defined(FEAT_BROWSE) || defined(PROTO)
3965/*
3966 * Generic browse function. Calls gui_mch_browse() when possible.
3967 * Later this may pop-up a non-GUI file selector (external command?).
3968 */
3969 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003970do_browse(
3971 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3972 char_u *title, /* title for the window */
3973 char_u *dflt, /* default file name (may include directory) */
3974 char_u *ext, /* extension added */
3975 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003977 char_u *filter, /* file name filter */
3978 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
3980 char_u *fname;
3981 static char_u *last_dir = NULL; /* last used directory */
3982 char_u *tofree = NULL;
3983 int save_browse = cmdmod.browse;
3984
3985 /* Must turn off browse to avoid that autocommands will get the
3986 * flag too! */
3987 cmdmod.browse = FALSE;
3988
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003989 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003991 if (flags & BROWSE_DIR)
3992 title = (char_u *)_("Select Directory dialog");
3993 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 title = (char_u *)_("Save File dialog");
3995 else
3996 title = (char_u *)_("Open File dialog");
3997 }
3998
3999 /* When no directory specified, use default file name, default dir, buffer
4000 * dir, last dir or current dir */
4001 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
4002 {
4003 if (mch_isdir(dflt)) /* default file name is a directory */
4004 {
4005 initdir = dflt;
4006 dflt = NULL;
4007 }
4008 else if (gettail(dflt) != dflt) /* default file name includes a path */
4009 {
4010 tofree = vim_strsave(dflt);
4011 if (tofree != NULL)
4012 {
4013 initdir = tofree;
4014 *gettail(initdir) = NUL;
4015 dflt = gettail(dflt);
4016 }
4017 }
4018 }
4019
4020 if (initdir == NULL || *initdir == NUL)
4021 {
4022 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004023 if (STRCMP(p_bsdir, "last") != 0
4024 && STRCMP(p_bsdir, "buffer") != 0
4025 && STRCMP(p_bsdir, "current") != 0
4026 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 initdir = p_bsdir;
4028 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004029 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 && buf != NULL && buf->b_ffname != NULL)
4031 {
4032 if (dflt == NULL || *dflt == NUL)
4033 dflt = gettail(curbuf->b_ffname);
4034 tofree = vim_strsave(curbuf->b_ffname);
4035 if (tofree != NULL)
4036 {
4037 initdir = tofree;
4038 *gettail(initdir) = NUL;
4039 }
4040 }
4041 /* When 'browsedir' is "last", use dir from last browse */
4042 else if (*p_bsdir == 'l')
4043 initdir = last_dir;
4044 /* When 'browsedir is "current", use current directory. This is the
4045 * default already, leave initdir empty. */
4046 }
4047
4048# ifdef FEAT_GUI
4049 if (gui.in_use) /* when this changes, also adjust f_has()! */
4050 {
4051 if (filter == NULL
4052# ifdef FEAT_EVAL
4053 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
4054 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
4055# endif
4056 )
4057 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004058 if (flags & BROWSE_DIR)
4059 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004060# if defined(FEAT_GUI_GTK) || defined(MSWIN)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004061 /* For systems that have a directory dialog. */
4062 fname = gui_mch_browsedir(title, initdir);
4063# else
4064 /* Generic solution for selecting a directory: select a file and
4065 * remove the file name. */
4066 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
4067# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004068# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004069 /* Win32 adds a dummy file name, others return an arbitrary file
4070 * name. GTK+ 2 returns only the directory, */
4071 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
4072 {
4073 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004074 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004075
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004076 if (tail == fname)
4077 *tail++ = '.'; /* use current dir */
4078 *tail = NUL;
4079 }
4080# endif
4081 }
4082 else
4083 fname = gui_mch_browse(flags & BROWSE_SAVE,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004084 title, dflt, ext, initdir, (char_u *)_(filter));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 /* We hang around in the dialog for a while, the user might do some
4087 * things to our files. The Win32 dialog allows deleting or renaming
4088 * a file, check timestamps. */
4089 need_check_timestamps = TRUE;
4090 did_check_timestamps = FALSE;
4091 }
4092 else
4093# endif
4094 {
4095 /* TODO: non-GUI file selector here */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004096 emsg(_("E338: Sorry, no file browser in console mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 fname = NULL;
4098 }
4099
4100 /* keep the directory for next time */
4101 if (fname != NULL)
4102 {
4103 vim_free(last_dir);
4104 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004105 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107 *gettail(last_dir) = NUL;
4108 if (*last_dir == NUL)
4109 {
4110 /* filename only returned, must be in current dir */
4111 vim_free(last_dir);
4112 last_dir = alloc(MAXPATHL);
4113 if (last_dir != NULL)
4114 mch_dirname(last_dir, MAXPATHL);
4115 }
4116 }
4117 }
4118
4119 vim_free(tofree);
4120 cmdmod.browse = save_browse;
4121
4122 return fname;
4123}
4124#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004125
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004126#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127static char *e_printf = N_("E766: Insufficient arguments for printf()");
4128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129/*
4130 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4131 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004132 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004133tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134{
4135 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004136 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004137 int err = FALSE;
4138
4139 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004140 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 else
4142 {
4143 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004144 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 if (err)
4146 n = 0;
4147 }
4148 return n;
4149}
4150
4151/*
4152 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004153 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004154 * are not converted to a string.
4155 * If "tofree" is not NULL echo_string() is used. All types are converted to
4156 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004157 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158 */
4159 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004160tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004162 int idx = *idxp - 1;
4163 char *s = NULL;
4164 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165
4166 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 else
4169 {
4170 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004171 if (tofree != NULL)
4172 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4173 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004174 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004175 }
4176 return s;
4177}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004178
4179# ifdef FEAT_FLOAT
4180/*
4181 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4182 */
4183 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004184tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004185{
4186 int idx = *idxp - 1;
4187 double f = 0;
4188
4189 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004190 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004191 else
4192 {
4193 ++*idxp;
4194 if (tvs[idx].v_type == VAR_FLOAT)
4195 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004196 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004197 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004198 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004199 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004200 }
4201 return f;
4202}
4203# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204#endif
4205
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004206#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004208 * Return the representation of infinity for printf() function:
4209 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4210 */
4211 static const char *
4212infinity_str(int positive,
4213 char fmt_spec,
4214 int force_sign,
4215 int space_for_positive)
4216{
4217 static const char *table[] =
4218 {
4219 "-inf", "inf", "+inf", " inf",
4220 "-INF", "INF", "+INF", " INF"
4221 };
4222 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4223
4224 if (ASCII_ISUPPER(fmt_spec))
4225 idx += 4;
4226 return table[idx];
4227}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004228#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004229
4230/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004231 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004232 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004233 * consistency.
4234 *
4235 * This code is based on snprintf.c - a portable implementation of snprintf
4236 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004237 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004238 * The original code, including useful comments, can be found here:
4239 * http://www.ijs.si/software/snprintf/
4240 *
4241 * This snprintf() only supports the following conversion specifiers:
4242 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4243 * with flags: '-', '+', ' ', '0' and '#'.
4244 * An asterisk is supported for field width as well as precision.
4245 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004246 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004247 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004248 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4249 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004250 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004251 * The locale is not used, the string is used as a byte string. This is only
4252 * relevant for double-byte encodings where the second byte may be '%'.
4253 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004254 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4255 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004256 *
4257 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004258 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004259 * is greater or equal to "str_m", not all characters from the result
4260 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4261 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004262 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004263 */
4264
4265/*
4266 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004268 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004269 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004270 */
4271
4272/* When generating prototypes all of this is skipped, cproto doesn't
4273 * understand this. */
4274#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004275
Bram Moolenaara800b422010-06-27 01:15:55 +02004276/* Like vim_vsnprintf() but append to the string. */
4277 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004278vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004279{
4280 va_list ap;
4281 int str_l;
4282 size_t len = STRLEN(str);
4283 size_t space;
4284
4285 if (str_m <= len)
4286 space = 0;
4287 else
4288 space = str_m - len;
4289 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004290 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004291 va_end(ap);
4292 return str_l;
4293}
Bram Moolenaara800b422010-06-27 01:15:55 +02004294
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004295 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004296vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004297{
4298 va_list ap;
4299 int str_l;
4300
4301 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004302 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303 va_end(ap);
4304 return str_l;
4305}
4306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004307 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004308vim_vsnprintf(
4309 char *str,
4310 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004311 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004312 va_list ap)
4313{
4314 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4315}
4316
4317 int
4318vim_vsnprintf_typval(
4319 char *str,
4320 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004321 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004322 va_list ap,
4323 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004324{
4325 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004326 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004327 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004328
4329 if (p == NULL)
4330 p = "";
4331 while (*p != NUL)
4332 {
4333 if (*p != '%')
4334 {
4335 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004336 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004337
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004338 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004339 if (str_l < str_m)
4340 {
4341 size_t avail = str_m - str_l;
4342
4343 mch_memmove(str + str_l, p, n > avail ? avail : n);
4344 }
4345 p += n;
4346 str_l += n;
4347 }
4348 else
4349 {
4350 size_t min_field_width = 0, precision = 0;
4351 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4352 int alternate_form = 0, force_sign = 0;
4353
4354 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4355 * ignored. */
4356 int space_for_positive = 1;
4357
4358 /* allowed values: \0, h, l, L */
4359 char length_modifier = '\0';
4360
4361 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004362# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004363# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004364 * That sounds reasonable to use as the maximum
4365 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004366# elif defined(FEAT_NUM64)
4367# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004368# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004369# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004370# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004371 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004372
4373 /* string address in case of string argument */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004374 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004375
4376 /* natural field width of arg without padding and sign */
4377 size_t str_arg_l;
4378
4379 /* unsigned char argument value - only defined for c conversion.
4380 * N.B. standard explicitly states the char argument for the c
4381 * conversion is unsigned */
4382 unsigned char uchar_arg;
4383
4384 /* number of zeros to be inserted for numeric conversions as
4385 * required by the precision or minimal field width */
4386 size_t number_of_zeros_to_pad = 0;
4387
4388 /* index into tmp where zero padding is to be inserted */
4389 size_t zero_padding_insertion_ind = 0;
4390
4391 /* current conversion specifier character */
4392 char fmt_spec = '\0';
4393
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004394 /* buffer for 's' and 'S' specs */
4395 char_u *tofree = NULL;
4396
4397
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004398 p++; /* skip '%' */
4399
4400 /* parse flags */
4401 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4402 || *p == '#' || *p == '\'')
4403 {
4404 switch (*p)
4405 {
4406 case '0': zero_padding = 1; break;
4407 case '-': justify_left = 1; break;
4408 case '+': force_sign = 1; space_for_positive = 0; break;
4409 case ' ': force_sign = 1;
4410 /* If both the ' ' and '+' flags appear, the ' '
4411 * flag should be ignored */
4412 break;
4413 case '#': alternate_form = 1; break;
4414 case '\'': break;
4415 }
4416 p++;
4417 }
4418 /* If the '0' and '-' flags both appear, the '0' flag should be
4419 * ignored. */
4420
4421 /* parse field width */
4422 if (*p == '*')
4423 {
4424 int j;
4425
4426 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004429 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430# endif
4431 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432 if (j >= 0)
4433 min_field_width = j;
4434 else
4435 {
4436 min_field_width = -j;
4437 justify_left = 1;
4438 }
4439 }
4440 else if (VIM_ISDIGIT((int)(*p)))
4441 {
4442 /* size_t could be wider than unsigned int; make sure we treat
4443 * argument like common implementations do */
4444 unsigned int uj = *p++ - '0';
4445
4446 while (VIM_ISDIGIT((int)(*p)))
4447 uj = 10 * uj + (unsigned int)(*p++ - '0');
4448 min_field_width = uj;
4449 }
4450
4451 /* parse precision */
4452 if (*p == '.')
4453 {
4454 p++;
4455 precision_specified = 1;
4456 if (*p == '*')
4457 {
4458 int j;
4459
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004461# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004462 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463# endif
4464 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004465 p++;
4466 if (j >= 0)
4467 precision = j;
4468 else
4469 {
4470 precision_specified = 0;
4471 precision = 0;
4472 }
4473 }
4474 else if (VIM_ISDIGIT((int)(*p)))
4475 {
4476 /* size_t could be wider than unsigned int; make sure we
4477 * treat argument like common implementations do */
4478 unsigned int uj = *p++ - '0';
4479
4480 while (VIM_ISDIGIT((int)(*p)))
4481 uj = 10 * uj + (unsigned int)(*p++ - '0');
4482 precision = uj;
4483 }
4484 }
4485
4486 /* parse 'h', 'l' and 'll' length modifiers */
4487 if (*p == 'h' || *p == 'l')
4488 {
4489 length_modifier = *p;
4490 p++;
4491 if (length_modifier == 'l' && *p == 'l')
4492 {
4493 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004494# ifdef FEAT_NUM64
4495 length_modifier = 'L';
4496# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004497 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004498# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004499 p++;
4500 }
4501 }
4502 fmt_spec = *p;
4503
4504 /* common synonyms: */
4505 switch (fmt_spec)
4506 {
4507 case 'i': fmt_spec = 'd'; break;
4508 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4509 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4510 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4511 default: break;
4512 }
4513
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004514# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4515 switch (fmt_spec)
4516 {
4517 case 'd': case 'u': case 'o': case 'x': case 'X':
4518 if (tvs != NULL && length_modifier == '\0')
4519 length_modifier = 'L';
4520 }
4521# endif
4522
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004523 /* get parameter value, do initial processing */
4524 switch (fmt_spec)
4525 {
4526 /* '%' and 'c' behave similar to 's' regarding flags and field
4527 * widths */
4528 case '%':
4529 case 'c':
4530 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004531 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 str_arg_l = 1;
4533 switch (fmt_spec)
4534 {
4535 case '%':
4536 str_arg = p;
4537 break;
4538
4539 case 'c':
4540 {
4541 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542
4543 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004544# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004545 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546# endif
4547 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004548 /* standard demands unsigned char */
4549 uchar_arg = (unsigned char)j;
4550 str_arg = (char *)&uchar_arg;
4551 break;
4552 }
4553
4554 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004555 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004558 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559# endif
4560 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004561 if (str_arg == NULL)
4562 {
4563 str_arg = "[NULL]";
4564 str_arg_l = 6;
4565 }
4566 /* make sure not to address string beyond the specified
4567 * precision !!! */
4568 else if (!precision_specified)
4569 str_arg_l = strlen(str_arg);
4570 /* truncate string if necessary as requested by precision */
4571 else if (precision == 0)
4572 str_arg_l = 0;
4573 else
4574 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004575 /* Don't put the #if inside memchr(), it can be a
4576 * macro. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004577 /* memchr on HP does not like n > 2^31 !!! */
4578 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004579 precision <= (size_t)0x7fffffffL ? precision
4580 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004581 str_arg_l = (q == NULL) ? precision
4582 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004583 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004584 if (fmt_spec == 'S')
4585 {
4586 if (min_field_width != 0)
4587 min_field_width += STRLEN(str_arg)
4588 - mb_string2cells((char_u *)str_arg, -1);
4589 if (precision)
4590 {
4591 char_u *p1 = (char_u *)str_arg;
4592 size_t i;
4593
4594 for (i = 0; i < precision && *p1; i++)
4595 p1 += mb_ptr2len(p1);
4596
4597 str_arg_l = precision = p1 - (char_u *)str_arg;
4598 }
4599 }
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
Bram Moolenaar4f974752019-02-17 17:44:42 +01004805# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004806 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;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004912 }
4913 }
4914 /* zero padding to specified precision? */
4915 if (num_of_digits < precision)
4916 number_of_zeros_to_pad = precision - num_of_digits;
4917 }
4918 /* zero padding to specified minimal field width? */
4919 if (!justify_left && zero_padding)
4920 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004921 int n = (int)(min_field_width - (str_arg_l
4922 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004923 if (n > 0)
4924 number_of_zeros_to_pad += n;
4925 }
4926 break;
4927 }
4928
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004929# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004930 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004931 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932 case 'e':
4933 case 'E':
4934 case 'g':
4935 case 'G':
4936 {
4937 /* Floating point. */
4938 double f;
4939 double abs_f;
4940 char format[40];
4941 int l;
4942 int remove_trailing_zeroes = FALSE;
4943
4944 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004945# if defined(FEAT_EVAL)
4946 tvs != NULL ? tv_float(tvs, &arg_idx) :
4947# endif
4948 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004949 abs_f = f < 0 ? -f : f;
4950
4951 if (fmt_spec == 'g' || fmt_spec == 'G')
4952 {
4953 /* Would be nice to use %g directly, but it prints
4954 * "1.0" as "1", we don't want that. */
4955 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4956 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004957 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004958 else
4959 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4960 remove_trailing_zeroes = TRUE;
4961 }
4962
Bram Moolenaar04186092016-08-29 21:55:35 +02004963 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004964# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004965 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004966# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004967 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004968# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004969 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004970 {
4971 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004972 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4973 force_sign, space_for_positive));
4974 str_arg_l = STRLEN(tmp);
4975 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004976 }
4977 else
4978 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004979 if (isnan(f))
4980 {
4981 /* Not a number: nan or NAN */
4982 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4983 : "nan");
4984 str_arg_l = 3;
4985 zero_padding = 0;
4986 }
4987 else if (isinf(f))
4988 {
4989 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4990 force_sign, space_for_positive));
4991 str_arg_l = STRLEN(tmp);
4992 zero_padding = 0;
4993 }
4994 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004995 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004996 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004997 format[0] = '%';
4998 l = 1;
4999 if (force_sign)
5000 format[l++] = space_for_positive ? ' ' : '+';
5001 if (precision_specified)
5002 {
5003 size_t max_prec = TMP_LEN - 10;
5004
5005 /* Make sure we don't get more digits than we
5006 * have room for. */
5007 if ((fmt_spec == 'f' || fmt_spec == 'F')
5008 && abs_f > 1.0)
5009 max_prec -= (size_t)log10(abs_f);
5010 if (precision > max_prec)
5011 precision = max_prec;
5012 l += sprintf(format + l, ".%d", (int)precision);
5013 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02005014 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02005015 format[l + 1] = NUL;
5016
Bram Moolenaare9997822016-08-28 16:03:38 +02005017 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01005018 }
Bram Moolenaar99922372016-08-27 15:26:35 +02005019
Bram Moolenaara7241f52008-06-24 20:39:31 +00005020 if (remove_trailing_zeroes)
5021 {
5022 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005023 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005024
5025 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02005026 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005027 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005028 else
5029 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005030 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005031 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005032 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005033 {
5034 /* Remove superfluous '+' and leading
5035 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005036 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005037 {
5038 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005039 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005040 --str_arg_l;
5041 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005042 i = (tp[1] == '-') ? 2 : 1;
5043 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005044 {
5045 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005046 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005047 --str_arg_l;
5048 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005049 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005050 }
5051 }
5052
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005053 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005054 /* Remove trailing zeroes, but keep the one
5055 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005056 while (tp > tmp + 2 && *tp == '0'
5057 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005058 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005059 STRMOVE(tp, tp + 1);
5060 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005061 --str_arg_l;
5062 }
5063 }
5064 else
5065 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005066 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005067
5068 /* Be consistent: some printf("%e") use 1.0e+12
5069 * and some 1.0e+012. Remove one zero in the last
5070 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005071 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005072 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005073 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5074 && tp[2] == '0'
5075 && vim_isdigit(tp[3])
5076 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005077 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005078 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005079 --str_arg_l;
5080 }
5081 }
5082 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005083 if (zero_padding && min_field_width > str_arg_l
5084 && (tmp[0] == '-' || force_sign))
5085 {
5086 /* padding 0's should be inserted after the sign */
5087 number_of_zeros_to_pad = min_field_width - str_arg_l;
5088 zero_padding_insertion_ind = 1;
5089 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005090 str_arg = tmp;
5091 break;
5092 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005093# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005094
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005095 default:
5096 /* unrecognized conversion specifier, keep format string
5097 * as-is */
5098 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005099 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005100 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005101 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005102
5103 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005104 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005105 str_arg = p;
5106 str_arg_l = 0;
5107 if (*p != NUL)
5108 str_arg_l++; /* include invalid conversion specifier
5109 unchanged if not at end-of-string */
5110 break;
5111 }
5112
5113 if (*p != NUL)
5114 p++; /* step over the just processed conversion specifier */
5115
5116 /* insert padding to the left as requested by min_field_width;
5117 * this does not include the zero padding in case of numerical
5118 * conversions*/
5119 if (!justify_left)
5120 {
5121 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005122 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005123
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005124 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005125 {
5126 if (str_l < str_m)
5127 {
5128 size_t avail = str_m - str_l;
5129
5130 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005131 (size_t)pn > avail ? avail
5132 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005133 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005134 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005135 }
5136 }
5137
5138 /* zero padding as requested by the precision or by the minimal
5139 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005140 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005141 {
5142 /* will not copy first part of numeric right now, *
5143 * force it to be copied later in its entirety */
5144 zero_padding_insertion_ind = 0;
5145 }
5146 else
5147 {
5148 /* insert first part of numerics (sign or '0x') before zero
5149 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005150 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005151
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005152 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005153 {
5154 if (str_l < str_m)
5155 {
5156 size_t avail = str_m - str_l;
5157
5158 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005159 (size_t)zn > avail ? avail
5160 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005161 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005162 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005163 }
5164
5165 /* insert zero padding as requested by the precision or min
5166 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005167 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005168 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005169 {
5170 if (str_l < str_m)
5171 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005172 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005173
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005174 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005175 (size_t)zn > avail ? avail
5176 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005177 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005178 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005179 }
5180 }
5181
5182 /* insert formatted string
5183 * (or as-is conversion specifier for unknown conversions) */
5184 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005185 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005186
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005187 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005188 {
5189 if (str_l < str_m)
5190 {
5191 size_t avail = str_m - str_l;
5192
5193 mch_memmove(str + str_l,
5194 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005195 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005196 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005197 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005198 }
5199 }
5200
5201 /* insert right padding */
5202 if (justify_left)
5203 {
5204 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005205 int pn = (int)(min_field_width
5206 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005207
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005208 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005209 {
5210 if (str_l < str_m)
5211 {
5212 size_t avail = str_m - str_l;
5213
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005214 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005215 (size_t)pn > avail ? avail
5216 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005217 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005218 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005219 }
5220 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005221 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005222 }
5223 }
5224
5225 if (str_m > 0)
5226 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005227 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005228 * overwriting the last character (shouldn't happen, but just in case)
5229 * */
5230 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5231 }
5232
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005233 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005234 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005236 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005237 * character), that is, the number of characters that would have been
5238 * written to the buffer if it were large enough. */
5239 return (int)str_l;
5240}
5241
5242#endif /* PROTO */