blob: a9851efab0c9b87c18b30989cf9af55c30ef667c [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 Moolenaara5d04232020-07-26 15:37:02 +0200464 char_u *sname = estack_sfile(FALSE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100465 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 Moolenaar9b7bf9e2020-07-11 22:14:59 +0200657 if (emsg_assert_fails_used && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200658 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200659 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200660 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200661 vim_free(emsg_assert_fails_context);
662 emsg_assert_fails_context = vim_strsave(
663 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200664 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200665
Bram Moolenaar85a20022019-12-21 18:25:54 +0100666 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 set_vim_var_string(VV_ERRMSG, s, -1);
668#endif
669
670 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000671 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 * But do write it to the redirection file.
673 */
674 if (emsg_silent != 0)
675 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200676 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200678 msg_start();
679 p = get_emsg_source();
680 if (p != NULL)
681 {
682 STRCAT(p, "\n");
683 redir_write(p, -1);
684 vim_free(p);
685 }
686 p = get_emsg_lnum();
687 if (p != NULL)
688 {
689 STRCAT(p, "\n");
690 redir_write(p, -1);
691 vim_free(p);
692 }
693 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100695#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200696 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 return TRUE;
699 }
700
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100701 ex_exitval = 1;
702
Bram Moolenaar85a20022019-12-21 18:25:54 +0100703 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 msg_silent = 0;
705 cmd_silent = FALSE;
706
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ++global_busy;
709
710 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200713 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100714 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200715#ifdef FEAT_EVAL
716 did_uncaught_emsg = TRUE;
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719
Bram Moolenaar85a20022019-12-21 18:25:54 +0100720 emsg_on_display = TRUE; // remember there is an error message
721 ++msg_scroll; // don't overwrite a previous message
722 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000723 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100724 need_wait_return = TRUE; // needed in case emsg() is called after
725 // wait_return has reset need_wait_return
726 // and a redraw is expected because
727 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
Bram Moolenaar958dc692016-12-01 15:34:12 +0100729#ifdef FEAT_JOB_CHANNEL
730 emsg_to_channel_log = TRUE;
731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 /*
733 * Display name and line number for the source of the error.
734 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000735 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737 /*
738 * Display the error message itself.
739 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100741 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100742
743#ifdef FEAT_JOB_CHANNEL
744 emsg_to_channel_log = FALSE;
745#endif
746 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747}
748
749/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100750 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
752 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100755 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100756 if (!emsg_not_now())
757 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100758 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759}
760
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100761#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100762/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100763 * Print an error message with format string and variable arguments.
764 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100765 */
766 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100767semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100768{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200769 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100770 if (!emsg_not_now())
771 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200772 if (IObuff == NULL)
773 {
774 // Very early in initialisation and already something wrong, just
775 // give the raw message so the user at least gets a hint.
776 return emsg_core((char_u *)s);
777 }
778 else
779 {
780 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100781
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200782 va_start(ap, s);
783 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
784 va_end(ap);
785 return emsg_core(IObuff);
786 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200788 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100789}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100790#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100791
792/*
793 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
794 * defined. It is used for internal errors only, so that they can be
795 * detected when fuzzing vim.
796 */
797 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100800 if (!emsg_not_now())
801 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100802#ifdef ABORT_ON_INTERNAL_ERROR
803 abort();
804#endif
805}
806
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100807#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100808/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100809 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100810 * defined. It is used for internal errors only, so that they can be
811 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100812 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100813 */
814 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100815siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100816{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100817 if (!emsg_not_now())
818 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200819 if (IObuff == NULL)
820 {
821 // Very early in initialisation and already something wrong, just
822 // give the raw message so the user at least gets a hint.
823 emsg_core((char_u *)s);
824 }
825 else
826 {
827 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200829 va_start(ap, s);
830 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
831 va_end(ap);
832 emsg_core(IObuff);
833 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100834 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100835# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100836 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100837# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100838}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100839#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100840
841/*
842 * Give an "Internal error" message.
843 */
844 void
845internal_error(char *where)
846{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100848}
849
Bram Moolenaardd589232020-02-29 17:38:12 +0100850/*
851 * Like internal_error() but do not call abort(), to avoid tests using
852 * test_unknown() and test_void() causing Vim to exit.
853 */
854 void
855internal_error_no_abort(char *where)
856{
857 semsg(_(e_intern2), where);
858}
859
Bram Moolenaar85a20022019-12-21 18:25:54 +0100860// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
Bram Moolenaardf177f62005-02-22 08:39:57 +0000862 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100863emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000864{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000866}
867
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 * Give an error message which contains %s for "name[len]".
870 */
871 void
872emsg_namelen(char *msg, char_u *name, int len)
873{
874 char_u *copy = vim_strnsave((char_u *)name, len);
875
876 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100877 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878}
879
880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 * Like msg(), but truncate to a single line if p_shm contains 't', or when
882 * "force" is TRUE. This truncates in another way as for normal messages.
883 * Careful: The string may be changed by msg_may_trunc()!
884 * Returns a pointer to the printed message, if wait_return() not called.
885 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100886 char *
887msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
889 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100890 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaar85a20022019-12-21 18:25:54 +0100892 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100893 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaar32526b32019-01-19 17:43:09 +0100895 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100898 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 msg_hist_off = FALSE;
900
901 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100902 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 return NULL;
904}
905
906/*
907 * Check if message "s" should be truncated at the start (for filenames).
908 * Return a pointer to where the truncated message starts.
909 * Note: May change the message by replacing a character with '<'.
910 */
911 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100912msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913{
914 int n;
915 int room;
916
917 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
918 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
919 && (n = (int)STRLEN(s) - room) > 0)
920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (has_mbyte)
922 {
923 int size = vim_strsize(s);
924
Bram Moolenaar85a20022019-12-21 18:25:54 +0100925 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000926 if (size <= room)
927 return s;
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 for (n = 0; size >= room; )
930 {
931 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000932 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934 --n;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 s += n;
937 *s = '<';
938 }
939 return s;
940}
941
942 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100943add_msg_hist(
944 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100945 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100946 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 struct msg_hist *p;
949
950 if (msg_hist_off || msg_silent != 0)
951 return;
952
Bram Moolenaar85a20022019-12-21 18:25:54 +0100953 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000954 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000955 (void)delete_first_msg();
956
Bram Moolenaar85a20022019-12-21 18:25:54 +0100957 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200958 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (p != NULL)
960 {
961 if (len < 0)
962 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100963 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 while (len > 0 && *s == '\n')
965 {
966 ++s;
967 --len;
968 }
969 while (len > 0 && s[len - 1] == '\n')
970 --len;
971 p->msg = vim_strnsave(s, len);
972 p->next = NULL;
973 p->attr = attr;
974 if (last_msg_hist != NULL)
975 last_msg_hist->next = p;
976 last_msg_hist = p;
977 if (first_msg_hist == NULL)
978 first_msg_hist = last_msg_hist;
979 ++msg_hist_len;
980 }
981}
982
983/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000984 * Delete the first (oldest) message from the history.
985 * Returns FAIL if there are no messages.
986 */
987 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100988delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000989{
990 struct msg_hist *p;
991
992 if (msg_hist_len <= 0)
993 return FAIL;
994 p = first_msg_hist;
995 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000996 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100997 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000998 vim_free(p->msg);
999 vim_free(p);
1000 --msg_hist_len;
1001 return OK;
1002}
1003
1004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * ":messages" command.
1006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001008ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 struct msg_hist *p;
1011 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001012 int c = 0;
1013
1014 if (STRCMP(eap->arg, "clear") == 0)
1015 {
1016 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1017
1018 while (msg_hist_len > keep)
1019 (void)delete_first_msg();
1020 return;
1021 }
1022
1023 if (*eap->arg != NUL)
1024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001025 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001026 return;
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 msg_hist_off = TRUE;
1030
Bram Moolenaar451f8492016-04-14 17:16:22 +02001031 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001032 if (eap->addr_count != 0)
1033 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001034 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001035 for (; p != NULL && !got_int; p = p->next)
1036 c++;
1037
1038 c -= eap->line2;
1039
Bram Moolenaar85a20022019-12-21 18:25:54 +01001040 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001041 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1042 p = p->next, c--);
1043 }
1044
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001045 if (p == first_msg_hist)
1046 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001047#ifdef FEAT_MULTI_LANG
1048 s = get_mess_lang();
1049#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001050 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001051#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001052 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001053 // The next comment is extracted by xgettext and put in po file for
1054 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001055 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001056 // Translator: Please replace the name and email address
1057 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001058 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001059 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001060 }
1061
Bram Moolenaar85a20022019-12-21 18:25:54 +01001062 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001063 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001065 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 msg_hist_off = FALSE;
1068}
1069
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001070#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
1072 * Call this after prompting the user. This will avoid a hit-return message
1073 * and a delay.
1074 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001075 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001076msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
1078 need_wait_return = FALSE;
1079 emsg_on_display = FALSE;
1080 cmdline_row = msg_row;
1081 msg_col = 0;
1082 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001083 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084}
1085#endif
1086
1087/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001088 * Wait for the user to hit a key (normally Enter).
1089 * If "redraw" is TRUE, clear and redraw the screen.
1090 * If "redraw" is FALSE, just redraw the screen.
1091 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
1093 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001094wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
1096 int c;
1097 int oldState;
1098 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001100 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001101 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 if (redraw == TRUE)
1104 must_redraw = CLEAR;
1105
Bram Moolenaar85a20022019-12-21 18:25:54 +01001106 // If using ":silent cmd", don't wait for a return. Also don't set
1107 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (msg_silent != 0)
1109 return;
1110
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001111 /*
1112 * When inside vgetc(), we can't wait for a typed character at all.
1113 * With the global command (and some others) we only need one return at
1114 * the end. Adjust cmdline_row to avoid the next message overwriting the
1115 * last one.
1116 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001117 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001119 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (no_wait_return)
1121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (!exmode_active)
1123 cmdline_row = msg_row;
1124 return;
1125 }
1126
Bram Moolenaar85a20022019-12-21 18:25:54 +01001127 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 oldState = State;
1129 if (quit_more)
1130 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001131 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 quit_more = FALSE;
1133 got_int = FALSE;
1134 }
1135 else if (exmode_active)
1136 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001137 msg_puts(" "); // make sure the cursor is on the right line
1138 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 got_int = FALSE;
1140 }
1141 else
1142 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001143 // Make sure the hit-return prompt is on screen when 'guioptions' was
1144 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 screenalloc(FALSE);
1146
1147 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001150 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001152 cmdline_row = msg_row;
1153
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // Avoid the sequence that the user types ":" at the hit-return prompt
1155 // to start an Ex command, but the file-changed dialog gets in the
1156 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001157 if (need_check_timestamps)
1158 check_timestamps(FALSE);
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 hit_return_msg();
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 do
1163 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001164 // Remember "got_int", if it is set vgetc() probably returns a
1165 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001167
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 // Don't do mappings here, we put the character back in the
1169 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001170 ++no_mapping;
1171 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001172
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // Temporarily disable Recording. If Recording is active, the
1174 // character will be recorded later, since it will be added to the
1175 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001176 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001177 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001178 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001179 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001181 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001183 --no_mapping;
1184 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001185 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001186 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // Strange way to allow copying (yanking) a modeless selection at
1190 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1191 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1193 {
1194 clip_copy_modeless_selection(TRUE);
1195 c = K_IGNORE;
1196 }
1197#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001198
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001199 /*
1200 * Allow scrolling back in the messages.
1201 * Also accept scroll-down commands when messages fill the screen,
1202 * to avoid that typing one 'j' too many makes the messages
1203 * disappear.
1204 */
1205 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001206 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001207 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1208 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001209 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001210 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001212 do_more_prompt(c);
1213 else
1214 {
1215 msg_didout = FALSE;
1216 c = K_IGNORE;
1217 msg_col =
1218#ifdef FEAT_RIGHTLEFT
1219 cmdmsg_rl ? Columns - 1 :
1220#endif
1221 0;
1222 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001223 if (quit_more)
1224 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001225 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001226 quit_more = FALSE;
1227 got_int = FALSE;
1228 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001229 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001230 {
1231 c = K_IGNORE;
1232 hit_return_msg();
1233 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001234 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001235 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001236 && (c == 'j' || c == 'd' || c == 'f'
1237 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001238 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 } while ((had_got_int && c == Ctrl_C)
1241 || c == K_IGNORE
1242#ifdef FEAT_GUI
1243 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1246 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1247 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001248 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001250 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 || (!mouse_has(MOUSE_RETURN)
1252 && mouse_row < msg_row
1253 && (c == K_LEFTMOUSE
1254 || c == K_MIDDLEMOUSE
1255 || c == K_RIGHTMOUSE
1256 || c == K_X1MOUSE
1257 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 );
1259 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 /*
1261 * Avoid that the mouse-up event causes visual mode to start.
1262 */
1263 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1264 || c == K_X1MOUSE || c == K_X2MOUSE)
1265 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001266 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001268 // Put the character back in the typeahead buffer. Don't use the
1269 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001270 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001271 do_redraw = TRUE; // need a redraw even though there is
1272 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 redir_off = FALSE;
1276
1277 /*
1278 * If the user hits ':', '?' or '/' we get a command line from the next
1279 * line.
1280 */
1281 if (c == ':' || c == '?' || c == '/')
1282 {
1283 if (!exmode_active)
1284 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001285 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001287#ifdef FEAT_TERMINAL
1288 skip_term_loop = TRUE;
1289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291
1292 /*
1293 * If the window size changed set_shellsize() will redraw the screen.
1294 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1295 * typed.
1296 */
1297 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001298 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 msg_check();
1301
1302#if defined(UNIX) || defined(VMS)
1303 /*
1304 * When switching screens, we need to output an extra newline on exit.
1305 */
1306 if (swapping_screen() && !termcap_active)
1307 newline_on_exit = TRUE;
1308#endif
1309
1310 need_wait_return = FALSE;
1311 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001312 emsg_on_display = FALSE; // can delete error message now
1313 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 reset_last_sourcing();
1315 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1316 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001317 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
Bram Moolenaar85a20022019-12-21 18:25:54 +01001319 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001321 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 shell_resized();
1323 }
1324 else if (!skip_redraw
1325 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1326 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001327 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 redraw_later(VALID);
1329 }
1330}
1331
1332/*
1333 * Write the hit-return prompt.
1334 */
1335 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001336hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001338 int save_p_more = p_more;
1339
Bram Moolenaar85a20022019-12-21 18:25:54 +01001340 p_more = FALSE; // don't want see this message when scrolling back
1341 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 msg_putchar('\n');
1343 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001344 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
Bram Moolenaar32526b32019-01-19 17:43:09 +01001346 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (!msg_use_printf())
1348 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001349 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350}
1351
1352/*
1353 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1354 */
1355 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001356set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 vim_free(keep_msg);
1359 if (s != NULL && msg_silent == 0)
1360 keep_msg = vim_strsave(s);
1361 else
1362 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001363 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001364 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365}
1366
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001367#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1368/*
1369 * If there currently is a message being displayed, set "keep_msg" to it, so
1370 * that it will be displayed again after redraw.
1371 */
1372 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001373set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001374{
1375 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1376 && (State & NORMAL))
1377 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1378}
1379#endif
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381/*
1382 * Prepare for outputting characters in the command line.
1383 */
1384 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001385msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 int did_return = FALSE;
1388
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001389 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001390 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001391
1392#ifdef FEAT_EVAL
1393 if (need_clr_eos)
1394 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001395 // Halfway an ":echo" command and getting an (error) message: clear
1396 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001397 need_clr_eos = FALSE;
1398 msg_clr_eos();
1399 }
1400#endif
1401
Bram Moolenaar85a20022019-12-21 18:25:54 +01001402 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 {
1404 msg_row = cmdline_row;
1405 msg_col =
1406#ifdef FEAT_RIGHTLEFT
1407 cmdmsg_rl ? Columns - 1 :
1408#endif
1409 0;
1410 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001411 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
1413 msg_putchar('\n');
1414 did_return = TRUE;
1415 if (exmode_active != EXMODE_NORMAL)
1416 cmdline_row = msg_row;
1417 }
1418 if (!msg_didany || lines_left < 0)
1419 msg_starthere();
1420 if (msg_silent == 0)
1421 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001422 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 cursor_off();
1424 }
1425
Bram Moolenaar85a20022019-12-21 18:25:54 +01001426 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (!did_return)
1428 redir_write((char_u *)"\n", -1);
1429}
1430
1431/*
1432 * Note that the current msg position is where messages start.
1433 */
1434 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001435msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437 lines_left = cmdline_row;
1438 msg_didany = FALSE;
1439}
1440
1441 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001442msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 msg_putchar_attr(c, 0);
1445}
1446
1447 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001448msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001450 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452 if (IS_SPECIAL(c))
1453 {
1454 buf[0] = K_SPECIAL;
1455 buf[1] = K_SECOND(c);
1456 buf[2] = K_THIRD(c);
1457 buf[3] = NUL;
1458 }
1459 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001460 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001461 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462}
1463
1464 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001465msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001467 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaar32526b32019-01-19 17:43:09 +01001469 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 msg_puts(buf);
1471}
1472
1473 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001474msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 msg_home_replace_attr(fname, 0);
1477}
1478
1479#if defined(FEAT_FIND_ID) || defined(PROTO)
1480 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001481msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001483 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484}
1485#endif
1486
1487 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001488msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 char_u *name;
1491
1492 name = home_replace_save(NULL, fname);
1493 if (name != NULL)
1494 msg_outtrans_attr(name, attr);
1495 vim_free(name);
1496}
1497
1498/*
1499 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001500 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 * Use attributes 'attr'.
1502 * Return the number of characters it takes on the screen.
1503 */
1504 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001505msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 return msg_outtrans_attr(str, 0);
1508}
1509
1510 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001511msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1514}
1515
1516 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001517msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 return msg_outtrans_len_attr(str, len, 0);
1520}
1521
1522/*
1523 * Output one character at "p". Return pointer to the next character.
1524 * Handles multi-byte characters.
1525 */
1526 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001527msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 int l;
1530
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001531 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 msg_outtrans_len_attr(p, l, attr);
1534 return p + l;
1535 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001536 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return p + 1;
1538}
1539
1540 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001541msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 int retval = 0;
1544 char_u *str = msgstr;
1545 char_u *plain_start = msgstr;
1546 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 int mb_l;
1548 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
Bram Moolenaar85a20022019-12-21 18:25:54 +01001550 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (attr & MSG_HIST)
1552 {
1553 add_msg_hist(str, len, attr);
1554 attr &= ~MSG_HIST;
1555 }
1556
Bram Moolenaar85a20022019-12-21 18:25:54 +01001557 // If the string starts with a composing character first draw a space on
1558 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001560 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
1562 /*
1563 * Go over the string. Special characters are translated and printed.
1564 * Normal characters are printed several at a time.
1565 */
1566 while (--len >= 0)
1567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001569 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001570 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001572 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 else
1574 mb_l = 1;
1575 if (has_mbyte && mb_l > 1)
1576 {
1577 c = (*mb_ptr2char)(str);
1578 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001579 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 retval += (*mb_ptr2cells)(str);
1581 else
1582 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 // unprintable multi-byte char: print the printable chars so
1584 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001586 msg_puts_attr_len((char *)plain_start,
1587 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001589 msg_puts_attr((char *)transchar(c),
1590 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 retval += char2cells(c);
1592 }
1593 len -= mb_l - 1;
1594 str += mb_l;
1595 }
1596 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 s = transchar_byte(*str);
1599 if (s[1] != NUL)
1600 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 // unprintable char: print the printable chars so far and the
1602 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001604 msg_puts_attr_len((char *)plain_start,
1605 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001607 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001608 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001610 else
1611 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 ++str;
1613 }
1614 }
1615
1616 if (str > plain_start)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001617 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001618 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
1620 return retval;
1621}
1622
1623#if defined(FEAT_QUICKFIX) || defined(PROTO)
1624 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001625msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
1627 int i;
1628 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1629
1630 arg = skipwhite(arg);
1631 for (i = 5; *arg && i >= 0; --i)
1632 if (*arg++ != str[i])
1633 break;
1634 if (i < 0)
1635 {
1636 msg_putchar('\n');
1637 for (i = 0; rs[i]; ++i)
1638 msg_putchar(rs[i] - 3);
1639 }
1640}
1641#endif
1642
1643/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001644 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 * Return the number of characters it takes on the screen.
1646 *
1647 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1648 * following character and shown as <F1>, <S-Up> etc. Any other character
1649 * which is not printable shown in <> form.
1650 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1651 * If a character is displayed in one of these special ways, is also
1652 * highlighted (its highlight name is '8' in the p_hl variable).
1653 * Otherwise characters are not highlighted.
1654 * This function is used to show mappings, where we want to see how to type
1655 * the character/string -- webb
1656 */
1657 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001658msg_outtrans_special(
1659 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001660 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001661 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 char_u *str = strstart;
1664 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001665 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 int attr;
1667 int len;
1668
Bram Moolenaar8820b482017-03-16 17:23:31 +01001669 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 while (*str != NUL)
1671 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001672 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if ((str == strstart || str[1] == NUL) && *str == ' ')
1674 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001675 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 ++str;
1677 }
1678 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001679 text = (char *)str2special(&str, from);
1680 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001681 if (maxlen > 0 && retval + len >= maxlen)
1682 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001683 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001684 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001685 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 retval += len;
1687 }
1688 return retval;
1689}
1690
Bram Moolenaarbd743252010-10-20 21:23:33 +02001691#if defined(FEAT_EVAL) || defined(PROTO)
1692/*
1693 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1694 * strings, in an allocated string.
1695 */
1696 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001697str2special_save(
1698 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001699 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001700{
1701 garray_T ga;
1702 char_u *p = str;
1703
1704 ga_init2(&ga, 1, 40);
1705 while (*p != NUL)
1706 ga_concat(&ga, str2special(&p, is_lhs));
1707 ga_append(&ga, NUL);
1708 return (char_u *)ga.ga_data;
1709}
1710#endif
1711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712/*
1713 * Return the printable string for the key codes at "*sp".
1714 * Used for translating the lhs or rhs of a mapping to printable chars.
1715 * Advances "sp" to the next code.
1716 */
1717 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001718str2special(
1719 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001720 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722 int c;
1723 static char_u buf[7];
1724 char_u *str = *sp;
1725 int modifiers = 0;
1726 int special = FALSE;
1727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (has_mbyte)
1729 {
1730 char_u *p;
1731
Bram Moolenaar85a20022019-12-21 18:25:54 +01001732 // Try to un-escape a multi-byte character. Return the un-escaped
1733 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 p = mb_unescape(sp);
1735 if (p != NULL)
1736 return p;
1737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
1739 c = *str;
1740 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1741 {
1742 if (str[1] == KS_MODIFIER)
1743 {
1744 modifiers = str[2];
1745 str += 3;
1746 c = *str;
1747 }
1748 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1749 {
1750 c = TO_SPECIAL(str[1], str[2]);
1751 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001753 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 special = TRUE;
1755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001757 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001759 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001760
Bram Moolenaar85a20022019-12-21 18:25:54 +01001761 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001762 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1763 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001764 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001765 *sp = str + 1;
1766 return buf;
1767 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001768 // Since 'special' is TRUE the multi-byte character 'c' will be
1769 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001770 c = (*mb_ptr2char)(str);
1771 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001773 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001774 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar85a20022019-12-21 18:25:54 +01001776 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1777 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (special || char2cells(c) > 1 || (from && c == ' '))
1779 return get_special_key_name(c, modifiers);
1780 buf[0] = c;
1781 buf[1] = NUL;
1782 return buf;
1783}
1784
1785/*
1786 * Translate a key sequence into special key names.
1787 */
1788 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001789str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
1791 char_u *s;
1792
1793 *buf = NUL;
1794 while (*sp)
1795 {
1796 s = str2special(&sp, FALSE);
1797 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1798 STRCAT(buf, s);
1799 }
1800}
1801
1802/*
1803 * print line for :print or :list command
1804 */
1805 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001806msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
1808 int c;
1809 int col = 0;
1810 int n_extra = 0;
1811 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001812 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001813 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001815 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 int l;
1818 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaardf177f62005-02-22 08:39:57 +00001820 if (curwin->w_p_list)
1821 list = TRUE;
1822
Bram Moolenaar85a20022019-12-21 18:25:54 +01001823 // find start of trailing whitespace
Bram Moolenaardf177f62005-02-22 08:39:57 +00001824 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001827 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 --trail;
1829 }
1830
Bram Moolenaar85a20022019-12-21 18:25:54 +01001831 // output a space for an empty line, otherwise the line will be
1832 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001833 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 msg_putchar(' ');
1835
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001836 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001838 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
1840 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001841 if (n_extra == 0 && c_final)
1842 c = c_final;
1843 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 c = c_extra;
1845 else
1846 c = *p_extra++;
1847 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001848 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001851 if (lcs_nbsp != NUL && list
1852 && (mb_ptr2char(s) == 160
1853 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001854 {
1855 mb_char2bytes(lcs_nbsp, buf);
1856 buf[(*mb_ptr2len)(buf)] = NUL;
1857 }
1858 else
1859 {
1860 mch_memmove(buf, s, (size_t)l);
1861 buf[l] = NUL;
1862 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001863 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 s += l;
1865 continue;
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 else
1868 {
1869 attr = 0;
1870 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001871 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001873 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001874#ifdef FEAT_VARTABS
1875 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1876 curbuf->b_p_vts_array) - 1;
1877#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001879#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001880 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 c = ' ';
1883 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001884 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
1886 else
1887 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001888 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001890 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001891 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001894 else if (c == 160 && list && lcs_nbsp != NUL)
1895 {
1896 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001897 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001898 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001899 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 p_extra = (char_u *)"";
1902 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001903 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 n_extra = 1;
1905 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001906 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 --s;
1908 }
1909 else if (c != NUL && (n = byte2cells(c)) > 1)
1910 {
1911 n_extra = n - 1;
1912 p_extra = transchar_byte(c);
1913 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001914 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001916 // Use special coloring to be able to distinguish <hex> from
1917 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001918 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 else if (c == ' ' && trail != NULL && s > trail)
1921 {
1922 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001923 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001925 else if (c == ' ' && list && lcs_space != NUL)
1926 {
1927 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001928 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931
1932 if (c == NUL)
1933 break;
1934
1935 msg_putchar_attr(c, attr);
1936 col++;
1937 }
1938 msg_clr_eos();
1939}
1940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941/*
1942 * Use screen_puts() to output one multi-byte character.
1943 * Return the pointer "s" advanced to the next character.
1944 */
1945 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001946screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
1948 int cw;
1949
Bram Moolenaar85a20022019-12-21 18:25:54 +01001950 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 cw = (*mb_ptr2cells)(s);
1952 if (cw > 1 && (
1953#ifdef FEAT_RIGHTLEFT
1954 cmdmsg_rl ? msg_col <= 1 :
1955#endif
1956 msg_col == Columns - 1))
1957 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001958 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001959 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 return s;
1961 }
1962
1963 screen_puts_len(s, l, msg_row, msg_col, attr);
1964#ifdef FEAT_RIGHTLEFT
1965 if (cmdmsg_rl)
1966 {
1967 msg_col -= cw;
1968 if (msg_col == 0)
1969 {
1970 msg_col = Columns;
1971 ++msg_row;
1972 }
1973 }
1974 else
1975#endif
1976 {
1977 msg_col += cw;
1978 if (msg_col >= Columns)
1979 {
1980 msg_col = 0;
1981 ++msg_row;
1982 }
1983 }
1984 return s + l;
1985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987/*
1988 * Output a string to the screen at position msg_row, msg_col.
1989 * Update msg_row and msg_col for the next message.
1990 */
1991 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001992msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001994 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995}
1996
1997 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001998msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002000 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001}
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003/*
2004 * Show a message in such a way that it always fits in the line. Cut out a
2005 * part in the middle and replace it with "..." when necessary.
2006 * Does not handle multi-byte characters!
2007 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002008 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002009msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 int slen = len;
2012 int room;
2013
2014 room = Columns - msg_col;
2015 if (len > room && room >= 20)
2016 {
2017 slen = (room - 3) / 2;
2018 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002019 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 }
2021 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2022}
2023
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002024 void
2025msg_outtrans_long_attr(char_u *longstr, int attr)
2026{
2027 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2028}
2029
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030/*
2031 * Basic function for writing a message with highlight attributes.
2032 */
2033 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002034msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 msg_puts_attr_len(s, -1, attr);
2037}
2038
2039/*
2040 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2041 * When "maxlen" is -1 there is no maximum length.
2042 * When "maxlen" is >= 0 the message is not put in the history.
2043 */
2044 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002045msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 /*
2048 * If redirection is on, also write to the redirection file.
2049 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002050 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 /*
2053 * Don't print anything when using ":silent cmd".
2054 */
2055 if (msg_silent != 0)
2056 return;
2057
Bram Moolenaar85a20022019-12-21 18:25:54 +01002058 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if ((attr & MSG_HIST) && maxlen < 0)
2060 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002061 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 attr &= ~MSG_HIST;
2063 }
2064
Bram Moolenaare8070982019-10-12 17:07:06 +02002065 // When writing something to the screen after it has scrolled, requires a
2066 // wait-return prompt later. Needed when scrolling, resetting
2067 // need_wait_return after some prompt, and then outputting something
2068 // without scrolling
2069 // Not needed when only using CR to move the cursor.
2070 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002072 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 /*
2075 * If there is no valid screen, use fprintf so we can see error messages.
2076 * If termcap is not active, we may be writing in an alternate console
2077 * window, cursor positioning may not work correctly (window size may be
2078 * different, e.g. for Win32 console) or we just don't know where the
2079 * cursor is.
2080 */
2081 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002082 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002083 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002084 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002087/*
2088 * The display part of msg_puts_attr_len().
2089 * May be called recursively to display scroll-back text.
2090 */
2091 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002092msg_puts_display(
2093 char_u *str,
2094 int maxlen,
2095 int attr,
2096 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002097{
2098 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002099 char_u *t_s = str; // string from "t_s" to "s" is still todo
2100 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002101 int l;
2102 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002103 char_u *sb_str = str;
2104 int sb_col = msg_col;
2105 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002106 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002109 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
2111 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002112 * We are at the end of the screen line when:
2113 * - When outputting a newline.
2114 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002116 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#ifdef FEAT_RIGHTLEFT
2118 cmdmsg_rl
2119 ? (
2120 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002121 || (*s == TAB && msg_col <= 7)
2122 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 :
2124#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002125 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002128 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002130 /*
2131 * The screen is scrolled up when at the last row (some terminals
2132 * scroll automatically, some don't. To avoid problems we scroll
2133 * ourselves).
2134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002137 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar85a20022019-12-21 18:25:54 +01002139 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (msg_no_more && lines_left == 0)
2141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
Bram Moolenaar85a20022019-12-21 18:25:54 +01002143 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002144 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002147 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 msg_col = Columns - 1;
2149
Bram Moolenaar85a20022019-12-21 18:25:54 +01002150 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002151 if (*s >= ' '
2152#ifdef FEAT_RIGHTLEFT
2153 && !cmdmsg_rl
2154#endif
2155 )
2156 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002157 if (has_mbyte)
2158 {
2159 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002160 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002161 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002162 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002163 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002164 s = screen_puts_mbyte(s, l, attr);
2165 }
2166 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002167 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002168 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002169 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002170 else
2171 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002172
2173 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002174 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002175 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2176
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002177 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002178 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 redraw_cmdline = TRUE;
2180 if (cmdline_row > 0 && !exmode_active)
2181 --cmdline_row;
2182
2183 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 * If screen is completely filled and 'more' is set then wait
2185 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002187 if (lines_left > 0)
2188 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002189 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 && !msg_no_more && !exmode_active)
2191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002193 if (do_more_prompt(NUL))
2194 s = confirm_msg_tail;
2195#else
2196 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#endif
2198 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002199 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002201
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 // When we displayed a char in last column need to check if there
2203 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002204 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002205 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002208 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002211 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002212 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2213 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002214 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002215 t_puts(&t_col, t_s, s, attr);
2216
2217 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002218 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002219 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
Bram Moolenaar85a20022019-12-21 18:25:54 +01002221 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002223 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002224#ifdef FEAT_RIGHTLEFT
2225 if (cmdmsg_rl)
2226 msg_col = Columns - 1;
2227 else
2228#endif
2229 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 msg_row = Rows - 1;
2232 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002233 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 msg_col = 0;
2236 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002237 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 if (msg_col)
2240 --msg_col;
2241 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 do
2245 msg_screen_putchar(' ', attr);
2246 while (msg_col & 7);
2247 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002248 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002249 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 else
2251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (has_mbyte)
2253 {
2254 cw = (*mb_ptr2cells)(s);
2255 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002256 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002257 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002259 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261 else
2262 {
2263 cw = 1;
2264 l = 1;
2265 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002266
Bram Moolenaar85a20022019-12-21 18:25:54 +01002267 // When drawing from right to left or when a double-wide character
2268 // doesn't fit, draw a single character here. Otherwise collect
2269 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (
2271# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002272 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002274 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (l > 1)
2277 s = screen_puts_mbyte(s, l, attr) - 1;
2278 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 msg_screen_putchar(*s, attr);
2280 }
2281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002283 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (t_col == 0)
2285 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 t_col += cw;
2287 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 }
2290 ++s;
2291 }
2292
Bram Moolenaar85a20022019-12-21 18:25:54 +01002293 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002295 t_puts(&t_col, t_s, s, attr);
2296 if (p_more && !recurse)
2297 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 msg_check();
2300}
2301
2302/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002303 * Return TRUE when ":filter pattern" was used and "msg" does not match
2304 * "pattern".
2305 */
2306 int
2307message_filtered(char_u *msg)
2308{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002309 int match;
2310
2311 if (cmdmod.filter_regmatch.regprog == NULL)
2312 return FALSE;
2313 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2314 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002315}
2316
2317/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002318 * Scroll the screen up one line for displaying the next message line.
2319 */
2320 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002321msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002322{
2323#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002324 // Remove the cursor before scrolling, ScreenLines[] is going
2325 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002326 if (gui.in_use)
2327 gui_undraw_cursor();
2328#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002329 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002330 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002331 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002332 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002333
2334 if (!can_clear((char_u *)" "))
2335 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002336 // Scrolling up doesn't result in the right background. Set the
2337 // background here. It's not efficient, but avoids that we have to do
2338 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002339 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2340
Bram Moolenaar85a20022019-12-21 18:25:54 +01002341 // Also clear the last char of the last but one line if it was not
2342 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002343 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2344 screen_fill((int)Rows - 2, (int)Rows - 1,
2345 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2346 }
2347}
2348
2349/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002350 * Increment "msg_scrolled".
2351 */
2352 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002353inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002354{
2355#ifdef FEAT_EVAL
2356 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2357 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002358 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002359 char_u *tofree = NULL;
2360 int len;
2361
Bram Moolenaar85a20022019-12-21 18:25:54 +01002362 // v:scrollstart is empty, set it to the script/function name and line
2363 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002364 if (p == NULL)
2365 p = (char_u *)_("Unknown");
2366 else
2367 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002368 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002369 tofree = alloc(len);
2370 if (tofree != NULL)
2371 {
2372 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002373 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002374 p = tofree;
2375 }
2376 }
2377 set_vim_var_string(VV_SCROLLSTART, p, -1);
2378 vim_free(tofree);
2379 }
2380#endif
2381 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002382 if (must_redraw < VALID)
2383 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002384}
2385
2386/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002387 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2388 * store the displayed text and remember where screen lines start.
2389 */
2390typedef struct msgchunk_S msgchunk_T;
2391struct msgchunk_S
2392{
2393 msgchunk_T *sb_next;
2394 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002395 char sb_eol; // TRUE when line ends after this text
2396 int sb_msg_col; // column in which text starts
2397 int sb_attr; // text attributes
2398 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002399};
2400
Bram Moolenaar85a20022019-12-21 18:25:54 +01002401static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002402
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002403static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002404
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002405typedef enum {
2406 SB_CLEAR_NONE = 0,
2407 SB_CLEAR_ALL,
2408 SB_CLEAR_CMDLINE_BUSY,
2409 SB_CLEAR_CMDLINE_DONE
2410} sb_clear_T;
2411
Bram Moolenaar85a20022019-12-21 18:25:54 +01002412// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002413static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002414
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002415/*
2416 * Store part of a printed message for displaying when scrolling back.
2417 */
2418 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002419store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002420 char_u **sb_str, // start of string
2421 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002422 int attr,
2423 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002424 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002425{
2426 msgchunk_T *mp;
2427
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002428 if (do_clear_sb_text == SB_CLEAR_ALL
2429 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002430 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002431 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2432 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002433 }
2434
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002435 if (s > *sb_str)
2436 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002437 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002438 if (mp != NULL)
2439 {
2440 mp->sb_eol = finish;
2441 mp->sb_msg_col = *sb_col;
2442 mp->sb_attr = attr;
2443 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2444
2445 if (last_msgchunk == NULL)
2446 {
2447 last_msgchunk = mp;
2448 mp->sb_prev = NULL;
2449 }
2450 else
2451 {
2452 mp->sb_prev = last_msgchunk;
2453 last_msgchunk->sb_next = mp;
2454 last_msgchunk = mp;
2455 }
2456 mp->sb_next = NULL;
2457 }
2458 }
2459 else if (finish && last_msgchunk != NULL)
2460 last_msgchunk->sb_eol = TRUE;
2461
2462 *sb_str = s;
2463 *sb_col = 0;
2464}
2465
2466/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002467 * Finished showing messages, clear the scroll-back text on the next message.
2468 */
2469 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002470may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002471{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002472 do_clear_sb_text = SB_CLEAR_ALL;
2473}
2474
2475/*
2476 * Starting to edit the command line, do not clear messages now.
2477 */
2478 void
2479sb_text_start_cmdline(void)
2480{
2481 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2482 msg_sb_eol();
2483}
2484
2485/*
2486 * Ending to edit the command line. Clear old lines but the last one later.
2487 */
2488 void
2489sb_text_end_cmdline(void)
2490{
2491 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002492}
2493
2494/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002495 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002496 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002497 * Called when redrawing the screen.
2498 */
2499 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002500clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002501{
2502 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002503 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002504
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002505 if (all)
2506 lastp = &last_msgchunk;
2507 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002508 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002509 if (last_msgchunk == NULL)
2510 return;
2511 lastp = &last_msgchunk->sb_prev;
2512 }
2513
2514 while (*lastp != NULL)
2515 {
2516 mp = (*lastp)->sb_prev;
2517 vim_free(*lastp);
2518 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002520}
2521
2522/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002523 * "g<" command.
2524 */
2525 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002526show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002527{
2528 msgchunk_T *mp;
2529
Bram Moolenaar85a20022019-12-21 18:25:54 +01002530 // Only show something if there is more than one line, otherwise it looks
2531 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002532 mp = msg_sb_start(last_msgchunk);
2533 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002534 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002535 else
2536 {
2537 do_more_prompt('G');
2538 wait_return(FALSE);
2539 }
2540}
2541
2542/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002543 * Move to the start of screen line in already displayed text.
2544 */
2545 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002546msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002547{
2548 msgchunk_T *mp = mps;
2549
2550 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2551 mp = mp->sb_prev;
2552 return mp;
2553}
2554
2555/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002556 * Mark the last message chunk as finishing the line.
2557 */
2558 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002559msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002560{
2561 if (last_msgchunk != NULL)
2562 last_msgchunk->sb_eol = TRUE;
2563}
2564
2565/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002566 * Display a screen line from previously displayed text at row "row".
2567 * Returns a pointer to the text for the next line (can be NULL).
2568 */
2569 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002570disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002571{
2572 msgchunk_T *mp = smp;
2573 char_u *p;
2574
2575 for (;;)
2576 {
2577 msg_row = row;
2578 msg_col = mp->sb_msg_col;
2579 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002580 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002581 ++p;
2582 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2583 if (mp->sb_eol || mp->sb_next == NULL)
2584 break;
2585 mp = mp->sb_next;
2586 }
2587 return mp->sb_next;
2588}
2589
2590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 * Output any postponed text for msg_puts_attr_len().
2592 */
2593 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002594t_puts(
2595 int *t_col,
2596 char_u *t_s,
2597 char_u *s,
2598 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002600 // output postponed text
2601 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002603 msg_col += *t_col;
2604 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002605 // If the string starts with a composing character don't increment the
2606 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2608 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (msg_col >= Columns)
2610 {
2611 msg_col = 0;
2612 ++msg_row;
2613 }
2614}
2615
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616/*
2617 * Returns TRUE when messages should be printed with mch_errmsg().
2618 * This is used when there is no valid screen, so we can see error messages.
2619 * If termcap is not active, we may be writing in an alternate console
2620 * window, cursor positioning may not work correctly (window size may be
2621 * different, e.g. for Win32 console) or we just don't know where the
2622 * cursor is.
2623 */
2624 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002625msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002628#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2629# ifdef VIMDLL
2630 || (!gui.in_use && !termcap_active)
2631# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635 || (swapping_screen() && !termcap_active)
2636 );
2637}
2638
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002639/*
2640 * Print a message when there is no valid screen.
2641 */
2642 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002643msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002644{
2645 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002646 char_u *buf = NULL;
2647 char_u *p = s;
2648
Bram Moolenaar4f974752019-02-17 17:44:42 +01002649#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002650 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002652#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002653 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654 {
2655 if (!(silent_mode && p_verbose == 0))
2656 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002657 // NL --> CR NL translation (for Unix, not for "--version")
2658 if (*s == NL)
2659 {
2660 int n = (int)(s - p);
2661
2662 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002663 if (buf != NULL)
2664 {
2665 memcpy(buf, p, n);
2666 if (!info_message)
2667 buf[n++] = CAR;
2668 buf[n++] = NL;
2669 buf[n++] = NUL;
2670 if (info_message) // informative message, not an error
2671 mch_msg((char *)buf);
2672 else
2673 mch_errmsg((char *)buf);
2674 vim_free(buf);
2675 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002676 p = s + 1;
2677 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002678 }
2679
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002680 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002681#ifdef FEAT_RIGHTLEFT
2682 if (cmdmsg_rl)
2683 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002684 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002685 msg_col = Columns - 1;
2686 else
2687 --msg_col;
2688 }
2689 else
2690#endif
2691 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002692 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693 msg_col = 0;
2694 else
2695 ++msg_col;
2696 }
2697 ++s;
2698 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002699
2700 if (*p != NUL && !(silent_mode && p_verbose == 0))
2701 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002702 int c = -1;
2703
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002704 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002705 {
2706 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002707 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002708 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002709 if (info_message)
2710 mch_msg((char *)p);
2711 else
2712 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002713 if (c != -1)
2714 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002715 }
2716
2717 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718
Bram Moolenaar4f974752019-02-17 17:44:42 +01002719#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002720 if (!(silent_mode && p_verbose == 0))
2721 mch_settmode(TMODE_RAW);
2722#endif
2723}
2724
2725/*
2726 * Show the more-prompt and handle the user response.
2727 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002728 * When at hit-enter prompt "typed_char" is the already typed character,
2729 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002730 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2731 */
2732 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002733do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002734{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002735 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002736 int used_typed_char = typed_char;
2737 int oldState = State;
2738 int c;
2739#ifdef FEAT_CON_DIALOG
2740 int retval = FALSE;
2741#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002742 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 msgchunk_T *mp_last = NULL;
2744 msgchunk_T *mp;
2745 int i;
2746
Bram Moolenaar85a20022019-12-21 18:25:54 +01002747 // We get called recursively when a timer callback outputs a message. In
2748 // that case don't show another prompt. Also when at the hit-Enter prompt
2749 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002750 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002751 return FALSE;
2752 entered = TRUE;
2753
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002754 if (typed_char == 'G')
2755 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002756 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002757 mp_last = msg_sb_start(last_msgchunk);
2758 for (i = 0; i < Rows - 2 && mp_last != NULL
2759 && mp_last->sb_prev != NULL; ++i)
2760 mp_last = msg_sb_start(mp_last->sb_prev);
2761 }
2762
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002763 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002765 if (typed_char == NUL)
2766 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002767 for (;;)
2768 {
2769 /*
2770 * Get a typed character directly from the user.
2771 */
2772 if (used_typed_char != NUL)
2773 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002774 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002775 used_typed_char = NUL;
2776 }
2777 else
2778 c = get_keystroke();
2779
2780#if defined(FEAT_MENU) && defined(FEAT_GUI)
2781 if (c == K_MENU)
2782 {
2783 int idx = get_menu_index(current_menu, ASKMORE);
2784
Bram Moolenaar85a20022019-12-21 18:25:54 +01002785 // Used a menu. If it starts with CTRL-Y, it must
2786 // be a "Copy" for the clipboard. Otherwise
2787 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 if (idx == MENU_INDEX_INVALID)
2789 continue;
2790 c = *current_menu->strings[idx];
2791 if (c != NUL && current_menu->strings[idx][1] != NUL)
2792 ins_typebuf(current_menu->strings[idx] + 1,
2793 current_menu->noremap[idx], 0, TRUE,
2794 current_menu->silent[idx]);
2795 }
2796#endif
2797
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002798 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 switch (c)
2800 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002801 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 case K_BS:
2803 case 'k':
2804 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002805 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002806 break;
2807
Bram Moolenaar85a20022019-12-21 18:25:54 +01002808 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809 case NL:
2810 case 'j':
2811 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002812 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 break;
2814
Bram Moolenaar85a20022019-12-21 18:25:54 +01002815 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002816 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 break;
2818
Bram Moolenaar85a20022019-12-21 18:25:54 +01002819 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002820 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821 break;
2822
Bram Moolenaar85a20022019-12-21 18:25:54 +01002823 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002824 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002825 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002826 break;
2827
Bram Moolenaar85a20022019-12-21 18:25:54 +01002828 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002829 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002830 case K_PAGEDOWN:
2831 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002832 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002833 break;
2834
Bram Moolenaar85a20022019-12-21 18:25:54 +01002835 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002836 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002837 break;
2838
Bram Moolenaar85a20022019-12-21 18:25:54 +01002839 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002840 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002841 lines_left = 999999;
2842 break;
2843
Bram Moolenaar85a20022019-12-21 18:25:54 +01002844 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002845#ifdef FEAT_CON_DIALOG
2846 if (!confirm_msg_used)
2847#endif
2848 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002849 // Since got_int is set all typeahead will be flushed, but we
2850 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002852#ifdef FEAT_TERMINAL
2853 skip_term_loop = TRUE;
2854#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002855 cmdline_row = Rows - 1; // put ':' on this line
2856 skip_redraw = TRUE; // skip redraw once
2857 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859 // FALLTHROUGH
2860 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861 case Ctrl_C:
2862 case ESC:
2863#ifdef FEAT_CON_DIALOG
2864 if (confirm_msg_used)
2865 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002866 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002867 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002868 }
2869 else
2870#endif
2871 {
2872 got_int = TRUE;
2873 quit_more = TRUE;
2874 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002875 // When there is some more output (wrapping line) display that
2876 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002877 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 break;
2879
2880#ifdef FEAT_CLIPBOARD
2881 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002882 // Strange way to allow copying (yanking) a modeless
2883 // selection at the more prompt. Use CTRL-Y,
2884 // because the same is used in Cmdline-mode and at the
2885 // hit-enter prompt. However, scrolling one line up
2886 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002887 if (clip_star.state == SELECT_DONE)
2888 clip_copy_modeless_selection(TRUE);
2889 continue;
2890#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002892 msg_moremsg(TRUE);
2893 continue;
2894 }
2895
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002896 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002897 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002898 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002899 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002900 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002901 if (mp_last == NULL)
2902 mp = msg_sb_start(last_msgchunk);
2903 else if (mp_last->sb_prev != NULL)
2904 mp = msg_sb_start(mp_last->sb_prev);
2905 else
2906 mp = NULL;
2907
Bram Moolenaar85a20022019-12-21 18:25:54 +01002908 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002909 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2910 ++i)
2911 mp = msg_sb_start(mp->sb_prev);
2912
2913 if (mp != NULL && mp->sb_prev != NULL)
2914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002916 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002917 {
2918 if (mp == NULL || mp->sb_prev == NULL)
2919 break;
2920 mp = msg_sb_start(mp->sb_prev);
2921 if (mp_last == NULL)
2922 mp_last = msg_sb_start(last_msgchunk);
2923 else
2924 mp_last = msg_sb_start(mp_last->sb_prev);
2925 }
2926
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002927 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002928 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002929 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002930 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002931 (void)disp_sb_line(0, mp);
2932 }
2933 else
2934 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002935 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002937 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2938 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002939 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002940 ++msg_scrolled;
2941 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002942 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002943 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 }
2945 }
2946 else
2947 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002948 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002949 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002950 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002951 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002952 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002953 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2955 (int)Columns, ' ', ' ', 0);
2956 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002957 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002958 }
2959 }
2960
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002961 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002962 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002963 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002964 screen_fill((int)Rows - 1, (int)Rows, 0,
2965 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 msg_moremsg(FALSE);
2967 continue;
2968 }
2969
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002971 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002972 }
2973
2974 break;
2975 }
2976
Bram Moolenaar85a20022019-12-21 18:25:54 +01002977 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2979 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002980 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002981 if (quit_more)
2982 {
2983 msg_row = Rows - 1;
2984 msg_col = 0;
2985 }
2986#ifdef FEAT_RIGHTLEFT
2987 else if (cmdmsg_rl)
2988 msg_col = Columns - 1;
2989#endif
2990
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002991 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002992#ifdef FEAT_CON_DIALOG
2993 return retval;
2994#else
2995 return FALSE;
2996#endif
2997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3000
3001#ifdef mch_errmsg
3002# undef mch_errmsg
3003#endif
3004#ifdef mch_msg
3005# undef mch_msg
3006#endif
3007
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003008#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3009 static void
3010mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003012 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003013 DWORD nwrite = 0;
3014 DWORD mode = 0;
3015 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3016
3017 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3018 && (int)GetConsoleCP() != enc_codepage)
3019 {
3020 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3021
3022 WriteConsoleW(h, w, len, &nwrite, NULL);
3023 vim_free(w);
3024 }
3025 else
3026 {
3027 fprintf(stderr, "%s", str);
3028 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003029}
3030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003032/*
3033 * Give an error message. To be used when the screen hasn't been initialized
3034 * yet. When stderr can't be used, collect error messages until the GUI has
3035 * started and they can be displayed in a message box.
3036 */
3037 void
3038mch_errmsg(char *str)
3039{
3040#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3041 int len;
3042#endif
3043
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003044#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003045 // On Unix use stderr if it's a tty.
3046 // When not going to start the GUI also use stderr.
3047 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003049# ifdef UNIX
3050# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003052# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 isatty(2)
3054# endif
3055# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003056 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003057# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003058# endif
3059# ifdef FEAT_GUI
3060 !(gui.in_use || gui.starting)
3061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 )
3063 {
3064 fprintf(stderr, "%s", str);
3065 return;
3066 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003069#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3070# ifdef VIMDLL
3071 if (!(gui.in_use || gui.starting))
3072# endif
3073 {
3074 mch_errmsg_c(str);
3075 return;
3076 }
3077#endif
3078
3079#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003080 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 emsg_on_display = FALSE;
3082
3083 len = (int)STRLEN(str) + 1;
3084 if (error_ga.ga_growsize == 0)
3085 {
3086 error_ga.ga_growsize = 80;
3087 error_ga.ga_itemsize = 1;
3088 }
3089 if (ga_grow(&error_ga, len) == OK)
3090 {
3091 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3092 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003093# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003094 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
3096 char_u *p;
3097
3098 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3099 for (;;)
3100 {
3101 p = vim_strchr(p, '\r');
3102 if (p == NULL)
3103 break;
3104 *p = ' ';
3105 }
3106 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003107# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003108 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003114#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3115 static void
3116mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003118 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003119 DWORD nwrite = 0;
3120 DWORD mode;
3121 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3122
3123
3124 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3125 && (int)GetConsoleCP() != enc_codepage)
3126 {
3127 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3128
3129 WriteConsoleW(h, w, len, &nwrite, NULL);
3130 vim_free(w);
3131 }
3132 else
3133 {
3134 printf("%s", str);
3135 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003136}
3137#endif
3138
3139/*
3140 * Give a message. To be used when the screen hasn't been initialized yet.
3141 * When there is no tty, collect messages until the GUI has started and they
3142 * can be displayed in a message box.
3143 */
3144 void
3145mch_msg(char *str)
3146{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003147#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003148 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3149 // uses mch_errmsg() when started from the desktop.
3150 // When not going to start the GUI also use stdout.
3151 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003153# ifdef UNIX
3154# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003156# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003160 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003162# endif
3163# ifdef FEAT_GUI
3164 !(gui.in_use || gui.starting)
3165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 )
3167 {
3168 printf("%s", str);
3169 return;
3170 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003171#endif
3172
3173#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3174# ifdef VIMDLL
3175 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003177 {
3178 mch_msg_c(str);
3179 return;
3180 }
3181#endif
3182#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003186#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188/*
3189 * Put a character on the screen at the current message position and advance
3190 * to the next position. Only for printable ASCII!
3191 */
3192 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003193msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003195 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 screen_putchar(c, msg_row, msg_col, attr);
3197#ifdef FEAT_RIGHTLEFT
3198 if (cmdmsg_rl)
3199 {
3200 if (--msg_col == 0)
3201 {
3202 msg_col = Columns;
3203 ++msg_row;
3204 }
3205 }
3206 else
3207#endif
3208 {
3209 if (++msg_col >= Columns)
3210 {
3211 msg_col = 0;
3212 ++msg_row;
3213 }
3214 }
3215}
3216
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003217 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003218msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003220 int attr;
3221 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaar8820b482017-03-16 17:23:31 +01003223 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003224 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003226 screen_puts((char_u *)
3227 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3228 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229}
3230
3231/*
3232 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3233 * exmode_active.
3234 */
3235 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003236repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 if (State == ASKMORE)
3239 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003240 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 msg_row = Rows - 1;
3242 }
3243#ifdef FEAT_CON_DIALOG
3244 else if (State == CONFIRM)
3245 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003246 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 msg_row = Rows - 1;
3248 }
3249#endif
3250 else if (State == EXTERNCMD)
3251 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003252 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 else if (State == HITRETURN || State == SETWSIZE)
3255 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003256 if (msg_row == Rows - 1)
3257 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003258 // Avoid drawing the "hit-enter" prompt below the previous one,
3259 // overwrite it. Esp. useful when regaining focus and a
3260 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003261 msg_didout = FALSE;
3262 msg_col = 0;
3263 msg_clr_eos();
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 hit_return_msg();
3266 msg_row = Rows - 1;
3267 }
3268}
3269
3270/*
3271 * msg_check_screen - check if the screen is initialized.
3272 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3273 * While starting the GUI the terminal codes will be set for the GUI, but the
3274 * output goes to the terminal. Don't use the terminal codes then.
3275 */
3276 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003277msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 if (!full_screen || !screen_valid(FALSE))
3280 return FALSE;
3281
3282 if (msg_row >= Rows)
3283 msg_row = Rows - 1;
3284 if (msg_col >= Columns)
3285 msg_col = Columns - 1;
3286 return TRUE;
3287}
3288
3289/*
3290 * Clear from current message position to end of screen.
3291 * Skip this when ":silent" was used, no need to clear for redirection.
3292 */
3293 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003294msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
3296 if (msg_silent == 0)
3297 msg_clr_eos_force();
3298}
3299
3300/*
3301 * Clear from current message position to end of screen.
3302 * Note: msg_col is not updated, so we remember the end of the message
3303 * for msg_check().
3304 */
3305 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003306msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 if (msg_use_printf())
3309 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003310 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003313 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003315 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317 }
3318 else
3319 {
3320#ifdef FEAT_RIGHTLEFT
3321 if (cmdmsg_rl)
3322 {
3323 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3324 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3325 }
3326 else
3327#endif
3328 {
3329 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3330 ' ', ' ', 0);
3331 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3332 }
3333 }
3334}
3335
3336/*
3337 * Clear the command line.
3338 */
3339 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003340msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 msg_row = cmdline_row;
3343 msg_col = 0;
3344 msg_clr_eos_force();
3345}
3346
3347/*
3348 * end putting a message on the screen
3349 * call wait_return if the message does not fit in the available space
3350 * return TRUE if wait_return not called.
3351 */
3352 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003353msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003356 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 * or the ruler option is set and we run into it,
3358 * we have to redraw the window.
3359 * Do not do this if we are abandoning the file or editing the command line.
3360 */
3361 if (!exiting && need_wait_return && !(State & CMDLINE))
3362 {
3363 wait_return(FALSE);
3364 return FALSE;
3365 }
3366 out_flush();
3367 return TRUE;
3368}
3369
3370/*
3371 * If the written message runs into the shown command or ruler, we have to
3372 * wait for hit-return and redraw the window later.
3373 */
3374 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003375msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 if (msg_row == Rows - 1 && msg_col >= sc_col)
3378 {
3379 need_wait_return = TRUE;
3380 redraw_cmdline = TRUE;
3381 }
3382}
3383
3384/*
3385 * May write a string to the redirection file.
3386 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3387 */
3388 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003389redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 char_u *s = str;
3392 static int cur_col = 0;
3393
Bram Moolenaar85a20022019-12-21 18:25:54 +01003394 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003395 if (redir_off)
3396 return;
3397
Bram Moolenaar85a20022019-12-21 18:25:54 +01003398 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003399 if (*p_vfile != NUL && verbose_fd == NULL)
3400 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003401
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003402 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003404 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (*s != '\n' && *s != '\r')
3406 {
3407 while (cur_col < msg_col)
3408 {
3409#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003410 if (redir_execute)
3411 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003412 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003414 else if (redir_vname)
3415 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003418 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003420 if (verbose_fd != NULL)
3421 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ++cur_col;
3423 }
3424 }
3425
3426#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003427 if (redir_execute)
3428 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003429 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003431 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003432 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433#endif
3434
Bram Moolenaar85a20022019-12-21 18:25:54 +01003435 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3437 {
3438#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003439 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003441 if (redir_fd != NULL)
3442 putc(*s, redir_fd);
3443 if (verbose_fd != NULL)
3444 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (*s == '\r' || *s == '\n')
3446 cur_col = 0;
3447 else if (*s == '\t')
3448 cur_col += (8 - cur_col % 8);
3449 else
3450 ++cur_col;
3451 ++s;
3452 }
3453
Bram Moolenaar85a20022019-12-21 18:25:54 +01003454 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 msg_col = cur_col;
3456 }
3457}
3458
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003459 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003460redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003461{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003462 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003463#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003464 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003465#endif
3466 ;
3467}
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003470 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003471 * Must always be called paired with verbose_leave()!
3472 */
3473 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003474verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003475{
3476 if (*p_vfile != NUL)
3477 ++msg_silent;
3478}
3479
3480/*
3481 * After giving verbose message.
3482 * Must always be called paired with verbose_enter()!
3483 */
3484 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003485verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003486{
3487 if (*p_vfile != NUL)
3488 if (--msg_silent < 0)
3489 msg_silent = 0;
3490}
3491
3492/*
3493 * Like verbose_enter() and set msg_scroll when displaying the message.
3494 */
3495 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003496verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003497{
3498 if (*p_vfile != NUL)
3499 ++msg_silent;
3500 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003501 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003502 msg_scroll = TRUE;
3503}
3504
3505/*
3506 * Like verbose_leave() and set cmdline_row when displaying the message.
3507 */
3508 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003509verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003510{
3511 if (*p_vfile != NUL)
3512 {
3513 if (--msg_silent < 0)
3514 msg_silent = 0;
3515 }
3516 else
3517 cmdline_row = msg_row;
3518}
3519
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003520/*
3521 * Called when 'verbosefile' is set: stop writing to the file.
3522 */
3523 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003524verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003525{
3526 if (verbose_fd != NULL)
3527 {
3528 fclose(verbose_fd);
3529 verbose_fd = NULL;
3530 }
3531 verbose_did_open = FALSE;
3532}
3533
3534/*
3535 * Open the file 'verbosefile'.
3536 * Return FAIL or OK.
3537 */
3538 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003539verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003540{
3541 if (verbose_fd == NULL && !verbose_did_open)
3542 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003543 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003544 verbose_did_open = TRUE;
3545
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003546 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003547 if (verbose_fd == NULL)
3548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003549 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003550 return FAIL;
3551 }
3552 }
3553 return OK;
3554}
3555
3556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 * Give a warning message (for searching).
3558 * Use 'w' highlighting and may repeat the message after redrawing
3559 */
3560 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003561give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003563 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (msg_silent != 0)
3565 return;
3566
Bram Moolenaar85a20022019-12-21 18:25:54 +01003567 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570#ifdef FEAT_EVAL
3571 set_vim_var_string(VV_WARNINGMSG, message, -1);
3572#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003573 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003575 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 else
3577 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003578 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003579 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003580 msg_didout = FALSE; // overwrite this message
3581 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 --no_wait_return;
3585}
3586
Bram Moolenaar113e1072019-01-20 15:30:40 +01003587#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003588 void
3589give_warning2(char_u *message, char_u *a1, int hl)
3590{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003591 if (IObuff == NULL)
3592 {
3593 // Very early in initialisation and already something wrong, just give
3594 // the raw message so the user at least gets a hint.
3595 give_warning((char_u *)message, hl);
3596 }
3597 else
3598 {
3599 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3600 give_warning(IObuff, hl);
3601 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003602}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003603#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605/*
3606 * Advance msg cursor to column "col".
3607 */
3608 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003609msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003611 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003613 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 return;
3615 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003616 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003618#ifdef FEAT_RIGHTLEFT
3619 if (cmdmsg_rl)
3620 while (msg_col > Columns - col)
3621 msg_putchar(' ');
3622 else
3623#endif
3624 while (msg_col < col)
3625 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626}
3627
3628#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3629/*
3630 * Used for "confirm()" function, and the :confirm command prefix.
3631 * Versions which haven't got flexible dialogs yet, and console
3632 * versions, get this generic handler which uses the command line.
3633 *
3634 * type = one of:
3635 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3636 * title = title string (can be NULL for default)
3637 * (neither used in console dialogs at the moment)
3638 *
3639 * Format of the "buttons" string:
3640 * "Button1Name\nButton2Name\nButton3Name"
3641 * The first button should normally be the default/accept
3642 * The second button should be the 'Cancel' button
3643 * Other buttons- use your imagination!
3644 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3645 * different letter.
3646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003648do_dialog(
3649 int type UNUSED,
3650 char_u *title UNUSED,
3651 char_u *message,
3652 char_u *buttons,
3653 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003654 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3655 // otherwise
3656 int ex_cmd) // when TRUE pressing : accepts default and starts
3657 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 int oldState;
3660 int retval = 0;
3661 char_u *hotkeys;
3662 int c;
3663 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003664 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003667 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003669 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670#endif
3671
3672#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003673 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3675 {
3676 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003677 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003678 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003679 need_wait_return = FALSE;
3680 emsg_on_display = FALSE;
3681 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
Bram Moolenaar85a20022019-12-21 18:25:54 +01003683 // Flush output to avoid that further messages and redrawing is done
3684 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 out_flush();
3686 gui_mch_update();
3687
3688 return c;
3689 }
3690#endif
3691
3692 oldState = State;
3693 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
Bram Moolenaar27321db2020-07-06 21:24:57 +02003696 // Ensure raw mode here.
3697 save_tmode = cur_tmode;
3698 settmode(TMODE_RAW);
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 /*
3701 * Since we wait for a keypress, don't make the
3702 * user press RETURN as well afterwards.
3703 */
3704 ++no_wait_return;
3705 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3706
3707 if (hotkeys != NULL)
3708 {
3709 for (;;)
3710 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003711 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 c = get_keystroke();
3713 switch (c)
3714 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003715 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 case NL:
3717 retval = dfltbutton;
3718 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003719 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 case ESC:
3721 retval = 0;
3722 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003723 default: // Could be a hotkey?
3724 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003726 if (c == ':' && ex_cmd)
3727 {
3728 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003729 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003730 break;
3731 }
3732
Bram Moolenaar85a20022019-12-21 18:25:54 +01003733 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 c = MB_TOLOWER(c);
3735 retval = 1;
3736 for (i = 0; hotkeys[i]; ++i)
3737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (has_mbyte)
3739 {
3740 if ((*mb_ptr2char)(hotkeys + i) == c)
3741 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003742 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 if (hotkeys[i] == c)
3746 break;
3747 ++retval;
3748 }
3749 if (hotkeys[i])
3750 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003751 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 continue;
3753 }
3754 break;
3755 }
3756
3757 vim_free(hotkeys);
3758 }
3759
Bram Moolenaar27321db2020-07-06 21:24:57 +02003760 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 --no_wait_return;
3764 msg_end_prompt();
3765
3766 return retval;
3767}
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769/*
3770 * Copy one character from "*from" to "*to", taking care of multi-byte
3771 * characters. Return the length of the character in bytes.
3772 */
3773 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003774copy_char(
3775 char_u *from,
3776 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003777 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 int len;
3780 int c;
3781
3782 if (has_mbyte)
3783 {
3784 if (lowercase)
3785 {
3786 c = MB_TOLOWER((*mb_ptr2char)(from));
3787 return (*mb_char2bytes)(c, to);
3788 }
3789 else
3790 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003791 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 mch_memmove(to, from, (size_t)len);
3793 return len;
3794 }
3795 }
3796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
3798 if (lowercase)
3799 *to = (char_u)TOLOWER_LOC(*from);
3800 else
3801 *to = *from;
3802 return 1;
3803 }
3804}
3805
3806/*
3807 * Format the dialog string, and display it at the bottom of
3808 * the screen. Return a string of hotkey chars (if defined) for
3809 * each 'button'. If a button has no hotkey defined, the first character of
3810 * the button is used.
3811 * The hotkeys can be multi-byte characters, but without combining chars.
3812 *
3813 * Returns an allocated string with hotkeys, or NULL for error.
3814 */
3815 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003816msg_show_console_dialog(
3817 char_u *message,
3818 char_u *buttons,
3819 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003822#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003823 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 char_u *hotk = NULL;
3825 char_u *msgp = NULL;
3826 char_u *hotkp = NULL;
3827 char_u *r;
3828 int copy;
3829#define HAS_HOTKEY_LEN 30
3830 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003831 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 int idx;
3833
3834 has_hotkey[0] = FALSE;
3835
3836 /*
3837 * First loop: compute the size of memory to allocate.
3838 * Second loop: copy to the allocated memory.
3839 */
3840 for (copy = 0; copy <= 1; ++copy)
3841 {
3842 r = buttons;
3843 idx = 0;
3844 while (*r)
3845 {
3846 if (*r == DLG_BUTTON_SEP)
3847 {
3848 if (copy)
3849 {
3850 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003851 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar85a20022019-12-21 18:25:54 +01003853 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003855 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003858 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (dfltbutton)
3860 --dfltbutton;
3861
Bram Moolenaar85a20022019-12-21 18:25:54 +01003862 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3864 first_hotkey = TRUE;
3865 }
3866 else
3867 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003868 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3869 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (idx < HAS_HOTKEY_LEN - 1)
3871 has_hotkey[++idx] = FALSE;
3872 }
3873 }
3874 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3875 {
3876 if (*r == DLG_HOTKEY_CHAR)
3877 ++r;
3878 first_hotkey = FALSE;
3879 if (copy)
3880 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003881 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 *msgp++ = *r;
3883 else
3884 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003885 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3887 msgp += copy_char(r, msgp, FALSE);
3888 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3889
Bram Moolenaar85a20022019-12-21 18:25:54 +01003890 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003891 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894 else
3895 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003896 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (idx < HAS_HOTKEY_LEN - 1)
3898 has_hotkey[idx] = TRUE;
3899 }
3900 }
3901 else
3902 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003903 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (copy)
3905 msgp += copy_char(r, msgp, FALSE);
3906 }
3907
Bram Moolenaar85a20022019-12-21 18:25:54 +01003908 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003909 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911
3912 if (copy)
3913 {
3914 *msgp++ = ':';
3915 *msgp++ = ' ';
3916 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 else
3919 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003920 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003921 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003922 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003923 + 3); // for the ": " and NUL
3924 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaar85a20022019-12-21 18:25:54 +01003926 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if (!has_hotkey[0])
3928 {
3929 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003930 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932
3933 /*
3934 * Now allocate and load the strings
3935 */
3936 vim_free(confirm_msg);
3937 confirm_msg = alloc(len);
3938 if (confirm_msg == NULL)
3939 return NULL;
3940 *confirm_msg = NUL;
3941 hotk = alloc(lenhotkey);
3942 if (hotk == NULL)
3943 return NULL;
3944
3945 *confirm_msg = '\n';
3946 STRCPY(confirm_msg + 1, message);
3947
3948 msgp = confirm_msg + 1 + STRLEN(message);
3949 hotkp = hotk;
3950
Bram Moolenaar85a20022019-12-21 18:25:54 +01003951 // Define first default hotkey. Keep the hotkey string NUL
3952 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003953 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar85a20022019-12-21 18:25:54 +01003955 // Remember where the choices start, displaying starts here when
3956 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 confirm_msg_tail = msgp;
3958 *msgp++ = '\n';
3959 }
3960 }
3961
3962 display_confirm_msg();
3963 return hotk;
3964}
3965
3966/*
3967 * Display the ":confirm" message. Also called when screen resized.
3968 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003969 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003970display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003972 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 ++confirm_msg_used;
3974 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003975 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 --confirm_msg_used;
3977}
3978
Bram Moolenaar85a20022019-12-21 18:25:54 +01003979#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3982
3983 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003984vim_dialog_yesno(
3985 int type,
3986 char_u *title,
3987 char_u *message,
3988 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
3990 if (do_dialog(type,
3991 title == NULL ? (char_u *)_("Question") : title,
3992 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003993 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 return VIM_YES;
3995 return VIM_NO;
3996}
3997
3998 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003999vim_dialog_yesnocancel(
4000 int type,
4001 char_u *title,
4002 char_u *message,
4003 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 switch (do_dialog(type,
4006 title == NULL ? (char_u *)_("Question") : title,
4007 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004008 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
4010 case 1: return VIM_YES;
4011 case 2: return VIM_NO;
4012 }
4013 return VIM_CANCEL;
4014}
4015
4016 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004017vim_dialog_yesnoallcancel(
4018 int type,
4019 char_u *title,
4020 char_u *message,
4021 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
4023 switch (do_dialog(type,
4024 title == NULL ? (char_u *)"Question" : title,
4025 message,
4026 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004027 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
4029 case 1: return VIM_YES;
4030 case 2: return VIM_NO;
4031 case 3: return VIM_ALL;
4032 case 4: return VIM_DISCARDALL;
4033 }
4034 return VIM_CANCEL;
4035}
4036
Bram Moolenaar85a20022019-12-21 18:25:54 +01004037#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004039#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004040static char *e_printf = N_("E766: Insufficient arguments for printf()");
4041
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004042/*
4043 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4044 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004045 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004046tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047{
4048 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004049 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004050 int err = FALSE;
4051
4052 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004053 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054 else
4055 {
4056 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004057 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 if (err)
4059 n = 0;
4060 }
4061 return n;
4062}
4063
4064/*
4065 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004066 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004067 * are not converted to a string.
4068 * If "tofree" is not NULL echo_string() is used. All types are converted to
4069 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004070 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004071 */
4072 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004073tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004075 int idx = *idxp - 1;
4076 char *s = NULL;
4077 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078
4079 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004080 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004081 else
4082 {
4083 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004084 if (tofree != NULL)
4085 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4086 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004087 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 }
4089 return s;
4090}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004091
4092# ifdef FEAT_FLOAT
4093/*
4094 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4095 */
4096 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004097tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004098{
4099 int idx = *idxp - 1;
4100 double f = 0;
4101
4102 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004103 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004104 else
4105 {
4106 ++*idxp;
4107 if (tvs[idx].v_type == VAR_FLOAT)
4108 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004109 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004110 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004111 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004112 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004113 }
4114 return f;
4115}
4116# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117#endif
4118
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004119#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004120/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004121 * Return the representation of infinity for printf() function:
4122 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4123 */
4124 static const char *
4125infinity_str(int positive,
4126 char fmt_spec,
4127 int force_sign,
4128 int space_for_positive)
4129{
4130 static const char *table[] =
4131 {
4132 "-inf", "inf", "+inf", " inf",
4133 "-INF", "INF", "+INF", " INF"
4134 };
4135 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4136
4137 if (ASCII_ISUPPER(fmt_spec))
4138 idx += 4;
4139 return table[idx];
4140}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004141#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004142
4143/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004144 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004145 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004146 * consistency.
4147 *
4148 * This code is based on snprintf.c - a portable implementation of snprintf
4149 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004150 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004151 * The original code, including useful comments, can be found here:
4152 * http://www.ijs.si/software/snprintf/
4153 *
4154 * This snprintf() only supports the following conversion specifiers:
4155 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4156 * with flags: '-', '+', ' ', '0' and '#'.
4157 * An asterisk is supported for field width as well as precision.
4158 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004159 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004160 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004161 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004162 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004163 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004164 * The locale is not used, the string is used as a byte string. This is only
4165 * relevant for double-byte encodings where the second byte may be '%'.
4166 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004167 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4168 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004169 *
4170 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004171 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004172 * is greater or equal to "str_m", not all characters from the result
4173 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4174 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004175 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004176 */
4177
4178/*
4179 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004181 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004182 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004183 */
4184
Bram Moolenaar85a20022019-12-21 18:25:54 +01004185// When generating prototypes all of this is skipped, cproto doesn't
4186// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004187#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004188
Bram Moolenaar85a20022019-12-21 18:25:54 +01004189// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004190 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004191vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004192{
4193 va_list ap;
4194 int str_l;
4195 size_t len = STRLEN(str);
4196 size_t space;
4197
4198 if (str_m <= len)
4199 space = 0;
4200 else
4201 space = str_m - len;
4202 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004203 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004204 va_end(ap);
4205 return str_l;
4206}
Bram Moolenaara800b422010-06-27 01:15:55 +02004207
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004208 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004209vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004210{
4211 va_list ap;
4212 int str_l;
4213
4214 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004215 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004216 va_end(ap);
4217 return str_l;
4218}
4219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004220 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004221vim_vsnprintf(
4222 char *str,
4223 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004224 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004225 va_list ap)
4226{
4227 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4228}
4229
4230 int
4231vim_vsnprintf_typval(
4232 char *str,
4233 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004234 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004235 va_list ap,
4236 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004237{
4238 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004239 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004240 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004241
4242 if (p == NULL)
4243 p = "";
4244 while (*p != NUL)
4245 {
4246 if (*p != '%')
4247 {
4248 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004249 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004250
Bram Moolenaar85a20022019-12-21 18:25:54 +01004251 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252 if (str_l < str_m)
4253 {
4254 size_t avail = str_m - str_l;
4255
4256 mch_memmove(str + str_l, p, n > avail ? avail : n);
4257 }
4258 p += n;
4259 str_l += n;
4260 }
4261 else
4262 {
4263 size_t min_field_width = 0, precision = 0;
4264 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4265 int alternate_form = 0, force_sign = 0;
4266
Bram Moolenaar85a20022019-12-21 18:25:54 +01004267 // If both the ' ' and '+' flags appear, the ' ' flag should be
4268 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004269 int space_for_positive = 1;
4270
Bram Moolenaar85a20022019-12-21 18:25:54 +01004271 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004272 char length_modifier = '\0';
4273
Bram Moolenaar85a20022019-12-21 18:25:54 +01004274 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004275# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004276# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4277 // That sounds reasonable to use as the maximum
4278 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004279# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004280# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004281# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004282 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283
Bram Moolenaar85a20022019-12-21 18:25:54 +01004284 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004285 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004286
Bram Moolenaar85a20022019-12-21 18:25:54 +01004287 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004288 size_t str_arg_l;
4289
Bram Moolenaar85a20022019-12-21 18:25:54 +01004290 // unsigned char argument value - only defined for c conversion.
4291 // N.B. standard explicitly states the char argument for the c
4292 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004293 unsigned char uchar_arg;
4294
Bram Moolenaar85a20022019-12-21 18:25:54 +01004295 // number of zeros to be inserted for numeric conversions as
4296 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004297 size_t number_of_zeros_to_pad = 0;
4298
Bram Moolenaar85a20022019-12-21 18:25:54 +01004299 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004300 size_t zero_padding_insertion_ind = 0;
4301
Bram Moolenaar85a20022019-12-21 18:25:54 +01004302 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303 char fmt_spec = '\0';
4304
Bram Moolenaar85a20022019-12-21 18:25:54 +01004305 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004306 char_u *tofree = NULL;
4307
4308
Bram Moolenaar85a20022019-12-21 18:25:54 +01004309 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004310
Bram Moolenaar85a20022019-12-21 18:25:54 +01004311 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004312 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4313 || *p == '#' || *p == '\'')
4314 {
4315 switch (*p)
4316 {
4317 case '0': zero_padding = 1; break;
4318 case '-': justify_left = 1; break;
4319 case '+': force_sign = 1; space_for_positive = 0; break;
4320 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004321 // If both the ' ' and '+' flags appear, the ' '
4322 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004323 break;
4324 case '#': alternate_form = 1; break;
4325 case '\'': break;
4326 }
4327 p++;
4328 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004329 // If the '0' and '-' flags both appear, the '0' flag should be
4330 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004331
Bram Moolenaar85a20022019-12-21 18:25:54 +01004332 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004333 if (*p == '*')
4334 {
4335 int j;
4336
4337 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004339# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004340 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341# endif
4342 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004343 if (j >= 0)
4344 min_field_width = j;
4345 else
4346 {
4347 min_field_width = -j;
4348 justify_left = 1;
4349 }
4350 }
4351 else if (VIM_ISDIGIT((int)(*p)))
4352 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004353 // size_t could be wider than unsigned int; make sure we treat
4354 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004355 unsigned int uj = *p++ - '0';
4356
4357 while (VIM_ISDIGIT((int)(*p)))
4358 uj = 10 * uj + (unsigned int)(*p++ - '0');
4359 min_field_width = uj;
4360 }
4361
Bram Moolenaar85a20022019-12-21 18:25:54 +01004362 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004363 if (*p == '.')
4364 {
4365 p++;
4366 precision_specified = 1;
4367 if (*p == '*')
4368 {
4369 int j;
4370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004373 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374# endif
4375 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004376 p++;
4377 if (j >= 0)
4378 precision = j;
4379 else
4380 {
4381 precision_specified = 0;
4382 precision = 0;
4383 }
4384 }
4385 else if (VIM_ISDIGIT((int)(*p)))
4386 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004387 // size_t could be wider than unsigned int; make sure we
4388 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004389 unsigned int uj = *p++ - '0';
4390
4391 while (VIM_ISDIGIT((int)(*p)))
4392 uj = 10 * uj + (unsigned int)(*p++ - '0');
4393 precision = uj;
4394 }
4395 }
4396
Bram Moolenaar85a20022019-12-21 18:25:54 +01004397 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004398 if (*p == 'h' || *p == 'l')
4399 {
4400 length_modifier = *p;
4401 p++;
4402 if (length_modifier == 'l' && *p == 'l')
4403 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004404 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004405 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004406 p++;
4407 }
4408 }
4409 fmt_spec = *p;
4410
Bram Moolenaar85a20022019-12-21 18:25:54 +01004411 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004412 switch (fmt_spec)
4413 {
4414 case 'i': fmt_spec = 'd'; break;
4415 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4416 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4417 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4418 default: break;
4419 }
4420
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004421# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004422 switch (fmt_spec)
4423 {
4424 case 'd': case 'u': case 'o': case 'x': case 'X':
4425 if (tvs != NULL && length_modifier == '\0')
4426 length_modifier = 'L';
4427 }
4428# endif
4429
Bram Moolenaar85a20022019-12-21 18:25:54 +01004430 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004431 switch (fmt_spec)
4432 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004433 // '%' and 'c' behave similar to 's' regarding flags and field
4434 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004435 case '%':
4436 case 'c':
4437 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004438 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004439 str_arg_l = 1;
4440 switch (fmt_spec)
4441 {
4442 case '%':
4443 str_arg = p;
4444 break;
4445
4446 case 'c':
4447 {
4448 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449
4450 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004452 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453# endif
4454 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004455 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004456 uchar_arg = (unsigned char)j;
4457 str_arg = (char *)&uchar_arg;
4458 break;
4459 }
4460
4461 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004462 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004465 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466# endif
4467 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004468 if (str_arg == NULL)
4469 {
4470 str_arg = "[NULL]";
4471 str_arg_l = 6;
4472 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004473 // make sure not to address string beyond the specified
4474 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004475 else if (!precision_specified)
4476 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004477 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004478 else if (precision == 0)
4479 str_arg_l = 0;
4480 else
4481 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004482 // Don't put the #if inside memchr(), it can be a
4483 // macro.
4484 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004485 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004486 precision <= (size_t)0x7fffffffL ? precision
4487 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004488 str_arg_l = (q == NULL) ? precision
4489 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004490 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004491 if (fmt_spec == 'S')
4492 {
4493 if (min_field_width != 0)
4494 min_field_width += STRLEN(str_arg)
4495 - mb_string2cells((char_u *)str_arg, -1);
4496 if (precision)
4497 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004498 char_u *p1;
4499 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004500
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004501 for (p1 = (char_u *)str_arg; *p1;
4502 p1 += mb_ptr2len(p1))
4503 {
4504 i += (size_t)mb_ptr2cells(p1);
4505 if (i > precision)
4506 break;
4507 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004508 str_arg_l = precision = p1 - (char_u *)str_arg;
4509 }
4510 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004511 break;
4512
4513 default:
4514 break;
4515 }
4516 break;
4517
Bram Moolenaar91984b92016-08-16 21:58:41 +02004518 case 'd': case 'u':
4519 case 'b': case 'B':
4520 case 'o':
4521 case 'x': case 'X':
4522 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004523 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004524 // NOTE: the u, b, o, x, X and p conversion specifiers
4525 // imply the value is unsigned; d implies a signed
4526 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527
Bram Moolenaar85a20022019-12-21 18:25:54 +01004528 // 0 if numeric argument is zero (or if pointer is
4529 // NULL for 'p'), +1 if greater than zero (or nonzero
4530 // for unsigned arguments), -1 if negative (unsigned
4531 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 int arg_sign = 0;
4533
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004534 // only set for length modifier h, or for no length
4535 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004536 int int_arg = 0;
4537 unsigned int uint_arg = 0;
4538
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004539 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004540 long int long_arg = 0;
4541 unsigned long int ulong_arg = 0;
4542
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004543 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004544 varnumber_T llong_arg = 0;
4545 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004546
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004547 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004548 uvarnumber_T bin_arg = 0;
4549
Bram Moolenaar85a20022019-12-21 18:25:54 +01004550 // pointer argument value -only defined for p
4551 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004552 void *ptr_arg = NULL;
4553
4554 if (fmt_spec == 'p')
4555 {
4556 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004559 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4560 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004561# endif
4562 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004563 if (ptr_arg != NULL)
4564 arg_sign = 1;
4565 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004566 else if (fmt_spec == 'b' || fmt_spec == 'B')
4567 {
4568 bin_arg =
4569# if defined(FEAT_EVAL)
4570 tvs != NULL ?
4571 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4572# endif
4573 va_arg(ap, uvarnumber_T);
4574 if (bin_arg != 0)
4575 arg_sign = 1;
4576 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004577 else if (fmt_spec == 'd')
4578 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004579 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004580 switch (length_modifier)
4581 {
4582 case '\0':
4583 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004584 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004587 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004588# endif
4589 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004590 if (int_arg > 0)
4591 arg_sign = 1;
4592 else if (int_arg < 0)
4593 arg_sign = -1;
4594 break;
4595 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004598 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004599# endif
4600 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004601 if (long_arg > 0)
4602 arg_sign = 1;
4603 else if (long_arg < 0)
4604 arg_sign = -1;
4605 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004606 case 'L':
4607 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004608# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004609 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004610# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004611 va_arg(ap, varnumber_T);
4612 if (llong_arg > 0)
4613 arg_sign = 1;
4614 else if (llong_arg < 0)
4615 arg_sign = -1;
4616 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004617 }
4618 }
4619 else
4620 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004621 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004622 switch (length_modifier)
4623 {
4624 case '\0':
4625 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004626 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004628 tvs != NULL ? (unsigned)
4629 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630# endif
4631 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004632 if (uint_arg != 0)
4633 arg_sign = 1;
4634 break;
4635 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004636 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004638 tvs != NULL ? (unsigned long)
4639 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004640# endif
4641 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004642 if (ulong_arg != 0)
4643 arg_sign = 1;
4644 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004645 case 'L':
4646 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004647# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004648 tvs != NULL ? (uvarnumber_T)
4649 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004650# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004651 va_arg(ap, uvarnumber_T);
4652 if (ullong_arg != 0)
4653 arg_sign = 1;
4654 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004655 }
4656 }
4657
4658 str_arg = tmp;
4659 str_arg_l = 0;
4660
Bram Moolenaar85a20022019-12-21 18:25:54 +01004661 // NOTE:
4662 // For d, i, u, o, x, and X conversions, if precision is
4663 // specified, the '0' flag should be ignored. This is so
4664 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4665 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004666 if (precision_specified)
4667 zero_padding = 0;
4668 if (fmt_spec == 'd')
4669 {
4670 if (force_sign && arg_sign >= 0)
4671 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004672 // leave negative numbers for sprintf to handle, to
4673 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004674 }
4675 else if (alternate_form)
4676 {
4677 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004678 && (fmt_spec == 'b' || fmt_spec == 'B'
4679 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004680 {
4681 tmp[str_arg_l++] = '0';
4682 tmp[str_arg_l++] = fmt_spec;
4683 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004684 // alternate form should have no effect for p
4685 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004686 }
4687
4688 zero_padding_insertion_ind = str_arg_l;
4689 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004690 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004691 if (precision == 0 && arg_sign == 0)
4692 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004693 // When zero value is formatted with an explicit
4694 // precision 0, the resulting formatted string is
4695 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004696 }
4697 else
4698 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004699 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004700 int f_l = 0;
4701
Bram Moolenaar85a20022019-12-21 18:25:54 +01004702 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004703 f[f_l++] = '%';
4704 if (!length_modifier)
4705 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004706 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004707 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004708# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004709 f[f_l++] = 'I';
4710 f[f_l++] = '6';
4711 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004712# else
4713 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004714 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004715# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004716 }
4717 else
4718 f[f_l++] = length_modifier;
4719 f[f_l++] = fmt_spec;
4720 f[f_l++] = '\0';
4721
4722 if (fmt_spec == 'p')
4723 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004724 else if (fmt_spec == 'b' || fmt_spec == 'B')
4725 {
4726 char b[8 * sizeof(uvarnumber_T)];
4727 size_t b_l = 0;
4728 uvarnumber_T bn = bin_arg;
4729
4730 do
4731 {
4732 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4733 bn >>= 1;
4734 }
4735 while (bn != 0);
4736
4737 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4738 str_arg_l += b_l;
4739 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004740 else if (fmt_spec == 'd')
4741 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004742 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 switch (length_modifier)
4744 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004745 case '\0': str_arg_l += sprintf(
4746 tmp + str_arg_l, f,
4747 int_arg);
4748 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004749 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004750 tmp + str_arg_l, f,
4751 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004752 break;
4753 case 'l': str_arg_l += sprintf(
4754 tmp + str_arg_l, f, long_arg);
4755 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004756 case 'L': str_arg_l += sprintf(
4757 tmp + str_arg_l, f, llong_arg);
4758 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004759 }
4760 }
4761 else
4762 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004763 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004764 switch (length_modifier)
4765 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004766 case '\0': str_arg_l += sprintf(
4767 tmp + str_arg_l, f,
4768 uint_arg);
4769 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004770 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004771 tmp + str_arg_l, f,
4772 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004773 break;
4774 case 'l': str_arg_l += sprintf(
4775 tmp + str_arg_l, f, ulong_arg);
4776 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004777 case 'L': str_arg_l += sprintf(
4778 tmp + str_arg_l, f, ullong_arg);
4779 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004780 }
4781 }
4782
Bram Moolenaar85a20022019-12-21 18:25:54 +01004783 // include the optional minus sign and possible
4784 // "0x" in the region before the zero padding
4785 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004786 if (zero_padding_insertion_ind < str_arg_l
4787 && tmp[zero_padding_insertion_ind] == '-')
4788 zero_padding_insertion_ind++;
4789 if (zero_padding_insertion_ind + 1 < str_arg_l
4790 && tmp[zero_padding_insertion_ind] == '0'
4791 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4792 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4793 zero_padding_insertion_ind += 2;
4794 }
4795
4796 {
4797 size_t num_of_digits = str_arg_l
4798 - zero_padding_insertion_ind;
4799
4800 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004801 // unless zero is already the first
4802 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803 && !(zero_padding_insertion_ind < str_arg_l
4804 && tmp[zero_padding_insertion_ind] == '0'))
4805 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004806 // assure leading zero for alternate-form
4807 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004808 if (!precision_specified
4809 || precision < num_of_digits + 1)
4810 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004811 // precision is increased to force the
4812 // first character to be zero, except if a
4813 // zero value is formatted with an
4814 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004815 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004816 }
4817 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004818 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004819 if (num_of_digits < precision)
4820 number_of_zeros_to_pad = precision - num_of_digits;
4821 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004822 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004823 if (!justify_left && zero_padding)
4824 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004825 int n = (int)(min_field_width - (str_arg_l
4826 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004827 if (n > 0)
4828 number_of_zeros_to_pad += n;
4829 }
4830 break;
4831 }
4832
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004833# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004834 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004835 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004836 case 'e':
4837 case 'E':
4838 case 'g':
4839 case 'G':
4840 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004841 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004842 double f;
4843 double abs_f;
4844 char format[40];
4845 int l;
4846 int remove_trailing_zeroes = FALSE;
4847
4848 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004849# if defined(FEAT_EVAL)
4850 tvs != NULL ? tv_float(tvs, &arg_idx) :
4851# endif
4852 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004853 abs_f = f < 0 ? -f : f;
4854
4855 if (fmt_spec == 'g' || fmt_spec == 'G')
4856 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004857 // Would be nice to use %g directly, but it prints
4858 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004859 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4860 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004861 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004862 else
4863 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4864 remove_trailing_zeroes = TRUE;
4865 }
4866
Bram Moolenaar04186092016-08-29 21:55:35 +02004867 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004868# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004869 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004870# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004871 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004872# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004873 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004874 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004875 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004876 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4877 force_sign, space_for_positive));
4878 str_arg_l = STRLEN(tmp);
4879 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004880 }
4881 else
4882 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004883 if (isnan(f))
4884 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004885 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004886 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4887 : "nan");
4888 str_arg_l = 3;
4889 zero_padding = 0;
4890 }
4891 else if (isinf(f))
4892 {
4893 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4894 force_sign, space_for_positive));
4895 str_arg_l = STRLEN(tmp);
4896 zero_padding = 0;
4897 }
4898 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004899 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004900 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004901 format[0] = '%';
4902 l = 1;
4903 if (force_sign)
4904 format[l++] = space_for_positive ? ' ' : '+';
4905 if (precision_specified)
4906 {
4907 size_t max_prec = TMP_LEN - 10;
4908
Bram Moolenaar85a20022019-12-21 18:25:54 +01004909 // Make sure we don't get more digits than we
4910 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004911 if ((fmt_spec == 'f' || fmt_spec == 'F')
4912 && abs_f > 1.0)
4913 max_prec -= (size_t)log10(abs_f);
4914 if (precision > max_prec)
4915 precision = max_prec;
4916 l += sprintf(format + l, ".%d", (int)precision);
4917 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004918 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004919 format[l + 1] = NUL;
4920
Bram Moolenaare9997822016-08-28 16:03:38 +02004921 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004922 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004923
Bram Moolenaara7241f52008-06-24 20:39:31 +00004924 if (remove_trailing_zeroes)
4925 {
4926 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004927 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004928
Bram Moolenaar85a20022019-12-21 18:25:54 +01004929 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004930 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004931 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932 else
4933 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004934 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004935 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004936 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004937 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004938 // Remove superfluous '+' and leading
4939 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004940 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004941 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004942 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004943 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004944 --str_arg_l;
4945 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004946 i = (tp[1] == '-') ? 2 : 1;
4947 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004949 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004950 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004951 --str_arg_l;
4952 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004953 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004954 }
4955 }
4956
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004957 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004958 // Remove trailing zeroes, but keep the one
4959 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004960 while (tp > tmp + 2 && *tp == '0'
4961 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004962 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004963 STRMOVE(tp, tp + 1);
4964 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004965 --str_arg_l;
4966 }
4967 }
4968 else
4969 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004970 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004971
Bram Moolenaar85a20022019-12-21 18:25:54 +01004972 // Be consistent: some printf("%e") use 1.0e+12
4973 // and some 1.0e+012. Remove one zero in the last
4974 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004975 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004976 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004977 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4978 && tp[2] == '0'
4979 && vim_isdigit(tp[3])
4980 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004981 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004982 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004983 --str_arg_l;
4984 }
4985 }
4986 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004987 if (zero_padding && min_field_width > str_arg_l
4988 && (tmp[0] == '-' || force_sign))
4989 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004990 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02004991 number_of_zeros_to_pad = min_field_width - str_arg_l;
4992 zero_padding_insertion_ind = 1;
4993 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004994 str_arg = tmp;
4995 break;
4996 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004997# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004998
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004999 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01005000 // unrecognized conversion specifier, keep format string
5001 // as-is
5002 zero_padding = 0; // turn zero padding off for non-numeric
5003 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005004 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01005005 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005006
Bram Moolenaar85a20022019-12-21 18:25:54 +01005007 // discard the unrecognized conversion, just keep *
5008 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005009 str_arg = p;
5010 str_arg_l = 0;
5011 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005012 str_arg_l++; // include invalid conversion specifier
5013 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005014 break;
5015 }
5016
5017 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005018 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005019
Bram Moolenaar85a20022019-12-21 18:25:54 +01005020 // insert padding to the left as requested by min_field_width;
5021 // this does not include the zero padding in case of numerical
5022 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005023 if (!justify_left)
5024 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005025 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005026 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005027
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005028 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005029 {
5030 if (str_l < str_m)
5031 {
5032 size_t avail = str_m - str_l;
5033
5034 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005035 (size_t)pn > avail ? avail
5036 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005037 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005038 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005039 }
5040 }
5041
Bram Moolenaar85a20022019-12-21 18:25:54 +01005042 // zero padding as requested by the precision or by the minimal
5043 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005044 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005045 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005046 // will not copy first part of numeric right now, *
5047 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005048 zero_padding_insertion_ind = 0;
5049 }
5050 else
5051 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005052 // insert first part of numerics (sign or '0x') before zero
5053 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005054 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005055
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005056 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005057 {
5058 if (str_l < str_m)
5059 {
5060 size_t avail = str_m - str_l;
5061
5062 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005063 (size_t)zn > avail ? avail
5064 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005065 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005066 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005067 }
5068
Bram Moolenaar85a20022019-12-21 18:25:54 +01005069 // insert zero padding as requested by the precision or min
5070 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005071 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005072 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005073 {
5074 if (str_l < str_m)
5075 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005076 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005077
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005078 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005079 (size_t)zn > avail ? avail
5080 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005081 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005082 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005083 }
5084 }
5085
Bram Moolenaar85a20022019-12-21 18:25:54 +01005086 // insert formatted string
5087 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005088 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005089 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005090
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005091 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005092 {
5093 if (str_l < str_m)
5094 {
5095 size_t avail = str_m - str_l;
5096
5097 mch_memmove(str + str_l,
5098 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005099 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005100 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005101 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005102 }
5103 }
5104
Bram Moolenaar85a20022019-12-21 18:25:54 +01005105 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005106 if (justify_left)
5107 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005108 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005109 int pn = (int)(min_field_width
5110 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005111
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005112 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005113 {
5114 if (str_l < str_m)
5115 {
5116 size_t avail = str_m - str_l;
5117
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005118 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005119 (size_t)pn > avail ? avail
5120 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005121 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005122 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005123 }
5124 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005125 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005126 }
5127 }
5128
5129 if (str_m > 0)
5130 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005131 // make sure the string is nul-terminated even at the expense of
5132 // overwriting the last character (shouldn't happen, but just in case)
5133 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005134 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5135 }
5136
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005137 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005138 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139
Bram Moolenaar85a20022019-12-21 18:25:54 +01005140 // Return the number of characters formatted (excluding trailing nul
5141 // character), that is, the number of characters that would have been
5142 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005143 return (int)str_l;
5144}
5145
Bram Moolenaar85a20022019-12-21 18:25:54 +01005146#endif // PROTO