blob: 07c3943ab4dab3d913eac909201dded1ae0cdb2f [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 Moolenaar79815f12016-07-09 17:07:29 +0200688 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200690 msg_start();
691 p = get_emsg_source();
692 if (p != NULL)
693 {
694 STRCAT(p, "\n");
695 redir_write(p, -1);
696 vim_free(p);
697 }
698 p = get_emsg_lnum();
699 if (p != NULL)
700 {
701 STRCAT(p, "\n");
702 redir_write(p, -1);
703 vim_free(p);
704 }
705 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100707#ifdef FEAT_EVAL
708 // Only increment did_emsg_def when :silent! wasn't used inside the
709 // :def function.
710 if (emsg_silent == emsg_silent_def)
711 ++did_emsg_def;
712#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100713#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200714 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 return TRUE;
717 }
718
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100719 ex_exitval = 1;
720
Bram Moolenaar85a20022019-12-21 18:25:54 +0100721 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 msg_silent = 0;
723 cmd_silent = FALSE;
724
Bram Moolenaar85a20022019-12-21 18:25:54 +0100725 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 ++global_busy;
727
728 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100729 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200731 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100732 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200733#ifdef FEAT_EVAL
734 did_uncaught_emsg = TRUE;
735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737
Bram Moolenaar85a20022019-12-21 18:25:54 +0100738 emsg_on_display = TRUE; // remember there is an error message
739 ++msg_scroll; // don't overwrite a previous message
740 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000741 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100742 need_wait_return = TRUE; // needed in case emsg() is called after
743 // wait_return has reset need_wait_return
744 // and a redraw is expected because
745 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
Bram Moolenaar958dc692016-12-01 15:34:12 +0100747#ifdef FEAT_JOB_CHANNEL
748 emsg_to_channel_log = TRUE;
749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 /*
751 * Display name and line number for the source of the error.
752 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000753 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 /*
756 * Display the error message itself.
757 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100758 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100759 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100760
761#ifdef FEAT_JOB_CHANNEL
762 emsg_to_channel_log = FALSE;
763#endif
764 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765}
766
767/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100768 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 */
770 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100771emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100773 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100774 if (!emsg_not_now())
775 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100776 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777}
778
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100779#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100780/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100781 * Print an error message with format string and variable arguments.
782 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100783 */
784 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100785semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100786{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200787 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100788 if (!emsg_not_now())
789 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200790 if (IObuff == NULL)
791 {
792 // Very early in initialisation and already something wrong, just
793 // give the raw message so the user at least gets a hint.
794 return emsg_core((char_u *)s);
795 }
796 else
797 {
798 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200800 va_start(ap, s);
801 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
802 va_end(ap);
803 return emsg_core(IObuff);
804 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200806 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100808#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100809
810/*
811 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
812 * defined. It is used for internal errors only, so that they can be
813 * detected when fuzzing vim.
814 */
815 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100816iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100817{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818 if (!emsg_not_now())
819 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100820#ifdef ABORT_ON_INTERNAL_ERROR
821 abort();
822#endif
823}
824
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100825#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100826/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100828 * defined. It is used for internal errors only, so that they can be
829 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100831 */
832 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100833siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100834{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100835 if (!emsg_not_now())
836 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200837 if (IObuff == NULL)
838 {
839 // Very early in initialisation and already something wrong, just
840 // give the raw message so the user at least gets a hint.
841 emsg_core((char_u *)s);
842 }
843 else
844 {
845 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200847 va_start(ap, s);
848 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
849 va_end(ap);
850 emsg_core(IObuff);
851 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100852 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100853# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100854 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100855# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100856}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100857#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100858
859/*
860 * Give an "Internal error" message.
861 */
862 void
863internal_error(char *where)
864{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100866}
867
Bram Moolenaardd589232020-02-29 17:38:12 +0100868/*
869 * Like internal_error() but do not call abort(), to avoid tests using
870 * test_unknown() and test_void() causing Vim to exit.
871 */
872 void
873internal_error_no_abort(char *where)
874{
875 semsg(_(e_intern2), where);
876}
877
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaardf177f62005-02-22 08:39:57 +0000880 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100881emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000882{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100883 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000884}
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 * Give an error message which contains %s for "name[len]".
888 */
889 void
890emsg_namelen(char *msg, char_u *name, int len)
891{
892 char_u *copy = vim_strnsave((char_u *)name, len);
893
894 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100895 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896}
897
898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * Like msg(), but truncate to a single line if p_shm contains 't', or when
900 * "force" is TRUE. This truncates in another way as for normal messages.
901 * Careful: The string may be changed by msg_may_trunc()!
902 * Returns a pointer to the printed message, if wait_return() not called.
903 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100904 char *
905msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100908 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar85a20022019-12-21 18:25:54 +0100910 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100911 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaar32526b32019-01-19 17:43:09 +0100913 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
915 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100916 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 msg_hist_off = FALSE;
918
919 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100920 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return NULL;
922}
923
924/*
925 * Check if message "s" should be truncated at the start (for filenames).
926 * Return a pointer to where the truncated message starts.
927 * Note: May change the message by replacing a character with '<'.
928 */
929 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100930msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 int n;
933 int room;
934
935 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
936 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
937 && (n = (int)STRLEN(s) - room) > 0)
938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (has_mbyte)
940 {
941 int size = vim_strsize(s);
942
Bram Moolenaar85a20022019-12-21 18:25:54 +0100943 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000944 if (size <= room)
945 return s;
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 for (n = 0; size >= room; )
948 {
949 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000950 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952 --n;
953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 s += n;
955 *s = '<';
956 }
957 return s;
958}
959
960 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100961add_msg_hist(
962 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100963 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100964 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 struct msg_hist *p;
967
968 if (msg_hist_off || msg_silent != 0)
969 return;
970
Bram Moolenaar85a20022019-12-21 18:25:54 +0100971 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000972 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000973 (void)delete_first_msg();
974
Bram Moolenaar85a20022019-12-21 18:25:54 +0100975 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200976 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (p != NULL)
978 {
979 if (len < 0)
980 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100981 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 while (len > 0 && *s == '\n')
983 {
984 ++s;
985 --len;
986 }
987 while (len > 0 && s[len - 1] == '\n')
988 --len;
989 p->msg = vim_strnsave(s, len);
990 p->next = NULL;
991 p->attr = attr;
992 if (last_msg_hist != NULL)
993 last_msg_hist->next = p;
994 last_msg_hist = p;
995 if (first_msg_hist == NULL)
996 first_msg_hist = last_msg_hist;
997 ++msg_hist_len;
998 }
999}
1000
1001/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001002 * Delete the first (oldest) message from the history.
1003 * Returns FAIL if there are no messages.
1004 */
1005 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001006delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001007{
1008 struct msg_hist *p;
1009
1010 if (msg_hist_len <= 0)
1011 return FAIL;
1012 p = first_msg_hist;
1013 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001014 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001015 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001016 vim_free(p->msg);
1017 vim_free(p);
1018 --msg_hist_len;
1019 return OK;
1020}
1021
1022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 * ":messages" command.
1024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001026ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 struct msg_hist *p;
1029 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001030 int c = 0;
1031
1032 if (STRCMP(eap->arg, "clear") == 0)
1033 {
1034 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1035
1036 while (msg_hist_len > keep)
1037 (void)delete_first_msg();
1038 return;
1039 }
1040
1041 if (*eap->arg != NUL)
1042 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001043 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001044 return;
1045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047 msg_hist_off = TRUE;
1048
Bram Moolenaar451f8492016-04-14 17:16:22 +02001049 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001050 if (eap->addr_count != 0)
1051 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001052 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001053 for (; p != NULL && !got_int; p = p->next)
1054 c++;
1055
1056 c -= eap->line2;
1057
Bram Moolenaar85a20022019-12-21 18:25:54 +01001058 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001059 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1060 p = p->next, c--);
1061 }
1062
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001063 if (p == first_msg_hist)
1064 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001065#ifdef FEAT_MULTI_LANG
1066 s = get_mess_lang();
1067#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001068 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001069#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001070 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001071 // The next comment is extracted by xgettext and put in po file for
1072 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001073 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001074 // Translator: Please replace the name and email address
1075 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001076 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001077 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001078 }
1079
Bram Moolenaar85a20022019-12-21 18:25:54 +01001080 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001081 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 msg_hist_off = FALSE;
1086}
1087
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001088#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089/*
1090 * Call this after prompting the user. This will avoid a hit-return message
1091 * and a delay.
1092 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001093 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001094msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
1096 need_wait_return = FALSE;
1097 emsg_on_display = FALSE;
1098 cmdline_row = msg_row;
1099 msg_col = 0;
1100 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001101 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102}
1103#endif
1104
1105/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001106 * Wait for the user to hit a key (normally Enter).
1107 * If "redraw" is TRUE, clear and redraw the screen.
1108 * If "redraw" is FALSE, just redraw the screen.
1109 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 */
1111 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001112wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int c;
1115 int oldState;
1116 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001118 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001119 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121 if (redraw == TRUE)
1122 must_redraw = CLEAR;
1123
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 // If using ":silent cmd", don't wait for a return. Also don't set
1125 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (msg_silent != 0)
1127 return;
1128
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001129 /*
1130 * When inside vgetc(), we can't wait for a typed character at all.
1131 * With the global command (and some others) we only need one return at
1132 * the end. Adjust cmdline_row to avoid the next message overwriting the
1133 * last one.
1134 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001135 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001137 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (no_wait_return)
1139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (!exmode_active)
1141 cmdline_row = msg_row;
1142 return;
1143 }
1144
Bram Moolenaar85a20022019-12-21 18:25:54 +01001145 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 oldState = State;
1147 if (quit_more)
1148 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001149 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 quit_more = FALSE;
1151 got_int = FALSE;
1152 }
1153 else if (exmode_active)
1154 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001155 msg_puts(" "); // make sure the cursor is on the right line
1156 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 got_int = FALSE;
1158 }
1159 else
1160 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001161 // Make sure the hit-return prompt is on screen when 'guioptions' was
1162 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 screenalloc(FALSE);
1164
1165 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001170 cmdline_row = msg_row;
1171
Bram Moolenaar85a20022019-12-21 18:25:54 +01001172 // Avoid the sequence that the user types ":" at the hit-return prompt
1173 // to start an Ex command, but the file-changed dialog gets in the
1174 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001175 if (need_check_timestamps)
1176 check_timestamps(FALSE);
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 hit_return_msg();
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 do
1181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001182 // Remember "got_int", if it is set vgetc() probably returns a
1183 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001185
Bram Moolenaar85a20022019-12-21 18:25:54 +01001186 // Don't do mappings here, we put the character back in the
1187 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001188 ++no_mapping;
1189 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001190
Bram Moolenaar85a20022019-12-21 18:25:54 +01001191 // Temporarily disable Recording. If Recording is active, the
1192 // character will be recorded later, since it will be added to the
1193 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001194 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001195 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001196 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001197 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001199 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001201 --no_mapping;
1202 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001203 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001204 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001207 // Strange way to allow copying (yanking) a modeless selection at
1208 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1209 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1211 {
1212 clip_copy_modeless_selection(TRUE);
1213 c = K_IGNORE;
1214 }
1215#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001216
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001217 /*
1218 * Allow scrolling back in the messages.
1219 * Also accept scroll-down commands when messages fill the screen,
1220 * to avoid that typing one 'j' too many makes the messages
1221 * disappear.
1222 */
1223 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001224 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001225 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1226 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001227 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001228 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001229 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001230 do_more_prompt(c);
1231 else
1232 {
1233 msg_didout = FALSE;
1234 c = K_IGNORE;
1235 msg_col =
1236#ifdef FEAT_RIGHTLEFT
1237 cmdmsg_rl ? Columns - 1 :
1238#endif
1239 0;
1240 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001241 if (quit_more)
1242 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001243 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001244 quit_more = FALSE;
1245 got_int = FALSE;
1246 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001247 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001248 {
1249 c = K_IGNORE;
1250 hit_return_msg();
1251 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001252 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001253 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001254 && (c == 'j' || c == 'd' || c == 'f'
1255 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001256 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 } while ((had_got_int && c == Ctrl_C)
1259 || c == K_IGNORE
1260#ifdef FEAT_GUI
1261 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1264 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1265 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001266 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001268 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 || (!mouse_has(MOUSE_RETURN)
1270 && mouse_row < msg_row
1271 && (c == K_LEFTMOUSE
1272 || c == K_MIDDLEMOUSE
1273 || c == K_RIGHTMOUSE
1274 || c == K_X1MOUSE
1275 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 );
1277 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 /*
1279 * Avoid that the mouse-up event causes visual mode to start.
1280 */
1281 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1282 || c == K_X1MOUSE || c == K_X2MOUSE)
1283 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001284 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001286 // Put the character back in the typeahead buffer. Don't use the
1287 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001288 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001289 do_redraw = TRUE; // need a redraw even though there is
1290 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 redir_off = FALSE;
1294
1295 /*
1296 * If the user hits ':', '?' or '/' we get a command line from the next
1297 * line.
1298 */
1299 if (c == ':' || c == '?' || c == '/')
1300 {
1301 if (!exmode_active)
1302 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001305#ifdef FEAT_TERMINAL
1306 skip_term_loop = TRUE;
1307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309
1310 /*
1311 * If the window size changed set_shellsize() will redraw the screen.
1312 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1313 * typed.
1314 */
1315 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001316 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 msg_check();
1319
1320#if defined(UNIX) || defined(VMS)
1321 /*
1322 * When switching screens, we need to output an extra newline on exit.
1323 */
1324 if (swapping_screen() && !termcap_active)
1325 newline_on_exit = TRUE;
1326#endif
1327
1328 need_wait_return = FALSE;
1329 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 emsg_on_display = FALSE; // can delete error message now
1331 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 reset_last_sourcing();
1333 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1334 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001335 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar85a20022019-12-21 18:25:54 +01001337 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001339 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 shell_resized();
1341 }
1342 else if (!skip_redraw
1343 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1344 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001345 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 redraw_later(VALID);
1347 }
1348}
1349
1350/*
1351 * Write the hit-return prompt.
1352 */
1353 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001354hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001356 int save_p_more = p_more;
1357
Bram Moolenaar85a20022019-12-21 18:25:54 +01001358 p_more = FALSE; // don't want see this message when scrolling back
1359 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 msg_putchar('\n');
1361 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001362 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
Bram Moolenaar32526b32019-01-19 17:43:09 +01001364 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (!msg_use_printf())
1366 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001367 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368}
1369
1370/*
1371 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1372 */
1373 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001374set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 vim_free(keep_msg);
1377 if (s != NULL && msg_silent == 0)
1378 keep_msg = vim_strsave(s);
1379 else
1380 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001381 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001382 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383}
1384
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001385#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1386/*
1387 * If there currently is a message being displayed, set "keep_msg" to it, so
1388 * that it will be displayed again after redraw.
1389 */
1390 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001391set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001392{
1393 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1394 && (State & NORMAL))
1395 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1396}
1397#endif
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399/*
1400 * Prepare for outputting characters in the command line.
1401 */
1402 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001403msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
1405 int did_return = FALSE;
1406
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001407 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001408 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001409
1410#ifdef FEAT_EVAL
1411 if (need_clr_eos)
1412 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001413 // Halfway an ":echo" command and getting an (error) message: clear
1414 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001415 need_clr_eos = FALSE;
1416 msg_clr_eos();
1417 }
1418#endif
1419
Bram Moolenaar85a20022019-12-21 18:25:54 +01001420 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
1422 msg_row = cmdline_row;
1423 msg_col =
1424#ifdef FEAT_RIGHTLEFT
1425 cmdmsg_rl ? Columns - 1 :
1426#endif
1427 0;
1428 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001429 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
1431 msg_putchar('\n');
1432 did_return = TRUE;
1433 if (exmode_active != EXMODE_NORMAL)
1434 cmdline_row = msg_row;
1435 }
1436 if (!msg_didany || lines_left < 0)
1437 msg_starthere();
1438 if (msg_silent == 0)
1439 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001440 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 cursor_off();
1442 }
1443
Bram Moolenaar85a20022019-12-21 18:25:54 +01001444 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (!did_return)
1446 redir_write((char_u *)"\n", -1);
1447}
1448
1449/*
1450 * Note that the current msg position is where messages start.
1451 */
1452 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001453msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 lines_left = cmdline_row;
1456 msg_didany = FALSE;
1457}
1458
1459 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001460msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 msg_putchar_attr(c, 0);
1463}
1464
1465 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001466msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001468 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470 if (IS_SPECIAL(c))
1471 {
1472 buf[0] = K_SPECIAL;
1473 buf[1] = K_SECOND(c);
1474 buf[2] = K_THIRD(c);
1475 buf[3] = NUL;
1476 }
1477 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001478 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001479 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480}
1481
1482 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001483msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001485 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
Bram Moolenaar32526b32019-01-19 17:43:09 +01001487 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 msg_puts(buf);
1489}
1490
1491 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001492msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
1494 msg_home_replace_attr(fname, 0);
1495}
1496
1497#if defined(FEAT_FIND_ID) || defined(PROTO)
1498 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001499msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001501 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502}
1503#endif
1504
1505 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001506msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 char_u *name;
1509
1510 name = home_replace_save(NULL, fname);
1511 if (name != NULL)
1512 msg_outtrans_attr(name, attr);
1513 vim_free(name);
1514}
1515
1516/*
1517 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001518 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 * Use attributes 'attr'.
1520 * Return the number of characters it takes on the screen.
1521 */
1522 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001523msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 return msg_outtrans_attr(str, 0);
1526}
1527
1528 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001529msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1532}
1533
1534 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001535msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 return msg_outtrans_len_attr(str, len, 0);
1538}
1539
1540/*
1541 * Output one character at "p". Return pointer to the next character.
1542 * Handles multi-byte characters.
1543 */
1544 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001545msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 int l;
1548
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001549 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 msg_outtrans_len_attr(p, l, attr);
1552 return p + l;
1553 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001554 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return p + 1;
1556}
1557
1558 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001559msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 int retval = 0;
1562 char_u *str = msgstr;
1563 char_u *plain_start = msgstr;
1564 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 int mb_l;
1566 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001567 int save_got_int = got_int;
1568
1569 // Only quit when got_int was set in here.
1570 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaar85a20022019-12-21 18:25:54 +01001572 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (attr & MSG_HIST)
1574 {
1575 add_msg_hist(str, len, attr);
1576 attr &= ~MSG_HIST;
1577 }
1578
Bram Moolenaar85a20022019-12-21 18:25:54 +01001579 // If the string starts with a composing character first draw a space on
1580 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001582 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 /*
1585 * Go over the string. Special characters are translated and printed.
1586 * Normal characters are printed several at a time.
1587 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001588 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001591 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001592 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001594 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 else
1596 mb_l = 1;
1597 if (has_mbyte && mb_l > 1)
1598 {
1599 c = (*mb_ptr2char)(str);
1600 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 retval += (*mb_ptr2cells)(str);
1603 else
1604 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 // unprintable multi-byte char: print the printable chars so
1606 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001608 msg_puts_attr_len((char *)plain_start,
1609 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001611 msg_puts_attr((char *)transchar(c),
1612 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 retval += char2cells(c);
1614 }
1615 len -= mb_l - 1;
1616 str += mb_l;
1617 }
1618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 s = transchar_byte(*str);
1621 if (s[1] != NUL)
1622 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001623 // unprintable char: print the printable chars so far and the
1624 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001626 msg_puts_attr_len((char *)plain_start,
1627 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001629 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001630 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001632 else
1633 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 ++str;
1635 }
1636 }
1637
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001638 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001639 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001640 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001642 got_int |= save_got_int;
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 return retval;
1645}
1646
1647#if defined(FEAT_QUICKFIX) || defined(PROTO)
1648 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001649msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651 int i;
1652 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1653
1654 arg = skipwhite(arg);
1655 for (i = 5; *arg && i >= 0; --i)
1656 if (*arg++ != str[i])
1657 break;
1658 if (i < 0)
1659 {
1660 msg_putchar('\n');
1661 for (i = 0; rs[i]; ++i)
1662 msg_putchar(rs[i] - 3);
1663 }
1664}
1665#endif
1666
1667/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001668 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 * Return the number of characters it takes on the screen.
1670 *
1671 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1672 * following character and shown as <F1>, <S-Up> etc. Any other character
1673 * which is not printable shown in <> form.
1674 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1675 * If a character is displayed in one of these special ways, is also
1676 * highlighted (its highlight name is '8' in the p_hl variable).
1677 * Otherwise characters are not highlighted.
1678 * This function is used to show mappings, where we want to see how to type
1679 * the character/string -- webb
1680 */
1681 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001682msg_outtrans_special(
1683 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001684 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001685 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 char_u *str = strstart;
1688 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001689 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 int attr;
1691 int len;
1692
Bram Moolenaar8820b482017-03-16 17:23:31 +01001693 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 while (*str != NUL)
1695 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if ((str == strstart || str[1] == NUL) && *str == ' ')
1698 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001699 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 ++str;
1701 }
1702 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001703 text = (char *)str2special(&str, from);
1704 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001705 if (maxlen > 0 && retval + len >= maxlen)
1706 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001707 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001708 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001709 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 retval += len;
1711 }
1712 return retval;
1713}
1714
Bram Moolenaarbd743252010-10-20 21:23:33 +02001715#if defined(FEAT_EVAL) || defined(PROTO)
1716/*
1717 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1718 * strings, in an allocated string.
1719 */
1720 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001721str2special_save(
1722 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001723 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001724{
1725 garray_T ga;
1726 char_u *p = str;
1727
1728 ga_init2(&ga, 1, 40);
1729 while (*p != NUL)
1730 ga_concat(&ga, str2special(&p, is_lhs));
1731 ga_append(&ga, NUL);
1732 return (char_u *)ga.ga_data;
1733}
1734#endif
1735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736/*
1737 * Return the printable string for the key codes at "*sp".
1738 * Used for translating the lhs or rhs of a mapping to printable chars.
1739 * Advances "sp" to the next code.
1740 */
1741 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001742str2special(
1743 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001744 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 int c;
1747 static char_u buf[7];
1748 char_u *str = *sp;
1749 int modifiers = 0;
1750 int special = FALSE;
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (has_mbyte)
1753 {
1754 char_u *p;
1755
Bram Moolenaar85a20022019-12-21 18:25:54 +01001756 // Try to un-escape a multi-byte character. Return the un-escaped
1757 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 p = mb_unescape(sp);
1759 if (p != NULL)
1760 return p;
1761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763 c = *str;
1764 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1765 {
1766 if (str[1] == KS_MODIFIER)
1767 {
1768 modifiers = str[2];
1769 str += 3;
1770 c = *str;
1771 }
1772 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1773 {
1774 c = TO_SPECIAL(str[1], str[2]);
1775 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001777 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 special = TRUE;
1779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001781 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001783 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001784
Bram Moolenaar85a20022019-12-21 18:25:54 +01001785 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001786 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1787 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001788 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001789 *sp = str + 1;
1790 return buf;
1791 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001792 // Since 'special' is TRUE the multi-byte character 'c' will be
1793 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001794 c = (*mb_ptr2char)(str);
1795 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001797 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001798 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
Bram Moolenaar85a20022019-12-21 18:25:54 +01001800 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1801 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (special || char2cells(c) > 1 || (from && c == ' '))
1803 return get_special_key_name(c, modifiers);
1804 buf[0] = c;
1805 buf[1] = NUL;
1806 return buf;
1807}
1808
1809/*
1810 * Translate a key sequence into special key names.
1811 */
1812 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001813str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 char_u *s;
1816
1817 *buf = NUL;
1818 while (*sp)
1819 {
1820 s = str2special(&sp, FALSE);
1821 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1822 STRCAT(buf, s);
1823 }
1824}
1825
1826/*
1827 * print line for :print or :list command
1828 */
1829 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001830msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 int c;
1833 int col = 0;
1834 int n_extra = 0;
1835 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001836 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001837 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001839 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001841 char_u *lead = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 int l;
1843 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaardf177f62005-02-22 08:39:57 +00001845 if (curwin->w_p_list)
1846 list = TRUE;
1847
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001848 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001850 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001851 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001852 {
1853 trail = s + STRLEN(s);
1854 while (trail > s && VIM_ISWHITE(trail[-1]))
1855 --trail;
1856 }
1857 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001858 if (curwin->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001859 {
1860 lead = s;
1861 while (VIM_ISWHITE(lead[0]))
1862 lead++;
1863 // in a line full of spaces all of them are treated as trailing
1864 if (*lead == NUL)
1865 lead = NULL;
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868
Bram Moolenaar85a20022019-12-21 18:25:54 +01001869 // output a space for an empty line, otherwise the line will be
1870 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001871 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 msg_putchar(' ');
1873
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001874 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001876 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001879 if (n_extra == 0 && c_final)
1880 c = c_final;
1881 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 c = c_extra;
1883 else
1884 c = *p_extra++;
1885 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001886 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001889 if (l >= MB_MAXBYTES)
1890 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001891 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001892 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001893 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001894 && (mb_ptr2char(s) == 160
1895 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001896 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001897 mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001898 buf[(*mb_ptr2len)(buf)] = NUL;
1899 }
1900 else
1901 {
1902 mch_memmove(buf, s, (size_t)l);
1903 buf[l] = NUL;
1904 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001905 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 s += l;
1907 continue;
1908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 else
1910 {
1911 attr = 0;
1912 c = *s++;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001913 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001915 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001916#ifdef FEAT_VARTABS
1917 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1918 curbuf->b_p_vts_array) - 1;
1919#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001921#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001922 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
1924 c = ' ';
1925 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001926 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928 else
1929 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001930 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1931 ? curwin->w_lcs_chars.tab3
1932 : curwin->w_lcs_chars.tab1;
1933 c_extra = curwin->w_lcs_chars.tab2;
1934 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001935 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001938 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001939 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001940 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001941 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001942 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001943 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 p_extra = (char_u *)"";
1946 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001947 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001949 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001950 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 --s;
1952 }
1953 else if (c != NUL && (n = byte2cells(c)) > 1)
1954 {
1955 n_extra = n - 1;
1956 p_extra = transchar_byte(c);
1957 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001958 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001960 // Use special coloring to be able to distinguish <hex> from
1961 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001962 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001964 else if (c == ' ' && lead != NULL && s <= lead)
1965 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001966 c = curwin->w_lcs_chars.lead;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001967 attr = HL_ATTR(HLF_8);
1968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 else if (c == ' ' && trail != NULL && s > trail)
1970 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001971 c = curwin->w_lcs_chars.trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001972 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001974 else if (c == ' ' && list && curwin->w_lcs_chars.space != NUL)
Bram Moolenaar1510f992015-04-22 22:18:22 +02001975 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001976 c = curwin->w_lcs_chars.space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001977 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
1980
1981 if (c == NUL)
1982 break;
1983
1984 msg_putchar_attr(c, attr);
1985 col++;
1986 }
1987 msg_clr_eos();
1988}
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990/*
1991 * Use screen_puts() to output one multi-byte character.
1992 * Return the pointer "s" advanced to the next character.
1993 */
1994 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001995screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 int cw;
1998
Bram Moolenaar85a20022019-12-21 18:25:54 +01001999 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 cw = (*mb_ptr2cells)(s);
2001 if (cw > 1 && (
2002#ifdef FEAT_RIGHTLEFT
2003 cmdmsg_rl ? msg_col <= 1 :
2004#endif
2005 msg_col == Columns - 1))
2006 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002007 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002008 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 return s;
2010 }
2011
2012 screen_puts_len(s, l, msg_row, msg_col, attr);
2013#ifdef FEAT_RIGHTLEFT
2014 if (cmdmsg_rl)
2015 {
2016 msg_col -= cw;
2017 if (msg_col == 0)
2018 {
2019 msg_col = Columns;
2020 ++msg_row;
2021 }
2022 }
2023 else
2024#endif
2025 {
2026 msg_col += cw;
2027 if (msg_col >= Columns)
2028 {
2029 msg_col = 0;
2030 ++msg_row;
2031 }
2032 }
2033 return s + l;
2034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036/*
2037 * Output a string to the screen at position msg_row, msg_col.
2038 * Update msg_row and msg_col for the next message.
2039 */
2040 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002041msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002043 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044}
2045
2046 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002047msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002049 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050}
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052/*
2053 * Show a message in such a way that it always fits in the line. Cut out a
2054 * part in the middle and replace it with "..." when necessary.
2055 * Does not handle multi-byte characters!
2056 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002057 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002058msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int slen = len;
2061 int room;
2062
2063 room = Columns - msg_col;
2064 if (len > room && room >= 20)
2065 {
2066 slen = (room - 3) / 2;
2067 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002068 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2071}
2072
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002073 void
2074msg_outtrans_long_attr(char_u *longstr, int attr)
2075{
2076 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2077}
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079/*
2080 * Basic function for writing a message with highlight attributes.
2081 */
2082 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002083msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 msg_puts_attr_len(s, -1, attr);
2086}
2087
2088/*
2089 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2090 * When "maxlen" is -1 there is no maximum length.
2091 * When "maxlen" is >= 0 the message is not put in the history.
2092 */
2093 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002094msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 /*
2097 * If redirection is on, also write to the redirection file.
2098 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002099 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
2101 /*
2102 * Don't print anything when using ":silent cmd".
2103 */
2104 if (msg_silent != 0)
2105 return;
2106
Bram Moolenaar85a20022019-12-21 18:25:54 +01002107 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if ((attr & MSG_HIST) && maxlen < 0)
2109 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002110 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 attr &= ~MSG_HIST;
2112 }
2113
Bram Moolenaare8070982019-10-12 17:07:06 +02002114 // When writing something to the screen after it has scrolled, requires a
2115 // wait-return prompt later. Needed when scrolling, resetting
2116 // need_wait_return after some prompt, and then outputting something
2117 // without scrolling
2118 // Not needed when only using CR to move the cursor.
2119 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002121 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 /*
2124 * If there is no valid screen, use fprintf so we can see error messages.
2125 * If termcap is not active, we may be writing in an alternate console
2126 * window, cursor positioning may not work correctly (window size may be
2127 * different, e.g. for Win32 console) or we just don't know where the
2128 * cursor is.
2129 */
2130 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002131 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002132 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002133 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002134}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002136/*
2137 * The display part of msg_puts_attr_len().
2138 * May be called recursively to display scroll-back text.
2139 */
2140 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002141msg_puts_display(
2142 char_u *str,
2143 int maxlen,
2144 int attr,
2145 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002146{
2147 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002148 char_u *t_s = str; // string from "t_s" to "s" is still todo
2149 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002150 int l;
2151 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002152 char_u *sb_str = str;
2153 int sb_col = msg_col;
2154 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002155 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002158 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
2160 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002161 * We are at the end of the screen line when:
2162 * - When outputting a newline.
2163 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002165 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#ifdef FEAT_RIGHTLEFT
2167 cmdmsg_rl
2168 ? (
2169 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002170 || (*s == TAB && msg_col <= 7)
2171 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 :
2173#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002174 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002177 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002179 /*
2180 * The screen is scrolled up when at the last row (some terminals
2181 * scroll automatically, some don't. To avoid problems we scroll
2182 * ourselves).
2183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002185 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002186 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 if (msg_no_more && lines_left == 0)
2190 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002193 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002196 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 msg_col = Columns - 1;
2198
Bram Moolenaar85a20022019-12-21 18:25:54 +01002199 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002200 if (*s >= ' '
2201#ifdef FEAT_RIGHTLEFT
2202 && !cmdmsg_rl
2203#endif
2204 )
2205 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002206 if (has_mbyte)
2207 {
2208 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002209 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002210 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002211 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002212 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002213 s = screen_puts_mbyte(s, l, attr);
2214 }
2215 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002216 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002217 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002218 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002219 else
2220 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002221
2222 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002223 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002224 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2225
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002226 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 redraw_cmdline = TRUE;
2229 if (cmdline_row > 0 && !exmode_active)
2230 --cmdline_row;
2231
2232 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002233 * If screen is completely filled and 'more' is set then wait
2234 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002236 if (lines_left > 0)
2237 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002238 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 && !msg_no_more && !exmode_active)
2240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002242 if (do_more_prompt(NUL))
2243 s = confirm_msg_tail;
2244#else
2245 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#endif
2247 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002248 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002250
Bram Moolenaar85a20022019-12-21 18:25:54 +01002251 // When we displayed a char in last column need to check if there
2252 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002253 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002254 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002257 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002260 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002261 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2262 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002264 t_puts(&t_col, t_s, s, attr);
2265
2266 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002267 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002268 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002272 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002273#ifdef FEAT_RIGHTLEFT
2274 if (cmdmsg_rl)
2275 msg_col = Columns - 1;
2276 else
2277#endif
2278 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002279 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 msg_row = Rows - 1;
2281 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
2284 msg_col = 0;
2285 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002286 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
2288 if (msg_col)
2289 --msg_col;
2290 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002291 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 do
2294 msg_screen_putchar(' ', attr);
2295 while (msg_col & 7);
2296 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002298 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 else
2300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (has_mbyte)
2302 {
2303 cw = (*mb_ptr2cells)(s);
2304 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002305 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002306 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002308 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310 else
2311 {
2312 cw = 1;
2313 l = 1;
2314 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002315
Bram Moolenaar85a20022019-12-21 18:25:54 +01002316 // When drawing from right to left or when a double-wide character
2317 // doesn't fit, draw a single character here. Otherwise collect
2318 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (
2320# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002321 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002323 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (l > 1)
2326 s = screen_puts_mbyte(s, l, attr) - 1;
2327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 msg_screen_putchar(*s, attr);
2329 }
2330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (t_col == 0)
2334 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 t_col += cw;
2336 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338 }
2339 ++s;
2340 }
2341
Bram Moolenaar85a20022019-12-21 18:25:54 +01002342 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002344 t_puts(&t_col, t_s, s, attr);
2345 if (p_more && !recurse)
2346 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348 msg_check();
2349}
2350
2351/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002352 * Return TRUE when ":filter pattern" was used and "msg" does not match
2353 * "pattern".
2354 */
2355 int
2356message_filtered(char_u *msg)
2357{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002358 int match;
2359
Bram Moolenaare1004402020-10-24 20:49:43 +02002360 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002361 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002362 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2363 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002364}
2365
2366/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002367 * Scroll the screen up one line for displaying the next message line.
2368 */
2369 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002370msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002371{
2372#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 // Remove the cursor before scrolling, ScreenLines[] is going
2374 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002375 if (gui.in_use)
2376 gui_undraw_cursor();
2377#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002378 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002379 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002380 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002381 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002382
2383 if (!can_clear((char_u *)" "))
2384 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002385 // Scrolling up doesn't result in the right background. Set the
2386 // background here. It's not efficient, but avoids that we have to do
2387 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002388 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2389
Bram Moolenaar85a20022019-12-21 18:25:54 +01002390 // Also clear the last char of the last but one line if it was not
2391 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002392 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2393 screen_fill((int)Rows - 2, (int)Rows - 1,
2394 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2395 }
2396}
2397
2398/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002399 * Increment "msg_scrolled".
2400 */
2401 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002402inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002403{
2404#ifdef FEAT_EVAL
2405 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2406 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002407 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002408 char_u *tofree = NULL;
2409 int len;
2410
Bram Moolenaar85a20022019-12-21 18:25:54 +01002411 // v:scrollstart is empty, set it to the script/function name and line
2412 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002413 if (p == NULL)
2414 p = (char_u *)_("Unknown");
2415 else
2416 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002417 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002418 tofree = alloc(len);
2419 if (tofree != NULL)
2420 {
2421 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002422 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002423 p = tofree;
2424 }
2425 }
2426 set_vim_var_string(VV_SCROLLSTART, p, -1);
2427 vim_free(tofree);
2428 }
2429#endif
2430 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002431 if (must_redraw < VALID)
2432 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002433}
2434
2435/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2437 * store the displayed text and remember where screen lines start.
2438 */
2439typedef struct msgchunk_S msgchunk_T;
2440struct msgchunk_S
2441{
2442 msgchunk_T *sb_next;
2443 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002444 char sb_eol; // TRUE when line ends after this text
2445 int sb_msg_col; // column in which text starts
2446 int sb_attr; // text attributes
2447 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002448};
2449
Bram Moolenaar85a20022019-12-21 18:25:54 +01002450static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002451
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002452static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002453
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002454typedef enum {
2455 SB_CLEAR_NONE = 0,
2456 SB_CLEAR_ALL,
2457 SB_CLEAR_CMDLINE_BUSY,
2458 SB_CLEAR_CMDLINE_DONE
2459} sb_clear_T;
2460
Bram Moolenaar85a20022019-12-21 18:25:54 +01002461// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002462static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002463
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464/*
2465 * Store part of a printed message for displaying when scrolling back.
2466 */
2467 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002468store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002469 char_u **sb_str, // start of string
2470 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002471 int attr,
2472 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002473 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002474{
2475 msgchunk_T *mp;
2476
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002477 if (do_clear_sb_text == SB_CLEAR_ALL
2478 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002479 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002480 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2481 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002482 }
2483
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002484 if (s > *sb_str)
2485 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002486 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002487 if (mp != NULL)
2488 {
2489 mp->sb_eol = finish;
2490 mp->sb_msg_col = *sb_col;
2491 mp->sb_attr = attr;
2492 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2493
2494 if (last_msgchunk == NULL)
2495 {
2496 last_msgchunk = mp;
2497 mp->sb_prev = NULL;
2498 }
2499 else
2500 {
2501 mp->sb_prev = last_msgchunk;
2502 last_msgchunk->sb_next = mp;
2503 last_msgchunk = mp;
2504 }
2505 mp->sb_next = NULL;
2506 }
2507 }
2508 else if (finish && last_msgchunk != NULL)
2509 last_msgchunk->sb_eol = TRUE;
2510
2511 *sb_str = s;
2512 *sb_col = 0;
2513}
2514
2515/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002516 * Finished showing messages, clear the scroll-back text on the next message.
2517 */
2518 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002519may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002520{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002521 do_clear_sb_text = SB_CLEAR_ALL;
2522}
2523
2524/*
2525 * Starting to edit the command line, do not clear messages now.
2526 */
2527 void
2528sb_text_start_cmdline(void)
2529{
2530 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2531 msg_sb_eol();
2532}
2533
2534/*
2535 * Ending to edit the command line. Clear old lines but the last one later.
2536 */
2537 void
2538sb_text_end_cmdline(void)
2539{
2540 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002541}
2542
2543/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002544 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002545 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546 * Called when redrawing the screen.
2547 */
2548 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002549clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002550{
2551 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002552 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002553
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002554 if (all)
2555 lastp = &last_msgchunk;
2556 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002557 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002558 if (last_msgchunk == NULL)
2559 return;
2560 lastp = &last_msgchunk->sb_prev;
2561 }
2562
2563 while (*lastp != NULL)
2564 {
2565 mp = (*lastp)->sb_prev;
2566 vim_free(*lastp);
2567 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002568 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002569}
2570
2571/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002572 * "g<" command.
2573 */
2574 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002575show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002576{
2577 msgchunk_T *mp;
2578
Bram Moolenaar85a20022019-12-21 18:25:54 +01002579 // Only show something if there is more than one line, otherwise it looks
2580 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002581 mp = msg_sb_start(last_msgchunk);
2582 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002583 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002584 else
2585 {
2586 do_more_prompt('G');
2587 wait_return(FALSE);
2588 }
2589}
2590
2591/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002592 * Move to the start of screen line in already displayed text.
2593 */
2594 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002595msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596{
2597 msgchunk_T *mp = mps;
2598
2599 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2600 mp = mp->sb_prev;
2601 return mp;
2602}
2603
2604/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002605 * Mark the last message chunk as finishing the line.
2606 */
2607 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002608msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002609{
2610 if (last_msgchunk != NULL)
2611 last_msgchunk->sb_eol = TRUE;
2612}
2613
2614/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002615 * Display a screen line from previously displayed text at row "row".
2616 * Returns a pointer to the text for the next line (can be NULL).
2617 */
2618 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002619disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002620{
2621 msgchunk_T *mp = smp;
2622 char_u *p;
2623
2624 for (;;)
2625 {
2626 msg_row = row;
2627 msg_col = mp->sb_msg_col;
2628 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002629 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002630 ++p;
2631 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2632 if (mp->sb_eol || mp->sb_next == NULL)
2633 break;
2634 mp = mp->sb_next;
2635 }
2636 return mp->sb_next;
2637}
2638
2639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * Output any postponed text for msg_puts_attr_len().
2641 */
2642 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002643t_puts(
2644 int *t_col,
2645 char_u *t_s,
2646 char_u *s,
2647 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 // output postponed text
2650 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002652 msg_col += *t_col;
2653 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002654 // If the string starts with a composing character don't increment the
2655 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2657 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (msg_col >= Columns)
2659 {
2660 msg_col = 0;
2661 ++msg_row;
2662 }
2663}
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
2666 * Returns TRUE when messages should be printed with mch_errmsg().
2667 * This is used when there is no valid screen, so we can see error messages.
2668 * If termcap is not active, we may be writing in an alternate console
2669 * window, cursor positioning may not work correctly (window size may be
2670 * different, e.g. for Win32 console) or we just don't know where the
2671 * cursor is.
2672 */
2673 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002674msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
2676 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002677#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2678# ifdef VIMDLL
2679 || (!gui.in_use && !termcap_active)
2680# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683#endif
2684 || (swapping_screen() && !termcap_active)
2685 );
2686}
2687
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002688/*
2689 * Print a message when there is no valid screen.
2690 */
2691 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002692msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693{
2694 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002695 char_u *buf = NULL;
2696 char_u *p = s;
2697
Bram Moolenaar4f974752019-02-17 17:44:42 +01002698#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002699 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002700 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002701#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002702 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002703 {
2704 if (!(silent_mode && p_verbose == 0))
2705 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002706 // NL --> CR NL translation (for Unix, not for "--version")
2707 if (*s == NL)
2708 {
2709 int n = (int)(s - p);
2710
2711 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002712 if (buf != NULL)
2713 {
2714 memcpy(buf, p, n);
2715 if (!info_message)
2716 buf[n++] = CAR;
2717 buf[n++] = NL;
2718 buf[n++] = NUL;
2719 if (info_message) // informative message, not an error
2720 mch_msg((char *)buf);
2721 else
2722 mch_errmsg((char *)buf);
2723 vim_free(buf);
2724 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002725 p = s + 1;
2726 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 }
2728
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002729 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002730#ifdef FEAT_RIGHTLEFT
2731 if (cmdmsg_rl)
2732 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002733 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002734 msg_col = Columns - 1;
2735 else
2736 --msg_col;
2737 }
2738 else
2739#endif
2740 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002741 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002742 msg_col = 0;
2743 else
2744 ++msg_col;
2745 }
2746 ++s;
2747 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002748
2749 if (*p != NUL && !(silent_mode && p_verbose == 0))
2750 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002751 int c = -1;
2752
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002753 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002754 {
2755 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002756 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002757 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002758 if (info_message)
2759 mch_msg((char *)p);
2760 else
2761 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002762 if (c != -1)
2763 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002764 }
2765
2766 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002767
Bram Moolenaar4f974752019-02-17 17:44:42 +01002768#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002769 if (!(silent_mode && p_verbose == 0))
2770 mch_settmode(TMODE_RAW);
2771#endif
2772}
2773
2774/*
2775 * Show the more-prompt and handle the user response.
2776 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002777 * When at hit-enter prompt "typed_char" is the already typed character,
2778 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002779 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2780 */
2781 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002782do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002783{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002784 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 int used_typed_char = typed_char;
2786 int oldState = State;
2787 int c;
2788#ifdef FEAT_CON_DIALOG
2789 int retval = FALSE;
2790#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002791 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002792 msgchunk_T *mp_last = NULL;
2793 msgchunk_T *mp;
2794 int i;
2795
Bram Moolenaar85a20022019-12-21 18:25:54 +01002796 // We get called recursively when a timer callback outputs a message. In
2797 // that case don't show another prompt. Also when at the hit-Enter prompt
2798 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002799 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002800 return FALSE;
2801 entered = TRUE;
2802
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002803 if (typed_char == 'G')
2804 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002805 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002806 mp_last = msg_sb_start(last_msgchunk);
2807 for (i = 0; i < Rows - 2 && mp_last != NULL
2808 && mp_last->sb_prev != NULL; ++i)
2809 mp_last = msg_sb_start(mp_last->sb_prev);
2810 }
2811
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002812 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002814 if (typed_char == NUL)
2815 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 for (;;)
2817 {
2818 /*
2819 * Get a typed character directly from the user.
2820 */
2821 if (used_typed_char != NUL)
2822 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002823 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002824 used_typed_char = NUL;
2825 }
2826 else
2827 c = get_keystroke();
2828
2829#if defined(FEAT_MENU) && defined(FEAT_GUI)
2830 if (c == K_MENU)
2831 {
2832 int idx = get_menu_index(current_menu, ASKMORE);
2833
Bram Moolenaar85a20022019-12-21 18:25:54 +01002834 // Used a menu. If it starts with CTRL-Y, it must
2835 // be a "Copy" for the clipboard. Otherwise
2836 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002837 if (idx == MENU_INDEX_INVALID)
2838 continue;
2839 c = *current_menu->strings[idx];
2840 if (c != NUL && current_menu->strings[idx][1] != NUL)
2841 ins_typebuf(current_menu->strings[idx] + 1,
2842 current_menu->noremap[idx], 0, TRUE,
2843 current_menu->silent[idx]);
2844 }
2845#endif
2846
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002847 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002848 switch (c)
2849 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002850 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 case K_BS:
2852 case 'k':
2853 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002854 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002855 break;
2856
Bram Moolenaar85a20022019-12-21 18:25:54 +01002857 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 case NL:
2859 case 'j':
2860 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002861 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002862 break;
2863
Bram Moolenaar85a20022019-12-21 18:25:54 +01002864 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002865 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 break;
2867
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002869 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002870 break;
2871
Bram Moolenaar85a20022019-12-21 18:25:54 +01002872 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002873 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002874 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002875 break;
2876
Bram Moolenaar85a20022019-12-21 18:25:54 +01002877 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002878 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002879 case K_PAGEDOWN:
2880 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002881 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882 break;
2883
Bram Moolenaar85a20022019-12-21 18:25:54 +01002884 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002885 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002886 break;
2887
Bram Moolenaar85a20022019-12-21 18:25:54 +01002888 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002889 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002890 lines_left = 999999;
2891 break;
2892
Bram Moolenaar85a20022019-12-21 18:25:54 +01002893 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002894#ifdef FEAT_CON_DIALOG
2895 if (!confirm_msg_used)
2896#endif
2897 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002898 // Since got_int is set all typeahead will be flushed, but we
2899 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002901#ifdef FEAT_TERMINAL
2902 skip_term_loop = TRUE;
2903#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002904 cmdline_row = Rows - 1; // put ':' on this line
2905 skip_redraw = TRUE; // skip redraw once
2906 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002907 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002908 // FALLTHROUGH
2909 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002910 case Ctrl_C:
2911 case ESC:
2912#ifdef FEAT_CON_DIALOG
2913 if (confirm_msg_used)
2914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002916 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002917 }
2918 else
2919#endif
2920 {
2921 got_int = TRUE;
2922 quit_more = TRUE;
2923 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // When there is some more output (wrapping line) display that
2925 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002926 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002927 break;
2928
2929#ifdef FEAT_CLIPBOARD
2930 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 // Strange way to allow copying (yanking) a modeless
2932 // selection at the more prompt. Use CTRL-Y,
2933 // because the same is used in Cmdline-mode and at the
2934 // hit-enter prompt. However, scrolling one line up
2935 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936 if (clip_star.state == SELECT_DONE)
2937 clip_copy_modeless_selection(TRUE);
2938 continue;
2939#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002940 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002941 msg_moremsg(TRUE);
2942 continue;
2943 }
2944
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002945 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002946 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002947 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002949 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002950 if (mp_last == NULL)
2951 mp = msg_sb_start(last_msgchunk);
2952 else if (mp_last->sb_prev != NULL)
2953 mp = msg_sb_start(mp_last->sb_prev);
2954 else
2955 mp = NULL;
2956
Bram Moolenaar85a20022019-12-21 18:25:54 +01002957 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002958 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2959 ++i)
2960 mp = msg_sb_start(mp->sb_prev);
2961
2962 if (mp != NULL && mp->sb_prev != NULL)
2963 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002964 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002965 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 {
2967 if (mp == NULL || mp->sb_prev == NULL)
2968 break;
2969 mp = msg_sb_start(mp->sb_prev);
2970 if (mp_last == NULL)
2971 mp_last = msg_sb_start(last_msgchunk);
2972 else
2973 mp_last = msg_sb_start(mp_last->sb_prev);
2974 }
2975
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002976 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002977 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002979 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002980 (void)disp_sb_line(0, mp);
2981 }
2982 else
2983 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002984 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002985 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002986 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2987 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002988 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002989 ++msg_scrolled;
2990 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002991 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002992 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002993 }
2994 }
2995 else
2996 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002997 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002998 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002999 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003000 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003001 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003002 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003003 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3004 (int)Columns, ' ', ' ', 0);
3005 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003006 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003007 }
3008 }
3009
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003010 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003011 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003012 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003013 screen_fill((int)Rows - 1, (int)Rows, 0,
3014 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003015 msg_moremsg(FALSE);
3016 continue;
3017 }
3018
Bram Moolenaar85a20022019-12-21 18:25:54 +01003019 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003020 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003021 }
3022
3023 break;
3024 }
3025
Bram Moolenaar85a20022019-12-21 18:25:54 +01003026 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003027 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3028 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003029 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003030 if (quit_more)
3031 {
3032 msg_row = Rows - 1;
3033 msg_col = 0;
3034 }
3035#ifdef FEAT_RIGHTLEFT
3036 else if (cmdmsg_rl)
3037 msg_col = Columns - 1;
3038#endif
3039
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003040 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041#ifdef FEAT_CON_DIALOG
3042 return retval;
3043#else
3044 return FALSE;
3045#endif
3046}
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3049
3050#ifdef mch_errmsg
3051# undef mch_errmsg
3052#endif
3053#ifdef mch_msg
3054# undef mch_msg
3055#endif
3056
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003057#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3058 static void
3059mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003061 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003062 DWORD nwrite = 0;
3063 DWORD mode = 0;
3064 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3065
3066 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3067 && (int)GetConsoleCP() != enc_codepage)
3068 {
3069 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3070
3071 WriteConsoleW(h, w, len, &nwrite, NULL);
3072 vim_free(w);
3073 }
3074 else
3075 {
3076 fprintf(stderr, "%s", str);
3077 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003078}
3079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003081/*
3082 * Give an error message. To be used when the screen hasn't been initialized
3083 * yet. When stderr can't be used, collect error messages until the GUI has
3084 * started and they can be displayed in a message box.
3085 */
3086 void
3087mch_errmsg(char *str)
3088{
3089#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3090 int len;
3091#endif
3092
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003093#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003094 // On Unix use stderr if it's a tty.
3095 // When not going to start the GUI also use stderr.
3096 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003098# ifdef UNIX
3099# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003101# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 isatty(2)
3103# endif
3104# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003105 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003106# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003107# endif
3108# ifdef FEAT_GUI
3109 !(gui.in_use || gui.starting)
3110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 )
3112 {
3113 fprintf(stderr, "%s", str);
3114 return;
3115 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003118#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3119# ifdef VIMDLL
3120 if (!(gui.in_use || gui.starting))
3121# endif
3122 {
3123 mch_errmsg_c(str);
3124 return;
3125 }
3126#endif
3127
3128#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003129 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 emsg_on_display = FALSE;
3131
3132 len = (int)STRLEN(str) + 1;
3133 if (error_ga.ga_growsize == 0)
3134 {
3135 error_ga.ga_growsize = 80;
3136 error_ga.ga_itemsize = 1;
3137 }
3138 if (ga_grow(&error_ga, len) == OK)
3139 {
3140 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3141 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003142# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003143 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 char_u *p;
3146
3147 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3148 for (;;)
3149 {
3150 p = vim_strchr(p, '\r');
3151 if (p == NULL)
3152 break;
3153 *p = ' ';
3154 }
3155 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003156# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003157 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161}
3162
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003163#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3164 static void
3165mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003167 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003168 DWORD nwrite = 0;
3169 DWORD mode;
3170 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3171
3172
3173 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3174 && (int)GetConsoleCP() != enc_codepage)
3175 {
3176 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3177
3178 WriteConsoleW(h, w, len, &nwrite, NULL);
3179 vim_free(w);
3180 }
3181 else
3182 {
3183 printf("%s", str);
3184 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003185}
3186#endif
3187
3188/*
3189 * Give a message. To be used when the screen hasn't been initialized yet.
3190 * When there is no tty, collect messages until the GUI has started and they
3191 * can be displayed in a message box.
3192 */
3193 void
3194mch_msg(char *str)
3195{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003196#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003197 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3198 // uses mch_errmsg() when started from the desktop.
3199 // When not going to start the GUI also use stdout.
3200 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003202# ifdef UNIX
3203# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003205# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003209 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003211# endif
3212# ifdef FEAT_GUI
3213 !(gui.in_use || gui.starting)
3214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 )
3216 {
3217 printf("%s", str);
3218 return;
3219 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003220#endif
3221
3222#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3223# ifdef VIMDLL
3224 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003226 {
3227 mch_msg_c(str);
3228 return;
3229 }
3230#endif
3231#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003235#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237/*
3238 * Put a character on the screen at the current message position and advance
3239 * to the next position. Only for printable ASCII!
3240 */
3241 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003242msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003244 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 screen_putchar(c, msg_row, msg_col, attr);
3246#ifdef FEAT_RIGHTLEFT
3247 if (cmdmsg_rl)
3248 {
3249 if (--msg_col == 0)
3250 {
3251 msg_col = Columns;
3252 ++msg_row;
3253 }
3254 }
3255 else
3256#endif
3257 {
3258 if (++msg_col >= Columns)
3259 {
3260 msg_col = 0;
3261 ++msg_row;
3262 }
3263 }
3264}
3265
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003266 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003267msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003269 int attr;
3270 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
Bram Moolenaar8820b482017-03-16 17:23:31 +01003272 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003273 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003275 screen_puts((char_u *)
3276 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3277 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278}
3279
3280/*
3281 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3282 * exmode_active.
3283 */
3284 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003285repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 if (State == ASKMORE)
3288 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003289 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 msg_row = Rows - 1;
3291 }
3292#ifdef FEAT_CON_DIALOG
3293 else if (State == CONFIRM)
3294 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003295 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 msg_row = Rows - 1;
3297 }
3298#endif
3299 else if (State == EXTERNCMD)
3300 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003301 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303 else if (State == HITRETURN || State == SETWSIZE)
3304 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003305 if (msg_row == Rows - 1)
3306 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003307 // Avoid drawing the "hit-enter" prompt below the previous one,
3308 // overwrite it. Esp. useful when regaining focus and a
3309 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003310 msg_didout = FALSE;
3311 msg_col = 0;
3312 msg_clr_eos();
3313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 hit_return_msg();
3315 msg_row = Rows - 1;
3316 }
3317}
3318
3319/*
3320 * msg_check_screen - check if the screen is initialized.
3321 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3322 * While starting the GUI the terminal codes will be set for the GUI, but the
3323 * output goes to the terminal. Don't use the terminal codes then.
3324 */
3325 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003326msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 if (!full_screen || !screen_valid(FALSE))
3329 return FALSE;
3330
3331 if (msg_row >= Rows)
3332 msg_row = Rows - 1;
3333 if (msg_col >= Columns)
3334 msg_col = Columns - 1;
3335 return TRUE;
3336}
3337
3338/*
3339 * Clear from current message position to end of screen.
3340 * Skip this when ":silent" was used, no need to clear for redirection.
3341 */
3342 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003343msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 if (msg_silent == 0)
3346 msg_clr_eos_force();
3347}
3348
3349/*
3350 * Clear from current message position to end of screen.
3351 * Note: msg_col is not updated, so we remember the end of the message
3352 * for msg_check().
3353 */
3354 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003355msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 if (msg_use_printf())
3358 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003359 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003362 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003364 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 }
3367 else
3368 {
3369#ifdef FEAT_RIGHTLEFT
3370 if (cmdmsg_rl)
3371 {
3372 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3373 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3374 }
3375 else
3376#endif
3377 {
3378 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3379 ' ', ' ', 0);
3380 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3381 }
3382 }
3383}
3384
3385/*
3386 * Clear the command line.
3387 */
3388 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003389msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 msg_row = cmdline_row;
3392 msg_col = 0;
3393 msg_clr_eos_force();
3394}
3395
3396/*
3397 * end putting a message on the screen
3398 * call wait_return if the message does not fit in the available space
3399 * return TRUE if wait_return not called.
3400 */
3401 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003402msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
3404 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003405 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 * or the ruler option is set and we run into it,
3407 * we have to redraw the window.
3408 * Do not do this if we are abandoning the file or editing the command line.
3409 */
3410 if (!exiting && need_wait_return && !(State & CMDLINE))
3411 {
3412 wait_return(FALSE);
3413 return FALSE;
3414 }
3415 out_flush();
3416 return TRUE;
3417}
3418
3419/*
3420 * If the written message runs into the shown command or ruler, we have to
3421 * wait for hit-return and redraw the window later.
3422 */
3423 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003424msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
3426 if (msg_row == Rows - 1 && msg_col >= sc_col)
3427 {
3428 need_wait_return = TRUE;
3429 redraw_cmdline = TRUE;
3430 }
3431}
3432
3433/*
3434 * May write a string to the redirection file.
3435 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3436 */
3437 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003438redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 char_u *s = str;
3441 static int cur_col = 0;
3442
Bram Moolenaar85a20022019-12-21 18:25:54 +01003443 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003444 if (redir_off)
3445 return;
3446
Bram Moolenaar85a20022019-12-21 18:25:54 +01003447 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003448 if (*p_vfile != NUL && verbose_fd == NULL)
3449 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003450
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003451 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003453 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (*s != '\n' && *s != '\r')
3455 {
3456 while (cur_col < msg_col)
3457 {
3458#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003459 if (redir_execute)
3460 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003461 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003463 else if (redir_vname)
3464 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003467 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003469 if (verbose_fd != NULL)
3470 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 ++cur_col;
3472 }
3473 }
3474
3475#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003476 if (redir_execute)
3477 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003478 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003480 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003481 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482#endif
3483
Bram Moolenaar85a20022019-12-21 18:25:54 +01003484 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3486 {
3487#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003488 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003490 if (redir_fd != NULL)
3491 putc(*s, redir_fd);
3492 if (verbose_fd != NULL)
3493 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (*s == '\r' || *s == '\n')
3495 cur_col = 0;
3496 else if (*s == '\t')
3497 cur_col += (8 - cur_col % 8);
3498 else
3499 ++cur_col;
3500 ++s;
3501 }
3502
Bram Moolenaar85a20022019-12-21 18:25:54 +01003503 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 msg_col = cur_col;
3505 }
3506}
3507
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003508 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003509redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003510{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003511 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003512#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003513 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003514#endif
3515 ;
3516}
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003519 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003520 * Must always be called paired with verbose_leave()!
3521 */
3522 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003523verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003524{
3525 if (*p_vfile != NUL)
3526 ++msg_silent;
3527}
3528
3529/*
3530 * After giving verbose message.
3531 * Must always be called paired with verbose_enter()!
3532 */
3533 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003534verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003535{
3536 if (*p_vfile != NUL)
3537 if (--msg_silent < 0)
3538 msg_silent = 0;
3539}
3540
3541/*
3542 * Like verbose_enter() and set msg_scroll when displaying the message.
3543 */
3544 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003545verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003546{
3547 if (*p_vfile != NUL)
3548 ++msg_silent;
3549 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003550 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003551 msg_scroll = TRUE;
3552}
3553
3554/*
3555 * Like verbose_leave() and set cmdline_row when displaying the message.
3556 */
3557 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003558verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003559{
3560 if (*p_vfile != NUL)
3561 {
3562 if (--msg_silent < 0)
3563 msg_silent = 0;
3564 }
3565 else
3566 cmdline_row = msg_row;
3567}
3568
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003569/*
3570 * Called when 'verbosefile' is set: stop writing to the file.
3571 */
3572 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003573verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003574{
3575 if (verbose_fd != NULL)
3576 {
3577 fclose(verbose_fd);
3578 verbose_fd = NULL;
3579 }
3580 verbose_did_open = FALSE;
3581}
3582
3583/*
3584 * Open the file 'verbosefile'.
3585 * Return FAIL or OK.
3586 */
3587 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003588verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003589{
3590 if (verbose_fd == NULL && !verbose_did_open)
3591 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003592 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003593 verbose_did_open = TRUE;
3594
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003595 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003596 if (verbose_fd == NULL)
3597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003598 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003599 return FAIL;
3600 }
3601 }
3602 return OK;
3603}
3604
3605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 * Give a warning message (for searching).
3607 * Use 'w' highlighting and may repeat the message after redrawing
3608 */
3609 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003610give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003612 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (msg_silent != 0)
3614 return;
3615
Bram Moolenaar85a20022019-12-21 18:25:54 +01003616 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619#ifdef FEAT_EVAL
3620 set_vim_var_string(VV_WARNINGMSG, message, -1);
3621#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003622 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003624 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 else
3626 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003627 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003628 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003629 msg_didout = FALSE; // overwrite this message
3630 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003632
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 --no_wait_return;
3634}
3635
Bram Moolenaar113e1072019-01-20 15:30:40 +01003636#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003637 void
3638give_warning2(char_u *message, char_u *a1, int hl)
3639{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003640 if (IObuff == NULL)
3641 {
3642 // Very early in initialisation and already something wrong, just give
3643 // the raw message so the user at least gets a hint.
3644 give_warning((char_u *)message, hl);
3645 }
3646 else
3647 {
3648 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3649 give_warning(IObuff, hl);
3650 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003651}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003652#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654/*
3655 * Advance msg cursor to column "col".
3656 */
3657 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003658msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003660 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003662 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return;
3664 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003665 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003667#ifdef FEAT_RIGHTLEFT
3668 if (cmdmsg_rl)
3669 while (msg_col > Columns - col)
3670 msg_putchar(' ');
3671 else
3672#endif
3673 while (msg_col < col)
3674 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675}
3676
3677#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3678/*
3679 * Used for "confirm()" function, and the :confirm command prefix.
3680 * Versions which haven't got flexible dialogs yet, and console
3681 * versions, get this generic handler which uses the command line.
3682 *
3683 * type = one of:
3684 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3685 * title = title string (can be NULL for default)
3686 * (neither used in console dialogs at the moment)
3687 *
3688 * Format of the "buttons" string:
3689 * "Button1Name\nButton2Name\nButton3Name"
3690 * The first button should normally be the default/accept
3691 * The second button should be the 'Cancel' button
3692 * Other buttons- use your imagination!
3693 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3694 * different letter.
3695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003697do_dialog(
3698 int type UNUSED,
3699 char_u *title UNUSED,
3700 char_u *message,
3701 char_u *buttons,
3702 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003703 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3704 // otherwise
3705 int ex_cmd) // when TRUE pressing : accepts default and starts
3706 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 int oldState;
3709 int retval = 0;
3710 char_u *hotkeys;
3711 int c;
3712 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003713 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003716 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003718 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#endif
3720
3721#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003722 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3724 {
3725 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003726 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003727 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003728 need_wait_return = FALSE;
3729 emsg_on_display = FALSE;
3730 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
Bram Moolenaar85a20022019-12-21 18:25:54 +01003732 // Flush output to avoid that further messages and redrawing is done
3733 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 out_flush();
3735 gui_mch_update();
3736
3737 return c;
3738 }
3739#endif
3740
3741 oldState = State;
3742 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
Bram Moolenaar27321db2020-07-06 21:24:57 +02003745 // Ensure raw mode here.
3746 save_tmode = cur_tmode;
3747 settmode(TMODE_RAW);
3748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 /*
3750 * Since we wait for a keypress, don't make the
3751 * user press RETURN as well afterwards.
3752 */
3753 ++no_wait_return;
3754 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3755
3756 if (hotkeys != NULL)
3757 {
3758 for (;;)
3759 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003760 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 c = get_keystroke();
3762 switch (c)
3763 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003764 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case NL:
3766 retval = dfltbutton;
3767 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003768 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 case ESC:
3770 retval = 0;
3771 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003772 default: // Could be a hotkey?
3773 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003775 if (c == ':' && ex_cmd)
3776 {
3777 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003778 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003779 break;
3780 }
3781
Bram Moolenaar85a20022019-12-21 18:25:54 +01003782 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 c = MB_TOLOWER(c);
3784 retval = 1;
3785 for (i = 0; hotkeys[i]; ++i)
3786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (has_mbyte)
3788 {
3789 if ((*mb_ptr2char)(hotkeys + i) == c)
3790 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003791 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (hotkeys[i] == c)
3795 break;
3796 ++retval;
3797 }
3798 if (hotkeys[i])
3799 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003800 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 continue;
3802 }
3803 break;
3804 }
3805
3806 vim_free(hotkeys);
3807 }
3808
Bram Moolenaar27321db2020-07-06 21:24:57 +02003809 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 --no_wait_return;
3813 msg_end_prompt();
3814
3815 return retval;
3816}
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818/*
3819 * Copy one character from "*from" to "*to", taking care of multi-byte
3820 * characters. Return the length of the character in bytes.
3821 */
3822 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003823copy_char(
3824 char_u *from,
3825 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003826 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 int len;
3829 int c;
3830
3831 if (has_mbyte)
3832 {
3833 if (lowercase)
3834 {
3835 c = MB_TOLOWER((*mb_ptr2char)(from));
3836 return (*mb_char2bytes)(c, to);
3837 }
3838 else
3839 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003840 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 mch_memmove(to, from, (size_t)len);
3842 return len;
3843 }
3844 }
3845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 if (lowercase)
3848 *to = (char_u)TOLOWER_LOC(*from);
3849 else
3850 *to = *from;
3851 return 1;
3852 }
3853}
3854
3855/*
3856 * Format the dialog string, and display it at the bottom of
3857 * the screen. Return a string of hotkey chars (if defined) for
3858 * each 'button'. If a button has no hotkey defined, the first character of
3859 * the button is used.
3860 * The hotkeys can be multi-byte characters, but without combining chars.
3861 *
3862 * Returns an allocated string with hotkeys, or NULL for error.
3863 */
3864 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003865msg_show_console_dialog(
3866 char_u *message,
3867 char_u *buttons,
3868 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
3870 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003871#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003872 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u *hotk = NULL;
3874 char_u *msgp = NULL;
3875 char_u *hotkp = NULL;
3876 char_u *r;
3877 int copy;
3878#define HAS_HOTKEY_LEN 30
3879 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003880 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 int idx;
3882
3883 has_hotkey[0] = FALSE;
3884
3885 /*
3886 * First loop: compute the size of memory to allocate.
3887 * Second loop: copy to the allocated memory.
3888 */
3889 for (copy = 0; copy <= 1; ++copy)
3890 {
3891 r = buttons;
3892 idx = 0;
3893 while (*r)
3894 {
3895 if (*r == DLG_BUTTON_SEP)
3896 {
3897 if (copy)
3898 {
3899 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003900 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaar85a20022019-12-21 18:25:54 +01003902 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003904 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003907 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 if (dfltbutton)
3909 --dfltbutton;
3910
Bram Moolenaar85a20022019-12-21 18:25:54 +01003911 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3913 first_hotkey = TRUE;
3914 }
3915 else
3916 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003917 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3918 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (idx < HAS_HOTKEY_LEN - 1)
3920 has_hotkey[++idx] = FALSE;
3921 }
3922 }
3923 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3924 {
3925 if (*r == DLG_HOTKEY_CHAR)
3926 ++r;
3927 first_hotkey = FALSE;
3928 if (copy)
3929 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003930 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 *msgp++ = *r;
3932 else
3933 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003934 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3936 msgp += copy_char(r, msgp, FALSE);
3937 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3938
Bram Moolenaar85a20022019-12-21 18:25:54 +01003939 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003940 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 }
3943 else
3944 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003945 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (idx < HAS_HOTKEY_LEN - 1)
3947 has_hotkey[idx] = TRUE;
3948 }
3949 }
3950 else
3951 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003952 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (copy)
3954 msgp += copy_char(r, msgp, FALSE);
3955 }
3956
Bram Moolenaar85a20022019-12-21 18:25:54 +01003957 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003958 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960
3961 if (copy)
3962 {
3963 *msgp++ = ':';
3964 *msgp++ = ' ';
3965 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 else
3968 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003969 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003970 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003971 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003972 + 3); // for the ": " and NUL
3973 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
Bram Moolenaar85a20022019-12-21 18:25:54 +01003975 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (!has_hotkey[0])
3977 {
3978 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003979 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981
3982 /*
3983 * Now allocate and load the strings
3984 */
3985 vim_free(confirm_msg);
3986 confirm_msg = alloc(len);
3987 if (confirm_msg == NULL)
3988 return NULL;
3989 *confirm_msg = NUL;
3990 hotk = alloc(lenhotkey);
3991 if (hotk == NULL)
3992 return NULL;
3993
3994 *confirm_msg = '\n';
3995 STRCPY(confirm_msg + 1, message);
3996
3997 msgp = confirm_msg + 1 + STRLEN(message);
3998 hotkp = hotk;
3999
Bram Moolenaar85a20022019-12-21 18:25:54 +01004000 // Define first default hotkey. Keep the hotkey string NUL
4001 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004002 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
Bram Moolenaar85a20022019-12-21 18:25:54 +01004004 // Remember where the choices start, displaying starts here when
4005 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 confirm_msg_tail = msgp;
4007 *msgp++ = '\n';
4008 }
4009 }
4010
4011 display_confirm_msg();
4012 return hotk;
4013}
4014
4015/*
4016 * Display the ":confirm" message. Also called when screen resized.
4017 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004018 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004019display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004021 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 ++confirm_msg_used;
4023 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004024 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 --confirm_msg_used;
4026}
4027
Bram Moolenaar85a20022019-12-21 18:25:54 +01004028#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
4030#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4031
4032 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004033vim_dialog_yesno(
4034 int type,
4035 char_u *title,
4036 char_u *message,
4037 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
4039 if (do_dialog(type,
4040 title == NULL ? (char_u *)_("Question") : title,
4041 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004042 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return VIM_YES;
4044 return VIM_NO;
4045}
4046
4047 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004048vim_dialog_yesnocancel(
4049 int type,
4050 char_u *title,
4051 char_u *message,
4052 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053{
4054 switch (do_dialog(type,
4055 title == NULL ? (char_u *)_("Question") : title,
4056 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004057 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
4059 case 1: return VIM_YES;
4060 case 2: return VIM_NO;
4061 }
4062 return VIM_CANCEL;
4063}
4064
4065 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004066vim_dialog_yesnoallcancel(
4067 int type,
4068 char_u *title,
4069 char_u *message,
4070 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
4072 switch (do_dialog(type,
4073 title == NULL ? (char_u *)"Question" : title,
4074 message,
4075 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004076 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 case 1: return VIM_YES;
4079 case 2: return VIM_NO;
4080 case 3: return VIM_ALL;
4081 case 4: return VIM_DISCARDALL;
4082 }
4083 return VIM_CANCEL;
4084}
4085
Bram Moolenaar85a20022019-12-21 18:25:54 +01004086#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004088#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004089static char *e_printf = N_("E766: Insufficient arguments for printf()");
4090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004091/*
4092 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4093 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004094 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004095tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096{
4097 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004098 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099 int err = FALSE;
4100
4101 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004102 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004103 else
4104 {
4105 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004106 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004107 if (err)
4108 n = 0;
4109 }
4110 return n;
4111}
4112
4113/*
4114 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004115 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004116 * are not converted to a string.
4117 * If "tofree" is not NULL echo_string() is used. All types are converted to
4118 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004119 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 */
4121 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004122tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004123{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004124 int idx = *idxp - 1;
4125 char *s = NULL;
4126 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127
4128 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004129 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 else
4131 {
4132 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004133 if (tofree != NULL)
4134 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4135 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004136 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004137 }
4138 return s;
4139}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004140
4141# ifdef FEAT_FLOAT
4142/*
4143 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4144 */
4145 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004146tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004147{
4148 int idx = *idxp - 1;
4149 double f = 0;
4150
4151 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004152 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004153 else
4154 {
4155 ++*idxp;
4156 if (tvs[idx].v_type == VAR_FLOAT)
4157 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004158 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004159 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004160 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004161 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004162 }
4163 return f;
4164}
4165# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004166#endif
4167
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004168#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004169/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004170 * Return the representation of infinity for printf() function:
4171 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4172 */
4173 static const char *
4174infinity_str(int positive,
4175 char fmt_spec,
4176 int force_sign,
4177 int space_for_positive)
4178{
4179 static const char *table[] =
4180 {
4181 "-inf", "inf", "+inf", " inf",
4182 "-INF", "INF", "+INF", " INF"
4183 };
4184 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4185
4186 if (ASCII_ISUPPER(fmt_spec))
4187 idx += 4;
4188 return table[idx];
4189}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004190#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004191
4192/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004193 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004194 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004195 * consistency.
4196 *
4197 * This code is based on snprintf.c - a portable implementation of snprintf
4198 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004199 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004200 * The original code, including useful comments, can be found here:
4201 * http://www.ijs.si/software/snprintf/
4202 *
4203 * This snprintf() only supports the following conversion specifiers:
4204 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4205 * with flags: '-', '+', ' ', '0' and '#'.
4206 * An asterisk is supported for field width as well as precision.
4207 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004208 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004209 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004210 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004211 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004212 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004213 * The locale is not used, the string is used as a byte string. This is only
4214 * relevant for double-byte encodings where the second byte may be '%'.
4215 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004216 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4217 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004218 *
4219 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004220 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004221 * is greater or equal to "str_m", not all characters from the result
4222 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4223 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004224 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004225 */
4226
4227/*
4228 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004229 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004230 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004231 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004232 */
4233
Bram Moolenaar85a20022019-12-21 18:25:54 +01004234// When generating prototypes all of this is skipped, cproto doesn't
4235// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004236#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004237
Bram Moolenaar85a20022019-12-21 18:25:54 +01004238// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004239 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004240vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004241{
4242 va_list ap;
4243 int str_l;
4244 size_t len = STRLEN(str);
4245 size_t space;
4246
4247 if (str_m <= len)
4248 space = 0;
4249 else
4250 space = str_m - len;
4251 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004252 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004253 va_end(ap);
4254 return str_l;
4255}
Bram Moolenaara800b422010-06-27 01:15:55 +02004256
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004257 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004258vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004259{
4260 va_list ap;
4261 int str_l;
4262
4263 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004264 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004265 va_end(ap);
4266 return str_l;
4267}
4268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004269 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004270vim_vsnprintf(
4271 char *str,
4272 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004273 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004274 va_list ap)
4275{
4276 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4277}
4278
4279 int
4280vim_vsnprintf_typval(
4281 char *str,
4282 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004283 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004284 va_list ap,
4285 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004286{
4287 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004288 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004289 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004290
4291 if (p == NULL)
4292 p = "";
4293 while (*p != NUL)
4294 {
4295 if (*p != '%')
4296 {
4297 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004298 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004299
Bram Moolenaar85a20022019-12-21 18:25:54 +01004300 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004301 if (str_l < str_m)
4302 {
4303 size_t avail = str_m - str_l;
4304
4305 mch_memmove(str + str_l, p, n > avail ? avail : n);
4306 }
4307 p += n;
4308 str_l += n;
4309 }
4310 else
4311 {
4312 size_t min_field_width = 0, precision = 0;
4313 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4314 int alternate_form = 0, force_sign = 0;
4315
Bram Moolenaar85a20022019-12-21 18:25:54 +01004316 // If both the ' ' and '+' flags appear, the ' ' flag should be
4317 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004318 int space_for_positive = 1;
4319
Bram Moolenaar85a20022019-12-21 18:25:54 +01004320 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004321 char length_modifier = '\0';
4322
Bram Moolenaar85a20022019-12-21 18:25:54 +01004323 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004324# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004325# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4326 // That sounds reasonable to use as the maximum
4327 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004328# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004329# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004330# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004331 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004332
Bram Moolenaar85a20022019-12-21 18:25:54 +01004333 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004334 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004335
Bram Moolenaar85a20022019-12-21 18:25:54 +01004336 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004337 size_t str_arg_l;
4338
Bram Moolenaar85a20022019-12-21 18:25:54 +01004339 // unsigned char argument value - only defined for c conversion.
4340 // N.B. standard explicitly states the char argument for the c
4341 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004342 unsigned char uchar_arg;
4343
Bram Moolenaar85a20022019-12-21 18:25:54 +01004344 // number of zeros to be inserted for numeric conversions as
4345 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004346 size_t number_of_zeros_to_pad = 0;
4347
Bram Moolenaar85a20022019-12-21 18:25:54 +01004348 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004349 size_t zero_padding_insertion_ind = 0;
4350
Bram Moolenaar85a20022019-12-21 18:25:54 +01004351 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004352 char fmt_spec = '\0';
4353
Bram Moolenaar85a20022019-12-21 18:25:54 +01004354 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004355 char_u *tofree = NULL;
4356
4357
Bram Moolenaar85a20022019-12-21 18:25:54 +01004358 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004359
Bram Moolenaar85a20022019-12-21 18:25:54 +01004360 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004361 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4362 || *p == '#' || *p == '\'')
4363 {
4364 switch (*p)
4365 {
4366 case '0': zero_padding = 1; break;
4367 case '-': justify_left = 1; break;
4368 case '+': force_sign = 1; space_for_positive = 0; break;
4369 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004370 // If both the ' ' and '+' flags appear, the ' '
4371 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004372 break;
4373 case '#': alternate_form = 1; break;
4374 case '\'': break;
4375 }
4376 p++;
4377 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004378 // If the '0' and '-' flags both appear, the '0' flag should be
4379 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004380
Bram Moolenaar85a20022019-12-21 18:25:54 +01004381 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004382 if (*p == '*')
4383 {
4384 int j;
4385
4386 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004389 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390# endif
4391 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004392 if (j >= 0)
4393 min_field_width = j;
4394 else
4395 {
4396 min_field_width = -j;
4397 justify_left = 1;
4398 }
4399 }
4400 else if (VIM_ISDIGIT((int)(*p)))
4401 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004402 // size_t could be wider than unsigned int; make sure we treat
4403 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004404 unsigned int uj = *p++ - '0';
4405
4406 while (VIM_ISDIGIT((int)(*p)))
4407 uj = 10 * uj + (unsigned int)(*p++ - '0');
4408 min_field_width = uj;
4409 }
4410
Bram Moolenaar85a20022019-12-21 18:25:54 +01004411 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004412 if (*p == '.')
4413 {
4414 p++;
4415 precision_specified = 1;
4416 if (*p == '*')
4417 {
4418 int j;
4419
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004422 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423# endif
4424 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004425 p++;
4426 if (j >= 0)
4427 precision = j;
4428 else
4429 {
4430 precision_specified = 0;
4431 precision = 0;
4432 }
4433 }
4434 else if (VIM_ISDIGIT((int)(*p)))
4435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004436 // size_t could be wider than unsigned int; make sure we
4437 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004438 unsigned int uj = *p++ - '0';
4439
4440 while (VIM_ISDIGIT((int)(*p)))
4441 uj = 10 * uj + (unsigned int)(*p++ - '0');
4442 precision = uj;
4443 }
4444 }
4445
Bram Moolenaar85a20022019-12-21 18:25:54 +01004446 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004447 if (*p == 'h' || *p == 'l')
4448 {
4449 length_modifier = *p;
4450 p++;
4451 if (length_modifier == 'l' && *p == 'l')
4452 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004453 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004454 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004455 p++;
4456 }
4457 }
4458 fmt_spec = *p;
4459
Bram Moolenaar85a20022019-12-21 18:25:54 +01004460 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461 switch (fmt_spec)
4462 {
4463 case 'i': fmt_spec = 'd'; break;
4464 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4465 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4466 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4467 default: break;
4468 }
4469
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004470# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004471 switch (fmt_spec)
4472 {
4473 case 'd': case 'u': case 'o': case 'x': case 'X':
4474 if (tvs != NULL && length_modifier == '\0')
4475 length_modifier = 'L';
4476 }
4477# endif
4478
Bram Moolenaar85a20022019-12-21 18:25:54 +01004479 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004480 switch (fmt_spec)
4481 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004482 // '%' and 'c' behave similar to 's' regarding flags and field
4483 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004484 case '%':
4485 case 'c':
4486 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004487 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004488 str_arg_l = 1;
4489 switch (fmt_spec)
4490 {
4491 case '%':
4492 str_arg = p;
4493 break;
4494
4495 case 'c':
4496 {
4497 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498
4499 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004501 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502# endif
4503 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004504 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004505 uchar_arg = (unsigned char)j;
4506 str_arg = (char *)&uchar_arg;
4507 break;
4508 }
4509
4510 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004511 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004514 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004515# endif
4516 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004517 if (str_arg == NULL)
4518 {
4519 str_arg = "[NULL]";
4520 str_arg_l = 6;
4521 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004522 // make sure not to address string beyond the specified
4523 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004524 else if (!precision_specified)
4525 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004526 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 else if (precision == 0)
4528 str_arg_l = 0;
4529 else
4530 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004531 // Don't put the #if inside memchr(), it can be a
4532 // macro.
4533 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004534 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004535 precision <= (size_t)0x7fffffffL ? precision
4536 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004537 str_arg_l = (q == NULL) ? precision
4538 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004539 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004540 if (fmt_spec == 'S')
4541 {
4542 if (min_field_width != 0)
4543 min_field_width += STRLEN(str_arg)
4544 - mb_string2cells((char_u *)str_arg, -1);
4545 if (precision)
4546 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004547 char_u *p1;
4548 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004549
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004550 for (p1 = (char_u *)str_arg; *p1;
4551 p1 += mb_ptr2len(p1))
4552 {
4553 i += (size_t)mb_ptr2cells(p1);
4554 if (i > precision)
4555 break;
4556 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004557 str_arg_l = precision = p1 - (char_u *)str_arg;
4558 }
4559 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004560 break;
4561
4562 default:
4563 break;
4564 }
4565 break;
4566
Bram Moolenaar91984b92016-08-16 21:58:41 +02004567 case 'd': case 'u':
4568 case 'b': case 'B':
4569 case 'o':
4570 case 'x': case 'X':
4571 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004572 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004573 // NOTE: the u, b, o, x, X and p conversion specifiers
4574 // imply the value is unsigned; d implies a signed
4575 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004576
Bram Moolenaar85a20022019-12-21 18:25:54 +01004577 // 0 if numeric argument is zero (or if pointer is
4578 // NULL for 'p'), +1 if greater than zero (or nonzero
4579 // for unsigned arguments), -1 if negative (unsigned
4580 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004581 int arg_sign = 0;
4582
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004583 // only set for length modifier h, or for no length
4584 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004585 int int_arg = 0;
4586 unsigned int uint_arg = 0;
4587
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004588 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004589 long int long_arg = 0;
4590 unsigned long int ulong_arg = 0;
4591
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004592 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004593 varnumber_T llong_arg = 0;
4594 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004595
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004596 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004597 uvarnumber_T bin_arg = 0;
4598
Bram Moolenaar85a20022019-12-21 18:25:54 +01004599 // pointer argument value -only defined for p
4600 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004601 void *ptr_arg = NULL;
4602
4603 if (fmt_spec == 'p')
4604 {
4605 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004608 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4609 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610# endif
4611 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004612 if (ptr_arg != NULL)
4613 arg_sign = 1;
4614 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004615 else if (fmt_spec == 'b' || fmt_spec == 'B')
4616 {
4617 bin_arg =
4618# if defined(FEAT_EVAL)
4619 tvs != NULL ?
4620 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4621# endif
4622 va_arg(ap, uvarnumber_T);
4623 if (bin_arg != 0)
4624 arg_sign = 1;
4625 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004626 else if (fmt_spec == 'd')
4627 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004628 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004629 switch (length_modifier)
4630 {
4631 case '\0':
4632 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004633 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004636 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637# endif
4638 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004639 if (int_arg > 0)
4640 arg_sign = 1;
4641 else if (int_arg < 0)
4642 arg_sign = -1;
4643 break;
4644 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004646# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004647 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648# endif
4649 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004650 if (long_arg > 0)
4651 arg_sign = 1;
4652 else if (long_arg < 0)
4653 arg_sign = -1;
4654 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004655 case 'L':
4656 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004657# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004658 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004659# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004660 va_arg(ap, varnumber_T);
4661 if (llong_arg > 0)
4662 arg_sign = 1;
4663 else if (llong_arg < 0)
4664 arg_sign = -1;
4665 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004666 }
4667 }
4668 else
4669 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004670 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004671 switch (length_modifier)
4672 {
4673 case '\0':
4674 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004675 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004677 tvs != NULL ? (unsigned)
4678 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679# endif
4680 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004681 if (uint_arg != 0)
4682 arg_sign = 1;
4683 break;
4684 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004685 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004686# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004687 tvs != NULL ? (unsigned long)
4688 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004689# endif
4690 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004691 if (ulong_arg != 0)
4692 arg_sign = 1;
4693 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004694 case 'L':
4695 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004696# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004697 tvs != NULL ? (uvarnumber_T)
4698 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004699# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004700 va_arg(ap, uvarnumber_T);
4701 if (ullong_arg != 0)
4702 arg_sign = 1;
4703 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004704 }
4705 }
4706
4707 str_arg = tmp;
4708 str_arg_l = 0;
4709
Bram Moolenaar85a20022019-12-21 18:25:54 +01004710 // NOTE:
4711 // For d, i, u, o, x, and X conversions, if precision is
4712 // specified, the '0' flag should be ignored. This is so
4713 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4714 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004715 if (precision_specified)
4716 zero_padding = 0;
4717 if (fmt_spec == 'd')
4718 {
4719 if (force_sign && arg_sign >= 0)
4720 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004721 // leave negative numbers for sprintf to handle, to
4722 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004723 }
4724 else if (alternate_form)
4725 {
4726 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004727 && (fmt_spec == 'b' || fmt_spec == 'B'
4728 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004729 {
4730 tmp[str_arg_l++] = '0';
4731 tmp[str_arg_l++] = fmt_spec;
4732 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004733 // alternate form should have no effect for p
4734 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004735 }
4736
4737 zero_padding_insertion_ind = str_arg_l;
4738 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004739 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004740 if (precision == 0 && arg_sign == 0)
4741 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004742 // When zero value is formatted with an explicit
4743 // precision 0, the resulting formatted string is
4744 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004745 }
4746 else
4747 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004748 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004749 int f_l = 0;
4750
Bram Moolenaar85a20022019-12-21 18:25:54 +01004751 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004752 f[f_l++] = '%';
4753 if (!length_modifier)
4754 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004755 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004756 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004757# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004758 f[f_l++] = 'I';
4759 f[f_l++] = '6';
4760 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004761# else
4762 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004763 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004764# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004765 }
4766 else
4767 f[f_l++] = length_modifier;
4768 f[f_l++] = fmt_spec;
4769 f[f_l++] = '\0';
4770
4771 if (fmt_spec == 'p')
4772 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004773 else if (fmt_spec == 'b' || fmt_spec == 'B')
4774 {
4775 char b[8 * sizeof(uvarnumber_T)];
4776 size_t b_l = 0;
4777 uvarnumber_T bn = bin_arg;
4778
4779 do
4780 {
4781 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4782 bn >>= 1;
4783 }
4784 while (bn != 0);
4785
4786 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4787 str_arg_l += b_l;
4788 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004789 else if (fmt_spec == 'd')
4790 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004791 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004792 switch (length_modifier)
4793 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004794 case '\0': str_arg_l += sprintf(
4795 tmp + str_arg_l, f,
4796 int_arg);
4797 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004798 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004799 tmp + str_arg_l, f,
4800 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004801 break;
4802 case 'l': str_arg_l += sprintf(
4803 tmp + str_arg_l, f, long_arg);
4804 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004805 case 'L': str_arg_l += sprintf(
4806 tmp + str_arg_l, f, llong_arg);
4807 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004808 }
4809 }
4810 else
4811 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004812 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004813 switch (length_modifier)
4814 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004815 case '\0': str_arg_l += sprintf(
4816 tmp + str_arg_l, f,
4817 uint_arg);
4818 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004819 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004820 tmp + str_arg_l, f,
4821 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004822 break;
4823 case 'l': str_arg_l += sprintf(
4824 tmp + str_arg_l, f, ulong_arg);
4825 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004826 case 'L': str_arg_l += sprintf(
4827 tmp + str_arg_l, f, ullong_arg);
4828 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004829 }
4830 }
4831
Bram Moolenaar85a20022019-12-21 18:25:54 +01004832 // include the optional minus sign and possible
4833 // "0x" in the region before the zero padding
4834 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004835 if (zero_padding_insertion_ind < str_arg_l
4836 && tmp[zero_padding_insertion_ind] == '-')
4837 zero_padding_insertion_ind++;
4838 if (zero_padding_insertion_ind + 1 < str_arg_l
4839 && tmp[zero_padding_insertion_ind] == '0'
4840 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4841 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4842 zero_padding_insertion_ind += 2;
4843 }
4844
4845 {
4846 size_t num_of_digits = str_arg_l
4847 - zero_padding_insertion_ind;
4848
4849 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004850 // unless zero is already the first
4851 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004852 && !(zero_padding_insertion_ind < str_arg_l
4853 && tmp[zero_padding_insertion_ind] == '0'))
4854 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004855 // assure leading zero for alternate-form
4856 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004857 if (!precision_specified
4858 || precision < num_of_digits + 1)
4859 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004860 // precision is increased to force the
4861 // first character to be zero, except if a
4862 // zero value is formatted with an
4863 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004864 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004865 }
4866 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004867 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004868 if (num_of_digits < precision)
4869 number_of_zeros_to_pad = precision - num_of_digits;
4870 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004871 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004872 if (!justify_left && zero_padding)
4873 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004874 int n = (int)(min_field_width - (str_arg_l
4875 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004876 if (n > 0)
4877 number_of_zeros_to_pad += n;
4878 }
4879 break;
4880 }
4881
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004882# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004883 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004884 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004885 case 'e':
4886 case 'E':
4887 case 'g':
4888 case 'G':
4889 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004890 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004891 double f;
4892 double abs_f;
4893 char format[40];
4894 int l;
4895 int remove_trailing_zeroes = FALSE;
4896
4897 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004898# if defined(FEAT_EVAL)
4899 tvs != NULL ? tv_float(tvs, &arg_idx) :
4900# endif
4901 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004902 abs_f = f < 0 ? -f : f;
4903
4904 if (fmt_spec == 'g' || fmt_spec == 'G')
4905 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004906 // Would be nice to use %g directly, but it prints
4907 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004908 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4909 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004910 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004911 else
4912 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4913 remove_trailing_zeroes = TRUE;
4914 }
4915
Bram Moolenaar04186092016-08-29 21:55:35 +02004916 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004917# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004918 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004919# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004920 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004921# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004922 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004923 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004924 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004925 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4926 force_sign, space_for_positive));
4927 str_arg_l = STRLEN(tmp);
4928 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004929 }
4930 else
4931 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004932 if (isnan(f))
4933 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004934 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004935 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4936 : "nan");
4937 str_arg_l = 3;
4938 zero_padding = 0;
4939 }
4940 else if (isinf(f))
4941 {
4942 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4943 force_sign, space_for_positive));
4944 str_arg_l = STRLEN(tmp);
4945 zero_padding = 0;
4946 }
4947 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004949 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004950 format[0] = '%';
4951 l = 1;
4952 if (force_sign)
4953 format[l++] = space_for_positive ? ' ' : '+';
4954 if (precision_specified)
4955 {
4956 size_t max_prec = TMP_LEN - 10;
4957
Bram Moolenaar85a20022019-12-21 18:25:54 +01004958 // Make sure we don't get more digits than we
4959 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004960 if ((fmt_spec == 'f' || fmt_spec == 'F')
4961 && abs_f > 1.0)
4962 max_prec -= (size_t)log10(abs_f);
4963 if (precision > max_prec)
4964 precision = max_prec;
4965 l += sprintf(format + l, ".%d", (int)precision);
4966 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004967 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004968 format[l + 1] = NUL;
4969
Bram Moolenaare9997822016-08-28 16:03:38 +02004970 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004971 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004972
Bram Moolenaara7241f52008-06-24 20:39:31 +00004973 if (remove_trailing_zeroes)
4974 {
4975 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004976 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004977
Bram Moolenaar85a20022019-12-21 18:25:54 +01004978 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004979 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004980 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004981 else
4982 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004983 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004984 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004985 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004986 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004987 // Remove superfluous '+' and leading
4988 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004989 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004990 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004991 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004992 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004993 --str_arg_l;
4994 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004995 i = (tp[1] == '-') ? 2 : 1;
4996 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004997 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004998 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004999 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005000 --str_arg_l;
5001 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005002 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005003 }
5004 }
5005
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005006 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005007 // Remove trailing zeroes, but keep the one
5008 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005009 while (tp > tmp + 2 && *tp == '0'
5010 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005011 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005012 STRMOVE(tp, tp + 1);
5013 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005014 --str_arg_l;
5015 }
5016 }
5017 else
5018 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005019 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005020
Bram Moolenaar85a20022019-12-21 18:25:54 +01005021 // Be consistent: some printf("%e") use 1.0e+12
5022 // and some 1.0e+012. Remove one zero in the last
5023 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005024 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005025 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005026 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5027 && tp[2] == '0'
5028 && vim_isdigit(tp[3])
5029 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005030 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005031 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005032 --str_arg_l;
5033 }
5034 }
5035 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005036 if (zero_padding && min_field_width > str_arg_l
5037 && (tmp[0] == '-' || force_sign))
5038 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005039 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02005040 number_of_zeros_to_pad = min_field_width - str_arg_l;
5041 zero_padding_insertion_ind = 1;
5042 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005043 str_arg = tmp;
5044 break;
5045 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005046# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005047
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005048 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01005049 // unrecognized conversion specifier, keep format string
5050 // as-is
5051 zero_padding = 0; // turn zero padding off for non-numeric
5052 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005053 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01005054 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005055
Bram Moolenaar85a20022019-12-21 18:25:54 +01005056 // discard the unrecognized conversion, just keep *
5057 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005058 str_arg = p;
5059 str_arg_l = 0;
5060 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005061 str_arg_l++; // include invalid conversion specifier
5062 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 break;
5064 }
5065
5066 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005067 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005068
Bram Moolenaar85a20022019-12-21 18:25:54 +01005069 // insert padding to the left as requested by min_field_width;
5070 // this does not include the zero padding in case of numerical
5071 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072 if (!justify_left)
5073 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005074 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005075 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005076
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005077 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005078 {
5079 if (str_l < str_m)
5080 {
5081 size_t avail = str_m - str_l;
5082
5083 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005084 (size_t)pn > avail ? avail
5085 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005086 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005087 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005088 }
5089 }
5090
Bram Moolenaar85a20022019-12-21 18:25:54 +01005091 // zero padding as requested by the precision or by the minimal
5092 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005093 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005095 // will not copy first part of numeric right now, *
5096 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005097 zero_padding_insertion_ind = 0;
5098 }
5099 else
5100 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005101 // insert first part of numerics (sign or '0x') before zero
5102 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005103 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005104
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005105 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005106 {
5107 if (str_l < str_m)
5108 {
5109 size_t avail = str_m - str_l;
5110
5111 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005112 (size_t)zn > avail ? avail
5113 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005114 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005115 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005116 }
5117
Bram Moolenaar85a20022019-12-21 18:25:54 +01005118 // insert zero padding as requested by the precision or min
5119 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005120 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005121 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005122 {
5123 if (str_l < str_m)
5124 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005125 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005126
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005127 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005128 (size_t)zn > avail ? avail
5129 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005130 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005131 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005132 }
5133 }
5134
Bram Moolenaar85a20022019-12-21 18:25:54 +01005135 // insert formatted string
5136 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005137 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005138 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005139
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005140 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005141 {
5142 if (str_l < str_m)
5143 {
5144 size_t avail = str_m - str_l;
5145
5146 mch_memmove(str + str_l,
5147 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005148 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005149 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005150 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005151 }
5152 }
5153
Bram Moolenaar85a20022019-12-21 18:25:54 +01005154 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005155 if (justify_left)
5156 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005157 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005158 int pn = (int)(min_field_width
5159 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005160
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005161 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005162 {
5163 if (str_l < str_m)
5164 {
5165 size_t avail = str_m - str_l;
5166
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005167 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005168 (size_t)pn > avail ? avail
5169 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005170 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005171 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005172 }
5173 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005174 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005175 }
5176 }
5177
5178 if (str_m > 0)
5179 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005180 // make sure the string is nul-terminated even at the expense of
5181 // overwriting the last character (shouldn't happen, but just in case)
5182 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005183 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5184 }
5185
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005186 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005187 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188
Bram Moolenaar85a20022019-12-21 18:25:54 +01005189 // Return the number of characters formatted (excluding trailing nul
5190 // character), that is, the number of characters that would have been
5191 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005192 return (int)str_l;
5193}
5194
Bram Moolenaar85a20022019-12-21 18:25:54 +01005195#endif // PROTO