blob: 6ec325eb4eec97b55692ab290aaf7c00ad454089 [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 int other_sourcing_name(void);
20static char_u *get_emsg_source(void);
21static char_u *get_emsg_lnum(void);
22static void add_msg_hist(char_u *s, int len, int attr);
23static void hit_return_msg(void);
24static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
29static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
30static void msg_scroll_up(void);
31static void inc_msg_scrolled(void);
32static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
33static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
34static void msg_puts_printf(char_u *str, int maxlen);
35static int do_more_prompt(int typed_char);
36static void msg_screen_putchar(int c, int attr);
37static int msg_check_screen(void);
38static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041static int confirm_msg_used = FALSE; /* displaying confirm_msg */
42static char_u *confirm_msg = NULL; /* ":confirm" message */
43static char_u *confirm_msg_tail; /* tail of confirm_msg */
44#endif
45
46struct msg_hist
47{
48 struct msg_hist *next;
49 char_u *msg;
50 int attr;
51};
52
53static struct msg_hist *first_msg_hist = NULL;
54static struct msg_hist *last_msg_hist = NULL;
55static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020057static FILE *verbose_fd = NULL;
58static int verbose_did_open = FALSE;
59
Bram Moolenaar071d4272004-06-13 20:20:40 +000060/*
61 * When writing messages to the screen, there are many different situations.
62 * A number of variables is used to remember the current state:
63 * msg_didany TRUE when messages were written since the last time the
64 * user reacted to a prompt.
65 * Reset: After hitting a key for the hit-return prompt,
66 * hitting <CR> for the command line or input().
67 * Set: When any message is written to the screen.
68 * msg_didout TRUE when something was written to the current line.
69 * Reset: When advancing to the next line, when the current
70 * text can be overwritten.
71 * Set: When any message is written to the screen.
72 * msg_nowait No extra delay for the last drawn message.
73 * Used in normal_cmd() before the mode message is drawn.
74 * emsg_on_display There was an error message recently. Indicates that there
75 * should be a delay before redrawing.
76 * msg_scroll The next message should not overwrite the current one.
77 * msg_scrolled How many lines the screen has been scrolled (because of
78 * messages). Used in update_screen() to scroll the screen
79 * back. Incremented each time the screen scrolls a line.
80 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
81 * writes something without scrolling should not make
82 * need_wait_return to be set. This is a hack to make ":ts"
83 * work without an extra prompt.
84 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010085 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 * need_wait_return TRUE when the hit-return prompt is needed.
87 * Reset: After giving the hit-return prompt, when the user
88 * has answered some other prompt.
89 * Set: When the ruler or typeahead display is overwritten,
90 * scrolling the screen for some message.
91 * keep_msg Message to be displayed after redrawing the screen, in
92 * main_loop().
93 * This is an allocated string or NULL when not used.
94 */
95
96/*
97 * msg(s) - displays the string 's' on the status line
98 * When terminal not initialized (yet) mch_errmsg(..) is used.
99 * return TRUE if wait_return not called
100 */
101 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100102msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103{
104 return msg_attr_keep(s, 0, FALSE);
105}
106
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000107#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000108 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000109/*
110 * Like msg() but keep it silent when 'verbosefile' is set.
111 */
112 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100113verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000114{
115 int n;
116
117 verbose_enter();
118 n = msg_attr_keep(s, 0, FALSE);
119 verbose_leave();
120
121 return n;
122}
123#endif
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100126msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
128 return msg_attr_keep(s, attr, FALSE);
129}
130
131 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100132msg_attr_keep(
133 char_u *s,
134 int attr,
135 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 static int entered = 0;
138 int retval;
139 char_u *buf = NULL;
140
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200141 /* Skip messages not matching ":filter pattern".
142 * Don't filter when there is an error. */
143 if (!emsg_on_display && message_filtered(s))
144 return TRUE;
145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef FEAT_EVAL
147 if (attr == 0)
148 set_vim_var_string(VV_STATUSMSG, s, -1);
149#endif
150
151 /*
152 * It is possible that displaying a messages causes a problem (e.g.,
153 * when redrawing the window), which causes another message, etc.. To
154 * break this loop, limit the recursiveness to 3 levels.
155 */
156 if (entered >= 3)
157 return TRUE;
158 ++entered;
159
160 /* Add message to history (unless it's a repeated kept message or a
161 * truncated message) */
162 if (s != keep_msg
163 || (*s != '<'
164 && last_msg_hist != NULL
165 && last_msg_hist->msg != NULL
166 && STRCMP(s, last_msg_hist->msg)))
167 add_msg_hist(s, -1, attr);
168
169 /* When displaying keep_msg, don't let msg_start() free it, caller must do
170 * that. */
171 if (s == keep_msg)
172 keep_msg = NULL;
173
174 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000175 msg_start();
176 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 if (buf != NULL)
178 s = buf;
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 msg_outtrans_attr(s, attr);
181 msg_clr_eos();
182 retval = msg_end();
183
184 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
185 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000186 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188 vim_free(buf);
189 --entered;
190 return retval;
191}
192
193/*
194 * Truncate a string such that it can be printed without causing a scroll.
195 * Returns an allocated string or NULL when no truncating is done.
196 */
197 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100198msg_strtrunc(
199 char_u *s,
200 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 char_u *buf = NULL;
203 int len;
204 int room;
205
206 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000207 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
208 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000211 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000212 /* Use all the columns. */
213 room = (int)(Rows - msg_row) * Columns - 1;
214 else
215 /* Use up to 'showcmd' column. */
216 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 if (len > room && room > 0)
218 {
219#ifdef FEAT_MBYTE
220 if (enc_utf8)
221 /* may have up to 18 bytes per cell (6 per char, up to two
222 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 else if (enc_dbcs == DBCS_JPNU)
225 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 else
228#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = room + 2;
230 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234 }
235 return buf;
236}
237
238/*
239 * Truncate a string "s" to "buf" with cell width "room".
240 * "s" and "buf" may be equal.
241 */
242 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100243trunc_string(
244 char_u *s,
245 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200246 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100247 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200249 size_t room = room_in - 3; /* "..." takes 3 chars */
250 size_t half;
251 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 int e;
253 int i;
254 int n;
255
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200256 if (room_in < 3)
257 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100261 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 if (s[e] == NUL)
264 {
265 /* text fits without truncating! */
266 buf[e] = NUL;
267 return;
268 }
269 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200270 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 break;
272 len += n;
273 buf[e] = s[e];
274#ifdef FEAT_MBYTE
275 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000276 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100278 if (++e == buflen)
279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 buf[e] = s[e];
281 }
282#endif
283 }
284
285 /* Last part: End of the string. */
286 i = e;
287#ifdef FEAT_MBYTE
288 if (enc_dbcs != 0)
289 {
290 /* For DBCS going backwards in a string is slow, but
291 * computing the cell width isn't too slow: go forward
292 * until the rest fits. */
293 n = vim_strsize(s + i);
294 while (len + n > room)
295 {
296 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000297 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 }
299 }
300 else if (enc_utf8)
301 {
302 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000303 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 for (;;)
305 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000306 do
307 half = half - (*mb_head_off)(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200308 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200310 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 break;
312 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200313 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 }
315 }
316 else
317#endif
318 {
319 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
320 len += n;
321 }
322
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200323
324 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100325 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200326 /* text fits without truncating */
327 if (s != buf)
328 {
329 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200330 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200331 len = buflen - 1;
332 len = len - e + 1;
333 if (len < 1)
334 buf[e - 1] = NUL;
335 else
336 mch_memmove(buf + e, s + e, len);
337 }
338 }
339 else if (e + 3 < buflen)
340 {
341 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100342 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200343 len = STRLEN(s + i) + 1;
344 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 len = buflen - e - 3 - 1;
346 mch_memmove(buf + e + 3, s + i, len);
347 buf[e + 3 + len - 1] = NUL;
348 }
349 else
350 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200351 /* can't fit in the "...", just truncate it */
352 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354}
355
356/*
357 * Automatic prototype generation does not understand this function.
358 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
359 * shorter than IOSIZE!!!
360 */
361#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000363int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100366# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369smsg(char_u *s, ...)
370{
371 va_list arglist;
372
373 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000374 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 va_end(arglist);
376 return msg(IObuff);
377}
378
379 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100380# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383smsg_attr(int attr, char_u *s, ...)
384{
385 va_list arglist;
386
387 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000388 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 va_end(arglist);
390 return msg_attr(IObuff, attr);
391}
392
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394
395/*
396 * Remember the last sourcing name/lnum used in an error message, so that it
397 * isn't printed each time when it didn't change.
398 */
399static int last_sourcing_lnum = 0;
400static char_u *last_sourcing_name = NULL;
401
402/*
403 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
404 * for the next error message;
405 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000406 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100407reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
409 vim_free(last_sourcing_name);
410 last_sourcing_name = NULL;
411 last_sourcing_lnum = 0;
412}
413
414/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000415 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
416 */
417 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100418other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419{
420 if (sourcing_name != NULL)
421 {
422 if (last_sourcing_name != NULL)
423 return STRCMP(sourcing_name, last_sourcing_name) != 0;
424 return TRUE;
425 }
426 return FALSE;
427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Get the message about the source, as used for an error message.
431 * Returns an allocated string with room for one more character.
432 * Returns NULL when no message is to be given.
433 */
434 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100435get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 char_u *Buf, *p;
438
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000439 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
441 p = (char_u *)_("Error detected while processing %s:");
442 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
443 if (Buf != NULL)
444 sprintf((char *)Buf, (char *)p, sourcing_name);
445 return Buf;
446 }
447 return NULL;
448}
449
450/*
451 * Get the message about the source lnum, as used for an error message.
452 * Returns an allocated string with room for one more character.
453 * Returns NULL when no message is to be given.
454 */
455 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100456get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 char_u *Buf, *p;
459
460 /* lnum is 0 when executing a command from the command line
461 * argument, we don't want a line number then */
462 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000463 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 && sourcing_lnum != 0)
465 {
466 p = (char_u *)_("line %4ld:");
467 Buf = alloc((unsigned)(STRLEN(p) + 20));
468 if (Buf != NULL)
469 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
470 return Buf;
471 }
472 return NULL;
473}
474
475/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000476 * Display name and line number for the source of an error.
477 * Remember the file name and line number, so that for the next error the info
478 * is only displayed if it changed.
479 */
480 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100481msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000482{
483 char_u *p;
484
485 ++no_wait_return;
486 p = get_emsg_source();
487 if (p != NULL)
488 {
489 msg_attr(p, attr);
490 vim_free(p);
491 }
492 p = get_emsg_lnum();
493 if (p != NULL)
494 {
495 msg_attr(p, hl_attr(HLF_N));
496 vim_free(p);
497 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
498 }
499
500 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000501 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000502 {
503 vim_free(last_sourcing_name);
504 if (sourcing_name == NULL)
505 last_sourcing_name = NULL;
506 else
507 last_sourcing_name = vim_strsave(sourcing_name);
508 }
509 --no_wait_return;
510}
511
512/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000513 * Return TRUE if not giving error messages right now:
514 * If "emsg_off" is set: no error messages at the moment.
515 * If "msg" is in 'debug': do error message but without side effects.
516 * If "emsg_skip" is set: never do error messages.
517 */
518 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100519emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000520{
521 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
522 && vim_strchr(p_debug, 't') == NULL)
523#ifdef FEAT_EVAL
524 || emsg_skip > 0
525#endif
526 )
527 return TRUE;
528 return FALSE;
529}
530
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200531#if !defined(HAVE_STRERROR) || defined(PROTO)
532/*
533 * Replacement for perror() that behaves more or less like emsg() was called.
534 * v:errmsg will be set and called_emsg will be set.
535 */
536 void
537do_perror(char *msg)
538{
539 perror(msg);
540 ++emsg_silent;
541 emsg((char_u *)msg);
542 --emsg_silent;
543}
544#endif
545
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * emsg() - display an error message
548 *
549 * Rings the bell, if appropriate, and calls message() to do the real work
550 * When terminal not initialized (yet) mch_errmsg(..) is used.
551 *
552 * return TRUE if wait_return not called
553 */
554 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100555emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 char_u *p;
559#ifdef FEAT_EVAL
560 int ignore = FALSE;
561 int severe;
562#endif
563
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100564 /* Skip this if not giving error messages at the moment. */
565 if (emsg_not_now())
566 return TRUE;
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 called_emsg = TRUE;
Bram Moolenaar8b778d52016-02-18 20:31:34 +0100569 if (emsg_silent == 0)
570 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000573 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
574 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 */
576#ifdef FEAT_EVAL
577 severe = emsg_severe;
578 emsg_severe = FALSE;
579#endif
580
Bram Moolenaar57657d82006-04-21 22:12:41 +0000581 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583#ifdef FEAT_EVAL
584 /*
585 * Cause a throw of an error exception if appropriate. Don't display
586 * the error message in this case. (If no matching catch clause will
587 * be found, the message will be displayed later on.) "ignore" is set
588 * when the message should be ignored completely (used for the
589 * interrupt message).
590 */
591 if (cause_errthrow(s, severe, &ignore) == TRUE)
592 {
593 if (!ignore)
594 did_emsg = TRUE;
595 return TRUE;
596 }
597
598 /* set "v:errmsg", also when using ":silent! cmd" */
599 set_vim_var_string(VV_ERRMSG, s, -1);
600#endif
601
602 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000603 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * But do write it to the redirection file.
605 */
606 if (emsg_silent != 0)
607 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200608 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200610 msg_start();
611 p = get_emsg_source();
612 if (p != NULL)
613 {
614 STRCAT(p, "\n");
615 redir_write(p, -1);
616 vim_free(p);
617 }
618 p = get_emsg_lnum();
619 if (p != NULL)
620 {
621 STRCAT(p, "\n");
622 redir_write(p, -1);
623 vim_free(p);
624 }
625 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 return TRUE;
628 }
629
630 /* Reset msg_silent, an error causes messages to be switched back on. */
631 msg_silent = 0;
632 cmd_silent = FALSE;
633
634 if (global_busy) /* break :global command */
635 ++global_busy;
636
637 if (p_eb)
638 beep_flush(); /* also includes flush_buffers() */
639 else
640 flush_buffers(FALSE); /* flush internal buffers */
641 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643
644 emsg_on_display = TRUE; /* remember there is an error message */
645 ++msg_scroll; /* don't overwrite a previous message */
646 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000647 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 need_wait_return = TRUE; /* needed in case emsg() is called after
649 * wait_return has reset need_wait_return
650 * and a redraw is expected because
651 * msg_scrolled is non-zero */
652
653 /*
654 * Display name and line number for the source of the error.
655 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000656 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
658 /*
659 * Display the error message itself.
660 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000661 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 return msg_attr(s, attr);
663}
664
665/*
666 * Print an error message with one "%s" and one string argument.
667 */
668 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100669emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 return emsg3(s, a1, NULL);
672}
673
Bram Moolenaarea424162005-06-16 21:51:00 +0000674/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100677emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678{
679 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
680}
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682/*
683 * Like msg(), but truncate to a single line if p_shm contains 't', or when
684 * "force" is TRUE. This truncates in another way as for normal messages.
685 * Careful: The string may be changed by msg_may_trunc()!
686 * Returns a pointer to the printed message, if wait_return() not called.
687 */
688 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100689msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
691 int n;
692
693 /* Add message to history before truncating */
694 add_msg_hist(s, -1, attr);
695
696 s = msg_may_trunc(force, s);
697
698 msg_hist_off = TRUE;
699 n = msg_attr(s, attr);
700 msg_hist_off = FALSE;
701
702 if (n)
703 return s;
704 return NULL;
705}
706
707/*
708 * Check if message "s" should be truncated at the start (for filenames).
709 * Return a pointer to where the truncated message starts.
710 * Note: May change the message by replacing a character with '<'.
711 */
712 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100713msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 int n;
716 int room;
717
718 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
719 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
720 && (n = (int)STRLEN(s) - room) > 0)
721 {
722#ifdef FEAT_MBYTE
723 if (has_mbyte)
724 {
725 int size = vim_strsize(s);
726
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000727 /* There may be room anyway when there are multibyte chars. */
728 if (size <= room)
729 return s;
730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 for (n = 0; size >= room; )
732 {
733 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000734 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 }
736 --n;
737 }
738#endif
739 s += n;
740 *s = '<';
741 }
742 return s;
743}
744
745 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100746add_msg_hist(
747 char_u *s,
748 int len, /* -1 for undetermined length */
749 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 struct msg_hist *p;
752
753 if (msg_hist_off || msg_silent != 0)
754 return;
755
756 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000757 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000758 (void)delete_first_msg();
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 /* allocate an entry and add the message at the end of the history */
761 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
762 if (p != NULL)
763 {
764 if (len < 0)
765 len = (int)STRLEN(s);
766 /* remove leading and trailing newlines */
767 while (len > 0 && *s == '\n')
768 {
769 ++s;
770 --len;
771 }
772 while (len > 0 && s[len - 1] == '\n')
773 --len;
774 p->msg = vim_strnsave(s, len);
775 p->next = NULL;
776 p->attr = attr;
777 if (last_msg_hist != NULL)
778 last_msg_hist->next = p;
779 last_msg_hist = p;
780 if (first_msg_hist == NULL)
781 first_msg_hist = last_msg_hist;
782 ++msg_hist_len;
783 }
784}
785
786/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000787 * Delete the first (oldest) message from the history.
788 * Returns FAIL if there are no messages.
789 */
790 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100791delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000792{
793 struct msg_hist *p;
794
795 if (msg_hist_len <= 0)
796 return FAIL;
797 p = first_msg_hist;
798 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000799 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200800 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000801 vim_free(p->msg);
802 vim_free(p);
803 --msg_hist_len;
804 return OK;
805}
806
807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 * ":messages" command.
809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200811ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 struct msg_hist *p;
814 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200815 int c = 0;
816
817 if (STRCMP(eap->arg, "clear") == 0)
818 {
819 int keep = eap->addr_count == 0 ? 0 : eap->line2;
820
821 while (msg_hist_len > keep)
822 (void)delete_first_msg();
823 return;
824 }
825
826 if (*eap->arg != NUL)
827 {
828 EMSG(_(e_invarg));
829 return;
830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832 msg_hist_off = TRUE;
833
Bram Moolenaar451f8492016-04-14 17:16:22 +0200834 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200835 if (eap->addr_count != 0)
836 {
837 /* Count total messages */
838 for (; p != NULL && !got_int; p = p->next)
839 c++;
840
841 c -= eap->line2;
842
843 /* Skip without number of messages specified */
844 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
845 p = p->next, c--);
846 }
847
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200848 if (p == first_msg_hist)
849 {
850 s = mch_getenv((char_u *)"LANG");
851 if (s != NULL && *s != NUL)
852 msg_attr((char_u *)
853 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
854 hl_attr(HLF_T));
855 }
856
Bram Moolenaar451f8492016-04-14 17:16:22 +0200857 /* Display what was not skipped. */
858 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (p->msg != NULL)
860 msg_attr(p->msg, p->attr);
861
862 msg_hist_off = FALSE;
863}
864
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000865#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866/*
867 * Call this after prompting the user. This will avoid a hit-return message
868 * and a delay.
869 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000870 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100871msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 need_wait_return = FALSE;
874 emsg_on_display = FALSE;
875 cmdline_row = msg_row;
876 msg_col = 0;
877 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100878 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879}
880#endif
881
882/*
883 * wait for the user to hit a key (normally a return)
884 * if 'redraw' is TRUE, clear and redraw the screen
885 * if 'redraw' is FALSE, just redraw the screen
886 * if 'redraw' is -1, don't redraw at all
887 */
888 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100889wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 int c;
892 int oldState;
893 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100895 int save_Recording;
896 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
898 if (redraw == TRUE)
899 must_redraw = CLEAR;
900
901 /* If using ":silent cmd", don't wait for a return. Also don't set
902 * need_wait_return to do it later. */
903 if (msg_silent != 0)
904 return;
905
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100906 /*
907 * When inside vgetc(), we can't wait for a typed character at all.
908 * With the global command (and some others) we only need one return at
909 * the end. Adjust cmdline_row to avoid the next message overwriting the
910 * last one.
911 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000912 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100914 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (no_wait_return)
916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (!exmode_active)
918 cmdline_row = msg_row;
919 return;
920 }
921
922 redir_off = TRUE; /* don't redirect this message */
923 oldState = State;
924 if (quit_more)
925 {
926 c = CAR; /* just pretend CR was hit */
927 quit_more = FALSE;
928 got_int = FALSE;
929 }
930 else if (exmode_active)
931 {
932 MSG_PUTS(" "); /* make sure the cursor is on the right line */
933 c = CAR; /* no need for a return in ex mode */
934 got_int = FALSE;
935 }
936 else
937 {
938 /* Make sure the hit-return prompt is on screen when 'guioptions' was
939 * just changed. */
940 screenalloc(FALSE);
941
942 State = HITRETURN;
943#ifdef FEAT_MOUSE
944 setmouse();
945#endif
946#ifdef USE_ON_FLY_SCROLL
947 dont_scroll = TRUE; /* disallow scrolling here */
948#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100949 cmdline_row = msg_row;
950
Bram Moolenaarec38d692013-04-24 15:12:32 +0200951 /* Avoid the sequence that the user types ":" at the hit-return prompt
952 * to start an Ex command, but the file-changed dialog gets in the
953 * way. */
954 if (need_check_timestamps)
955 check_timestamps(FALSE);
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 hit_return_msg();
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 do
960 {
961 /* Remember "got_int", if it is set vgetc() probably returns a
962 * CTRL-C, but we need to loop then. */
963 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000964
965 /* Don't do mappings here, we put the character back in the
966 * typeahead buffer. */
967 ++no_mapping;
968 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100969
970 /* Temporarily disable Recording. If Recording is active, the
971 * character will be recorded later, since it will be added to the
972 * typebuf after the loop */
973 save_Recording = Recording;
974 save_scriptout = scriptout;
975 Recording = FALSE;
976 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000978 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000980 --no_mapping;
981 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100982 Recording = save_Recording;
983 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef FEAT_CLIPBOARD
986 /* Strange way to allow copying (yanking) a modeless selection at
987 * the hit-enter prompt. Use CTRL-Y, because the same is used in
988 * Cmdline-mode and it's harmless when there is no selection. */
989 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
990 {
991 clip_copy_modeless_selection(TRUE);
992 c = K_IGNORE;
993 }
994#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000995
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000996 /*
997 * Allow scrolling back in the messages.
998 * Also accept scroll-down commands when messages fill the screen,
999 * to avoid that typing one 'j' too many makes the messages
1000 * disappear.
1001 */
1002 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001003 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001004 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1005 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001006 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001007 if (msg_scrolled > Rows)
1008 /* scroll back to show older messages */
1009 do_more_prompt(c);
1010 else
1011 {
1012 msg_didout = FALSE;
1013 c = K_IGNORE;
1014 msg_col =
1015#ifdef FEAT_RIGHTLEFT
1016 cmdmsg_rl ? Columns - 1 :
1017#endif
1018 0;
1019 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001020 if (quit_more)
1021 {
1022 c = CAR; /* just pretend CR was hit */
1023 quit_more = FALSE;
1024 got_int = FALSE;
1025 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001026 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001027 {
1028 c = K_IGNORE;
1029 hit_return_msg();
1030 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001031 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001032 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001033 && (c == 'j' || c == 'd' || c == 'f'
1034 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001035 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 } while ((had_got_int && c == Ctrl_C)
1038 || c == K_IGNORE
1039#ifdef FEAT_GUI
1040 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1041#endif
1042#ifdef FEAT_MOUSE
1043 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1044 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1045 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001046 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 || c == K_MOUSEDOWN || c == K_MOUSEUP
1048 || (!mouse_has(MOUSE_RETURN)
1049 && mouse_row < msg_row
1050 && (c == K_LEFTMOUSE
1051 || c == K_MIDDLEMOUSE
1052 || c == K_RIGHTMOUSE
1053 || c == K_X1MOUSE
1054 || c == K_X2MOUSE))
1055#endif
1056 );
1057 ui_breakcheck();
1058#ifdef FEAT_MOUSE
1059 /*
1060 * Avoid that the mouse-up event causes visual mode to start.
1061 */
1062 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1063 || c == K_X1MOUSE || c == K_X2MOUSE)
1064 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1065 else
1066#endif
1067 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1068 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001069 /* Put the character back in the typeahead buffer. Don't use the
1070 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001071 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001073 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 }
1076 redir_off = FALSE;
1077
1078 /*
1079 * If the user hits ':', '?' or '/' we get a command line from the next
1080 * line.
1081 */
1082 if (c == ':' || c == '?' || c == '/')
1083 {
1084 if (!exmode_active)
1085 cmdline_row = msg_row;
1086 skip_redraw = TRUE; /* skip redraw once */
1087 do_redraw = FALSE;
1088 }
1089
1090 /*
1091 * If the window size changed set_shellsize() will redraw the screen.
1092 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1093 * typed.
1094 */
1095 tmpState = State;
1096 State = oldState; /* restore State before set_shellsize */
1097#ifdef FEAT_MOUSE
1098 setmouse();
1099#endif
1100 msg_check();
1101
1102#if defined(UNIX) || defined(VMS)
1103 /*
1104 * When switching screens, we need to output an extra newline on exit.
1105 */
1106 if (swapping_screen() && !termcap_active)
1107 newline_on_exit = TRUE;
1108#endif
1109
1110 need_wait_return = FALSE;
1111 did_wait_return = TRUE;
1112 emsg_on_display = FALSE; /* can delete error message now */
1113 lines_left = -1; /* reset lines_left at next msg_start() */
1114 reset_last_sourcing();
1115 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1116 (Rows - cmdline_row - 1) * Columns + sc_col)
1117 {
1118 vim_free(keep_msg);
1119 keep_msg = NULL; /* don't redisplay message, it's too long */
1120 }
1121
1122 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1123 {
1124 starttermcap(); /* start termcap before redrawing */
1125 shell_resized();
1126 }
1127 else if (!skip_redraw
1128 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1129 {
1130 starttermcap(); /* start termcap before redrawing */
1131 redraw_later(VALID);
1132 }
1133}
1134
1135/*
1136 * Write the hit-return prompt.
1137 */
1138 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001139hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001141 int save_p_more = p_more;
1142
1143 p_more = FALSE; /* don't want see this message when scrolling back */
1144 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 msg_putchar('\n');
1146 if (got_int)
1147 MSG_PUTS(_("Interrupt: "));
1148
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001149 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (!msg_use_printf())
1151 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001152 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153}
1154
1155/*
1156 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1157 */
1158 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001159set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160{
1161 vim_free(keep_msg);
1162 if (s != NULL && msg_silent == 0)
1163 keep_msg = vim_strsave(s);
1164 else
1165 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001166 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001167 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168}
1169
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001170#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1171/*
1172 * If there currently is a message being displayed, set "keep_msg" to it, so
1173 * that it will be displayed again after redraw.
1174 */
1175 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001176set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001177{
1178 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1179 && (State & NORMAL))
1180 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1181}
1182#endif
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184/*
1185 * Prepare for outputting characters in the command line.
1186 */
1187 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001188msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
1190 int did_return = FALSE;
1191
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001192 if (!msg_silent)
1193 {
1194 vim_free(keep_msg);
1195 keep_msg = NULL; /* don't display old message now */
1196 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001197
1198#ifdef FEAT_EVAL
1199 if (need_clr_eos)
1200 {
1201 /* Halfway an ":echo" command and getting an (error) message: clear
1202 * any text from the command. */
1203 need_clr_eos = FALSE;
1204 msg_clr_eos();
1205 }
1206#endif
1207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 if (!msg_scroll && full_screen) /* overwrite last message */
1209 {
1210 msg_row = cmdline_row;
1211 msg_col =
1212#ifdef FEAT_RIGHTLEFT
1213 cmdmsg_rl ? Columns - 1 :
1214#endif
1215 0;
1216 }
1217 else if (msg_didout) /* start message on next line */
1218 {
1219 msg_putchar('\n');
1220 did_return = TRUE;
1221 if (exmode_active != EXMODE_NORMAL)
1222 cmdline_row = msg_row;
1223 }
1224 if (!msg_didany || lines_left < 0)
1225 msg_starthere();
1226 if (msg_silent == 0)
1227 {
1228 msg_didout = FALSE; /* no output on current line yet */
1229 cursor_off();
1230 }
1231
1232 /* when redirecting, may need to start a new line. */
1233 if (!did_return)
1234 redir_write((char_u *)"\n", -1);
1235}
1236
1237/*
1238 * Note that the current msg position is where messages start.
1239 */
1240 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001241msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 lines_left = cmdline_row;
1244 msg_didany = FALSE;
1245}
1246
1247 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001248msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 msg_putchar_attr(c, 0);
1251}
1252
1253 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001254msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
1256#ifdef FEAT_MBYTE
1257 char_u buf[MB_MAXBYTES + 1];
1258#else
1259 char_u buf[4];
1260#endif
1261
1262 if (IS_SPECIAL(c))
1263 {
1264 buf[0] = K_SPECIAL;
1265 buf[1] = K_SECOND(c);
1266 buf[2] = K_THIRD(c);
1267 buf[3] = NUL;
1268 }
1269 else
1270 {
1271#ifdef FEAT_MBYTE
1272 buf[(*mb_char2bytes)(c, buf)] = NUL;
1273#else
1274 buf[0] = c;
1275 buf[1] = NUL;
1276#endif
1277 }
1278 msg_puts_attr(buf, attr);
1279}
1280
1281 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001282msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 char_u buf[20];
1285
1286 sprintf((char *)buf, "%ld", n);
1287 msg_puts(buf);
1288}
1289
1290 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001291msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 msg_home_replace_attr(fname, 0);
1294}
1295
1296#if defined(FEAT_FIND_ID) || defined(PROTO)
1297 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001298msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 msg_home_replace_attr(fname, hl_attr(HLF_D));
1301}
1302#endif
1303
1304 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001305msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 char_u *name;
1308
1309 name = home_replace_save(NULL, fname);
1310 if (name != NULL)
1311 msg_outtrans_attr(name, attr);
1312 vim_free(name);
1313}
1314
1315/*
1316 * Output 'len' characters in 'str' (including NULs) with translation
1317 * if 'len' is -1, output upto a NUL character.
1318 * Use attributes 'attr'.
1319 * Return the number of characters it takes on the screen.
1320 */
1321 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001322msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 return msg_outtrans_attr(str, 0);
1325}
1326
1327 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001328msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1331}
1332
1333 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001334msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 return msg_outtrans_len_attr(str, len, 0);
1337}
1338
1339/*
1340 * Output one character at "p". Return pointer to the next character.
1341 * Handles multi-byte characters.
1342 */
1343 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001344msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346#ifdef FEAT_MBYTE
1347 int l;
1348
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001349 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 msg_outtrans_len_attr(p, l, attr);
1352 return p + l;
1353 }
1354#endif
1355 msg_puts_attr(transchar_byte(*p), attr);
1356 return p + 1;
1357}
1358
1359 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001360msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 int retval = 0;
1363 char_u *str = msgstr;
1364 char_u *plain_start = msgstr;
1365 char_u *s;
1366#ifdef FEAT_MBYTE
1367 int mb_l;
1368 int c;
1369#endif
1370
1371 /* if MSG_HIST flag set, add message to history */
1372 if (attr & MSG_HIST)
1373 {
1374 add_msg_hist(str, len, attr);
1375 attr &= ~MSG_HIST;
1376 }
1377
1378#ifdef FEAT_MBYTE
1379 /* If the string starts with a composing character first draw a space on
1380 * which the composing char can be drawn. */
1381 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1382 msg_puts_attr((char_u *)" ", attr);
1383#endif
1384
1385 /*
1386 * Go over the string. Special characters are translated and printed.
1387 * Normal characters are printed several at a time.
1388 */
1389 while (--len >= 0)
1390 {
1391#ifdef FEAT_MBYTE
1392 if (enc_utf8)
1393 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001394 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001396 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 else
1398 mb_l = 1;
1399 if (has_mbyte && mb_l > 1)
1400 {
1401 c = (*mb_ptr2char)(str);
1402 if (vim_isprintc(c))
1403 /* printable multi-byte char: count the cells. */
1404 retval += (*mb_ptr2cells)(str);
1405 else
1406 {
1407 /* unprintable multi-byte char: print the printable chars so
1408 * far and the translation of the unprintable char. */
1409 if (str > plain_start)
1410 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1411 attr);
1412 plain_start = str + mb_l;
1413 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1414 retval += char2cells(c);
1415 }
1416 len -= mb_l - 1;
1417 str += mb_l;
1418 }
1419 else
1420#endif
1421 {
1422 s = transchar_byte(*str);
1423 if (s[1] != NUL)
1424 {
1425 /* unprintable char: print the printable chars so far and the
1426 * translation of the unprintable char. */
1427 if (str > plain_start)
1428 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1429 attr);
1430 plain_start = str + 1;
1431 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001432 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001434 else
1435 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ++str;
1437 }
1438 }
1439
1440 if (str > plain_start)
1441 /* print the printable chars at the end */
1442 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1443
1444 return retval;
1445}
1446
1447#if defined(FEAT_QUICKFIX) || defined(PROTO)
1448 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001449msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 int i;
1452 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1453
1454 arg = skipwhite(arg);
1455 for (i = 5; *arg && i >= 0; --i)
1456 if (*arg++ != str[i])
1457 break;
1458 if (i < 0)
1459 {
1460 msg_putchar('\n');
1461 for (i = 0; rs[i]; ++i)
1462 msg_putchar(rs[i] - 3);
1463 }
1464}
1465#endif
1466
1467/*
1468 * Output the string 'str' upto a NUL character.
1469 * Return the number of characters it takes on the screen.
1470 *
1471 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1472 * following character and shown as <F1>, <S-Up> etc. Any other character
1473 * which is not printable shown in <> form.
1474 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1475 * If a character is displayed in one of these special ways, is also
1476 * highlighted (its highlight name is '8' in the p_hl variable).
1477 * Otherwise characters are not highlighted.
1478 * This function is used to show mappings, where we want to see how to type
1479 * the character/string -- webb
1480 */
1481 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001482msg_outtrans_special(
1483 char_u *strstart,
1484 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486 char_u *str = strstart;
1487 int retval = 0;
1488 char_u *string;
1489 int attr;
1490 int len;
1491
1492 attr = hl_attr(HLF_8);
1493 while (*str != NUL)
1494 {
1495 /* Leading and trailing spaces need to be displayed in <> form. */
1496 if ((str == strstart || str[1] == NUL) && *str == ' ')
1497 {
1498 string = (char_u *)"<Space>";
1499 ++str;
1500 }
1501 else
1502 string = str2special(&str, from);
1503 len = vim_strsize(string);
1504 /* Highlight special keys */
1505 msg_puts_attr(string, len > 1
1506#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001507 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#endif
1509 ? attr : 0);
1510 retval += len;
1511 }
1512 return retval;
1513}
1514
Bram Moolenaarbd743252010-10-20 21:23:33 +02001515#if defined(FEAT_EVAL) || defined(PROTO)
1516/*
1517 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1518 * strings, in an allocated string.
1519 */
1520 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001521str2special_save(
1522 char_u *str,
1523 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001524{
1525 garray_T ga;
1526 char_u *p = str;
1527
1528 ga_init2(&ga, 1, 40);
1529 while (*p != NUL)
1530 ga_concat(&ga, str2special(&p, is_lhs));
1531 ga_append(&ga, NUL);
1532 return (char_u *)ga.ga_data;
1533}
1534#endif
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536/*
1537 * Return the printable string for the key codes at "*sp".
1538 * Used for translating the lhs or rhs of a mapping to printable chars.
1539 * Advances "sp" to the next code.
1540 */
1541 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001542str2special(
1543 char_u **sp,
1544 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 int c;
1547 static char_u buf[7];
1548 char_u *str = *sp;
1549 int modifiers = 0;
1550 int special = FALSE;
1551
1552#ifdef FEAT_MBYTE
1553 if (has_mbyte)
1554 {
1555 char_u *p;
1556
1557 /* Try to un-escape a multi-byte character. Return the un-escaped
1558 * string if it is a multi-byte character. */
1559 p = mb_unescape(sp);
1560 if (p != NULL)
1561 return p;
1562 }
1563#endif
1564
1565 c = *str;
1566 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1567 {
1568 if (str[1] == KS_MODIFIER)
1569 {
1570 modifiers = str[2];
1571 str += 3;
1572 c = *str;
1573 }
1574 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1575 {
1576 c = TO_SPECIAL(str[1], str[2]);
1577 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001578 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 c = NUL;
1580 }
1581 if (IS_SPECIAL(c) || modifiers) /* special key */
1582 special = TRUE;
1583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001586 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001588 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001589
1590 /* For multi-byte characters check for an illegal byte. */
1591 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1592 {
1593 transchar_nonprint(buf, c);
1594 *sp = str + 1;
1595 return buf;
1596 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001597 /* Since 'special' is TRUE the multi-byte character 'c' will be
1598 * processed by get_special_key_name() */
1599 c = (*mb_ptr2char)(str);
1600 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001604 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1607 * Use <Space> only for lhs of a mapping. */
1608 if (special || char2cells(c) > 1 || (from && c == ' '))
1609 return get_special_key_name(c, modifiers);
1610 buf[0] = c;
1611 buf[1] = NUL;
1612 return buf;
1613}
1614
1615/*
1616 * Translate a key sequence into special key names.
1617 */
1618 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001619str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 char_u *s;
1622
1623 *buf = NUL;
1624 while (*sp)
1625 {
1626 s = str2special(&sp, FALSE);
1627 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1628 STRCAT(buf, s);
1629 }
1630}
1631
1632/*
1633 * print line for :print or :list command
1634 */
1635 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001636msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 int c;
1639 int col = 0;
1640 int n_extra = 0;
1641 int c_extra = 0;
1642 char_u *p_extra = NULL; /* init to make SASC shut up */
1643 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001644 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 char_u *trail = NULL;
1646#ifdef FEAT_MBYTE
1647 int l;
1648 char_u buf[MB_MAXBYTES + 1];
1649#endif
1650
Bram Moolenaardf177f62005-02-22 08:39:57 +00001651 if (curwin->w_p_list)
1652 list = TRUE;
1653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001655 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 trail = s + STRLEN(s);
1658 while (trail > s && vim_iswhite(trail[-1]))
1659 --trail;
1660 }
1661
1662 /* output a space for an empty line, otherwise the line will be
1663 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001664 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 msg_putchar(' ');
1666
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001667 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001669 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
1671 --n_extra;
1672 if (c_extra)
1673 c = c_extra;
1674 else
1675 c = *p_extra++;
1676 }
1677#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001678 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001681 if (lcs_nbsp != NUL && list
1682 && (mb_ptr2char(s) == 160
1683 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001684 {
1685 mb_char2bytes(lcs_nbsp, buf);
1686 buf[(*mb_ptr2len)(buf)] = NUL;
1687 }
1688 else
1689 {
1690 mch_memmove(buf, s, (size_t)l);
1691 buf[l] = NUL;
1692 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001693 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 s += l;
1695 continue;
1696 }
1697#endif
1698 else
1699 {
1700 attr = 0;
1701 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001702 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
1704 /* tab amount depends on current column */
1705 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001706 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
1708 c = ' ';
1709 c_extra = ' ';
1710 }
1711 else
1712 {
1713 c = lcs_tab1;
1714 c_extra = lcs_tab2;
1715 attr = hl_attr(HLF_8);
1716 }
1717 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001718 else if (c == 160 && list && lcs_nbsp != NUL)
1719 {
1720 c = lcs_nbsp;
1721 attr = hl_attr(HLF_8);
1722 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001723 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
1725 p_extra = (char_u *)"";
1726 c_extra = NUL;
1727 n_extra = 1;
1728 c = lcs_eol;
1729 attr = hl_attr(HLF_AT);
1730 --s;
1731 }
1732 else if (c != NUL && (n = byte2cells(c)) > 1)
1733 {
1734 n_extra = n - 1;
1735 p_extra = transchar_byte(c);
1736 c_extra = NUL;
1737 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001738 /* Use special coloring to be able to distinguish <hex> from
1739 * the same in plain text. */
1740 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742 else if (c == ' ' && trail != NULL && s > trail)
1743 {
1744 c = lcs_trail;
1745 attr = hl_attr(HLF_8);
1746 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001747 else if (c == ' ' && list && lcs_space != NUL)
1748 {
1749 c = lcs_space;
1750 attr = hl_attr(HLF_8);
1751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
1753
1754 if (c == NUL)
1755 break;
1756
1757 msg_putchar_attr(c, attr);
1758 col++;
1759 }
1760 msg_clr_eos();
1761}
1762
1763#ifdef FEAT_MBYTE
1764/*
1765 * Use screen_puts() to output one multi-byte character.
1766 * Return the pointer "s" advanced to the next character.
1767 */
1768 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001769screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771 int cw;
1772
1773 msg_didout = TRUE; /* remember that line is not empty */
1774 cw = (*mb_ptr2cells)(s);
1775 if (cw > 1 && (
1776#ifdef FEAT_RIGHTLEFT
1777 cmdmsg_rl ? msg_col <= 1 :
1778#endif
1779 msg_col == Columns - 1))
1780 {
1781 /* Doesn't fit, print a highlighted '>' to fill it up. */
1782 msg_screen_putchar('>', hl_attr(HLF_AT));
1783 return s;
1784 }
1785
1786 screen_puts_len(s, l, msg_row, msg_col, attr);
1787#ifdef FEAT_RIGHTLEFT
1788 if (cmdmsg_rl)
1789 {
1790 msg_col -= cw;
1791 if (msg_col == 0)
1792 {
1793 msg_col = Columns;
1794 ++msg_row;
1795 }
1796 }
1797 else
1798#endif
1799 {
1800 msg_col += cw;
1801 if (msg_col >= Columns)
1802 {
1803 msg_col = 0;
1804 ++msg_row;
1805 }
1806 }
1807 return s + l;
1808}
1809#endif
1810
1811/*
1812 * Output a string to the screen at position msg_row, msg_col.
1813 * Update msg_row and msg_col for the next message.
1814 */
1815 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001816msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001818 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820
1821 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001822msg_puts_title(
1823 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 msg_puts_attr(s, hl_attr(HLF_T));
1826}
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828/*
1829 * Show a message in such a way that it always fits in the line. Cut out a
1830 * part in the middle and replace it with "..." when necessary.
1831 * Does not handle multi-byte characters!
1832 */
1833 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001834msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001836 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837}
1838
1839 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001840msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 int slen = len;
1843 int room;
1844
1845 room = Columns - msg_col;
1846 if (len > room && room >= 20)
1847 {
1848 slen = (room - 3) / 2;
1849 msg_outtrans_len_attr(longstr, slen, attr);
1850 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1851 }
1852 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1853}
1854
1855/*
1856 * Basic function for writing a message with highlight attributes.
1857 */
1858 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001859msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861 msg_puts_attr_len(s, -1, attr);
1862}
1863
1864/*
1865 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1866 * When "maxlen" is -1 there is no maximum length.
1867 * When "maxlen" is >= 0 the message is not put in the history.
1868 */
1869 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001870msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 /*
1873 * If redirection is on, also write to the redirection file.
1874 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001875 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 /*
1878 * Don't print anything when using ":silent cmd".
1879 */
1880 if (msg_silent != 0)
1881 return;
1882
1883 /* if MSG_HIST flag set, add message to history */
1884 if ((attr & MSG_HIST) && maxlen < 0)
1885 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001886 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 attr &= ~MSG_HIST;
1888 }
1889
1890 /*
1891 * When writing something to the screen after it has scrolled, requires a
1892 * wait-return prompt later. Needed when scrolling, resetting
1893 * need_wait_return after some prompt, and then outputting something
1894 * without scrolling
1895 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001896 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 need_wait_return = TRUE;
1898 msg_didany = TRUE; /* remember that something was outputted */
1899
1900 /*
1901 * If there is no valid screen, use fprintf so we can see error messages.
1902 * If termcap is not active, we may be writing in an alternate console
1903 * window, cursor positioning may not work correctly (window size may be
1904 * different, e.g. for Win32 console) or we just don't know where the
1905 * cursor is.
1906 */
1907 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001908 msg_puts_printf(str, maxlen);
1909 else
1910 msg_puts_display(str, maxlen, attr, FALSE);
1911}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001913/*
1914 * The display part of msg_puts_attr_len().
1915 * May be called recursively to display scroll-back text.
1916 */
1917 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001918msg_puts_display(
1919 char_u *str,
1920 int maxlen,
1921 int attr,
1922 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001923{
1924 char_u *s = str;
1925 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1926 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1927#ifdef FEAT_MBYTE
1928 int l;
1929 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001931 char_u *sb_str = str;
1932 int sb_col = msg_col;
1933 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001934 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
1936 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001937 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
1939 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001940 * We are at the end of the screen line when:
1941 * - When outputting a newline.
1942 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001944 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945#ifdef FEAT_RIGHTLEFT
1946 cmdmsg_rl
1947 ? (
1948 msg_col <= 1
1949 || (*s == TAB && msg_col <= 7)
1950# ifdef FEAT_MBYTE
1951 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1952# endif
1953 )
1954 :
1955#endif
1956 (msg_col + t_col >= Columns - 1
1957 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1958# ifdef FEAT_MBYTE
1959 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1960 && msg_col + t_col >= Columns - 2)
1961# endif
1962 ))))
1963 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001964 /*
1965 * The screen is scrolled up when at the last row (some terminals
1966 * scroll automatically, some don't. To avoid problems we scroll
1967 * ourselves).
1968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001971 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001973 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (msg_no_more && lines_left == 0)
1975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 /* Scroll the screen up one line. */
1978 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 msg_row = Rows - 2;
1981 if (msg_col >= Columns) /* can happen after screen resize */
1982 msg_col = Columns - 1;
1983
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001984 /* Display char in last column before showing more-prompt. */
1985 if (*s >= ' '
1986#ifdef FEAT_RIGHTLEFT
1987 && !cmdmsg_rl
1988#endif
1989 )
1990 {
1991#ifdef FEAT_MBYTE
1992 if (has_mbyte)
1993 {
1994 if (enc_utf8 && maxlen >= 0)
1995 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001996 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001997 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001998 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001999 s = screen_puts_mbyte(s, l, attr);
2000 }
2001 else
2002#endif
2003 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002004 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002005 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002006 else
2007 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002008
2009 if (p_more)
2010 /* store text for scrolling back */
2011 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2012
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002013 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002014 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (must_redraw < VALID)
2016 must_redraw = VALID;
2017 redraw_cmdline = TRUE;
2018 if (cmdline_row > 0 && !exmode_active)
2019 --cmdline_row;
2020
2021 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002022 * If screen is completely filled and 'more' is set then wait
2023 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002025 if (lines_left > 0)
2026 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002027 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 && !msg_no_more && !exmode_active)
2029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002031 if (do_more_prompt(NUL))
2032 s = confirm_msg_tail;
2033#else
2034 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035#endif
2036 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002037 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002039
2040 /* When we displayed a char in last column need to check if there
2041 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002042 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002043 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002046 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 || msg_col + t_col >= Columns
2048#ifdef FEAT_MBYTE
2049 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2050 && msg_col + t_col >= Columns - 1)
2051#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002052 ;
2053 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2054 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002056 t_puts(&t_col, t_s, s, attr);
2057
2058 if (wrap && p_more && !recurse)
2059 /* store text for scrolling back */
2060 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 if (*s == '\n') /* go to next line */
2063 {
2064 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002065#ifdef FEAT_RIGHTLEFT
2066 if (cmdmsg_rl)
2067 msg_col = Columns - 1;
2068 else
2069#endif
2070 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (++msg_row >= Rows) /* safety check */
2072 msg_row = Rows - 1;
2073 }
2074 else if (*s == '\r') /* go to column 0 */
2075 {
2076 msg_col = 0;
2077 }
2078 else if (*s == '\b') /* go to previous char */
2079 {
2080 if (msg_col)
2081 --msg_col;
2082 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002083 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 do
2086 msg_screen_putchar(' ', attr);
2087 while (msg_col & 7);
2088 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002089 else if (*s == BELL) /* beep (from ":sh") */
2090 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 else
2092 {
2093#ifdef FEAT_MBYTE
2094 if (has_mbyte)
2095 {
2096 cw = (*mb_ptr2cells)(s);
2097 if (enc_utf8 && maxlen >= 0)
2098 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002099 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002101 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103 else
2104 {
2105 cw = 1;
2106 l = 1;
2107 }
2108#endif
2109 /* When drawing from right to left or when a double-wide character
2110 * doesn't fit, draw a single character here. Otherwise collect
2111 * characters and draw them all at once later. */
2112#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2113 if (
2114# ifdef FEAT_RIGHTLEFT
2115 cmdmsg_rl
2116# ifdef FEAT_MBYTE
2117 ||
2118# endif
2119# endif
2120# ifdef FEAT_MBYTE
2121 (cw > 1 && msg_col + t_col >= Columns - 1)
2122# endif
2123 )
2124 {
2125# ifdef FEAT_MBYTE
2126 if (l > 1)
2127 s = screen_puts_mbyte(s, l, attr) - 1;
2128 else
2129# endif
2130 msg_screen_putchar(*s, attr);
2131 }
2132 else
2133#endif
2134 {
2135 /* postpone this character until later */
2136 if (t_col == 0)
2137 t_s = s;
2138#ifdef FEAT_MBYTE
2139 t_col += cw;
2140 s += l - 1;
2141#else
2142 ++t_col;
2143#endif
2144 }
2145 }
2146 ++s;
2147 }
2148
2149 /* output any postponed text */
2150 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002151 t_puts(&t_col, t_s, s, attr);
2152 if (p_more && !recurse)
2153 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 msg_check();
2156}
2157
2158/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002159 * Return TRUE when ":filter pattern" was used and "msg" does not match
2160 * "pattern".
2161 */
2162 int
2163message_filtered(char_u *msg)
2164{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002165 int match;
2166
2167 if (cmdmod.filter_regmatch.regprog == NULL)
2168 return FALSE;
2169 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2170 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002171}
2172
2173/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002174 * Scroll the screen up one line for displaying the next message line.
2175 */
2176 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002177msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002178{
2179#ifdef FEAT_GUI
2180 /* Remove the cursor before scrolling, ScreenLines[] is going
2181 * to become invalid. */
2182 if (gui.in_use)
2183 gui_undraw_cursor();
2184#endif
2185 /* scrolling up always works */
2186 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2187
2188 if (!can_clear((char_u *)" "))
2189 {
2190 /* Scrolling up doesn't result in the right background. Set the
2191 * background here. It's not efficient, but avoids that we have to do
2192 * it all over the code. */
2193 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2194
2195 /* Also clear the last char of the last but one line if it was not
2196 * cleared before to avoid a scroll-up. */
2197 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2198 screen_fill((int)Rows - 2, (int)Rows - 1,
2199 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2200 }
2201}
2202
2203/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002204 * Increment "msg_scrolled".
2205 */
2206 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002207inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002208{
2209#ifdef FEAT_EVAL
2210 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2211 {
2212 char_u *p = sourcing_name;
2213 char_u *tofree = NULL;
2214 int len;
2215
2216 /* v:scrollstart is empty, set it to the script/function name and line
2217 * number */
2218 if (p == NULL)
2219 p = (char_u *)_("Unknown");
2220 else
2221 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002222 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002223 tofree = alloc(len);
2224 if (tofree != NULL)
2225 {
2226 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2227 p, (long)sourcing_lnum);
2228 p = tofree;
2229 }
2230 }
2231 set_vim_var_string(VV_SCROLLSTART, p, -1);
2232 vim_free(tofree);
2233 }
2234#endif
2235 ++msg_scrolled;
2236}
2237
2238/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002239 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2240 * store the displayed text and remember where screen lines start.
2241 */
2242typedef struct msgchunk_S msgchunk_T;
2243struct msgchunk_S
2244{
2245 msgchunk_T *sb_next;
2246 msgchunk_T *sb_prev;
2247 char sb_eol; /* TRUE when line ends after this text */
2248 int sb_msg_col; /* column in which text starts */
2249 int sb_attr; /* text attributes */
2250 char_u sb_text[1]; /* text to be displayed, actually longer */
2251};
2252
2253static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2254
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002255static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2256static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002257
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002258static int do_clear_sb_text = FALSE; /* clear text on next msg */
2259
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002260/*
2261 * Store part of a printed message for displaying when scrolling back.
2262 */
2263 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002264store_sb_text(
2265 char_u **sb_str, /* start of string */
2266 char_u *s, /* just after string */
2267 int attr,
2268 int *sb_col,
2269 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002270{
2271 msgchunk_T *mp;
2272
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002273 if (do_clear_sb_text)
2274 {
2275 clear_sb_text();
2276 do_clear_sb_text = FALSE;
2277 }
2278
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002279 if (s > *sb_str)
2280 {
2281 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2282 if (mp != NULL)
2283 {
2284 mp->sb_eol = finish;
2285 mp->sb_msg_col = *sb_col;
2286 mp->sb_attr = attr;
2287 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2288
2289 if (last_msgchunk == NULL)
2290 {
2291 last_msgchunk = mp;
2292 mp->sb_prev = NULL;
2293 }
2294 else
2295 {
2296 mp->sb_prev = last_msgchunk;
2297 last_msgchunk->sb_next = mp;
2298 last_msgchunk = mp;
2299 }
2300 mp->sb_next = NULL;
2301 }
2302 }
2303 else if (finish && last_msgchunk != NULL)
2304 last_msgchunk->sb_eol = TRUE;
2305
2306 *sb_str = s;
2307 *sb_col = 0;
2308}
2309
2310/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002311 * Finished showing messages, clear the scroll-back text on the next message.
2312 */
2313 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002314may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002315{
2316 do_clear_sb_text = TRUE;
2317}
2318
2319/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002320 * Clear any text remembered for scrolling back.
2321 * Called when redrawing the screen.
2322 */
2323 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002324clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002325{
2326 msgchunk_T *mp;
2327
2328 while (last_msgchunk != NULL)
2329 {
2330 mp = last_msgchunk->sb_prev;
2331 vim_free(last_msgchunk);
2332 last_msgchunk = mp;
2333 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002334}
2335
2336/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002337 * "g<" command.
2338 */
2339 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002340show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002341{
2342 msgchunk_T *mp;
2343
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002344 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002345 * weird, typing a command without output results in one line. */
2346 mp = msg_sb_start(last_msgchunk);
2347 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002348 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002349 else
2350 {
2351 do_more_prompt('G');
2352 wait_return(FALSE);
2353 }
2354}
2355
2356/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002357 * Move to the start of screen line in already displayed text.
2358 */
2359 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002360msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002361{
2362 msgchunk_T *mp = mps;
2363
2364 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2365 mp = mp->sb_prev;
2366 return mp;
2367}
2368
2369/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002370 * Mark the last message chunk as finishing the line.
2371 */
2372 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002373msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002374{
2375 if (last_msgchunk != NULL)
2376 last_msgchunk->sb_eol = TRUE;
2377}
2378
2379/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002380 * Display a screen line from previously displayed text at row "row".
2381 * Returns a pointer to the text for the next line (can be NULL).
2382 */
2383 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002384disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002385{
2386 msgchunk_T *mp = smp;
2387 char_u *p;
2388
2389 for (;;)
2390 {
2391 msg_row = row;
2392 msg_col = mp->sb_msg_col;
2393 p = mp->sb_text;
2394 if (*p == '\n') /* don't display the line break */
2395 ++p;
2396 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2397 if (mp->sb_eol || mp->sb_next == NULL)
2398 break;
2399 mp = mp->sb_next;
2400 }
2401 return mp->sb_next;
2402}
2403
2404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 * Output any postponed text for msg_puts_attr_len().
2406 */
2407 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002408t_puts(
2409 int *t_col,
2410 char_u *t_s,
2411 char_u *s,
2412 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 /* output postponed text */
2415 msg_didout = TRUE; /* remember that line is not empty */
2416 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002417 msg_col += *t_col;
2418 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419#ifdef FEAT_MBYTE
2420 /* If the string starts with a composing character don't increment the
2421 * column position for it. */
2422 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2423 --msg_col;
2424#endif
2425 if (msg_col >= Columns)
2426 {
2427 msg_col = 0;
2428 ++msg_row;
2429 }
2430}
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432/*
2433 * Returns TRUE when messages should be printed with mch_errmsg().
2434 * This is used when there is no valid screen, so we can see error messages.
2435 * If termcap is not active, we may be writing in an alternate console
2436 * window, cursor positioning may not work correctly (window size may be
2437 * different, e.g. for Win32 console) or we just don't know where the
2438 * cursor is.
2439 */
2440 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002441msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 return (!msg_check_screen()
2444#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2445 || !termcap_active
2446#endif
2447 || (swapping_screen() && !termcap_active)
2448 );
2449}
2450
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002451/*
2452 * Print a message when there is no valid screen.
2453 */
2454 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002455msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002456{
2457 char_u *s = str;
2458 char_u buf[4];
2459 char_u *p;
2460
2461#ifdef WIN3264
2462 if (!(silent_mode && p_verbose == 0))
2463 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2464#endif
2465 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2466 {
2467 if (!(silent_mode && p_verbose == 0))
2468 {
2469 /* NL --> CR NL translation (for Unix, not for "--version") */
2470 /* NL --> CR translation (for Mac) */
2471 p = &buf[0];
2472 if (*s == '\n' && !info_message)
2473 *p++ = '\r';
2474#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2475 else
2476#endif
2477 *p++ = *s;
2478 *p = '\0';
2479 if (info_message) /* informative message, not an error */
2480 mch_msg((char *)buf);
2481 else
2482 mch_errmsg((char *)buf);
2483 }
2484
2485 /* primitive way to compute the current column */
2486#ifdef FEAT_RIGHTLEFT
2487 if (cmdmsg_rl)
2488 {
2489 if (*s == '\r' || *s == '\n')
2490 msg_col = Columns - 1;
2491 else
2492 --msg_col;
2493 }
2494 else
2495#endif
2496 {
2497 if (*s == '\r' || *s == '\n')
2498 msg_col = 0;
2499 else
2500 ++msg_col;
2501 }
2502 ++s;
2503 }
2504 msg_didout = TRUE; /* assume that line is not empty */
2505
2506#ifdef WIN3264
2507 if (!(silent_mode && p_verbose == 0))
2508 mch_settmode(TMODE_RAW);
2509#endif
2510}
2511
2512/*
2513 * Show the more-prompt and handle the user response.
2514 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002515 * When at hit-enter prompt "typed_char" is the already typed character,
2516 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002517 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2518 */
2519 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002520do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002521{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002522 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002523 int used_typed_char = typed_char;
2524 int oldState = State;
2525 int c;
2526#ifdef FEAT_CON_DIALOG
2527 int retval = FALSE;
2528#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002529 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002530 msgchunk_T *mp_last = NULL;
2531 msgchunk_T *mp;
2532 int i;
2533
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002534 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002535 * that case don't show another prompt. Also when at the hit-Enter prompt
2536 * and nothing was typed. */
2537 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002538 return FALSE;
2539 entered = TRUE;
2540
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002541 if (typed_char == 'G')
2542 {
2543 /* "g<": Find first line on the last page. */
2544 mp_last = msg_sb_start(last_msgchunk);
2545 for (i = 0; i < Rows - 2 && mp_last != NULL
2546 && mp_last->sb_prev != NULL; ++i)
2547 mp_last = msg_sb_start(mp_last->sb_prev);
2548 }
2549
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002550 State = ASKMORE;
2551#ifdef FEAT_MOUSE
2552 setmouse();
2553#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002554 if (typed_char == NUL)
2555 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002556 for (;;)
2557 {
2558 /*
2559 * Get a typed character directly from the user.
2560 */
2561 if (used_typed_char != NUL)
2562 {
2563 c = used_typed_char; /* was typed at hit-enter prompt */
2564 used_typed_char = NUL;
2565 }
2566 else
2567 c = get_keystroke();
2568
2569#if defined(FEAT_MENU) && defined(FEAT_GUI)
2570 if (c == K_MENU)
2571 {
2572 int idx = get_menu_index(current_menu, ASKMORE);
2573
2574 /* Used a menu. If it starts with CTRL-Y, it must
2575 * be a "Copy" for the clipboard. Otherwise
2576 * assume that we end */
2577 if (idx == MENU_INDEX_INVALID)
2578 continue;
2579 c = *current_menu->strings[idx];
2580 if (c != NUL && current_menu->strings[idx][1] != NUL)
2581 ins_typebuf(current_menu->strings[idx] + 1,
2582 current_menu->noremap[idx], 0, TRUE,
2583 current_menu->silent[idx]);
2584 }
2585#endif
2586
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002587 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002588 switch (c)
2589 {
2590 case BS: /* scroll one line back */
2591 case K_BS:
2592 case 'k':
2593 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002594 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002595 break;
2596
2597 case CAR: /* one extra line */
2598 case NL:
2599 case 'j':
2600 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002601 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002602 break;
2603
2604 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002605 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 break;
2607
2608 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002609 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002610 break;
2611
2612 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002613 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002614 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002615 break;
2616
2617 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002618 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002619 case K_PAGEDOWN:
2620 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002621 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002622 break;
2623
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002624 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002625 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002626 break;
2627
2628 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002629 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002630 lines_left = 999999;
2631 break;
2632
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002633 case ':': /* start new command line */
2634#ifdef FEAT_CON_DIALOG
2635 if (!confirm_msg_used)
2636#endif
2637 {
2638 /* Since got_int is set all typeahead will be flushed, but we
2639 * want to keep this ':', remember that in a special way. */
2640 typeahead_noflush(':');
2641 cmdline_row = Rows - 1; /* put ':' on this line */
2642 skip_redraw = TRUE; /* skip redraw once */
2643 need_wait_return = FALSE; /* don't wait in main() */
2644 }
2645 /*FALLTHROUGH*/
2646 case 'q': /* quit */
2647 case Ctrl_C:
2648 case ESC:
2649#ifdef FEAT_CON_DIALOG
2650 if (confirm_msg_used)
2651 {
2652 /* Jump to the choices of the dialog. */
2653 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654 }
2655 else
2656#endif
2657 {
2658 got_int = TRUE;
2659 quit_more = TRUE;
2660 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002661 /* When there is some more output (wrapping line) display that
2662 * without another prompt. */
2663 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002664 break;
2665
2666#ifdef FEAT_CLIPBOARD
2667 case Ctrl_Y:
2668 /* Strange way to allow copying (yanking) a modeless
2669 * selection at the more prompt. Use CTRL-Y,
2670 * because the same is used in Cmdline-mode and at the
2671 * hit-enter prompt. However, scrolling one line up
2672 * might be expected... */
2673 if (clip_star.state == SELECT_DONE)
2674 clip_copy_modeless_selection(TRUE);
2675 continue;
2676#endif
2677 default: /* no valid response */
2678 msg_moremsg(TRUE);
2679 continue;
2680 }
2681
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002682 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002683 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002684 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002685 {
2686 /* go to start of last line */
2687 if (mp_last == NULL)
2688 mp = msg_sb_start(last_msgchunk);
2689 else if (mp_last->sb_prev != NULL)
2690 mp = msg_sb_start(mp_last->sb_prev);
2691 else
2692 mp = NULL;
2693
2694 /* go to start of line at top of the screen */
2695 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2696 ++i)
2697 mp = msg_sb_start(mp->sb_prev);
2698
2699 if (mp != NULL && mp->sb_prev != NULL)
2700 {
2701 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002702 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002703 {
2704 if (mp == NULL || mp->sb_prev == NULL)
2705 break;
2706 mp = msg_sb_start(mp->sb_prev);
2707 if (mp_last == NULL)
2708 mp_last = msg_sb_start(last_msgchunk);
2709 else
2710 mp_last = msg_sb_start(mp_last->sb_prev);
2711 }
2712
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002713 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002714 (int)Rows, NULL) == OK)
2715 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002716 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002717 (void)disp_sb_line(0, mp);
2718 }
2719 else
2720 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002721 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002723 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2724 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002725 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002726 ++msg_scrolled;
2727 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002729 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002730 }
2731 }
2732 else
2733 {
2734 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002735 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002736 {
2737 /* scroll up, display line at bottom */
2738 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002739 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2741 (int)Columns, ' ', ' ', 0);
2742 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002743 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002744 }
2745 }
2746
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002747 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002748 {
2749 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002750 screen_fill((int)Rows - 1, (int)Rows, 0,
2751 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002752 msg_moremsg(FALSE);
2753 continue;
2754 }
2755
2756 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002757 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002758 }
2759
2760 break;
2761 }
2762
2763 /* clear the --more-- message */
2764 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2765 State = oldState;
2766#ifdef FEAT_MOUSE
2767 setmouse();
2768#endif
2769 if (quit_more)
2770 {
2771 msg_row = Rows - 1;
2772 msg_col = 0;
2773 }
2774#ifdef FEAT_RIGHTLEFT
2775 else if (cmdmsg_rl)
2776 msg_col = Columns - 1;
2777#endif
2778
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002779 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002780#ifdef FEAT_CON_DIALOG
2781 return retval;
2782#else
2783 return FALSE;
2784#endif
2785}
2786
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2788
2789#ifdef mch_errmsg
2790# undef mch_errmsg
2791#endif
2792#ifdef mch_msg
2793# undef mch_msg
2794#endif
2795
2796/*
2797 * Give an error message. To be used when the screen hasn't been initialized
2798 * yet. When stderr can't be used, collect error messages until the GUI has
2799 * started and they can be displayed in a message box.
2800 */
2801 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002802mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
2804 int len;
2805
2806#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2807 /* On Unix use stderr if it's a tty.
2808 * When not going to start the GUI also use stderr.
2809 * On Mac, when started from Finder, stderr is the console. */
2810 if (
2811# ifdef UNIX
2812# ifdef MACOS_X_UNIX
2813 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2814# else
2815 isatty(2)
2816# endif
2817# ifdef FEAT_GUI
2818 ||
2819# endif
2820# endif
2821# ifdef FEAT_GUI
2822 !(gui.in_use || gui.starting)
2823# endif
2824 )
2825 {
2826 fprintf(stderr, "%s", str);
2827 return;
2828 }
2829#endif
2830
2831 /* avoid a delay for a message that isn't there */
2832 emsg_on_display = FALSE;
2833
2834 len = (int)STRLEN(str) + 1;
2835 if (error_ga.ga_growsize == 0)
2836 {
2837 error_ga.ga_growsize = 80;
2838 error_ga.ga_itemsize = 1;
2839 }
2840 if (ga_grow(&error_ga, len) == OK)
2841 {
2842 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2843 (char_u *)str, len);
2844#ifdef UNIX
2845 /* remove CR characters, they are displayed */
2846 {
2847 char_u *p;
2848
2849 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2850 for (;;)
2851 {
2852 p = vim_strchr(p, '\r');
2853 if (p == NULL)
2854 break;
2855 *p = ' ';
2856 }
2857 }
2858#endif
2859 --len; /* don't count the NUL at the end */
2860 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862}
2863
2864/*
2865 * Give a message. To be used when the screen hasn't been initialized yet.
2866 * When there is no tty, collect messages until the GUI has started and they
2867 * can be displayed in a message box.
2868 */
2869 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002870mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2873 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2874 * uses mch_errmsg() when started from the desktop.
2875 * When not going to start the GUI also use stdout.
2876 * On Mac, when started from Finder, stderr is the console. */
2877 if (
2878# ifdef UNIX
2879# ifdef MACOS_X_UNIX
2880 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2881# else
2882 isatty(2)
2883# endif
2884# ifdef FEAT_GUI
2885 ||
2886# endif
2887# endif
2888# ifdef FEAT_GUI
2889 !(gui.in_use || gui.starting)
2890# endif
2891 )
2892 {
2893 printf("%s", str);
2894 return;
2895 }
2896# endif
2897 mch_errmsg(str);
2898}
2899#endif /* USE_MCH_ERRMSG */
2900
2901/*
2902 * Put a character on the screen at the current message position and advance
2903 * to the next position. Only for printable ASCII!
2904 */
2905 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002906msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
2908 msg_didout = TRUE; /* remember that line is not empty */
2909 screen_putchar(c, msg_row, msg_col, attr);
2910#ifdef FEAT_RIGHTLEFT
2911 if (cmdmsg_rl)
2912 {
2913 if (--msg_col == 0)
2914 {
2915 msg_col = Columns;
2916 ++msg_row;
2917 }
2918 }
2919 else
2920#endif
2921 {
2922 if (++msg_col >= Columns)
2923 {
2924 msg_col = 0;
2925 ++msg_row;
2926 }
2927 }
2928}
2929
2930 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002931msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002933 int attr;
2934 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002937 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002939 screen_puts((char_u *)
2940 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2941 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943
2944/*
2945 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2946 * exmode_active.
2947 */
2948 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002949repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 if (State == ASKMORE)
2952 {
2953 msg_moremsg(TRUE); /* display --more-- message again */
2954 msg_row = Rows - 1;
2955 }
2956#ifdef FEAT_CON_DIALOG
2957 else if (State == CONFIRM)
2958 {
2959 display_confirm_msg(); /* display ":confirm" message again */
2960 msg_row = Rows - 1;
2961 }
2962#endif
2963 else if (State == EXTERNCMD)
2964 {
2965 windgoto(msg_row, msg_col); /* put cursor back */
2966 }
2967 else if (State == HITRETURN || State == SETWSIZE)
2968 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002969 if (msg_row == Rows - 1)
2970 {
2971 /* Avoid drawing the "hit-enter" prompt below the previous one,
2972 * overwrite it. Esp. useful when regaining focus and a
2973 * FocusGained autocmd exists but didn't draw anything. */
2974 msg_didout = FALSE;
2975 msg_col = 0;
2976 msg_clr_eos();
2977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 hit_return_msg();
2979 msg_row = Rows - 1;
2980 }
2981}
2982
2983/*
2984 * msg_check_screen - check if the screen is initialized.
2985 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2986 * While starting the GUI the terminal codes will be set for the GUI, but the
2987 * output goes to the terminal. Don't use the terminal codes then.
2988 */
2989 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002990msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 if (!full_screen || !screen_valid(FALSE))
2993 return FALSE;
2994
2995 if (msg_row >= Rows)
2996 msg_row = Rows - 1;
2997 if (msg_col >= Columns)
2998 msg_col = Columns - 1;
2999 return TRUE;
3000}
3001
3002/*
3003 * Clear from current message position to end of screen.
3004 * Skip this when ":silent" was used, no need to clear for redirection.
3005 */
3006 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003007msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 if (msg_silent == 0)
3010 msg_clr_eos_force();
3011}
3012
3013/*
3014 * Clear from current message position to end of screen.
3015 * Note: msg_col is not updated, so we remember the end of the message
3016 * for msg_check().
3017 */
3018 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003019msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
3021 if (msg_use_printf())
3022 {
3023 if (full_screen) /* only when termcap codes are valid */
3024 {
3025 if (*T_CD)
3026 out_str(T_CD); /* clear to end of display */
3027 else if (*T_CE)
3028 out_str(T_CE); /* clear to end of line */
3029 }
3030 }
3031 else
3032 {
3033#ifdef FEAT_RIGHTLEFT
3034 if (cmdmsg_rl)
3035 {
3036 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3037 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3038 }
3039 else
3040#endif
3041 {
3042 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3043 ' ', ' ', 0);
3044 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3045 }
3046 }
3047}
3048
3049/*
3050 * Clear the command line.
3051 */
3052 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003053msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 msg_row = cmdline_row;
3056 msg_col = 0;
3057 msg_clr_eos_force();
3058}
3059
3060/*
3061 * end putting a message on the screen
3062 * call wait_return if the message does not fit in the available space
3063 * return TRUE if wait_return not called.
3064 */
3065 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003066msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003069 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 * or the ruler option is set and we run into it,
3071 * we have to redraw the window.
3072 * Do not do this if we are abandoning the file or editing the command line.
3073 */
3074 if (!exiting && need_wait_return && !(State & CMDLINE))
3075 {
3076 wait_return(FALSE);
3077 return FALSE;
3078 }
3079 out_flush();
3080 return TRUE;
3081}
3082
3083/*
3084 * If the written message runs into the shown command or ruler, we have to
3085 * wait for hit-return and redraw the window later.
3086 */
3087 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003088msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 if (msg_row == Rows - 1 && msg_col >= sc_col)
3091 {
3092 need_wait_return = TRUE;
3093 redraw_cmdline = TRUE;
3094 }
3095}
3096
3097/*
3098 * May write a string to the redirection file.
3099 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3100 */
3101 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003102redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 char_u *s = str;
3105 static int cur_col = 0;
3106
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003107 /* Don't do anything for displaying prompts and the like. */
3108 if (redir_off)
3109 return;
3110
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003111 /* If 'verbosefile' is set prepare for writing in that file. */
3112 if (*p_vfile != NUL && verbose_fd == NULL)
3113 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003114
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003115 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
3117 /* If the string doesn't start with CR or NL, go to msg_col */
3118 if (*s != '\n' && *s != '\r')
3119 {
3120 while (cur_col < msg_col)
3121 {
3122#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003123 if (redir_execute)
3124 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003125 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003127 else if (redir_vname)
3128 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003131 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003133 if (verbose_fd != NULL)
3134 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 ++cur_col;
3136 }
3137 }
3138
3139#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003140 if (redir_execute)
3141 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003142 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003144 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003145 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
3147
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003148 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3150 {
3151#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003152 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003154 if (redir_fd != NULL)
3155 putc(*s, redir_fd);
3156 if (verbose_fd != NULL)
3157 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (*s == '\r' || *s == '\n')
3159 cur_col = 0;
3160 else if (*s == '\t')
3161 cur_col += (8 - cur_col % 8);
3162 else
3163 ++cur_col;
3164 ++s;
3165 }
3166
3167 if (msg_silent != 0) /* should update msg_col */
3168 msg_col = cur_col;
3169 }
3170}
3171
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003172 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003173redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003174{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003175 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003176#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003177 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003178#endif
3179 ;
3180}
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003183 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003184 * Must always be called paired with verbose_leave()!
3185 */
3186 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003187verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003188{
3189 if (*p_vfile != NUL)
3190 ++msg_silent;
3191}
3192
3193/*
3194 * After giving verbose message.
3195 * Must always be called paired with verbose_enter()!
3196 */
3197 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003198verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003199{
3200 if (*p_vfile != NUL)
3201 if (--msg_silent < 0)
3202 msg_silent = 0;
3203}
3204
3205/*
3206 * Like verbose_enter() and set msg_scroll when displaying the message.
3207 */
3208 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003209verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003210{
3211 if (*p_vfile != NUL)
3212 ++msg_silent;
3213 else
3214 /* always scroll up, don't overwrite */
3215 msg_scroll = TRUE;
3216}
3217
3218/*
3219 * Like verbose_leave() and set cmdline_row when displaying the message.
3220 */
3221 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003223{
3224 if (*p_vfile != NUL)
3225 {
3226 if (--msg_silent < 0)
3227 msg_silent = 0;
3228 }
3229 else
3230 cmdline_row = msg_row;
3231}
3232
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003233/*
3234 * Called when 'verbosefile' is set: stop writing to the file.
3235 */
3236 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003237verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003238{
3239 if (verbose_fd != NULL)
3240 {
3241 fclose(verbose_fd);
3242 verbose_fd = NULL;
3243 }
3244 verbose_did_open = FALSE;
3245}
3246
3247/*
3248 * Open the file 'verbosefile'.
3249 * Return FAIL or OK.
3250 */
3251 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003252verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003253{
3254 if (verbose_fd == NULL && !verbose_did_open)
3255 {
3256 /* Only give the error message once. */
3257 verbose_did_open = TRUE;
3258
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003259 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003260 if (verbose_fd == NULL)
3261 {
3262 EMSG2(_(e_notopen), p_vfile);
3263 return FAIL;
3264 }
3265 }
3266 return OK;
3267}
3268
3269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 * Give a warning message (for searching).
3271 * Use 'w' highlighting and may repeat the message after redrawing
3272 */
3273 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003274give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 /* Don't do this for ":silent". */
3277 if (msg_silent != 0)
3278 return;
3279
3280 /* Don't want a hit-enter prompt here. */
3281 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#ifdef FEAT_EVAL
3284 set_vim_var_string(VV_WARNINGMSG, message, -1);
3285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 vim_free(keep_msg);
3287 keep_msg = NULL;
3288 if (hl)
3289 keep_msg_attr = hl_attr(HLF_W);
3290 else
3291 keep_msg_attr = 0;
3292 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003293 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 msg_didout = FALSE; /* overwrite this message */
3295 msg_nowait = TRUE; /* don't wait for this message */
3296 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003297
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 --no_wait_return;
3299}
3300
3301/*
3302 * Advance msg cursor to column "col".
3303 */
3304 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003305msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 if (msg_silent != 0) /* nothing to advance to */
3308 {
3309 msg_col = col; /* for redirection, may fill it up later */
3310 return;
3311 }
3312 if (col >= Columns) /* not enough room */
3313 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003314#ifdef FEAT_RIGHTLEFT
3315 if (cmdmsg_rl)
3316 while (msg_col > Columns - col)
3317 msg_putchar(' ');
3318 else
3319#endif
3320 while (msg_col < col)
3321 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322}
3323
3324#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3325/*
3326 * Used for "confirm()" function, and the :confirm command prefix.
3327 * Versions which haven't got flexible dialogs yet, and console
3328 * versions, get this generic handler which uses the command line.
3329 *
3330 * type = one of:
3331 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3332 * title = title string (can be NULL for default)
3333 * (neither used in console dialogs at the moment)
3334 *
3335 * Format of the "buttons" string:
3336 * "Button1Name\nButton2Name\nButton3Name"
3337 * The first button should normally be the default/accept
3338 * The second button should be the 'Cancel' button
3339 * Other buttons- use your imagination!
3340 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3341 * different letter.
3342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003344do_dialog(
3345 int type UNUSED,
3346 char_u *title UNUSED,
3347 char_u *message,
3348 char_u *buttons,
3349 int dfltbutton,
3350 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003351 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003352 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003353 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 int oldState;
3356 int retval = 0;
3357 char_u *hotkeys;
3358 int c;
3359 int i;
3360
3361#ifndef NO_CONSOLE
3362 /* Don't output anything in silent mode ("ex -s") */
3363 if (silent_mode)
3364 return dfltbutton; /* return default option */
3365#endif
3366
3367#ifdef FEAT_GUI_DIALOG
3368 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3369 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3370 {
3371 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003372 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003373 /* avoid a hit-enter prompt without clearing the cmdline */
3374 need_wait_return = FALSE;
3375 emsg_on_display = FALSE;
3376 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
3378 /* Flush output to avoid that further messages and redrawing is done
3379 * in the wrong order. */
3380 out_flush();
3381 gui_mch_update();
3382
3383 return c;
3384 }
3385#endif
3386
3387 oldState = State;
3388 State = CONFIRM;
3389#ifdef FEAT_MOUSE
3390 setmouse();
3391#endif
3392
3393 /*
3394 * Since we wait for a keypress, don't make the
3395 * user press RETURN as well afterwards.
3396 */
3397 ++no_wait_return;
3398 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3399
3400 if (hotkeys != NULL)
3401 {
3402 for (;;)
3403 {
3404 /* Get a typed character directly from the user. */
3405 c = get_keystroke();
3406 switch (c)
3407 {
3408 case CAR: /* User accepts default option */
3409 case NL:
3410 retval = dfltbutton;
3411 break;
3412 case Ctrl_C: /* User aborts/cancels */
3413 case ESC:
3414 retval = 0;
3415 break;
3416 default: /* Could be a hotkey? */
3417 if (c < 0) /* special keys are ignored here */
3418 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003419 if (c == ':' && ex_cmd)
3420 {
3421 retval = dfltbutton;
3422 ins_char_typebuf(':');
3423 break;
3424 }
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 /* Make the character lowercase, as chars in "hotkeys" are. */
3427 c = MB_TOLOWER(c);
3428 retval = 1;
3429 for (i = 0; hotkeys[i]; ++i)
3430 {
3431#ifdef FEAT_MBYTE
3432 if (has_mbyte)
3433 {
3434 if ((*mb_ptr2char)(hotkeys + i) == c)
3435 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003436 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 else
3439#endif
3440 if (hotkeys[i] == c)
3441 break;
3442 ++retval;
3443 }
3444 if (hotkeys[i])
3445 break;
3446 /* No hotkey match, so keep waiting */
3447 continue;
3448 }
3449 break;
3450 }
3451
3452 vim_free(hotkeys);
3453 }
3454
3455 State = oldState;
3456#ifdef FEAT_MOUSE
3457 setmouse();
3458#endif
3459 --no_wait_return;
3460 msg_end_prompt();
3461
3462 return retval;
3463}
3464
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003465static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467/*
3468 * Copy one character from "*from" to "*to", taking care of multi-byte
3469 * characters. Return the length of the character in bytes.
3470 */
3471 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003472copy_char(
3473 char_u *from,
3474 char_u *to,
3475 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477#ifdef FEAT_MBYTE
3478 int len;
3479 int c;
3480
3481 if (has_mbyte)
3482 {
3483 if (lowercase)
3484 {
3485 c = MB_TOLOWER((*mb_ptr2char)(from));
3486 return (*mb_char2bytes)(c, to);
3487 }
3488 else
3489 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003490 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 mch_memmove(to, from, (size_t)len);
3492 return len;
3493 }
3494 }
3495 else
3496#endif
3497 {
3498 if (lowercase)
3499 *to = (char_u)TOLOWER_LOC(*from);
3500 else
3501 *to = *from;
3502 return 1;
3503 }
3504}
3505
3506/*
3507 * Format the dialog string, and display it at the bottom of
3508 * the screen. Return a string of hotkey chars (if defined) for
3509 * each 'button'. If a button has no hotkey defined, the first character of
3510 * the button is used.
3511 * The hotkeys can be multi-byte characters, but without combining chars.
3512 *
3513 * Returns an allocated string with hotkeys, or NULL for error.
3514 */
3515 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003516msg_show_console_dialog(
3517 char_u *message,
3518 char_u *buttons,
3519 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 int len = 0;
3522#ifdef FEAT_MBYTE
3523# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3524#else
3525# define HOTK_LEN 1
3526#endif
3527 int lenhotkey = HOTK_LEN; /* count first button */
3528 char_u *hotk = NULL;
3529 char_u *msgp = NULL;
3530 char_u *hotkp = NULL;
3531 char_u *r;
3532 int copy;
3533#define HAS_HOTKEY_LEN 30
3534 char_u has_hotkey[HAS_HOTKEY_LEN];
3535 int first_hotkey = FALSE; /* first char of button is hotkey */
3536 int idx;
3537
3538 has_hotkey[0] = FALSE;
3539
3540 /*
3541 * First loop: compute the size of memory to allocate.
3542 * Second loop: copy to the allocated memory.
3543 */
3544 for (copy = 0; copy <= 1; ++copy)
3545 {
3546 r = buttons;
3547 idx = 0;
3548 while (*r)
3549 {
3550 if (*r == DLG_BUTTON_SEP)
3551 {
3552 if (copy)
3553 {
3554 *msgp++ = ',';
3555 *msgp++ = ' '; /* '\n' -> ', ' */
3556
3557 /* advance to next hotkey and set default hotkey */
3558#ifdef FEAT_MBYTE
3559 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003560 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
3562#endif
3563 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003564 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (dfltbutton)
3566 --dfltbutton;
3567
3568 /* If no hotkey is specified first char is used. */
3569 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3570 first_hotkey = TRUE;
3571 }
3572 else
3573 {
3574 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3575 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3576 if (idx < HAS_HOTKEY_LEN - 1)
3577 has_hotkey[++idx] = FALSE;
3578 }
3579 }
3580 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3581 {
3582 if (*r == DLG_HOTKEY_CHAR)
3583 ++r;
3584 first_hotkey = FALSE;
3585 if (copy)
3586 {
3587 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3588 *msgp++ = *r;
3589 else
3590 {
3591 /* '&a' -> '[a]' */
3592 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3593 msgp += copy_char(r, msgp, FALSE);
3594 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3595
3596 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003597 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599 }
3600 else
3601 {
3602 ++len; /* '&a' -> '[a]' */
3603 if (idx < HAS_HOTKEY_LEN - 1)
3604 has_hotkey[idx] = TRUE;
3605 }
3606 }
3607 else
3608 {
3609 /* everything else copy literally */
3610 if (copy)
3611 msgp += copy_char(r, msgp, FALSE);
3612 }
3613
3614 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003615 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617
3618 if (copy)
3619 {
3620 *msgp++ = ':';
3621 *msgp++ = ' ';
3622 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 else
3625 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003626 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003627 + 2 /* for the NL's */
3628 + STRLEN(buttons)
3629 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003630 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 /* If no hotkey is specified first char is used. */
3633 if (!has_hotkey[0])
3634 {
3635 first_hotkey = TRUE;
3636 len += 2; /* "x" -> "[x]" */
3637 }
3638
3639 /*
3640 * Now allocate and load the strings
3641 */
3642 vim_free(confirm_msg);
3643 confirm_msg = alloc(len);
3644 if (confirm_msg == NULL)
3645 return NULL;
3646 *confirm_msg = NUL;
3647 hotk = alloc(lenhotkey);
3648 if (hotk == NULL)
3649 return NULL;
3650
3651 *confirm_msg = '\n';
3652 STRCPY(confirm_msg + 1, message);
3653
3654 msgp = confirm_msg + 1 + STRLEN(message);
3655 hotkp = hotk;
3656
Bram Moolenaar6a516062007-07-05 08:11:42 +00003657 /* Define first default hotkey. Keep the hotkey string NUL
3658 * terminated to avoid reading past the end. */
3659 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 /* Remember where the choices start, displaying starts here when
3662 * "hotkp" typed at the more prompt. */
3663 confirm_msg_tail = msgp;
3664 *msgp++ = '\n';
3665 }
3666 }
3667
3668 display_confirm_msg();
3669 return hotk;
3670}
3671
3672/*
3673 * Display the ":confirm" message. Also called when screen resized.
3674 */
3675 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003676display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
3678 /* avoid that 'q' at the more prompt truncates the message here */
3679 ++confirm_msg_used;
3680 if (confirm_msg != NULL)
3681 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3682 --confirm_msg_used;
3683}
3684
3685#endif /* FEAT_CON_DIALOG */
3686
3687#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3688
3689 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003690vim_dialog_yesno(
3691 int type,
3692 char_u *title,
3693 char_u *message,
3694 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
3696 if (do_dialog(type,
3697 title == NULL ? (char_u *)_("Question") : title,
3698 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003699 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return VIM_YES;
3701 return VIM_NO;
3702}
3703
3704 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003705vim_dialog_yesnocancel(
3706 int type,
3707 char_u *title,
3708 char_u *message,
3709 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 switch (do_dialog(type,
3712 title == NULL ? (char_u *)_("Question") : title,
3713 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003714 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 case 1: return VIM_YES;
3717 case 2: return VIM_NO;
3718 }
3719 return VIM_CANCEL;
3720}
3721
3722 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003723vim_dialog_yesnoallcancel(
3724 int type,
3725 char_u *title,
3726 char_u *message,
3727 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
3729 switch (do_dialog(type,
3730 title == NULL ? (char_u *)"Question" : title,
3731 message,
3732 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003733 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
3735 case 1: return VIM_YES;
3736 case 2: return VIM_NO;
3737 case 3: return VIM_ALL;
3738 case 4: return VIM_DISCARDALL;
3739 }
3740 return VIM_CANCEL;
3741}
3742
3743#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3744
3745#if defined(FEAT_BROWSE) || defined(PROTO)
3746/*
3747 * Generic browse function. Calls gui_mch_browse() when possible.
3748 * Later this may pop-up a non-GUI file selector (external command?).
3749 */
3750 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003751do_browse(
3752 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3753 char_u *title, /* title for the window */
3754 char_u *dflt, /* default file name (may include directory) */
3755 char_u *ext, /* extension added */
3756 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003758 char_u *filter, /* file name filter */
3759 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
3761 char_u *fname;
3762 static char_u *last_dir = NULL; /* last used directory */
3763 char_u *tofree = NULL;
3764 int save_browse = cmdmod.browse;
3765
3766 /* Must turn off browse to avoid that autocommands will get the
3767 * flag too! */
3768 cmdmod.browse = FALSE;
3769
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003770 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003772 if (flags & BROWSE_DIR)
3773 title = (char_u *)_("Select Directory dialog");
3774 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 title = (char_u *)_("Save File dialog");
3776 else
3777 title = (char_u *)_("Open File dialog");
3778 }
3779
3780 /* When no directory specified, use default file name, default dir, buffer
3781 * dir, last dir or current dir */
3782 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3783 {
3784 if (mch_isdir(dflt)) /* default file name is a directory */
3785 {
3786 initdir = dflt;
3787 dflt = NULL;
3788 }
3789 else if (gettail(dflt) != dflt) /* default file name includes a path */
3790 {
3791 tofree = vim_strsave(dflt);
3792 if (tofree != NULL)
3793 {
3794 initdir = tofree;
3795 *gettail(initdir) = NUL;
3796 dflt = gettail(dflt);
3797 }
3798 }
3799 }
3800
3801 if (initdir == NULL || *initdir == NUL)
3802 {
3803 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003804 if (STRCMP(p_bsdir, "last") != 0
3805 && STRCMP(p_bsdir, "buffer") != 0
3806 && STRCMP(p_bsdir, "current") != 0
3807 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 initdir = p_bsdir;
3809 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003810 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 && buf != NULL && buf->b_ffname != NULL)
3812 {
3813 if (dflt == NULL || *dflt == NUL)
3814 dflt = gettail(curbuf->b_ffname);
3815 tofree = vim_strsave(curbuf->b_ffname);
3816 if (tofree != NULL)
3817 {
3818 initdir = tofree;
3819 *gettail(initdir) = NUL;
3820 }
3821 }
3822 /* When 'browsedir' is "last", use dir from last browse */
3823 else if (*p_bsdir == 'l')
3824 initdir = last_dir;
3825 /* When 'browsedir is "current", use current directory. This is the
3826 * default already, leave initdir empty. */
3827 }
3828
3829# ifdef FEAT_GUI
3830 if (gui.in_use) /* when this changes, also adjust f_has()! */
3831 {
3832 if (filter == NULL
3833# ifdef FEAT_EVAL
3834 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3835 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3836# endif
3837 )
3838 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003839 if (flags & BROWSE_DIR)
3840 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003841# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003842 /* For systems that have a directory dialog. */
3843 fname = gui_mch_browsedir(title, initdir);
3844# else
3845 /* Generic solution for selecting a directory: select a file and
3846 * remove the file name. */
3847 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3848# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003849# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003850 /* Win32 adds a dummy file name, others return an arbitrary file
3851 * name. GTK+ 2 returns only the directory, */
3852 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3853 {
3854 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003855 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003856
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003857 if (tail == fname)
3858 *tail++ = '.'; /* use current dir */
3859 *tail = NUL;
3860 }
3861# endif
3862 }
3863 else
3864 fname = gui_mch_browse(flags & BROWSE_SAVE,
3865 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
3867 /* We hang around in the dialog for a while, the user might do some
3868 * things to our files. The Win32 dialog allows deleting or renaming
3869 * a file, check timestamps. */
3870 need_check_timestamps = TRUE;
3871 did_check_timestamps = FALSE;
3872 }
3873 else
3874# endif
3875 {
3876 /* TODO: non-GUI file selector here */
3877 EMSG(_("E338: Sorry, no file browser in console mode"));
3878 fname = NULL;
3879 }
3880
3881 /* keep the directory for next time */
3882 if (fname != NULL)
3883 {
3884 vim_free(last_dir);
3885 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003886 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888 *gettail(last_dir) = NUL;
3889 if (*last_dir == NUL)
3890 {
3891 /* filename only returned, must be in current dir */
3892 vim_free(last_dir);
3893 last_dir = alloc(MAXPATHL);
3894 if (last_dir != NULL)
3895 mch_dirname(last_dir, MAXPATHL);
3896 }
3897 }
3898 }
3899
3900 vim_free(tofree);
3901 cmdmod.browse = save_browse;
3902
3903 return fname;
3904}
3905#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003906
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003907#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908static char *e_printf = N_("E766: Insufficient arguments for printf()");
3909
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003910static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003911static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003912# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003913static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003914# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003915
3916/*
3917 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3918 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003919 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003920tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921{
3922 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003923 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 int err = FALSE;
3925
3926 if (tvs[idx].v_type == VAR_UNKNOWN)
3927 EMSG(_(e_printf));
3928 else
3929 {
3930 ++*idxp;
3931 n = get_tv_number_chk(&tvs[idx], &err);
3932 if (err)
3933 n = 0;
3934 }
3935 return n;
3936}
3937
3938/*
3939 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003940 * If "tofree" is NULL get_tv_string_chk() is used. Some types (e.g. List)
3941 * are not converted to a string.
3942 * If "tofree" is not NULL echo_string() is used. All types are converted to
3943 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00003944 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003945 */
3946 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003947tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003948{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003949 int idx = *idxp - 1;
3950 char *s = NULL;
3951 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003952
3953 if (tvs[idx].v_type == VAR_UNKNOWN)
3954 EMSG(_(e_printf));
3955 else
3956 {
3957 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003958 if (tofree != NULL)
3959 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
3960 else
3961 s = (char *)get_tv_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 }
3963 return s;
3964}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003965
3966# ifdef FEAT_FLOAT
3967/*
3968 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3969 */
3970 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003971tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003972{
3973 int idx = *idxp - 1;
3974 double f = 0;
3975
3976 if (tvs[idx].v_type == VAR_UNKNOWN)
3977 EMSG(_(e_printf));
3978 else
3979 {
3980 ++*idxp;
3981 if (tvs[idx].v_type == VAR_FLOAT)
3982 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003983 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003984 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003985 else
3986 EMSG(_("E807: Expected Float argument for printf()"));
3987 }
3988 return f;
3989}
3990# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991#endif
3992
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02003993#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003994/*
Bram Moolenaare9997822016-08-28 16:03:38 +02003995 * Return the representation of infinity for printf() function:
3996 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
3997 */
3998 static const char *
3999infinity_str(int positive,
4000 char fmt_spec,
4001 int force_sign,
4002 int space_for_positive)
4003{
4004 static const char *table[] =
4005 {
4006 "-inf", "inf", "+inf", " inf",
4007 "-INF", "INF", "+INF", " INF"
4008 };
4009 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4010
4011 if (ASCII_ISUPPER(fmt_spec))
4012 idx += 4;
4013 return table[idx];
4014}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004015#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004016
4017/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004018 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004019 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004020 * consistency.
4021 *
4022 * This code is based on snprintf.c - a portable implementation of snprintf
4023 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004024 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004025 * The original code, including useful comments, can be found here:
4026 * http://www.ijs.si/software/snprintf/
4027 *
4028 * This snprintf() only supports the following conversion specifiers:
4029 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4030 * with flags: '-', '+', ' ', '0' and '#'.
4031 * An asterisk is supported for field width as well as precision.
4032 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004033 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004034 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004035 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4036 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004037 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004038 * The locale is not used, the string is used as a byte string. This is only
4039 * relevant for double-byte encodings where the second byte may be '%'.
4040 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004041 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4042 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004043 *
4044 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004045 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004046 * is greater or equal to "str_m", not all characters from the result
4047 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4048 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004049 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004050 */
4051
4052/*
4053 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054 *
4055 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004056 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004057 */
4058
4059/* When generating prototypes all of this is skipped, cproto doesn't
4060 * understand this. */
4061#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004062
Bram Moolenaara800b422010-06-27 01:15:55 +02004063/* Like vim_vsnprintf() but append to the string. */
4064 int
4065vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4066{
4067 va_list ap;
4068 int str_l;
4069 size_t len = STRLEN(str);
4070 size_t space;
4071
4072 if (str_m <= len)
4073 space = 0;
4074 else
4075 space = str_m - len;
4076 va_start(ap, fmt);
4077 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4078 va_end(ap);
4079 return str_l;
4080}
Bram Moolenaara800b422010-06-27 01:15:55 +02004081
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004082 int
4083vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4084{
4085 va_list ap;
4086 int str_l;
4087
4088 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004089 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004090 va_end(ap);
4091 return str_l;
4092}
4093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004094 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004095vim_vsnprintf(
4096 char *str,
4097 size_t str_m,
4098 char *fmt,
4099 va_list ap,
4100 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004101{
4102 size_t str_l = 0;
4103 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004104 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004105
4106 if (p == NULL)
4107 p = "";
4108 while (*p != NUL)
4109 {
4110 if (*p != '%')
4111 {
4112 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004113 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004114
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004115 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004116 if (str_l < str_m)
4117 {
4118 size_t avail = str_m - str_l;
4119
4120 mch_memmove(str + str_l, p, n > avail ? avail : n);
4121 }
4122 p += n;
4123 str_l += n;
4124 }
4125 else
4126 {
4127 size_t min_field_width = 0, precision = 0;
4128 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4129 int alternate_form = 0, force_sign = 0;
4130
4131 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4132 * ignored. */
4133 int space_for_positive = 1;
4134
4135 /* allowed values: \0, h, l, L */
4136 char length_modifier = '\0';
4137
4138 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004139# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004140# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004141 * That sounds reasonable to use as the maximum
4142 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004143# elif defined(FEAT_NUM64)
4144# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004145# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004146# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004147# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004148 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004149
4150 /* string address in case of string argument */
4151 char *str_arg;
4152
4153 /* natural field width of arg without padding and sign */
4154 size_t str_arg_l;
4155
4156 /* unsigned char argument value - only defined for c conversion.
4157 * N.B. standard explicitly states the char argument for the c
4158 * conversion is unsigned */
4159 unsigned char uchar_arg;
4160
4161 /* number of zeros to be inserted for numeric conversions as
4162 * required by the precision or minimal field width */
4163 size_t number_of_zeros_to_pad = 0;
4164
4165 /* index into tmp where zero padding is to be inserted */
4166 size_t zero_padding_insertion_ind = 0;
4167
4168 /* current conversion specifier character */
4169 char fmt_spec = '\0';
4170
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004171 /* buffer for 's' and 'S' specs */
4172 char_u *tofree = NULL;
4173
4174
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004175 str_arg = NULL;
4176 p++; /* skip '%' */
4177
4178 /* parse flags */
4179 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4180 || *p == '#' || *p == '\'')
4181 {
4182 switch (*p)
4183 {
4184 case '0': zero_padding = 1; break;
4185 case '-': justify_left = 1; break;
4186 case '+': force_sign = 1; space_for_positive = 0; break;
4187 case ' ': force_sign = 1;
4188 /* If both the ' ' and '+' flags appear, the ' '
4189 * flag should be ignored */
4190 break;
4191 case '#': alternate_form = 1; break;
4192 case '\'': break;
4193 }
4194 p++;
4195 }
4196 /* If the '0' and '-' flags both appear, the '0' flag should be
4197 * ignored. */
4198
4199 /* parse field width */
4200 if (*p == '*')
4201 {
4202 int j;
4203
4204 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004207 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208# endif
4209 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004210 if (j >= 0)
4211 min_field_width = j;
4212 else
4213 {
4214 min_field_width = -j;
4215 justify_left = 1;
4216 }
4217 }
4218 else if (VIM_ISDIGIT((int)(*p)))
4219 {
4220 /* size_t could be wider than unsigned int; make sure we treat
4221 * argument like common implementations do */
4222 unsigned int uj = *p++ - '0';
4223
4224 while (VIM_ISDIGIT((int)(*p)))
4225 uj = 10 * uj + (unsigned int)(*p++ - '0');
4226 min_field_width = uj;
4227 }
4228
4229 /* parse precision */
4230 if (*p == '.')
4231 {
4232 p++;
4233 precision_specified = 1;
4234 if (*p == '*')
4235 {
4236 int j;
4237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004240 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241# endif
4242 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004243 p++;
4244 if (j >= 0)
4245 precision = j;
4246 else
4247 {
4248 precision_specified = 0;
4249 precision = 0;
4250 }
4251 }
4252 else if (VIM_ISDIGIT((int)(*p)))
4253 {
4254 /* size_t could be wider than unsigned int; make sure we
4255 * treat argument like common implementations do */
4256 unsigned int uj = *p++ - '0';
4257
4258 while (VIM_ISDIGIT((int)(*p)))
4259 uj = 10 * uj + (unsigned int)(*p++ - '0');
4260 precision = uj;
4261 }
4262 }
4263
4264 /* parse 'h', 'l' and 'll' length modifiers */
4265 if (*p == 'h' || *p == 'l')
4266 {
4267 length_modifier = *p;
4268 p++;
4269 if (length_modifier == 'l' && *p == 'l')
4270 {
4271 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004272# ifdef FEAT_NUM64
4273 length_modifier = 'L';
4274# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004275 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004276# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004277 p++;
4278 }
4279 }
4280 fmt_spec = *p;
4281
4282 /* common synonyms: */
4283 switch (fmt_spec)
4284 {
4285 case 'i': fmt_spec = 'd'; break;
4286 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4287 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4288 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4289 default: break;
4290 }
4291
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004292# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4293 switch (fmt_spec)
4294 {
4295 case 'd': case 'u': case 'o': case 'x': case 'X':
4296 if (tvs != NULL && length_modifier == '\0')
4297 length_modifier = 'L';
4298 }
4299# endif
4300
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004301 /* get parameter value, do initial processing */
4302 switch (fmt_spec)
4303 {
4304 /* '%' and 'c' behave similar to 's' regarding flags and field
4305 * widths */
4306 case '%':
4307 case 'c':
4308 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004309 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004310 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004311 str_arg_l = 1;
4312 switch (fmt_spec)
4313 {
4314 case '%':
4315 str_arg = p;
4316 break;
4317
4318 case 'c':
4319 {
4320 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321
4322 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004324 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004325# endif
4326 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004327 /* standard demands unsigned char */
4328 uchar_arg = (unsigned char)j;
4329 str_arg = (char *)&uchar_arg;
4330 break;
4331 }
4332
4333 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004334 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004337 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338# endif
4339 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004340 if (str_arg == NULL)
4341 {
4342 str_arg = "[NULL]";
4343 str_arg_l = 6;
4344 }
4345 /* make sure not to address string beyond the specified
4346 * precision !!! */
4347 else if (!precision_specified)
4348 str_arg_l = strlen(str_arg);
4349 /* truncate string if necessary as requested by precision */
4350 else if (precision == 0)
4351 str_arg_l = 0;
4352 else
4353 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004354 /* Don't put the #if inside memchr(), it can be a
4355 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004356# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004357 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004358# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004359 /* memchr on HP does not like n > 2^31 !!! */
4360 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004361 precision <= (size_t)0x7fffffffL ? precision
4362 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004363# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004364 str_arg_l = (q == NULL) ? precision
4365 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004366 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004367# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004368 if (fmt_spec == 'S')
4369 {
4370 if (min_field_width != 0)
4371 min_field_width += STRLEN(str_arg)
4372 - mb_string2cells((char_u *)str_arg, -1);
4373 if (precision)
4374 {
4375 char_u *p1 = (char_u *)str_arg;
4376 size_t i;
4377
4378 for (i = 0; i < precision && *p1; i++)
4379 p1 += mb_ptr2len(p1);
4380
4381 str_arg_l = precision = p1 - (char_u *)str_arg;
4382 }
4383 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004384# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004385 break;
4386
4387 default:
4388 break;
4389 }
4390 break;
4391
Bram Moolenaar91984b92016-08-16 21:58:41 +02004392 case 'd': case 'u':
4393 case 'b': case 'B':
4394 case 'o':
4395 case 'x': case 'X':
4396 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004397 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004398 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004399 * imply the value is unsigned; d implies a signed
4400 * value */
4401
4402 /* 0 if numeric argument is zero (or if pointer is
4403 * NULL for 'p'), +1 if greater than zero (or nonzero
4404 * for unsigned arguments), -1 if negative (unsigned
4405 * argument is never negative) */
4406 int arg_sign = 0;
4407
4408 /* only defined for length modifier h, or for no
4409 * length modifiers */
4410 int int_arg = 0;
4411 unsigned int uint_arg = 0;
4412
4413 /* only defined for length modifier l */
4414 long int long_arg = 0;
4415 unsigned long int ulong_arg = 0;
4416
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004417# ifdef FEAT_NUM64
4418 /* only defined for length modifier ll */
4419 varnumber_T llong_arg = 0;
4420 uvarnumber_T ullong_arg = 0;
4421# endif
4422
Bram Moolenaare9997822016-08-28 16:03:38 +02004423 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004424 uvarnumber_T bin_arg = 0;
4425
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004426 /* pointer argument value -only defined for p
4427 * conversion */
4428 void *ptr_arg = NULL;
4429
4430 if (fmt_spec == 'p')
4431 {
4432 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004435 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4436 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437# endif
4438 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004439 if (ptr_arg != NULL)
4440 arg_sign = 1;
4441 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004442 else if (fmt_spec == 'b' || fmt_spec == 'B')
4443 {
4444 bin_arg =
4445# if defined(FEAT_EVAL)
4446 tvs != NULL ?
4447 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4448# endif
4449 va_arg(ap, uvarnumber_T);
4450 if (bin_arg != 0)
4451 arg_sign = 1;
4452 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004453 else if (fmt_spec == 'd')
4454 {
4455 /* signed */
4456 switch (length_modifier)
4457 {
4458 case '\0':
4459 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 /* char and short arguments are passed as int. */
4461 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004463 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464# endif
4465 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004466 if (int_arg > 0)
4467 arg_sign = 1;
4468 else if (int_arg < 0)
4469 arg_sign = -1;
4470 break;
4471 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004473# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004474 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475# endif
4476 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004477 if (long_arg > 0)
4478 arg_sign = 1;
4479 else if (long_arg < 0)
4480 arg_sign = -1;
4481 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004482# ifdef FEAT_NUM64
4483 case 'L':
4484 llong_arg =
4485# if defined(FEAT_EVAL)
4486 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4487# endif
4488 va_arg(ap, varnumber_T);
4489 if (llong_arg > 0)
4490 arg_sign = 1;
4491 else if (llong_arg < 0)
4492 arg_sign = -1;
4493 break;
4494# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004495 }
4496 }
4497 else
4498 {
4499 /* unsigned */
4500 switch (length_modifier)
4501 {
4502 case '\0':
4503 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004506 tvs != NULL ? (unsigned)
4507 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508# endif
4509 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004510 if (uint_arg != 0)
4511 arg_sign = 1;
4512 break;
4513 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004515# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004516 tvs != NULL ? (unsigned long)
4517 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004518# endif
4519 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004520 if (ulong_arg != 0)
4521 arg_sign = 1;
4522 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004523# ifdef FEAT_NUM64
4524 case 'L':
4525 ullong_arg =
4526# if defined(FEAT_EVAL)
4527 tvs != NULL ? (uvarnumber_T)
4528 tv_nr(tvs, &arg_idx) :
4529# endif
4530 va_arg(ap, uvarnumber_T);
4531 if (ullong_arg != 0)
4532 arg_sign = 1;
4533 break;
4534# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004535 }
4536 }
4537
4538 str_arg = tmp;
4539 str_arg_l = 0;
4540
4541 /* NOTE:
4542 * For d, i, u, o, x, and X conversions, if precision is
4543 * specified, the '0' flag should be ignored. This is so
4544 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4545 * FreeBSD, NetBSD; but not with Perl.
4546 */
4547 if (precision_specified)
4548 zero_padding = 0;
4549 if (fmt_spec == 'd')
4550 {
4551 if (force_sign && arg_sign >= 0)
4552 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4553 /* leave negative numbers for sprintf to handle, to
4554 * avoid handling tricky cases like (short int)-32768 */
4555 }
4556 else if (alternate_form)
4557 {
4558 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004559 && (fmt_spec == 'b' || fmt_spec == 'B'
4560 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004561 {
4562 tmp[str_arg_l++] = '0';
4563 tmp[str_arg_l++] = fmt_spec;
4564 }
4565 /* alternate form should have no effect for p
4566 * conversion, but ... */
4567 }
4568
4569 zero_padding_insertion_ind = str_arg_l;
4570 if (!precision_specified)
4571 precision = 1; /* default precision is 1 */
4572 if (precision == 0 && arg_sign == 0)
4573 {
4574 /* When zero value is formatted with an explicit
4575 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004576 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004577 }
4578 else
4579 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004580 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004581 int f_l = 0;
4582
4583 /* construct a simple format string for sprintf */
4584 f[f_l++] = '%';
4585 if (!length_modifier)
4586 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004587 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004588 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004589# ifdef FEAT_NUM64
4590# ifdef WIN3264
4591 f[f_l++] = 'I';
4592 f[f_l++] = '6';
4593 f[f_l++] = '4';
4594# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004595 f[f_l++] = 'l';
4596 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004597# endif
4598# else
4599 f[f_l++] = 'l';
4600# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004601 }
4602 else
4603 f[f_l++] = length_modifier;
4604 f[f_l++] = fmt_spec;
4605 f[f_l++] = '\0';
4606
4607 if (fmt_spec == 'p')
4608 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004609 else if (fmt_spec == 'b' || fmt_spec == 'B')
4610 {
4611 char b[8 * sizeof(uvarnumber_T)];
4612 size_t b_l = 0;
4613 uvarnumber_T bn = bin_arg;
4614
4615 do
4616 {
4617 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4618 bn >>= 1;
4619 }
4620 while (bn != 0);
4621
4622 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4623 str_arg_l += b_l;
4624 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004625 else if (fmt_spec == 'd')
4626 {
4627 /* signed */
4628 switch (length_modifier)
4629 {
4630 case '\0':
4631 case 'h': str_arg_l += sprintf(
4632 tmp + str_arg_l, f, int_arg);
4633 break;
4634 case 'l': str_arg_l += sprintf(
4635 tmp + str_arg_l, f, long_arg);
4636 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004637# ifdef FEAT_NUM64
4638 case 'L': str_arg_l += sprintf(
4639 tmp + str_arg_l, f, llong_arg);
4640 break;
4641# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004642 }
4643 }
4644 else
4645 {
4646 /* unsigned */
4647 switch (length_modifier)
4648 {
4649 case '\0':
4650 case 'h': str_arg_l += sprintf(
4651 tmp + str_arg_l, f, uint_arg);
4652 break;
4653 case 'l': str_arg_l += sprintf(
4654 tmp + str_arg_l, f, ulong_arg);
4655 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004656# ifdef FEAT_NUM64
4657 case 'L': str_arg_l += sprintf(
4658 tmp + str_arg_l, f, ullong_arg);
4659 break;
4660# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004661 }
4662 }
4663
4664 /* include the optional minus sign and possible
4665 * "0x" in the region before the zero padding
4666 * insertion point */
4667 if (zero_padding_insertion_ind < str_arg_l
4668 && tmp[zero_padding_insertion_ind] == '-')
4669 zero_padding_insertion_ind++;
4670 if (zero_padding_insertion_ind + 1 < str_arg_l
4671 && tmp[zero_padding_insertion_ind] == '0'
4672 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4673 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4674 zero_padding_insertion_ind += 2;
4675 }
4676
4677 {
4678 size_t num_of_digits = str_arg_l
4679 - zero_padding_insertion_ind;
4680
4681 if (alternate_form && fmt_spec == 'o'
4682 /* unless zero is already the first
4683 * character */
4684 && !(zero_padding_insertion_ind < str_arg_l
4685 && tmp[zero_padding_insertion_ind] == '0'))
4686 {
4687 /* assure leading zero for alternate-form
4688 * octal numbers */
4689 if (!precision_specified
4690 || precision < num_of_digits + 1)
4691 {
4692 /* precision is increased to force the
4693 * first character to be zero, except if a
4694 * zero value is formatted with an
4695 * explicit precision of zero */
4696 precision = num_of_digits + 1;
4697 precision_specified = 1;
4698 }
4699 }
4700 /* zero padding to specified precision? */
4701 if (num_of_digits < precision)
4702 number_of_zeros_to_pad = precision - num_of_digits;
4703 }
4704 /* zero padding to specified minimal field width? */
4705 if (!justify_left && zero_padding)
4706 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004707 int n = (int)(min_field_width - (str_arg_l
4708 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004709 if (n > 0)
4710 number_of_zeros_to_pad += n;
4711 }
4712 break;
4713 }
4714
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004715# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004716 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004717 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004718 case 'e':
4719 case 'E':
4720 case 'g':
4721 case 'G':
4722 {
4723 /* Floating point. */
4724 double f;
4725 double abs_f;
4726 char format[40];
4727 int l;
4728 int remove_trailing_zeroes = FALSE;
4729
4730 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004731# if defined(FEAT_EVAL)
4732 tvs != NULL ? tv_float(tvs, &arg_idx) :
4733# endif
4734 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004735 abs_f = f < 0 ? -f : f;
4736
4737 if (fmt_spec == 'g' || fmt_spec == 'G')
4738 {
4739 /* Would be nice to use %g directly, but it prints
4740 * "1.0" as "1", we don't want that. */
4741 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4742 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004743 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004744 else
4745 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4746 remove_trailing_zeroes = TRUE;
4747 }
4748
Bram Moolenaar04186092016-08-29 21:55:35 +02004749 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004750# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004751 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004752# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004753 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004754# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004755 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004756 {
4757 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004758 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4759 force_sign, space_for_positive));
4760 str_arg_l = STRLEN(tmp);
4761 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004762 }
4763 else
4764 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004765 if (isnan(f))
4766 {
4767 /* Not a number: nan or NAN */
4768 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4769 : "nan");
4770 str_arg_l = 3;
4771 zero_padding = 0;
4772 }
4773 else if (isinf(f))
4774 {
4775 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4776 force_sign, space_for_positive));
4777 str_arg_l = STRLEN(tmp);
4778 zero_padding = 0;
4779 }
4780 else
Bram Moolenaar04186092016-08-29 21:55:35 +02004781 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004782 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004783 format[0] = '%';
4784 l = 1;
4785 if (force_sign)
4786 format[l++] = space_for_positive ? ' ' : '+';
4787 if (precision_specified)
4788 {
4789 size_t max_prec = TMP_LEN - 10;
4790
4791 /* Make sure we don't get more digits than we
4792 * have room for. */
4793 if ((fmt_spec == 'f' || fmt_spec == 'F')
4794 && abs_f > 1.0)
4795 max_prec -= (size_t)log10(abs_f);
4796 if (precision > max_prec)
4797 precision = max_prec;
4798 l += sprintf(format + l, ".%d", (int)precision);
4799 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004800 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004801 format[l + 1] = NUL;
4802
Bram Moolenaare9997822016-08-28 16:03:38 +02004803 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar04186092016-08-29 21:55:35 +02004804 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004805
Bram Moolenaara7241f52008-06-24 20:39:31 +00004806 if (remove_trailing_zeroes)
4807 {
4808 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004809 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004810
4811 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02004812 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004813 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004814 else
4815 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004816 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004817 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004818 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004819 {
4820 /* Remove superfluous '+' and leading
4821 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004822 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004823 {
4824 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004825 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004826 --str_arg_l;
4827 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004828 i = (tp[1] == '-') ? 2 : 1;
4829 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004830 {
4831 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004832 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004833 --str_arg_l;
4834 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004835 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004836 }
4837 }
4838
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004839 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004840 /* Remove trailing zeroes, but keep the one
4841 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004842 while (tp > tmp + 2 && *tp == '0'
4843 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004844 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004845 STRMOVE(tp, tp + 1);
4846 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004847 --str_arg_l;
4848 }
4849 }
4850 else
4851 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004852 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004853
4854 /* Be consistent: some printf("%e") use 1.0e+12
4855 * and some 1.0e+012. Remove one zero in the last
4856 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004857 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004858 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004859 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4860 && tp[2] == '0'
4861 && vim_isdigit(tp[3])
4862 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004863 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004864 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004865 --str_arg_l;
4866 }
4867 }
4868 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004869 if (zero_padding && min_field_width > str_arg_l
4870 && (tmp[0] == '-' || force_sign))
4871 {
4872 /* padding 0's should be inserted after the sign */
4873 number_of_zeros_to_pad = min_field_width - str_arg_l;
4874 zero_padding_insertion_ind = 1;
4875 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004876 str_arg = tmp;
4877 break;
4878 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004879# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004880
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004881 default:
4882 /* unrecognized conversion specifier, keep format string
4883 * as-is */
4884 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004885 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004886 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004887 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004888
4889 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004890 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004891 str_arg = p;
4892 str_arg_l = 0;
4893 if (*p != NUL)
4894 str_arg_l++; /* include invalid conversion specifier
4895 unchanged if not at end-of-string */
4896 break;
4897 }
4898
4899 if (*p != NUL)
4900 p++; /* step over the just processed conversion specifier */
4901
4902 /* insert padding to the left as requested by min_field_width;
4903 * this does not include the zero padding in case of numerical
4904 * conversions*/
4905 if (!justify_left)
4906 {
4907 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004908 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004909
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004910 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004911 {
4912 if (str_l < str_m)
4913 {
4914 size_t avail = str_m - str_l;
4915
4916 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004917 (size_t)pn > avail ? avail
4918 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004919 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004920 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004921 }
4922 }
4923
4924 /* zero padding as requested by the precision or by the minimal
4925 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004926 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004927 {
4928 /* will not copy first part of numeric right now, *
4929 * force it to be copied later in its entirety */
4930 zero_padding_insertion_ind = 0;
4931 }
4932 else
4933 {
4934 /* insert first part of numerics (sign or '0x') before zero
4935 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004936 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004937
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004938 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004939 {
4940 if (str_l < str_m)
4941 {
4942 size_t avail = str_m - str_l;
4943
4944 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004945 (size_t)zn > avail ? avail
4946 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004947 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004948 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004949 }
4950
4951 /* insert zero padding as requested by the precision or min
4952 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004953 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004954 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004955 {
4956 if (str_l < str_m)
4957 {
4958 size_t avail = str_m-str_l;
4959
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004960 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004961 (size_t)zn > avail ? avail
4962 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004963 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004964 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004965 }
4966 }
4967
4968 /* insert formatted string
4969 * (or as-is conversion specifier for unknown conversions) */
4970 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004971 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004972
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004973 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004974 {
4975 if (str_l < str_m)
4976 {
4977 size_t avail = str_m - str_l;
4978
4979 mch_memmove(str + str_l,
4980 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004981 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004982 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004983 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004984 }
4985 }
4986
4987 /* insert right padding */
4988 if (justify_left)
4989 {
4990 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004991 int pn = (int)(min_field_width
4992 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004993
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004994 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004995 {
4996 if (str_l < str_m)
4997 {
4998 size_t avail = str_m - str_l;
4999
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005000 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005001 (size_t)pn > avail ? avail
5002 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005003 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005004 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005005 }
5006 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005007 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005008 }
5009 }
5010
5011 if (str_m > 0)
5012 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005013 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005014 * overwriting the last character (shouldn't happen, but just in case)
5015 * */
5016 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5017 }
5018
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005019 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005020 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005021
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005022 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005023 * character), that is, the number of characters that would have been
5024 * written to the buffer if it were large enough. */
5025 return (int)str_l;
5026}
5027
5028#endif /* PROTO */