blob: 387a142a22e14897f2634d6a5bd5b4092ea72193 [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);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002591 if (buf != NULL)
2592 {
2593 memcpy(buf, p, n);
2594 if (!info_message)
2595 buf[n++] = CAR;
2596 buf[n++] = NL;
2597 buf[n++] = NUL;
2598 if (info_message) // informative message, not an error
2599 mch_msg((char *)buf);
2600 else
2601 mch_errmsg((char *)buf);
2602 vim_free(buf);
2603 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002604 p = s + 1;
2605 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 }
2607
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002608 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002609#ifdef FEAT_RIGHTLEFT
2610 if (cmdmsg_rl)
2611 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002612 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002613 msg_col = Columns - 1;
2614 else
2615 --msg_col;
2616 }
2617 else
2618#endif
2619 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002620 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002621 msg_col = 0;
2622 else
2623 ++msg_col;
2624 }
2625 ++s;
2626 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002627
2628 if (*p != NUL && !(silent_mode && p_verbose == 0))
2629 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002630 int c = -1;
2631
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002632 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002633 {
2634 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002635 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002636 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002637 if (info_message)
2638 mch_msg((char *)p);
2639 else
2640 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002641 if (c != -1)
2642 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002643 }
2644
2645 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002646
Bram Moolenaar4f974752019-02-17 17:44:42 +01002647#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002648 if (!(silent_mode && p_verbose == 0))
2649 mch_settmode(TMODE_RAW);
2650#endif
2651}
2652
2653/*
2654 * Show the more-prompt and handle the user response.
2655 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002656 * When at hit-enter prompt "typed_char" is the already typed character,
2657 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002658 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2659 */
2660 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002661do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002662{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002663 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002664 int used_typed_char = typed_char;
2665 int oldState = State;
2666 int c;
2667#ifdef FEAT_CON_DIALOG
2668 int retval = FALSE;
2669#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002670 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002671 msgchunk_T *mp_last = NULL;
2672 msgchunk_T *mp;
2673 int i;
2674
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002675 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002676 * that case don't show another prompt. Also when at the hit-Enter prompt
2677 * and nothing was typed. */
2678 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002679 return FALSE;
2680 entered = TRUE;
2681
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002682 if (typed_char == 'G')
2683 {
2684 /* "g<": Find first line on the last page. */
2685 mp_last = msg_sb_start(last_msgchunk);
2686 for (i = 0; i < Rows - 2 && mp_last != NULL
2687 && mp_last->sb_prev != NULL; ++i)
2688 mp_last = msg_sb_start(mp_last->sb_prev);
2689 }
2690
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002691 State = ASKMORE;
2692#ifdef FEAT_MOUSE
2693 setmouse();
2694#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002695 if (typed_char == NUL)
2696 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002697 for (;;)
2698 {
2699 /*
2700 * Get a typed character directly from the user.
2701 */
2702 if (used_typed_char != NUL)
2703 {
2704 c = used_typed_char; /* was typed at hit-enter prompt */
2705 used_typed_char = NUL;
2706 }
2707 else
2708 c = get_keystroke();
2709
2710#if defined(FEAT_MENU) && defined(FEAT_GUI)
2711 if (c == K_MENU)
2712 {
2713 int idx = get_menu_index(current_menu, ASKMORE);
2714
2715 /* Used a menu. If it starts with CTRL-Y, it must
2716 * be a "Copy" for the clipboard. Otherwise
2717 * assume that we end */
2718 if (idx == MENU_INDEX_INVALID)
2719 continue;
2720 c = *current_menu->strings[idx];
2721 if (c != NUL && current_menu->strings[idx][1] != NUL)
2722 ins_typebuf(current_menu->strings[idx] + 1,
2723 current_menu->noremap[idx], 0, TRUE,
2724 current_menu->silent[idx]);
2725 }
2726#endif
2727
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002728 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729 switch (c)
2730 {
2731 case BS: /* scroll one line back */
2732 case K_BS:
2733 case 'k':
2734 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002735 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002736 break;
2737
2738 case CAR: /* one extra line */
2739 case NL:
2740 case 'j':
2741 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002742 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 break;
2744
2745 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002746 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002747 break;
2748
2749 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002750 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002751 break;
2752
2753 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002754 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002755 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002756 break;
2757
2758 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002759 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002760 case K_PAGEDOWN:
2761 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002762 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002763 break;
2764
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002765 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002766 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002767 break;
2768
2769 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002770 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002771 lines_left = 999999;
2772 break;
2773
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002774 case ':': /* start new command line */
2775#ifdef FEAT_CON_DIALOG
2776 if (!confirm_msg_used)
2777#endif
2778 {
2779 /* Since got_int is set all typeahead will be flushed, but we
2780 * want to keep this ':', remember that in a special way. */
2781 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002782#ifdef FEAT_TERMINAL
2783 skip_term_loop = TRUE;
2784#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 cmdline_row = Rows - 1; /* put ':' on this line */
2786 skip_redraw = TRUE; /* skip redraw once */
2787 need_wait_return = FALSE; /* don't wait in main() */
2788 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002789 /* FALLTHROUGH */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002790 case 'q': /* quit */
2791 case Ctrl_C:
2792 case ESC:
2793#ifdef FEAT_CON_DIALOG
2794 if (confirm_msg_used)
2795 {
2796 /* Jump to the choices of the dialog. */
2797 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002798 }
2799 else
2800#endif
2801 {
2802 got_int = TRUE;
2803 quit_more = TRUE;
2804 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002805 /* When there is some more output (wrapping line) display that
2806 * without another prompt. */
2807 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002808 break;
2809
2810#ifdef FEAT_CLIPBOARD
2811 case Ctrl_Y:
2812 /* Strange way to allow copying (yanking) a modeless
2813 * selection at the more prompt. Use CTRL-Y,
2814 * because the same is used in Cmdline-mode and at the
2815 * hit-enter prompt. However, scrolling one line up
2816 * might be expected... */
2817 if (clip_star.state == SELECT_DONE)
2818 clip_copy_modeless_selection(TRUE);
2819 continue;
2820#endif
2821 default: /* no valid response */
2822 msg_moremsg(TRUE);
2823 continue;
2824 }
2825
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002826 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002828 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002829 {
2830 /* go to start of last line */
2831 if (mp_last == NULL)
2832 mp = msg_sb_start(last_msgchunk);
2833 else if (mp_last->sb_prev != NULL)
2834 mp = msg_sb_start(mp_last->sb_prev);
2835 else
2836 mp = NULL;
2837
2838 /* go to start of line at top of the screen */
2839 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2840 ++i)
2841 mp = msg_sb_start(mp->sb_prev);
2842
2843 if (mp != NULL && mp->sb_prev != NULL)
2844 {
2845 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002846 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002847 {
2848 if (mp == NULL || mp->sb_prev == NULL)
2849 break;
2850 mp = msg_sb_start(mp->sb_prev);
2851 if (mp_last == NULL)
2852 mp_last = msg_sb_start(last_msgchunk);
2853 else
2854 mp_last = msg_sb_start(mp_last->sb_prev);
2855 }
2856
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002857 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002858 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002859 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002860 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861 (void)disp_sb_line(0, mp);
2862 }
2863 else
2864 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002865 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002867 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2868 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002869 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002870 ++msg_scrolled;
2871 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002872 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002873 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002874 }
2875 }
2876 else
2877 {
2878 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002879 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002880 {
2881 /* scroll up, display line at bottom */
2882 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002883 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002884 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2885 (int)Columns, ' ', ' ', 0);
2886 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002887 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002888 }
2889 }
2890
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002891 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002892 {
2893 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002894 screen_fill((int)Rows - 1, (int)Rows, 0,
2895 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 msg_moremsg(FALSE);
2897 continue;
2898 }
2899
2900 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002901 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002902 }
2903
2904 break;
2905 }
2906
2907 /* clear the --more-- message */
2908 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2909 State = oldState;
2910#ifdef FEAT_MOUSE
2911 setmouse();
2912#endif
2913 if (quit_more)
2914 {
2915 msg_row = Rows - 1;
2916 msg_col = 0;
2917 }
2918#ifdef FEAT_RIGHTLEFT
2919 else if (cmdmsg_rl)
2920 msg_col = Columns - 1;
2921#endif
2922
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002923 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002924#ifdef FEAT_CON_DIALOG
2925 return retval;
2926#else
2927 return FALSE;
2928#endif
2929}
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2932
2933#ifdef mch_errmsg
2934# undef mch_errmsg
2935#endif
2936#ifdef mch_msg
2937# undef mch_msg
2938#endif
2939
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002940#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2941 static void
2942mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01002944 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002945 DWORD nwrite = 0;
2946 DWORD mode = 0;
2947 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
2948
2949 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
2950 && (int)GetConsoleCP() != enc_codepage)
2951 {
2952 WCHAR *w = enc_to_utf16((char_u *)str, &len);
2953
2954 WriteConsoleW(h, w, len, &nwrite, NULL);
2955 vim_free(w);
2956 }
2957 else
2958 {
2959 fprintf(stderr, "%s", str);
2960 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002961}
2962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002964/*
2965 * Give an error message. To be used when the screen hasn't been initialized
2966 * yet. When stderr can't be used, collect error messages until the GUI has
2967 * started and they can be displayed in a message box.
2968 */
2969 void
2970mch_errmsg(char *str)
2971{
2972#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
2973 int len;
2974#endif
2975
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02002976#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 /* On Unix use stderr if it's a tty.
2978 * When not going to start the GUI also use stderr.
2979 * On Mac, when started from Finder, stderr is the console. */
2980 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002981# ifdef UNIX
2982# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002984# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 isatty(2)
2986# endif
2987# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002988 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002989# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002990# endif
2991# ifdef FEAT_GUI
2992 !(gui.in_use || gui.starting)
2993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 )
2995 {
2996 fprintf(stderr, "%s", str);
2997 return;
2998 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003001#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3002# ifdef VIMDLL
3003 if (!(gui.in_use || gui.starting))
3004# endif
3005 {
3006 mch_errmsg_c(str);
3007 return;
3008 }
3009#endif
3010
3011#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 /* avoid a delay for a message that isn't there */
3013 emsg_on_display = FALSE;
3014
3015 len = (int)STRLEN(str) + 1;
3016 if (error_ga.ga_growsize == 0)
3017 {
3018 error_ga.ga_growsize = 80;
3019 error_ga.ga_itemsize = 1;
3020 }
3021 if (ga_grow(&error_ga, len) == OK)
3022 {
3023 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3024 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003025# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 /* remove CR characters, they are displayed */
3027 {
3028 char_u *p;
3029
3030 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3031 for (;;)
3032 {
3033 p = vim_strchr(p, '\r');
3034 if (p == NULL)
3035 break;
3036 *p = ' ';
3037 }
3038 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 --len; /* don't count the NUL at the end */
3041 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044}
3045
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003046#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3047 static void
3048mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003050 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003051 DWORD nwrite = 0;
3052 DWORD mode;
3053 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3054
3055
3056 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3057 && (int)GetConsoleCP() != enc_codepage)
3058 {
3059 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3060
3061 WriteConsoleW(h, w, len, &nwrite, NULL);
3062 vim_free(w);
3063 }
3064 else
3065 {
3066 printf("%s", str);
3067 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003068}
3069#endif
3070
3071/*
3072 * Give a message. To be used when the screen hasn't been initialized yet.
3073 * When there is no tty, collect messages until the GUI has started and they
3074 * can be displayed in a message box.
3075 */
3076 void
3077mch_msg(char *str)
3078{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003079#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3081 * uses mch_errmsg() when started from the desktop.
3082 * When not going to start the GUI also use stdout.
3083 * On Mac, when started from Finder, stderr is the console. */
3084 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003085# ifdef UNIX
3086# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003088# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003092 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003094# endif
3095# ifdef FEAT_GUI
3096 !(gui.in_use || gui.starting)
3097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 )
3099 {
3100 printf("%s", str);
3101 return;
3102 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003103#endif
3104
3105#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3106# ifdef VIMDLL
3107 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003109 {
3110 mch_msg_c(str);
3111 return;
3112 }
3113#endif
3114#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117}
3118#endif /* USE_MCH_ERRMSG */
3119
3120/*
3121 * Put a character on the screen at the current message position and advance
3122 * to the next position. Only for printable ASCII!
3123 */
3124 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003125msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 msg_didout = TRUE; /* remember that line is not empty */
3128 screen_putchar(c, msg_row, msg_col, attr);
3129#ifdef FEAT_RIGHTLEFT
3130 if (cmdmsg_rl)
3131 {
3132 if (--msg_col == 0)
3133 {
3134 msg_col = Columns;
3135 ++msg_row;
3136 }
3137 }
3138 else
3139#endif
3140 {
3141 if (++msg_col >= Columns)
3142 {
3143 msg_col = 0;
3144 ++msg_row;
3145 }
3146 }
3147}
3148
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003149 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003150msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003152 int attr;
3153 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
Bram Moolenaar8820b482017-03-16 17:23:31 +01003155 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003156 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003158 screen_puts((char_u *)
3159 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3160 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161}
3162
3163/*
3164 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3165 * exmode_active.
3166 */
3167 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003168repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 if (State == ASKMORE)
3171 {
3172 msg_moremsg(TRUE); /* display --more-- message again */
3173 msg_row = Rows - 1;
3174 }
3175#ifdef FEAT_CON_DIALOG
3176 else if (State == CONFIRM)
3177 {
3178 display_confirm_msg(); /* display ":confirm" message again */
3179 msg_row = Rows - 1;
3180 }
3181#endif
3182 else if (State == EXTERNCMD)
3183 {
3184 windgoto(msg_row, msg_col); /* put cursor back */
3185 }
3186 else if (State == HITRETURN || State == SETWSIZE)
3187 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003188 if (msg_row == Rows - 1)
3189 {
3190 /* Avoid drawing the "hit-enter" prompt below the previous one,
3191 * overwrite it. Esp. useful when regaining focus and a
3192 * FocusGained autocmd exists but didn't draw anything. */
3193 msg_didout = FALSE;
3194 msg_col = 0;
3195 msg_clr_eos();
3196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 hit_return_msg();
3198 msg_row = Rows - 1;
3199 }
3200}
3201
3202/*
3203 * msg_check_screen - check if the screen is initialized.
3204 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3205 * While starting the GUI the terminal codes will be set for the GUI, but the
3206 * output goes to the terminal. Don't use the terminal codes then.
3207 */
3208 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003209msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 if (!full_screen || !screen_valid(FALSE))
3212 return FALSE;
3213
3214 if (msg_row >= Rows)
3215 msg_row = Rows - 1;
3216 if (msg_col >= Columns)
3217 msg_col = Columns - 1;
3218 return TRUE;
3219}
3220
3221/*
3222 * Clear from current message position to end of screen.
3223 * Skip this when ":silent" was used, no need to clear for redirection.
3224 */
3225 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003226msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 if (msg_silent == 0)
3229 msg_clr_eos_force();
3230}
3231
3232/*
3233 * Clear from current message position to end of screen.
3234 * Note: msg_col is not updated, so we remember the end of the message
3235 * for msg_check().
3236 */
3237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003238msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 if (msg_use_printf())
3241 {
3242 if (full_screen) /* only when termcap codes are valid */
3243 {
3244 if (*T_CD)
3245 out_str(T_CD); /* clear to end of display */
3246 else if (*T_CE)
3247 out_str(T_CE); /* clear to end of line */
3248 }
3249 }
3250 else
3251 {
3252#ifdef FEAT_RIGHTLEFT
3253 if (cmdmsg_rl)
3254 {
3255 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3256 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3257 }
3258 else
3259#endif
3260 {
3261 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3262 ' ', ' ', 0);
3263 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3264 }
3265 }
3266}
3267
3268/*
3269 * Clear the command line.
3270 */
3271 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003272msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 msg_row = cmdline_row;
3275 msg_col = 0;
3276 msg_clr_eos_force();
3277}
3278
3279/*
3280 * end putting a message on the screen
3281 * call wait_return if the message does not fit in the available space
3282 * return TRUE if wait_return not called.
3283 */
3284 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003285msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003288 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * or the ruler option is set and we run into it,
3290 * we have to redraw the window.
3291 * Do not do this if we are abandoning the file or editing the command line.
3292 */
3293 if (!exiting && need_wait_return && !(State & CMDLINE))
3294 {
3295 wait_return(FALSE);
3296 return FALSE;
3297 }
3298 out_flush();
3299 return TRUE;
3300}
3301
3302/*
3303 * If the written message runs into the shown command or ruler, we have to
3304 * wait for hit-return and redraw the window later.
3305 */
3306 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003307msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 if (msg_row == Rows - 1 && msg_col >= sc_col)
3310 {
3311 need_wait_return = TRUE;
3312 redraw_cmdline = TRUE;
3313 }
3314}
3315
3316/*
3317 * May write a string to the redirection file.
3318 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3319 */
3320 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003321redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 char_u *s = str;
3324 static int cur_col = 0;
3325
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003326 /* Don't do anything for displaying prompts and the like. */
3327 if (redir_off)
3328 return;
3329
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003330 /* If 'verbosefile' is set prepare for writing in that file. */
3331 if (*p_vfile != NUL && verbose_fd == NULL)
3332 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003333
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003334 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 /* If the string doesn't start with CR or NL, go to msg_col */
3337 if (*s != '\n' && *s != '\r')
3338 {
3339 while (cur_col < msg_col)
3340 {
3341#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003342 if (redir_execute)
3343 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003344 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003346 else if (redir_vname)
3347 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003350 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003352 if (verbose_fd != NULL)
3353 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 ++cur_col;
3355 }
3356 }
3357
3358#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003359 if (redir_execute)
3360 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003361 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003363 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003364 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#endif
3366
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003367 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3369 {
3370#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003371 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003373 if (redir_fd != NULL)
3374 putc(*s, redir_fd);
3375 if (verbose_fd != NULL)
3376 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (*s == '\r' || *s == '\n')
3378 cur_col = 0;
3379 else if (*s == '\t')
3380 cur_col += (8 - cur_col % 8);
3381 else
3382 ++cur_col;
3383 ++s;
3384 }
3385
3386 if (msg_silent != 0) /* should update msg_col */
3387 msg_col = cur_col;
3388 }
3389}
3390
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003391 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003392redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003393{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003394 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003395#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003396 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003397#endif
3398 ;
3399}
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003402 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003403 * Must always be called paired with verbose_leave()!
3404 */
3405 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003406verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003407{
3408 if (*p_vfile != NUL)
3409 ++msg_silent;
3410}
3411
3412/*
3413 * After giving verbose message.
3414 * Must always be called paired with verbose_enter()!
3415 */
3416 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003417verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003418{
3419 if (*p_vfile != NUL)
3420 if (--msg_silent < 0)
3421 msg_silent = 0;
3422}
3423
3424/*
3425 * Like verbose_enter() and set msg_scroll when displaying the message.
3426 */
3427 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003428verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003429{
3430 if (*p_vfile != NUL)
3431 ++msg_silent;
3432 else
3433 /* always scroll up, don't overwrite */
3434 msg_scroll = TRUE;
3435}
3436
3437/*
3438 * Like verbose_leave() and set cmdline_row when displaying the message.
3439 */
3440 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003441verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003442{
3443 if (*p_vfile != NUL)
3444 {
3445 if (--msg_silent < 0)
3446 msg_silent = 0;
3447 }
3448 else
3449 cmdline_row = msg_row;
3450}
3451
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003452/*
3453 * Called when 'verbosefile' is set: stop writing to the file.
3454 */
3455 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003456verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003457{
3458 if (verbose_fd != NULL)
3459 {
3460 fclose(verbose_fd);
3461 verbose_fd = NULL;
3462 }
3463 verbose_did_open = FALSE;
3464}
3465
3466/*
3467 * Open the file 'verbosefile'.
3468 * Return FAIL or OK.
3469 */
3470 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003471verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003472{
3473 if (verbose_fd == NULL && !verbose_did_open)
3474 {
3475 /* Only give the error message once. */
3476 verbose_did_open = TRUE;
3477
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003478 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003479 if (verbose_fd == NULL)
3480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003481 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003482 return FAIL;
3483 }
3484 }
3485 return OK;
3486}
3487
3488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 * Give a warning message (for searching).
3490 * Use 'w' highlighting and may repeat the message after redrawing
3491 */
3492 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003493give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 /* Don't do this for ":silent". */
3496 if (msg_silent != 0)
3497 return;
3498
3499 /* Don't want a hit-enter prompt here. */
3500 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502#ifdef FEAT_EVAL
3503 set_vim_var_string(VV_WARNINGMSG, message, -1);
3504#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003505 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003507 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 else
3509 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003510 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003511 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 msg_didout = FALSE; /* overwrite this message */
3513 msg_nowait = TRUE; /* don't wait for this message */
3514 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 --no_wait_return;
3517}
3518
Bram Moolenaar113e1072019-01-20 15:30:40 +01003519#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003520 void
3521give_warning2(char_u *message, char_u *a1, int hl)
3522{
3523 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3524 give_warning(IObuff, hl);
3525}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003526#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528/*
3529 * Advance msg cursor to column "col".
3530 */
3531 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003532msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
3534 if (msg_silent != 0) /* nothing to advance to */
3535 {
3536 msg_col = col; /* for redirection, may fill it up later */
3537 return;
3538 }
3539 if (col >= Columns) /* not enough room */
3540 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003541#ifdef FEAT_RIGHTLEFT
3542 if (cmdmsg_rl)
3543 while (msg_col > Columns - col)
3544 msg_putchar(' ');
3545 else
3546#endif
3547 while (msg_col < col)
3548 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
3551#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3552/*
3553 * Used for "confirm()" function, and the :confirm command prefix.
3554 * Versions which haven't got flexible dialogs yet, and console
3555 * versions, get this generic handler which uses the command line.
3556 *
3557 * type = one of:
3558 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3559 * title = title string (can be NULL for default)
3560 * (neither used in console dialogs at the moment)
3561 *
3562 * Format of the "buttons" string:
3563 * "Button1Name\nButton2Name\nButton3Name"
3564 * The first button should normally be the default/accept
3565 * The second button should be the 'Cancel' button
3566 * Other buttons- use your imagination!
3567 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3568 * different letter.
3569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003571do_dialog(
3572 int type UNUSED,
3573 char_u *title UNUSED,
3574 char_u *message,
3575 char_u *buttons,
3576 int dfltbutton,
3577 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003578 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003579 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003580 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 int oldState;
3583 int retval = 0;
3584 char_u *hotkeys;
3585 int c;
3586 int i;
3587
3588#ifndef NO_CONSOLE
3589 /* Don't output anything in silent mode ("ex -s") */
3590 if (silent_mode)
3591 return dfltbutton; /* return default option */
3592#endif
3593
3594#ifdef FEAT_GUI_DIALOG
3595 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3596 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3597 {
3598 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003599 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003600 /* avoid a hit-enter prompt without clearing the cmdline */
3601 need_wait_return = FALSE;
3602 emsg_on_display = FALSE;
3603 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 /* Flush output to avoid that further messages and redrawing is done
3606 * in the wrong order. */
3607 out_flush();
3608 gui_mch_update();
3609
3610 return c;
3611 }
3612#endif
3613
3614 oldState = State;
3615 State = CONFIRM;
3616#ifdef FEAT_MOUSE
3617 setmouse();
3618#endif
3619
3620 /*
3621 * Since we wait for a keypress, don't make the
3622 * user press RETURN as well afterwards.
3623 */
3624 ++no_wait_return;
3625 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3626
3627 if (hotkeys != NULL)
3628 {
3629 for (;;)
3630 {
3631 /* Get a typed character directly from the user. */
3632 c = get_keystroke();
3633 switch (c)
3634 {
3635 case CAR: /* User accepts default option */
3636 case NL:
3637 retval = dfltbutton;
3638 break;
3639 case Ctrl_C: /* User aborts/cancels */
3640 case ESC:
3641 retval = 0;
3642 break;
3643 default: /* Could be a hotkey? */
3644 if (c < 0) /* special keys are ignored here */
3645 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003646 if (c == ':' && ex_cmd)
3647 {
3648 retval = dfltbutton;
3649 ins_char_typebuf(':');
3650 break;
3651 }
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 /* Make the character lowercase, as chars in "hotkeys" are. */
3654 c = MB_TOLOWER(c);
3655 retval = 1;
3656 for (i = 0; hotkeys[i]; ++i)
3657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (has_mbyte)
3659 {
3660 if ((*mb_ptr2char)(hotkeys + i) == c)
3661 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003662 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (hotkeys[i] == c)
3666 break;
3667 ++retval;
3668 }
3669 if (hotkeys[i])
3670 break;
3671 /* No hotkey match, so keep waiting */
3672 continue;
3673 }
3674 break;
3675 }
3676
3677 vim_free(hotkeys);
3678 }
3679
3680 State = oldState;
3681#ifdef FEAT_MOUSE
3682 setmouse();
3683#endif
3684 --no_wait_return;
3685 msg_end_prompt();
3686
3687 return retval;
3688}
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690/*
3691 * Copy one character from "*from" to "*to", taking care of multi-byte
3692 * characters. Return the length of the character in bytes.
3693 */
3694 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003695copy_char(
3696 char_u *from,
3697 char_u *to,
3698 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 int len;
3701 int c;
3702
3703 if (has_mbyte)
3704 {
3705 if (lowercase)
3706 {
3707 c = MB_TOLOWER((*mb_ptr2char)(from));
3708 return (*mb_char2bytes)(c, to);
3709 }
3710 else
3711 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003712 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 mch_memmove(to, from, (size_t)len);
3714 return len;
3715 }
3716 }
3717 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 {
3719 if (lowercase)
3720 *to = (char_u)TOLOWER_LOC(*from);
3721 else
3722 *to = *from;
3723 return 1;
3724 }
3725}
3726
3727/*
3728 * Format the dialog string, and display it at the bottom of
3729 * the screen. Return a string of hotkey chars (if defined) for
3730 * each 'button'. If a button has no hotkey defined, the first character of
3731 * the button is used.
3732 * The hotkeys can be multi-byte characters, but without combining chars.
3733 *
3734 * Returns an allocated string with hotkeys, or NULL for error.
3735 */
3736 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003737msg_show_console_dialog(
3738 char_u *message,
3739 char_u *buttons,
3740 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003743#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 int lenhotkey = HOTK_LEN; /* count first button */
3745 char_u *hotk = NULL;
3746 char_u *msgp = NULL;
3747 char_u *hotkp = NULL;
3748 char_u *r;
3749 int copy;
3750#define HAS_HOTKEY_LEN 30
3751 char_u has_hotkey[HAS_HOTKEY_LEN];
3752 int first_hotkey = FALSE; /* first char of button is hotkey */
3753 int idx;
3754
3755 has_hotkey[0] = FALSE;
3756
3757 /*
3758 * First loop: compute the size of memory to allocate.
3759 * Second loop: copy to the allocated memory.
3760 */
3761 for (copy = 0; copy <= 1; ++copy)
3762 {
3763 r = buttons;
3764 idx = 0;
3765 while (*r)
3766 {
3767 if (*r == DLG_BUTTON_SEP)
3768 {
3769 if (copy)
3770 {
3771 *msgp++ = ',';
3772 *msgp++ = ' '; /* '\n' -> ', ' */
3773
3774 /* advance to next hotkey and set default hotkey */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003776 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003779 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 if (dfltbutton)
3781 --dfltbutton;
3782
3783 /* If no hotkey is specified first char is used. */
3784 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3785 first_hotkey = TRUE;
3786 }
3787 else
3788 {
3789 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3790 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3791 if (idx < HAS_HOTKEY_LEN - 1)
3792 has_hotkey[++idx] = FALSE;
3793 }
3794 }
3795 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3796 {
3797 if (*r == DLG_HOTKEY_CHAR)
3798 ++r;
3799 first_hotkey = FALSE;
3800 if (copy)
3801 {
3802 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3803 *msgp++ = *r;
3804 else
3805 {
3806 /* '&a' -> '[a]' */
3807 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3808 msgp += copy_char(r, msgp, FALSE);
3809 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3810
3811 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003812 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814 }
3815 else
3816 {
3817 ++len; /* '&a' -> '[a]' */
3818 if (idx < HAS_HOTKEY_LEN - 1)
3819 has_hotkey[idx] = TRUE;
3820 }
3821 }
3822 else
3823 {
3824 /* everything else copy literally */
3825 if (copy)
3826 msgp += copy_char(r, msgp, FALSE);
3827 }
3828
3829 /* advance to the next character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003830 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
3833 if (copy)
3834 {
3835 *msgp++ = ':';
3836 *msgp++ = ' ';
3837 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 else
3840 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003841 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003842 + 2 /* for the NL's */
3843 + STRLEN(buttons)
3844 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 /* If no hotkey is specified first char is used. */
3848 if (!has_hotkey[0])
3849 {
3850 first_hotkey = TRUE;
3851 len += 2; /* "x" -> "[x]" */
3852 }
3853
3854 /*
3855 * Now allocate and load the strings
3856 */
3857 vim_free(confirm_msg);
3858 confirm_msg = alloc(len);
3859 if (confirm_msg == NULL)
3860 return NULL;
3861 *confirm_msg = NUL;
3862 hotk = alloc(lenhotkey);
3863 if (hotk == NULL)
3864 return NULL;
3865
3866 *confirm_msg = '\n';
3867 STRCPY(confirm_msg + 1, message);
3868
3869 msgp = confirm_msg + 1 + STRLEN(message);
3870 hotkp = hotk;
3871
Bram Moolenaar6a516062007-07-05 08:11:42 +00003872 /* Define first default hotkey. Keep the hotkey string NUL
3873 * terminated to avoid reading past the end. */
3874 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
3876 /* Remember where the choices start, displaying starts here when
3877 * "hotkp" typed at the more prompt. */
3878 confirm_msg_tail = msgp;
3879 *msgp++ = '\n';
3880 }
3881 }
3882
3883 display_confirm_msg();
3884 return hotk;
3885}
3886
3887/*
3888 * Display the ":confirm" message. Also called when screen resized.
3889 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003890 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003891display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
3893 /* avoid that 'q' at the more prompt truncates the message here */
3894 ++confirm_msg_used;
3895 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003896 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 --confirm_msg_used;
3898}
3899
3900#endif /* FEAT_CON_DIALOG */
3901
3902#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3903
3904 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003905vim_dialog_yesno(
3906 int type,
3907 char_u *title,
3908 char_u *message,
3909 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910{
3911 if (do_dialog(type,
3912 title == NULL ? (char_u *)_("Question") : title,
3913 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003914 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 return VIM_YES;
3916 return VIM_NO;
3917}
3918
3919 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003920vim_dialog_yesnocancel(
3921 int type,
3922 char_u *title,
3923 char_u *message,
3924 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926 switch (do_dialog(type,
3927 title == NULL ? (char_u *)_("Question") : title,
3928 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003929 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
3931 case 1: return VIM_YES;
3932 case 2: return VIM_NO;
3933 }
3934 return VIM_CANCEL;
3935}
3936
3937 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003938vim_dialog_yesnoallcancel(
3939 int type,
3940 char_u *title,
3941 char_u *message,
3942 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 switch (do_dialog(type,
3945 title == NULL ? (char_u *)"Question" : title,
3946 message,
3947 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003948 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
3950 case 1: return VIM_YES;
3951 case 2: return VIM_NO;
3952 case 3: return VIM_ALL;
3953 case 4: return VIM_DISCARDALL;
3954 }
3955 return VIM_CANCEL;
3956}
3957
3958#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3959
3960#if defined(FEAT_BROWSE) || defined(PROTO)
3961/*
3962 * Generic browse function. Calls gui_mch_browse() when possible.
3963 * Later this may pop-up a non-GUI file selector (external command?).
3964 */
3965 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003966do_browse(
3967 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3968 char_u *title, /* title for the window */
3969 char_u *dflt, /* default file name (may include directory) */
3970 char_u *ext, /* extension added */
3971 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003973 char_u *filter, /* file name filter */
3974 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 char_u *fname;
3977 static char_u *last_dir = NULL; /* last used directory */
3978 char_u *tofree = NULL;
3979 int save_browse = cmdmod.browse;
3980
3981 /* Must turn off browse to avoid that autocommands will get the
3982 * flag too! */
3983 cmdmod.browse = FALSE;
3984
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003985 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003987 if (flags & BROWSE_DIR)
3988 title = (char_u *)_("Select Directory dialog");
3989 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 title = (char_u *)_("Save File dialog");
3991 else
3992 title = (char_u *)_("Open File dialog");
3993 }
3994
3995 /* When no directory specified, use default file name, default dir, buffer
3996 * dir, last dir or current dir */
3997 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3998 {
3999 if (mch_isdir(dflt)) /* default file name is a directory */
4000 {
4001 initdir = dflt;
4002 dflt = NULL;
4003 }
4004 else if (gettail(dflt) != dflt) /* default file name includes a path */
4005 {
4006 tofree = vim_strsave(dflt);
4007 if (tofree != NULL)
4008 {
4009 initdir = tofree;
4010 *gettail(initdir) = NUL;
4011 dflt = gettail(dflt);
4012 }
4013 }
4014 }
4015
4016 if (initdir == NULL || *initdir == NUL)
4017 {
4018 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004019 if (STRCMP(p_bsdir, "last") != 0
4020 && STRCMP(p_bsdir, "buffer") != 0
4021 && STRCMP(p_bsdir, "current") != 0
4022 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 initdir = p_bsdir;
4024 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004025 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 && buf != NULL && buf->b_ffname != NULL)
4027 {
4028 if (dflt == NULL || *dflt == NUL)
4029 dflt = gettail(curbuf->b_ffname);
4030 tofree = vim_strsave(curbuf->b_ffname);
4031 if (tofree != NULL)
4032 {
4033 initdir = tofree;
4034 *gettail(initdir) = NUL;
4035 }
4036 }
4037 /* When 'browsedir' is "last", use dir from last browse */
4038 else if (*p_bsdir == 'l')
4039 initdir = last_dir;
4040 /* When 'browsedir is "current", use current directory. This is the
4041 * default already, leave initdir empty. */
4042 }
4043
4044# ifdef FEAT_GUI
4045 if (gui.in_use) /* when this changes, also adjust f_has()! */
4046 {
4047 if (filter == NULL
4048# ifdef FEAT_EVAL
4049 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
4050 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
4051# endif
4052 )
4053 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004054 if (flags & BROWSE_DIR)
4055 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004056# if defined(FEAT_GUI_GTK) || defined(MSWIN)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004057 /* For systems that have a directory dialog. */
4058 fname = gui_mch_browsedir(title, initdir);
4059# else
4060 /* Generic solution for selecting a directory: select a file and
4061 * remove the file name. */
4062 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
4063# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004064# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004065 /* Win32 adds a dummy file name, others return an arbitrary file
4066 * name. GTK+ 2 returns only the directory, */
4067 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
4068 {
4069 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004070 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004071
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004072 if (tail == fname)
4073 *tail++ = '.'; /* use current dir */
4074 *tail = NUL;
4075 }
4076# endif
4077 }
4078 else
4079 fname = gui_mch_browse(flags & BROWSE_SAVE,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004080 title, dflt, ext, initdir, (char_u *)_(filter));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 /* We hang around in the dialog for a while, the user might do some
4083 * things to our files. The Win32 dialog allows deleting or renaming
4084 * a file, check timestamps. */
4085 need_check_timestamps = TRUE;
4086 did_check_timestamps = FALSE;
4087 }
4088 else
4089# endif
4090 {
4091 /* TODO: non-GUI file selector here */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004092 emsg(_("E338: Sorry, no file browser in console mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 fname = NULL;
4094 }
4095
4096 /* keep the directory for next time */
4097 if (fname != NULL)
4098 {
4099 vim_free(last_dir);
4100 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004101 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 *gettail(last_dir) = NUL;
4104 if (*last_dir == NUL)
4105 {
4106 /* filename only returned, must be in current dir */
4107 vim_free(last_dir);
4108 last_dir = alloc(MAXPATHL);
4109 if (last_dir != NULL)
4110 mch_dirname(last_dir, MAXPATHL);
4111 }
4112 }
4113 }
4114
4115 vim_free(tofree);
4116 cmdmod.browse = save_browse;
4117
4118 return fname;
4119}
4120#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004121
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004122#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004123static char *e_printf = N_("E766: Insufficient arguments for printf()");
4124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004125/*
4126 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4127 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004128 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004129tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130{
4131 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004132 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133 int err = FALSE;
4134
4135 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004136 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004137 else
4138 {
4139 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004140 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 if (err)
4142 n = 0;
4143 }
4144 return n;
4145}
4146
4147/*
4148 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004149 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004150 * are not converted to a string.
4151 * If "tofree" is not NULL echo_string() is used. All types are converted to
4152 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004153 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004154 */
4155 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004156tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004158 int idx = *idxp - 1;
4159 char *s = NULL;
4160 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161
4162 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004163 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004164 else
4165 {
4166 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004167 if (tofree != NULL)
4168 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4169 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004170 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171 }
4172 return s;
4173}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004174
4175# ifdef FEAT_FLOAT
4176/*
4177 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4178 */
4179 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004180tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004181{
4182 int idx = *idxp - 1;
4183 double f = 0;
4184
4185 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004186 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004187 else
4188 {
4189 ++*idxp;
4190 if (tvs[idx].v_type == VAR_FLOAT)
4191 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004192 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004193 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004194 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004195 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004196 }
4197 return f;
4198}
4199# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200#endif
4201
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004202#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004203/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004204 * Return the representation of infinity for printf() function:
4205 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4206 */
4207 static const char *
4208infinity_str(int positive,
4209 char fmt_spec,
4210 int force_sign,
4211 int space_for_positive)
4212{
4213 static const char *table[] =
4214 {
4215 "-inf", "inf", "+inf", " inf",
4216 "-INF", "INF", "+INF", " INF"
4217 };
4218 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4219
4220 if (ASCII_ISUPPER(fmt_spec))
4221 idx += 4;
4222 return table[idx];
4223}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004224#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004225
4226/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004227 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004228 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004229 * consistency.
4230 *
4231 * This code is based on snprintf.c - a portable implementation of snprintf
4232 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004233 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004234 * The original code, including useful comments, can be found here:
4235 * http://www.ijs.si/software/snprintf/
4236 *
4237 * This snprintf() only supports the following conversion specifiers:
4238 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4239 * with flags: '-', '+', ' ', '0' and '#'.
4240 * An asterisk is supported for field width as well as precision.
4241 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004242 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004243 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004244 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4245 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004246 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004247 * The locale is not used, the string is used as a byte string. This is only
4248 * relevant for double-byte encodings where the second byte may be '%'.
4249 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004250 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4251 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252 *
4253 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004254 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004255 * is greater or equal to "str_m", not all characters from the result
4256 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4257 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004258 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004259 */
4260
4261/*
4262 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004263 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004264 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004265 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004266 */
4267
4268/* When generating prototypes all of this is skipped, cproto doesn't
4269 * understand this. */
4270#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004271
Bram Moolenaara800b422010-06-27 01:15:55 +02004272/* Like vim_vsnprintf() but append to the string. */
4273 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004275{
4276 va_list ap;
4277 int str_l;
4278 size_t len = STRLEN(str);
4279 size_t space;
4280
4281 if (str_m <= len)
4282 space = 0;
4283 else
4284 space = str_m - len;
4285 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004286 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004287 va_end(ap);
4288 return str_l;
4289}
Bram Moolenaara800b422010-06-27 01:15:55 +02004290
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004291 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004292vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004293{
4294 va_list ap;
4295 int str_l;
4296
4297 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004298 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004299 va_end(ap);
4300 return str_l;
4301}
4302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004303 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004304vim_vsnprintf(
4305 char *str,
4306 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004307 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004308 va_list ap)
4309{
4310 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4311}
4312
4313 int
4314vim_vsnprintf_typval(
4315 char *str,
4316 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004317 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004318 va_list ap,
4319 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004320{
4321 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004322 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004323 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004324
4325 if (p == NULL)
4326 p = "";
4327 while (*p != NUL)
4328 {
4329 if (*p != '%')
4330 {
4331 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004332 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004333
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004334 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004335 if (str_l < str_m)
4336 {
4337 size_t avail = str_m - str_l;
4338
4339 mch_memmove(str + str_l, p, n > avail ? avail : n);
4340 }
4341 p += n;
4342 str_l += n;
4343 }
4344 else
4345 {
4346 size_t min_field_width = 0, precision = 0;
4347 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4348 int alternate_form = 0, force_sign = 0;
4349
4350 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4351 * ignored. */
4352 int space_for_positive = 1;
4353
4354 /* allowed values: \0, h, l, L */
4355 char length_modifier = '\0';
4356
4357 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004358# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004359# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004360 * That sounds reasonable to use as the maximum
4361 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004362# elif defined(FEAT_NUM64)
4363# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004364# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004365# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004366# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004367 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004368
4369 /* string address in case of string argument */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004370 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004371
4372 /* natural field width of arg without padding and sign */
4373 size_t str_arg_l;
4374
4375 /* unsigned char argument value - only defined for c conversion.
4376 * N.B. standard explicitly states the char argument for the c
4377 * conversion is unsigned */
4378 unsigned char uchar_arg;
4379
4380 /* number of zeros to be inserted for numeric conversions as
4381 * required by the precision or minimal field width */
4382 size_t number_of_zeros_to_pad = 0;
4383
4384 /* index into tmp where zero padding is to be inserted */
4385 size_t zero_padding_insertion_ind = 0;
4386
4387 /* current conversion specifier character */
4388 char fmt_spec = '\0';
4389
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004390 /* buffer for 's' and 'S' specs */
4391 char_u *tofree = NULL;
4392
4393
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004394 p++; /* skip '%' */
4395
4396 /* parse flags */
4397 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4398 || *p == '#' || *p == '\'')
4399 {
4400 switch (*p)
4401 {
4402 case '0': zero_padding = 1; break;
4403 case '-': justify_left = 1; break;
4404 case '+': force_sign = 1; space_for_positive = 0; break;
4405 case ' ': force_sign = 1;
4406 /* If both the ' ' and '+' flags appear, the ' '
4407 * flag should be ignored */
4408 break;
4409 case '#': alternate_form = 1; break;
4410 case '\'': break;
4411 }
4412 p++;
4413 }
4414 /* If the '0' and '-' flags both appear, the '0' flag should be
4415 * ignored. */
4416
4417 /* parse field width */
4418 if (*p == '*')
4419 {
4420 int j;
4421
4422 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004425 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426# endif
4427 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004428 if (j >= 0)
4429 min_field_width = j;
4430 else
4431 {
4432 min_field_width = -j;
4433 justify_left = 1;
4434 }
4435 }
4436 else if (VIM_ISDIGIT((int)(*p)))
4437 {
4438 /* size_t could be wider than unsigned int; make sure we treat
4439 * argument like common implementations do */
4440 unsigned int uj = *p++ - '0';
4441
4442 while (VIM_ISDIGIT((int)(*p)))
4443 uj = 10 * uj + (unsigned int)(*p++ - '0');
4444 min_field_width = uj;
4445 }
4446
4447 /* parse precision */
4448 if (*p == '.')
4449 {
4450 p++;
4451 precision_specified = 1;
4452 if (*p == '*')
4453 {
4454 int j;
4455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004458 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459# endif
4460 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461 p++;
4462 if (j >= 0)
4463 precision = j;
4464 else
4465 {
4466 precision_specified = 0;
4467 precision = 0;
4468 }
4469 }
4470 else if (VIM_ISDIGIT((int)(*p)))
4471 {
4472 /* size_t could be wider than unsigned int; make sure we
4473 * treat argument like common implementations do */
4474 unsigned int uj = *p++ - '0';
4475
4476 while (VIM_ISDIGIT((int)(*p)))
4477 uj = 10 * uj + (unsigned int)(*p++ - '0');
4478 precision = uj;
4479 }
4480 }
4481
4482 /* parse 'h', 'l' and 'll' length modifiers */
4483 if (*p == 'h' || *p == 'l')
4484 {
4485 length_modifier = *p;
4486 p++;
4487 if (length_modifier == 'l' && *p == 'l')
4488 {
4489 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004490# ifdef FEAT_NUM64
4491 length_modifier = 'L';
4492# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004493 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004494# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004495 p++;
4496 }
4497 }
4498 fmt_spec = *p;
4499
4500 /* common synonyms: */
4501 switch (fmt_spec)
4502 {
4503 case 'i': fmt_spec = 'd'; break;
4504 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4505 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4506 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4507 default: break;
4508 }
4509
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004510# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4511 switch (fmt_spec)
4512 {
4513 case 'd': case 'u': case 'o': case 'x': case 'X':
4514 if (tvs != NULL && length_modifier == '\0')
4515 length_modifier = 'L';
4516 }
4517# endif
4518
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004519 /* get parameter value, do initial processing */
4520 switch (fmt_spec)
4521 {
4522 /* '%' and 'c' behave similar to 's' regarding flags and field
4523 * widths */
4524 case '%':
4525 case 'c':
4526 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004527 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004528 str_arg_l = 1;
4529 switch (fmt_spec)
4530 {
4531 case '%':
4532 str_arg = p;
4533 break;
4534
4535 case 'c':
4536 {
4537 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538
4539 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004541 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542# endif
4543 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004544 /* standard demands unsigned char */
4545 uchar_arg = (unsigned char)j;
4546 str_arg = (char *)&uchar_arg;
4547 break;
4548 }
4549
4550 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004551 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004554 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555# endif
4556 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004557 if (str_arg == NULL)
4558 {
4559 str_arg = "[NULL]";
4560 str_arg_l = 6;
4561 }
4562 /* make sure not to address string beyond the specified
4563 * precision !!! */
4564 else if (!precision_specified)
4565 str_arg_l = strlen(str_arg);
4566 /* truncate string if necessary as requested by precision */
4567 else if (precision == 0)
4568 str_arg_l = 0;
4569 else
4570 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004571 /* Don't put the #if inside memchr(), it can be a
4572 * macro. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004573 /* memchr on HP does not like n > 2^31 !!! */
4574 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004575 precision <= (size_t)0x7fffffffL ? precision
4576 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004577 str_arg_l = (q == NULL) ? precision
4578 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004579 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004580 if (fmt_spec == 'S')
4581 {
4582 if (min_field_width != 0)
4583 min_field_width += STRLEN(str_arg)
4584 - mb_string2cells((char_u *)str_arg, -1);
4585 if (precision)
4586 {
4587 char_u *p1 = (char_u *)str_arg;
4588 size_t i;
4589
4590 for (i = 0; i < precision && *p1; i++)
4591 p1 += mb_ptr2len(p1);
4592
4593 str_arg_l = precision = p1 - (char_u *)str_arg;
4594 }
4595 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004596 break;
4597
4598 default:
4599 break;
4600 }
4601 break;
4602
Bram Moolenaar91984b92016-08-16 21:58:41 +02004603 case 'd': case 'u':
4604 case 'b': case 'B':
4605 case 'o':
4606 case 'x': case 'X':
4607 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004608 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004609 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004610 * imply the value is unsigned; d implies a signed
4611 * value */
4612
4613 /* 0 if numeric argument is zero (or if pointer is
4614 * NULL for 'p'), +1 if greater than zero (or nonzero
4615 * for unsigned arguments), -1 if negative (unsigned
4616 * argument is never negative) */
4617 int arg_sign = 0;
4618
4619 /* only defined for length modifier h, or for no
4620 * length modifiers */
4621 int int_arg = 0;
4622 unsigned int uint_arg = 0;
4623
4624 /* only defined for length modifier l */
4625 long int long_arg = 0;
4626 unsigned long int ulong_arg = 0;
4627
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004628# ifdef FEAT_NUM64
4629 /* only defined for length modifier ll */
4630 varnumber_T llong_arg = 0;
4631 uvarnumber_T ullong_arg = 0;
4632# endif
4633
Bram Moolenaare9997822016-08-28 16:03:38 +02004634 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004635 uvarnumber_T bin_arg = 0;
4636
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004637 /* pointer argument value -only defined for p
4638 * conversion */
4639 void *ptr_arg = NULL;
4640
4641 if (fmt_spec == 'p')
4642 {
4643 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004646 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4647 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648# endif
4649 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004650 if (ptr_arg != NULL)
4651 arg_sign = 1;
4652 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004653 else if (fmt_spec == 'b' || fmt_spec == 'B')
4654 {
4655 bin_arg =
4656# if defined(FEAT_EVAL)
4657 tvs != NULL ?
4658 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4659# endif
4660 va_arg(ap, uvarnumber_T);
4661 if (bin_arg != 0)
4662 arg_sign = 1;
4663 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004664 else if (fmt_spec == 'd')
4665 {
4666 /* signed */
4667 switch (length_modifier)
4668 {
4669 case '\0':
4670 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004671 /* char and short arguments are passed as int. */
4672 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004674 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004675# endif
4676 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004677 if (int_arg > 0)
4678 arg_sign = 1;
4679 else if (int_arg < 0)
4680 arg_sign = -1;
4681 break;
4682 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004685 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004686# endif
4687 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004688 if (long_arg > 0)
4689 arg_sign = 1;
4690 else if (long_arg < 0)
4691 arg_sign = -1;
4692 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004693# ifdef FEAT_NUM64
4694 case 'L':
4695 llong_arg =
4696# if defined(FEAT_EVAL)
4697 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4698# endif
4699 va_arg(ap, varnumber_T);
4700 if (llong_arg > 0)
4701 arg_sign = 1;
4702 else if (llong_arg < 0)
4703 arg_sign = -1;
4704 break;
4705# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004706 }
4707 }
4708 else
4709 {
4710 /* unsigned */
4711 switch (length_modifier)
4712 {
4713 case '\0':
4714 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004715 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004716# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004717 tvs != NULL ? (unsigned)
4718 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004719# endif
4720 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004721 if (uint_arg != 0)
4722 arg_sign = 1;
4723 break;
4724 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004727 tvs != NULL ? (unsigned long)
4728 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004729# endif
4730 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004731 if (ulong_arg != 0)
4732 arg_sign = 1;
4733 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004734# ifdef FEAT_NUM64
4735 case 'L':
4736 ullong_arg =
4737# if defined(FEAT_EVAL)
4738 tvs != NULL ? (uvarnumber_T)
4739 tv_nr(tvs, &arg_idx) :
4740# endif
4741 va_arg(ap, uvarnumber_T);
4742 if (ullong_arg != 0)
4743 arg_sign = 1;
4744 break;
4745# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004746 }
4747 }
4748
4749 str_arg = tmp;
4750 str_arg_l = 0;
4751
4752 /* NOTE:
4753 * For d, i, u, o, x, and X conversions, if precision is
4754 * specified, the '0' flag should be ignored. This is so
4755 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4756 * FreeBSD, NetBSD; but not with Perl.
4757 */
4758 if (precision_specified)
4759 zero_padding = 0;
4760 if (fmt_spec == 'd')
4761 {
4762 if (force_sign && arg_sign >= 0)
4763 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4764 /* leave negative numbers for sprintf to handle, to
4765 * avoid handling tricky cases like (short int)-32768 */
4766 }
4767 else if (alternate_form)
4768 {
4769 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004770 && (fmt_spec == 'b' || fmt_spec == 'B'
4771 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004772 {
4773 tmp[str_arg_l++] = '0';
4774 tmp[str_arg_l++] = fmt_spec;
4775 }
4776 /* alternate form should have no effect for p
4777 * conversion, but ... */
4778 }
4779
4780 zero_padding_insertion_ind = str_arg_l;
4781 if (!precision_specified)
4782 precision = 1; /* default precision is 1 */
4783 if (precision == 0 && arg_sign == 0)
4784 {
4785 /* When zero value is formatted with an explicit
4786 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004787 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004788 }
4789 else
4790 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004791 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004792 int f_l = 0;
4793
4794 /* construct a simple format string for sprintf */
4795 f[f_l++] = '%';
4796 if (!length_modifier)
4797 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004798 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004800# ifdef FEAT_NUM64
Bram Moolenaar4f974752019-02-17 17:44:42 +01004801# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004802 f[f_l++] = 'I';
4803 f[f_l++] = '6';
4804 f[f_l++] = '4';
4805# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004806 f[f_l++] = 'l';
4807 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004808# endif
4809# else
4810 f[f_l++] = 'l';
4811# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004812 }
4813 else
4814 f[f_l++] = length_modifier;
4815 f[f_l++] = fmt_spec;
4816 f[f_l++] = '\0';
4817
4818 if (fmt_spec == 'p')
4819 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004820 else if (fmt_spec == 'b' || fmt_spec == 'B')
4821 {
4822 char b[8 * sizeof(uvarnumber_T)];
4823 size_t b_l = 0;
4824 uvarnumber_T bn = bin_arg;
4825
4826 do
4827 {
4828 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4829 bn >>= 1;
4830 }
4831 while (bn != 0);
4832
4833 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4834 str_arg_l += b_l;
4835 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004836 else if (fmt_spec == 'd')
4837 {
4838 /* signed */
4839 switch (length_modifier)
4840 {
4841 case '\0':
4842 case 'h': str_arg_l += sprintf(
4843 tmp + str_arg_l, f, int_arg);
4844 break;
4845 case 'l': str_arg_l += sprintf(
4846 tmp + str_arg_l, f, long_arg);
4847 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004848# ifdef FEAT_NUM64
4849 case 'L': str_arg_l += sprintf(
4850 tmp + str_arg_l, f, llong_arg);
4851 break;
4852# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004853 }
4854 }
4855 else
4856 {
4857 /* unsigned */
4858 switch (length_modifier)
4859 {
4860 case '\0':
4861 case 'h': str_arg_l += sprintf(
4862 tmp + str_arg_l, f, uint_arg);
4863 break;
4864 case 'l': str_arg_l += sprintf(
4865 tmp + str_arg_l, f, ulong_arg);
4866 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004867# ifdef FEAT_NUM64
4868 case 'L': str_arg_l += sprintf(
4869 tmp + str_arg_l, f, ullong_arg);
4870 break;
4871# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004872 }
4873 }
4874
4875 /* include the optional minus sign and possible
4876 * "0x" in the region before the zero padding
4877 * insertion point */
4878 if (zero_padding_insertion_ind < str_arg_l
4879 && tmp[zero_padding_insertion_ind] == '-')
4880 zero_padding_insertion_ind++;
4881 if (zero_padding_insertion_ind + 1 < str_arg_l
4882 && tmp[zero_padding_insertion_ind] == '0'
4883 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4884 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4885 zero_padding_insertion_ind += 2;
4886 }
4887
4888 {
4889 size_t num_of_digits = str_arg_l
4890 - zero_padding_insertion_ind;
4891
4892 if (alternate_form && fmt_spec == 'o'
4893 /* unless zero is already the first
4894 * character */
4895 && !(zero_padding_insertion_ind < str_arg_l
4896 && tmp[zero_padding_insertion_ind] == '0'))
4897 {
4898 /* assure leading zero for alternate-form
4899 * octal numbers */
4900 if (!precision_specified
4901 || precision < num_of_digits + 1)
4902 {
4903 /* precision is increased to force the
4904 * first character to be zero, except if a
4905 * zero value is formatted with an
4906 * explicit precision of zero */
4907 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004908 }
4909 }
4910 /* zero padding to specified precision? */
4911 if (num_of_digits < precision)
4912 number_of_zeros_to_pad = precision - num_of_digits;
4913 }
4914 /* zero padding to specified minimal field width? */
4915 if (!justify_left && zero_padding)
4916 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004917 int n = (int)(min_field_width - (str_arg_l
4918 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004919 if (n > 0)
4920 number_of_zeros_to_pad += n;
4921 }
4922 break;
4923 }
4924
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004925# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004926 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004927 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004928 case 'e':
4929 case 'E':
4930 case 'g':
4931 case 'G':
4932 {
4933 /* Floating point. */
4934 double f;
4935 double abs_f;
4936 char format[40];
4937 int l;
4938 int remove_trailing_zeroes = FALSE;
4939
4940 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004941# if defined(FEAT_EVAL)
4942 tvs != NULL ? tv_float(tvs, &arg_idx) :
4943# endif
4944 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004945 abs_f = f < 0 ? -f : f;
4946
4947 if (fmt_spec == 'g' || fmt_spec == 'G')
4948 {
4949 /* Would be nice to use %g directly, but it prints
4950 * "1.0" as "1", we don't want that. */
4951 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4952 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004953 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004954 else
4955 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4956 remove_trailing_zeroes = TRUE;
4957 }
4958
Bram Moolenaar04186092016-08-29 21:55:35 +02004959 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004960# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004961 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004962# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004963 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004964# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004965 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004966 {
4967 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004968 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4969 force_sign, space_for_positive));
4970 str_arg_l = STRLEN(tmp);
4971 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004972 }
4973 else
4974 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004975 if (isnan(f))
4976 {
4977 /* Not a number: nan or NAN */
4978 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4979 : "nan");
4980 str_arg_l = 3;
4981 zero_padding = 0;
4982 }
4983 else if (isinf(f))
4984 {
4985 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4986 force_sign, space_for_positive));
4987 str_arg_l = STRLEN(tmp);
4988 zero_padding = 0;
4989 }
4990 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004991 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004992 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004993 format[0] = '%';
4994 l = 1;
4995 if (force_sign)
4996 format[l++] = space_for_positive ? ' ' : '+';
4997 if (precision_specified)
4998 {
4999 size_t max_prec = TMP_LEN - 10;
5000
5001 /* Make sure we don't get more digits than we
5002 * have room for. */
5003 if ((fmt_spec == 'f' || fmt_spec == 'F')
5004 && abs_f > 1.0)
5005 max_prec -= (size_t)log10(abs_f);
5006 if (precision > max_prec)
5007 precision = max_prec;
5008 l += sprintf(format + l, ".%d", (int)precision);
5009 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02005010 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02005011 format[l + 1] = NUL;
5012
Bram Moolenaare9997822016-08-28 16:03:38 +02005013 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01005014 }
Bram Moolenaar99922372016-08-27 15:26:35 +02005015
Bram Moolenaara7241f52008-06-24 20:39:31 +00005016 if (remove_trailing_zeroes)
5017 {
5018 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005019 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005020
5021 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02005022 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005023 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005024 else
5025 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005026 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005027 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005028 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005029 {
5030 /* Remove superfluous '+' and leading
5031 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005032 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005033 {
5034 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005035 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005036 --str_arg_l;
5037 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005038 i = (tp[1] == '-') ? 2 : 1;
5039 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005040 {
5041 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005042 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005043 --str_arg_l;
5044 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005045 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005046 }
5047 }
5048
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005049 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005050 /* Remove trailing zeroes, but keep the one
5051 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005052 while (tp > tmp + 2 && *tp == '0'
5053 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005054 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005055 STRMOVE(tp, tp + 1);
5056 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005057 --str_arg_l;
5058 }
5059 }
5060 else
5061 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005062 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005063
5064 /* Be consistent: some printf("%e") use 1.0e+12
5065 * and some 1.0e+012. Remove one zero in the last
5066 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005067 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005068 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005069 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5070 && tp[2] == '0'
5071 && vim_isdigit(tp[3])
5072 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005073 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005074 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005075 --str_arg_l;
5076 }
5077 }
5078 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005079 if (zero_padding && min_field_width > str_arg_l
5080 && (tmp[0] == '-' || force_sign))
5081 {
5082 /* padding 0's should be inserted after the sign */
5083 number_of_zeros_to_pad = min_field_width - str_arg_l;
5084 zero_padding_insertion_ind = 1;
5085 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005086 str_arg = tmp;
5087 break;
5088 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005089# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005090
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005091 default:
5092 /* unrecognized conversion specifier, keep format string
5093 * as-is */
5094 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005095 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005096 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005097 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005098
5099 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005100 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005101 str_arg = p;
5102 str_arg_l = 0;
5103 if (*p != NUL)
5104 str_arg_l++; /* include invalid conversion specifier
5105 unchanged if not at end-of-string */
5106 break;
5107 }
5108
5109 if (*p != NUL)
5110 p++; /* step over the just processed conversion specifier */
5111
5112 /* insert padding to the left as requested by min_field_width;
5113 * this does not include the zero padding in case of numerical
5114 * conversions*/
5115 if (!justify_left)
5116 {
5117 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005118 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005119
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005120 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005121 {
5122 if (str_l < str_m)
5123 {
5124 size_t avail = str_m - str_l;
5125
5126 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005127 (size_t)pn > avail ? avail
5128 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005129 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005130 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005131 }
5132 }
5133
5134 /* zero padding as requested by the precision or by the minimal
5135 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005136 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005137 {
5138 /* will not copy first part of numeric right now, *
5139 * force it to be copied later in its entirety */
5140 zero_padding_insertion_ind = 0;
5141 }
5142 else
5143 {
5144 /* insert first part of numerics (sign or '0x') before zero
5145 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005146 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005147
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005148 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005149 {
5150 if (str_l < str_m)
5151 {
5152 size_t avail = str_m - str_l;
5153
5154 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005155 (size_t)zn > avail ? avail
5156 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005157 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005158 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005159 }
5160
5161 /* insert zero padding as requested by the precision or min
5162 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005163 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005164 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005165 {
5166 if (str_l < str_m)
5167 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005168 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005169
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005170 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005171 (size_t)zn > avail ? avail
5172 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005173 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005174 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005175 }
5176 }
5177
5178 /* insert formatted string
5179 * (or as-is conversion specifier for unknown conversions) */
5180 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005181 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005182
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005183 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005184 {
5185 if (str_l < str_m)
5186 {
5187 size_t avail = str_m - str_l;
5188
5189 mch_memmove(str + str_l,
5190 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005191 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005192 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005193 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005194 }
5195 }
5196
5197 /* insert right padding */
5198 if (justify_left)
5199 {
5200 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005201 int pn = (int)(min_field_width
5202 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005203
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005204 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005205 {
5206 if (str_l < str_m)
5207 {
5208 size_t avail = str_m - str_l;
5209
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005210 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005211 (size_t)pn > avail ? avail
5212 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005213 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005214 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005215 }
5216 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005217 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005218 }
5219 }
5220
5221 if (str_m > 0)
5222 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005223 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005224 * overwriting the last character (shouldn't happen, but just in case)
5225 * */
5226 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5227 }
5228
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005229 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005230 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005232 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005233 * character), that is, the number of characters that would have been
5234 * written to the buffer if it were large enough. */
5235 return (int)str_l;
5236}
5237
5238#endif /* PROTO */