blob: a26a63798b50d7dc6394a82994fcb840ea336504 [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
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#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);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int msg_check_screen(void);
33static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010036static int confirm_msg_used = FALSE; // displaying confirm_msg
37static char_u *confirm_msg = NULL; // ":confirm" message
38static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010041#ifdef FEAT_JOB_CHANNEL
42static int emsg_to_channel_log = FALSE;
43#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020056static FILE *verbose_fd = NULL;
57static int verbose_did_open = FALSE;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059/*
60 * When writing messages to the screen, there are many different situations.
61 * A number of variables is used to remember the current state:
62 * msg_didany TRUE when messages were written since the last time the
63 * user reacted to a prompt.
64 * Reset: After hitting a key for the hit-return prompt,
65 * hitting <CR> for the command line or input().
66 * Set: When any message is written to the screen.
67 * msg_didout TRUE when something was written to the current line.
68 * Reset: When advancing to the next line, when the current
69 * text can be overwritten.
70 * Set: When any message is written to the screen.
71 * msg_nowait No extra delay for the last drawn message.
72 * Used in normal_cmd() before the mode message is drawn.
73 * emsg_on_display There was an error message recently. Indicates that there
74 * should be a delay before redrawing.
75 * msg_scroll The next message should not overwrite the current one.
76 * msg_scrolled How many lines the screen has been scrolled (because of
77 * messages). Used in update_screen() to scroll the screen
78 * back. Incremented each time the screen scrolls a line.
79 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
80 * writes something without scrolling should not make
81 * need_wait_return to be set. This is a hack to make ":ts"
82 * work without an extra prompt.
83 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010084 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 * need_wait_return TRUE when the hit-return prompt is needed.
86 * Reset: After giving the hit-return prompt, when the user
87 * has answered some other prompt.
88 * Set: When the ruler or typeahead display is overwritten,
89 * scrolling the screen for some message.
90 * keep_msg Message to be displayed after redrawing the screen, in
91 * main_loop().
92 * This is an allocated string or NULL when not used.
93 */
94
95/*
96 * msg(s) - displays the string 's' on the status line
97 * When terminal not initialized (yet) mch_errmsg(..) is used.
98 * return TRUE if wait_return not called
99 */
100 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100101msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 return msg_attr_keep(s, 0, FALSE);
104}
105
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106/*
107 * Like msg() but keep it silent when 'verbosefile' is set.
108 */
109 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100110verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111{
112 int n;
113
114 verbose_enter();
115 n = msg_attr_keep(s, 0, FALSE);
116 verbose_leave();
117
118 return n;
119}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100122msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 return msg_attr_keep(s, attr, FALSE);
125}
126
127 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100128msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100129 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100130 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100131 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 static int entered = 0;
134 int retval;
135 char_u *buf = NULL;
136
Bram Moolenaar85a20022019-12-21 18:25:54 +0100137 // Skip messages not matching ":filter pattern".
138 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100139 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200140 return TRUE;
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_EVAL
143 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100144 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
146
147 /*
148 * It is possible that displaying a messages causes a problem (e.g.,
149 * when redrawing the window), which causes another message, etc.. To
150 * break this loop, limit the recursiveness to 3 levels.
151 */
152 if (entered >= 3)
153 return TRUE;
154 ++entered;
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // Add message to history (unless it's a repeated kept message or a
157 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100158 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 || (*s != '<'
160 && last_msg_hist != NULL
161 && last_msg_hist->msg != NULL
162 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100163 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaar958dc692016-12-01 15:34:12 +0100165#ifdef FEAT_JOB_CHANNEL
166 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100167 // Write message in the channel log.
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200168 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100169#endif
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000172 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100173 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar32526b32019-01-19 17:43:09 +0100177 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 msg_clr_eos();
179 retval = msg_end();
180
Bram Moolenaar32526b32019-01-19 17:43:09 +0100181 if (keep && retval && vim_strsize((char_u *)s)
182 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
183 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185 vim_free(buf);
186 --entered;
187 return retval;
188}
189
190/*
191 * Truncate a string such that it can be printed without causing a scroll.
192 * Returns an allocated string or NULL when no truncating is done.
193 */
194 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100195msg_strtrunc(
196 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 char_u *buf = NULL;
200 int len;
201 int room;
202
Bram Moolenaar85a20022019-12-21 18:25:54 +0100203 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000204 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
205 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000208 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100209 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000210 room = (int)(Rows - msg_row) * Columns - 1;
211 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100212 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000213 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if (len > room && room > 0)
215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // may have up to 18 bytes per cell (6 per char, up to two
218 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100219 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100221 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100222 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = room + 2;
225 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229 }
230 return buf;
231}
232
233/*
234 * Truncate a string "s" to "buf" with cell width "room".
235 * "s" and "buf" may be equal.
236 */
237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100238trunc_string(
239 char_u *s,
240 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200241 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100242 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100244 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200245 size_t half;
246 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 int e;
248 int i;
249 int n;
250
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200251 if (room_in < 3)
252 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254
Bram Moolenaar85a20022019-12-21 18:25:54 +0100255 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100256 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
258 if (s[e] == NUL)
259 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100260 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 buf[e] = NUL;
262 return;
263 }
264 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200265 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 break;
267 len += n;
268 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000270 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100272 if (++e == buflen)
273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 buf[e] = s[e];
275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
277
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 if (enc_dbcs != 0)
281 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100282 // For DBCS going backwards in a string is slow, but
283 // computing the cell width isn't too slow: go forward
284 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 n = vim_strsize(s + i);
286 while (len + n > room)
287 {
288 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000289 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 }
291 }
292 else if (enc_utf8)
293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000295 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 for (;;)
297 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000298 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200299 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200300 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200302 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 break;
304 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200305 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307 }
308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100310 for (i = (int)STRLEN(s);
311 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 len += n;
313 }
314
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200315
316 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100317 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100318 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200319 if (s != buf)
320 {
321 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200322 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200323 len = buflen - 1;
324 len = len - e + 1;
325 if (len < 1)
326 buf[e - 1] = NUL;
327 else
328 mch_memmove(buf + e, s + e, len);
329 }
330 }
331 else if (e + 3 < buflen)
332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100333 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100334 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200335 len = STRLEN(s + i) + 1;
336 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100337 len = buflen - e - 3 - 1;
338 mch_memmove(buf + e + 3, s + i, len);
339 buf[e + 3 + len - 1] = NUL;
340 }
341 else
342 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100343 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200344 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346}
347
348/*
349 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100350 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 * shorter than IOSIZE!!!
352 */
353#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100355int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200360 if (IObuff == NULL)
361 {
362 // Very early in initialisation and already something wrong, just
363 // give the raw message so the user at least gets a hint.
364 return msg((char *)s);
365 }
366 else
367 {
368 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200370 va_start(arglist, s);
371 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
372 va_end(arglist);
373 return msg((char *)IObuff);
374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375}
376
377 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100378smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200380 if (IObuff == NULL)
381 {
382 // Very early in initialisation and already something wrong, just
383 // give the raw message so the user at least gets a hint.
384 return msg_attr((char *)s, attr);
385 }
386 else
387 {
388 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200390 va_start(arglist, s);
391 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
392 va_end(arglist);
393 return msg_attr((char *)IObuff, attr);
394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395}
396
Bram Moolenaare0429682018-07-01 16:44:03 +0200397 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100398smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200399{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200400 if (IObuff == NULL)
401 {
402 // Very early in initialisation and already something wrong, just
403 // give the raw message so the user at least gets a hint.
404 return msg_attr_keep((char *)s, attr, TRUE);
405 }
406 else
407 {
408 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200409
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200410 va_start(arglist, s);
411 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
412 va_end(arglist);
413 return msg_attr_keep((char *)IObuff, attr, TRUE);
414 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200415}
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#endif
418
419/*
420 * Remember the last sourcing name/lnum used in an error message, so that it
421 * isn't printed each time when it didn't change.
422 */
423static int last_sourcing_lnum = 0;
424static char_u *last_sourcing_name = NULL;
425
426/*
427 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
428 * for the next error message;
429 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000430 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100431reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100433 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 last_sourcing_lnum = 0;
435}
436
437/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100438 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000439 */
440 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100441other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000442{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100443 if (SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000444 {
445 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100446 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000447 return TRUE;
448 }
449 return FALSE;
450}
451
452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * Get the message about the source, as used for an error message.
454 * Returns an allocated string with room for one more character.
455 * Returns NULL when no message is to be given.
456 */
457 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100458get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 char_u *Buf, *p;
461
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100462 if (SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100464 char_u *sname = estack_sfile();
465 char_u *tofree = sname;
466
467 if (sname == NULL)
468 sname = SOURCING_NAME;
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100471 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100473 sprintf((char *)Buf, (char *)p, sname);
474 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 return Buf;
476 }
477 return NULL;
478}
479
480/*
481 * Get the message about the source lnum, as used for an error message.
482 * Returns an allocated string with room for one more character.
483 * Returns NULL when no message is to be given.
484 */
485 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100486get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
488 char_u *Buf, *p;
489
Bram Moolenaar85a20022019-12-21 18:25:54 +0100490 // lnum is 0 when executing a command from the command line
491 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100492 if (SOURCING_NAME != NULL
493 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
494 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200497 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100499 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 return Buf;
501 }
502 return NULL;
503}
504
505/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000506 * Display name and line number for the source of an error.
507 * Remember the file name and line number, so that for the next error the info
508 * is only displayed if it changed.
509 */
510 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100511msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000512{
513 char_u *p;
514
515 ++no_wait_return;
516 p = get_emsg_source();
517 if (p != NULL)
518 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100519 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000520 vim_free(p);
521 }
522 p = get_emsg_lnum();
523 if (p != NULL)
524 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100525 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000526 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100527 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000528 }
529
Bram Moolenaar85a20022019-12-21 18:25:54 +0100530 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100531 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000532 {
533 vim_free(last_sourcing_name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100534 if (SOURCING_NAME == NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000535 last_sourcing_name = NULL;
536 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100537 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000538 }
539 --no_wait_return;
540}
541
542/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000543 * Return TRUE if not giving error messages right now:
544 * If "emsg_off" is set: no error messages at the moment.
545 * If "msg" is in 'debug': do error message but without side effects.
546 * If "emsg_skip" is set: never do error messages.
547 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200548 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100549emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000550{
551 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
552 && vim_strchr(p_debug, 't') == NULL)
553#ifdef FEAT_EVAL
554 || emsg_skip > 0
555#endif
556 )
557 return TRUE;
558 return FALSE;
559}
560
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100561#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100562static garray_T ignore_error_list = GA_EMPTY;
563
564 void
565ignore_error_for_testing(char_u *error)
566{
567 if (ignore_error_list.ga_itemsize == 0)
568 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
569
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100570 if (STRCMP("RESET", error) == 0)
571 ga_clear_strings(&ignore_error_list);
572 else
573 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100574}
575
576 static int
577ignore_error(char_u *msg)
578{
579 int i;
580
581 for (i = 0; i < ignore_error_list.ga_len; ++i)
582 if (strstr((char *)msg,
583 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
584 return TRUE;
585 return FALSE;
586}
587#endif
588
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200589#if !defined(HAVE_STRERROR) || defined(PROTO)
590/*
591 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100592 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200593 */
594 void
595do_perror(char *msg)
596{
597 perror(msg);
598 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100599 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200600 --emsg_silent;
601}
602#endif
603
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000604/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100605 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 *
607 * Rings the bell, if appropriate, and calls message() to do the real work
608 * When terminal not initialized (yet) mch_errmsg(..) is used.
609 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100610 * Return TRUE if wait_return not called.
611 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100613 static int
614emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100618 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#ifdef FEAT_EVAL
620 int ignore = FALSE;
621 int severe;
622#endif
623
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100624#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100625 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100626 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100627 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100628 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100629#endif
630
Bram Moolenaar53989552019-12-23 22:59:18 +0100631 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100634 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
635 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 severe = emsg_severe;
637 emsg_severe = FALSE;
638#endif
639
Bram Moolenaar57657d82006-04-21 22:12:41 +0000640 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {
642#ifdef FEAT_EVAL
643 /*
644 * Cause a throw of an error exception if appropriate. Don't display
645 * the error message in this case. (If no matching catch clause will
646 * be found, the message will be displayed later on.) "ignore" is set
647 * when the message should be ignored completely (used for the
648 * interrupt message).
649 */
650 if (cause_errthrow(s, severe, &ignore) == TRUE)
651 {
652 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100653 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 return TRUE;
655 }
656
Bram Moolenaar85a20022019-12-21 18:25:54 +0100657 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 set_vim_var_string(VV_ERRMSG, s, -1);
659#endif
660
661 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000662 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 * But do write it to the redirection file.
664 */
665 if (emsg_silent != 0)
666 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200667 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200669 msg_start();
670 p = get_emsg_source();
671 if (p != NULL)
672 {
673 STRCAT(p, "\n");
674 redir_write(p, -1);
675 vim_free(p);
676 }
677 p = get_emsg_lnum();
678 if (p != NULL)
679 {
680 STRCAT(p, "\n");
681 redir_write(p, -1);
682 vim_free(p);
683 }
684 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100686#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200687 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 return TRUE;
690 }
691
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100692 ex_exitval = 1;
693
Bram Moolenaar85a20022019-12-21 18:25:54 +0100694 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 msg_silent = 0;
696 cmd_silent = FALSE;
697
Bram Moolenaar85a20022019-12-21 18:25:54 +0100698 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ++global_busy;
700
701 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100702 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200704 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100705 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200706#ifdef FEAT_EVAL
707 did_uncaught_emsg = TRUE;
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 emsg_on_display = TRUE; // remember there is an error message
712 ++msg_scroll; // don't overwrite a previous message
713 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000714 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100715 need_wait_return = TRUE; // needed in case emsg() is called after
716 // wait_return has reset need_wait_return
717 // and a redraw is expected because
718 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar958dc692016-12-01 15:34:12 +0100720#ifdef FEAT_JOB_CHANNEL
721 emsg_to_channel_log = TRUE;
722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 /*
724 * Display name and line number for the source of the error.
725 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000726 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
728 /*
729 * Display the error message itself.
730 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100731 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100732 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100733
734#ifdef FEAT_JOB_CHANNEL
735 emsg_to_channel_log = FALSE;
736#endif
737 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
740/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100741 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
743 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100746 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100747 if (!emsg_not_now())
748 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100749 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100752#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100753/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 * Print an error message with format string and variable arguments.
755 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100756 */
757 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100758semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100759{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200760 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100761 if (!emsg_not_now())
762 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200763 if (IObuff == NULL)
764 {
765 // Very early in initialisation and already something wrong, just
766 // give the raw message so the user at least gets a hint.
767 return emsg_core((char_u *)s);
768 }
769 else
770 {
771 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100772
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200773 va_start(ap, s);
774 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
775 va_end(ap);
776 return emsg_core(IObuff);
777 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200779 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100780}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100781#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100782
783/*
784 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
785 * defined. It is used for internal errors only, so that they can be
786 * detected when fuzzing vim.
787 */
788 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100789iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100790{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100791 if (!emsg_not_now())
792 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100793#ifdef ABORT_ON_INTERNAL_ERROR
794 abort();
795#endif
796}
797
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100798#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100800 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100801 * defined. It is used for internal errors only, so that they can be
802 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100804 */
805 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100808 if (!emsg_not_now())
809 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200810 if (IObuff == NULL)
811 {
812 // Very early in initialisation and already something wrong, just
813 // give the raw message so the user at least gets a hint.
814 emsg_core((char_u *)s);
815 }
816 else
817 {
818 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100819
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200820 va_start(ap, s);
821 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
822 va_end(ap);
823 emsg_core(IObuff);
824 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100825 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100826# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100827 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100828# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100829}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100830#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100831
832/*
833 * Give an "Internal error" message.
834 */
835 void
836internal_error(char *where)
837{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839}
840
Bram Moolenaardd589232020-02-29 17:38:12 +0100841/*
842 * Like internal_error() but do not call abort(), to avoid tests using
843 * test_unknown() and test_void() causing Vim to exit.
844 */
845 void
846internal_error_no_abort(char *where)
847{
848 semsg(_(e_intern2), where);
849}
850
Bram Moolenaar85a20022019-12-21 18:25:54 +0100851// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaardf177f62005-02-22 08:39:57 +0000853 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100854emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000855{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100856 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000857}
858
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 * Give an error message which contains %s for "name[len]".
861 */
862 void
863emsg_namelen(char *msg, char_u *name, int len)
864{
865 char_u *copy = vim_strnsave((char_u *)name, len);
866
867 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100868 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869}
870
871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 * Like msg(), but truncate to a single line if p_shm contains 't', or when
873 * "force" is TRUE. This truncates in another way as for normal messages.
874 * Careful: The string may be changed by msg_may_trunc()!
875 * Returns a pointer to the printed message, if wait_return() not called.
876 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100877 char *
878msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
880 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100881 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaar85a20022019-12-21 18:25:54 +0100883 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100884 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaar32526b32019-01-19 17:43:09 +0100886 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100889 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 msg_hist_off = FALSE;
891
892 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100893 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 return NULL;
895}
896
897/*
898 * Check if message "s" should be truncated at the start (for filenames).
899 * Return a pointer to where the truncated message starts.
900 * Note: May change the message by replacing a character with '<'.
901 */
902 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100903msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 int n;
906 int room;
907
908 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
909 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
910 && (n = (int)STRLEN(s) - room) > 0)
911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (has_mbyte)
913 {
914 int size = vim_strsize(s);
915
Bram Moolenaar85a20022019-12-21 18:25:54 +0100916 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000917 if (size <= room)
918 return s;
919
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 for (n = 0; size >= room; )
921 {
922 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000923 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925 --n;
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 s += n;
928 *s = '<';
929 }
930 return s;
931}
932
933 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100934add_msg_hist(
935 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100936 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100937 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 struct msg_hist *p;
940
941 if (msg_hist_off || msg_silent != 0)
942 return;
943
Bram Moolenaar85a20022019-12-21 18:25:54 +0100944 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000945 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000946 (void)delete_first_msg();
947
Bram Moolenaar85a20022019-12-21 18:25:54 +0100948 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200949 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 if (p != NULL)
951 {
952 if (len < 0)
953 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100954 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 while (len > 0 && *s == '\n')
956 {
957 ++s;
958 --len;
959 }
960 while (len > 0 && s[len - 1] == '\n')
961 --len;
962 p->msg = vim_strnsave(s, len);
963 p->next = NULL;
964 p->attr = attr;
965 if (last_msg_hist != NULL)
966 last_msg_hist->next = p;
967 last_msg_hist = p;
968 if (first_msg_hist == NULL)
969 first_msg_hist = last_msg_hist;
970 ++msg_hist_len;
971 }
972}
973
974/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000975 * Delete the first (oldest) message from the history.
976 * Returns FAIL if there are no messages.
977 */
978 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100979delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000980{
981 struct msg_hist *p;
982
983 if (msg_hist_len <= 0)
984 return FAIL;
985 p = first_msg_hist;
986 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000987 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100988 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000989 vim_free(p->msg);
990 vim_free(p);
991 --msg_hist_len;
992 return OK;
993}
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * ":messages" command.
997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200999ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
1001 struct msg_hist *p;
1002 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001003 int c = 0;
1004
1005 if (STRCMP(eap->arg, "clear") == 0)
1006 {
1007 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1008
1009 while (msg_hist_len > keep)
1010 (void)delete_first_msg();
1011 return;
1012 }
1013
1014 if (*eap->arg != NUL)
1015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001016 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001017 return;
1018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020 msg_hist_off = TRUE;
1021
Bram Moolenaar451f8492016-04-14 17:16:22 +02001022 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001023 if (eap->addr_count != 0)
1024 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001025 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001026 for (; p != NULL && !got_int; p = p->next)
1027 c++;
1028
1029 c -= eap->line2;
1030
Bram Moolenaar85a20022019-12-21 18:25:54 +01001031 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001032 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1033 p = p->next, c--);
1034 }
1035
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001036 if (p == first_msg_hist)
1037 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001038#ifdef FEAT_MULTI_LANG
1039 s = get_mess_lang();
1040#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001041 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001042#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001043 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001044 // The next comment is extracted by xgettext and put in po file for
1045 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001046 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001047 // Translator: Please replace the name and email address
1048 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001049 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001050 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001051 }
1052
Bram Moolenaar85a20022019-12-21 18:25:54 +01001053 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001054 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001056 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058 msg_hist_off = FALSE;
1059}
1060
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001061#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062/*
1063 * Call this after prompting the user. This will avoid a hit-return message
1064 * and a delay.
1065 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001066 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001067msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 need_wait_return = FALSE;
1070 emsg_on_display = FALSE;
1071 cmdline_row = msg_row;
1072 msg_col = 0;
1073 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001074 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075}
1076#endif
1077
1078/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001079 * Wait for the user to hit a key (normally Enter).
1080 * If "redraw" is TRUE, clear and redraw the screen.
1081 * If "redraw" is FALSE, just redraw the screen.
1082 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 */
1084 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001085wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
1087 int c;
1088 int oldState;
1089 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001091 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001092 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 if (redraw == TRUE)
1095 must_redraw = CLEAR;
1096
Bram Moolenaar85a20022019-12-21 18:25:54 +01001097 // If using ":silent cmd", don't wait for a return. Also don't set
1098 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (msg_silent != 0)
1100 return;
1101
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001102 /*
1103 * When inside vgetc(), we can't wait for a typed character at all.
1104 * With the global command (and some others) we only need one return at
1105 * the end. Adjust cmdline_row to avoid the next message overwriting the
1106 * last one.
1107 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001108 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001110 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (no_wait_return)
1112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 if (!exmode_active)
1114 cmdline_row = msg_row;
1115 return;
1116 }
1117
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 oldState = State;
1120 if (quit_more)
1121 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001122 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 quit_more = FALSE;
1124 got_int = FALSE;
1125 }
1126 else if (exmode_active)
1127 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 msg_puts(" "); // make sure the cursor is on the right line
1129 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 got_int = FALSE;
1131 }
1132 else
1133 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001134 // Make sure the hit-return prompt is on screen when 'guioptions' was
1135 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 screenalloc(FALSE);
1137
1138 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001141 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001143 cmdline_row = msg_row;
1144
Bram Moolenaar85a20022019-12-21 18:25:54 +01001145 // Avoid the sequence that the user types ":" at the hit-return prompt
1146 // to start an Ex command, but the file-changed dialog gets in the
1147 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001148 if (need_check_timestamps)
1149 check_timestamps(FALSE);
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 hit_return_msg();
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 do
1154 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001155 // Remember "got_int", if it is set vgetc() probably returns a
1156 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001158
Bram Moolenaar85a20022019-12-21 18:25:54 +01001159 // Don't do mappings here, we put the character back in the
1160 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001161 ++no_mapping;
1162 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001163
Bram Moolenaar85a20022019-12-21 18:25:54 +01001164 // Temporarily disable Recording. If Recording is active, the
1165 // character will be recorded later, since it will be added to the
1166 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001167 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001168 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001169 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001170 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001172 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001174 --no_mapping;
1175 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001176 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001177 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001180 // Strange way to allow copying (yanking) a modeless selection at
1181 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1182 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1184 {
1185 clip_copy_modeless_selection(TRUE);
1186 c = K_IGNORE;
1187 }
1188#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001189
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001190 /*
1191 * Allow scrolling back in the messages.
1192 * Also accept scroll-down commands when messages fill the screen,
1193 * to avoid that typing one 'j' too many makes the messages
1194 * disappear.
1195 */
1196 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001197 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001198 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1199 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001200 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001201 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001203 do_more_prompt(c);
1204 else
1205 {
1206 msg_didout = FALSE;
1207 c = K_IGNORE;
1208 msg_col =
1209#ifdef FEAT_RIGHTLEFT
1210 cmdmsg_rl ? Columns - 1 :
1211#endif
1212 0;
1213 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001214 if (quit_more)
1215 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001216 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001217 quit_more = FALSE;
1218 got_int = FALSE;
1219 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001220 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001221 {
1222 c = K_IGNORE;
1223 hit_return_msg();
1224 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001225 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001226 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001227 && (c == 'j' || c == 'd' || c == 'f'
1228 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001229 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 } while ((had_got_int && c == Ctrl_C)
1232 || c == K_IGNORE
1233#ifdef FEAT_GUI
1234 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1237 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1238 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001239 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001241 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 || (!mouse_has(MOUSE_RETURN)
1243 && mouse_row < msg_row
1244 && (c == K_LEFTMOUSE
1245 || c == K_MIDDLEMOUSE
1246 || c == K_RIGHTMOUSE
1247 || c == K_X1MOUSE
1248 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 );
1250 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 /*
1252 * Avoid that the mouse-up event causes visual mode to start.
1253 */
1254 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1255 || c == K_X1MOUSE || c == K_X2MOUSE)
1256 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001257 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001259 // Put the character back in the typeahead buffer. Don't use the
1260 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001261 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001262 do_redraw = TRUE; // need a redraw even though there is
1263 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266 redir_off = FALSE;
1267
1268 /*
1269 * If the user hits ':', '?' or '/' we get a command line from the next
1270 * line.
1271 */
1272 if (c == ':' || c == '?' || c == '/')
1273 {
1274 if (!exmode_active)
1275 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001276 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001278#ifdef FEAT_TERMINAL
1279 skip_term_loop = TRUE;
1280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282
1283 /*
1284 * If the window size changed set_shellsize() will redraw the screen.
1285 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1286 * typed.
1287 */
1288 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001289 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 msg_check();
1292
1293#if defined(UNIX) || defined(VMS)
1294 /*
1295 * When switching screens, we need to output an extra newline on exit.
1296 */
1297 if (swapping_screen() && !termcap_active)
1298 newline_on_exit = TRUE;
1299#endif
1300
1301 need_wait_return = FALSE;
1302 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 emsg_on_display = FALSE; // can delete error message now
1304 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 reset_last_sourcing();
1306 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1307 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001308 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
Bram Moolenaar85a20022019-12-21 18:25:54 +01001310 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001312 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 shell_resized();
1314 }
1315 else if (!skip_redraw
1316 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1317 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 redraw_later(VALID);
1320 }
1321}
1322
1323/*
1324 * Write the hit-return prompt.
1325 */
1326 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001327hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001329 int save_p_more = p_more;
1330
Bram Moolenaar85a20022019-12-21 18:25:54 +01001331 p_more = FALSE; // don't want see this message when scrolling back
1332 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 msg_putchar('\n');
1334 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001335 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar32526b32019-01-19 17:43:09 +01001337 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (!msg_use_printf())
1339 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001340 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341}
1342
1343/*
1344 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1345 */
1346 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001347set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 vim_free(keep_msg);
1350 if (s != NULL && msg_silent == 0)
1351 keep_msg = vim_strsave(s);
1352 else
1353 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001354 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001355 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356}
1357
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001358#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1359/*
1360 * If there currently is a message being displayed, set "keep_msg" to it, so
1361 * that it will be displayed again after redraw.
1362 */
1363 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001364set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001365{
1366 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1367 && (State & NORMAL))
1368 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1369}
1370#endif
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372/*
1373 * Prepare for outputting characters in the command line.
1374 */
1375 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001376msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 int did_return = FALSE;
1379
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001380 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001381 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001382
1383#ifdef FEAT_EVAL
1384 if (need_clr_eos)
1385 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001386 // Halfway an ":echo" command and getting an (error) message: clear
1387 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001388 need_clr_eos = FALSE;
1389 msg_clr_eos();
1390 }
1391#endif
1392
Bram Moolenaar85a20022019-12-21 18:25:54 +01001393 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
1395 msg_row = cmdline_row;
1396 msg_col =
1397#ifdef FEAT_RIGHTLEFT
1398 cmdmsg_rl ? Columns - 1 :
1399#endif
1400 0;
1401 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001402 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 {
1404 msg_putchar('\n');
1405 did_return = TRUE;
1406 if (exmode_active != EXMODE_NORMAL)
1407 cmdline_row = msg_row;
1408 }
1409 if (!msg_didany || lines_left < 0)
1410 msg_starthere();
1411 if (msg_silent == 0)
1412 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001413 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 cursor_off();
1415 }
1416
Bram Moolenaar85a20022019-12-21 18:25:54 +01001417 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (!did_return)
1419 redir_write((char_u *)"\n", -1);
1420}
1421
1422/*
1423 * Note that the current msg position is where messages start.
1424 */
1425 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001426msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 lines_left = cmdline_row;
1429 msg_didany = FALSE;
1430}
1431
1432 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001433msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 msg_putchar_attr(c, 0);
1436}
1437
1438 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001439msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001441 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443 if (IS_SPECIAL(c))
1444 {
1445 buf[0] = K_SPECIAL;
1446 buf[1] = K_SECOND(c);
1447 buf[2] = K_THIRD(c);
1448 buf[3] = NUL;
1449 }
1450 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001451 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001452 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453}
1454
1455 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001456msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001458 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaar32526b32019-01-19 17:43:09 +01001460 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 msg_puts(buf);
1462}
1463
1464 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001465msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 msg_home_replace_attr(fname, 0);
1468}
1469
1470#if defined(FEAT_FIND_ID) || defined(PROTO)
1471 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001472msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001474 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475}
1476#endif
1477
1478 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001479msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 char_u *name;
1482
1483 name = home_replace_save(NULL, fname);
1484 if (name != NULL)
1485 msg_outtrans_attr(name, attr);
1486 vim_free(name);
1487}
1488
1489/*
1490 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001491 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 * Use attributes 'attr'.
1493 * Return the number of characters it takes on the screen.
1494 */
1495 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001496msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 return msg_outtrans_attr(str, 0);
1499}
1500
1501 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001502msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1505}
1506
1507 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001508msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 return msg_outtrans_len_attr(str, len, 0);
1511}
1512
1513/*
1514 * Output one character at "p". Return pointer to the next character.
1515 * Handles multi-byte characters.
1516 */
1517 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001518msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 int l;
1521
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001522 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
1524 msg_outtrans_len_attr(p, l, attr);
1525 return p + l;
1526 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001527 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 return p + 1;
1529}
1530
1531 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001532msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
1534 int retval = 0;
1535 char_u *str = msgstr;
1536 char_u *plain_start = msgstr;
1537 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 int mb_l;
1539 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaar85a20022019-12-21 18:25:54 +01001541 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (attr & MSG_HIST)
1543 {
1544 add_msg_hist(str, len, attr);
1545 attr &= ~MSG_HIST;
1546 }
1547
Bram Moolenaar85a20022019-12-21 18:25:54 +01001548 // If the string starts with a composing character first draw a space on
1549 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001551 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
1553 /*
1554 * Go over the string. Special characters are translated and printed.
1555 * Normal characters are printed several at a time.
1556 */
1557 while (--len >= 0)
1558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001560 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001561 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001563 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 else
1565 mb_l = 1;
1566 if (has_mbyte && mb_l > 1)
1567 {
1568 c = (*mb_ptr2char)(str);
1569 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001570 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 retval += (*mb_ptr2cells)(str);
1572 else
1573 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001574 // unprintable multi-byte char: print the printable chars so
1575 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001577 msg_puts_attr_len((char *)plain_start,
1578 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001580 msg_puts_attr((char *)transchar(c),
1581 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 retval += char2cells(c);
1583 }
1584 len -= mb_l - 1;
1585 str += mb_l;
1586 }
1587 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 s = transchar_byte(*str);
1590 if (s[1] != NUL)
1591 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001592 // unprintable char: print the printable chars so far and the
1593 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001595 msg_puts_attr_len((char *)plain_start,
1596 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001598 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001599 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001601 else
1602 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 ++str;
1604 }
1605 }
1606
1607 if (str > plain_start)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001609 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 return retval;
1612}
1613
1614#if defined(FEAT_QUICKFIX) || defined(PROTO)
1615 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001616msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
1618 int i;
1619 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1620
1621 arg = skipwhite(arg);
1622 for (i = 5; *arg && i >= 0; --i)
1623 if (*arg++ != str[i])
1624 break;
1625 if (i < 0)
1626 {
1627 msg_putchar('\n');
1628 for (i = 0; rs[i]; ++i)
1629 msg_putchar(rs[i] - 3);
1630 }
1631}
1632#endif
1633
1634/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001635 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * Return the number of characters it takes on the screen.
1637 *
1638 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1639 * following character and shown as <F1>, <S-Up> etc. Any other character
1640 * which is not printable shown in <> form.
1641 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1642 * If a character is displayed in one of these special ways, is also
1643 * highlighted (its highlight name is '8' in the p_hl variable).
1644 * Otherwise characters are not highlighted.
1645 * This function is used to show mappings, where we want to see how to type
1646 * the character/string -- webb
1647 */
1648 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001649msg_outtrans_special(
1650 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001651 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001652 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 char_u *str = strstart;
1655 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001656 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 int attr;
1658 int len;
1659
Bram Moolenaar8820b482017-03-16 17:23:31 +01001660 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 while (*str != NUL)
1662 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001663 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if ((str == strstart || str[1] == NUL) && *str == ' ')
1665 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001666 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 ++str;
1668 }
1669 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001670 text = (char *)str2special(&str, from);
1671 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001672 if (maxlen > 0 && retval + len >= maxlen)
1673 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001674 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001675 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001676 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 retval += len;
1678 }
1679 return retval;
1680}
1681
Bram Moolenaarbd743252010-10-20 21:23:33 +02001682#if defined(FEAT_EVAL) || defined(PROTO)
1683/*
1684 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1685 * strings, in an allocated string.
1686 */
1687 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001688str2special_save(
1689 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001690 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001691{
1692 garray_T ga;
1693 char_u *p = str;
1694
1695 ga_init2(&ga, 1, 40);
1696 while (*p != NUL)
1697 ga_concat(&ga, str2special(&p, is_lhs));
1698 ga_append(&ga, NUL);
1699 return (char_u *)ga.ga_data;
1700}
1701#endif
1702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703/*
1704 * Return the printable string for the key codes at "*sp".
1705 * Used for translating the lhs or rhs of a mapping to printable chars.
1706 * Advances "sp" to the next code.
1707 */
1708 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001709str2special(
1710 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001711 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 int c;
1714 static char_u buf[7];
1715 char_u *str = *sp;
1716 int modifiers = 0;
1717 int special = FALSE;
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (has_mbyte)
1720 {
1721 char_u *p;
1722
Bram Moolenaar85a20022019-12-21 18:25:54 +01001723 // Try to un-escape a multi-byte character. Return the un-escaped
1724 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 p = mb_unescape(sp);
1726 if (p != NULL)
1727 return p;
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 c = *str;
1731 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1732 {
1733 if (str[1] == KS_MODIFIER)
1734 {
1735 modifiers = str[2];
1736 str += 3;
1737 c = *str;
1738 }
1739 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1740 {
1741 c = TO_SPECIAL(str[1], str[2]);
1742 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001744 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 special = TRUE;
1746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001748 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001750 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001751
Bram Moolenaar85a20022019-12-21 18:25:54 +01001752 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001753 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1754 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001755 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001756 *sp = str + 1;
1757 return buf;
1758 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001759 // Since 'special' is TRUE the multi-byte character 'c' will be
1760 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001761 c = (*mb_ptr2char)(str);
1762 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001764 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001765 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
Bram Moolenaar85a20022019-12-21 18:25:54 +01001767 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1768 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (special || char2cells(c) > 1 || (from && c == ' '))
1770 return get_special_key_name(c, modifiers);
1771 buf[0] = c;
1772 buf[1] = NUL;
1773 return buf;
1774}
1775
1776/*
1777 * Translate a key sequence into special key names.
1778 */
1779 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001780str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 char_u *s;
1783
1784 *buf = NUL;
1785 while (*sp)
1786 {
1787 s = str2special(&sp, FALSE);
1788 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1789 STRCAT(buf, s);
1790 }
1791}
1792
1793/*
1794 * print line for :print or :list command
1795 */
1796 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001797msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 int c;
1800 int col = 0;
1801 int n_extra = 0;
1802 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001803 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001804 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001806 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 int l;
1809 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaardf177f62005-02-22 08:39:57 +00001811 if (curwin->w_p_list)
1812 list = TRUE;
1813
Bram Moolenaar85a20022019-12-21 18:25:54 +01001814 // find start of trailing whitespace
Bram Moolenaardf177f62005-02-22 08:39:57 +00001815 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
1817 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001818 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 --trail;
1820 }
1821
Bram Moolenaar85a20022019-12-21 18:25:54 +01001822 // output a space for an empty line, otherwise the line will be
1823 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001824 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 msg_putchar(' ');
1826
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001827 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001829 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
1831 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001832 if (n_extra == 0 && c_final)
1833 c = c_final;
1834 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 c = c_extra;
1836 else
1837 c = *p_extra++;
1838 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001839 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001842 if (lcs_nbsp != NUL && list
1843 && (mb_ptr2char(s) == 160
1844 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001845 {
1846 mb_char2bytes(lcs_nbsp, buf);
1847 buf[(*mb_ptr2len)(buf)] = NUL;
1848 }
1849 else
1850 {
1851 mch_memmove(buf, s, (size_t)l);
1852 buf[l] = NUL;
1853 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001854 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 s += l;
1856 continue;
1857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 else
1859 {
1860 attr = 0;
1861 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001862 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001864 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001865#ifdef FEAT_VARTABS
1866 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1867 curbuf->b_p_vts_array) - 1;
1868#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001870#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001871 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
1873 c = ' ';
1874 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001875 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 else
1878 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001879 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001881 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001882 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001885 else if (c == 160 && list && lcs_nbsp != NUL)
1886 {
1887 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001888 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001889 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001890 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
1892 p_extra = (char_u *)"";
1893 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001894 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 n_extra = 1;
1896 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001897 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 --s;
1899 }
1900 else if (c != NUL && (n = byte2cells(c)) > 1)
1901 {
1902 n_extra = n - 1;
1903 p_extra = transchar_byte(c);
1904 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001905 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001907 // Use special coloring to be able to distinguish <hex> from
1908 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001909 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
1911 else if (c == ' ' && trail != NULL && s > trail)
1912 {
1913 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001914 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001916 else if (c == ' ' && list && lcs_space != NUL)
1917 {
1918 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001919 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922
1923 if (c == NUL)
1924 break;
1925
1926 msg_putchar_attr(c, attr);
1927 col++;
1928 }
1929 msg_clr_eos();
1930}
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932/*
1933 * Use screen_puts() to output one multi-byte character.
1934 * Return the pointer "s" advanced to the next character.
1935 */
1936 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001937screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938{
1939 int cw;
1940
Bram Moolenaar85a20022019-12-21 18:25:54 +01001941 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 cw = (*mb_ptr2cells)(s);
1943 if (cw > 1 && (
1944#ifdef FEAT_RIGHTLEFT
1945 cmdmsg_rl ? msg_col <= 1 :
1946#endif
1947 msg_col == Columns - 1))
1948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001949 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001950 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 return s;
1952 }
1953
1954 screen_puts_len(s, l, msg_row, msg_col, attr);
1955#ifdef FEAT_RIGHTLEFT
1956 if (cmdmsg_rl)
1957 {
1958 msg_col -= cw;
1959 if (msg_col == 0)
1960 {
1961 msg_col = Columns;
1962 ++msg_row;
1963 }
1964 }
1965 else
1966#endif
1967 {
1968 msg_col += cw;
1969 if (msg_col >= Columns)
1970 {
1971 msg_col = 0;
1972 ++msg_row;
1973 }
1974 }
1975 return s + l;
1976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978/*
1979 * Output a string to the screen at position msg_row, msg_col.
1980 * Update msg_row and msg_col for the next message.
1981 */
1982 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001983msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001985 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986}
1987
1988 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001989msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001991 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992}
1993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994/*
1995 * Show a message in such a way that it always fits in the line. Cut out a
1996 * part in the middle and replace it with "..." when necessary.
1997 * Does not handle multi-byte characters!
1998 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001999 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002000msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 int slen = len;
2003 int room;
2004
2005 room = Columns - msg_col;
2006 if (len > room && room >= 20)
2007 {
2008 slen = (room - 3) / 2;
2009 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002010 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2013}
2014
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002015 void
2016msg_outtrans_long_attr(char_u *longstr, int attr)
2017{
2018 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2019}
2020
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021/*
2022 * Basic function for writing a message with highlight attributes.
2023 */
2024 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002025msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
2027 msg_puts_attr_len(s, -1, attr);
2028}
2029
2030/*
2031 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2032 * When "maxlen" is -1 there is no maximum length.
2033 * When "maxlen" is >= 0 the message is not put in the history.
2034 */
2035 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002036msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 /*
2039 * If redirection is on, also write to the redirection file.
2040 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002041 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 /*
2044 * Don't print anything when using ":silent cmd".
2045 */
2046 if (msg_silent != 0)
2047 return;
2048
Bram Moolenaar85a20022019-12-21 18:25:54 +01002049 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if ((attr & MSG_HIST) && maxlen < 0)
2051 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002052 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 attr &= ~MSG_HIST;
2054 }
2055
Bram Moolenaare8070982019-10-12 17:07:06 +02002056 // When writing something to the screen after it has scrolled, requires a
2057 // wait-return prompt later. Needed when scrolling, resetting
2058 // need_wait_return after some prompt, and then outputting something
2059 // without scrolling
2060 // Not needed when only using CR to move the cursor.
2061 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002063 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 /*
2066 * If there is no valid screen, use fprintf so we can see error messages.
2067 * If termcap is not active, we may be writing in an alternate console
2068 * window, cursor positioning may not work correctly (window size may be
2069 * different, e.g. for Win32 console) or we just don't know where the
2070 * cursor is.
2071 */
2072 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002073 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002074 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002075 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002078/*
2079 * The display part of msg_puts_attr_len().
2080 * May be called recursively to display scroll-back text.
2081 */
2082 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002083msg_puts_display(
2084 char_u *str,
2085 int maxlen,
2086 int attr,
2087 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002088{
2089 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002090 char_u *t_s = str; // string from "t_s" to "s" is still todo
2091 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002092 int l;
2093 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002094 char_u *sb_str = str;
2095 int sb_col = msg_col;
2096 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002097 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002100 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002103 * We are at the end of the screen line when:
2104 * - When outputting a newline.
2105 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002107 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108#ifdef FEAT_RIGHTLEFT
2109 cmdmsg_rl
2110 ? (
2111 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002112 || (*s == TAB && msg_col <= 7)
2113 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 :
2115#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002116 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002119 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002121 /*
2122 * The screen is scrolled up when at the last row (some terminals
2123 * scroll automatically, some don't. To avoid problems we scroll
2124 * ourselves).
2125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002127 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002128 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
Bram Moolenaar85a20022019-12-21 18:25:54 +01002130 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (msg_no_more && lines_left == 0)
2132 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
Bram Moolenaar85a20022019-12-21 18:25:54 +01002134 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002135 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002138 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 msg_col = Columns - 1;
2140
Bram Moolenaar85a20022019-12-21 18:25:54 +01002141 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002142 if (*s >= ' '
2143#ifdef FEAT_RIGHTLEFT
2144 && !cmdmsg_rl
2145#endif
2146 )
2147 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148 if (has_mbyte)
2149 {
2150 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002152 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002153 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002154 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002155 s = screen_puts_mbyte(s, l, attr);
2156 }
2157 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002158 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002159 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002160 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002161 else
2162 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002163
2164 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002165 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002166 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2167
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002168 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002169 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 redraw_cmdline = TRUE;
2171 if (cmdline_row > 0 && !exmode_active)
2172 --cmdline_row;
2173
2174 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002175 * If screen is completely filled and 'more' is set then wait
2176 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002178 if (lines_left > 0)
2179 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002180 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 && !msg_no_more && !exmode_active)
2182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 if (do_more_prompt(NUL))
2185 s = confirm_msg_tail;
2186#else
2187 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188#endif
2189 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002190 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002192
Bram Moolenaar85a20022019-12-21 18:25:54 +01002193 // When we displayed a char in last column need to check if there
2194 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002195 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002196 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002199 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002202 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002203 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2204 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002205 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002206 t_puts(&t_col, t_s, s, attr);
2207
2208 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002209 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002210 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
Bram Moolenaar85a20022019-12-21 18:25:54 +01002212 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002214 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002215#ifdef FEAT_RIGHTLEFT
2216 if (cmdmsg_rl)
2217 msg_col = Columns - 1;
2218 else
2219#endif
2220 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002221 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 msg_row = Rows - 1;
2223 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 msg_col = 0;
2227 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 if (msg_col)
2231 --msg_col;
2232 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002233 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 do
2236 msg_screen_putchar(' ', attr);
2237 while (msg_col & 7);
2238 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002239 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002240 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 else
2242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (has_mbyte)
2244 {
2245 cw = (*mb_ptr2cells)(s);
2246 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002247 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002248 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002250 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
2252 else
2253 {
2254 cw = 1;
2255 l = 1;
2256 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002257
Bram Moolenaar85a20022019-12-21 18:25:54 +01002258 // When drawing from right to left or when a double-wide character
2259 // doesn't fit, draw a single character here. Otherwise collect
2260 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (
2262# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002263 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002265 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (l > 1)
2268 s = screen_puts_mbyte(s, l, attr) - 1;
2269 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 msg_screen_putchar(*s, attr);
2271 }
2272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002274 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (t_col == 0)
2276 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 t_col += cw;
2278 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280 }
2281 ++s;
2282 }
2283
Bram Moolenaar85a20022019-12-21 18:25:54 +01002284 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002286 t_puts(&t_col, t_s, s, attr);
2287 if (p_more && !recurse)
2288 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 msg_check();
2291}
2292
2293/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002294 * Return TRUE when ":filter pattern" was used and "msg" does not match
2295 * "pattern".
2296 */
2297 int
2298message_filtered(char_u *msg)
2299{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002300 int match;
2301
2302 if (cmdmod.filter_regmatch.regprog == NULL)
2303 return FALSE;
2304 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2305 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002306}
2307
2308/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002309 * Scroll the screen up one line for displaying the next message line.
2310 */
2311 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002312msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002313{
2314#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002315 // Remove the cursor before scrolling, ScreenLines[] is going
2316 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002317 if (gui.in_use)
2318 gui_undraw_cursor();
2319#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002320 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002321 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002322 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002323 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002324
2325 if (!can_clear((char_u *)" "))
2326 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002327 // Scrolling up doesn't result in the right background. Set the
2328 // background here. It's not efficient, but avoids that we have to do
2329 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002330 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2331
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 // Also clear the last char of the last but one line if it was not
2333 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002334 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2335 screen_fill((int)Rows - 2, (int)Rows - 1,
2336 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2337 }
2338}
2339
2340/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002341 * Increment "msg_scrolled".
2342 */
2343 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002344inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002345{
2346#ifdef FEAT_EVAL
2347 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2348 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002349 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002350 char_u *tofree = NULL;
2351 int len;
2352
Bram Moolenaar85a20022019-12-21 18:25:54 +01002353 // v:scrollstart is empty, set it to the script/function name and line
2354 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002355 if (p == NULL)
2356 p = (char_u *)_("Unknown");
2357 else
2358 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002359 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002360 tofree = alloc(len);
2361 if (tofree != NULL)
2362 {
2363 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002364 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002365 p = tofree;
2366 }
2367 }
2368 set_vim_var_string(VV_SCROLLSTART, p, -1);
2369 vim_free(tofree);
2370 }
2371#endif
2372 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002373 if (must_redraw < VALID)
2374 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002375}
2376
2377/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002378 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2379 * store the displayed text and remember where screen lines start.
2380 */
2381typedef struct msgchunk_S msgchunk_T;
2382struct msgchunk_S
2383{
2384 msgchunk_T *sb_next;
2385 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002386 char sb_eol; // TRUE when line ends after this text
2387 int sb_msg_col; // column in which text starts
2388 int sb_attr; // text attributes
2389 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002390};
2391
Bram Moolenaar85a20022019-12-21 18:25:54 +01002392static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002393
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002394static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002395
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002396typedef enum {
2397 SB_CLEAR_NONE = 0,
2398 SB_CLEAR_ALL,
2399 SB_CLEAR_CMDLINE_BUSY,
2400 SB_CLEAR_CMDLINE_DONE
2401} sb_clear_T;
2402
Bram Moolenaar85a20022019-12-21 18:25:54 +01002403// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002404static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002405
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002406/*
2407 * Store part of a printed message for displaying when scrolling back.
2408 */
2409 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002410store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002411 char_u **sb_str, // start of string
2412 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002413 int attr,
2414 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002415 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002416{
2417 msgchunk_T *mp;
2418
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002419 if (do_clear_sb_text == SB_CLEAR_ALL
2420 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002421 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002422 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2423 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002424 }
2425
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002426 if (s > *sb_str)
2427 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002428 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002429 if (mp != NULL)
2430 {
2431 mp->sb_eol = finish;
2432 mp->sb_msg_col = *sb_col;
2433 mp->sb_attr = attr;
2434 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2435
2436 if (last_msgchunk == NULL)
2437 {
2438 last_msgchunk = mp;
2439 mp->sb_prev = NULL;
2440 }
2441 else
2442 {
2443 mp->sb_prev = last_msgchunk;
2444 last_msgchunk->sb_next = mp;
2445 last_msgchunk = mp;
2446 }
2447 mp->sb_next = NULL;
2448 }
2449 }
2450 else if (finish && last_msgchunk != NULL)
2451 last_msgchunk->sb_eol = TRUE;
2452
2453 *sb_str = s;
2454 *sb_col = 0;
2455}
2456
2457/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002458 * Finished showing messages, clear the scroll-back text on the next message.
2459 */
2460 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002461may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002462{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002463 do_clear_sb_text = SB_CLEAR_ALL;
2464}
2465
2466/*
2467 * Starting to edit the command line, do not clear messages now.
2468 */
2469 void
2470sb_text_start_cmdline(void)
2471{
2472 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2473 msg_sb_eol();
2474}
2475
2476/*
2477 * Ending to edit the command line. Clear old lines but the last one later.
2478 */
2479 void
2480sb_text_end_cmdline(void)
2481{
2482 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002483}
2484
2485/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002486 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002487 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002488 * Called when redrawing the screen.
2489 */
2490 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002491clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002492{
2493 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002494 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002495
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002496 if (all)
2497 lastp = &last_msgchunk;
2498 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002499 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002500 if (last_msgchunk == NULL)
2501 return;
2502 lastp = &last_msgchunk->sb_prev;
2503 }
2504
2505 while (*lastp != NULL)
2506 {
2507 mp = (*lastp)->sb_prev;
2508 vim_free(*lastp);
2509 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002511}
2512
2513/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002514 * "g<" command.
2515 */
2516 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002517show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002518{
2519 msgchunk_T *mp;
2520
Bram Moolenaar85a20022019-12-21 18:25:54 +01002521 // Only show something if there is more than one line, otherwise it looks
2522 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002523 mp = msg_sb_start(last_msgchunk);
2524 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002525 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002526 else
2527 {
2528 do_more_prompt('G');
2529 wait_return(FALSE);
2530 }
2531}
2532
2533/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002534 * Move to the start of screen line in already displayed text.
2535 */
2536 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002537msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002538{
2539 msgchunk_T *mp = mps;
2540
2541 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2542 mp = mp->sb_prev;
2543 return mp;
2544}
2545
2546/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002547 * Mark the last message chunk as finishing the line.
2548 */
2549 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002550msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002551{
2552 if (last_msgchunk != NULL)
2553 last_msgchunk->sb_eol = TRUE;
2554}
2555
2556/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002557 * Display a screen line from previously displayed text at row "row".
2558 * Returns a pointer to the text for the next line (can be NULL).
2559 */
2560 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002561disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002562{
2563 msgchunk_T *mp = smp;
2564 char_u *p;
2565
2566 for (;;)
2567 {
2568 msg_row = row;
2569 msg_col = mp->sb_msg_col;
2570 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002571 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002572 ++p;
2573 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2574 if (mp->sb_eol || mp->sb_next == NULL)
2575 break;
2576 mp = mp->sb_next;
2577 }
2578 return mp->sb_next;
2579}
2580
2581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 * Output any postponed text for msg_puts_attr_len().
2583 */
2584 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002585t_puts(
2586 int *t_col,
2587 char_u *t_s,
2588 char_u *s,
2589 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002591 // output postponed text
2592 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002594 msg_col += *t_col;
2595 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002596 // If the string starts with a composing character don't increment the
2597 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2599 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (msg_col >= Columns)
2601 {
2602 msg_col = 0;
2603 ++msg_row;
2604 }
2605}
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607/*
2608 * Returns TRUE when messages should be printed with mch_errmsg().
2609 * This is used when there is no valid screen, so we can see error messages.
2610 * If termcap is not active, we may be writing in an alternate console
2611 * window, cursor positioning may not work correctly (window size may be
2612 * different, e.g. for Win32 console) or we just don't know where the
2613 * cursor is.
2614 */
2615 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002616msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002619#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2620# ifdef VIMDLL
2621 || (!gui.in_use && !termcap_active)
2622# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625#endif
2626 || (swapping_screen() && !termcap_active)
2627 );
2628}
2629
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002630/*
2631 * Print a message when there is no valid screen.
2632 */
2633 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002634msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002635{
2636 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002637 char_u *buf = NULL;
2638 char_u *p = s;
2639
Bram Moolenaar4f974752019-02-17 17:44:42 +01002640#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002641 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002642 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002643#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002644 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645 {
2646 if (!(silent_mode && p_verbose == 0))
2647 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002648 // NL --> CR NL translation (for Unix, not for "--version")
2649 if (*s == NL)
2650 {
2651 int n = (int)(s - p);
2652
2653 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002654 if (buf != NULL)
2655 {
2656 memcpy(buf, p, n);
2657 if (!info_message)
2658 buf[n++] = CAR;
2659 buf[n++] = NL;
2660 buf[n++] = NUL;
2661 if (info_message) // informative message, not an error
2662 mch_msg((char *)buf);
2663 else
2664 mch_errmsg((char *)buf);
2665 vim_free(buf);
2666 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002667 p = s + 1;
2668 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002669 }
2670
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002671 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002672#ifdef FEAT_RIGHTLEFT
2673 if (cmdmsg_rl)
2674 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002675 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002676 msg_col = Columns - 1;
2677 else
2678 --msg_col;
2679 }
2680 else
2681#endif
2682 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002683 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684 msg_col = 0;
2685 else
2686 ++msg_col;
2687 }
2688 ++s;
2689 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002690
2691 if (*p != NUL && !(silent_mode && p_verbose == 0))
2692 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002693 int c = -1;
2694
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002695 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002696 {
2697 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002698 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002699 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002700 if (info_message)
2701 mch_msg((char *)p);
2702 else
2703 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002704 if (c != -1)
2705 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002706 }
2707
2708 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002709
Bram Moolenaar4f974752019-02-17 17:44:42 +01002710#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002711 if (!(silent_mode && p_verbose == 0))
2712 mch_settmode(TMODE_RAW);
2713#endif
2714}
2715
2716/*
2717 * Show the more-prompt and handle the user response.
2718 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002719 * When at hit-enter prompt "typed_char" is the already typed character,
2720 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2722 */
2723 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002724do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002725{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002726 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 int used_typed_char = typed_char;
2728 int oldState = State;
2729 int c;
2730#ifdef FEAT_CON_DIALOG
2731 int retval = FALSE;
2732#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002733 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002734 msgchunk_T *mp_last = NULL;
2735 msgchunk_T *mp;
2736 int i;
2737
Bram Moolenaar85a20022019-12-21 18:25:54 +01002738 // We get called recursively when a timer callback outputs a message. In
2739 // that case don't show another prompt. Also when at the hit-Enter prompt
2740 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002741 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002742 return FALSE;
2743 entered = TRUE;
2744
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002745 if (typed_char == 'G')
2746 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002747 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002748 mp_last = msg_sb_start(last_msgchunk);
2749 for (i = 0; i < Rows - 2 && mp_last != NULL
2750 && mp_last->sb_prev != NULL; ++i)
2751 mp_last = msg_sb_start(mp_last->sb_prev);
2752 }
2753
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002754 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002756 if (typed_char == NUL)
2757 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002758 for (;;)
2759 {
2760 /*
2761 * Get a typed character directly from the user.
2762 */
2763 if (used_typed_char != NUL)
2764 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002765 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002766 used_typed_char = NUL;
2767 }
2768 else
2769 c = get_keystroke();
2770
2771#if defined(FEAT_MENU) && defined(FEAT_GUI)
2772 if (c == K_MENU)
2773 {
2774 int idx = get_menu_index(current_menu, ASKMORE);
2775
Bram Moolenaar85a20022019-12-21 18:25:54 +01002776 // Used a menu. If it starts with CTRL-Y, it must
2777 // be a "Copy" for the clipboard. Otherwise
2778 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002779 if (idx == MENU_INDEX_INVALID)
2780 continue;
2781 c = *current_menu->strings[idx];
2782 if (c != NUL && current_menu->strings[idx][1] != NUL)
2783 ins_typebuf(current_menu->strings[idx] + 1,
2784 current_menu->noremap[idx], 0, TRUE,
2785 current_menu->silent[idx]);
2786 }
2787#endif
2788
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002789 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002790 switch (c)
2791 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002792 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 case K_BS:
2794 case 'k':
2795 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002796 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 break;
2798
Bram Moolenaar85a20022019-12-21 18:25:54 +01002799 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002800 case NL:
2801 case 'j':
2802 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002803 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002804 break;
2805
Bram Moolenaar85a20022019-12-21 18:25:54 +01002806 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002807 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002808 break;
2809
Bram Moolenaar85a20022019-12-21 18:25:54 +01002810 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002811 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002812 break;
2813
Bram Moolenaar85a20022019-12-21 18:25:54 +01002814 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002815 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002816 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 break;
2818
Bram Moolenaar85a20022019-12-21 18:25:54 +01002819 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002820 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821 case K_PAGEDOWN:
2822 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002823 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002824 break;
2825
Bram Moolenaar85a20022019-12-21 18:25:54 +01002826 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002827 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002828 break;
2829
Bram Moolenaar85a20022019-12-21 18:25:54 +01002830 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002831 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002832 lines_left = 999999;
2833 break;
2834
Bram Moolenaar85a20022019-12-21 18:25:54 +01002835 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002836#ifdef FEAT_CON_DIALOG
2837 if (!confirm_msg_used)
2838#endif
2839 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002840 // Since got_int is set all typeahead will be flushed, but we
2841 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002843#ifdef FEAT_TERMINAL
2844 skip_term_loop = TRUE;
2845#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002846 cmdline_row = Rows - 1; // put ':' on this line
2847 skip_redraw = TRUE; // skip redraw once
2848 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002849 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002850 // FALLTHROUGH
2851 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002852 case Ctrl_C:
2853 case ESC:
2854#ifdef FEAT_CON_DIALOG
2855 if (confirm_msg_used)
2856 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002857 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002859 }
2860 else
2861#endif
2862 {
2863 got_int = TRUE;
2864 quit_more = TRUE;
2865 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002866 // When there is some more output (wrapping line) display that
2867 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002868 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002869 break;
2870
2871#ifdef FEAT_CLIPBOARD
2872 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002873 // Strange way to allow copying (yanking) a modeless
2874 // selection at the more prompt. Use CTRL-Y,
2875 // because the same is used in Cmdline-mode and at the
2876 // hit-enter prompt. However, scrolling one line up
2877 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 if (clip_star.state == SELECT_DONE)
2879 clip_copy_modeless_selection(TRUE);
2880 continue;
2881#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002882 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 msg_moremsg(TRUE);
2884 continue;
2885 }
2886
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002887 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002888 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002889 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002890 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002892 if (mp_last == NULL)
2893 mp = msg_sb_start(last_msgchunk);
2894 else if (mp_last->sb_prev != NULL)
2895 mp = msg_sb_start(mp_last->sb_prev);
2896 else
2897 mp = NULL;
2898
Bram Moolenaar85a20022019-12-21 18:25:54 +01002899 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2901 ++i)
2902 mp = msg_sb_start(mp->sb_prev);
2903
2904 if (mp != NULL && mp->sb_prev != NULL)
2905 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002906 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002907 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002908 {
2909 if (mp == NULL || mp->sb_prev == NULL)
2910 break;
2911 mp = msg_sb_start(mp->sb_prev);
2912 if (mp_last == NULL)
2913 mp_last = msg_sb_start(last_msgchunk);
2914 else
2915 mp_last = msg_sb_start(mp_last->sb_prev);
2916 }
2917
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002918 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002919 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002920 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002921 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002922 (void)disp_sb_line(0, mp);
2923 }
2924 else
2925 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002926 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002927 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002928 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2929 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002930 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002931 ++msg_scrolled;
2932 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002933 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002934 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002935 }
2936 }
2937 else
2938 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002939 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002940 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002941 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002942 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002943 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002944 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002945 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2946 (int)Columns, ' ', ' ', 0);
2947 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002948 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002949 }
2950 }
2951
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002952 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002953 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002954 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002955 screen_fill((int)Rows - 1, (int)Rows, 0,
2956 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 msg_moremsg(FALSE);
2958 continue;
2959 }
2960
Bram Moolenaar85a20022019-12-21 18:25:54 +01002961 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002962 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002963 }
2964
2965 break;
2966 }
2967
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002969 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2970 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002971 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002972 if (quit_more)
2973 {
2974 msg_row = Rows - 1;
2975 msg_col = 0;
2976 }
2977#ifdef FEAT_RIGHTLEFT
2978 else if (cmdmsg_rl)
2979 msg_col = Columns - 1;
2980#endif
2981
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002982 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002983#ifdef FEAT_CON_DIALOG
2984 return retval;
2985#else
2986 return FALSE;
2987#endif
2988}
2989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2991
2992#ifdef mch_errmsg
2993# undef mch_errmsg
2994#endif
2995#ifdef mch_msg
2996# undef mch_msg
2997#endif
2998
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002999#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3000 static void
3001mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003003 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003004 DWORD nwrite = 0;
3005 DWORD mode = 0;
3006 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3007
3008 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3009 && (int)GetConsoleCP() != enc_codepage)
3010 {
3011 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3012
3013 WriteConsoleW(h, w, len, &nwrite, NULL);
3014 vim_free(w);
3015 }
3016 else
3017 {
3018 fprintf(stderr, "%s", str);
3019 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003020}
3021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003023/*
3024 * Give an error message. To be used when the screen hasn't been initialized
3025 * yet. When stderr can't be used, collect error messages until the GUI has
3026 * started and they can be displayed in a message box.
3027 */
3028 void
3029mch_errmsg(char *str)
3030{
3031#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3032 int len;
3033#endif
3034
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003035#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003036 // On Unix use stderr if it's a tty.
3037 // When not going to start the GUI also use stderr.
3038 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003040# ifdef UNIX
3041# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003043# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 isatty(2)
3045# endif
3046# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003047 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003048# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003049# endif
3050# ifdef FEAT_GUI
3051 !(gui.in_use || gui.starting)
3052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 )
3054 {
3055 fprintf(stderr, "%s", str);
3056 return;
3057 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003060#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3061# ifdef VIMDLL
3062 if (!(gui.in_use || gui.starting))
3063# endif
3064 {
3065 mch_errmsg_c(str);
3066 return;
3067 }
3068#endif
3069
3070#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003071 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 emsg_on_display = FALSE;
3073
3074 len = (int)STRLEN(str) + 1;
3075 if (error_ga.ga_growsize == 0)
3076 {
3077 error_ga.ga_growsize = 80;
3078 error_ga.ga_itemsize = 1;
3079 }
3080 if (ga_grow(&error_ga, len) == OK)
3081 {
3082 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3083 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003084# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003085 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
3087 char_u *p;
3088
3089 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3090 for (;;)
3091 {
3092 p = vim_strchr(p, '\r');
3093 if (p == NULL)
3094 break;
3095 *p = ' ';
3096 }
3097 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003098# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003099 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003105#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3106 static void
3107mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003109 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003110 DWORD nwrite = 0;
3111 DWORD mode;
3112 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3113
3114
3115 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3116 && (int)GetConsoleCP() != enc_codepage)
3117 {
3118 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3119
3120 WriteConsoleW(h, w, len, &nwrite, NULL);
3121 vim_free(w);
3122 }
3123 else
3124 {
3125 printf("%s", str);
3126 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003127}
3128#endif
3129
3130/*
3131 * Give a message. To be used when the screen hasn't been initialized yet.
3132 * When there is no tty, collect messages until the GUI has started and they
3133 * can be displayed in a message box.
3134 */
3135 void
3136mch_msg(char *str)
3137{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003138#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003139 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3140 // uses mch_errmsg() when started from the desktop.
3141 // When not going to start the GUI also use stdout.
3142 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003144# ifdef UNIX
3145# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003147# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003151 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003153# endif
3154# ifdef FEAT_GUI
3155 !(gui.in_use || gui.starting)
3156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 )
3158 {
3159 printf("%s", str);
3160 return;
3161 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003162#endif
3163
3164#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3165# ifdef VIMDLL
3166 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003168 {
3169 mch_msg_c(str);
3170 return;
3171 }
3172#endif
3173#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003177#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179/*
3180 * Put a character on the screen at the current message position and advance
3181 * to the next position. Only for printable ASCII!
3182 */
3183 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003184msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003186 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 screen_putchar(c, msg_row, msg_col, attr);
3188#ifdef FEAT_RIGHTLEFT
3189 if (cmdmsg_rl)
3190 {
3191 if (--msg_col == 0)
3192 {
3193 msg_col = Columns;
3194 ++msg_row;
3195 }
3196 }
3197 else
3198#endif
3199 {
3200 if (++msg_col >= Columns)
3201 {
3202 msg_col = 0;
3203 ++msg_row;
3204 }
3205 }
3206}
3207
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003208 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003209msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003211 int attr;
3212 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaar8820b482017-03-16 17:23:31 +01003214 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003215 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003217 screen_puts((char_u *)
3218 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3219 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
3222/*
3223 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3224 * exmode_active.
3225 */
3226 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003227repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 if (State == ASKMORE)
3230 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003231 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 msg_row = Rows - 1;
3233 }
3234#ifdef FEAT_CON_DIALOG
3235 else if (State == CONFIRM)
3236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003237 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 msg_row = Rows - 1;
3239 }
3240#endif
3241 else if (State == EXTERNCMD)
3242 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003243 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245 else if (State == HITRETURN || State == SETWSIZE)
3246 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003247 if (msg_row == Rows - 1)
3248 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003249 // Avoid drawing the "hit-enter" prompt below the previous one,
3250 // overwrite it. Esp. useful when regaining focus and a
3251 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003252 msg_didout = FALSE;
3253 msg_col = 0;
3254 msg_clr_eos();
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 hit_return_msg();
3257 msg_row = Rows - 1;
3258 }
3259}
3260
3261/*
3262 * msg_check_screen - check if the screen is initialized.
3263 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3264 * While starting the GUI the terminal codes will be set for the GUI, but the
3265 * output goes to the terminal. Don't use the terminal codes then.
3266 */
3267 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003268msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 if (!full_screen || !screen_valid(FALSE))
3271 return FALSE;
3272
3273 if (msg_row >= Rows)
3274 msg_row = Rows - 1;
3275 if (msg_col >= Columns)
3276 msg_col = Columns - 1;
3277 return TRUE;
3278}
3279
3280/*
3281 * Clear from current message position to end of screen.
3282 * Skip this when ":silent" was used, no need to clear for redirection.
3283 */
3284 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003285msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 if (msg_silent == 0)
3288 msg_clr_eos_force();
3289}
3290
3291/*
3292 * Clear from current message position to end of screen.
3293 * Note: msg_col is not updated, so we remember the end of the message
3294 * for msg_check().
3295 */
3296 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003297msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 if (msg_use_printf())
3300 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003301 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
3303 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003304 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003306 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 }
3309 else
3310 {
3311#ifdef FEAT_RIGHTLEFT
3312 if (cmdmsg_rl)
3313 {
3314 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3315 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3316 }
3317 else
3318#endif
3319 {
3320 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3321 ' ', ' ', 0);
3322 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3323 }
3324 }
3325}
3326
3327/*
3328 * Clear the command line.
3329 */
3330 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003331msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 msg_row = cmdline_row;
3334 msg_col = 0;
3335 msg_clr_eos_force();
3336}
3337
3338/*
3339 * end putting a message on the screen
3340 * call wait_return if the message does not fit in the available space
3341 * return TRUE if wait_return not called.
3342 */
3343 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003344msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003347 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 * or the ruler option is set and we run into it,
3349 * we have to redraw the window.
3350 * Do not do this if we are abandoning the file or editing the command line.
3351 */
3352 if (!exiting && need_wait_return && !(State & CMDLINE))
3353 {
3354 wait_return(FALSE);
3355 return FALSE;
3356 }
3357 out_flush();
3358 return TRUE;
3359}
3360
3361/*
3362 * If the written message runs into the shown command or ruler, we have to
3363 * wait for hit-return and redraw the window later.
3364 */
3365 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003366msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 if (msg_row == Rows - 1 && msg_col >= sc_col)
3369 {
3370 need_wait_return = TRUE;
3371 redraw_cmdline = TRUE;
3372 }
3373}
3374
3375/*
3376 * May write a string to the redirection file.
3377 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3378 */
3379 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003380redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 char_u *s = str;
3383 static int cur_col = 0;
3384
Bram Moolenaar85a20022019-12-21 18:25:54 +01003385 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003386 if (redir_off)
3387 return;
3388
Bram Moolenaar85a20022019-12-21 18:25:54 +01003389 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003390 if (*p_vfile != NUL && verbose_fd == NULL)
3391 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003392
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003393 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003395 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (*s != '\n' && *s != '\r')
3397 {
3398 while (cur_col < msg_col)
3399 {
3400#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003401 if (redir_execute)
3402 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003403 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003405 else if (redir_vname)
3406 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003409 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003411 if (verbose_fd != NULL)
3412 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 ++cur_col;
3414 }
3415 }
3416
3417#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003418 if (redir_execute)
3419 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003420 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003422 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003423 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424#endif
3425
Bram Moolenaar85a20022019-12-21 18:25:54 +01003426 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3428 {
3429#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003430 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003432 if (redir_fd != NULL)
3433 putc(*s, redir_fd);
3434 if (verbose_fd != NULL)
3435 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (*s == '\r' || *s == '\n')
3437 cur_col = 0;
3438 else if (*s == '\t')
3439 cur_col += (8 - cur_col % 8);
3440 else
3441 ++cur_col;
3442 ++s;
3443 }
3444
Bram Moolenaar85a20022019-12-21 18:25:54 +01003445 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 msg_col = cur_col;
3447 }
3448}
3449
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003450 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003451redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003452{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003453 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003454#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003455 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003456#endif
3457 ;
3458}
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003461 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003462 * Must always be called paired with verbose_leave()!
3463 */
3464 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003465verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003466{
3467 if (*p_vfile != NUL)
3468 ++msg_silent;
3469}
3470
3471/*
3472 * After giving verbose message.
3473 * Must always be called paired with verbose_enter()!
3474 */
3475 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003476verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003477{
3478 if (*p_vfile != NUL)
3479 if (--msg_silent < 0)
3480 msg_silent = 0;
3481}
3482
3483/*
3484 * Like verbose_enter() and set msg_scroll when displaying the message.
3485 */
3486 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003487verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003488{
3489 if (*p_vfile != NUL)
3490 ++msg_silent;
3491 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003492 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003493 msg_scroll = TRUE;
3494}
3495
3496/*
3497 * Like verbose_leave() and set cmdline_row when displaying the message.
3498 */
3499 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003500verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003501{
3502 if (*p_vfile != NUL)
3503 {
3504 if (--msg_silent < 0)
3505 msg_silent = 0;
3506 }
3507 else
3508 cmdline_row = msg_row;
3509}
3510
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003511/*
3512 * Called when 'verbosefile' is set: stop writing to the file.
3513 */
3514 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003515verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003516{
3517 if (verbose_fd != NULL)
3518 {
3519 fclose(verbose_fd);
3520 verbose_fd = NULL;
3521 }
3522 verbose_did_open = FALSE;
3523}
3524
3525/*
3526 * Open the file 'verbosefile'.
3527 * Return FAIL or OK.
3528 */
3529 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003530verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003531{
3532 if (verbose_fd == NULL && !verbose_did_open)
3533 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003534 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003535 verbose_did_open = TRUE;
3536
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003537 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003538 if (verbose_fd == NULL)
3539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003540 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003541 return FAIL;
3542 }
3543 }
3544 return OK;
3545}
3546
3547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 * Give a warning message (for searching).
3549 * Use 'w' highlighting and may repeat the message after redrawing
3550 */
3551 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003552give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003554 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (msg_silent != 0)
3556 return;
3557
Bram Moolenaar85a20022019-12-21 18:25:54 +01003558 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#ifdef FEAT_EVAL
3562 set_vim_var_string(VV_WARNINGMSG, message, -1);
3563#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003564 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003566 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 else
3568 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003569 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003570 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003571 msg_didout = FALSE; // overwrite this message
3572 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 --no_wait_return;
3576}
3577
Bram Moolenaar113e1072019-01-20 15:30:40 +01003578#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003579 void
3580give_warning2(char_u *message, char_u *a1, int hl)
3581{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003582 if (IObuff == NULL)
3583 {
3584 // Very early in initialisation and already something wrong, just give
3585 // the raw message so the user at least gets a hint.
3586 give_warning((char_u *)message, hl);
3587 }
3588 else
3589 {
3590 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3591 give_warning(IObuff, hl);
3592 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003593}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003594#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596/*
3597 * Advance msg cursor to column "col".
3598 */
3599 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003600msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003602 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003604 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 return;
3606 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003607 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003609#ifdef FEAT_RIGHTLEFT
3610 if (cmdmsg_rl)
3611 while (msg_col > Columns - col)
3612 msg_putchar(' ');
3613 else
3614#endif
3615 while (msg_col < col)
3616 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617}
3618
3619#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3620/*
3621 * Used for "confirm()" function, and the :confirm command prefix.
3622 * Versions which haven't got flexible dialogs yet, and console
3623 * versions, get this generic handler which uses the command line.
3624 *
3625 * type = one of:
3626 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3627 * title = title string (can be NULL for default)
3628 * (neither used in console dialogs at the moment)
3629 *
3630 * Format of the "buttons" string:
3631 * "Button1Name\nButton2Name\nButton3Name"
3632 * The first button should normally be the default/accept
3633 * The second button should be the 'Cancel' button
3634 * Other buttons- use your imagination!
3635 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3636 * different letter.
3637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003639do_dialog(
3640 int type UNUSED,
3641 char_u *title UNUSED,
3642 char_u *message,
3643 char_u *buttons,
3644 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003645 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3646 // otherwise
3647 int ex_cmd) // when TRUE pressing : accepts default and starts
3648 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
3650 int oldState;
3651 int retval = 0;
3652 char_u *hotkeys;
3653 int c;
3654 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003655 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
3657#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003658 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003660 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661#endif
3662
3663#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003664 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3666 {
3667 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003668 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003669 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003670 need_wait_return = FALSE;
3671 emsg_on_display = FALSE;
3672 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar85a20022019-12-21 18:25:54 +01003674 // Flush output to avoid that further messages and redrawing is done
3675 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 out_flush();
3677 gui_mch_update();
3678
3679 return c;
3680 }
3681#endif
3682
3683 oldState = State;
3684 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar27321db2020-07-06 21:24:57 +02003687 // Ensure raw mode here.
3688 save_tmode = cur_tmode;
3689 settmode(TMODE_RAW);
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 /*
3692 * Since we wait for a keypress, don't make the
3693 * user press RETURN as well afterwards.
3694 */
3695 ++no_wait_return;
3696 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3697
3698 if (hotkeys != NULL)
3699 {
3700 for (;;)
3701 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003702 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 c = get_keystroke();
3704 switch (c)
3705 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003706 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 case NL:
3708 retval = dfltbutton;
3709 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003710 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 case ESC:
3712 retval = 0;
3713 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003714 default: // Could be a hotkey?
3715 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003717 if (c == ':' && ex_cmd)
3718 {
3719 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003720 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003721 break;
3722 }
3723
Bram Moolenaar85a20022019-12-21 18:25:54 +01003724 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 c = MB_TOLOWER(c);
3726 retval = 1;
3727 for (i = 0; hotkeys[i]; ++i)
3728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (has_mbyte)
3730 {
3731 if ((*mb_ptr2char)(hotkeys + i) == c)
3732 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003733 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (hotkeys[i] == c)
3737 break;
3738 ++retval;
3739 }
3740 if (hotkeys[i])
3741 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003742 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 continue;
3744 }
3745 break;
3746 }
3747
3748 vim_free(hotkeys);
3749 }
3750
Bram Moolenaar27321db2020-07-06 21:24:57 +02003751 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 --no_wait_return;
3755 msg_end_prompt();
3756
3757 return retval;
3758}
3759
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760/*
3761 * Copy one character from "*from" to "*to", taking care of multi-byte
3762 * characters. Return the length of the character in bytes.
3763 */
3764 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003765copy_char(
3766 char_u *from,
3767 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003768 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 int len;
3771 int c;
3772
3773 if (has_mbyte)
3774 {
3775 if (lowercase)
3776 {
3777 c = MB_TOLOWER((*mb_ptr2char)(from));
3778 return (*mb_char2bytes)(c, to);
3779 }
3780 else
3781 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003782 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 mch_memmove(to, from, (size_t)len);
3784 return len;
3785 }
3786 }
3787 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
3789 if (lowercase)
3790 *to = (char_u)TOLOWER_LOC(*from);
3791 else
3792 *to = *from;
3793 return 1;
3794 }
3795}
3796
3797/*
3798 * Format the dialog string, and display it at the bottom of
3799 * the screen. Return a string of hotkey chars (if defined) for
3800 * each 'button'. If a button has no hotkey defined, the first character of
3801 * the button is used.
3802 * The hotkeys can be multi-byte characters, but without combining chars.
3803 *
3804 * Returns an allocated string with hotkeys, or NULL for error.
3805 */
3806 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003807msg_show_console_dialog(
3808 char_u *message,
3809 char_u *buttons,
3810 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003813#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003814 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 char_u *hotk = NULL;
3816 char_u *msgp = NULL;
3817 char_u *hotkp = NULL;
3818 char_u *r;
3819 int copy;
3820#define HAS_HOTKEY_LEN 30
3821 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003822 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 int idx;
3824
3825 has_hotkey[0] = FALSE;
3826
3827 /*
3828 * First loop: compute the size of memory to allocate.
3829 * Second loop: copy to the allocated memory.
3830 */
3831 for (copy = 0; copy <= 1; ++copy)
3832 {
3833 r = buttons;
3834 idx = 0;
3835 while (*r)
3836 {
3837 if (*r == DLG_BUTTON_SEP)
3838 {
3839 if (copy)
3840 {
3841 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003842 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
Bram Moolenaar85a20022019-12-21 18:25:54 +01003844 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003846 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003849 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (dfltbutton)
3851 --dfltbutton;
3852
Bram Moolenaar85a20022019-12-21 18:25:54 +01003853 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3855 first_hotkey = TRUE;
3856 }
3857 else
3858 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003859 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3860 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (idx < HAS_HOTKEY_LEN - 1)
3862 has_hotkey[++idx] = FALSE;
3863 }
3864 }
3865 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3866 {
3867 if (*r == DLG_HOTKEY_CHAR)
3868 ++r;
3869 first_hotkey = FALSE;
3870 if (copy)
3871 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003872 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 *msgp++ = *r;
3874 else
3875 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003876 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3878 msgp += copy_char(r, msgp, FALSE);
3879 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3880
Bram Moolenaar85a20022019-12-21 18:25:54 +01003881 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003882 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884 }
3885 else
3886 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003887 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (idx < HAS_HOTKEY_LEN - 1)
3889 has_hotkey[idx] = TRUE;
3890 }
3891 }
3892 else
3893 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003894 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (copy)
3896 msgp += copy_char(r, msgp, FALSE);
3897 }
3898
Bram Moolenaar85a20022019-12-21 18:25:54 +01003899 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003900 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
3903 if (copy)
3904 {
3905 *msgp++ = ':';
3906 *msgp++ = ' ';
3907 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 else
3910 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003911 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003912 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003913 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003914 + 3); // for the ": " and NUL
3915 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaar85a20022019-12-21 18:25:54 +01003917 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (!has_hotkey[0])
3919 {
3920 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003921 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923
3924 /*
3925 * Now allocate and load the strings
3926 */
3927 vim_free(confirm_msg);
3928 confirm_msg = alloc(len);
3929 if (confirm_msg == NULL)
3930 return NULL;
3931 *confirm_msg = NUL;
3932 hotk = alloc(lenhotkey);
3933 if (hotk == NULL)
3934 return NULL;
3935
3936 *confirm_msg = '\n';
3937 STRCPY(confirm_msg + 1, message);
3938
3939 msgp = confirm_msg + 1 + STRLEN(message);
3940 hotkp = hotk;
3941
Bram Moolenaar85a20022019-12-21 18:25:54 +01003942 // Define first default hotkey. Keep the hotkey string NUL
3943 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003944 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
Bram Moolenaar85a20022019-12-21 18:25:54 +01003946 // Remember where the choices start, displaying starts here when
3947 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 confirm_msg_tail = msgp;
3949 *msgp++ = '\n';
3950 }
3951 }
3952
3953 display_confirm_msg();
3954 return hotk;
3955}
3956
3957/*
3958 * Display the ":confirm" message. Also called when screen resized.
3959 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003960 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003961display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003963 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 ++confirm_msg_used;
3965 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003966 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 --confirm_msg_used;
3968}
3969
Bram Moolenaar85a20022019-12-21 18:25:54 +01003970#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
3972#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3973
3974 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003975vim_dialog_yesno(
3976 int type,
3977 char_u *title,
3978 char_u *message,
3979 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 if (do_dialog(type,
3982 title == NULL ? (char_u *)_("Question") : title,
3983 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003984 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return VIM_YES;
3986 return VIM_NO;
3987}
3988
3989 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003990vim_dialog_yesnocancel(
3991 int type,
3992 char_u *title,
3993 char_u *message,
3994 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
3996 switch (do_dialog(type,
3997 title == NULL ? (char_u *)_("Question") : title,
3998 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003999 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
4001 case 1: return VIM_YES;
4002 case 2: return VIM_NO;
4003 }
4004 return VIM_CANCEL;
4005}
4006
4007 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004008vim_dialog_yesnoallcancel(
4009 int type,
4010 char_u *title,
4011 char_u *message,
4012 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 switch (do_dialog(type,
4015 title == NULL ? (char_u *)"Question" : title,
4016 message,
4017 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004018 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
4020 case 1: return VIM_YES;
4021 case 2: return VIM_NO;
4022 case 3: return VIM_ALL;
4023 case 4: return VIM_DISCARDALL;
4024 }
4025 return VIM_CANCEL;
4026}
4027
Bram Moolenaar85a20022019-12-21 18:25:54 +01004028#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004030#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004031static char *e_printf = N_("E766: Insufficient arguments for printf()");
4032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004033/*
4034 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4035 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004036 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004037tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004038{
4039 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004040 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041 int err = FALSE;
4042
4043 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004044 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004045 else
4046 {
4047 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004048 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004049 if (err)
4050 n = 0;
4051 }
4052 return n;
4053}
4054
4055/*
4056 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004057 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004058 * are not converted to a string.
4059 * If "tofree" is not NULL echo_string() is used. All types are converted to
4060 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004061 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 */
4063 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004064tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004066 int idx = *idxp - 1;
4067 char *s = NULL;
4068 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069
4070 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004071 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 else
4073 {
4074 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004075 if (tofree != NULL)
4076 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4077 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004078 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004079 }
4080 return s;
4081}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004082
4083# ifdef FEAT_FLOAT
4084/*
4085 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4086 */
4087 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004088tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004089{
4090 int idx = *idxp - 1;
4091 double f = 0;
4092
4093 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004094 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004095 else
4096 {
4097 ++*idxp;
4098 if (tvs[idx].v_type == VAR_FLOAT)
4099 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004100 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004101 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004102 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004103 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004104 }
4105 return f;
4106}
4107# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004108#endif
4109
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004110#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004111/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004112 * Return the representation of infinity for printf() function:
4113 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4114 */
4115 static const char *
4116infinity_str(int positive,
4117 char fmt_spec,
4118 int force_sign,
4119 int space_for_positive)
4120{
4121 static const char *table[] =
4122 {
4123 "-inf", "inf", "+inf", " inf",
4124 "-INF", "INF", "+INF", " INF"
4125 };
4126 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4127
4128 if (ASCII_ISUPPER(fmt_spec))
4129 idx += 4;
4130 return table[idx];
4131}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004132#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004133
4134/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004135 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004136 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004137 * consistency.
4138 *
4139 * This code is based on snprintf.c - a portable implementation of snprintf
4140 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004141 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004142 * The original code, including useful comments, can be found here:
4143 * http://www.ijs.si/software/snprintf/
4144 *
4145 * This snprintf() only supports the following conversion specifiers:
4146 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4147 * with flags: '-', '+', ' ', '0' and '#'.
4148 * An asterisk is supported for field width as well as precision.
4149 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004150 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004151 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004152 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004153 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004154 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004155 * The locale is not used, the string is used as a byte string. This is only
4156 * relevant for double-byte encodings where the second byte may be '%'.
4157 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004158 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4159 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004160 *
4161 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004162 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004163 * is greater or equal to "str_m", not all characters from the result
4164 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4165 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004166 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004167 */
4168
4169/*
4170 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004172 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004173 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004174 */
4175
Bram Moolenaar85a20022019-12-21 18:25:54 +01004176// When generating prototypes all of this is skipped, cproto doesn't
4177// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004178#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004179
Bram Moolenaar85a20022019-12-21 18:25:54 +01004180// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004181 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004182vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004183{
4184 va_list ap;
4185 int str_l;
4186 size_t len = STRLEN(str);
4187 size_t space;
4188
4189 if (str_m <= len)
4190 space = 0;
4191 else
4192 space = str_m - len;
4193 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004194 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004195 va_end(ap);
4196 return str_l;
4197}
Bram Moolenaara800b422010-06-27 01:15:55 +02004198
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004199 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004200vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004201{
4202 va_list ap;
4203 int str_l;
4204
4205 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004206 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207 va_end(ap);
4208 return str_l;
4209}
4210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004211 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004212vim_vsnprintf(
4213 char *str,
4214 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004215 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004216 va_list ap)
4217{
4218 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4219}
4220
4221 int
4222vim_vsnprintf_typval(
4223 char *str,
4224 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004225 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004226 va_list ap,
4227 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004228{
4229 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004230 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004231 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004232
4233 if (p == NULL)
4234 p = "";
4235 while (*p != NUL)
4236 {
4237 if (*p != '%')
4238 {
4239 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004240 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004241
Bram Moolenaar85a20022019-12-21 18:25:54 +01004242 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004243 if (str_l < str_m)
4244 {
4245 size_t avail = str_m - str_l;
4246
4247 mch_memmove(str + str_l, p, n > avail ? avail : n);
4248 }
4249 p += n;
4250 str_l += n;
4251 }
4252 else
4253 {
4254 size_t min_field_width = 0, precision = 0;
4255 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4256 int alternate_form = 0, force_sign = 0;
4257
Bram Moolenaar85a20022019-12-21 18:25:54 +01004258 // If both the ' ' and '+' flags appear, the ' ' flag should be
4259 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004260 int space_for_positive = 1;
4261
Bram Moolenaar85a20022019-12-21 18:25:54 +01004262 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004263 char length_modifier = '\0';
4264
Bram Moolenaar85a20022019-12-21 18:25:54 +01004265 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004266# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004267# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4268 // That sounds reasonable to use as the maximum
4269 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004270# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004271# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004272# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004273 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004274
Bram Moolenaar85a20022019-12-21 18:25:54 +01004275 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004276 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004277
Bram Moolenaar85a20022019-12-21 18:25:54 +01004278 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004279 size_t str_arg_l;
4280
Bram Moolenaar85a20022019-12-21 18:25:54 +01004281 // unsigned char argument value - only defined for c conversion.
4282 // N.B. standard explicitly states the char argument for the c
4283 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004284 unsigned char uchar_arg;
4285
Bram Moolenaar85a20022019-12-21 18:25:54 +01004286 // number of zeros to be inserted for numeric conversions as
4287 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004288 size_t number_of_zeros_to_pad = 0;
4289
Bram Moolenaar85a20022019-12-21 18:25:54 +01004290 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004291 size_t zero_padding_insertion_ind = 0;
4292
Bram Moolenaar85a20022019-12-21 18:25:54 +01004293 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004294 char fmt_spec = '\0';
4295
Bram Moolenaar85a20022019-12-21 18:25:54 +01004296 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004297 char_u *tofree = NULL;
4298
4299
Bram Moolenaar85a20022019-12-21 18:25:54 +01004300 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004301
Bram Moolenaar85a20022019-12-21 18:25:54 +01004302 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4304 || *p == '#' || *p == '\'')
4305 {
4306 switch (*p)
4307 {
4308 case '0': zero_padding = 1; break;
4309 case '-': justify_left = 1; break;
4310 case '+': force_sign = 1; space_for_positive = 0; break;
4311 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004312 // If both the ' ' and '+' flags appear, the ' '
4313 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004314 break;
4315 case '#': alternate_form = 1; break;
4316 case '\'': break;
4317 }
4318 p++;
4319 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004320 // If the '0' and '-' flags both appear, the '0' flag should be
4321 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004322
Bram Moolenaar85a20022019-12-21 18:25:54 +01004323 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004324 if (*p == '*')
4325 {
4326 int j;
4327
4328 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004331 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332# endif
4333 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004334 if (j >= 0)
4335 min_field_width = j;
4336 else
4337 {
4338 min_field_width = -j;
4339 justify_left = 1;
4340 }
4341 }
4342 else if (VIM_ISDIGIT((int)(*p)))
4343 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004344 // size_t could be wider than unsigned int; make sure we treat
4345 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004346 unsigned int uj = *p++ - '0';
4347
4348 while (VIM_ISDIGIT((int)(*p)))
4349 uj = 10 * uj + (unsigned int)(*p++ - '0');
4350 min_field_width = uj;
4351 }
4352
Bram Moolenaar85a20022019-12-21 18:25:54 +01004353 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004354 if (*p == '.')
4355 {
4356 p++;
4357 precision_specified = 1;
4358 if (*p == '*')
4359 {
4360 int j;
4361
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004362 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004364 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365# endif
4366 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004367 p++;
4368 if (j >= 0)
4369 precision = j;
4370 else
4371 {
4372 precision_specified = 0;
4373 precision = 0;
4374 }
4375 }
4376 else if (VIM_ISDIGIT((int)(*p)))
4377 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004378 // size_t could be wider than unsigned int; make sure we
4379 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004380 unsigned int uj = *p++ - '0';
4381
4382 while (VIM_ISDIGIT((int)(*p)))
4383 uj = 10 * uj + (unsigned int)(*p++ - '0');
4384 precision = uj;
4385 }
4386 }
4387
Bram Moolenaar85a20022019-12-21 18:25:54 +01004388 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004389 if (*p == 'h' || *p == 'l')
4390 {
4391 length_modifier = *p;
4392 p++;
4393 if (length_modifier == 'l' && *p == 'l')
4394 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004395 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004396 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004397 p++;
4398 }
4399 }
4400 fmt_spec = *p;
4401
Bram Moolenaar85a20022019-12-21 18:25:54 +01004402 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403 switch (fmt_spec)
4404 {
4405 case 'i': fmt_spec = 'd'; break;
4406 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4407 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4408 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4409 default: break;
4410 }
4411
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004412# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004413 switch (fmt_spec)
4414 {
4415 case 'd': case 'u': case 'o': case 'x': case 'X':
4416 if (tvs != NULL && length_modifier == '\0')
4417 length_modifier = 'L';
4418 }
4419# endif
4420
Bram Moolenaar85a20022019-12-21 18:25:54 +01004421 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004422 switch (fmt_spec)
4423 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004424 // '%' and 'c' behave similar to 's' regarding flags and field
4425 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004426 case '%':
4427 case 'c':
4428 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004429 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004430 str_arg_l = 1;
4431 switch (fmt_spec)
4432 {
4433 case '%':
4434 str_arg = p;
4435 break;
4436
4437 case 'c':
4438 {
4439 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440
4441 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004443 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444# endif
4445 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004446 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004447 uchar_arg = (unsigned char)j;
4448 str_arg = (char *)&uchar_arg;
4449 break;
4450 }
4451
4452 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004453 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004456 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457# endif
4458 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004459 if (str_arg == NULL)
4460 {
4461 str_arg = "[NULL]";
4462 str_arg_l = 6;
4463 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004464 // make sure not to address string beyond the specified
4465 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004466 else if (!precision_specified)
4467 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004468 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004469 else if (precision == 0)
4470 str_arg_l = 0;
4471 else
4472 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004473 // Don't put the #if inside memchr(), it can be a
4474 // macro.
4475 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004476 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004477 precision <= (size_t)0x7fffffffL ? precision
4478 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004479 str_arg_l = (q == NULL) ? precision
4480 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004481 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004482 if (fmt_spec == 'S')
4483 {
4484 if (min_field_width != 0)
4485 min_field_width += STRLEN(str_arg)
4486 - mb_string2cells((char_u *)str_arg, -1);
4487 if (precision)
4488 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004489 char_u *p1;
4490 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004491
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004492 for (p1 = (char_u *)str_arg; *p1;
4493 p1 += mb_ptr2len(p1))
4494 {
4495 i += (size_t)mb_ptr2cells(p1);
4496 if (i > precision)
4497 break;
4498 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004499 str_arg_l = precision = p1 - (char_u *)str_arg;
4500 }
4501 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004502 break;
4503
4504 default:
4505 break;
4506 }
4507 break;
4508
Bram Moolenaar91984b92016-08-16 21:58:41 +02004509 case 'd': case 'u':
4510 case 'b': case 'B':
4511 case 'o':
4512 case 'x': case 'X':
4513 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004514 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004515 // NOTE: the u, b, o, x, X and p conversion specifiers
4516 // imply the value is unsigned; d implies a signed
4517 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518
Bram Moolenaar85a20022019-12-21 18:25:54 +01004519 // 0 if numeric argument is zero (or if pointer is
4520 // NULL for 'p'), +1 if greater than zero (or nonzero
4521 // for unsigned arguments), -1 if negative (unsigned
4522 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004523 int arg_sign = 0;
4524
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004525 // only set for length modifier h, or for no length
4526 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 int int_arg = 0;
4528 unsigned int uint_arg = 0;
4529
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004530 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004531 long int long_arg = 0;
4532 unsigned long int ulong_arg = 0;
4533
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004534 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004535 varnumber_T llong_arg = 0;
4536 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004537
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004538 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004539 uvarnumber_T bin_arg = 0;
4540
Bram Moolenaar85a20022019-12-21 18:25:54 +01004541 // pointer argument value -only defined for p
4542 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004543 void *ptr_arg = NULL;
4544
4545 if (fmt_spec == 'p')
4546 {
4547 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004549# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004550 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4551 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552# endif
4553 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004554 if (ptr_arg != NULL)
4555 arg_sign = 1;
4556 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004557 else if (fmt_spec == 'b' || fmt_spec == 'B')
4558 {
4559 bin_arg =
4560# if defined(FEAT_EVAL)
4561 tvs != NULL ?
4562 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4563# endif
4564 va_arg(ap, uvarnumber_T);
4565 if (bin_arg != 0)
4566 arg_sign = 1;
4567 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004568 else if (fmt_spec == 'd')
4569 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004570 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004571 switch (length_modifier)
4572 {
4573 case '\0':
4574 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004575 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004578 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579# endif
4580 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004581 if (int_arg > 0)
4582 arg_sign = 1;
4583 else if (int_arg < 0)
4584 arg_sign = -1;
4585 break;
4586 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004588# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004589 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590# endif
4591 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004592 if (long_arg > 0)
4593 arg_sign = 1;
4594 else if (long_arg < 0)
4595 arg_sign = -1;
4596 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004597 case 'L':
4598 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004599# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004600 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004601# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004602 va_arg(ap, varnumber_T);
4603 if (llong_arg > 0)
4604 arg_sign = 1;
4605 else if (llong_arg < 0)
4606 arg_sign = -1;
4607 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004608 }
4609 }
4610 else
4611 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004612 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004613 switch (length_modifier)
4614 {
4615 case '\0':
4616 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004619 tvs != NULL ? (unsigned)
4620 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621# endif
4622 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004623 if (uint_arg != 0)
4624 arg_sign = 1;
4625 break;
4626 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004628# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004629 tvs != NULL ? (unsigned long)
4630 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004631# endif
4632 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004633 if (ulong_arg != 0)
4634 arg_sign = 1;
4635 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004636 case 'L':
4637 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004638# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004639 tvs != NULL ? (uvarnumber_T)
4640 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004641# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004642 va_arg(ap, uvarnumber_T);
4643 if (ullong_arg != 0)
4644 arg_sign = 1;
4645 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004646 }
4647 }
4648
4649 str_arg = tmp;
4650 str_arg_l = 0;
4651
Bram Moolenaar85a20022019-12-21 18:25:54 +01004652 // NOTE:
4653 // For d, i, u, o, x, and X conversions, if precision is
4654 // specified, the '0' flag should be ignored. This is so
4655 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4656 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004657 if (precision_specified)
4658 zero_padding = 0;
4659 if (fmt_spec == 'd')
4660 {
4661 if (force_sign && arg_sign >= 0)
4662 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004663 // leave negative numbers for sprintf to handle, to
4664 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004665 }
4666 else if (alternate_form)
4667 {
4668 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004669 && (fmt_spec == 'b' || fmt_spec == 'B'
4670 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004671 {
4672 tmp[str_arg_l++] = '0';
4673 tmp[str_arg_l++] = fmt_spec;
4674 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004675 // alternate form should have no effect for p
4676 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004677 }
4678
4679 zero_padding_insertion_ind = str_arg_l;
4680 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004681 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004682 if (precision == 0 && arg_sign == 0)
4683 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004684 // When zero value is formatted with an explicit
4685 // precision 0, the resulting formatted string is
4686 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004687 }
4688 else
4689 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004690 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004691 int f_l = 0;
4692
Bram Moolenaar85a20022019-12-21 18:25:54 +01004693 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004694 f[f_l++] = '%';
4695 if (!length_modifier)
4696 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004697 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004698 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004699# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004700 f[f_l++] = 'I';
4701 f[f_l++] = '6';
4702 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004703# else
4704 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004705 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004706# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004707 }
4708 else
4709 f[f_l++] = length_modifier;
4710 f[f_l++] = fmt_spec;
4711 f[f_l++] = '\0';
4712
4713 if (fmt_spec == 'p')
4714 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004715 else if (fmt_spec == 'b' || fmt_spec == 'B')
4716 {
4717 char b[8 * sizeof(uvarnumber_T)];
4718 size_t b_l = 0;
4719 uvarnumber_T bn = bin_arg;
4720
4721 do
4722 {
4723 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4724 bn >>= 1;
4725 }
4726 while (bn != 0);
4727
4728 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4729 str_arg_l += b_l;
4730 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004731 else if (fmt_spec == 'd')
4732 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004733 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004734 switch (length_modifier)
4735 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004736 case '\0': str_arg_l += sprintf(
4737 tmp + str_arg_l, f,
4738 int_arg);
4739 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004740 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004741 tmp + str_arg_l, f,
4742 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 break;
4744 case 'l': str_arg_l += sprintf(
4745 tmp + str_arg_l, f, long_arg);
4746 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004747 case 'L': str_arg_l += sprintf(
4748 tmp + str_arg_l, f, llong_arg);
4749 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004750 }
4751 }
4752 else
4753 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004754 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004755 switch (length_modifier)
4756 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004757 case '\0': str_arg_l += sprintf(
4758 tmp + str_arg_l, f,
4759 uint_arg);
4760 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004761 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004762 tmp + str_arg_l, f,
4763 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004764 break;
4765 case 'l': str_arg_l += sprintf(
4766 tmp + str_arg_l, f, ulong_arg);
4767 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004768 case 'L': str_arg_l += sprintf(
4769 tmp + str_arg_l, f, ullong_arg);
4770 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004771 }
4772 }
4773
Bram Moolenaar85a20022019-12-21 18:25:54 +01004774 // include the optional minus sign and possible
4775 // "0x" in the region before the zero padding
4776 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004777 if (zero_padding_insertion_ind < str_arg_l
4778 && tmp[zero_padding_insertion_ind] == '-')
4779 zero_padding_insertion_ind++;
4780 if (zero_padding_insertion_ind + 1 < str_arg_l
4781 && tmp[zero_padding_insertion_ind] == '0'
4782 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4783 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4784 zero_padding_insertion_ind += 2;
4785 }
4786
4787 {
4788 size_t num_of_digits = str_arg_l
4789 - zero_padding_insertion_ind;
4790
4791 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004792 // unless zero is already the first
4793 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004794 && !(zero_padding_insertion_ind < str_arg_l
4795 && tmp[zero_padding_insertion_ind] == '0'))
4796 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004797 // assure leading zero for alternate-form
4798 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 if (!precision_specified
4800 || precision < num_of_digits + 1)
4801 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004802 // precision is increased to force the
4803 // first character to be zero, except if a
4804 // zero value is formatted with an
4805 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004806 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004807 }
4808 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004809 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004810 if (num_of_digits < precision)
4811 number_of_zeros_to_pad = precision - num_of_digits;
4812 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004813 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004814 if (!justify_left && zero_padding)
4815 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004816 int n = (int)(min_field_width - (str_arg_l
4817 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004818 if (n > 0)
4819 number_of_zeros_to_pad += n;
4820 }
4821 break;
4822 }
4823
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004824# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004825 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004826 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004827 case 'e':
4828 case 'E':
4829 case 'g':
4830 case 'G':
4831 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004832 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004833 double f;
4834 double abs_f;
4835 char format[40];
4836 int l;
4837 int remove_trailing_zeroes = FALSE;
4838
4839 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004840# if defined(FEAT_EVAL)
4841 tvs != NULL ? tv_float(tvs, &arg_idx) :
4842# endif
4843 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004844 abs_f = f < 0 ? -f : f;
4845
4846 if (fmt_spec == 'g' || fmt_spec == 'G')
4847 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004848 // Would be nice to use %g directly, but it prints
4849 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004850 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4851 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004852 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004853 else
4854 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4855 remove_trailing_zeroes = TRUE;
4856 }
4857
Bram Moolenaar04186092016-08-29 21:55:35 +02004858 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004859# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004860 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004861# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004862 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004863# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004864 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004865 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004866 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004867 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4868 force_sign, space_for_positive));
4869 str_arg_l = STRLEN(tmp);
4870 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004871 }
4872 else
4873 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004874 if (isnan(f))
4875 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004876 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004877 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4878 : "nan");
4879 str_arg_l = 3;
4880 zero_padding = 0;
4881 }
4882 else if (isinf(f))
4883 {
4884 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4885 force_sign, space_for_positive));
4886 str_arg_l = STRLEN(tmp);
4887 zero_padding = 0;
4888 }
4889 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004890 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004891 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004892 format[0] = '%';
4893 l = 1;
4894 if (force_sign)
4895 format[l++] = space_for_positive ? ' ' : '+';
4896 if (precision_specified)
4897 {
4898 size_t max_prec = TMP_LEN - 10;
4899
Bram Moolenaar85a20022019-12-21 18:25:54 +01004900 // Make sure we don't get more digits than we
4901 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004902 if ((fmt_spec == 'f' || fmt_spec == 'F')
4903 && abs_f > 1.0)
4904 max_prec -= (size_t)log10(abs_f);
4905 if (precision > max_prec)
4906 precision = max_prec;
4907 l += sprintf(format + l, ".%d", (int)precision);
4908 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004909 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004910 format[l + 1] = NUL;
4911
Bram Moolenaare9997822016-08-28 16:03:38 +02004912 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004913 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004914
Bram Moolenaara7241f52008-06-24 20:39:31 +00004915 if (remove_trailing_zeroes)
4916 {
4917 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004918 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004919
Bram Moolenaar85a20022019-12-21 18:25:54 +01004920 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004921 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004922 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004923 else
4924 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004925 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004926 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004927 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004928 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004929 // Remove superfluous '+' and leading
4930 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004931 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004933 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004934 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004935 --str_arg_l;
4936 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004937 i = (tp[1] == '-') ? 2 : 1;
4938 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004939 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004940 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004941 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004942 --str_arg_l;
4943 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004944 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004945 }
4946 }
4947
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004948 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004949 // Remove trailing zeroes, but keep the one
4950 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004951 while (tp > tmp + 2 && *tp == '0'
4952 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004953 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004954 STRMOVE(tp, tp + 1);
4955 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004956 --str_arg_l;
4957 }
4958 }
4959 else
4960 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004961 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004962
Bram Moolenaar85a20022019-12-21 18:25:54 +01004963 // Be consistent: some printf("%e") use 1.0e+12
4964 // and some 1.0e+012. Remove one zero in the last
4965 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004966 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004967 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004968 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4969 && tp[2] == '0'
4970 && vim_isdigit(tp[3])
4971 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004972 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004973 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004974 --str_arg_l;
4975 }
4976 }
4977 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004978 if (zero_padding && min_field_width > str_arg_l
4979 && (tmp[0] == '-' || force_sign))
4980 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004981 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02004982 number_of_zeros_to_pad = min_field_width - str_arg_l;
4983 zero_padding_insertion_ind = 1;
4984 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004985 str_arg = tmp;
4986 break;
4987 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004988# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004989
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004990 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01004991 // unrecognized conversion specifier, keep format string
4992 // as-is
4993 zero_padding = 0; // turn zero padding off for non-numeric
4994 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004995 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004996 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004997
Bram Moolenaar85a20022019-12-21 18:25:54 +01004998 // discard the unrecognized conversion, just keep *
4999 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005000 str_arg = p;
5001 str_arg_l = 0;
5002 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005003 str_arg_l++; // include invalid conversion specifier
5004 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005005 break;
5006 }
5007
5008 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005009 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005010
Bram Moolenaar85a20022019-12-21 18:25:54 +01005011 // insert padding to the left as requested by min_field_width;
5012 // this does not include the zero padding in case of numerical
5013 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005014 if (!justify_left)
5015 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005016 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005017 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005018
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005019 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005020 {
5021 if (str_l < str_m)
5022 {
5023 size_t avail = str_m - str_l;
5024
5025 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005026 (size_t)pn > avail ? avail
5027 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005028 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005029 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005030 }
5031 }
5032
Bram Moolenaar85a20022019-12-21 18:25:54 +01005033 // zero padding as requested by the precision or by the minimal
5034 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005035 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005036 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005037 // will not copy first part of numeric right now, *
5038 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005039 zero_padding_insertion_ind = 0;
5040 }
5041 else
5042 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005043 // insert first part of numerics (sign or '0x') before zero
5044 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005045 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005046
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005047 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005048 {
5049 if (str_l < str_m)
5050 {
5051 size_t avail = str_m - str_l;
5052
5053 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005054 (size_t)zn > avail ? avail
5055 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005056 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005057 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005058 }
5059
Bram Moolenaar85a20022019-12-21 18:25:54 +01005060 // insert zero padding as requested by the precision or min
5061 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005062 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005063 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005064 {
5065 if (str_l < str_m)
5066 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005067 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005068
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005069 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005070 (size_t)zn > avail ? avail
5071 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005073 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005074 }
5075 }
5076
Bram Moolenaar85a20022019-12-21 18:25:54 +01005077 // insert formatted string
5078 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005079 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005080 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005081
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005082 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005083 {
5084 if (str_l < str_m)
5085 {
5086 size_t avail = str_m - str_l;
5087
5088 mch_memmove(str + str_l,
5089 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005090 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005091 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005092 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005093 }
5094 }
5095
Bram Moolenaar85a20022019-12-21 18:25:54 +01005096 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005097 if (justify_left)
5098 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005099 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005100 int pn = (int)(min_field_width
5101 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005102
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005103 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005104 {
5105 if (str_l < str_m)
5106 {
5107 size_t avail = str_m - str_l;
5108
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005109 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005110 (size_t)pn > avail ? avail
5111 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005112 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005113 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005114 }
5115 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005116 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005117 }
5118 }
5119
5120 if (str_m > 0)
5121 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005122 // make sure the string is nul-terminated even at the expense of
5123 // overwriting the last character (shouldn't happen, but just in case)
5124 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005125 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5126 }
5127
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005128 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005129 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005130
Bram Moolenaar85a20022019-12-21 18:25:54 +01005131 // Return the number of characters formatted (excluding trailing nul
5132 // character), that is, the number of characters that would have been
5133 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005134 return (int)str_l;
5135}
5136
Bram Moolenaar85a20022019-12-21 18:25:54 +01005137#endif // PROTO