blob: 0119145cf4a27ee851445486b5e8ec923fd33a26 [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 Moolenaar1a47ae32019-12-29 23:04:25 +0100464 char_u *sname = estack_sfile();
465 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 Moolenaar85a20022019-12-21 18:25:54 +0100657 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 set_vim_var_string(VV_ERRMSG, s, -1);
659#endif
660
661 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000662 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 * But do write it to the redirection file.
664 */
665 if (emsg_silent != 0)
666 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200667 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200669 msg_start();
670 p = get_emsg_source();
671 if (p != NULL)
672 {
673 STRCAT(p, "\n");
674 redir_write(p, -1);
675 vim_free(p);
676 }
677 p = get_emsg_lnum();
678 if (p != NULL)
679 {
680 STRCAT(p, "\n");
681 redir_write(p, -1);
682 vim_free(p);
683 }
684 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100686#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200687 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 return TRUE;
690 }
691
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100692 ex_exitval = 1;
693
Bram Moolenaar85a20022019-12-21 18:25:54 +0100694 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 msg_silent = 0;
696 cmd_silent = FALSE;
697
Bram Moolenaar85a20022019-12-21 18:25:54 +0100698 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ++global_busy;
700
701 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100702 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200704 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100705 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200706#ifdef FEAT_EVAL
707 did_uncaught_emsg = TRUE;
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 emsg_on_display = TRUE; // remember there is an error message
712 ++msg_scroll; // don't overwrite a previous message
713 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000714 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100715 need_wait_return = TRUE; // needed in case emsg() is called after
716 // wait_return has reset need_wait_return
717 // and a redraw is expected because
718 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar958dc692016-12-01 15:34:12 +0100720#ifdef FEAT_JOB_CHANNEL
721 emsg_to_channel_log = TRUE;
722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 /*
724 * Display name and line number for the source of the error.
725 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000726 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
728 /*
729 * Display the error message itself.
730 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100731 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100732 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100733
734#ifdef FEAT_JOB_CHANNEL
735 emsg_to_channel_log = FALSE;
736#endif
737 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
740/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100741 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
743 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100746 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100747 if (!emsg_not_now())
748 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100749 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100752#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100753/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 * Print an error message with format string and variable arguments.
755 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100756 */
757 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100758semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100759{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200760 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100761 if (!emsg_not_now())
762 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200763 if (IObuff == NULL)
764 {
765 // Very early in initialisation and already something wrong, just
766 // give the raw message so the user at least gets a hint.
767 return emsg_core((char_u *)s);
768 }
769 else
770 {
771 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100772
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200773 va_start(ap, s);
774 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
775 va_end(ap);
776 return emsg_core(IObuff);
777 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200779 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100780}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100781#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100782
783/*
784 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
785 * defined. It is used for internal errors only, so that they can be
786 * detected when fuzzing vim.
787 */
788 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100789iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100790{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100791 if (!emsg_not_now())
792 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100793#ifdef ABORT_ON_INTERNAL_ERROR
794 abort();
795#endif
796}
797
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100798#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100800 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100801 * defined. It is used for internal errors only, so that they can be
802 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100804 */
805 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100808 if (!emsg_not_now())
809 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200810 if (IObuff == NULL)
811 {
812 // Very early in initialisation and already something wrong, just
813 // give the raw message so the user at least gets a hint.
814 emsg_core((char_u *)s);
815 }
816 else
817 {
818 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100819
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200820 va_start(ap, s);
821 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
822 va_end(ap);
823 emsg_core(IObuff);
824 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100825 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100826# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100827 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100828# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100829}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100830#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100831
832/*
833 * Give an "Internal error" message.
834 */
835 void
836internal_error(char *where)
837{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839}
840
Bram Moolenaardd589232020-02-29 17:38:12 +0100841/*
842 * Like internal_error() but do not call abort(), to avoid tests using
843 * test_unknown() and test_void() causing Vim to exit.
844 */
845 void
846internal_error_no_abort(char *where)
847{
848 semsg(_(e_intern2), where);
849}
850
Bram Moolenaar85a20022019-12-21 18:25:54 +0100851// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaardf177f62005-02-22 08:39:57 +0000853 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100854emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000855{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100856 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000857}
858
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 * Give an error message which contains %s for "name[len]".
861 */
862 void
863emsg_namelen(char *msg, char_u *name, int len)
864{
865 char_u *copy = vim_strnsave((char_u *)name, len);
866
867 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
868}
869
870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 * Like msg(), but truncate to a single line if p_shm contains 't', or when
872 * "force" is TRUE. This truncates in another way as for normal messages.
873 * Careful: The string may be changed by msg_may_trunc()!
874 * Returns a pointer to the printed message, if wait_return() not called.
875 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100876 char *
877msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100880 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaar85a20022019-12-21 18:25:54 +0100882 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100883 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
Bram Moolenaar32526b32019-01-19 17:43:09 +0100885 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100888 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 msg_hist_off = FALSE;
890
891 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100892 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 return NULL;
894}
895
896/*
897 * Check if message "s" should be truncated at the start (for filenames).
898 * Return a pointer to where the truncated message starts.
899 * Note: May change the message by replacing a character with '<'.
900 */
901 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100902msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
904 int n;
905 int room;
906
907 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
908 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
909 && (n = (int)STRLEN(s) - room) > 0)
910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (has_mbyte)
912 {
913 int size = vim_strsize(s);
914
Bram Moolenaar85a20022019-12-21 18:25:54 +0100915 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000916 if (size <= room)
917 return s;
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 for (n = 0; size >= room; )
920 {
921 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000922 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924 --n;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 s += n;
927 *s = '<';
928 }
929 return s;
930}
931
932 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100933add_msg_hist(
934 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100935 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100936 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
938 struct msg_hist *p;
939
940 if (msg_hist_off || msg_silent != 0)
941 return;
942
Bram Moolenaar85a20022019-12-21 18:25:54 +0100943 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000944 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000945 (void)delete_first_msg();
946
Bram Moolenaar85a20022019-12-21 18:25:54 +0100947 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200948 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (p != NULL)
950 {
951 if (len < 0)
952 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100953 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 while (len > 0 && *s == '\n')
955 {
956 ++s;
957 --len;
958 }
959 while (len > 0 && s[len - 1] == '\n')
960 --len;
961 p->msg = vim_strnsave(s, len);
962 p->next = NULL;
963 p->attr = attr;
964 if (last_msg_hist != NULL)
965 last_msg_hist->next = p;
966 last_msg_hist = p;
967 if (first_msg_hist == NULL)
968 first_msg_hist = last_msg_hist;
969 ++msg_hist_len;
970 }
971}
972
973/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000974 * Delete the first (oldest) message from the history.
975 * Returns FAIL if there are no messages.
976 */
977 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100978delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000979{
980 struct msg_hist *p;
981
982 if (msg_hist_len <= 0)
983 return FAIL;
984 p = first_msg_hist;
985 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000986 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100987 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000988 vim_free(p->msg);
989 vim_free(p);
990 --msg_hist_len;
991 return OK;
992}
993
994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 * ":messages" command.
996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200998ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
1000 struct msg_hist *p;
1001 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001002 int c = 0;
1003
1004 if (STRCMP(eap->arg, "clear") == 0)
1005 {
1006 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1007
1008 while (msg_hist_len > keep)
1009 (void)delete_first_msg();
1010 return;
1011 }
1012
1013 if (*eap->arg != NUL)
1014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001015 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001016 return;
1017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019 msg_hist_off = TRUE;
1020
Bram Moolenaar451f8492016-04-14 17:16:22 +02001021 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001022 if (eap->addr_count != 0)
1023 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001024 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001025 for (; p != NULL && !got_int; p = p->next)
1026 c++;
1027
1028 c -= eap->line2;
1029
Bram Moolenaar85a20022019-12-21 18:25:54 +01001030 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001031 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1032 p = p->next, c--);
1033 }
1034
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001035 if (p == first_msg_hist)
1036 {
1037 s = mch_getenv((char_u *)"LANG");
1038 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001039 // The next comment is extracted by xgettext and put in po file for
1040 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001041 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001042 // Translator: Please replace the name and email address
1043 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001044 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001045 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001046 }
1047
Bram Moolenaar85a20022019-12-21 18:25:54 +01001048 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001049 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001051 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053 msg_hist_off = FALSE;
1054}
1055
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001056#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057/*
1058 * Call this after prompting the user. This will avoid a hit-return message
1059 * and a delay.
1060 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001061 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001062msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
1064 need_wait_return = FALSE;
1065 emsg_on_display = FALSE;
1066 cmdline_row = msg_row;
1067 msg_col = 0;
1068 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001069 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070}
1071#endif
1072
1073/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001074 * Wait for the user to hit a key (normally Enter).
1075 * If "redraw" is TRUE, clear and redraw the screen.
1076 * If "redraw" is FALSE, just redraw the screen.
1077 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 */
1079 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001080wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 int c;
1083 int oldState;
1084 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001086 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001087 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
1089 if (redraw == TRUE)
1090 must_redraw = CLEAR;
1091
Bram Moolenaar85a20022019-12-21 18:25:54 +01001092 // If using ":silent cmd", don't wait for a return. Also don't set
1093 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (msg_silent != 0)
1095 return;
1096
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001097 /*
1098 * When inside vgetc(), we can't wait for a typed character at all.
1099 * With the global command (and some others) we only need one return at
1100 * the end. Adjust cmdline_row to avoid the next message overwriting the
1101 * last one.
1102 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001103 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001105 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (no_wait_return)
1107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (!exmode_active)
1109 cmdline_row = msg_row;
1110 return;
1111 }
1112
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 oldState = State;
1115 if (quit_more)
1116 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001117 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 quit_more = FALSE;
1119 got_int = FALSE;
1120 }
1121 else if (exmode_active)
1122 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001123 msg_puts(" "); // make sure the cursor is on the right line
1124 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 got_int = FALSE;
1126 }
1127 else
1128 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 // Make sure the hit-return prompt is on screen when 'guioptions' was
1130 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 screenalloc(FALSE);
1132
1133 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001136 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001138 cmdline_row = msg_row;
1139
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // Avoid the sequence that the user types ":" at the hit-return prompt
1141 // to start an Ex command, but the file-changed dialog gets in the
1142 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001143 if (need_check_timestamps)
1144 check_timestamps(FALSE);
1145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 hit_return_msg();
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 do
1149 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001150 // Remember "got_int", if it is set vgetc() probably returns a
1151 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001153
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // Don't do mappings here, we put the character back in the
1155 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001156 ++no_mapping;
1157 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001158
Bram Moolenaar85a20022019-12-21 18:25:54 +01001159 // Temporarily disable Recording. If Recording is active, the
1160 // character will be recorded later, since it will be added to the
1161 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001162 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001163 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001164 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001165 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001167 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001169 --no_mapping;
1170 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001171 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001172 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001175 // Strange way to allow copying (yanking) a modeless selection at
1176 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1177 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1179 {
1180 clip_copy_modeless_selection(TRUE);
1181 c = K_IGNORE;
1182 }
1183#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001184
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001185 /*
1186 * Allow scrolling back in the messages.
1187 * Also accept scroll-down commands when messages fill the screen,
1188 * to avoid that typing one 'j' too many makes the messages
1189 * disappear.
1190 */
1191 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001192 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001193 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1194 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001195 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001196 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001197 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001198 do_more_prompt(c);
1199 else
1200 {
1201 msg_didout = FALSE;
1202 c = K_IGNORE;
1203 msg_col =
1204#ifdef FEAT_RIGHTLEFT
1205 cmdmsg_rl ? Columns - 1 :
1206#endif
1207 0;
1208 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001209 if (quit_more)
1210 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001212 quit_more = FALSE;
1213 got_int = FALSE;
1214 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001215 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001216 {
1217 c = K_IGNORE;
1218 hit_return_msg();
1219 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001220 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001221 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001222 && (c == 'j' || c == 'd' || c == 'f'
1223 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001224 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 } while ((had_got_int && c == Ctrl_C)
1227 || c == K_IGNORE
1228#ifdef FEAT_GUI
1229 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1232 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1233 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001234 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001236 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 || (!mouse_has(MOUSE_RETURN)
1238 && mouse_row < msg_row
1239 && (c == K_LEFTMOUSE
1240 || c == K_MIDDLEMOUSE
1241 || c == K_RIGHTMOUSE
1242 || c == K_X1MOUSE
1243 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 );
1245 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 /*
1247 * Avoid that the mouse-up event causes visual mode to start.
1248 */
1249 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1250 || c == K_X1MOUSE || c == K_X2MOUSE)
1251 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001252 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001254 // Put the character back in the typeahead buffer. Don't use the
1255 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001256 ins_char_typebuf(c);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001257 do_redraw = TRUE; // need a redraw even though there is
1258 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 redir_off = FALSE;
1262
1263 /*
1264 * If the user hits ':', '?' or '/' we get a command line from the next
1265 * line.
1266 */
1267 if (c == ':' || c == '?' || c == '/')
1268 {
1269 if (!exmode_active)
1270 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001271 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001273#ifdef FEAT_TERMINAL
1274 skip_term_loop = TRUE;
1275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277
1278 /*
1279 * If the window size changed set_shellsize() will redraw the screen.
1280 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1281 * typed.
1282 */
1283 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001284 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 msg_check();
1287
1288#if defined(UNIX) || defined(VMS)
1289 /*
1290 * When switching screens, we need to output an extra newline on exit.
1291 */
1292 if (swapping_screen() && !termcap_active)
1293 newline_on_exit = TRUE;
1294#endif
1295
1296 need_wait_return = FALSE;
1297 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001298 emsg_on_display = FALSE; // can delete error message now
1299 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 reset_last_sourcing();
1301 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1302 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
Bram Moolenaar85a20022019-12-21 18:25:54 +01001305 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001307 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 shell_resized();
1309 }
1310 else if (!skip_redraw
1311 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1312 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001313 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 redraw_later(VALID);
1315 }
1316}
1317
1318/*
1319 * Write the hit-return prompt.
1320 */
1321 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001322hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001324 int save_p_more = p_more;
1325
Bram Moolenaar85a20022019-12-21 18:25:54 +01001326 p_more = FALSE; // don't want see this message when scrolling back
1327 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 msg_putchar('\n');
1329 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001330 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaar32526b32019-01-19 17:43:09 +01001332 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (!msg_use_printf())
1334 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001335 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336}
1337
1338/*
1339 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1340 */
1341 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001342set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
1344 vim_free(keep_msg);
1345 if (s != NULL && msg_silent == 0)
1346 keep_msg = vim_strsave(s);
1347 else
1348 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001349 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001350 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351}
1352
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001353#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1354/*
1355 * If there currently is a message being displayed, set "keep_msg" to it, so
1356 * that it will be displayed again after redraw.
1357 */
1358 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001359set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001360{
1361 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1362 && (State & NORMAL))
1363 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1364}
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
1368 * Prepare for outputting characters in the command line.
1369 */
1370 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001371msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
1373 int did_return = FALSE;
1374
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001375 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001376 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001377
1378#ifdef FEAT_EVAL
1379 if (need_clr_eos)
1380 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001381 // Halfway an ":echo" command and getting an (error) message: clear
1382 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001383 need_clr_eos = FALSE;
1384 msg_clr_eos();
1385 }
1386#endif
1387
Bram Moolenaar85a20022019-12-21 18:25:54 +01001388 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
1390 msg_row = cmdline_row;
1391 msg_col =
1392#ifdef FEAT_RIGHTLEFT
1393 cmdmsg_rl ? Columns - 1 :
1394#endif
1395 0;
1396 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001397 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
1399 msg_putchar('\n');
1400 did_return = TRUE;
1401 if (exmode_active != EXMODE_NORMAL)
1402 cmdline_row = msg_row;
1403 }
1404 if (!msg_didany || lines_left < 0)
1405 msg_starthere();
1406 if (msg_silent == 0)
1407 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001408 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 cursor_off();
1410 }
1411
Bram Moolenaar85a20022019-12-21 18:25:54 +01001412 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (!did_return)
1414 redir_write((char_u *)"\n", -1);
1415}
1416
1417/*
1418 * Note that the current msg position is where messages start.
1419 */
1420 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001421msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 lines_left = cmdline_row;
1424 msg_didany = FALSE;
1425}
1426
1427 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001428msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430 msg_putchar_attr(c, 0);
1431}
1432
1433 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001434msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001436 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 if (IS_SPECIAL(c))
1439 {
1440 buf[0] = K_SPECIAL;
1441 buf[1] = K_SECOND(c);
1442 buf[2] = K_THIRD(c);
1443 buf[3] = NUL;
1444 }
1445 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001446 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001447 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448}
1449
1450 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001451msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001453 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
Bram Moolenaar32526b32019-01-19 17:43:09 +01001455 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 msg_puts(buf);
1457}
1458
1459 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001460msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 msg_home_replace_attr(fname, 0);
1463}
1464
1465#if defined(FEAT_FIND_ID) || defined(PROTO)
1466 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001467msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001469 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470}
1471#endif
1472
1473 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001474msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 char_u *name;
1477
1478 name = home_replace_save(NULL, fname);
1479 if (name != NULL)
1480 msg_outtrans_attr(name, attr);
1481 vim_free(name);
1482}
1483
1484/*
1485 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001486 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 * Use attributes 'attr'.
1488 * Return the number of characters it takes on the screen.
1489 */
1490 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001491msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 return msg_outtrans_attr(str, 0);
1494}
1495
1496 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001497msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1500}
1501
1502 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001503msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 return msg_outtrans_len_attr(str, len, 0);
1506}
1507
1508/*
1509 * Output one character at "p". Return pointer to the next character.
1510 * Handles multi-byte characters.
1511 */
1512 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001513msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 int l;
1516
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001517 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 msg_outtrans_len_attr(p, l, attr);
1520 return p + l;
1521 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001522 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 return p + 1;
1524}
1525
1526 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001527msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 int retval = 0;
1530 char_u *str = msgstr;
1531 char_u *plain_start = msgstr;
1532 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 int mb_l;
1534 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaar85a20022019-12-21 18:25:54 +01001536 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (attr & MSG_HIST)
1538 {
1539 add_msg_hist(str, len, attr);
1540 attr &= ~MSG_HIST;
1541 }
1542
Bram Moolenaar85a20022019-12-21 18:25:54 +01001543 // If the string starts with a composing character first draw a space on
1544 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001546 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548 /*
1549 * Go over the string. Special characters are translated and printed.
1550 * Normal characters are printed several at a time.
1551 */
1552 while (--len >= 0)
1553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001555 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001556 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001558 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 else
1560 mb_l = 1;
1561 if (has_mbyte && mb_l > 1)
1562 {
1563 c = (*mb_ptr2char)(str);
1564 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001565 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 retval += (*mb_ptr2cells)(str);
1567 else
1568 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001569 // unprintable multi-byte char: print the printable chars so
1570 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001572 msg_puts_attr_len((char *)plain_start,
1573 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001575 msg_puts_attr((char *)transchar(c),
1576 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 retval += char2cells(c);
1578 }
1579 len -= mb_l - 1;
1580 str += mb_l;
1581 }
1582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 s = transchar_byte(*str);
1585 if (s[1] != NUL)
1586 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001587 // unprintable char: print the printable chars so far and the
1588 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001590 msg_puts_attr_len((char *)plain_start,
1591 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001593 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001594 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001596 else
1597 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 ++str;
1599 }
1600 }
1601
1602 if (str > plain_start)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001604 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606 return retval;
1607}
1608
1609#if defined(FEAT_QUICKFIX) || defined(PROTO)
1610 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001611msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 int i;
1614 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1615
1616 arg = skipwhite(arg);
1617 for (i = 5; *arg && i >= 0; --i)
1618 if (*arg++ != str[i])
1619 break;
1620 if (i < 0)
1621 {
1622 msg_putchar('\n');
1623 for (i = 0; rs[i]; ++i)
1624 msg_putchar(rs[i] - 3);
1625 }
1626}
1627#endif
1628
1629/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001630 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 * Return the number of characters it takes on the screen.
1632 *
1633 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1634 * following character and shown as <F1>, <S-Up> etc. Any other character
1635 * which is not printable shown in <> form.
1636 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1637 * If a character is displayed in one of these special ways, is also
1638 * highlighted (its highlight name is '8' in the p_hl variable).
1639 * Otherwise characters are not highlighted.
1640 * This function is used to show mappings, where we want to see how to type
1641 * the character/string -- webb
1642 */
1643 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001644msg_outtrans_special(
1645 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001646 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001647 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 char_u *str = strstart;
1650 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001651 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 int attr;
1653 int len;
1654
Bram Moolenaar8820b482017-03-16 17:23:31 +01001655 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 while (*str != NUL)
1657 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001658 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if ((str == strstart || str[1] == NUL) && *str == ' ')
1660 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001661 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 ++str;
1663 }
1664 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001665 text = (char *)str2special(&str, from);
1666 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001667 if (maxlen > 0 && retval + len >= maxlen)
1668 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001669 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001670 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001671 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 retval += len;
1673 }
1674 return retval;
1675}
1676
Bram Moolenaarbd743252010-10-20 21:23:33 +02001677#if defined(FEAT_EVAL) || defined(PROTO)
1678/*
1679 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1680 * strings, in an allocated string.
1681 */
1682 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001683str2special_save(
1684 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001685 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001686{
1687 garray_T ga;
1688 char_u *p = str;
1689
1690 ga_init2(&ga, 1, 40);
1691 while (*p != NUL)
1692 ga_concat(&ga, str2special(&p, is_lhs));
1693 ga_append(&ga, NUL);
1694 return (char_u *)ga.ga_data;
1695}
1696#endif
1697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698/*
1699 * Return the printable string for the key codes at "*sp".
1700 * Used for translating the lhs or rhs of a mapping to printable chars.
1701 * Advances "sp" to the next code.
1702 */
1703 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001704str2special(
1705 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001706 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 int c;
1709 static char_u buf[7];
1710 char_u *str = *sp;
1711 int modifiers = 0;
1712 int special = FALSE;
1713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (has_mbyte)
1715 {
1716 char_u *p;
1717
Bram Moolenaar85a20022019-12-21 18:25:54 +01001718 // Try to un-escape a multi-byte character. Return the un-escaped
1719 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 p = mb_unescape(sp);
1721 if (p != NULL)
1722 return p;
1723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
1725 c = *str;
1726 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1727 {
1728 if (str[1] == KS_MODIFIER)
1729 {
1730 modifiers = str[2];
1731 str += 3;
1732 c = *str;
1733 }
1734 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1735 {
1736 c = TO_SPECIAL(str[1], str[2]);
1737 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001739 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 special = TRUE;
1741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001743 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001745 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001746
Bram Moolenaar85a20022019-12-21 18:25:54 +01001747 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001748 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1749 {
1750 transchar_nonprint(buf, c);
1751 *sp = str + 1;
1752 return buf;
1753 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001754 // Since 'special' is TRUE the multi-byte character 'c' will be
1755 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001756 c = (*mb_ptr2char)(str);
1757 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001759 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001760 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Bram Moolenaar85a20022019-12-21 18:25:54 +01001762 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1763 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (special || char2cells(c) > 1 || (from && c == ' '))
1765 return get_special_key_name(c, modifiers);
1766 buf[0] = c;
1767 buf[1] = NUL;
1768 return buf;
1769}
1770
1771/*
1772 * Translate a key sequence into special key names.
1773 */
1774 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001775str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 char_u *s;
1778
1779 *buf = NUL;
1780 while (*sp)
1781 {
1782 s = str2special(&sp, FALSE);
1783 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1784 STRCAT(buf, s);
1785 }
1786}
1787
1788/*
1789 * print line for :print or :list command
1790 */
1791 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001792msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
1794 int c;
1795 int col = 0;
1796 int n_extra = 0;
1797 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001798 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001799 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001801 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 int l;
1804 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaardf177f62005-02-22 08:39:57 +00001806 if (curwin->w_p_list)
1807 list = TRUE;
1808
Bram Moolenaar85a20022019-12-21 18:25:54 +01001809 // find start of trailing whitespace
Bram Moolenaardf177f62005-02-22 08:39:57 +00001810 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 {
1812 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001813 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 --trail;
1815 }
1816
Bram Moolenaar85a20022019-12-21 18:25:54 +01001817 // output a space for an empty line, otherwise the line will be
1818 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001819 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 msg_putchar(' ');
1821
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001822 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001824 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001827 if (n_extra == 0 && c_final)
1828 c = c_final;
1829 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 c = c_extra;
1831 else
1832 c = *p_extra++;
1833 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001834 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001837 if (lcs_nbsp != NUL && list
1838 && (mb_ptr2char(s) == 160
1839 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001840 {
1841 mb_char2bytes(lcs_nbsp, buf);
1842 buf[(*mb_ptr2len)(buf)] = NUL;
1843 }
1844 else
1845 {
1846 mch_memmove(buf, s, (size_t)l);
1847 buf[l] = NUL;
1848 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001849 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 s += l;
1851 continue;
1852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 else
1854 {
1855 attr = 0;
1856 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001857 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001859 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001860#ifdef FEAT_VARTABS
1861 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1862 curbuf->b_p_vts_array) - 1;
1863#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001865#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001866 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
1868 c = ' ';
1869 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001870 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 else
1873 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001874 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001876 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001877 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001880 else if (c == 160 && list && lcs_nbsp != NUL)
1881 {
1882 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001883 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001884 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001885 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 p_extra = (char_u *)"";
1888 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001889 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 n_extra = 1;
1891 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001892 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 --s;
1894 }
1895 else if (c != NUL && (n = byte2cells(c)) > 1)
1896 {
1897 n_extra = n - 1;
1898 p_extra = transchar_byte(c);
1899 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001900 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001902 // Use special coloring to be able to distinguish <hex> from
1903 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001904 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else if (c == ' ' && trail != NULL && s > trail)
1907 {
1908 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001909 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001911 else if (c == ' ' && list && lcs_space != NUL)
1912 {
1913 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001914 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917
1918 if (c == NUL)
1919 break;
1920
1921 msg_putchar_attr(c, attr);
1922 col++;
1923 }
1924 msg_clr_eos();
1925}
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927/*
1928 * Use screen_puts() to output one multi-byte character.
1929 * Return the pointer "s" advanced to the next character.
1930 */
1931 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001932screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
1934 int cw;
1935
Bram Moolenaar85a20022019-12-21 18:25:54 +01001936 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 cw = (*mb_ptr2cells)(s);
1938 if (cw > 1 && (
1939#ifdef FEAT_RIGHTLEFT
1940 cmdmsg_rl ? msg_col <= 1 :
1941#endif
1942 msg_col == Columns - 1))
1943 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001944 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001945 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 return s;
1947 }
1948
1949 screen_puts_len(s, l, msg_row, msg_col, attr);
1950#ifdef FEAT_RIGHTLEFT
1951 if (cmdmsg_rl)
1952 {
1953 msg_col -= cw;
1954 if (msg_col == 0)
1955 {
1956 msg_col = Columns;
1957 ++msg_row;
1958 }
1959 }
1960 else
1961#endif
1962 {
1963 msg_col += cw;
1964 if (msg_col >= Columns)
1965 {
1966 msg_col = 0;
1967 ++msg_row;
1968 }
1969 }
1970 return s + l;
1971}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973/*
1974 * Output a string to the screen at position msg_row, msg_col.
1975 * Update msg_row and msg_col for the next message.
1976 */
1977 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001978msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001980 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981}
1982
1983 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001984msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001986 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987}
1988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989/*
1990 * Show a message in such a way that it always fits in the line. Cut out a
1991 * part in the middle and replace it with "..." when necessary.
1992 * Does not handle multi-byte characters!
1993 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001994 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001995msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 int slen = len;
1998 int room;
1999
2000 room = Columns - msg_col;
2001 if (len > room && room >= 20)
2002 {
2003 slen = (room - 3) / 2;
2004 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002005 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2008}
2009
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002010 void
2011msg_outtrans_long_attr(char_u *longstr, int attr)
2012{
2013 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2014}
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016/*
2017 * Basic function for writing a message with highlight attributes.
2018 */
2019 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002020msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
2022 msg_puts_attr_len(s, -1, attr);
2023}
2024
2025/*
2026 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2027 * When "maxlen" is -1 there is no maximum length.
2028 * When "maxlen" is >= 0 the message is not put in the history.
2029 */
2030 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002031msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 /*
2034 * If redirection is on, also write to the redirection file.
2035 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002036 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 /*
2039 * Don't print anything when using ":silent cmd".
2040 */
2041 if (msg_silent != 0)
2042 return;
2043
Bram Moolenaar85a20022019-12-21 18:25:54 +01002044 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if ((attr & MSG_HIST) && maxlen < 0)
2046 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002047 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 attr &= ~MSG_HIST;
2049 }
2050
Bram Moolenaare8070982019-10-12 17:07:06 +02002051 // When writing something to the screen after it has scrolled, requires a
2052 // wait-return prompt later. Needed when scrolling, resetting
2053 // need_wait_return after some prompt, and then outputting something
2054 // without scrolling
2055 // Not needed when only using CR to move the cursor.
2056 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002058 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
2060 /*
2061 * If there is no valid screen, use fprintf so we can see error messages.
2062 * If termcap is not active, we may be writing in an alternate console
2063 * window, cursor positioning may not work correctly (window size may be
2064 * different, e.g. for Win32 console) or we just don't know where the
2065 * cursor is.
2066 */
2067 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002068 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002069 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002070 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002073/*
2074 * The display part of msg_puts_attr_len().
2075 * May be called recursively to display scroll-back text.
2076 */
2077 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002078msg_puts_display(
2079 char_u *str,
2080 int maxlen,
2081 int attr,
2082 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002083{
2084 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002085 char_u *t_s = str; // string from "t_s" to "s" is still todo
2086 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002087 int l;
2088 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002089 char_u *sb_str = str;
2090 int sb_col = msg_col;
2091 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002092 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002095 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002098 * We are at the end of the screen line when:
2099 * - When outputting a newline.
2100 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002102 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#ifdef FEAT_RIGHTLEFT
2104 cmdmsg_rl
2105 ? (
2106 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002107 || (*s == TAB && msg_col <= 7)
2108 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 :
2110#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002111 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002114 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002116 /*
2117 * The screen is scrolled up when at the last row (some terminals
2118 * scroll automatically, some don't. To avoid problems we scroll
2119 * ourselves).
2120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002122 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002123 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
Bram Moolenaar85a20022019-12-21 18:25:54 +01002125 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (msg_no_more && lines_left == 0)
2127 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
Bram Moolenaar85a20022019-12-21 18:25:54 +01002129 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002130 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002133 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 msg_col = Columns - 1;
2135
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002137 if (*s >= ' '
2138#ifdef FEAT_RIGHTLEFT
2139 && !cmdmsg_rl
2140#endif
2141 )
2142 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002143 if (has_mbyte)
2144 {
2145 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002146 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002147 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002149 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002150 s = screen_puts_mbyte(s, l, attr);
2151 }
2152 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002153 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002154 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002155 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002156 else
2157 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002158
2159 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002160 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002161 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2162
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002163 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002164 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 redraw_cmdline = TRUE;
2166 if (cmdline_row > 0 && !exmode_active)
2167 --cmdline_row;
2168
2169 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002170 * If screen is completely filled and 'more' is set then wait
2171 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002173 if (lines_left > 0)
2174 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002175 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 && !msg_no_more && !exmode_active)
2177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002179 if (do_more_prompt(NUL))
2180 s = confirm_msg_tail;
2181#else
2182 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#endif
2184 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002185 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002187
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 // When we displayed a char in last column need to check if there
2189 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002190 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002191 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 }
2193
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002194 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002197 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002198 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2199 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002200 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002201 t_puts(&t_col, t_s, s, attr);
2202
2203 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002205 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
Bram Moolenaar85a20022019-12-21 18:25:54 +01002207 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002209 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002210#ifdef FEAT_RIGHTLEFT
2211 if (cmdmsg_rl)
2212 msg_col = Columns - 1;
2213 else
2214#endif
2215 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002216 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 msg_row = Rows - 1;
2218 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002219 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
2221 msg_col = 0;
2222 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002223 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 if (msg_col)
2226 --msg_col;
2227 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 do
2231 msg_screen_putchar(' ', attr);
2232 while (msg_col & 7);
2233 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002234 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002235 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 else
2237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 if (has_mbyte)
2239 {
2240 cw = (*mb_ptr2cells)(s);
2241 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002243 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002245 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 else
2248 {
2249 cw = 1;
2250 l = 1;
2251 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002252
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 // When drawing from right to left or when a double-wide character
2254 // doesn't fit, draw a single character here. Otherwise collect
2255 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (
2257# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002258 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002260 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (l > 1)
2263 s = screen_puts_mbyte(s, l, attr) - 1;
2264 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 msg_screen_putchar(*s, attr);
2266 }
2267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002269 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (t_col == 0)
2271 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 t_col += cw;
2273 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275 }
2276 ++s;
2277 }
2278
Bram Moolenaar85a20022019-12-21 18:25:54 +01002279 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002281 t_puts(&t_col, t_s, s, attr);
2282 if (p_more && !recurse)
2283 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 msg_check();
2286}
2287
2288/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002289 * Return TRUE when ":filter pattern" was used and "msg" does not match
2290 * "pattern".
2291 */
2292 int
2293message_filtered(char_u *msg)
2294{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002295 int match;
2296
2297 if (cmdmod.filter_regmatch.regprog == NULL)
2298 return FALSE;
2299 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2300 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002301}
2302
2303/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002304 * Scroll the screen up one line for displaying the next message line.
2305 */
2306 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002307msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002308{
2309#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002310 // Remove the cursor before scrolling, ScreenLines[] is going
2311 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002312 if (gui.in_use)
2313 gui_undraw_cursor();
2314#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002315 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002316 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002317 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002318 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002319
2320 if (!can_clear((char_u *)" "))
2321 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002322 // Scrolling up doesn't result in the right background. Set the
2323 // background here. It's not efficient, but avoids that we have to do
2324 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002325 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2326
Bram Moolenaar85a20022019-12-21 18:25:54 +01002327 // Also clear the last char of the last but one line if it was not
2328 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002329 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2330 screen_fill((int)Rows - 2, (int)Rows - 1,
2331 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2332 }
2333}
2334
2335/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002336 * Increment "msg_scrolled".
2337 */
2338 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002339inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002340{
2341#ifdef FEAT_EVAL
2342 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2343 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002344 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002345 char_u *tofree = NULL;
2346 int len;
2347
Bram Moolenaar85a20022019-12-21 18:25:54 +01002348 // v:scrollstart is empty, set it to the script/function name and line
2349 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002350 if (p == NULL)
2351 p = (char_u *)_("Unknown");
2352 else
2353 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002354 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002355 tofree = alloc(len);
2356 if (tofree != NULL)
2357 {
2358 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002359 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002360 p = tofree;
2361 }
2362 }
2363 set_vim_var_string(VV_SCROLLSTART, p, -1);
2364 vim_free(tofree);
2365 }
2366#endif
2367 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002368 if (must_redraw < VALID)
2369 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002370}
2371
2372/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002373 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2374 * store the displayed text and remember where screen lines start.
2375 */
2376typedef struct msgchunk_S msgchunk_T;
2377struct msgchunk_S
2378{
2379 msgchunk_T *sb_next;
2380 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002381 char sb_eol; // TRUE when line ends after this text
2382 int sb_msg_col; // column in which text starts
2383 int sb_attr; // text attributes
2384 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002385};
2386
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002388
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002389static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002390
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002391typedef enum {
2392 SB_CLEAR_NONE = 0,
2393 SB_CLEAR_ALL,
2394 SB_CLEAR_CMDLINE_BUSY,
2395 SB_CLEAR_CMDLINE_DONE
2396} sb_clear_T;
2397
Bram Moolenaar85a20022019-12-21 18:25:54 +01002398// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002399static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002400
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002401/*
2402 * Store part of a printed message for displaying when scrolling back.
2403 */
2404 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002405store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002406 char_u **sb_str, // start of string
2407 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002408 int attr,
2409 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002410 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002411{
2412 msgchunk_T *mp;
2413
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002414 if (do_clear_sb_text == SB_CLEAR_ALL
2415 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002416 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002417 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2418 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002419 }
2420
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002421 if (s > *sb_str)
2422 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002423 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002424 if (mp != NULL)
2425 {
2426 mp->sb_eol = finish;
2427 mp->sb_msg_col = *sb_col;
2428 mp->sb_attr = attr;
2429 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2430
2431 if (last_msgchunk == NULL)
2432 {
2433 last_msgchunk = mp;
2434 mp->sb_prev = NULL;
2435 }
2436 else
2437 {
2438 mp->sb_prev = last_msgchunk;
2439 last_msgchunk->sb_next = mp;
2440 last_msgchunk = mp;
2441 }
2442 mp->sb_next = NULL;
2443 }
2444 }
2445 else if (finish && last_msgchunk != NULL)
2446 last_msgchunk->sb_eol = TRUE;
2447
2448 *sb_str = s;
2449 *sb_col = 0;
2450}
2451
2452/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002453 * Finished showing messages, clear the scroll-back text on the next message.
2454 */
2455 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002456may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002457{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002458 do_clear_sb_text = SB_CLEAR_ALL;
2459}
2460
2461/*
2462 * Starting to edit the command line, do not clear messages now.
2463 */
2464 void
2465sb_text_start_cmdline(void)
2466{
2467 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2468 msg_sb_eol();
2469}
2470
2471/*
2472 * Ending to edit the command line. Clear old lines but the last one later.
2473 */
2474 void
2475sb_text_end_cmdline(void)
2476{
2477 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002478}
2479
2480/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002481 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002482 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002483 * Called when redrawing the screen.
2484 */
2485 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002486clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002487{
2488 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002489 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002490
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002491 if (all)
2492 lastp = &last_msgchunk;
2493 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002494 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002495 if (last_msgchunk == NULL)
2496 return;
2497 lastp = &last_msgchunk->sb_prev;
2498 }
2499
2500 while (*lastp != NULL)
2501 {
2502 mp = (*lastp)->sb_prev;
2503 vim_free(*lastp);
2504 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506}
2507
2508/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002509 * "g<" command.
2510 */
2511 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002512show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002513{
2514 msgchunk_T *mp;
2515
Bram Moolenaar85a20022019-12-21 18:25:54 +01002516 // Only show something if there is more than one line, otherwise it looks
2517 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002518 mp = msg_sb_start(last_msgchunk);
2519 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002520 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002521 else
2522 {
2523 do_more_prompt('G');
2524 wait_return(FALSE);
2525 }
2526}
2527
2528/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002529 * Move to the start of screen line in already displayed text.
2530 */
2531 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002532msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002533{
2534 msgchunk_T *mp = mps;
2535
2536 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2537 mp = mp->sb_prev;
2538 return mp;
2539}
2540
2541/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002542 * Mark the last message chunk as finishing the line.
2543 */
2544 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002545msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002546{
2547 if (last_msgchunk != NULL)
2548 last_msgchunk->sb_eol = TRUE;
2549}
2550
2551/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002552 * Display a screen line from previously displayed text at row "row".
2553 * Returns a pointer to the text for the next line (can be NULL).
2554 */
2555 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002556disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002557{
2558 msgchunk_T *mp = smp;
2559 char_u *p;
2560
2561 for (;;)
2562 {
2563 msg_row = row;
2564 msg_col = mp->sb_msg_col;
2565 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002566 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002567 ++p;
2568 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2569 if (mp->sb_eol || mp->sb_next == NULL)
2570 break;
2571 mp = mp->sb_next;
2572 }
2573 return mp->sb_next;
2574}
2575
2576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 * Output any postponed text for msg_puts_attr_len().
2578 */
2579 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002580t_puts(
2581 int *t_col,
2582 char_u *t_s,
2583 char_u *s,
2584 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002586 // output postponed text
2587 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002589 msg_col += *t_col;
2590 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002591 // If the string starts with a composing character don't increment the
2592 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2594 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (msg_col >= Columns)
2596 {
2597 msg_col = 0;
2598 ++msg_row;
2599 }
2600}
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602/*
2603 * Returns TRUE when messages should be printed with mch_errmsg().
2604 * This is used when there is no valid screen, so we can see error messages.
2605 * If termcap is not active, we may be writing in an alternate console
2606 * window, cursor positioning may not work correctly (window size may be
2607 * different, e.g. for Win32 console) or we just don't know where the
2608 * cursor is.
2609 */
2610 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002611msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
2613 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002614#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2615# ifdef VIMDLL
2616 || (!gui.in_use && !termcap_active)
2617# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#endif
2621 || (swapping_screen() && !termcap_active)
2622 );
2623}
2624
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002625/*
2626 * Print a message when there is no valid screen.
2627 */
2628 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002629msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002630{
2631 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002632 char_u *buf = NULL;
2633 char_u *p = s;
2634
Bram Moolenaar4f974752019-02-17 17:44:42 +01002635#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002636 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002637 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002638#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002639 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002640 {
2641 if (!(silent_mode && p_verbose == 0))
2642 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002643 // NL --> CR NL translation (for Unix, not for "--version")
2644 if (*s == NL)
2645 {
2646 int n = (int)(s - p);
2647
2648 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002649 if (buf != NULL)
2650 {
2651 memcpy(buf, p, n);
2652 if (!info_message)
2653 buf[n++] = CAR;
2654 buf[n++] = NL;
2655 buf[n++] = NUL;
2656 if (info_message) // informative message, not an error
2657 mch_msg((char *)buf);
2658 else
2659 mch_errmsg((char *)buf);
2660 vim_free(buf);
2661 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002662 p = s + 1;
2663 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002664 }
2665
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002666 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002667#ifdef FEAT_RIGHTLEFT
2668 if (cmdmsg_rl)
2669 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002670 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002671 msg_col = Columns - 1;
2672 else
2673 --msg_col;
2674 }
2675 else
2676#endif
2677 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002678 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002679 msg_col = 0;
2680 else
2681 ++msg_col;
2682 }
2683 ++s;
2684 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002685
2686 if (*p != NUL && !(silent_mode && p_verbose == 0))
2687 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002688 int c = -1;
2689
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002690 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002691 {
2692 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002693 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002694 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002695 if (info_message)
2696 mch_msg((char *)p);
2697 else
2698 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002699 if (c != -1)
2700 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002701 }
2702
2703 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002704
Bram Moolenaar4f974752019-02-17 17:44:42 +01002705#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002706 if (!(silent_mode && p_verbose == 0))
2707 mch_settmode(TMODE_RAW);
2708#endif
2709}
2710
2711/*
2712 * Show the more-prompt and handle the user response.
2713 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002714 * When at hit-enter prompt "typed_char" is the already typed character,
2715 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002716 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2717 */
2718 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002719do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002720{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002721 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 int used_typed_char = typed_char;
2723 int oldState = State;
2724 int c;
2725#ifdef FEAT_CON_DIALOG
2726 int retval = FALSE;
2727#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002728 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729 msgchunk_T *mp_last = NULL;
2730 msgchunk_T *mp;
2731 int i;
2732
Bram Moolenaar85a20022019-12-21 18:25:54 +01002733 // We get called recursively when a timer callback outputs a message. In
2734 // that case don't show another prompt. Also when at the hit-Enter prompt
2735 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002736 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002737 return FALSE;
2738 entered = TRUE;
2739
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002740 if (typed_char == 'G')
2741 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002742 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002743 mp_last = msg_sb_start(last_msgchunk);
2744 for (i = 0; i < Rows - 2 && mp_last != NULL
2745 && mp_last->sb_prev != NULL; ++i)
2746 mp_last = msg_sb_start(mp_last->sb_prev);
2747 }
2748
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002749 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002750 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002751 if (typed_char == NUL)
2752 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002753 for (;;)
2754 {
2755 /*
2756 * Get a typed character directly from the user.
2757 */
2758 if (used_typed_char != NUL)
2759 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002760 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002761 used_typed_char = NUL;
2762 }
2763 else
2764 c = get_keystroke();
2765
2766#if defined(FEAT_MENU) && defined(FEAT_GUI)
2767 if (c == K_MENU)
2768 {
2769 int idx = get_menu_index(current_menu, ASKMORE);
2770
Bram Moolenaar85a20022019-12-21 18:25:54 +01002771 // Used a menu. If it starts with CTRL-Y, it must
2772 // be a "Copy" for the clipboard. Otherwise
2773 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002774 if (idx == MENU_INDEX_INVALID)
2775 continue;
2776 c = *current_menu->strings[idx];
2777 if (c != NUL && current_menu->strings[idx][1] != NUL)
2778 ins_typebuf(current_menu->strings[idx] + 1,
2779 current_menu->noremap[idx], 0, TRUE,
2780 current_menu->silent[idx]);
2781 }
2782#endif
2783
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002784 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 switch (c)
2786 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002787 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 case K_BS:
2789 case 'k':
2790 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002791 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002792 break;
2793
Bram Moolenaar85a20022019-12-21 18:25:54 +01002794 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 case NL:
2796 case 'j':
2797 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002798 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 break;
2800
Bram Moolenaar85a20022019-12-21 18:25:54 +01002801 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002802 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002803 break;
2804
Bram Moolenaar85a20022019-12-21 18:25:54 +01002805 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002806 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 break;
2808
Bram Moolenaar85a20022019-12-21 18:25:54 +01002809 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002810 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002811 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002812 break;
2813
Bram Moolenaar85a20022019-12-21 18:25:54 +01002814 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002815 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 case K_PAGEDOWN:
2817 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002818 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819 break;
2820
Bram Moolenaar85a20022019-12-21 18:25:54 +01002821 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002822 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002823 break;
2824
Bram Moolenaar85a20022019-12-21 18:25:54 +01002825 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002826 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002827 lines_left = 999999;
2828 break;
2829
Bram Moolenaar85a20022019-12-21 18:25:54 +01002830 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002831#ifdef FEAT_CON_DIALOG
2832 if (!confirm_msg_used)
2833#endif
2834 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002835 // Since got_int is set all typeahead will be flushed, but we
2836 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002837 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002838#ifdef FEAT_TERMINAL
2839 skip_term_loop = TRUE;
2840#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002841 cmdline_row = Rows - 1; // put ':' on this line
2842 skip_redraw = TRUE; // skip redraw once
2843 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002844 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002845 // FALLTHROUGH
2846 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002847 case Ctrl_C:
2848 case ESC:
2849#ifdef FEAT_CON_DIALOG
2850 if (confirm_msg_used)
2851 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002852 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002853 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002854 }
2855 else
2856#endif
2857 {
2858 got_int = TRUE;
2859 quit_more = TRUE;
2860 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002861 // When there is some more output (wrapping line) display that
2862 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002863 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002864 break;
2865
2866#ifdef FEAT_CLIPBOARD
2867 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 // Strange way to allow copying (yanking) a modeless
2869 // selection at the more prompt. Use CTRL-Y,
2870 // because the same is used in Cmdline-mode and at the
2871 // hit-enter prompt. However, scrolling one line up
2872 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 if (clip_star.state == SELECT_DONE)
2874 clip_copy_modeless_selection(TRUE);
2875 continue;
2876#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002877 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 msg_moremsg(TRUE);
2879 continue;
2880 }
2881
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002882 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002884 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002886 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002887 if (mp_last == NULL)
2888 mp = msg_sb_start(last_msgchunk);
2889 else if (mp_last->sb_prev != NULL)
2890 mp = msg_sb_start(mp_last->sb_prev);
2891 else
2892 mp = NULL;
2893
Bram Moolenaar85a20022019-12-21 18:25:54 +01002894 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002895 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2896 ++i)
2897 mp = msg_sb_start(mp->sb_prev);
2898
2899 if (mp != NULL && mp->sb_prev != NULL)
2900 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002901 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002902 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002903 {
2904 if (mp == NULL || mp->sb_prev == NULL)
2905 break;
2906 mp = msg_sb_start(mp->sb_prev);
2907 if (mp_last == NULL)
2908 mp_last = msg_sb_start(last_msgchunk);
2909 else
2910 mp_last = msg_sb_start(mp_last->sb_prev);
2911 }
2912
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002913 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002914 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002915 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002916 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002917 (void)disp_sb_line(0, mp);
2918 }
2919 else
2920 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002921 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002922 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002923 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2924 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002925 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002926 ++msg_scrolled;
2927 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002929 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002930 }
2931 }
2932 else
2933 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002935 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002937 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002939 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002940 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2941 (int)Columns, ' ', ' ', 0);
2942 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002943 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 }
2945 }
2946
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002947 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002949 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002950 screen_fill((int)Rows - 1, (int)Rows, 0,
2951 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002952 msg_moremsg(FALSE);
2953 continue;
2954 }
2955
Bram Moolenaar85a20022019-12-21 18:25:54 +01002956 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002957 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002958 }
2959
2960 break;
2961 }
2962
Bram Moolenaar85a20022019-12-21 18:25:54 +01002963 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002964 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2965 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002967 if (quit_more)
2968 {
2969 msg_row = Rows - 1;
2970 msg_col = 0;
2971 }
2972#ifdef FEAT_RIGHTLEFT
2973 else if (cmdmsg_rl)
2974 msg_col = Columns - 1;
2975#endif
2976
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002977 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978#ifdef FEAT_CON_DIALOG
2979 return retval;
2980#else
2981 return FALSE;
2982#endif
2983}
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2986
2987#ifdef mch_errmsg
2988# undef mch_errmsg
2989#endif
2990#ifdef mch_msg
2991# undef mch_msg
2992#endif
2993
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002994#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2995 static void
2996mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01002998 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002999 DWORD nwrite = 0;
3000 DWORD mode = 0;
3001 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3002
3003 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3004 && (int)GetConsoleCP() != enc_codepage)
3005 {
3006 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3007
3008 WriteConsoleW(h, w, len, &nwrite, NULL);
3009 vim_free(w);
3010 }
3011 else
3012 {
3013 fprintf(stderr, "%s", str);
3014 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003015}
3016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003018/*
3019 * Give an error message. To be used when the screen hasn't been initialized
3020 * yet. When stderr can't be used, collect error messages until the GUI has
3021 * started and they can be displayed in a message box.
3022 */
3023 void
3024mch_errmsg(char *str)
3025{
3026#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3027 int len;
3028#endif
3029
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003030#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003031 // On Unix use stderr if it's a tty.
3032 // When not going to start the GUI also use stderr.
3033 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003035# ifdef UNIX
3036# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003038# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 isatty(2)
3040# endif
3041# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003042 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003043# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003044# endif
3045# ifdef FEAT_GUI
3046 !(gui.in_use || gui.starting)
3047# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 )
3049 {
3050 fprintf(stderr, "%s", str);
3051 return;
3052 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003055#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3056# ifdef VIMDLL
3057 if (!(gui.in_use || gui.starting))
3058# endif
3059 {
3060 mch_errmsg_c(str);
3061 return;
3062 }
3063#endif
3064
3065#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003066 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 emsg_on_display = FALSE;
3068
3069 len = (int)STRLEN(str) + 1;
3070 if (error_ga.ga_growsize == 0)
3071 {
3072 error_ga.ga_growsize = 80;
3073 error_ga.ga_itemsize = 1;
3074 }
3075 if (ga_grow(&error_ga, len) == OK)
3076 {
3077 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3078 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003079# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003080 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 {
3082 char_u *p;
3083
3084 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3085 for (;;)
3086 {
3087 p = vim_strchr(p, '\r');
3088 if (p == NULL)
3089 break;
3090 *p = ' ';
3091 }
3092 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003093# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003094 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
3099
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003100#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3101 static void
3102mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003104 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003105 DWORD nwrite = 0;
3106 DWORD mode;
3107 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3108
3109
3110 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3111 && (int)GetConsoleCP() != enc_codepage)
3112 {
3113 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3114
3115 WriteConsoleW(h, w, len, &nwrite, NULL);
3116 vim_free(w);
3117 }
3118 else
3119 {
3120 printf("%s", str);
3121 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003122}
3123#endif
3124
3125/*
3126 * Give a message. To be used when the screen hasn't been initialized yet.
3127 * When there is no tty, collect messages until the GUI has started and they
3128 * can be displayed in a message box.
3129 */
3130 void
3131mch_msg(char *str)
3132{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003133#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003134 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3135 // uses mch_errmsg() when started from the desktop.
3136 // When not going to start the GUI also use stdout.
3137 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003139# ifdef UNIX
3140# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003142# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003146 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003148# endif
3149# ifdef FEAT_GUI
3150 !(gui.in_use || gui.starting)
3151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 )
3153 {
3154 printf("%s", str);
3155 return;
3156 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003157#endif
3158
3159#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3160# ifdef VIMDLL
3161 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003163 {
3164 mch_msg_c(str);
3165 return;
3166 }
3167#endif
3168#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003172#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174/*
3175 * Put a character on the screen at the current message position and advance
3176 * to the next position. Only for printable ASCII!
3177 */
3178 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003179msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003181 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 screen_putchar(c, msg_row, msg_col, attr);
3183#ifdef FEAT_RIGHTLEFT
3184 if (cmdmsg_rl)
3185 {
3186 if (--msg_col == 0)
3187 {
3188 msg_col = Columns;
3189 ++msg_row;
3190 }
3191 }
3192 else
3193#endif
3194 {
3195 if (++msg_col >= Columns)
3196 {
3197 msg_col = 0;
3198 ++msg_row;
3199 }
3200 }
3201}
3202
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003203 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003204msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003206 int attr;
3207 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar8820b482017-03-16 17:23:31 +01003209 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003210 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003212 screen_puts((char_u *)
3213 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3214 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215}
3216
3217/*
3218 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3219 * exmode_active.
3220 */
3221 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 if (State == ASKMORE)
3225 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003226 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 msg_row = Rows - 1;
3228 }
3229#ifdef FEAT_CON_DIALOG
3230 else if (State == CONFIRM)
3231 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003232 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 msg_row = Rows - 1;
3234 }
3235#endif
3236 else if (State == EXTERNCMD)
3237 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240 else if (State == HITRETURN || State == SETWSIZE)
3241 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003242 if (msg_row == Rows - 1)
3243 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003244 // Avoid drawing the "hit-enter" prompt below the previous one,
3245 // overwrite it. Esp. useful when regaining focus and a
3246 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003247 msg_didout = FALSE;
3248 msg_col = 0;
3249 msg_clr_eos();
3250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 hit_return_msg();
3252 msg_row = Rows - 1;
3253 }
3254}
3255
3256/*
3257 * msg_check_screen - check if the screen is initialized.
3258 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3259 * While starting the GUI the terminal codes will be set for the GUI, but the
3260 * output goes to the terminal. Don't use the terminal codes then.
3261 */
3262 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003263msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 if (!full_screen || !screen_valid(FALSE))
3266 return FALSE;
3267
3268 if (msg_row >= Rows)
3269 msg_row = Rows - 1;
3270 if (msg_col >= Columns)
3271 msg_col = Columns - 1;
3272 return TRUE;
3273}
3274
3275/*
3276 * Clear from current message position to end of screen.
3277 * Skip this when ":silent" was used, no need to clear for redirection.
3278 */
3279 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003280msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 if (msg_silent == 0)
3283 msg_clr_eos_force();
3284}
3285
3286/*
3287 * Clear from current message position to end of screen.
3288 * Note: msg_col is not updated, so we remember the end of the message
3289 * for msg_check().
3290 */
3291 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003292msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 if (msg_use_printf())
3295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003296 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003299 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003301 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303 }
3304 else
3305 {
3306#ifdef FEAT_RIGHTLEFT
3307 if (cmdmsg_rl)
3308 {
3309 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3310 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3311 }
3312 else
3313#endif
3314 {
3315 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3316 ' ', ' ', 0);
3317 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3318 }
3319 }
3320}
3321
3322/*
3323 * Clear the command line.
3324 */
3325 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003326msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 msg_row = cmdline_row;
3329 msg_col = 0;
3330 msg_clr_eos_force();
3331}
3332
3333/*
3334 * end putting a message on the screen
3335 * call wait_return if the message does not fit in the available space
3336 * return TRUE if wait_return not called.
3337 */
3338 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003339msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003342 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 * or the ruler option is set and we run into it,
3344 * we have to redraw the window.
3345 * Do not do this if we are abandoning the file or editing the command line.
3346 */
3347 if (!exiting && need_wait_return && !(State & CMDLINE))
3348 {
3349 wait_return(FALSE);
3350 return FALSE;
3351 }
3352 out_flush();
3353 return TRUE;
3354}
3355
3356/*
3357 * If the written message runs into the shown command or ruler, we have to
3358 * wait for hit-return and redraw the window later.
3359 */
3360 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003361msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 if (msg_row == Rows - 1 && msg_col >= sc_col)
3364 {
3365 need_wait_return = TRUE;
3366 redraw_cmdline = TRUE;
3367 }
3368}
3369
3370/*
3371 * May write a string to the redirection file.
3372 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3373 */
3374 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003375redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 char_u *s = str;
3378 static int cur_col = 0;
3379
Bram Moolenaar85a20022019-12-21 18:25:54 +01003380 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003381 if (redir_off)
3382 return;
3383
Bram Moolenaar85a20022019-12-21 18:25:54 +01003384 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003385 if (*p_vfile != NUL && verbose_fd == NULL)
3386 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003387
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003388 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003390 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (*s != '\n' && *s != '\r')
3392 {
3393 while (cur_col < msg_col)
3394 {
3395#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003396 if (redir_execute)
3397 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003398 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003400 else if (redir_vname)
3401 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003404 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003406 if (verbose_fd != NULL)
3407 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 ++cur_col;
3409 }
3410 }
3411
3412#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003413 if (redir_execute)
3414 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003415 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003417 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003418 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#endif
3420
Bram Moolenaar85a20022019-12-21 18:25:54 +01003421 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3423 {
3424#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003425 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003427 if (redir_fd != NULL)
3428 putc(*s, redir_fd);
3429 if (verbose_fd != NULL)
3430 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (*s == '\r' || *s == '\n')
3432 cur_col = 0;
3433 else if (*s == '\t')
3434 cur_col += (8 - cur_col % 8);
3435 else
3436 ++cur_col;
3437 ++s;
3438 }
3439
Bram Moolenaar85a20022019-12-21 18:25:54 +01003440 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 msg_col = cur_col;
3442 }
3443}
3444
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003445 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003446redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003447{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003448 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003449#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003450 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003451#endif
3452 ;
3453}
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003456 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003457 * Must always be called paired with verbose_leave()!
3458 */
3459 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003460verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003461{
3462 if (*p_vfile != NUL)
3463 ++msg_silent;
3464}
3465
3466/*
3467 * After giving verbose message.
3468 * Must always be called paired with verbose_enter()!
3469 */
3470 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003471verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003472{
3473 if (*p_vfile != NUL)
3474 if (--msg_silent < 0)
3475 msg_silent = 0;
3476}
3477
3478/*
3479 * Like verbose_enter() and set msg_scroll when displaying the message.
3480 */
3481 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003482verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003483{
3484 if (*p_vfile != NUL)
3485 ++msg_silent;
3486 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003487 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003488 msg_scroll = TRUE;
3489}
3490
3491/*
3492 * Like verbose_leave() and set cmdline_row when displaying the message.
3493 */
3494 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003495verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003496{
3497 if (*p_vfile != NUL)
3498 {
3499 if (--msg_silent < 0)
3500 msg_silent = 0;
3501 }
3502 else
3503 cmdline_row = msg_row;
3504}
3505
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003506/*
3507 * Called when 'verbosefile' is set: stop writing to the file.
3508 */
3509 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003510verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003511{
3512 if (verbose_fd != NULL)
3513 {
3514 fclose(verbose_fd);
3515 verbose_fd = NULL;
3516 }
3517 verbose_did_open = FALSE;
3518}
3519
3520/*
3521 * Open the file 'verbosefile'.
3522 * Return FAIL or OK.
3523 */
3524 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003525verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003526{
3527 if (verbose_fd == NULL && !verbose_did_open)
3528 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003529 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003530 verbose_did_open = TRUE;
3531
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003532 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003533 if (verbose_fd == NULL)
3534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003535 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003536 return FAIL;
3537 }
3538 }
3539 return OK;
3540}
3541
3542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 * Give a warning message (for searching).
3544 * Use 'w' highlighting and may repeat the message after redrawing
3545 */
3546 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003547give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003549 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (msg_silent != 0)
3551 return;
3552
Bram Moolenaar85a20022019-12-21 18:25:54 +01003553 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#ifdef FEAT_EVAL
3557 set_vim_var_string(VV_WARNINGMSG, message, -1);
3558#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003559 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003561 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
3563 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003564 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003565 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003566 msg_didout = FALSE; // overwrite this message
3567 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 --no_wait_return;
3571}
3572
Bram Moolenaar113e1072019-01-20 15:30:40 +01003573#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003574 void
3575give_warning2(char_u *message, char_u *a1, int hl)
3576{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003577 if (IObuff == NULL)
3578 {
3579 // Very early in initialisation and already something wrong, just give
3580 // the raw message so the user at least gets a hint.
3581 give_warning((char_u *)message, hl);
3582 }
3583 else
3584 {
3585 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3586 give_warning(IObuff, hl);
3587 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003588}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003589#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003590
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591/*
3592 * Advance msg cursor to column "col".
3593 */
3594 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003595msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003597 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003599 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 return;
3601 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003602 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003604#ifdef FEAT_RIGHTLEFT
3605 if (cmdmsg_rl)
3606 while (msg_col > Columns - col)
3607 msg_putchar(' ');
3608 else
3609#endif
3610 while (msg_col < col)
3611 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612}
3613
3614#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3615/*
3616 * Used for "confirm()" function, and the :confirm command prefix.
3617 * Versions which haven't got flexible dialogs yet, and console
3618 * versions, get this generic handler which uses the command line.
3619 *
3620 * type = one of:
3621 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3622 * title = title string (can be NULL for default)
3623 * (neither used in console dialogs at the moment)
3624 *
3625 * Format of the "buttons" string:
3626 * "Button1Name\nButton2Name\nButton3Name"
3627 * The first button should normally be the default/accept
3628 * The second button should be the 'Cancel' button
3629 * Other buttons- use your imagination!
3630 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3631 * different letter.
3632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003634do_dialog(
3635 int type UNUSED,
3636 char_u *title UNUSED,
3637 char_u *message,
3638 char_u *buttons,
3639 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003640 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3641 // otherwise
3642 int ex_cmd) // when TRUE pressing : accepts default and starts
3643 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 int oldState;
3646 int retval = 0;
3647 char_u *hotkeys;
3648 int c;
3649 int i;
3650
3651#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003652 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003654 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655#endif
3656
3657#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003658 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3660 {
3661 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003662 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003663 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003664 need_wait_return = FALSE;
3665 emsg_on_display = FALSE;
3666 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
Bram Moolenaar85a20022019-12-21 18:25:54 +01003668 // Flush output to avoid that further messages and redrawing is done
3669 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 out_flush();
3671 gui_mch_update();
3672
3673 return c;
3674 }
3675#endif
3676
3677 oldState = State;
3678 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
3681 /*
3682 * Since we wait for a keypress, don't make the
3683 * user press RETURN as well afterwards.
3684 */
3685 ++no_wait_return;
3686 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3687
3688 if (hotkeys != NULL)
3689 {
3690 for (;;)
3691 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003692 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 c = get_keystroke();
3694 switch (c)
3695 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003696 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 case NL:
3698 retval = dfltbutton;
3699 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003700 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 case ESC:
3702 retval = 0;
3703 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003704 default: // Could be a hotkey?
3705 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003707 if (c == ':' && ex_cmd)
3708 {
3709 retval = dfltbutton;
3710 ins_char_typebuf(':');
3711 break;
3712 }
3713
Bram Moolenaar85a20022019-12-21 18:25:54 +01003714 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 c = MB_TOLOWER(c);
3716 retval = 1;
3717 for (i = 0; hotkeys[i]; ++i)
3718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (has_mbyte)
3720 {
3721 if ((*mb_ptr2char)(hotkeys + i) == c)
3722 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003723 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (hotkeys[i] == c)
3727 break;
3728 ++retval;
3729 }
3730 if (hotkeys[i])
3731 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003732 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 continue;
3734 }
3735 break;
3736 }
3737
3738 vim_free(hotkeys);
3739 }
3740
3741 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 --no_wait_return;
3744 msg_end_prompt();
3745
3746 return retval;
3747}
3748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749/*
3750 * Copy one character from "*from" to "*to", taking care of multi-byte
3751 * characters. Return the length of the character in bytes.
3752 */
3753 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003754copy_char(
3755 char_u *from,
3756 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003757 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 int len;
3760 int c;
3761
3762 if (has_mbyte)
3763 {
3764 if (lowercase)
3765 {
3766 c = MB_TOLOWER((*mb_ptr2char)(from));
3767 return (*mb_char2bytes)(c, to);
3768 }
3769 else
3770 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003771 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 mch_memmove(to, from, (size_t)len);
3773 return len;
3774 }
3775 }
3776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
3778 if (lowercase)
3779 *to = (char_u)TOLOWER_LOC(*from);
3780 else
3781 *to = *from;
3782 return 1;
3783 }
3784}
3785
3786/*
3787 * Format the dialog string, and display it at the bottom of
3788 * the screen. Return a string of hotkey chars (if defined) for
3789 * each 'button'. If a button has no hotkey defined, the first character of
3790 * the button is used.
3791 * The hotkeys can be multi-byte characters, but without combining chars.
3792 *
3793 * Returns an allocated string with hotkeys, or NULL for error.
3794 */
3795 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003796msg_show_console_dialog(
3797 char_u *message,
3798 char_u *buttons,
3799 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003802#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003803 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 char_u *hotk = NULL;
3805 char_u *msgp = NULL;
3806 char_u *hotkp = NULL;
3807 char_u *r;
3808 int copy;
3809#define HAS_HOTKEY_LEN 30
3810 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003811 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 int idx;
3813
3814 has_hotkey[0] = FALSE;
3815
3816 /*
3817 * First loop: compute the size of memory to allocate.
3818 * Second loop: copy to the allocated memory.
3819 */
3820 for (copy = 0; copy <= 1; ++copy)
3821 {
3822 r = buttons;
3823 idx = 0;
3824 while (*r)
3825 {
3826 if (*r == DLG_BUTTON_SEP)
3827 {
3828 if (copy)
3829 {
3830 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003831 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Bram Moolenaar85a20022019-12-21 18:25:54 +01003833 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003835 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003838 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 if (dfltbutton)
3840 --dfltbutton;
3841
Bram Moolenaar85a20022019-12-21 18:25:54 +01003842 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3844 first_hotkey = TRUE;
3845 }
3846 else
3847 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003848 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3849 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (idx < HAS_HOTKEY_LEN - 1)
3851 has_hotkey[++idx] = FALSE;
3852 }
3853 }
3854 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3855 {
3856 if (*r == DLG_HOTKEY_CHAR)
3857 ++r;
3858 first_hotkey = FALSE;
3859 if (copy)
3860 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003861 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 *msgp++ = *r;
3863 else
3864 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003865 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3867 msgp += copy_char(r, msgp, FALSE);
3868 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3869
Bram Moolenaar85a20022019-12-21 18:25:54 +01003870 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003871 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873 }
3874 else
3875 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003876 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (idx < HAS_HOTKEY_LEN - 1)
3878 has_hotkey[idx] = TRUE;
3879 }
3880 }
3881 else
3882 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003883 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (copy)
3885 msgp += copy_char(r, msgp, FALSE);
3886 }
3887
Bram Moolenaar85a20022019-12-21 18:25:54 +01003888 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003889 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891
3892 if (copy)
3893 {
3894 *msgp++ = ':';
3895 *msgp++ = ' ';
3896 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898 else
3899 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003900 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003901 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003902 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003903 + 3); // for the ": " and NUL
3904 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaar85a20022019-12-21 18:25:54 +01003906 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (!has_hotkey[0])
3908 {
3909 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003910 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912
3913 /*
3914 * Now allocate and load the strings
3915 */
3916 vim_free(confirm_msg);
3917 confirm_msg = alloc(len);
3918 if (confirm_msg == NULL)
3919 return NULL;
3920 *confirm_msg = NUL;
3921 hotk = alloc(lenhotkey);
3922 if (hotk == NULL)
3923 return NULL;
3924
3925 *confirm_msg = '\n';
3926 STRCPY(confirm_msg + 1, message);
3927
3928 msgp = confirm_msg + 1 + STRLEN(message);
3929 hotkp = hotk;
3930
Bram Moolenaar85a20022019-12-21 18:25:54 +01003931 // Define first default hotkey. Keep the hotkey string NUL
3932 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003933 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
Bram Moolenaar85a20022019-12-21 18:25:54 +01003935 // Remember where the choices start, displaying starts here when
3936 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 confirm_msg_tail = msgp;
3938 *msgp++ = '\n';
3939 }
3940 }
3941
3942 display_confirm_msg();
3943 return hotk;
3944}
3945
3946/*
3947 * Display the ":confirm" message. Also called when screen resized.
3948 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003949 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003950display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003952 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 ++confirm_msg_used;
3954 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003955 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 --confirm_msg_used;
3957}
3958
Bram Moolenaar85a20022019-12-21 18:25:54 +01003959#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
3961#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3962
3963 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003964vim_dialog_yesno(
3965 int type,
3966 char_u *title,
3967 char_u *message,
3968 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
3970 if (do_dialog(type,
3971 title == NULL ? (char_u *)_("Question") : title,
3972 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003973 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 return VIM_YES;
3975 return VIM_NO;
3976}
3977
3978 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003979vim_dialog_yesnocancel(
3980 int type,
3981 char_u *title,
3982 char_u *message,
3983 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
3985 switch (do_dialog(type,
3986 title == NULL ? (char_u *)_("Question") : title,
3987 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003988 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 case 1: return VIM_YES;
3991 case 2: return VIM_NO;
3992 }
3993 return VIM_CANCEL;
3994}
3995
3996 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003997vim_dialog_yesnoallcancel(
3998 int type,
3999 char_u *title,
4000 char_u *message,
4001 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 switch (do_dialog(type,
4004 title == NULL ? (char_u *)"Question" : title,
4005 message,
4006 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004007 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 case 1: return VIM_YES;
4010 case 2: return VIM_NO;
4011 case 3: return VIM_ALL;
4012 case 4: return VIM_DISCARDALL;
4013 }
4014 return VIM_CANCEL;
4015}
4016
Bram Moolenaar85a20022019-12-21 18:25:54 +01004017#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004019#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004020static char *e_printf = N_("E766: Insufficient arguments for printf()");
4021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004022/*
4023 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4024 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004025 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004026tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004027{
4028 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004029 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004030 int err = FALSE;
4031
4032 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004033 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004034 else
4035 {
4036 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004037 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004038 if (err)
4039 n = 0;
4040 }
4041 return n;
4042}
4043
4044/*
4045 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004046 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004047 * are not converted to a string.
4048 * If "tofree" is not NULL echo_string() is used. All types are converted to
4049 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004050 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004051 */
4052 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004053tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004055 int idx = *idxp - 1;
4056 char *s = NULL;
4057 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058
4059 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004060 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004061 else
4062 {
4063 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004064 if (tofree != NULL)
4065 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4066 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004067 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004068 }
4069 return s;
4070}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004071
4072# ifdef FEAT_FLOAT
4073/*
4074 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4075 */
4076 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004077tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004078{
4079 int idx = *idxp - 1;
4080 double f = 0;
4081
4082 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004083 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004084 else
4085 {
4086 ++*idxp;
4087 if (tvs[idx].v_type == VAR_FLOAT)
4088 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004089 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004090 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004091 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004092 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004093 }
4094 return f;
4095}
4096# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004097#endif
4098
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004099#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004100/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004101 * Return the representation of infinity for printf() function:
4102 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4103 */
4104 static const char *
4105infinity_str(int positive,
4106 char fmt_spec,
4107 int force_sign,
4108 int space_for_positive)
4109{
4110 static const char *table[] =
4111 {
4112 "-inf", "inf", "+inf", " inf",
4113 "-INF", "INF", "+INF", " INF"
4114 };
4115 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4116
4117 if (ASCII_ISUPPER(fmt_spec))
4118 idx += 4;
4119 return table[idx];
4120}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004121#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004122
4123/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004124 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004125 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004126 * consistency.
4127 *
4128 * This code is based on snprintf.c - a portable implementation of snprintf
4129 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004130 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004131 * The original code, including useful comments, can be found here:
4132 * http://www.ijs.si/software/snprintf/
4133 *
4134 * This snprintf() only supports the following conversion specifiers:
4135 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4136 * with flags: '-', '+', ' ', '0' and '#'.
4137 * An asterisk is supported for field width as well as precision.
4138 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004139 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004140 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004141 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004142 * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004143 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004144 * The locale is not used, the string is used as a byte string. This is only
4145 * relevant for double-byte encodings where the second byte may be '%'.
4146 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004147 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4148 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004149 *
4150 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004151 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004152 * is greater or equal to "str_m", not all characters from the result
4153 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4154 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004155 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004156 */
4157
4158/*
4159 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004160 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004161 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004162 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004163 */
4164
Bram Moolenaar85a20022019-12-21 18:25:54 +01004165// When generating prototypes all of this is skipped, cproto doesn't
4166// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004167#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004168
Bram Moolenaar85a20022019-12-21 18:25:54 +01004169// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004170 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004171vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004172{
4173 va_list ap;
4174 int str_l;
4175 size_t len = STRLEN(str);
4176 size_t space;
4177
4178 if (str_m <= len)
4179 space = 0;
4180 else
4181 space = str_m - len;
4182 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004183 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004184 va_end(ap);
4185 return str_l;
4186}
Bram Moolenaara800b422010-06-27 01:15:55 +02004187
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004188 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004189vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004190{
4191 va_list ap;
4192 int str_l;
4193
4194 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004195 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004196 va_end(ap);
4197 return str_l;
4198}
4199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004201vim_vsnprintf(
4202 char *str,
4203 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004204 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004205 va_list ap)
4206{
4207 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4208}
4209
4210 int
4211vim_vsnprintf_typval(
4212 char *str,
4213 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004214 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004215 va_list ap,
4216 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004217{
4218 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004219 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004220 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004221
4222 if (p == NULL)
4223 p = "";
4224 while (*p != NUL)
4225 {
4226 if (*p != '%')
4227 {
4228 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004229 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230
Bram Moolenaar85a20022019-12-21 18:25:54 +01004231 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004232 if (str_l < str_m)
4233 {
4234 size_t avail = str_m - str_l;
4235
4236 mch_memmove(str + str_l, p, n > avail ? avail : n);
4237 }
4238 p += n;
4239 str_l += n;
4240 }
4241 else
4242 {
4243 size_t min_field_width = 0, precision = 0;
4244 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4245 int alternate_form = 0, force_sign = 0;
4246
Bram Moolenaar85a20022019-12-21 18:25:54 +01004247 // If both the ' ' and '+' flags appear, the ' ' flag should be
4248 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004249 int space_for_positive = 1;
4250
Bram Moolenaar85a20022019-12-21 18:25:54 +01004251 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252 char length_modifier = '\0';
4253
Bram Moolenaar85a20022019-12-21 18:25:54 +01004254 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004255# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004256# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4257 // That sounds reasonable to use as the maximum
4258 // printable.
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004259# else
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004260# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004261# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004262 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004263
Bram Moolenaar85a20022019-12-21 18:25:54 +01004264 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004265 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004266
Bram Moolenaar85a20022019-12-21 18:25:54 +01004267 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004268 size_t str_arg_l;
4269
Bram Moolenaar85a20022019-12-21 18:25:54 +01004270 // unsigned char argument value - only defined for c conversion.
4271 // N.B. standard explicitly states the char argument for the c
4272 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004273 unsigned char uchar_arg;
4274
Bram Moolenaar85a20022019-12-21 18:25:54 +01004275 // number of zeros to be inserted for numeric conversions as
4276 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004277 size_t number_of_zeros_to_pad = 0;
4278
Bram Moolenaar85a20022019-12-21 18:25:54 +01004279 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004280 size_t zero_padding_insertion_ind = 0;
4281
Bram Moolenaar85a20022019-12-21 18:25:54 +01004282 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283 char fmt_spec = '\0';
4284
Bram Moolenaar85a20022019-12-21 18:25:54 +01004285 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004286 char_u *tofree = NULL;
4287
4288
Bram Moolenaar85a20022019-12-21 18:25:54 +01004289 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004290
Bram Moolenaar85a20022019-12-21 18:25:54 +01004291 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004292 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4293 || *p == '#' || *p == '\'')
4294 {
4295 switch (*p)
4296 {
4297 case '0': zero_padding = 1; break;
4298 case '-': justify_left = 1; break;
4299 case '+': force_sign = 1; space_for_positive = 0; break;
4300 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004301 // If both the ' ' and '+' flags appear, the ' '
4302 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303 break;
4304 case '#': alternate_form = 1; break;
4305 case '\'': break;
4306 }
4307 p++;
4308 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004309 // If the '0' and '-' flags both appear, the '0' flag should be
4310 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004311
Bram Moolenaar85a20022019-12-21 18:25:54 +01004312 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004313 if (*p == '*')
4314 {
4315 int j;
4316
4317 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004319# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004320 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321# endif
4322 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004323 if (j >= 0)
4324 min_field_width = j;
4325 else
4326 {
4327 min_field_width = -j;
4328 justify_left = 1;
4329 }
4330 }
4331 else if (VIM_ISDIGIT((int)(*p)))
4332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004333 // size_t could be wider than unsigned int; make sure we treat
4334 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004335 unsigned int uj = *p++ - '0';
4336
4337 while (VIM_ISDIGIT((int)(*p)))
4338 uj = 10 * uj + (unsigned int)(*p++ - '0');
4339 min_field_width = uj;
4340 }
4341
Bram Moolenaar85a20022019-12-21 18:25:54 +01004342 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004343 if (*p == '.')
4344 {
4345 p++;
4346 precision_specified = 1;
4347 if (*p == '*')
4348 {
4349 int j;
4350
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004352# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004353 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354# endif
4355 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004356 p++;
4357 if (j >= 0)
4358 precision = j;
4359 else
4360 {
4361 precision_specified = 0;
4362 precision = 0;
4363 }
4364 }
4365 else if (VIM_ISDIGIT((int)(*p)))
4366 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004367 // size_t could be wider than unsigned int; make sure we
4368 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004369 unsigned int uj = *p++ - '0';
4370
4371 while (VIM_ISDIGIT((int)(*p)))
4372 uj = 10 * uj + (unsigned int)(*p++ - '0');
4373 precision = uj;
4374 }
4375 }
4376
Bram Moolenaar85a20022019-12-21 18:25:54 +01004377 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004378 if (*p == 'h' || *p == 'l')
4379 {
4380 length_modifier = *p;
4381 p++;
4382 if (length_modifier == 'l' && *p == 'l')
4383 {
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004384 // double l = __int64 / varnumber_T
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004385 length_modifier = 'L';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004386 p++;
4387 }
4388 }
4389 fmt_spec = *p;
4390
Bram Moolenaar85a20022019-12-21 18:25:54 +01004391 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004392 switch (fmt_spec)
4393 {
4394 case 'i': fmt_spec = 'd'; break;
4395 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4396 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4397 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4398 default: break;
4399 }
4400
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004401# if defined(FEAT_EVAL)
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004402 switch (fmt_spec)
4403 {
4404 case 'd': case 'u': case 'o': case 'x': case 'X':
4405 if (tvs != NULL && length_modifier == '\0')
4406 length_modifier = 'L';
4407 }
4408# endif
4409
Bram Moolenaar85a20022019-12-21 18:25:54 +01004410 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004411 switch (fmt_spec)
4412 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004413 // '%' and 'c' behave similar to 's' regarding flags and field
4414 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004415 case '%':
4416 case 'c':
4417 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004418 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004419 str_arg_l = 1;
4420 switch (fmt_spec)
4421 {
4422 case '%':
4423 str_arg = p;
4424 break;
4425
4426 case 'c':
4427 {
4428 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429
4430 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004432 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433# endif
4434 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004435 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004436 uchar_arg = (unsigned char)j;
4437 str_arg = (char *)&uchar_arg;
4438 break;
4439 }
4440
4441 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004442 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004445 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446# endif
4447 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004448 if (str_arg == NULL)
4449 {
4450 str_arg = "[NULL]";
4451 str_arg_l = 6;
4452 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004453 // make sure not to address string beyond the specified
4454 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004455 else if (!precision_specified)
4456 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004457 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004458 else if (precision == 0)
4459 str_arg_l = 0;
4460 else
4461 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004462 // Don't put the #if inside memchr(), it can be a
4463 // macro.
4464 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004465 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004466 precision <= (size_t)0x7fffffffL ? precision
4467 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004468 str_arg_l = (q == NULL) ? precision
4469 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004470 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004471 if (fmt_spec == 'S')
4472 {
4473 if (min_field_width != 0)
4474 min_field_width += STRLEN(str_arg)
4475 - mb_string2cells((char_u *)str_arg, -1);
4476 if (precision)
4477 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004478 char_u *p1;
4479 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004480
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004481 for (p1 = (char_u *)str_arg; *p1;
4482 p1 += mb_ptr2len(p1))
4483 {
4484 i += (size_t)mb_ptr2cells(p1);
4485 if (i > precision)
4486 break;
4487 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004488 str_arg_l = precision = p1 - (char_u *)str_arg;
4489 }
4490 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004491 break;
4492
4493 default:
4494 break;
4495 }
4496 break;
4497
Bram Moolenaar91984b92016-08-16 21:58:41 +02004498 case 'd': case 'u':
4499 case 'b': case 'B':
4500 case 'o':
4501 case 'x': case 'X':
4502 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004503 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004504 // NOTE: the u, b, o, x, X and p conversion specifiers
4505 // imply the value is unsigned; d implies a signed
4506 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004507
Bram Moolenaar85a20022019-12-21 18:25:54 +01004508 // 0 if numeric argument is zero (or if pointer is
4509 // NULL for 'p'), +1 if greater than zero (or nonzero
4510 // for unsigned arguments), -1 if negative (unsigned
4511 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004512 int arg_sign = 0;
4513
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004514 // only set for length modifier h, or for no length
4515 // modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004516 int int_arg = 0;
4517 unsigned int uint_arg = 0;
4518
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004519 // only set for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004520 long int long_arg = 0;
4521 unsigned long int ulong_arg = 0;
4522
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004523 // only set for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004524 varnumber_T llong_arg = 0;
4525 uvarnumber_T ullong_arg = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004526
Bram Moolenaarf9706e92020-02-22 14:27:04 +01004527 // only set for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004528 uvarnumber_T bin_arg = 0;
4529
Bram Moolenaar85a20022019-12-21 18:25:54 +01004530 // pointer argument value -only defined for p
4531 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 void *ptr_arg = NULL;
4533
4534 if (fmt_spec == 'p')
4535 {
4536 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004539 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4540 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541# endif
4542 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004543 if (ptr_arg != NULL)
4544 arg_sign = 1;
4545 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004546 else if (fmt_spec == 'b' || fmt_spec == 'B')
4547 {
4548 bin_arg =
4549# if defined(FEAT_EVAL)
4550 tvs != NULL ?
4551 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4552# endif
4553 va_arg(ap, uvarnumber_T);
4554 if (bin_arg != 0)
4555 arg_sign = 1;
4556 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004557 else if (fmt_spec == 'd')
4558 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004559 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004560 switch (length_modifier)
4561 {
4562 case '\0':
4563 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004564 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004565 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004567 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004568# endif
4569 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004570 if (int_arg > 0)
4571 arg_sign = 1;
4572 else if (int_arg < 0)
4573 arg_sign = -1;
4574 break;
4575 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004578 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579# endif
4580 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004581 if (long_arg > 0)
4582 arg_sign = 1;
4583 else if (long_arg < 0)
4584 arg_sign = -1;
4585 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004586 case 'L':
4587 llong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004588# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004589 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004590# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004591 va_arg(ap, varnumber_T);
4592 if (llong_arg > 0)
4593 arg_sign = 1;
4594 else if (llong_arg < 0)
4595 arg_sign = -1;
4596 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004597 }
4598 }
4599 else
4600 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004601 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004602 switch (length_modifier)
4603 {
4604 case '\0':
4605 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004608 tvs != NULL ? (unsigned)
4609 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610# endif
4611 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004612 if (uint_arg != 0)
4613 arg_sign = 1;
4614 break;
4615 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004618 tvs != NULL ? (unsigned long)
4619 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620# endif
4621 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004622 if (ulong_arg != 0)
4623 arg_sign = 1;
4624 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004625 case 'L':
4626 ullong_arg =
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004627# if defined(FEAT_EVAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004628 tvs != NULL ? (uvarnumber_T)
4629 tv_nr(tvs, &arg_idx) :
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004630# endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004631 va_arg(ap, uvarnumber_T);
4632 if (ullong_arg != 0)
4633 arg_sign = 1;
4634 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004635 }
4636 }
4637
4638 str_arg = tmp;
4639 str_arg_l = 0;
4640
Bram Moolenaar85a20022019-12-21 18:25:54 +01004641 // NOTE:
4642 // For d, i, u, o, x, and X conversions, if precision is
4643 // specified, the '0' flag should be ignored. This is so
4644 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4645 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004646 if (precision_specified)
4647 zero_padding = 0;
4648 if (fmt_spec == 'd')
4649 {
4650 if (force_sign && arg_sign >= 0)
4651 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004652 // leave negative numbers for sprintf to handle, to
4653 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004654 }
4655 else if (alternate_form)
4656 {
4657 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004658 && (fmt_spec == 'b' || fmt_spec == 'B'
4659 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004660 {
4661 tmp[str_arg_l++] = '0';
4662 tmp[str_arg_l++] = fmt_spec;
4663 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004664 // alternate form should have no effect for p
4665 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004666 }
4667
4668 zero_padding_insertion_ind = str_arg_l;
4669 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004670 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004671 if (precision == 0 && arg_sign == 0)
4672 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004673 // When zero value is formatted with an explicit
4674 // precision 0, the resulting formatted string is
4675 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004676 }
4677 else
4678 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004679 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004680 int f_l = 0;
4681
Bram Moolenaar85a20022019-12-21 18:25:54 +01004682 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004683 f[f_l++] = '%';
4684 if (!length_modifier)
4685 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004686 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004687 {
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004688# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004689 f[f_l++] = 'I';
4690 f[f_l++] = '6';
4691 f[f_l++] = '4';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004692# else
4693 f[f_l++] = 'l';
Bram Moolenaar82f654e2020-02-17 22:12:50 +01004694 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004695# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004696 }
4697 else
4698 f[f_l++] = length_modifier;
4699 f[f_l++] = fmt_spec;
4700 f[f_l++] = '\0';
4701
4702 if (fmt_spec == 'p')
4703 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004704 else if (fmt_spec == 'b' || fmt_spec == 'B')
4705 {
4706 char b[8 * sizeof(uvarnumber_T)];
4707 size_t b_l = 0;
4708 uvarnumber_T bn = bin_arg;
4709
4710 do
4711 {
4712 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4713 bn >>= 1;
4714 }
4715 while (bn != 0);
4716
4717 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4718 str_arg_l += b_l;
4719 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004720 else if (fmt_spec == 'd')
4721 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004722 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004723 switch (length_modifier)
4724 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004725 case '\0': str_arg_l += sprintf(
4726 tmp + str_arg_l, f,
4727 int_arg);
4728 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004729 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004730 tmp + str_arg_l, f,
4731 (short)int_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004732 break;
4733 case 'l': str_arg_l += sprintf(
4734 tmp + str_arg_l, f, long_arg);
4735 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004736 case 'L': str_arg_l += sprintf(
4737 tmp + str_arg_l, f, llong_arg);
4738 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004739 }
4740 }
4741 else
4742 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004743 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004744 switch (length_modifier)
4745 {
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004746 case '\0': str_arg_l += sprintf(
4747 tmp + str_arg_l, f,
4748 uint_arg);
4749 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004750 case 'h': str_arg_l += sprintf(
Bram Moolenaar693e80e2020-03-08 18:41:09 +01004751 tmp + str_arg_l, f,
4752 (unsigned short)uint_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004753 break;
4754 case 'l': str_arg_l += sprintf(
4755 tmp + str_arg_l, f, ulong_arg);
4756 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004757 case 'L': str_arg_l += sprintf(
4758 tmp + str_arg_l, f, ullong_arg);
4759 break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004760 }
4761 }
4762
Bram Moolenaar85a20022019-12-21 18:25:54 +01004763 // include the optional minus sign and possible
4764 // "0x" in the region before the zero padding
4765 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004766 if (zero_padding_insertion_ind < str_arg_l
4767 && tmp[zero_padding_insertion_ind] == '-')
4768 zero_padding_insertion_ind++;
4769 if (zero_padding_insertion_ind + 1 < str_arg_l
4770 && tmp[zero_padding_insertion_ind] == '0'
4771 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4772 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4773 zero_padding_insertion_ind += 2;
4774 }
4775
4776 {
4777 size_t num_of_digits = str_arg_l
4778 - zero_padding_insertion_ind;
4779
4780 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004781 // unless zero is already the first
4782 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004783 && !(zero_padding_insertion_ind < str_arg_l
4784 && tmp[zero_padding_insertion_ind] == '0'))
4785 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004786 // assure leading zero for alternate-form
4787 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004788 if (!precision_specified
4789 || precision < num_of_digits + 1)
4790 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004791 // precision is increased to force the
4792 // first character to be zero, except if a
4793 // zero value is formatted with an
4794 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004795 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004796 }
4797 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004798 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 if (num_of_digits < precision)
4800 number_of_zeros_to_pad = precision - num_of_digits;
4801 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004802 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803 if (!justify_left && zero_padding)
4804 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004805 int n = (int)(min_field_width - (str_arg_l
4806 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004807 if (n > 0)
4808 number_of_zeros_to_pad += n;
4809 }
4810 break;
4811 }
4812
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004813# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004814 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004815 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004816 case 'e':
4817 case 'E':
4818 case 'g':
4819 case 'G':
4820 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004821 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004822 double f;
4823 double abs_f;
4824 char format[40];
4825 int l;
4826 int remove_trailing_zeroes = FALSE;
4827
4828 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004829# if defined(FEAT_EVAL)
4830 tvs != NULL ? tv_float(tvs, &arg_idx) :
4831# endif
4832 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004833 abs_f = f < 0 ? -f : f;
4834
4835 if (fmt_spec == 'g' || fmt_spec == 'G')
4836 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004837 // Would be nice to use %g directly, but it prints
4838 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004839 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4840 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004841 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004842 else
4843 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4844 remove_trailing_zeroes = TRUE;
4845 }
4846
Bram Moolenaar04186092016-08-29 21:55:35 +02004847 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004848# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004849 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004850# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004851 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004852# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004853 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004854 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004855 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004856 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4857 force_sign, space_for_positive));
4858 str_arg_l = STRLEN(tmp);
4859 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004860 }
4861 else
4862 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004863 if (isnan(f))
4864 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004865 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004866 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4867 : "nan");
4868 str_arg_l = 3;
4869 zero_padding = 0;
4870 }
4871 else if (isinf(f))
4872 {
4873 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;
4877 }
4878 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004879 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004880 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004881 format[0] = '%';
4882 l = 1;
4883 if (force_sign)
4884 format[l++] = space_for_positive ? ' ' : '+';
4885 if (precision_specified)
4886 {
4887 size_t max_prec = TMP_LEN - 10;
4888
Bram Moolenaar85a20022019-12-21 18:25:54 +01004889 // Make sure we don't get more digits than we
4890 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004891 if ((fmt_spec == 'f' || fmt_spec == 'F')
4892 && abs_f > 1.0)
4893 max_prec -= (size_t)log10(abs_f);
4894 if (precision > max_prec)
4895 precision = max_prec;
4896 l += sprintf(format + l, ".%d", (int)precision);
4897 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004898 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004899 format[l + 1] = NUL;
4900
Bram Moolenaare9997822016-08-28 16:03:38 +02004901 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004902 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004903
Bram Moolenaara7241f52008-06-24 20:39:31 +00004904 if (remove_trailing_zeroes)
4905 {
4906 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004907 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004908
Bram Moolenaar85a20022019-12-21 18:25:54 +01004909 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004910 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004911 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004912 else
4913 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004914 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004915 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004916 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004917 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004918 // Remove superfluous '+' and leading
4919 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004920 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004921 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004922 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004923 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004924 --str_arg_l;
4925 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004926 i = (tp[1] == '-') ? 2 : 1;
4927 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004928 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004929 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004930 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004931 --str_arg_l;
4932 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004933 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004934 }
4935 }
4936
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004937 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004938 // Remove trailing zeroes, but keep the one
4939 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004940 while (tp > tmp + 2 && *tp == '0'
4941 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004942 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004943 STRMOVE(tp, tp + 1);
4944 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004945 --str_arg_l;
4946 }
4947 }
4948 else
4949 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004950 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004951
Bram Moolenaar85a20022019-12-21 18:25:54 +01004952 // Be consistent: some printf("%e") use 1.0e+12
4953 // and some 1.0e+012. Remove one zero in the last
4954 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004955 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004956 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004957 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4958 && tp[2] == '0'
4959 && vim_isdigit(tp[3])
4960 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004961 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004962 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004963 --str_arg_l;
4964 }
4965 }
4966 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004967 if (zero_padding && min_field_width > str_arg_l
4968 && (tmp[0] == '-' || force_sign))
4969 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004970 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02004971 number_of_zeros_to_pad = min_field_width - str_arg_l;
4972 zero_padding_insertion_ind = 1;
4973 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004974 str_arg = tmp;
4975 break;
4976 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004977# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004978
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004979 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01004980 // unrecognized conversion specifier, keep format string
4981 // as-is
4982 zero_padding = 0; // turn zero padding off for non-numeric
4983 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004984 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004985 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004986
Bram Moolenaar85a20022019-12-21 18:25:54 +01004987 // discard the unrecognized conversion, just keep *
4988 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004989 str_arg = p;
4990 str_arg_l = 0;
4991 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004992 str_arg_l++; // include invalid conversion specifier
4993 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004994 break;
4995 }
4996
4997 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004998 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004999
Bram Moolenaar85a20022019-12-21 18:25:54 +01005000 // insert padding to the left as requested by min_field_width;
5001 // this does not include the zero padding in case of numerical
5002 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005003 if (!justify_left)
5004 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005005 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005006 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005007
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005008 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005009 {
5010 if (str_l < str_m)
5011 {
5012 size_t avail = str_m - str_l;
5013
5014 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005015 (size_t)pn > avail ? avail
5016 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005017 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005018 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005019 }
5020 }
5021
Bram Moolenaar85a20022019-12-21 18:25:54 +01005022 // zero padding as requested by the precision or by the minimal
5023 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005024 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005025 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005026 // will not copy first part of numeric right now, *
5027 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005028 zero_padding_insertion_ind = 0;
5029 }
5030 else
5031 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005032 // insert first part of numerics (sign or '0x') before zero
5033 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005034 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005035
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005036 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005037 {
5038 if (str_l < str_m)
5039 {
5040 size_t avail = str_m - str_l;
5041
5042 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005043 (size_t)zn > avail ? avail
5044 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005045 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005046 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005047 }
5048
Bram Moolenaar85a20022019-12-21 18:25:54 +01005049 // insert zero padding as requested by the precision or min
5050 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005051 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005052 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005053 {
5054 if (str_l < str_m)
5055 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005056 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005057
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005058 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005059 (size_t)zn > avail ? avail
5060 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005061 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005062 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 }
5064 }
5065
Bram Moolenaar85a20022019-12-21 18:25:54 +01005066 // insert formatted string
5067 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005068 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005069 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005070
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005071 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072 {
5073 if (str_l < str_m)
5074 {
5075 size_t avail = str_m - str_l;
5076
5077 mch_memmove(str + str_l,
5078 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005079 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005080 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005081 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005082 }
5083 }
5084
Bram Moolenaar85a20022019-12-21 18:25:54 +01005085 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005086 if (justify_left)
5087 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005088 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005089 int pn = (int)(min_field_width
5090 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005091
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005092 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005093 {
5094 if (str_l < str_m)
5095 {
5096 size_t avail = str_m - str_l;
5097
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005098 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005099 (size_t)pn > avail ? avail
5100 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005101 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005102 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103 }
5104 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005105 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005106 }
5107 }
5108
5109 if (str_m > 0)
5110 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005111 // make sure the string is nul-terminated even at the expense of
5112 // overwriting the last character (shouldn't happen, but just in case)
5113 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005114 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5115 }
5116
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005117 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005118 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005119
Bram Moolenaar85a20022019-12-21 18:25:54 +01005120 // Return the number of characters formatted (excluding trailing nul
5121 // character), that is, the number of characters that would have been
5122 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005123 return (int)str_l;
5124}
5125
Bram Moolenaar85a20022019-12-21 18:25:54 +01005126#endif // PROTO