blob: 38e695900f8fbe55ce4f92d32a7440c8abb600b1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar44ca54a2016-08-27 15:41:32 +020015#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17#include "vim.h"
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static void add_msg_hist(char_u *s, int len, int attr);
20static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010022static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int msg_check_screen(void);
33static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010036static int confirm_msg_used = FALSE; // displaying confirm_msg
37static char_u *confirm_msg = NULL; // ":confirm" message
38static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010041#ifdef FEAT_JOB_CHANNEL
42static int emsg_to_channel_log = FALSE;
43#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020056static FILE *verbose_fd = NULL;
57static int verbose_did_open = FALSE;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059/*
60 * When writing messages to the screen, there are many different situations.
61 * A number of variables is used to remember the current state:
62 * msg_didany TRUE when messages were written since the last time the
63 * user reacted to a prompt.
64 * Reset: After hitting a key for the hit-return prompt,
65 * hitting <CR> for the command line or input().
66 * Set: When any message is written to the screen.
67 * msg_didout TRUE when something was written to the current line.
68 * Reset: When advancing to the next line, when the current
69 * text can be overwritten.
70 * Set: When any message is written to the screen.
71 * msg_nowait No extra delay for the last drawn message.
72 * Used in normal_cmd() before the mode message is drawn.
73 * emsg_on_display There was an error message recently. Indicates that there
74 * should be a delay before redrawing.
75 * msg_scroll The next message should not overwrite the current one.
76 * msg_scrolled How many lines the screen has been scrolled (because of
77 * messages). Used in update_screen() to scroll the screen
78 * back. Incremented each time the screen scrolls a line.
79 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
80 * writes something without scrolling should not make
81 * need_wait_return to be set. This is a hack to make ":ts"
82 * work without an extra prompt.
83 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010084 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 * need_wait_return TRUE when the hit-return prompt is needed.
86 * Reset: After giving the hit-return prompt, when the user
87 * has answered some other prompt.
88 * Set: When the ruler or typeahead display is overwritten,
89 * scrolling the screen for some message.
90 * keep_msg Message to be displayed after redrawing the screen, in
91 * main_loop().
92 * This is an allocated string or NULL when not used.
93 */
94
95/*
96 * msg(s) - displays the string 's' on the status line
97 * When terminal not initialized (yet) mch_errmsg(..) is used.
98 * return TRUE if wait_return not called
99 */
100 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100101msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 return msg_attr_keep(s, 0, FALSE);
104}
105
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106/*
107 * Like msg() but keep it silent when 'verbosefile' is set.
108 */
109 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100110verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111{
112 int n;
113
114 verbose_enter();
115 n = msg_attr_keep(s, 0, FALSE);
116 verbose_leave();
117
118 return n;
119}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100122msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 return msg_attr_keep(s, attr, FALSE);
125}
126
127 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100128msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100129 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100130 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100131 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 static int entered = 0;
134 int retval;
135 char_u *buf = NULL;
136
Bram Moolenaar85a20022019-12-21 18:25:54 +0100137 // Skip messages not matching ":filter pattern".
138 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100139 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200140 return TRUE;
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_EVAL
143 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100144 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
146
147 /*
148 * It is possible that displaying a messages causes a problem (e.g.,
149 * when redrawing the window), which causes another message, etc.. To
150 * break this loop, limit the recursiveness to 3 levels.
151 */
152 if (entered >= 3)
153 return TRUE;
154 ++entered;
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // Add message to history (unless it's a repeated kept message or a
157 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100158 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 || (*s != '<'
160 && last_msg_hist != NULL
161 && last_msg_hist->msg != NULL
162 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100163 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaar958dc692016-12-01 15:34:12 +0100165#ifdef FEAT_JOB_CHANNEL
166 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100167 // Write message in the channel log.
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200168 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100169#endif
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000172 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100173 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar32526b32019-01-19 17:43:09 +0100177 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 msg_clr_eos();
179 retval = msg_end();
180
Bram Moolenaar32526b32019-01-19 17:43:09 +0100181 if (keep && retval && vim_strsize((char_u *)s)
182 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
183 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185 vim_free(buf);
186 --entered;
187 return retval;
188}
189
190/*
191 * Truncate a string such that it can be printed without causing a scroll.
192 * Returns an allocated string or NULL when no truncating is done.
193 */
194 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100195msg_strtrunc(
196 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 char_u *buf = NULL;
200 int len;
201 int room;
202
Bram Moolenaar85a20022019-12-21 18:25:54 +0100203 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000204 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
205 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000208 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100209 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000210 room = (int)(Rows - msg_row) * Columns - 1;
211 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100212 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000213 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if (len > room && room > 0)
215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // may have up to 18 bytes per cell (6 per char, up to two
218 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100219 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100221 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100222 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = room + 2;
225 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229 }
230 return buf;
231}
232
233/*
234 * Truncate a string "s" to "buf" with cell width "room".
235 * "s" and "buf" may be equal.
236 */
237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100238trunc_string(
239 char_u *s,
240 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200241 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100242 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100244 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200245 size_t half;
246 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 int e;
248 int i;
249 int n;
250
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200251 if (room_in < 3)
252 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254
Bram Moolenaar85a20022019-12-21 18:25:54 +0100255 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100256 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
258 if (s[e] == NUL)
259 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100260 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 buf[e] = NUL;
262 return;
263 }
264 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200265 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 break;
267 len += n;
268 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000270 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100272 if (++e == buflen)
273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 buf[e] = s[e];
275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
277
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 if (enc_dbcs != 0)
281 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100282 // For DBCS going backwards in a string is slow, but
283 // computing the cell width isn't too slow: go forward
284 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 n = vim_strsize(s + i);
286 while (len + n > room)
287 {
288 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000289 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 }
291 }
292 else if (enc_utf8)
293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000295 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 for (;;)
297 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000298 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200299 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200300 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200302 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 break;
304 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200305 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307 }
308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100310 for (i = (int)STRLEN(s);
311 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 len += n;
313 }
314
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200315
316 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100317 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100318 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200319 if (s != buf)
320 {
321 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200322 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200323 len = buflen - 1;
324 len = len - e + 1;
325 if (len < 1)
326 buf[e - 1] = NUL;
327 else
328 mch_memmove(buf + e, s + e, len);
329 }
330 }
331 else if (e + 3 < buflen)
332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100333 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100334 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200335 len = STRLEN(s + i) + 1;
336 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100337 len = buflen - e - 3 - 1;
338 mch_memmove(buf + e + 3, s + i, len);
339 buf[e + 3 + len - 1] = NUL;
340 }
341 else
342 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100343 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200344 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346}
347
348/*
349 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100350 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 * shorter than IOSIZE!!!
352 */
353#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100355int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200360 if (IObuff == NULL)
361 {
362 // Very early in initialisation and already something wrong, just
363 // give the raw message so the user at least gets a hint.
364 return msg((char *)s);
365 }
366 else
367 {
368 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200370 va_start(arglist, s);
371 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
372 va_end(arglist);
373 return msg((char *)IObuff);
374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375}
376
377 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100378smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200380 if (IObuff == NULL)
381 {
382 // Very early in initialisation and already something wrong, just
383 // give the raw message so the user at least gets a hint.
384 return msg_attr((char *)s, attr);
385 }
386 else
387 {
388 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200390 va_start(arglist, s);
391 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
392 va_end(arglist);
393 return msg_attr((char *)IObuff, attr);
394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395}
396
Bram Moolenaare0429682018-07-01 16:44:03 +0200397 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100398smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200399{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200400 if (IObuff == NULL)
401 {
402 // Very early in initialisation and already something wrong, just
403 // give the raw message so the user at least gets a hint.
404 return msg_attr_keep((char *)s, attr, TRUE);
405 }
406 else
407 {
408 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200409
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200410 va_start(arglist, s);
411 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
412 va_end(arglist);
413 return msg_attr_keep((char *)IObuff, attr, TRUE);
414 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200415}
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#endif
418
419/*
420 * Remember the last sourcing name/lnum used in an error message, so that it
421 * isn't printed each time when it didn't change.
422 */
423static int last_sourcing_lnum = 0;
424static char_u *last_sourcing_name = NULL;
425
426/*
427 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
428 * for the next error message;
429 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000430 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100431reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100433 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 last_sourcing_lnum = 0;
435}
436
437/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100438 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000439 */
440 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100441other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000442{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100443 if (SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000444 {
445 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100446 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000447 return TRUE;
448 }
449 return FALSE;
450}
451
452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * Get the message about the source, as used for an error message.
454 * Returns an allocated string with room for one more character.
455 * Returns NULL when no message is to be given.
456 */
457 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100458get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 char_u *Buf, *p;
461
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100462 if (SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {
Bram Moolenaara5d04232020-07-26 15:37:02 +0200464 char_u *sname = estack_sfile(FALSE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100465 char_u *tofree = sname;
466
467 if (sname == NULL)
468 sname = SOURCING_NAME;
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100471 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100473 sprintf((char *)Buf, (char *)p, sname);
474 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 return Buf;
476 }
477 return NULL;
478}
479
480/*
481 * Get the message about the source lnum, as used for an error message.
482 * Returns an allocated string with room for one more character.
483 * Returns NULL when no message is to be given.
484 */
485 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100486get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
488 char_u *Buf, *p;
489
Bram Moolenaar85a20022019-12-21 18:25:54 +0100490 // lnum is 0 when executing a command from the command line
491 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100492 if (SOURCING_NAME != NULL
493 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
494 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200497 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100499 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 return Buf;
501 }
502 return NULL;
503}
504
505/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000506 * Display name and line number for the source of an error.
507 * Remember the file name and line number, so that for the next error the info
508 * is only displayed if it changed.
509 */
510 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100511msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000512{
513 char_u *p;
514
515 ++no_wait_return;
516 p = get_emsg_source();
517 if (p != NULL)
518 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100519 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000520 vim_free(p);
521 }
522 p = get_emsg_lnum();
523 if (p != NULL)
524 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100525 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000526 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100527 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000528 }
529
Bram Moolenaar85a20022019-12-21 18:25:54 +0100530 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100531 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000532 {
533 vim_free(last_sourcing_name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100534 if (SOURCING_NAME == NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000535 last_sourcing_name = NULL;
536 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100537 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000538 }
539 --no_wait_return;
540}
541
542/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000543 * Return TRUE if not giving error messages right now:
544 * If "emsg_off" is set: no error messages at the moment.
545 * If "msg" is in 'debug': do error message but without side effects.
546 * If "emsg_skip" is set: never do error messages.
547 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200548 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100549emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000550{
551 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
552 && vim_strchr(p_debug, 't') == NULL)
553#ifdef FEAT_EVAL
554 || emsg_skip > 0
555#endif
556 )
557 return TRUE;
558 return FALSE;
559}
560
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100561#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100562static garray_T ignore_error_list = GA_EMPTY;
563
564 void
565ignore_error_for_testing(char_u *error)
566{
567 if (ignore_error_list.ga_itemsize == 0)
568 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
569
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100570 if (STRCMP("RESET", error) == 0)
571 ga_clear_strings(&ignore_error_list);
572 else
573 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100574}
575
576 static int
577ignore_error(char_u *msg)
578{
579 int i;
580
581 for (i = 0; i < ignore_error_list.ga_len; ++i)
582 if (strstr((char *)msg,
583 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
584 return TRUE;
585 return FALSE;
586}
587#endif
588
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200589#if !defined(HAVE_STRERROR) || defined(PROTO)
590/*
591 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100592 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200593 */
594 void
595do_perror(char *msg)
596{
597 perror(msg);
598 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100599 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200600 --emsg_silent;
601}
602#endif
603
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000604/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100605 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 *
607 * Rings the bell, if appropriate, and calls message() to do the real work
608 * When terminal not initialized (yet) mch_errmsg(..) is used.
609 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100610 * Return TRUE if wait_return not called.
611 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100613 static int
614emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100618 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#ifdef FEAT_EVAL
620 int ignore = FALSE;
621 int severe;
622#endif
623
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100624#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100625 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100626 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100627 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100628 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100629#endif
630
Bram Moolenaar53989552019-12-23 22:59:18 +0100631 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100634 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
635 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 severe = emsg_severe;
637 emsg_severe = FALSE;
638#endif
639
Bram Moolenaar57657d82006-04-21 22:12:41 +0000640 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {
642#ifdef FEAT_EVAL
643 /*
644 * Cause a throw of an error exception if appropriate. Don't display
645 * the error message in this case. (If no matching catch clause will
646 * be found, the message will be displayed later on.) "ignore" is set
647 * when the message should be ignored completely (used for the
648 * interrupt message).
649 */
650 if (cause_errthrow(s, severe, &ignore) == TRUE)
651 {
652 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100653 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 return TRUE;
655 }
656
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200657 if (emsg_assert_fails_used && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200658 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200659 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200660 emsg_assert_fails_lnum = SOURCING_LNUM;
661 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200662
Bram Moolenaar85a20022019-12-21 18:25:54 +0100663 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 set_vim_var_string(VV_ERRMSG, s, -1);
665#endif
666
667 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000668 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 * But do write it to the redirection file.
670 */
671 if (emsg_silent != 0)
672 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200673 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200675 msg_start();
676 p = get_emsg_source();
677 if (p != NULL)
678 {
679 STRCAT(p, "\n");
680 redir_write(p, -1);
681 vim_free(p);
682 }
683 p = get_emsg_lnum();
684 if (p != NULL)
685 {
686 STRCAT(p, "\n");
687 redir_write(p, -1);
688 vim_free(p);
689 }
690 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100692#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200693 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 return TRUE;
696 }
697
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100698 ex_exitval = 1;
699
Bram Moolenaar85a20022019-12-21 18:25:54 +0100700 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 msg_silent = 0;
702 cmd_silent = FALSE;
703
Bram Moolenaar85a20022019-12-21 18:25:54 +0100704 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ++global_busy;
706
707 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200710 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100711 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200712#ifdef FEAT_EVAL
713 did_uncaught_emsg = TRUE;
714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
716
Bram Moolenaar85a20022019-12-21 18:25:54 +0100717 emsg_on_display = TRUE; // remember there is an error message
718 ++msg_scroll; // don't overwrite a previous message
719 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000720 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100721 need_wait_return = TRUE; // needed in case emsg() is called after
722 // wait_return has reset need_wait_return
723 // and a redraw is expected because
724 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaar958dc692016-12-01 15:34:12 +0100726#ifdef FEAT_JOB_CHANNEL
727 emsg_to_channel_log = TRUE;
728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 /*
730 * Display name and line number for the source of the error.
731 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000732 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 /*
735 * Display the error message itself.
736 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100737 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100738 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100739
740#ifdef FEAT_JOB_CHANNEL
741 emsg_to_channel_log = FALSE;
742#endif
743 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744}
745
746/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100747 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100750emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100752 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 if (!emsg_not_now())
754 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100755 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756}
757
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100758#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100759/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100760 * Print an error message with format string and variable arguments.
761 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100762 */
763 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100765{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200766 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100767 if (!emsg_not_now())
768 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200769 if (IObuff == NULL)
770 {
771 // Very early in initialisation and already something wrong, just
772 // give the raw message so the user at least gets a hint.
773 return emsg_core((char_u *)s);
774 }
775 else
776 {
777 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100778
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200779 va_start(ap, s);
780 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
781 va_end(ap);
782 return emsg_core(IObuff);
783 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200785 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100786}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100787#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100788
789/*
790 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
791 * defined. It is used for internal errors only, so that they can be
792 * detected when fuzzing vim.
793 */
794 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100795iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100796{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100797 if (!emsg_not_now())
798 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799#ifdef ABORT_ON_INTERNAL_ERROR
800 abort();
801#endif
802}
803
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100804#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100805/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807 * defined. It is used for internal errors only, so that they can be
808 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100809 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100810 */
811 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100812siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100813{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100814 if (!emsg_not_now())
815 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200816 if (IObuff == NULL)
817 {
818 // Very early in initialisation and already something wrong, just
819 // give the raw message so the user at least gets a hint.
820 emsg_core((char_u *)s);
821 }
822 else
823 {
824 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100825
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200826 va_start(ap, s);
827 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
828 va_end(ap);
829 emsg_core(IObuff);
830 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100832# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100833 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100834# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100835}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100836#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837
838/*
839 * Give an "Internal error" message.
840 */
841 void
842internal_error(char *where)
843{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100844 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845}
846
Bram Moolenaardd589232020-02-29 17:38:12 +0100847/*
848 * Like internal_error() but do not call abort(), to avoid tests using
849 * test_unknown() and test_void() causing Vim to exit.
850 */
851 void
852internal_error_no_abort(char *where)
853{
854 semsg(_(e_intern2), where);
855}
856
Bram Moolenaar85a20022019-12-21 18:25:54 +0100857// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaardf177f62005-02-22 08:39:57 +0000859 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100860emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000861{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100862 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000863}
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 * Give an error message which contains %s for "name[len]".
867 */
868 void
869emsg_namelen(char *msg, char_u *name, int len)
870{
871 char_u *copy = vim_strnsave((char_u *)name, len);
872
873 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100874 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875}
876
877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Like msg(), but truncate to a single line if p_shm contains 't', or when
879 * "force" is TRUE. This truncates in another way as for normal messages.
880 * Careful: The string may be changed by msg_may_trunc()!
881 * Returns a pointer to the printed message, if wait_return() not called.
882 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100883 char *
884msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
886 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100887 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
Bram Moolenaar85a20022019-12-21 18:25:54 +0100889 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100890 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaar32526b32019-01-19 17:43:09 +0100892 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100895 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 msg_hist_off = FALSE;
897
898 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100899 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 return NULL;
901}
902
903/*
904 * Check if message "s" should be truncated at the start (for filenames).
905 * Return a pointer to where the truncated message starts.
906 * Note: May change the message by replacing a character with '<'.
907 */
908 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100909msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 int n;
912 int room;
913
914 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
915 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
916 && (n = (int)STRLEN(s) - room) > 0)
917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (has_mbyte)
919 {
920 int size = vim_strsize(s);
921
Bram Moolenaar85a20022019-12-21 18:25:54 +0100922 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000923 if (size <= room)
924 return s;
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 for (n = 0; size >= room; )
927 {
928 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000929 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931 --n;
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 s += n;
934 *s = '<';
935 }
936 return s;
937}
938
939 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100940add_msg_hist(
941 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100942 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100943 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
945 struct msg_hist *p;
946
947 if (msg_hist_off || msg_silent != 0)
948 return;
949
Bram Moolenaar85a20022019-12-21 18:25:54 +0100950 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000951 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000952 (void)delete_first_msg();
953
Bram Moolenaar85a20022019-12-21 18:25:54 +0100954 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200955 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (p != NULL)
957 {
958 if (len < 0)
959 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100960 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 while (len > 0 && *s == '\n')
962 {
963 ++s;
964 --len;
965 }
966 while (len > 0 && s[len - 1] == '\n')
967 --len;
968 p->msg = vim_strnsave(s, len);
969 p->next = NULL;
970 p->attr = attr;
971 if (last_msg_hist != NULL)
972 last_msg_hist->next = p;
973 last_msg_hist = p;
974 if (first_msg_hist == NULL)
975 first_msg_hist = last_msg_hist;
976 ++msg_hist_len;
977 }
978}
979
980/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000981 * Delete the first (oldest) message from the history.
982 * Returns FAIL if there are no messages.
983 */
984 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100985delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000986{
987 struct msg_hist *p;
988
989 if (msg_hist_len <= 0)
990 return FAIL;
991 p = first_msg_hist;
992 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000993 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100994 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000995 vim_free(p->msg);
996 vim_free(p);
997 --msg_hist_len;
998 return OK;
999}
1000
1001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * ":messages" command.
1003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001005ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 struct msg_hist *p;
1008 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001009 int c = 0;
1010
1011 if (STRCMP(eap->arg, "clear") == 0)
1012 {
1013 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1014
1015 while (msg_hist_len > keep)
1016 (void)delete_first_msg();
1017 return;
1018 }
1019
1020 if (*eap->arg != NUL)
1021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001022 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001023 return;
1024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
1026 msg_hist_off = TRUE;
1027
Bram Moolenaar451f8492016-04-14 17:16:22 +02001028 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001029 if (eap->addr_count != 0)
1030 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001031 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001032 for (; p != NULL && !got_int; p = p->next)
1033 c++;
1034
1035 c -= eap->line2;
1036
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001038 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1039 p = p->next, c--);
1040 }
1041
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001042 if (p == first_msg_hist)
1043 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001044#ifdef FEAT_MULTI_LANG
1045 s = get_mess_lang();
1046#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001047 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001048#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001049 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001050 // The next comment is extracted by xgettext and put in po file for
1051 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001052 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001053 // Translator: Please replace the name and email address
1054 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001055 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001056 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001057 }
1058
Bram Moolenaar85a20022019-12-21 18:25:54 +01001059 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001060 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001062 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064 msg_hist_off = FALSE;
1065}
1066
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001067#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068/*
1069 * Call this after prompting the user. This will avoid a hit-return message
1070 * and a delay.
1071 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001072 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001073msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075 need_wait_return = FALSE;
1076 emsg_on_display = FALSE;
1077 cmdline_row = msg_row;
1078 msg_col = 0;
1079 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001080 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081}
1082#endif
1083
1084/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001085 * Wait for the user to hit a key (normally Enter).
1086 * If "redraw" is TRUE, clear and redraw the screen.
1087 * If "redraw" is FALSE, just redraw the screen.
1088 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
1090 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001091wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
1093 int c;
1094 int oldState;
1095 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001097 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001098 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100 if (redraw == TRUE)
1101 must_redraw = CLEAR;
1102
Bram Moolenaar85a20022019-12-21 18:25:54 +01001103 // If using ":silent cmd", don't wait for a return. Also don't set
1104 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (msg_silent != 0)
1106 return;
1107
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001108 /*
1109 * When inside vgetc(), we can't wait for a typed character at all.
1110 * With the global command (and some others) we only need one return at
1111 * the end. Adjust cmdline_row to avoid the next message overwriting the
1112 * last one.
1113 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001114 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001116 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (no_wait_return)
1118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (!exmode_active)
1120 cmdline_row = msg_row;
1121 return;
1122 }
1123
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 oldState = State;
1126 if (quit_more)
1127 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 quit_more = FALSE;
1130 got_int = FALSE;
1131 }
1132 else if (exmode_active)
1133 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001134 msg_puts(" "); // make sure the cursor is on the right line
1135 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 got_int = FALSE;
1137 }
1138 else
1139 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // Make sure the hit-return prompt is on screen when 'guioptions' was
1141 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 screenalloc(FALSE);
1143
1144 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001149 cmdline_row = msg_row;
1150
Bram Moolenaar85a20022019-12-21 18:25:54 +01001151 // Avoid the sequence that the user types ":" at the hit-return prompt
1152 // to start an Ex command, but the file-changed dialog gets in the
1153 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001154 if (need_check_timestamps)
1155 check_timestamps(FALSE);
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 hit_return_msg();
1158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 do
1160 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001161 // Remember "got_int", if it is set vgetc() probably returns a
1162 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001164
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 // Don't do mappings here, we put the character back in the
1166 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001167 ++no_mapping;
1168 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001169
Bram Moolenaar85a20022019-12-21 18:25:54 +01001170 // Temporarily disable Recording. If Recording is active, the
1171 // character will be recorded later, since it will be added to the
1172 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001173 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001174 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001175 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001176 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001178 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001180 --no_mapping;
1181 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001182 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001183 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001186 // Strange way to allow copying (yanking) a modeless selection at
1187 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1188 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1190 {
1191 clip_copy_modeless_selection(TRUE);
1192 c = K_IGNORE;
1193 }
1194#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001195
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001196 /*
1197 * Allow scrolling back in the messages.
1198 * Also accept scroll-down commands when messages fill the screen,
1199 * to avoid that typing one 'j' too many makes the messages
1200 * disappear.
1201 */
1202 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001203 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001204 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1205 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001206 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001207 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001209 do_more_prompt(c);
1210 else
1211 {
1212 msg_didout = FALSE;
1213 c = K_IGNORE;
1214 msg_col =
1215#ifdef FEAT_RIGHTLEFT
1216 cmdmsg_rl ? Columns - 1 :
1217#endif
1218 0;
1219 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001220 if (quit_more)
1221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001223 quit_more = FALSE;
1224 got_int = FALSE;
1225 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001226 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001227 {
1228 c = K_IGNORE;
1229 hit_return_msg();
1230 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001231 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001232 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001233 && (c == 'j' || c == 'd' || c == 'f'
1234 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001235 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 } while ((had_got_int && c == Ctrl_C)
1238 || c == K_IGNORE
1239#ifdef FEAT_GUI
1240 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1243 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1244 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001245 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001247 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 || (!mouse_has(MOUSE_RETURN)
1249 && mouse_row < msg_row
1250 && (c == K_LEFTMOUSE
1251 || c == K_MIDDLEMOUSE
1252 || c == K_RIGHTMOUSE
1253 || c == K_X1MOUSE
1254 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 );
1256 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /*
1258 * Avoid that the mouse-up event causes visual mode to start.
1259 */
1260 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1261 || c == K_X1MOUSE || c == K_X2MOUSE)
1262 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001263 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001265 // Put the character back in the typeahead buffer. Don't use the
1266 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001267 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001268 do_redraw = TRUE; // need a redraw even though there is
1269 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 redir_off = FALSE;
1273
1274 /*
1275 * If the user hits ':', '?' or '/' we get a command line from the next
1276 * line.
1277 */
1278 if (c == ':' || c == '?' || c == '/')
1279 {
1280 if (!exmode_active)
1281 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001282 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001284#ifdef FEAT_TERMINAL
1285 skip_term_loop = TRUE;
1286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288
1289 /*
1290 * If the window size changed set_shellsize() will redraw the screen.
1291 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1292 * typed.
1293 */
1294 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001295 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 msg_check();
1298
1299#if defined(UNIX) || defined(VMS)
1300 /*
1301 * When switching screens, we need to output an extra newline on exit.
1302 */
1303 if (swapping_screen() && !termcap_active)
1304 newline_on_exit = TRUE;
1305#endif
1306
1307 need_wait_return = FALSE;
1308 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001309 emsg_on_display = FALSE; // can delete error message now
1310 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 reset_last_sourcing();
1312 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1313 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001314 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
Bram Moolenaar85a20022019-12-21 18:25:54 +01001316 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 shell_resized();
1320 }
1321 else if (!skip_redraw
1322 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 redraw_later(VALID);
1326 }
1327}
1328
1329/*
1330 * Write the hit-return prompt.
1331 */
1332 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001333hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001335 int save_p_more = p_more;
1336
Bram Moolenaar85a20022019-12-21 18:25:54 +01001337 p_more = FALSE; // don't want see this message when scrolling back
1338 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 msg_putchar('\n');
1340 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001341 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
Bram Moolenaar32526b32019-01-19 17:43:09 +01001343 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (!msg_use_printf())
1345 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001346 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347}
1348
1349/*
1350 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1351 */
1352 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001353set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 vim_free(keep_msg);
1356 if (s != NULL && msg_silent == 0)
1357 keep_msg = vim_strsave(s);
1358 else
1359 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001360 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001361 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362}
1363
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001364#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1365/*
1366 * If there currently is a message being displayed, set "keep_msg" to it, so
1367 * that it will be displayed again after redraw.
1368 */
1369 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001370set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001371{
1372 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1373 && (State & NORMAL))
1374 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1375}
1376#endif
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378/*
1379 * Prepare for outputting characters in the command line.
1380 */
1381 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001382msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 int did_return = FALSE;
1385
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001386 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001387 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001388
1389#ifdef FEAT_EVAL
1390 if (need_clr_eos)
1391 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001392 // Halfway an ":echo" command and getting an (error) message: clear
1393 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001394 need_clr_eos = FALSE;
1395 msg_clr_eos();
1396 }
1397#endif
1398
Bram Moolenaar85a20022019-12-21 18:25:54 +01001399 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 {
1401 msg_row = cmdline_row;
1402 msg_col =
1403#ifdef FEAT_RIGHTLEFT
1404 cmdmsg_rl ? Columns - 1 :
1405#endif
1406 0;
1407 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001408 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 {
1410 msg_putchar('\n');
1411 did_return = TRUE;
1412 if (exmode_active != EXMODE_NORMAL)
1413 cmdline_row = msg_row;
1414 }
1415 if (!msg_didany || lines_left < 0)
1416 msg_starthere();
1417 if (msg_silent == 0)
1418 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001419 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 cursor_off();
1421 }
1422
Bram Moolenaar85a20022019-12-21 18:25:54 +01001423 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (!did_return)
1425 redir_write((char_u *)"\n", -1);
1426}
1427
1428/*
1429 * Note that the current msg position is where messages start.
1430 */
1431 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001432msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 lines_left = cmdline_row;
1435 msg_didany = FALSE;
1436}
1437
1438 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001439msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 msg_putchar_attr(c, 0);
1442}
1443
1444 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001445msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001447 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 if (IS_SPECIAL(c))
1450 {
1451 buf[0] = K_SPECIAL;
1452 buf[1] = K_SECOND(c);
1453 buf[2] = K_THIRD(c);
1454 buf[3] = NUL;
1455 }
1456 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001457 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001458 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459}
1460
1461 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001462msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001464 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaar32526b32019-01-19 17:43:09 +01001466 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 msg_puts(buf);
1468}
1469
1470 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001471msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473 msg_home_replace_attr(fname, 0);
1474}
1475
1476#if defined(FEAT_FIND_ID) || defined(PROTO)
1477 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001478msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001480 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481}
1482#endif
1483
1484 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001485msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 char_u *name;
1488
1489 name = home_replace_save(NULL, fname);
1490 if (name != NULL)
1491 msg_outtrans_attr(name, attr);
1492 vim_free(name);
1493}
1494
1495/*
1496 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001497 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 * Use attributes 'attr'.
1499 * Return the number of characters it takes on the screen.
1500 */
1501 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001502msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 return msg_outtrans_attr(str, 0);
1505}
1506
1507 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001508msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1511}
1512
1513 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001514msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 return msg_outtrans_len_attr(str, len, 0);
1517}
1518
1519/*
1520 * Output one character at "p". Return pointer to the next character.
1521 * Handles multi-byte characters.
1522 */
1523 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001524msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 int l;
1527
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001528 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 msg_outtrans_len_attr(p, l, attr);
1531 return p + l;
1532 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001533 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return p + 1;
1535}
1536
1537 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001538msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 int retval = 0;
1541 char_u *str = msgstr;
1542 char_u *plain_start = msgstr;
1543 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 int mb_l;
1545 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaar85a20022019-12-21 18:25:54 +01001547 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (attr & MSG_HIST)
1549 {
1550 add_msg_hist(str, len, attr);
1551 attr &= ~MSG_HIST;
1552 }
1553
Bram Moolenaar85a20022019-12-21 18:25:54 +01001554 // If the string starts with a composing character first draw a space on
1555 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001557 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
1559 /*
1560 * Go over the string. Special characters are translated and printed.
1561 * Normal characters are printed several at a time.
1562 */
1563 while (--len >= 0)
1564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001566 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001567 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001569 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 else
1571 mb_l = 1;
1572 if (has_mbyte && mb_l > 1)
1573 {
1574 c = (*mb_ptr2char)(str);
1575 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001576 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 retval += (*mb_ptr2cells)(str);
1578 else
1579 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001580 // unprintable multi-byte char: print the printable chars so
1581 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001583 msg_puts_attr_len((char *)plain_start,
1584 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001586 msg_puts_attr((char *)transchar(c),
1587 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 retval += char2cells(c);
1589 }
1590 len -= mb_l - 1;
1591 str += mb_l;
1592 }
1593 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 s = transchar_byte(*str);
1596 if (s[1] != NUL)
1597 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001598 // unprintable char: print the printable chars so far and the
1599 // 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 + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001604 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001605 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001607 else
1608 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 ++str;
1610 }
1611 }
1612
1613 if (str > plain_start)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001614 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001615 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 return retval;
1618}
1619
1620#if defined(FEAT_QUICKFIX) || defined(PROTO)
1621 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001622msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 int i;
1625 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1626
1627 arg = skipwhite(arg);
1628 for (i = 5; *arg && i >= 0; --i)
1629 if (*arg++ != str[i])
1630 break;
1631 if (i < 0)
1632 {
1633 msg_putchar('\n');
1634 for (i = 0; rs[i]; ++i)
1635 msg_putchar(rs[i] - 3);
1636 }
1637}
1638#endif
1639
1640/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001641 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 * Return the number of characters it takes on the screen.
1643 *
1644 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1645 * following character and shown as <F1>, <S-Up> etc. Any other character
1646 * which is not printable shown in <> form.
1647 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1648 * If a character is displayed in one of these special ways, is also
1649 * highlighted (its highlight name is '8' in the p_hl variable).
1650 * Otherwise characters are not highlighted.
1651 * This function is used to show mappings, where we want to see how to type
1652 * the character/string -- webb
1653 */
1654 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001655msg_outtrans_special(
1656 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001657 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001658 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 char_u *str = strstart;
1661 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001662 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 int attr;
1664 int len;
1665
Bram Moolenaar8820b482017-03-16 17:23:31 +01001666 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 while (*str != NUL)
1668 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001669 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if ((str == strstart || str[1] == NUL) && *str == ' ')
1671 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001672 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 ++str;
1674 }
1675 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001676 text = (char *)str2special(&str, from);
1677 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001678 if (maxlen > 0 && retval + len >= maxlen)
1679 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001680 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001681 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001682 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 retval += len;
1684 }
1685 return retval;
1686}
1687
Bram Moolenaarbd743252010-10-20 21:23:33 +02001688#if defined(FEAT_EVAL) || defined(PROTO)
1689/*
1690 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1691 * strings, in an allocated string.
1692 */
1693 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001694str2special_save(
1695 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001697{
1698 garray_T ga;
1699 char_u *p = str;
1700
1701 ga_init2(&ga, 1, 40);
1702 while (*p != NUL)
1703 ga_concat(&ga, str2special(&p, is_lhs));
1704 ga_append(&ga, NUL);
1705 return (char_u *)ga.ga_data;
1706}
1707#endif
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/*
1710 * Return the printable string for the key codes at "*sp".
1711 * Used for translating the lhs or rhs of a mapping to printable chars.
1712 * Advances "sp" to the next code.
1713 */
1714 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001715str2special(
1716 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001717 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 int c;
1720 static char_u buf[7];
1721 char_u *str = *sp;
1722 int modifiers = 0;
1723 int special = FALSE;
1724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (has_mbyte)
1726 {
1727 char_u *p;
1728
Bram Moolenaar85a20022019-12-21 18:25:54 +01001729 // Try to un-escape a multi-byte character. Return the un-escaped
1730 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 p = mb_unescape(sp);
1732 if (p != NULL)
1733 return p;
1734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
1736 c = *str;
1737 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1738 {
1739 if (str[1] == KS_MODIFIER)
1740 {
1741 modifiers = str[2];
1742 str += 3;
1743 c = *str;
1744 }
1745 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1746 {
1747 c = TO_SPECIAL(str[1], str[2]);
1748 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001750 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 special = TRUE;
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001754 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001756 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001757
Bram Moolenaar85a20022019-12-21 18:25:54 +01001758 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001759 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1760 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001761 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001762 *sp = str + 1;
1763 return buf;
1764 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001765 // Since 'special' is TRUE the multi-byte character 'c' will be
1766 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001767 c = (*mb_ptr2char)(str);
1768 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001770 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001771 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Bram Moolenaar85a20022019-12-21 18:25:54 +01001773 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1774 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (special || char2cells(c) > 1 || (from && c == ' '))
1776 return get_special_key_name(c, modifiers);
1777 buf[0] = c;
1778 buf[1] = NUL;
1779 return buf;
1780}
1781
1782/*
1783 * Translate a key sequence into special key names.
1784 */
1785 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001786str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 char_u *s;
1789
1790 *buf = NUL;
1791 while (*sp)
1792 {
1793 s = str2special(&sp, FALSE);
1794 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1795 STRCAT(buf, s);
1796 }
1797}
1798
1799/*
1800 * print line for :print or :list command
1801 */
1802 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001803msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
1805 int c;
1806 int col = 0;
1807 int n_extra = 0;
1808 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001809 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001810 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001812 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 int l;
1815 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
Bram Moolenaardf177f62005-02-22 08:39:57 +00001817 if (curwin->w_p_list)
1818 list = TRUE;
1819
Bram Moolenaar85a20022019-12-21 18:25:54 +01001820 // find start of trailing whitespace
Bram Moolenaardf177f62005-02-22 08:39:57 +00001821 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
1823 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001824 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 --trail;
1826 }
1827
Bram Moolenaar85a20022019-12-21 18:25:54 +01001828 // output a space for an empty line, otherwise the line will be
1829 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001830 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 msg_putchar(' ');
1832
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001833 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001835 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001838 if (n_extra == 0 && c_final)
1839 c = c_final;
1840 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 c = c_extra;
1842 else
1843 c = *p_extra++;
1844 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001845 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
1847 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001848 if (lcs_nbsp != NUL && list
1849 && (mb_ptr2char(s) == 160
1850 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001851 {
1852 mb_char2bytes(lcs_nbsp, buf);
1853 buf[(*mb_ptr2len)(buf)] = NUL;
1854 }
1855 else
1856 {
1857 mch_memmove(buf, s, (size_t)l);
1858 buf[l] = NUL;
1859 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001860 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 s += l;
1862 continue;
1863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 else
1865 {
1866 attr = 0;
1867 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001868 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001870 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001871#ifdef FEAT_VARTABS
1872 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1873 curbuf->b_p_vts_array) - 1;
1874#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001876#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001877 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 c = ' ';
1880 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001881 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 else
1884 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001885 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001887 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001888 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001891 else if (c == 160 && list && lcs_nbsp != NUL)
1892 {
1893 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001894 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001895 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001896 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 {
1898 p_extra = (char_u *)"";
1899 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001900 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 n_extra = 1;
1902 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001903 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 --s;
1905 }
1906 else if (c != NUL && (n = byte2cells(c)) > 1)
1907 {
1908 n_extra = n - 1;
1909 p_extra = transchar_byte(c);
1910 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001911 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001913 // Use special coloring to be able to distinguish <hex> from
1914 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001915 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 else if (c == ' ' && trail != NULL && s > trail)
1918 {
1919 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001920 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001922 else if (c == ' ' && list && lcs_space != NUL)
1923 {
1924 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001925 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928
1929 if (c == NUL)
1930 break;
1931
1932 msg_putchar_attr(c, attr);
1933 col++;
1934 }
1935 msg_clr_eos();
1936}
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938/*
1939 * Use screen_puts() to output one multi-byte character.
1940 * Return the pointer "s" advanced to the next character.
1941 */
1942 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001943screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944{
1945 int cw;
1946
Bram Moolenaar85a20022019-12-21 18:25:54 +01001947 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 cw = (*mb_ptr2cells)(s);
1949 if (cw > 1 && (
1950#ifdef FEAT_RIGHTLEFT
1951 cmdmsg_rl ? msg_col <= 1 :
1952#endif
1953 msg_col == Columns - 1))
1954 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001955 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001956 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 return s;
1958 }
1959
1960 screen_puts_len(s, l, msg_row, msg_col, attr);
1961#ifdef FEAT_RIGHTLEFT
1962 if (cmdmsg_rl)
1963 {
1964 msg_col -= cw;
1965 if (msg_col == 0)
1966 {
1967 msg_col = Columns;
1968 ++msg_row;
1969 }
1970 }
1971 else
1972#endif
1973 {
1974 msg_col += cw;
1975 if (msg_col >= Columns)
1976 {
1977 msg_col = 0;
1978 ++msg_row;
1979 }
1980 }
1981 return s + l;
1982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984/*
1985 * Output a string to the screen at position msg_row, msg_col.
1986 * Update msg_row and msg_col for the next message.
1987 */
1988 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001989msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001991 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992}
1993
1994 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001995msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001997 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998}
1999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000/*
2001 * Show a message in such a way that it always fits in the line. Cut out a
2002 * part in the middle and replace it with "..." when necessary.
2003 * Does not handle multi-byte characters!
2004 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002005 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002006msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 int slen = len;
2009 int room;
2010
2011 room = Columns - msg_col;
2012 if (len > room && room >= 20)
2013 {
2014 slen = (room - 3) / 2;
2015 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002016 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2019}
2020
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002021 void
2022msg_outtrans_long_attr(char_u *longstr, int attr)
2023{
2024 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2025}
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027/*
2028 * Basic function for writing a message with highlight attributes.
2029 */
2030 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002031msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
2033 msg_puts_attr_len(s, -1, attr);
2034}
2035
2036/*
2037 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2038 * When "maxlen" is -1 there is no maximum length.
2039 * When "maxlen" is >= 0 the message is not put in the history.
2040 */
2041 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002042msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 /*
2045 * If redirection is on, also write to the redirection file.
2046 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002047 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 /*
2050 * Don't print anything when using ":silent cmd".
2051 */
2052 if (msg_silent != 0)
2053 return;
2054
Bram Moolenaar85a20022019-12-21 18:25:54 +01002055 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if ((attr & MSG_HIST) && maxlen < 0)
2057 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002058 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 attr &= ~MSG_HIST;
2060 }
2061
Bram Moolenaare8070982019-10-12 17:07:06 +02002062 // When writing something to the screen after it has scrolled, requires a
2063 // wait-return prompt later. Needed when scrolling, resetting
2064 // need_wait_return after some prompt, and then outputting something
2065 // without scrolling
2066 // Not needed when only using CR to move the cursor.
2067 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002069 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
2071 /*
2072 * If there is no valid screen, use fprintf so we can see error messages.
2073 * If termcap is not active, we may be writing in an alternate console
2074 * window, cursor positioning may not work correctly (window size may be
2075 * different, e.g. for Win32 console) or we just don't know where the
2076 * cursor is.
2077 */
2078 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002079 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002080 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002081 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002084/*
2085 * The display part of msg_puts_attr_len().
2086 * May be called recursively to display scroll-back text.
2087 */
2088 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002089msg_puts_display(
2090 char_u *str,
2091 int maxlen,
2092 int attr,
2093 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002094{
2095 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002096 char_u *t_s = str; // string from "t_s" to "s" is still todo
2097 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002098 int l;
2099 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002100 char_u *sb_str = str;
2101 int sb_col = msg_col;
2102 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002103 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002106 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
2108 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002109 * We are at the end of the screen line when:
2110 * - When outputting a newline.
2111 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002113 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#ifdef FEAT_RIGHTLEFT
2115 cmdmsg_rl
2116 ? (
2117 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002118 || (*s == TAB && msg_col <= 7)
2119 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 :
2121#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002122 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002125 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002127 /*
2128 * The screen is scrolled up when at the last row (some terminals
2129 * scroll automatically, some don't. To avoid problems we scroll
2130 * ourselves).
2131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002133 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002134 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (msg_no_more && lines_left == 0)
2138 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
Bram Moolenaar85a20022019-12-21 18:25:54 +01002140 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002141 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 msg_col = Columns - 1;
2146
Bram Moolenaar85a20022019-12-21 18:25:54 +01002147 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148 if (*s >= ' '
2149#ifdef FEAT_RIGHTLEFT
2150 && !cmdmsg_rl
2151#endif
2152 )
2153 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002154 if (has_mbyte)
2155 {
2156 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002157 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002158 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002159 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002160 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002161 s = screen_puts_mbyte(s, l, attr);
2162 }
2163 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002164 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002165 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002166 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002167 else
2168 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002169
2170 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002171 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002172 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2173
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002174 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002175 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 redraw_cmdline = TRUE;
2177 if (cmdline_row > 0 && !exmode_active)
2178 --cmdline_row;
2179
2180 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002181 * If screen is completely filled and 'more' is set then wait
2182 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002184 if (lines_left > 0)
2185 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002186 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 && !msg_no_more && !exmode_active)
2188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002190 if (do_more_prompt(NUL))
2191 s = confirm_msg_tail;
2192#else
2193 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#endif
2195 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002196 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002198
Bram Moolenaar85a20022019-12-21 18:25:54 +01002199 // When we displayed a char in last column need to check if there
2200 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002201 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002202 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002205 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002208 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002209 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2210 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002212 t_puts(&t_col, t_s, s, attr);
2213
2214 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002215 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002216 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
Bram Moolenaar85a20022019-12-21 18:25:54 +01002218 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002221#ifdef FEAT_RIGHTLEFT
2222 if (cmdmsg_rl)
2223 msg_col = Columns - 1;
2224 else
2225#endif
2226 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 msg_row = Rows - 1;
2229 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 msg_col = 0;
2233 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002234 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 if (msg_col)
2237 --msg_col;
2238 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002239 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
2241 do
2242 msg_screen_putchar(' ', attr);
2243 while (msg_col & 7);
2244 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002245 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002246 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 else
2248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (has_mbyte)
2250 {
2251 cw = (*mb_ptr2cells)(s);
2252 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002254 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002256 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258 else
2259 {
2260 cw = 1;
2261 l = 1;
2262 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002263
Bram Moolenaar85a20022019-12-21 18:25:54 +01002264 // When drawing from right to left or when a double-wide character
2265 // doesn't fit, draw a single character here. Otherwise collect
2266 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (
2268# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002269 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002271 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (l > 1)
2274 s = screen_puts_mbyte(s, l, attr) - 1;
2275 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 msg_screen_putchar(*s, attr);
2277 }
2278 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002280 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (t_col == 0)
2282 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 t_col += cw;
2284 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286 }
2287 ++s;
2288 }
2289
Bram Moolenaar85a20022019-12-21 18:25:54 +01002290 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002292 t_puts(&t_col, t_s, s, attr);
2293 if (p_more && !recurse)
2294 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296 msg_check();
2297}
2298
2299/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002300 * Return TRUE when ":filter pattern" was used and "msg" does not match
2301 * "pattern".
2302 */
2303 int
2304message_filtered(char_u *msg)
2305{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002306 int match;
2307
2308 if (cmdmod.filter_regmatch.regprog == NULL)
2309 return FALSE;
2310 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2311 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002312}
2313
2314/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002315 * Scroll the screen up one line for displaying the next message line.
2316 */
2317 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002318msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002319{
2320#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002321 // Remove the cursor before scrolling, ScreenLines[] is going
2322 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002323 if (gui.in_use)
2324 gui_undraw_cursor();
2325#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002326 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002327 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002328 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002329 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002330
2331 if (!can_clear((char_u *)" "))
2332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 // Scrolling up doesn't result in the right background. Set the
2334 // background here. It's not efficient, but avoids that we have to do
2335 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002336 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2337
Bram Moolenaar85a20022019-12-21 18:25:54 +01002338 // Also clear the last char of the last but one line if it was not
2339 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002340 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2341 screen_fill((int)Rows - 2, (int)Rows - 1,
2342 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2343 }
2344}
2345
2346/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002347 * Increment "msg_scrolled".
2348 */
2349 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002350inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002351{
2352#ifdef FEAT_EVAL
2353 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2354 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002355 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002356 char_u *tofree = NULL;
2357 int len;
2358
Bram Moolenaar85a20022019-12-21 18:25:54 +01002359 // v:scrollstart is empty, set it to the script/function name and line
2360 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002361 if (p == NULL)
2362 p = (char_u *)_("Unknown");
2363 else
2364 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002365 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002366 tofree = alloc(len);
2367 if (tofree != NULL)
2368 {
2369 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002370 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002371 p = tofree;
2372 }
2373 }
2374 set_vim_var_string(VV_SCROLLSTART, p, -1);
2375 vim_free(tofree);
2376 }
2377#endif
2378 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002379 if (must_redraw < VALID)
2380 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002381}
2382
2383/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002384 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2385 * store the displayed text and remember where screen lines start.
2386 */
2387typedef struct msgchunk_S msgchunk_T;
2388struct msgchunk_S
2389{
2390 msgchunk_T *sb_next;
2391 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002392 char sb_eol; // TRUE when line ends after this text
2393 int sb_msg_col; // column in which text starts
2394 int sb_attr; // text attributes
2395 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002396};
2397
Bram Moolenaar85a20022019-12-21 18:25:54 +01002398static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002399
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002400static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002401
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002402typedef enum {
2403 SB_CLEAR_NONE = 0,
2404 SB_CLEAR_ALL,
2405 SB_CLEAR_CMDLINE_BUSY,
2406 SB_CLEAR_CMDLINE_DONE
2407} sb_clear_T;
2408
Bram Moolenaar85a20022019-12-21 18:25:54 +01002409// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002410static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002411
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002412/*
2413 * Store part of a printed message for displaying when scrolling back.
2414 */
2415 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002416store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002417 char_u **sb_str, // start of string
2418 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002419 int attr,
2420 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002421 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002422{
2423 msgchunk_T *mp;
2424
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002425 if (do_clear_sb_text == SB_CLEAR_ALL
2426 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002427 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002428 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2429 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002430 }
2431
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002432 if (s > *sb_str)
2433 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002434 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002435 if (mp != NULL)
2436 {
2437 mp->sb_eol = finish;
2438 mp->sb_msg_col = *sb_col;
2439 mp->sb_attr = attr;
2440 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2441
2442 if (last_msgchunk == NULL)
2443 {
2444 last_msgchunk = mp;
2445 mp->sb_prev = NULL;
2446 }
2447 else
2448 {
2449 mp->sb_prev = last_msgchunk;
2450 last_msgchunk->sb_next = mp;
2451 last_msgchunk = mp;
2452 }
2453 mp->sb_next = NULL;
2454 }
2455 }
2456 else if (finish && last_msgchunk != NULL)
2457 last_msgchunk->sb_eol = TRUE;
2458
2459 *sb_str = s;
2460 *sb_col = 0;
2461}
2462
2463/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002464 * Finished showing messages, clear the scroll-back text on the next message.
2465 */
2466 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002467may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002468{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002469 do_clear_sb_text = SB_CLEAR_ALL;
2470}
2471
2472/*
2473 * Starting to edit the command line, do not clear messages now.
2474 */
2475 void
2476sb_text_start_cmdline(void)
2477{
2478 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2479 msg_sb_eol();
2480}
2481
2482/*
2483 * Ending to edit the command line. Clear old lines but the last one later.
2484 */
2485 void
2486sb_text_end_cmdline(void)
2487{
2488 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002489}
2490
2491/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002492 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002493 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002494 * Called when redrawing the screen.
2495 */
2496 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002497clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002498{
2499 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002500 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002501
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002502 if (all)
2503 lastp = &last_msgchunk;
2504 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002506 if (last_msgchunk == NULL)
2507 return;
2508 lastp = &last_msgchunk->sb_prev;
2509 }
2510
2511 while (*lastp != NULL)
2512 {
2513 mp = (*lastp)->sb_prev;
2514 vim_free(*lastp);
2515 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002516 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002517}
2518
2519/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002520 * "g<" command.
2521 */
2522 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002523show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002524{
2525 msgchunk_T *mp;
2526
Bram Moolenaar85a20022019-12-21 18:25:54 +01002527 // Only show something if there is more than one line, otherwise it looks
2528 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002529 mp = msg_sb_start(last_msgchunk);
2530 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002531 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002532 else
2533 {
2534 do_more_prompt('G');
2535 wait_return(FALSE);
2536 }
2537}
2538
2539/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002540 * Move to the start of screen line in already displayed text.
2541 */
2542 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002543msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002544{
2545 msgchunk_T *mp = mps;
2546
2547 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2548 mp = mp->sb_prev;
2549 return mp;
2550}
2551
2552/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002553 * Mark the last message chunk as finishing the line.
2554 */
2555 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002556msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002557{
2558 if (last_msgchunk != NULL)
2559 last_msgchunk->sb_eol = TRUE;
2560}
2561
2562/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002563 * Display a screen line from previously displayed text at row "row".
2564 * Returns a pointer to the text for the next line (can be NULL).
2565 */
2566 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002567disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002568{
2569 msgchunk_T *mp = smp;
2570 char_u *p;
2571
2572 for (;;)
2573 {
2574 msg_row = row;
2575 msg_col = mp->sb_msg_col;
2576 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002577 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002578 ++p;
2579 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2580 if (mp->sb_eol || mp->sb_next == NULL)
2581 break;
2582 mp = mp->sb_next;
2583 }
2584 return mp->sb_next;
2585}
2586
2587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 * Output any postponed text for msg_puts_attr_len().
2589 */
2590 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002591t_puts(
2592 int *t_col,
2593 char_u *t_s,
2594 char_u *s,
2595 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002597 // output postponed text
2598 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002600 msg_col += *t_col;
2601 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002602 // If the string starts with a composing character don't increment the
2603 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2605 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (msg_col >= Columns)
2607 {
2608 msg_col = 0;
2609 ++msg_row;
2610 }
2611}
2612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613/*
2614 * Returns TRUE when messages should be printed with mch_errmsg().
2615 * This is used when there is no valid screen, so we can see error messages.
2616 * If termcap is not active, we may be writing in an alternate console
2617 * window, cursor positioning may not work correctly (window size may be
2618 * different, e.g. for Win32 console) or we just don't know where the
2619 * cursor is.
2620 */
2621 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002622msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002625#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2626# ifdef VIMDLL
2627 || (!gui.in_use && !termcap_active)
2628# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631#endif
2632 || (swapping_screen() && !termcap_active)
2633 );
2634}
2635
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002636/*
2637 * Print a message when there is no valid screen.
2638 */
2639 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002640msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002641{
2642 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002643 char_u *buf = NULL;
2644 char_u *p = s;
2645
Bram Moolenaar4f974752019-02-17 17:44:42 +01002646#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002647 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002648 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002649#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002650 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002651 {
2652 if (!(silent_mode && p_verbose == 0))
2653 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002654 // NL --> CR NL translation (for Unix, not for "--version")
2655 if (*s == NL)
2656 {
2657 int n = (int)(s - p);
2658
2659 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002660 if (buf != NULL)
2661 {
2662 memcpy(buf, p, n);
2663 if (!info_message)
2664 buf[n++] = CAR;
2665 buf[n++] = NL;
2666 buf[n++] = NUL;
2667 if (info_message) // informative message, not an error
2668 mch_msg((char *)buf);
2669 else
2670 mch_errmsg((char *)buf);
2671 vim_free(buf);
2672 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002673 p = s + 1;
2674 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002675 }
2676
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002677 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002678#ifdef FEAT_RIGHTLEFT
2679 if (cmdmsg_rl)
2680 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002681 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002682 msg_col = Columns - 1;
2683 else
2684 --msg_col;
2685 }
2686 else
2687#endif
2688 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002689 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690 msg_col = 0;
2691 else
2692 ++msg_col;
2693 }
2694 ++s;
2695 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002696
2697 if (*p != NUL && !(silent_mode && p_verbose == 0))
2698 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002699 int c = -1;
2700
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002701 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002702 {
2703 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002704 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002705 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002706 if (info_message)
2707 mch_msg((char *)p);
2708 else
2709 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002710 if (c != -1)
2711 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002712 }
2713
2714 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002715
Bram Moolenaar4f974752019-02-17 17:44:42 +01002716#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002717 if (!(silent_mode && p_verbose == 0))
2718 mch_settmode(TMODE_RAW);
2719#endif
2720}
2721
2722/*
2723 * Show the more-prompt and handle the user response.
2724 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002725 * When at hit-enter prompt "typed_char" is the already typed character,
2726 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2728 */
2729 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002730do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002731{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002732 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733 int used_typed_char = typed_char;
2734 int oldState = State;
2735 int c;
2736#ifdef FEAT_CON_DIALOG
2737 int retval = FALSE;
2738#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002739 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 msgchunk_T *mp_last = NULL;
2741 msgchunk_T *mp;
2742 int i;
2743
Bram Moolenaar85a20022019-12-21 18:25:54 +01002744 // We get called recursively when a timer callback outputs a message. In
2745 // that case don't show another prompt. Also when at the hit-Enter prompt
2746 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002747 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002748 return FALSE;
2749 entered = TRUE;
2750
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002751 if (typed_char == 'G')
2752 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002753 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002754 mp_last = msg_sb_start(last_msgchunk);
2755 for (i = 0; i < Rows - 2 && mp_last != NULL
2756 && mp_last->sb_prev != NULL; ++i)
2757 mp_last = msg_sb_start(mp_last->sb_prev);
2758 }
2759
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002760 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002761 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002762 if (typed_char == NUL)
2763 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764 for (;;)
2765 {
2766 /*
2767 * Get a typed character directly from the user.
2768 */
2769 if (used_typed_char != NUL)
2770 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002771 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002772 used_typed_char = NUL;
2773 }
2774 else
2775 c = get_keystroke();
2776
2777#if defined(FEAT_MENU) && defined(FEAT_GUI)
2778 if (c == K_MENU)
2779 {
2780 int idx = get_menu_index(current_menu, ASKMORE);
2781
Bram Moolenaar85a20022019-12-21 18:25:54 +01002782 // Used a menu. If it starts with CTRL-Y, it must
2783 // be a "Copy" for the clipboard. Otherwise
2784 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 if (idx == MENU_INDEX_INVALID)
2786 continue;
2787 c = *current_menu->strings[idx];
2788 if (c != NUL && current_menu->strings[idx][1] != NUL)
2789 ins_typebuf(current_menu->strings[idx] + 1,
2790 current_menu->noremap[idx], 0, TRUE,
2791 current_menu->silent[idx]);
2792 }
2793#endif
2794
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002795 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 switch (c)
2797 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002798 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 case K_BS:
2800 case 'k':
2801 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002802 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002803 break;
2804
Bram Moolenaar85a20022019-12-21 18:25:54 +01002805 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002806 case NL:
2807 case 'j':
2808 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002809 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002810 break;
2811
Bram Moolenaar85a20022019-12-21 18:25:54 +01002812 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002813 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002814 break;
2815
Bram Moolenaar85a20022019-12-21 18:25:54 +01002816 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002817 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002818 break;
2819
Bram Moolenaar85a20022019-12-21 18:25:54 +01002820 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002821 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002822 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002823 break;
2824
Bram Moolenaar85a20022019-12-21 18:25:54 +01002825 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002826 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 case K_PAGEDOWN:
2828 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002829 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002830 break;
2831
Bram Moolenaar85a20022019-12-21 18:25:54 +01002832 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002833 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002834 break;
2835
Bram Moolenaar85a20022019-12-21 18:25:54 +01002836 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002837 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002838 lines_left = 999999;
2839 break;
2840
Bram Moolenaar85a20022019-12-21 18:25:54 +01002841 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842#ifdef FEAT_CON_DIALOG
2843 if (!confirm_msg_used)
2844#endif
2845 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002846 // Since got_int is set all typeahead will be flushed, but we
2847 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002848 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002849#ifdef FEAT_TERMINAL
2850 skip_term_loop = TRUE;
2851#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002852 cmdline_row = Rows - 1; // put ':' on this line
2853 skip_redraw = TRUE; // skip redraw once
2854 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002855 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002856 // FALLTHROUGH
2857 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 case Ctrl_C:
2859 case ESC:
2860#ifdef FEAT_CON_DIALOG
2861 if (confirm_msg_used)
2862 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002863 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002864 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002865 }
2866 else
2867#endif
2868 {
2869 got_int = TRUE;
2870 quit_more = TRUE;
2871 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002872 // When there is some more output (wrapping line) display that
2873 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002874 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002875 break;
2876
2877#ifdef FEAT_CLIPBOARD
2878 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002879 // Strange way to allow copying (yanking) a modeless
2880 // selection at the more prompt. Use CTRL-Y,
2881 // because the same is used in Cmdline-mode and at the
2882 // hit-enter prompt. However, scrolling one line up
2883 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002884 if (clip_star.state == SELECT_DONE)
2885 clip_copy_modeless_selection(TRUE);
2886 continue;
2887#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002888 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002889 msg_moremsg(TRUE);
2890 continue;
2891 }
2892
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002893 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002894 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002895 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002897 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002898 if (mp_last == NULL)
2899 mp = msg_sb_start(last_msgchunk);
2900 else if (mp_last->sb_prev != NULL)
2901 mp = msg_sb_start(mp_last->sb_prev);
2902 else
2903 mp = NULL;
2904
Bram Moolenaar85a20022019-12-21 18:25:54 +01002905 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002906 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2907 ++i)
2908 mp = msg_sb_start(mp->sb_prev);
2909
2910 if (mp != NULL && mp->sb_prev != NULL)
2911 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002912 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002913 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002914 {
2915 if (mp == NULL || mp->sb_prev == NULL)
2916 break;
2917 mp = msg_sb_start(mp->sb_prev);
2918 if (mp_last == NULL)
2919 mp_last = msg_sb_start(last_msgchunk);
2920 else
2921 mp_last = msg_sb_start(mp_last->sb_prev);
2922 }
2923
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002924 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002925 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002926 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002927 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928 (void)disp_sb_line(0, mp);
2929 }
2930 else
2931 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002932 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002933 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002934 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2935 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002937 ++msg_scrolled;
2938 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002939 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002940 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002941 }
2942 }
2943 else
2944 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002945 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002946 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002947 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002948 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002949 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002950 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002951 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2952 (int)Columns, ' ', ' ', 0);
2953 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002954 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002955 }
2956 }
2957
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002958 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002959 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002961 screen_fill((int)Rows - 1, (int)Rows, 0,
2962 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002963 msg_moremsg(FALSE);
2964 continue;
2965 }
2966
Bram Moolenaar85a20022019-12-21 18:25:54 +01002967 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002968 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002969 }
2970
2971 break;
2972 }
2973
Bram Moolenaar85a20022019-12-21 18:25:54 +01002974 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002975 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2976 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002977 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978 if (quit_more)
2979 {
2980 msg_row = Rows - 1;
2981 msg_col = 0;
2982 }
2983#ifdef FEAT_RIGHTLEFT
2984 else if (cmdmsg_rl)
2985 msg_col = Columns - 1;
2986#endif
2987
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002988 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002989#ifdef FEAT_CON_DIALOG
2990 return retval;
2991#else
2992 return FALSE;
2993#endif
2994}
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2997
2998#ifdef mch_errmsg
2999# undef mch_errmsg
3000#endif
3001#ifdef mch_msg
3002# undef mch_msg
3003#endif
3004
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003005#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3006 static void
3007mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003009 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003010 DWORD nwrite = 0;
3011 DWORD mode = 0;
3012 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3013
3014 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3015 && (int)GetConsoleCP() != enc_codepage)
3016 {
3017 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3018
3019 WriteConsoleW(h, w, len, &nwrite, NULL);
3020 vim_free(w);
3021 }
3022 else
3023 {
3024 fprintf(stderr, "%s", str);
3025 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003026}
3027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003029/*
3030 * Give an error message. To be used when the screen hasn't been initialized
3031 * yet. When stderr can't be used, collect error messages until the GUI has
3032 * started and they can be displayed in a message box.
3033 */
3034 void
3035mch_errmsg(char *str)
3036{
3037#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3038 int len;
3039#endif
3040
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003041#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003042 // On Unix use stderr if it's a tty.
3043 // When not going to start the GUI also use stderr.
3044 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003046# ifdef UNIX
3047# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003049# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 isatty(2)
3051# endif
3052# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003053 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003054# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003055# endif
3056# ifdef FEAT_GUI
3057 !(gui.in_use || gui.starting)
3058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 )
3060 {
3061 fprintf(stderr, "%s", str);
3062 return;
3063 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003066#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3067# ifdef VIMDLL
3068 if (!(gui.in_use || gui.starting))
3069# endif
3070 {
3071 mch_errmsg_c(str);
3072 return;
3073 }
3074#endif
3075
3076#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003077 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 emsg_on_display = FALSE;
3079
3080 len = (int)STRLEN(str) + 1;
3081 if (error_ga.ga_growsize == 0)
3082 {
3083 error_ga.ga_growsize = 80;
3084 error_ga.ga_itemsize = 1;
3085 }
3086 if (ga_grow(&error_ga, len) == OK)
3087 {
3088 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3089 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003090# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003091 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 char_u *p;
3094
3095 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3096 for (;;)
3097 {
3098 p = vim_strchr(p, '\r');
3099 if (p == NULL)
3100 break;
3101 *p = ' ';
3102 }
3103 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003104# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003105 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109}
3110
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003111#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3112 static void
3113mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003115 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003116 DWORD nwrite = 0;
3117 DWORD mode;
3118 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3119
3120
3121 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3122 && (int)GetConsoleCP() != enc_codepage)
3123 {
3124 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3125
3126 WriteConsoleW(h, w, len, &nwrite, NULL);
3127 vim_free(w);
3128 }
3129 else
3130 {
3131 printf("%s", str);
3132 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003133}
3134#endif
3135
3136/*
3137 * Give a message. To be used when the screen hasn't been initialized yet.
3138 * When there is no tty, collect messages until the GUI has started and they
3139 * can be displayed in a message box.
3140 */
3141 void
3142mch_msg(char *str)
3143{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003144#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003145 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3146 // uses mch_errmsg() when started from the desktop.
3147 // When not going to start the GUI also use stdout.
3148 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003150# ifdef UNIX
3151# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003153# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003157 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003159# endif
3160# ifdef FEAT_GUI
3161 !(gui.in_use || gui.starting)
3162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 )
3164 {
3165 printf("%s", str);
3166 return;
3167 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003168#endif
3169
3170#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3171# ifdef VIMDLL
3172 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003174 {
3175 mch_msg_c(str);
3176 return;
3177 }
3178#endif
3179#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003183#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185/*
3186 * Put a character on the screen at the current message position and advance
3187 * to the next position. Only for printable ASCII!
3188 */
3189 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003190msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003192 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 screen_putchar(c, msg_row, msg_col, attr);
3194#ifdef FEAT_RIGHTLEFT
3195 if (cmdmsg_rl)
3196 {
3197 if (--msg_col == 0)
3198 {
3199 msg_col = Columns;
3200 ++msg_row;
3201 }
3202 }
3203 else
3204#endif
3205 {
3206 if (++msg_col >= Columns)
3207 {
3208 msg_col = 0;
3209 ++msg_row;
3210 }
3211 }
3212}
3213
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003214 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003215msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003217 int attr;
3218 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar8820b482017-03-16 17:23:31 +01003220 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003221 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003223 screen_puts((char_u *)
3224 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3225 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226}
3227
3228/*
3229 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3230 * exmode_active.
3231 */
3232 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 if (State == ASKMORE)
3236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003237 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 msg_row = Rows - 1;
3239 }
3240#ifdef FEAT_CON_DIALOG
3241 else if (State == CONFIRM)
3242 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003243 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 msg_row = Rows - 1;
3245 }
3246#endif
3247 else if (State == EXTERNCMD)
3248 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003249 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251 else if (State == HITRETURN || State == SETWSIZE)
3252 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003253 if (msg_row == Rows - 1)
3254 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003255 // Avoid drawing the "hit-enter" prompt below the previous one,
3256 // overwrite it. Esp. useful when regaining focus and a
3257 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003258 msg_didout = FALSE;
3259 msg_col = 0;
3260 msg_clr_eos();
3261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 hit_return_msg();
3263 msg_row = Rows - 1;
3264 }
3265}
3266
3267/*
3268 * msg_check_screen - check if the screen is initialized.
3269 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3270 * While starting the GUI the terminal codes will be set for the GUI, but the
3271 * output goes to the terminal. Don't use the terminal codes then.
3272 */
3273 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003274msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 if (!full_screen || !screen_valid(FALSE))
3277 return FALSE;
3278
3279 if (msg_row >= Rows)
3280 msg_row = Rows - 1;
3281 if (msg_col >= Columns)
3282 msg_col = Columns - 1;
3283 return TRUE;
3284}
3285
3286/*
3287 * Clear from current message position to end of screen.
3288 * Skip this when ":silent" was used, no need to clear for redirection.
3289 */
3290 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003291msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 if (msg_silent == 0)
3294 msg_clr_eos_force();
3295}
3296
3297/*
3298 * Clear from current message position to end of screen.
3299 * Note: msg_col is not updated, so we remember the end of the message
3300 * for msg_check().
3301 */
3302 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003303msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 if (msg_use_printf())
3306 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003307 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003310 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003312 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 }
3315 else
3316 {
3317#ifdef FEAT_RIGHTLEFT
3318 if (cmdmsg_rl)
3319 {
3320 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3321 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3322 }
3323 else
3324#endif
3325 {
3326 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3327 ' ', ' ', 0);
3328 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3329 }
3330 }
3331}
3332
3333/*
3334 * Clear the command line.
3335 */
3336 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003337msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 msg_row = cmdline_row;
3340 msg_col = 0;
3341 msg_clr_eos_force();
3342}
3343
3344/*
3345 * end putting a message on the screen
3346 * call wait_return if the message does not fit in the available space
3347 * return TRUE if wait_return not called.
3348 */
3349 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003350msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003353 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 * or the ruler option is set and we run into it,
3355 * we have to redraw the window.
3356 * Do not do this if we are abandoning the file or editing the command line.
3357 */
3358 if (!exiting && need_wait_return && !(State & CMDLINE))
3359 {
3360 wait_return(FALSE);
3361 return FALSE;
3362 }
3363 out_flush();
3364 return TRUE;
3365}
3366
3367/*
3368 * If the written message runs into the shown command or ruler, we have to
3369 * wait for hit-return and redraw the window later.
3370 */
3371 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003372msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 if (msg_row == Rows - 1 && msg_col >= sc_col)
3375 {
3376 need_wait_return = TRUE;
3377 redraw_cmdline = TRUE;
3378 }
3379}
3380
3381/*
3382 * May write a string to the redirection file.
3383 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3384 */
3385 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003386redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 char_u *s = str;
3389 static int cur_col = 0;
3390
Bram Moolenaar85a20022019-12-21 18:25:54 +01003391 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003392 if (redir_off)
3393 return;
3394
Bram Moolenaar85a20022019-12-21 18:25:54 +01003395 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003396 if (*p_vfile != NUL && verbose_fd == NULL)
3397 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003398
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003399 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003401 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (*s != '\n' && *s != '\r')
3403 {
3404 while (cur_col < msg_col)
3405 {
3406#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003407 if (redir_execute)
3408 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003409 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003411 else if (redir_vname)
3412 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003415 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003417 if (verbose_fd != NULL)
3418 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 ++cur_col;
3420 }
3421 }
3422
3423#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003424 if (redir_execute)
3425 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003426 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003428 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003429 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430#endif
3431
Bram Moolenaar85a20022019-12-21 18:25:54 +01003432 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3434 {
3435#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003436 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003438 if (redir_fd != NULL)
3439 putc(*s, redir_fd);
3440 if (verbose_fd != NULL)
3441 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (*s == '\r' || *s == '\n')
3443 cur_col = 0;
3444 else if (*s == '\t')
3445 cur_col += (8 - cur_col % 8);
3446 else
3447 ++cur_col;
3448 ++s;
3449 }
3450
Bram Moolenaar85a20022019-12-21 18:25:54 +01003451 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 msg_col = cur_col;
3453 }
3454}
3455
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003456 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003457redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003458{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003459 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003460#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003461 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003462#endif
3463 ;
3464}
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003467 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003468 * Must always be called paired with verbose_leave()!
3469 */
3470 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003471verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003472{
3473 if (*p_vfile != NUL)
3474 ++msg_silent;
3475}
3476
3477/*
3478 * After giving verbose message.
3479 * Must always be called paired with verbose_enter()!
3480 */
3481 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003482verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003483{
3484 if (*p_vfile != NUL)
3485 if (--msg_silent < 0)
3486 msg_silent = 0;
3487}
3488
3489/*
3490 * Like verbose_enter() and set msg_scroll when displaying the message.
3491 */
3492 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003493verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003494{
3495 if (*p_vfile != NUL)
3496 ++msg_silent;
3497 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003498 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003499 msg_scroll = TRUE;
3500}
3501
3502/*
3503 * Like verbose_leave() and set cmdline_row when displaying the message.
3504 */
3505 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003506verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003507{
3508 if (*p_vfile != NUL)
3509 {
3510 if (--msg_silent < 0)
3511 msg_silent = 0;
3512 }
3513 else
3514 cmdline_row = msg_row;
3515}
3516
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003517/*
3518 * Called when 'verbosefile' is set: stop writing to the file.
3519 */
3520 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003521verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003522{
3523 if (verbose_fd != NULL)
3524 {
3525 fclose(verbose_fd);
3526 verbose_fd = NULL;
3527 }
3528 verbose_did_open = FALSE;
3529}
3530
3531/*
3532 * Open the file 'verbosefile'.
3533 * Return FAIL or OK.
3534 */
3535 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003536verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003537{
3538 if (verbose_fd == NULL && !verbose_did_open)
3539 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003540 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003541 verbose_did_open = TRUE;
3542
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003543 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003544 if (verbose_fd == NULL)
3545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003546 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003547 return FAIL;
3548 }
3549 }
3550 return OK;
3551}
3552
3553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 * Give a warning message (for searching).
3555 * Use 'w' highlighting and may repeat the message after redrawing
3556 */
3557 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003558give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003560 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (msg_silent != 0)
3562 return;
3563
Bram Moolenaar85a20022019-12-21 18:25:54 +01003564 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#ifdef FEAT_EVAL
3568 set_vim_var_string(VV_WARNINGMSG, message, -1);
3569#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003570 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003572 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 else
3574 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003575 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003576 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003577 msg_didout = FALSE; // overwrite this message
3578 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 --no_wait_return;
3582}
3583
Bram Moolenaar113e1072019-01-20 15:30:40 +01003584#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003585 void
3586give_warning2(char_u *message, char_u *a1, int hl)
3587{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003588 if (IObuff == NULL)
3589 {
3590 // Very early in initialisation and already something wrong, just give
3591 // the raw message so the user at least gets a hint.
3592 give_warning((char_u *)message, hl);
3593 }
3594 else
3595 {
3596 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3597 give_warning(IObuff, hl);
3598 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003599}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003600#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602/*
3603 * Advance msg cursor to column "col".
3604 */
3605 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003606msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003608 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003610 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return;
3612 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003613 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003615#ifdef FEAT_RIGHTLEFT
3616 if (cmdmsg_rl)
3617 while (msg_col > Columns - col)
3618 msg_putchar(' ');
3619 else
3620#endif
3621 while (msg_col < col)
3622 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623}
3624
3625#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3626/*
3627 * Used for "confirm()" function, and the :confirm command prefix.
3628 * Versions which haven't got flexible dialogs yet, and console
3629 * versions, get this generic handler which uses the command line.
3630 *
3631 * type = one of:
3632 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3633 * title = title string (can be NULL for default)
3634 * (neither used in console dialogs at the moment)
3635 *
3636 * Format of the "buttons" string:
3637 * "Button1Name\nButton2Name\nButton3Name"
3638 * The first button should normally be the default/accept
3639 * The second button should be the 'Cancel' button
3640 * Other buttons- use your imagination!
3641 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3642 * different letter.
3643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003645do_dialog(
3646 int type UNUSED,
3647 char_u *title UNUSED,
3648 char_u *message,
3649 char_u *buttons,
3650 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003651 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3652 // otherwise
3653 int ex_cmd) // when TRUE pressing : accepts default and starts
3654 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 int oldState;
3657 int retval = 0;
3658 char_u *hotkeys;
3659 int c;
3660 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003661 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003664 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003666 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667#endif
3668
3669#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003670 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3672 {
3673 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003674 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003675 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003676 need_wait_return = FALSE;
3677 emsg_on_display = FALSE;
3678 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaar85a20022019-12-21 18:25:54 +01003680 // Flush output to avoid that further messages and redrawing is done
3681 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 out_flush();
3683 gui_mch_update();
3684
3685 return c;
3686 }
3687#endif
3688
3689 oldState = State;
3690 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
Bram Moolenaar27321db2020-07-06 21:24:57 +02003693 // Ensure raw mode here.
3694 save_tmode = cur_tmode;
3695 settmode(TMODE_RAW);
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 /*
3698 * Since we wait for a keypress, don't make the
3699 * user press RETURN as well afterwards.
3700 */
3701 ++no_wait_return;
3702 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3703
3704 if (hotkeys != NULL)
3705 {
3706 for (;;)
3707 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003708 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 c = get_keystroke();
3710 switch (c)
3711 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003712 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 case NL:
3714 retval = dfltbutton;
3715 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003716 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 case ESC:
3718 retval = 0;
3719 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003720 default: // Could be a hotkey?
3721 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003723 if (c == ':' && ex_cmd)
3724 {
3725 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003726 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003727 break;
3728 }
3729
Bram Moolenaar85a20022019-12-21 18:25:54 +01003730 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 c = MB_TOLOWER(c);
3732 retval = 1;
3733 for (i = 0; hotkeys[i]; ++i)
3734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if (has_mbyte)
3736 {
3737 if ((*mb_ptr2char)(hotkeys + i) == c)
3738 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003739 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (hotkeys[i] == c)
3743 break;
3744 ++retval;
3745 }
3746 if (hotkeys[i])
3747 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003748 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 continue;
3750 }
3751 break;
3752 }
3753
3754 vim_free(hotkeys);
3755 }
3756
Bram Moolenaar27321db2020-07-06 21:24:57 +02003757 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 --no_wait_return;
3761 msg_end_prompt();
3762
3763 return retval;
3764}
3765
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766/*
3767 * Copy one character from "*from" to "*to", taking care of multi-byte
3768 * characters. Return the length of the character in bytes.
3769 */
3770 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003771copy_char(
3772 char_u *from,
3773 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003774 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 int len;
3777 int c;
3778
3779 if (has_mbyte)
3780 {
3781 if (lowercase)
3782 {
3783 c = MB_TOLOWER((*mb_ptr2char)(from));
3784 return (*mb_char2bytes)(c, to);
3785 }
3786 else
3787 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003788 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 mch_memmove(to, from, (size_t)len);
3790 return len;
3791 }
3792 }
3793 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
3795 if (lowercase)
3796 *to = (char_u)TOLOWER_LOC(*from);
3797 else
3798 *to = *from;
3799 return 1;
3800 }
3801}
3802
3803/*
3804 * Format the dialog string, and display it at the bottom of
3805 * the screen. Return a string of hotkey chars (if defined) for
3806 * each 'button'. If a button has no hotkey defined, the first character of
3807 * the button is used.
3808 * The hotkeys can be multi-byte characters, but without combining chars.
3809 *
3810 * Returns an allocated string with hotkeys, or NULL for error.
3811 */
3812 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003813msg_show_console_dialog(
3814 char_u *message,
3815 char_u *buttons,
3816 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
3818 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003819#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003820 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 char_u *hotk = NULL;
3822 char_u *msgp = NULL;
3823 char_u *hotkp = NULL;
3824 char_u *r;
3825 int copy;
3826#define HAS_HOTKEY_LEN 30
3827 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003828 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int idx;
3830
3831 has_hotkey[0] = FALSE;
3832
3833 /*
3834 * First loop: compute the size of memory to allocate.
3835 * Second loop: copy to the allocated memory.
3836 */
3837 for (copy = 0; copy <= 1; ++copy)
3838 {
3839 r = buttons;
3840 idx = 0;
3841 while (*r)
3842 {
3843 if (*r == DLG_BUTTON_SEP)
3844 {
3845 if (copy)
3846 {
3847 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003848 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
Bram Moolenaar85a20022019-12-21 18:25:54 +01003850 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003852 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003855 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (dfltbutton)
3857 --dfltbutton;
3858
Bram Moolenaar85a20022019-12-21 18:25:54 +01003859 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3861 first_hotkey = TRUE;
3862 }
3863 else
3864 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003865 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3866 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (idx < HAS_HOTKEY_LEN - 1)
3868 has_hotkey[++idx] = FALSE;
3869 }
3870 }
3871 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3872 {
3873 if (*r == DLG_HOTKEY_CHAR)
3874 ++r;
3875 first_hotkey = FALSE;
3876 if (copy)
3877 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003878 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 *msgp++ = *r;
3880 else
3881 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003882 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3884 msgp += copy_char(r, msgp, FALSE);
3885 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3886
Bram Moolenaar85a20022019-12-21 18:25:54 +01003887 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003888 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890 }
3891 else
3892 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003893 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (idx < HAS_HOTKEY_LEN - 1)
3895 has_hotkey[idx] = TRUE;
3896 }
3897 }
3898 else
3899 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003900 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (copy)
3902 msgp += copy_char(r, msgp, FALSE);
3903 }
3904
Bram Moolenaar85a20022019-12-21 18:25:54 +01003905 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003906 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908
3909 if (copy)
3910 {
3911 *msgp++ = ':';
3912 *msgp++ = ' ';
3913 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915 else
3916 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003917 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003918 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003919 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003920 + 3); // for the ": " and NUL
3921 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar85a20022019-12-21 18:25:54 +01003923 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (!has_hotkey[0])
3925 {
3926 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003927 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929
3930 /*
3931 * Now allocate and load the strings
3932 */
3933 vim_free(confirm_msg);
3934 confirm_msg = alloc(len);
3935 if (confirm_msg == NULL)
3936 return NULL;
3937 *confirm_msg = NUL;
3938 hotk = alloc(lenhotkey);
3939 if (hotk == NULL)
3940 return NULL;
3941
3942 *confirm_msg = '\n';
3943 STRCPY(confirm_msg + 1, message);
3944
3945 msgp = confirm_msg + 1 + STRLEN(message);
3946 hotkp = hotk;
3947
Bram Moolenaar85a20022019-12-21 18:25:54 +01003948 // Define first default hotkey. Keep the hotkey string NUL
3949 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003950 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
Bram Moolenaar85a20022019-12-21 18:25:54 +01003952 // Remember where the choices start, displaying starts here when
3953 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 confirm_msg_tail = msgp;
3955 *msgp++ = '\n';
3956 }
3957 }
3958
3959 display_confirm_msg();
3960 return hotk;
3961}
3962
3963/*
3964 * Display the ":confirm" message. Also called when screen resized.
3965 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003966 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003967display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003969 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 ++confirm_msg_used;
3971 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003972 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 --confirm_msg_used;
3974}
3975
Bram Moolenaar85a20022019-12-21 18:25:54 +01003976#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3979
3980 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003981vim_dialog_yesno(
3982 int type,
3983 char_u *title,
3984 char_u *message,
3985 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 if (do_dialog(type,
3988 title == NULL ? (char_u *)_("Question") : title,
3989 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003990 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 return VIM_YES;
3992 return VIM_NO;
3993}
3994
3995 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003996vim_dialog_yesnocancel(
3997 int type,
3998 char_u *title,
3999 char_u *message,
4000 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
4002 switch (do_dialog(type,
4003 title == NULL ? (char_u *)_("Question") : title,
4004 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004005 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
4007 case 1: return VIM_YES;
4008 case 2: return VIM_NO;
4009 }
4010 return VIM_CANCEL;
4011}
4012
4013 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004014vim_dialog_yesnoallcancel(
4015 int type,
4016 char_u *title,
4017 char_u *message,
4018 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
4020 switch (do_dialog(type,
4021 title == NULL ? (char_u *)"Question" : title,
4022 message,
4023 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004024 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026 case 1: return VIM_YES;
4027 case 2: return VIM_NO;
4028 case 3: return VIM_ALL;
4029 case 4: return VIM_DISCARDALL;
4030 }
4031 return VIM_CANCEL;
4032}
4033
Bram Moolenaar85a20022019-12-21 18:25:54 +01004034#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004036#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004037static char *e_printf = N_("E766: Insufficient arguments for printf()");
4038
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039/*
4040 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4041 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004042 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004043tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044{
4045 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004046 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 int err = FALSE;
4048
4049 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004050 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004051 else
4052 {
4053 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004054 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055 if (err)
4056 n = 0;
4057 }
4058 return n;
4059}
4060
4061/*
4062 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004063 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004064 * are not converted to a string.
4065 * If "tofree" is not NULL echo_string() is used. All types are converted to
4066 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004067 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004068 */
4069 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004070tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004071{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004072 int idx = *idxp - 1;
4073 char *s = NULL;
4074 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075
4076 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004077 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 else
4079 {
4080 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004081 if (tofree != NULL)
4082 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4083 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004084 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085 }
4086 return s;
4087}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004088
4089# ifdef FEAT_FLOAT
4090/*
4091 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4092 */
4093 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004094tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004095{
4096 int idx = *idxp - 1;
4097 double f = 0;
4098
4099 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004100 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004101 else
4102 {
4103 ++*idxp;
4104 if (tvs[idx].v_type == VAR_FLOAT)
4105 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004106 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004107 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004108 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004109 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004110 }
4111 return f;
4112}
4113# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004114#endif
4115
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004116#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004117/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004118 * Return the representation of infinity for printf() function:
4119 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4120 */
4121 static const char *
4122infinity_str(int positive,
4123 char fmt_spec,
4124 int force_sign,
4125 int space_for_positive)
4126{
4127 static const char *table[] =
4128 {
4129 "-inf", "inf", "+inf", " inf",
4130 "-INF", "INF", "+INF", " INF"
4131 };
4132 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4133
4134 if (ASCII_ISUPPER(fmt_spec))
4135 idx += 4;
4136 return table[idx];
4137}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004138#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004139
4140/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004141 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004142 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004143 * consistency.
4144 *
4145 * This code is based on snprintf.c - a portable implementation of snprintf
4146 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004147 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004148 * The original code, including useful comments, can be found here:
4149 * http://www.ijs.si/software/snprintf/
4150 *
4151 * This snprintf() only supports the following conversion specifiers:
4152 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4153 * with flags: '-', '+', ' ', '0' and '#'.
4154 * An asterisk is supported for field width as well as precision.
4155 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004156 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004157 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004158 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004159 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004160 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004161 * The locale is not used, the string is used as a byte string. This is only
4162 * relevant for double-byte encodings where the second byte may be '%'.
4163 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004164 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4165 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004166 *
4167 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004168 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004169 * is greater or equal to "str_m", not all characters from the result
4170 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4171 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004172 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004173 */
4174
4175/*
4176 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004178 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004179 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004180 */
4181
Bram Moolenaar85a20022019-12-21 18:25:54 +01004182// When generating prototypes all of this is skipped, cproto doesn't
4183// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004185
Bram Moolenaar85a20022019-12-21 18:25:54 +01004186// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004187 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004189{
4190 va_list ap;
4191 int str_l;
4192 size_t len = STRLEN(str);
4193 size_t space;
4194
4195 if (str_m <= len)
4196 space = 0;
4197 else
4198 space = str_m - len;
4199 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004200 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004201 va_end(ap);
4202 return str_l;
4203}
Bram Moolenaara800b422010-06-27 01:15:55 +02004204
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004205 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004206vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207{
4208 va_list ap;
4209 int str_l;
4210
4211 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004212 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004213 va_end(ap);
4214 return str_l;
4215}
4216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004218vim_vsnprintf(
4219 char *str,
4220 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004221 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004222 va_list ap)
4223{
4224 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4225}
4226
4227 int
4228vim_vsnprintf_typval(
4229 char *str,
4230 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004231 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004232 va_list ap,
4233 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004234{
4235 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004236 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004237 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004238
4239 if (p == NULL)
4240 p = "";
4241 while (*p != NUL)
4242 {
4243 if (*p != '%')
4244 {
4245 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004246 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004247
Bram Moolenaar85a20022019-12-21 18:25:54 +01004248 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004249 if (str_l < str_m)
4250 {
4251 size_t avail = str_m - str_l;
4252
4253 mch_memmove(str + str_l, p, n > avail ? avail : n);
4254 }
4255 p += n;
4256 str_l += n;
4257 }
4258 else
4259 {
4260 size_t min_field_width = 0, precision = 0;
4261 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4262 int alternate_form = 0, force_sign = 0;
4263
Bram Moolenaar85a20022019-12-21 18:25:54 +01004264 // If both the ' ' and '+' flags appear, the ' ' flag should be
4265 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004266 int space_for_positive = 1;
4267
Bram Moolenaar85a20022019-12-21 18:25:54 +01004268 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004269 char length_modifier = '\0';
4270
Bram Moolenaar85a20022019-12-21 18:25:54 +01004271 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004272# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004273# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4274 // That sounds reasonable to use as the maximum
4275 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004276# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004277# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004278# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004279 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004280
Bram Moolenaar85a20022019-12-21 18:25:54 +01004281 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004282 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283
Bram Moolenaar85a20022019-12-21 18:25:54 +01004284 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004285 size_t str_arg_l;
4286
Bram Moolenaar85a20022019-12-21 18:25:54 +01004287 // unsigned char argument value - only defined for c conversion.
4288 // N.B. standard explicitly states the char argument for the c
4289 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004290 unsigned char uchar_arg;
4291
Bram Moolenaar85a20022019-12-21 18:25:54 +01004292 // number of zeros to be inserted for numeric conversions as
4293 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004294 size_t number_of_zeros_to_pad = 0;
4295
Bram Moolenaar85a20022019-12-21 18:25:54 +01004296 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004297 size_t zero_padding_insertion_ind = 0;
4298
Bram Moolenaar85a20022019-12-21 18:25:54 +01004299 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004300 char fmt_spec = '\0';
4301
Bram Moolenaar85a20022019-12-21 18:25:54 +01004302 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004303 char_u *tofree = NULL;
4304
4305
Bram Moolenaar85a20022019-12-21 18:25:54 +01004306 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004307
Bram Moolenaar85a20022019-12-21 18:25:54 +01004308 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004309 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4310 || *p == '#' || *p == '\'')
4311 {
4312 switch (*p)
4313 {
4314 case '0': zero_padding = 1; break;
4315 case '-': justify_left = 1; break;
4316 case '+': force_sign = 1; space_for_positive = 0; break;
4317 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004318 // If both the ' ' and '+' flags appear, the ' '
4319 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004320 break;
4321 case '#': alternate_form = 1; break;
4322 case '\'': break;
4323 }
4324 p++;
4325 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004326 // If the '0' and '-' flags both appear, the '0' flag should be
4327 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004328
Bram Moolenaar85a20022019-12-21 18:25:54 +01004329 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004330 if (*p == '*')
4331 {
4332 int j;
4333
4334 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004337 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338# endif
4339 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004340 if (j >= 0)
4341 min_field_width = j;
4342 else
4343 {
4344 min_field_width = -j;
4345 justify_left = 1;
4346 }
4347 }
4348 else if (VIM_ISDIGIT((int)(*p)))
4349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004350 // size_t could be wider than unsigned int; make sure we treat
4351 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004352 unsigned int uj = *p++ - '0';
4353
4354 while (VIM_ISDIGIT((int)(*p)))
4355 uj = 10 * uj + (unsigned int)(*p++ - '0');
4356 min_field_width = uj;
4357 }
4358
Bram Moolenaar85a20022019-12-21 18:25:54 +01004359 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004360 if (*p == '.')
4361 {
4362 p++;
4363 precision_specified = 1;
4364 if (*p == '*')
4365 {
4366 int j;
4367
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004370 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371# endif
4372 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004373 p++;
4374 if (j >= 0)
4375 precision = j;
4376 else
4377 {
4378 precision_specified = 0;
4379 precision = 0;
4380 }
4381 }
4382 else if (VIM_ISDIGIT((int)(*p)))
4383 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004384 // size_t could be wider than unsigned int; make sure we
4385 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004386 unsigned int uj = *p++ - '0';
4387
4388 while (VIM_ISDIGIT((int)(*p)))
4389 uj = 10 * uj + (unsigned int)(*p++ - '0');
4390 precision = uj;
4391 }
4392 }
4393
Bram Moolenaar85a20022019-12-21 18:25:54 +01004394 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004395 if (*p == 'h' || *p == 'l')
4396 {
4397 length_modifier = *p;
4398 p++;
4399 if (length_modifier == 'l' && *p == 'l')
4400 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004401 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004402 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403 p++;
4404 }
4405 }
4406 fmt_spec = *p;
4407
Bram Moolenaar85a20022019-12-21 18:25:54 +01004408 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004409 switch (fmt_spec)
4410 {
4411 case 'i': fmt_spec = 'd'; break;
4412 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4413 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4414 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4415 default: break;
4416 }
4417
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004418# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004419 switch (fmt_spec)
4420 {
4421 case 'd': case 'u': case 'o': case 'x': case 'X':
4422 if (tvs != NULL && length_modifier == '\0')
4423 length_modifier = 'L';
4424 }
4425# endif
4426
Bram Moolenaar85a20022019-12-21 18:25:54 +01004427 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004428 switch (fmt_spec)
4429 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004430 // '%' and 'c' behave similar to 's' regarding flags and field
4431 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432 case '%':
4433 case 'c':
4434 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004435 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004436 str_arg_l = 1;
4437 switch (fmt_spec)
4438 {
4439 case '%':
4440 str_arg = p;
4441 break;
4442
4443 case 'c':
4444 {
4445 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446
4447 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004449 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450# endif
4451 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004452 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004453 uchar_arg = (unsigned char)j;
4454 str_arg = (char *)&uchar_arg;
4455 break;
4456 }
4457
4458 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004459 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004461# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004462 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463# endif
4464 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004465 if (str_arg == NULL)
4466 {
4467 str_arg = "[NULL]";
4468 str_arg_l = 6;
4469 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004470 // make sure not to address string beyond the specified
4471 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004472 else if (!precision_specified)
4473 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004474 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004475 else if (precision == 0)
4476 str_arg_l = 0;
4477 else
4478 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004479 // Don't put the #if inside memchr(), it can be a
4480 // macro.
4481 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004482 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004483 precision <= (size_t)0x7fffffffL ? precision
4484 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004485 str_arg_l = (q == NULL) ? precision
4486 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004487 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004488 if (fmt_spec == 'S')
4489 {
4490 if (min_field_width != 0)
4491 min_field_width += STRLEN(str_arg)
4492 - mb_string2cells((char_u *)str_arg, -1);
4493 if (precision)
4494 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004495 char_u *p1;
4496 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004497
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004498 for (p1 = (char_u *)str_arg; *p1;
4499 p1 += mb_ptr2len(p1))
4500 {
4501 i += (size_t)mb_ptr2cells(p1);
4502 if (i > precision)
4503 break;
4504 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004505 str_arg_l = precision = p1 - (char_u *)str_arg;
4506 }
4507 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004508 break;
4509
4510 default:
4511 break;
4512 }
4513 break;
4514
Bram Moolenaar91984b92016-08-16 21:58:41 +02004515 case 'd': case 'u':
4516 case 'b': case 'B':
4517 case 'o':
4518 case 'x': case 'X':
4519 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004520 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004521 // NOTE: the u, b, o, x, X and p conversion specifiers
4522 // imply the value is unsigned; d implies a signed
4523 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004524
Bram Moolenaar85a20022019-12-21 18:25:54 +01004525 // 0 if numeric argument is zero (or if pointer is
4526 // NULL for 'p'), +1 if greater than zero (or nonzero
4527 // for unsigned arguments), -1 if negative (unsigned
4528 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004529 int arg_sign = 0;
4530
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004531 // only set for length modifier h, or for no length
4532 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004533 int int_arg = 0;
4534 unsigned int uint_arg = 0;
4535
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004536 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004537 long int long_arg = 0;
4538 unsigned long int ulong_arg = 0;
4539
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004540 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004541 varnumber_T llong_arg = 0;
4542 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004543
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004544 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004545 uvarnumber_T bin_arg = 0;
4546
Bram Moolenaar85a20022019-12-21 18:25:54 +01004547 // pointer argument value -only defined for p
4548 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004549 void *ptr_arg = NULL;
4550
4551 if (fmt_spec == 'p')
4552 {
4553 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004556 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4557 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558# endif
4559 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004560 if (ptr_arg != NULL)
4561 arg_sign = 1;
4562 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004563 else if (fmt_spec == 'b' || fmt_spec == 'B')
4564 {
4565 bin_arg =
4566# if defined(FEAT_EVAL)
4567 tvs != NULL ?
4568 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4569# endif
4570 va_arg(ap, uvarnumber_T);
4571 if (bin_arg != 0)
4572 arg_sign = 1;
4573 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004574 else if (fmt_spec == 'd')
4575 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004576 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004577 switch (length_modifier)
4578 {
4579 case '\0':
4580 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004581 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004582 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004584 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585# endif
4586 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004587 if (int_arg > 0)
4588 arg_sign = 1;
4589 else if (int_arg < 0)
4590 arg_sign = -1;
4591 break;
4592 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004595 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596# endif
4597 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004598 if (long_arg > 0)
4599 arg_sign = 1;
4600 else if (long_arg < 0)
4601 arg_sign = -1;
4602 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004603 case 'L':
4604 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004605# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004606 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004607# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004608 va_arg(ap, varnumber_T);
4609 if (llong_arg > 0)
4610 arg_sign = 1;
4611 else if (llong_arg < 0)
4612 arg_sign = -1;
4613 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004614 }
4615 }
4616 else
4617 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004618 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004619 switch (length_modifier)
4620 {
4621 case '\0':
4622 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004624# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004625 tvs != NULL ? (unsigned)
4626 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627# endif
4628 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004629 if (uint_arg != 0)
4630 arg_sign = 1;
4631 break;
4632 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004633 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004635 tvs != NULL ? (unsigned long)
4636 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637# endif
4638 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004639 if (ulong_arg != 0)
4640 arg_sign = 1;
4641 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004642 case 'L':
4643 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004644# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004645 tvs != NULL ? (uvarnumber_T)
4646 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004647# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004648 va_arg(ap, uvarnumber_T);
4649 if (ullong_arg != 0)
4650 arg_sign = 1;
4651 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004652 }
4653 }
4654
4655 str_arg = tmp;
4656 str_arg_l = 0;
4657
Bram Moolenaar85a20022019-12-21 18:25:54 +01004658 // NOTE:
4659 // For d, i, u, o, x, and X conversions, if precision is
4660 // specified, the '0' flag should be ignored. This is so
4661 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4662 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004663 if (precision_specified)
4664 zero_padding = 0;
4665 if (fmt_spec == 'd')
4666 {
4667 if (force_sign && arg_sign >= 0)
4668 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004669 // leave negative numbers for sprintf to handle, to
4670 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004671 }
4672 else if (alternate_form)
4673 {
4674 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004675 && (fmt_spec == 'b' || fmt_spec == 'B'
4676 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004677 {
4678 tmp[str_arg_l++] = '0';
4679 tmp[str_arg_l++] = fmt_spec;
4680 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004681 // alternate form should have no effect for p
4682 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004683 }
4684
4685 zero_padding_insertion_ind = str_arg_l;
4686 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004687 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004688 if (precision == 0 && arg_sign == 0)
4689 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004690 // When zero value is formatted with an explicit
4691 // precision 0, the resulting formatted string is
4692 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004693 }
4694 else
4695 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004696 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004697 int f_l = 0;
4698
Bram Moolenaar85a20022019-12-21 18:25:54 +01004699 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004700 f[f_l++] = '%';
4701 if (!length_modifier)
4702 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004703 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004704 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004705# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004706 f[f_l++] = 'I';
4707 f[f_l++] = '6';
4708 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004709# else
4710 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004711 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004712# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004713 }
4714 else
4715 f[f_l++] = length_modifier;
4716 f[f_l++] = fmt_spec;
4717 f[f_l++] = '\0';
4718
4719 if (fmt_spec == 'p')
4720 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004721 else if (fmt_spec == 'b' || fmt_spec == 'B')
4722 {
4723 char b[8 * sizeof(uvarnumber_T)];
4724 size_t b_l = 0;
4725 uvarnumber_T bn = bin_arg;
4726
4727 do
4728 {
4729 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4730 bn >>= 1;
4731 }
4732 while (bn != 0);
4733
4734 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4735 str_arg_l += b_l;
4736 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004737 else if (fmt_spec == 'd')
4738 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004739 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004740 switch (length_modifier)
4741 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004742 case '\0': str_arg_l += sprintf(
4743 tmp + str_arg_l, f,
4744 int_arg);
4745 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004746 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004747 tmp + str_arg_l, f,
4748 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004749 break;
4750 case 'l': str_arg_l += sprintf(
4751 tmp + str_arg_l, f, long_arg);
4752 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004753 case 'L': str_arg_l += sprintf(
4754 tmp + str_arg_l, f, llong_arg);
4755 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004756 }
4757 }
4758 else
4759 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004760 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004761 switch (length_modifier)
4762 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004763 case '\0': str_arg_l += sprintf(
4764 tmp + str_arg_l, f,
4765 uint_arg);
4766 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004767 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004768 tmp + str_arg_l, f,
4769 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004770 break;
4771 case 'l': str_arg_l += sprintf(
4772 tmp + str_arg_l, f, ulong_arg);
4773 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004774 case 'L': str_arg_l += sprintf(
4775 tmp + str_arg_l, f, ullong_arg);
4776 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004777 }
4778 }
4779
Bram Moolenaar85a20022019-12-21 18:25:54 +01004780 // include the optional minus sign and possible
4781 // "0x" in the region before the zero padding
4782 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004783 if (zero_padding_insertion_ind < str_arg_l
4784 && tmp[zero_padding_insertion_ind] == '-')
4785 zero_padding_insertion_ind++;
4786 if (zero_padding_insertion_ind + 1 < str_arg_l
4787 && tmp[zero_padding_insertion_ind] == '0'
4788 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4789 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4790 zero_padding_insertion_ind += 2;
4791 }
4792
4793 {
4794 size_t num_of_digits = str_arg_l
4795 - zero_padding_insertion_ind;
4796
4797 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004798 // unless zero is already the first
4799 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004800 && !(zero_padding_insertion_ind < str_arg_l
4801 && tmp[zero_padding_insertion_ind] == '0'))
4802 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004803 // assure leading zero for alternate-form
4804 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004805 if (!precision_specified
4806 || precision < num_of_digits + 1)
4807 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004808 // precision is increased to force the
4809 // first character to be zero, except if a
4810 // zero value is formatted with an
4811 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004812 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004813 }
4814 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004815 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004816 if (num_of_digits < precision)
4817 number_of_zeros_to_pad = precision - num_of_digits;
4818 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004819 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004820 if (!justify_left && zero_padding)
4821 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004822 int n = (int)(min_field_width - (str_arg_l
4823 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004824 if (n > 0)
4825 number_of_zeros_to_pad += n;
4826 }
4827 break;
4828 }
4829
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004830# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004831 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004832 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004833 case 'e':
4834 case 'E':
4835 case 'g':
4836 case 'G':
4837 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004838 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004839 double f;
4840 double abs_f;
4841 char format[40];
4842 int l;
4843 int remove_trailing_zeroes = FALSE;
4844
4845 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004846# if defined(FEAT_EVAL)
4847 tvs != NULL ? tv_float(tvs, &arg_idx) :
4848# endif
4849 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004850 abs_f = f < 0 ? -f : f;
4851
4852 if (fmt_spec == 'g' || fmt_spec == 'G')
4853 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004854 // Would be nice to use %g directly, but it prints
4855 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004856 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4857 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004858 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004859 else
4860 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4861 remove_trailing_zeroes = TRUE;
4862 }
4863
Bram Moolenaar04186092016-08-29 21:55:35 +02004864 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004865# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004866 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004867# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004868 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004869# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004870 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004871 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004872 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004873 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4874 force_sign, space_for_positive));
4875 str_arg_l = STRLEN(tmp);
4876 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004877 }
4878 else
4879 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004880 if (isnan(f))
4881 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004882 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004883 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4884 : "nan");
4885 str_arg_l = 3;
4886 zero_padding = 0;
4887 }
4888 else if (isinf(f))
4889 {
4890 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4891 force_sign, space_for_positive));
4892 str_arg_l = STRLEN(tmp);
4893 zero_padding = 0;
4894 }
4895 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004896 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004897 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004898 format[0] = '%';
4899 l = 1;
4900 if (force_sign)
4901 format[l++] = space_for_positive ? ' ' : '+';
4902 if (precision_specified)
4903 {
4904 size_t max_prec = TMP_LEN - 10;
4905
Bram Moolenaar85a20022019-12-21 18:25:54 +01004906 // Make sure we don't get more digits than we
4907 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004908 if ((fmt_spec == 'f' || fmt_spec == 'F')
4909 && abs_f > 1.0)
4910 max_prec -= (size_t)log10(abs_f);
4911 if (precision > max_prec)
4912 precision = max_prec;
4913 l += sprintf(format + l, ".%d", (int)precision);
4914 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004915 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004916 format[l + 1] = NUL;
4917
Bram Moolenaare9997822016-08-28 16:03:38 +02004918 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004919 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004920
Bram Moolenaara7241f52008-06-24 20:39:31 +00004921 if (remove_trailing_zeroes)
4922 {
4923 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004924 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004925
Bram Moolenaar85a20022019-12-21 18:25:54 +01004926 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004927 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004928 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004929 else
4930 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004931 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004933 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004934 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004935 // Remove superfluous '+' and leading
4936 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004937 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004938 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004939 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004940 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004941 --str_arg_l;
4942 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004943 i = (tp[1] == '-') ? 2 : 1;
4944 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004945 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004946 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004947 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004948 --str_arg_l;
4949 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004950 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004951 }
4952 }
4953
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004954 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004955 // Remove trailing zeroes, but keep the one
4956 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004957 while (tp > tmp + 2 && *tp == '0'
4958 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004959 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004960 STRMOVE(tp, tp + 1);
4961 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004962 --str_arg_l;
4963 }
4964 }
4965 else
4966 {
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 // Be consistent: some printf("%e") use 1.0e+12
4970 // and some 1.0e+012. Remove one zero in the last
4971 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004972 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004973 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004974 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4975 && tp[2] == '0'
4976 && vim_isdigit(tp[3])
4977 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004978 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004979 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004980 --str_arg_l;
4981 }
4982 }
4983 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004984 if (zero_padding && min_field_width > str_arg_l
4985 && (tmp[0] == '-' || force_sign))
4986 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004987 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02004988 number_of_zeros_to_pad = min_field_width - str_arg_l;
4989 zero_padding_insertion_ind = 1;
4990 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004991 str_arg = tmp;
4992 break;
4993 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004994# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004995
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004996 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01004997 // unrecognized conversion specifier, keep format string
4998 // as-is
4999 zero_padding = 0; // turn zero padding off for non-numeric
5000 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005001 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01005002 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005003
Bram Moolenaar85a20022019-12-21 18:25:54 +01005004 // discard the unrecognized conversion, just keep *
5005 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005006 str_arg = p;
5007 str_arg_l = 0;
5008 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005009 str_arg_l++; // include invalid conversion specifier
5010 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005011 break;
5012 }
5013
5014 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01005015 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005016
Bram Moolenaar85a20022019-12-21 18:25:54 +01005017 // insert padding to the left as requested by min_field_width;
5018 // this does not include the zero padding in case of numerical
5019 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005020 if (!justify_left)
5021 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005022 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005023 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005024
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005025 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005026 {
5027 if (str_l < str_m)
5028 {
5029 size_t avail = str_m - str_l;
5030
5031 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005032 (size_t)pn > avail ? avail
5033 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005034 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005035 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005036 }
5037 }
5038
Bram Moolenaar85a20022019-12-21 18:25:54 +01005039 // zero padding as requested by the precision or by the minimal
5040 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005041 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005042 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005043 // will not copy first part of numeric right now, *
5044 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005045 zero_padding_insertion_ind = 0;
5046 }
5047 else
5048 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005049 // insert first part of numerics (sign or '0x') before zero
5050 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005051 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005052
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005053 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005054 {
5055 if (str_l < str_m)
5056 {
5057 size_t avail = str_m - str_l;
5058
5059 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005060 (size_t)zn > avail ? avail
5061 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005062 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005063 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005064 }
5065
Bram Moolenaar85a20022019-12-21 18:25:54 +01005066 // insert zero padding as requested by the precision or min
5067 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005068 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005069 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005070 {
5071 if (str_l < str_m)
5072 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005073 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005074
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005075 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005076 (size_t)zn > avail ? avail
5077 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005078 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005079 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005080 }
5081 }
5082
Bram Moolenaar85a20022019-12-21 18:25:54 +01005083 // insert formatted string
5084 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005085 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005086 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005087
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005088 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005089 {
5090 if (str_l < str_m)
5091 {
5092 size_t avail = str_m - str_l;
5093
5094 mch_memmove(str + str_l,
5095 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005096 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005097 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005098 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005099 }
5100 }
5101
Bram Moolenaar85a20022019-12-21 18:25:54 +01005102 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103 if (justify_left)
5104 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005105 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005106 int pn = (int)(min_field_width
5107 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005108
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005109 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005110 {
5111 if (str_l < str_m)
5112 {
5113 size_t avail = str_m - str_l;
5114
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005115 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005116 (size_t)pn > avail ? avail
5117 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005118 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005119 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005120 }
5121 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005122 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005123 }
5124 }
5125
5126 if (str_m > 0)
5127 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005128 // make sure the string is nul-terminated even at the expense of
5129 // overwriting the last character (shouldn't happen, but just in case)
5130 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005131 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5132 }
5133
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005134 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005135 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136
Bram Moolenaar85a20022019-12-21 18:25:54 +01005137 // Return the number of characters formatted (excluding trailing nul
5138 // character), that is, the number of characters that would have been
5139 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005140 return (int)str_l;
5141}
5142
Bram Moolenaar85a20022019-12-21 18:25:54 +01005143#endif // PROTO