blob: 448173c0b97159fc36df2cf08061115c706649c2 [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);
31static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static int confirm_msg_used = FALSE; /* displaying confirm_msg */
36static char_u *confirm_msg = NULL; /* ":confirm" message */
37static char_u *confirm_msg_tail; /* tail of confirm_msg */
38#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010039#ifdef FEAT_JOB_CHANNEL
40static int emsg_to_channel_log = FALSE;
41#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43struct msg_hist
44{
45 struct msg_hist *next;
46 char_u *msg;
47 int attr;
48};
49
50static struct msg_hist *first_msg_hist = NULL;
51static struct msg_hist *last_msg_hist = NULL;
52static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020054static FILE *verbose_fd = NULL;
55static int verbose_did_open = FALSE;
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057/*
58 * When writing messages to the screen, there are many different situations.
59 * A number of variables is used to remember the current state:
60 * msg_didany TRUE when messages were written since the last time the
61 * user reacted to a prompt.
62 * Reset: After hitting a key for the hit-return prompt,
63 * hitting <CR> for the command line or input().
64 * Set: When any message is written to the screen.
65 * msg_didout TRUE when something was written to the current line.
66 * Reset: When advancing to the next line, when the current
67 * text can be overwritten.
68 * Set: When any message is written to the screen.
69 * msg_nowait No extra delay for the last drawn message.
70 * Used in normal_cmd() before the mode message is drawn.
71 * emsg_on_display There was an error message recently. Indicates that there
72 * should be a delay before redrawing.
73 * msg_scroll The next message should not overwrite the current one.
74 * msg_scrolled How many lines the screen has been scrolled (because of
75 * messages). Used in update_screen() to scroll the screen
76 * back. Incremented each time the screen scrolls a line.
77 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
78 * writes something without scrolling should not make
79 * need_wait_return to be set. This is a hack to make ":ts"
80 * work without an extra prompt.
81 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010082 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 * need_wait_return TRUE when the hit-return prompt is needed.
84 * Reset: After giving the hit-return prompt, when the user
85 * has answered some other prompt.
86 * Set: When the ruler or typeahead display is overwritten,
87 * scrolling the screen for some message.
88 * keep_msg Message to be displayed after redrawing the screen, in
89 * main_loop().
90 * This is an allocated string or NULL when not used.
91 */
92
93/*
94 * msg(s) - displays the string 's' on the status line
95 * When terminal not initialized (yet) mch_errmsg(..) is used.
96 * return TRUE if wait_return not called
97 */
98 int
Bram Moolenaar32526b32019-01-19 17:43:09 +010099msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100{
101 return msg_attr_keep(s, 0, FALSE);
102}
103
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000104#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000105 || defined(FEAT_GUI_GTK) || defined(PROTO)
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}
120#endif
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100123msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 return msg_attr_keep(s, attr, FALSE);
126}
127
128 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100130 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100131 int attr,
132 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
134 static int entered = 0;
135 int retval;
136 char_u *buf = NULL;
137
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200138 /* Skip messages not matching ":filter pattern".
139 * Don't filter when there is an error. */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100140 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200141 return TRUE;
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#ifdef FEAT_EVAL
144 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100145 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#endif
147
148 /*
149 * It is possible that displaying a messages causes a problem (e.g.,
150 * when redrawing the window), which causes another message, etc.. To
151 * break this loop, limit the recursiveness to 3 levels.
152 */
153 if (entered >= 3)
154 return TRUE;
155 ++entered;
156
157 /* Add message to history (unless it's a repeated kept message or a
158 * truncated message) */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100159 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 || (*s != '<'
161 && last_msg_hist != NULL
162 && last_msg_hist->msg != NULL
163 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100164 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar958dc692016-12-01 15:34:12 +0100166#ifdef FEAT_JOB_CHANNEL
167 if (emsg_to_channel_log)
Bram Moolenaar958dc692016-12-01 15:34:12 +0100168 /* Write message in the channel log. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200169 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 /* When displaying keep_msg, don't let msg_start() free it, caller must do
173 * that. */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100174 if ((char_u *)s == keep_msg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 keep_msg = NULL;
176
177 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000178 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100179 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100181 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar32526b32019-01-19 17:43:09 +0100183 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 msg_clr_eos();
185 retval = msg_end();
186
Bram Moolenaar32526b32019-01-19 17:43:09 +0100187 if (keep && retval && vim_strsize((char_u *)s)
188 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
189 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
191 vim_free(buf);
192 --entered;
193 return retval;
194}
195
196/*
197 * Truncate a string such that it can be printed without causing a scroll.
198 * Returns an allocated string or NULL when no truncating is done.
199 */
200 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100201msg_strtrunc(
202 char_u *s,
203 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
205 char_u *buf = NULL;
206 int len;
207 int room;
208
209 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000210 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
211 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {
213 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000214 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000215 /* Use all the columns. */
216 room = (int)(Rows - msg_row) * Columns - 1;
217 else
218 /* Use up to 'showcmd' column. */
219 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 if (len > room && room > 0)
221 {
222#ifdef FEAT_MBYTE
223 if (enc_utf8)
224 /* may have up to 18 bytes per cell (6 per char, up to two
225 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 else if (enc_dbcs == DBCS_JPNU)
228 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 else
231#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 len = room + 2;
233 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100235 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237 }
238 return buf;
239}
240
241/*
242 * Truncate a string "s" to "buf" with cell width "room".
243 * "s" and "buf" may be equal.
244 */
245 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100246trunc_string(
247 char_u *s,
248 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200249 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100250 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200252 size_t room = room_in - 3; /* "..." takes 3 chars */
253 size_t half;
254 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 int e;
256 int i;
257 int n;
258
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200259 if (room_in < 3)
260 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100264 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {
266 if (s[e] == NUL)
267 {
268 /* text fits without truncating! */
269 buf[e] = NUL;
270 return;
271 }
272 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200273 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 break;
275 len += n;
276 buf[e] = s[e];
277#ifdef FEAT_MBYTE
278 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000279 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100281 if (++e == buflen)
282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 buf[e] = s[e];
284 }
285#endif
286 }
287
288 /* Last part: End of the string. */
289 i = e;
290#ifdef FEAT_MBYTE
291 if (enc_dbcs != 0)
292 {
293 /* For DBCS going backwards in a string is slow, but
294 * computing the cell width isn't too slow: go forward
295 * until the rest fits. */
296 n = vim_strsize(s + i);
297 while (len + n > room)
298 {
299 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000300 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 }
302 }
303 else if (enc_utf8)
304 {
305 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000306 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 for (;;)
308 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000309 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200310 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200311 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200313 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 break;
315 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200316 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 }
318 }
319 else
320#endif
321 {
322 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
323 len += n;
324 }
325
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200326
327 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100328 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200329 /* text fits without truncating */
330 if (s != buf)
331 {
332 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200333 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200334 len = buflen - 1;
335 len = len - e + 1;
336 if (len < 1)
337 buf[e - 1] = NUL;
338 else
339 mch_memmove(buf + e, s + e, len);
340 }
341 }
342 else if (e + 3 < buflen)
343 {
344 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200346 len = STRLEN(s + i) + 1;
347 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100348 len = buflen - e - 3 - 1;
349 mch_memmove(buf + e + 3, s + i, len);
350 buf[e + 3 + len - 1] = NUL;
351 }
352 else
353 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200354 /* can't fit in the "...", just truncate it */
355 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357}
358
359/*
360 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 * shorter than IOSIZE!!!
363 */
364#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100366int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000367
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100369# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100371# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100372smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
374 va_list arglist;
375
376 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100379 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380}
381
382 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100383# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100385# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100386smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
388 va_list arglist;
389
390 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100391 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100393 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394}
395
Bram Moolenaare0429682018-07-01 16:44:03 +0200396 int
397# ifdef __BORLANDC__
398_RTLENTRYF
399# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100400smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200401{
402 va_list arglist;
403
404 va_start(arglist, s);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100405 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
Bram Moolenaare0429682018-07-01 16:44:03 +0200406 va_end(arglist);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100407 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200408}
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#endif
411
412/*
413 * Remember the last sourcing name/lnum used in an error message, so that it
414 * isn't printed each time when it didn't change.
415 */
416static int last_sourcing_lnum = 0;
417static char_u *last_sourcing_name = NULL;
418
419/*
420 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
421 * for the next error message;
422 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000423 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100424reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100426 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 last_sourcing_lnum = 0;
428}
429
430/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000431 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
432 */
433 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100434other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000435{
436 if (sourcing_name != NULL)
437 {
438 if (last_sourcing_name != NULL)
439 return STRCMP(sourcing_name, last_sourcing_name) != 0;
440 return TRUE;
441 }
442 return FALSE;
443}
444
445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 * Get the message about the source, as used for an error message.
447 * Returns an allocated string with room for one more character.
448 * Returns NULL when no message is to be given.
449 */
450 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100451get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 char_u *Buf, *p;
454
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000455 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
457 p = (char_u *)_("Error detected while processing %s:");
458 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
459 if (Buf != NULL)
460 sprintf((char *)Buf, (char *)p, sourcing_name);
461 return Buf;
462 }
463 return NULL;
464}
465
466/*
467 * Get the message about the source lnum, as used for an error message.
468 * Returns an allocated string with room for one more character.
469 * Returns NULL when no message is to be given.
470 */
471 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100472get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 char_u *Buf, *p;
475
476 /* lnum is 0 when executing a command from the command line
477 * argument, we don't want a line number then */
478 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000479 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 && sourcing_lnum != 0)
481 {
482 p = (char_u *)_("line %4ld:");
483 Buf = alloc((unsigned)(STRLEN(p) + 20));
484 if (Buf != NULL)
485 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
486 return Buf;
487 }
488 return NULL;
489}
490
491/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000492 * Display name and line number for the source of an error.
493 * Remember the file name and line number, so that for the next error the info
494 * is only displayed if it changed.
495 */
496 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100497msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000498{
499 char_u *p;
500
501 ++no_wait_return;
502 p = get_emsg_source();
503 if (p != NULL)
504 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100505 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000506 vim_free(p);
507 }
508 p = get_emsg_lnum();
509 if (p != NULL)
510 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100511 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000512 vim_free(p);
513 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
514 }
515
516 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000517 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000518 {
519 vim_free(last_sourcing_name);
520 if (sourcing_name == NULL)
521 last_sourcing_name = NULL;
522 else
523 last_sourcing_name = vim_strsave(sourcing_name);
524 }
525 --no_wait_return;
526}
527
528/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000529 * Return TRUE if not giving error messages right now:
530 * If "emsg_off" is set: no error messages at the moment.
531 * If "msg" is in 'debug': do error message but without side effects.
532 * If "emsg_skip" is set: never do error messages.
533 */
534 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100535emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000536{
537 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
538 && vim_strchr(p_debug, 't') == NULL)
539#ifdef FEAT_EVAL
540 || emsg_skip > 0
541#endif
542 )
543 return TRUE;
544 return FALSE;
545}
546
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100547#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100548static garray_T ignore_error_list = GA_EMPTY;
549
550 void
551ignore_error_for_testing(char_u *error)
552{
553 if (ignore_error_list.ga_itemsize == 0)
554 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
555
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100556 if (STRCMP("RESET", error) == 0)
557 ga_clear_strings(&ignore_error_list);
558 else
559 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100560}
561
562 static int
563ignore_error(char_u *msg)
564{
565 int i;
566
567 for (i = 0; i < ignore_error_list.ga_len; ++i)
568 if (strstr((char *)msg,
569 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
570 return TRUE;
571 return FALSE;
572}
573#endif
574
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200575#if !defined(HAVE_STRERROR) || defined(PROTO)
576/*
577 * Replacement for perror() that behaves more or less like emsg() was called.
578 * v:errmsg will be set and called_emsg will be set.
579 */
580 void
581do_perror(char *msg)
582{
583 perror(msg);
584 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100585 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200586 --emsg_silent;
587}
588#endif
589
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000590/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100591 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 *
593 * Rings the bell, if appropriate, and calls message() to do the real work
594 * When terminal not initialized (yet) mch_errmsg(..) is used.
595 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100596 * Return TRUE if wait_return not called.
597 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100599 static int
600emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100604 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605#ifdef FEAT_EVAL
606 int ignore = FALSE;
607 int severe;
608#endif
609
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100610#ifdef FEAT_EVAL
611 /* When testing some errors are turned into a normal message. */
612 if (ignore_error(s))
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +0100613 /* don't call msg() if it results in a dialog */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100614 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100615#endif
616
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 called_emsg = TRUE;
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#ifdef FEAT_EVAL
Bram Moolenaare723c422017-09-06 23:40:10 +0200620 /* If "emsg_severe" is TRUE: When an error exception is to be thrown,
621 * prefer this message over previous messages for the same command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 severe = emsg_severe;
623 emsg_severe = FALSE;
624#endif
625
Bram Moolenaar57657d82006-04-21 22:12:41 +0000626 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {
628#ifdef FEAT_EVAL
629 /*
630 * Cause a throw of an error exception if appropriate. Don't display
631 * the error message in this case. (If no matching catch clause will
632 * be found, the message will be displayed later on.) "ignore" is set
633 * when the message should be ignored completely (used for the
634 * interrupt message).
635 */
636 if (cause_errthrow(s, severe, &ignore) == TRUE)
637 {
638 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100639 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 return TRUE;
641 }
642
643 /* set "v:errmsg", also when using ":silent! cmd" */
644 set_vim_var_string(VV_ERRMSG, s, -1);
645#endif
646
647 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000648 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * But do write it to the redirection file.
650 */
651 if (emsg_silent != 0)
652 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200653 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200655 msg_start();
656 p = get_emsg_source();
657 if (p != NULL)
658 {
659 STRCAT(p, "\n");
660 redir_write(p, -1);
661 vim_free(p);
662 }
663 p = get_emsg_lnum();
664 if (p != NULL)
665 {
666 STRCAT(p, "\n");
667 redir_write(p, -1);
668 vim_free(p);
669 }
670 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100672#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200673 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 return TRUE;
676 }
677
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100678 ex_exitval = 1;
679
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200680 /* Reset msg_silent, an error causes messages to be switched back on.
681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 msg_silent = 0;
683 cmd_silent = FALSE;
684
685 if (global_busy) /* break :global command */
686 ++global_busy;
687
688 if (p_eb)
689 beep_flush(); /* also includes flush_buffers() */
690 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200691 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100692 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200693#ifdef FEAT_EVAL
694 did_uncaught_emsg = TRUE;
695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697
698 emsg_on_display = TRUE; /* remember there is an error message */
699 ++msg_scroll; /* don't overwrite a previous message */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100700 attr = HL_ATTR(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000701 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 need_wait_return = TRUE; /* needed in case emsg() is called after
703 * wait_return has reset need_wait_return
704 * and a redraw is expected because
705 * msg_scrolled is non-zero */
706
Bram Moolenaar958dc692016-12-01 15:34:12 +0100707#ifdef FEAT_JOB_CHANNEL
708 emsg_to_channel_log = TRUE;
709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 /*
711 * Display name and line number for the source of the error.
712 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000713 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
715 /*
716 * Display the error message itself.
717 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000718 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100719 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100720
721#ifdef FEAT_JOB_CHANNEL
722 emsg_to_channel_log = FALSE;
723#endif
724 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100728 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
730 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100733 /* Skip this if not giving error messages at the moment. */
734 if (!emsg_not_now())
735 return emsg_core((char_u *)s);
736 return TRUE; /* no error messages at the moment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
Bram Moolenaar95f09602016-11-10 20:01:45 +0100739/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100740 * Print an error message with format string and variable arguments.
741 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100742 */
743 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100745{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100746 /* Skip this if not giving error messages at the moment. */
747 if (!emsg_not_now())
748 {
749 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100750
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100751 va_start(ap, s);
752 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
753 va_end(ap);
754 return emsg_core(IObuff);
755 }
756 return TRUE; /* no error messages at the moment */
Bram Moolenaar95f09602016-11-10 20:01:45 +0100757}
758
759/*
760 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
761 * defined. It is used for internal errors only, so that they can be
762 * detected when fuzzing vim.
763 */
764 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100765iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100766{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100767 if (!emsg_not_now())
768 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100769#ifdef ABORT_ON_INTERNAL_ERROR
770 abort();
771#endif
772}
773
774/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100776 * defined. It is used for internal errors only, so that they can be
777 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100779 */
780 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100781siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100782{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100783 if (!emsg_not_now())
784 {
785 va_list ap;
786
787 va_start(ap, s);
788 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
789 va_end(ap);
790 emsg_core(IObuff);
791 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100792#ifdef ABORT_ON_INTERNAL_ERROR
793 abort();
794#endif
795}
796
797/*
798 * Give an "Internal error" message.
799 */
800 void
801internal_error(char *where)
802{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100804}
805
Bram Moolenaarea424162005-06-16 21:51:00 +0000806/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaardf177f62005-02-22 08:39:57 +0000808 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100809emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000810{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100811 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000812}
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814/*
815 * Like msg(), but truncate to a single line if p_shm contains 't', or when
816 * "force" is TRUE. This truncates in another way as for normal messages.
817 * Careful: The string may be changed by msg_may_trunc()!
818 * Returns a pointer to the printed message, if wait_return() not called.
819 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100820 char *
821msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100824 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /* Add message to history before truncating */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100827 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
Bram Moolenaar32526b32019-01-19 17:43:09 +0100829 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100832 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 msg_hist_off = FALSE;
834
835 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100836 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 return NULL;
838}
839
840/*
841 * Check if message "s" should be truncated at the start (for filenames).
842 * Return a pointer to where the truncated message starts.
843 * Note: May change the message by replacing a character with '<'.
844 */
845 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100846msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848 int n;
849 int room;
850
851 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
852 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
853 && (n = (int)STRLEN(s) - room) > 0)
854 {
855#ifdef FEAT_MBYTE
856 if (has_mbyte)
857 {
858 int size = vim_strsize(s);
859
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000860 /* There may be room anyway when there are multibyte chars. */
861 if (size <= room)
862 return s;
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 for (n = 0; size >= room; )
865 {
866 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000867 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 --n;
870 }
871#endif
872 s += n;
873 *s = '<';
874 }
875 return s;
876}
877
878 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100879add_msg_hist(
880 char_u *s,
881 int len, /* -1 for undetermined length */
882 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
884 struct msg_hist *p;
885
886 if (msg_hist_off || msg_silent != 0)
887 return;
888
889 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000890 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000891 (void)delete_first_msg();
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 /* allocate an entry and add the message at the end of the history */
894 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
895 if (p != NULL)
896 {
897 if (len < 0)
898 len = (int)STRLEN(s);
899 /* remove leading and trailing newlines */
900 while (len > 0 && *s == '\n')
901 {
902 ++s;
903 --len;
904 }
905 while (len > 0 && s[len - 1] == '\n')
906 --len;
907 p->msg = vim_strnsave(s, len);
908 p->next = NULL;
909 p->attr = attr;
910 if (last_msg_hist != NULL)
911 last_msg_hist->next = p;
912 last_msg_hist = p;
913 if (first_msg_hist == NULL)
914 first_msg_hist = last_msg_hist;
915 ++msg_hist_len;
916 }
917}
918
919/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000920 * Delete the first (oldest) message from the history.
921 * Returns FAIL if there are no messages.
922 */
923 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100924delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000925{
926 struct msg_hist *p;
927
928 if (msg_hist_len <= 0)
929 return FAIL;
930 p = first_msg_hist;
931 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000932 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200933 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000934 vim_free(p->msg);
935 vim_free(p);
936 --msg_hist_len;
937 return OK;
938}
939
940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * ":messages" command.
942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200944ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
946 struct msg_hist *p;
947 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200948 int c = 0;
949
950 if (STRCMP(eap->arg, "clear") == 0)
951 {
952 int keep = eap->addr_count == 0 ? 0 : eap->line2;
953
954 while (msg_hist_len > keep)
955 (void)delete_first_msg();
956 return;
957 }
958
959 if (*eap->arg != NUL)
960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100961 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +0200962 return;
963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
965 msg_hist_off = TRUE;
966
Bram Moolenaar451f8492016-04-14 17:16:22 +0200967 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200968 if (eap->addr_count != 0)
969 {
970 /* Count total messages */
971 for (; p != NULL && !got_int; p = p->next)
972 c++;
973
974 c -= eap->line2;
975
976 /* Skip without number of messages specified */
977 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
978 p = p->next, c--);
979 }
980
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200981 if (p == first_msg_hist)
982 {
983 s = mch_getenv((char_u *)"LANG");
984 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +0200985 // The next comment is extracted by xgettext and put in po file for
986 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100987 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +0200988 // Translator: Please replace the name and email address
989 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200990 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +0100991 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200992 }
993
Bram Moolenaar451f8492016-04-14 17:16:22 +0200994 /* Display what was not skipped. */
995 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100997 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
999 msg_hist_off = FALSE;
1000}
1001
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001002#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003/*
1004 * Call this after prompting the user. This will avoid a hit-return message
1005 * and a delay.
1006 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001007 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001008msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 need_wait_return = FALSE;
1011 emsg_on_display = FALSE;
1012 cmdline_row = msg_row;
1013 msg_col = 0;
1014 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001015 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016}
1017#endif
1018
1019/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001020 * Wait for the user to hit a key (normally Enter).
1021 * If "redraw" is TRUE, clear and redraw the screen.
1022 * If "redraw" is FALSE, just redraw the screen.
1023 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 */
1025 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001026wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 int c;
1029 int oldState;
1030 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001032 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001033 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 if (redraw == TRUE)
1036 must_redraw = CLEAR;
1037
1038 /* If using ":silent cmd", don't wait for a return. Also don't set
1039 * need_wait_return to do it later. */
1040 if (msg_silent != 0)
1041 return;
1042
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001043 /*
1044 * When inside vgetc(), we can't wait for a typed character at all.
1045 * With the global command (and some others) we only need one return at
1046 * the end. Adjust cmdline_row to avoid the next message overwriting the
1047 * last one.
1048 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001049 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001051 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (no_wait_return)
1053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (!exmode_active)
1055 cmdline_row = msg_row;
1056 return;
1057 }
1058
1059 redir_off = TRUE; /* don't redirect this message */
1060 oldState = State;
1061 if (quit_more)
1062 {
1063 c = CAR; /* just pretend CR was hit */
1064 quit_more = FALSE;
1065 got_int = FALSE;
1066 }
1067 else if (exmode_active)
1068 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001069 msg_puts(" "); /* make sure the cursor is on the right line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 c = CAR; /* no need for a return in ex mode */
1071 got_int = FALSE;
1072 }
1073 else
1074 {
1075 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1076 * just changed. */
1077 screenalloc(FALSE);
1078
1079 State = HITRETURN;
1080#ifdef FEAT_MOUSE
1081 setmouse();
1082#endif
1083#ifdef USE_ON_FLY_SCROLL
1084 dont_scroll = TRUE; /* disallow scrolling here */
1085#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001086 cmdline_row = msg_row;
1087
Bram Moolenaarec38d692013-04-24 15:12:32 +02001088 /* Avoid the sequence that the user types ":" at the hit-return prompt
1089 * to start an Ex command, but the file-changed dialog gets in the
1090 * way. */
1091 if (need_check_timestamps)
1092 check_timestamps(FALSE);
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 hit_return_msg();
1095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 do
1097 {
1098 /* Remember "got_int", if it is set vgetc() probably returns a
1099 * CTRL-C, but we need to loop then. */
1100 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001101
1102 /* Don't do mappings here, we put the character back in the
1103 * typeahead buffer. */
1104 ++no_mapping;
1105 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001106
1107 /* Temporarily disable Recording. If Recording is active, the
1108 * character will be recorded later, since it will be added to the
1109 * typebuf after the loop */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001110 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001111 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001112 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001113 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001115 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001117 --no_mapping;
1118 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001119 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001120 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#ifdef FEAT_CLIPBOARD
1123 /* Strange way to allow copying (yanking) a modeless selection at
1124 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1125 * Cmdline-mode and it's harmless when there is no selection. */
1126 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1127 {
1128 clip_copy_modeless_selection(TRUE);
1129 c = K_IGNORE;
1130 }
1131#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001132
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001133 /*
1134 * Allow scrolling back in the messages.
1135 * Also accept scroll-down commands when messages fill the screen,
1136 * to avoid that typing one 'j' too many makes the messages
1137 * disappear.
1138 */
1139 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001140 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001141 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1142 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001143 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001144 if (msg_scrolled > Rows)
1145 /* scroll back to show older messages */
1146 do_more_prompt(c);
1147 else
1148 {
1149 msg_didout = FALSE;
1150 c = K_IGNORE;
1151 msg_col =
1152#ifdef FEAT_RIGHTLEFT
1153 cmdmsg_rl ? Columns - 1 :
1154#endif
1155 0;
1156 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001157 if (quit_more)
1158 {
1159 c = CAR; /* just pretend CR was hit */
1160 quit_more = FALSE;
1161 got_int = FALSE;
1162 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001163 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001164 {
1165 c = K_IGNORE;
1166 hit_return_msg();
1167 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001168 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001169 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001170 && (c == 'j' || c == 'd' || c == 'f'
1171 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001172 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 } while ((had_got_int && c == Ctrl_C)
1175 || c == K_IGNORE
1176#ifdef FEAT_GUI
1177 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1178#endif
1179#ifdef FEAT_MOUSE
1180 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1181 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1182 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001183 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001185 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 || (!mouse_has(MOUSE_RETURN)
1187 && mouse_row < msg_row
1188 && (c == K_LEFTMOUSE
1189 || c == K_MIDDLEMOUSE
1190 || c == K_RIGHTMOUSE
1191 || c == K_X1MOUSE
1192 || c == K_X2MOUSE))
1193#endif
1194 );
1195 ui_breakcheck();
1196#ifdef FEAT_MOUSE
1197 /*
1198 * Avoid that the mouse-up event causes visual mode to start.
1199 */
1200 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1201 || c == K_X1MOUSE || c == K_X2MOUSE)
1202 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1203 else
1204#endif
1205 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1206 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001207 /* Put the character back in the typeahead buffer. Don't use the
1208 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001209 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001211 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
1214 redir_off = FALSE;
1215
1216 /*
1217 * If the user hits ':', '?' or '/' we get a command line from the next
1218 * line.
1219 */
1220 if (c == ':' || c == '?' || c == '/')
1221 {
1222 if (!exmode_active)
1223 cmdline_row = msg_row;
1224 skip_redraw = TRUE; /* skip redraw once */
1225 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001226#ifdef FEAT_TERMINAL
1227 skip_term_loop = TRUE;
1228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
1231 /*
1232 * If the window size changed set_shellsize() will redraw the screen.
1233 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1234 * typed.
1235 */
1236 tmpState = State;
1237 State = oldState; /* restore State before set_shellsize */
1238#ifdef FEAT_MOUSE
1239 setmouse();
1240#endif
1241 msg_check();
1242
1243#if defined(UNIX) || defined(VMS)
1244 /*
1245 * When switching screens, we need to output an extra newline on exit.
1246 */
1247 if (swapping_screen() && !termcap_active)
1248 newline_on_exit = TRUE;
1249#endif
1250
1251 need_wait_return = FALSE;
1252 did_wait_return = TRUE;
1253 emsg_on_display = FALSE; /* can delete error message now */
1254 lines_left = -1; /* reset lines_left at next msg_start() */
1255 reset_last_sourcing();
1256 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1257 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001258 VIM_CLEAR(keep_msg); /* don't redisplay message, it's too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
1260 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1261 {
1262 starttermcap(); /* start termcap before redrawing */
1263 shell_resized();
1264 }
1265 else if (!skip_redraw
1266 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1267 {
1268 starttermcap(); /* start termcap before redrawing */
1269 redraw_later(VALID);
1270 }
1271}
1272
1273/*
1274 * Write the hit-return prompt.
1275 */
1276 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001277hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001279 int save_p_more = p_more;
1280
1281 p_more = FALSE; /* don't want see this message when scrolling back */
1282 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 msg_putchar('\n');
1284 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001285 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
Bram Moolenaar32526b32019-01-19 17:43:09 +01001287 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (!msg_use_printf())
1289 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001290 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291}
1292
1293/*
1294 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1295 */
1296 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001297set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 vim_free(keep_msg);
1300 if (s != NULL && msg_silent == 0)
1301 keep_msg = vim_strsave(s);
1302 else
1303 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001304 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001305 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306}
1307
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001308#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1309/*
1310 * If there currently is a message being displayed, set "keep_msg" to it, so
1311 * that it will be displayed again after redraw.
1312 */
1313 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001314set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001315{
1316 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1317 && (State & NORMAL))
1318 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1319}
1320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322/*
1323 * Prepare for outputting characters in the command line.
1324 */
1325 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001326msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 int did_return = FALSE;
1329
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001330 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001331 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001332
1333#ifdef FEAT_EVAL
1334 if (need_clr_eos)
1335 {
1336 /* Halfway an ":echo" command and getting an (error) message: clear
1337 * any text from the command. */
1338 need_clr_eos = FALSE;
1339 msg_clr_eos();
1340 }
1341#endif
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (!msg_scroll && full_screen) /* overwrite last message */
1344 {
1345 msg_row = cmdline_row;
1346 msg_col =
1347#ifdef FEAT_RIGHTLEFT
1348 cmdmsg_rl ? Columns - 1 :
1349#endif
1350 0;
1351 }
1352 else if (msg_didout) /* start message on next line */
1353 {
1354 msg_putchar('\n');
1355 did_return = TRUE;
1356 if (exmode_active != EXMODE_NORMAL)
1357 cmdline_row = msg_row;
1358 }
1359 if (!msg_didany || lines_left < 0)
1360 msg_starthere();
1361 if (msg_silent == 0)
1362 {
1363 msg_didout = FALSE; /* no output on current line yet */
1364 cursor_off();
1365 }
1366
1367 /* when redirecting, may need to start a new line. */
1368 if (!did_return)
1369 redir_write((char_u *)"\n", -1);
1370}
1371
1372/*
1373 * Note that the current msg position is where messages start.
1374 */
1375 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001376msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 lines_left = cmdline_row;
1379 msg_didany = FALSE;
1380}
1381
1382 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001383msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 msg_putchar_attr(c, 0);
1386}
1387
1388 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001389msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391#ifdef FEAT_MBYTE
Bram Moolenaar32526b32019-01-19 17:43:09 +01001392 char buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001394 char buf[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#endif
1396
1397 if (IS_SPECIAL(c))
1398 {
1399 buf[0] = K_SPECIAL;
1400 buf[1] = K_SECOND(c);
1401 buf[2] = K_THIRD(c);
1402 buf[3] = NUL;
1403 }
1404 else
1405 {
1406#ifdef FEAT_MBYTE
Bram Moolenaar32526b32019-01-19 17:43:09 +01001407 buf[(*mb_char2bytes)(c, (char_u *)buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408#else
1409 buf[0] = c;
1410 buf[1] = NUL;
1411#endif
1412 }
1413 msg_puts_attr(buf, attr);
1414}
1415
1416 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001417msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001419 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaar32526b32019-01-19 17:43:09 +01001421 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 msg_puts(buf);
1423}
1424
1425 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001426msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 msg_home_replace_attr(fname, 0);
1429}
1430
1431#if defined(FEAT_FIND_ID) || defined(PROTO)
1432 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001433msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001435 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436}
1437#endif
1438
1439 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001440msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 char_u *name;
1443
1444 name = home_replace_save(NULL, fname);
1445 if (name != NULL)
1446 msg_outtrans_attr(name, attr);
1447 vim_free(name);
1448}
1449
1450/*
1451 * Output 'len' characters in 'str' (including NULs) with translation
1452 * if 'len' is -1, output upto a NUL character.
1453 * Use attributes 'attr'.
1454 * Return the number of characters it takes on the screen.
1455 */
1456 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001457msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 return msg_outtrans_attr(str, 0);
1460}
1461
1462 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001463msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1466}
1467
1468 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001469msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 return msg_outtrans_len_attr(str, len, 0);
1472}
1473
1474/*
1475 * Output one character at "p". Return pointer to the next character.
1476 * Handles multi-byte characters.
1477 */
1478 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001479msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481#ifdef FEAT_MBYTE
1482 int l;
1483
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001484 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 msg_outtrans_len_attr(p, l, attr);
1487 return p + l;
1488 }
1489#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01001490 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 return p + 1;
1492}
1493
1494 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001495msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 int retval = 0;
1498 char_u *str = msgstr;
1499 char_u *plain_start = msgstr;
1500 char_u *s;
1501#ifdef FEAT_MBYTE
1502 int mb_l;
1503 int c;
1504#endif
1505
1506 /* if MSG_HIST flag set, add message to history */
1507 if (attr & MSG_HIST)
1508 {
1509 add_msg_hist(str, len, attr);
1510 attr &= ~MSG_HIST;
1511 }
1512
1513#ifdef FEAT_MBYTE
1514 /* If the string starts with a composing character first draw a space on
1515 * which the composing char can be drawn. */
1516 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001517 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518#endif
1519
1520 /*
1521 * Go over the string. Special characters are translated and printed.
1522 * Normal characters are printed several at a time.
1523 */
1524 while (--len >= 0)
1525 {
1526#ifdef FEAT_MBYTE
1527 if (enc_utf8)
1528 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001529 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001531 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 else
1533 mb_l = 1;
1534 if (has_mbyte && mb_l > 1)
1535 {
1536 c = (*mb_ptr2char)(str);
1537 if (vim_isprintc(c))
1538 /* printable multi-byte char: count the cells. */
1539 retval += (*mb_ptr2cells)(str);
1540 else
1541 {
1542 /* unprintable multi-byte char: print the printable chars so
1543 * far and the translation of the unprintable char. */
1544 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001545 msg_puts_attr_len((char *)plain_start,
1546 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001548 msg_puts_attr((char *)transchar(c),
1549 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 retval += char2cells(c);
1551 }
1552 len -= mb_l - 1;
1553 str += mb_l;
1554 }
1555 else
1556#endif
1557 {
1558 s = transchar_byte(*str);
1559 if (s[1] != NUL)
1560 {
1561 /* unprintable char: print the printable chars so far and the
1562 * translation of the unprintable char. */
1563 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001564 msg_puts_attr_len((char *)plain_start,
1565 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001567 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001568 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001570 else
1571 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 ++str;
1573 }
1574 }
1575
1576 if (str > plain_start)
1577 /* print the printable chars at the end */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001578 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 return retval;
1581}
1582
1583#if defined(FEAT_QUICKFIX) || defined(PROTO)
1584 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001585msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 int i;
1588 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1589
1590 arg = skipwhite(arg);
1591 for (i = 5; *arg && i >= 0; --i)
1592 if (*arg++ != str[i])
1593 break;
1594 if (i < 0)
1595 {
1596 msg_putchar('\n');
1597 for (i = 0; rs[i]; ++i)
1598 msg_putchar(rs[i] - 3);
1599 }
1600}
1601#endif
1602
1603/*
1604 * Output the string 'str' upto a NUL character.
1605 * Return the number of characters it takes on the screen.
1606 *
1607 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1608 * following character and shown as <F1>, <S-Up> etc. Any other character
1609 * which is not printable shown in <> form.
1610 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1611 * If a character is displayed in one of these special ways, is also
1612 * highlighted (its highlight name is '8' in the p_hl variable).
1613 * Otherwise characters are not highlighted.
1614 * This function is used to show mappings, where we want to see how to type
1615 * the character/string -- webb
1616 */
1617 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001618msg_outtrans_special(
1619 char_u *strstart,
1620 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 char_u *str = strstart;
1623 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001624 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 int attr;
1626 int len;
1627
Bram Moolenaar8820b482017-03-16 17:23:31 +01001628 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 while (*str != NUL)
1630 {
1631 /* Leading and trailing spaces need to be displayed in <> form. */
1632 if ((str == strstart || str[1] == NUL) && *str == ' ')
1633 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001634 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 ++str;
1636 }
1637 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001638 text = (char *)str2special(&str, from);
1639 len = vim_strsize((char_u *)text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 /* Highlight special keys */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001641 msg_puts_attr(text, len > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#ifdef FEAT_MBYTE
Bram Moolenaar32526b32019-01-19 17:43:09 +01001643 && (*mb_ptr2len)((char_u *)text) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645 ? attr : 0);
1646 retval += len;
1647 }
1648 return retval;
1649}
1650
Bram Moolenaarbd743252010-10-20 21:23:33 +02001651#if defined(FEAT_EVAL) || defined(PROTO)
1652/*
1653 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1654 * strings, in an allocated string.
1655 */
1656 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001657str2special_save(
1658 char_u *str,
1659 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001660{
1661 garray_T ga;
1662 char_u *p = str;
1663
1664 ga_init2(&ga, 1, 40);
1665 while (*p != NUL)
1666 ga_concat(&ga, str2special(&p, is_lhs));
1667 ga_append(&ga, NUL);
1668 return (char_u *)ga.ga_data;
1669}
1670#endif
1671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672/*
1673 * Return the printable string for the key codes at "*sp".
1674 * Used for translating the lhs or rhs of a mapping to printable chars.
1675 * Advances "sp" to the next code.
1676 */
1677 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001678str2special(
1679 char_u **sp,
1680 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 int c;
1683 static char_u buf[7];
1684 char_u *str = *sp;
1685 int modifiers = 0;
1686 int special = FALSE;
1687
1688#ifdef FEAT_MBYTE
1689 if (has_mbyte)
1690 {
1691 char_u *p;
1692
1693 /* Try to un-escape a multi-byte character. Return the un-escaped
1694 * string if it is a multi-byte character. */
1695 p = mb_unescape(sp);
1696 if (p != NULL)
1697 return p;
1698 }
1699#endif
1700
1701 c = *str;
1702 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1703 {
1704 if (str[1] == KS_MODIFIER)
1705 {
1706 modifiers = str[2];
1707 str += 3;
1708 c = *str;
1709 }
1710 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1711 {
1712 c = TO_SPECIAL(str[1], str[2]);
1713 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 if (IS_SPECIAL(c) || modifiers) /* special key */
1716 special = TRUE;
1717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001720 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001722 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001723
1724 /* For multi-byte characters check for an illegal byte. */
1725 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1726 {
1727 transchar_nonprint(buf, c);
1728 *sp = str + 1;
1729 return buf;
1730 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001731 /* Since 'special' is TRUE the multi-byte character 'c' will be
1732 * processed by get_special_key_name() */
1733 c = (*mb_ptr2char)(str);
1734 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001738 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1741 * Use <Space> only for lhs of a mapping. */
1742 if (special || char2cells(c) > 1 || (from && c == ' '))
1743 return get_special_key_name(c, modifiers);
1744 buf[0] = c;
1745 buf[1] = NUL;
1746 return buf;
1747}
1748
1749/*
1750 * Translate a key sequence into special key names.
1751 */
1752 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001753str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 char_u *s;
1756
1757 *buf = NUL;
1758 while (*sp)
1759 {
1760 s = str2special(&sp, FALSE);
1761 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1762 STRCAT(buf, s);
1763 }
1764}
1765
1766/*
1767 * print line for :print or :list command
1768 */
1769 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001770msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 int c;
1773 int col = 0;
1774 int n_extra = 0;
1775 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001776 int c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 char_u *p_extra = NULL; /* init to make SASC shut up */
1778 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001779 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 char_u *trail = NULL;
1781#ifdef FEAT_MBYTE
1782 int l;
1783 char_u buf[MB_MAXBYTES + 1];
1784#endif
1785
Bram Moolenaardf177f62005-02-22 08:39:57 +00001786 if (curwin->w_p_list)
1787 list = TRUE;
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001790 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001793 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 --trail;
1795 }
1796
1797 /* output a space for an empty line, otherwise the line will be
1798 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001799 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 msg_putchar(' ');
1801
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001802 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001804 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
1806 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001807 if (n_extra == 0 && c_final)
1808 c = c_final;
1809 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 c = c_extra;
1811 else
1812 c = *p_extra++;
1813 }
1814#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001815 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
1817 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001818 if (lcs_nbsp != NUL && list
1819 && (mb_ptr2char(s) == 160
1820 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001821 {
1822 mb_char2bytes(lcs_nbsp, buf);
1823 buf[(*mb_ptr2len)(buf)] = NUL;
1824 }
1825 else
1826 {
1827 mch_memmove(buf, s, (size_t)l);
1828 buf[l] = NUL;
1829 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001830 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 s += l;
1832 continue;
1833 }
1834#endif
1835 else
1836 {
1837 attr = 0;
1838 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001839 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001842#ifdef FEAT_VARTABS
1843 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1844 curbuf->b_p_vts_array) - 1;
1845#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001847#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001848 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 c = ' ';
1851 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001852 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854 else
1855 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001856 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001858 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001859 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001862 else if (c == 160 && list && lcs_nbsp != NUL)
1863 {
1864 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001865 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001866 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001867 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 p_extra = (char_u *)"";
1870 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001871 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 n_extra = 1;
1873 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001874 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 --s;
1876 }
1877 else if (c != NUL && (n = byte2cells(c)) > 1)
1878 {
1879 n_extra = n - 1;
1880 p_extra = transchar_byte(c);
1881 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001882 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001884 /* Use special coloring to be able to distinguish <hex> from
1885 * the same in plain text. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001886 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 else if (c == ' ' && trail != NULL && s > trail)
1889 {
1890 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001891 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001893 else if (c == ' ' && list && lcs_space != NUL)
1894 {
1895 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001896 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899
1900 if (c == NUL)
1901 break;
1902
1903 msg_putchar_attr(c, attr);
1904 col++;
1905 }
1906 msg_clr_eos();
1907}
1908
1909#ifdef FEAT_MBYTE
1910/*
1911 * Use screen_puts() to output one multi-byte character.
1912 * Return the pointer "s" advanced to the next character.
1913 */
1914 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001915screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 int cw;
1918
1919 msg_didout = TRUE; /* remember that line is not empty */
1920 cw = (*mb_ptr2cells)(s);
1921 if (cw > 1 && (
1922#ifdef FEAT_RIGHTLEFT
1923 cmdmsg_rl ? msg_col <= 1 :
1924#endif
1925 msg_col == Columns - 1))
1926 {
1927 /* Doesn't fit, print a highlighted '>' to fill it up. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001928 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return s;
1930 }
1931
1932 screen_puts_len(s, l, msg_row, msg_col, attr);
1933#ifdef FEAT_RIGHTLEFT
1934 if (cmdmsg_rl)
1935 {
1936 msg_col -= cw;
1937 if (msg_col == 0)
1938 {
1939 msg_col = Columns;
1940 ++msg_row;
1941 }
1942 }
1943 else
1944#endif
1945 {
1946 msg_col += cw;
1947 if (msg_col >= Columns)
1948 {
1949 msg_col = 0;
1950 ++msg_row;
1951 }
1952 }
1953 return s + l;
1954}
1955#endif
1956
1957/*
1958 * Output a string to the screen at position msg_row, msg_col.
1959 * Update msg_row and msg_col for the next message.
1960 */
1961 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001962msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001964 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965}
1966
1967 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001968msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001970 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971}
1972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973/*
1974 * Show a message in such a way that it always fits in the line. Cut out a
1975 * part in the middle and replace it with "..." when necessary.
1976 * Does not handle multi-byte characters!
1977 */
1978 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001979msg_outtrans_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001981 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982}
1983
1984 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001985msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 int slen = len;
1988 int room;
1989
1990 room = Columns - msg_col;
1991 if (len > room && room >= 20)
1992 {
1993 slen = (room - 3) / 2;
1994 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001995 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1998}
1999
2000/*
2001 * Basic function for writing a message with highlight attributes.
2002 */
2003 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002004msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 msg_puts_attr_len(s, -1, attr);
2007}
2008
2009/*
2010 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2011 * When "maxlen" is -1 there is no maximum length.
2012 * When "maxlen" is >= 0 the message is not put in the history.
2013 */
2014 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002015msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 /*
2018 * If redirection is on, also write to the redirection file.
2019 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002020 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 /*
2023 * Don't print anything when using ":silent cmd".
2024 */
2025 if (msg_silent != 0)
2026 return;
2027
2028 /* if MSG_HIST flag set, add message to history */
2029 if ((attr & MSG_HIST) && maxlen < 0)
2030 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002031 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 attr &= ~MSG_HIST;
2033 }
2034
2035 /*
2036 * When writing something to the screen after it has scrolled, requires a
2037 * wait-return prompt later. Needed when scrolling, resetting
2038 * need_wait_return after some prompt, and then outputting something
2039 * without scrolling
2040 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002041 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 need_wait_return = TRUE;
2043 msg_didany = TRUE; /* remember that something was outputted */
2044
2045 /*
2046 * If there is no valid screen, use fprintf so we can see error messages.
2047 * If termcap is not active, we may be writing in an alternate console
2048 * window, cursor positioning may not work correctly (window size may be
2049 * different, e.g. for Win32 console) or we just don't know where the
2050 * cursor is.
2051 */
2052 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002053 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002054 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002055 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002058/*
2059 * The display part of msg_puts_attr_len().
2060 * May be called recursively to display scroll-back text.
2061 */
2062 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002063msg_puts_display(
2064 char_u *str,
2065 int maxlen,
2066 int attr,
2067 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002068{
2069 char_u *s = str;
2070 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2071 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
2072#ifdef FEAT_MBYTE
2073 int l;
2074 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002076 char_u *sb_str = str;
2077 int sb_col = msg_col;
2078 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002079 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002082 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002085 * We are at the end of the screen line when:
2086 * - When outputting a newline.
2087 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002089 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090#ifdef FEAT_RIGHTLEFT
2091 cmdmsg_rl
2092 ? (
2093 msg_col <= 1
2094 || (*s == TAB && msg_col <= 7)
2095# ifdef FEAT_MBYTE
2096 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
2097# endif
2098 )
2099 :
2100#endif
2101 (msg_col + t_col >= Columns - 1
2102 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
2103# ifdef FEAT_MBYTE
2104 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2105 && msg_col + t_col >= Columns - 2)
2106# endif
2107 ))))
2108 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002109 /*
2110 * The screen is scrolled up when at the last row (some terminals
2111 * scroll automatically, some don't. To avoid problems we scroll
2112 * ourselves).
2113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002116 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002118 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (msg_no_more && lines_left == 0)
2120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002122 /* Scroll the screen up one line. */
2123 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 msg_row = Rows - 2;
2126 if (msg_col >= Columns) /* can happen after screen resize */
2127 msg_col = Columns - 1;
2128
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002129 /* Display char in last column before showing more-prompt. */
2130 if (*s >= ' '
2131#ifdef FEAT_RIGHTLEFT
2132 && !cmdmsg_rl
2133#endif
2134 )
2135 {
2136#ifdef FEAT_MBYTE
2137 if (has_mbyte)
2138 {
2139 if (enc_utf8 && maxlen >= 0)
2140 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002141 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002142 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002143 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002144 s = screen_puts_mbyte(s, l, attr);
2145 }
2146 else
2147#endif
2148 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002149 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002150 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002151 else
2152 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002153
2154 if (p_more)
2155 /* store text for scrolling back */
2156 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2157
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002158 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002159 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 redraw_cmdline = TRUE;
2161 if (cmdline_row > 0 && !exmode_active)
2162 --cmdline_row;
2163
2164 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002165 * If screen is completely filled and 'more' is set then wait
2166 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002168 if (lines_left > 0)
2169 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002170 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 && !msg_no_more && !exmode_active)
2172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002174 if (do_more_prompt(NUL))
2175 s = confirm_msg_tail;
2176#else
2177 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#endif
2179 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002180 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002182
2183 /* When we displayed a char in last column need to check if there
2184 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002185 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002186 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 }
2188
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002189 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 || msg_col + t_col >= Columns
2191#ifdef FEAT_MBYTE
2192 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2193 && msg_col + t_col >= Columns - 1)
2194#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002195 ;
2196 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2197 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002199 t_puts(&t_col, t_s, s, attr);
2200
2201 if (wrap && p_more && !recurse)
2202 /* store text for scrolling back */
2203 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205 if (*s == '\n') /* go to next line */
2206 {
2207 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002208#ifdef FEAT_RIGHTLEFT
2209 if (cmdmsg_rl)
2210 msg_col = Columns - 1;
2211 else
2212#endif
2213 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (++msg_row >= Rows) /* safety check */
2215 msg_row = Rows - 1;
2216 }
2217 else if (*s == '\r') /* go to column 0 */
2218 {
2219 msg_col = 0;
2220 }
2221 else if (*s == '\b') /* go to previous char */
2222 {
2223 if (msg_col)
2224 --msg_col;
2225 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002226 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 do
2229 msg_screen_putchar(' ', attr);
2230 while (msg_col & 7);
2231 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002232 else if (*s == BELL) /* beep (from ":sh") */
2233 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 else
2235 {
2236#ifdef FEAT_MBYTE
2237 if (has_mbyte)
2238 {
2239 cw = (*mb_ptr2cells)(s);
2240 if (enc_utf8 && maxlen >= 0)
2241 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002242 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002244 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
2246 else
2247 {
2248 cw = 1;
2249 l = 1;
2250 }
2251#endif
2252 /* When drawing from right to left or when a double-wide character
2253 * doesn't fit, draw a single character here. Otherwise collect
2254 * characters and draw them all at once later. */
2255#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2256 if (
2257# ifdef FEAT_RIGHTLEFT
2258 cmdmsg_rl
2259# ifdef FEAT_MBYTE
2260 ||
2261# endif
2262# endif
2263# ifdef FEAT_MBYTE
2264 (cw > 1 && msg_col + t_col >= Columns - 1)
2265# endif
2266 )
2267 {
2268# ifdef FEAT_MBYTE
2269 if (l > 1)
2270 s = screen_puts_mbyte(s, l, attr) - 1;
2271 else
2272# endif
2273 msg_screen_putchar(*s, attr);
2274 }
2275 else
2276#endif
2277 {
2278 /* postpone this character until later */
2279 if (t_col == 0)
2280 t_s = s;
2281#ifdef FEAT_MBYTE
2282 t_col += cw;
2283 s += l - 1;
2284#else
2285 ++t_col;
2286#endif
2287 }
2288 }
2289 ++s;
2290 }
2291
2292 /* output any postponed text */
2293 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002294 t_puts(&t_col, t_s, s, attr);
2295 if (p_more && !recurse)
2296 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 msg_check();
2299}
2300
2301/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002302 * Return TRUE when ":filter pattern" was used and "msg" does not match
2303 * "pattern".
2304 */
2305 int
2306message_filtered(char_u *msg)
2307{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002308 int match;
2309
2310 if (cmdmod.filter_regmatch.regprog == NULL)
2311 return FALSE;
2312 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2313 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002314}
2315
2316/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002317 * Scroll the screen up one line for displaying the next message line.
2318 */
2319 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002320msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002321{
2322#ifdef FEAT_GUI
2323 /* Remove the cursor before scrolling, ScreenLines[] is going
2324 * to become invalid. */
2325 if (gui.in_use)
2326 gui_undraw_cursor();
2327#endif
2328 /* scrolling up always works */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002329 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002330 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002331 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002332
2333 if (!can_clear((char_u *)" "))
2334 {
2335 /* Scrolling up doesn't result in the right background. Set the
2336 * background here. It's not efficient, but avoids that we have to do
2337 * it all over the code. */
2338 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2339
2340 /* Also clear the last char of the last but one line if it was not
2341 * cleared before to avoid a scroll-up. */
2342 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2343 screen_fill((int)Rows - 2, (int)Rows - 1,
2344 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2345 }
2346}
2347
2348/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002349 * Increment "msg_scrolled".
2350 */
2351 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002352inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002353{
2354#ifdef FEAT_EVAL
2355 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2356 {
2357 char_u *p = sourcing_name;
2358 char_u *tofree = NULL;
2359 int len;
2360
2361 /* v:scrollstart is empty, set it to the script/function name and line
2362 * number */
2363 if (p == NULL)
2364 p = (char_u *)_("Unknown");
2365 else
2366 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002367 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002368 tofree = alloc(len);
2369 if (tofree != NULL)
2370 {
2371 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2372 p, (long)sourcing_lnum);
2373 p = tofree;
2374 }
2375 }
2376 set_vim_var_string(VV_SCROLLSTART, p, -1);
2377 vim_free(tofree);
2378 }
2379#endif
2380 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002381 if (must_redraw < VALID)
2382 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002383}
2384
2385/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002386 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2387 * store the displayed text and remember where screen lines start.
2388 */
2389typedef struct msgchunk_S msgchunk_T;
2390struct msgchunk_S
2391{
2392 msgchunk_T *sb_next;
2393 msgchunk_T *sb_prev;
2394 char sb_eol; /* TRUE when line ends after this text */
2395 int sb_msg_col; /* column in which text starts */
2396 int sb_attr; /* text attributes */
2397 char_u sb_text[1]; /* text to be displayed, actually longer */
2398};
2399
2400static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2401
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002402static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002403
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002404typedef enum {
2405 SB_CLEAR_NONE = 0,
2406 SB_CLEAR_ALL,
2407 SB_CLEAR_CMDLINE_BUSY,
2408 SB_CLEAR_CMDLINE_DONE
2409} sb_clear_T;
2410
2411/* When to clear text on next msg. */
2412static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002413
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002414/*
2415 * Store part of a printed message for displaying when scrolling back.
2416 */
2417 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002418store_sb_text(
2419 char_u **sb_str, /* start of string */
2420 char_u *s, /* just after string */
2421 int attr,
2422 int *sb_col,
2423 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002424{
2425 msgchunk_T *mp;
2426
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002427 if (do_clear_sb_text == SB_CLEAR_ALL
2428 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002429 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002430 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2431 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002432 }
2433
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002434 if (s > *sb_str)
2435 {
2436 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2437 if (mp != NULL)
2438 {
2439 mp->sb_eol = finish;
2440 mp->sb_msg_col = *sb_col;
2441 mp->sb_attr = attr;
2442 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2443
2444 if (last_msgchunk == NULL)
2445 {
2446 last_msgchunk = mp;
2447 mp->sb_prev = NULL;
2448 }
2449 else
2450 {
2451 mp->sb_prev = last_msgchunk;
2452 last_msgchunk->sb_next = mp;
2453 last_msgchunk = mp;
2454 }
2455 mp->sb_next = NULL;
2456 }
2457 }
2458 else if (finish && last_msgchunk != NULL)
2459 last_msgchunk->sb_eol = TRUE;
2460
2461 *sb_str = s;
2462 *sb_col = 0;
2463}
2464
2465/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002466 * Finished showing messages, clear the scroll-back text on the next message.
2467 */
2468 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002469may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002470{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002471 do_clear_sb_text = SB_CLEAR_ALL;
2472}
2473
2474/*
2475 * Starting to edit the command line, do not clear messages now.
2476 */
2477 void
2478sb_text_start_cmdline(void)
2479{
2480 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2481 msg_sb_eol();
2482}
2483
2484/*
2485 * Ending to edit the command line. Clear old lines but the last one later.
2486 */
2487 void
2488sb_text_end_cmdline(void)
2489{
2490 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002491}
2492
2493/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002494 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002495 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002496 * Called when redrawing the screen.
2497 */
2498 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002499clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002500{
2501 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002502 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002503
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002504 if (all)
2505 lastp = &last_msgchunk;
2506 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002507 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002508 if (last_msgchunk == NULL)
2509 return;
2510 lastp = &last_msgchunk->sb_prev;
2511 }
2512
2513 while (*lastp != NULL)
2514 {
2515 mp = (*lastp)->sb_prev;
2516 vim_free(*lastp);
2517 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002518 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519}
2520
2521/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002522 * "g<" command.
2523 */
2524 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002525show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002526{
2527 msgchunk_T *mp;
2528
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002529 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002530 * weird, typing a command without output results in one line. */
2531 mp = msg_sb_start(last_msgchunk);
2532 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002533 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002534 else
2535 {
2536 do_more_prompt('G');
2537 wait_return(FALSE);
2538 }
2539}
2540
2541/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002542 * Move to the start of screen line in already displayed text.
2543 */
2544 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002545msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546{
2547 msgchunk_T *mp = mps;
2548
2549 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2550 mp = mp->sb_prev;
2551 return mp;
2552}
2553
2554/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002555 * Mark the last message chunk as finishing the line.
2556 */
2557 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002558msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002559{
2560 if (last_msgchunk != NULL)
2561 last_msgchunk->sb_eol = TRUE;
2562}
2563
2564/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002565 * Display a screen line from previously displayed text at row "row".
2566 * Returns a pointer to the text for the next line (can be NULL).
2567 */
2568 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002569disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002570{
2571 msgchunk_T *mp = smp;
2572 char_u *p;
2573
2574 for (;;)
2575 {
2576 msg_row = row;
2577 msg_col = mp->sb_msg_col;
2578 p = mp->sb_text;
2579 if (*p == '\n') /* don't display the line break */
2580 ++p;
2581 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2582 if (mp->sb_eol || mp->sb_next == NULL)
2583 break;
2584 mp = mp->sb_next;
2585 }
2586 return mp->sb_next;
2587}
2588
2589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 * Output any postponed text for msg_puts_attr_len().
2591 */
2592 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002593t_puts(
2594 int *t_col,
2595 char_u *t_s,
2596 char_u *s,
2597 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 /* output postponed text */
2600 msg_didout = TRUE; /* remember that line is not empty */
2601 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002602 msg_col += *t_col;
2603 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604#ifdef FEAT_MBYTE
2605 /* If the string starts with a composing character don't increment the
2606 * column position for it. */
2607 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2608 --msg_col;
2609#endif
2610 if (msg_col >= Columns)
2611 {
2612 msg_col = 0;
2613 ++msg_row;
2614 }
2615}
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617/*
2618 * Returns TRUE when messages should be printed with mch_errmsg().
2619 * This is used when there is no valid screen, so we can see error messages.
2620 * If termcap is not active, we may be writing in an alternate console
2621 * window, cursor positioning may not work correctly (window size may be
2622 * different, e.g. for Win32 console) or we just don't know where the
2623 * cursor is.
2624 */
2625 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002626msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 return (!msg_check_screen()
2629#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2630 || !termcap_active
2631#endif
2632 || (swapping_screen() && !termcap_active)
2633 );
2634}
2635
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002636/*
2637 * Print a message when there is no valid screen.
2638 */
2639 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002640msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002641{
2642 char_u *s = str;
2643 char_u buf[4];
2644 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645#ifdef WIN3264
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002646# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2647 char_u *ccp = NULL;
2648
2649# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002650 if (!(silent_mode && p_verbose == 0))
2651 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002652
2653# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2654 if (enc_codepage >= 0 && (int)GetConsoleCP() != enc_codepage)
2655 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02002656 int inlen = (int)STRLEN(str);
Bram Moolenaar1b66c002017-08-03 18:55:00 +02002657 int outlen;
2658 WCHAR *widestr = (WCHAR *)enc_to_utf16(str, &inlen);
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002659
2660 if (widestr != NULL)
2661 {
Bram Moolenaar1b66c002017-08-03 18:55:00 +02002662 WideCharToMultiByte_alloc(GetConsoleCP(), 0, widestr, inlen,
2663 (LPSTR *)&ccp, &outlen, 0, 0);
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002664 vim_free(widestr);
2665 s = str = ccp;
2666 }
2667 }
2668# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002669#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002670 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002671 {
2672 if (!(silent_mode && p_verbose == 0))
2673 {
2674 /* NL --> CR NL translation (for Unix, not for "--version") */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002675 p = &buf[0];
2676 if (*s == '\n' && !info_message)
2677 *p++ = '\r';
Bram Moolenaard0573012017-10-28 21:11:06 +02002678#if defined(USE_CR)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002679 else
2680#endif
2681 *p++ = *s;
2682 *p = '\0';
2683 if (info_message) /* informative message, not an error */
2684 mch_msg((char *)buf);
2685 else
2686 mch_errmsg((char *)buf);
2687 }
2688
2689 /* primitive way to compute the current column */
2690#ifdef FEAT_RIGHTLEFT
2691 if (cmdmsg_rl)
2692 {
2693 if (*s == '\r' || *s == '\n')
2694 msg_col = Columns - 1;
2695 else
2696 --msg_col;
2697 }
2698 else
2699#endif
2700 {
2701 if (*s == '\r' || *s == '\n')
2702 msg_col = 0;
2703 else
2704 ++msg_col;
2705 }
2706 ++s;
2707 }
2708 msg_didout = TRUE; /* assume that line is not empty */
2709
2710#ifdef WIN3264
Bram Moolenaar01efafa2017-08-03 17:37:48 +02002711# if defined(FEAT_MBYTE) && !defined(FEAT_GUI_MSWIN)
2712 vim_free(ccp);
2713# endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002714 if (!(silent_mode && p_verbose == 0))
2715 mch_settmode(TMODE_RAW);
2716#endif
2717}
2718
2719/*
2720 * Show the more-prompt and handle the user response.
2721 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002722 * When at hit-enter prompt "typed_char" is the already typed character,
2723 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002724 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2725 */
2726 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002727do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002729 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002730 int used_typed_char = typed_char;
2731 int oldState = State;
2732 int c;
2733#ifdef FEAT_CON_DIALOG
2734 int retval = FALSE;
2735#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002736 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002737 msgchunk_T *mp_last = NULL;
2738 msgchunk_T *mp;
2739 int i;
2740
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002741 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002742 * that case don't show another prompt. Also when at the hit-Enter prompt
2743 * and nothing was typed. */
2744 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002745 return FALSE;
2746 entered = TRUE;
2747
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002748 if (typed_char == 'G')
2749 {
2750 /* "g<": Find first line on the last page. */
2751 mp_last = msg_sb_start(last_msgchunk);
2752 for (i = 0; i < Rows - 2 && mp_last != NULL
2753 && mp_last->sb_prev != NULL; ++i)
2754 mp_last = msg_sb_start(mp_last->sb_prev);
2755 }
2756
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002757 State = ASKMORE;
2758#ifdef FEAT_MOUSE
2759 setmouse();
2760#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002761 if (typed_char == NUL)
2762 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002763 for (;;)
2764 {
2765 /*
2766 * Get a typed character directly from the user.
2767 */
2768 if (used_typed_char != NUL)
2769 {
2770 c = used_typed_char; /* was typed at hit-enter prompt */
2771 used_typed_char = NUL;
2772 }
2773 else
2774 c = get_keystroke();
2775
2776#if defined(FEAT_MENU) && defined(FEAT_GUI)
2777 if (c == K_MENU)
2778 {
2779 int idx = get_menu_index(current_menu, ASKMORE);
2780
2781 /* Used a menu. If it starts with CTRL-Y, it must
2782 * be a "Copy" for the clipboard. Otherwise
2783 * assume that we end */
2784 if (idx == MENU_INDEX_INVALID)
2785 continue;
2786 c = *current_menu->strings[idx];
2787 if (c != NUL && current_menu->strings[idx][1] != NUL)
2788 ins_typebuf(current_menu->strings[idx] + 1,
2789 current_menu->noremap[idx], 0, TRUE,
2790 current_menu->silent[idx]);
2791 }
2792#endif
2793
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002794 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 switch (c)
2796 {
2797 case BS: /* scroll one line back */
2798 case K_BS:
2799 case 'k':
2800 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002801 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 break;
2803
2804 case CAR: /* one extra line */
2805 case NL:
2806 case 'j':
2807 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002808 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809 break;
2810
2811 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002812 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 break;
2814
2815 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002816 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 break;
2818
2819 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002820 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002821 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002822 break;
2823
2824 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002825 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002826 case K_PAGEDOWN:
2827 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002828 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002829 break;
2830
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002831 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002832 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002833 break;
2834
2835 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002836 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002837 lines_left = 999999;
2838 break;
2839
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002840 case ':': /* start new command line */
2841#ifdef FEAT_CON_DIALOG
2842 if (!confirm_msg_used)
2843#endif
2844 {
2845 /* Since got_int is set all typeahead will be flushed, but we
2846 * want to keep this ':', remember that in a special way. */
2847 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002848#ifdef FEAT_TERMINAL
2849 skip_term_loop = TRUE;
2850#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 cmdline_row = Rows - 1; /* put ':' on this line */
2852 skip_redraw = TRUE; /* skip redraw once */
2853 need_wait_return = FALSE; /* don't wait in main() */
2854 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002855 /* FALLTHROUGH */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002856 case 'q': /* quit */
2857 case Ctrl_C:
2858 case ESC:
2859#ifdef FEAT_CON_DIALOG
2860 if (confirm_msg_used)
2861 {
2862 /* Jump to the choices of the dialog. */
2863 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002864 }
2865 else
2866#endif
2867 {
2868 got_int = TRUE;
2869 quit_more = TRUE;
2870 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002871 /* When there is some more output (wrapping line) display that
2872 * without another prompt. */
2873 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002874 break;
2875
2876#ifdef FEAT_CLIPBOARD
2877 case Ctrl_Y:
2878 /* Strange way to allow copying (yanking) a modeless
2879 * selection at the more prompt. Use CTRL-Y,
2880 * because the same is used in Cmdline-mode and at the
2881 * hit-enter prompt. However, scrolling one line up
2882 * might be expected... */
2883 if (clip_star.state == SELECT_DONE)
2884 clip_copy_modeless_selection(TRUE);
2885 continue;
2886#endif
2887 default: /* no valid response */
2888 msg_moremsg(TRUE);
2889 continue;
2890 }
2891
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002892 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002893 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002894 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002895 {
2896 /* go to start of last line */
2897 if (mp_last == NULL)
2898 mp = msg_sb_start(last_msgchunk);
2899 else if (mp_last->sb_prev != NULL)
2900 mp = msg_sb_start(mp_last->sb_prev);
2901 else
2902 mp = NULL;
2903
2904 /* go to start of line at top of the screen */
2905 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2906 ++i)
2907 mp = msg_sb_start(mp->sb_prev);
2908
2909 if (mp != NULL && mp->sb_prev != NULL)
2910 {
2911 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002912 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 {
2914 if (mp == NULL || mp->sb_prev == NULL)
2915 break;
2916 mp = msg_sb_start(mp->sb_prev);
2917 if (mp_last == NULL)
2918 mp_last = msg_sb_start(last_msgchunk);
2919 else
2920 mp_last = msg_sb_start(mp_last->sb_prev);
2921 }
2922
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002923 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002924 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002925 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002926 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002927 (void)disp_sb_line(0, mp);
2928 }
2929 else
2930 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002931 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002932 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002933 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2934 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002935 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002936 ++msg_scrolled;
2937 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002939 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002940 }
2941 }
2942 else
2943 {
2944 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002945 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002946 {
2947 /* scroll up, display line at bottom */
2948 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002949 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002950 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2951 (int)Columns, ' ', ' ', 0);
2952 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002953 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954 }
2955 }
2956
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002957 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002958 {
2959 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002960 screen_fill((int)Rows - 1, (int)Rows, 0,
2961 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002962 msg_moremsg(FALSE);
2963 continue;
2964 }
2965
2966 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002967 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002968 }
2969
2970 break;
2971 }
2972
2973 /* clear the --more-- message */
2974 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2975 State = oldState;
2976#ifdef FEAT_MOUSE
2977 setmouse();
2978#endif
2979 if (quit_more)
2980 {
2981 msg_row = Rows - 1;
2982 msg_col = 0;
2983 }
2984#ifdef FEAT_RIGHTLEFT
2985 else if (cmdmsg_rl)
2986 msg_col = Columns - 1;
2987#endif
2988
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002989 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002990#ifdef FEAT_CON_DIALOG
2991 return retval;
2992#else
2993 return FALSE;
2994#endif
2995}
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2998
2999#ifdef mch_errmsg
3000# undef mch_errmsg
3001#endif
3002#ifdef mch_msg
3003# undef mch_msg
3004#endif
3005
3006/*
3007 * Give an error message. To be used when the screen hasn't been initialized
3008 * yet. When stderr can't be used, collect error messages until the GUI has
3009 * started and they can be displayed in a message box.
3010 */
3011 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003012mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int len;
3015
3016#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3017 /* On Unix use stderr if it's a tty.
3018 * When not going to start the GUI also use stderr.
3019 * On Mac, when started from Finder, stderr is the console. */
3020 if (
3021# ifdef UNIX
Bram Moolenaard0573012017-10-28 21:11:06 +02003022# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3024# else
3025 isatty(2)
3026# endif
3027# ifdef FEAT_GUI
3028 ||
3029# endif
3030# endif
3031# ifdef FEAT_GUI
3032 !(gui.in_use || gui.starting)
3033# endif
3034 )
3035 {
3036 fprintf(stderr, "%s", str);
3037 return;
3038 }
3039#endif
3040
3041 /* avoid a delay for a message that isn't there */
3042 emsg_on_display = FALSE;
3043
3044 len = (int)STRLEN(str) + 1;
3045 if (error_ga.ga_growsize == 0)
3046 {
3047 error_ga.ga_growsize = 80;
3048 error_ga.ga_itemsize = 1;
3049 }
3050 if (ga_grow(&error_ga, len) == OK)
3051 {
3052 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3053 (char_u *)str, len);
3054#ifdef UNIX
3055 /* remove CR characters, they are displayed */
3056 {
3057 char_u *p;
3058
3059 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3060 for (;;)
3061 {
3062 p = vim_strchr(p, '\r');
3063 if (p == NULL)
3064 break;
3065 *p = ' ';
3066 }
3067 }
3068#endif
3069 --len; /* don't count the NUL at the end */
3070 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072}
3073
3074/*
3075 * Give a message. To be used when the screen hasn't been initialized yet.
3076 * When there is no tty, collect messages until the GUI has started and they
3077 * can be displayed in a message box.
3078 */
3079 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003080mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3083 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3084 * uses mch_errmsg() when started from the desktop.
3085 * When not going to start the GUI also use stdout.
3086 * On Mac, when started from Finder, stderr is the console. */
3087 if (
3088# ifdef UNIX
Bram Moolenaard0573012017-10-28 21:11:06 +02003089# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3091# else
3092 isatty(2)
3093# endif
3094# ifdef FEAT_GUI
3095 ||
3096# endif
3097# endif
3098# ifdef FEAT_GUI
3099 !(gui.in_use || gui.starting)
3100# endif
3101 )
3102 {
3103 printf("%s", str);
3104 return;
3105 }
3106# endif
3107 mch_errmsg(str);
3108}
3109#endif /* USE_MCH_ERRMSG */
3110
3111/*
3112 * Put a character on the screen at the current message position and advance
3113 * to the next position. Only for printable ASCII!
3114 */
3115 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003116msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 msg_didout = TRUE; /* remember that line is not empty */
3119 screen_putchar(c, msg_row, msg_col, attr);
3120#ifdef FEAT_RIGHTLEFT
3121 if (cmdmsg_rl)
3122 {
3123 if (--msg_col == 0)
3124 {
3125 msg_col = Columns;
3126 ++msg_row;
3127 }
3128 }
3129 else
3130#endif
3131 {
3132 if (++msg_col >= Columns)
3133 {
3134 msg_col = 0;
3135 ++msg_row;
3136 }
3137 }
3138}
3139
3140 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003141msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003143 int attr;
3144 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaar8820b482017-03-16 17:23:31 +01003146 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003147 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003149 screen_puts((char_u *)
3150 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3151 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152}
3153
3154/*
3155 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3156 * exmode_active.
3157 */
3158 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003159repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 if (State == ASKMORE)
3162 {
3163 msg_moremsg(TRUE); /* display --more-- message again */
3164 msg_row = Rows - 1;
3165 }
3166#ifdef FEAT_CON_DIALOG
3167 else if (State == CONFIRM)
3168 {
3169 display_confirm_msg(); /* display ":confirm" message again */
3170 msg_row = Rows - 1;
3171 }
3172#endif
3173 else if (State == EXTERNCMD)
3174 {
3175 windgoto(msg_row, msg_col); /* put cursor back */
3176 }
3177 else if (State == HITRETURN || State == SETWSIZE)
3178 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003179 if (msg_row == Rows - 1)
3180 {
3181 /* Avoid drawing the "hit-enter" prompt below the previous one,
3182 * overwrite it. Esp. useful when regaining focus and a
3183 * FocusGained autocmd exists but didn't draw anything. */
3184 msg_didout = FALSE;
3185 msg_col = 0;
3186 msg_clr_eos();
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 hit_return_msg();
3189 msg_row = Rows - 1;
3190 }
3191}
3192
3193/*
3194 * msg_check_screen - check if the screen is initialized.
3195 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3196 * While starting the GUI the terminal codes will be set for the GUI, but the
3197 * output goes to the terminal. Don't use the terminal codes then.
3198 */
3199 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003200msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 if (!full_screen || !screen_valid(FALSE))
3203 return FALSE;
3204
3205 if (msg_row >= Rows)
3206 msg_row = Rows - 1;
3207 if (msg_col >= Columns)
3208 msg_col = Columns - 1;
3209 return TRUE;
3210}
3211
3212/*
3213 * Clear from current message position to end of screen.
3214 * Skip this when ":silent" was used, no need to clear for redirection.
3215 */
3216 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003217msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 if (msg_silent == 0)
3220 msg_clr_eos_force();
3221}
3222
3223/*
3224 * Clear from current message position to end of screen.
3225 * Note: msg_col is not updated, so we remember the end of the message
3226 * for msg_check().
3227 */
3228 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003229msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 if (msg_use_printf())
3232 {
3233 if (full_screen) /* only when termcap codes are valid */
3234 {
3235 if (*T_CD)
3236 out_str(T_CD); /* clear to end of display */
3237 else if (*T_CE)
3238 out_str(T_CE); /* clear to end of line */
3239 }
3240 }
3241 else
3242 {
3243#ifdef FEAT_RIGHTLEFT
3244 if (cmdmsg_rl)
3245 {
3246 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3247 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3248 }
3249 else
3250#endif
3251 {
3252 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3253 ' ', ' ', 0);
3254 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3255 }
3256 }
3257}
3258
3259/*
3260 * Clear the command line.
3261 */
3262 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003263msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 msg_row = cmdline_row;
3266 msg_col = 0;
3267 msg_clr_eos_force();
3268}
3269
3270/*
3271 * end putting a message on the screen
3272 * call wait_return if the message does not fit in the available space
3273 * return TRUE if wait_return not called.
3274 */
3275 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003276msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003279 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 * or the ruler option is set and we run into it,
3281 * we have to redraw the window.
3282 * Do not do this if we are abandoning the file or editing the command line.
3283 */
3284 if (!exiting && need_wait_return && !(State & CMDLINE))
3285 {
3286 wait_return(FALSE);
3287 return FALSE;
3288 }
3289 out_flush();
3290 return TRUE;
3291}
3292
3293/*
3294 * If the written message runs into the shown command or ruler, we have to
3295 * wait for hit-return and redraw the window later.
3296 */
3297 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003298msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 if (msg_row == Rows - 1 && msg_col >= sc_col)
3301 {
3302 need_wait_return = TRUE;
3303 redraw_cmdline = TRUE;
3304 }
3305}
3306
3307/*
3308 * May write a string to the redirection file.
3309 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3310 */
3311 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003312redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 char_u *s = str;
3315 static int cur_col = 0;
3316
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003317 /* Don't do anything for displaying prompts and the like. */
3318 if (redir_off)
3319 return;
3320
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003321 /* If 'verbosefile' is set prepare for writing in that file. */
3322 if (*p_vfile != NUL && verbose_fd == NULL)
3323 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003324
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003325 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
3327 /* If the string doesn't start with CR or NL, go to msg_col */
3328 if (*s != '\n' && *s != '\r')
3329 {
3330 while (cur_col < msg_col)
3331 {
3332#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003333 if (redir_execute)
3334 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003335 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003337 else if (redir_vname)
3338 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003341 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003343 if (verbose_fd != NULL)
3344 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 ++cur_col;
3346 }
3347 }
3348
3349#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003350 if (redir_execute)
3351 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003352 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003354 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003355 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#endif
3357
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003358 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3360 {
3361#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003362 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003364 if (redir_fd != NULL)
3365 putc(*s, redir_fd);
3366 if (verbose_fd != NULL)
3367 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (*s == '\r' || *s == '\n')
3369 cur_col = 0;
3370 else if (*s == '\t')
3371 cur_col += (8 - cur_col % 8);
3372 else
3373 ++cur_col;
3374 ++s;
3375 }
3376
3377 if (msg_silent != 0) /* should update msg_col */
3378 msg_col = cur_col;
3379 }
3380}
3381
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003382 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003383redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003384{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003385 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003386#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003387 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003388#endif
3389 ;
3390}
3391
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003393 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003394 * Must always be called paired with verbose_leave()!
3395 */
3396 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003397verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003398{
3399 if (*p_vfile != NUL)
3400 ++msg_silent;
3401}
3402
3403/*
3404 * After giving verbose message.
3405 * Must always be called paired with verbose_enter()!
3406 */
3407 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003408verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003409{
3410 if (*p_vfile != NUL)
3411 if (--msg_silent < 0)
3412 msg_silent = 0;
3413}
3414
3415/*
3416 * Like verbose_enter() and set msg_scroll when displaying the message.
3417 */
3418 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003419verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003420{
3421 if (*p_vfile != NUL)
3422 ++msg_silent;
3423 else
3424 /* always scroll up, don't overwrite */
3425 msg_scroll = TRUE;
3426}
3427
3428/*
3429 * Like verbose_leave() and set cmdline_row when displaying the message.
3430 */
3431 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003432verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003433{
3434 if (*p_vfile != NUL)
3435 {
3436 if (--msg_silent < 0)
3437 msg_silent = 0;
3438 }
3439 else
3440 cmdline_row = msg_row;
3441}
3442
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003443/*
3444 * Called when 'verbosefile' is set: stop writing to the file.
3445 */
3446 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003447verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003448{
3449 if (verbose_fd != NULL)
3450 {
3451 fclose(verbose_fd);
3452 verbose_fd = NULL;
3453 }
3454 verbose_did_open = FALSE;
3455}
3456
3457/*
3458 * Open the file 'verbosefile'.
3459 * Return FAIL or OK.
3460 */
3461 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003462verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003463{
3464 if (verbose_fd == NULL && !verbose_did_open)
3465 {
3466 /* Only give the error message once. */
3467 verbose_did_open = TRUE;
3468
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003469 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003470 if (verbose_fd == NULL)
3471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003472 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003473 return FAIL;
3474 }
3475 }
3476 return OK;
3477}
3478
3479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 * Give a warning message (for searching).
3481 * Use 'w' highlighting and may repeat the message after redrawing
3482 */
3483 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003484give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
3486 /* Don't do this for ":silent". */
3487 if (msg_silent != 0)
3488 return;
3489
3490 /* Don't want a hit-enter prompt here. */
3491 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003492
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493#ifdef FEAT_EVAL
3494 set_vim_var_string(VV_WARNINGMSG, message, -1);
3495#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003496 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003498 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 else
3500 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003501 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003502 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 msg_didout = FALSE; /* overwrite this message */
3504 msg_nowait = TRUE; /* don't wait for this message */
3505 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 --no_wait_return;
3508}
3509
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003510 void
3511give_warning2(char_u *message, char_u *a1, int hl)
3512{
3513 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3514 give_warning(IObuff, hl);
3515}
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517/*
3518 * Advance msg cursor to column "col".
3519 */
3520 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003521msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 if (msg_silent != 0) /* nothing to advance to */
3524 {
3525 msg_col = col; /* for redirection, may fill it up later */
3526 return;
3527 }
3528 if (col >= Columns) /* not enough room */
3529 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003530#ifdef FEAT_RIGHTLEFT
3531 if (cmdmsg_rl)
3532 while (msg_col > Columns - col)
3533 msg_putchar(' ');
3534 else
3535#endif
3536 while (msg_col < col)
3537 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538}
3539
3540#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3541/*
3542 * Used for "confirm()" function, and the :confirm command prefix.
3543 * Versions which haven't got flexible dialogs yet, and console
3544 * versions, get this generic handler which uses the command line.
3545 *
3546 * type = one of:
3547 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3548 * title = title string (can be NULL for default)
3549 * (neither used in console dialogs at the moment)
3550 *
3551 * Format of the "buttons" string:
3552 * "Button1Name\nButton2Name\nButton3Name"
3553 * The first button should normally be the default/accept
3554 * The second button should be the 'Cancel' button
3555 * Other buttons- use your imagination!
3556 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3557 * different letter.
3558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003560do_dialog(
3561 int type UNUSED,
3562 char_u *title UNUSED,
3563 char_u *message,
3564 char_u *buttons,
3565 int dfltbutton,
3566 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003567 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003568 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003569 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 int oldState;
3572 int retval = 0;
3573 char_u *hotkeys;
3574 int c;
3575 int i;
3576
3577#ifndef NO_CONSOLE
3578 /* Don't output anything in silent mode ("ex -s") */
3579 if (silent_mode)
3580 return dfltbutton; /* return default option */
3581#endif
3582
3583#ifdef FEAT_GUI_DIALOG
3584 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3585 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3586 {
3587 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003588 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003589 /* avoid a hit-enter prompt without clearing the cmdline */
3590 need_wait_return = FALSE;
3591 emsg_on_display = FALSE;
3592 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
3594 /* Flush output to avoid that further messages and redrawing is done
3595 * in the wrong order. */
3596 out_flush();
3597 gui_mch_update();
3598
3599 return c;
3600 }
3601#endif
3602
3603 oldState = State;
3604 State = CONFIRM;
3605#ifdef FEAT_MOUSE
3606 setmouse();
3607#endif
3608
3609 /*
3610 * Since we wait for a keypress, don't make the
3611 * user press RETURN as well afterwards.
3612 */
3613 ++no_wait_return;
3614 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3615
3616 if (hotkeys != NULL)
3617 {
3618 for (;;)
3619 {
3620 /* Get a typed character directly from the user. */
3621 c = get_keystroke();
3622 switch (c)
3623 {
3624 case CAR: /* User accepts default option */
3625 case NL:
3626 retval = dfltbutton;
3627 break;
3628 case Ctrl_C: /* User aborts/cancels */
3629 case ESC:
3630 retval = 0;
3631 break;
3632 default: /* Could be a hotkey? */
3633 if (c < 0) /* special keys are ignored here */
3634 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003635 if (c == ':' && ex_cmd)
3636 {
3637 retval = dfltbutton;
3638 ins_char_typebuf(':');
3639 break;
3640 }
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 /* Make the character lowercase, as chars in "hotkeys" are. */
3643 c = MB_TOLOWER(c);
3644 retval = 1;
3645 for (i = 0; hotkeys[i]; ++i)
3646 {
3647#ifdef FEAT_MBYTE
3648 if (has_mbyte)
3649 {
3650 if ((*mb_ptr2char)(hotkeys + i) == c)
3651 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003652 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654 else
3655#endif
3656 if (hotkeys[i] == c)
3657 break;
3658 ++retval;
3659 }
3660 if (hotkeys[i])
3661 break;
3662 /* No hotkey match, so keep waiting */
3663 continue;
3664 }
3665 break;
3666 }
3667
3668 vim_free(hotkeys);
3669 }
3670
3671 State = oldState;
3672#ifdef FEAT_MOUSE
3673 setmouse();
3674#endif
3675 --no_wait_return;
3676 msg_end_prompt();
3677
3678 return retval;
3679}
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681/*
3682 * Copy one character from "*from" to "*to", taking care of multi-byte
3683 * characters. Return the length of the character in bytes.
3684 */
3685 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003686copy_char(
3687 char_u *from,
3688 char_u *to,
3689 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691#ifdef FEAT_MBYTE
3692 int len;
3693 int c;
3694
3695 if (has_mbyte)
3696 {
3697 if (lowercase)
3698 {
3699 c = MB_TOLOWER((*mb_ptr2char)(from));
3700 return (*mb_char2bytes)(c, to);
3701 }
3702 else
3703 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003704 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 mch_memmove(to, from, (size_t)len);
3706 return len;
3707 }
3708 }
3709 else
3710#endif
3711 {
3712 if (lowercase)
3713 *to = (char_u)TOLOWER_LOC(*from);
3714 else
3715 *to = *from;
3716 return 1;
3717 }
3718}
3719
3720/*
3721 * Format the dialog string, and display it at the bottom of
3722 * the screen. Return a string of hotkey chars (if defined) for
3723 * each 'button'. If a button has no hotkey defined, the first character of
3724 * the button is used.
3725 * The hotkeys can be multi-byte characters, but without combining chars.
3726 *
3727 * Returns an allocated string with hotkeys, or NULL for error.
3728 */
3729 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003730msg_show_console_dialog(
3731 char_u *message,
3732 char_u *buttons,
3733 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
3735 int len = 0;
3736#ifdef FEAT_MBYTE
3737# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3738#else
3739# define HOTK_LEN 1
3740#endif
3741 int lenhotkey = HOTK_LEN; /* count first button */
3742 char_u *hotk = NULL;
3743 char_u *msgp = NULL;
3744 char_u *hotkp = NULL;
3745 char_u *r;
3746 int copy;
3747#define HAS_HOTKEY_LEN 30
3748 char_u has_hotkey[HAS_HOTKEY_LEN];
3749 int first_hotkey = FALSE; /* first char of button is hotkey */
3750 int idx;
3751
3752 has_hotkey[0] = FALSE;
3753
3754 /*
3755 * First loop: compute the size of memory to allocate.
3756 * Second loop: copy to the allocated memory.
3757 */
3758 for (copy = 0; copy <= 1; ++copy)
3759 {
3760 r = buttons;
3761 idx = 0;
3762 while (*r)
3763 {
3764 if (*r == DLG_BUTTON_SEP)
3765 {
3766 if (copy)
3767 {
3768 *msgp++ = ',';
3769 *msgp++ = ' '; /* '\n' -> ', ' */
3770
3771 /* advance to next hotkey and set default hotkey */
3772#ifdef FEAT_MBYTE
3773 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003774 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 else
3776#endif
3777 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003778 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (dfltbutton)
3780 --dfltbutton;
3781
3782 /* If no hotkey is specified first char is used. */
3783 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3784 first_hotkey = TRUE;
3785 }
3786 else
3787 {
3788 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3789 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3790 if (idx < HAS_HOTKEY_LEN - 1)
3791 has_hotkey[++idx] = FALSE;
3792 }
3793 }
3794 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3795 {
3796 if (*r == DLG_HOTKEY_CHAR)
3797 ++r;
3798 first_hotkey = FALSE;
3799 if (copy)
3800 {
3801 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3802 *msgp++ = *r;
3803 else
3804 {
3805 /* '&a' -> '[a]' */
3806 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3807 msgp += copy_char(r, msgp, FALSE);
3808 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3809
3810 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003811 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813 }
3814 else
3815 {
3816 ++len; /* '&a' -> '[a]' */
3817 if (idx < HAS_HOTKEY_LEN - 1)
3818 has_hotkey[idx] = TRUE;
3819 }
3820 }
3821 else
3822 {
3823 /* everything else copy literally */
3824 if (copy)
3825 msgp += copy_char(r, msgp, FALSE);
3826 }
3827
3828 /* advance to the next character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003829 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831
3832 if (copy)
3833 {
3834 *msgp++ = ':';
3835 *msgp++ = ' ';
3836 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 else
3839 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003840 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003841 + 2 /* for the NL's */
3842 + STRLEN(buttons)
3843 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003844 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
3846 /* If no hotkey is specified first char is used. */
3847 if (!has_hotkey[0])
3848 {
3849 first_hotkey = TRUE;
3850 len += 2; /* "x" -> "[x]" */
3851 }
3852
3853 /*
3854 * Now allocate and load the strings
3855 */
3856 vim_free(confirm_msg);
3857 confirm_msg = alloc(len);
3858 if (confirm_msg == NULL)
3859 return NULL;
3860 *confirm_msg = NUL;
3861 hotk = alloc(lenhotkey);
3862 if (hotk == NULL)
3863 return NULL;
3864
3865 *confirm_msg = '\n';
3866 STRCPY(confirm_msg + 1, message);
3867
3868 msgp = confirm_msg + 1 + STRLEN(message);
3869 hotkp = hotk;
3870
Bram Moolenaar6a516062007-07-05 08:11:42 +00003871 /* Define first default hotkey. Keep the hotkey string NUL
3872 * terminated to avoid reading past the end. */
3873 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
3875 /* Remember where the choices start, displaying starts here when
3876 * "hotkp" typed at the more prompt. */
3877 confirm_msg_tail = msgp;
3878 *msgp++ = '\n';
3879 }
3880 }
3881
3882 display_confirm_msg();
3883 return hotk;
3884}
3885
3886/*
3887 * Display the ":confirm" message. Also called when screen resized.
3888 */
3889 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003890display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
3892 /* avoid that 'q' at the more prompt truncates the message here */
3893 ++confirm_msg_used;
3894 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003895 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 --confirm_msg_used;
3897}
3898
3899#endif /* FEAT_CON_DIALOG */
3900
3901#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3902
3903 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003904vim_dialog_yesno(
3905 int type,
3906 char_u *title,
3907 char_u *message,
3908 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 if (do_dialog(type,
3911 title == NULL ? (char_u *)_("Question") : title,
3912 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003913 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return VIM_YES;
3915 return VIM_NO;
3916}
3917
3918 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003919vim_dialog_yesnocancel(
3920 int type,
3921 char_u *title,
3922 char_u *message,
3923 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
3925 switch (do_dialog(type,
3926 title == NULL ? (char_u *)_("Question") : title,
3927 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003928 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 case 1: return VIM_YES;
3931 case 2: return VIM_NO;
3932 }
3933 return VIM_CANCEL;
3934}
3935
3936 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003937vim_dialog_yesnoallcancel(
3938 int type,
3939 char_u *title,
3940 char_u *message,
3941 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 switch (do_dialog(type,
3944 title == NULL ? (char_u *)"Question" : title,
3945 message,
3946 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003947 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 case 1: return VIM_YES;
3950 case 2: return VIM_NO;
3951 case 3: return VIM_ALL;
3952 case 4: return VIM_DISCARDALL;
3953 }
3954 return VIM_CANCEL;
3955}
3956
3957#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3958
3959#if defined(FEAT_BROWSE) || defined(PROTO)
3960/*
3961 * Generic browse function. Calls gui_mch_browse() when possible.
3962 * Later this may pop-up a non-GUI file selector (external command?).
3963 */
3964 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003965do_browse(
3966 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3967 char_u *title, /* title for the window */
3968 char_u *dflt, /* default file name (may include directory) */
3969 char_u *ext, /* extension added */
3970 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003972 char_u *filter, /* file name filter */
3973 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 char_u *fname;
3976 static char_u *last_dir = NULL; /* last used directory */
3977 char_u *tofree = NULL;
3978 int save_browse = cmdmod.browse;
3979
3980 /* Must turn off browse to avoid that autocommands will get the
3981 * flag too! */
3982 cmdmod.browse = FALSE;
3983
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003984 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003986 if (flags & BROWSE_DIR)
3987 title = (char_u *)_("Select Directory dialog");
3988 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 title = (char_u *)_("Save File dialog");
3990 else
3991 title = (char_u *)_("Open File dialog");
3992 }
3993
3994 /* When no directory specified, use default file name, default dir, buffer
3995 * dir, last dir or current dir */
3996 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3997 {
3998 if (mch_isdir(dflt)) /* default file name is a directory */
3999 {
4000 initdir = dflt;
4001 dflt = NULL;
4002 }
4003 else if (gettail(dflt) != dflt) /* default file name includes a path */
4004 {
4005 tofree = vim_strsave(dflt);
4006 if (tofree != NULL)
4007 {
4008 initdir = tofree;
4009 *gettail(initdir) = NUL;
4010 dflt = gettail(dflt);
4011 }
4012 }
4013 }
4014
4015 if (initdir == NULL || *initdir == NUL)
4016 {
4017 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004018 if (STRCMP(p_bsdir, "last") != 0
4019 && STRCMP(p_bsdir, "buffer") != 0
4020 && STRCMP(p_bsdir, "current") != 0
4021 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 initdir = p_bsdir;
4023 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004024 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 && buf != NULL && buf->b_ffname != NULL)
4026 {
4027 if (dflt == NULL || *dflt == NUL)
4028 dflt = gettail(curbuf->b_ffname);
4029 tofree = vim_strsave(curbuf->b_ffname);
4030 if (tofree != NULL)
4031 {
4032 initdir = tofree;
4033 *gettail(initdir) = NUL;
4034 }
4035 }
4036 /* When 'browsedir' is "last", use dir from last browse */
4037 else if (*p_bsdir == 'l')
4038 initdir = last_dir;
4039 /* When 'browsedir is "current", use current directory. This is the
4040 * default already, leave initdir empty. */
4041 }
4042
4043# ifdef FEAT_GUI
4044 if (gui.in_use) /* when this changes, also adjust f_has()! */
4045 {
4046 if (filter == NULL
4047# ifdef FEAT_EVAL
4048 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
4049 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
4050# endif
4051 )
4052 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004053 if (flags & BROWSE_DIR)
4054 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004055# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004056 /* For systems that have a directory dialog. */
4057 fname = gui_mch_browsedir(title, initdir);
4058# else
4059 /* Generic solution for selecting a directory: select a file and
4060 * remove the file name. */
4061 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
4062# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004063# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004064 /* Win32 adds a dummy file name, others return an arbitrary file
4065 * name. GTK+ 2 returns only the directory, */
4066 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
4067 {
4068 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004069 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004070
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004071 if (tail == fname)
4072 *tail++ = '.'; /* use current dir */
4073 *tail = NUL;
4074 }
4075# endif
4076 }
4077 else
4078 fname = gui_mch_browse(flags & BROWSE_SAVE,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004079 title, dflt, ext, initdir, (char_u *)_(filter));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 /* We hang around in the dialog for a while, the user might do some
4082 * things to our files. The Win32 dialog allows deleting or renaming
4083 * a file, check timestamps. */
4084 need_check_timestamps = TRUE;
4085 did_check_timestamps = FALSE;
4086 }
4087 else
4088# endif
4089 {
4090 /* TODO: non-GUI file selector here */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004091 emsg(_("E338: Sorry, no file browser in console mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 fname = NULL;
4093 }
4094
4095 /* keep the directory for next time */
4096 if (fname != NULL)
4097 {
4098 vim_free(last_dir);
4099 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004100 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 *gettail(last_dir) = NUL;
4103 if (*last_dir == NUL)
4104 {
4105 /* filename only returned, must be in current dir */
4106 vim_free(last_dir);
4107 last_dir = alloc(MAXPATHL);
4108 if (last_dir != NULL)
4109 mch_dirname(last_dir, MAXPATHL);
4110 }
4111 }
4112 }
4113
4114 vim_free(tofree);
4115 cmdmod.browse = save_browse;
4116
4117 return fname;
4118}
4119#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004120
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004121#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122static char *e_printf = N_("E766: Insufficient arguments for printf()");
4123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124/*
4125 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4126 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004127 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004128tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129{
4130 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004131 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004132 int err = FALSE;
4133
4134 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004135 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 else
4137 {
4138 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004139 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004140 if (err)
4141 n = 0;
4142 }
4143 return n;
4144}
4145
4146/*
4147 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004148 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004149 * are not converted to a string.
4150 * If "tofree" is not NULL echo_string() is used. All types are converted to
4151 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004152 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004153 */
4154 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004155tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004157 int idx = *idxp - 1;
4158 char *s = NULL;
4159 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004160
4161 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004162 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004163 else
4164 {
4165 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004166 if (tofree != NULL)
4167 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4168 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004169 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004170 }
4171 return s;
4172}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004173
4174# ifdef FEAT_FLOAT
4175/*
4176 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4177 */
4178 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004179tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004180{
4181 int idx = *idxp - 1;
4182 double f = 0;
4183
4184 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004186 else
4187 {
4188 ++*idxp;
4189 if (tvs[idx].v_type == VAR_FLOAT)
4190 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004191 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004192 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004193 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004194 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004195 }
4196 return f;
4197}
4198# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199#endif
4200
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004201#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004202/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004203 * Return the representation of infinity for printf() function:
4204 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4205 */
4206 static const char *
4207infinity_str(int positive,
4208 char fmt_spec,
4209 int force_sign,
4210 int space_for_positive)
4211{
4212 static const char *table[] =
4213 {
4214 "-inf", "inf", "+inf", " inf",
4215 "-INF", "INF", "+INF", " INF"
4216 };
4217 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4218
4219 if (ASCII_ISUPPER(fmt_spec))
4220 idx += 4;
4221 return table[idx];
4222}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004223#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004224
4225/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004226 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004227 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004228 * consistency.
4229 *
4230 * This code is based on snprintf.c - a portable implementation of snprintf
4231 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004232 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004233 * The original code, including useful comments, can be found here:
4234 * http://www.ijs.si/software/snprintf/
4235 *
4236 * This snprintf() only supports the following conversion specifiers:
4237 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4238 * with flags: '-', '+', ' ', '0' and '#'.
4239 * An asterisk is supported for field width as well as precision.
4240 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004241 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004242 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004243 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4244 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004245 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004246 * The locale is not used, the string is used as a byte string. This is only
4247 * relevant for double-byte encodings where the second byte may be '%'.
4248 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004249 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4250 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004251 *
4252 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004253 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004254 * is greater or equal to "str_m", not all characters from the result
4255 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4256 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004257 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004258 */
4259
4260/*
4261 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004263 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004264 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004265 */
4266
4267/* When generating prototypes all of this is skipped, cproto doesn't
4268 * understand this. */
4269#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004270
Bram Moolenaara800b422010-06-27 01:15:55 +02004271/* Like vim_vsnprintf() but append to the string. */
4272 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004273vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004274{
4275 va_list ap;
4276 int str_l;
4277 size_t len = STRLEN(str);
4278 size_t space;
4279
4280 if (str_m <= len)
4281 space = 0;
4282 else
4283 space = str_m - len;
4284 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004285 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004286 va_end(ap);
4287 return str_l;
4288}
Bram Moolenaara800b422010-06-27 01:15:55 +02004289
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004290 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004291vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004292{
4293 va_list ap;
4294 int str_l;
4295
4296 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004297 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004298 va_end(ap);
4299 return str_l;
4300}
4301
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004302 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004303vim_vsnprintf(
4304 char *str,
4305 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004306 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004307 va_list ap)
4308{
4309 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4310}
4311
4312 int
4313vim_vsnprintf_typval(
4314 char *str,
4315 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004316 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004317 va_list ap,
4318 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004319{
4320 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004321 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004322 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004323
4324 if (p == NULL)
4325 p = "";
4326 while (*p != NUL)
4327 {
4328 if (*p != '%')
4329 {
4330 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004331 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004332
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004333 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004334 if (str_l < str_m)
4335 {
4336 size_t avail = str_m - str_l;
4337
4338 mch_memmove(str + str_l, p, n > avail ? avail : n);
4339 }
4340 p += n;
4341 str_l += n;
4342 }
4343 else
4344 {
4345 size_t min_field_width = 0, precision = 0;
4346 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4347 int alternate_form = 0, force_sign = 0;
4348
4349 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4350 * ignored. */
4351 int space_for_positive = 1;
4352
4353 /* allowed values: \0, h, l, L */
4354 char length_modifier = '\0';
4355
4356 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004357# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004358# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004359 * That sounds reasonable to use as the maximum
4360 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004361# elif defined(FEAT_NUM64)
4362# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004363# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004364# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004365# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004366 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004367
4368 /* string address in case of string argument */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004369 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004370
4371 /* natural field width of arg without padding and sign */
4372 size_t str_arg_l;
4373
4374 /* unsigned char argument value - only defined for c conversion.
4375 * N.B. standard explicitly states the char argument for the c
4376 * conversion is unsigned */
4377 unsigned char uchar_arg;
4378
4379 /* number of zeros to be inserted for numeric conversions as
4380 * required by the precision or minimal field width */
4381 size_t number_of_zeros_to_pad = 0;
4382
4383 /* index into tmp where zero padding is to be inserted */
4384 size_t zero_padding_insertion_ind = 0;
4385
4386 /* current conversion specifier character */
4387 char fmt_spec = '\0';
4388
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004389 /* buffer for 's' and 'S' specs */
4390 char_u *tofree = NULL;
4391
4392
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004393 p++; /* skip '%' */
4394
4395 /* parse flags */
4396 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4397 || *p == '#' || *p == '\'')
4398 {
4399 switch (*p)
4400 {
4401 case '0': zero_padding = 1; break;
4402 case '-': justify_left = 1; break;
4403 case '+': force_sign = 1; space_for_positive = 0; break;
4404 case ' ': force_sign = 1;
4405 /* If both the ' ' and '+' flags appear, the ' '
4406 * flag should be ignored */
4407 break;
4408 case '#': alternate_form = 1; break;
4409 case '\'': break;
4410 }
4411 p++;
4412 }
4413 /* If the '0' and '-' flags both appear, the '0' flag should be
4414 * ignored. */
4415
4416 /* parse field width */
4417 if (*p == '*')
4418 {
4419 int j;
4420
4421 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004424 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425# endif
4426 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004427 if (j >= 0)
4428 min_field_width = j;
4429 else
4430 {
4431 min_field_width = -j;
4432 justify_left = 1;
4433 }
4434 }
4435 else if (VIM_ISDIGIT((int)(*p)))
4436 {
4437 /* size_t could be wider than unsigned int; make sure we treat
4438 * argument like common implementations do */
4439 unsigned int uj = *p++ - '0';
4440
4441 while (VIM_ISDIGIT((int)(*p)))
4442 uj = 10 * uj + (unsigned int)(*p++ - '0');
4443 min_field_width = uj;
4444 }
4445
4446 /* parse precision */
4447 if (*p == '.')
4448 {
4449 p++;
4450 precision_specified = 1;
4451 if (*p == '*')
4452 {
4453 int j;
4454
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004457 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458# endif
4459 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004460 p++;
4461 if (j >= 0)
4462 precision = j;
4463 else
4464 {
4465 precision_specified = 0;
4466 precision = 0;
4467 }
4468 }
4469 else if (VIM_ISDIGIT((int)(*p)))
4470 {
4471 /* size_t could be wider than unsigned int; make sure we
4472 * treat argument like common implementations do */
4473 unsigned int uj = *p++ - '0';
4474
4475 while (VIM_ISDIGIT((int)(*p)))
4476 uj = 10 * uj + (unsigned int)(*p++ - '0');
4477 precision = uj;
4478 }
4479 }
4480
4481 /* parse 'h', 'l' and 'll' length modifiers */
4482 if (*p == 'h' || *p == 'l')
4483 {
4484 length_modifier = *p;
4485 p++;
4486 if (length_modifier == 'l' && *p == 'l')
4487 {
4488 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004489# ifdef FEAT_NUM64
4490 length_modifier = 'L';
4491# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004493# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004494 p++;
4495 }
4496 }
4497 fmt_spec = *p;
4498
4499 /* common synonyms: */
4500 switch (fmt_spec)
4501 {
4502 case 'i': fmt_spec = 'd'; break;
4503 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4504 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4505 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4506 default: break;
4507 }
4508
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004509# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4510 switch (fmt_spec)
4511 {
4512 case 'd': case 'u': case 'o': case 'x': case 'X':
4513 if (tvs != NULL && length_modifier == '\0')
4514 length_modifier = 'L';
4515 }
4516# endif
4517
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518 /* get parameter value, do initial processing */
4519 switch (fmt_spec)
4520 {
4521 /* '%' and 'c' behave similar to 's' regarding flags and field
4522 * widths */
4523 case '%':
4524 case 'c':
4525 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004526 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 length_modifier = '\0';
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 Moolenaar92b8b2d2016-01-29 22:36:45 +01004573# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004574 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004575# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004576 /* memchr on HP does not like n > 2^31 !!! */
4577 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004578 precision <= (size_t)0x7fffffffL ? precision
4579 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004580# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004581 str_arg_l = (q == NULL) ? precision
4582 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004583 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004584# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004585 if (fmt_spec == 'S')
4586 {
4587 if (min_field_width != 0)
4588 min_field_width += STRLEN(str_arg)
4589 - mb_string2cells((char_u *)str_arg, -1);
4590 if (precision)
4591 {
4592 char_u *p1 = (char_u *)str_arg;
4593 size_t i;
4594
4595 for (i = 0; i < precision && *p1; i++)
4596 p1 += mb_ptr2len(p1);
4597
4598 str_arg_l = precision = p1 - (char_u *)str_arg;
4599 }
4600 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004601# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004602 break;
4603
4604 default:
4605 break;
4606 }
4607 break;
4608
Bram Moolenaar91984b92016-08-16 21:58:41 +02004609 case 'd': case 'u':
4610 case 'b': case 'B':
4611 case 'o':
4612 case 'x': case 'X':
4613 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004614 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004615 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004616 * imply the value is unsigned; d implies a signed
4617 * value */
4618
4619 /* 0 if numeric argument is zero (or if pointer is
4620 * NULL for 'p'), +1 if greater than zero (or nonzero
4621 * for unsigned arguments), -1 if negative (unsigned
4622 * argument is never negative) */
4623 int arg_sign = 0;
4624
4625 /* only defined for length modifier h, or for no
4626 * length modifiers */
4627 int int_arg = 0;
4628 unsigned int uint_arg = 0;
4629
4630 /* only defined for length modifier l */
4631 long int long_arg = 0;
4632 unsigned long int ulong_arg = 0;
4633
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004634# ifdef FEAT_NUM64
4635 /* only defined for length modifier ll */
4636 varnumber_T llong_arg = 0;
4637 uvarnumber_T ullong_arg = 0;
4638# endif
4639
Bram Moolenaare9997822016-08-28 16:03:38 +02004640 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004641 uvarnumber_T bin_arg = 0;
4642
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004643 /* pointer argument value -only defined for p
4644 * conversion */
4645 void *ptr_arg = NULL;
4646
4647 if (fmt_spec == 'p')
4648 {
4649 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004652 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4653 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654# endif
4655 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004656 if (ptr_arg != NULL)
4657 arg_sign = 1;
4658 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004659 else if (fmt_spec == 'b' || fmt_spec == 'B')
4660 {
4661 bin_arg =
4662# if defined(FEAT_EVAL)
4663 tvs != NULL ?
4664 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4665# endif
4666 va_arg(ap, uvarnumber_T);
4667 if (bin_arg != 0)
4668 arg_sign = 1;
4669 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004670 else if (fmt_spec == 'd')
4671 {
4672 /* signed */
4673 switch (length_modifier)
4674 {
4675 case '\0':
4676 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004677 /* char and short arguments are passed as int. */
4678 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004680 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004681# endif
4682 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004683 if (int_arg > 0)
4684 arg_sign = 1;
4685 else if (int_arg < 0)
4686 arg_sign = -1;
4687 break;
4688 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004689 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004691 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692# endif
4693 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004694 if (long_arg > 0)
4695 arg_sign = 1;
4696 else if (long_arg < 0)
4697 arg_sign = -1;
4698 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004699# ifdef FEAT_NUM64
4700 case 'L':
4701 llong_arg =
4702# if defined(FEAT_EVAL)
4703 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4704# endif
4705 va_arg(ap, varnumber_T);
4706 if (llong_arg > 0)
4707 arg_sign = 1;
4708 else if (llong_arg < 0)
4709 arg_sign = -1;
4710 break;
4711# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004712 }
4713 }
4714 else
4715 {
4716 /* unsigned */
4717 switch (length_modifier)
4718 {
4719 case '\0':
4720 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004721 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004722# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004723 tvs != NULL ? (unsigned)
4724 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725# endif
4726 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004727 if (uint_arg != 0)
4728 arg_sign = 1;
4729 break;
4730 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004731 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004732# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004733 tvs != NULL ? (unsigned long)
4734 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004735# endif
4736 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004737 if (ulong_arg != 0)
4738 arg_sign = 1;
4739 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004740# ifdef FEAT_NUM64
4741 case 'L':
4742 ullong_arg =
4743# if defined(FEAT_EVAL)
4744 tvs != NULL ? (uvarnumber_T)
4745 tv_nr(tvs, &arg_idx) :
4746# endif
4747 va_arg(ap, uvarnumber_T);
4748 if (ullong_arg != 0)
4749 arg_sign = 1;
4750 break;
4751# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004752 }
4753 }
4754
4755 str_arg = tmp;
4756 str_arg_l = 0;
4757
4758 /* NOTE:
4759 * For d, i, u, o, x, and X conversions, if precision is
4760 * specified, the '0' flag should be ignored. This is so
4761 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4762 * FreeBSD, NetBSD; but not with Perl.
4763 */
4764 if (precision_specified)
4765 zero_padding = 0;
4766 if (fmt_spec == 'd')
4767 {
4768 if (force_sign && arg_sign >= 0)
4769 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4770 /* leave negative numbers for sprintf to handle, to
4771 * avoid handling tricky cases like (short int)-32768 */
4772 }
4773 else if (alternate_form)
4774 {
4775 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004776 && (fmt_spec == 'b' || fmt_spec == 'B'
4777 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004778 {
4779 tmp[str_arg_l++] = '0';
4780 tmp[str_arg_l++] = fmt_spec;
4781 }
4782 /* alternate form should have no effect for p
4783 * conversion, but ... */
4784 }
4785
4786 zero_padding_insertion_ind = str_arg_l;
4787 if (!precision_specified)
4788 precision = 1; /* default precision is 1 */
4789 if (precision == 0 && arg_sign == 0)
4790 {
4791 /* When zero value is formatted with an explicit
4792 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004793 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004794 }
4795 else
4796 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004797 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004798 int f_l = 0;
4799
4800 /* construct a simple format string for sprintf */
4801 f[f_l++] = '%';
4802 if (!length_modifier)
4803 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004804 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004805 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004806# ifdef FEAT_NUM64
4807# ifdef WIN3264
4808 f[f_l++] = 'I';
4809 f[f_l++] = '6';
4810 f[f_l++] = '4';
4811# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004812 f[f_l++] = 'l';
4813 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004814# endif
4815# else
4816 f[f_l++] = 'l';
4817# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004818 }
4819 else
4820 f[f_l++] = length_modifier;
4821 f[f_l++] = fmt_spec;
4822 f[f_l++] = '\0';
4823
4824 if (fmt_spec == 'p')
4825 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004826 else if (fmt_spec == 'b' || fmt_spec == 'B')
4827 {
4828 char b[8 * sizeof(uvarnumber_T)];
4829 size_t b_l = 0;
4830 uvarnumber_T bn = bin_arg;
4831
4832 do
4833 {
4834 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4835 bn >>= 1;
4836 }
4837 while (bn != 0);
4838
4839 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4840 str_arg_l += b_l;
4841 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004842 else if (fmt_spec == 'd')
4843 {
4844 /* signed */
4845 switch (length_modifier)
4846 {
4847 case '\0':
4848 case 'h': str_arg_l += sprintf(
4849 tmp + str_arg_l, f, int_arg);
4850 break;
4851 case 'l': str_arg_l += sprintf(
4852 tmp + str_arg_l, f, long_arg);
4853 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004854# ifdef FEAT_NUM64
4855 case 'L': str_arg_l += sprintf(
4856 tmp + str_arg_l, f, llong_arg);
4857 break;
4858# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004859 }
4860 }
4861 else
4862 {
4863 /* unsigned */
4864 switch (length_modifier)
4865 {
4866 case '\0':
4867 case 'h': str_arg_l += sprintf(
4868 tmp + str_arg_l, f, uint_arg);
4869 break;
4870 case 'l': str_arg_l += sprintf(
4871 tmp + str_arg_l, f, ulong_arg);
4872 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004873# ifdef FEAT_NUM64
4874 case 'L': str_arg_l += sprintf(
4875 tmp + str_arg_l, f, ullong_arg);
4876 break;
4877# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004878 }
4879 }
4880
4881 /* include the optional minus sign and possible
4882 * "0x" in the region before the zero padding
4883 * insertion point */
4884 if (zero_padding_insertion_ind < str_arg_l
4885 && tmp[zero_padding_insertion_ind] == '-')
4886 zero_padding_insertion_ind++;
4887 if (zero_padding_insertion_ind + 1 < str_arg_l
4888 && tmp[zero_padding_insertion_ind] == '0'
4889 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4890 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4891 zero_padding_insertion_ind += 2;
4892 }
4893
4894 {
4895 size_t num_of_digits = str_arg_l
4896 - zero_padding_insertion_ind;
4897
4898 if (alternate_form && fmt_spec == 'o'
4899 /* unless zero is already the first
4900 * character */
4901 && !(zero_padding_insertion_ind < str_arg_l
4902 && tmp[zero_padding_insertion_ind] == '0'))
4903 {
4904 /* assure leading zero for alternate-form
4905 * octal numbers */
4906 if (!precision_specified
4907 || precision < num_of_digits + 1)
4908 {
4909 /* precision is increased to force the
4910 * first character to be zero, except if a
4911 * zero value is formatted with an
4912 * explicit precision of zero */
4913 precision = num_of_digits + 1;
4914 precision_specified = 1;
4915 }
4916 }
4917 /* zero padding to specified precision? */
4918 if (num_of_digits < precision)
4919 number_of_zeros_to_pad = precision - num_of_digits;
4920 }
4921 /* zero padding to specified minimal field width? */
4922 if (!justify_left && zero_padding)
4923 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004924 int n = (int)(min_field_width - (str_arg_l
4925 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004926 if (n > 0)
4927 number_of_zeros_to_pad += n;
4928 }
4929 break;
4930 }
4931
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004932# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004933 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004934 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004935 case 'e':
4936 case 'E':
4937 case 'g':
4938 case 'G':
4939 {
4940 /* Floating point. */
4941 double f;
4942 double abs_f;
4943 char format[40];
4944 int l;
4945 int remove_trailing_zeroes = FALSE;
4946
4947 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004948# if defined(FEAT_EVAL)
4949 tvs != NULL ? tv_float(tvs, &arg_idx) :
4950# endif
4951 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004952 abs_f = f < 0 ? -f : f;
4953
4954 if (fmt_spec == 'g' || fmt_spec == 'G')
4955 {
4956 /* Would be nice to use %g directly, but it prints
4957 * "1.0" as "1", we don't want that. */
4958 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4959 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004960 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004961 else
4962 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4963 remove_trailing_zeroes = TRUE;
4964 }
4965
Bram Moolenaar04186092016-08-29 21:55:35 +02004966 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004967# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004968 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004969# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004970 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004971# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004972 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004973 {
4974 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004975 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4976 force_sign, space_for_positive));
4977 str_arg_l = STRLEN(tmp);
4978 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004979 }
4980 else
4981 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004982 if (isnan(f))
4983 {
4984 /* Not a number: nan or NAN */
4985 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4986 : "nan");
4987 str_arg_l = 3;
4988 zero_padding = 0;
4989 }
4990 else if (isinf(f))
4991 {
4992 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4993 force_sign, space_for_positive));
4994 str_arg_l = STRLEN(tmp);
4995 zero_padding = 0;
4996 }
4997 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004998 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004999 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02005000 format[0] = '%';
5001 l = 1;
5002 if (force_sign)
5003 format[l++] = space_for_positive ? ' ' : '+';
5004 if (precision_specified)
5005 {
5006 size_t max_prec = TMP_LEN - 10;
5007
5008 /* Make sure we don't get more digits than we
5009 * have room for. */
5010 if ((fmt_spec == 'f' || fmt_spec == 'F')
5011 && abs_f > 1.0)
5012 max_prec -= (size_t)log10(abs_f);
5013 if (precision > max_prec)
5014 precision = max_prec;
5015 l += sprintf(format + l, ".%d", (int)precision);
5016 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02005017 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02005018 format[l + 1] = NUL;
5019
Bram Moolenaare9997822016-08-28 16:03:38 +02005020 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01005021 }
Bram Moolenaar99922372016-08-27 15:26:35 +02005022
Bram Moolenaara7241f52008-06-24 20:39:31 +00005023 if (remove_trailing_zeroes)
5024 {
5025 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005026 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005027
5028 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02005029 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005030 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005031 else
5032 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005033 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005034 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005035 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005036 {
5037 /* Remove superfluous '+' and leading
5038 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005039 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005040 {
5041 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005042 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005043 --str_arg_l;
5044 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005045 i = (tp[1] == '-') ? 2 : 1;
5046 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005047 {
5048 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005049 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005050 --str_arg_l;
5051 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005052 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005053 }
5054 }
5055
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005056 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005057 /* Remove trailing zeroes, but keep the one
5058 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005059 while (tp > tmp + 2 && *tp == '0'
5060 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00005061 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005062 STRMOVE(tp, tp + 1);
5063 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005064 --str_arg_l;
5065 }
5066 }
5067 else
5068 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005069 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00005070
5071 /* Be consistent: some printf("%e") use 1.0e+12
5072 * and some 1.0e+012. Remove one zero in the last
5073 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005074 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00005075 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005076 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
5077 && tp[2] == '0'
5078 && vim_isdigit(tp[3])
5079 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00005080 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005081 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00005082 --str_arg_l;
5083 }
5084 }
5085 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005086 if (zero_padding && min_field_width > str_arg_l
5087 && (tmp[0] == '-' || force_sign))
5088 {
5089 /* padding 0's should be inserted after the sign */
5090 number_of_zeros_to_pad = min_field_width - str_arg_l;
5091 zero_padding_insertion_ind = 1;
5092 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005093 str_arg = tmp;
5094 break;
5095 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005096# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005097
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005098 default:
5099 /* unrecognized conversion specifier, keep format string
5100 * as-is */
5101 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005102 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005104 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005105
5106 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005107 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005108 str_arg = p;
5109 str_arg_l = 0;
5110 if (*p != NUL)
5111 str_arg_l++; /* include invalid conversion specifier
5112 unchanged if not at end-of-string */
5113 break;
5114 }
5115
5116 if (*p != NUL)
5117 p++; /* step over the just processed conversion specifier */
5118
5119 /* insert padding to the left as requested by min_field_width;
5120 * this does not include the zero padding in case of numerical
5121 * conversions*/
5122 if (!justify_left)
5123 {
5124 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005125 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005126
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005127 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005128 {
5129 if (str_l < str_m)
5130 {
5131 size_t avail = str_m - str_l;
5132
5133 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005134 (size_t)pn > avail ? avail
5135 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005136 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005137 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005138 }
5139 }
5140
5141 /* zero padding as requested by the precision or by the minimal
5142 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005143 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005144 {
5145 /* will not copy first part of numeric right now, *
5146 * force it to be copied later in its entirety */
5147 zero_padding_insertion_ind = 0;
5148 }
5149 else
5150 {
5151 /* insert first part of numerics (sign or '0x') before zero
5152 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005153 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005154
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005155 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005156 {
5157 if (str_l < str_m)
5158 {
5159 size_t avail = str_m - str_l;
5160
5161 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005162 (size_t)zn > avail ? avail
5163 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005164 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005165 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005166 }
5167
5168 /* insert zero padding as requested by the precision or min
5169 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005170 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005171 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005172 {
5173 if (str_l < str_m)
5174 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005175 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005176
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005177 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005178 (size_t)zn > avail ? avail
5179 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005180 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005181 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005182 }
5183 }
5184
5185 /* insert formatted string
5186 * (or as-is conversion specifier for unknown conversions) */
5187 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005188 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005189
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005190 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005191 {
5192 if (str_l < str_m)
5193 {
5194 size_t avail = str_m - str_l;
5195
5196 mch_memmove(str + str_l,
5197 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005198 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005199 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005200 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005201 }
5202 }
5203
5204 /* insert right padding */
5205 if (justify_left)
5206 {
5207 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005208 int pn = (int)(min_field_width
5209 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005210
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005211 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005212 {
5213 if (str_l < str_m)
5214 {
5215 size_t avail = str_m - str_l;
5216
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005217 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005218 (size_t)pn > avail ? avail
5219 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005220 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005221 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005222 }
5223 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005224 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005225 }
5226 }
5227
5228 if (str_m > 0)
5229 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005230 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005231 * overwriting the last character (shouldn't happen, but just in case)
5232 * */
5233 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5234 }
5235
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005236 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005237 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005239 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005240 * character), that is, the number of characters that would have been
5241 * written to the buffer if it were large enough. */
5242 return (int)str_l;
5243}
5244
5245#endif /* PROTO */