blob: 935fd8c447e97c6f471da68d9aadd90a3183154c [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 Moolenaar85a20022019-12-21 18:25:54 +0100841// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaardf177f62005-02-22 08:39:57 +0000843 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100844emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000845{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000847}
848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 * Give an error message which contains %s for "name[len]".
851 */
852 void
853emsg_namelen(char *msg, char_u *name, int len)
854{
855 char_u *copy = vim_strnsave((char_u *)name, len);
856
857 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
858}
859
860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 * Like msg(), but truncate to a single line if p_shm contains 't', or when
862 * "force" is TRUE. This truncates in another way as for normal messages.
863 * Careful: The string may be changed by msg_may_trunc()!
864 * Returns a pointer to the printed message, if wait_return() not called.
865 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100866 char *
867msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868{
869 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100870 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaar85a20022019-12-21 18:25:54 +0100872 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100873 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaar32526b32019-01-19 17:43:09 +0100875 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
877 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100878 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 msg_hist_off = FALSE;
880
881 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100882 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 return NULL;
884}
885
886/*
887 * Check if message "s" should be truncated at the start (for filenames).
888 * Return a pointer to where the truncated message starts.
889 * Note: May change the message by replacing a character with '<'.
890 */
891 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100892msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 int n;
895 int room;
896
897 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
898 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
899 && (n = (int)STRLEN(s) - room) > 0)
900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (has_mbyte)
902 {
903 int size = vim_strsize(s);
904
Bram Moolenaar85a20022019-12-21 18:25:54 +0100905 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000906 if (size <= room)
907 return s;
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 for (n = 0; size >= room; )
910 {
911 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000912 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914 --n;
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 s += n;
917 *s = '<';
918 }
919 return s;
920}
921
922 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100923add_msg_hist(
924 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100925 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100926 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 struct msg_hist *p;
929
930 if (msg_hist_off || msg_silent != 0)
931 return;
932
Bram Moolenaar85a20022019-12-21 18:25:54 +0100933 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000934 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000935 (void)delete_first_msg();
936
Bram Moolenaar85a20022019-12-21 18:25:54 +0100937 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200938 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (p != NULL)
940 {
941 if (len < 0)
942 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100943 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 while (len > 0 && *s == '\n')
945 {
946 ++s;
947 --len;
948 }
949 while (len > 0 && s[len - 1] == '\n')
950 --len;
951 p->msg = vim_strnsave(s, len);
952 p->next = NULL;
953 p->attr = attr;
954 if (last_msg_hist != NULL)
955 last_msg_hist->next = p;
956 last_msg_hist = p;
957 if (first_msg_hist == NULL)
958 first_msg_hist = last_msg_hist;
959 ++msg_hist_len;
960 }
961}
962
963/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000964 * Delete the first (oldest) message from the history.
965 * Returns FAIL if there are no messages.
966 */
967 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100968delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000969{
970 struct msg_hist *p;
971
972 if (msg_hist_len <= 0)
973 return FAIL;
974 p = first_msg_hist;
975 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000976 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100977 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000978 vim_free(p->msg);
979 vim_free(p);
980 --msg_hist_len;
981 return OK;
982}
983
984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 * ":messages" command.
986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200988ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 struct msg_hist *p;
991 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200992 int c = 0;
993
994 if (STRCMP(eap->arg, "clear") == 0)
995 {
996 int keep = eap->addr_count == 0 ? 0 : eap->line2;
997
998 while (msg_hist_len > keep)
999 (void)delete_first_msg();
1000 return;
1001 }
1002
1003 if (*eap->arg != NUL)
1004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001005 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001006 return;
1007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 msg_hist_off = TRUE;
1010
Bram Moolenaar451f8492016-04-14 17:16:22 +02001011 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001012 if (eap->addr_count != 0)
1013 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001014 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001015 for (; p != NULL && !got_int; p = p->next)
1016 c++;
1017
1018 c -= eap->line2;
1019
Bram Moolenaar85a20022019-12-21 18:25:54 +01001020 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001021 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1022 p = p->next, c--);
1023 }
1024
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001025 if (p == first_msg_hist)
1026 {
1027 s = mch_getenv((char_u *)"LANG");
1028 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001029 // The next comment is extracted by xgettext and put in po file for
1030 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001031 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001032 // Translator: Please replace the name and email address
1033 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001034 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001035 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001036 }
1037
Bram Moolenaar85a20022019-12-21 18:25:54 +01001038 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001039 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001041 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 msg_hist_off = FALSE;
1044}
1045
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001046#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047/*
1048 * Call this after prompting the user. This will avoid a hit-return message
1049 * and a delay.
1050 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001051 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001052msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 need_wait_return = FALSE;
1055 emsg_on_display = FALSE;
1056 cmdline_row = msg_row;
1057 msg_col = 0;
1058 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001059 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061#endif
1062
1063/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001064 * Wait for the user to hit a key (normally Enter).
1065 * If "redraw" is TRUE, clear and redraw the screen.
1066 * If "redraw" is FALSE, just redraw the screen.
1067 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 */
1069 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001070wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 int c;
1073 int oldState;
1074 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001076 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001077 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 if (redraw == TRUE)
1080 must_redraw = CLEAR;
1081
Bram Moolenaar85a20022019-12-21 18:25:54 +01001082 // If using ":silent cmd", don't wait for a return. Also don't set
1083 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (msg_silent != 0)
1085 return;
1086
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001087 /*
1088 * When inside vgetc(), we can't wait for a typed character at all.
1089 * With the global command (and some others) we only need one return at
1090 * the end. Adjust cmdline_row to avoid the next message overwriting the
1091 * last one.
1092 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001093 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001095 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (no_wait_return)
1097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (!exmode_active)
1099 cmdline_row = msg_row;
1100 return;
1101 }
1102
Bram Moolenaar85a20022019-12-21 18:25:54 +01001103 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 oldState = State;
1105 if (quit_more)
1106 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001107 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 quit_more = FALSE;
1109 got_int = FALSE;
1110 }
1111 else if (exmode_active)
1112 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 msg_puts(" "); // make sure the cursor is on the right line
1114 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 got_int = FALSE;
1116 }
1117 else
1118 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001119 // Make sure the hit-return prompt is on screen when 'guioptions' was
1120 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 screenalloc(FALSE);
1122
1123 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001126 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001128 cmdline_row = msg_row;
1129
Bram Moolenaar85a20022019-12-21 18:25:54 +01001130 // Avoid the sequence that the user types ":" at the hit-return prompt
1131 // to start an Ex command, but the file-changed dialog gets in the
1132 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001133 if (need_check_timestamps)
1134 check_timestamps(FALSE);
1135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 hit_return_msg();
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 do
1139 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // Remember "got_int", if it is set vgetc() probably returns a
1141 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001143
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // Don't do mappings here, we put the character back in the
1145 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001146 ++no_mapping;
1147 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001148
Bram Moolenaar85a20022019-12-21 18:25:54 +01001149 // Temporarily disable Recording. If Recording is active, the
1150 // character will be recorded later, since it will be added to the
1151 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001152 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001153 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001154 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001155 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001157 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001159 --no_mapping;
1160 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001161 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001162 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 // Strange way to allow copying (yanking) a modeless selection at
1166 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1167 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1169 {
1170 clip_copy_modeless_selection(TRUE);
1171 c = K_IGNORE;
1172 }
1173#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001174
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001175 /*
1176 * Allow scrolling back in the messages.
1177 * Also accept scroll-down commands when messages fill the screen,
1178 * to avoid that typing one 'j' too many makes the messages
1179 * disappear.
1180 */
1181 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001182 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001183 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1184 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001185 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001186 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001187 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001188 do_more_prompt(c);
1189 else
1190 {
1191 msg_didout = FALSE;
1192 c = K_IGNORE;
1193 msg_col =
1194#ifdef FEAT_RIGHTLEFT
1195 cmdmsg_rl ? Columns - 1 :
1196#endif
1197 0;
1198 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001199 if (quit_more)
1200 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001202 quit_more = FALSE;
1203 got_int = FALSE;
1204 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001205 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001206 {
1207 c = K_IGNORE;
1208 hit_return_msg();
1209 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001210 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001211 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001212 && (c == 'j' || c == 'd' || c == 'f'
1213 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001214 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 } while ((had_got_int && c == Ctrl_C)
1217 || c == K_IGNORE
1218#ifdef FEAT_GUI
1219 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1222 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1223 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001224 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001226 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 || (!mouse_has(MOUSE_RETURN)
1228 && mouse_row < msg_row
1229 && (c == K_LEFTMOUSE
1230 || c == K_MIDDLEMOUSE
1231 || c == K_RIGHTMOUSE
1232 || c == K_X1MOUSE
1233 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 );
1235 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 /*
1237 * Avoid that the mouse-up event causes visual mode to start.
1238 */
1239 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1240 || c == K_X1MOUSE || c == K_X2MOUSE)
1241 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001242 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001244 // Put the character back in the typeahead buffer. Don't use the
1245 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001246 ins_char_typebuf(c);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001247 do_redraw = TRUE; // need a redraw even though there is
1248 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 redir_off = FALSE;
1252
1253 /*
1254 * If the user hits ':', '?' or '/' we get a command line from the next
1255 * line.
1256 */
1257 if (c == ':' || c == '?' || c == '/')
1258 {
1259 if (!exmode_active)
1260 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001261 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001263#ifdef FEAT_TERMINAL
1264 skip_term_loop = TRUE;
1265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267
1268 /*
1269 * If the window size changed set_shellsize() will redraw the screen.
1270 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1271 * typed.
1272 */
1273 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001274 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 msg_check();
1277
1278#if defined(UNIX) || defined(VMS)
1279 /*
1280 * When switching screens, we need to output an extra newline on exit.
1281 */
1282 if (swapping_screen() && !termcap_active)
1283 newline_on_exit = TRUE;
1284#endif
1285
1286 need_wait_return = FALSE;
1287 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001288 emsg_on_display = FALSE; // can delete error message now
1289 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 reset_last_sourcing();
1291 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1292 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001293 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
Bram Moolenaar85a20022019-12-21 18:25:54 +01001295 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001297 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 shell_resized();
1299 }
1300 else if (!skip_redraw
1301 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1302 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 redraw_later(VALID);
1305 }
1306}
1307
1308/*
1309 * Write the hit-return prompt.
1310 */
1311 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001312hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001314 int save_p_more = p_more;
1315
Bram Moolenaar85a20022019-12-21 18:25:54 +01001316 p_more = FALSE; // don't want see this message when scrolling back
1317 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 msg_putchar('\n');
1319 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001320 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaar32526b32019-01-19 17:43:09 +01001322 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (!msg_use_printf())
1324 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001325 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326}
1327
1328/*
1329 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1330 */
1331 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001332set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 vim_free(keep_msg);
1335 if (s != NULL && msg_silent == 0)
1336 keep_msg = vim_strsave(s);
1337 else
1338 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001339 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001340 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341}
1342
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001343#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1344/*
1345 * If there currently is a message being displayed, set "keep_msg" to it, so
1346 * that it will be displayed again after redraw.
1347 */
1348 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001349set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001350{
1351 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1352 && (State & NORMAL))
1353 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1354}
1355#endif
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357/*
1358 * Prepare for outputting characters in the command line.
1359 */
1360 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001361msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 int did_return = FALSE;
1364
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001365 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001366 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001367
1368#ifdef FEAT_EVAL
1369 if (need_clr_eos)
1370 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001371 // Halfway an ":echo" command and getting an (error) message: clear
1372 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001373 need_clr_eos = FALSE;
1374 msg_clr_eos();
1375 }
1376#endif
1377
Bram Moolenaar85a20022019-12-21 18:25:54 +01001378 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 msg_row = cmdline_row;
1381 msg_col =
1382#ifdef FEAT_RIGHTLEFT
1383 cmdmsg_rl ? Columns - 1 :
1384#endif
1385 0;
1386 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001387 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
1389 msg_putchar('\n');
1390 did_return = TRUE;
1391 if (exmode_active != EXMODE_NORMAL)
1392 cmdline_row = msg_row;
1393 }
1394 if (!msg_didany || lines_left < 0)
1395 msg_starthere();
1396 if (msg_silent == 0)
1397 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001398 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 cursor_off();
1400 }
1401
Bram Moolenaar85a20022019-12-21 18:25:54 +01001402 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (!did_return)
1404 redir_write((char_u *)"\n", -1);
1405}
1406
1407/*
1408 * Note that the current msg position is where messages start.
1409 */
1410 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001411msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 lines_left = cmdline_row;
1414 msg_didany = FALSE;
1415}
1416
1417 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001418msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 msg_putchar_attr(c, 0);
1421}
1422
1423 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001424msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001426 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 if (IS_SPECIAL(c))
1429 {
1430 buf[0] = K_SPECIAL;
1431 buf[1] = K_SECOND(c);
1432 buf[2] = K_THIRD(c);
1433 buf[3] = NUL;
1434 }
1435 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001436 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001437 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438}
1439
1440 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001441msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001443 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
Bram Moolenaar32526b32019-01-19 17:43:09 +01001445 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 msg_puts(buf);
1447}
1448
1449 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001450msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 msg_home_replace_attr(fname, 0);
1453}
1454
1455#if defined(FEAT_FIND_ID) || defined(PROTO)
1456 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001457msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001459 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460}
1461#endif
1462
1463 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001464msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 char_u *name;
1467
1468 name = home_replace_save(NULL, fname);
1469 if (name != NULL)
1470 msg_outtrans_attr(name, attr);
1471 vim_free(name);
1472}
1473
1474/*
1475 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001476 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 * Use attributes 'attr'.
1478 * Return the number of characters it takes on the screen.
1479 */
1480 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001481msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 return msg_outtrans_attr(str, 0);
1484}
1485
1486 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001487msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1490}
1491
1492 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001493msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 return msg_outtrans_len_attr(str, len, 0);
1496}
1497
1498/*
1499 * Output one character at "p". Return pointer to the next character.
1500 * Handles multi-byte characters.
1501 */
1502 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001503msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 int l;
1506
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001507 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 {
1509 msg_outtrans_len_attr(p, l, attr);
1510 return p + l;
1511 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001512 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 return p + 1;
1514}
1515
1516 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001517msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 int retval = 0;
1520 char_u *str = msgstr;
1521 char_u *plain_start = msgstr;
1522 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 int mb_l;
1524 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar85a20022019-12-21 18:25:54 +01001526 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (attr & MSG_HIST)
1528 {
1529 add_msg_hist(str, len, attr);
1530 attr &= ~MSG_HIST;
1531 }
1532
Bram Moolenaar85a20022019-12-21 18:25:54 +01001533 // If the string starts with a composing character first draw a space on
1534 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001536 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538 /*
1539 * Go over the string. Special characters are translated and printed.
1540 * Normal characters are printed several at a time.
1541 */
1542 while (--len >= 0)
1543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001545 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001546 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001548 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 else
1550 mb_l = 1;
1551 if (has_mbyte && mb_l > 1)
1552 {
1553 c = (*mb_ptr2char)(str);
1554 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001555 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 retval += (*mb_ptr2cells)(str);
1557 else
1558 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001559 // unprintable multi-byte char: print the printable chars so
1560 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001562 msg_puts_attr_len((char *)plain_start,
1563 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001565 msg_puts_attr((char *)transchar(c),
1566 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 retval += char2cells(c);
1568 }
1569 len -= mb_l - 1;
1570 str += mb_l;
1571 }
1572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 s = transchar_byte(*str);
1575 if (s[1] != NUL)
1576 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001577 // unprintable char: print the printable chars so far and the
1578 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001580 msg_puts_attr_len((char *)plain_start,
1581 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001583 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001584 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001586 else
1587 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 ++str;
1589 }
1590 }
1591
1592 if (str > plain_start)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001593 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001594 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
1596 return retval;
1597}
1598
1599#if defined(FEAT_QUICKFIX) || defined(PROTO)
1600 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001601msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 int i;
1604 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1605
1606 arg = skipwhite(arg);
1607 for (i = 5; *arg && i >= 0; --i)
1608 if (*arg++ != str[i])
1609 break;
1610 if (i < 0)
1611 {
1612 msg_putchar('\n');
1613 for (i = 0; rs[i]; ++i)
1614 msg_putchar(rs[i] - 3);
1615 }
1616}
1617#endif
1618
1619/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001620 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 * Return the number of characters it takes on the screen.
1622 *
1623 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1624 * following character and shown as <F1>, <S-Up> etc. Any other character
1625 * which is not printable shown in <> form.
1626 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1627 * If a character is displayed in one of these special ways, is also
1628 * highlighted (its highlight name is '8' in the p_hl variable).
1629 * Otherwise characters are not highlighted.
1630 * This function is used to show mappings, where we want to see how to type
1631 * the character/string -- webb
1632 */
1633 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001634msg_outtrans_special(
1635 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001636 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001637 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 char_u *str = strstart;
1640 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001641 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int attr;
1643 int len;
1644
Bram Moolenaar8820b482017-03-16 17:23:31 +01001645 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 while (*str != NUL)
1647 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001648 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if ((str == strstart || str[1] == NUL) && *str == ' ')
1650 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001651 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 ++str;
1653 }
1654 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001655 text = (char *)str2special(&str, from);
1656 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001657 if (maxlen > 0 && retval + len >= maxlen)
1658 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001659 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001660 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001661 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 retval += len;
1663 }
1664 return retval;
1665}
1666
Bram Moolenaarbd743252010-10-20 21:23:33 +02001667#if defined(FEAT_EVAL) || defined(PROTO)
1668/*
1669 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1670 * strings, in an allocated string.
1671 */
1672 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001673str2special_save(
1674 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001675 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001676{
1677 garray_T ga;
1678 char_u *p = str;
1679
1680 ga_init2(&ga, 1, 40);
1681 while (*p != NUL)
1682 ga_concat(&ga, str2special(&p, is_lhs));
1683 ga_append(&ga, NUL);
1684 return (char_u *)ga.ga_data;
1685}
1686#endif
1687
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688/*
1689 * Return the printable string for the key codes at "*sp".
1690 * Used for translating the lhs or rhs of a mapping to printable chars.
1691 * Advances "sp" to the next code.
1692 */
1693 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001694str2special(
1695 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 int c;
1699 static char_u buf[7];
1700 char_u *str = *sp;
1701 int modifiers = 0;
1702 int special = FALSE;
1703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (has_mbyte)
1705 {
1706 char_u *p;
1707
Bram Moolenaar85a20022019-12-21 18:25:54 +01001708 // Try to un-escape a multi-byte character. Return the un-escaped
1709 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 p = mb_unescape(sp);
1711 if (p != NULL)
1712 return p;
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 c = *str;
1716 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1717 {
1718 if (str[1] == KS_MODIFIER)
1719 {
1720 modifiers = str[2];
1721 str += 3;
1722 c = *str;
1723 }
1724 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1725 {
1726 c = TO_SPECIAL(str[1], str[2]);
1727 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001729 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 special = TRUE;
1731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001733 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001735 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001736
Bram Moolenaar85a20022019-12-21 18:25:54 +01001737 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001738 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1739 {
1740 transchar_nonprint(buf, c);
1741 *sp = str + 1;
1742 return buf;
1743 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001744 // Since 'special' is TRUE the multi-byte character 'c' will be
1745 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001746 c = (*mb_ptr2char)(str);
1747 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001749 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001750 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar85a20022019-12-21 18:25:54 +01001752 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1753 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if (special || char2cells(c) > 1 || (from && c == ' '))
1755 return get_special_key_name(c, modifiers);
1756 buf[0] = c;
1757 buf[1] = NUL;
1758 return buf;
1759}
1760
1761/*
1762 * Translate a key sequence into special key names.
1763 */
1764 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001765str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 char_u *s;
1768
1769 *buf = NUL;
1770 while (*sp)
1771 {
1772 s = str2special(&sp, FALSE);
1773 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1774 STRCAT(buf, s);
1775 }
1776}
1777
1778/*
1779 * print line for :print or :list command
1780 */
1781 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001782msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
1784 int c;
1785 int col = 0;
1786 int n_extra = 0;
1787 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001788 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001789 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001791 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 int l;
1794 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaardf177f62005-02-22 08:39:57 +00001796 if (curwin->w_p_list)
1797 list = TRUE;
1798
Bram Moolenaar85a20022019-12-21 18:25:54 +01001799 // find start of trailing whitespace
Bram Moolenaardf177f62005-02-22 08:39:57 +00001800 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001803 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 --trail;
1805 }
1806
Bram Moolenaar85a20022019-12-21 18:25:54 +01001807 // output a space for an empty line, otherwise the line will be
1808 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001809 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 msg_putchar(' ');
1811
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001812 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001814 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001817 if (n_extra == 0 && c_final)
1818 c = c_final;
1819 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 c = c_extra;
1821 else
1822 c = *p_extra++;
1823 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001824 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001827 if (lcs_nbsp != NUL && list
1828 && (mb_ptr2char(s) == 160
1829 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001830 {
1831 mb_char2bytes(lcs_nbsp, buf);
1832 buf[(*mb_ptr2len)(buf)] = NUL;
1833 }
1834 else
1835 {
1836 mch_memmove(buf, s, (size_t)l);
1837 buf[l] = NUL;
1838 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001839 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 s += l;
1841 continue;
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 else
1844 {
1845 attr = 0;
1846 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001847 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001849 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001850#ifdef FEAT_VARTABS
1851 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1852 curbuf->b_p_vts_array) - 1;
1853#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001855#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001856 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 c = ' ';
1859 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001860 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862 else
1863 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001864 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001866 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001867 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001870 else if (c == 160 && list && lcs_nbsp != NUL)
1871 {
1872 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001873 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001874 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001875 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 p_extra = (char_u *)"";
1878 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001879 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 n_extra = 1;
1881 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001882 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 --s;
1884 }
1885 else if (c != NUL && (n = byte2cells(c)) > 1)
1886 {
1887 n_extra = n - 1;
1888 p_extra = transchar_byte(c);
1889 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001890 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001892 // Use special coloring to be able to distinguish <hex> from
1893 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001894 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 else if (c == ' ' && trail != NULL && s > trail)
1897 {
1898 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001899 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001901 else if (c == ' ' && list && lcs_space != NUL)
1902 {
1903 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001904 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907
1908 if (c == NUL)
1909 break;
1910
1911 msg_putchar_attr(c, attr);
1912 col++;
1913 }
1914 msg_clr_eos();
1915}
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
1918 * Use screen_puts() to output one multi-byte character.
1919 * Return the pointer "s" advanced to the next character.
1920 */
1921 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001922screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923{
1924 int cw;
1925
Bram Moolenaar85a20022019-12-21 18:25:54 +01001926 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 cw = (*mb_ptr2cells)(s);
1928 if (cw > 1 && (
1929#ifdef FEAT_RIGHTLEFT
1930 cmdmsg_rl ? msg_col <= 1 :
1931#endif
1932 msg_col == Columns - 1))
1933 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001934 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001935 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 return s;
1937 }
1938
1939 screen_puts_len(s, l, msg_row, msg_col, attr);
1940#ifdef FEAT_RIGHTLEFT
1941 if (cmdmsg_rl)
1942 {
1943 msg_col -= cw;
1944 if (msg_col == 0)
1945 {
1946 msg_col = Columns;
1947 ++msg_row;
1948 }
1949 }
1950 else
1951#endif
1952 {
1953 msg_col += cw;
1954 if (msg_col >= Columns)
1955 {
1956 msg_col = 0;
1957 ++msg_row;
1958 }
1959 }
1960 return s + l;
1961}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963/*
1964 * Output a string to the screen at position msg_row, msg_col.
1965 * Update msg_row and msg_col for the next message.
1966 */
1967 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001968msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001970 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971}
1972
1973 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001974msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001976 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977}
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979/*
1980 * Show a message in such a way that it always fits in the line. Cut out a
1981 * part in the middle and replace it with "..." when necessary.
1982 * Does not handle multi-byte characters!
1983 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001984 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001985msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 int slen = len;
1988 int room;
1989
1990 room = Columns - msg_col;
1991 if (len > room && room >= 20)
1992 {
1993 slen = (room - 3) / 2;
1994 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001995 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1998}
1999
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002000 void
2001msg_outtrans_long_attr(char_u *longstr, int attr)
2002{
2003 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2004}
2005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006/*
2007 * Basic function for writing a message with highlight attributes.
2008 */
2009 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002010msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 msg_puts_attr_len(s, -1, attr);
2013}
2014
2015/*
2016 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2017 * When "maxlen" is -1 there is no maximum length.
2018 * When "maxlen" is >= 0 the message is not put in the history.
2019 */
2020 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002021msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 /*
2024 * If redirection is on, also write to the redirection file.
2025 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002026 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 /*
2029 * Don't print anything when using ":silent cmd".
2030 */
2031 if (msg_silent != 0)
2032 return;
2033
Bram Moolenaar85a20022019-12-21 18:25:54 +01002034 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if ((attr & MSG_HIST) && maxlen < 0)
2036 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002037 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 attr &= ~MSG_HIST;
2039 }
2040
Bram Moolenaare8070982019-10-12 17:07:06 +02002041 // When writing something to the screen after it has scrolled, requires a
2042 // wait-return prompt later. Needed when scrolling, resetting
2043 // need_wait_return after some prompt, and then outputting something
2044 // without scrolling
2045 // Not needed when only using CR to move the cursor.
2046 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002048 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /*
2051 * If there is no valid screen, use fprintf so we can see error messages.
2052 * If termcap is not active, we may be writing in an alternate console
2053 * window, cursor positioning may not work correctly (window size may be
2054 * different, e.g. for Win32 console) or we just don't know where the
2055 * cursor is.
2056 */
2057 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002058 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002059 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002060 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002063/*
2064 * The display part of msg_puts_attr_len().
2065 * May be called recursively to display scroll-back text.
2066 */
2067 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002068msg_puts_display(
2069 char_u *str,
2070 int maxlen,
2071 int attr,
2072 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002073{
2074 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002075 char_u *t_s = str; // string from "t_s" to "s" is still todo
2076 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002077 int l;
2078 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002079 char_u *sb_str = str;
2080 int sb_col = msg_col;
2081 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002082 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002085 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
2087 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002088 * We are at the end of the screen line when:
2089 * - When outputting a newline.
2090 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002092 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093#ifdef FEAT_RIGHTLEFT
2094 cmdmsg_rl
2095 ? (
2096 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002097 || (*s == TAB && msg_col <= 7)
2098 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 :
2100#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002101 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002104 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002106 /*
2107 * The screen is scrolled up when at the last row (some terminals
2108 * scroll automatically, some don't. To avoid problems we scroll
2109 * ourselves).
2110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002112 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002113 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar85a20022019-12-21 18:25:54 +01002115 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (msg_no_more && lines_left == 0)
2117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar85a20022019-12-21 18:25:54 +01002119 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002120 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002123 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 msg_col = Columns - 1;
2125
Bram Moolenaar85a20022019-12-21 18:25:54 +01002126 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002127 if (*s >= ' '
2128#ifdef FEAT_RIGHTLEFT
2129 && !cmdmsg_rl
2130#endif
2131 )
2132 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002133 if (has_mbyte)
2134 {
2135 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002137 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002138 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002139 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002140 s = screen_puts_mbyte(s, l, attr);
2141 }
2142 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002143 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002144 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002145 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002146 else
2147 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148
2149 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002150 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002151 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2152
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002153 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002154 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 redraw_cmdline = TRUE;
2156 if (cmdline_row > 0 && !exmode_active)
2157 --cmdline_row;
2158
2159 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002160 * If screen is completely filled and 'more' is set then wait
2161 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002163 if (lines_left > 0)
2164 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002165 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 && !msg_no_more && !exmode_active)
2167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002169 if (do_more_prompt(NUL))
2170 s = confirm_msg_tail;
2171#else
2172 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#endif
2174 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002175 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002177
Bram Moolenaar85a20022019-12-21 18:25:54 +01002178 // When we displayed a char in last column need to check if there
2179 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002180 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002181 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
2183
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002187 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002188 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2189 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002190 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002191 t_puts(&t_col, t_s, s, attr);
2192
2193 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002194 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002195 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaar85a20022019-12-21 18:25:54 +01002197 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002199 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002200#ifdef FEAT_RIGHTLEFT
2201 if (cmdmsg_rl)
2202 msg_col = Columns - 1;
2203 else
2204#endif
2205 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002206 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 msg_row = Rows - 1;
2208 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002209 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
2211 msg_col = 0;
2212 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002213 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
2215 if (msg_col)
2216 --msg_col;
2217 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002218 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 do
2221 msg_screen_putchar(' ', attr);
2222 while (msg_col & 7);
2223 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002225 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 else
2227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (has_mbyte)
2229 {
2230 cw = (*mb_ptr2cells)(s);
2231 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002232 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002233 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002235 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237 else
2238 {
2239 cw = 1;
2240 l = 1;
2241 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002242
Bram Moolenaar85a20022019-12-21 18:25:54 +01002243 // When drawing from right to left or when a double-wide character
2244 // doesn't fit, draw a single character here. Otherwise collect
2245 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (
2247# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002248 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002250 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (l > 1)
2253 s = screen_puts_mbyte(s, l, attr) - 1;
2254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 msg_screen_putchar(*s, attr);
2256 }
2257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002259 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (t_col == 0)
2261 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 t_col += cw;
2263 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265 }
2266 ++s;
2267 }
2268
Bram Moolenaar85a20022019-12-21 18:25:54 +01002269 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002271 t_puts(&t_col, t_s, s, attr);
2272 if (p_more && !recurse)
2273 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275 msg_check();
2276}
2277
2278/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002279 * Return TRUE when ":filter pattern" was used and "msg" does not match
2280 * "pattern".
2281 */
2282 int
2283message_filtered(char_u *msg)
2284{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002285 int match;
2286
2287 if (cmdmod.filter_regmatch.regprog == NULL)
2288 return FALSE;
2289 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2290 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002291}
2292
2293/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002294 * Scroll the screen up one line for displaying the next message line.
2295 */
2296 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002297msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002298{
2299#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002300 // Remove the cursor before scrolling, ScreenLines[] is going
2301 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002302 if (gui.in_use)
2303 gui_undraw_cursor();
2304#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002305 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002306 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002307 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002308 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002309
2310 if (!can_clear((char_u *)" "))
2311 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002312 // Scrolling up doesn't result in the right background. Set the
2313 // background here. It's not efficient, but avoids that we have to do
2314 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002315 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2316
Bram Moolenaar85a20022019-12-21 18:25:54 +01002317 // Also clear the last char of the last but one line if it was not
2318 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002319 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2320 screen_fill((int)Rows - 2, (int)Rows - 1,
2321 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2322 }
2323}
2324
2325/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002326 * Increment "msg_scrolled".
2327 */
2328 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002329inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002330{
2331#ifdef FEAT_EVAL
2332 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2333 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002334 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002335 char_u *tofree = NULL;
2336 int len;
2337
Bram Moolenaar85a20022019-12-21 18:25:54 +01002338 // v:scrollstart is empty, set it to the script/function name and line
2339 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002340 if (p == NULL)
2341 p = (char_u *)_("Unknown");
2342 else
2343 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002344 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002345 tofree = alloc(len);
2346 if (tofree != NULL)
2347 {
2348 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002349 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002350 p = tofree;
2351 }
2352 }
2353 set_vim_var_string(VV_SCROLLSTART, p, -1);
2354 vim_free(tofree);
2355 }
2356#endif
2357 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002358 if (must_redraw < VALID)
2359 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002360}
2361
2362/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002363 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2364 * store the displayed text and remember where screen lines start.
2365 */
2366typedef struct msgchunk_S msgchunk_T;
2367struct msgchunk_S
2368{
2369 msgchunk_T *sb_next;
2370 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002371 char sb_eol; // TRUE when line ends after this text
2372 int sb_msg_col; // column in which text starts
2373 int sb_attr; // text attributes
2374 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002375};
2376
Bram Moolenaar85a20022019-12-21 18:25:54 +01002377static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002378
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002379static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002380
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002381typedef enum {
2382 SB_CLEAR_NONE = 0,
2383 SB_CLEAR_ALL,
2384 SB_CLEAR_CMDLINE_BUSY,
2385 SB_CLEAR_CMDLINE_DONE
2386} sb_clear_T;
2387
Bram Moolenaar85a20022019-12-21 18:25:54 +01002388// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002389static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002390
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002391/*
2392 * Store part of a printed message for displaying when scrolling back.
2393 */
2394 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002395store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002396 char_u **sb_str, // start of string
2397 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002398 int attr,
2399 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002400 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002401{
2402 msgchunk_T *mp;
2403
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002404 if (do_clear_sb_text == SB_CLEAR_ALL
2405 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002406 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002407 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2408 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002409 }
2410
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002411 if (s > *sb_str)
2412 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002413 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002414 if (mp != NULL)
2415 {
2416 mp->sb_eol = finish;
2417 mp->sb_msg_col = *sb_col;
2418 mp->sb_attr = attr;
2419 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2420
2421 if (last_msgchunk == NULL)
2422 {
2423 last_msgchunk = mp;
2424 mp->sb_prev = NULL;
2425 }
2426 else
2427 {
2428 mp->sb_prev = last_msgchunk;
2429 last_msgchunk->sb_next = mp;
2430 last_msgchunk = mp;
2431 }
2432 mp->sb_next = NULL;
2433 }
2434 }
2435 else if (finish && last_msgchunk != NULL)
2436 last_msgchunk->sb_eol = TRUE;
2437
2438 *sb_str = s;
2439 *sb_col = 0;
2440}
2441
2442/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002443 * Finished showing messages, clear the scroll-back text on the next message.
2444 */
2445 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002446may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002447{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002448 do_clear_sb_text = SB_CLEAR_ALL;
2449}
2450
2451/*
2452 * Starting to edit the command line, do not clear messages now.
2453 */
2454 void
2455sb_text_start_cmdline(void)
2456{
2457 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2458 msg_sb_eol();
2459}
2460
2461/*
2462 * Ending to edit the command line. Clear old lines but the last one later.
2463 */
2464 void
2465sb_text_end_cmdline(void)
2466{
2467 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002468}
2469
2470/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002471 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002472 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002473 * Called when redrawing the screen.
2474 */
2475 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002476clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002477{
2478 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002479 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002480
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002481 if (all)
2482 lastp = &last_msgchunk;
2483 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002484 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002485 if (last_msgchunk == NULL)
2486 return;
2487 lastp = &last_msgchunk->sb_prev;
2488 }
2489
2490 while (*lastp != NULL)
2491 {
2492 mp = (*lastp)->sb_prev;
2493 vim_free(*lastp);
2494 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002495 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002496}
2497
2498/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002499 * "g<" command.
2500 */
2501 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002502show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002503{
2504 msgchunk_T *mp;
2505
Bram Moolenaar85a20022019-12-21 18:25:54 +01002506 // Only show something if there is more than one line, otherwise it looks
2507 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002508 mp = msg_sb_start(last_msgchunk);
2509 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002510 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002511 else
2512 {
2513 do_more_prompt('G');
2514 wait_return(FALSE);
2515 }
2516}
2517
2518/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 * Move to the start of screen line in already displayed text.
2520 */
2521 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002522msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002523{
2524 msgchunk_T *mp = mps;
2525
2526 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2527 mp = mp->sb_prev;
2528 return mp;
2529}
2530
2531/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002532 * Mark the last message chunk as finishing the line.
2533 */
2534 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002535msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002536{
2537 if (last_msgchunk != NULL)
2538 last_msgchunk->sb_eol = TRUE;
2539}
2540
2541/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002542 * Display a screen line from previously displayed text at row "row".
2543 * Returns a pointer to the text for the next line (can be NULL).
2544 */
2545 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002546disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002547{
2548 msgchunk_T *mp = smp;
2549 char_u *p;
2550
2551 for (;;)
2552 {
2553 msg_row = row;
2554 msg_col = mp->sb_msg_col;
2555 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002556 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002557 ++p;
2558 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2559 if (mp->sb_eol || mp->sb_next == NULL)
2560 break;
2561 mp = mp->sb_next;
2562 }
2563 return mp->sb_next;
2564}
2565
2566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 * Output any postponed text for msg_puts_attr_len().
2568 */
2569 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002570t_puts(
2571 int *t_col,
2572 char_u *t_s,
2573 char_u *s,
2574 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002576 // output postponed text
2577 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002579 msg_col += *t_col;
2580 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002581 // If the string starts with a composing character don't increment the
2582 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2584 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (msg_col >= Columns)
2586 {
2587 msg_col = 0;
2588 ++msg_row;
2589 }
2590}
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592/*
2593 * Returns TRUE when messages should be printed with mch_errmsg().
2594 * This is used when there is no valid screen, so we can see error messages.
2595 * If termcap is not active, we may be writing in an alternate console
2596 * window, cursor positioning may not work correctly (window size may be
2597 * different, e.g. for Win32 console) or we just don't know where the
2598 * cursor is.
2599 */
2600 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002601msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002604#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2605# ifdef VIMDLL
2606 || (!gui.in_use && !termcap_active)
2607# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610#endif
2611 || (swapping_screen() && !termcap_active)
2612 );
2613}
2614
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002615/*
2616 * Print a message when there is no valid screen.
2617 */
2618 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002619msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002620{
2621 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002622 char_u *buf = NULL;
2623 char_u *p = s;
2624
Bram Moolenaar4f974752019-02-17 17:44:42 +01002625#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002626 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002627 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002628#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002629 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002630 {
2631 if (!(silent_mode && p_verbose == 0))
2632 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002633 // NL --> CR NL translation (for Unix, not for "--version")
2634 if (*s == NL)
2635 {
2636 int n = (int)(s - p);
2637
2638 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002639 if (buf != NULL)
2640 {
2641 memcpy(buf, p, n);
2642 if (!info_message)
2643 buf[n++] = CAR;
2644 buf[n++] = NL;
2645 buf[n++] = NUL;
2646 if (info_message) // informative message, not an error
2647 mch_msg((char *)buf);
2648 else
2649 mch_errmsg((char *)buf);
2650 vim_free(buf);
2651 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002652 p = s + 1;
2653 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654 }
2655
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002656 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002657#ifdef FEAT_RIGHTLEFT
2658 if (cmdmsg_rl)
2659 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002660 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002661 msg_col = Columns - 1;
2662 else
2663 --msg_col;
2664 }
2665 else
2666#endif
2667 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002668 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002669 msg_col = 0;
2670 else
2671 ++msg_col;
2672 }
2673 ++s;
2674 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002675
2676 if (*p != NUL && !(silent_mode && p_verbose == 0))
2677 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002678 int c = -1;
2679
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002680 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002681 {
2682 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002683 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002684 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002685 if (info_message)
2686 mch_msg((char *)p);
2687 else
2688 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002689 if (c != -1)
2690 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002691 }
2692
2693 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002694
Bram Moolenaar4f974752019-02-17 17:44:42 +01002695#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002696 if (!(silent_mode && p_verbose == 0))
2697 mch_settmode(TMODE_RAW);
2698#endif
2699}
2700
2701/*
2702 * Show the more-prompt and handle the user response.
2703 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002704 * When at hit-enter prompt "typed_char" is the already typed character,
2705 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002706 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2707 */
2708 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002709do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002710{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002711 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002712 int used_typed_char = typed_char;
2713 int oldState = State;
2714 int c;
2715#ifdef FEAT_CON_DIALOG
2716 int retval = FALSE;
2717#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002718 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002719 msgchunk_T *mp_last = NULL;
2720 msgchunk_T *mp;
2721 int i;
2722
Bram Moolenaar85a20022019-12-21 18:25:54 +01002723 // We get called recursively when a timer callback outputs a message. In
2724 // that case don't show another prompt. Also when at the hit-Enter prompt
2725 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002726 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002727 return FALSE;
2728 entered = TRUE;
2729
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002730 if (typed_char == 'G')
2731 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002732 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002733 mp_last = msg_sb_start(last_msgchunk);
2734 for (i = 0; i < Rows - 2 && mp_last != NULL
2735 && mp_last->sb_prev != NULL; ++i)
2736 mp_last = msg_sb_start(mp_last->sb_prev);
2737 }
2738
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002739 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002741 if (typed_char == NUL)
2742 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 for (;;)
2744 {
2745 /*
2746 * Get a typed character directly from the user.
2747 */
2748 if (used_typed_char != NUL)
2749 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002750 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002751 used_typed_char = NUL;
2752 }
2753 else
2754 c = get_keystroke();
2755
2756#if defined(FEAT_MENU) && defined(FEAT_GUI)
2757 if (c == K_MENU)
2758 {
2759 int idx = get_menu_index(current_menu, ASKMORE);
2760
Bram Moolenaar85a20022019-12-21 18:25:54 +01002761 // Used a menu. If it starts with CTRL-Y, it must
2762 // be a "Copy" for the clipboard. Otherwise
2763 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764 if (idx == MENU_INDEX_INVALID)
2765 continue;
2766 c = *current_menu->strings[idx];
2767 if (c != NUL && current_menu->strings[idx][1] != NUL)
2768 ins_typebuf(current_menu->strings[idx] + 1,
2769 current_menu->noremap[idx], 0, TRUE,
2770 current_menu->silent[idx]);
2771 }
2772#endif
2773
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002774 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002775 switch (c)
2776 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002777 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002778 case K_BS:
2779 case 'k':
2780 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002781 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782 break;
2783
Bram Moolenaar85a20022019-12-21 18:25:54 +01002784 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 case NL:
2786 case 'j':
2787 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002788 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002789 break;
2790
Bram Moolenaar85a20022019-12-21 18:25:54 +01002791 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002792 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 break;
2794
Bram Moolenaar85a20022019-12-21 18:25:54 +01002795 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002796 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 break;
2798
Bram Moolenaar85a20022019-12-21 18:25:54 +01002799 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002800 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002801 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 break;
2803
Bram Moolenaar85a20022019-12-21 18:25:54 +01002804 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002805 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002806 case K_PAGEDOWN:
2807 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002808 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809 break;
2810
Bram Moolenaar85a20022019-12-21 18:25:54 +01002811 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002812 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002813 break;
2814
Bram Moolenaar85a20022019-12-21 18:25:54 +01002815 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002816 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002817 lines_left = 999999;
2818 break;
2819
Bram Moolenaar85a20022019-12-21 18:25:54 +01002820 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821#ifdef FEAT_CON_DIALOG
2822 if (!confirm_msg_used)
2823#endif
2824 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002825 // Since got_int is set all typeahead will be flushed, but we
2826 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002828#ifdef FEAT_TERMINAL
2829 skip_term_loop = TRUE;
2830#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002831 cmdline_row = Rows - 1; // put ':' on this line
2832 skip_redraw = TRUE; // skip redraw once
2833 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002834 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002835 // FALLTHROUGH
2836 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002837 case Ctrl_C:
2838 case ESC:
2839#ifdef FEAT_CON_DIALOG
2840 if (confirm_msg_used)
2841 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002842 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002843 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002844 }
2845 else
2846#endif
2847 {
2848 got_int = TRUE;
2849 quit_more = TRUE;
2850 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002851 // When there is some more output (wrapping line) display that
2852 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002853 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002854 break;
2855
2856#ifdef FEAT_CLIPBOARD
2857 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002858 // Strange way to allow copying (yanking) a modeless
2859 // selection at the more prompt. Use CTRL-Y,
2860 // because the same is used in Cmdline-mode and at the
2861 // hit-enter prompt. However, scrolling one line up
2862 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002863 if (clip_star.state == SELECT_DONE)
2864 clip_copy_modeless_selection(TRUE);
2865 continue;
2866#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002867 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002868 msg_moremsg(TRUE);
2869 continue;
2870 }
2871
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002872 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002874 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002875 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002876 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002877 if (mp_last == NULL)
2878 mp = msg_sb_start(last_msgchunk);
2879 else if (mp_last->sb_prev != NULL)
2880 mp = msg_sb_start(mp_last->sb_prev);
2881 else
2882 mp = NULL;
2883
Bram Moolenaar85a20022019-12-21 18:25:54 +01002884 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2886 ++i)
2887 mp = msg_sb_start(mp->sb_prev);
2888
2889 if (mp != NULL && mp->sb_prev != NULL)
2890 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002892 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002893 {
2894 if (mp == NULL || mp->sb_prev == NULL)
2895 break;
2896 mp = msg_sb_start(mp->sb_prev);
2897 if (mp_last == NULL)
2898 mp_last = msg_sb_start(last_msgchunk);
2899 else
2900 mp_last = msg_sb_start(mp_last->sb_prev);
2901 }
2902
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002903 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002904 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002905 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002906 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002907 (void)disp_sb_line(0, mp);
2908 }
2909 else
2910 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002911 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002912 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002913 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2914 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002915 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002916 ++msg_scrolled;
2917 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002918 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002919 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002920 }
2921 }
2922 else
2923 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002925 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002926 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002927 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002929 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002930 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2931 (int)Columns, ' ', ' ', 0);
2932 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002933 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002934 }
2935 }
2936
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002937 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002939 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002940 screen_fill((int)Rows - 1, (int)Rows, 0,
2941 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002942 msg_moremsg(FALSE);
2943 continue;
2944 }
2945
Bram Moolenaar85a20022019-12-21 18:25:54 +01002946 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002947 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002948 }
2949
2950 break;
2951 }
2952
Bram Moolenaar85a20022019-12-21 18:25:54 +01002953 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2955 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002956 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 if (quit_more)
2958 {
2959 msg_row = Rows - 1;
2960 msg_col = 0;
2961 }
2962#ifdef FEAT_RIGHTLEFT
2963 else if (cmdmsg_rl)
2964 msg_col = Columns - 1;
2965#endif
2966
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002967 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002968#ifdef FEAT_CON_DIALOG
2969 return retval;
2970#else
2971 return FALSE;
2972#endif
2973}
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2976
2977#ifdef mch_errmsg
2978# undef mch_errmsg
2979#endif
2980#ifdef mch_msg
2981# undef mch_msg
2982#endif
2983
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002984#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2985 static void
2986mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01002988 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002989 DWORD nwrite = 0;
2990 DWORD mode = 0;
2991 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
2992
2993 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
2994 && (int)GetConsoleCP() != enc_codepage)
2995 {
2996 WCHAR *w = enc_to_utf16((char_u *)str, &len);
2997
2998 WriteConsoleW(h, w, len, &nwrite, NULL);
2999 vim_free(w);
3000 }
3001 else
3002 {
3003 fprintf(stderr, "%s", str);
3004 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003005}
3006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003008/*
3009 * Give an error message. To be used when the screen hasn't been initialized
3010 * yet. When stderr can't be used, collect error messages until the GUI has
3011 * started and they can be displayed in a message box.
3012 */
3013 void
3014mch_errmsg(char *str)
3015{
3016#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3017 int len;
3018#endif
3019
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003020#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003021 // On Unix use stderr if it's a tty.
3022 // When not going to start the GUI also use stderr.
3023 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003025# ifdef UNIX
3026# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003028# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 isatty(2)
3030# endif
3031# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003032 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003033# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003034# endif
3035# ifdef FEAT_GUI
3036 !(gui.in_use || gui.starting)
3037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 )
3039 {
3040 fprintf(stderr, "%s", str);
3041 return;
3042 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003045#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3046# ifdef VIMDLL
3047 if (!(gui.in_use || gui.starting))
3048# endif
3049 {
3050 mch_errmsg_c(str);
3051 return;
3052 }
3053#endif
3054
3055#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003056 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 emsg_on_display = FALSE;
3058
3059 len = (int)STRLEN(str) + 1;
3060 if (error_ga.ga_growsize == 0)
3061 {
3062 error_ga.ga_growsize = 80;
3063 error_ga.ga_itemsize = 1;
3064 }
3065 if (ga_grow(&error_ga, len) == OK)
3066 {
3067 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3068 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003069# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003070 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 char_u *p;
3073
3074 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3075 for (;;)
3076 {
3077 p = vim_strchr(p, '\r');
3078 if (p == NULL)
3079 break;
3080 *p = ' ';
3081 }
3082 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003083# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003084 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003090#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3091 static void
3092mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003094 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003095 DWORD nwrite = 0;
3096 DWORD mode;
3097 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3098
3099
3100 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3101 && (int)GetConsoleCP() != enc_codepage)
3102 {
3103 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3104
3105 WriteConsoleW(h, w, len, &nwrite, NULL);
3106 vim_free(w);
3107 }
3108 else
3109 {
3110 printf("%s", str);
3111 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003112}
3113#endif
3114
3115/*
3116 * Give a message. To be used when the screen hasn't been initialized yet.
3117 * When there is no tty, collect messages until the GUI has started and they
3118 * can be displayed in a message box.
3119 */
3120 void
3121mch_msg(char *str)
3122{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003123#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003124 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3125 // uses mch_errmsg() when started from the desktop.
3126 // When not going to start the GUI also use stdout.
3127 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003129# ifdef UNIX
3130# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003132# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003136 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003138# endif
3139# ifdef FEAT_GUI
3140 !(gui.in_use || gui.starting)
3141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 )
3143 {
3144 printf("%s", str);
3145 return;
3146 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003147#endif
3148
3149#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3150# ifdef VIMDLL
3151 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003153 {
3154 mch_msg_c(str);
3155 return;
3156 }
3157#endif
3158#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003162#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164/*
3165 * Put a character on the screen at the current message position and advance
3166 * to the next position. Only for printable ASCII!
3167 */
3168 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003169msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003171 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 screen_putchar(c, msg_row, msg_col, attr);
3173#ifdef FEAT_RIGHTLEFT
3174 if (cmdmsg_rl)
3175 {
3176 if (--msg_col == 0)
3177 {
3178 msg_col = Columns;
3179 ++msg_row;
3180 }
3181 }
3182 else
3183#endif
3184 {
3185 if (++msg_col >= Columns)
3186 {
3187 msg_col = 0;
3188 ++msg_row;
3189 }
3190 }
3191}
3192
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003193 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003194msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003196 int attr;
3197 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
Bram Moolenaar8820b482017-03-16 17:23:31 +01003199 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003200 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003202 screen_puts((char_u *)
3203 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3204 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
3207/*
3208 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3209 * exmode_active.
3210 */
3211 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003212repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 if (State == ASKMORE)
3215 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003216 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 msg_row = Rows - 1;
3218 }
3219#ifdef FEAT_CON_DIALOG
3220 else if (State == CONFIRM)
3221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 msg_row = Rows - 1;
3224 }
3225#endif
3226 else if (State == EXTERNCMD)
3227 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003228 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
3230 else if (State == HITRETURN || State == SETWSIZE)
3231 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003232 if (msg_row == Rows - 1)
3233 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003234 // Avoid drawing the "hit-enter" prompt below the previous one,
3235 // overwrite it. Esp. useful when regaining focus and a
3236 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003237 msg_didout = FALSE;
3238 msg_col = 0;
3239 msg_clr_eos();
3240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 hit_return_msg();
3242 msg_row = Rows - 1;
3243 }
3244}
3245
3246/*
3247 * msg_check_screen - check if the screen is initialized.
3248 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3249 * While starting the GUI the terminal codes will be set for the GUI, but the
3250 * output goes to the terminal. Don't use the terminal codes then.
3251 */
3252 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003253msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 if (!full_screen || !screen_valid(FALSE))
3256 return FALSE;
3257
3258 if (msg_row >= Rows)
3259 msg_row = Rows - 1;
3260 if (msg_col >= Columns)
3261 msg_col = Columns - 1;
3262 return TRUE;
3263}
3264
3265/*
3266 * Clear from current message position to end of screen.
3267 * Skip this when ":silent" was used, no need to clear for redirection.
3268 */
3269 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003270msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 if (msg_silent == 0)
3273 msg_clr_eos_force();
3274}
3275
3276/*
3277 * Clear from current message position to end of screen.
3278 * Note: msg_col is not updated, so we remember the end of the message
3279 * for msg_check().
3280 */
3281 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003282msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 if (msg_use_printf())
3285 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003286 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003289 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003291 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293 }
3294 else
3295 {
3296#ifdef FEAT_RIGHTLEFT
3297 if (cmdmsg_rl)
3298 {
3299 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3300 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3301 }
3302 else
3303#endif
3304 {
3305 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3306 ' ', ' ', 0);
3307 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3308 }
3309 }
3310}
3311
3312/*
3313 * Clear the command line.
3314 */
3315 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003316msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 msg_row = cmdline_row;
3319 msg_col = 0;
3320 msg_clr_eos_force();
3321}
3322
3323/*
3324 * end putting a message on the screen
3325 * call wait_return if the message does not fit in the available space
3326 * return TRUE if wait_return not called.
3327 */
3328 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003329msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003332 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 * or the ruler option is set and we run into it,
3334 * we have to redraw the window.
3335 * Do not do this if we are abandoning the file or editing the command line.
3336 */
3337 if (!exiting && need_wait_return && !(State & CMDLINE))
3338 {
3339 wait_return(FALSE);
3340 return FALSE;
3341 }
3342 out_flush();
3343 return TRUE;
3344}
3345
3346/*
3347 * If the written message runs into the shown command or ruler, we have to
3348 * wait for hit-return and redraw the window later.
3349 */
3350 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003351msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 if (msg_row == Rows - 1 && msg_col >= sc_col)
3354 {
3355 need_wait_return = TRUE;
3356 redraw_cmdline = TRUE;
3357 }
3358}
3359
3360/*
3361 * May write a string to the redirection file.
3362 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3363 */
3364 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003365redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 char_u *s = str;
3368 static int cur_col = 0;
3369
Bram Moolenaar85a20022019-12-21 18:25:54 +01003370 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003371 if (redir_off)
3372 return;
3373
Bram Moolenaar85a20022019-12-21 18:25:54 +01003374 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003375 if (*p_vfile != NUL && verbose_fd == NULL)
3376 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003377
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003378 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003380 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (*s != '\n' && *s != '\r')
3382 {
3383 while (cur_col < msg_col)
3384 {
3385#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003386 if (redir_execute)
3387 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003388 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003390 else if (redir_vname)
3391 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003394 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003396 if (verbose_fd != NULL)
3397 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 ++cur_col;
3399 }
3400 }
3401
3402#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003403 if (redir_execute)
3404 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003405 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003407 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003408 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409#endif
3410
Bram Moolenaar85a20022019-12-21 18:25:54 +01003411 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3413 {
3414#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003415 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003417 if (redir_fd != NULL)
3418 putc(*s, redir_fd);
3419 if (verbose_fd != NULL)
3420 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (*s == '\r' || *s == '\n')
3422 cur_col = 0;
3423 else if (*s == '\t')
3424 cur_col += (8 - cur_col % 8);
3425 else
3426 ++cur_col;
3427 ++s;
3428 }
3429
Bram Moolenaar85a20022019-12-21 18:25:54 +01003430 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 msg_col = cur_col;
3432 }
3433}
3434
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003435 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003436redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003437{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003438 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003439#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003440 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003441#endif
3442 ;
3443}
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003446 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003447 * Must always be called paired with verbose_leave()!
3448 */
3449 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003450verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003451{
3452 if (*p_vfile != NUL)
3453 ++msg_silent;
3454}
3455
3456/*
3457 * After giving verbose message.
3458 * Must always be called paired with verbose_enter()!
3459 */
3460 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003461verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003462{
3463 if (*p_vfile != NUL)
3464 if (--msg_silent < 0)
3465 msg_silent = 0;
3466}
3467
3468/*
3469 * Like verbose_enter() and set msg_scroll when displaying the message.
3470 */
3471 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003472verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003473{
3474 if (*p_vfile != NUL)
3475 ++msg_silent;
3476 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003477 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003478 msg_scroll = TRUE;
3479}
3480
3481/*
3482 * Like verbose_leave() and set cmdline_row when displaying the message.
3483 */
3484 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003485verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003486{
3487 if (*p_vfile != NUL)
3488 {
3489 if (--msg_silent < 0)
3490 msg_silent = 0;
3491 }
3492 else
3493 cmdline_row = msg_row;
3494}
3495
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003496/*
3497 * Called when 'verbosefile' is set: stop writing to the file.
3498 */
3499 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003500verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003501{
3502 if (verbose_fd != NULL)
3503 {
3504 fclose(verbose_fd);
3505 verbose_fd = NULL;
3506 }
3507 verbose_did_open = FALSE;
3508}
3509
3510/*
3511 * Open the file 'verbosefile'.
3512 * Return FAIL or OK.
3513 */
3514 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003515verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003516{
3517 if (verbose_fd == NULL && !verbose_did_open)
3518 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003519 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003520 verbose_did_open = TRUE;
3521
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003522 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003523 if (verbose_fd == NULL)
3524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003525 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003526 return FAIL;
3527 }
3528 }
3529 return OK;
3530}
3531
3532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * Give a warning message (for searching).
3534 * Use 'w' highlighting and may repeat the message after redrawing
3535 */
3536 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003537give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003539 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (msg_silent != 0)
3541 return;
3542
Bram Moolenaar85a20022019-12-21 18:25:54 +01003543 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#ifdef FEAT_EVAL
3547 set_vim_var_string(VV_WARNINGMSG, message, -1);
3548#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003549 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003551 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else
3553 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003554 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003555 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003556 msg_didout = FALSE; // overwrite this message
3557 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 --no_wait_return;
3561}
3562
Bram Moolenaar113e1072019-01-20 15:30:40 +01003563#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003564 void
3565give_warning2(char_u *message, char_u *a1, int hl)
3566{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003567 if (IObuff == NULL)
3568 {
3569 // Very early in initialisation and already something wrong, just give
3570 // the raw message so the user at least gets a hint.
3571 give_warning((char_u *)message, hl);
3572 }
3573 else
3574 {
3575 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3576 give_warning(IObuff, hl);
3577 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003578}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003579#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581/*
3582 * Advance msg cursor to column "col".
3583 */
3584 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003585msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003587 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003589 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 return;
3591 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003592 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003594#ifdef FEAT_RIGHTLEFT
3595 if (cmdmsg_rl)
3596 while (msg_col > Columns - col)
3597 msg_putchar(' ');
3598 else
3599#endif
3600 while (msg_col < col)
3601 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602}
3603
3604#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3605/*
3606 * Used for "confirm()" function, and the :confirm command prefix.
3607 * Versions which haven't got flexible dialogs yet, and console
3608 * versions, get this generic handler which uses the command line.
3609 *
3610 * type = one of:
3611 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3612 * title = title string (can be NULL for default)
3613 * (neither used in console dialogs at the moment)
3614 *
3615 * Format of the "buttons" string:
3616 * "Button1Name\nButton2Name\nButton3Name"
3617 * The first button should normally be the default/accept
3618 * The second button should be the 'Cancel' button
3619 * Other buttons- use your imagination!
3620 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3621 * different letter.
3622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003624do_dialog(
3625 int type UNUSED,
3626 char_u *title UNUSED,
3627 char_u *message,
3628 char_u *buttons,
3629 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003630 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3631 // otherwise
3632 int ex_cmd) // when TRUE pressing : accepts default and starts
3633 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
3635 int oldState;
3636 int retval = 0;
3637 char_u *hotkeys;
3638 int c;
3639 int i;
3640
3641#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003642 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003644 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645#endif
3646
3647#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003648 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3650 {
3651 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003652 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003653 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003654 need_wait_return = FALSE;
3655 emsg_on_display = FALSE;
3656 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar85a20022019-12-21 18:25:54 +01003658 // Flush output to avoid that further messages and redrawing is done
3659 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 out_flush();
3661 gui_mch_update();
3662
3663 return c;
3664 }
3665#endif
3666
3667 oldState = State;
3668 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671 /*
3672 * Since we wait for a keypress, don't make the
3673 * user press RETURN as well afterwards.
3674 */
3675 ++no_wait_return;
3676 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3677
3678 if (hotkeys != NULL)
3679 {
3680 for (;;)
3681 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003682 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 c = get_keystroke();
3684 switch (c)
3685 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003686 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 case NL:
3688 retval = dfltbutton;
3689 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003690 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 case ESC:
3692 retval = 0;
3693 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003694 default: // Could be a hotkey?
3695 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003697 if (c == ':' && ex_cmd)
3698 {
3699 retval = dfltbutton;
3700 ins_char_typebuf(':');
3701 break;
3702 }
3703
Bram Moolenaar85a20022019-12-21 18:25:54 +01003704 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 c = MB_TOLOWER(c);
3706 retval = 1;
3707 for (i = 0; hotkeys[i]; ++i)
3708 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 if (has_mbyte)
3710 {
3711 if ((*mb_ptr2char)(hotkeys + i) == c)
3712 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003713 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (hotkeys[i] == c)
3717 break;
3718 ++retval;
3719 }
3720 if (hotkeys[i])
3721 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003722 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 continue;
3724 }
3725 break;
3726 }
3727
3728 vim_free(hotkeys);
3729 }
3730
3731 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 --no_wait_return;
3734 msg_end_prompt();
3735
3736 return retval;
3737}
3738
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739/*
3740 * Copy one character from "*from" to "*to", taking care of multi-byte
3741 * characters. Return the length of the character in bytes.
3742 */
3743 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003744copy_char(
3745 char_u *from,
3746 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003747 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 int len;
3750 int c;
3751
3752 if (has_mbyte)
3753 {
3754 if (lowercase)
3755 {
3756 c = MB_TOLOWER((*mb_ptr2char)(from));
3757 return (*mb_char2bytes)(c, to);
3758 }
3759 else
3760 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003761 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 mch_memmove(to, from, (size_t)len);
3763 return len;
3764 }
3765 }
3766 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
3768 if (lowercase)
3769 *to = (char_u)TOLOWER_LOC(*from);
3770 else
3771 *to = *from;
3772 return 1;
3773 }
3774}
3775
3776/*
3777 * Format the dialog string, and display it at the bottom of
3778 * the screen. Return a string of hotkey chars (if defined) for
3779 * each 'button'. If a button has no hotkey defined, the first character of
3780 * the button is used.
3781 * The hotkeys can be multi-byte characters, but without combining chars.
3782 *
3783 * Returns an allocated string with hotkeys, or NULL for error.
3784 */
3785 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003786msg_show_console_dialog(
3787 char_u *message,
3788 char_u *buttons,
3789 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
3791 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003792#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003793 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 char_u *hotk = NULL;
3795 char_u *msgp = NULL;
3796 char_u *hotkp = NULL;
3797 char_u *r;
3798 int copy;
3799#define HAS_HOTKEY_LEN 30
3800 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003801 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 int idx;
3803
3804 has_hotkey[0] = FALSE;
3805
3806 /*
3807 * First loop: compute the size of memory to allocate.
3808 * Second loop: copy to the allocated memory.
3809 */
3810 for (copy = 0; copy <= 1; ++copy)
3811 {
3812 r = buttons;
3813 idx = 0;
3814 while (*r)
3815 {
3816 if (*r == DLG_BUTTON_SEP)
3817 {
3818 if (copy)
3819 {
3820 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003821 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaar85a20022019-12-21 18:25:54 +01003823 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003825 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003828 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (dfltbutton)
3830 --dfltbutton;
3831
Bram Moolenaar85a20022019-12-21 18:25:54 +01003832 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3834 first_hotkey = TRUE;
3835 }
3836 else
3837 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003838 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3839 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (idx < HAS_HOTKEY_LEN - 1)
3841 has_hotkey[++idx] = FALSE;
3842 }
3843 }
3844 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3845 {
3846 if (*r == DLG_HOTKEY_CHAR)
3847 ++r;
3848 first_hotkey = FALSE;
3849 if (copy)
3850 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003851 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 *msgp++ = *r;
3853 else
3854 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003855 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3857 msgp += copy_char(r, msgp, FALSE);
3858 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3859
Bram Moolenaar85a20022019-12-21 18:25:54 +01003860 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003861 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 }
3864 else
3865 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003866 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (idx < HAS_HOTKEY_LEN - 1)
3868 has_hotkey[idx] = TRUE;
3869 }
3870 }
3871 else
3872 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003873 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (copy)
3875 msgp += copy_char(r, msgp, FALSE);
3876 }
3877
Bram Moolenaar85a20022019-12-21 18:25:54 +01003878 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003879 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882 if (copy)
3883 {
3884 *msgp++ = ':';
3885 *msgp++ = ' ';
3886 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888 else
3889 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003890 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003891 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003892 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003893 + 3); // for the ": " and NUL
3894 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
Bram Moolenaar85a20022019-12-21 18:25:54 +01003896 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (!has_hotkey[0])
3898 {
3899 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003900 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
3903 /*
3904 * Now allocate and load the strings
3905 */
3906 vim_free(confirm_msg);
3907 confirm_msg = alloc(len);
3908 if (confirm_msg == NULL)
3909 return NULL;
3910 *confirm_msg = NUL;
3911 hotk = alloc(lenhotkey);
3912 if (hotk == NULL)
3913 return NULL;
3914
3915 *confirm_msg = '\n';
3916 STRCPY(confirm_msg + 1, message);
3917
3918 msgp = confirm_msg + 1 + STRLEN(message);
3919 hotkp = hotk;
3920
Bram Moolenaar85a20022019-12-21 18:25:54 +01003921 // Define first default hotkey. Keep the hotkey string NUL
3922 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003923 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar85a20022019-12-21 18:25:54 +01003925 // Remember where the choices start, displaying starts here when
3926 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 confirm_msg_tail = msgp;
3928 *msgp++ = '\n';
3929 }
3930 }
3931
3932 display_confirm_msg();
3933 return hotk;
3934}
3935
3936/*
3937 * Display the ":confirm" message. Also called when screen resized.
3938 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003939 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003940display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003942 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 ++confirm_msg_used;
3944 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003945 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 --confirm_msg_used;
3947}
3948
Bram Moolenaar85a20022019-12-21 18:25:54 +01003949#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
3951#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3952
3953 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003954vim_dialog_yesno(
3955 int type,
3956 char_u *title,
3957 char_u *message,
3958 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
3960 if (do_dialog(type,
3961 title == NULL ? (char_u *)_("Question") : title,
3962 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003963 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return VIM_YES;
3965 return VIM_NO;
3966}
3967
3968 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003969vim_dialog_yesnocancel(
3970 int type,
3971 char_u *title,
3972 char_u *message,
3973 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 switch (do_dialog(type,
3976 title == NULL ? (char_u *)_("Question") : title,
3977 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003978 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 {
3980 case 1: return VIM_YES;
3981 case 2: return VIM_NO;
3982 }
3983 return VIM_CANCEL;
3984}
3985
3986 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003987vim_dialog_yesnoallcancel(
3988 int type,
3989 char_u *title,
3990 char_u *message,
3991 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 switch (do_dialog(type,
3994 title == NULL ? (char_u *)"Question" : title,
3995 message,
3996 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003997 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
3999 case 1: return VIM_YES;
4000 case 2: return VIM_NO;
4001 case 3: return VIM_ALL;
4002 case 4: return VIM_DISCARDALL;
4003 }
4004 return VIM_CANCEL;
4005}
4006
Bram Moolenaar85a20022019-12-21 18:25:54 +01004007#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004009#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004010static char *e_printf = N_("E766: Insufficient arguments for printf()");
4011
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004012/*
4013 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4014 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004015 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004016tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004017{
4018 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004019 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004020 int err = FALSE;
4021
4022 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004023 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 else
4025 {
4026 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004027 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004028 if (err)
4029 n = 0;
4030 }
4031 return n;
4032}
4033
4034/*
4035 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004036 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004037 * are not converted to a string.
4038 * If "tofree" is not NULL echo_string() is used. All types are converted to
4039 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004040 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041 */
4042 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004043tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004045 int idx = *idxp - 1;
4046 char *s = NULL;
4047 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004048
4049 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004050 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004051 else
4052 {
4053 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004054 if (tofree != NULL)
4055 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4056 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004057 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 }
4059 return s;
4060}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004061
4062# ifdef FEAT_FLOAT
4063/*
4064 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4065 */
4066 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004067tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004068{
4069 int idx = *idxp - 1;
4070 double f = 0;
4071
4072 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004073 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004074 else
4075 {
4076 ++*idxp;
4077 if (tvs[idx].v_type == VAR_FLOAT)
4078 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004079 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004080 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004081 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004082 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004083 }
4084 return f;
4085}
4086# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087#endif
4088
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004089#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004090/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004091 * Return the representation of infinity for printf() function:
4092 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4093 */
4094 static const char *
4095infinity_str(int positive,
4096 char fmt_spec,
4097 int force_sign,
4098 int space_for_positive)
4099{
4100 static const char *table[] =
4101 {
4102 "-inf", "inf", "+inf", " inf",
4103 "-INF", "INF", "+INF", " INF"
4104 };
4105 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4106
4107 if (ASCII_ISUPPER(fmt_spec))
4108 idx += 4;
4109 return table[idx];
4110}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004111#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004112
4113/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004114 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004115 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004116 * consistency.
4117 *
4118 * This code is based on snprintf.c - a portable implementation of snprintf
4119 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004120 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004121 * The original code, including useful comments, can be found here:
4122 * http://www.ijs.si/software/snprintf/
4123 *
4124 * This snprintf() only supports the following conversion specifiers:
4125 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4126 * with flags: '-', '+', ' ', '0' and '#'.
4127 * An asterisk is supported for field width as well as precision.
4128 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004129 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004130 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004131 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4132 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004133 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004134 * The locale is not used, the string is used as a byte string. This is only
4135 * relevant for double-byte encodings where the second byte may be '%'.
4136 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004137 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4138 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004139 *
4140 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004141 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004142 * is greater or equal to "str_m", not all characters from the result
4143 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4144 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004145 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004146 */
4147
4148/*
4149 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004151 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004152 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004153 */
4154
Bram Moolenaar85a20022019-12-21 18:25:54 +01004155// When generating prototypes all of this is skipped, cproto doesn't
4156// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004157#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004158
Bram Moolenaar85a20022019-12-21 18:25:54 +01004159// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004160 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004161vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004162{
4163 va_list ap;
4164 int str_l;
4165 size_t len = STRLEN(str);
4166 size_t space;
4167
4168 if (str_m <= len)
4169 space = 0;
4170 else
4171 space = str_m - len;
4172 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004173 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004174 va_end(ap);
4175 return str_l;
4176}
Bram Moolenaara800b422010-06-27 01:15:55 +02004177
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004178 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004179vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004180{
4181 va_list ap;
4182 int str_l;
4183
4184 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004185 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004186 va_end(ap);
4187 return str_l;
4188}
4189
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004190 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004191vim_vsnprintf(
4192 char *str,
4193 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004194 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004195 va_list ap)
4196{
4197 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4198}
4199
4200 int
4201vim_vsnprintf_typval(
4202 char *str,
4203 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004204 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004205 va_list ap,
4206 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207{
4208 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004209 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004210 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004211
4212 if (p == NULL)
4213 p = "";
4214 while (*p != NUL)
4215 {
4216 if (*p != '%')
4217 {
4218 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004219 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004220
Bram Moolenaar85a20022019-12-21 18:25:54 +01004221 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004222 if (str_l < str_m)
4223 {
4224 size_t avail = str_m - str_l;
4225
4226 mch_memmove(str + str_l, p, n > avail ? avail : n);
4227 }
4228 p += n;
4229 str_l += n;
4230 }
4231 else
4232 {
4233 size_t min_field_width = 0, precision = 0;
4234 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4235 int alternate_form = 0, force_sign = 0;
4236
Bram Moolenaar85a20022019-12-21 18:25:54 +01004237 // If both the ' ' and '+' flags appear, the ' ' flag should be
4238 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004239 int space_for_positive = 1;
4240
Bram Moolenaar85a20022019-12-21 18:25:54 +01004241 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004242 char length_modifier = '\0';
4243
Bram Moolenaar85a20022019-12-21 18:25:54 +01004244 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004245# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004246# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4247 // That sounds reasonable to use as the maximum
4248 // printable.
Bram Moolenaar91984b92016-08-16 21:58:41 +02004249# elif defined(FEAT_NUM64)
4250# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004251# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004252# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004253# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004254 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004255
Bram Moolenaar85a20022019-12-21 18:25:54 +01004256 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004257 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004258
Bram Moolenaar85a20022019-12-21 18:25:54 +01004259 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004260 size_t str_arg_l;
4261
Bram Moolenaar85a20022019-12-21 18:25:54 +01004262 // unsigned char argument value - only defined for c conversion.
4263 // N.B. standard explicitly states the char argument for the c
4264 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004265 unsigned char uchar_arg;
4266
Bram Moolenaar85a20022019-12-21 18:25:54 +01004267 // number of zeros to be inserted for numeric conversions as
4268 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004269 size_t number_of_zeros_to_pad = 0;
4270
Bram Moolenaar85a20022019-12-21 18:25:54 +01004271 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004272 size_t zero_padding_insertion_ind = 0;
4273
Bram Moolenaar85a20022019-12-21 18:25:54 +01004274 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004275 char fmt_spec = '\0';
4276
Bram Moolenaar85a20022019-12-21 18:25:54 +01004277 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004278 char_u *tofree = NULL;
4279
4280
Bram Moolenaar85a20022019-12-21 18:25:54 +01004281 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004282
Bram Moolenaar85a20022019-12-21 18:25:54 +01004283 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004284 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4285 || *p == '#' || *p == '\'')
4286 {
4287 switch (*p)
4288 {
4289 case '0': zero_padding = 1; break;
4290 case '-': justify_left = 1; break;
4291 case '+': force_sign = 1; space_for_positive = 0; break;
4292 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004293 // If both the ' ' and '+' flags appear, the ' '
4294 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004295 break;
4296 case '#': alternate_form = 1; break;
4297 case '\'': break;
4298 }
4299 p++;
4300 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004301 // If the '0' and '-' flags both appear, the '0' flag should be
4302 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303
Bram Moolenaar85a20022019-12-21 18:25:54 +01004304 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004305 if (*p == '*')
4306 {
4307 int j;
4308
4309 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004311# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004312 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004313# endif
4314 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004315 if (j >= 0)
4316 min_field_width = j;
4317 else
4318 {
4319 min_field_width = -j;
4320 justify_left = 1;
4321 }
4322 }
4323 else if (VIM_ISDIGIT((int)(*p)))
4324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004325 // size_t could be wider than unsigned int; make sure we treat
4326 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004327 unsigned int uj = *p++ - '0';
4328
4329 while (VIM_ISDIGIT((int)(*p)))
4330 uj = 10 * uj + (unsigned int)(*p++ - '0');
4331 min_field_width = uj;
4332 }
4333
Bram Moolenaar85a20022019-12-21 18:25:54 +01004334 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004335 if (*p == '.')
4336 {
4337 p++;
4338 precision_specified = 1;
4339 if (*p == '*')
4340 {
4341 int j;
4342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004343 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004345 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004346# endif
4347 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004348 p++;
4349 if (j >= 0)
4350 precision = j;
4351 else
4352 {
4353 precision_specified = 0;
4354 precision = 0;
4355 }
4356 }
4357 else if (VIM_ISDIGIT((int)(*p)))
4358 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004359 // size_t could be wider than unsigned int; make sure we
4360 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004361 unsigned int uj = *p++ - '0';
4362
4363 while (VIM_ISDIGIT((int)(*p)))
4364 uj = 10 * uj + (unsigned int)(*p++ - '0');
4365 precision = uj;
4366 }
4367 }
4368
Bram Moolenaar85a20022019-12-21 18:25:54 +01004369 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004370 if (*p == 'h' || *p == 'l')
4371 {
4372 length_modifier = *p;
4373 p++;
4374 if (length_modifier == 'l' && *p == 'l')
4375 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004376 // double l = long long
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004377# ifdef FEAT_NUM64
4378 length_modifier = 'L';
4379# else
Bram Moolenaar85a20022019-12-21 18:25:54 +01004380 length_modifier = 'l'; // treat it as a single 'l'
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004381# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004382 p++;
4383 }
4384 }
4385 fmt_spec = *p;
4386
Bram Moolenaar85a20022019-12-21 18:25:54 +01004387 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004388 switch (fmt_spec)
4389 {
4390 case 'i': fmt_spec = 'd'; break;
4391 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4392 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4393 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4394 default: break;
4395 }
4396
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004397# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4398 switch (fmt_spec)
4399 {
4400 case 'd': case 'u': case 'o': case 'x': case 'X':
4401 if (tvs != NULL && length_modifier == '\0')
4402 length_modifier = 'L';
4403 }
4404# endif
4405
Bram Moolenaar85a20022019-12-21 18:25:54 +01004406 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004407 switch (fmt_spec)
4408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004409 // '%' and 'c' behave similar to 's' regarding flags and field
4410 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004411 case '%':
4412 case 'c':
4413 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004414 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004415 str_arg_l = 1;
4416 switch (fmt_spec)
4417 {
4418 case '%':
4419 str_arg = p;
4420 break;
4421
4422 case 'c':
4423 {
4424 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425
4426 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004428 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429# endif
4430 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004431 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432 uchar_arg = (unsigned char)j;
4433 str_arg = (char *)&uchar_arg;
4434 break;
4435 }
4436
4437 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004438 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004441 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442# endif
4443 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004444 if (str_arg == NULL)
4445 {
4446 str_arg = "[NULL]";
4447 str_arg_l = 6;
4448 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004449 // make sure not to address string beyond the specified
4450 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004451 else if (!precision_specified)
4452 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004453 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004454 else if (precision == 0)
4455 str_arg_l = 0;
4456 else
4457 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004458 // Don't put the #if inside memchr(), it can be a
4459 // macro.
4460 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004462 precision <= (size_t)0x7fffffffL ? precision
4463 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004464 str_arg_l = (q == NULL) ? precision
4465 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004466 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004467 if (fmt_spec == 'S')
4468 {
4469 if (min_field_width != 0)
4470 min_field_width += STRLEN(str_arg)
4471 - mb_string2cells((char_u *)str_arg, -1);
4472 if (precision)
4473 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004474 char_u *p1;
4475 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004476
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004477 for (p1 = (char_u *)str_arg; *p1;
4478 p1 += mb_ptr2len(p1))
4479 {
4480 i += (size_t)mb_ptr2cells(p1);
4481 if (i > precision)
4482 break;
4483 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004484 str_arg_l = precision = p1 - (char_u *)str_arg;
4485 }
4486 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004487 break;
4488
4489 default:
4490 break;
4491 }
4492 break;
4493
Bram Moolenaar91984b92016-08-16 21:58:41 +02004494 case 'd': case 'u':
4495 case 'b': case 'B':
4496 case 'o':
4497 case 'x': case 'X':
4498 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004499 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004500 // NOTE: the u, b, o, x, X and p conversion specifiers
4501 // imply the value is unsigned; d implies a signed
4502 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004503
Bram Moolenaar85a20022019-12-21 18:25:54 +01004504 // 0 if numeric argument is zero (or if pointer is
4505 // NULL for 'p'), +1 if greater than zero (or nonzero
4506 // for unsigned arguments), -1 if negative (unsigned
4507 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004508 int arg_sign = 0;
4509
Bram Moolenaar85a20022019-12-21 18:25:54 +01004510 // only defined for length modifier h, or for no
4511 // length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004512 int int_arg = 0;
4513 unsigned int uint_arg = 0;
4514
Bram Moolenaar85a20022019-12-21 18:25:54 +01004515 // only defined for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004516 long int long_arg = 0;
4517 unsigned long int ulong_arg = 0;
4518
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004519# ifdef FEAT_NUM64
Bram Moolenaar85a20022019-12-21 18:25:54 +01004520 // only defined for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004521 varnumber_T llong_arg = 0;
4522 uvarnumber_T ullong_arg = 0;
4523# endif
4524
Bram Moolenaar85a20022019-12-21 18:25:54 +01004525 // only defined for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004526 uvarnumber_T bin_arg = 0;
4527
Bram Moolenaar85a20022019-12-21 18:25:54 +01004528 // pointer argument value -only defined for p
4529 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004530 void *ptr_arg = NULL;
4531
4532 if (fmt_spec == 'p')
4533 {
4534 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004537 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4538 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539# endif
4540 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004541 if (ptr_arg != NULL)
4542 arg_sign = 1;
4543 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004544 else if (fmt_spec == 'b' || fmt_spec == 'B')
4545 {
4546 bin_arg =
4547# if defined(FEAT_EVAL)
4548 tvs != NULL ?
4549 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4550# endif
4551 va_arg(ap, uvarnumber_T);
4552 if (bin_arg != 0)
4553 arg_sign = 1;
4554 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004555 else if (fmt_spec == 'd')
4556 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004557 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004558 switch (length_modifier)
4559 {
4560 case '\0':
4561 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004562 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004565 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566# endif
4567 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004568 if (int_arg > 0)
4569 arg_sign = 1;
4570 else if (int_arg < 0)
4571 arg_sign = -1;
4572 break;
4573 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004576 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577# endif
4578 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004579 if (long_arg > 0)
4580 arg_sign = 1;
4581 else if (long_arg < 0)
4582 arg_sign = -1;
4583 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004584# ifdef FEAT_NUM64
4585 case 'L':
4586 llong_arg =
4587# if defined(FEAT_EVAL)
4588 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4589# endif
4590 va_arg(ap, varnumber_T);
4591 if (llong_arg > 0)
4592 arg_sign = 1;
4593 else if (llong_arg < 0)
4594 arg_sign = -1;
4595 break;
4596# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004597 }
4598 }
4599 else
4600 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004601 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004602 switch (length_modifier)
4603 {
4604 case '\0':
4605 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004608 tvs != NULL ? (unsigned)
4609 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610# endif
4611 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004612 if (uint_arg != 0)
4613 arg_sign = 1;
4614 break;
4615 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004618 tvs != NULL ? (unsigned long)
4619 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620# endif
4621 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004622 if (ulong_arg != 0)
4623 arg_sign = 1;
4624 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004625# ifdef FEAT_NUM64
4626 case 'L':
4627 ullong_arg =
4628# if defined(FEAT_EVAL)
4629 tvs != NULL ? (uvarnumber_T)
4630 tv_nr(tvs, &arg_idx) :
4631# endif
4632 va_arg(ap, uvarnumber_T);
4633 if (ullong_arg != 0)
4634 arg_sign = 1;
4635 break;
4636# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004637 }
4638 }
4639
4640 str_arg = tmp;
4641 str_arg_l = 0;
4642
Bram Moolenaar85a20022019-12-21 18:25:54 +01004643 // NOTE:
4644 // For d, i, u, o, x, and X conversions, if precision is
4645 // specified, the '0' flag should be ignored. This is so
4646 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4647 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004648 if (precision_specified)
4649 zero_padding = 0;
4650 if (fmt_spec == 'd')
4651 {
4652 if (force_sign && arg_sign >= 0)
4653 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004654 // leave negative numbers for sprintf to handle, to
4655 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004656 }
4657 else if (alternate_form)
4658 {
4659 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004660 && (fmt_spec == 'b' || fmt_spec == 'B'
4661 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004662 {
4663 tmp[str_arg_l++] = '0';
4664 tmp[str_arg_l++] = fmt_spec;
4665 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004666 // alternate form should have no effect for p
4667 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004668 }
4669
4670 zero_padding_insertion_ind = str_arg_l;
4671 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004672 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004673 if (precision == 0 && arg_sign == 0)
4674 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004675 // When zero value is formatted with an explicit
4676 // precision 0, the resulting formatted string is
4677 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004678 }
4679 else
4680 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004681 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004682 int f_l = 0;
4683
Bram Moolenaar85a20022019-12-21 18:25:54 +01004684 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004685 f[f_l++] = '%';
4686 if (!length_modifier)
4687 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004688 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004689 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004690# ifdef FEAT_NUM64
Bram Moolenaar4f974752019-02-17 17:44:42 +01004691# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004692 f[f_l++] = 'I';
4693 f[f_l++] = '6';
4694 f[f_l++] = '4';
4695# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004696 f[f_l++] = 'l';
4697 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004698# endif
4699# else
4700 f[f_l++] = 'l';
4701# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004702 }
4703 else
4704 f[f_l++] = length_modifier;
4705 f[f_l++] = fmt_spec;
4706 f[f_l++] = '\0';
4707
4708 if (fmt_spec == 'p')
4709 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004710 else if (fmt_spec == 'b' || fmt_spec == 'B')
4711 {
4712 char b[8 * sizeof(uvarnumber_T)];
4713 size_t b_l = 0;
4714 uvarnumber_T bn = bin_arg;
4715
4716 do
4717 {
4718 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4719 bn >>= 1;
4720 }
4721 while (bn != 0);
4722
4723 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4724 str_arg_l += b_l;
4725 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004726 else if (fmt_spec == 'd')
4727 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004728 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004729 switch (length_modifier)
4730 {
4731 case '\0':
4732 case 'h': str_arg_l += sprintf(
4733 tmp + str_arg_l, f, int_arg);
4734 break;
4735 case 'l': str_arg_l += sprintf(
4736 tmp + str_arg_l, f, long_arg);
4737 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004738# ifdef FEAT_NUM64
4739 case 'L': str_arg_l += sprintf(
4740 tmp + str_arg_l, f, llong_arg);
4741 break;
4742# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 }
4744 }
4745 else
4746 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004747 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004748 switch (length_modifier)
4749 {
4750 case '\0':
4751 case 'h': str_arg_l += sprintf(
4752 tmp + str_arg_l, f, uint_arg);
4753 break;
4754 case 'l': str_arg_l += sprintf(
4755 tmp + str_arg_l, f, ulong_arg);
4756 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004757# ifdef FEAT_NUM64
4758 case 'L': str_arg_l += sprintf(
4759 tmp + str_arg_l, f, ullong_arg);
4760 break;
4761# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004762 }
4763 }
4764
Bram Moolenaar85a20022019-12-21 18:25:54 +01004765 // include the optional minus sign and possible
4766 // "0x" in the region before the zero padding
4767 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004768 if (zero_padding_insertion_ind < str_arg_l
4769 && tmp[zero_padding_insertion_ind] == '-')
4770 zero_padding_insertion_ind++;
4771 if (zero_padding_insertion_ind + 1 < str_arg_l
4772 && tmp[zero_padding_insertion_ind] == '0'
4773 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4774 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4775 zero_padding_insertion_ind += 2;
4776 }
4777
4778 {
4779 size_t num_of_digits = str_arg_l
4780 - zero_padding_insertion_ind;
4781
4782 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004783 // unless zero is already the first
4784 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004785 && !(zero_padding_insertion_ind < str_arg_l
4786 && tmp[zero_padding_insertion_ind] == '0'))
4787 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004788 // assure leading zero for alternate-form
4789 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004790 if (!precision_specified
4791 || precision < num_of_digits + 1)
4792 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004793 // precision is increased to force the
4794 // first character to be zero, except if a
4795 // zero value is formatted with an
4796 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004797 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004798 }
4799 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004800 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004801 if (num_of_digits < precision)
4802 number_of_zeros_to_pad = precision - num_of_digits;
4803 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004804 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004805 if (!justify_left && zero_padding)
4806 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004807 int n = (int)(min_field_width - (str_arg_l
4808 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004809 if (n > 0)
4810 number_of_zeros_to_pad += n;
4811 }
4812 break;
4813 }
4814
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004815# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004816 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004817 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004818 case 'e':
4819 case 'E':
4820 case 'g':
4821 case 'G':
4822 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004823 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004824 double f;
4825 double abs_f;
4826 char format[40];
4827 int l;
4828 int remove_trailing_zeroes = FALSE;
4829
4830 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004831# if defined(FEAT_EVAL)
4832 tvs != NULL ? tv_float(tvs, &arg_idx) :
4833# endif
4834 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004835 abs_f = f < 0 ? -f : f;
4836
4837 if (fmt_spec == 'g' || fmt_spec == 'G')
4838 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004839 // Would be nice to use %g directly, but it prints
4840 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004841 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4842 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004843 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004844 else
4845 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4846 remove_trailing_zeroes = TRUE;
4847 }
4848
Bram Moolenaar04186092016-08-29 21:55:35 +02004849 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004850# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004851 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004852# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004853 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004854# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004855 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004856 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004857 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004858 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4859 force_sign, space_for_positive));
4860 str_arg_l = STRLEN(tmp);
4861 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004862 }
4863 else
4864 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004865 if (isnan(f))
4866 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004867 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004868 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4869 : "nan");
4870 str_arg_l = 3;
4871 zero_padding = 0;
4872 }
4873 else if (isinf(f))
4874 {
4875 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4876 force_sign, space_for_positive));
4877 str_arg_l = STRLEN(tmp);
4878 zero_padding = 0;
4879 }
4880 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004881 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004882 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004883 format[0] = '%';
4884 l = 1;
4885 if (force_sign)
4886 format[l++] = space_for_positive ? ' ' : '+';
4887 if (precision_specified)
4888 {
4889 size_t max_prec = TMP_LEN - 10;
4890
Bram Moolenaar85a20022019-12-21 18:25:54 +01004891 // Make sure we don't get more digits than we
4892 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004893 if ((fmt_spec == 'f' || fmt_spec == 'F')
4894 && abs_f > 1.0)
4895 max_prec -= (size_t)log10(abs_f);
4896 if (precision > max_prec)
4897 precision = max_prec;
4898 l += sprintf(format + l, ".%d", (int)precision);
4899 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004900 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004901 format[l + 1] = NUL;
4902
Bram Moolenaare9997822016-08-28 16:03:38 +02004903 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004904 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004905
Bram Moolenaara7241f52008-06-24 20:39:31 +00004906 if (remove_trailing_zeroes)
4907 {
4908 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004909 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004910
Bram Moolenaar85a20022019-12-21 18:25:54 +01004911 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004912 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004913 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004914 else
4915 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004916 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004917 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004918 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004919 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004920 // Remove superfluous '+' and leading
4921 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004922 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004923 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004924 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004925 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004926 --str_arg_l;
4927 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004928 i = (tp[1] == '-') ? 2 : 1;
4929 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004930 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004931 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004932 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004933 --str_arg_l;
4934 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004935 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004936 }
4937 }
4938
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004939 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004940 // Remove trailing zeroes, but keep the one
4941 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004942 while (tp > tmp + 2 && *tp == '0'
4943 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004944 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004945 STRMOVE(tp, tp + 1);
4946 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004947 --str_arg_l;
4948 }
4949 }
4950 else
4951 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004952 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004953
Bram Moolenaar85a20022019-12-21 18:25:54 +01004954 // Be consistent: some printf("%e") use 1.0e+12
4955 // and some 1.0e+012. Remove one zero in the last
4956 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004957 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004958 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004959 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4960 && tp[2] == '0'
4961 && vim_isdigit(tp[3])
4962 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004963 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004964 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004965 --str_arg_l;
4966 }
4967 }
4968 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004969 if (zero_padding && min_field_width > str_arg_l
4970 && (tmp[0] == '-' || force_sign))
4971 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004972 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02004973 number_of_zeros_to_pad = min_field_width - str_arg_l;
4974 zero_padding_insertion_ind = 1;
4975 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004976 str_arg = tmp;
4977 break;
4978 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004979# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004980
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004981 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01004982 // unrecognized conversion specifier, keep format string
4983 // as-is
4984 zero_padding = 0; // turn zero padding off for non-numeric
4985 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004986 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004987 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004988
Bram Moolenaar85a20022019-12-21 18:25:54 +01004989 // discard the unrecognized conversion, just keep *
4990 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004991 str_arg = p;
4992 str_arg_l = 0;
4993 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004994 str_arg_l++; // include invalid conversion specifier
4995 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004996 break;
4997 }
4998
4999 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005000 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005001
Bram Moolenaar85a20022019-12-21 18:25:54 +01005002 // insert padding to the left as requested by min_field_width;
5003 // this does not include the zero padding in case of numerical
5004 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005005 if (!justify_left)
5006 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005007 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005008 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005009
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005010 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005011 {
5012 if (str_l < str_m)
5013 {
5014 size_t avail = str_m - str_l;
5015
5016 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005017 (size_t)pn > avail ? avail
5018 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005019 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005020 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005021 }
5022 }
5023
Bram Moolenaar85a20022019-12-21 18:25:54 +01005024 // zero padding as requested by the precision or by the minimal
5025 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005026 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005027 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005028 // will not copy first part of numeric right now, *
5029 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005030 zero_padding_insertion_ind = 0;
5031 }
5032 else
5033 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005034 // insert first part of numerics (sign or '0x') before zero
5035 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005036 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005037
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005038 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005039 {
5040 if (str_l < str_m)
5041 {
5042 size_t avail = str_m - str_l;
5043
5044 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005045 (size_t)zn > avail ? avail
5046 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005047 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005048 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005049 }
5050
Bram Moolenaar85a20022019-12-21 18:25:54 +01005051 // insert zero padding as requested by the precision or min
5052 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005053 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005054 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005055 {
5056 if (str_l < str_m)
5057 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005058 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005059
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005060 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005061 (size_t)zn > avail ? avail
5062 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005064 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005065 }
5066 }
5067
Bram Moolenaar85a20022019-12-21 18:25:54 +01005068 // insert formatted string
5069 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005070 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005071 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005073 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005074 {
5075 if (str_l < str_m)
5076 {
5077 size_t avail = str_m - str_l;
5078
5079 mch_memmove(str + str_l,
5080 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005081 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005082 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005083 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005084 }
5085 }
5086
Bram Moolenaar85a20022019-12-21 18:25:54 +01005087 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005088 if (justify_left)
5089 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005090 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005091 int pn = (int)(min_field_width
5092 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005093
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005094 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005095 {
5096 if (str_l < str_m)
5097 {
5098 size_t avail = str_m - str_l;
5099
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005100 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005101 (size_t)pn > avail ? avail
5102 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005104 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005105 }
5106 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005107 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005108 }
5109 }
5110
5111 if (str_m > 0)
5112 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005113 // make sure the string is nul-terminated even at the expense of
5114 // overwriting the last character (shouldn't happen, but just in case)
5115 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005116 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5117 }
5118
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005119 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005120 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121
Bram Moolenaar85a20022019-12-21 18:25:54 +01005122 // Return the number of characters formatted (excluding trailing nul
5123 // character), that is, the number of characters that would have been
5124 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005125 return (int)str_l;
5126}
5127
Bram Moolenaar85a20022019-12-21 18:25:54 +01005128#endif // PROTO