blob: f2fe23b58239b1be0c3d9bf200029a044f306751 [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 Moolenaar62818152021-02-14 15:37:30 +0100251 if (*s == NUL)
252 {
253 if (buflen > 0)
254 *buf = NUL;
255 return;
256 }
257
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200258 if (room_in < 3)
259 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
Bram Moolenaar85a20022019-12-21 18:25:54 +0100262 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100263 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 {
265 if (s[e] == NUL)
266 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100267 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 buf[e] = NUL;
269 return;
270 }
271 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200272 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 break;
274 len += n;
275 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000277 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100279 if (++e == buflen)
280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 buf[e] = s[e];
282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 }
284
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 if (enc_dbcs != 0)
288 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100289 // For DBCS going backwards in a string is slow, but
290 // computing the cell width isn't too slow: go forward
291 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 n = vim_strsize(s + i);
293 while (len + n > room)
294 {
295 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000296 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 }
298 }
299 else if (enc_utf8)
300 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100301 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000302 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 for (;;)
304 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000305 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200306 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200307 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200309 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 break;
311 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200312 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 }
314 }
315 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100317 for (i = (int)STRLEN(s);
318 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 len += n;
320 }
321
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200322
323 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100325 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200326 if (s != buf)
327 {
328 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200329 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200330 len = buflen - 1;
331 len = len - e + 1;
332 if (len < 1)
333 buf[e - 1] = NUL;
334 else
335 mch_memmove(buf + e, s + e, len);
336 }
337 }
338 else if (e + 3 < buflen)
339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100341 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200342 len = STRLEN(s + i) + 1;
343 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100344 len = buflen - e - 3 - 1;
345 mch_memmove(buf + e + 3, s + i, len);
346 buf[e + 3 + len - 1] = NUL;
347 }
348 else
349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100350 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200351 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353}
354
355/*
356 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100357 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 * shorter than IOSIZE!!!
359 */
360#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100362int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100365smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200367 if (IObuff == NULL)
368 {
369 // Very early in initialisation and already something wrong, just
370 // give the raw message so the user at least gets a hint.
371 return msg((char *)s);
372 }
373 else
374 {
375 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200377 va_start(arglist, s);
378 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
379 va_end(arglist);
380 return msg((char *)IObuff);
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382}
383
384 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100385smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200387 if (IObuff == NULL)
388 {
389 // Very early in initialisation and already something wrong, just
390 // give the raw message so the user at least gets a hint.
391 return msg_attr((char *)s, attr);
392 }
393 else
394 {
395 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200397 va_start(arglist, s);
398 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
399 va_end(arglist);
400 return msg_attr((char *)IObuff, attr);
401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402}
403
Bram Moolenaare0429682018-07-01 16:44:03 +0200404 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100405smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200406{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200407 if (IObuff == NULL)
408 {
409 // Very early in initialisation and already something wrong, just
410 // give the raw message so the user at least gets a hint.
411 return msg_attr_keep((char *)s, attr, TRUE);
412 }
413 else
414 {
415 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200416
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200417 va_start(arglist, s);
418 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
419 va_end(arglist);
420 return msg_attr_keep((char *)IObuff, attr, TRUE);
421 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200422}
423
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424#endif
425
426/*
427 * Remember the last sourcing name/lnum used in an error message, so that it
428 * isn't printed each time when it didn't change.
429 */
430static int last_sourcing_lnum = 0;
431static char_u *last_sourcing_name = NULL;
432
433/*
434 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
435 * for the next error message;
436 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000437 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100438reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100440 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 last_sourcing_lnum = 0;
442}
443
444/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100445 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000446 */
447 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100448other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000449{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100450 if (SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000451 {
452 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100453 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000454 return TRUE;
455 }
456 return FALSE;
457}
458
459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 * Get the message about the source, as used for an error message.
461 * Returns an allocated string with room for one more character.
462 * Returns NULL when no message is to be given.
463 */
464 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100465get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 char_u *Buf, *p;
468
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100469 if (SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200471 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100472 char_u *tofree = sname;
473
474 if (sname == NULL)
475 sname = SOURCING_NAME;
476
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200477#ifdef FEAT_EVAL
478 if (estack_compiling)
479 p = (char_u *)_("Error detected while compiling %s:");
480 else
481#endif
482 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100483 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100485 sprintf((char *)Buf, (char *)p, sname);
486 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 return Buf;
488 }
489 return NULL;
490}
491
492/*
493 * Get the message about the source lnum, as used for an error message.
494 * Returns an allocated string with room for one more character.
495 * Returns NULL when no message is to be given.
496 */
497 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100498get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 char_u *Buf, *p;
501
Bram Moolenaar85a20022019-12-21 18:25:54 +0100502 // lnum is 0 when executing a command from the command line
503 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100504 if (SOURCING_NAME != NULL
505 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
506 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200509 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100511 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 return Buf;
513 }
514 return NULL;
515}
516
517/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000518 * Display name and line number for the source of an error.
519 * Remember the file name and line number, so that for the next error the info
520 * is only displayed if it changed.
521 */
522 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100523msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000524{
525 char_u *p;
526
527 ++no_wait_return;
528 p = get_emsg_source();
529 if (p != NULL)
530 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100531 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000532 vim_free(p);
533 }
534 p = get_emsg_lnum();
535 if (p != NULL)
536 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100537 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000538 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100539 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000540 }
541
Bram Moolenaar85a20022019-12-21 18:25:54 +0100542 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100543 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000544 {
545 vim_free(last_sourcing_name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100546 if (SOURCING_NAME == NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000547 last_sourcing_name = NULL;
548 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100549 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000550 }
551 --no_wait_return;
552}
553
554/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000555 * Return TRUE if not giving error messages right now:
556 * If "emsg_off" is set: no error messages at the moment.
557 * If "msg" is in 'debug': do error message but without side effects.
558 * If "emsg_skip" is set: never do error messages.
559 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200560 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100561emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000562{
563 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
564 && vim_strchr(p_debug, 't') == NULL)
565#ifdef FEAT_EVAL
566 || emsg_skip > 0
567#endif
568 )
569 return TRUE;
570 return FALSE;
571}
572
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100573#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100574static garray_T ignore_error_list = GA_EMPTY;
575
576 void
577ignore_error_for_testing(char_u *error)
578{
579 if (ignore_error_list.ga_itemsize == 0)
580 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
581
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100582 if (STRCMP("RESET", error) == 0)
583 ga_clear_strings(&ignore_error_list);
584 else
585 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100586}
587
588 static int
589ignore_error(char_u *msg)
590{
591 int i;
592
593 for (i = 0; i < ignore_error_list.ga_len; ++i)
594 if (strstr((char *)msg,
595 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
596 return TRUE;
597 return FALSE;
598}
599#endif
600
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200601#if !defined(HAVE_STRERROR) || defined(PROTO)
602/*
603 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100604 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200605 */
606 void
607do_perror(char *msg)
608{
609 perror(msg);
610 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100611 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200612 --emsg_silent;
613}
614#endif
615
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000616/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100617 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 *
619 * Rings the bell, if appropriate, and calls message() to do the real work
620 * When terminal not initialized (yet) mch_errmsg(..) is used.
621 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100622 * Return TRUE if wait_return not called.
623 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100625 static int
626emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100630 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_EVAL
632 int ignore = FALSE;
633 int severe;
634#endif
635
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100636#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100637 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100638 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100639 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100640 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100641#endif
642
Bram Moolenaar53989552019-12-23 22:59:18 +0100643 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100646 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
647 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 severe = emsg_severe;
649 emsg_severe = FALSE;
650#endif
651
Bram Moolenaar57657d82006-04-21 22:12:41 +0000652 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {
654#ifdef FEAT_EVAL
655 /*
656 * Cause a throw of an error exception if appropriate. Don't display
657 * the error message in this case. (If no matching catch clause will
658 * be found, the message will be displayed later on.) "ignore" is set
659 * when the message should be ignored completely (used for the
660 * interrupt message).
661 */
662 if (cause_errthrow(s, severe, &ignore) == TRUE)
663 {
664 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100665 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return TRUE;
667 }
668
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100669 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200670 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200671 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200672 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200673 vim_free(emsg_assert_fails_context);
674 emsg_assert_fails_context = vim_strsave(
675 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200676 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200677
Bram Moolenaar85a20022019-12-21 18:25:54 +0100678 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 set_vim_var_string(VV_ERRMSG, s, -1);
680#endif
681
682 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000683 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * But do write it to the redirection file.
685 */
686 if (emsg_silent != 0)
687 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200688#ifdef FEAT_EVAL
689 ++did_emsg_silent;
690#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200691 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200693 msg_start();
694 p = get_emsg_source();
695 if (p != NULL)
696 {
697 STRCAT(p, "\n");
698 redir_write(p, -1);
699 vim_free(p);
700 }
701 p = get_emsg_lnum();
702 if (p != NULL)
703 {
704 STRCAT(p, "\n");
705 redir_write(p, -1);
706 vim_free(p);
707 }
708 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100710#ifdef FEAT_EVAL
711 // Only increment did_emsg_def when :silent! wasn't used inside the
712 // :def function.
713 if (emsg_silent == emsg_silent_def)
714 ++did_emsg_def;
715#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100716#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200717 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 return TRUE;
720 }
721
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100722 ex_exitval = 1;
723
Bram Moolenaar85a20022019-12-21 18:25:54 +0100724 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 msg_silent = 0;
726 cmd_silent = FALSE;
727
Bram Moolenaar85a20022019-12-21 18:25:54 +0100728 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 ++global_busy;
730
731 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100732 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200734 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100735 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200736#ifdef FEAT_EVAL
737 did_uncaught_emsg = TRUE;
738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740
Bram Moolenaar85a20022019-12-21 18:25:54 +0100741 emsg_on_display = TRUE; // remember there is an error message
742 ++msg_scroll; // don't overwrite a previous message
743 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000744 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100745 need_wait_return = TRUE; // needed in case emsg() is called after
746 // wait_return has reset need_wait_return
747 // and a redraw is expected because
748 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Bram Moolenaar958dc692016-12-01 15:34:12 +0100750#ifdef FEAT_JOB_CHANNEL
751 emsg_to_channel_log = TRUE;
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /*
754 * Display name and line number for the source of the error.
755 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000756 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 /*
759 * Display the error message itself.
760 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100761 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100762 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100763
764#ifdef FEAT_JOB_CHANNEL
765 emsg_to_channel_log = FALSE;
766#endif
767 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768}
769
770/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100771 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 */
773 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100774emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100776 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100777 if (!emsg_not_now())
778 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100779 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100782#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100783/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 * Print an error message with format string and variable arguments.
785 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100786 */
787 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100788semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100789{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200790 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100791 if (!emsg_not_now())
792 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200793 if (IObuff == NULL)
794 {
795 // Very early in initialisation and already something wrong, just
796 // give the raw message so the user at least gets a hint.
797 return emsg_core((char_u *)s);
798 }
799 else
800 {
801 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100802
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200803 va_start(ap, s);
804 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
805 va_end(ap);
806 return emsg_core(IObuff);
807 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100808 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200809 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100810}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100811#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100812
813/*
814 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
815 * defined. It is used for internal errors only, so that they can be
816 * detected when fuzzing vim.
817 */
818 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100819iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100820{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100821 if (!emsg_not_now())
822 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100823#ifdef ABORT_ON_INTERNAL_ERROR
824 abort();
825#endif
826}
827
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100828#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100829/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100831 * defined. It is used for internal errors only, so that they can be
832 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100833 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100834 */
835 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100836siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 if (!emsg_not_now())
839 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200840 if (IObuff == NULL)
841 {
842 // Very early in initialisation and already something wrong, just
843 // give the raw message so the user at least gets a hint.
844 emsg_core((char_u *)s);
845 }
846 else
847 {
848 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100849
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200850 va_start(ap, s);
851 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
852 va_end(ap);
853 emsg_core(IObuff);
854 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100855 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100856# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100857 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100858# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100859}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100860#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100861
862/*
863 * Give an "Internal error" message.
864 */
865 void
866internal_error(char *where)
867{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100868 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100869}
870
Bram Moolenaardd589232020-02-29 17:38:12 +0100871/*
872 * Like internal_error() but do not call abort(), to avoid tests using
873 * test_unknown() and test_void() causing Vim to exit.
874 */
875 void
876internal_error_no_abort(char *where)
877{
878 semsg(_(e_intern2), where);
879}
880
Bram Moolenaar85a20022019-12-21 18:25:54 +0100881// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaardf177f62005-02-22 08:39:57 +0000883 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100884emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000885{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100886 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000887}
888
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 * Give an error message which contains %s for "name[len]".
891 */
892 void
893emsg_namelen(char *msg, char_u *name, int len)
894{
895 char_u *copy = vim_strnsave((char_u *)name, len);
896
897 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100898 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899}
900
901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 * Like msg(), but truncate to a single line if p_shm contains 't', or when
903 * "force" is TRUE. This truncates in another way as for normal messages.
904 * Careful: The string may be changed by msg_may_trunc()!
905 * Returns a pointer to the printed message, if wait_return() not called.
906 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100907 char *
908msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100911 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaar85a20022019-12-21 18:25:54 +0100913 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100914 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaar32526b32019-01-19 17:43:09 +0100916 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100919 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 msg_hist_off = FALSE;
921
922 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100923 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 return NULL;
925}
926
927/*
928 * Check if message "s" should be truncated at the start (for filenames).
929 * Return a pointer to where the truncated message starts.
930 * Note: May change the message by replacing a character with '<'.
931 */
932 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100933msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
935 int n;
936 int room;
937
938 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
939 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
940 && (n = (int)STRLEN(s) - room) > 0)
941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 if (has_mbyte)
943 {
944 int size = vim_strsize(s);
945
Bram Moolenaar85a20022019-12-21 18:25:54 +0100946 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000947 if (size <= room)
948 return s;
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 for (n = 0; size >= room; )
951 {
952 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000953 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
955 --n;
956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 s += n;
958 *s = '<';
959 }
960 return s;
961}
962
963 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100964add_msg_hist(
965 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100966 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100967 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
969 struct msg_hist *p;
970
971 if (msg_hist_off || msg_silent != 0)
972 return;
973
Bram Moolenaar85a20022019-12-21 18:25:54 +0100974 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000975 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000976 (void)delete_first_msg();
977
Bram Moolenaar85a20022019-12-21 18:25:54 +0100978 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200979 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (p != NULL)
981 {
982 if (len < 0)
983 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100984 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 while (len > 0 && *s == '\n')
986 {
987 ++s;
988 --len;
989 }
990 while (len > 0 && s[len - 1] == '\n')
991 --len;
992 p->msg = vim_strnsave(s, len);
993 p->next = NULL;
994 p->attr = attr;
995 if (last_msg_hist != NULL)
996 last_msg_hist->next = p;
997 last_msg_hist = p;
998 if (first_msg_hist == NULL)
999 first_msg_hist = last_msg_hist;
1000 ++msg_hist_len;
1001 }
1002}
1003
1004/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001005 * Delete the first (oldest) message from the history.
1006 * Returns FAIL if there are no messages.
1007 */
1008 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001009delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001010{
1011 struct msg_hist *p;
1012
1013 if (msg_hist_len <= 0)
1014 return FAIL;
1015 p = first_msg_hist;
1016 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001017 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001018 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001019 vim_free(p->msg);
1020 vim_free(p);
1021 --msg_hist_len;
1022 return OK;
1023}
1024
1025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * ":messages" command.
1027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001029ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 struct msg_hist *p;
1032 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001033 int c = 0;
1034
1035 if (STRCMP(eap->arg, "clear") == 0)
1036 {
1037 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1038
1039 while (msg_hist_len > keep)
1040 (void)delete_first_msg();
1041 return;
1042 }
1043
1044 if (*eap->arg != NUL)
1045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001046 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001047 return;
1048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 msg_hist_off = TRUE;
1051
Bram Moolenaar451f8492016-04-14 17:16:22 +02001052 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001053 if (eap->addr_count != 0)
1054 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001055 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001056 for (; p != NULL && !got_int; p = p->next)
1057 c++;
1058
1059 c -= eap->line2;
1060
Bram Moolenaar85a20022019-12-21 18:25:54 +01001061 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001062 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1063 p = p->next, c--);
1064 }
1065
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001066 if (p == first_msg_hist)
1067 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001068#ifdef FEAT_MULTI_LANG
1069 s = get_mess_lang();
1070#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001071 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001072#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001073 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001074 // The next comment is extracted by xgettext and put in po file for
1075 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001076 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001077 // Translator: Please replace the name and email address
1078 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001079 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001080 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001081 }
1082
Bram Moolenaar85a20022019-12-21 18:25:54 +01001083 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001084 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001086 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 msg_hist_off = FALSE;
1089}
1090
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001091#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092/*
1093 * Call this after prompting the user. This will avoid a hit-return message
1094 * and a delay.
1095 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001096 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001097msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
1099 need_wait_return = FALSE;
1100 emsg_on_display = FALSE;
1101 cmdline_row = msg_row;
1102 msg_col = 0;
1103 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001104 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105}
1106#endif
1107
1108/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001109 * Wait for the user to hit a key (normally Enter).
1110 * If "redraw" is TRUE, clear and redraw the screen.
1111 * If "redraw" is FALSE, just redraw the screen.
1112 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
1114 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001115wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 int c;
1118 int oldState;
1119 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001121 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001122 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124 if (redraw == TRUE)
1125 must_redraw = CLEAR;
1126
Bram Moolenaar85a20022019-12-21 18:25:54 +01001127 // If using ":silent cmd", don't wait for a return. Also don't set
1128 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (msg_silent != 0)
1130 return;
1131
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001132 /*
1133 * When inside vgetc(), we can't wait for a typed character at all.
1134 * With the global command (and some others) we only need one return at
1135 * the end. Adjust cmdline_row to avoid the next message overwriting the
1136 * last one.
1137 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001138 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001140 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (no_wait_return)
1142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (!exmode_active)
1144 cmdline_row = msg_row;
1145 return;
1146 }
1147
Bram Moolenaar85a20022019-12-21 18:25:54 +01001148 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 oldState = State;
1150 if (quit_more)
1151 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001152 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 quit_more = FALSE;
1154 got_int = FALSE;
1155 }
1156 else if (exmode_active)
1157 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001158 msg_puts(" "); // make sure the cursor is on the right line
1159 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 got_int = FALSE;
1161 }
1162 else
1163 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001164 // Make sure the hit-return prompt is on screen when 'guioptions' was
1165 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 screenalloc(FALSE);
1167
1168 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001171 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001173 cmdline_row = msg_row;
1174
Bram Moolenaar85a20022019-12-21 18:25:54 +01001175 // Avoid the sequence that the user types ":" at the hit-return prompt
1176 // to start an Ex command, but the file-changed dialog gets in the
1177 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001178 if (need_check_timestamps)
1179 check_timestamps(FALSE);
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 hit_return_msg();
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 do
1184 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001185 // Remember "got_int", if it is set vgetc() probably returns a
1186 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001188
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // Don't do mappings here, we put the character back in the
1190 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001191 ++no_mapping;
1192 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001193
Bram Moolenaar85a20022019-12-21 18:25:54 +01001194 // Temporarily disable Recording. If Recording is active, the
1195 // character will be recorded later, since it will be added to the
1196 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001197 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001198 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001199 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001200 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001202 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001204 --no_mapping;
1205 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001206 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001207 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001210 // Strange way to allow copying (yanking) a modeless selection at
1211 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1212 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1214 {
1215 clip_copy_modeless_selection(TRUE);
1216 c = K_IGNORE;
1217 }
1218#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001219
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001220 /*
1221 * Allow scrolling back in the messages.
1222 * Also accept scroll-down commands when messages fill the screen,
1223 * to avoid that typing one 'j' too many makes the messages
1224 * disappear.
1225 */
1226 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001227 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001228 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1229 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001230 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001231 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001232 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001233 do_more_prompt(c);
1234 else
1235 {
1236 msg_didout = FALSE;
1237 c = K_IGNORE;
1238 msg_col =
1239#ifdef FEAT_RIGHTLEFT
1240 cmdmsg_rl ? Columns - 1 :
1241#endif
1242 0;
1243 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001244 if (quit_more)
1245 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001246 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001247 quit_more = FALSE;
1248 got_int = FALSE;
1249 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001250 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001251 {
1252 c = K_IGNORE;
1253 hit_return_msg();
1254 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001255 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001256 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001257 && (c == 'j' || c == 'd' || c == 'f'
1258 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001259 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 } while ((had_got_int && c == Ctrl_C)
1262 || c == K_IGNORE
1263#ifdef FEAT_GUI
1264 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1267 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1268 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001269 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001271 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 || (!mouse_has(MOUSE_RETURN)
1273 && mouse_row < msg_row
1274 && (c == K_LEFTMOUSE
1275 || c == K_MIDDLEMOUSE
1276 || c == K_RIGHTMOUSE
1277 || c == K_X1MOUSE
1278 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 );
1280 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 /*
1282 * Avoid that the mouse-up event causes visual mode to start.
1283 */
1284 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1285 || c == K_X1MOUSE || c == K_X2MOUSE)
1286 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001287 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001289 // Put the character back in the typeahead buffer. Don't use the
1290 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001291 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001292 do_redraw = TRUE; // need a redraw even though there is
1293 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296 redir_off = FALSE;
1297
1298 /*
1299 * If the user hits ':', '?' or '/' we get a command line from the next
1300 * line.
1301 */
1302 if (c == ':' || c == '?' || c == '/')
1303 {
1304 if (!exmode_active)
1305 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001306 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001308#ifdef FEAT_TERMINAL
1309 skip_term_loop = TRUE;
1310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312
1313 /*
1314 * If the window size changed set_shellsize() will redraw the screen.
1315 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1316 * typed.
1317 */
1318 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001319 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 msg_check();
1322
1323#if defined(UNIX) || defined(VMS)
1324 /*
1325 * When switching screens, we need to output an extra newline on exit.
1326 */
1327 if (swapping_screen() && !termcap_active)
1328 newline_on_exit = TRUE;
1329#endif
1330
1331 need_wait_return = FALSE;
1332 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001333 emsg_on_display = FALSE; // can delete error message now
1334 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 reset_last_sourcing();
1336 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1337 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
Bram Moolenaar85a20022019-12-21 18:25:54 +01001340 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001342 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 shell_resized();
1344 }
1345 else if (!skip_redraw
1346 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 redraw_later(VALID);
1350 }
1351}
1352
1353/*
1354 * Write the hit-return prompt.
1355 */
1356 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001357hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001359 int save_p_more = p_more;
1360
Bram Moolenaar85a20022019-12-21 18:25:54 +01001361 p_more = FALSE; // don't want see this message when scrolling back
1362 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 msg_putchar('\n');
1364 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001365 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaar32526b32019-01-19 17:43:09 +01001367 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (!msg_use_printf())
1369 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001370 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371}
1372
1373/*
1374 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1375 */
1376 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001377set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379 vim_free(keep_msg);
1380 if (s != NULL && msg_silent == 0)
1381 keep_msg = vim_strsave(s);
1382 else
1383 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001384 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001385 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386}
1387
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001388#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1389/*
1390 * If there currently is a message being displayed, set "keep_msg" to it, so
1391 * that it will be displayed again after redraw.
1392 */
1393 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001394set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001395{
1396 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1397 && (State & NORMAL))
1398 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1399}
1400#endif
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402/*
1403 * Prepare for outputting characters in the command line.
1404 */
1405 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001406msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 int did_return = FALSE;
1409
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001410 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001411 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001412
1413#ifdef FEAT_EVAL
1414 if (need_clr_eos)
1415 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001416 // Halfway an ":echo" command and getting an (error) message: clear
1417 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001418 need_clr_eos = FALSE;
1419 msg_clr_eos();
1420 }
1421#endif
1422
Bram Moolenaar85a20022019-12-21 18:25:54 +01001423 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
1425 msg_row = cmdline_row;
1426 msg_col =
1427#ifdef FEAT_RIGHTLEFT
1428 cmdmsg_rl ? Columns - 1 :
1429#endif
1430 0;
1431 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001432 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
1434 msg_putchar('\n');
1435 did_return = TRUE;
1436 if (exmode_active != EXMODE_NORMAL)
1437 cmdline_row = msg_row;
1438 }
1439 if (!msg_didany || lines_left < 0)
1440 msg_starthere();
1441 if (msg_silent == 0)
1442 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001443 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 cursor_off();
1445 }
1446
Bram Moolenaar85a20022019-12-21 18:25:54 +01001447 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (!did_return)
1449 redir_write((char_u *)"\n", -1);
1450}
1451
1452/*
1453 * Note that the current msg position is where messages start.
1454 */
1455 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001456msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 lines_left = cmdline_row;
1459 msg_didany = FALSE;
1460}
1461
1462 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001463msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 msg_putchar_attr(c, 0);
1466}
1467
1468 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001469msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001471 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473 if (IS_SPECIAL(c))
1474 {
1475 buf[0] = K_SPECIAL;
1476 buf[1] = K_SECOND(c);
1477 buf[2] = K_THIRD(c);
1478 buf[3] = NUL;
1479 }
1480 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001481 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001482 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483}
1484
1485 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001486msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001488 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaar32526b32019-01-19 17:43:09 +01001490 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 msg_puts(buf);
1492}
1493
1494 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001495msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 msg_home_replace_attr(fname, 0);
1498}
1499
1500#if defined(FEAT_FIND_ID) || defined(PROTO)
1501 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001502msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001504 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505}
1506#endif
1507
1508 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001509msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 char_u *name;
1512
1513 name = home_replace_save(NULL, fname);
1514 if (name != NULL)
1515 msg_outtrans_attr(name, attr);
1516 vim_free(name);
1517}
1518
1519/*
1520 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001521 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 * Use attributes 'attr'.
1523 * Return the number of characters it takes on the screen.
1524 */
1525 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001526msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 return msg_outtrans_attr(str, 0);
1529}
1530
1531 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001532msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
1534 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1535}
1536
1537 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001538msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 return msg_outtrans_len_attr(str, len, 0);
1541}
1542
1543/*
1544 * Output one character at "p". Return pointer to the next character.
1545 * Handles multi-byte characters.
1546 */
1547 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001548msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 int l;
1551
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001552 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554 msg_outtrans_len_attr(p, l, attr);
1555 return p + l;
1556 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001557 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 return p + 1;
1559}
1560
1561 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001562msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 int retval = 0;
1565 char_u *str = msgstr;
1566 char_u *plain_start = msgstr;
1567 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 int mb_l;
1569 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001570 int save_got_int = got_int;
1571
1572 // Only quit when got_int was set in here.
1573 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaar85a20022019-12-21 18:25:54 +01001575 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (attr & MSG_HIST)
1577 {
1578 add_msg_hist(str, len, attr);
1579 attr &= ~MSG_HIST;
1580 }
1581
Bram Moolenaar85a20022019-12-21 18:25:54 +01001582 // If the string starts with a composing character first draw a space on
1583 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001585 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 /*
1588 * Go over the string. Special characters are translated and printed.
1589 * Normal characters are printed several at a time.
1590 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001591 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001595 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001597 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 else
1599 mb_l = 1;
1600 if (has_mbyte && mb_l > 1)
1601 {
1602 c = (*mb_ptr2char)(str);
1603 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001604 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 retval += (*mb_ptr2cells)(str);
1606 else
1607 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 // unprintable multi-byte char: print the printable chars so
1609 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001611 msg_puts_attr_len((char *)plain_start,
1612 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001614 msg_puts_attr((char *)transchar(c),
1615 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 retval += char2cells(c);
1617 }
1618 len -= mb_l - 1;
1619 str += mb_l;
1620 }
1621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 s = transchar_byte(*str);
1624 if (s[1] != NUL)
1625 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001626 // unprintable char: print the printable chars so far and the
1627 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001629 msg_puts_attr_len((char *)plain_start,
1630 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001632 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001633 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001635 else
1636 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 ++str;
1638 }
1639 }
1640
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001641 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001642 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001643 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001645 got_int |= save_got_int;
1646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 return retval;
1648}
1649
1650#if defined(FEAT_QUICKFIX) || defined(PROTO)
1651 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001652msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 int i;
1655 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1656
1657 arg = skipwhite(arg);
1658 for (i = 5; *arg && i >= 0; --i)
1659 if (*arg++ != str[i])
1660 break;
1661 if (i < 0)
1662 {
1663 msg_putchar('\n');
1664 for (i = 0; rs[i]; ++i)
1665 msg_putchar(rs[i] - 3);
1666 }
1667}
1668#endif
1669
1670/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001671 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 * Return the number of characters it takes on the screen.
1673 *
1674 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1675 * following character and shown as <F1>, <S-Up> etc. Any other character
1676 * which is not printable shown in <> form.
1677 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1678 * If a character is displayed in one of these special ways, is also
1679 * highlighted (its highlight name is '8' in the p_hl variable).
1680 * Otherwise characters are not highlighted.
1681 * This function is used to show mappings, where we want to see how to type
1682 * the character/string -- webb
1683 */
1684 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001685msg_outtrans_special(
1686 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001687 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001688 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 char_u *str = strstart;
1691 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001692 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 int attr;
1694 int len;
1695
Bram Moolenaar8820b482017-03-16 17:23:31 +01001696 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 while (*str != NUL)
1698 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001699 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if ((str == strstart || str[1] == NUL) && *str == ' ')
1701 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001702 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 ++str;
1704 }
1705 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001706 text = (char *)str2special(&str, from);
1707 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001708 if (maxlen > 0 && retval + len >= maxlen)
1709 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001710 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001711 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001712 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 retval += len;
1714 }
1715 return retval;
1716}
1717
Bram Moolenaarbd743252010-10-20 21:23:33 +02001718#if defined(FEAT_EVAL) || defined(PROTO)
1719/*
1720 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1721 * strings, in an allocated string.
1722 */
1723 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001724str2special_save(
1725 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001726 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001727{
1728 garray_T ga;
1729 char_u *p = str;
1730
1731 ga_init2(&ga, 1, 40);
1732 while (*p != NUL)
1733 ga_concat(&ga, str2special(&p, is_lhs));
1734 ga_append(&ga, NUL);
1735 return (char_u *)ga.ga_data;
1736}
1737#endif
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739/*
1740 * Return the printable string for the key codes at "*sp".
1741 * Used for translating the lhs or rhs of a mapping to printable chars.
1742 * Advances "sp" to the next code.
1743 */
1744 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001745str2special(
1746 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001747 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 int c;
1750 static char_u buf[7];
1751 char_u *str = *sp;
1752 int modifiers = 0;
1753 int special = FALSE;
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (has_mbyte)
1756 {
1757 char_u *p;
1758
Bram Moolenaar85a20022019-12-21 18:25:54 +01001759 // Try to un-escape a multi-byte character. Return the un-escaped
1760 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 p = mb_unescape(sp);
1762 if (p != NULL)
1763 return p;
1764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 c = *str;
1767 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1768 {
1769 if (str[1] == KS_MODIFIER)
1770 {
1771 modifiers = str[2];
1772 str += 3;
1773 c = *str;
1774 }
1775 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1776 {
1777 c = TO_SPECIAL(str[1], str[2]);
1778 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001780 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 special = TRUE;
1782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001784 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001786 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001787
Bram Moolenaar85a20022019-12-21 18:25:54 +01001788 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001789 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1790 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001791 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001792 *sp = str + 1;
1793 return buf;
1794 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001795 // Since 'special' is TRUE the multi-byte character 'c' will be
1796 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001797 c = (*mb_ptr2char)(str);
1798 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001800 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001801 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
Bram Moolenaar85a20022019-12-21 18:25:54 +01001803 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1804 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (special || char2cells(c) > 1 || (from && c == ' '))
1806 return get_special_key_name(c, modifiers);
1807 buf[0] = c;
1808 buf[1] = NUL;
1809 return buf;
1810}
1811
1812/*
1813 * Translate a key sequence into special key names.
1814 */
1815 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001816str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 char_u *s;
1819
1820 *buf = NUL;
1821 while (*sp)
1822 {
1823 s = str2special(&sp, FALSE);
1824 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1825 STRCAT(buf, s);
1826 }
1827}
1828
1829/*
1830 * print line for :print or :list command
1831 */
1832 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001833msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 int c;
1836 int col = 0;
1837 int n_extra = 0;
1838 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001839 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001840 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001842 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001844 char_u *lead = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 int l;
1846 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaardf177f62005-02-22 08:39:57 +00001848 if (curwin->w_p_list)
1849 list = TRUE;
1850
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001851 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001853 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001854 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001855 {
1856 trail = s + STRLEN(s);
1857 while (trail > s && VIM_ISWHITE(trail[-1]))
1858 --trail;
1859 }
1860 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001861 if (curwin->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001862 {
1863 lead = s;
1864 while (VIM_ISWHITE(lead[0]))
1865 lead++;
1866 // in a line full of spaces all of them are treated as trailing
1867 if (*lead == NUL)
1868 lead = NULL;
1869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871
Bram Moolenaar85a20022019-12-21 18:25:54 +01001872 // output a space for an empty line, otherwise the line will be
1873 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001874 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 msg_putchar(' ');
1876
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001877 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001879 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
1881 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001882 if (n_extra == 0 && c_final)
1883 c = c_final;
1884 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 c = c_extra;
1886 else
1887 c = *p_extra++;
1888 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001889 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
1891 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001892 if (l >= MB_MAXBYTES)
1893 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001894 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001895 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001896 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001897 && (mb_ptr2char(s) == 160
1898 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001899 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001900 mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001901 buf[(*mb_ptr2len)(buf)] = NUL;
1902 }
1903 else
1904 {
1905 mch_memmove(buf, s, (size_t)l);
1906 buf[l] = NUL;
1907 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001908 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 s += l;
1910 continue;
1911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 else
1913 {
1914 attr = 0;
1915 c = *s++;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001916 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001918 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001919#ifdef FEAT_VARTABS
1920 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1921 curbuf->b_p_vts_array) - 1;
1922#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001924#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001925 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 c = ' ';
1928 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001929 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 else
1932 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001933 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1934 ? curwin->w_lcs_chars.tab3
1935 : curwin->w_lcs_chars.tab1;
1936 c_extra = curwin->w_lcs_chars.tab2;
1937 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001938 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001941 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001942 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001943 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001944 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001945 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001946 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
1948 p_extra = (char_u *)"";
1949 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001950 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001952 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001953 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 --s;
1955 }
1956 else if (c != NUL && (n = byte2cells(c)) > 1)
1957 {
1958 n_extra = n - 1;
1959 p_extra = transchar_byte(c);
1960 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001961 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001963 // Use special coloring to be able to distinguish <hex> from
1964 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001965 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001967 else if (c == ' ' && lead != NULL && s <= lead)
1968 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001969 c = curwin->w_lcs_chars.lead;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001970 attr = HL_ATTR(HLF_8);
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 else if (c == ' ' && trail != NULL && s > trail)
1973 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001974 c = curwin->w_lcs_chars.trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001975 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001977 else if (c == ' ' && list && curwin->w_lcs_chars.space != NUL)
Bram Moolenaar1510f992015-04-22 22:18:22 +02001978 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001979 c = curwin->w_lcs_chars.space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001980 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983
1984 if (c == NUL)
1985 break;
1986
1987 msg_putchar_attr(c, attr);
1988 col++;
1989 }
1990 msg_clr_eos();
1991}
1992
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993/*
1994 * Use screen_puts() to output one multi-byte character.
1995 * Return the pointer "s" advanced to the next character.
1996 */
1997 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001998screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 int cw;
2001
Bram Moolenaar85a20022019-12-21 18:25:54 +01002002 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 cw = (*mb_ptr2cells)(s);
2004 if (cw > 1 && (
2005#ifdef FEAT_RIGHTLEFT
2006 cmdmsg_rl ? msg_col <= 1 :
2007#endif
2008 msg_col == Columns - 1))
2009 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002010 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002011 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 return s;
2013 }
2014
2015 screen_puts_len(s, l, msg_row, msg_col, attr);
2016#ifdef FEAT_RIGHTLEFT
2017 if (cmdmsg_rl)
2018 {
2019 msg_col -= cw;
2020 if (msg_col == 0)
2021 {
2022 msg_col = Columns;
2023 ++msg_row;
2024 }
2025 }
2026 else
2027#endif
2028 {
2029 msg_col += cw;
2030 if (msg_col >= Columns)
2031 {
2032 msg_col = 0;
2033 ++msg_row;
2034 }
2035 }
2036 return s + l;
2037}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
2039/*
2040 * Output a string to the screen at position msg_row, msg_col.
2041 * Update msg_row and msg_col for the next message.
2042 */
2043 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002044msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002046 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047}
2048
2049 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002050msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002052 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053}
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055/*
2056 * Show a message in such a way that it always fits in the line. Cut out a
2057 * part in the middle and replace it with "..." when necessary.
2058 * Does not handle multi-byte characters!
2059 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002060 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002061msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 int slen = len;
2064 int room;
2065
2066 room = Columns - msg_col;
2067 if (len > room && room >= 20)
2068 {
2069 slen = (room - 3) / 2;
2070 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002071 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2074}
2075
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002076 void
2077msg_outtrans_long_attr(char_u *longstr, int attr)
2078{
2079 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2080}
2081
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082/*
2083 * Basic function for writing a message with highlight attributes.
2084 */
2085 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002086msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
2088 msg_puts_attr_len(s, -1, attr);
2089}
2090
2091/*
2092 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2093 * When "maxlen" is -1 there is no maximum length.
2094 * When "maxlen" is >= 0 the message is not put in the history.
2095 */
2096 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002097msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 /*
2100 * If redirection is on, also write to the redirection file.
2101 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002102 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
2104 /*
2105 * Don't print anything when using ":silent cmd".
2106 */
2107 if (msg_silent != 0)
2108 return;
2109
Bram Moolenaar85a20022019-12-21 18:25:54 +01002110 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if ((attr & MSG_HIST) && maxlen < 0)
2112 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002113 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 attr &= ~MSG_HIST;
2115 }
2116
Bram Moolenaare8070982019-10-12 17:07:06 +02002117 // When writing something to the screen after it has scrolled, requires a
2118 // wait-return prompt later. Needed when scrolling, resetting
2119 // need_wait_return after some prompt, and then outputting something
2120 // without scrolling
2121 // Not needed when only using CR to move the cursor.
2122 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002124 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
2126 /*
2127 * If there is no valid screen, use fprintf so we can see error messages.
2128 * If termcap is not active, we may be writing in an alternate console
2129 * window, cursor positioning may not work correctly (window size may be
2130 * different, e.g. for Win32 console) or we just don't know where the
2131 * cursor is.
2132 */
2133 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002134 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002135 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002136 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002139/*
2140 * The display part of msg_puts_attr_len().
2141 * May be called recursively to display scroll-back text.
2142 */
2143 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002144msg_puts_display(
2145 char_u *str,
2146 int maxlen,
2147 int attr,
2148 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002149{
2150 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 char_u *t_s = str; // string from "t_s" to "s" is still todo
2152 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002153 int l;
2154 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002155 char_u *sb_str = str;
2156 int sb_col = msg_col;
2157 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002158 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
2160 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002161 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
2163 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002164 * We are at the end of the screen line when:
2165 * - When outputting a newline.
2166 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002168 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#ifdef FEAT_RIGHTLEFT
2170 cmdmsg_rl
2171 ? (
2172 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002173 || (*s == TAB && msg_col <= 7)
2174 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 :
2176#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002177 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002180 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002182 /*
2183 * The screen is scrolled up when at the last row (some terminals
2184 * scroll automatically, some don't. To avoid problems we scroll
2185 * ourselves).
2186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002189 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
Bram Moolenaar85a20022019-12-21 18:25:54 +01002191 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if (msg_no_more && lines_left == 0)
2193 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
Bram Moolenaar85a20022019-12-21 18:25:54 +01002195 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002196 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002199 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 msg_col = Columns - 1;
2201
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002203 if (*s >= ' '
2204#ifdef FEAT_RIGHTLEFT
2205 && !cmdmsg_rl
2206#endif
2207 )
2208 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002209 if (has_mbyte)
2210 {
2211 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002212 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002213 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002214 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002215 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002216 s = screen_puts_mbyte(s, l, attr);
2217 }
2218 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002219 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002220 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002221 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002222 else
2223 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002224
2225 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002226 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002227 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2228
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002229 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 redraw_cmdline = TRUE;
2232 if (cmdline_row > 0 && !exmode_active)
2233 --cmdline_row;
2234
2235 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002236 * If screen is completely filled and 'more' is set then wait
2237 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002239 if (lines_left > 0)
2240 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002241 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 && !msg_no_more && !exmode_active)
2243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002245 if (do_more_prompt(NUL))
2246 s = confirm_msg_tail;
2247#else
2248 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
2250 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002251 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002253
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 // When we displayed a char in last column need to check if there
2255 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002256 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002257 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002260 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002263 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002264 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2265 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002266 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002267 t_puts(&t_col, t_s, s, attr);
2268
2269 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002271 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaar85a20022019-12-21 18:25:54 +01002273 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002275 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002276#ifdef FEAT_RIGHTLEFT
2277 if (cmdmsg_rl)
2278 msg_col = Columns - 1;
2279 else
2280#endif
2281 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 msg_row = Rows - 1;
2284 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002285 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 msg_col = 0;
2288 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002289 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 if (msg_col)
2292 --msg_col;
2293 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002294 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 do
2297 msg_screen_putchar(' ', attr);
2298 while (msg_col & 7);
2299 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002300 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002301 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 else
2303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (has_mbyte)
2305 {
2306 cw = (*mb_ptr2cells)(s);
2307 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002309 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002311 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313 else
2314 {
2315 cw = 1;
2316 l = 1;
2317 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002318
Bram Moolenaar85a20022019-12-21 18:25:54 +01002319 // When drawing from right to left or when a double-wide character
2320 // doesn't fit, draw a single character here. Otherwise collect
2321 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (
2323# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002324 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002326 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (l > 1)
2329 s = screen_puts_mbyte(s, l, attr) - 1;
2330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 msg_screen_putchar(*s, attr);
2332 }
2333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002335 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (t_col == 0)
2337 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 t_col += cw;
2339 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341 }
2342 ++s;
2343 }
2344
Bram Moolenaar85a20022019-12-21 18:25:54 +01002345 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002347 t_puts(&t_col, t_s, s, attr);
2348 if (p_more && !recurse)
2349 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 msg_check();
2352}
2353
2354/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002355 * Return TRUE when ":filter pattern" was used and "msg" does not match
2356 * "pattern".
2357 */
2358 int
2359message_filtered(char_u *msg)
2360{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002361 int match;
2362
Bram Moolenaare1004402020-10-24 20:49:43 +02002363 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002364 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002365 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2366 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002367}
2368
2369/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002370 * Scroll the screen up one line for displaying the next message line.
2371 */
2372 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002373msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002374{
2375#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002376 // Remove the cursor before scrolling, ScreenLines[] is going
2377 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002378 if (gui.in_use)
2379 gui_undraw_cursor();
2380#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002381 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002382 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002383 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002384 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002385
2386 if (!can_clear((char_u *)" "))
2387 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002388 // Scrolling up doesn't result in the right background. Set the
2389 // background here. It's not efficient, but avoids that we have to do
2390 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002391 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2392
Bram Moolenaar85a20022019-12-21 18:25:54 +01002393 // Also clear the last char of the last but one line if it was not
2394 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002395 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2396 screen_fill((int)Rows - 2, (int)Rows - 1,
2397 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2398 }
2399}
2400
2401/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002402 * Increment "msg_scrolled".
2403 */
2404 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002405inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002406{
2407#ifdef FEAT_EVAL
2408 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2409 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002410 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002411 char_u *tofree = NULL;
2412 int len;
2413
Bram Moolenaar85a20022019-12-21 18:25:54 +01002414 // v:scrollstart is empty, set it to the script/function name and line
2415 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002416 if (p == NULL)
2417 p = (char_u *)_("Unknown");
2418 else
2419 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002420 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002421 tofree = alloc(len);
2422 if (tofree != NULL)
2423 {
2424 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002425 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002426 p = tofree;
2427 }
2428 }
2429 set_vim_var_string(VV_SCROLLSTART, p, -1);
2430 vim_free(tofree);
2431 }
2432#endif
2433 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002434 if (must_redraw < VALID)
2435 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002436}
2437
2438/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002439 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2440 * store the displayed text and remember where screen lines start.
2441 */
2442typedef struct msgchunk_S msgchunk_T;
2443struct msgchunk_S
2444{
2445 msgchunk_T *sb_next;
2446 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002447 char sb_eol; // TRUE when line ends after this text
2448 int sb_msg_col; // column in which text starts
2449 int sb_attr; // text attributes
2450 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002451};
2452
Bram Moolenaar85a20022019-12-21 18:25:54 +01002453static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002454
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002455static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002456
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002457typedef enum {
2458 SB_CLEAR_NONE = 0,
2459 SB_CLEAR_ALL,
2460 SB_CLEAR_CMDLINE_BUSY,
2461 SB_CLEAR_CMDLINE_DONE
2462} sb_clear_T;
2463
Bram Moolenaar85a20022019-12-21 18:25:54 +01002464// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002465static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002466
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002467/*
2468 * Store part of a printed message for displaying when scrolling back.
2469 */
2470 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002471store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002472 char_u **sb_str, // start of string
2473 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002474 int attr,
2475 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002476 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002477{
2478 msgchunk_T *mp;
2479
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002480 if (do_clear_sb_text == SB_CLEAR_ALL
2481 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002482 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002483 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2484 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002485 }
2486
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002487 if (s > *sb_str)
2488 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002489 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002490 if (mp != NULL)
2491 {
2492 mp->sb_eol = finish;
2493 mp->sb_msg_col = *sb_col;
2494 mp->sb_attr = attr;
2495 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2496
2497 if (last_msgchunk == NULL)
2498 {
2499 last_msgchunk = mp;
2500 mp->sb_prev = NULL;
2501 }
2502 else
2503 {
2504 mp->sb_prev = last_msgchunk;
2505 last_msgchunk->sb_next = mp;
2506 last_msgchunk = mp;
2507 }
2508 mp->sb_next = NULL;
2509 }
2510 }
2511 else if (finish && last_msgchunk != NULL)
2512 last_msgchunk->sb_eol = TRUE;
2513
2514 *sb_str = s;
2515 *sb_col = 0;
2516}
2517
2518/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002519 * Finished showing messages, clear the scroll-back text on the next message.
2520 */
2521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002522may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002523{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002524 do_clear_sb_text = SB_CLEAR_ALL;
2525}
2526
2527/*
2528 * Starting to edit the command line, do not clear messages now.
2529 */
2530 void
2531sb_text_start_cmdline(void)
2532{
2533 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2534 msg_sb_eol();
2535}
2536
2537/*
2538 * Ending to edit the command line. Clear old lines but the last one later.
2539 */
2540 void
2541sb_text_end_cmdline(void)
2542{
2543 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002544}
2545
2546/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002547 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002548 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002549 * Called when redrawing the screen.
2550 */
2551 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002552clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002553{
2554 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002555 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002556
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002557 if (all)
2558 lastp = &last_msgchunk;
2559 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002560 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002561 if (last_msgchunk == NULL)
2562 return;
2563 lastp = &last_msgchunk->sb_prev;
2564 }
2565
2566 while (*lastp != NULL)
2567 {
2568 mp = (*lastp)->sb_prev;
2569 vim_free(*lastp);
2570 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002571 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002572}
2573
2574/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002575 * "g<" command.
2576 */
2577 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002578show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002579{
2580 msgchunk_T *mp;
2581
Bram Moolenaar85a20022019-12-21 18:25:54 +01002582 // Only show something if there is more than one line, otherwise it looks
2583 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002584 mp = msg_sb_start(last_msgchunk);
2585 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002586 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002587 else
2588 {
2589 do_more_prompt('G');
2590 wait_return(FALSE);
2591 }
2592}
2593
2594/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002595 * Move to the start of screen line in already displayed text.
2596 */
2597 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002598msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002599{
2600 msgchunk_T *mp = mps;
2601
2602 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2603 mp = mp->sb_prev;
2604 return mp;
2605}
2606
2607/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002608 * Mark the last message chunk as finishing the line.
2609 */
2610 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002611msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002612{
2613 if (last_msgchunk != NULL)
2614 last_msgchunk->sb_eol = TRUE;
2615}
2616
2617/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618 * Display a screen line from previously displayed text at row "row".
2619 * Returns a pointer to the text for the next line (can be NULL).
2620 */
2621 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002622disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002623{
2624 msgchunk_T *mp = smp;
2625 char_u *p;
2626
2627 for (;;)
2628 {
2629 msg_row = row;
2630 msg_col = mp->sb_msg_col;
2631 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002632 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002633 ++p;
2634 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2635 if (mp->sb_eol || mp->sb_next == NULL)
2636 break;
2637 mp = mp->sb_next;
2638 }
2639 return mp->sb_next;
2640}
2641
2642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 * Output any postponed text for msg_puts_attr_len().
2644 */
2645 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002646t_puts(
2647 int *t_col,
2648 char_u *t_s,
2649 char_u *s,
2650 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002652 // output postponed text
2653 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002655 msg_col += *t_col;
2656 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 // If the string starts with a composing character don't increment the
2658 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2660 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (msg_col >= Columns)
2662 {
2663 msg_col = 0;
2664 ++msg_row;
2665 }
2666}
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668/*
2669 * Returns TRUE when messages should be printed with mch_errmsg().
2670 * This is used when there is no valid screen, so we can see error messages.
2671 * If termcap is not active, we may be writing in an alternate console
2672 * window, cursor positioning may not work correctly (window size may be
2673 * different, e.g. for Win32 console) or we just don't know where the
2674 * cursor is.
2675 */
2676 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002677msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002680#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2681# ifdef VIMDLL
2682 || (!gui.in_use && !termcap_active)
2683# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#endif
2687 || (swapping_screen() && !termcap_active)
2688 );
2689}
2690
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002691/*
2692 * Print a message when there is no valid screen.
2693 */
2694 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002695msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002696{
2697 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002698 char_u *buf = NULL;
2699 char_u *p = s;
2700
Bram Moolenaar4f974752019-02-17 17:44:42 +01002701#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002702 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002703 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002704#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002705 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002706 {
2707 if (!(silent_mode && p_verbose == 0))
2708 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002709 // NL --> CR NL translation (for Unix, not for "--version")
2710 if (*s == NL)
2711 {
2712 int n = (int)(s - p);
2713
2714 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002715 if (buf != NULL)
2716 {
2717 memcpy(buf, p, n);
2718 if (!info_message)
2719 buf[n++] = CAR;
2720 buf[n++] = NL;
2721 buf[n++] = NUL;
2722 if (info_message) // informative message, not an error
2723 mch_msg((char *)buf);
2724 else
2725 mch_errmsg((char *)buf);
2726 vim_free(buf);
2727 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002728 p = s + 1;
2729 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002730 }
2731
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002732 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733#ifdef FEAT_RIGHTLEFT
2734 if (cmdmsg_rl)
2735 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002736 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002737 msg_col = Columns - 1;
2738 else
2739 --msg_col;
2740 }
2741 else
2742#endif
2743 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002744 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002745 msg_col = 0;
2746 else
2747 ++msg_col;
2748 }
2749 ++s;
2750 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002751
2752 if (*p != NUL && !(silent_mode && p_verbose == 0))
2753 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002754 int c = -1;
2755
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002756 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002757 {
2758 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002759 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002760 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002761 if (info_message)
2762 mch_msg((char *)p);
2763 else
2764 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002765 if (c != -1)
2766 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002767 }
2768
2769 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002770
Bram Moolenaar4f974752019-02-17 17:44:42 +01002771#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002772 if (!(silent_mode && p_verbose == 0))
2773 mch_settmode(TMODE_RAW);
2774#endif
2775}
2776
2777/*
2778 * Show the more-prompt and handle the user response.
2779 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002780 * When at hit-enter prompt "typed_char" is the already typed character,
2781 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2783 */
2784 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002785do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002786{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002787 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 int used_typed_char = typed_char;
2789 int oldState = State;
2790 int c;
2791#ifdef FEAT_CON_DIALOG
2792 int retval = FALSE;
2793#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002794 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 msgchunk_T *mp_last = NULL;
2796 msgchunk_T *mp;
2797 int i;
2798
Bram Moolenaar85a20022019-12-21 18:25:54 +01002799 // We get called recursively when a timer callback outputs a message. In
2800 // that case don't show another prompt. Also when at the hit-Enter prompt
2801 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002802 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002803 return FALSE;
2804 entered = TRUE;
2805
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002806 if (typed_char == 'G')
2807 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002808 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002809 mp_last = msg_sb_start(last_msgchunk);
2810 for (i = 0; i < Rows - 2 && mp_last != NULL
2811 && mp_last->sb_prev != NULL; ++i)
2812 mp_last = msg_sb_start(mp_last->sb_prev);
2813 }
2814
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002815 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002817 if (typed_char == NUL)
2818 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819 for (;;)
2820 {
2821 /*
2822 * Get a typed character directly from the user.
2823 */
2824 if (used_typed_char != NUL)
2825 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002826 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 used_typed_char = NUL;
2828 }
2829 else
2830 c = get_keystroke();
2831
2832#if defined(FEAT_MENU) && defined(FEAT_GUI)
2833 if (c == K_MENU)
2834 {
2835 int idx = get_menu_index(current_menu, ASKMORE);
2836
Bram Moolenaar85a20022019-12-21 18:25:54 +01002837 // Used a menu. If it starts with CTRL-Y, it must
2838 // be a "Copy" for the clipboard. Otherwise
2839 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002840 if (idx == MENU_INDEX_INVALID)
2841 continue;
2842 c = *current_menu->strings[idx];
2843 if (c != NUL && current_menu->strings[idx][1] != NUL)
2844 ins_typebuf(current_menu->strings[idx] + 1,
2845 current_menu->noremap[idx], 0, TRUE,
2846 current_menu->silent[idx]);
2847 }
2848#endif
2849
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002850 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 switch (c)
2852 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002853 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002854 case K_BS:
2855 case 'k':
2856 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002857 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 break;
2859
Bram Moolenaar85a20022019-12-21 18:25:54 +01002860 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861 case NL:
2862 case 'j':
2863 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002864 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002865 break;
2866
Bram Moolenaar85a20022019-12-21 18:25:54 +01002867 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002868 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002869 break;
2870
Bram Moolenaar85a20022019-12-21 18:25:54 +01002871 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002872 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 break;
2874
Bram Moolenaar85a20022019-12-21 18:25:54 +01002875 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002876 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002877 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 break;
2879
Bram Moolenaar85a20022019-12-21 18:25:54 +01002880 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002881 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882 case K_PAGEDOWN:
2883 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002884 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 break;
2886
Bram Moolenaar85a20022019-12-21 18:25:54 +01002887 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002888 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002889 break;
2890
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002892 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002893 lines_left = 999999;
2894 break;
2895
Bram Moolenaar85a20022019-12-21 18:25:54 +01002896 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002897#ifdef FEAT_CON_DIALOG
2898 if (!confirm_msg_used)
2899#endif
2900 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002901 // Since got_int is set all typeahead will be flushed, but we
2902 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002903 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002904#ifdef FEAT_TERMINAL
2905 skip_term_loop = TRUE;
2906#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002907 cmdline_row = Rows - 1; // put ':' on this line
2908 skip_redraw = TRUE; // skip redraw once
2909 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002910 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002911 // FALLTHROUGH
2912 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 case Ctrl_C:
2914 case ESC:
2915#ifdef FEAT_CON_DIALOG
2916 if (confirm_msg_used)
2917 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002918 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002919 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002920 }
2921 else
2922#endif
2923 {
2924 got_int = TRUE;
2925 quit_more = TRUE;
2926 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002927 // When there is some more output (wrapping line) display that
2928 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002929 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002930 break;
2931
2932#ifdef FEAT_CLIPBOARD
2933 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 // Strange way to allow copying (yanking) a modeless
2935 // selection at the more prompt. Use CTRL-Y,
2936 // because the same is used in Cmdline-mode and at the
2937 // hit-enter prompt. However, scrolling one line up
2938 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002939 if (clip_star.state == SELECT_DONE)
2940 clip_copy_modeless_selection(TRUE);
2941 continue;
2942#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002943 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 msg_moremsg(TRUE);
2945 continue;
2946 }
2947
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002948 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002949 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002950 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002951 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002952 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002953 if (mp_last == NULL)
2954 mp = msg_sb_start(last_msgchunk);
2955 else if (mp_last->sb_prev != NULL)
2956 mp = msg_sb_start(mp_last->sb_prev);
2957 else
2958 mp = NULL;
2959
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002961 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2962 ++i)
2963 mp = msg_sb_start(mp->sb_prev);
2964
2965 if (mp != NULL && mp->sb_prev != NULL)
2966 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002967 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002968 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002969 {
2970 if (mp == NULL || mp->sb_prev == NULL)
2971 break;
2972 mp = msg_sb_start(mp->sb_prev);
2973 if (mp_last == NULL)
2974 mp_last = msg_sb_start(last_msgchunk);
2975 else
2976 mp_last = msg_sb_start(mp_last->sb_prev);
2977 }
2978
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002979 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002980 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002981 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002982 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002983 (void)disp_sb_line(0, mp);
2984 }
2985 else
2986 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002987 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002988 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002989 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2990 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002991 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002992 ++msg_scrolled;
2993 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002994 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002995 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002996 }
2997 }
2998 else
2999 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003000 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003001 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003002 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003003 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003004 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003005 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003006 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3007 (int)Columns, ' ', ' ', 0);
3008 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003009 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003010 }
3011 }
3012
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003013 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003014 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003015 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003016 screen_fill((int)Rows - 1, (int)Rows, 0,
3017 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003018 msg_moremsg(FALSE);
3019 continue;
3020 }
3021
Bram Moolenaar85a20022019-12-21 18:25:54 +01003022 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003023 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003024 }
3025
3026 break;
3027 }
3028
Bram Moolenaar85a20022019-12-21 18:25:54 +01003029 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003030 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3031 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003032 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003033 if (quit_more)
3034 {
3035 msg_row = Rows - 1;
3036 msg_col = 0;
3037 }
3038#ifdef FEAT_RIGHTLEFT
3039 else if (cmdmsg_rl)
3040 msg_col = Columns - 1;
3041#endif
3042
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003043 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003044#ifdef FEAT_CON_DIALOG
3045 return retval;
3046#else
3047 return FALSE;
3048#endif
3049}
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3052
3053#ifdef mch_errmsg
3054# undef mch_errmsg
3055#endif
3056#ifdef mch_msg
3057# undef mch_msg
3058#endif
3059
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003060#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3061 static void
3062mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003064 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003065 DWORD nwrite = 0;
3066 DWORD mode = 0;
3067 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3068
3069 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3070 && (int)GetConsoleCP() != enc_codepage)
3071 {
3072 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3073
3074 WriteConsoleW(h, w, len, &nwrite, NULL);
3075 vim_free(w);
3076 }
3077 else
3078 {
3079 fprintf(stderr, "%s", str);
3080 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003081}
3082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003084/*
3085 * Give an error message. To be used when the screen hasn't been initialized
3086 * yet. When stderr can't be used, collect error messages until the GUI has
3087 * started and they can be displayed in a message box.
3088 */
3089 void
3090mch_errmsg(char *str)
3091{
3092#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3093 int len;
3094#endif
3095
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003096#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003097 // On Unix use stderr if it's a tty.
3098 // When not going to start the GUI also use stderr.
3099 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003101# ifdef UNIX
3102# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003104# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 isatty(2)
3106# endif
3107# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003108 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003109# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003110# endif
3111# ifdef FEAT_GUI
3112 !(gui.in_use || gui.starting)
3113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 )
3115 {
3116 fprintf(stderr, "%s", str);
3117 return;
3118 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003121#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3122# ifdef VIMDLL
3123 if (!(gui.in_use || gui.starting))
3124# endif
3125 {
3126 mch_errmsg_c(str);
3127 return;
3128 }
3129#endif
3130
3131#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003132 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 emsg_on_display = FALSE;
3134
3135 len = (int)STRLEN(str) + 1;
3136 if (error_ga.ga_growsize == 0)
3137 {
3138 error_ga.ga_growsize = 80;
3139 error_ga.ga_itemsize = 1;
3140 }
3141 if (ga_grow(&error_ga, len) == OK)
3142 {
3143 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3144 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003145# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003146 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 char_u *p;
3149
3150 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3151 for (;;)
3152 {
3153 p = vim_strchr(p, '\r');
3154 if (p == NULL)
3155 break;
3156 *p = ' ';
3157 }
3158 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003159# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003160 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164}
3165
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003166#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3167 static void
3168mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003170 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003171 DWORD nwrite = 0;
3172 DWORD mode;
3173 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3174
3175
3176 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3177 && (int)GetConsoleCP() != enc_codepage)
3178 {
3179 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3180
3181 WriteConsoleW(h, w, len, &nwrite, NULL);
3182 vim_free(w);
3183 }
3184 else
3185 {
3186 printf("%s", str);
3187 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003188}
3189#endif
3190
3191/*
3192 * Give a message. To be used when the screen hasn't been initialized yet.
3193 * When there is no tty, collect messages until the GUI has started and they
3194 * can be displayed in a message box.
3195 */
3196 void
3197mch_msg(char *str)
3198{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003199#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003200 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3201 // uses mch_errmsg() when started from the desktop.
3202 // When not going to start the GUI also use stdout.
3203 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003205# ifdef UNIX
3206# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003208# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003212 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003214# endif
3215# ifdef FEAT_GUI
3216 !(gui.in_use || gui.starting)
3217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 )
3219 {
3220 printf("%s", str);
3221 return;
3222 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003223#endif
3224
3225#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3226# ifdef VIMDLL
3227 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003229 {
3230 mch_msg_c(str);
3231 return;
3232 }
3233#endif
3234#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240/*
3241 * Put a character on the screen at the current message position and advance
3242 * to the next position. Only for printable ASCII!
3243 */
3244 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003245msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003247 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 screen_putchar(c, msg_row, msg_col, attr);
3249#ifdef FEAT_RIGHTLEFT
3250 if (cmdmsg_rl)
3251 {
3252 if (--msg_col == 0)
3253 {
3254 msg_col = Columns;
3255 ++msg_row;
3256 }
3257 }
3258 else
3259#endif
3260 {
3261 if (++msg_col >= Columns)
3262 {
3263 msg_col = 0;
3264 ++msg_row;
3265 }
3266 }
3267}
3268
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003269 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003270msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003272 int attr;
3273 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaar8820b482017-03-16 17:23:31 +01003275 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003276 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003278 screen_puts((char_u *)
3279 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3280 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282
3283/*
3284 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3285 * exmode_active.
3286 */
3287 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003288repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 if (State == ASKMORE)
3291 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003292 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 msg_row = Rows - 1;
3294 }
3295#ifdef FEAT_CON_DIALOG
3296 else if (State == CONFIRM)
3297 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003298 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 msg_row = Rows - 1;
3300 }
3301#endif
3302 else if (State == EXTERNCMD)
3303 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003304 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306 else if (State == HITRETURN || State == SETWSIZE)
3307 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003308 if (msg_row == Rows - 1)
3309 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003310 // Avoid drawing the "hit-enter" prompt below the previous one,
3311 // overwrite it. Esp. useful when regaining focus and a
3312 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003313 msg_didout = FALSE;
3314 msg_col = 0;
3315 msg_clr_eos();
3316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 hit_return_msg();
3318 msg_row = Rows - 1;
3319 }
3320}
3321
3322/*
3323 * msg_check_screen - check if the screen is initialized.
3324 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3325 * While starting the GUI the terminal codes will be set for the GUI, but the
3326 * output goes to the terminal. Don't use the terminal codes then.
3327 */
3328 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003329msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 if (!full_screen || !screen_valid(FALSE))
3332 return FALSE;
3333
3334 if (msg_row >= Rows)
3335 msg_row = Rows - 1;
3336 if (msg_col >= Columns)
3337 msg_col = Columns - 1;
3338 return TRUE;
3339}
3340
3341/*
3342 * Clear from current message position to end of screen.
3343 * Skip this when ":silent" was used, no need to clear for redirection.
3344 */
3345 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003346msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 if (msg_silent == 0)
3349 msg_clr_eos_force();
3350}
3351
3352/*
3353 * Clear from current message position to end of screen.
3354 * Note: msg_col is not updated, so we remember the end of the message
3355 * for msg_check().
3356 */
3357 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003358msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 if (msg_use_printf())
3361 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003362 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
3364 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003365 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003367 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 }
3370 else
3371 {
3372#ifdef FEAT_RIGHTLEFT
3373 if (cmdmsg_rl)
3374 {
3375 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3376 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3377 }
3378 else
3379#endif
3380 {
3381 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3382 ' ', ' ', 0);
3383 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3384 }
3385 }
3386}
3387
3388/*
3389 * Clear the command line.
3390 */
3391 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003392msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 msg_row = cmdline_row;
3395 msg_col = 0;
3396 msg_clr_eos_force();
3397}
3398
3399/*
3400 * end putting a message on the screen
3401 * call wait_return if the message does not fit in the available space
3402 * return TRUE if wait_return not called.
3403 */
3404 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003405msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
3407 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003408 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 * or the ruler option is set and we run into it,
3410 * we have to redraw the window.
3411 * Do not do this if we are abandoning the file or editing the command line.
3412 */
3413 if (!exiting && need_wait_return && !(State & CMDLINE))
3414 {
3415 wait_return(FALSE);
3416 return FALSE;
3417 }
3418 out_flush();
3419 return TRUE;
3420}
3421
3422/*
3423 * If the written message runs into the shown command or ruler, we have to
3424 * wait for hit-return and redraw the window later.
3425 */
3426 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003427msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 if (msg_row == Rows - 1 && msg_col >= sc_col)
3430 {
3431 need_wait_return = TRUE;
3432 redraw_cmdline = TRUE;
3433 }
3434}
3435
3436/*
3437 * May write a string to the redirection file.
3438 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3439 */
3440 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003441redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *s = str;
3444 static int cur_col = 0;
3445
Bram Moolenaar85a20022019-12-21 18:25:54 +01003446 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003447 if (redir_off)
3448 return;
3449
Bram Moolenaar85a20022019-12-21 18:25:54 +01003450 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003451 if (*p_vfile != NUL && verbose_fd == NULL)
3452 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003453
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003454 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003456 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (*s != '\n' && *s != '\r')
3458 {
3459 while (cur_col < msg_col)
3460 {
3461#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003462 if (redir_execute)
3463 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003464 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003466 else if (redir_vname)
3467 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003470 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003472 if (verbose_fd != NULL)
3473 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 ++cur_col;
3475 }
3476 }
3477
3478#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003479 if (redir_execute)
3480 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003481 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003483 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003484 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#endif
3486
Bram Moolenaar85a20022019-12-21 18:25:54 +01003487 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3489 {
3490#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003491 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003493 if (redir_fd != NULL)
3494 putc(*s, redir_fd);
3495 if (verbose_fd != NULL)
3496 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (*s == '\r' || *s == '\n')
3498 cur_col = 0;
3499 else if (*s == '\t')
3500 cur_col += (8 - cur_col % 8);
3501 else
3502 ++cur_col;
3503 ++s;
3504 }
3505
Bram Moolenaar85a20022019-12-21 18:25:54 +01003506 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 msg_col = cur_col;
3508 }
3509}
3510
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003511 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003512redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003513{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003514 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003515#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003516 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003517#endif
3518 ;
3519}
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003522 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003523 * Must always be called paired with verbose_leave()!
3524 */
3525 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003526verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003527{
3528 if (*p_vfile != NUL)
3529 ++msg_silent;
3530}
3531
3532/*
3533 * After giving verbose message.
3534 * Must always be called paired with verbose_enter()!
3535 */
3536 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003537verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003538{
3539 if (*p_vfile != NUL)
3540 if (--msg_silent < 0)
3541 msg_silent = 0;
3542}
3543
3544/*
3545 * Like verbose_enter() and set msg_scroll when displaying the message.
3546 */
3547 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003548verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003549{
3550 if (*p_vfile != NUL)
3551 ++msg_silent;
3552 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003553 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003554 msg_scroll = TRUE;
3555}
3556
3557/*
3558 * Like verbose_leave() and set cmdline_row when displaying the message.
3559 */
3560 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003561verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003562{
3563 if (*p_vfile != NUL)
3564 {
3565 if (--msg_silent < 0)
3566 msg_silent = 0;
3567 }
3568 else
3569 cmdline_row = msg_row;
3570}
3571
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003572/*
3573 * Called when 'verbosefile' is set: stop writing to the file.
3574 */
3575 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003576verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003577{
3578 if (verbose_fd != NULL)
3579 {
3580 fclose(verbose_fd);
3581 verbose_fd = NULL;
3582 }
3583 verbose_did_open = FALSE;
3584}
3585
3586/*
3587 * Open the file 'verbosefile'.
3588 * Return FAIL or OK.
3589 */
3590 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003591verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003592{
3593 if (verbose_fd == NULL && !verbose_did_open)
3594 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003595 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003596 verbose_did_open = TRUE;
3597
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003598 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003599 if (verbose_fd == NULL)
3600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003601 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003602 return FAIL;
3603 }
3604 }
3605 return OK;
3606}
3607
3608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 * Give a warning message (for searching).
3610 * Use 'w' highlighting and may repeat the message after redrawing
3611 */
3612 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003613give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003615 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 if (msg_silent != 0)
3617 return;
3618
Bram Moolenaar85a20022019-12-21 18:25:54 +01003619 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#ifdef FEAT_EVAL
3623 set_vim_var_string(VV_WARNINGMSG, message, -1);
3624#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003625 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003627 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 else
3629 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003630 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003631 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003632 msg_didout = FALSE; // overwrite this message
3633 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 --no_wait_return;
3637}
3638
Bram Moolenaar113e1072019-01-20 15:30:40 +01003639#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003640 void
3641give_warning2(char_u *message, char_u *a1, int hl)
3642{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003643 if (IObuff == NULL)
3644 {
3645 // Very early in initialisation and already something wrong, just give
3646 // the raw message so the user at least gets a hint.
3647 give_warning((char_u *)message, hl);
3648 }
3649 else
3650 {
3651 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3652 give_warning(IObuff, hl);
3653 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003654}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003655#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657/*
3658 * Advance msg cursor to column "col".
3659 */
3660 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003661msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003663 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003665 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 return;
3667 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003668 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003670#ifdef FEAT_RIGHTLEFT
3671 if (cmdmsg_rl)
3672 while (msg_col > Columns - col)
3673 msg_putchar(' ');
3674 else
3675#endif
3676 while (msg_col < col)
3677 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678}
3679
3680#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3681/*
3682 * Used for "confirm()" function, and the :confirm command prefix.
3683 * Versions which haven't got flexible dialogs yet, and console
3684 * versions, get this generic handler which uses the command line.
3685 *
3686 * type = one of:
3687 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3688 * title = title string (can be NULL for default)
3689 * (neither used in console dialogs at the moment)
3690 *
3691 * Format of the "buttons" string:
3692 * "Button1Name\nButton2Name\nButton3Name"
3693 * The first button should normally be the default/accept
3694 * The second button should be the 'Cancel' button
3695 * Other buttons- use your imagination!
3696 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3697 * different letter.
3698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003700do_dialog(
3701 int type UNUSED,
3702 char_u *title UNUSED,
3703 char_u *message,
3704 char_u *buttons,
3705 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003706 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3707 // otherwise
3708 int ex_cmd) // when TRUE pressing : accepts default and starts
3709 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 int oldState;
3712 int retval = 0;
3713 char_u *hotkeys;
3714 int c;
3715 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003716 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003719 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003721 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#endif
3723
3724#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003725 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3727 {
3728 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003729 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003730 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003731 need_wait_return = FALSE;
3732 emsg_on_display = FALSE;
3733 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaar85a20022019-12-21 18:25:54 +01003735 // Flush output to avoid that further messages and redrawing is done
3736 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 out_flush();
3738 gui_mch_update();
3739
3740 return c;
3741 }
3742#endif
3743
3744 oldState = State;
3745 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
Bram Moolenaar27321db2020-07-06 21:24:57 +02003748 // Ensure raw mode here.
3749 save_tmode = cur_tmode;
3750 settmode(TMODE_RAW);
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 /*
3753 * Since we wait for a keypress, don't make the
3754 * user press RETURN as well afterwards.
3755 */
3756 ++no_wait_return;
3757 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3758
3759 if (hotkeys != NULL)
3760 {
3761 for (;;)
3762 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003763 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 c = get_keystroke();
3765 switch (c)
3766 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003767 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 case NL:
3769 retval = dfltbutton;
3770 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003771 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case ESC:
3773 retval = 0;
3774 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003775 default: // Could be a hotkey?
3776 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003778 if (c == ':' && ex_cmd)
3779 {
3780 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003781 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003782 break;
3783 }
3784
Bram Moolenaar85a20022019-12-21 18:25:54 +01003785 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 c = MB_TOLOWER(c);
3787 retval = 1;
3788 for (i = 0; hotkeys[i]; ++i)
3789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (has_mbyte)
3791 {
3792 if ((*mb_ptr2char)(hotkeys + i) == c)
3793 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003794 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (hotkeys[i] == c)
3798 break;
3799 ++retval;
3800 }
3801 if (hotkeys[i])
3802 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003803 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 continue;
3805 }
3806 break;
3807 }
3808
3809 vim_free(hotkeys);
3810 }
3811
Bram Moolenaar27321db2020-07-06 21:24:57 +02003812 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 --no_wait_return;
3816 msg_end_prompt();
3817
3818 return retval;
3819}
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821/*
3822 * Copy one character from "*from" to "*to", taking care of multi-byte
3823 * characters. Return the length of the character in bytes.
3824 */
3825 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003826copy_char(
3827 char_u *from,
3828 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003829 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 int len;
3832 int c;
3833
3834 if (has_mbyte)
3835 {
3836 if (lowercase)
3837 {
3838 c = MB_TOLOWER((*mb_ptr2char)(from));
3839 return (*mb_char2bytes)(c, to);
3840 }
3841 else
3842 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003843 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 mch_memmove(to, from, (size_t)len);
3845 return len;
3846 }
3847 }
3848 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850 if (lowercase)
3851 *to = (char_u)TOLOWER_LOC(*from);
3852 else
3853 *to = *from;
3854 return 1;
3855 }
3856}
3857
3858/*
3859 * Format the dialog string, and display it at the bottom of
3860 * the screen. Return a string of hotkey chars (if defined) for
3861 * each 'button'. If a button has no hotkey defined, the first character of
3862 * the button is used.
3863 * The hotkeys can be multi-byte characters, but without combining chars.
3864 *
3865 * Returns an allocated string with hotkeys, or NULL for error.
3866 */
3867 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003868msg_show_console_dialog(
3869 char_u *message,
3870 char_u *buttons,
3871 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003874#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003875 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 char_u *hotk = NULL;
3877 char_u *msgp = NULL;
3878 char_u *hotkp = NULL;
3879 char_u *r;
3880 int copy;
3881#define HAS_HOTKEY_LEN 30
3882 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003883 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 int idx;
3885
3886 has_hotkey[0] = FALSE;
3887
3888 /*
3889 * First loop: compute the size of memory to allocate.
3890 * Second loop: copy to the allocated memory.
3891 */
3892 for (copy = 0; copy <= 1; ++copy)
3893 {
3894 r = buttons;
3895 idx = 0;
3896 while (*r)
3897 {
3898 if (*r == DLG_BUTTON_SEP)
3899 {
3900 if (copy)
3901 {
3902 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003903 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
Bram Moolenaar85a20022019-12-21 18:25:54 +01003905 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003907 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003910 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (dfltbutton)
3912 --dfltbutton;
3913
Bram Moolenaar85a20022019-12-21 18:25:54 +01003914 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3916 first_hotkey = TRUE;
3917 }
3918 else
3919 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003920 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3921 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if (idx < HAS_HOTKEY_LEN - 1)
3923 has_hotkey[++idx] = FALSE;
3924 }
3925 }
3926 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3927 {
3928 if (*r == DLG_HOTKEY_CHAR)
3929 ++r;
3930 first_hotkey = FALSE;
3931 if (copy)
3932 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003933 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 *msgp++ = *r;
3935 else
3936 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003937 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3939 msgp += copy_char(r, msgp, FALSE);
3940 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3941
Bram Moolenaar85a20022019-12-21 18:25:54 +01003942 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003943 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 }
3946 else
3947 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003948 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 if (idx < HAS_HOTKEY_LEN - 1)
3950 has_hotkey[idx] = TRUE;
3951 }
3952 }
3953 else
3954 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003955 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (copy)
3957 msgp += copy_char(r, msgp, FALSE);
3958 }
3959
Bram Moolenaar85a20022019-12-21 18:25:54 +01003960 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003961 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963
3964 if (copy)
3965 {
3966 *msgp++ = ':';
3967 *msgp++ = ' ';
3968 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 else
3971 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003972 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003973 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003974 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003975 + 3); // for the ": " and NUL
3976 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaar85a20022019-12-21 18:25:54 +01003978 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (!has_hotkey[0])
3980 {
3981 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003982 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984
3985 /*
3986 * Now allocate and load the strings
3987 */
3988 vim_free(confirm_msg);
3989 confirm_msg = alloc(len);
3990 if (confirm_msg == NULL)
3991 return NULL;
3992 *confirm_msg = NUL;
3993 hotk = alloc(lenhotkey);
3994 if (hotk == NULL)
3995 return NULL;
3996
3997 *confirm_msg = '\n';
3998 STRCPY(confirm_msg + 1, message);
3999
4000 msgp = confirm_msg + 1 + STRLEN(message);
4001 hotkp = hotk;
4002
Bram Moolenaar85a20022019-12-21 18:25:54 +01004003 // Define first default hotkey. Keep the hotkey string NUL
4004 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004005 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
Bram Moolenaar85a20022019-12-21 18:25:54 +01004007 // Remember where the choices start, displaying starts here when
4008 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 confirm_msg_tail = msgp;
4010 *msgp++ = '\n';
4011 }
4012 }
4013
4014 display_confirm_msg();
4015 return hotk;
4016}
4017
4018/*
4019 * Display the ":confirm" message. Also called when screen resized.
4020 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004021 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004022display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004024 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 ++confirm_msg_used;
4026 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004027 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 --confirm_msg_used;
4029}
4030
Bram Moolenaar85a20022019-12-21 18:25:54 +01004031#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
4033#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4034
4035 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004036vim_dialog_yesno(
4037 int type,
4038 char_u *title,
4039 char_u *message,
4040 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
4042 if (do_dialog(type,
4043 title == NULL ? (char_u *)_("Question") : title,
4044 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004045 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return VIM_YES;
4047 return VIM_NO;
4048}
4049
4050 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004051vim_dialog_yesnocancel(
4052 int type,
4053 char_u *title,
4054 char_u *message,
4055 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056{
4057 switch (do_dialog(type,
4058 title == NULL ? (char_u *)_("Question") : title,
4059 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004060 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
4062 case 1: return VIM_YES;
4063 case 2: return VIM_NO;
4064 }
4065 return VIM_CANCEL;
4066}
4067
4068 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004069vim_dialog_yesnoallcancel(
4070 int type,
4071 char_u *title,
4072 char_u *message,
4073 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
4075 switch (do_dialog(type,
4076 title == NULL ? (char_u *)"Question" : title,
4077 message,
4078 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004079 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 case 1: return VIM_YES;
4082 case 2: return VIM_NO;
4083 case 3: return VIM_ALL;
4084 case 4: return VIM_DISCARDALL;
4085 }
4086 return VIM_CANCEL;
4087}
4088
Bram Moolenaar85a20022019-12-21 18:25:54 +01004089#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004091#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004092static char *e_printf = N_("E766: Insufficient arguments for printf()");
4093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004094/*
4095 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4096 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004097 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004098tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099{
4100 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004101 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004102 int err = FALSE;
4103
4104 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004105 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004106 else
4107 {
4108 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004109 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110 if (err)
4111 n = 0;
4112 }
4113 return n;
4114}
4115
4116/*
4117 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004118 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004119 * are not converted to a string.
4120 * If "tofree" is not NULL echo_string() is used. All types are converted to
4121 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004122 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004123 */
4124 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004125tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004126{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004127 int idx = *idxp - 1;
4128 char *s = NULL;
4129 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130
4131 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004132 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133 else
4134 {
4135 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004136 if (tofree != NULL)
4137 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4138 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004139 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004140 }
4141 return s;
4142}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004143
4144# ifdef FEAT_FLOAT
4145/*
4146 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4147 */
4148 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004149tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004150{
4151 int idx = *idxp - 1;
4152 double f = 0;
4153
4154 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004155 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004156 else
4157 {
4158 ++*idxp;
4159 if (tvs[idx].v_type == VAR_FLOAT)
4160 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004161 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004162 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004163 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004164 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004165 }
4166 return f;
4167}
4168# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004169#endif
4170
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004171#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004172/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004173 * Return the representation of infinity for printf() function:
4174 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4175 */
4176 static const char *
4177infinity_str(int positive,
4178 char fmt_spec,
4179 int force_sign,
4180 int space_for_positive)
4181{
4182 static const char *table[] =
4183 {
4184 "-inf", "inf", "+inf", " inf",
4185 "-INF", "INF", "+INF", " INF"
4186 };
4187 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4188
4189 if (ASCII_ISUPPER(fmt_spec))
4190 idx += 4;
4191 return table[idx];
4192}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004193#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004194
4195/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004196 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004197 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004198 * consistency.
4199 *
4200 * This code is based on snprintf.c - a portable implementation of snprintf
4201 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004202 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004203 * The original code, including useful comments, can be found here:
4204 * http://www.ijs.si/software/snprintf/
4205 *
4206 * This snprintf() only supports the following conversion specifiers:
4207 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4208 * with flags: '-', '+', ' ', '0' and '#'.
4209 * An asterisk is supported for field width as well as precision.
4210 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004211 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004212 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004213 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004214 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004215 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004216 * The locale is not used, the string is used as a byte string. This is only
4217 * relevant for double-byte encodings where the second byte may be '%'.
4218 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004219 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4220 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004221 *
4222 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004223 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004224 * is greater or equal to "str_m", not all characters from the result
4225 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4226 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004227 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004228 */
4229
4230/*
4231 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004233 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004234 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004235 */
4236
Bram Moolenaar85a20022019-12-21 18:25:54 +01004237// When generating prototypes all of this is skipped, cproto doesn't
4238// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004239#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004240
Bram Moolenaar85a20022019-12-21 18:25:54 +01004241// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004242 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004243vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004244{
4245 va_list ap;
4246 int str_l;
4247 size_t len = STRLEN(str);
4248 size_t space;
4249
4250 if (str_m <= len)
4251 space = 0;
4252 else
4253 space = str_m - len;
4254 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004255 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004256 va_end(ap);
4257 return str_l;
4258}
Bram Moolenaara800b422010-06-27 01:15:55 +02004259
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004260 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004261vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004262{
4263 va_list ap;
4264 int str_l;
4265
4266 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004267 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004268 va_end(ap);
4269 return str_l;
4270}
4271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004273vim_vsnprintf(
4274 char *str,
4275 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004276 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004277 va_list ap)
4278{
4279 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4280}
4281
4282 int
4283vim_vsnprintf_typval(
4284 char *str,
4285 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004286 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004287 va_list ap,
4288 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004289{
4290 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004291 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004292 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004293
4294 if (p == NULL)
4295 p = "";
4296 while (*p != NUL)
4297 {
4298 if (*p != '%')
4299 {
4300 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004301 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004302
Bram Moolenaar85a20022019-12-21 18:25:54 +01004303 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004304 if (str_l < str_m)
4305 {
4306 size_t avail = str_m - str_l;
4307
4308 mch_memmove(str + str_l, p, n > avail ? avail : n);
4309 }
4310 p += n;
4311 str_l += n;
4312 }
4313 else
4314 {
4315 size_t min_field_width = 0, precision = 0;
4316 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4317 int alternate_form = 0, force_sign = 0;
4318
Bram Moolenaar85a20022019-12-21 18:25:54 +01004319 // If both the ' ' and '+' flags appear, the ' ' flag should be
4320 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004321 int space_for_positive = 1;
4322
Bram Moolenaar85a20022019-12-21 18:25:54 +01004323 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004324 char length_modifier = '\0';
4325
Bram Moolenaar85a20022019-12-21 18:25:54 +01004326 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004327# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004328# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4329 // That sounds reasonable to use as the maximum
4330 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004331# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004332# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004333# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004334 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004335
Bram Moolenaar85a20022019-12-21 18:25:54 +01004336 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004337 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004338
Bram Moolenaar85a20022019-12-21 18:25:54 +01004339 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004340 size_t str_arg_l;
4341
Bram Moolenaar85a20022019-12-21 18:25:54 +01004342 // unsigned char argument value - only defined for c conversion.
4343 // N.B. standard explicitly states the char argument for the c
4344 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004345 unsigned char uchar_arg;
4346
Bram Moolenaar85a20022019-12-21 18:25:54 +01004347 // number of zeros to be inserted for numeric conversions as
4348 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004349 size_t number_of_zeros_to_pad = 0;
4350
Bram Moolenaar85a20022019-12-21 18:25:54 +01004351 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004352 size_t zero_padding_insertion_ind = 0;
4353
Bram Moolenaar85a20022019-12-21 18:25:54 +01004354 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004355 char fmt_spec = '\0';
4356
Bram Moolenaar85a20022019-12-21 18:25:54 +01004357 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004358 char_u *tofree = NULL;
4359
4360
Bram Moolenaar85a20022019-12-21 18:25:54 +01004361 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004362
Bram Moolenaar85a20022019-12-21 18:25:54 +01004363 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004364 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4365 || *p == '#' || *p == '\'')
4366 {
4367 switch (*p)
4368 {
4369 case '0': zero_padding = 1; break;
4370 case '-': justify_left = 1; break;
4371 case '+': force_sign = 1; space_for_positive = 0; break;
4372 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004373 // If both the ' ' and '+' flags appear, the ' '
4374 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004375 break;
4376 case '#': alternate_form = 1; break;
4377 case '\'': break;
4378 }
4379 p++;
4380 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004381 // If the '0' and '-' flags both appear, the '0' flag should be
4382 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004383
Bram Moolenaar85a20022019-12-21 18:25:54 +01004384 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004385 if (*p == '*')
4386 {
4387 int j;
4388
4389 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004392 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393# endif
4394 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004395 if (j >= 0)
4396 min_field_width = j;
4397 else
4398 {
4399 min_field_width = -j;
4400 justify_left = 1;
4401 }
4402 }
4403 else if (VIM_ISDIGIT((int)(*p)))
4404 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004405 // size_t could be wider than unsigned int; make sure we treat
4406 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004407 unsigned int uj = *p++ - '0';
4408
4409 while (VIM_ISDIGIT((int)(*p)))
4410 uj = 10 * uj + (unsigned int)(*p++ - '0');
4411 min_field_width = uj;
4412 }
4413
Bram Moolenaar85a20022019-12-21 18:25:54 +01004414 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004415 if (*p == '.')
4416 {
4417 p++;
4418 precision_specified = 1;
4419 if (*p == '*')
4420 {
4421 int j;
4422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004425 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426# endif
4427 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004428 p++;
4429 if (j >= 0)
4430 precision = j;
4431 else
4432 {
4433 precision_specified = 0;
4434 precision = 0;
4435 }
4436 }
4437 else if (VIM_ISDIGIT((int)(*p)))
4438 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004439 // size_t could be wider than unsigned int; make sure we
4440 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004441 unsigned int uj = *p++ - '0';
4442
4443 while (VIM_ISDIGIT((int)(*p)))
4444 uj = 10 * uj + (unsigned int)(*p++ - '0');
4445 precision = uj;
4446 }
4447 }
4448
Bram Moolenaar85a20022019-12-21 18:25:54 +01004449 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004450 if (*p == 'h' || *p == 'l')
4451 {
4452 length_modifier = *p;
4453 p++;
4454 if (length_modifier == 'l' && *p == 'l')
4455 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004456 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004457 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004458 p++;
4459 }
4460 }
4461 fmt_spec = *p;
4462
Bram Moolenaar85a20022019-12-21 18:25:54 +01004463 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004464 switch (fmt_spec)
4465 {
4466 case 'i': fmt_spec = 'd'; break;
4467 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4468 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4469 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4470 default: break;
4471 }
4472
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004473# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004474 switch (fmt_spec)
4475 {
4476 case 'd': case 'u': case 'o': case 'x': case 'X':
4477 if (tvs != NULL && length_modifier == '\0')
4478 length_modifier = 'L';
4479 }
4480# endif
4481
Bram Moolenaar85a20022019-12-21 18:25:54 +01004482 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004483 switch (fmt_spec)
4484 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004485 // '%' and 'c' behave similar to 's' regarding flags and field
4486 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004487 case '%':
4488 case 'c':
4489 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004490 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004491 str_arg_l = 1;
4492 switch (fmt_spec)
4493 {
4494 case '%':
4495 str_arg = p;
4496 break;
4497
4498 case 'c':
4499 {
4500 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501
4502 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004504 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505# endif
4506 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004507 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004508 uchar_arg = (unsigned char)j;
4509 str_arg = (char *)&uchar_arg;
4510 break;
4511 }
4512
4513 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004514 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004515 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004516# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004517 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004518# endif
4519 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004520 if (str_arg == NULL)
4521 {
4522 str_arg = "[NULL]";
4523 str_arg_l = 6;
4524 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004525 // make sure not to address string beyond the specified
4526 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 else if (!precision_specified)
4528 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004529 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004530 else if (precision == 0)
4531 str_arg_l = 0;
4532 else
4533 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004534 // Don't put the #if inside memchr(), it can be a
4535 // macro.
4536 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004537 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004538 precision <= (size_t)0x7fffffffL ? precision
4539 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004540 str_arg_l = (q == NULL) ? precision
4541 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004542 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004543 if (fmt_spec == 'S')
4544 {
4545 if (min_field_width != 0)
4546 min_field_width += STRLEN(str_arg)
4547 - mb_string2cells((char_u *)str_arg, -1);
4548 if (precision)
4549 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004550 char_u *p1;
4551 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004552
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004553 for (p1 = (char_u *)str_arg; *p1;
4554 p1 += mb_ptr2len(p1))
4555 {
4556 i += (size_t)mb_ptr2cells(p1);
4557 if (i > precision)
4558 break;
4559 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004560 str_arg_l = precision = p1 - (char_u *)str_arg;
4561 }
4562 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004563 break;
4564
4565 default:
4566 break;
4567 }
4568 break;
4569
Bram Moolenaar91984b92016-08-16 21:58:41 +02004570 case 'd': case 'u':
4571 case 'b': case 'B':
4572 case 'o':
4573 case 'x': case 'X':
4574 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004575 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004576 // NOTE: the u, b, o, x, X and p conversion specifiers
4577 // imply the value is unsigned; d implies a signed
4578 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004579
Bram Moolenaar85a20022019-12-21 18:25:54 +01004580 // 0 if numeric argument is zero (or if pointer is
4581 // NULL for 'p'), +1 if greater than zero (or nonzero
4582 // for unsigned arguments), -1 if negative (unsigned
4583 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004584 int arg_sign = 0;
4585
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004586 // only set for length modifier h, or for no length
4587 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004588 int int_arg = 0;
4589 unsigned int uint_arg = 0;
4590
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004591 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004592 long int long_arg = 0;
4593 unsigned long int ulong_arg = 0;
4594
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004595 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004596 varnumber_T llong_arg = 0;
4597 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004598
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004599 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004600 uvarnumber_T bin_arg = 0;
4601
Bram Moolenaar85a20022019-12-21 18:25:54 +01004602 // pointer argument value -only defined for p
4603 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004604 void *ptr_arg = NULL;
4605
4606 if (fmt_spec == 'p')
4607 {
4608 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004611 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4612 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613# endif
4614 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004615 if (ptr_arg != NULL)
4616 arg_sign = 1;
4617 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004618 else if (fmt_spec == 'b' || fmt_spec == 'B')
4619 {
4620 bin_arg =
4621# if defined(FEAT_EVAL)
4622 tvs != NULL ?
4623 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4624# endif
4625 va_arg(ap, uvarnumber_T);
4626 if (bin_arg != 0)
4627 arg_sign = 1;
4628 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004629 else if (fmt_spec == 'd')
4630 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004631 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004632 switch (length_modifier)
4633 {
4634 case '\0':
4635 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004636 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004639 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004640# endif
4641 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004642 if (int_arg > 0)
4643 arg_sign = 1;
4644 else if (int_arg < 0)
4645 arg_sign = -1;
4646 break;
4647 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004650 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651# endif
4652 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004653 if (long_arg > 0)
4654 arg_sign = 1;
4655 else if (long_arg < 0)
4656 arg_sign = -1;
4657 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004658 case 'L':
4659 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004660# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004661 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004662# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004663 va_arg(ap, varnumber_T);
4664 if (llong_arg > 0)
4665 arg_sign = 1;
4666 else if (llong_arg < 0)
4667 arg_sign = -1;
4668 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004669 }
4670 }
4671 else
4672 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004673 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004674 switch (length_modifier)
4675 {
4676 case '\0':
4677 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004678 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004680 tvs != NULL ? (unsigned)
4681 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682# endif
4683 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004684 if (uint_arg != 0)
4685 arg_sign = 1;
4686 break;
4687 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004688 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004689# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004690 tvs != NULL ? (unsigned long)
4691 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692# endif
4693 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004694 if (ulong_arg != 0)
4695 arg_sign = 1;
4696 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004697 case 'L':
4698 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004699# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004700 tvs != NULL ? (uvarnumber_T)
4701 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004702# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004703 va_arg(ap, uvarnumber_T);
4704 if (ullong_arg != 0)
4705 arg_sign = 1;
4706 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004707 }
4708 }
4709
4710 str_arg = tmp;
4711 str_arg_l = 0;
4712
Bram Moolenaar85a20022019-12-21 18:25:54 +01004713 // NOTE:
4714 // For d, i, u, o, x, and X conversions, if precision is
4715 // specified, the '0' flag should be ignored. This is so
4716 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4717 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004718 if (precision_specified)
4719 zero_padding = 0;
4720 if (fmt_spec == 'd')
4721 {
4722 if (force_sign && arg_sign >= 0)
4723 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004724 // leave negative numbers for sprintf to handle, to
4725 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004726 }
4727 else if (alternate_form)
4728 {
4729 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004730 && (fmt_spec == 'b' || fmt_spec == 'B'
4731 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004732 {
4733 tmp[str_arg_l++] = '0';
4734 tmp[str_arg_l++] = fmt_spec;
4735 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004736 // alternate form should have no effect for p
4737 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004738 }
4739
4740 zero_padding_insertion_ind = str_arg_l;
4741 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004742 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 if (precision == 0 && arg_sign == 0)
4744 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004745 // When zero value is formatted with an explicit
4746 // precision 0, the resulting formatted string is
4747 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004748 }
4749 else
4750 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004751 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004752 int f_l = 0;
4753
Bram Moolenaar85a20022019-12-21 18:25:54 +01004754 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004755 f[f_l++] = '%';
4756 if (!length_modifier)
4757 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004758 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004759 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004760# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004761 f[f_l++] = 'I';
4762 f[f_l++] = '6';
4763 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004764# else
4765 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004766 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004767# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004768 }
4769 else
4770 f[f_l++] = length_modifier;
4771 f[f_l++] = fmt_spec;
4772 f[f_l++] = '\0';
4773
4774 if (fmt_spec == 'p')
4775 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004776 else if (fmt_spec == 'b' || fmt_spec == 'B')
4777 {
4778 char b[8 * sizeof(uvarnumber_T)];
4779 size_t b_l = 0;
4780 uvarnumber_T bn = bin_arg;
4781
4782 do
4783 {
4784 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4785 bn >>= 1;
4786 }
4787 while (bn != 0);
4788
4789 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4790 str_arg_l += b_l;
4791 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004792 else if (fmt_spec == 'd')
4793 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004794 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004795 switch (length_modifier)
4796 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004797 case '\0': str_arg_l += sprintf(
4798 tmp + str_arg_l, f,
4799 int_arg);
4800 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004801 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004802 tmp + str_arg_l, f,
4803 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004804 break;
4805 case 'l': str_arg_l += sprintf(
4806 tmp + str_arg_l, f, long_arg);
4807 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004808 case 'L': str_arg_l += sprintf(
4809 tmp + str_arg_l, f, llong_arg);
4810 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004811 }
4812 }
4813 else
4814 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004815 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004816 switch (length_modifier)
4817 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004818 case '\0': str_arg_l += sprintf(
4819 tmp + str_arg_l, f,
4820 uint_arg);
4821 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004822 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004823 tmp + str_arg_l, f,
4824 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004825 break;
4826 case 'l': str_arg_l += sprintf(
4827 tmp + str_arg_l, f, ulong_arg);
4828 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004829 case 'L': str_arg_l += sprintf(
4830 tmp + str_arg_l, f, ullong_arg);
4831 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004832 }
4833 }
4834
Bram Moolenaar85a20022019-12-21 18:25:54 +01004835 // include the optional minus sign and possible
4836 // "0x" in the region before the zero padding
4837 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004838 if (zero_padding_insertion_ind < str_arg_l
4839 && tmp[zero_padding_insertion_ind] == '-')
4840 zero_padding_insertion_ind++;
4841 if (zero_padding_insertion_ind + 1 < str_arg_l
4842 && tmp[zero_padding_insertion_ind] == '0'
4843 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4844 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4845 zero_padding_insertion_ind += 2;
4846 }
4847
4848 {
4849 size_t num_of_digits = str_arg_l
4850 - zero_padding_insertion_ind;
4851
4852 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004853 // unless zero is already the first
4854 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004855 && !(zero_padding_insertion_ind < str_arg_l
4856 && tmp[zero_padding_insertion_ind] == '0'))
4857 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004858 // assure leading zero for alternate-form
4859 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004860 if (!precision_specified
4861 || precision < num_of_digits + 1)
4862 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004863 // precision is increased to force the
4864 // first character to be zero, except if a
4865 // zero value is formatted with an
4866 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004867 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004868 }
4869 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004870 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004871 if (num_of_digits < precision)
4872 number_of_zeros_to_pad = precision - num_of_digits;
4873 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004874 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004875 if (!justify_left && zero_padding)
4876 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004877 int n = (int)(min_field_width - (str_arg_l
4878 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004879 if (n > 0)
4880 number_of_zeros_to_pad += n;
4881 }
4882 break;
4883 }
4884
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004885# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004886 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004887 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004888 case 'e':
4889 case 'E':
4890 case 'g':
4891 case 'G':
4892 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004893 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004894 double f;
4895 double abs_f;
4896 char format[40];
4897 int l;
4898 int remove_trailing_zeroes = FALSE;
4899
4900 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004901# if defined(FEAT_EVAL)
4902 tvs != NULL ? tv_float(tvs, &arg_idx) :
4903# endif
4904 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004905 abs_f = f < 0 ? -f : f;
4906
4907 if (fmt_spec == 'g' || fmt_spec == 'G')
4908 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004909 // Would be nice to use %g directly, but it prints
4910 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004911 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4912 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004913 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004914 else
4915 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4916 remove_trailing_zeroes = TRUE;
4917 }
4918
Bram Moolenaar04186092016-08-29 21:55:35 +02004919 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004920# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004921 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004922# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004923 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004924# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004925 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004926 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004927 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004928 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4929 force_sign, space_for_positive));
4930 str_arg_l = STRLEN(tmp);
4931 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932 }
4933 else
4934 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004935 if (isnan(f))
4936 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004937 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004938 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4939 : "nan");
4940 str_arg_l = 3;
4941 zero_padding = 0;
4942 }
4943 else if (isinf(f))
4944 {
4945 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4946 force_sign, space_for_positive));
4947 str_arg_l = STRLEN(tmp);
4948 zero_padding = 0;
4949 }
4950 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004951 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004952 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004953 format[0] = '%';
4954 l = 1;
4955 if (force_sign)
4956 format[l++] = space_for_positive ? ' ' : '+';
4957 if (precision_specified)
4958 {
4959 size_t max_prec = TMP_LEN - 10;
4960
Bram Moolenaar85a20022019-12-21 18:25:54 +01004961 // Make sure we don't get more digits than we
4962 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004963 if ((fmt_spec == 'f' || fmt_spec == 'F')
4964 && abs_f > 1.0)
4965 max_prec -= (size_t)log10(abs_f);
4966 if (precision > max_prec)
4967 precision = max_prec;
4968 l += sprintf(format + l, ".%d", (int)precision);
4969 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004970 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004971 format[l + 1] = NUL;
4972
Bram Moolenaare9997822016-08-28 16:03:38 +02004973 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004974 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004975
Bram Moolenaara7241f52008-06-24 20:39:31 +00004976 if (remove_trailing_zeroes)
4977 {
4978 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004979 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004980
Bram Moolenaar85a20022019-12-21 18:25:54 +01004981 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004982 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004983 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004984 else
4985 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004986 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004987 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004988 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004989 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004990 // Remove superfluous '+' and leading
4991 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004992 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004993 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004994 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004995 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004996 --str_arg_l;
4997 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004998 i = (tp[1] == '-') ? 2 : 1;
4999 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005000 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005001 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005002 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005003 --str_arg_l;
5004 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005005 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005006 }
5007 }
5008
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005009 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005010 // Remove trailing zeroes, but keep the one
5011 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005012 while (tp > tmp + 2 && *tp == '0'
5013 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005014 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005015 STRMOVE(tp, tp + 1);
5016 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005017 --str_arg_l;
5018 }
5019 }
5020 else
5021 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005022 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005023
Bram Moolenaar85a20022019-12-21 18:25:54 +01005024 // Be consistent: some printf("%e") use 1.0e+12
5025 // and some 1.0e+012. Remove one zero in the last
5026 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005027 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005028 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005029 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5030 && tp[2] == '0'
5031 && vim_isdigit(tp[3])
5032 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005033 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005034 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005035 --str_arg_l;
5036 }
5037 }
5038 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005039 if (zero_padding && min_field_width > str_arg_l
5040 && (tmp[0] == '-' || force_sign))
5041 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005042 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02005043 number_of_zeros_to_pad = min_field_width - str_arg_l;
5044 zero_padding_insertion_ind = 1;
5045 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005046 str_arg = tmp;
5047 break;
5048 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005049# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005050
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005051 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01005052 // unrecognized conversion specifier, keep format string
5053 // as-is
5054 zero_padding = 0; // turn zero padding off for non-numeric
5055 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005056 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01005057 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005058
Bram Moolenaar85a20022019-12-21 18:25:54 +01005059 // discard the unrecognized conversion, just keep *
5060 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005061 str_arg = p;
5062 str_arg_l = 0;
5063 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005064 str_arg_l++; // include invalid conversion specifier
5065 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005066 break;
5067 }
5068
5069 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005070 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005071
Bram Moolenaar85a20022019-12-21 18:25:54 +01005072 // insert padding to the left as requested by min_field_width;
5073 // this does not include the zero padding in case of numerical
5074 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005075 if (!justify_left)
5076 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005077 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005078 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005079
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005080 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005081 {
5082 if (str_l < str_m)
5083 {
5084 size_t avail = str_m - str_l;
5085
5086 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005087 (size_t)pn > avail ? avail
5088 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005089 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005090 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005091 }
5092 }
5093
Bram Moolenaar85a20022019-12-21 18:25:54 +01005094 // zero padding as requested by the precision or by the minimal
5095 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005096 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005097 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005098 // will not copy first part of numeric right now, *
5099 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005100 zero_padding_insertion_ind = 0;
5101 }
5102 else
5103 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005104 // insert first part of numerics (sign or '0x') before zero
5105 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005106 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005107
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005108 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005109 {
5110 if (str_l < str_m)
5111 {
5112 size_t avail = str_m - str_l;
5113
5114 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005115 (size_t)zn > avail ? avail
5116 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005117 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005118 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005119 }
5120
Bram Moolenaar85a20022019-12-21 18:25:54 +01005121 // insert zero padding as requested by the precision or min
5122 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005123 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005124 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005125 {
5126 if (str_l < str_m)
5127 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005128 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005129
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005130 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005131 (size_t)zn > avail ? avail
5132 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005133 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005134 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005135 }
5136 }
5137
Bram Moolenaar85a20022019-12-21 18:25:54 +01005138 // insert formatted string
5139 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005140 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005141 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005142
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005143 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005144 {
5145 if (str_l < str_m)
5146 {
5147 size_t avail = str_m - str_l;
5148
5149 mch_memmove(str + str_l,
5150 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005151 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005152 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005153 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005154 }
5155 }
5156
Bram Moolenaar85a20022019-12-21 18:25:54 +01005157 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005158 if (justify_left)
5159 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005160 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005161 int pn = (int)(min_field_width
5162 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005163
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005164 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005165 {
5166 if (str_l < str_m)
5167 {
5168 size_t avail = str_m - str_l;
5169
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005170 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005171 (size_t)pn > avail ? avail
5172 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005173 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005174 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005175 }
5176 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005177 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005178 }
5179 }
5180
5181 if (str_m > 0)
5182 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005183 // make sure the string is nul-terminated even at the expense of
5184 // overwriting the last character (shouldn't happen, but just in case)
5185 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005186 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5187 }
5188
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005189 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005190 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005191
Bram Moolenaar85a20022019-12-21 18:25:54 +01005192 // Return the number of characters formatted (excluding trailing nul
5193 // character), that is, the number of characters that would have been
5194 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005195 return (int)str_l;
5196}
5197
Bram Moolenaar85a20022019-12-21 18:25:54 +01005198#endif // PROTO