blob: 8adc224115ecaedb6a7c70847b0ad58dc94c0f07 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar44ca54a2016-08-27 15:41:32 +020015#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17#include "vim.h"
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static void add_msg_hist(char_u *s, int len, int attr);
20static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010022static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int msg_check_screen(void);
33static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010036static int confirm_msg_used = FALSE; // displaying confirm_msg
37static char_u *confirm_msg = NULL; // ":confirm" message
38static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010041#ifdef FEAT_JOB_CHANNEL
42static int emsg_to_channel_log = FALSE;
43#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020056static FILE *verbose_fd = NULL;
57static int verbose_did_open = FALSE;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059/*
60 * When writing messages to the screen, there are many different situations.
61 * A number of variables is used to remember the current state:
62 * msg_didany TRUE when messages were written since the last time the
63 * user reacted to a prompt.
64 * Reset: After hitting a key for the hit-return prompt,
65 * hitting <CR> for the command line or input().
66 * Set: When any message is written to the screen.
67 * msg_didout TRUE when something was written to the current line.
68 * Reset: When advancing to the next line, when the current
69 * text can be overwritten.
70 * Set: When any message is written to the screen.
71 * msg_nowait No extra delay for the last drawn message.
72 * Used in normal_cmd() before the mode message is drawn.
73 * emsg_on_display There was an error message recently. Indicates that there
74 * should be a delay before redrawing.
75 * msg_scroll The next message should not overwrite the current one.
76 * msg_scrolled How many lines the screen has been scrolled (because of
77 * messages). Used in update_screen() to scroll the screen
78 * back. Incremented each time the screen scrolls a line.
79 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
80 * writes something without scrolling should not make
81 * need_wait_return to be set. This is a hack to make ":ts"
82 * work without an extra prompt.
83 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010084 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 * need_wait_return TRUE when the hit-return prompt is needed.
86 * Reset: After giving the hit-return prompt, when the user
87 * has answered some other prompt.
88 * Set: When the ruler or typeahead display is overwritten,
89 * scrolling the screen for some message.
90 * keep_msg Message to be displayed after redrawing the screen, in
91 * main_loop().
92 * This is an allocated string or NULL when not used.
93 */
94
95/*
96 * msg(s) - displays the string 's' on the status line
97 * When terminal not initialized (yet) mch_errmsg(..) is used.
98 * return TRUE if wait_return not called
99 */
100 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100101msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 return msg_attr_keep(s, 0, FALSE);
104}
105
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106/*
107 * Like msg() but keep it silent when 'verbosefile' is set.
108 */
109 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100110verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111{
112 int n;
113
114 verbose_enter();
115 n = msg_attr_keep(s, 0, FALSE);
116 verbose_leave();
117
118 return n;
119}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100122msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 return msg_attr_keep(s, attr, FALSE);
125}
126
127 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100128msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100129 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100130 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100131 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 static int entered = 0;
134 int retval;
135 char_u *buf = NULL;
136
Bram Moolenaar85a20022019-12-21 18:25:54 +0100137 // Skip messages not matching ":filter pattern".
138 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100139 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200140 return TRUE;
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_EVAL
143 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100144 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
146
147 /*
148 * It is possible that displaying a messages causes a problem (e.g.,
149 * when redrawing the window), which causes another message, etc.. To
150 * break this loop, limit the recursiveness to 3 levels.
151 */
152 if (entered >= 3)
153 return TRUE;
154 ++entered;
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // Add message to history (unless it's a repeated kept message or a
157 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100158 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 || (*s != '<'
160 && last_msg_hist != NULL
161 && last_msg_hist->msg != NULL
162 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100163 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaar958dc692016-12-01 15:34:12 +0100165#ifdef FEAT_JOB_CHANNEL
166 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100167 // Write message in the channel log.
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200168 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100169#endif
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000172 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100173 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar32526b32019-01-19 17:43:09 +0100177 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 msg_clr_eos();
179 retval = msg_end();
180
Bram Moolenaar32526b32019-01-19 17:43:09 +0100181 if (keep && retval && vim_strsize((char_u *)s)
182 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
183 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185 vim_free(buf);
186 --entered;
187 return retval;
188}
189
190/*
191 * Truncate a string such that it can be printed without causing a scroll.
192 * Returns an allocated string or NULL when no truncating is done.
193 */
194 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100195msg_strtrunc(
196 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 char_u *buf = NULL;
200 int len;
201 int room;
202
Bram Moolenaar85a20022019-12-21 18:25:54 +0100203 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000204 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
205 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000208 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100209 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000210 room = (int)(Rows - msg_row) * Columns - 1;
211 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100212 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000213 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if (len > room && room > 0)
215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // may have up to 18 bytes per cell (6 per char, up to two
218 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100219 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100221 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100222 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = room + 2;
225 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229 }
230 return buf;
231}
232
233/*
234 * Truncate a string "s" to "buf" with cell width "room".
235 * "s" and "buf" may be equal.
236 */
237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100238trunc_string(
239 char_u *s,
240 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200241 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100242 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100244 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200245 size_t half;
246 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 int e;
248 int i;
249 int n;
250
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200251 if (room_in < 3)
252 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254
Bram Moolenaar85a20022019-12-21 18:25:54 +0100255 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100256 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
258 if (s[e] == NUL)
259 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100260 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 buf[e] = NUL;
262 return;
263 }
264 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200265 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 break;
267 len += n;
268 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000270 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100272 if (++e == buflen)
273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 buf[e] = s[e];
275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
277
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 if (enc_dbcs != 0)
281 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100282 // For DBCS going backwards in a string is slow, but
283 // computing the cell width isn't too slow: go forward
284 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 n = vim_strsize(s + i);
286 while (len + n > room)
287 {
288 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000289 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 }
291 }
292 else if (enc_utf8)
293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000295 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 for (;;)
297 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000298 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200299 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200300 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200302 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 break;
304 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200305 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307 }
308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100310 for (i = (int)STRLEN(s);
311 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 len += n;
313 }
314
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200315
316 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100317 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100318 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200319 if (s != buf)
320 {
321 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200322 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200323 len = buflen - 1;
324 len = len - e + 1;
325 if (len < 1)
326 buf[e - 1] = NUL;
327 else
328 mch_memmove(buf + e, s + e, len);
329 }
330 }
331 else if (e + 3 < buflen)
332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100333 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100334 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200335 len = STRLEN(s + i) + 1;
336 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100337 len = buflen - e - 3 - 1;
338 mch_memmove(buf + e + 3, s + i, len);
339 buf[e + 3 + len - 1] = NUL;
340 }
341 else
342 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100343 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200344 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346}
347
348/*
349 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100350 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 * shorter than IOSIZE!!!
352 */
353#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100355int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200360 if (IObuff == NULL)
361 {
362 // Very early in initialisation and already something wrong, just
363 // give the raw message so the user at least gets a hint.
364 return msg((char *)s);
365 }
366 else
367 {
368 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200370 va_start(arglist, s);
371 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
372 va_end(arglist);
373 return msg((char *)IObuff);
374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375}
376
377 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100378smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200380 if (IObuff == NULL)
381 {
382 // Very early in initialisation and already something wrong, just
383 // give the raw message so the user at least gets a hint.
384 return msg_attr((char *)s, attr);
385 }
386 else
387 {
388 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200390 va_start(arglist, s);
391 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
392 va_end(arglist);
393 return msg_attr((char *)IObuff, attr);
394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395}
396
Bram Moolenaare0429682018-07-01 16:44:03 +0200397 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100398smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200399{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200400 if (IObuff == NULL)
401 {
402 // Very early in initialisation and already something wrong, just
403 // give the raw message so the user at least gets a hint.
404 return msg_attr_keep((char *)s, attr, TRUE);
405 }
406 else
407 {
408 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200409
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200410 va_start(arglist, s);
411 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
412 va_end(arglist);
413 return msg_attr_keep((char *)IObuff, attr, TRUE);
414 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200415}
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#endif
418
419/*
420 * Remember the last sourcing name/lnum used in an error message, so that it
421 * isn't printed each time when it didn't change.
422 */
423static int last_sourcing_lnum = 0;
424static char_u *last_sourcing_name = NULL;
425
426/*
427 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
428 * for the next error message;
429 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000430 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100431reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100433 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 last_sourcing_lnum = 0;
435}
436
437/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100438 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000439 */
440 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100441other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000442{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100443 if (SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000444 {
445 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100446 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000447 return TRUE;
448 }
449 return FALSE;
450}
451
452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * Get the message about the source, as used for an error message.
454 * Returns an allocated string with room for one more character.
455 * Returns NULL when no message is to be given.
456 */
457 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100458get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 char_u *Buf, *p;
461
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100462 if (SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200464 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100465 char_u *tofree = sname;
466
467 if (sname == NULL)
468 sname = SOURCING_NAME;
469
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200470#ifdef FEAT_EVAL
471 if (estack_compiling)
472 p = (char_u *)_("Error detected while compiling %s:");
473 else
474#endif
475 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100476 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100478 sprintf((char *)Buf, (char *)p, sname);
479 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 return Buf;
481 }
482 return NULL;
483}
484
485/*
486 * Get the message about the source lnum, as used for an error message.
487 * Returns an allocated string with room for one more character.
488 * Returns NULL when no message is to be given.
489 */
490 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100491get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 char_u *Buf, *p;
494
Bram Moolenaar85a20022019-12-21 18:25:54 +0100495 // lnum is 0 when executing a command from the command line
496 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100497 if (SOURCING_NAME != NULL
498 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
499 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200502 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100504 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 return Buf;
506 }
507 return NULL;
508}
509
510/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000511 * Display name and line number for the source of an error.
512 * Remember the file name and line number, so that for the next error the info
513 * is only displayed if it changed.
514 */
515 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100516msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000517{
518 char_u *p;
519
520 ++no_wait_return;
521 p = get_emsg_source();
522 if (p != NULL)
523 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100524 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000525 vim_free(p);
526 }
527 p = get_emsg_lnum();
528 if (p != NULL)
529 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100530 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000531 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100532 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000533 }
534
Bram Moolenaar85a20022019-12-21 18:25:54 +0100535 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100536 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537 {
538 vim_free(last_sourcing_name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100539 if (SOURCING_NAME == NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000540 last_sourcing_name = NULL;
541 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100542 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000543 }
544 --no_wait_return;
545}
546
547/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000548 * Return TRUE if not giving error messages right now:
549 * If "emsg_off" is set: no error messages at the moment.
550 * If "msg" is in 'debug': do error message but without side effects.
551 * If "emsg_skip" is set: never do error messages.
552 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200553 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100554emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000555{
556 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
557 && vim_strchr(p_debug, 't') == NULL)
558#ifdef FEAT_EVAL
559 || emsg_skip > 0
560#endif
561 )
562 return TRUE;
563 return FALSE;
564}
565
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100566#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100567static garray_T ignore_error_list = GA_EMPTY;
568
569 void
570ignore_error_for_testing(char_u *error)
571{
572 if (ignore_error_list.ga_itemsize == 0)
573 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
574
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100575 if (STRCMP("RESET", error) == 0)
576 ga_clear_strings(&ignore_error_list);
577 else
578 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100579}
580
581 static int
582ignore_error(char_u *msg)
583{
584 int i;
585
586 for (i = 0; i < ignore_error_list.ga_len; ++i)
587 if (strstr((char *)msg,
588 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
589 return TRUE;
590 return FALSE;
591}
592#endif
593
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200594#if !defined(HAVE_STRERROR) || defined(PROTO)
595/*
596 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100597 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200598 */
599 void
600do_perror(char *msg)
601{
602 perror(msg);
603 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100604 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200605 --emsg_silent;
606}
607#endif
608
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000609/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100610 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 *
612 * Rings the bell, if appropriate, and calls message() to do the real work
613 * When terminal not initialized (yet) mch_errmsg(..) is used.
614 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100615 * Return TRUE if wait_return not called.
616 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100618 static int
619emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100623 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#ifdef FEAT_EVAL
625 int ignore = FALSE;
626 int severe;
627#endif
628
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100629#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100630 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100631 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100632 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100633 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100634#endif
635
Bram Moolenaar53989552019-12-23 22:59:18 +0100636 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100639 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
640 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 severe = emsg_severe;
642 emsg_severe = FALSE;
643#endif
644
Bram Moolenaar57657d82006-04-21 22:12:41 +0000645 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {
647#ifdef FEAT_EVAL
648 /*
649 * Cause a throw of an error exception if appropriate. Don't display
650 * the error message in this case. (If no matching catch clause will
651 * be found, the message will be displayed later on.) "ignore" is set
652 * when the message should be ignored completely (used for the
653 * interrupt message).
654 */
655 if (cause_errthrow(s, severe, &ignore) == TRUE)
656 {
657 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100658 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return TRUE;
660 }
661
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100662 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200663 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200664 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200665 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200666 vim_free(emsg_assert_fails_context);
667 emsg_assert_fails_context = vim_strsave(
668 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200669 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200670
Bram Moolenaar85a20022019-12-21 18:25:54 +0100671 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 set_vim_var_string(VV_ERRMSG, s, -1);
673#endif
674
675 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000676 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * But do write it to the redirection file.
678 */
679 if (emsg_silent != 0)
680 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200681 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200683 msg_start();
684 p = get_emsg_source();
685 if (p != NULL)
686 {
687 STRCAT(p, "\n");
688 redir_write(p, -1);
689 vim_free(p);
690 }
691 p = get_emsg_lnum();
692 if (p != NULL)
693 {
694 STRCAT(p, "\n");
695 redir_write(p, -1);
696 vim_free(p);
697 }
698 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100700#ifdef FEAT_EVAL
701 // Only increment did_emsg_def when :silent! wasn't used inside the
702 // :def function.
703 if (emsg_silent == emsg_silent_def)
704 ++did_emsg_def;
705#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100706#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200707 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 return TRUE;
710 }
711
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100712 ex_exitval = 1;
713
Bram Moolenaar85a20022019-12-21 18:25:54 +0100714 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 msg_silent = 0;
716 cmd_silent = FALSE;
717
Bram Moolenaar85a20022019-12-21 18:25:54 +0100718 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 ++global_busy;
720
721 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100722 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200724 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100725 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200726#ifdef FEAT_EVAL
727 did_uncaught_emsg = TRUE;
728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 }
730
Bram Moolenaar85a20022019-12-21 18:25:54 +0100731 emsg_on_display = TRUE; // remember there is an error message
732 ++msg_scroll; // don't overwrite a previous message
733 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000734 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100735 need_wait_return = TRUE; // needed in case emsg() is called after
736 // wait_return has reset need_wait_return
737 // and a redraw is expected because
738 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar958dc692016-12-01 15:34:12 +0100740#ifdef FEAT_JOB_CHANNEL
741 emsg_to_channel_log = TRUE;
742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 /*
744 * Display name and line number for the source of the error.
745 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000746 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
748 /*
749 * Display the error message itself.
750 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100751 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100752 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100753
754#ifdef FEAT_JOB_CHANNEL
755 emsg_to_channel_log = FALSE;
756#endif
757 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758}
759
760/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100761 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
763 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100766 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100767 if (!emsg_not_now())
768 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100769 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100772#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100773/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100774 * Print an error message with format string and variable arguments.
775 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100776 */
777 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100779{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200780 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100781 if (!emsg_not_now())
782 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200783 if (IObuff == NULL)
784 {
785 // Very early in initialisation and already something wrong, just
786 // give the raw message so the user at least gets a hint.
787 return emsg_core((char_u *)s);
788 }
789 else
790 {
791 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100792
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200793 va_start(ap, s);
794 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
795 va_end(ap);
796 return emsg_core(IObuff);
797 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200799 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100800}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100801#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100802
803/*
804 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
805 * defined. It is used for internal errors only, so that they can be
806 * detected when fuzzing vim.
807 */
808 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100809iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100810{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100811 if (!emsg_not_now())
812 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100813#ifdef ABORT_ON_INTERNAL_ERROR
814 abort();
815#endif
816}
817
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100818#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100819/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100821 * defined. It is used for internal errors only, so that they can be
822 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100823 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100824 */
825 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100827{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828 if (!emsg_not_now())
829 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200830 if (IObuff == NULL)
831 {
832 // Very early in initialisation and already something wrong, just
833 // give the raw message so the user at least gets a hint.
834 emsg_core((char_u *)s);
835 }
836 else
837 {
838 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100839
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200840 va_start(ap, s);
841 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
842 va_end(ap);
843 emsg_core(IObuff);
844 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100846# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100848# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100849}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100850#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100851
852/*
853 * Give an "Internal error" message.
854 */
855 void
856internal_error(char *where)
857{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100858 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100859}
860
Bram Moolenaardd589232020-02-29 17:38:12 +0100861/*
862 * Like internal_error() but do not call abort(), to avoid tests using
863 * test_unknown() and test_void() causing Vim to exit.
864 */
865 void
866internal_error_no_abort(char *where)
867{
868 semsg(_(e_intern2), where);
869}
870
Bram Moolenaar85a20022019-12-21 18:25:54 +0100871// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
Bram Moolenaardf177f62005-02-22 08:39:57 +0000873 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100874emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000875{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100876 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000877}
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 * Give an error message which contains %s for "name[len]".
881 */
882 void
883emsg_namelen(char *msg, char_u *name, int len)
884{
885 char_u *copy = vim_strnsave((char_u *)name, len);
886
887 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100888 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889}
890
891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 * Like msg(), but truncate to a single line if p_shm contains 't', or when
893 * "force" is TRUE. This truncates in another way as for normal messages.
894 * Careful: The string may be changed by msg_may_trunc()!
895 * Returns a pointer to the printed message, if wait_return() not called.
896 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100897 char *
898msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100901 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
Bram Moolenaar85a20022019-12-21 18:25:54 +0100903 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100904 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaar32526b32019-01-19 17:43:09 +0100906 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
908 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100909 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 msg_hist_off = FALSE;
911
912 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100913 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 return NULL;
915}
916
917/*
918 * Check if message "s" should be truncated at the start (for filenames).
919 * Return a pointer to where the truncated message starts.
920 * Note: May change the message by replacing a character with '<'.
921 */
922 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100923msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924{
925 int n;
926 int room;
927
928 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
929 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
930 && (n = (int)STRLEN(s) - room) > 0)
931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (has_mbyte)
933 {
934 int size = vim_strsize(s);
935
Bram Moolenaar85a20022019-12-21 18:25:54 +0100936 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000937 if (size <= room)
938 return s;
939
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 for (n = 0; size >= room; )
941 {
942 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000943 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945 --n;
946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 s += n;
948 *s = '<';
949 }
950 return s;
951}
952
953 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100954add_msg_hist(
955 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100956 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100957 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
959 struct msg_hist *p;
960
961 if (msg_hist_off || msg_silent != 0)
962 return;
963
Bram Moolenaar85a20022019-12-21 18:25:54 +0100964 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000965 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000966 (void)delete_first_msg();
967
Bram Moolenaar85a20022019-12-21 18:25:54 +0100968 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200969 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (p != NULL)
971 {
972 if (len < 0)
973 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100974 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 while (len > 0 && *s == '\n')
976 {
977 ++s;
978 --len;
979 }
980 while (len > 0 && s[len - 1] == '\n')
981 --len;
982 p->msg = vim_strnsave(s, len);
983 p->next = NULL;
984 p->attr = attr;
985 if (last_msg_hist != NULL)
986 last_msg_hist->next = p;
987 last_msg_hist = p;
988 if (first_msg_hist == NULL)
989 first_msg_hist = last_msg_hist;
990 ++msg_hist_len;
991 }
992}
993
994/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000995 * Delete the first (oldest) message from the history.
996 * Returns FAIL if there are no messages.
997 */
998 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100999delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001000{
1001 struct msg_hist *p;
1002
1003 if (msg_hist_len <= 0)
1004 return FAIL;
1005 p = first_msg_hist;
1006 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001007 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001008 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001009 vim_free(p->msg);
1010 vim_free(p);
1011 --msg_hist_len;
1012 return OK;
1013}
1014
1015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * ":messages" command.
1017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001019ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 struct msg_hist *p;
1022 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001023 int c = 0;
1024
1025 if (STRCMP(eap->arg, "clear") == 0)
1026 {
1027 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1028
1029 while (msg_hist_len > keep)
1030 (void)delete_first_msg();
1031 return;
1032 }
1033
1034 if (*eap->arg != NUL)
1035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001036 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001037 return;
1038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 msg_hist_off = TRUE;
1041
Bram Moolenaar451f8492016-04-14 17:16:22 +02001042 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001043 if (eap->addr_count != 0)
1044 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001045 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001046 for (; p != NULL && !got_int; p = p->next)
1047 c++;
1048
1049 c -= eap->line2;
1050
Bram Moolenaar85a20022019-12-21 18:25:54 +01001051 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001052 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1053 p = p->next, c--);
1054 }
1055
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001056 if (p == first_msg_hist)
1057 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001058#ifdef FEAT_MULTI_LANG
1059 s = get_mess_lang();
1060#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001061 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001062#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001063 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001064 // The next comment is extracted by xgettext and put in po file for
1065 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001066 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001067 // Translator: Please replace the name and email address
1068 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001069 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001070 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001071 }
1072
Bram Moolenaar85a20022019-12-21 18:25:54 +01001073 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001074 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001076 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078 msg_hist_off = FALSE;
1079}
1080
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001081#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/*
1083 * Call this after prompting the user. This will avoid a hit-return message
1084 * and a delay.
1085 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001086 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001087msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 need_wait_return = FALSE;
1090 emsg_on_display = FALSE;
1091 cmdline_row = msg_row;
1092 msg_col = 0;
1093 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001094 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095}
1096#endif
1097
1098/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001099 * Wait for the user to hit a key (normally Enter).
1100 * If "redraw" is TRUE, clear and redraw the screen.
1101 * If "redraw" is FALSE, just redraw the screen.
1102 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 */
1104 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001105wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
1107 int c;
1108 int oldState;
1109 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001111 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001112 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
1114 if (redraw == TRUE)
1115 must_redraw = CLEAR;
1116
Bram Moolenaar85a20022019-12-21 18:25:54 +01001117 // If using ":silent cmd", don't wait for a return. Also don't set
1118 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (msg_silent != 0)
1120 return;
1121
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001122 /*
1123 * When inside vgetc(), we can't wait for a typed character at all.
1124 * With the global command (and some others) we only need one return at
1125 * the end. Adjust cmdline_row to avoid the next message overwriting the
1126 * last one.
1127 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001128 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001130 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (no_wait_return)
1132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (!exmode_active)
1134 cmdline_row = msg_row;
1135 return;
1136 }
1137
Bram Moolenaar85a20022019-12-21 18:25:54 +01001138 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 oldState = State;
1140 if (quit_more)
1141 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 quit_more = FALSE;
1144 got_int = FALSE;
1145 }
1146 else if (exmode_active)
1147 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001148 msg_puts(" "); // make sure the cursor is on the right line
1149 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 got_int = FALSE;
1151 }
1152 else
1153 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // Make sure the hit-return prompt is on screen when 'guioptions' was
1155 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 screenalloc(FALSE);
1157
1158 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001161 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001163 cmdline_row = msg_row;
1164
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 // Avoid the sequence that the user types ":" at the hit-return prompt
1166 // to start an Ex command, but the file-changed dialog gets in the
1167 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001168 if (need_check_timestamps)
1169 check_timestamps(FALSE);
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 hit_return_msg();
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 do
1174 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001175 // Remember "got_int", if it is set vgetc() probably returns a
1176 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001178
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 // Don't do mappings here, we put the character back in the
1180 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001181 ++no_mapping;
1182 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001183
Bram Moolenaar85a20022019-12-21 18:25:54 +01001184 // Temporarily disable Recording. If Recording is active, the
1185 // character will be recorded later, since it will be added to the
1186 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001187 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001188 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001189 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001190 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001192 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001194 --no_mapping;
1195 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001196 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001197 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001200 // Strange way to allow copying (yanking) a modeless selection at
1201 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1202 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1204 {
1205 clip_copy_modeless_selection(TRUE);
1206 c = K_IGNORE;
1207 }
1208#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001209
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001210 /*
1211 * Allow scrolling back in the messages.
1212 * Also accept scroll-down commands when messages fill the screen,
1213 * to avoid that typing one 'j' too many makes the messages
1214 * disappear.
1215 */
1216 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001217 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001218 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1219 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001220 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001221 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001223 do_more_prompt(c);
1224 else
1225 {
1226 msg_didout = FALSE;
1227 c = K_IGNORE;
1228 msg_col =
1229#ifdef FEAT_RIGHTLEFT
1230 cmdmsg_rl ? Columns - 1 :
1231#endif
1232 0;
1233 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001234 if (quit_more)
1235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001236 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001237 quit_more = FALSE;
1238 got_int = FALSE;
1239 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001240 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001241 {
1242 c = K_IGNORE;
1243 hit_return_msg();
1244 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001245 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001246 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001247 && (c == 'j' || c == 'd' || c == 'f'
1248 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001249 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 } while ((had_got_int && c == Ctrl_C)
1252 || c == K_IGNORE
1253#ifdef FEAT_GUI
1254 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1257 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1258 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001259 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001261 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 || (!mouse_has(MOUSE_RETURN)
1263 && mouse_row < msg_row
1264 && (c == K_LEFTMOUSE
1265 || c == K_MIDDLEMOUSE
1266 || c == K_RIGHTMOUSE
1267 || c == K_X1MOUSE
1268 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 );
1270 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 /*
1272 * Avoid that the mouse-up event causes visual mode to start.
1273 */
1274 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1275 || c == K_X1MOUSE || c == K_X2MOUSE)
1276 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001277 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 // Put the character back in the typeahead buffer. Don't use the
1280 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001281 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001282 do_redraw = TRUE; // need a redraw even though there is
1283 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 redir_off = FALSE;
1287
1288 /*
1289 * If the user hits ':', '?' or '/' we get a command line from the next
1290 * line.
1291 */
1292 if (c == ':' || c == '?' || c == '/')
1293 {
1294 if (!exmode_active)
1295 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001296 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001298#ifdef FEAT_TERMINAL
1299 skip_term_loop = TRUE;
1300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302
1303 /*
1304 * If the window size changed set_shellsize() will redraw the screen.
1305 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1306 * typed.
1307 */
1308 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001309 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 msg_check();
1312
1313#if defined(UNIX) || defined(VMS)
1314 /*
1315 * When switching screens, we need to output an extra newline on exit.
1316 */
1317 if (swapping_screen() && !termcap_active)
1318 newline_on_exit = TRUE;
1319#endif
1320
1321 need_wait_return = FALSE;
1322 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001323 emsg_on_display = FALSE; // can delete error message now
1324 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 reset_last_sourcing();
1326 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1327 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001328 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001332 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 shell_resized();
1334 }
1335 else if (!skip_redraw
1336 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1337 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 redraw_later(VALID);
1340 }
1341}
1342
1343/*
1344 * Write the hit-return prompt.
1345 */
1346 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001347hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001349 int save_p_more = p_more;
1350
Bram Moolenaar85a20022019-12-21 18:25:54 +01001351 p_more = FALSE; // don't want see this message when scrolling back
1352 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 msg_putchar('\n');
1354 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001355 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar32526b32019-01-19 17:43:09 +01001357 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (!msg_use_printf())
1359 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001360 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361}
1362
1363/*
1364 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1365 */
1366 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001367set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
1369 vim_free(keep_msg);
1370 if (s != NULL && msg_silent == 0)
1371 keep_msg = vim_strsave(s);
1372 else
1373 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001374 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001375 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376}
1377
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001378#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1379/*
1380 * If there currently is a message being displayed, set "keep_msg" to it, so
1381 * that it will be displayed again after redraw.
1382 */
1383 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001384set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001385{
1386 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1387 && (State & NORMAL))
1388 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1389}
1390#endif
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392/*
1393 * Prepare for outputting characters in the command line.
1394 */
1395 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001396msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
1398 int did_return = FALSE;
1399
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001400 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001401 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001402
1403#ifdef FEAT_EVAL
1404 if (need_clr_eos)
1405 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001406 // Halfway an ":echo" command and getting an (error) message: clear
1407 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001408 need_clr_eos = FALSE;
1409 msg_clr_eos();
1410 }
1411#endif
1412
Bram Moolenaar85a20022019-12-21 18:25:54 +01001413 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
1415 msg_row = cmdline_row;
1416 msg_col =
1417#ifdef FEAT_RIGHTLEFT
1418 cmdmsg_rl ? Columns - 1 :
1419#endif
1420 0;
1421 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001422 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424 msg_putchar('\n');
1425 did_return = TRUE;
1426 if (exmode_active != EXMODE_NORMAL)
1427 cmdline_row = msg_row;
1428 }
1429 if (!msg_didany || lines_left < 0)
1430 msg_starthere();
1431 if (msg_silent == 0)
1432 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001433 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 cursor_off();
1435 }
1436
Bram Moolenaar85a20022019-12-21 18:25:54 +01001437 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (!did_return)
1439 redir_write((char_u *)"\n", -1);
1440}
1441
1442/*
1443 * Note that the current msg position is where messages start.
1444 */
1445 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001446msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 lines_left = cmdline_row;
1449 msg_didany = FALSE;
1450}
1451
1452 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001453msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 msg_putchar_attr(c, 0);
1456}
1457
1458 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001459msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001461 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463 if (IS_SPECIAL(c))
1464 {
1465 buf[0] = K_SPECIAL;
1466 buf[1] = K_SECOND(c);
1467 buf[2] = K_THIRD(c);
1468 buf[3] = NUL;
1469 }
1470 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001471 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001472 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473}
1474
1475 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001476msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001478 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
Bram Moolenaar32526b32019-01-19 17:43:09 +01001480 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 msg_puts(buf);
1482}
1483
1484 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001485msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 msg_home_replace_attr(fname, 0);
1488}
1489
1490#if defined(FEAT_FIND_ID) || defined(PROTO)
1491 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001492msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001494 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495}
1496#endif
1497
1498 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001499msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 char_u *name;
1502
1503 name = home_replace_save(NULL, fname);
1504 if (name != NULL)
1505 msg_outtrans_attr(name, attr);
1506 vim_free(name);
1507}
1508
1509/*
1510 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001511 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 * Use attributes 'attr'.
1513 * Return the number of characters it takes on the screen.
1514 */
1515 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001516msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 return msg_outtrans_attr(str, 0);
1519}
1520
1521 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001522msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1525}
1526
1527 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001528msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 return msg_outtrans_len_attr(str, len, 0);
1531}
1532
1533/*
1534 * Output one character at "p". Return pointer to the next character.
1535 * Handles multi-byte characters.
1536 */
1537 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001538msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 int l;
1541
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001542 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 msg_outtrans_len_attr(p, l, attr);
1545 return p + l;
1546 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001547 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 return p + 1;
1549}
1550
1551 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001552msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 int retval = 0;
1555 char_u *str = msgstr;
1556 char_u *plain_start = msgstr;
1557 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 int mb_l;
1559 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001560 int save_got_int = got_int;
1561
1562 // Only quit when got_int was set in here.
1563 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaar85a20022019-12-21 18:25:54 +01001565 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (attr & MSG_HIST)
1567 {
1568 add_msg_hist(str, len, attr);
1569 attr &= ~MSG_HIST;
1570 }
1571
Bram Moolenaar85a20022019-12-21 18:25:54 +01001572 // If the string starts with a composing character first draw a space on
1573 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001575 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 /*
1578 * Go over the string. Special characters are translated and printed.
1579 * Normal characters are printed several at a time.
1580 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001581 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001584 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001585 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001587 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 else
1589 mb_l = 1;
1590 if (has_mbyte && mb_l > 1)
1591 {
1592 c = (*mb_ptr2char)(str);
1593 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 retval += (*mb_ptr2cells)(str);
1596 else
1597 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001598 // unprintable multi-byte char: print the printable chars so
1599 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001601 msg_puts_attr_len((char *)plain_start,
1602 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001604 msg_puts_attr((char *)transchar(c),
1605 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 retval += char2cells(c);
1607 }
1608 len -= mb_l - 1;
1609 str += mb_l;
1610 }
1611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 s = transchar_byte(*str);
1614 if (s[1] != NUL)
1615 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001616 // unprintable char: print the printable chars so far and the
1617 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001619 msg_puts_attr_len((char *)plain_start,
1620 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001622 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001623 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001625 else
1626 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 ++str;
1628 }
1629 }
1630
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001631 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001632 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001633 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001635 got_int |= save_got_int;
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 return retval;
1638}
1639
1640#if defined(FEAT_QUICKFIX) || defined(PROTO)
1641 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001642msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 int i;
1645 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1646
1647 arg = skipwhite(arg);
1648 for (i = 5; *arg && i >= 0; --i)
1649 if (*arg++ != str[i])
1650 break;
1651 if (i < 0)
1652 {
1653 msg_putchar('\n');
1654 for (i = 0; rs[i]; ++i)
1655 msg_putchar(rs[i] - 3);
1656 }
1657}
1658#endif
1659
1660/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001661 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 * Return the number of characters it takes on the screen.
1663 *
1664 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1665 * following character and shown as <F1>, <S-Up> etc. Any other character
1666 * which is not printable shown in <> form.
1667 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1668 * If a character is displayed in one of these special ways, is also
1669 * highlighted (its highlight name is '8' in the p_hl variable).
1670 * Otherwise characters are not highlighted.
1671 * This function is used to show mappings, where we want to see how to type
1672 * the character/string -- webb
1673 */
1674 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001675msg_outtrans_special(
1676 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001677 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001678 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 char_u *str = strstart;
1681 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001682 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 int attr;
1684 int len;
1685
Bram Moolenaar8820b482017-03-16 17:23:31 +01001686 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 while (*str != NUL)
1688 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001689 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if ((str == strstart || str[1] == NUL) && *str == ' ')
1691 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001692 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 ++str;
1694 }
1695 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001696 text = (char *)str2special(&str, from);
1697 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001698 if (maxlen > 0 && retval + len >= maxlen)
1699 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001700 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001701 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001702 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 retval += len;
1704 }
1705 return retval;
1706}
1707
Bram Moolenaarbd743252010-10-20 21:23:33 +02001708#if defined(FEAT_EVAL) || defined(PROTO)
1709/*
1710 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1711 * strings, in an allocated string.
1712 */
1713 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001714str2special_save(
1715 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001716 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001717{
1718 garray_T ga;
1719 char_u *p = str;
1720
1721 ga_init2(&ga, 1, 40);
1722 while (*p != NUL)
1723 ga_concat(&ga, str2special(&p, is_lhs));
1724 ga_append(&ga, NUL);
1725 return (char_u *)ga.ga_data;
1726}
1727#endif
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729/*
1730 * Return the printable string for the key codes at "*sp".
1731 * Used for translating the lhs or rhs of a mapping to printable chars.
1732 * Advances "sp" to the next code.
1733 */
1734 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001735str2special(
1736 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001737 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 int c;
1740 static char_u buf[7];
1741 char_u *str = *sp;
1742 int modifiers = 0;
1743 int special = FALSE;
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 if (has_mbyte)
1746 {
1747 char_u *p;
1748
Bram Moolenaar85a20022019-12-21 18:25:54 +01001749 // Try to un-escape a multi-byte character. Return the un-escaped
1750 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 p = mb_unescape(sp);
1752 if (p != NULL)
1753 return p;
1754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756 c = *str;
1757 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1758 {
1759 if (str[1] == KS_MODIFIER)
1760 {
1761 modifiers = str[2];
1762 str += 3;
1763 c = *str;
1764 }
1765 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1766 {
1767 c = TO_SPECIAL(str[1], str[2]);
1768 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001770 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 special = TRUE;
1772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001774 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001776 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001777
Bram Moolenaar85a20022019-12-21 18:25:54 +01001778 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001779 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1780 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001781 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001782 *sp = str + 1;
1783 return buf;
1784 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001785 // Since 'special' is TRUE the multi-byte character 'c' will be
1786 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001787 c = (*mb_ptr2char)(str);
1788 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001790 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001791 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
Bram Moolenaar85a20022019-12-21 18:25:54 +01001793 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1794 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (special || char2cells(c) > 1 || (from && c == ' '))
1796 return get_special_key_name(c, modifiers);
1797 buf[0] = c;
1798 buf[1] = NUL;
1799 return buf;
1800}
1801
1802/*
1803 * Translate a key sequence into special key names.
1804 */
1805 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001806str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
1808 char_u *s;
1809
1810 *buf = NUL;
1811 while (*sp)
1812 {
1813 s = str2special(&sp, FALSE);
1814 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1815 STRCAT(buf, s);
1816 }
1817}
1818
1819/*
1820 * print line for :print or :list command
1821 */
1822 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001823msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 int c;
1826 int col = 0;
1827 int n_extra = 0;
1828 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001829 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001830 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001832 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001834 char_u *lead = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 int l;
1836 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
Bram Moolenaardf177f62005-02-22 08:39:57 +00001838 if (curwin->w_p_list)
1839 list = TRUE;
1840
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001841 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001843 // find start of trailing whitespace
1844 if (lcs_trail)
1845 {
1846 trail = s + STRLEN(s);
1847 while (trail > s && VIM_ISWHITE(trail[-1]))
1848 --trail;
1849 }
1850 // find end of leading whitespace
1851 if (lcs_lead)
1852 {
1853 lead = s;
1854 while (VIM_ISWHITE(lead[0]))
1855 lead++;
1856 // in a line full of spaces all of them are treated as trailing
1857 if (*lead == NUL)
1858 lead = NULL;
1859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861
Bram Moolenaar85a20022019-12-21 18:25:54 +01001862 // output a space for an empty line, otherwise the line will be
1863 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001864 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 msg_putchar(' ');
1866
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001867 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001869 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
1871 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001872 if (n_extra == 0 && c_final)
1873 c = c_final;
1874 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 c = c_extra;
1876 else
1877 c = *p_extra++;
1878 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001879 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
1881 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001882 if (l >= MB_MAXBYTES)
1883 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001884 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001885 }
1886 else if (lcs_nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001887 && (mb_ptr2char(s) == 160
1888 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001889 {
1890 mb_char2bytes(lcs_nbsp, buf);
1891 buf[(*mb_ptr2len)(buf)] = NUL;
1892 }
1893 else
1894 {
1895 mch_memmove(buf, s, (size_t)l);
1896 buf[l] = NUL;
1897 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001898 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 s += l;
1900 continue;
1901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 else
1903 {
1904 attr = 0;
1905 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001906 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001908 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001909#ifdef FEAT_VARTABS
1910 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1911 curbuf->b_p_vts_array) - 1;
1912#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001914#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001915 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 c = ' ';
1918 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001919 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 else
1922 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001923 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001925 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001926 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001929 else if (c == 160 && list && lcs_nbsp != NUL)
1930 {
1931 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001932 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001933 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001934 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936 p_extra = (char_u *)"";
1937 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001938 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 n_extra = 1;
1940 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001941 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 --s;
1943 }
1944 else if (c != NUL && (n = byte2cells(c)) > 1)
1945 {
1946 n_extra = n - 1;
1947 p_extra = transchar_byte(c);
1948 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001949 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001951 // Use special coloring to be able to distinguish <hex> from
1952 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001953 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001955 else if (c == ' ' && lead != NULL && s <= lead)
1956 {
1957 c = lcs_lead;
1958 attr = HL_ATTR(HLF_8);
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 else if (c == ' ' && trail != NULL && s > trail)
1961 {
1962 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001963 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001965 else if (c == ' ' && list && lcs_space != NUL)
1966 {
1967 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001968 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971
1972 if (c == NUL)
1973 break;
1974
1975 msg_putchar_attr(c, attr);
1976 col++;
1977 }
1978 msg_clr_eos();
1979}
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981/*
1982 * Use screen_puts() to output one multi-byte character.
1983 * Return the pointer "s" advanced to the next character.
1984 */
1985 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001986screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 int cw;
1989
Bram Moolenaar85a20022019-12-21 18:25:54 +01001990 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 cw = (*mb_ptr2cells)(s);
1992 if (cw > 1 && (
1993#ifdef FEAT_RIGHTLEFT
1994 cmdmsg_rl ? msg_col <= 1 :
1995#endif
1996 msg_col == Columns - 1))
1997 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001998 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001999 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return s;
2001 }
2002
2003 screen_puts_len(s, l, msg_row, msg_col, attr);
2004#ifdef FEAT_RIGHTLEFT
2005 if (cmdmsg_rl)
2006 {
2007 msg_col -= cw;
2008 if (msg_col == 0)
2009 {
2010 msg_col = Columns;
2011 ++msg_row;
2012 }
2013 }
2014 else
2015#endif
2016 {
2017 msg_col += cw;
2018 if (msg_col >= Columns)
2019 {
2020 msg_col = 0;
2021 ++msg_row;
2022 }
2023 }
2024 return s + l;
2025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027/*
2028 * Output a string to the screen at position msg_row, msg_col.
2029 * Update msg_row and msg_col for the next message.
2030 */
2031 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002032msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002034 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035}
2036
2037 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002038msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002040 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041}
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043/*
2044 * Show a message in such a way that it always fits in the line. Cut out a
2045 * part in the middle and replace it with "..." when necessary.
2046 * Does not handle multi-byte characters!
2047 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002048 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002049msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 int slen = len;
2052 int room;
2053
2054 room = Columns - msg_col;
2055 if (len > room && room >= 20)
2056 {
2057 slen = (room - 3) / 2;
2058 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002059 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2062}
2063
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002064 void
2065msg_outtrans_long_attr(char_u *longstr, int attr)
2066{
2067 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2068}
2069
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070/*
2071 * Basic function for writing a message with highlight attributes.
2072 */
2073 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002074msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075{
2076 msg_puts_attr_len(s, -1, attr);
2077}
2078
2079/*
2080 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2081 * When "maxlen" is -1 there is no maximum length.
2082 * When "maxlen" is >= 0 the message is not put in the history.
2083 */
2084 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002085msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 /*
2088 * If redirection is on, also write to the redirection file.
2089 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002090 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092 /*
2093 * Don't print anything when using ":silent cmd".
2094 */
2095 if (msg_silent != 0)
2096 return;
2097
Bram Moolenaar85a20022019-12-21 18:25:54 +01002098 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if ((attr & MSG_HIST) && maxlen < 0)
2100 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002101 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 attr &= ~MSG_HIST;
2103 }
2104
Bram Moolenaare8070982019-10-12 17:07:06 +02002105 // When writing something to the screen after it has scrolled, requires a
2106 // wait-return prompt later. Needed when scrolling, resetting
2107 // need_wait_return after some prompt, and then outputting something
2108 // without scrolling
2109 // Not needed when only using CR to move the cursor.
2110 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002112 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114 /*
2115 * If there is no valid screen, use fprintf so we can see error messages.
2116 * If termcap is not active, we may be writing in an alternate console
2117 * window, cursor positioning may not work correctly (window size may be
2118 * different, e.g. for Win32 console) or we just don't know where the
2119 * cursor is.
2120 */
2121 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002122 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002123 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002124 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002127/*
2128 * The display part of msg_puts_attr_len().
2129 * May be called recursively to display scroll-back text.
2130 */
2131 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002132msg_puts_display(
2133 char_u *str,
2134 int maxlen,
2135 int attr,
2136 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002137{
2138 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002139 char_u *t_s = str; // string from "t_s" to "s" is still todo
2140 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002141 int l;
2142 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002143 char_u *sb_str = str;
2144 int sb_col = msg_col;
2145 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002146 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002149 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002152 * We are at the end of the screen line when:
2153 * - When outputting a newline.
2154 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002156 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157#ifdef FEAT_RIGHTLEFT
2158 cmdmsg_rl
2159 ? (
2160 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002161 || (*s == TAB && msg_col <= 7)
2162 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 :
2164#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002165 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002168 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002170 /*
2171 * The screen is scrolled up when at the last row (some terminals
2172 * scroll automatically, some don't. To avoid problems we scroll
2173 * ourselves).
2174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002176 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002177 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaar85a20022019-12-21 18:25:54 +01002179 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (msg_no_more && lines_left == 0)
2181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar85a20022019-12-21 18:25:54 +01002183 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
2186 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002187 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 msg_col = Columns - 1;
2189
Bram Moolenaar85a20022019-12-21 18:25:54 +01002190 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002191 if (*s >= ' '
2192#ifdef FEAT_RIGHTLEFT
2193 && !cmdmsg_rl
2194#endif
2195 )
2196 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002197 if (has_mbyte)
2198 {
2199 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002200 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002201 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002202 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002203 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002204 s = screen_puts_mbyte(s, l, attr);
2205 }
2206 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002207 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002208 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002209 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002210 else
2211 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002212
2213 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002214 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002215 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2216
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002217 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002218 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 redraw_cmdline = TRUE;
2220 if (cmdline_row > 0 && !exmode_active)
2221 --cmdline_row;
2222
2223 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002224 * If screen is completely filled and 'more' is set then wait
2225 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002227 if (lines_left > 0)
2228 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002229 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 && !msg_no_more && !exmode_active)
2231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002233 if (do_more_prompt(NUL))
2234 s = confirm_msg_tail;
2235#else
2236 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
2238 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002239 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002241
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 // When we displayed a char in last column need to check if there
2243 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002244 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002245 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002248 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002251 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002252 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2253 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002255 t_puts(&t_col, t_s, s, attr);
2256
2257 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002258 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002259 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
Bram Moolenaar85a20022019-12-21 18:25:54 +01002261 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002264#ifdef FEAT_RIGHTLEFT
2265 if (cmdmsg_rl)
2266 msg_col = Columns - 1;
2267 else
2268#endif
2269 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 msg_row = Rows - 1;
2272 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002273 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 msg_col = 0;
2276 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002277 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 if (msg_col)
2280 --msg_col;
2281 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
2284 do
2285 msg_screen_putchar(' ', attr);
2286 while (msg_col & 7);
2287 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002288 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002289 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 else
2291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (has_mbyte)
2293 {
2294 cw = (*mb_ptr2cells)(s);
2295 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002296 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002297 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002299 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 else
2302 {
2303 cw = 1;
2304 l = 1;
2305 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002306
Bram Moolenaar85a20022019-12-21 18:25:54 +01002307 // When drawing from right to left or when a double-wide character
2308 // doesn't fit, draw a single character here. Otherwise collect
2309 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (
2311# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002312 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002314 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (l > 1)
2317 s = screen_puts_mbyte(s, l, attr) - 1;
2318 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 msg_screen_putchar(*s, attr);
2320 }
2321 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002323 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (t_col == 0)
2325 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 t_col += cw;
2327 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 }
2330 ++s;
2331 }
2332
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002335 t_puts(&t_col, t_s, s, attr);
2336 if (p_more && !recurse)
2337 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
2339 msg_check();
2340}
2341
2342/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002343 * Return TRUE when ":filter pattern" was used and "msg" does not match
2344 * "pattern".
2345 */
2346 int
2347message_filtered(char_u *msg)
2348{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002349 int match;
2350
Bram Moolenaare1004402020-10-24 20:49:43 +02002351 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002352 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002353 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2354 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002355}
2356
2357/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002358 * Scroll the screen up one line for displaying the next message line.
2359 */
2360 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002361msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002362{
2363#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002364 // Remove the cursor before scrolling, ScreenLines[] is going
2365 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002366 if (gui.in_use)
2367 gui_undraw_cursor();
2368#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002369 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002370 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002371 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002372 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002373
2374 if (!can_clear((char_u *)" "))
2375 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002376 // Scrolling up doesn't result in the right background. Set the
2377 // background here. It's not efficient, but avoids that we have to do
2378 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002379 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2380
Bram Moolenaar85a20022019-12-21 18:25:54 +01002381 // Also clear the last char of the last but one line if it was not
2382 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002383 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2384 screen_fill((int)Rows - 2, (int)Rows - 1,
2385 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2386 }
2387}
2388
2389/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002390 * Increment "msg_scrolled".
2391 */
2392 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002393inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002394{
2395#ifdef FEAT_EVAL
2396 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2397 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002398 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002399 char_u *tofree = NULL;
2400 int len;
2401
Bram Moolenaar85a20022019-12-21 18:25:54 +01002402 // v:scrollstart is empty, set it to the script/function name and line
2403 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002404 if (p == NULL)
2405 p = (char_u *)_("Unknown");
2406 else
2407 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002408 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002409 tofree = alloc(len);
2410 if (tofree != NULL)
2411 {
2412 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002413 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002414 p = tofree;
2415 }
2416 }
2417 set_vim_var_string(VV_SCROLLSTART, p, -1);
2418 vim_free(tofree);
2419 }
2420#endif
2421 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002422 if (must_redraw < VALID)
2423 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002424}
2425
2426/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002427 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2428 * store the displayed text and remember where screen lines start.
2429 */
2430typedef struct msgchunk_S msgchunk_T;
2431struct msgchunk_S
2432{
2433 msgchunk_T *sb_next;
2434 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002435 char sb_eol; // TRUE when line ends after this text
2436 int sb_msg_col; // column in which text starts
2437 int sb_attr; // text attributes
2438 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002439};
2440
Bram Moolenaar85a20022019-12-21 18:25:54 +01002441static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002442
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002443static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002444
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002445typedef enum {
2446 SB_CLEAR_NONE = 0,
2447 SB_CLEAR_ALL,
2448 SB_CLEAR_CMDLINE_BUSY,
2449 SB_CLEAR_CMDLINE_DONE
2450} sb_clear_T;
2451
Bram Moolenaar85a20022019-12-21 18:25:54 +01002452// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002453static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002454
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002455/*
2456 * Store part of a printed message for displaying when scrolling back.
2457 */
2458 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002459store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002460 char_u **sb_str, // start of string
2461 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002462 int attr,
2463 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002464 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002465{
2466 msgchunk_T *mp;
2467
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002468 if (do_clear_sb_text == SB_CLEAR_ALL
2469 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002470 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002471 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2472 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002473 }
2474
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002475 if (s > *sb_str)
2476 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002477 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002478 if (mp != NULL)
2479 {
2480 mp->sb_eol = finish;
2481 mp->sb_msg_col = *sb_col;
2482 mp->sb_attr = attr;
2483 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2484
2485 if (last_msgchunk == NULL)
2486 {
2487 last_msgchunk = mp;
2488 mp->sb_prev = NULL;
2489 }
2490 else
2491 {
2492 mp->sb_prev = last_msgchunk;
2493 last_msgchunk->sb_next = mp;
2494 last_msgchunk = mp;
2495 }
2496 mp->sb_next = NULL;
2497 }
2498 }
2499 else if (finish && last_msgchunk != NULL)
2500 last_msgchunk->sb_eol = TRUE;
2501
2502 *sb_str = s;
2503 *sb_col = 0;
2504}
2505
2506/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002507 * Finished showing messages, clear the scroll-back text on the next message.
2508 */
2509 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002510may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002511{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002512 do_clear_sb_text = SB_CLEAR_ALL;
2513}
2514
2515/*
2516 * Starting to edit the command line, do not clear messages now.
2517 */
2518 void
2519sb_text_start_cmdline(void)
2520{
2521 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2522 msg_sb_eol();
2523}
2524
2525/*
2526 * Ending to edit the command line. Clear old lines but the last one later.
2527 */
2528 void
2529sb_text_end_cmdline(void)
2530{
2531 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002532}
2533
2534/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002535 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002536 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002537 * Called when redrawing the screen.
2538 */
2539 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002540clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002541{
2542 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002543 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002544
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002545 if (all)
2546 lastp = &last_msgchunk;
2547 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002548 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002549 if (last_msgchunk == NULL)
2550 return;
2551 lastp = &last_msgchunk->sb_prev;
2552 }
2553
2554 while (*lastp != NULL)
2555 {
2556 mp = (*lastp)->sb_prev;
2557 vim_free(*lastp);
2558 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002559 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002560}
2561
2562/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002563 * "g<" command.
2564 */
2565 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002566show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002567{
2568 msgchunk_T *mp;
2569
Bram Moolenaar85a20022019-12-21 18:25:54 +01002570 // Only show something if there is more than one line, otherwise it looks
2571 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002572 mp = msg_sb_start(last_msgchunk);
2573 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002574 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002575 else
2576 {
2577 do_more_prompt('G');
2578 wait_return(FALSE);
2579 }
2580}
2581
2582/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002583 * Move to the start of screen line in already displayed text.
2584 */
2585 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002586msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587{
2588 msgchunk_T *mp = mps;
2589
2590 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2591 mp = mp->sb_prev;
2592 return mp;
2593}
2594
2595/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002596 * Mark the last message chunk as finishing the line.
2597 */
2598 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002599msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002600{
2601 if (last_msgchunk != NULL)
2602 last_msgchunk->sb_eol = TRUE;
2603}
2604
2605/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 * Display a screen line from previously displayed text at row "row".
2607 * Returns a pointer to the text for the next line (can be NULL).
2608 */
2609 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002610disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002611{
2612 msgchunk_T *mp = smp;
2613 char_u *p;
2614
2615 for (;;)
2616 {
2617 msg_row = row;
2618 msg_col = mp->sb_msg_col;
2619 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002620 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002621 ++p;
2622 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2623 if (mp->sb_eol || mp->sb_next == NULL)
2624 break;
2625 mp = mp->sb_next;
2626 }
2627 return mp->sb_next;
2628}
2629
2630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * Output any postponed text for msg_puts_attr_len().
2632 */
2633 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002634t_puts(
2635 int *t_col,
2636 char_u *t_s,
2637 char_u *s,
2638 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002640 // output postponed text
2641 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002643 msg_col += *t_col;
2644 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002645 // If the string starts with a composing character don't increment the
2646 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2648 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (msg_col >= Columns)
2650 {
2651 msg_col = 0;
2652 ++msg_row;
2653 }
2654}
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656/*
2657 * Returns TRUE when messages should be printed with mch_errmsg().
2658 * This is used when there is no valid screen, so we can see error messages.
2659 * If termcap is not active, we may be writing in an alternate console
2660 * window, cursor positioning may not work correctly (window size may be
2661 * different, e.g. for Win32 console) or we just don't know where the
2662 * cursor is.
2663 */
2664 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002665msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002668#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2669# ifdef VIMDLL
2670 || (!gui.in_use && !termcap_active)
2671# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#endif
2675 || (swapping_screen() && !termcap_active)
2676 );
2677}
2678
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002679/*
2680 * Print a message when there is no valid screen.
2681 */
2682 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002683msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684{
2685 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002686 char_u *buf = NULL;
2687 char_u *p = s;
2688
Bram Moolenaar4f974752019-02-17 17:44:42 +01002689#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002691 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002692#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002693 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002694 {
2695 if (!(silent_mode && p_verbose == 0))
2696 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002697 // NL --> CR NL translation (for Unix, not for "--version")
2698 if (*s == NL)
2699 {
2700 int n = (int)(s - p);
2701
2702 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002703 if (buf != NULL)
2704 {
2705 memcpy(buf, p, n);
2706 if (!info_message)
2707 buf[n++] = CAR;
2708 buf[n++] = NL;
2709 buf[n++] = NUL;
2710 if (info_message) // informative message, not an error
2711 mch_msg((char *)buf);
2712 else
2713 mch_errmsg((char *)buf);
2714 vim_free(buf);
2715 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002716 p = s + 1;
2717 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718 }
2719
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002720 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721#ifdef FEAT_RIGHTLEFT
2722 if (cmdmsg_rl)
2723 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002724 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002725 msg_col = Columns - 1;
2726 else
2727 --msg_col;
2728 }
2729 else
2730#endif
2731 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002732 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733 msg_col = 0;
2734 else
2735 ++msg_col;
2736 }
2737 ++s;
2738 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002739
2740 if (*p != NUL && !(silent_mode && p_verbose == 0))
2741 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002742 int c = -1;
2743
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002744 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002745 {
2746 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002747 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002748 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002749 if (info_message)
2750 mch_msg((char *)p);
2751 else
2752 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002753 if (c != -1)
2754 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002755 }
2756
2757 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002758
Bram Moolenaar4f974752019-02-17 17:44:42 +01002759#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002760 if (!(silent_mode && p_verbose == 0))
2761 mch_settmode(TMODE_RAW);
2762#endif
2763}
2764
2765/*
2766 * Show the more-prompt and handle the user response.
2767 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002768 * When at hit-enter prompt "typed_char" is the already typed character,
2769 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002770 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2771 */
2772 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002773do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002774{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002775 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002776 int used_typed_char = typed_char;
2777 int oldState = State;
2778 int c;
2779#ifdef FEAT_CON_DIALOG
2780 int retval = FALSE;
2781#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002782 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002783 msgchunk_T *mp_last = NULL;
2784 msgchunk_T *mp;
2785 int i;
2786
Bram Moolenaar85a20022019-12-21 18:25:54 +01002787 // We get called recursively when a timer callback outputs a message. In
2788 // that case don't show another prompt. Also when at the hit-Enter prompt
2789 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002790 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002791 return FALSE;
2792 entered = TRUE;
2793
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002794 if (typed_char == 'G')
2795 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002796 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002797 mp_last = msg_sb_start(last_msgchunk);
2798 for (i = 0; i < Rows - 2 && mp_last != NULL
2799 && mp_last->sb_prev != NULL; ++i)
2800 mp_last = msg_sb_start(mp_last->sb_prev);
2801 }
2802
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002803 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002804 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002805 if (typed_char == NUL)
2806 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 for (;;)
2808 {
2809 /*
2810 * Get a typed character directly from the user.
2811 */
2812 if (used_typed_char != NUL)
2813 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002814 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002815 used_typed_char = NUL;
2816 }
2817 else
2818 c = get_keystroke();
2819
2820#if defined(FEAT_MENU) && defined(FEAT_GUI)
2821 if (c == K_MENU)
2822 {
2823 int idx = get_menu_index(current_menu, ASKMORE);
2824
Bram Moolenaar85a20022019-12-21 18:25:54 +01002825 // Used a menu. If it starts with CTRL-Y, it must
2826 // be a "Copy" for the clipboard. Otherwise
2827 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002828 if (idx == MENU_INDEX_INVALID)
2829 continue;
2830 c = *current_menu->strings[idx];
2831 if (c != NUL && current_menu->strings[idx][1] != NUL)
2832 ins_typebuf(current_menu->strings[idx] + 1,
2833 current_menu->noremap[idx], 0, TRUE,
2834 current_menu->silent[idx]);
2835 }
2836#endif
2837
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002838 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002839 switch (c)
2840 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002841 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842 case K_BS:
2843 case 'k':
2844 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002845 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002846 break;
2847
Bram Moolenaar85a20022019-12-21 18:25:54 +01002848 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002849 case NL:
2850 case 'j':
2851 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002852 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002853 break;
2854
Bram Moolenaar85a20022019-12-21 18:25:54 +01002855 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002856 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002857 break;
2858
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002860 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861 break;
2862
Bram Moolenaar85a20022019-12-21 18:25:54 +01002863 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002864 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002865 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 break;
2867
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002869 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002870 case K_PAGEDOWN:
2871 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002872 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 break;
2874
Bram Moolenaar85a20022019-12-21 18:25:54 +01002875 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002876 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002877 break;
2878
Bram Moolenaar85a20022019-12-21 18:25:54 +01002879 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002880 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002881 lines_left = 999999;
2882 break;
2883
Bram Moolenaar85a20022019-12-21 18:25:54 +01002884 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885#ifdef FEAT_CON_DIALOG
2886 if (!confirm_msg_used)
2887#endif
2888 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 // Since got_int is set all typeahead will be flushed, but we
2890 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002891 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002892#ifdef FEAT_TERMINAL
2893 skip_term_loop = TRUE;
2894#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002895 cmdline_row = Rows - 1; // put ':' on this line
2896 skip_redraw = TRUE; // skip redraw once
2897 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002898 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002899 // FALLTHROUGH
2900 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002901 case Ctrl_C:
2902 case ESC:
2903#ifdef FEAT_CON_DIALOG
2904 if (confirm_msg_used)
2905 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002906 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002907 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002908 }
2909 else
2910#endif
2911 {
2912 got_int = TRUE;
2913 quit_more = TRUE;
2914 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 // When there is some more output (wrapping line) display that
2916 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002917 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002918 break;
2919
2920#ifdef FEAT_CLIPBOARD
2921 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002922 // Strange way to allow copying (yanking) a modeless
2923 // selection at the more prompt. Use CTRL-Y,
2924 // because the same is used in Cmdline-mode and at the
2925 // hit-enter prompt. However, scrolling one line up
2926 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002927 if (clip_star.state == SELECT_DONE)
2928 clip_copy_modeless_selection(TRUE);
2929 continue;
2930#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002932 msg_moremsg(TRUE);
2933 continue;
2934 }
2935
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002936 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002937 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002938 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002939 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002940 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002941 if (mp_last == NULL)
2942 mp = msg_sb_start(last_msgchunk);
2943 else if (mp_last->sb_prev != NULL)
2944 mp = msg_sb_start(mp_last->sb_prev);
2945 else
2946 mp = NULL;
2947
Bram Moolenaar85a20022019-12-21 18:25:54 +01002948 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002949 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2950 ++i)
2951 mp = msg_sb_start(mp->sb_prev);
2952
2953 if (mp != NULL && mp->sb_prev != NULL)
2954 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002955 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002956 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 {
2958 if (mp == NULL || mp->sb_prev == NULL)
2959 break;
2960 mp = msg_sb_start(mp->sb_prev);
2961 if (mp_last == NULL)
2962 mp_last = msg_sb_start(last_msgchunk);
2963 else
2964 mp_last = msg_sb_start(mp_last->sb_prev);
2965 }
2966
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002967 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002968 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002969 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002971 (void)disp_sb_line(0, mp);
2972 }
2973 else
2974 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002975 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002976 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002977 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2978 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002979 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002980 ++msg_scrolled;
2981 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002982 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002983 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002984 }
2985 }
2986 else
2987 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002988 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002989 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002990 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002991 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002992 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002993 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002994 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2995 (int)Columns, ' ', ' ', 0);
2996 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002997 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002998 }
2999 }
3000
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003001 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003002 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003003 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003004 screen_fill((int)Rows - 1, (int)Rows, 0,
3005 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003006 msg_moremsg(FALSE);
3007 continue;
3008 }
3009
Bram Moolenaar85a20022019-12-21 18:25:54 +01003010 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003011 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003012 }
3013
3014 break;
3015 }
3016
Bram Moolenaar85a20022019-12-21 18:25:54 +01003017 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003018 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3019 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003020 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003021 if (quit_more)
3022 {
3023 msg_row = Rows - 1;
3024 msg_col = 0;
3025 }
3026#ifdef FEAT_RIGHTLEFT
3027 else if (cmdmsg_rl)
3028 msg_col = Columns - 1;
3029#endif
3030
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003031 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003032#ifdef FEAT_CON_DIALOG
3033 return retval;
3034#else
3035 return FALSE;
3036#endif
3037}
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3040
3041#ifdef mch_errmsg
3042# undef mch_errmsg
3043#endif
3044#ifdef mch_msg
3045# undef mch_msg
3046#endif
3047
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003048#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3049 static void
3050mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003052 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003053 DWORD nwrite = 0;
3054 DWORD mode = 0;
3055 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3056
3057 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3058 && (int)GetConsoleCP() != enc_codepage)
3059 {
3060 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3061
3062 WriteConsoleW(h, w, len, &nwrite, NULL);
3063 vim_free(w);
3064 }
3065 else
3066 {
3067 fprintf(stderr, "%s", str);
3068 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003069}
3070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003072/*
3073 * Give an error message. To be used when the screen hasn't been initialized
3074 * yet. When stderr can't be used, collect error messages until the GUI has
3075 * started and they can be displayed in a message box.
3076 */
3077 void
3078mch_errmsg(char *str)
3079{
3080#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3081 int len;
3082#endif
3083
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003084#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003085 // On Unix use stderr if it's a tty.
3086 // When not going to start the GUI also use stderr.
3087 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003089# ifdef UNIX
3090# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003092# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 isatty(2)
3094# endif
3095# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003096 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003097# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003098# endif
3099# ifdef FEAT_GUI
3100 !(gui.in_use || gui.starting)
3101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 )
3103 {
3104 fprintf(stderr, "%s", str);
3105 return;
3106 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003109#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3110# ifdef VIMDLL
3111 if (!(gui.in_use || gui.starting))
3112# endif
3113 {
3114 mch_errmsg_c(str);
3115 return;
3116 }
3117#endif
3118
3119#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003120 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 emsg_on_display = FALSE;
3122
3123 len = (int)STRLEN(str) + 1;
3124 if (error_ga.ga_growsize == 0)
3125 {
3126 error_ga.ga_growsize = 80;
3127 error_ga.ga_itemsize = 1;
3128 }
3129 if (ga_grow(&error_ga, len) == OK)
3130 {
3131 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3132 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003133# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003134 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 char_u *p;
3137
3138 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3139 for (;;)
3140 {
3141 p = vim_strchr(p, '\r');
3142 if (p == NULL)
3143 break;
3144 *p = ' ';
3145 }
3146 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003147# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003148 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152}
3153
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003154#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3155 static void
3156mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003158 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003159 DWORD nwrite = 0;
3160 DWORD mode;
3161 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3162
3163
3164 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3165 && (int)GetConsoleCP() != enc_codepage)
3166 {
3167 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3168
3169 WriteConsoleW(h, w, len, &nwrite, NULL);
3170 vim_free(w);
3171 }
3172 else
3173 {
3174 printf("%s", str);
3175 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003176}
3177#endif
3178
3179/*
3180 * Give a message. To be used when the screen hasn't been initialized yet.
3181 * When there is no tty, collect messages until the GUI has started and they
3182 * can be displayed in a message box.
3183 */
3184 void
3185mch_msg(char *str)
3186{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003187#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003188 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3189 // uses mch_errmsg() when started from the desktop.
3190 // When not going to start the GUI also use stdout.
3191 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003193# ifdef UNIX
3194# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003196# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003200 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003202# endif
3203# ifdef FEAT_GUI
3204 !(gui.in_use || gui.starting)
3205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 )
3207 {
3208 printf("%s", str);
3209 return;
3210 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003211#endif
3212
3213#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3214# ifdef VIMDLL
3215 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003217 {
3218 mch_msg_c(str);
3219 return;
3220 }
3221#endif
3222#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003226#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228/*
3229 * Put a character on the screen at the current message position and advance
3230 * to the next position. Only for printable ASCII!
3231 */
3232 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003235 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 screen_putchar(c, msg_row, msg_col, attr);
3237#ifdef FEAT_RIGHTLEFT
3238 if (cmdmsg_rl)
3239 {
3240 if (--msg_col == 0)
3241 {
3242 msg_col = Columns;
3243 ++msg_row;
3244 }
3245 }
3246 else
3247#endif
3248 {
3249 if (++msg_col >= Columns)
3250 {
3251 msg_col = 0;
3252 ++msg_row;
3253 }
3254 }
3255}
3256
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003257 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003258msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003260 int attr;
3261 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
Bram Moolenaar8820b482017-03-16 17:23:31 +01003263 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003264 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003266 screen_puts((char_u *)
3267 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3268 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269}
3270
3271/*
3272 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3273 * exmode_active.
3274 */
3275 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003276repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 if (State == ASKMORE)
3279 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003280 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 msg_row = Rows - 1;
3282 }
3283#ifdef FEAT_CON_DIALOG
3284 else if (State == CONFIRM)
3285 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003286 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 msg_row = Rows - 1;
3288 }
3289#endif
3290 else if (State == EXTERNCMD)
3291 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003292 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294 else if (State == HITRETURN || State == SETWSIZE)
3295 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003296 if (msg_row == Rows - 1)
3297 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003298 // Avoid drawing the "hit-enter" prompt below the previous one,
3299 // overwrite it. Esp. useful when regaining focus and a
3300 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003301 msg_didout = FALSE;
3302 msg_col = 0;
3303 msg_clr_eos();
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 hit_return_msg();
3306 msg_row = Rows - 1;
3307 }
3308}
3309
3310/*
3311 * msg_check_screen - check if the screen is initialized.
3312 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3313 * While starting the GUI the terminal codes will be set for the GUI, but the
3314 * output goes to the terminal. Don't use the terminal codes then.
3315 */
3316 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003317msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 if (!full_screen || !screen_valid(FALSE))
3320 return FALSE;
3321
3322 if (msg_row >= Rows)
3323 msg_row = Rows - 1;
3324 if (msg_col >= Columns)
3325 msg_col = Columns - 1;
3326 return TRUE;
3327}
3328
3329/*
3330 * Clear from current message position to end of screen.
3331 * Skip this when ":silent" was used, no need to clear for redirection.
3332 */
3333 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003334msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 if (msg_silent == 0)
3337 msg_clr_eos_force();
3338}
3339
3340/*
3341 * Clear from current message position to end of screen.
3342 * Note: msg_col is not updated, so we remember the end of the message
3343 * for msg_check().
3344 */
3345 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003346msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 if (msg_use_printf())
3349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003350 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
3352 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003353 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003355 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357 }
3358 else
3359 {
3360#ifdef FEAT_RIGHTLEFT
3361 if (cmdmsg_rl)
3362 {
3363 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3364 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3365 }
3366 else
3367#endif
3368 {
3369 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3370 ' ', ' ', 0);
3371 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3372 }
3373 }
3374}
3375
3376/*
3377 * Clear the command line.
3378 */
3379 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003380msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 msg_row = cmdline_row;
3383 msg_col = 0;
3384 msg_clr_eos_force();
3385}
3386
3387/*
3388 * end putting a message on the screen
3389 * call wait_return if the message does not fit in the available space
3390 * return TRUE if wait_return not called.
3391 */
3392 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003393msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003396 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 * or the ruler option is set and we run into it,
3398 * we have to redraw the window.
3399 * Do not do this if we are abandoning the file or editing the command line.
3400 */
3401 if (!exiting && need_wait_return && !(State & CMDLINE))
3402 {
3403 wait_return(FALSE);
3404 return FALSE;
3405 }
3406 out_flush();
3407 return TRUE;
3408}
3409
3410/*
3411 * If the written message runs into the shown command or ruler, we have to
3412 * wait for hit-return and redraw the window later.
3413 */
3414 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003415msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 if (msg_row == Rows - 1 && msg_col >= sc_col)
3418 {
3419 need_wait_return = TRUE;
3420 redraw_cmdline = TRUE;
3421 }
3422}
3423
3424/*
3425 * May write a string to the redirection file.
3426 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3427 */
3428 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003429redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 char_u *s = str;
3432 static int cur_col = 0;
3433
Bram Moolenaar85a20022019-12-21 18:25:54 +01003434 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003435 if (redir_off)
3436 return;
3437
Bram Moolenaar85a20022019-12-21 18:25:54 +01003438 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003439 if (*p_vfile != NUL && verbose_fd == NULL)
3440 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003441
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003442 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003444 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (*s != '\n' && *s != '\r')
3446 {
3447 while (cur_col < msg_col)
3448 {
3449#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003450 if (redir_execute)
3451 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003452 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003454 else if (redir_vname)
3455 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003458 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003460 if (verbose_fd != NULL)
3461 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 ++cur_col;
3463 }
3464 }
3465
3466#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003467 if (redir_execute)
3468 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003469 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003471 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003472 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
3474
Bram Moolenaar85a20022019-12-21 18:25:54 +01003475 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3477 {
3478#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003479 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003481 if (redir_fd != NULL)
3482 putc(*s, redir_fd);
3483 if (verbose_fd != NULL)
3484 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (*s == '\r' || *s == '\n')
3486 cur_col = 0;
3487 else if (*s == '\t')
3488 cur_col += (8 - cur_col % 8);
3489 else
3490 ++cur_col;
3491 ++s;
3492 }
3493
Bram Moolenaar85a20022019-12-21 18:25:54 +01003494 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 msg_col = cur_col;
3496 }
3497}
3498
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003499 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003500redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003501{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003502 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003503#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003504 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003505#endif
3506 ;
3507}
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003510 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003511 * Must always be called paired with verbose_leave()!
3512 */
3513 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003514verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003515{
3516 if (*p_vfile != NUL)
3517 ++msg_silent;
3518}
3519
3520/*
3521 * After giving verbose message.
3522 * Must always be called paired with verbose_enter()!
3523 */
3524 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003525verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003526{
3527 if (*p_vfile != NUL)
3528 if (--msg_silent < 0)
3529 msg_silent = 0;
3530}
3531
3532/*
3533 * Like verbose_enter() and set msg_scroll when displaying the message.
3534 */
3535 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003536verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003537{
3538 if (*p_vfile != NUL)
3539 ++msg_silent;
3540 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003541 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003542 msg_scroll = TRUE;
3543}
3544
3545/*
3546 * Like verbose_leave() and set cmdline_row when displaying the message.
3547 */
3548 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003549verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003550{
3551 if (*p_vfile != NUL)
3552 {
3553 if (--msg_silent < 0)
3554 msg_silent = 0;
3555 }
3556 else
3557 cmdline_row = msg_row;
3558}
3559
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003560/*
3561 * Called when 'verbosefile' is set: stop writing to the file.
3562 */
3563 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003564verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003565{
3566 if (verbose_fd != NULL)
3567 {
3568 fclose(verbose_fd);
3569 verbose_fd = NULL;
3570 }
3571 verbose_did_open = FALSE;
3572}
3573
3574/*
3575 * Open the file 'verbosefile'.
3576 * Return FAIL or OK.
3577 */
3578 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003579verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003580{
3581 if (verbose_fd == NULL && !verbose_did_open)
3582 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003583 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003584 verbose_did_open = TRUE;
3585
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003586 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003587 if (verbose_fd == NULL)
3588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003589 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003590 return FAIL;
3591 }
3592 }
3593 return OK;
3594}
3595
3596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 * Give a warning message (for searching).
3598 * Use 'w' highlighting and may repeat the message after redrawing
3599 */
3600 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003601give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003603 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (msg_silent != 0)
3605 return;
3606
Bram Moolenaar85a20022019-12-21 18:25:54 +01003607 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610#ifdef FEAT_EVAL
3611 set_vim_var_string(VV_WARNINGMSG, message, -1);
3612#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003613 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003615 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 else
3617 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003618 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003619 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003620 msg_didout = FALSE; // overwrite this message
3621 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 --no_wait_return;
3625}
3626
Bram Moolenaar113e1072019-01-20 15:30:40 +01003627#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003628 void
3629give_warning2(char_u *message, char_u *a1, int hl)
3630{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003631 if (IObuff == NULL)
3632 {
3633 // Very early in initialisation and already something wrong, just give
3634 // the raw message so the user at least gets a hint.
3635 give_warning((char_u *)message, hl);
3636 }
3637 else
3638 {
3639 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3640 give_warning(IObuff, hl);
3641 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003642}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003643#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645/*
3646 * Advance msg cursor to column "col".
3647 */
3648 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003649msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003651 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003653 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 return;
3655 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003656 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003658#ifdef FEAT_RIGHTLEFT
3659 if (cmdmsg_rl)
3660 while (msg_col > Columns - col)
3661 msg_putchar(' ');
3662 else
3663#endif
3664 while (msg_col < col)
3665 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666}
3667
3668#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3669/*
3670 * Used for "confirm()" function, and the :confirm command prefix.
3671 * Versions which haven't got flexible dialogs yet, and console
3672 * versions, get this generic handler which uses the command line.
3673 *
3674 * type = one of:
3675 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3676 * title = title string (can be NULL for default)
3677 * (neither used in console dialogs at the moment)
3678 *
3679 * Format of the "buttons" string:
3680 * "Button1Name\nButton2Name\nButton3Name"
3681 * The first button should normally be the default/accept
3682 * The second button should be the 'Cancel' button
3683 * Other buttons- use your imagination!
3684 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3685 * different letter.
3686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003688do_dialog(
3689 int type UNUSED,
3690 char_u *title UNUSED,
3691 char_u *message,
3692 char_u *buttons,
3693 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003694 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3695 // otherwise
3696 int ex_cmd) // when TRUE pressing : accepts default and starts
3697 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 int oldState;
3700 int retval = 0;
3701 char_u *hotkeys;
3702 int c;
3703 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003704 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
3706#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003707 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003709 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710#endif
3711
3712#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003713 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3715 {
3716 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003717 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003718 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003719 need_wait_return = FALSE;
3720 emsg_on_display = FALSE;
3721 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaar85a20022019-12-21 18:25:54 +01003723 // Flush output to avoid that further messages and redrawing is done
3724 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 out_flush();
3726 gui_mch_update();
3727
3728 return c;
3729 }
3730#endif
3731
3732 oldState = State;
3733 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar27321db2020-07-06 21:24:57 +02003736 // Ensure raw mode here.
3737 save_tmode = cur_tmode;
3738 settmode(TMODE_RAW);
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 /*
3741 * Since we wait for a keypress, don't make the
3742 * user press RETURN as well afterwards.
3743 */
3744 ++no_wait_return;
3745 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3746
3747 if (hotkeys != NULL)
3748 {
3749 for (;;)
3750 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003751 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 c = get_keystroke();
3753 switch (c)
3754 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003755 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 case NL:
3757 retval = dfltbutton;
3758 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003759 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 case ESC:
3761 retval = 0;
3762 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003763 default: // Could be a hotkey?
3764 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003766 if (c == ':' && ex_cmd)
3767 {
3768 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003769 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003770 break;
3771 }
3772
Bram Moolenaar85a20022019-12-21 18:25:54 +01003773 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 c = MB_TOLOWER(c);
3775 retval = 1;
3776 for (i = 0; hotkeys[i]; ++i)
3777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (has_mbyte)
3779 {
3780 if ((*mb_ptr2char)(hotkeys + i) == c)
3781 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003782 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 if (hotkeys[i] == c)
3786 break;
3787 ++retval;
3788 }
3789 if (hotkeys[i])
3790 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003791 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 continue;
3793 }
3794 break;
3795 }
3796
3797 vim_free(hotkeys);
3798 }
3799
Bram Moolenaar27321db2020-07-06 21:24:57 +02003800 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 --no_wait_return;
3804 msg_end_prompt();
3805
3806 return retval;
3807}
3808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809/*
3810 * Copy one character from "*from" to "*to", taking care of multi-byte
3811 * characters. Return the length of the character in bytes.
3812 */
3813 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003814copy_char(
3815 char_u *from,
3816 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003817 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 int len;
3820 int c;
3821
3822 if (has_mbyte)
3823 {
3824 if (lowercase)
3825 {
3826 c = MB_TOLOWER((*mb_ptr2char)(from));
3827 return (*mb_char2bytes)(c, to);
3828 }
3829 else
3830 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003831 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 mch_memmove(to, from, (size_t)len);
3833 return len;
3834 }
3835 }
3836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
3838 if (lowercase)
3839 *to = (char_u)TOLOWER_LOC(*from);
3840 else
3841 *to = *from;
3842 return 1;
3843 }
3844}
3845
3846/*
3847 * Format the dialog string, and display it at the bottom of
3848 * the screen. Return a string of hotkey chars (if defined) for
3849 * each 'button'. If a button has no hotkey defined, the first character of
3850 * the button is used.
3851 * The hotkeys can be multi-byte characters, but without combining chars.
3852 *
3853 * Returns an allocated string with hotkeys, or NULL for error.
3854 */
3855 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003856msg_show_console_dialog(
3857 char_u *message,
3858 char_u *buttons,
3859 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
3861 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003862#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003863 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 char_u *hotk = NULL;
3865 char_u *msgp = NULL;
3866 char_u *hotkp = NULL;
3867 char_u *r;
3868 int copy;
3869#define HAS_HOTKEY_LEN 30
3870 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003871 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 int idx;
3873
3874 has_hotkey[0] = FALSE;
3875
3876 /*
3877 * First loop: compute the size of memory to allocate.
3878 * Second loop: copy to the allocated memory.
3879 */
3880 for (copy = 0; copy <= 1; ++copy)
3881 {
3882 r = buttons;
3883 idx = 0;
3884 while (*r)
3885 {
3886 if (*r == DLG_BUTTON_SEP)
3887 {
3888 if (copy)
3889 {
3890 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003891 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
Bram Moolenaar85a20022019-12-21 18:25:54 +01003893 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003895 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003898 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (dfltbutton)
3900 --dfltbutton;
3901
Bram Moolenaar85a20022019-12-21 18:25:54 +01003902 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3904 first_hotkey = TRUE;
3905 }
3906 else
3907 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003908 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3909 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if (idx < HAS_HOTKEY_LEN - 1)
3911 has_hotkey[++idx] = FALSE;
3912 }
3913 }
3914 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3915 {
3916 if (*r == DLG_HOTKEY_CHAR)
3917 ++r;
3918 first_hotkey = FALSE;
3919 if (copy)
3920 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003921 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 *msgp++ = *r;
3923 else
3924 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003925 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3927 msgp += copy_char(r, msgp, FALSE);
3928 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3929
Bram Moolenaar85a20022019-12-21 18:25:54 +01003930 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003931 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 }
3934 else
3935 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003936 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (idx < HAS_HOTKEY_LEN - 1)
3938 has_hotkey[idx] = TRUE;
3939 }
3940 }
3941 else
3942 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003943 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (copy)
3945 msgp += copy_char(r, msgp, FALSE);
3946 }
3947
Bram Moolenaar85a20022019-12-21 18:25:54 +01003948 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003949 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951
3952 if (copy)
3953 {
3954 *msgp++ = ':';
3955 *msgp++ = ' ';
3956 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958 else
3959 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003960 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003961 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003962 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003963 + 3); // for the ": " and NUL
3964 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
Bram Moolenaar85a20022019-12-21 18:25:54 +01003966 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 if (!has_hotkey[0])
3968 {
3969 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003970 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972
3973 /*
3974 * Now allocate and load the strings
3975 */
3976 vim_free(confirm_msg);
3977 confirm_msg = alloc(len);
3978 if (confirm_msg == NULL)
3979 return NULL;
3980 *confirm_msg = NUL;
3981 hotk = alloc(lenhotkey);
3982 if (hotk == NULL)
3983 return NULL;
3984
3985 *confirm_msg = '\n';
3986 STRCPY(confirm_msg + 1, message);
3987
3988 msgp = confirm_msg + 1 + STRLEN(message);
3989 hotkp = hotk;
3990
Bram Moolenaar85a20022019-12-21 18:25:54 +01003991 // Define first default hotkey. Keep the hotkey string NUL
3992 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003993 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar85a20022019-12-21 18:25:54 +01003995 // Remember where the choices start, displaying starts here when
3996 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 confirm_msg_tail = msgp;
3998 *msgp++ = '\n';
3999 }
4000 }
4001
4002 display_confirm_msg();
4003 return hotk;
4004}
4005
4006/*
4007 * Display the ":confirm" message. Also called when screen resized.
4008 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004009 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004010display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004012 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 ++confirm_msg_used;
4014 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004015 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 --confirm_msg_used;
4017}
4018
Bram Moolenaar85a20022019-12-21 18:25:54 +01004019#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
4021#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4022
4023 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004024vim_dialog_yesno(
4025 int type,
4026 char_u *title,
4027 char_u *message,
4028 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
4030 if (do_dialog(type,
4031 title == NULL ? (char_u *)_("Question") : title,
4032 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004033 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return VIM_YES;
4035 return VIM_NO;
4036}
4037
4038 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004039vim_dialog_yesnocancel(
4040 int type,
4041 char_u *title,
4042 char_u *message,
4043 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 switch (do_dialog(type,
4046 title == NULL ? (char_u *)_("Question") : title,
4047 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004048 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
4050 case 1: return VIM_YES;
4051 case 2: return VIM_NO;
4052 }
4053 return VIM_CANCEL;
4054}
4055
4056 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004057vim_dialog_yesnoallcancel(
4058 int type,
4059 char_u *title,
4060 char_u *message,
4061 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
4063 switch (do_dialog(type,
4064 title == NULL ? (char_u *)"Question" : title,
4065 message,
4066 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004067 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 case 1: return VIM_YES;
4070 case 2: return VIM_NO;
4071 case 3: return VIM_ALL;
4072 case 4: return VIM_DISCARDALL;
4073 }
4074 return VIM_CANCEL;
4075}
4076
Bram Moolenaar85a20022019-12-21 18:25:54 +01004077#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004079#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004080static char *e_printf = N_("E766: Insufficient arguments for printf()");
4081
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082/*
4083 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4084 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004085 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004086tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087{
4088 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004089 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004090 int err = FALSE;
4091
4092 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004093 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004094 else
4095 {
4096 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004097 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 if (err)
4099 n = 0;
4100 }
4101 return n;
4102}
4103
4104/*
4105 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004106 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004107 * are not converted to a string.
4108 * If "tofree" is not NULL echo_string() is used. All types are converted to
4109 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004110 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004111 */
4112 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004113tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004114{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004115 int idx = *idxp - 1;
4116 char *s = NULL;
4117 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118
4119 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004120 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004121 else
4122 {
4123 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004124 if (tofree != NULL)
4125 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4126 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004127 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004128 }
4129 return s;
4130}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004131
4132# ifdef FEAT_FLOAT
4133/*
4134 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4135 */
4136 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004137tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004138{
4139 int idx = *idxp - 1;
4140 double f = 0;
4141
4142 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004143 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004144 else
4145 {
4146 ++*idxp;
4147 if (tvs[idx].v_type == VAR_FLOAT)
4148 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004149 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004150 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004151 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004152 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004153 }
4154 return f;
4155}
4156# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157#endif
4158
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004159#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004160/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004161 * Return the representation of infinity for printf() function:
4162 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4163 */
4164 static const char *
4165infinity_str(int positive,
4166 char fmt_spec,
4167 int force_sign,
4168 int space_for_positive)
4169{
4170 static const char *table[] =
4171 {
4172 "-inf", "inf", "+inf", " inf",
4173 "-INF", "INF", "+INF", " INF"
4174 };
4175 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4176
4177 if (ASCII_ISUPPER(fmt_spec))
4178 idx += 4;
4179 return table[idx];
4180}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004181#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004182
4183/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004185 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004186 * consistency.
4187 *
4188 * This code is based on snprintf.c - a portable implementation of snprintf
4189 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004190 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004191 * The original code, including useful comments, can be found here:
4192 * http://www.ijs.si/software/snprintf/
4193 *
4194 * This snprintf() only supports the following conversion specifiers:
4195 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4196 * with flags: '-', '+', ' ', '0' and '#'.
4197 * An asterisk is supported for field width as well as precision.
4198 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004199 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004200 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004201 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004202 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004203 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004204 * The locale is not used, the string is used as a byte string. This is only
4205 * relevant for double-byte encodings where the second byte may be '%'.
4206 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004207 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4208 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004209 *
4210 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004211 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004212 * is greater or equal to "str_m", not all characters from the result
4213 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4214 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004215 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004216 */
4217
4218/*
4219 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004220 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004221 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004222 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223 */
4224
Bram Moolenaar85a20022019-12-21 18:25:54 +01004225// When generating prototypes all of this is skipped, cproto doesn't
4226// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004227#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004228
Bram Moolenaar85a20022019-12-21 18:25:54 +01004229// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004230 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004231vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004232{
4233 va_list ap;
4234 int str_l;
4235 size_t len = STRLEN(str);
4236 size_t space;
4237
4238 if (str_m <= len)
4239 space = 0;
4240 else
4241 space = str_m - len;
4242 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004243 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004244 va_end(ap);
4245 return str_l;
4246}
Bram Moolenaara800b422010-06-27 01:15:55 +02004247
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004248 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004249vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004250{
4251 va_list ap;
4252 int str_l;
4253
4254 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004255 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004256 va_end(ap);
4257 return str_l;
4258}
4259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004261vim_vsnprintf(
4262 char *str,
4263 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004264 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004265 va_list ap)
4266{
4267 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4268}
4269
4270 int
4271vim_vsnprintf_typval(
4272 char *str,
4273 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004275 va_list ap,
4276 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004277{
4278 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004279 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004280 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004281
4282 if (p == NULL)
4283 p = "";
4284 while (*p != NUL)
4285 {
4286 if (*p != '%')
4287 {
4288 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004289 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004290
Bram Moolenaar85a20022019-12-21 18:25:54 +01004291 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004292 if (str_l < str_m)
4293 {
4294 size_t avail = str_m - str_l;
4295
4296 mch_memmove(str + str_l, p, n > avail ? avail : n);
4297 }
4298 p += n;
4299 str_l += n;
4300 }
4301 else
4302 {
4303 size_t min_field_width = 0, precision = 0;
4304 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4305 int alternate_form = 0, force_sign = 0;
4306
Bram Moolenaar85a20022019-12-21 18:25:54 +01004307 // If both the ' ' and '+' flags appear, the ' ' flag should be
4308 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004309 int space_for_positive = 1;
4310
Bram Moolenaar85a20022019-12-21 18:25:54 +01004311 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004312 char length_modifier = '\0';
4313
Bram Moolenaar85a20022019-12-21 18:25:54 +01004314 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004315# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004316# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4317 // That sounds reasonable to use as the maximum
4318 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004319# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004320# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004321# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004322 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004323
Bram Moolenaar85a20022019-12-21 18:25:54 +01004324 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004325 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004326
Bram Moolenaar85a20022019-12-21 18:25:54 +01004327 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004328 size_t str_arg_l;
4329
Bram Moolenaar85a20022019-12-21 18:25:54 +01004330 // unsigned char argument value - only defined for c conversion.
4331 // N.B. standard explicitly states the char argument for the c
4332 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004333 unsigned char uchar_arg;
4334
Bram Moolenaar85a20022019-12-21 18:25:54 +01004335 // number of zeros to be inserted for numeric conversions as
4336 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004337 size_t number_of_zeros_to_pad = 0;
4338
Bram Moolenaar85a20022019-12-21 18:25:54 +01004339 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004340 size_t zero_padding_insertion_ind = 0;
4341
Bram Moolenaar85a20022019-12-21 18:25:54 +01004342 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004343 char fmt_spec = '\0';
4344
Bram Moolenaar85a20022019-12-21 18:25:54 +01004345 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004346 char_u *tofree = NULL;
4347
4348
Bram Moolenaar85a20022019-12-21 18:25:54 +01004349 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004350
Bram Moolenaar85a20022019-12-21 18:25:54 +01004351 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004352 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4353 || *p == '#' || *p == '\'')
4354 {
4355 switch (*p)
4356 {
4357 case '0': zero_padding = 1; break;
4358 case '-': justify_left = 1; break;
4359 case '+': force_sign = 1; space_for_positive = 0; break;
4360 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004361 // If both the ' ' and '+' flags appear, the ' '
4362 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004363 break;
4364 case '#': alternate_form = 1; break;
4365 case '\'': break;
4366 }
4367 p++;
4368 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004369 // If the '0' and '-' flags both appear, the '0' flag should be
4370 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004371
Bram Moolenaar85a20022019-12-21 18:25:54 +01004372 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004373 if (*p == '*')
4374 {
4375 int j;
4376
4377 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004380 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004381# endif
4382 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004383 if (j >= 0)
4384 min_field_width = j;
4385 else
4386 {
4387 min_field_width = -j;
4388 justify_left = 1;
4389 }
4390 }
4391 else if (VIM_ISDIGIT((int)(*p)))
4392 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004393 // size_t could be wider than unsigned int; make sure we treat
4394 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004395 unsigned int uj = *p++ - '0';
4396
4397 while (VIM_ISDIGIT((int)(*p)))
4398 uj = 10 * uj + (unsigned int)(*p++ - '0');
4399 min_field_width = uj;
4400 }
4401
Bram Moolenaar85a20022019-12-21 18:25:54 +01004402 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403 if (*p == '.')
4404 {
4405 p++;
4406 precision_specified = 1;
4407 if (*p == '*')
4408 {
4409 int j;
4410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004413 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414# endif
4415 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004416 p++;
4417 if (j >= 0)
4418 precision = j;
4419 else
4420 {
4421 precision_specified = 0;
4422 precision = 0;
4423 }
4424 }
4425 else if (VIM_ISDIGIT((int)(*p)))
4426 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004427 // size_t could be wider than unsigned int; make sure we
4428 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004429 unsigned int uj = *p++ - '0';
4430
4431 while (VIM_ISDIGIT((int)(*p)))
4432 uj = 10 * uj + (unsigned int)(*p++ - '0');
4433 precision = uj;
4434 }
4435 }
4436
Bram Moolenaar85a20022019-12-21 18:25:54 +01004437 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004438 if (*p == 'h' || *p == 'l')
4439 {
4440 length_modifier = *p;
4441 p++;
4442 if (length_modifier == 'l' && *p == 'l')
4443 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004444 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004445 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004446 p++;
4447 }
4448 }
4449 fmt_spec = *p;
4450
Bram Moolenaar85a20022019-12-21 18:25:54 +01004451 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004452 switch (fmt_spec)
4453 {
4454 case 'i': fmt_spec = 'd'; break;
4455 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4456 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4457 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4458 default: break;
4459 }
4460
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004461# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004462 switch (fmt_spec)
4463 {
4464 case 'd': case 'u': case 'o': case 'x': case 'X':
4465 if (tvs != NULL && length_modifier == '\0')
4466 length_modifier = 'L';
4467 }
4468# endif
4469
Bram Moolenaar85a20022019-12-21 18:25:54 +01004470 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004471 switch (fmt_spec)
4472 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004473 // '%' and 'c' behave similar to 's' regarding flags and field
4474 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004475 case '%':
4476 case 'c':
4477 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004478 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004479 str_arg_l = 1;
4480 switch (fmt_spec)
4481 {
4482 case '%':
4483 str_arg = p;
4484 break;
4485
4486 case 'c':
4487 {
4488 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489
4490 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004492 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493# endif
4494 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004495 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004496 uchar_arg = (unsigned char)j;
4497 str_arg = (char *)&uchar_arg;
4498 break;
4499 }
4500
4501 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004502 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004505 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506# endif
4507 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004508 if (str_arg == NULL)
4509 {
4510 str_arg = "[NULL]";
4511 str_arg_l = 6;
4512 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004513 // make sure not to address string beyond the specified
4514 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004515 else if (!precision_specified)
4516 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004517 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518 else if (precision == 0)
4519 str_arg_l = 0;
4520 else
4521 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004522 // Don't put the #if inside memchr(), it can be a
4523 // macro.
4524 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004525 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004526 precision <= (size_t)0x7fffffffL ? precision
4527 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004528 str_arg_l = (q == NULL) ? precision
4529 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004530 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004531 if (fmt_spec == 'S')
4532 {
4533 if (min_field_width != 0)
4534 min_field_width += STRLEN(str_arg)
4535 - mb_string2cells((char_u *)str_arg, -1);
4536 if (precision)
4537 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004538 char_u *p1;
4539 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004540
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004541 for (p1 = (char_u *)str_arg; *p1;
4542 p1 += mb_ptr2len(p1))
4543 {
4544 i += (size_t)mb_ptr2cells(p1);
4545 if (i > precision)
4546 break;
4547 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004548 str_arg_l = precision = p1 - (char_u *)str_arg;
4549 }
4550 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004551 break;
4552
4553 default:
4554 break;
4555 }
4556 break;
4557
Bram Moolenaar91984b92016-08-16 21:58:41 +02004558 case 'd': case 'u':
4559 case 'b': case 'B':
4560 case 'o':
4561 case 'x': case 'X':
4562 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004563 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004564 // NOTE: the u, b, o, x, X and p conversion specifiers
4565 // imply the value is unsigned; d implies a signed
4566 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004567
Bram Moolenaar85a20022019-12-21 18:25:54 +01004568 // 0 if numeric argument is zero (or if pointer is
4569 // NULL for 'p'), +1 if greater than zero (or nonzero
4570 // for unsigned arguments), -1 if negative (unsigned
4571 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004572 int arg_sign = 0;
4573
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004574 // only set for length modifier h, or for no length
4575 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004576 int int_arg = 0;
4577 unsigned int uint_arg = 0;
4578
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004579 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004580 long int long_arg = 0;
4581 unsigned long int ulong_arg = 0;
4582
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004583 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004584 varnumber_T llong_arg = 0;
4585 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004586
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004587 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004588 uvarnumber_T bin_arg = 0;
4589
Bram Moolenaar85a20022019-12-21 18:25:54 +01004590 // pointer argument value -only defined for p
4591 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004592 void *ptr_arg = NULL;
4593
4594 if (fmt_spec == 'p')
4595 {
4596 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004599 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4600 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601# endif
4602 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004603 if (ptr_arg != NULL)
4604 arg_sign = 1;
4605 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004606 else if (fmt_spec == 'b' || fmt_spec == 'B')
4607 {
4608 bin_arg =
4609# if defined(FEAT_EVAL)
4610 tvs != NULL ?
4611 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4612# endif
4613 va_arg(ap, uvarnumber_T);
4614 if (bin_arg != 0)
4615 arg_sign = 1;
4616 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004617 else if (fmt_spec == 'd')
4618 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004619 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004620 switch (length_modifier)
4621 {
4622 case '\0':
4623 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004624 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004625 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004626# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004627 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004628# endif
4629 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004630 if (int_arg > 0)
4631 arg_sign = 1;
4632 else if (int_arg < 0)
4633 arg_sign = -1;
4634 break;
4635 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004636 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004638 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639# endif
4640 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004641 if (long_arg > 0)
4642 arg_sign = 1;
4643 else if (long_arg < 0)
4644 arg_sign = -1;
4645 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004646 case 'L':
4647 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004648# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004649 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004650# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004651 va_arg(ap, varnumber_T);
4652 if (llong_arg > 0)
4653 arg_sign = 1;
4654 else if (llong_arg < 0)
4655 arg_sign = -1;
4656 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004657 }
4658 }
4659 else
4660 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004661 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004662 switch (length_modifier)
4663 {
4664 case '\0':
4665 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004666 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004667# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004668 tvs != NULL ? (unsigned)
4669 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670# endif
4671 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004672 if (uint_arg != 0)
4673 arg_sign = 1;
4674 break;
4675 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004677# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004678 tvs != NULL ? (unsigned long)
4679 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004680# endif
4681 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004682 if (ulong_arg != 0)
4683 arg_sign = 1;
4684 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004685 case 'L':
4686 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004687# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004688 tvs != NULL ? (uvarnumber_T)
4689 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004690# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004691 va_arg(ap, uvarnumber_T);
4692 if (ullong_arg != 0)
4693 arg_sign = 1;
4694 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004695 }
4696 }
4697
4698 str_arg = tmp;
4699 str_arg_l = 0;
4700
Bram Moolenaar85a20022019-12-21 18:25:54 +01004701 // NOTE:
4702 // For d, i, u, o, x, and X conversions, if precision is
4703 // specified, the '0' flag should be ignored. This is so
4704 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4705 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004706 if (precision_specified)
4707 zero_padding = 0;
4708 if (fmt_spec == 'd')
4709 {
4710 if (force_sign && arg_sign >= 0)
4711 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004712 // leave negative numbers for sprintf to handle, to
4713 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004714 }
4715 else if (alternate_form)
4716 {
4717 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004718 && (fmt_spec == 'b' || fmt_spec == 'B'
4719 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004720 {
4721 tmp[str_arg_l++] = '0';
4722 tmp[str_arg_l++] = fmt_spec;
4723 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004724 // alternate form should have no effect for p
4725 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004726 }
4727
4728 zero_padding_insertion_ind = str_arg_l;
4729 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004730 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004731 if (precision == 0 && arg_sign == 0)
4732 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004733 // When zero value is formatted with an explicit
4734 // precision 0, the resulting formatted string is
4735 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004736 }
4737 else
4738 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004739 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004740 int f_l = 0;
4741
Bram Moolenaar85a20022019-12-21 18:25:54 +01004742 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 f[f_l++] = '%';
4744 if (!length_modifier)
4745 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004746 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004747 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004748# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004749 f[f_l++] = 'I';
4750 f[f_l++] = '6';
4751 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004752# else
4753 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004754 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004755# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004756 }
4757 else
4758 f[f_l++] = length_modifier;
4759 f[f_l++] = fmt_spec;
4760 f[f_l++] = '\0';
4761
4762 if (fmt_spec == 'p')
4763 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004764 else if (fmt_spec == 'b' || fmt_spec == 'B')
4765 {
4766 char b[8 * sizeof(uvarnumber_T)];
4767 size_t b_l = 0;
4768 uvarnumber_T bn = bin_arg;
4769
4770 do
4771 {
4772 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4773 bn >>= 1;
4774 }
4775 while (bn != 0);
4776
4777 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4778 str_arg_l += b_l;
4779 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004780 else if (fmt_spec == 'd')
4781 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004782 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004783 switch (length_modifier)
4784 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004785 case '\0': str_arg_l += sprintf(
4786 tmp + str_arg_l, f,
4787 int_arg);
4788 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004789 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004790 tmp + str_arg_l, f,
4791 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004792 break;
4793 case 'l': str_arg_l += sprintf(
4794 tmp + str_arg_l, f, long_arg);
4795 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004796 case 'L': str_arg_l += sprintf(
4797 tmp + str_arg_l, f, llong_arg);
4798 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 }
4800 }
4801 else
4802 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004803 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004804 switch (length_modifier)
4805 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004806 case '\0': str_arg_l += sprintf(
4807 tmp + str_arg_l, f,
4808 uint_arg);
4809 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004810 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004811 tmp + str_arg_l, f,
4812 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004813 break;
4814 case 'l': str_arg_l += sprintf(
4815 tmp + str_arg_l, f, ulong_arg);
4816 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004817 case 'L': str_arg_l += sprintf(
4818 tmp + str_arg_l, f, ullong_arg);
4819 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004820 }
4821 }
4822
Bram Moolenaar85a20022019-12-21 18:25:54 +01004823 // include the optional minus sign and possible
4824 // "0x" in the region before the zero padding
4825 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004826 if (zero_padding_insertion_ind < str_arg_l
4827 && tmp[zero_padding_insertion_ind] == '-')
4828 zero_padding_insertion_ind++;
4829 if (zero_padding_insertion_ind + 1 < str_arg_l
4830 && tmp[zero_padding_insertion_ind] == '0'
4831 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4832 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4833 zero_padding_insertion_ind += 2;
4834 }
4835
4836 {
4837 size_t num_of_digits = str_arg_l
4838 - zero_padding_insertion_ind;
4839
4840 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004841 // unless zero is already the first
4842 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004843 && !(zero_padding_insertion_ind < str_arg_l
4844 && tmp[zero_padding_insertion_ind] == '0'))
4845 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004846 // assure leading zero for alternate-form
4847 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004848 if (!precision_specified
4849 || precision < num_of_digits + 1)
4850 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004851 // precision is increased to force the
4852 // first character to be zero, except if a
4853 // zero value is formatted with an
4854 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004855 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004856 }
4857 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004858 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004859 if (num_of_digits < precision)
4860 number_of_zeros_to_pad = precision - num_of_digits;
4861 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004862 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004863 if (!justify_left && zero_padding)
4864 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004865 int n = (int)(min_field_width - (str_arg_l
4866 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004867 if (n > 0)
4868 number_of_zeros_to_pad += n;
4869 }
4870 break;
4871 }
4872
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004873# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004874 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004875 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004876 case 'e':
4877 case 'E':
4878 case 'g':
4879 case 'G':
4880 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004881 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004882 double f;
4883 double abs_f;
4884 char format[40];
4885 int l;
4886 int remove_trailing_zeroes = FALSE;
4887
4888 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004889# if defined(FEAT_EVAL)
4890 tvs != NULL ? tv_float(tvs, &arg_idx) :
4891# endif
4892 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004893 abs_f = f < 0 ? -f : f;
4894
4895 if (fmt_spec == 'g' || fmt_spec == 'G')
4896 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004897 // Would be nice to use %g directly, but it prints
4898 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004899 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4900 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004901 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004902 else
4903 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4904 remove_trailing_zeroes = TRUE;
4905 }
4906
Bram Moolenaar04186092016-08-29 21:55:35 +02004907 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004908# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004909 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004910# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004911 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004912# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004913 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004915 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004916 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4917 force_sign, space_for_positive));
4918 str_arg_l = STRLEN(tmp);
4919 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004920 }
4921 else
4922 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004923 if (isnan(f))
4924 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004925 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004926 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4927 : "nan");
4928 str_arg_l = 3;
4929 zero_padding = 0;
4930 }
4931 else if (isinf(f))
4932 {
4933 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4934 force_sign, space_for_positive));
4935 str_arg_l = STRLEN(tmp);
4936 zero_padding = 0;
4937 }
4938 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004939 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004940 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004941 format[0] = '%';
4942 l = 1;
4943 if (force_sign)
4944 format[l++] = space_for_positive ? ' ' : '+';
4945 if (precision_specified)
4946 {
4947 size_t max_prec = TMP_LEN - 10;
4948
Bram Moolenaar85a20022019-12-21 18:25:54 +01004949 // Make sure we don't get more digits than we
4950 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004951 if ((fmt_spec == 'f' || fmt_spec == 'F')
4952 && abs_f > 1.0)
4953 max_prec -= (size_t)log10(abs_f);
4954 if (precision > max_prec)
4955 precision = max_prec;
4956 l += sprintf(format + l, ".%d", (int)precision);
4957 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004958 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004959 format[l + 1] = NUL;
4960
Bram Moolenaare9997822016-08-28 16:03:38 +02004961 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004962 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004963
Bram Moolenaara7241f52008-06-24 20:39:31 +00004964 if (remove_trailing_zeroes)
4965 {
4966 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004967 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004968
Bram Moolenaar85a20022019-12-21 18:25:54 +01004969 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004970 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004971 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004972 else
4973 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004974 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004975 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004976 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004977 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004978 // Remove superfluous '+' and leading
4979 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004980 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004981 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004982 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004983 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004984 --str_arg_l;
4985 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004986 i = (tp[1] == '-') ? 2 : 1;
4987 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004988 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004989 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004990 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004991 --str_arg_l;
4992 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004993 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004994 }
4995 }
4996
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004997 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004998 // Remove trailing zeroes, but keep the one
4999 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005000 while (tp > tmp + 2 && *tp == '0'
5001 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005002 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005003 STRMOVE(tp, tp + 1);
5004 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005005 --str_arg_l;
5006 }
5007 }
5008 else
5009 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005010 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005011
Bram Moolenaar85a20022019-12-21 18:25:54 +01005012 // Be consistent: some printf("%e") use 1.0e+12
5013 // and some 1.0e+012. Remove one zero in the last
5014 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005015 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005016 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005017 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5018 && tp[2] == '0'
5019 && vim_isdigit(tp[3])
5020 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005021 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005022 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005023 --str_arg_l;
5024 }
5025 }
5026 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005027 if (zero_padding && min_field_width > str_arg_l
5028 && (tmp[0] == '-' || force_sign))
5029 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005030 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02005031 number_of_zeros_to_pad = min_field_width - str_arg_l;
5032 zero_padding_insertion_ind = 1;
5033 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005034 str_arg = tmp;
5035 break;
5036 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005037# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005038
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005039 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01005040 // unrecognized conversion specifier, keep format string
5041 // as-is
5042 zero_padding = 0; // turn zero padding off for non-numeric
5043 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005044 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01005045 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005046
Bram Moolenaar85a20022019-12-21 18:25:54 +01005047 // discard the unrecognized conversion, just keep *
5048 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005049 str_arg = p;
5050 str_arg_l = 0;
5051 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005052 str_arg_l++; // include invalid conversion specifier
5053 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005054 break;
5055 }
5056
5057 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005058 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005059
Bram Moolenaar85a20022019-12-21 18:25:54 +01005060 // insert padding to the left as requested by min_field_width;
5061 // this does not include the zero padding in case of numerical
5062 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 if (!justify_left)
5064 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005065 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005066 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005067
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005068 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005069 {
5070 if (str_l < str_m)
5071 {
5072 size_t avail = str_m - str_l;
5073
5074 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005075 (size_t)pn > avail ? avail
5076 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005077 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005078 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005079 }
5080 }
5081
Bram Moolenaar85a20022019-12-21 18:25:54 +01005082 // zero padding as requested by the precision or by the minimal
5083 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005084 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005085 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005086 // will not copy first part of numeric right now, *
5087 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005088 zero_padding_insertion_ind = 0;
5089 }
5090 else
5091 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005092 // insert first part of numerics (sign or '0x') before zero
5093 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005094 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005095
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005096 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005097 {
5098 if (str_l < str_m)
5099 {
5100 size_t avail = str_m - str_l;
5101
5102 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005103 (size_t)zn > avail ? avail
5104 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005105 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005106 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005107 }
5108
Bram Moolenaar85a20022019-12-21 18:25:54 +01005109 // insert zero padding as requested by the precision or min
5110 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005111 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005112 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005113 {
5114 if (str_l < str_m)
5115 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005116 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005117
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005118 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005119 (size_t)zn > avail ? avail
5120 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005121 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005122 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005123 }
5124 }
5125
Bram Moolenaar85a20022019-12-21 18:25:54 +01005126 // insert formatted string
5127 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005128 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005129 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005130
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005131 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005132 {
5133 if (str_l < str_m)
5134 {
5135 size_t avail = str_m - str_l;
5136
5137 mch_memmove(str + str_l,
5138 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005139 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005140 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005141 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005142 }
5143 }
5144
Bram Moolenaar85a20022019-12-21 18:25:54 +01005145 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005146 if (justify_left)
5147 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005148 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005149 int pn = (int)(min_field_width
5150 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005151
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005152 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005153 {
5154 if (str_l < str_m)
5155 {
5156 size_t avail = str_m - str_l;
5157
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005158 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005159 (size_t)pn > avail ? avail
5160 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005161 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005162 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005163 }
5164 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005165 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005166 }
5167 }
5168
5169 if (str_m > 0)
5170 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005171 // make sure the string is nul-terminated even at the expense of
5172 // overwriting the last character (shouldn't happen, but just in case)
5173 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005174 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5175 }
5176
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005177 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005178 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179
Bram Moolenaar85a20022019-12-21 18:25:54 +01005180 // Return the number of characters formatted (excluding trailing nul
5181 // character), that is, the number of characters that would have been
5182 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005183 return (int)str_l;
5184}
5185
Bram Moolenaar85a20022019-12-21 18:25:54 +01005186#endif // PROTO