blob: da81fa01577ec4db68c00326b738da77e0c8303c [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
14#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 Moolenaar071d4272004-06-13 20:20:40 +000036static 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,
131 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 Moolenaar7b668e82016-08-23 23:51:21 +0200137 /* 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
156 /* 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 Moolenaar958dc692016-12-01 15:34:12 +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 Moolenaar071d4272004-06-13 20:20:40 +0000171 /* When displaying keep_msg, don't let msg_start() free it, caller must do
172 * that. */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100173 if ((char_u *)s == keep_msg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 keep_msg = NULL;
175
176 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000177 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100178 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaar32526b32019-01-19 17:43:09 +0100182 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 msg_clr_eos();
184 retval = msg_end();
185
Bram Moolenaar32526b32019-01-19 17:43:09 +0100186 if (keep && retval && vim_strsize((char_u *)s)
187 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
188 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190 vim_free(buf);
191 --entered;
192 return retval;
193}
194
195/*
196 * Truncate a string such that it can be printed without causing a scroll.
197 * Returns an allocated string or NULL when no truncating is done.
198 */
199 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100200msg_strtrunc(
201 char_u *s,
202 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 char_u *buf = NULL;
205 int len;
206 int room;
207
208 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000209 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
210 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000213 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000214 /* Use all the columns. */
215 room = (int)(Rows - msg_row) * Columns - 1;
216 else
217 /* Use up to 'showcmd' column. */
218 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (len > room && room > 0)
220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 if (enc_utf8)
222 /* may have up to 18 bytes per cell (6 per char, up to two
223 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 else if (enc_dbcs == DBCS_JPNU)
226 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = room + 2;
230 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234 }
235 return buf;
236}
237
238/*
239 * Truncate a string "s" to "buf" with cell width "room".
240 * "s" and "buf" may be equal.
241 */
242 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100243trunc_string(
244 char_u *s,
245 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200246 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100247 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200249 size_t room = room_in - 3; /* "..." takes 3 chars */
250 size_t half;
251 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 int e;
253 int i;
254 int n;
255
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200256 if (room_in < 3)
257 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100261 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 if (s[e] == NUL)
264 {
265 /* text fits without truncating! */
266 buf[e] = NUL;
267 return;
268 }
269 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200270 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 break;
272 len += n;
273 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000275 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100277 if (++e == buflen)
278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 buf[e] = s[e];
280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 }
282
283 /* Last part: End of the string. */
284 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 if (enc_dbcs != 0)
286 {
287 /* For DBCS going backwards in a string is slow, but
288 * computing the cell width isn't too slow: go forward
289 * until the rest fits. */
290 n = vim_strsize(s + i);
291 while (len + n > room)
292 {
293 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000294 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 }
296 }
297 else if (enc_utf8)
298 {
299 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000300 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 for (;;)
302 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000303 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200304 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200305 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200307 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 break;
309 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200310 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 }
312 }
313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 {
315 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
316 len += n;
317 }
318
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200319
320 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100321 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200322 /* text fits without truncating */
323 if (s != buf)
324 {
325 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200326 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200327 len = buflen - 1;
328 len = len - e + 1;
329 if (len < 1)
330 buf[e - 1] = NUL;
331 else
332 mch_memmove(buf + e, s + e, len);
333 }
334 }
335 else if (e + 3 < buflen)
336 {
337 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100338 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200339 len = STRLEN(s + i) + 1;
340 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100341 len = buflen - e - 3 - 1;
342 mch_memmove(buf + e + 3, s + i, len);
343 buf[e + 3 + len - 1] = NUL;
344 }
345 else
346 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200347 /* can't fit in the "...", just truncate it */
348 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350}
351
352/*
353 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100354 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 * shorter than IOSIZE!!!
356 */
357#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100359int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100362smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364 va_list arglist;
365
366 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100367 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100369 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370}
371
372 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100373smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374{
375 va_list arglist;
376
377 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100378 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100380 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381}
382
Bram Moolenaare0429682018-07-01 16:44:03 +0200383 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100384smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200385{
386 va_list arglist;
387
388 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100389 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaare0429682018-07-01 16:44:03 +0200390 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100391 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200392}
393
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#endif
395
396/*
397 * Remember the last sourcing name/lnum used in an error message, so that it
398 * isn't printed each time when it didn't change.
399 */
400static int last_sourcing_lnum = 0;
401static char_u *last_sourcing_name = NULL;
402
403/*
404 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
405 * for the next error message;
406 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000407 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100408reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100410 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 last_sourcing_lnum = 0;
412}
413
414/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000415 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
416 */
417 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100418other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419{
420 if (sourcing_name != NULL)
421 {
422 if (last_sourcing_name != NULL)
423 return STRCMP(sourcing_name, last_sourcing_name) != 0;
424 return TRUE;
425 }
426 return FALSE;
427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Get the message about the source, as used for an error message.
431 * Returns an allocated string with room for one more character.
432 * Returns NULL when no message is to be given.
433 */
434 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100435get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 char_u *Buf, *p;
438
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000439 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
441 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200442 Buf = alloc(STRLEN(sourcing_name) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 if (Buf != NULL)
444 sprintf((char *)Buf, (char *)p, sourcing_name);
445 return Buf;
446 }
447 return NULL;
448}
449
450/*
451 * Get the message about the source lnum, as used for an error message.
452 * Returns an allocated string with room for one more character.
453 * Returns NULL when no message is to be given.
454 */
455 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100456get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 char_u *Buf, *p;
459
460 /* lnum is 0 when executing a command from the command line
461 * argument, we don't want a line number then */
462 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000463 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 && sourcing_lnum != 0)
465 {
466 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200467 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 if (Buf != NULL)
469 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
470 return Buf;
471 }
472 return NULL;
473}
474
475/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000476 * Display name and line number for the source of an error.
477 * Remember the file name and line number, so that for the next error the info
478 * is only displayed if it changed.
479 */
480 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100481msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000482{
483 char_u *p;
484
485 ++no_wait_return;
486 p = get_emsg_source();
487 if (p != NULL)
488 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100489 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000490 vim_free(p);
491 }
492 p = get_emsg_lnum();
493 if (p != NULL)
494 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100495 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000496 vim_free(p);
497 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
498 }
499
500 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000501 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000502 {
503 vim_free(last_sourcing_name);
504 if (sourcing_name == NULL)
505 last_sourcing_name = NULL;
506 else
507 last_sourcing_name = vim_strsave(sourcing_name);
508 }
509 --no_wait_return;
510}
511
512/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000513 * Return TRUE if not giving error messages right now:
514 * If "emsg_off" is set: no error messages at the moment.
515 * If "msg" is in 'debug': do error message but without side effects.
516 * If "emsg_skip" is set: never do error messages.
517 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200518 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100519emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000520{
521 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
522 && vim_strchr(p_debug, 't') == NULL)
523#ifdef FEAT_EVAL
524 || emsg_skip > 0
525#endif
526 )
527 return TRUE;
528 return FALSE;
529}
530
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100531#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100532static garray_T ignore_error_list = GA_EMPTY;
533
534 void
535ignore_error_for_testing(char_u *error)
536{
537 if (ignore_error_list.ga_itemsize == 0)
538 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
539
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100540 if (STRCMP("RESET", error) == 0)
541 ga_clear_strings(&ignore_error_list);
542 else
543 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100544}
545
546 static int
547ignore_error(char_u *msg)
548{
549 int i;
550
551 for (i = 0; i < ignore_error_list.ga_len; ++i)
552 if (strstr((char *)msg,
553 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
554 return TRUE;
555 return FALSE;
556}
557#endif
558
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200559#if !defined(HAVE_STRERROR) || defined(PROTO)
560/*
561 * Replacement for perror() that behaves more or less like emsg() was called.
562 * v:errmsg will be set and called_emsg will be set.
563 */
564 void
565do_perror(char *msg)
566{
567 perror(msg);
568 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100569 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200570 --emsg_silent;
571}
572#endif
573
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000574/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100575 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 *
577 * Rings the bell, if appropriate, and calls message() to do the real work
578 * When terminal not initialized (yet) mch_errmsg(..) is used.
579 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100580 * Return TRUE if wait_return not called.
581 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100583 static int
584emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100588 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589#ifdef FEAT_EVAL
590 int ignore = FALSE;
591 int severe;
592#endif
593
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100594#ifdef FEAT_EVAL
595 /* When testing some errors are turned into a normal message. */
596 if (ignore_error(s))
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +0100597 /* don't call msg() if it results in a dialog */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100598 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100599#endif
600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 called_emsg = TRUE;
602
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603#ifdef FEAT_EVAL
Bram Moolenaare723c422017-09-06 23:40:10 +0200604 /* If "emsg_severe" is TRUE: When an error exception is to be thrown,
605 * prefer this message over previous messages for the same command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 severe = emsg_severe;
607 emsg_severe = FALSE;
608#endif
609
Bram Moolenaar57657d82006-04-21 22:12:41 +0000610 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {
612#ifdef FEAT_EVAL
613 /*
614 * Cause a throw of an error exception if appropriate. Don't display
615 * the error message in this case. (If no matching catch clause will
616 * be found, the message will be displayed later on.) "ignore" is set
617 * when the message should be ignored completely (used for the
618 * interrupt message).
619 */
620 if (cause_errthrow(s, severe, &ignore) == TRUE)
621 {
622 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100623 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 return TRUE;
625 }
626
627 /* set "v:errmsg", also when using ":silent! cmd" */
628 set_vim_var_string(VV_ERRMSG, s, -1);
629#endif
630
631 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000632 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 * But do write it to the redirection file.
634 */
635 if (emsg_silent != 0)
636 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200637 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200639 msg_start();
640 p = get_emsg_source();
641 if (p != NULL)
642 {
643 STRCAT(p, "\n");
644 redir_write(p, -1);
645 vim_free(p);
646 }
647 p = get_emsg_lnum();
648 if (p != NULL)
649 {
650 STRCAT(p, "\n");
651 redir_write(p, -1);
652 vim_free(p);
653 }
654 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100656#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200657 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return TRUE;
660 }
661
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100662 ex_exitval = 1;
663
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200664 /* Reset msg_silent, an error causes messages to be switched back on.
665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 msg_silent = 0;
667 cmd_silent = FALSE;
668
669 if (global_busy) /* break :global command */
670 ++global_busy;
671
672 if (p_eb)
673 beep_flush(); /* also includes flush_buffers() */
674 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200675 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100676 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200677#ifdef FEAT_EVAL
678 did_uncaught_emsg = TRUE;
679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681
682 emsg_on_display = TRUE; /* remember there is an error message */
683 ++msg_scroll; /* don't overwrite a previous message */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100684 attr = HL_ATTR(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000685 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 need_wait_return = TRUE; /* needed in case emsg() is called after
687 * wait_return has reset need_wait_return
688 * and a redraw is expected because
689 * msg_scrolled is non-zero */
690
Bram Moolenaar958dc692016-12-01 15:34:12 +0100691#ifdef FEAT_JOB_CHANNEL
692 emsg_to_channel_log = TRUE;
693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 /*
695 * Display name and line number for the source of the error.
696 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000697 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699 /*
700 * Display the error message itself.
701 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000702 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100703 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100704
705#ifdef FEAT_JOB_CHANNEL
706 emsg_to_channel_log = FALSE;
707#endif
708 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709}
710
711/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100712 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 */
714 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 /* Skip this if not giving error messages at the moment. */
718 if (!emsg_not_now())
719 return emsg_core((char_u *)s);
720 return TRUE; /* no error messages at the moment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721}
722
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100723#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100724/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100725 * Print an error message with format string and variable arguments.
726 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100727 */
728 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100729semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100730{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731 /* Skip this if not giving error messages at the moment. */
732 if (!emsg_not_now())
733 {
734 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100735
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100736 va_start(ap, s);
737 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
738 va_end(ap);
739 return emsg_core(IObuff);
740 }
741 return TRUE; /* no error messages at the moment */
Bram Moolenaar95f09602016-11-10 20:01:45 +0100742}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100743#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100744
745/*
746 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
747 * defined. It is used for internal errors only, so that they can be
748 * detected when fuzzing vim.
749 */
750 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100751iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100752{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 if (!emsg_not_now())
754 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100755#ifdef ABORT_ON_INTERNAL_ERROR
756 abort();
757#endif
758}
759
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100760#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100761/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100762 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100763 * defined. It is used for internal errors only, so that they can be
764 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100765 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100766 */
767 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100768siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100769{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100770 if (!emsg_not_now())
771 {
772 va_list ap;
773
774 va_start(ap, s);
775 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
776 va_end(ap);
777 emsg_core(IObuff);
778 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100779# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100780 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100781# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100782}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100783#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100784
785/*
786 * Give an "Internal error" message.
787 */
788 void
789internal_error(char *where)
790{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100791 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100792}
793
Bram Moolenaarea424162005-06-16 21:51:00 +0000794/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaardf177f62005-02-22 08:39:57 +0000796 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100797emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000798{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000800}
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802/*
803 * Like msg(), but truncate to a single line if p_shm contains 't', or when
804 * "force" is TRUE. This truncates in another way as for normal messages.
805 * Careful: The string may be changed by msg_may_trunc()!
806 * Returns a pointer to the printed message, if wait_return() not called.
807 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100808 char *
809msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100812 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 /* Add message to history before truncating */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100815 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Bram Moolenaar32526b32019-01-19 17:43:09 +0100817 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100820 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 msg_hist_off = FALSE;
822
823 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100824 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 return NULL;
826}
827
828/*
829 * Check if message "s" should be truncated at the start (for filenames).
830 * Return a pointer to where the truncated message starts.
831 * Note: May change the message by replacing a character with '<'.
832 */
833 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100834msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 int n;
837 int room;
838
839 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
840 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
841 && (n = (int)STRLEN(s) - room) > 0)
842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (has_mbyte)
844 {
845 int size = vim_strsize(s);
846
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000847 /* There may be room anyway when there are multibyte chars. */
848 if (size <= room)
849 return s;
850
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 for (n = 0; size >= room; )
852 {
853 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000854 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 --n;
857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 s += n;
859 *s = '<';
860 }
861 return s;
862}
863
864 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100865add_msg_hist(
866 char_u *s,
867 int len, /* -1 for undetermined length */
868 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 struct msg_hist *p;
871
872 if (msg_hist_off || msg_silent != 0)
873 return;
874
875 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000876 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000877 (void)delete_first_msg();
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 /* allocate an entry and add the message at the end of the history */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200880 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (p != NULL)
882 {
883 if (len < 0)
884 len = (int)STRLEN(s);
885 /* remove leading and trailing newlines */
886 while (len > 0 && *s == '\n')
887 {
888 ++s;
889 --len;
890 }
891 while (len > 0 && s[len - 1] == '\n')
892 --len;
893 p->msg = vim_strnsave(s, len);
894 p->next = NULL;
895 p->attr = attr;
896 if (last_msg_hist != NULL)
897 last_msg_hist->next = p;
898 last_msg_hist = p;
899 if (first_msg_hist == NULL)
900 first_msg_hist = last_msg_hist;
901 ++msg_hist_len;
902 }
903}
904
905/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000906 * Delete the first (oldest) message from the history.
907 * Returns FAIL if there are no messages.
908 */
909 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100910delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000911{
912 struct msg_hist *p;
913
914 if (msg_hist_len <= 0)
915 return FAIL;
916 p = first_msg_hist;
917 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000918 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200919 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000920 vim_free(p->msg);
921 vim_free(p);
922 --msg_hist_len;
923 return OK;
924}
925
926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 * ":messages" command.
928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200930ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 struct msg_hist *p;
933 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200934 int c = 0;
935
936 if (STRCMP(eap->arg, "clear") == 0)
937 {
938 int keep = eap->addr_count == 0 ? 0 : eap->line2;
939
940 while (msg_hist_len > keep)
941 (void)delete_first_msg();
942 return;
943 }
944
945 if (*eap->arg != NUL)
946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100947 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +0200948 return;
949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
951 msg_hist_off = TRUE;
952
Bram Moolenaar451f8492016-04-14 17:16:22 +0200953 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200954 if (eap->addr_count != 0)
955 {
956 /* Count total messages */
957 for (; p != NULL && !got_int; p = p->next)
958 c++;
959
960 c -= eap->line2;
961
962 /* Skip without number of messages specified */
963 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
964 p = p->next, c--);
965 }
966
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200967 if (p == first_msg_hist)
968 {
969 s = mch_getenv((char_u *)"LANG");
970 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +0200971 // The next comment is extracted by xgettext and put in po file for
972 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100973 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +0200974 // Translator: Please replace the name and email address
975 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200976 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +0100977 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200978 }
979
Bram Moolenaar451f8492016-04-14 17:16:22 +0200980 /* Display what was not skipped. */
981 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100983 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 msg_hist_off = FALSE;
986}
987
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000988#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Call this after prompting the user. This will avoid a hit-return message
991 * and a delay.
992 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000993 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100994msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 need_wait_return = FALSE;
997 emsg_on_display = FALSE;
998 cmdline_row = msg_row;
999 msg_col = 0;
1000 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001001 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002}
1003#endif
1004
1005/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001006 * Wait for the user to hit a key (normally Enter).
1007 * If "redraw" is TRUE, clear and redraw the screen.
1008 * If "redraw" is FALSE, just redraw the screen.
1009 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
1011 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001012wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 int c;
1015 int oldState;
1016 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001018 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001019 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
1021 if (redraw == TRUE)
1022 must_redraw = CLEAR;
1023
1024 /* If using ":silent cmd", don't wait for a return. Also don't set
1025 * need_wait_return to do it later. */
1026 if (msg_silent != 0)
1027 return;
1028
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001029 /*
1030 * When inside vgetc(), we can't wait for a typed character at all.
1031 * With the global command (and some others) we only need one return at
1032 * the end. Adjust cmdline_row to avoid the next message overwriting the
1033 * last one.
1034 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001035 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001037 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (no_wait_return)
1039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (!exmode_active)
1041 cmdline_row = msg_row;
1042 return;
1043 }
1044
1045 redir_off = TRUE; /* don't redirect this message */
1046 oldState = State;
1047 if (quit_more)
1048 {
1049 c = CAR; /* just pretend CR was hit */
1050 quit_more = FALSE;
1051 got_int = FALSE;
1052 }
1053 else if (exmode_active)
1054 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001055 msg_puts(" "); /* make sure the cursor is on the right line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 c = CAR; /* no need for a return in ex mode */
1057 got_int = FALSE;
1058 }
1059 else
1060 {
1061 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1062 * just changed. */
1063 screenalloc(FALSE);
1064
1065 State = HITRETURN;
1066#ifdef FEAT_MOUSE
1067 setmouse();
1068#endif
1069#ifdef USE_ON_FLY_SCROLL
1070 dont_scroll = TRUE; /* disallow scrolling here */
1071#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001072 cmdline_row = msg_row;
1073
Bram Moolenaarec38d692013-04-24 15:12:32 +02001074 /* Avoid the sequence that the user types ":" at the hit-return prompt
1075 * to start an Ex command, but the file-changed dialog gets in the
1076 * way. */
1077 if (need_check_timestamps)
1078 check_timestamps(FALSE);
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 hit_return_msg();
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 do
1083 {
1084 /* Remember "got_int", if it is set vgetc() probably returns a
1085 * CTRL-C, but we need to loop then. */
1086 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001087
1088 /* Don't do mappings here, we put the character back in the
1089 * typeahead buffer. */
1090 ++no_mapping;
1091 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001092
1093 /* Temporarily disable Recording. If Recording is active, the
1094 * character will be recorded later, since it will be added to the
1095 * typebuf after the loop */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001096 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001097 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001098 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001099 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001101 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001103 --no_mapping;
1104 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001105 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001106 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#ifdef FEAT_CLIPBOARD
1109 /* Strange way to allow copying (yanking) a modeless selection at
1110 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1111 * Cmdline-mode and it's harmless when there is no selection. */
1112 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1113 {
1114 clip_copy_modeless_selection(TRUE);
1115 c = K_IGNORE;
1116 }
1117#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001118
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001119 /*
1120 * Allow scrolling back in the messages.
1121 * Also accept scroll-down commands when messages fill the screen,
1122 * to avoid that typing one 'j' too many makes the messages
1123 * disappear.
1124 */
1125 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001126 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001127 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1128 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001129 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001130 if (msg_scrolled > Rows)
1131 /* scroll back to show older messages */
1132 do_more_prompt(c);
1133 else
1134 {
1135 msg_didout = FALSE;
1136 c = K_IGNORE;
1137 msg_col =
1138#ifdef FEAT_RIGHTLEFT
1139 cmdmsg_rl ? Columns - 1 :
1140#endif
1141 0;
1142 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001143 if (quit_more)
1144 {
1145 c = CAR; /* just pretend CR was hit */
1146 quit_more = FALSE;
1147 got_int = FALSE;
1148 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001149 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001150 {
1151 c = K_IGNORE;
1152 hit_return_msg();
1153 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001154 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001155 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001156 && (c == 'j' || c == 'd' || c == 'f'
1157 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001158 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 } while ((had_got_int && c == Ctrl_C)
1161 || c == K_IGNORE
1162#ifdef FEAT_GUI
1163 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1164#endif
1165#ifdef FEAT_MOUSE
1166 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1167 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1168 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001169 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001171 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 || (!mouse_has(MOUSE_RETURN)
1173 && mouse_row < msg_row
1174 && (c == K_LEFTMOUSE
1175 || c == K_MIDDLEMOUSE
1176 || c == K_RIGHTMOUSE
1177 || c == K_X1MOUSE
1178 || c == K_X2MOUSE))
1179#endif
1180 );
1181 ui_breakcheck();
1182#ifdef FEAT_MOUSE
1183 /*
1184 * Avoid that the mouse-up event causes visual mode to start.
1185 */
1186 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1187 || c == K_X1MOUSE || c == K_X2MOUSE)
1188 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1189 else
1190#endif
1191 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1192 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001193 /* Put the character back in the typeahead buffer. Don't use the
1194 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001195 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001197 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
1200 redir_off = FALSE;
1201
1202 /*
1203 * If the user hits ':', '?' or '/' we get a command line from the next
1204 * line.
1205 */
1206 if (c == ':' || c == '?' || c == '/')
1207 {
1208 if (!exmode_active)
1209 cmdline_row = msg_row;
1210 skip_redraw = TRUE; /* skip redraw once */
1211 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001212#ifdef FEAT_TERMINAL
1213 skip_term_loop = TRUE;
1214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216
1217 /*
1218 * If the window size changed set_shellsize() will redraw the screen.
1219 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1220 * typed.
1221 */
1222 tmpState = State;
1223 State = oldState; /* restore State before set_shellsize */
1224#ifdef FEAT_MOUSE
1225 setmouse();
1226#endif
1227 msg_check();
1228
1229#if defined(UNIX) || defined(VMS)
1230 /*
1231 * When switching screens, we need to output an extra newline on exit.
1232 */
1233 if (swapping_screen() && !termcap_active)
1234 newline_on_exit = TRUE;
1235#endif
1236
1237 need_wait_return = FALSE;
1238 did_wait_return = TRUE;
1239 emsg_on_display = FALSE; /* can delete error message now */
1240 lines_left = -1; /* reset lines_left at next msg_start() */
1241 reset_last_sourcing();
1242 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1243 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001244 VIM_CLEAR(keep_msg); /* don't redisplay message, it's too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1247 {
1248 starttermcap(); /* start termcap before redrawing */
1249 shell_resized();
1250 }
1251 else if (!skip_redraw
1252 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1253 {
1254 starttermcap(); /* start termcap before redrawing */
1255 redraw_later(VALID);
1256 }
1257}
1258
1259/*
1260 * Write the hit-return prompt.
1261 */
1262 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001263hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001265 int save_p_more = p_more;
1266
1267 p_more = FALSE; /* don't want see this message when scrolling back */
1268 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 msg_putchar('\n');
1270 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001271 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
Bram Moolenaar32526b32019-01-19 17:43:09 +01001273 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 if (!msg_use_printf())
1275 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001276 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277}
1278
1279/*
1280 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1281 */
1282 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001283set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 vim_free(keep_msg);
1286 if (s != NULL && msg_silent == 0)
1287 keep_msg = vim_strsave(s);
1288 else
1289 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001290 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001291 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292}
1293
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001294#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1295/*
1296 * If there currently is a message being displayed, set "keep_msg" to it, so
1297 * that it will be displayed again after redraw.
1298 */
1299 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001300set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001301{
1302 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1303 && (State & NORMAL))
1304 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1305}
1306#endif
1307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308/*
1309 * Prepare for outputting characters in the command line.
1310 */
1311 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001312msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 int did_return = FALSE;
1315
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001316 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001317 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001318
1319#ifdef FEAT_EVAL
1320 if (need_clr_eos)
1321 {
1322 /* Halfway an ":echo" command and getting an (error) message: clear
1323 * any text from the command. */
1324 need_clr_eos = FALSE;
1325 msg_clr_eos();
1326 }
1327#endif
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (!msg_scroll && full_screen) /* overwrite last message */
1330 {
1331 msg_row = cmdline_row;
1332 msg_col =
1333#ifdef FEAT_RIGHTLEFT
1334 cmdmsg_rl ? Columns - 1 :
1335#endif
1336 0;
1337 }
1338 else if (msg_didout) /* start message on next line */
1339 {
1340 msg_putchar('\n');
1341 did_return = TRUE;
1342 if (exmode_active != EXMODE_NORMAL)
1343 cmdline_row = msg_row;
1344 }
1345 if (!msg_didany || lines_left < 0)
1346 msg_starthere();
1347 if (msg_silent == 0)
1348 {
1349 msg_didout = FALSE; /* no output on current line yet */
1350 cursor_off();
1351 }
1352
1353 /* when redirecting, may need to start a new line. */
1354 if (!did_return)
1355 redir_write((char_u *)"\n", -1);
1356}
1357
1358/*
1359 * Note that the current msg position is where messages start.
1360 */
1361 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001362msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
1364 lines_left = cmdline_row;
1365 msg_didany = FALSE;
1366}
1367
1368 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001369msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 msg_putchar_attr(c, 0);
1372}
1373
1374 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001375msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001377 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 if (IS_SPECIAL(c))
1380 {
1381 buf[0] = K_SPECIAL;
1382 buf[1] = K_SECOND(c);
1383 buf[2] = K_THIRD(c);
1384 buf[3] = NUL;
1385 }
1386 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001387 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001388 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389}
1390
1391 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001392msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001394 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar32526b32019-01-19 17:43:09 +01001396 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 msg_puts(buf);
1398}
1399
1400 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001401msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 msg_home_replace_attr(fname, 0);
1404}
1405
1406#if defined(FEAT_FIND_ID) || defined(PROTO)
1407 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001408msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001410 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411}
1412#endif
1413
1414 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001415msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 char_u *name;
1418
1419 name = home_replace_save(NULL, fname);
1420 if (name != NULL)
1421 msg_outtrans_attr(name, attr);
1422 vim_free(name);
1423}
1424
1425/*
1426 * Output 'len' characters in 'str' (including NULs) with translation
1427 * if 'len' is -1, output upto a NUL character.
1428 * Use attributes 'attr'.
1429 * Return the number of characters it takes on the screen.
1430 */
1431 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001432msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 return msg_outtrans_attr(str, 0);
1435}
1436
1437 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001438msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1441}
1442
1443 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001444msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 return msg_outtrans_len_attr(str, len, 0);
1447}
1448
1449/*
1450 * Output one character at "p". Return pointer to the next character.
1451 * Handles multi-byte characters.
1452 */
1453 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001454msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 int l;
1457
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001458 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
1460 msg_outtrans_len_attr(p, l, attr);
1461 return p + l;
1462 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001463 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return p + 1;
1465}
1466
1467 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001468msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 int retval = 0;
1471 char_u *str = msgstr;
1472 char_u *plain_start = msgstr;
1473 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 int mb_l;
1475 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477 /* if MSG_HIST flag set, add message to history */
1478 if (attr & MSG_HIST)
1479 {
1480 add_msg_hist(str, len, attr);
1481 attr &= ~MSG_HIST;
1482 }
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 /* If the string starts with a composing character first draw a space on
1485 * which the composing char can be drawn. */
1486 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001487 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 /*
1490 * Go over the string. Special characters are translated and printed.
1491 * Normal characters are printed several at a time.
1492 */
1493 while (--len >= 0)
1494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (enc_utf8)
1496 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001497 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001499 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 else
1501 mb_l = 1;
1502 if (has_mbyte && mb_l > 1)
1503 {
1504 c = (*mb_ptr2char)(str);
1505 if (vim_isprintc(c))
1506 /* printable multi-byte char: count the cells. */
1507 retval += (*mb_ptr2cells)(str);
1508 else
1509 {
1510 /* unprintable multi-byte char: print the printable chars so
1511 * far and the translation of the unprintable char. */
1512 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001513 msg_puts_attr_len((char *)plain_start,
1514 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001516 msg_puts_attr((char *)transchar(c),
1517 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 retval += char2cells(c);
1519 }
1520 len -= mb_l - 1;
1521 str += mb_l;
1522 }
1523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525 s = transchar_byte(*str);
1526 if (s[1] != NUL)
1527 {
1528 /* unprintable char: print the printable chars so far and the
1529 * translation of the unprintable char. */
1530 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001531 msg_puts_attr_len((char *)plain_start,
1532 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001534 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001535 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001537 else
1538 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 ++str;
1540 }
1541 }
1542
1543 if (str > plain_start)
1544 /* print the printable chars at the end */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001545 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
1547 return retval;
1548}
1549
1550#if defined(FEAT_QUICKFIX) || defined(PROTO)
1551 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001552msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 int i;
1555 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1556
1557 arg = skipwhite(arg);
1558 for (i = 5; *arg && i >= 0; --i)
1559 if (*arg++ != str[i])
1560 break;
1561 if (i < 0)
1562 {
1563 msg_putchar('\n');
1564 for (i = 0; rs[i]; ++i)
1565 msg_putchar(rs[i] - 3);
1566 }
1567}
1568#endif
1569
1570/*
1571 * Output the string 'str' upto a NUL character.
1572 * Return the number of characters it takes on the screen.
1573 *
1574 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1575 * following character and shown as <F1>, <S-Up> etc. Any other character
1576 * which is not printable shown in <> form.
1577 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1578 * If a character is displayed in one of these special ways, is also
1579 * highlighted (its highlight name is '8' in the p_hl variable).
1580 * Otherwise characters are not highlighted.
1581 * This function is used to show mappings, where we want to see how to type
1582 * the character/string -- webb
1583 */
1584 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001585msg_outtrans_special(
1586 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001587 int from, // TRUE for lhs of a mapping
1588 int maxlen) // screen columns, 0 for unlimeted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 char_u *str = strstart;
1591 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001592 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 int attr;
1594 int len;
1595
Bram Moolenaar8820b482017-03-16 17:23:31 +01001596 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 while (*str != NUL)
1598 {
1599 /* Leading and trailing spaces need to be displayed in <> form. */
1600 if ((str == strstart || str[1] == NUL) && *str == ' ')
1601 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001602 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 ++str;
1604 }
1605 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001606 text = (char *)str2special(&str, from);
1607 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001608 if (maxlen > 0 && retval + len >= maxlen)
1609 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /* Highlight special keys */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001611 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001612 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 retval += len;
1614 }
1615 return retval;
1616}
1617
Bram Moolenaarbd743252010-10-20 21:23:33 +02001618#if defined(FEAT_EVAL) || defined(PROTO)
1619/*
1620 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1621 * strings, in an allocated string.
1622 */
1623 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001624str2special_save(
1625 char_u *str,
1626 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001627{
1628 garray_T ga;
1629 char_u *p = str;
1630
1631 ga_init2(&ga, 1, 40);
1632 while (*p != NUL)
1633 ga_concat(&ga, str2special(&p, is_lhs));
1634 ga_append(&ga, NUL);
1635 return (char_u *)ga.ga_data;
1636}
1637#endif
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639/*
1640 * Return the printable string for the key codes at "*sp".
1641 * Used for translating the lhs or rhs of a mapping to printable chars.
1642 * Advances "sp" to the next code.
1643 */
1644 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001645str2special(
1646 char_u **sp,
1647 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 int c;
1650 static char_u buf[7];
1651 char_u *str = *sp;
1652 int modifiers = 0;
1653 int special = FALSE;
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (has_mbyte)
1656 {
1657 char_u *p;
1658
1659 /* Try to un-escape a multi-byte character. Return the un-escaped
1660 * string if it is a multi-byte character. */
1661 p = mb_unescape(sp);
1662 if (p != NULL)
1663 return p;
1664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 c = *str;
1667 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1668 {
1669 if (str[1] == KS_MODIFIER)
1670 {
1671 modifiers = str[2];
1672 str += 3;
1673 c = *str;
1674 }
1675 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1676 {
1677 c = TO_SPECIAL(str[1], str[2]);
1678 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
1680 if (IS_SPECIAL(c) || modifiers) /* special key */
1681 special = TRUE;
1682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001684 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001686 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001687
1688 /* For multi-byte characters check for an illegal byte. */
1689 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1690 {
1691 transchar_nonprint(buf, c);
1692 *sp = str + 1;
1693 return buf;
1694 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001695 /* Since 'special' is TRUE the multi-byte character 'c' will be
1696 * processed by get_special_key_name() */
1697 c = (*mb_ptr2char)(str);
1698 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001700 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001701 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1704 * Use <Space> only for lhs of a mapping. */
1705 if (special || char2cells(c) > 1 || (from && c == ' '))
1706 return get_special_key_name(c, modifiers);
1707 buf[0] = c;
1708 buf[1] = NUL;
1709 return buf;
1710}
1711
1712/*
1713 * Translate a key sequence into special key names.
1714 */
1715 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001716str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 char_u *s;
1719
1720 *buf = NUL;
1721 while (*sp)
1722 {
1723 s = str2special(&sp, FALSE);
1724 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1725 STRCAT(buf, s);
1726 }
1727}
1728
1729/*
1730 * print line for :print or :list command
1731 */
1732 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001733msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 int c;
1736 int col = 0;
1737 int n_extra = 0;
1738 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001739 int c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 char_u *p_extra = NULL; /* init to make SASC shut up */
1741 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001742 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 int l;
1745 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaardf177f62005-02-22 08:39:57 +00001747 if (curwin->w_p_list)
1748 list = TRUE;
1749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001751 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001754 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 --trail;
1756 }
1757
1758 /* output a space for an empty line, otherwise the line will be
1759 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001760 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 msg_putchar(' ');
1762
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001763 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001765 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
1767 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001768 if (n_extra == 0 && c_final)
1769 c = c_final;
1770 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 c = c_extra;
1772 else
1773 c = *p_extra++;
1774 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001775 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
1777 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001778 if (lcs_nbsp != NUL && list
1779 && (mb_ptr2char(s) == 160
1780 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001781 {
1782 mb_char2bytes(lcs_nbsp, buf);
1783 buf[(*mb_ptr2len)(buf)] = NUL;
1784 }
1785 else
1786 {
1787 mch_memmove(buf, s, (size_t)l);
1788 buf[l] = NUL;
1789 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001790 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 s += l;
1792 continue;
1793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 else
1795 {
1796 attr = 0;
1797 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001798 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
1800 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001801#ifdef FEAT_VARTABS
1802 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1803 curbuf->b_p_vts_array) - 1;
1804#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001806#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001807 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
1809 c = ' ';
1810 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001811 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 else
1814 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001815 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001817 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001818 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001821 else if (c == 160 && list && lcs_nbsp != NUL)
1822 {
1823 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001824 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001825 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001826 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 {
1828 p_extra = (char_u *)"";
1829 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001830 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 n_extra = 1;
1832 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001833 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 --s;
1835 }
1836 else if (c != NUL && (n = byte2cells(c)) > 1)
1837 {
1838 n_extra = n - 1;
1839 p_extra = transchar_byte(c);
1840 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001841 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001843 /* Use special coloring to be able to distinguish <hex> from
1844 * the same in plain text. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001845 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 else if (c == ' ' && trail != NULL && s > trail)
1848 {
1849 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001850 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001852 else if (c == ' ' && list && lcs_space != NUL)
1853 {
1854 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001855 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858
1859 if (c == NUL)
1860 break;
1861
1862 msg_putchar_attr(c, attr);
1863 col++;
1864 }
1865 msg_clr_eos();
1866}
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868/*
1869 * Use screen_puts() to output one multi-byte character.
1870 * Return the pointer "s" advanced to the next character.
1871 */
1872 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001873screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
1875 int cw;
1876
1877 msg_didout = TRUE; /* remember that line is not empty */
1878 cw = (*mb_ptr2cells)(s);
1879 if (cw > 1 && (
1880#ifdef FEAT_RIGHTLEFT
1881 cmdmsg_rl ? msg_col <= 1 :
1882#endif
1883 msg_col == Columns - 1))
1884 {
1885 /* Doesn't fit, print a highlighted '>' to fill it up. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001886 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 return s;
1888 }
1889
1890 screen_puts_len(s, l, msg_row, msg_col, attr);
1891#ifdef FEAT_RIGHTLEFT
1892 if (cmdmsg_rl)
1893 {
1894 msg_col -= cw;
1895 if (msg_col == 0)
1896 {
1897 msg_col = Columns;
1898 ++msg_row;
1899 }
1900 }
1901 else
1902#endif
1903 {
1904 msg_col += cw;
1905 if (msg_col >= Columns)
1906 {
1907 msg_col = 0;
1908 ++msg_row;
1909 }
1910 }
1911 return s + l;
1912}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914/*
1915 * Output a string to the screen at position msg_row, msg_col.
1916 * Update msg_row and msg_col for the next message.
1917 */
1918 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001919msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001921 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922}
1923
1924 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001925msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001927 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928}
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
1931 * Show a message in such a way that it always fits in the line. Cut out a
1932 * part in the middle and replace it with "..." when necessary.
1933 * Does not handle multi-byte characters!
1934 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001935 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001936msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
1938 int slen = len;
1939 int room;
1940
1941 room = Columns - msg_col;
1942 if (len > room && room >= 20)
1943 {
1944 slen = (room - 3) / 2;
1945 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001946 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1949}
1950
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001951 void
1952msg_outtrans_long_attr(char_u *longstr, int attr)
1953{
1954 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
1955}
1956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957/*
1958 * Basic function for writing a message with highlight attributes.
1959 */
1960 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001961msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 msg_puts_attr_len(s, -1, attr);
1964}
1965
1966/*
1967 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1968 * When "maxlen" is -1 there is no maximum length.
1969 * When "maxlen" is >= 0 the message is not put in the history.
1970 */
1971 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001972msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 /*
1975 * If redirection is on, also write to the redirection file.
1976 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001977 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979 /*
1980 * Don't print anything when using ":silent cmd".
1981 */
1982 if (msg_silent != 0)
1983 return;
1984
1985 /* if MSG_HIST flag set, add message to history */
1986 if ((attr & MSG_HIST) && maxlen < 0)
1987 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001988 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 attr &= ~MSG_HIST;
1990 }
1991
1992 /*
1993 * When writing something to the screen after it has scrolled, requires a
1994 * wait-return prompt later. Needed when scrolling, resetting
1995 * need_wait_return after some prompt, and then outputting something
1996 * without scrolling
1997 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001998 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 need_wait_return = TRUE;
2000 msg_didany = TRUE; /* remember that something was outputted */
2001
2002 /*
2003 * If there is no valid screen, use fprintf so we can see error messages.
2004 * If termcap is not active, we may be writing in an alternate console
2005 * window, cursor positioning may not work correctly (window size may be
2006 * different, e.g. for Win32 console) or we just don't know where the
2007 * cursor is.
2008 */
2009 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002010 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002011 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002012 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002015/*
2016 * The display part of msg_puts_attr_len().
2017 * May be called recursively to display scroll-back text.
2018 */
2019 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002020msg_puts_display(
2021 char_u *str,
2022 int maxlen,
2023 int attr,
2024 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002025{
2026 char_u *s = str;
2027 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2028 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002029 int l;
2030 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002031 char_u *sb_str = str;
2032 int sb_col = msg_col;
2033 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002034 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002037 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002040 * We are at the end of the screen line when:
2041 * - When outputting a newline.
2042 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002044 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#ifdef FEAT_RIGHTLEFT
2046 cmdmsg_rl
2047 ? (
2048 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002049 || (*s == TAB && msg_col <= 7)
2050 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 :
2052#endif
2053 (msg_col + t_col >= Columns - 1
2054 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002056 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002058 /*
2059 * The screen is scrolled up when at the last row (some terminals
2060 * scroll automatically, some don't. To avoid problems we scroll
2061 * ourselves).
2062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002065 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002067 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (msg_no_more && lines_left == 0)
2069 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002071 /* Scroll the screen up one line. */
2072 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 msg_row = Rows - 2;
2075 if (msg_col >= Columns) /* can happen after screen resize */
2076 msg_col = Columns - 1;
2077
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002078 /* Display char in last column before showing more-prompt. */
2079 if (*s >= ' '
2080#ifdef FEAT_RIGHTLEFT
2081 && !cmdmsg_rl
2082#endif
2083 )
2084 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002085 if (has_mbyte)
2086 {
2087 if (enc_utf8 && maxlen >= 0)
2088 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002089 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002090 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002091 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002092 s = screen_puts_mbyte(s, l, attr);
2093 }
2094 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002095 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002096 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002097 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002098 else
2099 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002100
2101 if (p_more)
2102 /* store text for scrolling back */
2103 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2104
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002105 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002106 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 redraw_cmdline = TRUE;
2108 if (cmdline_row > 0 && !exmode_active)
2109 --cmdline_row;
2110
2111 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002112 * If screen is completely filled and 'more' is set then wait
2113 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002115 if (lines_left > 0)
2116 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002117 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 && !msg_no_more && !exmode_active)
2119 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002121 if (do_more_prompt(NUL))
2122 s = confirm_msg_tail;
2123#else
2124 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#endif
2126 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002127 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002129
2130 /* When we displayed a char in last column need to check if there
2131 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002132 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002133 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002136 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002139 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002140 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2141 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002143 t_puts(&t_col, t_s, s, attr);
2144
2145 if (wrap && p_more && !recurse)
2146 /* store text for scrolling back */
2147 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
2149 if (*s == '\n') /* go to next line */
2150 {
2151 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002152#ifdef FEAT_RIGHTLEFT
2153 if (cmdmsg_rl)
2154 msg_col = Columns - 1;
2155 else
2156#endif
2157 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (++msg_row >= Rows) /* safety check */
2159 msg_row = Rows - 1;
2160 }
2161 else if (*s == '\r') /* go to column 0 */
2162 {
2163 msg_col = 0;
2164 }
2165 else if (*s == '\b') /* go to previous char */
2166 {
2167 if (msg_col)
2168 --msg_col;
2169 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002170 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
2172 do
2173 msg_screen_putchar(' ', attr);
2174 while (msg_col & 7);
2175 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002176 else if (*s == BELL) /* beep (from ":sh") */
2177 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 else
2179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (has_mbyte)
2181 {
2182 cw = (*mb_ptr2cells)(s);
2183 if (enc_utf8 && maxlen >= 0)
2184 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002185 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002187 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 }
2189 else
2190 {
2191 cw = 1;
2192 l = 1;
2193 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 /* When drawing from right to left or when a double-wide character
2196 * doesn't fit, draw a single character here. Otherwise collect
2197 * characters and draw them all at once later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (
2199# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002200 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002202 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (l > 1)
2205 s = screen_puts_mbyte(s, l, attr) - 1;
2206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 msg_screen_putchar(*s, attr);
2208 }
2209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
2211 /* postpone this character until later */
2212 if (t_col == 0)
2213 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 t_col += cw;
2215 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 }
2218 ++s;
2219 }
2220
2221 /* output any postponed text */
2222 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002223 t_puts(&t_col, t_s, s, attr);
2224 if (p_more && !recurse)
2225 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 msg_check();
2228}
2229
2230/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002231 * Return TRUE when ":filter pattern" was used and "msg" does not match
2232 * "pattern".
2233 */
2234 int
2235message_filtered(char_u *msg)
2236{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002237 int match;
2238
2239 if (cmdmod.filter_regmatch.regprog == NULL)
2240 return FALSE;
2241 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2242 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002243}
2244
2245/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002246 * Scroll the screen up one line for displaying the next message line.
2247 */
2248 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002249msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002250{
2251#ifdef FEAT_GUI
2252 /* Remove the cursor before scrolling, ScreenLines[] is going
2253 * to become invalid. */
2254 if (gui.in_use)
2255 gui_undraw_cursor();
2256#endif
2257 /* scrolling up always works */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002258 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002259 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002260 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002261
2262 if (!can_clear((char_u *)" "))
2263 {
2264 /* Scrolling up doesn't result in the right background. Set the
2265 * background here. It's not efficient, but avoids that we have to do
2266 * it all over the code. */
2267 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2268
2269 /* Also clear the last char of the last but one line if it was not
2270 * cleared before to avoid a scroll-up. */
2271 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2272 screen_fill((int)Rows - 2, (int)Rows - 1,
2273 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2274 }
2275}
2276
2277/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002278 * Increment "msg_scrolled".
2279 */
2280 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002281inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002282{
2283#ifdef FEAT_EVAL
2284 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2285 {
2286 char_u *p = sourcing_name;
2287 char_u *tofree = NULL;
2288 int len;
2289
2290 /* v:scrollstart is empty, set it to the script/function name and line
2291 * number */
2292 if (p == NULL)
2293 p = (char_u *)_("Unknown");
2294 else
2295 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002296 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002297 tofree = alloc(len);
2298 if (tofree != NULL)
2299 {
2300 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2301 p, (long)sourcing_lnum);
2302 p = tofree;
2303 }
2304 }
2305 set_vim_var_string(VV_SCROLLSTART, p, -1);
2306 vim_free(tofree);
2307 }
2308#endif
2309 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002310 if (must_redraw < VALID)
2311 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002312}
2313
2314/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002315 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2316 * store the displayed text and remember where screen lines start.
2317 */
2318typedef struct msgchunk_S msgchunk_T;
2319struct msgchunk_S
2320{
2321 msgchunk_T *sb_next;
2322 msgchunk_T *sb_prev;
2323 char sb_eol; /* TRUE when line ends after this text */
2324 int sb_msg_col; /* column in which text starts */
2325 int sb_attr; /* text attributes */
2326 char_u sb_text[1]; /* text to be displayed, actually longer */
2327};
2328
2329static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2330
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002331static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002332
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002333typedef enum {
2334 SB_CLEAR_NONE = 0,
2335 SB_CLEAR_ALL,
2336 SB_CLEAR_CMDLINE_BUSY,
2337 SB_CLEAR_CMDLINE_DONE
2338} sb_clear_T;
2339
2340/* When to clear text on next msg. */
2341static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002342
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002343/*
2344 * Store part of a printed message for displaying when scrolling back.
2345 */
2346 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002347store_sb_text(
2348 char_u **sb_str, /* start of string */
2349 char_u *s, /* just after string */
2350 int attr,
2351 int *sb_col,
2352 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002353{
2354 msgchunk_T *mp;
2355
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002356 if (do_clear_sb_text == SB_CLEAR_ALL
2357 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002358 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002359 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2360 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002361 }
2362
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002363 if (s > *sb_str)
2364 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002365 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002366 if (mp != NULL)
2367 {
2368 mp->sb_eol = finish;
2369 mp->sb_msg_col = *sb_col;
2370 mp->sb_attr = attr;
2371 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2372
2373 if (last_msgchunk == NULL)
2374 {
2375 last_msgchunk = mp;
2376 mp->sb_prev = NULL;
2377 }
2378 else
2379 {
2380 mp->sb_prev = last_msgchunk;
2381 last_msgchunk->sb_next = mp;
2382 last_msgchunk = mp;
2383 }
2384 mp->sb_next = NULL;
2385 }
2386 }
2387 else if (finish && last_msgchunk != NULL)
2388 last_msgchunk->sb_eol = TRUE;
2389
2390 *sb_str = s;
2391 *sb_col = 0;
2392}
2393
2394/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002395 * Finished showing messages, clear the scroll-back text on the next message.
2396 */
2397 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002398may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002399{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002400 do_clear_sb_text = SB_CLEAR_ALL;
2401}
2402
2403/*
2404 * Starting to edit the command line, do not clear messages now.
2405 */
2406 void
2407sb_text_start_cmdline(void)
2408{
2409 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2410 msg_sb_eol();
2411}
2412
2413/*
2414 * Ending to edit the command line. Clear old lines but the last one later.
2415 */
2416 void
2417sb_text_end_cmdline(void)
2418{
2419 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002420}
2421
2422/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002423 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002424 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002425 * Called when redrawing the screen.
2426 */
2427 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002428clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002429{
2430 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002431 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002432
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002433 if (all)
2434 lastp = &last_msgchunk;
2435 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002437 if (last_msgchunk == NULL)
2438 return;
2439 lastp = &last_msgchunk->sb_prev;
2440 }
2441
2442 while (*lastp != NULL)
2443 {
2444 mp = (*lastp)->sb_prev;
2445 vim_free(*lastp);
2446 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002447 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002448}
2449
2450/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002451 * "g<" command.
2452 */
2453 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002454show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002455{
2456 msgchunk_T *mp;
2457
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002458 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002459 * weird, typing a command without output results in one line. */
2460 mp = msg_sb_start(last_msgchunk);
2461 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002462 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002463 else
2464 {
2465 do_more_prompt('G');
2466 wait_return(FALSE);
2467 }
2468}
2469
2470/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002471 * Move to the start of screen line in already displayed text.
2472 */
2473 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002474msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002475{
2476 msgchunk_T *mp = mps;
2477
2478 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2479 mp = mp->sb_prev;
2480 return mp;
2481}
2482
2483/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002484 * Mark the last message chunk as finishing the line.
2485 */
2486 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002487msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002488{
2489 if (last_msgchunk != NULL)
2490 last_msgchunk->sb_eol = TRUE;
2491}
2492
2493/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002494 * Display a screen line from previously displayed text at row "row".
2495 * Returns a pointer to the text for the next line (can be NULL).
2496 */
2497 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002498disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002499{
2500 msgchunk_T *mp = smp;
2501 char_u *p;
2502
2503 for (;;)
2504 {
2505 msg_row = row;
2506 msg_col = mp->sb_msg_col;
2507 p = mp->sb_text;
2508 if (*p == '\n') /* don't display the line break */
2509 ++p;
2510 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2511 if (mp->sb_eol || mp->sb_next == NULL)
2512 break;
2513 mp = mp->sb_next;
2514 }
2515 return mp->sb_next;
2516}
2517
2518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Output any postponed text for msg_puts_attr_len().
2520 */
2521 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002522t_puts(
2523 int *t_col,
2524 char_u *t_s,
2525 char_u *s,
2526 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528 /* output postponed text */
2529 msg_didout = TRUE; /* remember that line is not empty */
2530 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002531 msg_col += *t_col;
2532 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 /* If the string starts with a composing character don't increment the
2534 * column position for it. */
2535 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2536 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (msg_col >= Columns)
2538 {
2539 msg_col = 0;
2540 ++msg_row;
2541 }
2542}
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
2545 * Returns TRUE when messages should be printed with mch_errmsg().
2546 * This is used when there is no valid screen, so we can see error messages.
2547 * If termcap is not active, we may be writing in an alternate console
2548 * window, cursor positioning may not work correctly (window size may be
2549 * different, e.g. for Win32 console) or we just don't know where the
2550 * cursor is.
2551 */
2552 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002553msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002556#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2557# ifdef VIMDLL
2558 || (!gui.in_use && !termcap_active)
2559# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#endif
2563 || (swapping_screen() && !termcap_active)
2564 );
2565}
2566
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002567/*
2568 * Print a message when there is no valid screen.
2569 */
2570 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002571msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002572{
2573 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002574 char_u *buf = NULL;
2575 char_u *p = s;
2576
Bram Moolenaar4f974752019-02-17 17:44:42 +01002577#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002578 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002579 mch_settmode(TMODE_COOK); /* handle CR and NL correctly */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002580#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002581 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002582 {
2583 if (!(silent_mode && p_verbose == 0))
2584 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002585 // NL --> CR NL translation (for Unix, not for "--version")
2586 if (*s == NL)
2587 {
2588 int n = (int)(s - p);
2589
2590 buf = alloc(n + 3);
2591 memcpy(buf, p, n);
2592 if (!info_message)
2593 buf[n++] = CAR;
Bram Moolenaar00590742019-02-15 21:06:09 +01002594 buf[n++] = NL;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002595 buf[n++] = NUL;
2596 if (info_message) // informative message, not an error
2597 mch_msg((char *)buf);
2598 else
2599 mch_errmsg((char *)buf);
2600 vim_free(buf);
2601 p = s + 1;
2602 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002603 }
2604
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002605 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606#ifdef FEAT_RIGHTLEFT
2607 if (cmdmsg_rl)
2608 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002609 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002610 msg_col = Columns - 1;
2611 else
2612 --msg_col;
2613 }
2614 else
2615#endif
2616 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002617 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618 msg_col = 0;
2619 else
2620 ++msg_col;
2621 }
2622 ++s;
2623 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002624
2625 if (*p != NUL && !(silent_mode && p_verbose == 0))
2626 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002627 int c = -1;
2628
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002629 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002630 {
2631 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002632 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002633 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002634 if (info_message)
2635 mch_msg((char *)p);
2636 else
2637 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002638 if (c != -1)
2639 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002640 }
2641
2642 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002643
Bram Moolenaar4f974752019-02-17 17:44:42 +01002644#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645 if (!(silent_mode && p_verbose == 0))
2646 mch_settmode(TMODE_RAW);
2647#endif
2648}
2649
2650/*
2651 * Show the more-prompt and handle the user response.
2652 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002653 * When at hit-enter prompt "typed_char" is the already typed character,
2654 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002655 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2656 */
2657 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002658do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002659{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002660 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002661 int used_typed_char = typed_char;
2662 int oldState = State;
2663 int c;
2664#ifdef FEAT_CON_DIALOG
2665 int retval = FALSE;
2666#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002667 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002668 msgchunk_T *mp_last = NULL;
2669 msgchunk_T *mp;
2670 int i;
2671
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002672 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002673 * that case don't show another prompt. Also when at the hit-Enter prompt
2674 * and nothing was typed. */
2675 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002676 return FALSE;
2677 entered = TRUE;
2678
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002679 if (typed_char == 'G')
2680 {
2681 /* "g<": Find first line on the last page. */
2682 mp_last = msg_sb_start(last_msgchunk);
2683 for (i = 0; i < Rows - 2 && mp_last != NULL
2684 && mp_last->sb_prev != NULL; ++i)
2685 mp_last = msg_sb_start(mp_last->sb_prev);
2686 }
2687
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002688 State = ASKMORE;
2689#ifdef FEAT_MOUSE
2690 setmouse();
2691#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002692 if (typed_char == NUL)
2693 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002694 for (;;)
2695 {
2696 /*
2697 * Get a typed character directly from the user.
2698 */
2699 if (used_typed_char != NUL)
2700 {
2701 c = used_typed_char; /* was typed at hit-enter prompt */
2702 used_typed_char = NUL;
2703 }
2704 else
2705 c = get_keystroke();
2706
2707#if defined(FEAT_MENU) && defined(FEAT_GUI)
2708 if (c == K_MENU)
2709 {
2710 int idx = get_menu_index(current_menu, ASKMORE);
2711
2712 /* Used a menu. If it starts with CTRL-Y, it must
2713 * be a "Copy" for the clipboard. Otherwise
2714 * assume that we end */
2715 if (idx == MENU_INDEX_INVALID)
2716 continue;
2717 c = *current_menu->strings[idx];
2718 if (c != NUL && current_menu->strings[idx][1] != NUL)
2719 ins_typebuf(current_menu->strings[idx] + 1,
2720 current_menu->noremap[idx], 0, TRUE,
2721 current_menu->silent[idx]);
2722 }
2723#endif
2724
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002725 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002726 switch (c)
2727 {
2728 case BS: /* scroll one line back */
2729 case K_BS:
2730 case 'k':
2731 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002732 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733 break;
2734
2735 case CAR: /* one extra line */
2736 case NL:
2737 case 'j':
2738 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002739 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 break;
2741
2742 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002743 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002744 break;
2745
2746 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002747 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002748 break;
2749
2750 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002751 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002752 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002753 break;
2754
2755 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002756 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002757 case K_PAGEDOWN:
2758 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002759 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002760 break;
2761
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002762 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002763 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002764 break;
2765
2766 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002767 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002768 lines_left = 999999;
2769 break;
2770
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002771 case ':': /* start new command line */
2772#ifdef FEAT_CON_DIALOG
2773 if (!confirm_msg_used)
2774#endif
2775 {
2776 /* Since got_int is set all typeahead will be flushed, but we
2777 * want to keep this ':', remember that in a special way. */
2778 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002779#ifdef FEAT_TERMINAL
2780 skip_term_loop = TRUE;
2781#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782 cmdline_row = Rows - 1; /* put ':' on this line */
2783 skip_redraw = TRUE; /* skip redraw once */
2784 need_wait_return = FALSE; /* don't wait in main() */
2785 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002786 /* FALLTHROUGH */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787 case 'q': /* quit */
2788 case Ctrl_C:
2789 case ESC:
2790#ifdef FEAT_CON_DIALOG
2791 if (confirm_msg_used)
2792 {
2793 /* Jump to the choices of the dialog. */
2794 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 }
2796 else
2797#endif
2798 {
2799 got_int = TRUE;
2800 quit_more = TRUE;
2801 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002802 /* When there is some more output (wrapping line) display that
2803 * without another prompt. */
2804 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002805 break;
2806
2807#ifdef FEAT_CLIPBOARD
2808 case Ctrl_Y:
2809 /* Strange way to allow copying (yanking) a modeless
2810 * selection at the more prompt. Use CTRL-Y,
2811 * because the same is used in Cmdline-mode and at the
2812 * hit-enter prompt. However, scrolling one line up
2813 * might be expected... */
2814 if (clip_star.state == SELECT_DONE)
2815 clip_copy_modeless_selection(TRUE);
2816 continue;
2817#endif
2818 default: /* no valid response */
2819 msg_moremsg(TRUE);
2820 continue;
2821 }
2822
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002823 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002824 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002825 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002826 {
2827 /* go to start of last line */
2828 if (mp_last == NULL)
2829 mp = msg_sb_start(last_msgchunk);
2830 else if (mp_last->sb_prev != NULL)
2831 mp = msg_sb_start(mp_last->sb_prev);
2832 else
2833 mp = NULL;
2834
2835 /* go to start of line at top of the screen */
2836 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2837 ++i)
2838 mp = msg_sb_start(mp->sb_prev);
2839
2840 if (mp != NULL && mp->sb_prev != NULL)
2841 {
2842 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002843 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002844 {
2845 if (mp == NULL || mp->sb_prev == NULL)
2846 break;
2847 mp = msg_sb_start(mp->sb_prev);
2848 if (mp_last == NULL)
2849 mp_last = msg_sb_start(last_msgchunk);
2850 else
2851 mp_last = msg_sb_start(mp_last->sb_prev);
2852 }
2853
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002854 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002855 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002856 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002857 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 (void)disp_sb_line(0, mp);
2859 }
2860 else
2861 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002862 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002863 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002864 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2865 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002867 ++msg_scrolled;
2868 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002869 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002870 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002871 }
2872 }
2873 else
2874 {
2875 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002876 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002877 {
2878 /* scroll up, display line at bottom */
2879 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002880 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002881 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2882 (int)Columns, ' ', ' ', 0);
2883 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002884 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 }
2886 }
2887
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002888 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002889 {
2890 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002891 screen_fill((int)Rows - 1, (int)Rows, 0,
2892 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002893 msg_moremsg(FALSE);
2894 continue;
2895 }
2896
2897 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002898 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002899 }
2900
2901 break;
2902 }
2903
2904 /* clear the --more-- message */
2905 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2906 State = oldState;
2907#ifdef FEAT_MOUSE
2908 setmouse();
2909#endif
2910 if (quit_more)
2911 {
2912 msg_row = Rows - 1;
2913 msg_col = 0;
2914 }
2915#ifdef FEAT_RIGHTLEFT
2916 else if (cmdmsg_rl)
2917 msg_col = Columns - 1;
2918#endif
2919
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002920 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002921#ifdef FEAT_CON_DIALOG
2922 return retval;
2923#else
2924 return FALSE;
2925#endif
2926}
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2929
2930#ifdef mch_errmsg
2931# undef mch_errmsg
2932#endif
2933#ifdef mch_msg
2934# undef mch_msg
2935#endif
2936
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002937#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2938 static void
2939mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01002941 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002942 DWORD nwrite = 0;
2943 DWORD mode = 0;
2944 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
2945
2946 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
2947 && (int)GetConsoleCP() != enc_codepage)
2948 {
2949 WCHAR *w = enc_to_utf16((char_u *)str, &len);
2950
2951 WriteConsoleW(h, w, len, &nwrite, NULL);
2952 vim_free(w);
2953 }
2954 else
2955 {
2956 fprintf(stderr, "%s", str);
2957 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002958}
2959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002961/*
2962 * Give an error message. To be used when the screen hasn't been initialized
2963 * yet. When stderr can't be used, collect error messages until the GUI has
2964 * started and they can be displayed in a message box.
2965 */
2966 void
2967mch_errmsg(char *str)
2968{
2969#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
2970 int len;
2971#endif
2972
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02002973#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 /* On Unix use stderr if it's a tty.
2975 * When not going to start the GUI also use stderr.
2976 * On Mac, when started from Finder, stderr is the console. */
2977 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002978# ifdef UNIX
2979# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002981# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 isatty(2)
2983# endif
2984# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002985 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002986# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002987# endif
2988# ifdef FEAT_GUI
2989 !(gui.in_use || gui.starting)
2990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 )
2992 {
2993 fprintf(stderr, "%s", str);
2994 return;
2995 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002998#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2999# ifdef VIMDLL
3000 if (!(gui.in_use || gui.starting))
3001# endif
3002 {
3003 mch_errmsg_c(str);
3004 return;
3005 }
3006#endif
3007
3008#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 /* avoid a delay for a message that isn't there */
3010 emsg_on_display = FALSE;
3011
3012 len = (int)STRLEN(str) + 1;
3013 if (error_ga.ga_growsize == 0)
3014 {
3015 error_ga.ga_growsize = 80;
3016 error_ga.ga_itemsize = 1;
3017 }
3018 if (ga_grow(&error_ga, len) == OK)
3019 {
3020 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3021 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003022# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 /* remove CR characters, they are displayed */
3024 {
3025 char_u *p;
3026
3027 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3028 for (;;)
3029 {
3030 p = vim_strchr(p, '\r');
3031 if (p == NULL)
3032 break;
3033 *p = ' ';
3034 }
3035 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 --len; /* don't count the NUL at the end */
3038 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041}
3042
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003043#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3044 static void
3045mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003047 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003048 DWORD nwrite = 0;
3049 DWORD mode;
3050 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3051
3052
3053 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3054 && (int)GetConsoleCP() != enc_codepage)
3055 {
3056 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3057
3058 WriteConsoleW(h, w, len, &nwrite, NULL);
3059 vim_free(w);
3060 }
3061 else
3062 {
3063 printf("%s", str);
3064 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003065}
3066#endif
3067
3068/*
3069 * Give a message. To be used when the screen hasn't been initialized yet.
3070 * When there is no tty, collect messages until the GUI has started and they
3071 * can be displayed in a message box.
3072 */
3073 void
3074mch_msg(char *str)
3075{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003076#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3078 * uses mch_errmsg() when started from the desktop.
3079 * When not going to start the GUI also use stdout.
3080 * On Mac, when started from Finder, stderr is the console. */
3081 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003082# ifdef UNIX
3083# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003085# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003089 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003091# endif
3092# ifdef FEAT_GUI
3093 !(gui.in_use || gui.starting)
3094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 )
3096 {
3097 printf("%s", str);
3098 return;
3099 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003100#endif
3101
3102#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3103# ifdef VIMDLL
3104 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003106 {
3107 mch_msg_c(str);
3108 return;
3109 }
3110#endif
3111#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114}
3115#endif /* USE_MCH_ERRMSG */
3116
3117/*
3118 * Put a character on the screen at the current message position and advance
3119 * to the next position. Only for printable ASCII!
3120 */
3121 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003122msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 msg_didout = TRUE; /* remember that line is not empty */
3125 screen_putchar(c, msg_row, msg_col, attr);
3126#ifdef FEAT_RIGHTLEFT
3127 if (cmdmsg_rl)
3128 {
3129 if (--msg_col == 0)
3130 {
3131 msg_col = Columns;
3132 ++msg_row;
3133 }
3134 }
3135 else
3136#endif
3137 {
3138 if (++msg_col >= Columns)
3139 {
3140 msg_col = 0;
3141 ++msg_row;
3142 }
3143 }
3144}
3145
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003146 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003147msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003149 int attr;
3150 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaar8820b482017-03-16 17:23:31 +01003152 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003153 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003155 screen_puts((char_u *)
3156 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3157 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158}
3159
3160/*
3161 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3162 * exmode_active.
3163 */
3164 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003165repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 if (State == ASKMORE)
3168 {
3169 msg_moremsg(TRUE); /* display --more-- message again */
3170 msg_row = Rows - 1;
3171 }
3172#ifdef FEAT_CON_DIALOG
3173 else if (State == CONFIRM)
3174 {
3175 display_confirm_msg(); /* display ":confirm" message again */
3176 msg_row = Rows - 1;
3177 }
3178#endif
3179 else if (State == EXTERNCMD)
3180 {
3181 windgoto(msg_row, msg_col); /* put cursor back */
3182 }
3183 else if (State == HITRETURN || State == SETWSIZE)
3184 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003185 if (msg_row == Rows - 1)
3186 {
3187 /* Avoid drawing the "hit-enter" prompt below the previous one,
3188 * overwrite it. Esp. useful when regaining focus and a
3189 * FocusGained autocmd exists but didn't draw anything. */
3190 msg_didout = FALSE;
3191 msg_col = 0;
3192 msg_clr_eos();
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 hit_return_msg();
3195 msg_row = Rows - 1;
3196 }
3197}
3198
3199/*
3200 * msg_check_screen - check if the screen is initialized.
3201 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3202 * While starting the GUI the terminal codes will be set for the GUI, but the
3203 * output goes to the terminal. Don't use the terminal codes then.
3204 */
3205 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003206msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
3208 if (!full_screen || !screen_valid(FALSE))
3209 return FALSE;
3210
3211 if (msg_row >= Rows)
3212 msg_row = Rows - 1;
3213 if (msg_col >= Columns)
3214 msg_col = Columns - 1;
3215 return TRUE;
3216}
3217
3218/*
3219 * Clear from current message position to end of screen.
3220 * Skip this when ":silent" was used, no need to clear for redirection.
3221 */
3222 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003223msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 if (msg_silent == 0)
3226 msg_clr_eos_force();
3227}
3228
3229/*
3230 * Clear from current message position to end of screen.
3231 * Note: msg_col is not updated, so we remember the end of the message
3232 * for msg_check().
3233 */
3234 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003235msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 if (msg_use_printf())
3238 {
3239 if (full_screen) /* only when termcap codes are valid */
3240 {
3241 if (*T_CD)
3242 out_str(T_CD); /* clear to end of display */
3243 else if (*T_CE)
3244 out_str(T_CE); /* clear to end of line */
3245 }
3246 }
3247 else
3248 {
3249#ifdef FEAT_RIGHTLEFT
3250 if (cmdmsg_rl)
3251 {
3252 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3253 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3254 }
3255 else
3256#endif
3257 {
3258 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3259 ' ', ' ', 0);
3260 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3261 }
3262 }
3263}
3264
3265/*
3266 * Clear the command line.
3267 */
3268 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003269msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 msg_row = cmdline_row;
3272 msg_col = 0;
3273 msg_clr_eos_force();
3274}
3275
3276/*
3277 * end putting a message on the screen
3278 * call wait_return if the message does not fit in the available space
3279 * return TRUE if wait_return not called.
3280 */
3281 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003282msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003285 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 * or the ruler option is set and we run into it,
3287 * we have to redraw the window.
3288 * Do not do this if we are abandoning the file or editing the command line.
3289 */
3290 if (!exiting && need_wait_return && !(State & CMDLINE))
3291 {
3292 wait_return(FALSE);
3293 return FALSE;
3294 }
3295 out_flush();
3296 return TRUE;
3297}
3298
3299/*
3300 * If the written message runs into the shown command or ruler, we have to
3301 * wait for hit-return and redraw the window later.
3302 */
3303 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003304msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 if (msg_row == Rows - 1 && msg_col >= sc_col)
3307 {
3308 need_wait_return = TRUE;
3309 redraw_cmdline = TRUE;
3310 }
3311}
3312
3313/*
3314 * May write a string to the redirection file.
3315 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3316 */
3317 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003318redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 char_u *s = str;
3321 static int cur_col = 0;
3322
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003323 /* Don't do anything for displaying prompts and the like. */
3324 if (redir_off)
3325 return;
3326
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003327 /* If 'verbosefile' is set prepare for writing in that file. */
3328 if (*p_vfile != NUL && verbose_fd == NULL)
3329 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003330
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003331 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
3333 /* If the string doesn't start with CR or NL, go to msg_col */
3334 if (*s != '\n' && *s != '\r')
3335 {
3336 while (cur_col < msg_col)
3337 {
3338#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003339 if (redir_execute)
3340 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003341 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003343 else if (redir_vname)
3344 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003347 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003349 if (verbose_fd != NULL)
3350 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 ++cur_col;
3352 }
3353 }
3354
3355#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003356 if (redir_execute)
3357 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003358 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003360 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003361 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#endif
3363
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003364 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3366 {
3367#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003368 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003370 if (redir_fd != NULL)
3371 putc(*s, redir_fd);
3372 if (verbose_fd != NULL)
3373 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (*s == '\r' || *s == '\n')
3375 cur_col = 0;
3376 else if (*s == '\t')
3377 cur_col += (8 - cur_col % 8);
3378 else
3379 ++cur_col;
3380 ++s;
3381 }
3382
3383 if (msg_silent != 0) /* should update msg_col */
3384 msg_col = cur_col;
3385 }
3386}
3387
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003388 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003389redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003390{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003391 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003392#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003393 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003394#endif
3395 ;
3396}
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003399 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003400 * Must always be called paired with verbose_leave()!
3401 */
3402 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003403verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003404{
3405 if (*p_vfile != NUL)
3406 ++msg_silent;
3407}
3408
3409/*
3410 * After giving verbose message.
3411 * Must always be called paired with verbose_enter()!
3412 */
3413 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003414verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003415{
3416 if (*p_vfile != NUL)
3417 if (--msg_silent < 0)
3418 msg_silent = 0;
3419}
3420
3421/*
3422 * Like verbose_enter() and set msg_scroll when displaying the message.
3423 */
3424 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003425verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003426{
3427 if (*p_vfile != NUL)
3428 ++msg_silent;
3429 else
3430 /* always scroll up, don't overwrite */
3431 msg_scroll = TRUE;
3432}
3433
3434/*
3435 * Like verbose_leave() and set cmdline_row when displaying the message.
3436 */
3437 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003438verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003439{
3440 if (*p_vfile != NUL)
3441 {
3442 if (--msg_silent < 0)
3443 msg_silent = 0;
3444 }
3445 else
3446 cmdline_row = msg_row;
3447}
3448
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003449/*
3450 * Called when 'verbosefile' is set: stop writing to the file.
3451 */
3452 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003453verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003454{
3455 if (verbose_fd != NULL)
3456 {
3457 fclose(verbose_fd);
3458 verbose_fd = NULL;
3459 }
3460 verbose_did_open = FALSE;
3461}
3462
3463/*
3464 * Open the file 'verbosefile'.
3465 * Return FAIL or OK.
3466 */
3467 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003468verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003469{
3470 if (verbose_fd == NULL && !verbose_did_open)
3471 {
3472 /* Only give the error message once. */
3473 verbose_did_open = TRUE;
3474
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003475 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003476 if (verbose_fd == NULL)
3477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003478 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003479 return FAIL;
3480 }
3481 }
3482 return OK;
3483}
3484
3485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 * Give a warning message (for searching).
3487 * Use 'w' highlighting and may repeat the message after redrawing
3488 */
3489 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003490give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491{
3492 /* Don't do this for ":silent". */
3493 if (msg_silent != 0)
3494 return;
3495
3496 /* Don't want a hit-enter prompt here. */
3497 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499#ifdef FEAT_EVAL
3500 set_vim_var_string(VV_WARNINGMSG, message, -1);
3501#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003502 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003504 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 else
3506 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003507 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003508 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 msg_didout = FALSE; /* overwrite this message */
3510 msg_nowait = TRUE; /* don't wait for this message */
3511 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 --no_wait_return;
3514}
3515
Bram Moolenaar113e1072019-01-20 15:30:40 +01003516#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003517 void
3518give_warning2(char_u *message, char_u *a1, int hl)
3519{
3520 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3521 give_warning(IObuff, hl);
3522}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003523#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525/*
3526 * Advance msg cursor to column "col".
3527 */
3528 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003529msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531 if (msg_silent != 0) /* nothing to advance to */
3532 {
3533 msg_col = col; /* for redirection, may fill it up later */
3534 return;
3535 }
3536 if (col >= Columns) /* not enough room */
3537 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003538#ifdef FEAT_RIGHTLEFT
3539 if (cmdmsg_rl)
3540 while (msg_col > Columns - col)
3541 msg_putchar(' ');
3542 else
3543#endif
3544 while (msg_col < col)
3545 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546}
3547
3548#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3549/*
3550 * Used for "confirm()" function, and the :confirm command prefix.
3551 * Versions which haven't got flexible dialogs yet, and console
3552 * versions, get this generic handler which uses the command line.
3553 *
3554 * type = one of:
3555 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3556 * title = title string (can be NULL for default)
3557 * (neither used in console dialogs at the moment)
3558 *
3559 * Format of the "buttons" string:
3560 * "Button1Name\nButton2Name\nButton3Name"
3561 * The first button should normally be the default/accept
3562 * The second button should be the 'Cancel' button
3563 * Other buttons- use your imagination!
3564 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3565 * different letter.
3566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003568do_dialog(
3569 int type UNUSED,
3570 char_u *title UNUSED,
3571 char_u *message,
3572 char_u *buttons,
3573 int dfltbutton,
3574 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003575 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003576 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003577 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 int oldState;
3580 int retval = 0;
3581 char_u *hotkeys;
3582 int c;
3583 int i;
3584
3585#ifndef NO_CONSOLE
3586 /* Don't output anything in silent mode ("ex -s") */
3587 if (silent_mode)
3588 return dfltbutton; /* return default option */
3589#endif
3590
3591#ifdef FEAT_GUI_DIALOG
3592 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3593 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3594 {
3595 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003596 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003597 /* avoid a hit-enter prompt without clearing the cmdline */
3598 need_wait_return = FALSE;
3599 emsg_on_display = FALSE;
3600 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 /* Flush output to avoid that further messages and redrawing is done
3603 * in the wrong order. */
3604 out_flush();
3605 gui_mch_update();
3606
3607 return c;
3608 }
3609#endif
3610
3611 oldState = State;
3612 State = CONFIRM;
3613#ifdef FEAT_MOUSE
3614 setmouse();
3615#endif
3616
3617 /*
3618 * Since we wait for a keypress, don't make the
3619 * user press RETURN as well afterwards.
3620 */
3621 ++no_wait_return;
3622 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3623
3624 if (hotkeys != NULL)
3625 {
3626 for (;;)
3627 {
3628 /* Get a typed character directly from the user. */
3629 c = get_keystroke();
3630 switch (c)
3631 {
3632 case CAR: /* User accepts default option */
3633 case NL:
3634 retval = dfltbutton;
3635 break;
3636 case Ctrl_C: /* User aborts/cancels */
3637 case ESC:
3638 retval = 0;
3639 break;
3640 default: /* Could be a hotkey? */
3641 if (c < 0) /* special keys are ignored here */
3642 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003643 if (c == ':' && ex_cmd)
3644 {
3645 retval = dfltbutton;
3646 ins_char_typebuf(':');
3647 break;
3648 }
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 /* Make the character lowercase, as chars in "hotkeys" are. */
3651 c = MB_TOLOWER(c);
3652 retval = 1;
3653 for (i = 0; hotkeys[i]; ++i)
3654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (has_mbyte)
3656 {
3657 if ((*mb_ptr2char)(hotkeys + i) == c)
3658 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003659 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (hotkeys[i] == c)
3663 break;
3664 ++retval;
3665 }
3666 if (hotkeys[i])
3667 break;
3668 /* No hotkey match, so keep waiting */
3669 continue;
3670 }
3671 break;
3672 }
3673
3674 vim_free(hotkeys);
3675 }
3676
3677 State = oldState;
3678#ifdef FEAT_MOUSE
3679 setmouse();
3680#endif
3681 --no_wait_return;
3682 msg_end_prompt();
3683
3684 return retval;
3685}
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687/*
3688 * Copy one character from "*from" to "*to", taking care of multi-byte
3689 * characters. Return the length of the character in bytes.
3690 */
3691 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003692copy_char(
3693 char_u *from,
3694 char_u *to,
3695 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 int len;
3698 int c;
3699
3700 if (has_mbyte)
3701 {
3702 if (lowercase)
3703 {
3704 c = MB_TOLOWER((*mb_ptr2char)(from));
3705 return (*mb_char2bytes)(c, to);
3706 }
3707 else
3708 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003709 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 mch_memmove(to, from, (size_t)len);
3711 return len;
3712 }
3713 }
3714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 if (lowercase)
3717 *to = (char_u)TOLOWER_LOC(*from);
3718 else
3719 *to = *from;
3720 return 1;
3721 }
3722}
3723
3724/*
3725 * Format the dialog string, and display it at the bottom of
3726 * the screen. Return a string of hotkey chars (if defined) for
3727 * each 'button'. If a button has no hotkey defined, the first character of
3728 * the button is used.
3729 * The hotkeys can be multi-byte characters, but without combining chars.
3730 *
3731 * Returns an allocated string with hotkeys, or NULL for error.
3732 */
3733 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003734msg_show_console_dialog(
3735 char_u *message,
3736 char_u *buttons,
3737 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003740#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int lenhotkey = HOTK_LEN; /* count first button */
3742 char_u *hotk = NULL;
3743 char_u *msgp = NULL;
3744 char_u *hotkp = NULL;
3745 char_u *r;
3746 int copy;
3747#define HAS_HOTKEY_LEN 30
3748 char_u has_hotkey[HAS_HOTKEY_LEN];
3749 int first_hotkey = FALSE; /* first char of button is hotkey */
3750 int idx;
3751
3752 has_hotkey[0] = FALSE;
3753
3754 /*
3755 * First loop: compute the size of memory to allocate.
3756 * Second loop: copy to the allocated memory.
3757 */
3758 for (copy = 0; copy <= 1; ++copy)
3759 {
3760 r = buttons;
3761 idx = 0;
3762 while (*r)
3763 {
3764 if (*r == DLG_BUTTON_SEP)
3765 {
3766 if (copy)
3767 {
3768 *msgp++ = ',';
3769 *msgp++ = ' '; /* '\n' -> ', ' */
3770
3771 /* advance to next hotkey and set default hotkey */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003773 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003776 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (dfltbutton)
3778 --dfltbutton;
3779
3780 /* If no hotkey is specified first char is used. */
3781 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3782 first_hotkey = TRUE;
3783 }
3784 else
3785 {
3786 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3787 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3788 if (idx < HAS_HOTKEY_LEN - 1)
3789 has_hotkey[++idx] = FALSE;
3790 }
3791 }
3792 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3793 {
3794 if (*r == DLG_HOTKEY_CHAR)
3795 ++r;
3796 first_hotkey = FALSE;
3797 if (copy)
3798 {
3799 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3800 *msgp++ = *r;
3801 else
3802 {
3803 /* '&a' -> '[a]' */
3804 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3805 msgp += copy_char(r, msgp, FALSE);
3806 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3807
3808 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003809 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811 }
3812 else
3813 {
3814 ++len; /* '&a' -> '[a]' */
3815 if (idx < HAS_HOTKEY_LEN - 1)
3816 has_hotkey[idx] = TRUE;
3817 }
3818 }
3819 else
3820 {
3821 /* everything else copy literally */
3822 if (copy)
3823 msgp += copy_char(r, msgp, FALSE);
3824 }
3825
3826 /* advance to the next character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003827 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829
3830 if (copy)
3831 {
3832 *msgp++ = ':';
3833 *msgp++ = ' ';
3834 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 else
3837 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003838 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003839 + 2 /* for the NL's */
3840 + STRLEN(buttons)
3841 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003842 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 /* If no hotkey is specified first char is used. */
3845 if (!has_hotkey[0])
3846 {
3847 first_hotkey = TRUE;
3848 len += 2; /* "x" -> "[x]" */
3849 }
3850
3851 /*
3852 * Now allocate and load the strings
3853 */
3854 vim_free(confirm_msg);
3855 confirm_msg = alloc(len);
3856 if (confirm_msg == NULL)
3857 return NULL;
3858 *confirm_msg = NUL;
3859 hotk = alloc(lenhotkey);
3860 if (hotk == NULL)
3861 return NULL;
3862
3863 *confirm_msg = '\n';
3864 STRCPY(confirm_msg + 1, message);
3865
3866 msgp = confirm_msg + 1 + STRLEN(message);
3867 hotkp = hotk;
3868
Bram Moolenaar6a516062007-07-05 08:11:42 +00003869 /* Define first default hotkey. Keep the hotkey string NUL
3870 * terminated to avoid reading past the end. */
3871 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
3873 /* Remember where the choices start, displaying starts here when
3874 * "hotkp" typed at the more prompt. */
3875 confirm_msg_tail = msgp;
3876 *msgp++ = '\n';
3877 }
3878 }
3879
3880 display_confirm_msg();
3881 return hotk;
3882}
3883
3884/*
3885 * Display the ":confirm" message. Also called when screen resized.
3886 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003887 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003888display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890 /* avoid that 'q' at the more prompt truncates the message here */
3891 ++confirm_msg_used;
3892 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003893 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 --confirm_msg_used;
3895}
3896
3897#endif /* FEAT_CON_DIALOG */
3898
3899#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3900
3901 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003902vim_dialog_yesno(
3903 int type,
3904 char_u *title,
3905 char_u *message,
3906 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
3908 if (do_dialog(type,
3909 title == NULL ? (char_u *)_("Question") : title,
3910 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003911 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 return VIM_YES;
3913 return VIM_NO;
3914}
3915
3916 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003917vim_dialog_yesnocancel(
3918 int type,
3919 char_u *title,
3920 char_u *message,
3921 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 switch (do_dialog(type,
3924 title == NULL ? (char_u *)_("Question") : title,
3925 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003926 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
3928 case 1: return VIM_YES;
3929 case 2: return VIM_NO;
3930 }
3931 return VIM_CANCEL;
3932}
3933
3934 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003935vim_dialog_yesnoallcancel(
3936 int type,
3937 char_u *title,
3938 char_u *message,
3939 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 switch (do_dialog(type,
3942 title == NULL ? (char_u *)"Question" : title,
3943 message,
3944 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003945 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
3947 case 1: return VIM_YES;
3948 case 2: return VIM_NO;
3949 case 3: return VIM_ALL;
3950 case 4: return VIM_DISCARDALL;
3951 }
3952 return VIM_CANCEL;
3953}
3954
3955#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3956
3957#if defined(FEAT_BROWSE) || defined(PROTO)
3958/*
3959 * Generic browse function. Calls gui_mch_browse() when possible.
3960 * Later this may pop-up a non-GUI file selector (external command?).
3961 */
3962 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003963do_browse(
3964 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3965 char_u *title, /* title for the window */
3966 char_u *dflt, /* default file name (may include directory) */
3967 char_u *ext, /* extension added */
3968 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003970 char_u *filter, /* file name filter */
3971 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
3973 char_u *fname;
3974 static char_u *last_dir = NULL; /* last used directory */
3975 char_u *tofree = NULL;
3976 int save_browse = cmdmod.browse;
3977
3978 /* Must turn off browse to avoid that autocommands will get the
3979 * flag too! */
3980 cmdmod.browse = FALSE;
3981
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003982 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003984 if (flags & BROWSE_DIR)
3985 title = (char_u *)_("Select Directory dialog");
3986 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 title = (char_u *)_("Save File dialog");
3988 else
3989 title = (char_u *)_("Open File dialog");
3990 }
3991
3992 /* When no directory specified, use default file name, default dir, buffer
3993 * dir, last dir or current dir */
3994 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3995 {
3996 if (mch_isdir(dflt)) /* default file name is a directory */
3997 {
3998 initdir = dflt;
3999 dflt = NULL;
4000 }
4001 else if (gettail(dflt) != dflt) /* default file name includes a path */
4002 {
4003 tofree = vim_strsave(dflt);
4004 if (tofree != NULL)
4005 {
4006 initdir = tofree;
4007 *gettail(initdir) = NUL;
4008 dflt = gettail(dflt);
4009 }
4010 }
4011 }
4012
4013 if (initdir == NULL || *initdir == NUL)
4014 {
4015 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004016 if (STRCMP(p_bsdir, "last") != 0
4017 && STRCMP(p_bsdir, "buffer") != 0
4018 && STRCMP(p_bsdir, "current") != 0
4019 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 initdir = p_bsdir;
4021 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004022 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 && buf != NULL && buf->b_ffname != NULL)
4024 {
4025 if (dflt == NULL || *dflt == NUL)
4026 dflt = gettail(curbuf->b_ffname);
4027 tofree = vim_strsave(curbuf->b_ffname);
4028 if (tofree != NULL)
4029 {
4030 initdir = tofree;
4031 *gettail(initdir) = NUL;
4032 }
4033 }
4034 /* When 'browsedir' is "last", use dir from last browse */
4035 else if (*p_bsdir == 'l')
4036 initdir = last_dir;
4037 /* When 'browsedir is "current", use current directory. This is the
4038 * default already, leave initdir empty. */
4039 }
4040
4041# ifdef FEAT_GUI
4042 if (gui.in_use) /* when this changes, also adjust f_has()! */
4043 {
4044 if (filter == NULL
4045# ifdef FEAT_EVAL
4046 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
4047 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
4048# endif
4049 )
4050 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004051 if (flags & BROWSE_DIR)
4052 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004053# if defined(FEAT_GUI_GTK) || defined(MSWIN)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004054 /* For systems that have a directory dialog. */
4055 fname = gui_mch_browsedir(title, initdir);
4056# else
4057 /* Generic solution for selecting a directory: select a file and
4058 * remove the file name. */
4059 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
4060# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004061# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004062 /* Win32 adds a dummy file name, others return an arbitrary file
4063 * name. GTK+ 2 returns only the directory, */
4064 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
4065 {
4066 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004067 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004068
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004069 if (tail == fname)
4070 *tail++ = '.'; /* use current dir */
4071 *tail = NUL;
4072 }
4073# endif
4074 }
4075 else
4076 fname = gui_mch_browse(flags & BROWSE_SAVE,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004077 title, dflt, ext, initdir, (char_u *)_(filter));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 /* We hang around in the dialog for a while, the user might do some
4080 * things to our files. The Win32 dialog allows deleting or renaming
4081 * a file, check timestamps. */
4082 need_check_timestamps = TRUE;
4083 did_check_timestamps = FALSE;
4084 }
4085 else
4086# endif
4087 {
4088 /* TODO: non-GUI file selector here */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004089 emsg(_("E338: Sorry, no file browser in console mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 fname = NULL;
4091 }
4092
4093 /* keep the directory for next time */
4094 if (fname != NULL)
4095 {
4096 vim_free(last_dir);
4097 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004098 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
4100 *gettail(last_dir) = NUL;
4101 if (*last_dir == NUL)
4102 {
4103 /* filename only returned, must be in current dir */
4104 vim_free(last_dir);
4105 last_dir = alloc(MAXPATHL);
4106 if (last_dir != NULL)
4107 mch_dirname(last_dir, MAXPATHL);
4108 }
4109 }
4110 }
4111
4112 vim_free(tofree);
4113 cmdmod.browse = save_browse;
4114
4115 return fname;
4116}
4117#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004118
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004119#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120static char *e_printf = N_("E766: Insufficient arguments for printf()");
4121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122/*
4123 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4124 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004125 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004126tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127{
4128 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004129 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 int err = FALSE;
4131
4132 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004133 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 else
4135 {
4136 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004137 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004138 if (err)
4139 n = 0;
4140 }
4141 return n;
4142}
4143
4144/*
4145 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004146 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004147 * are not converted to a string.
4148 * If "tofree" is not NULL echo_string() is used. All types are converted to
4149 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004150 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 */
4152 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004153tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004154{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004155 int idx = *idxp - 1;
4156 char *s = NULL;
4157 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158
4159 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004160 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161 else
4162 {
4163 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004164 if (tofree != NULL)
4165 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4166 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004167 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 }
4169 return s;
4170}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004171
4172# ifdef FEAT_FLOAT
4173/*
4174 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4175 */
4176 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004177tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004178{
4179 int idx = *idxp - 1;
4180 double f = 0;
4181
4182 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004183 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004184 else
4185 {
4186 ++*idxp;
4187 if (tvs[idx].v_type == VAR_FLOAT)
4188 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004189 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004190 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004191 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004192 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004193 }
4194 return f;
4195}
4196# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004197#endif
4198
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004199#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004200/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004201 * Return the representation of infinity for printf() function:
4202 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4203 */
4204 static const char *
4205infinity_str(int positive,
4206 char fmt_spec,
4207 int force_sign,
4208 int space_for_positive)
4209{
4210 static const char *table[] =
4211 {
4212 "-inf", "inf", "+inf", " inf",
4213 "-INF", "INF", "+INF", " INF"
4214 };
4215 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4216
4217 if (ASCII_ISUPPER(fmt_spec))
4218 idx += 4;
4219 return table[idx];
4220}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004221#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004222
4223/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004224 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004225 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004226 * consistency.
4227 *
4228 * This code is based on snprintf.c - a portable implementation of snprintf
4229 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004230 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004231 * The original code, including useful comments, can be found here:
4232 * http://www.ijs.si/software/snprintf/
4233 *
4234 * This snprintf() only supports the following conversion specifiers:
4235 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4236 * with flags: '-', '+', ' ', '0' and '#'.
4237 * An asterisk is supported for field width as well as precision.
4238 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004239 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004240 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004241 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4242 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004243 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004244 * The locale is not used, the string is used as a byte string. This is only
4245 * relevant for double-byte encodings where the second byte may be '%'.
4246 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004247 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4248 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004249 *
4250 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004251 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004252 * is greater or equal to "str_m", not all characters from the result
4253 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4254 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004255 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004256 */
4257
4258/*
4259 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004261 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004262 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004263 */
4264
4265/* When generating prototypes all of this is skipped, cproto doesn't
4266 * understand this. */
4267#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004268
Bram Moolenaara800b422010-06-27 01:15:55 +02004269/* Like vim_vsnprintf() but append to the string. */
4270 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004271vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004272{
4273 va_list ap;
4274 int str_l;
4275 size_t len = STRLEN(str);
4276 size_t space;
4277
4278 if (str_m <= len)
4279 space = 0;
4280 else
4281 space = str_m - len;
4282 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004283 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004284 va_end(ap);
4285 return str_l;
4286}
Bram Moolenaara800b422010-06-27 01:15:55 +02004287
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004288 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004289vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004290{
4291 va_list ap;
4292 int str_l;
4293
4294 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004295 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004296 va_end(ap);
4297 return str_l;
4298}
4299
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004301vim_vsnprintf(
4302 char *str,
4303 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004304 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004305 va_list ap)
4306{
4307 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4308}
4309
4310 int
4311vim_vsnprintf_typval(
4312 char *str,
4313 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004314 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004315 va_list ap,
4316 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004317{
4318 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004319 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004320 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004321
4322 if (p == NULL)
4323 p = "";
4324 while (*p != NUL)
4325 {
4326 if (*p != '%')
4327 {
4328 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004329 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004330
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004331 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004332 if (str_l < str_m)
4333 {
4334 size_t avail = str_m - str_l;
4335
4336 mch_memmove(str + str_l, p, n > avail ? avail : n);
4337 }
4338 p += n;
4339 str_l += n;
4340 }
4341 else
4342 {
4343 size_t min_field_width = 0, precision = 0;
4344 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4345 int alternate_form = 0, force_sign = 0;
4346
4347 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4348 * ignored. */
4349 int space_for_positive = 1;
4350
4351 /* allowed values: \0, h, l, L */
4352 char length_modifier = '\0';
4353
4354 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004355# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004356# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004357 * That sounds reasonable to use as the maximum
4358 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004359# elif defined(FEAT_NUM64)
4360# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004361# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004362# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004363# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004364 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004365
4366 /* string address in case of string argument */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004367 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004368
4369 /* natural field width of arg without padding and sign */
4370 size_t str_arg_l;
4371
4372 /* unsigned char argument value - only defined for c conversion.
4373 * N.B. standard explicitly states the char argument for the c
4374 * conversion is unsigned */
4375 unsigned char uchar_arg;
4376
4377 /* number of zeros to be inserted for numeric conversions as
4378 * required by the precision or minimal field width */
4379 size_t number_of_zeros_to_pad = 0;
4380
4381 /* index into tmp where zero padding is to be inserted */
4382 size_t zero_padding_insertion_ind = 0;
4383
4384 /* current conversion specifier character */
4385 char fmt_spec = '\0';
4386
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004387 /* buffer for 's' and 'S' specs */
4388 char_u *tofree = NULL;
4389
4390
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004391 p++; /* skip '%' */
4392
4393 /* parse flags */
4394 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4395 || *p == '#' || *p == '\'')
4396 {
4397 switch (*p)
4398 {
4399 case '0': zero_padding = 1; break;
4400 case '-': justify_left = 1; break;
4401 case '+': force_sign = 1; space_for_positive = 0; break;
4402 case ' ': force_sign = 1;
4403 /* If both the ' ' and '+' flags appear, the ' '
4404 * flag should be ignored */
4405 break;
4406 case '#': alternate_form = 1; break;
4407 case '\'': break;
4408 }
4409 p++;
4410 }
4411 /* If the '0' and '-' flags both appear, the '0' flag should be
4412 * ignored. */
4413
4414 /* parse field width */
4415 if (*p == '*')
4416 {
4417 int j;
4418
4419 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004422 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423# endif
4424 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004425 if (j >= 0)
4426 min_field_width = j;
4427 else
4428 {
4429 min_field_width = -j;
4430 justify_left = 1;
4431 }
4432 }
4433 else if (VIM_ISDIGIT((int)(*p)))
4434 {
4435 /* size_t could be wider than unsigned int; make sure we treat
4436 * argument like common implementations do */
4437 unsigned int uj = *p++ - '0';
4438
4439 while (VIM_ISDIGIT((int)(*p)))
4440 uj = 10 * uj + (unsigned int)(*p++ - '0');
4441 min_field_width = uj;
4442 }
4443
4444 /* parse precision */
4445 if (*p == '.')
4446 {
4447 p++;
4448 precision_specified = 1;
4449 if (*p == '*')
4450 {
4451 int j;
4452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004455 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456# endif
4457 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004458 p++;
4459 if (j >= 0)
4460 precision = j;
4461 else
4462 {
4463 precision_specified = 0;
4464 precision = 0;
4465 }
4466 }
4467 else if (VIM_ISDIGIT((int)(*p)))
4468 {
4469 /* size_t could be wider than unsigned int; make sure we
4470 * treat argument like common implementations do */
4471 unsigned int uj = *p++ - '0';
4472
4473 while (VIM_ISDIGIT((int)(*p)))
4474 uj = 10 * uj + (unsigned int)(*p++ - '0');
4475 precision = uj;
4476 }
4477 }
4478
4479 /* parse 'h', 'l' and 'll' length modifiers */
4480 if (*p == 'h' || *p == 'l')
4481 {
4482 length_modifier = *p;
4483 p++;
4484 if (length_modifier == 'l' && *p == 'l')
4485 {
4486 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004487# ifdef FEAT_NUM64
4488 length_modifier = 'L';
4489# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004490 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004491# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492 p++;
4493 }
4494 }
4495 fmt_spec = *p;
4496
4497 /* common synonyms: */
4498 switch (fmt_spec)
4499 {
4500 case 'i': fmt_spec = 'd'; break;
4501 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4502 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4503 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4504 default: break;
4505 }
4506
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004507# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4508 switch (fmt_spec)
4509 {
4510 case 'd': case 'u': case 'o': case 'x': case 'X':
4511 if (tvs != NULL && length_modifier == '\0')
4512 length_modifier = 'L';
4513 }
4514# endif
4515
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004516 /* get parameter value, do initial processing */
4517 switch (fmt_spec)
4518 {
4519 /* '%' and 'c' behave similar to 's' regarding flags and field
4520 * widths */
4521 case '%':
4522 case 'c':
4523 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004524 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004525 str_arg_l = 1;
4526 switch (fmt_spec)
4527 {
4528 case '%':
4529 str_arg = p;
4530 break;
4531
4532 case 'c':
4533 {
4534 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535
4536 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004538 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539# endif
4540 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004541 /* standard demands unsigned char */
4542 uchar_arg = (unsigned char)j;
4543 str_arg = (char *)&uchar_arg;
4544 break;
4545 }
4546
4547 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004548 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004549 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004551 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552# endif
4553 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004554 if (str_arg == NULL)
4555 {
4556 str_arg = "[NULL]";
4557 str_arg_l = 6;
4558 }
4559 /* make sure not to address string beyond the specified
4560 * precision !!! */
4561 else if (!precision_specified)
4562 str_arg_l = strlen(str_arg);
4563 /* truncate string if necessary as requested by precision */
4564 else if (precision == 0)
4565 str_arg_l = 0;
4566 else
4567 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004568 /* Don't put the #if inside memchr(), it can be a
4569 * macro. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004570 /* memchr on HP does not like n > 2^31 !!! */
4571 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004572 precision <= (size_t)0x7fffffffL ? precision
4573 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004574 str_arg_l = (q == NULL) ? precision
4575 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004576 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004577 if (fmt_spec == 'S')
4578 {
4579 if (min_field_width != 0)
4580 min_field_width += STRLEN(str_arg)
4581 - mb_string2cells((char_u *)str_arg, -1);
4582 if (precision)
4583 {
4584 char_u *p1 = (char_u *)str_arg;
4585 size_t i;
4586
4587 for (i = 0; i < precision && *p1; i++)
4588 p1 += mb_ptr2len(p1);
4589
4590 str_arg_l = precision = p1 - (char_u *)str_arg;
4591 }
4592 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004593 break;
4594
4595 default:
4596 break;
4597 }
4598 break;
4599
Bram Moolenaar91984b92016-08-16 21:58:41 +02004600 case 'd': case 'u':
4601 case 'b': case 'B':
4602 case 'o':
4603 case 'x': case 'X':
4604 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004605 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004606 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004607 * imply the value is unsigned; d implies a signed
4608 * value */
4609
4610 /* 0 if numeric argument is zero (or if pointer is
4611 * NULL for 'p'), +1 if greater than zero (or nonzero
4612 * for unsigned arguments), -1 if negative (unsigned
4613 * argument is never negative) */
4614 int arg_sign = 0;
4615
4616 /* only defined for length modifier h, or for no
4617 * length modifiers */
4618 int int_arg = 0;
4619 unsigned int uint_arg = 0;
4620
4621 /* only defined for length modifier l */
4622 long int long_arg = 0;
4623 unsigned long int ulong_arg = 0;
4624
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004625# ifdef FEAT_NUM64
4626 /* only defined for length modifier ll */
4627 varnumber_T llong_arg = 0;
4628 uvarnumber_T ullong_arg = 0;
4629# endif
4630
Bram Moolenaare9997822016-08-28 16:03:38 +02004631 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004632 uvarnumber_T bin_arg = 0;
4633
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004634 /* pointer argument value -only defined for p
4635 * conversion */
4636 void *ptr_arg = NULL;
4637
4638 if (fmt_spec == 'p')
4639 {
4640 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004643 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4644 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645# endif
4646 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004647 if (ptr_arg != NULL)
4648 arg_sign = 1;
4649 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004650 else if (fmt_spec == 'b' || fmt_spec == 'B')
4651 {
4652 bin_arg =
4653# if defined(FEAT_EVAL)
4654 tvs != NULL ?
4655 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4656# endif
4657 va_arg(ap, uvarnumber_T);
4658 if (bin_arg != 0)
4659 arg_sign = 1;
4660 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004661 else if (fmt_spec == 'd')
4662 {
4663 /* signed */
4664 switch (length_modifier)
4665 {
4666 case '\0':
4667 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 /* char and short arguments are passed as int. */
4669 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004671 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004672# endif
4673 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004674 if (int_arg > 0)
4675 arg_sign = 1;
4676 else if (int_arg < 0)
4677 arg_sign = -1;
4678 break;
4679 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004680 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004681# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004682 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683# endif
4684 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004685 if (long_arg > 0)
4686 arg_sign = 1;
4687 else if (long_arg < 0)
4688 arg_sign = -1;
4689 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004690# ifdef FEAT_NUM64
4691 case 'L':
4692 llong_arg =
4693# if defined(FEAT_EVAL)
4694 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4695# endif
4696 va_arg(ap, varnumber_T);
4697 if (llong_arg > 0)
4698 arg_sign = 1;
4699 else if (llong_arg < 0)
4700 arg_sign = -1;
4701 break;
4702# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004703 }
4704 }
4705 else
4706 {
4707 /* unsigned */
4708 switch (length_modifier)
4709 {
4710 case '\0':
4711 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004712 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004713# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004714 tvs != NULL ? (unsigned)
4715 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004716# endif
4717 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004718 if (uint_arg != 0)
4719 arg_sign = 1;
4720 break;
4721 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004722 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004723# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004724 tvs != NULL ? (unsigned long)
4725 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726# endif
4727 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004728 if (ulong_arg != 0)
4729 arg_sign = 1;
4730 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004731# ifdef FEAT_NUM64
4732 case 'L':
4733 ullong_arg =
4734# if defined(FEAT_EVAL)
4735 tvs != NULL ? (uvarnumber_T)
4736 tv_nr(tvs, &arg_idx) :
4737# endif
4738 va_arg(ap, uvarnumber_T);
4739 if (ullong_arg != 0)
4740 arg_sign = 1;
4741 break;
4742# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 }
4744 }
4745
4746 str_arg = tmp;
4747 str_arg_l = 0;
4748
4749 /* NOTE:
4750 * For d, i, u, o, x, and X conversions, if precision is
4751 * specified, the '0' flag should be ignored. This is so
4752 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4753 * FreeBSD, NetBSD; but not with Perl.
4754 */
4755 if (precision_specified)
4756 zero_padding = 0;
4757 if (fmt_spec == 'd')
4758 {
4759 if (force_sign && arg_sign >= 0)
4760 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4761 /* leave negative numbers for sprintf to handle, to
4762 * avoid handling tricky cases like (short int)-32768 */
4763 }
4764 else if (alternate_form)
4765 {
4766 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004767 && (fmt_spec == 'b' || fmt_spec == 'B'
4768 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004769 {
4770 tmp[str_arg_l++] = '0';
4771 tmp[str_arg_l++] = fmt_spec;
4772 }
4773 /* alternate form should have no effect for p
4774 * conversion, but ... */
4775 }
4776
4777 zero_padding_insertion_ind = str_arg_l;
4778 if (!precision_specified)
4779 precision = 1; /* default precision is 1 */
4780 if (precision == 0 && arg_sign == 0)
4781 {
4782 /* When zero value is formatted with an explicit
4783 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004784 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004785 }
4786 else
4787 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004788 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004789 int f_l = 0;
4790
4791 /* construct a simple format string for sprintf */
4792 f[f_l++] = '%';
4793 if (!length_modifier)
4794 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004795 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004796 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004797# ifdef FEAT_NUM64
Bram Moolenaar4f974752019-02-17 17:44:42 +01004798# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004799 f[f_l++] = 'I';
4800 f[f_l++] = '6';
4801 f[f_l++] = '4';
4802# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803 f[f_l++] = 'l';
4804 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004805# endif
4806# else
4807 f[f_l++] = 'l';
4808# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004809 }
4810 else
4811 f[f_l++] = length_modifier;
4812 f[f_l++] = fmt_spec;
4813 f[f_l++] = '\0';
4814
4815 if (fmt_spec == 'p')
4816 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004817 else if (fmt_spec == 'b' || fmt_spec == 'B')
4818 {
4819 char b[8 * sizeof(uvarnumber_T)];
4820 size_t b_l = 0;
4821 uvarnumber_T bn = bin_arg;
4822
4823 do
4824 {
4825 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4826 bn >>= 1;
4827 }
4828 while (bn != 0);
4829
4830 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4831 str_arg_l += b_l;
4832 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004833 else if (fmt_spec == 'd')
4834 {
4835 /* signed */
4836 switch (length_modifier)
4837 {
4838 case '\0':
4839 case 'h': str_arg_l += sprintf(
4840 tmp + str_arg_l, f, int_arg);
4841 break;
4842 case 'l': str_arg_l += sprintf(
4843 tmp + str_arg_l, f, long_arg);
4844 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004845# ifdef FEAT_NUM64
4846 case 'L': str_arg_l += sprintf(
4847 tmp + str_arg_l, f, llong_arg);
4848 break;
4849# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004850 }
4851 }
4852 else
4853 {
4854 /* unsigned */
4855 switch (length_modifier)
4856 {
4857 case '\0':
4858 case 'h': str_arg_l += sprintf(
4859 tmp + str_arg_l, f, uint_arg);
4860 break;
4861 case 'l': str_arg_l += sprintf(
4862 tmp + str_arg_l, f, ulong_arg);
4863 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004864# ifdef FEAT_NUM64
4865 case 'L': str_arg_l += sprintf(
4866 tmp + str_arg_l, f, ullong_arg);
4867 break;
4868# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004869 }
4870 }
4871
4872 /* include the optional minus sign and possible
4873 * "0x" in the region before the zero padding
4874 * insertion point */
4875 if (zero_padding_insertion_ind < str_arg_l
4876 && tmp[zero_padding_insertion_ind] == '-')
4877 zero_padding_insertion_ind++;
4878 if (zero_padding_insertion_ind + 1 < str_arg_l
4879 && tmp[zero_padding_insertion_ind] == '0'
4880 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4881 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4882 zero_padding_insertion_ind += 2;
4883 }
4884
4885 {
4886 size_t num_of_digits = str_arg_l
4887 - zero_padding_insertion_ind;
4888
4889 if (alternate_form && fmt_spec == 'o'
4890 /* unless zero is already the first
4891 * character */
4892 && !(zero_padding_insertion_ind < str_arg_l
4893 && tmp[zero_padding_insertion_ind] == '0'))
4894 {
4895 /* assure leading zero for alternate-form
4896 * octal numbers */
4897 if (!precision_specified
4898 || precision < num_of_digits + 1)
4899 {
4900 /* precision is increased to force the
4901 * first character to be zero, except if a
4902 * zero value is formatted with an
4903 * explicit precision of zero */
4904 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004905 }
4906 }
4907 /* zero padding to specified precision? */
4908 if (num_of_digits < precision)
4909 number_of_zeros_to_pad = precision - num_of_digits;
4910 }
4911 /* zero padding to specified minimal field width? */
4912 if (!justify_left && zero_padding)
4913 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004914 int n = (int)(min_field_width - (str_arg_l
4915 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004916 if (n > 0)
4917 number_of_zeros_to_pad += n;
4918 }
4919 break;
4920 }
4921
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004922# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004923 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004924 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004925 case 'e':
4926 case 'E':
4927 case 'g':
4928 case 'G':
4929 {
4930 /* Floating point. */
4931 double f;
4932 double abs_f;
4933 char format[40];
4934 int l;
4935 int remove_trailing_zeroes = FALSE;
4936
4937 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004938# if defined(FEAT_EVAL)
4939 tvs != NULL ? tv_float(tvs, &arg_idx) :
4940# endif
4941 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004942 abs_f = f < 0 ? -f : f;
4943
4944 if (fmt_spec == 'g' || fmt_spec == 'G')
4945 {
4946 /* Would be nice to use %g directly, but it prints
4947 * "1.0" as "1", we don't want that. */
4948 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4949 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004950 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004951 else
4952 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4953 remove_trailing_zeroes = TRUE;
4954 }
4955
Bram Moolenaar04186092016-08-29 21:55:35 +02004956 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004957# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004958 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004959# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004960 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004961# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004962 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004963 {
4964 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004965 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4966 force_sign, space_for_positive));
4967 str_arg_l = STRLEN(tmp);
4968 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004969 }
4970 else
4971 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004972 if (isnan(f))
4973 {
4974 /* Not a number: nan or NAN */
4975 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4976 : "nan");
4977 str_arg_l = 3;
4978 zero_padding = 0;
4979 }
4980 else if (isinf(f))
4981 {
4982 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4983 force_sign, space_for_positive));
4984 str_arg_l = STRLEN(tmp);
4985 zero_padding = 0;
4986 }
4987 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004988 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004989 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004990 format[0] = '%';
4991 l = 1;
4992 if (force_sign)
4993 format[l++] = space_for_positive ? ' ' : '+';
4994 if (precision_specified)
4995 {
4996 size_t max_prec = TMP_LEN - 10;
4997
4998 /* Make sure we don't get more digits than we
4999 * have room for. */
5000 if ((fmt_spec == 'f' || fmt_spec == 'F')
5001 && abs_f > 1.0)
5002 max_prec -= (size_t)log10(abs_f);
5003 if (precision > max_prec)
5004 precision = max_prec;
5005 l += sprintf(format + l, ".%d", (int)precision);
5006 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02005007 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02005008 format[l + 1] = NUL;
5009
Bram Moolenaare9997822016-08-28 16:03:38 +02005010 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01005011 }
Bram Moolenaar99922372016-08-27 15:26:35 +02005012
Bram Moolenaara7241f52008-06-24 20:39:31 +00005013 if (remove_trailing_zeroes)
5014 {
5015 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005016 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005017
5018 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02005019 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005020 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005021 else
5022 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005023 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005024 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005025 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005026 {
5027 /* Remove superfluous '+' and leading
5028 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005029 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005030 {
5031 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005032 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005033 --str_arg_l;
5034 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005035 i = (tp[1] == '-') ? 2 : 1;
5036 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005037 {
5038 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005039 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005040 --str_arg_l;
5041 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005042 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005043 }
5044 }
5045
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005046 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005047 /* Remove trailing zeroes, but keep the one
5048 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005049 while (tp > tmp + 2 && *tp == '0'
5050 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005051 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005052 STRMOVE(tp, tp + 1);
5053 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005054 --str_arg_l;
5055 }
5056 }
5057 else
5058 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005059 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005060
5061 /* Be consistent: some printf("%e") use 1.0e+12
5062 * and some 1.0e+012. Remove one zero in the last
5063 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005064 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005065 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005066 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5067 && tp[2] == '0'
5068 && vim_isdigit(tp[3])
5069 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005070 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005071 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005072 --str_arg_l;
5073 }
5074 }
5075 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005076 if (zero_padding && min_field_width > str_arg_l
5077 && (tmp[0] == '-' || force_sign))
5078 {
5079 /* padding 0's should be inserted after the sign */
5080 number_of_zeros_to_pad = min_field_width - str_arg_l;
5081 zero_padding_insertion_ind = 1;
5082 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005083 str_arg = tmp;
5084 break;
5085 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005086# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005087
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005088 default:
5089 /* unrecognized conversion specifier, keep format string
5090 * as-is */
5091 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005092 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005093 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005094 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005095
5096 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005097 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005098 str_arg = p;
5099 str_arg_l = 0;
5100 if (*p != NUL)
5101 str_arg_l++; /* include invalid conversion specifier
5102 unchanged if not at end-of-string */
5103 break;
5104 }
5105
5106 if (*p != NUL)
5107 p++; /* step over the just processed conversion specifier */
5108
5109 /* insert padding to the left as requested by min_field_width;
5110 * this does not include the zero padding in case of numerical
5111 * conversions*/
5112 if (!justify_left)
5113 {
5114 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005115 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005116
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005117 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005118 {
5119 if (str_l < str_m)
5120 {
5121 size_t avail = str_m - str_l;
5122
5123 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005124 (size_t)pn > avail ? avail
5125 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005126 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005127 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005128 }
5129 }
5130
5131 /* zero padding as requested by the precision or by the minimal
5132 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005133 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005134 {
5135 /* will not copy first part of numeric right now, *
5136 * force it to be copied later in its entirety */
5137 zero_padding_insertion_ind = 0;
5138 }
5139 else
5140 {
5141 /* insert first part of numerics (sign or '0x') before zero
5142 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005143 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005144
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005145 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005146 {
5147 if (str_l < str_m)
5148 {
5149 size_t avail = str_m - str_l;
5150
5151 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005152 (size_t)zn > avail ? avail
5153 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005154 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005155 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005156 }
5157
5158 /* insert zero padding as requested by the precision or min
5159 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005160 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005161 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005162 {
5163 if (str_l < str_m)
5164 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005165 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005166
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005167 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005168 (size_t)zn > avail ? avail
5169 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005170 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005171 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005172 }
5173 }
5174
5175 /* insert formatted string
5176 * (or as-is conversion specifier for unknown conversions) */
5177 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005178 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005179
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005180 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005181 {
5182 if (str_l < str_m)
5183 {
5184 size_t avail = str_m - str_l;
5185
5186 mch_memmove(str + str_l,
5187 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005188 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005189 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005190 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005191 }
5192 }
5193
5194 /* insert right padding */
5195 if (justify_left)
5196 {
5197 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005198 int pn = (int)(min_field_width
5199 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005200
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005201 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005202 {
5203 if (str_l < str_m)
5204 {
5205 size_t avail = str_m - str_l;
5206
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005207 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005208 (size_t)pn > avail ? avail
5209 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005210 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005211 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005212 }
5213 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005214 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005215 }
5216 }
5217
5218 if (str_m > 0)
5219 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005220 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005221 * overwriting the last character (shouldn't happen, but just in case)
5222 * */
5223 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5224 }
5225
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005226 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005227 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005229 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005230 * character), that is, the number of characters that would have been
5231 * written to the buffer if it were large enough. */
5232 return (int)str_l;
5233}
5234
5235#endif /* PROTO */