blob: 43e7e5e526d20eb1fde1b25b7104350e74c5073f [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 Moolenaar4f25b1a2020-09-10 19:25:05 +0200464 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100465 char_u *tofree = sname;
466
467 if (sname == NULL)
468 sname = SOURCING_NAME;
469
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200470#ifdef FEAT_EVAL
471 if (estack_compiling)
472 p = (char_u *)_("Error detected while compiling %s:");
473 else
474#endif
475 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100476 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100478 sprintf((char *)Buf, (char *)p, sname);
479 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 return Buf;
481 }
482 return NULL;
483}
484
485/*
486 * Get the message about the source lnum, as used for an error message.
487 * Returns an allocated string with room for one more character.
488 * Returns NULL when no message is to be given.
489 */
490 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100491get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 char_u *Buf, *p;
494
Bram Moolenaar85a20022019-12-21 18:25:54 +0100495 // lnum is 0 when executing a command from the command line
496 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100497 if (SOURCING_NAME != NULL
498 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
499 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200502 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100504 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 return Buf;
506 }
507 return NULL;
508}
509
510/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000511 * Display name and line number for the source of an error.
512 * Remember the file name and line number, so that for the next error the info
513 * is only displayed if it changed.
514 */
515 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100516msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000517{
518 char_u *p;
519
520 ++no_wait_return;
521 p = get_emsg_source();
522 if (p != NULL)
523 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100524 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000525 vim_free(p);
526 }
527 p = get_emsg_lnum();
528 if (p != NULL)
529 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100530 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000531 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100532 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000533 }
534
Bram Moolenaar85a20022019-12-21 18:25:54 +0100535 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100536 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537 {
538 vim_free(last_sourcing_name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100539 if (SOURCING_NAME == NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000540 last_sourcing_name = NULL;
541 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100542 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000543 }
544 --no_wait_return;
545}
546
547/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000548 * Return TRUE if not giving error messages right now:
549 * If "emsg_off" is set: no error messages at the moment.
550 * If "msg" is in 'debug': do error message but without side effects.
551 * If "emsg_skip" is set: never do error messages.
552 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200553 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100554emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000555{
556 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
557 && vim_strchr(p_debug, 't') == NULL)
558#ifdef FEAT_EVAL
559 || emsg_skip > 0
560#endif
561 )
562 return TRUE;
563 return FALSE;
564}
565
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100566#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100567static garray_T ignore_error_list = GA_EMPTY;
568
569 void
570ignore_error_for_testing(char_u *error)
571{
572 if (ignore_error_list.ga_itemsize == 0)
573 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
574
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100575 if (STRCMP("RESET", error) == 0)
576 ga_clear_strings(&ignore_error_list);
577 else
578 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100579}
580
581 static int
582ignore_error(char_u *msg)
583{
584 int i;
585
586 for (i = 0; i < ignore_error_list.ga_len; ++i)
587 if (strstr((char *)msg,
588 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
589 return TRUE;
590 return FALSE;
591}
592#endif
593
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200594#if !defined(HAVE_STRERROR) || defined(PROTO)
595/*
596 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100597 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200598 */
599 void
600do_perror(char *msg)
601{
602 perror(msg);
603 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100604 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200605 --emsg_silent;
606}
607#endif
608
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000609/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100610 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 *
612 * Rings the bell, if appropriate, and calls message() to do the real work
613 * When terminal not initialized (yet) mch_errmsg(..) is used.
614 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100615 * Return TRUE if wait_return not called.
616 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100618 static int
619emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100623 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#ifdef FEAT_EVAL
625 int ignore = FALSE;
626 int severe;
627#endif
628
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100629#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100630 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100631 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100632 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100633 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100634#endif
635
Bram Moolenaar53989552019-12-23 22:59:18 +0100636 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100639 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
640 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 severe = emsg_severe;
642 emsg_severe = FALSE;
643#endif
644
Bram Moolenaar57657d82006-04-21 22:12:41 +0000645 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {
647#ifdef FEAT_EVAL
648 /*
649 * Cause a throw of an error exception if appropriate. Don't display
650 * the error message in this case. (If no matching catch clause will
651 * be found, the message will be displayed later on.) "ignore" is set
652 * when the message should be ignored completely (used for the
653 * interrupt message).
654 */
655 if (cause_errthrow(s, severe, &ignore) == TRUE)
656 {
657 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100658 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return TRUE;
660 }
661
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100662 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200663 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200664 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200665 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200666 vim_free(emsg_assert_fails_context);
667 emsg_assert_fails_context = vim_strsave(
668 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200669 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200670
Bram Moolenaar85a20022019-12-21 18:25:54 +0100671 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 set_vim_var_string(VV_ERRMSG, s, -1);
673#endif
674
675 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000676 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * But do write it to the redirection file.
678 */
679 if (emsg_silent != 0)
680 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200681 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200683 msg_start();
684 p = get_emsg_source();
685 if (p != NULL)
686 {
687 STRCAT(p, "\n");
688 redir_write(p, -1);
689 vim_free(p);
690 }
691 p = get_emsg_lnum();
692 if (p != NULL)
693 {
694 STRCAT(p, "\n");
695 redir_write(p, -1);
696 vim_free(p);
697 }
698 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100700#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200701 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 return TRUE;
704 }
705
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100706 ex_exitval = 1;
707
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 msg_silent = 0;
710 cmd_silent = FALSE;
711
Bram Moolenaar85a20022019-12-21 18:25:54 +0100712 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 ++global_busy;
714
715 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100716 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200718 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100719 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200720#ifdef FEAT_EVAL
721 did_uncaught_emsg = TRUE;
722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
724
Bram Moolenaar85a20022019-12-21 18:25:54 +0100725 emsg_on_display = TRUE; // remember there is an error message
726 ++msg_scroll; // don't overwrite a previous message
727 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000728 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100729 need_wait_return = TRUE; // needed in case emsg() is called after
730 // wait_return has reset need_wait_return
731 // and a redraw is expected because
732 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
Bram Moolenaar958dc692016-12-01 15:34:12 +0100734#ifdef FEAT_JOB_CHANNEL
735 emsg_to_channel_log = TRUE;
736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 /*
738 * Display name and line number for the source of the error.
739 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000740 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
742 /*
743 * Display the error message itself.
744 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100745 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100746 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100747
748#ifdef FEAT_JOB_CHANNEL
749 emsg_to_channel_log = FALSE;
750#endif
751 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753
754/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100755 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100758emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100760 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100761 if (!emsg_not_now())
762 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100763 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764}
765
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100766#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100767/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100768 * Print an error message with format string and variable arguments.
769 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100770 */
771 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100773{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200774 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775 if (!emsg_not_now())
776 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200777 if (IObuff == NULL)
778 {
779 // Very early in initialisation and already something wrong, just
780 // give the raw message so the user at least gets a hint.
781 return emsg_core((char_u *)s);
782 }
783 else
784 {
785 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100786
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200787 va_start(ap, s);
788 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
789 va_end(ap);
790 return emsg_core(IObuff);
791 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100792 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200793 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100794}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100795#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100796
797/*
798 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
799 * defined. It is used for internal errors only, so that they can be
800 * detected when fuzzing vim.
801 */
802 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100804{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 if (!emsg_not_now())
806 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807#ifdef ABORT_ON_INTERNAL_ERROR
808 abort();
809#endif
810}
811
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100812#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100813/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100814 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100815 * defined. It is used for internal errors only, so that they can be
816 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100817 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818 */
819 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100821{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100822 if (!emsg_not_now())
823 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200824 if (IObuff == NULL)
825 {
826 // Very early in initialisation and already something wrong, just
827 // give the raw message so the user at least gets a hint.
828 emsg_core((char_u *)s);
829 }
830 else
831 {
832 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100833
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200834 va_start(ap, s);
835 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
836 va_end(ap);
837 emsg_core(IObuff);
838 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100839 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100840# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100841 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100842# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100843}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100844#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845
846/*
847 * Give an "Internal error" message.
848 */
849 void
850internal_error(char *where)
851{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100852 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100853}
854
Bram Moolenaardd589232020-02-29 17:38:12 +0100855/*
856 * Like internal_error() but do not call abort(), to avoid tests using
857 * test_unknown() and test_void() causing Vim to exit.
858 */
859 void
860internal_error_no_abort(char *where)
861{
862 semsg(_(e_intern2), where);
863}
864
Bram Moolenaar85a20022019-12-21 18:25:54 +0100865// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaardf177f62005-02-22 08:39:57 +0000867 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100868emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000869{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100870 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000871}
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 * Give an error message which contains %s for "name[len]".
875 */
876 void
877emsg_namelen(char *msg, char_u *name, int len)
878{
879 char_u *copy = vim_strnsave((char_u *)name, len);
880
881 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100882 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883}
884
885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * Like msg(), but truncate to a single line if p_shm contains 't', or when
887 * "force" is TRUE. This truncates in another way as for normal messages.
888 * Careful: The string may be changed by msg_may_trunc()!
889 * Returns a pointer to the printed message, if wait_return() not called.
890 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100891 char *
892msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100895 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar85a20022019-12-21 18:25:54 +0100897 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100898 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar32526b32019-01-19 17:43:09 +0100900 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100903 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 msg_hist_off = FALSE;
905
906 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100907 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return NULL;
909}
910
911/*
912 * Check if message "s" should be truncated at the start (for filenames).
913 * Return a pointer to where the truncated message starts.
914 * Note: May change the message by replacing a character with '<'.
915 */
916 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100917msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 int n;
920 int room;
921
922 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
923 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
924 && (n = (int)STRLEN(s) - room) > 0)
925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (has_mbyte)
927 {
928 int size = vim_strsize(s);
929
Bram Moolenaar85a20022019-12-21 18:25:54 +0100930 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000931 if (size <= room)
932 return s;
933
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 for (n = 0; size >= room; )
935 {
936 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000937 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
939 --n;
940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 s += n;
942 *s = '<';
943 }
944 return s;
945}
946
947 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100948add_msg_hist(
949 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100950 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100951 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
953 struct msg_hist *p;
954
955 if (msg_hist_off || msg_silent != 0)
956 return;
957
Bram Moolenaar85a20022019-12-21 18:25:54 +0100958 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000959 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000960 (void)delete_first_msg();
961
Bram Moolenaar85a20022019-12-21 18:25:54 +0100962 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200963 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (p != NULL)
965 {
966 if (len < 0)
967 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100968 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 while (len > 0 && *s == '\n')
970 {
971 ++s;
972 --len;
973 }
974 while (len > 0 && s[len - 1] == '\n')
975 --len;
976 p->msg = vim_strnsave(s, len);
977 p->next = NULL;
978 p->attr = attr;
979 if (last_msg_hist != NULL)
980 last_msg_hist->next = p;
981 last_msg_hist = p;
982 if (first_msg_hist == NULL)
983 first_msg_hist = last_msg_hist;
984 ++msg_hist_len;
985 }
986}
987
988/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000989 * Delete the first (oldest) message from the history.
990 * Returns FAIL if there are no messages.
991 */
992 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100993delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000994{
995 struct msg_hist *p;
996
997 if (msg_hist_len <= 0)
998 return FAIL;
999 p = first_msg_hist;
1000 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001001 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001002 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001003 vim_free(p->msg);
1004 vim_free(p);
1005 --msg_hist_len;
1006 return OK;
1007}
1008
1009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 * ":messages" command.
1011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001013ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
1015 struct msg_hist *p;
1016 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001017 int c = 0;
1018
1019 if (STRCMP(eap->arg, "clear") == 0)
1020 {
1021 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1022
1023 while (msg_hist_len > keep)
1024 (void)delete_first_msg();
1025 return;
1026 }
1027
1028 if (*eap->arg != NUL)
1029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001030 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001031 return;
1032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 msg_hist_off = TRUE;
1035
Bram Moolenaar451f8492016-04-14 17:16:22 +02001036 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001037 if (eap->addr_count != 0)
1038 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001039 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001040 for (; p != NULL && !got_int; p = p->next)
1041 c++;
1042
1043 c -= eap->line2;
1044
Bram Moolenaar85a20022019-12-21 18:25:54 +01001045 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001046 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1047 p = p->next, c--);
1048 }
1049
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001050 if (p == first_msg_hist)
1051 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001052#ifdef FEAT_MULTI_LANG
1053 s = get_mess_lang();
1054#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001055 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001056#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001057 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001058 // The next comment is extracted by xgettext and put in po file for
1059 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001060 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001061 // Translator: Please replace the name and email address
1062 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001063 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001064 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001065 }
1066
Bram Moolenaar85a20022019-12-21 18:25:54 +01001067 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001068 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001070 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
1072 msg_hist_off = FALSE;
1073}
1074
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001075#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076/*
1077 * Call this after prompting the user. This will avoid a hit-return message
1078 * and a delay.
1079 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001080 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001081msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 need_wait_return = FALSE;
1084 emsg_on_display = FALSE;
1085 cmdline_row = msg_row;
1086 msg_col = 0;
1087 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001088 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090#endif
1091
1092/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001093 * Wait for the user to hit a key (normally Enter).
1094 * If "redraw" is TRUE, clear and redraw the screen.
1095 * If "redraw" is FALSE, just redraw the screen.
1096 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 */
1098 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001099wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 int c;
1102 int oldState;
1103 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001105 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001106 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107
1108 if (redraw == TRUE)
1109 must_redraw = CLEAR;
1110
Bram Moolenaar85a20022019-12-21 18:25:54 +01001111 // If using ":silent cmd", don't wait for a return. Also don't set
1112 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 if (msg_silent != 0)
1114 return;
1115
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001116 /*
1117 * When inside vgetc(), we can't wait for a typed character at all.
1118 * With the global command (and some others) we only need one return at
1119 * the end. Adjust cmdline_row to avoid the next message overwriting the
1120 * last one.
1121 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001122 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001124 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (no_wait_return)
1126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (!exmode_active)
1128 cmdline_row = msg_row;
1129 return;
1130 }
1131
Bram Moolenaar85a20022019-12-21 18:25:54 +01001132 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 oldState = State;
1134 if (quit_more)
1135 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001136 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 quit_more = FALSE;
1138 got_int = FALSE;
1139 }
1140 else if (exmode_active)
1141 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 msg_puts(" "); // make sure the cursor is on the right line
1143 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 got_int = FALSE;
1145 }
1146 else
1147 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001148 // Make sure the hit-return prompt is on screen when 'guioptions' was
1149 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 screenalloc(FALSE);
1151
1152 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001155 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001157 cmdline_row = msg_row;
1158
Bram Moolenaar85a20022019-12-21 18:25:54 +01001159 // Avoid the sequence that the user types ":" at the hit-return prompt
1160 // to start an Ex command, but the file-changed dialog gets in the
1161 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001162 if (need_check_timestamps)
1163 check_timestamps(FALSE);
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 hit_return_msg();
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 do
1168 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001169 // Remember "got_int", if it is set vgetc() probably returns a
1170 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001172
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // Don't do mappings here, we put the character back in the
1174 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001175 ++no_mapping;
1176 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001177
Bram Moolenaar85a20022019-12-21 18:25:54 +01001178 // Temporarily disable Recording. If Recording is active, the
1179 // character will be recorded later, since it will be added to the
1180 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001181 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001182 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001183 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001184 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001186 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001188 --no_mapping;
1189 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001190 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001191 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001194 // Strange way to allow copying (yanking) a modeless selection at
1195 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1196 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1198 {
1199 clip_copy_modeless_selection(TRUE);
1200 c = K_IGNORE;
1201 }
1202#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001203
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001204 /*
1205 * Allow scrolling back in the messages.
1206 * Also accept scroll-down commands when messages fill the screen,
1207 * to avoid that typing one 'j' too many makes the messages
1208 * disappear.
1209 */
1210 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001211 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001212 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1213 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001214 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001215 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001216 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001217 do_more_prompt(c);
1218 else
1219 {
1220 msg_didout = FALSE;
1221 c = K_IGNORE;
1222 msg_col =
1223#ifdef FEAT_RIGHTLEFT
1224 cmdmsg_rl ? Columns - 1 :
1225#endif
1226 0;
1227 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001228 if (quit_more)
1229 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001230 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001231 quit_more = FALSE;
1232 got_int = FALSE;
1233 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001234 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001235 {
1236 c = K_IGNORE;
1237 hit_return_msg();
1238 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001239 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001240 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001241 && (c == 'j' || c == 'd' || c == 'f'
1242 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001243 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 } while ((had_got_int && c == Ctrl_C)
1246 || c == K_IGNORE
1247#ifdef FEAT_GUI
1248 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1251 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1252 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001253 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001255 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 || (!mouse_has(MOUSE_RETURN)
1257 && mouse_row < msg_row
1258 && (c == K_LEFTMOUSE
1259 || c == K_MIDDLEMOUSE
1260 || c == K_RIGHTMOUSE
1261 || c == K_X1MOUSE
1262 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 );
1264 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 /*
1266 * Avoid that the mouse-up event causes visual mode to start.
1267 */
1268 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1269 || c == K_X1MOUSE || c == K_X2MOUSE)
1270 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001271 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001273 // Put the character back in the typeahead buffer. Don't use the
1274 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001275 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001276 do_redraw = TRUE; // need a redraw even though there is
1277 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280 redir_off = FALSE;
1281
1282 /*
1283 * If the user hits ':', '?' or '/' we get a command line from the next
1284 * line.
1285 */
1286 if (c == ':' || c == '?' || c == '/')
1287 {
1288 if (!exmode_active)
1289 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001290 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001292#ifdef FEAT_TERMINAL
1293 skip_term_loop = TRUE;
1294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296
1297 /*
1298 * If the window size changed set_shellsize() will redraw the screen.
1299 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1300 * typed.
1301 */
1302 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 msg_check();
1306
1307#if defined(UNIX) || defined(VMS)
1308 /*
1309 * When switching screens, we need to output an extra newline on exit.
1310 */
1311 if (swapping_screen() && !termcap_active)
1312 newline_on_exit = TRUE;
1313#endif
1314
1315 need_wait_return = FALSE;
1316 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001317 emsg_on_display = FALSE; // can delete error message now
1318 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 reset_last_sourcing();
1320 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1321 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001326 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 shell_resized();
1328 }
1329 else if (!skip_redraw
1330 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001332 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 redraw_later(VALID);
1334 }
1335}
1336
1337/*
1338 * Write the hit-return prompt.
1339 */
1340 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001341hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001343 int save_p_more = p_more;
1344
Bram Moolenaar85a20022019-12-21 18:25:54 +01001345 p_more = FALSE; // don't want see this message when scrolling back
1346 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 msg_putchar('\n');
1348 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001349 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaar32526b32019-01-19 17:43:09 +01001351 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (!msg_use_printf())
1353 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001354 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355}
1356
1357/*
1358 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1359 */
1360 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001361set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 vim_free(keep_msg);
1364 if (s != NULL && msg_silent == 0)
1365 keep_msg = vim_strsave(s);
1366 else
1367 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001368 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001369 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370}
1371
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001372#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1373/*
1374 * If there currently is a message being displayed, set "keep_msg" to it, so
1375 * that it will be displayed again after redraw.
1376 */
1377 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001378set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001379{
1380 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1381 && (State & NORMAL))
1382 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1383}
1384#endif
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386/*
1387 * Prepare for outputting characters in the command line.
1388 */
1389 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001390msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 int did_return = FALSE;
1393
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001394 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001395 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001396
1397#ifdef FEAT_EVAL
1398 if (need_clr_eos)
1399 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001400 // Halfway an ":echo" command and getting an (error) message: clear
1401 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001402 need_clr_eos = FALSE;
1403 msg_clr_eos();
1404 }
1405#endif
1406
Bram Moolenaar85a20022019-12-21 18:25:54 +01001407 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 {
1409 msg_row = cmdline_row;
1410 msg_col =
1411#ifdef FEAT_RIGHTLEFT
1412 cmdmsg_rl ? Columns - 1 :
1413#endif
1414 0;
1415 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001416 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
1418 msg_putchar('\n');
1419 did_return = TRUE;
1420 if (exmode_active != EXMODE_NORMAL)
1421 cmdline_row = msg_row;
1422 }
1423 if (!msg_didany || lines_left < 0)
1424 msg_starthere();
1425 if (msg_silent == 0)
1426 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001427 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 cursor_off();
1429 }
1430
Bram Moolenaar85a20022019-12-21 18:25:54 +01001431 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (!did_return)
1433 redir_write((char_u *)"\n", -1);
1434}
1435
1436/*
1437 * Note that the current msg position is where messages start.
1438 */
1439 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001440msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 lines_left = cmdline_row;
1443 msg_didany = FALSE;
1444}
1445
1446 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001447msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 msg_putchar_attr(c, 0);
1450}
1451
1452 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001453msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001455 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 if (IS_SPECIAL(c))
1458 {
1459 buf[0] = K_SPECIAL;
1460 buf[1] = K_SECOND(c);
1461 buf[2] = K_THIRD(c);
1462 buf[3] = NUL;
1463 }
1464 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001465 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001466 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467}
1468
1469 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001470msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001472 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar32526b32019-01-19 17:43:09 +01001474 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 msg_puts(buf);
1476}
1477
1478 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001479msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 msg_home_replace_attr(fname, 0);
1482}
1483
1484#if defined(FEAT_FIND_ID) || defined(PROTO)
1485 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001486msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001488 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489}
1490#endif
1491
1492 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001493msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 char_u *name;
1496
1497 name = home_replace_save(NULL, fname);
1498 if (name != NULL)
1499 msg_outtrans_attr(name, attr);
1500 vim_free(name);
1501}
1502
1503/*
1504 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001505 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 * Use attributes 'attr'.
1507 * Return the number of characters it takes on the screen.
1508 */
1509 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001510msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 return msg_outtrans_attr(str, 0);
1513}
1514
1515 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001516msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1519}
1520
1521 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001522msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 return msg_outtrans_len_attr(str, len, 0);
1525}
1526
1527/*
1528 * Output one character at "p". Return pointer to the next character.
1529 * Handles multi-byte characters.
1530 */
1531 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001532msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 int l;
1535
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001536 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 msg_outtrans_len_attr(p, l, attr);
1539 return p + l;
1540 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001541 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return p + 1;
1543}
1544
1545 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001546msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 int retval = 0;
1549 char_u *str = msgstr;
1550 char_u *plain_start = msgstr;
1551 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 int mb_l;
1553 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001554 int save_got_int = got_int;
1555
1556 // Only quit when got_int was set in here.
1557 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar85a20022019-12-21 18:25:54 +01001559 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (attr & MSG_HIST)
1561 {
1562 add_msg_hist(str, len, attr);
1563 attr &= ~MSG_HIST;
1564 }
1565
Bram Moolenaar85a20022019-12-21 18:25:54 +01001566 // If the string starts with a composing character first draw a space on
1567 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001569 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571 /*
1572 * Go over the string. Special characters are translated and printed.
1573 * Normal characters are printed several at a time.
1574 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001575 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001578 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001579 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001581 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 else
1583 mb_l = 1;
1584 if (has_mbyte && mb_l > 1)
1585 {
1586 c = (*mb_ptr2char)(str);
1587 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001588 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 retval += (*mb_ptr2cells)(str);
1590 else
1591 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001592 // unprintable multi-byte char: print the printable chars so
1593 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001595 msg_puts_attr_len((char *)plain_start,
1596 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001598 msg_puts_attr((char *)transchar(c),
1599 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 retval += char2cells(c);
1601 }
1602 len -= mb_l - 1;
1603 str += mb_l;
1604 }
1605 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607 s = transchar_byte(*str);
1608 if (s[1] != NUL)
1609 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001610 // unprintable char: print the printable chars so far and the
1611 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001613 msg_puts_attr_len((char *)plain_start,
1614 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001616 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001617 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001619 else
1620 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 ++str;
1622 }
1623 }
1624
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001625 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001626 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001627 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001629 got_int |= save_got_int;
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 return retval;
1632}
1633
1634#if defined(FEAT_QUICKFIX) || defined(PROTO)
1635 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001636msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 int i;
1639 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1640
1641 arg = skipwhite(arg);
1642 for (i = 5; *arg && i >= 0; --i)
1643 if (*arg++ != str[i])
1644 break;
1645 if (i < 0)
1646 {
1647 msg_putchar('\n');
1648 for (i = 0; rs[i]; ++i)
1649 msg_putchar(rs[i] - 3);
1650 }
1651}
1652#endif
1653
1654/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001655 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 * Return the number of characters it takes on the screen.
1657 *
1658 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1659 * following character and shown as <F1>, <S-Up> etc. Any other character
1660 * which is not printable shown in <> form.
1661 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1662 * If a character is displayed in one of these special ways, is also
1663 * highlighted (its highlight name is '8' in the p_hl variable).
1664 * Otherwise characters are not highlighted.
1665 * This function is used to show mappings, where we want to see how to type
1666 * the character/string -- webb
1667 */
1668 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001669msg_outtrans_special(
1670 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001671 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001672 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 char_u *str = strstart;
1675 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001676 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 int attr;
1678 int len;
1679
Bram Moolenaar8820b482017-03-16 17:23:31 +01001680 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 while (*str != NUL)
1682 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001683 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if ((str == strstart || str[1] == NUL) && *str == ' ')
1685 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001686 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 ++str;
1688 }
1689 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001690 text = (char *)str2special(&str, from);
1691 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001692 if (maxlen > 0 && retval + len >= maxlen)
1693 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001694 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001695 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001696 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 retval += len;
1698 }
1699 return retval;
1700}
1701
Bram Moolenaarbd743252010-10-20 21:23:33 +02001702#if defined(FEAT_EVAL) || defined(PROTO)
1703/*
1704 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1705 * strings, in an allocated string.
1706 */
1707 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001708str2special_save(
1709 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001710 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001711{
1712 garray_T ga;
1713 char_u *p = str;
1714
1715 ga_init2(&ga, 1, 40);
1716 while (*p != NUL)
1717 ga_concat(&ga, str2special(&p, is_lhs));
1718 ga_append(&ga, NUL);
1719 return (char_u *)ga.ga_data;
1720}
1721#endif
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723/*
1724 * Return the printable string for the key codes at "*sp".
1725 * Used for translating the lhs or rhs of a mapping to printable chars.
1726 * Advances "sp" to the next code.
1727 */
1728 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001729str2special(
1730 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001731 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 int c;
1734 static char_u buf[7];
1735 char_u *str = *sp;
1736 int modifiers = 0;
1737 int special = FALSE;
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (has_mbyte)
1740 {
1741 char_u *p;
1742
Bram Moolenaar85a20022019-12-21 18:25:54 +01001743 // Try to un-escape a multi-byte character. Return the un-escaped
1744 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 p = mb_unescape(sp);
1746 if (p != NULL)
1747 return p;
1748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750 c = *str;
1751 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1752 {
1753 if (str[1] == KS_MODIFIER)
1754 {
1755 modifiers = str[2];
1756 str += 3;
1757 c = *str;
1758 }
1759 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1760 {
1761 c = TO_SPECIAL(str[1], str[2]);
1762 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001764 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 special = TRUE;
1766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001768 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001770 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001771
Bram Moolenaar85a20022019-12-21 18:25:54 +01001772 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001773 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1774 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001775 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001776 *sp = str + 1;
1777 return buf;
1778 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 // Since 'special' is TRUE the multi-byte character 'c' will be
1780 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001781 c = (*mb_ptr2char)(str);
1782 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001784 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001785 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar85a20022019-12-21 18:25:54 +01001787 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1788 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (special || char2cells(c) > 1 || (from && c == ' '))
1790 return get_special_key_name(c, modifiers);
1791 buf[0] = c;
1792 buf[1] = NUL;
1793 return buf;
1794}
1795
1796/*
1797 * Translate a key sequence into special key names.
1798 */
1799 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001800str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 char_u *s;
1803
1804 *buf = NUL;
1805 while (*sp)
1806 {
1807 s = str2special(&sp, FALSE);
1808 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1809 STRCAT(buf, s);
1810 }
1811}
1812
1813/*
1814 * print line for :print or :list command
1815 */
1816 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001817msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 int c;
1820 int col = 0;
1821 int n_extra = 0;
1822 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001823 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001824 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001826 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 int l;
1829 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
Bram Moolenaardf177f62005-02-22 08:39:57 +00001831 if (curwin->w_p_list)
1832 list = TRUE;
1833
Bram Moolenaar85a20022019-12-21 18:25:54 +01001834 // find start of trailing whitespace
Bram Moolenaardf177f62005-02-22 08:39:57 +00001835 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001838 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 --trail;
1840 }
1841
Bram Moolenaar85a20022019-12-21 18:25:54 +01001842 // output a space for an empty line, otherwise the line will be
1843 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001844 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 msg_putchar(' ');
1846
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001847 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001849 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001852 if (n_extra == 0 && c_final)
1853 c = c_final;
1854 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 c = c_extra;
1856 else
1857 c = *p_extra++;
1858 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001859 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001862 if (l >= MB_MAXBYTES)
1863 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001864 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001865 }
1866 else if (lcs_nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001867 && (mb_ptr2char(s) == 160
1868 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001869 {
1870 mb_char2bytes(lcs_nbsp, buf);
1871 buf[(*mb_ptr2len)(buf)] = NUL;
1872 }
1873 else
1874 {
1875 mch_memmove(buf, s, (size_t)l);
1876 buf[l] = NUL;
1877 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001878 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 s += l;
1880 continue;
1881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 else
1883 {
1884 attr = 0;
1885 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001886 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001888 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001889#ifdef FEAT_VARTABS
1890 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1891 curbuf->b_p_vts_array) - 1;
1892#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001894#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001895 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 c = ' ';
1898 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001899 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 else
1902 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001903 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001905 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001906 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001909 else if (c == 160 && list && lcs_nbsp != NUL)
1910 {
1911 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001912 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001913 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001914 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
1916 p_extra = (char_u *)"";
1917 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001918 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 n_extra = 1;
1920 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001921 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 --s;
1923 }
1924 else if (c != NUL && (n = byte2cells(c)) > 1)
1925 {
1926 n_extra = n - 1;
1927 p_extra = transchar_byte(c);
1928 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001929 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001931 // Use special coloring to be able to distinguish <hex> from
1932 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001933 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
1935 else if (c == ' ' && trail != NULL && s > trail)
1936 {
1937 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001938 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001940 else if (c == ' ' && list && lcs_space != NUL)
1941 {
1942 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001943 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
1946
1947 if (c == NUL)
1948 break;
1949
1950 msg_putchar_attr(c, attr);
1951 col++;
1952 }
1953 msg_clr_eos();
1954}
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956/*
1957 * Use screen_puts() to output one multi-byte character.
1958 * Return the pointer "s" advanced to the next character.
1959 */
1960 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001961screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 int cw;
1964
Bram Moolenaar85a20022019-12-21 18:25:54 +01001965 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 cw = (*mb_ptr2cells)(s);
1967 if (cw > 1 && (
1968#ifdef FEAT_RIGHTLEFT
1969 cmdmsg_rl ? msg_col <= 1 :
1970#endif
1971 msg_col == Columns - 1))
1972 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001973 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001974 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 return s;
1976 }
1977
1978 screen_puts_len(s, l, msg_row, msg_col, attr);
1979#ifdef FEAT_RIGHTLEFT
1980 if (cmdmsg_rl)
1981 {
1982 msg_col -= cw;
1983 if (msg_col == 0)
1984 {
1985 msg_col = Columns;
1986 ++msg_row;
1987 }
1988 }
1989 else
1990#endif
1991 {
1992 msg_col += cw;
1993 if (msg_col >= Columns)
1994 {
1995 msg_col = 0;
1996 ++msg_row;
1997 }
1998 }
1999 return s + l;
2000}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002/*
2003 * Output a string to the screen at position msg_row, msg_col.
2004 * Update msg_row and msg_col for the next message.
2005 */
2006 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002007msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002009 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010}
2011
2012 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002013msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002015 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016}
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018/*
2019 * Show a message in such a way that it always fits in the line. Cut out a
2020 * part in the middle and replace it with "..." when necessary.
2021 * Does not handle multi-byte characters!
2022 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002023 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002024msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
2026 int slen = len;
2027 int room;
2028
2029 room = Columns - msg_col;
2030 if (len > room && room >= 20)
2031 {
2032 slen = (room - 3) / 2;
2033 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002034 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2037}
2038
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002039 void
2040msg_outtrans_long_attr(char_u *longstr, int attr)
2041{
2042 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2043}
2044
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045/*
2046 * Basic function for writing a message with highlight attributes.
2047 */
2048 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002049msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 msg_puts_attr_len(s, -1, attr);
2052}
2053
2054/*
2055 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2056 * When "maxlen" is -1 there is no maximum length.
2057 * When "maxlen" is >= 0 the message is not put in the history.
2058 */
2059 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002060msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 /*
2063 * If redirection is on, also write to the redirection file.
2064 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002065 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 /*
2068 * Don't print anything when using ":silent cmd".
2069 */
2070 if (msg_silent != 0)
2071 return;
2072
Bram Moolenaar85a20022019-12-21 18:25:54 +01002073 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if ((attr & MSG_HIST) && maxlen < 0)
2075 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002076 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 attr &= ~MSG_HIST;
2078 }
2079
Bram Moolenaare8070982019-10-12 17:07:06 +02002080 // When writing something to the screen after it has scrolled, requires a
2081 // wait-return prompt later. Needed when scrolling, resetting
2082 // need_wait_return after some prompt, and then outputting something
2083 // without scrolling
2084 // Not needed when only using CR to move the cursor.
2085 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002087 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 /*
2090 * If there is no valid screen, use fprintf so we can see error messages.
2091 * If termcap is not active, we may be writing in an alternate console
2092 * window, cursor positioning may not work correctly (window size may be
2093 * different, e.g. for Win32 console) or we just don't know where the
2094 * cursor is.
2095 */
2096 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002097 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002098 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002099 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002102/*
2103 * The display part of msg_puts_attr_len().
2104 * May be called recursively to display scroll-back text.
2105 */
2106 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002107msg_puts_display(
2108 char_u *str,
2109 int maxlen,
2110 int attr,
2111 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002112{
2113 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002114 char_u *t_s = str; // string from "t_s" to "s" is still todo
2115 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002116 int l;
2117 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002118 char_u *sb_str = str;
2119 int sb_col = msg_col;
2120 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002121 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002124 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002127 * We are at the end of the screen line when:
2128 * - When outputting a newline.
2129 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002131 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#ifdef FEAT_RIGHTLEFT
2133 cmdmsg_rl
2134 ? (
2135 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002136 || (*s == TAB && msg_col <= 7)
2137 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 :
2139#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002140 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002143 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002145 /*
2146 * The screen is scrolled up when at the last row (some terminals
2147 * scroll automatically, some don't. To avoid problems we scroll
2148 * ourselves).
2149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002152 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
Bram Moolenaar85a20022019-12-21 18:25:54 +01002154 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (msg_no_more && lines_left == 0)
2156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar85a20022019-12-21 18:25:54 +01002158 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002159 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002162 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 msg_col = Columns - 1;
2164
Bram Moolenaar85a20022019-12-21 18:25:54 +01002165 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002166 if (*s >= ' '
2167#ifdef FEAT_RIGHTLEFT
2168 && !cmdmsg_rl
2169#endif
2170 )
2171 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002172 if (has_mbyte)
2173 {
2174 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002175 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002176 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002177 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002178 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002179 s = screen_puts_mbyte(s, l, attr);
2180 }
2181 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002182 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002183 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002185 else
2186 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002187
2188 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002189 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002190 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2191
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002192 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002193 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 redraw_cmdline = TRUE;
2195 if (cmdline_row > 0 && !exmode_active)
2196 --cmdline_row;
2197
2198 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002199 * If screen is completely filled and 'more' is set then wait
2200 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002202 if (lines_left > 0)
2203 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002204 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 && !msg_no_more && !exmode_active)
2206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002208 if (do_more_prompt(NUL))
2209 s = confirm_msg_tail;
2210#else
2211 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212#endif
2213 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002214 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002216
Bram Moolenaar85a20022019-12-21 18:25:54 +01002217 // When we displayed a char in last column need to check if there
2218 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002219 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002220 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002223 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002226 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002227 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2228 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002230 t_puts(&t_col, t_s, s, attr);
2231
2232 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002233 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002234 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaar85a20022019-12-21 18:25:54 +01002236 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002238 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002239#ifdef FEAT_RIGHTLEFT
2240 if (cmdmsg_rl)
2241 msg_col = Columns - 1;
2242 else
2243#endif
2244 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002245 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 msg_row = Rows - 1;
2247 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002248 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 msg_col = 0;
2251 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002252 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 if (msg_col)
2255 --msg_col;
2256 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002257 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 do
2260 msg_screen_putchar(' ', attr);
2261 while (msg_col & 7);
2262 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002264 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 else
2266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (has_mbyte)
2268 {
2269 cw = (*mb_ptr2cells)(s);
2270 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002271 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002272 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002274 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 else
2277 {
2278 cw = 1;
2279 l = 1;
2280 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002281
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 // When drawing from right to left or when a double-wide character
2283 // doesn't fit, draw a single character here. Otherwise collect
2284 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (
2286# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002287 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002289 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (l > 1)
2292 s = screen_puts_mbyte(s, l, attr) - 1;
2293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 msg_screen_putchar(*s, attr);
2295 }
2296 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002298 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (t_col == 0)
2300 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 t_col += cw;
2302 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 }
2305 ++s;
2306 }
2307
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002310 t_puts(&t_col, t_s, s, attr);
2311 if (p_more && !recurse)
2312 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
2314 msg_check();
2315}
2316
2317/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002318 * Return TRUE when ":filter pattern" was used and "msg" does not match
2319 * "pattern".
2320 */
2321 int
2322message_filtered(char_u *msg)
2323{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002324 int match;
2325
Bram Moolenaare1004402020-10-24 20:49:43 +02002326 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002327 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002328 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2329 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002330}
2331
2332/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002333 * Scroll the screen up one line for displaying the next message line.
2334 */
2335 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002336msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002337{
2338#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002339 // Remove the cursor before scrolling, ScreenLines[] is going
2340 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002341 if (gui.in_use)
2342 gui_undraw_cursor();
2343#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002344 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002345 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002346 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002347 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002348
2349 if (!can_clear((char_u *)" "))
2350 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002351 // Scrolling up doesn't result in the right background. Set the
2352 // background here. It's not efficient, but avoids that we have to do
2353 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002354 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2355
Bram Moolenaar85a20022019-12-21 18:25:54 +01002356 // Also clear the last char of the last but one line if it was not
2357 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002358 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2359 screen_fill((int)Rows - 2, (int)Rows - 1,
2360 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2361 }
2362}
2363
2364/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002365 * Increment "msg_scrolled".
2366 */
2367 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002368inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002369{
2370#ifdef FEAT_EVAL
2371 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2372 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002373 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002374 char_u *tofree = NULL;
2375 int len;
2376
Bram Moolenaar85a20022019-12-21 18:25:54 +01002377 // v:scrollstart is empty, set it to the script/function name and line
2378 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002379 if (p == NULL)
2380 p = (char_u *)_("Unknown");
2381 else
2382 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002383 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002384 tofree = alloc(len);
2385 if (tofree != NULL)
2386 {
2387 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002388 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002389 p = tofree;
2390 }
2391 }
2392 set_vim_var_string(VV_SCROLLSTART, p, -1);
2393 vim_free(tofree);
2394 }
2395#endif
2396 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002397 if (must_redraw < VALID)
2398 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002399}
2400
2401/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002402 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2403 * store the displayed text and remember where screen lines start.
2404 */
2405typedef struct msgchunk_S msgchunk_T;
2406struct msgchunk_S
2407{
2408 msgchunk_T *sb_next;
2409 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002410 char sb_eol; // TRUE when line ends after this text
2411 int sb_msg_col; // column in which text starts
2412 int sb_attr; // text attributes
2413 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002414};
2415
Bram Moolenaar85a20022019-12-21 18:25:54 +01002416static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002417
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002418static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002419
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002420typedef enum {
2421 SB_CLEAR_NONE = 0,
2422 SB_CLEAR_ALL,
2423 SB_CLEAR_CMDLINE_BUSY,
2424 SB_CLEAR_CMDLINE_DONE
2425} sb_clear_T;
2426
Bram Moolenaar85a20022019-12-21 18:25:54 +01002427// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002428static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002429
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002430/*
2431 * Store part of a printed message for displaying when scrolling back.
2432 */
2433 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002434store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002435 char_u **sb_str, // start of string
2436 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002437 int attr,
2438 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002440{
2441 msgchunk_T *mp;
2442
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002443 if (do_clear_sb_text == SB_CLEAR_ALL
2444 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002445 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002446 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2447 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002448 }
2449
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002450 if (s > *sb_str)
2451 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002452 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002453 if (mp != NULL)
2454 {
2455 mp->sb_eol = finish;
2456 mp->sb_msg_col = *sb_col;
2457 mp->sb_attr = attr;
2458 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2459
2460 if (last_msgchunk == NULL)
2461 {
2462 last_msgchunk = mp;
2463 mp->sb_prev = NULL;
2464 }
2465 else
2466 {
2467 mp->sb_prev = last_msgchunk;
2468 last_msgchunk->sb_next = mp;
2469 last_msgchunk = mp;
2470 }
2471 mp->sb_next = NULL;
2472 }
2473 }
2474 else if (finish && last_msgchunk != NULL)
2475 last_msgchunk->sb_eol = TRUE;
2476
2477 *sb_str = s;
2478 *sb_col = 0;
2479}
2480
2481/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002482 * Finished showing messages, clear the scroll-back text on the next message.
2483 */
2484 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002485may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002486{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002487 do_clear_sb_text = SB_CLEAR_ALL;
2488}
2489
2490/*
2491 * Starting to edit the command line, do not clear messages now.
2492 */
2493 void
2494sb_text_start_cmdline(void)
2495{
2496 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2497 msg_sb_eol();
2498}
2499
2500/*
2501 * Ending to edit the command line. Clear old lines but the last one later.
2502 */
2503 void
2504sb_text_end_cmdline(void)
2505{
2506 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002507}
2508
2509/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002511 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002512 * Called when redrawing the screen.
2513 */
2514 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002515clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002516{
2517 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002518 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002520 if (all)
2521 lastp = &last_msgchunk;
2522 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002523 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002524 if (last_msgchunk == NULL)
2525 return;
2526 lastp = &last_msgchunk->sb_prev;
2527 }
2528
2529 while (*lastp != NULL)
2530 {
2531 mp = (*lastp)->sb_prev;
2532 vim_free(*lastp);
2533 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002534 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002535}
2536
2537/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002538 * "g<" command.
2539 */
2540 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002541show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002542{
2543 msgchunk_T *mp;
2544
Bram Moolenaar85a20022019-12-21 18:25:54 +01002545 // Only show something if there is more than one line, otherwise it looks
2546 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002547 mp = msg_sb_start(last_msgchunk);
2548 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002549 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002550 else
2551 {
2552 do_more_prompt('G');
2553 wait_return(FALSE);
2554 }
2555}
2556
2557/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002558 * Move to the start of screen line in already displayed text.
2559 */
2560 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002561msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002562{
2563 msgchunk_T *mp = mps;
2564
2565 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2566 mp = mp->sb_prev;
2567 return mp;
2568}
2569
2570/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002571 * Mark the last message chunk as finishing the line.
2572 */
2573 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002574msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002575{
2576 if (last_msgchunk != NULL)
2577 last_msgchunk->sb_eol = TRUE;
2578}
2579
2580/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002581 * Display a screen line from previously displayed text at row "row".
2582 * Returns a pointer to the text for the next line (can be NULL).
2583 */
2584 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002585disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002586{
2587 msgchunk_T *mp = smp;
2588 char_u *p;
2589
2590 for (;;)
2591 {
2592 msg_row = row;
2593 msg_col = mp->sb_msg_col;
2594 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002595 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596 ++p;
2597 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2598 if (mp->sb_eol || mp->sb_next == NULL)
2599 break;
2600 mp = mp->sb_next;
2601 }
2602 return mp->sb_next;
2603}
2604
2605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 * Output any postponed text for msg_puts_attr_len().
2607 */
2608 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002609t_puts(
2610 int *t_col,
2611 char_u *t_s,
2612 char_u *s,
2613 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002615 // output postponed text
2616 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618 msg_col += *t_col;
2619 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002620 // If the string starts with a composing character don't increment the
2621 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2623 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (msg_col >= Columns)
2625 {
2626 msg_col = 0;
2627 ++msg_row;
2628 }
2629}
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631/*
2632 * Returns TRUE when messages should be printed with mch_errmsg().
2633 * This is used when there is no valid screen, so we can see error messages.
2634 * If termcap is not active, we may be writing in an alternate console
2635 * window, cursor positioning may not work correctly (window size may be
2636 * different, e.g. for Win32 console) or we just don't know where the
2637 * cursor is.
2638 */
2639 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002640msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
2642 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002643#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2644# ifdef VIMDLL
2645 || (!gui.in_use && !termcap_active)
2646# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#endif
2650 || (swapping_screen() && !termcap_active)
2651 );
2652}
2653
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654/*
2655 * Print a message when there is no valid screen.
2656 */
2657 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002658msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002659{
2660 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002661 char_u *buf = NULL;
2662 char_u *p = s;
2663
Bram Moolenaar4f974752019-02-17 17:44:42 +01002664#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002665 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002666 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002667#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002668 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002669 {
2670 if (!(silent_mode && p_verbose == 0))
2671 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002672 // NL --> CR NL translation (for Unix, not for "--version")
2673 if (*s == NL)
2674 {
2675 int n = (int)(s - p);
2676
2677 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002678 if (buf != NULL)
2679 {
2680 memcpy(buf, p, n);
2681 if (!info_message)
2682 buf[n++] = CAR;
2683 buf[n++] = NL;
2684 buf[n++] = NUL;
2685 if (info_message) // informative message, not an error
2686 mch_msg((char *)buf);
2687 else
2688 mch_errmsg((char *)buf);
2689 vim_free(buf);
2690 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002691 p = s + 1;
2692 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693 }
2694
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002695 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002696#ifdef FEAT_RIGHTLEFT
2697 if (cmdmsg_rl)
2698 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002699 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002700 msg_col = Columns - 1;
2701 else
2702 --msg_col;
2703 }
2704 else
2705#endif
2706 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002707 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002708 msg_col = 0;
2709 else
2710 ++msg_col;
2711 }
2712 ++s;
2713 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002714
2715 if (*p != NUL && !(silent_mode && p_verbose == 0))
2716 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002717 int c = -1;
2718
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002719 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002720 {
2721 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002722 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002723 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002724 if (info_message)
2725 mch_msg((char *)p);
2726 else
2727 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002728 if (c != -1)
2729 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002730 }
2731
2732 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733
Bram Moolenaar4f974752019-02-17 17:44:42 +01002734#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002735 if (!(silent_mode && p_verbose == 0))
2736 mch_settmode(TMODE_RAW);
2737#endif
2738}
2739
2740/*
2741 * Show the more-prompt and handle the user response.
2742 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002743 * When at hit-enter prompt "typed_char" is the already typed character,
2744 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002745 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2746 */
2747 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002748do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002749{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002750 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002751 int used_typed_char = typed_char;
2752 int oldState = State;
2753 int c;
2754#ifdef FEAT_CON_DIALOG
2755 int retval = FALSE;
2756#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002757 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002758 msgchunk_T *mp_last = NULL;
2759 msgchunk_T *mp;
2760 int i;
2761
Bram Moolenaar85a20022019-12-21 18:25:54 +01002762 // We get called recursively when a timer callback outputs a message. In
2763 // that case don't show another prompt. Also when at the hit-Enter prompt
2764 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002765 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002766 return FALSE;
2767 entered = TRUE;
2768
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002769 if (typed_char == 'G')
2770 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002771 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002772 mp_last = msg_sb_start(last_msgchunk);
2773 for (i = 0; i < Rows - 2 && mp_last != NULL
2774 && mp_last->sb_prev != NULL; ++i)
2775 mp_last = msg_sb_start(mp_last->sb_prev);
2776 }
2777
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002778 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002779 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002780 if (typed_char == NUL)
2781 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782 for (;;)
2783 {
2784 /*
2785 * Get a typed character directly from the user.
2786 */
2787 if (used_typed_char != NUL)
2788 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002789 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002790 used_typed_char = NUL;
2791 }
2792 else
2793 c = get_keystroke();
2794
2795#if defined(FEAT_MENU) && defined(FEAT_GUI)
2796 if (c == K_MENU)
2797 {
2798 int idx = get_menu_index(current_menu, ASKMORE);
2799
Bram Moolenaar85a20022019-12-21 18:25:54 +01002800 // Used a menu. If it starts with CTRL-Y, it must
2801 // be a "Copy" for the clipboard. Otherwise
2802 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002803 if (idx == MENU_INDEX_INVALID)
2804 continue;
2805 c = *current_menu->strings[idx];
2806 if (c != NUL && current_menu->strings[idx][1] != NUL)
2807 ins_typebuf(current_menu->strings[idx] + 1,
2808 current_menu->noremap[idx], 0, TRUE,
2809 current_menu->silent[idx]);
2810 }
2811#endif
2812
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002813 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002814 switch (c)
2815 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002816 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 case K_BS:
2818 case 'k':
2819 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002820 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821 break;
2822
Bram Moolenaar85a20022019-12-21 18:25:54 +01002823 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002824 case NL:
2825 case 'j':
2826 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002827 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002828 break;
2829
Bram Moolenaar85a20022019-12-21 18:25:54 +01002830 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002831 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002832 break;
2833
Bram Moolenaar85a20022019-12-21 18:25:54 +01002834 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002835 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002836 break;
2837
Bram Moolenaar85a20022019-12-21 18:25:54 +01002838 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002839 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002840 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002841 break;
2842
Bram Moolenaar85a20022019-12-21 18:25:54 +01002843 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002844 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002845 case K_PAGEDOWN:
2846 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002847 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002848 break;
2849
Bram Moolenaar85a20022019-12-21 18:25:54 +01002850 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002851 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002852 break;
2853
Bram Moolenaar85a20022019-12-21 18:25:54 +01002854 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002855 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002856 lines_left = 999999;
2857 break;
2858
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002860#ifdef FEAT_CON_DIALOG
2861 if (!confirm_msg_used)
2862#endif
2863 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002864 // Since got_int is set all typeahead will be flushed, but we
2865 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002867#ifdef FEAT_TERMINAL
2868 skip_term_loop = TRUE;
2869#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002870 cmdline_row = Rows - 1; // put ':' on this line
2871 skip_redraw = TRUE; // skip redraw once
2872 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002874 // FALLTHROUGH
2875 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002876 case Ctrl_C:
2877 case ESC:
2878#ifdef FEAT_CON_DIALOG
2879 if (confirm_msg_used)
2880 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002881 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 }
2884 else
2885#endif
2886 {
2887 got_int = TRUE;
2888 quit_more = TRUE;
2889 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002890 // When there is some more output (wrapping line) display that
2891 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002892 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002893 break;
2894
2895#ifdef FEAT_CLIPBOARD
2896 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002897 // Strange way to allow copying (yanking) a modeless
2898 // selection at the more prompt. Use CTRL-Y,
2899 // because the same is used in Cmdline-mode and at the
2900 // hit-enter prompt. However, scrolling one line up
2901 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002902 if (clip_star.state == SELECT_DONE)
2903 clip_copy_modeless_selection(TRUE);
2904 continue;
2905#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002906 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002907 msg_moremsg(TRUE);
2908 continue;
2909 }
2910
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002911 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002912 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002913 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002916 if (mp_last == NULL)
2917 mp = msg_sb_start(last_msgchunk);
2918 else if (mp_last->sb_prev != NULL)
2919 mp = msg_sb_start(mp_last->sb_prev);
2920 else
2921 mp = NULL;
2922
Bram Moolenaar85a20022019-12-21 18:25:54 +01002923 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002924 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2925 ++i)
2926 mp = msg_sb_start(mp->sb_prev);
2927
2928 if (mp != NULL && mp->sb_prev != NULL)
2929 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002930 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002931 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002932 {
2933 if (mp == NULL || mp->sb_prev == NULL)
2934 break;
2935 mp = msg_sb_start(mp->sb_prev);
2936 if (mp_last == NULL)
2937 mp_last = msg_sb_start(last_msgchunk);
2938 else
2939 mp_last = msg_sb_start(mp_last->sb_prev);
2940 }
2941
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002942 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002943 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002945 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002946 (void)disp_sb_line(0, mp);
2947 }
2948 else
2949 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002950 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002951 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002952 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2953 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002955 ++msg_scrolled;
2956 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002958 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002959 }
2960 }
2961 else
2962 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002963 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002964 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002965 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002966 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002967 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002968 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002969 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2970 (int)Columns, ' ', ' ', 0);
2971 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002972 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002973 }
2974 }
2975
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002976 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002977 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002978 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002979 screen_fill((int)Rows - 1, (int)Rows, 0,
2980 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002981 msg_moremsg(FALSE);
2982 continue;
2983 }
2984
Bram Moolenaar85a20022019-12-21 18:25:54 +01002985 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002986 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002987 }
2988
2989 break;
2990 }
2991
Bram Moolenaar85a20022019-12-21 18:25:54 +01002992 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002993 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2994 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002995 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002996 if (quit_more)
2997 {
2998 msg_row = Rows - 1;
2999 msg_col = 0;
3000 }
3001#ifdef FEAT_RIGHTLEFT
3002 else if (cmdmsg_rl)
3003 msg_col = Columns - 1;
3004#endif
3005
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003006 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003007#ifdef FEAT_CON_DIALOG
3008 return retval;
3009#else
3010 return FALSE;
3011#endif
3012}
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3015
3016#ifdef mch_errmsg
3017# undef mch_errmsg
3018#endif
3019#ifdef mch_msg
3020# undef mch_msg
3021#endif
3022
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003023#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3024 static void
3025mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003027 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003028 DWORD nwrite = 0;
3029 DWORD mode = 0;
3030 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3031
3032 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3033 && (int)GetConsoleCP() != enc_codepage)
3034 {
3035 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3036
3037 WriteConsoleW(h, w, len, &nwrite, NULL);
3038 vim_free(w);
3039 }
3040 else
3041 {
3042 fprintf(stderr, "%s", str);
3043 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003044}
3045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003047/*
3048 * Give an error message. To be used when the screen hasn't been initialized
3049 * yet. When stderr can't be used, collect error messages until the GUI has
3050 * started and they can be displayed in a message box.
3051 */
3052 void
3053mch_errmsg(char *str)
3054{
3055#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3056 int len;
3057#endif
3058
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003059#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003060 // On Unix use stderr if it's a tty.
3061 // When not going to start the GUI also use stderr.
3062 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003064# ifdef UNIX
3065# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003067# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 isatty(2)
3069# endif
3070# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003071 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003072# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003073# endif
3074# ifdef FEAT_GUI
3075 !(gui.in_use || gui.starting)
3076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 )
3078 {
3079 fprintf(stderr, "%s", str);
3080 return;
3081 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003084#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3085# ifdef VIMDLL
3086 if (!(gui.in_use || gui.starting))
3087# endif
3088 {
3089 mch_errmsg_c(str);
3090 return;
3091 }
3092#endif
3093
3094#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003095 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 emsg_on_display = FALSE;
3097
3098 len = (int)STRLEN(str) + 1;
3099 if (error_ga.ga_growsize == 0)
3100 {
3101 error_ga.ga_growsize = 80;
3102 error_ga.ga_itemsize = 1;
3103 }
3104 if (ga_grow(&error_ga, len) == OK)
3105 {
3106 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3107 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003108# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003109 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 char_u *p;
3112
3113 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3114 for (;;)
3115 {
3116 p = vim_strchr(p, '\r');
3117 if (p == NULL)
3118 break;
3119 *p = ' ';
3120 }
3121 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003122# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003123 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127}
3128
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003129#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3130 static void
3131mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003133 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003134 DWORD nwrite = 0;
3135 DWORD mode;
3136 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3137
3138
3139 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3140 && (int)GetConsoleCP() != enc_codepage)
3141 {
3142 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3143
3144 WriteConsoleW(h, w, len, &nwrite, NULL);
3145 vim_free(w);
3146 }
3147 else
3148 {
3149 printf("%s", str);
3150 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003151}
3152#endif
3153
3154/*
3155 * Give a message. To be used when the screen hasn't been initialized yet.
3156 * When there is no tty, collect messages until the GUI has started and they
3157 * can be displayed in a message box.
3158 */
3159 void
3160mch_msg(char *str)
3161{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003162#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003163 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3164 // uses mch_errmsg() when started from the desktop.
3165 // When not going to start the GUI also use stdout.
3166 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003168# ifdef UNIX
3169# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003171# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003175 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003177# endif
3178# ifdef FEAT_GUI
3179 !(gui.in_use || gui.starting)
3180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 )
3182 {
3183 printf("%s", str);
3184 return;
3185 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003186#endif
3187
3188#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3189# ifdef VIMDLL
3190 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003192 {
3193 mch_msg_c(str);
3194 return;
3195 }
3196#endif
3197#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003201#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203/*
3204 * Put a character on the screen at the current message position and advance
3205 * to the next position. Only for printable ASCII!
3206 */
3207 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003208msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003210 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 screen_putchar(c, msg_row, msg_col, attr);
3212#ifdef FEAT_RIGHTLEFT
3213 if (cmdmsg_rl)
3214 {
3215 if (--msg_col == 0)
3216 {
3217 msg_col = Columns;
3218 ++msg_row;
3219 }
3220 }
3221 else
3222#endif
3223 {
3224 if (++msg_col >= Columns)
3225 {
3226 msg_col = 0;
3227 ++msg_row;
3228 }
3229 }
3230}
3231
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003232 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003235 int attr;
3236 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
Bram Moolenaar8820b482017-03-16 17:23:31 +01003238 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003239 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003241 screen_puts((char_u *)
3242 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3243 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246/*
3247 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3248 * exmode_active.
3249 */
3250 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003251repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
3253 if (State == ASKMORE)
3254 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003255 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 msg_row = Rows - 1;
3257 }
3258#ifdef FEAT_CON_DIALOG
3259 else if (State == CONFIRM)
3260 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003261 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 msg_row = Rows - 1;
3263 }
3264#endif
3265 else if (State == EXTERNCMD)
3266 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003267 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269 else if (State == HITRETURN || State == SETWSIZE)
3270 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003271 if (msg_row == Rows - 1)
3272 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003273 // Avoid drawing the "hit-enter" prompt below the previous one,
3274 // overwrite it. Esp. useful when regaining focus and a
3275 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003276 msg_didout = FALSE;
3277 msg_col = 0;
3278 msg_clr_eos();
3279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 hit_return_msg();
3281 msg_row = Rows - 1;
3282 }
3283}
3284
3285/*
3286 * msg_check_screen - check if the screen is initialized.
3287 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3288 * While starting the GUI the terminal codes will be set for the GUI, but the
3289 * output goes to the terminal. Don't use the terminal codes then.
3290 */
3291 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003292msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 if (!full_screen || !screen_valid(FALSE))
3295 return FALSE;
3296
3297 if (msg_row >= Rows)
3298 msg_row = Rows - 1;
3299 if (msg_col >= Columns)
3300 msg_col = Columns - 1;
3301 return TRUE;
3302}
3303
3304/*
3305 * Clear from current message position to end of screen.
3306 * Skip this when ":silent" was used, no need to clear for redirection.
3307 */
3308 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003309msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 if (msg_silent == 0)
3312 msg_clr_eos_force();
3313}
3314
3315/*
3316 * Clear from current message position to end of screen.
3317 * Note: msg_col is not updated, so we remember the end of the message
3318 * for msg_check().
3319 */
3320 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003321msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 if (msg_use_printf())
3324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003325 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
3327 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003328 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003330 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
3332 }
3333 else
3334 {
3335#ifdef FEAT_RIGHTLEFT
3336 if (cmdmsg_rl)
3337 {
3338 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3339 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3340 }
3341 else
3342#endif
3343 {
3344 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3345 ' ', ' ', 0);
3346 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3347 }
3348 }
3349}
3350
3351/*
3352 * Clear the command line.
3353 */
3354 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003355msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 msg_row = cmdline_row;
3358 msg_col = 0;
3359 msg_clr_eos_force();
3360}
3361
3362/*
3363 * end putting a message on the screen
3364 * call wait_return if the message does not fit in the available space
3365 * return TRUE if wait_return not called.
3366 */
3367 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003368msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003371 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 * or the ruler option is set and we run into it,
3373 * we have to redraw the window.
3374 * Do not do this if we are abandoning the file or editing the command line.
3375 */
3376 if (!exiting && need_wait_return && !(State & CMDLINE))
3377 {
3378 wait_return(FALSE);
3379 return FALSE;
3380 }
3381 out_flush();
3382 return TRUE;
3383}
3384
3385/*
3386 * If the written message runs into the shown command or ruler, we have to
3387 * wait for hit-return and redraw the window later.
3388 */
3389 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003390msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 if (msg_row == Rows - 1 && msg_col >= sc_col)
3393 {
3394 need_wait_return = TRUE;
3395 redraw_cmdline = TRUE;
3396 }
3397}
3398
3399/*
3400 * May write a string to the redirection file.
3401 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3402 */
3403 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003404redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
3406 char_u *s = str;
3407 static int cur_col = 0;
3408
Bram Moolenaar85a20022019-12-21 18:25:54 +01003409 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003410 if (redir_off)
3411 return;
3412
Bram Moolenaar85a20022019-12-21 18:25:54 +01003413 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003414 if (*p_vfile != NUL && verbose_fd == NULL)
3415 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003416
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003417 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003419 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (*s != '\n' && *s != '\r')
3421 {
3422 while (cur_col < msg_col)
3423 {
3424#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003425 if (redir_execute)
3426 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003427 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003429 else if (redir_vname)
3430 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003433 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003435 if (verbose_fd != NULL)
3436 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 ++cur_col;
3438 }
3439 }
3440
3441#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003442 if (redir_execute)
3443 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003444 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003446 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003447 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#endif
3449
Bram Moolenaar85a20022019-12-21 18:25:54 +01003450 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3452 {
3453#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003454 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003456 if (redir_fd != NULL)
3457 putc(*s, redir_fd);
3458 if (verbose_fd != NULL)
3459 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (*s == '\r' || *s == '\n')
3461 cur_col = 0;
3462 else if (*s == '\t')
3463 cur_col += (8 - cur_col % 8);
3464 else
3465 ++cur_col;
3466 ++s;
3467 }
3468
Bram Moolenaar85a20022019-12-21 18:25:54 +01003469 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 msg_col = cur_col;
3471 }
3472}
3473
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003474 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003475redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003476{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003477 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003478#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003479 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003480#endif
3481 ;
3482}
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003485 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003486 * Must always be called paired with verbose_leave()!
3487 */
3488 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003489verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003490{
3491 if (*p_vfile != NUL)
3492 ++msg_silent;
3493}
3494
3495/*
3496 * After giving verbose message.
3497 * Must always be called paired with verbose_enter()!
3498 */
3499 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003500verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003501{
3502 if (*p_vfile != NUL)
3503 if (--msg_silent < 0)
3504 msg_silent = 0;
3505}
3506
3507/*
3508 * Like verbose_enter() and set msg_scroll when displaying the message.
3509 */
3510 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003511verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003512{
3513 if (*p_vfile != NUL)
3514 ++msg_silent;
3515 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003516 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003517 msg_scroll = TRUE;
3518}
3519
3520/*
3521 * Like verbose_leave() and set cmdline_row when displaying the message.
3522 */
3523 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003524verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003525{
3526 if (*p_vfile != NUL)
3527 {
3528 if (--msg_silent < 0)
3529 msg_silent = 0;
3530 }
3531 else
3532 cmdline_row = msg_row;
3533}
3534
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003535/*
3536 * Called when 'verbosefile' is set: stop writing to the file.
3537 */
3538 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003539verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003540{
3541 if (verbose_fd != NULL)
3542 {
3543 fclose(verbose_fd);
3544 verbose_fd = NULL;
3545 }
3546 verbose_did_open = FALSE;
3547}
3548
3549/*
3550 * Open the file 'verbosefile'.
3551 * Return FAIL or OK.
3552 */
3553 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003554verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003555{
3556 if (verbose_fd == NULL && !verbose_did_open)
3557 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003558 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003559 verbose_did_open = TRUE;
3560
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003561 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003562 if (verbose_fd == NULL)
3563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003564 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003565 return FAIL;
3566 }
3567 }
3568 return OK;
3569}
3570
3571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 * Give a warning message (for searching).
3573 * Use 'w' highlighting and may repeat the message after redrawing
3574 */
3575 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003576give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003578 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (msg_silent != 0)
3580 return;
3581
Bram Moolenaar85a20022019-12-21 18:25:54 +01003582 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#ifdef FEAT_EVAL
3586 set_vim_var_string(VV_WARNINGMSG, message, -1);
3587#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003588 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003590 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 else
3592 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003593 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003594 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003595 msg_didout = FALSE; // overwrite this message
3596 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 --no_wait_return;
3600}
3601
Bram Moolenaar113e1072019-01-20 15:30:40 +01003602#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003603 void
3604give_warning2(char_u *message, char_u *a1, int hl)
3605{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003606 if (IObuff == NULL)
3607 {
3608 // Very early in initialisation and already something wrong, just give
3609 // the raw message so the user at least gets a hint.
3610 give_warning((char_u *)message, hl);
3611 }
3612 else
3613 {
3614 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3615 give_warning(IObuff, hl);
3616 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003617}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003618#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620/*
3621 * Advance msg cursor to column "col".
3622 */
3623 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003624msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003626 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003628 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 return;
3630 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003631 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003633#ifdef FEAT_RIGHTLEFT
3634 if (cmdmsg_rl)
3635 while (msg_col > Columns - col)
3636 msg_putchar(' ');
3637 else
3638#endif
3639 while (msg_col < col)
3640 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641}
3642
3643#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3644/*
3645 * Used for "confirm()" function, and the :confirm command prefix.
3646 * Versions which haven't got flexible dialogs yet, and console
3647 * versions, get this generic handler which uses the command line.
3648 *
3649 * type = one of:
3650 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3651 * title = title string (can be NULL for default)
3652 * (neither used in console dialogs at the moment)
3653 *
3654 * Format of the "buttons" string:
3655 * "Button1Name\nButton2Name\nButton3Name"
3656 * The first button should normally be the default/accept
3657 * The second button should be the 'Cancel' button
3658 * Other buttons- use your imagination!
3659 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3660 * different letter.
3661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003663do_dialog(
3664 int type UNUSED,
3665 char_u *title UNUSED,
3666 char_u *message,
3667 char_u *buttons,
3668 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003669 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3670 // otherwise
3671 int ex_cmd) // when TRUE pressing : accepts default and starts
3672 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 int oldState;
3675 int retval = 0;
3676 char_u *hotkeys;
3677 int c;
3678 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003679 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
3681#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003682 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003684 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#endif
3686
3687#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003688 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3690 {
3691 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003692 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003693 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003694 need_wait_return = FALSE;
3695 emsg_on_display = FALSE;
3696 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
Bram Moolenaar85a20022019-12-21 18:25:54 +01003698 // Flush output to avoid that further messages and redrawing is done
3699 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 out_flush();
3701 gui_mch_update();
3702
3703 return c;
3704 }
3705#endif
3706
3707 oldState = State;
3708 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaar27321db2020-07-06 21:24:57 +02003711 // Ensure raw mode here.
3712 save_tmode = cur_tmode;
3713 settmode(TMODE_RAW);
3714
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 /*
3716 * Since we wait for a keypress, don't make the
3717 * user press RETURN as well afterwards.
3718 */
3719 ++no_wait_return;
3720 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3721
3722 if (hotkeys != NULL)
3723 {
3724 for (;;)
3725 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003726 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 c = get_keystroke();
3728 switch (c)
3729 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003730 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 case NL:
3732 retval = dfltbutton;
3733 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003734 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 case ESC:
3736 retval = 0;
3737 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003738 default: // Could be a hotkey?
3739 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003741 if (c == ':' && ex_cmd)
3742 {
3743 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003744 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003745 break;
3746 }
3747
Bram Moolenaar85a20022019-12-21 18:25:54 +01003748 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 c = MB_TOLOWER(c);
3750 retval = 1;
3751 for (i = 0; hotkeys[i]; ++i)
3752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (has_mbyte)
3754 {
3755 if ((*mb_ptr2char)(hotkeys + i) == c)
3756 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003757 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (hotkeys[i] == c)
3761 break;
3762 ++retval;
3763 }
3764 if (hotkeys[i])
3765 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003766 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 continue;
3768 }
3769 break;
3770 }
3771
3772 vim_free(hotkeys);
3773 }
3774
Bram Moolenaar27321db2020-07-06 21:24:57 +02003775 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 --no_wait_return;
3779 msg_end_prompt();
3780
3781 return retval;
3782}
3783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784/*
3785 * Copy one character from "*from" to "*to", taking care of multi-byte
3786 * characters. Return the length of the character in bytes.
3787 */
3788 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003789copy_char(
3790 char_u *from,
3791 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003792 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 int len;
3795 int c;
3796
3797 if (has_mbyte)
3798 {
3799 if (lowercase)
3800 {
3801 c = MB_TOLOWER((*mb_ptr2char)(from));
3802 return (*mb_char2bytes)(c, to);
3803 }
3804 else
3805 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003806 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 mch_memmove(to, from, (size_t)len);
3808 return len;
3809 }
3810 }
3811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 if (lowercase)
3814 *to = (char_u)TOLOWER_LOC(*from);
3815 else
3816 *to = *from;
3817 return 1;
3818 }
3819}
3820
3821/*
3822 * Format the dialog string, and display it at the bottom of
3823 * the screen. Return a string of hotkey chars (if defined) for
3824 * each 'button'. If a button has no hotkey defined, the first character of
3825 * the button is used.
3826 * The hotkeys can be multi-byte characters, but without combining chars.
3827 *
3828 * Returns an allocated string with hotkeys, or NULL for error.
3829 */
3830 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003831msg_show_console_dialog(
3832 char_u *message,
3833 char_u *buttons,
3834 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
3836 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003837#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003838 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 char_u *hotk = NULL;
3840 char_u *msgp = NULL;
3841 char_u *hotkp = NULL;
3842 char_u *r;
3843 int copy;
3844#define HAS_HOTKEY_LEN 30
3845 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003846 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 int idx;
3848
3849 has_hotkey[0] = FALSE;
3850
3851 /*
3852 * First loop: compute the size of memory to allocate.
3853 * Second loop: copy to the allocated memory.
3854 */
3855 for (copy = 0; copy <= 1; ++copy)
3856 {
3857 r = buttons;
3858 idx = 0;
3859 while (*r)
3860 {
3861 if (*r == DLG_BUTTON_SEP)
3862 {
3863 if (copy)
3864 {
3865 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003866 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
Bram Moolenaar85a20022019-12-21 18:25:54 +01003868 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003870 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003873 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (dfltbutton)
3875 --dfltbutton;
3876
Bram Moolenaar85a20022019-12-21 18:25:54 +01003877 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3879 first_hotkey = TRUE;
3880 }
3881 else
3882 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003883 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3884 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if (idx < HAS_HOTKEY_LEN - 1)
3886 has_hotkey[++idx] = FALSE;
3887 }
3888 }
3889 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3890 {
3891 if (*r == DLG_HOTKEY_CHAR)
3892 ++r;
3893 first_hotkey = FALSE;
3894 if (copy)
3895 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003896 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 *msgp++ = *r;
3898 else
3899 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003900 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3902 msgp += copy_char(r, msgp, FALSE);
3903 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3904
Bram Moolenaar85a20022019-12-21 18:25:54 +01003905 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003906 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909 else
3910 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003911 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (idx < HAS_HOTKEY_LEN - 1)
3913 has_hotkey[idx] = TRUE;
3914 }
3915 }
3916 else
3917 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003918 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (copy)
3920 msgp += copy_char(r, msgp, FALSE);
3921 }
3922
Bram Moolenaar85a20022019-12-21 18:25:54 +01003923 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003924 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
3926
3927 if (copy)
3928 {
3929 *msgp++ = ':';
3930 *msgp++ = ' ';
3931 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 else
3934 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003935 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003936 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003937 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003938 + 3); // for the ": " and NUL
3939 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar85a20022019-12-21 18:25:54 +01003941 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (!has_hotkey[0])
3943 {
3944 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003945 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947
3948 /*
3949 * Now allocate and load the strings
3950 */
3951 vim_free(confirm_msg);
3952 confirm_msg = alloc(len);
3953 if (confirm_msg == NULL)
3954 return NULL;
3955 *confirm_msg = NUL;
3956 hotk = alloc(lenhotkey);
3957 if (hotk == NULL)
3958 return NULL;
3959
3960 *confirm_msg = '\n';
3961 STRCPY(confirm_msg + 1, message);
3962
3963 msgp = confirm_msg + 1 + STRLEN(message);
3964 hotkp = hotk;
3965
Bram Moolenaar85a20022019-12-21 18:25:54 +01003966 // Define first default hotkey. Keep the hotkey string NUL
3967 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003968 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
Bram Moolenaar85a20022019-12-21 18:25:54 +01003970 // Remember where the choices start, displaying starts here when
3971 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 confirm_msg_tail = msgp;
3973 *msgp++ = '\n';
3974 }
3975 }
3976
3977 display_confirm_msg();
3978 return hotk;
3979}
3980
3981/*
3982 * Display the ":confirm" message. Also called when screen resized.
3983 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003984 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003985display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003987 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 ++confirm_msg_used;
3989 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003990 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 --confirm_msg_used;
3992}
3993
Bram Moolenaar85a20022019-12-21 18:25:54 +01003994#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3997
3998 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003999vim_dialog_yesno(
4000 int type,
4001 char_u *title,
4002 char_u *message,
4003 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 if (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"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 return VIM_YES;
4010 return VIM_NO;
4011}
4012
4013 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004014vim_dialog_yesnocancel(
4015 int type,
4016 char_u *title,
4017 char_u *message,
4018 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
4020 switch (do_dialog(type,
4021 title == NULL ? (char_u *)_("Question") : title,
4022 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004023 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025 case 1: return VIM_YES;
4026 case 2: return VIM_NO;
4027 }
4028 return VIM_CANCEL;
4029}
4030
4031 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004032vim_dialog_yesnoallcancel(
4033 int type,
4034 char_u *title,
4035 char_u *message,
4036 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
4038 switch (do_dialog(type,
4039 title == NULL ? (char_u *)"Question" : title,
4040 message,
4041 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004042 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
4044 case 1: return VIM_YES;
4045 case 2: return VIM_NO;
4046 case 3: return VIM_ALL;
4047 case 4: return VIM_DISCARDALL;
4048 }
4049 return VIM_CANCEL;
4050}
4051
Bram Moolenaar85a20022019-12-21 18:25:54 +01004052#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004054#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055static char *e_printf = N_("E766: Insufficient arguments for printf()");
4056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004057/*
4058 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4059 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004060 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004061tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062{
4063 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004064 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065 int err = FALSE;
4066
4067 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004068 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069 else
4070 {
4071 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004072 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004073 if (err)
4074 n = 0;
4075 }
4076 return n;
4077}
4078
4079/*
4080 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004081 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004082 * are not converted to a string.
4083 * If "tofree" is not NULL echo_string() is used. All types are converted to
4084 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004085 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004086 */
4087 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004088tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004089{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004090 int idx = *idxp - 1;
4091 char *s = NULL;
4092 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093
4094 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004095 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096 else
4097 {
4098 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004099 if (tofree != NULL)
4100 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4101 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004102 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004103 }
4104 return s;
4105}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004106
4107# ifdef FEAT_FLOAT
4108/*
4109 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4110 */
4111 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004112tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004113{
4114 int idx = *idxp - 1;
4115 double f = 0;
4116
4117 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004118 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004119 else
4120 {
4121 ++*idxp;
4122 if (tvs[idx].v_type == VAR_FLOAT)
4123 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004124 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004125 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004126 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004127 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004128 }
4129 return f;
4130}
4131# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004132#endif
4133
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004134#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004135/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004136 * Return the representation of infinity for printf() function:
4137 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4138 */
4139 static const char *
4140infinity_str(int positive,
4141 char fmt_spec,
4142 int force_sign,
4143 int space_for_positive)
4144{
4145 static const char *table[] =
4146 {
4147 "-inf", "inf", "+inf", " inf",
4148 "-INF", "INF", "+INF", " INF"
4149 };
4150 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4151
4152 if (ASCII_ISUPPER(fmt_spec))
4153 idx += 4;
4154 return table[idx];
4155}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004156#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004157
4158/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004159 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004160 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004161 * consistency.
4162 *
4163 * This code is based on snprintf.c - a portable implementation of snprintf
4164 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004165 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004166 * The original code, including useful comments, can be found here:
4167 * http://www.ijs.si/software/snprintf/
4168 *
4169 * This snprintf() only supports the following conversion specifiers:
4170 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4171 * with flags: '-', '+', ' ', '0' and '#'.
4172 * An asterisk is supported for field width as well as precision.
4173 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004174 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004175 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004176 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004177 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004178 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004179 * The locale is not used, the string is used as a byte string. This is only
4180 * relevant for double-byte encodings where the second byte may be '%'.
4181 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004182 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4183 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184 *
4185 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004186 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004187 * is greater or equal to "str_m", not all characters from the result
4188 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4189 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004190 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004191 */
4192
4193/*
4194 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004195 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004196 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004197 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004198 */
4199
Bram Moolenaar85a20022019-12-21 18:25:54 +01004200// When generating prototypes all of this is skipped, cproto doesn't
4201// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004202#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004203
Bram Moolenaar85a20022019-12-21 18:25:54 +01004204// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004205 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004206vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004207{
4208 va_list ap;
4209 int str_l;
4210 size_t len = STRLEN(str);
4211 size_t space;
4212
4213 if (str_m <= len)
4214 space = 0;
4215 else
4216 space = str_m - len;
4217 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004218 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004219 va_end(ap);
4220 return str_l;
4221}
Bram Moolenaara800b422010-06-27 01:15:55 +02004222
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004224vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004225{
4226 va_list ap;
4227 int str_l;
4228
4229 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004230 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004231 va_end(ap);
4232 return str_l;
4233}
4234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004236vim_vsnprintf(
4237 char *str,
4238 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004239 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004240 va_list ap)
4241{
4242 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4243}
4244
4245 int
4246vim_vsnprintf_typval(
4247 char *str,
4248 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004249 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004250 va_list ap,
4251 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252{
4253 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004254 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004255 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004256
4257 if (p == NULL)
4258 p = "";
4259 while (*p != NUL)
4260 {
4261 if (*p != '%')
4262 {
4263 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004264 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004265
Bram Moolenaar85a20022019-12-21 18:25:54 +01004266 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004267 if (str_l < str_m)
4268 {
4269 size_t avail = str_m - str_l;
4270
4271 mch_memmove(str + str_l, p, n > avail ? avail : n);
4272 }
4273 p += n;
4274 str_l += n;
4275 }
4276 else
4277 {
4278 size_t min_field_width = 0, precision = 0;
4279 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4280 int alternate_form = 0, force_sign = 0;
4281
Bram Moolenaar85a20022019-12-21 18:25:54 +01004282 // If both the ' ' and '+' flags appear, the ' ' flag should be
4283 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004284 int space_for_positive = 1;
4285
Bram Moolenaar85a20022019-12-21 18:25:54 +01004286 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004287 char length_modifier = '\0';
4288
Bram Moolenaar85a20022019-12-21 18:25:54 +01004289 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004290# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004291# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4292 // That sounds reasonable to use as the maximum
4293 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004294# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004295# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004296# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004297 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004298
Bram Moolenaar85a20022019-12-21 18:25:54 +01004299 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004300 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004301
Bram Moolenaar85a20022019-12-21 18:25:54 +01004302 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303 size_t str_arg_l;
4304
Bram Moolenaar85a20022019-12-21 18:25:54 +01004305 // unsigned char argument value - only defined for c conversion.
4306 // N.B. standard explicitly states the char argument for the c
4307 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004308 unsigned char uchar_arg;
4309
Bram Moolenaar85a20022019-12-21 18:25:54 +01004310 // number of zeros to be inserted for numeric conversions as
4311 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004312 size_t number_of_zeros_to_pad = 0;
4313
Bram Moolenaar85a20022019-12-21 18:25:54 +01004314 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004315 size_t zero_padding_insertion_ind = 0;
4316
Bram Moolenaar85a20022019-12-21 18:25:54 +01004317 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004318 char fmt_spec = '\0';
4319
Bram Moolenaar85a20022019-12-21 18:25:54 +01004320 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004321 char_u *tofree = NULL;
4322
4323
Bram Moolenaar85a20022019-12-21 18:25:54 +01004324 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004325
Bram Moolenaar85a20022019-12-21 18:25:54 +01004326 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004327 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4328 || *p == '#' || *p == '\'')
4329 {
4330 switch (*p)
4331 {
4332 case '0': zero_padding = 1; break;
4333 case '-': justify_left = 1; break;
4334 case '+': force_sign = 1; space_for_positive = 0; break;
4335 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004336 // If both the ' ' and '+' flags appear, the ' '
4337 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004338 break;
4339 case '#': alternate_form = 1; break;
4340 case '\'': break;
4341 }
4342 p++;
4343 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004344 // If the '0' and '-' flags both appear, the '0' flag should be
4345 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004346
Bram Moolenaar85a20022019-12-21 18:25:54 +01004347 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004348 if (*p == '*')
4349 {
4350 int j;
4351
4352 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004355 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356# endif
4357 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004358 if (j >= 0)
4359 min_field_width = j;
4360 else
4361 {
4362 min_field_width = -j;
4363 justify_left = 1;
4364 }
4365 }
4366 else if (VIM_ISDIGIT((int)(*p)))
4367 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004368 // size_t could be wider than unsigned int; make sure we treat
4369 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004370 unsigned int uj = *p++ - '0';
4371
4372 while (VIM_ISDIGIT((int)(*p)))
4373 uj = 10 * uj + (unsigned int)(*p++ - '0');
4374 min_field_width = uj;
4375 }
4376
Bram Moolenaar85a20022019-12-21 18:25:54 +01004377 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004378 if (*p == '.')
4379 {
4380 p++;
4381 precision_specified = 1;
4382 if (*p == '*')
4383 {
4384 int j;
4385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004388 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389# endif
4390 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004391 p++;
4392 if (j >= 0)
4393 precision = j;
4394 else
4395 {
4396 precision_specified = 0;
4397 precision = 0;
4398 }
4399 }
4400 else if (VIM_ISDIGIT((int)(*p)))
4401 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004402 // size_t could be wider than unsigned int; make sure we
4403 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004404 unsigned int uj = *p++ - '0';
4405
4406 while (VIM_ISDIGIT((int)(*p)))
4407 uj = 10 * uj + (unsigned int)(*p++ - '0');
4408 precision = uj;
4409 }
4410 }
4411
Bram Moolenaar85a20022019-12-21 18:25:54 +01004412 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004413 if (*p == 'h' || *p == 'l')
4414 {
4415 length_modifier = *p;
4416 p++;
4417 if (length_modifier == 'l' && *p == 'l')
4418 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004419 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004420 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004421 p++;
4422 }
4423 }
4424 fmt_spec = *p;
4425
Bram Moolenaar85a20022019-12-21 18:25:54 +01004426 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004427 switch (fmt_spec)
4428 {
4429 case 'i': fmt_spec = 'd'; break;
4430 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4431 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4432 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4433 default: break;
4434 }
4435
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004436# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004437 switch (fmt_spec)
4438 {
4439 case 'd': case 'u': case 'o': case 'x': case 'X':
4440 if (tvs != NULL && length_modifier == '\0')
4441 length_modifier = 'L';
4442 }
4443# endif
4444
Bram Moolenaar85a20022019-12-21 18:25:54 +01004445 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004446 switch (fmt_spec)
4447 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004448 // '%' and 'c' behave similar to 's' regarding flags and field
4449 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004450 case '%':
4451 case 'c':
4452 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004453 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004454 str_arg_l = 1;
4455 switch (fmt_spec)
4456 {
4457 case '%':
4458 str_arg = p;
4459 break;
4460
4461 case 'c':
4462 {
4463 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464
4465 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004467 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004468# endif
4469 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004470 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004471 uchar_arg = (unsigned char)j;
4472 str_arg = (char *)&uchar_arg;
4473 break;
4474 }
4475
4476 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004477 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004478 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004480 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004481# endif
4482 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004483 if (str_arg == NULL)
4484 {
4485 str_arg = "[NULL]";
4486 str_arg_l = 6;
4487 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004488 // make sure not to address string beyond the specified
4489 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004490 else if (!precision_specified)
4491 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004492 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004493 else if (precision == 0)
4494 str_arg_l = 0;
4495 else
4496 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004497 // Don't put the #if inside memchr(), it can be a
4498 // macro.
4499 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004500 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004501 precision <= (size_t)0x7fffffffL ? precision
4502 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004503 str_arg_l = (q == NULL) ? precision
4504 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004505 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004506 if (fmt_spec == 'S')
4507 {
4508 if (min_field_width != 0)
4509 min_field_width += STRLEN(str_arg)
4510 - mb_string2cells((char_u *)str_arg, -1);
4511 if (precision)
4512 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004513 char_u *p1;
4514 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004515
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004516 for (p1 = (char_u *)str_arg; *p1;
4517 p1 += mb_ptr2len(p1))
4518 {
4519 i += (size_t)mb_ptr2cells(p1);
4520 if (i > precision)
4521 break;
4522 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004523 str_arg_l = precision = p1 - (char_u *)str_arg;
4524 }
4525 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004526 break;
4527
4528 default:
4529 break;
4530 }
4531 break;
4532
Bram Moolenaar91984b92016-08-16 21:58:41 +02004533 case 'd': case 'u':
4534 case 'b': case 'B':
4535 case 'o':
4536 case 'x': case 'X':
4537 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004538 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004539 // NOTE: the u, b, o, x, X and p conversion specifiers
4540 // imply the value is unsigned; d implies a signed
4541 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004542
Bram Moolenaar85a20022019-12-21 18:25:54 +01004543 // 0 if numeric argument is zero (or if pointer is
4544 // NULL for 'p'), +1 if greater than zero (or nonzero
4545 // for unsigned arguments), -1 if negative (unsigned
4546 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004547 int arg_sign = 0;
4548
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004549 // only set for length modifier h, or for no length
4550 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004551 int int_arg = 0;
4552 unsigned int uint_arg = 0;
4553
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004554 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004555 long int long_arg = 0;
4556 unsigned long int ulong_arg = 0;
4557
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004558 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004559 varnumber_T llong_arg = 0;
4560 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004561
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004562 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004563 uvarnumber_T bin_arg = 0;
4564
Bram Moolenaar85a20022019-12-21 18:25:54 +01004565 // pointer argument value -only defined for p
4566 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004567 void *ptr_arg = NULL;
4568
4569 if (fmt_spec == 'p')
4570 {
4571 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004573# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004574 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4575 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576# endif
4577 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004578 if (ptr_arg != NULL)
4579 arg_sign = 1;
4580 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004581 else if (fmt_spec == 'b' || fmt_spec == 'B')
4582 {
4583 bin_arg =
4584# if defined(FEAT_EVAL)
4585 tvs != NULL ?
4586 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4587# endif
4588 va_arg(ap, uvarnumber_T);
4589 if (bin_arg != 0)
4590 arg_sign = 1;
4591 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004592 else if (fmt_spec == 'd')
4593 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004594 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004595 switch (length_modifier)
4596 {
4597 case '\0':
4598 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004599 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004602 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603# endif
4604 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004605 if (int_arg > 0)
4606 arg_sign = 1;
4607 else if (int_arg < 0)
4608 arg_sign = -1;
4609 break;
4610 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004611 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004613 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614# endif
4615 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004616 if (long_arg > 0)
4617 arg_sign = 1;
4618 else if (long_arg < 0)
4619 arg_sign = -1;
4620 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004621 case 'L':
4622 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004623# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004624 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004625# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004626 va_arg(ap, varnumber_T);
4627 if (llong_arg > 0)
4628 arg_sign = 1;
4629 else if (llong_arg < 0)
4630 arg_sign = -1;
4631 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004632 }
4633 }
4634 else
4635 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004636 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004637 switch (length_modifier)
4638 {
4639 case '\0':
4640 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004643 tvs != NULL ? (unsigned)
4644 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645# endif
4646 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004647 if (uint_arg != 0)
4648 arg_sign = 1;
4649 break;
4650 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004653 tvs != NULL ? (unsigned long)
4654 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004655# endif
4656 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004657 if (ulong_arg != 0)
4658 arg_sign = 1;
4659 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004660 case 'L':
4661 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004662# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004663 tvs != NULL ? (uvarnumber_T)
4664 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004665# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004666 va_arg(ap, uvarnumber_T);
4667 if (ullong_arg != 0)
4668 arg_sign = 1;
4669 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004670 }
4671 }
4672
4673 str_arg = tmp;
4674 str_arg_l = 0;
4675
Bram Moolenaar85a20022019-12-21 18:25:54 +01004676 // NOTE:
4677 // For d, i, u, o, x, and X conversions, if precision is
4678 // specified, the '0' flag should be ignored. This is so
4679 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4680 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004681 if (precision_specified)
4682 zero_padding = 0;
4683 if (fmt_spec == 'd')
4684 {
4685 if (force_sign && arg_sign >= 0)
4686 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004687 // leave negative numbers for sprintf to handle, to
4688 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004689 }
4690 else if (alternate_form)
4691 {
4692 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004693 && (fmt_spec == 'b' || fmt_spec == 'B'
4694 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004695 {
4696 tmp[str_arg_l++] = '0';
4697 tmp[str_arg_l++] = fmt_spec;
4698 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004699 // alternate form should have no effect for p
4700 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004701 }
4702
4703 zero_padding_insertion_ind = str_arg_l;
4704 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004705 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004706 if (precision == 0 && arg_sign == 0)
4707 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004708 // When zero value is formatted with an explicit
4709 // precision 0, the resulting formatted string is
4710 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004711 }
4712 else
4713 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004714 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004715 int f_l = 0;
4716
Bram Moolenaar85a20022019-12-21 18:25:54 +01004717 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004718 f[f_l++] = '%';
4719 if (!length_modifier)
4720 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004721 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004722 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004723# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004724 f[f_l++] = 'I';
4725 f[f_l++] = '6';
4726 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004727# else
4728 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004729 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004730# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004731 }
4732 else
4733 f[f_l++] = length_modifier;
4734 f[f_l++] = fmt_spec;
4735 f[f_l++] = '\0';
4736
4737 if (fmt_spec == 'p')
4738 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004739 else if (fmt_spec == 'b' || fmt_spec == 'B')
4740 {
4741 char b[8 * sizeof(uvarnumber_T)];
4742 size_t b_l = 0;
4743 uvarnumber_T bn = bin_arg;
4744
4745 do
4746 {
4747 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4748 bn >>= 1;
4749 }
4750 while (bn != 0);
4751
4752 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4753 str_arg_l += b_l;
4754 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004755 else if (fmt_spec == 'd')
4756 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004757 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004758 switch (length_modifier)
4759 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004760 case '\0': str_arg_l += sprintf(
4761 tmp + str_arg_l, f,
4762 int_arg);
4763 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004764 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004765 tmp + str_arg_l, f,
4766 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004767 break;
4768 case 'l': str_arg_l += sprintf(
4769 tmp + str_arg_l, f, long_arg);
4770 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004771 case 'L': str_arg_l += sprintf(
4772 tmp + str_arg_l, f, llong_arg);
4773 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004774 }
4775 }
4776 else
4777 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004778 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004779 switch (length_modifier)
4780 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004781 case '\0': str_arg_l += sprintf(
4782 tmp + str_arg_l, f,
4783 uint_arg);
4784 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004785 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004786 tmp + str_arg_l, f,
4787 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004788 break;
4789 case 'l': str_arg_l += sprintf(
4790 tmp + str_arg_l, f, ulong_arg);
4791 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004792 case 'L': str_arg_l += sprintf(
4793 tmp + str_arg_l, f, ullong_arg);
4794 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004795 }
4796 }
4797
Bram Moolenaar85a20022019-12-21 18:25:54 +01004798 // include the optional minus sign and possible
4799 // "0x" in the region before the zero padding
4800 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004801 if (zero_padding_insertion_ind < str_arg_l
4802 && tmp[zero_padding_insertion_ind] == '-')
4803 zero_padding_insertion_ind++;
4804 if (zero_padding_insertion_ind + 1 < str_arg_l
4805 && tmp[zero_padding_insertion_ind] == '0'
4806 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4807 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4808 zero_padding_insertion_ind += 2;
4809 }
4810
4811 {
4812 size_t num_of_digits = str_arg_l
4813 - zero_padding_insertion_ind;
4814
4815 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004816 // unless zero is already the first
4817 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004818 && !(zero_padding_insertion_ind < str_arg_l
4819 && tmp[zero_padding_insertion_ind] == '0'))
4820 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004821 // assure leading zero for alternate-form
4822 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004823 if (!precision_specified
4824 || precision < num_of_digits + 1)
4825 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004826 // precision is increased to force the
4827 // first character to be zero, except if a
4828 // zero value is formatted with an
4829 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004830 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004831 }
4832 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004833 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004834 if (num_of_digits < precision)
4835 number_of_zeros_to_pad = precision - num_of_digits;
4836 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004837 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004838 if (!justify_left && zero_padding)
4839 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004840 int n = (int)(min_field_width - (str_arg_l
4841 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004842 if (n > 0)
4843 number_of_zeros_to_pad += n;
4844 }
4845 break;
4846 }
4847
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004848# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004849 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004850 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004851 case 'e':
4852 case 'E':
4853 case 'g':
4854 case 'G':
4855 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004856 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004857 double f;
4858 double abs_f;
4859 char format[40];
4860 int l;
4861 int remove_trailing_zeroes = FALSE;
4862
4863 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004864# if defined(FEAT_EVAL)
4865 tvs != NULL ? tv_float(tvs, &arg_idx) :
4866# endif
4867 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004868 abs_f = f < 0 ? -f : f;
4869
4870 if (fmt_spec == 'g' || fmt_spec == 'G')
4871 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004872 // Would be nice to use %g directly, but it prints
4873 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004874 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4875 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004876 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004877 else
4878 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4879 remove_trailing_zeroes = TRUE;
4880 }
4881
Bram Moolenaar04186092016-08-29 21:55:35 +02004882 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004883# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004884 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004885# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004886 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004887# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004888 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004889 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004890 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004891 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4892 force_sign, space_for_positive));
4893 str_arg_l = STRLEN(tmp);
4894 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004895 }
4896 else
4897 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004898 if (isnan(f))
4899 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004900 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004901 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4902 : "nan");
4903 str_arg_l = 3;
4904 zero_padding = 0;
4905 }
4906 else if (isinf(f))
4907 {
4908 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4909 force_sign, space_for_positive));
4910 str_arg_l = STRLEN(tmp);
4911 zero_padding = 0;
4912 }
4913 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004915 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004916 format[0] = '%';
4917 l = 1;
4918 if (force_sign)
4919 format[l++] = space_for_positive ? ' ' : '+';
4920 if (precision_specified)
4921 {
4922 size_t max_prec = TMP_LEN - 10;
4923
Bram Moolenaar85a20022019-12-21 18:25:54 +01004924 // Make sure we don't get more digits than we
4925 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004926 if ((fmt_spec == 'f' || fmt_spec == 'F')
4927 && abs_f > 1.0)
4928 max_prec -= (size_t)log10(abs_f);
4929 if (precision > max_prec)
4930 precision = max_prec;
4931 l += sprintf(format + l, ".%d", (int)precision);
4932 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004933 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004934 format[l + 1] = NUL;
4935
Bram Moolenaare9997822016-08-28 16:03:38 +02004936 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004937 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004938
Bram Moolenaara7241f52008-06-24 20:39:31 +00004939 if (remove_trailing_zeroes)
4940 {
4941 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004942 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004943
Bram Moolenaar85a20022019-12-21 18:25:54 +01004944 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004945 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004946 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004947 else
4948 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004949 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004950 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004951 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004952 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004953 // Remove superfluous '+' and leading
4954 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004955 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004956 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004957 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004958 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004959 --str_arg_l;
4960 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004961 i = (tp[1] == '-') ? 2 : 1;
4962 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004963 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004964 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004965 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004966 --str_arg_l;
4967 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004968 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004969 }
4970 }
4971
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004972 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004973 // Remove trailing zeroes, but keep the one
4974 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004975 while (tp > tmp + 2 && *tp == '0'
4976 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004977 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004978 STRMOVE(tp, tp + 1);
4979 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004980 --str_arg_l;
4981 }
4982 }
4983 else
4984 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004985 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004986
Bram Moolenaar85a20022019-12-21 18:25:54 +01004987 // Be consistent: some printf("%e") use 1.0e+12
4988 // and some 1.0e+012. Remove one zero in the last
4989 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004990 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004991 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004992 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4993 && tp[2] == '0'
4994 && vim_isdigit(tp[3])
4995 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004996 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004997 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004998 --str_arg_l;
4999 }
5000 }
5001 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005002 if (zero_padding && min_field_width > str_arg_l
5003 && (tmp[0] == '-' || force_sign))
5004 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005005 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02005006 number_of_zeros_to_pad = min_field_width - str_arg_l;
5007 zero_padding_insertion_ind = 1;
5008 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005009 str_arg = tmp;
5010 break;
5011 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005012# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005013
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005014 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01005015 // unrecognized conversion specifier, keep format string
5016 // as-is
5017 zero_padding = 0; // turn zero padding off for non-numeric
5018 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005019 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01005020 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005021
Bram Moolenaar85a20022019-12-21 18:25:54 +01005022 // discard the unrecognized conversion, just keep *
5023 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005024 str_arg = p;
5025 str_arg_l = 0;
5026 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005027 str_arg_l++; // include invalid conversion specifier
5028 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005029 break;
5030 }
5031
5032 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005033 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005034
Bram Moolenaar85a20022019-12-21 18:25:54 +01005035 // insert padding to the left as requested by min_field_width;
5036 // this does not include the zero padding in case of numerical
5037 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005038 if (!justify_left)
5039 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005040 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005041 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005042
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005043 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005044 {
5045 if (str_l < str_m)
5046 {
5047 size_t avail = str_m - str_l;
5048
5049 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005050 (size_t)pn > avail ? avail
5051 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005052 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005053 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005054 }
5055 }
5056
Bram Moolenaar85a20022019-12-21 18:25:54 +01005057 // zero padding as requested by the precision or by the minimal
5058 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005059 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005060 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005061 // will not copy first part of numeric right now, *
5062 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 zero_padding_insertion_ind = 0;
5064 }
5065 else
5066 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005067 // insert first part of numerics (sign or '0x') before zero
5068 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005069 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005070
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005071 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072 {
5073 if (str_l < str_m)
5074 {
5075 size_t avail = str_m - str_l;
5076
5077 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005078 (size_t)zn > avail ? avail
5079 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005080 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005081 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005082 }
5083
Bram Moolenaar85a20022019-12-21 18:25:54 +01005084 // insert zero padding as requested by the precision or min
5085 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005086 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005087 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005088 {
5089 if (str_l < str_m)
5090 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005091 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005092
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005093 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005094 (size_t)zn > avail ? avail
5095 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005096 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005097 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005098 }
5099 }
5100
Bram Moolenaar85a20022019-12-21 18:25:54 +01005101 // insert formatted string
5102 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005104 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005105
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005106 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005107 {
5108 if (str_l < str_m)
5109 {
5110 size_t avail = str_m - str_l;
5111
5112 mch_memmove(str + str_l,
5113 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005114 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005115 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005116 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005117 }
5118 }
5119
Bram Moolenaar85a20022019-12-21 18:25:54 +01005120 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005121 if (justify_left)
5122 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005123 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005124 int pn = (int)(min_field_width
5125 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005126
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005127 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005128 {
5129 if (str_l < str_m)
5130 {
5131 size_t avail = str_m - str_l;
5132
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005133 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005134 (size_t)pn > avail ? avail
5135 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005136 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005137 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005138 }
5139 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005140 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005141 }
5142 }
5143
5144 if (str_m > 0)
5145 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005146 // make sure the string is nul-terminated even at the expense of
5147 // overwriting the last character (shouldn't happen, but just in case)
5148 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005149 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5150 }
5151
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005152 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005153 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154
Bram Moolenaar85a20022019-12-21 18:25:54 +01005155 // Return the number of characters formatted (excluding trailing nul
5156 // character), that is, the number of characters that would have been
5157 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005158 return (int)str_l;
5159}
5160
Bram Moolenaar85a20022019-12-21 18:25:54 +01005161#endif // PROTO