blob: 7dda6fa893419bfb2dd1b9001e56b51aed554ae9 [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
Bram Moolenaar958dc692016-12-01 15:34:12 +010045#ifdef FEAT_JOB_CHANNEL
46static int emsg_to_channel_log = FALSE;
47#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49struct msg_hist
50{
51 struct msg_hist *next;
52 char_u *msg;
53 int attr;
54};
55
56static struct msg_hist *first_msg_hist = NULL;
57static struct msg_hist *last_msg_hist = NULL;
58static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020060static FILE *verbose_fd = NULL;
61static int verbose_did_open = FALSE;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063/*
64 * When writing messages to the screen, there are many different situations.
65 * A number of variables is used to remember the current state:
66 * msg_didany TRUE when messages were written since the last time the
67 * user reacted to a prompt.
68 * Reset: After hitting a key for the hit-return prompt,
69 * hitting <CR> for the command line or input().
70 * Set: When any message is written to the screen.
71 * msg_didout TRUE when something was written to the current line.
72 * Reset: When advancing to the next line, when the current
73 * text can be overwritten.
74 * Set: When any message is written to the screen.
75 * msg_nowait No extra delay for the last drawn message.
76 * Used in normal_cmd() before the mode message is drawn.
77 * emsg_on_display There was an error message recently. Indicates that there
78 * should be a delay before redrawing.
79 * msg_scroll The next message should not overwrite the current one.
80 * msg_scrolled How many lines the screen has been scrolled (because of
81 * messages). Used in update_screen() to scroll the screen
82 * back. Incremented each time the screen scrolls a line.
83 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
84 * writes something without scrolling should not make
85 * need_wait_return to be set. This is a hack to make ":ts"
86 * work without an extra prompt.
87 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010088 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 * need_wait_return TRUE when the hit-return prompt is needed.
90 * Reset: After giving the hit-return prompt, when the user
91 * has answered some other prompt.
92 * Set: When the ruler or typeahead display is overwritten,
93 * scrolling the screen for some message.
94 * keep_msg Message to be displayed after redrawing the screen, in
95 * main_loop().
96 * This is an allocated string or NULL when not used.
97 */
98
99/*
100 * msg(s) - displays the string 's' on the status line
101 * When terminal not initialized (yet) mch_errmsg(..) is used.
102 * return TRUE if wait_return not called
103 */
104 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100105msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106{
107 return msg_attr_keep(s, 0, FALSE);
108}
109
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000111 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000112/*
113 * Like msg() but keep it silent when 'verbosefile' is set.
114 */
115 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100116verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000117{
118 int n;
119
120 verbose_enter();
121 n = msg_attr_keep(s, 0, FALSE);
122 verbose_leave();
123
124 return n;
125}
126#endif
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
131 return msg_attr_keep(s, attr, FALSE);
132}
133
134 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100135msg_attr_keep(
136 char_u *s,
137 int attr,
138 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 static int entered = 0;
141 int retval;
142 char_u *buf = NULL;
143
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200144 /* Skip messages not matching ":filter pattern".
145 * Don't filter when there is an error. */
146 if (!emsg_on_display && message_filtered(s))
147 return TRUE;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#ifdef FEAT_EVAL
150 if (attr == 0)
151 set_vim_var_string(VV_STATUSMSG, s, -1);
152#endif
153
154 /*
155 * It is possible that displaying a messages causes a problem (e.g.,
156 * when redrawing the window), which causes another message, etc.. To
157 * break this loop, limit the recursiveness to 3 levels.
158 */
159 if (entered >= 3)
160 return TRUE;
161 ++entered;
162
163 /* Add message to history (unless it's a repeated kept message or a
164 * truncated message) */
165 if (s != keep_msg
166 || (*s != '<'
167 && last_msg_hist != NULL
168 && last_msg_hist->msg != NULL
169 && STRCMP(s, last_msg_hist->msg)))
170 add_msg_hist(s, -1, attr);
171
Bram Moolenaar958dc692016-12-01 15:34:12 +0100172#ifdef FEAT_JOB_CHANNEL
173 if (emsg_to_channel_log)
174 {
175 /* Write message in the channel log. */
176 ch_logs(NULL, "ERROR: %s", (char *)s);
177 }
178#endif
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 /* When displaying keep_msg, don't let msg_start() free it, caller must do
181 * that. */
182 if (s == keep_msg)
183 keep_msg = NULL;
184
185 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000186 msg_start();
187 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (buf != NULL)
189 s = buf;
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 msg_outtrans_attr(s, attr);
192 msg_clr_eos();
193 retval = msg_end();
194
195 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
196 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000197 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199 vim_free(buf);
200 --entered;
201 return retval;
202}
203
204/*
205 * Truncate a string such that it can be printed without causing a scroll.
206 * Returns an allocated string or NULL when no truncating is done.
207 */
208 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100209msg_strtrunc(
210 char_u *s,
211 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212{
213 char_u *buf = NULL;
214 int len;
215 int room;
216
217 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000218 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
219 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 {
221 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000222 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000223 /* Use all the columns. */
224 room = (int)(Rows - msg_row) * Columns - 1;
225 else
226 /* Use up to 'showcmd' column. */
227 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 if (len > room && room > 0)
229 {
230#ifdef FEAT_MBYTE
231 if (enc_utf8)
232 /* may have up to 18 bytes per cell (6 per char, up to two
233 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100234 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 else if (enc_dbcs == DBCS_JPNU)
236 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100237 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 else
239#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100240 len = room + 2;
241 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100243 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245 }
246 return buf;
247}
248
249/*
250 * Truncate a string "s" to "buf" with cell width "room".
251 * "s" and "buf" may be equal.
252 */
253 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100254trunc_string(
255 char_u *s,
256 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200257 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100258 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200260 size_t room = room_in - 3; /* "..." takes 3 chars */
261 size_t half;
262 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 int e;
264 int i;
265 int n;
266
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200267 if (room_in < 3)
268 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100272 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 {
274 if (s[e] == NUL)
275 {
276 /* text fits without truncating! */
277 buf[e] = NUL;
278 return;
279 }
280 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200281 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 break;
283 len += n;
284 buf[e] = s[e];
285#ifdef FEAT_MBYTE
286 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000287 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100289 if (++e == buflen)
290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 buf[e] = s[e];
292 }
293#endif
294 }
295
296 /* Last part: End of the string. */
297 i = e;
298#ifdef FEAT_MBYTE
299 if (enc_dbcs != 0)
300 {
301 /* For DBCS going backwards in a string is slow, but
302 * computing the cell width isn't too slow: go forward
303 * until the rest fits. */
304 n = vim_strsize(s + i);
305 while (len + n > room)
306 {
307 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000308 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 }
310 }
311 else if (enc_utf8)
312 {
313 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000314 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 for (;;)
316 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000317 do
318 half = half - (*mb_head_off)(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200319 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200321 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 break;
323 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200324 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 }
326 }
327 else
328#endif
329 {
330 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
331 len += n;
332 }
333
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200334
335 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100336 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200337 /* text fits without truncating */
338 if (s != buf)
339 {
340 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200341 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200342 len = buflen - 1;
343 len = len - e + 1;
344 if (len < 1)
345 buf[e - 1] = NUL;
346 else
347 mch_memmove(buf + e, s + e, len);
348 }
349 }
350 else if (e + 3 < buflen)
351 {
352 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100353 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200354 len = STRLEN(s + i) + 1;
355 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100356 len = buflen - e - 3 - 1;
357 mch_memmove(buf + e + 3, s + i, len);
358 buf[e + 3 + len - 1] = NUL;
359 }
360 else
361 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200362 /* can't fit in the "...", just truncate it */
363 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365}
366
367/*
368 * Automatic prototype generation does not understand this function.
369 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
370 * shorter than IOSIZE!!!
371 */
372#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000374int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100377# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380smsg(char_u *s, ...)
381{
382 va_list arglist;
383
384 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000385 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 va_end(arglist);
387 return msg(IObuff);
388}
389
390 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100391# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394smsg_attr(int attr, char_u *s, ...)
395{
396 va_list arglist;
397
398 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000399 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 va_end(arglist);
401 return msg_attr(IObuff, attr);
402}
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#endif
405
406/*
407 * Remember the last sourcing name/lnum used in an error message, so that it
408 * isn't printed each time when it didn't change.
409 */
410static int last_sourcing_lnum = 0;
411static char_u *last_sourcing_name = NULL;
412
413/*
414 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
415 * for the next error message;
416 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000417 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100418reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
420 vim_free(last_sourcing_name);
421 last_sourcing_name = NULL;
422 last_sourcing_lnum = 0;
423}
424
425/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000426 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
427 */
428 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100429other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000430{
431 if (sourcing_name != NULL)
432 {
433 if (last_sourcing_name != NULL)
434 return STRCMP(sourcing_name, last_sourcing_name) != 0;
435 return TRUE;
436 }
437 return FALSE;
438}
439
440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 * Get the message about the source, as used for an error message.
442 * Returns an allocated string with room for one more character.
443 * Returns NULL when no message is to be given.
444 */
445 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100446get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
448 char_u *Buf, *p;
449
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000450 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {
452 p = (char_u *)_("Error detected while processing %s:");
453 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
454 if (Buf != NULL)
455 sprintf((char *)Buf, (char *)p, sourcing_name);
456 return Buf;
457 }
458 return NULL;
459}
460
461/*
462 * Get the message about the source lnum, as used for an error message.
463 * Returns an allocated string with room for one more character.
464 * Returns NULL when no message is to be given.
465 */
466 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100467get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 char_u *Buf, *p;
470
471 /* lnum is 0 when executing a command from the command line
472 * argument, we don't want a line number then */
473 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000474 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 && sourcing_lnum != 0)
476 {
477 p = (char_u *)_("line %4ld:");
478 Buf = alloc((unsigned)(STRLEN(p) + 20));
479 if (Buf != NULL)
480 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
481 return Buf;
482 }
483 return NULL;
484}
485
486/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000487 * Display name and line number for the source of an error.
488 * Remember the file name and line number, so that for the next error the info
489 * is only displayed if it changed.
490 */
491 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100492msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000493{
494 char_u *p;
495
496 ++no_wait_return;
497 p = get_emsg_source();
498 if (p != NULL)
499 {
500 msg_attr(p, attr);
501 vim_free(p);
502 }
503 p = get_emsg_lnum();
504 if (p != NULL)
505 {
506 msg_attr(p, hl_attr(HLF_N));
507 vim_free(p);
508 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
509 }
510
511 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000512 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000513 {
514 vim_free(last_sourcing_name);
515 if (sourcing_name == NULL)
516 last_sourcing_name = NULL;
517 else
518 last_sourcing_name = vim_strsave(sourcing_name);
519 }
520 --no_wait_return;
521}
522
523/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000524 * Return TRUE if not giving error messages right now:
525 * If "emsg_off" is set: no error messages at the moment.
526 * If "msg" is in 'debug': do error message but without side effects.
527 * If "emsg_skip" is set: never do error messages.
528 */
529 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100530emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000531{
532 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
533 && vim_strchr(p_debug, 't') == NULL)
534#ifdef FEAT_EVAL
535 || emsg_skip > 0
536#endif
537 )
538 return TRUE;
539 return FALSE;
540}
541
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200542#if !defined(HAVE_STRERROR) || defined(PROTO)
543/*
544 * Replacement for perror() that behaves more or less like emsg() was called.
545 * v:errmsg will be set and called_emsg will be set.
546 */
547 void
548do_perror(char *msg)
549{
550 perror(msg);
551 ++emsg_silent;
552 emsg((char_u *)msg);
553 --emsg_silent;
554}
555#endif
556
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 * emsg() - display an error message
559 *
560 * Rings the bell, if appropriate, and calls message() to do the real work
561 * When terminal not initialized (yet) mch_errmsg(..) is used.
562 *
563 * return TRUE if wait_return not called
564 */
565 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100566emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100570 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571#ifdef FEAT_EVAL
572 int ignore = FALSE;
573 int severe;
574#endif
575
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100576 /* Skip this if not giving error messages at the moment. */
577 if (emsg_not_now())
578 return TRUE;
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 called_emsg = TRUE;
Bram Moolenaar8b778d52016-02-18 20:31:34 +0100581 if (emsg_silent == 0)
582 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
584 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000585 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
586 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 */
588#ifdef FEAT_EVAL
589 severe = emsg_severe;
590 emsg_severe = FALSE;
591#endif
592
Bram Moolenaar57657d82006-04-21 22:12:41 +0000593 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
595#ifdef FEAT_EVAL
596 /*
597 * Cause a throw of an error exception if appropriate. Don't display
598 * the error message in this case. (If no matching catch clause will
599 * be found, the message will be displayed later on.) "ignore" is set
600 * when the message should be ignored completely (used for the
601 * interrupt message).
602 */
603 if (cause_errthrow(s, severe, &ignore) == TRUE)
604 {
605 if (!ignore)
606 did_emsg = TRUE;
607 return TRUE;
608 }
609
610 /* set "v:errmsg", also when using ":silent! cmd" */
611 set_vim_var_string(VV_ERRMSG, s, -1);
612#endif
613
614 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000615 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 * But do write it to the redirection file.
617 */
618 if (emsg_silent != 0)
619 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200620 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200622 msg_start();
623 p = get_emsg_source();
624 if (p != NULL)
625 {
626 STRCAT(p, "\n");
627 redir_write(p, -1);
628 vim_free(p);
629 }
630 p = get_emsg_lnum();
631 if (p != NULL)
632 {
633 STRCAT(p, "\n");
634 redir_write(p, -1);
635 vim_free(p);
636 }
637 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100639#ifdef FEAT_JOB_CHANNEL
640 ch_logs(NULL, "ERROR: %s", (char *)s);
641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 return TRUE;
643 }
644
645 /* Reset msg_silent, an error causes messages to be switched back on. */
646 msg_silent = 0;
647 cmd_silent = FALSE;
648
649 if (global_busy) /* break :global command */
650 ++global_busy;
651
652 if (p_eb)
653 beep_flush(); /* also includes flush_buffers() */
654 else
655 flush_buffers(FALSE); /* flush internal buffers */
656 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658
659 emsg_on_display = TRUE; /* remember there is an error message */
660 ++msg_scroll; /* don't overwrite a previous message */
661 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000662 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 need_wait_return = TRUE; /* needed in case emsg() is called after
664 * wait_return has reset need_wait_return
665 * and a redraw is expected because
666 * msg_scrolled is non-zero */
667
Bram Moolenaar958dc692016-12-01 15:34:12 +0100668#ifdef FEAT_JOB_CHANNEL
669 emsg_to_channel_log = TRUE;
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 /*
672 * Display name and line number for the source of the error.
673 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000674 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 /*
677 * Display the error message itself.
678 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000679 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar958dc692016-12-01 15:34:12 +0100680 r = msg_attr(s, attr);
681
682#ifdef FEAT_JOB_CHANNEL
683 emsg_to_channel_log = FALSE;
684#endif
685 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686}
687
Bram Moolenaar95f09602016-11-10 20:01:45 +0100688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689/*
690 * Print an error message with one "%s" and one string argument.
691 */
692 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100693emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
695 return emsg3(s, a1, NULL);
696}
697
Bram Moolenaar95f09602016-11-10 20:01:45 +0100698/*
699 * Print an error message with one or two "%s" and one or two string arguments.
700 * This is not in message.c to avoid a warning for prototypes.
701 */
702 int
703emsg3(char_u *s, char_u *a1, char_u *a2)
704{
705 if (emsg_not_now())
706 return TRUE; /* no error messages at the moment */
707 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
708 return emsg(IObuff);
709}
710
711/*
712 * Print an error message with one "%ld" and one long int argument.
713 * This is not in message.c to avoid a warning for prototypes.
714 */
715 int
716emsgn(char_u *s, long n)
717{
718 if (emsg_not_now())
719 return TRUE; /* no error messages at the moment */
720 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
721 return emsg(IObuff);
722}
723
724/*
725 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
726 * defined. It is used for internal errors only, so that they can be
727 * detected when fuzzing vim.
728 */
729 void
730iemsg(char_u *s)
731{
732 msg(s);
733#ifdef ABORT_ON_INTERNAL_ERROR
734 abort();
735#endif
736}
737
738
739/*
740 * Same as emsg2(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
741 * defined. It is used for internal errors only, so that they can be
742 * detected when fuzzing vim.
743 */
744 void
745iemsg2(char_u *s, char_u *a1)
746{
747 emsg2(s, a1);
748#ifdef ABORT_ON_INTERNAL_ERROR
749 abort();
750#endif
751}
752
753/*
754 * Same as emsgn(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
755 * defined. It is used for internal errors only, so that they can be
756 * detected when fuzzing vim.
757 */
758 void
759iemsgn(char_u *s, long n)
760{
761 emsgn(s, n);
762#ifdef ABORT_ON_INTERNAL_ERROR
763 abort();
764#endif
765}
766
767/*
768 * Give an "Internal error" message.
769 */
770 void
771internal_error(char *where)
772{
773 IEMSG2(_(e_intern2), where);
774}
775
Bram Moolenaarea424162005-06-16 21:51:00 +0000776/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaardf177f62005-02-22 08:39:57 +0000778 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100779emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000780{
781 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
782}
783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784/*
785 * Like msg(), but truncate to a single line if p_shm contains 't', or when
786 * "force" is TRUE. This truncates in another way as for normal messages.
787 * Careful: The string may be changed by msg_may_trunc()!
788 * Returns a pointer to the printed message, if wait_return() not called.
789 */
790 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100791msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 int n;
794
795 /* Add message to history before truncating */
796 add_msg_hist(s, -1, attr);
797
798 s = msg_may_trunc(force, s);
799
800 msg_hist_off = TRUE;
801 n = msg_attr(s, attr);
802 msg_hist_off = FALSE;
803
804 if (n)
805 return s;
806 return NULL;
807}
808
809/*
810 * Check if message "s" should be truncated at the start (for filenames).
811 * Return a pointer to where the truncated message starts.
812 * Note: May change the message by replacing a character with '<'.
813 */
814 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100815msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 int n;
818 int room;
819
820 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
821 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
822 && (n = (int)STRLEN(s) - room) > 0)
823 {
824#ifdef FEAT_MBYTE
825 if (has_mbyte)
826 {
827 int size = vim_strsize(s);
828
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000829 /* There may be room anyway when there are multibyte chars. */
830 if (size <= room)
831 return s;
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 for (n = 0; size >= room; )
834 {
835 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000836 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 --n;
839 }
840#endif
841 s += n;
842 *s = '<';
843 }
844 return s;
845}
846
847 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100848add_msg_hist(
849 char_u *s,
850 int len, /* -1 for undetermined length */
851 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 struct msg_hist *p;
854
855 if (msg_hist_off || msg_silent != 0)
856 return;
857
858 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000859 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000860 (void)delete_first_msg();
861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 /* allocate an entry and add the message at the end of the history */
863 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
864 if (p != NULL)
865 {
866 if (len < 0)
867 len = (int)STRLEN(s);
868 /* remove leading and trailing newlines */
869 while (len > 0 && *s == '\n')
870 {
871 ++s;
872 --len;
873 }
874 while (len > 0 && s[len - 1] == '\n')
875 --len;
876 p->msg = vim_strnsave(s, len);
877 p->next = NULL;
878 p->attr = attr;
879 if (last_msg_hist != NULL)
880 last_msg_hist->next = p;
881 last_msg_hist = p;
882 if (first_msg_hist == NULL)
883 first_msg_hist = last_msg_hist;
884 ++msg_hist_len;
885 }
886}
887
888/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000889 * Delete the first (oldest) message from the history.
890 * Returns FAIL if there are no messages.
891 */
892 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100893delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000894{
895 struct msg_hist *p;
896
897 if (msg_hist_len <= 0)
898 return FAIL;
899 p = first_msg_hist;
900 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000901 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200902 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000903 vim_free(p->msg);
904 vim_free(p);
905 --msg_hist_len;
906 return OK;
907}
908
909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 * ":messages" command.
911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200913ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 struct msg_hist *p;
916 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200917 int c = 0;
918
919 if (STRCMP(eap->arg, "clear") == 0)
920 {
921 int keep = eap->addr_count == 0 ? 0 : eap->line2;
922
923 while (msg_hist_len > keep)
924 (void)delete_first_msg();
925 return;
926 }
927
928 if (*eap->arg != NUL)
929 {
930 EMSG(_(e_invarg));
931 return;
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
934 msg_hist_off = TRUE;
935
Bram Moolenaar451f8492016-04-14 17:16:22 +0200936 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200937 if (eap->addr_count != 0)
938 {
939 /* Count total messages */
940 for (; p != NULL && !got_int; p = p->next)
941 c++;
942
943 c -= eap->line2;
944
945 /* Skip without number of messages specified */
946 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
947 p = p->next, c--);
948 }
949
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200950 if (p == first_msg_hist)
951 {
952 s = mch_getenv((char_u *)"LANG");
953 if (s != NULL && *s != NUL)
954 msg_attr((char_u *)
955 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
956 hl_attr(HLF_T));
957 }
958
Bram Moolenaar451f8492016-04-14 17:16:22 +0200959 /* Display what was not skipped. */
960 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (p->msg != NULL)
962 msg_attr(p->msg, p->attr);
963
964 msg_hist_off = FALSE;
965}
966
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000967#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968/*
969 * Call this after prompting the user. This will avoid a hit-return message
970 * and a delay.
971 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000972 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100973msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
975 need_wait_return = FALSE;
976 emsg_on_display = FALSE;
977 cmdline_row = msg_row;
978 msg_col = 0;
979 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100980 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982#endif
983
984/*
985 * wait for the user to hit a key (normally a return)
986 * if 'redraw' is TRUE, clear and redraw the screen
987 * if 'redraw' is FALSE, just redraw the screen
988 * if 'redraw' is -1, don't redraw at all
989 */
990 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100991wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 int c;
994 int oldState;
995 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100997 int save_Recording;
998 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000 if (redraw == TRUE)
1001 must_redraw = CLEAR;
1002
1003 /* If using ":silent cmd", don't wait for a return. Also don't set
1004 * need_wait_return to do it later. */
1005 if (msg_silent != 0)
1006 return;
1007
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001008 /*
1009 * When inside vgetc(), we can't wait for a typed character at all.
1010 * With the global command (and some others) we only need one return at
1011 * the end. Adjust cmdline_row to avoid the next message overwriting the
1012 * last one.
1013 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001014 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001016 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (no_wait_return)
1018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (!exmode_active)
1020 cmdline_row = msg_row;
1021 return;
1022 }
1023
1024 redir_off = TRUE; /* don't redirect this message */
1025 oldState = State;
1026 if (quit_more)
1027 {
1028 c = CAR; /* just pretend CR was hit */
1029 quit_more = FALSE;
1030 got_int = FALSE;
1031 }
1032 else if (exmode_active)
1033 {
1034 MSG_PUTS(" "); /* make sure the cursor is on the right line */
1035 c = CAR; /* no need for a return in ex mode */
1036 got_int = FALSE;
1037 }
1038 else
1039 {
1040 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1041 * just changed. */
1042 screenalloc(FALSE);
1043
1044 State = HITRETURN;
1045#ifdef FEAT_MOUSE
1046 setmouse();
1047#endif
1048#ifdef USE_ON_FLY_SCROLL
1049 dont_scroll = TRUE; /* disallow scrolling here */
1050#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001051 cmdline_row = msg_row;
1052
Bram Moolenaarec38d692013-04-24 15:12:32 +02001053 /* Avoid the sequence that the user types ":" at the hit-return prompt
1054 * to start an Ex command, but the file-changed dialog gets in the
1055 * way. */
1056 if (need_check_timestamps)
1057 check_timestamps(FALSE);
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 hit_return_msg();
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 do
1062 {
1063 /* Remember "got_int", if it is set vgetc() probably returns a
1064 * CTRL-C, but we need to loop then. */
1065 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001066
1067 /* Don't do mappings here, we put the character back in the
1068 * typeahead buffer. */
1069 ++no_mapping;
1070 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001071
1072 /* Temporarily disable Recording. If Recording is active, the
1073 * character will be recorded later, since it will be added to the
1074 * typebuf after the loop */
1075 save_Recording = Recording;
1076 save_scriptout = scriptout;
1077 Recording = FALSE;
1078 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001080 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001082 --no_mapping;
1083 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001084 Recording = save_Recording;
1085 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef FEAT_CLIPBOARD
1088 /* Strange way to allow copying (yanking) a modeless selection at
1089 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1090 * Cmdline-mode and it's harmless when there is no selection. */
1091 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1092 {
1093 clip_copy_modeless_selection(TRUE);
1094 c = K_IGNORE;
1095 }
1096#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001097
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001098 /*
1099 * Allow scrolling back in the messages.
1100 * Also accept scroll-down commands when messages fill the screen,
1101 * to avoid that typing one 'j' too many makes the messages
1102 * disappear.
1103 */
1104 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001105 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001106 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1107 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001108 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001109 if (msg_scrolled > Rows)
1110 /* scroll back to show older messages */
1111 do_more_prompt(c);
1112 else
1113 {
1114 msg_didout = FALSE;
1115 c = K_IGNORE;
1116 msg_col =
1117#ifdef FEAT_RIGHTLEFT
1118 cmdmsg_rl ? Columns - 1 :
1119#endif
1120 0;
1121 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001122 if (quit_more)
1123 {
1124 c = CAR; /* just pretend CR was hit */
1125 quit_more = FALSE;
1126 got_int = FALSE;
1127 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001128 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001129 {
1130 c = K_IGNORE;
1131 hit_return_msg();
1132 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001133 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001134 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001135 && (c == 'j' || c == 'd' || c == 'f'
1136 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001137 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 } while ((had_got_int && c == Ctrl_C)
1140 || c == K_IGNORE
1141#ifdef FEAT_GUI
1142 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1143#endif
1144#ifdef FEAT_MOUSE
1145 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1146 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1147 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001148 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 || c == K_MOUSEDOWN || c == K_MOUSEUP
1150 || (!mouse_has(MOUSE_RETURN)
1151 && mouse_row < msg_row
1152 && (c == K_LEFTMOUSE
1153 || c == K_MIDDLEMOUSE
1154 || c == K_RIGHTMOUSE
1155 || c == K_X1MOUSE
1156 || c == K_X2MOUSE))
1157#endif
1158 );
1159 ui_breakcheck();
1160#ifdef FEAT_MOUSE
1161 /*
1162 * Avoid that the mouse-up event causes visual mode to start.
1163 */
1164 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1165 || c == K_X1MOUSE || c == K_X2MOUSE)
1166 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1167 else
1168#endif
1169 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1170 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001171 /* Put the character back in the typeahead buffer. Don't use the
1172 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001173 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001175 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
1178 redir_off = FALSE;
1179
1180 /*
1181 * If the user hits ':', '?' or '/' we get a command line from the next
1182 * line.
1183 */
1184 if (c == ':' || c == '?' || c == '/')
1185 {
1186 if (!exmode_active)
1187 cmdline_row = msg_row;
1188 skip_redraw = TRUE; /* skip redraw once */
1189 do_redraw = FALSE;
1190 }
1191
1192 /*
1193 * If the window size changed set_shellsize() will redraw the screen.
1194 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1195 * typed.
1196 */
1197 tmpState = State;
1198 State = oldState; /* restore State before set_shellsize */
1199#ifdef FEAT_MOUSE
1200 setmouse();
1201#endif
1202 msg_check();
1203
1204#if defined(UNIX) || defined(VMS)
1205 /*
1206 * When switching screens, we need to output an extra newline on exit.
1207 */
1208 if (swapping_screen() && !termcap_active)
1209 newline_on_exit = TRUE;
1210#endif
1211
1212 need_wait_return = FALSE;
1213 did_wait_return = TRUE;
1214 emsg_on_display = FALSE; /* can delete error message now */
1215 lines_left = -1; /* reset lines_left at next msg_start() */
1216 reset_last_sourcing();
1217 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1218 (Rows - cmdline_row - 1) * Columns + sc_col)
1219 {
1220 vim_free(keep_msg);
1221 keep_msg = NULL; /* don't redisplay message, it's too long */
1222 }
1223
1224 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1225 {
1226 starttermcap(); /* start termcap before redrawing */
1227 shell_resized();
1228 }
1229 else if (!skip_redraw
1230 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1231 {
1232 starttermcap(); /* start termcap before redrawing */
1233 redraw_later(VALID);
1234 }
1235}
1236
1237/*
1238 * Write the hit-return prompt.
1239 */
1240 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001241hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001243 int save_p_more = p_more;
1244
1245 p_more = FALSE; /* don't want see this message when scrolling back */
1246 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 msg_putchar('\n');
1248 if (got_int)
1249 MSG_PUTS(_("Interrupt: "));
1250
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001251 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (!msg_use_printf())
1253 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001254 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255}
1256
1257/*
1258 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1259 */
1260 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001261set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262{
1263 vim_free(keep_msg);
1264 if (s != NULL && msg_silent == 0)
1265 keep_msg = vim_strsave(s);
1266 else
1267 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001268 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001269 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270}
1271
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001272#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1273/*
1274 * If there currently is a message being displayed, set "keep_msg" to it, so
1275 * that it will be displayed again after redraw.
1276 */
1277 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001278set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001279{
1280 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1281 && (State & NORMAL))
1282 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1283}
1284#endif
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286/*
1287 * Prepare for outputting characters in the command line.
1288 */
1289 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001290msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 int did_return = FALSE;
1293
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001294 if (!msg_silent)
1295 {
1296 vim_free(keep_msg);
1297 keep_msg = NULL; /* don't display old message now */
1298 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001299
1300#ifdef FEAT_EVAL
1301 if (need_clr_eos)
1302 {
1303 /* Halfway an ":echo" command and getting an (error) message: clear
1304 * any text from the command. */
1305 need_clr_eos = FALSE;
1306 msg_clr_eos();
1307 }
1308#endif
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (!msg_scroll && full_screen) /* overwrite last message */
1311 {
1312 msg_row = cmdline_row;
1313 msg_col =
1314#ifdef FEAT_RIGHTLEFT
1315 cmdmsg_rl ? Columns - 1 :
1316#endif
1317 0;
1318 }
1319 else if (msg_didout) /* start message on next line */
1320 {
1321 msg_putchar('\n');
1322 did_return = TRUE;
1323 if (exmode_active != EXMODE_NORMAL)
1324 cmdline_row = msg_row;
1325 }
1326 if (!msg_didany || lines_left < 0)
1327 msg_starthere();
1328 if (msg_silent == 0)
1329 {
1330 msg_didout = FALSE; /* no output on current line yet */
1331 cursor_off();
1332 }
1333
1334 /* when redirecting, may need to start a new line. */
1335 if (!did_return)
1336 redir_write((char_u *)"\n", -1);
1337}
1338
1339/*
1340 * Note that the current msg position is where messages start.
1341 */
1342 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001343msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 lines_left = cmdline_row;
1346 msg_didany = FALSE;
1347}
1348
1349 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001350msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 msg_putchar_attr(c, 0);
1353}
1354
1355 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001356msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358#ifdef FEAT_MBYTE
1359 char_u buf[MB_MAXBYTES + 1];
1360#else
1361 char_u buf[4];
1362#endif
1363
1364 if (IS_SPECIAL(c))
1365 {
1366 buf[0] = K_SPECIAL;
1367 buf[1] = K_SECOND(c);
1368 buf[2] = K_THIRD(c);
1369 buf[3] = NUL;
1370 }
1371 else
1372 {
1373#ifdef FEAT_MBYTE
1374 buf[(*mb_char2bytes)(c, buf)] = NUL;
1375#else
1376 buf[0] = c;
1377 buf[1] = NUL;
1378#endif
1379 }
1380 msg_puts_attr(buf, attr);
1381}
1382
1383 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001384msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 char_u buf[20];
1387
1388 sprintf((char *)buf, "%ld", n);
1389 msg_puts(buf);
1390}
1391
1392 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001393msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 msg_home_replace_attr(fname, 0);
1396}
1397
1398#if defined(FEAT_FIND_ID) || defined(PROTO)
1399 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001400msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 msg_home_replace_attr(fname, hl_attr(HLF_D));
1403}
1404#endif
1405
1406 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001407msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *name;
1410
1411 name = home_replace_save(NULL, fname);
1412 if (name != NULL)
1413 msg_outtrans_attr(name, attr);
1414 vim_free(name);
1415}
1416
1417/*
1418 * Output 'len' characters in 'str' (including NULs) with translation
1419 * if 'len' is -1, output upto a NUL character.
1420 * Use attributes 'attr'.
1421 * Return the number of characters it takes on the screen.
1422 */
1423 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001424msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 return msg_outtrans_attr(str, 0);
1427}
1428
1429 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001430msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1433}
1434
1435 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001436msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 return msg_outtrans_len_attr(str, len, 0);
1439}
1440
1441/*
1442 * Output one character at "p". Return pointer to the next character.
1443 * Handles multi-byte characters.
1444 */
1445 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001446msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448#ifdef FEAT_MBYTE
1449 int l;
1450
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001451 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
1453 msg_outtrans_len_attr(p, l, attr);
1454 return p + l;
1455 }
1456#endif
1457 msg_puts_attr(transchar_byte(*p), attr);
1458 return p + 1;
1459}
1460
1461 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001462msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 int retval = 0;
1465 char_u *str = msgstr;
1466 char_u *plain_start = msgstr;
1467 char_u *s;
1468#ifdef FEAT_MBYTE
1469 int mb_l;
1470 int c;
1471#endif
1472
1473 /* if MSG_HIST flag set, add message to history */
1474 if (attr & MSG_HIST)
1475 {
1476 add_msg_hist(str, len, attr);
1477 attr &= ~MSG_HIST;
1478 }
1479
1480#ifdef FEAT_MBYTE
1481 /* If the string starts with a composing character first draw a space on
1482 * which the composing char can be drawn. */
1483 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1484 msg_puts_attr((char_u *)" ", attr);
1485#endif
1486
1487 /*
1488 * Go over the string. Special characters are translated and printed.
1489 * Normal characters are printed several at a time.
1490 */
1491 while (--len >= 0)
1492 {
1493#ifdef FEAT_MBYTE
1494 if (enc_utf8)
1495 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001496 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001498 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 else
1500 mb_l = 1;
1501 if (has_mbyte && mb_l > 1)
1502 {
1503 c = (*mb_ptr2char)(str);
1504 if (vim_isprintc(c))
1505 /* printable multi-byte char: count the cells. */
1506 retval += (*mb_ptr2cells)(str);
1507 else
1508 {
1509 /* unprintable multi-byte char: print the printable chars so
1510 * far and the translation of the unprintable char. */
1511 if (str > plain_start)
1512 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1513 attr);
1514 plain_start = str + mb_l;
1515 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1516 retval += char2cells(c);
1517 }
1518 len -= mb_l - 1;
1519 str += mb_l;
1520 }
1521 else
1522#endif
1523 {
1524 s = transchar_byte(*str);
1525 if (s[1] != NUL)
1526 {
1527 /* unprintable char: print the printable chars so far and the
1528 * translation of the unprintable char. */
1529 if (str > plain_start)
1530 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1531 attr);
1532 plain_start = str + 1;
1533 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001534 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001536 else
1537 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 ++str;
1539 }
1540 }
1541
1542 if (str > plain_start)
1543 /* print the printable chars at the end */
1544 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1545
1546 return retval;
1547}
1548
1549#if defined(FEAT_QUICKFIX) || defined(PROTO)
1550 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001551msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 int i;
1554 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1555
1556 arg = skipwhite(arg);
1557 for (i = 5; *arg && i >= 0; --i)
1558 if (*arg++ != str[i])
1559 break;
1560 if (i < 0)
1561 {
1562 msg_putchar('\n');
1563 for (i = 0; rs[i]; ++i)
1564 msg_putchar(rs[i] - 3);
1565 }
1566}
1567#endif
1568
1569/*
1570 * Output the string 'str' upto a NUL character.
1571 * Return the number of characters it takes on the screen.
1572 *
1573 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1574 * following character and shown as <F1>, <S-Up> etc. Any other character
1575 * which is not printable shown in <> form.
1576 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1577 * If a character is displayed in one of these special ways, is also
1578 * highlighted (its highlight name is '8' in the p_hl variable).
1579 * Otherwise characters are not highlighted.
1580 * This function is used to show mappings, where we want to see how to type
1581 * the character/string -- webb
1582 */
1583 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001584msg_outtrans_special(
1585 char_u *strstart,
1586 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
1588 char_u *str = strstart;
1589 int retval = 0;
1590 char_u *string;
1591 int attr;
1592 int len;
1593
1594 attr = hl_attr(HLF_8);
1595 while (*str != NUL)
1596 {
1597 /* Leading and trailing spaces need to be displayed in <> form. */
1598 if ((str == strstart || str[1] == NUL) && *str == ' ')
1599 {
1600 string = (char_u *)"<Space>";
1601 ++str;
1602 }
1603 else
1604 string = str2special(&str, from);
1605 len = vim_strsize(string);
1606 /* Highlight special keys */
1607 msg_puts_attr(string, len > 1
1608#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001609 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610#endif
1611 ? attr : 0);
1612 retval += len;
1613 }
1614 return retval;
1615}
1616
Bram Moolenaarbd743252010-10-20 21:23:33 +02001617#if defined(FEAT_EVAL) || defined(PROTO)
1618/*
1619 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1620 * strings, in an allocated string.
1621 */
1622 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001623str2special_save(
1624 char_u *str,
1625 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001626{
1627 garray_T ga;
1628 char_u *p = str;
1629
1630 ga_init2(&ga, 1, 40);
1631 while (*p != NUL)
1632 ga_concat(&ga, str2special(&p, is_lhs));
1633 ga_append(&ga, NUL);
1634 return (char_u *)ga.ga_data;
1635}
1636#endif
1637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638/*
1639 * Return the printable string for the key codes at "*sp".
1640 * Used for translating the lhs or rhs of a mapping to printable chars.
1641 * Advances "sp" to the next code.
1642 */
1643 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001644str2special(
1645 char_u **sp,
1646 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 int c;
1649 static char_u buf[7];
1650 char_u *str = *sp;
1651 int modifiers = 0;
1652 int special = FALSE;
1653
1654#ifdef FEAT_MBYTE
1655 if (has_mbyte)
1656 {
1657 char_u *p;
1658
1659 /* Try to un-escape a multi-byte character. Return the un-escaped
1660 * string if it is a multi-byte character. */
1661 p = mb_unescape(sp);
1662 if (p != NULL)
1663 return p;
1664 }
1665#endif
1666
1667 c = *str;
1668 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1669 {
1670 if (str[1] == KS_MODIFIER)
1671 {
1672 modifiers = str[2];
1673 str += 3;
1674 c = *str;
1675 }
1676 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1677 {
1678 c = TO_SPECIAL(str[1], str[2]);
1679 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001680 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 c = NUL;
1682 }
1683 if (IS_SPECIAL(c) || modifiers) /* special key */
1684 special = TRUE;
1685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001688 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001690 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001691
1692 /* For multi-byte characters check for an illegal byte. */
1693 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1694 {
1695 transchar_nonprint(buf, c);
1696 *sp = str + 1;
1697 return buf;
1698 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001699 /* Since 'special' is TRUE the multi-byte character 'c' will be
1700 * processed by get_special_key_name() */
1701 c = (*mb_ptr2char)(str);
1702 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001706 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1709 * Use <Space> only for lhs of a mapping. */
1710 if (special || char2cells(c) > 1 || (from && c == ' '))
1711 return get_special_key_name(c, modifiers);
1712 buf[0] = c;
1713 buf[1] = NUL;
1714 return buf;
1715}
1716
1717/*
1718 * Translate a key sequence into special key names.
1719 */
1720 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001721str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 char_u *s;
1724
1725 *buf = NUL;
1726 while (*sp)
1727 {
1728 s = str2special(&sp, FALSE);
1729 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1730 STRCAT(buf, s);
1731 }
1732}
1733
1734/*
1735 * print line for :print or :list command
1736 */
1737 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001738msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 int c;
1741 int col = 0;
1742 int n_extra = 0;
1743 int c_extra = 0;
1744 char_u *p_extra = NULL; /* init to make SASC shut up */
1745 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001746 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 char_u *trail = NULL;
1748#ifdef FEAT_MBYTE
1749 int l;
1750 char_u buf[MB_MAXBYTES + 1];
1751#endif
1752
Bram Moolenaardf177f62005-02-22 08:39:57 +00001753 if (curwin->w_p_list)
1754 list = TRUE;
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001757 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 trail = s + STRLEN(s);
1760 while (trail > s && vim_iswhite(trail[-1]))
1761 --trail;
1762 }
1763
1764 /* output a space for an empty line, otherwise the line will be
1765 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001766 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 msg_putchar(' ');
1768
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001769 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001771 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 {
1773 --n_extra;
1774 if (c_extra)
1775 c = c_extra;
1776 else
1777 c = *p_extra++;
1778 }
1779#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001780 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
1782 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001783 if (lcs_nbsp != NUL && list
1784 && (mb_ptr2char(s) == 160
1785 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001786 {
1787 mb_char2bytes(lcs_nbsp, buf);
1788 buf[(*mb_ptr2len)(buf)] = NUL;
1789 }
1790 else
1791 {
1792 mch_memmove(buf, s, (size_t)l);
1793 buf[l] = NUL;
1794 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001795 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 s += l;
1797 continue;
1798 }
1799#endif
1800 else
1801 {
1802 attr = 0;
1803 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001804 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
1806 /* tab amount depends on current column */
1807 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001808 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 c = ' ';
1811 c_extra = ' ';
1812 }
1813 else
1814 {
1815 c = lcs_tab1;
1816 c_extra = lcs_tab2;
1817 attr = hl_attr(HLF_8);
1818 }
1819 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001820 else if (c == 160 && list && lcs_nbsp != NUL)
1821 {
1822 c = lcs_nbsp;
1823 attr = hl_attr(HLF_8);
1824 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001825 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
1827 p_extra = (char_u *)"";
1828 c_extra = NUL;
1829 n_extra = 1;
1830 c = lcs_eol;
1831 attr = hl_attr(HLF_AT);
1832 --s;
1833 }
1834 else if (c != NUL && (n = byte2cells(c)) > 1)
1835 {
1836 n_extra = n - 1;
1837 p_extra = transchar_byte(c);
1838 c_extra = NUL;
1839 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001840 /* Use special coloring to be able to distinguish <hex> from
1841 * the same in plain text. */
1842 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 else if (c == ' ' && trail != NULL && s > trail)
1845 {
1846 c = lcs_trail;
1847 attr = hl_attr(HLF_8);
1848 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001849 else if (c == ' ' && list && lcs_space != NUL)
1850 {
1851 c = lcs_space;
1852 attr = hl_attr(HLF_8);
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855
1856 if (c == NUL)
1857 break;
1858
1859 msg_putchar_attr(c, attr);
1860 col++;
1861 }
1862 msg_clr_eos();
1863}
1864
1865#ifdef FEAT_MBYTE
1866/*
1867 * Use screen_puts() to output one multi-byte character.
1868 * Return the pointer "s" advanced to the next character.
1869 */
1870 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001871screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872{
1873 int cw;
1874
1875 msg_didout = TRUE; /* remember that line is not empty */
1876 cw = (*mb_ptr2cells)(s);
1877 if (cw > 1 && (
1878#ifdef FEAT_RIGHTLEFT
1879 cmdmsg_rl ? msg_col <= 1 :
1880#endif
1881 msg_col == Columns - 1))
1882 {
1883 /* Doesn't fit, print a highlighted '>' to fill it up. */
1884 msg_screen_putchar('>', hl_attr(HLF_AT));
1885 return s;
1886 }
1887
1888 screen_puts_len(s, l, msg_row, msg_col, attr);
1889#ifdef FEAT_RIGHTLEFT
1890 if (cmdmsg_rl)
1891 {
1892 msg_col -= cw;
1893 if (msg_col == 0)
1894 {
1895 msg_col = Columns;
1896 ++msg_row;
1897 }
1898 }
1899 else
1900#endif
1901 {
1902 msg_col += cw;
1903 if (msg_col >= Columns)
1904 {
1905 msg_col = 0;
1906 ++msg_row;
1907 }
1908 }
1909 return s + l;
1910}
1911#endif
1912
1913/*
1914 * Output a string to the screen at position msg_row, msg_col.
1915 * Update msg_row and msg_col for the next message.
1916 */
1917 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001918msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001920 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921}
1922
1923 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001924msg_puts_title(
1925 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
1927 msg_puts_attr(s, hl_attr(HLF_T));
1928}
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
1931 * Show a message in such a way that it always fits in the line. Cut out a
1932 * part in the middle and replace it with "..." when necessary.
1933 * Does not handle multi-byte characters!
1934 */
1935 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001936msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001938 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939}
1940
1941 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001942msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943{
1944 int slen = len;
1945 int room;
1946
1947 room = Columns - msg_col;
1948 if (len > room && room >= 20)
1949 {
1950 slen = (room - 3) / 2;
1951 msg_outtrans_len_attr(longstr, slen, attr);
1952 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1953 }
1954 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1955}
1956
1957/*
1958 * Basic function for writing a message with highlight attributes.
1959 */
1960 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001961msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 msg_puts_attr_len(s, -1, attr);
1964}
1965
1966/*
1967 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1968 * When "maxlen" is -1 there is no maximum length.
1969 * When "maxlen" is >= 0 the message is not put in the history.
1970 */
1971 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001972msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 /*
1975 * If redirection is on, also write to the redirection file.
1976 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979 /*
1980 * Don't print anything when using ":silent cmd".
1981 */
1982 if (msg_silent != 0)
1983 return;
1984
1985 /* if MSG_HIST flag set, add message to history */
1986 if ((attr & MSG_HIST) && maxlen < 0)
1987 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001988 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 attr &= ~MSG_HIST;
1990 }
1991
1992 /*
1993 * When writing something to the screen after it has scrolled, requires a
1994 * wait-return prompt later. Needed when scrolling, resetting
1995 * need_wait_return after some prompt, and then outputting something
1996 * without scrolling
1997 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001998 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 need_wait_return = TRUE;
2000 msg_didany = TRUE; /* remember that something was outputted */
2001
2002 /*
2003 * If there is no valid screen, use fprintf so we can see error messages.
2004 * If termcap is not active, we may be writing in an alternate console
2005 * window, cursor positioning may not work correctly (window size may be
2006 * different, e.g. for Win32 console) or we just don't know where the
2007 * cursor is.
2008 */
2009 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002010 msg_puts_printf(str, maxlen);
2011 else
2012 msg_puts_display(str, maxlen, attr, FALSE);
2013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002015/*
2016 * The display part of msg_puts_attr_len().
2017 * May be called recursively to display scroll-back text.
2018 */
2019 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002020msg_puts_display(
2021 char_u *str,
2022 int maxlen,
2023 int attr,
2024 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002025{
2026 char_u *s = str;
2027 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2028 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
2029#ifdef FEAT_MBYTE
2030 int l;
2031 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002033 char_u *sb_str = str;
2034 int sb_col = msg_col;
2035 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002036 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002039 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002042 * We are at the end of the screen line when:
2043 * - When outputting a newline.
2044 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002046 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047#ifdef FEAT_RIGHTLEFT
2048 cmdmsg_rl
2049 ? (
2050 msg_col <= 1
2051 || (*s == TAB && msg_col <= 7)
2052# ifdef FEAT_MBYTE
2053 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
2054# endif
2055 )
2056 :
2057#endif
2058 (msg_col + t_col >= Columns - 1
2059 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
2060# ifdef FEAT_MBYTE
2061 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2062 && msg_col + t_col >= Columns - 2)
2063# endif
2064 ))))
2065 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002066 /*
2067 * The screen is scrolled up when at the last row (some terminals
2068 * scroll automatically, some don't. To avoid problems we scroll
2069 * ourselves).
2070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002073 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002075 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 if (msg_no_more && lines_left == 0)
2077 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002079 /* Scroll the screen up one line. */
2080 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 msg_row = Rows - 2;
2083 if (msg_col >= Columns) /* can happen after screen resize */
2084 msg_col = Columns - 1;
2085
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002086 /* Display char in last column before showing more-prompt. */
2087 if (*s >= ' '
2088#ifdef FEAT_RIGHTLEFT
2089 && !cmdmsg_rl
2090#endif
2091 )
2092 {
2093#ifdef FEAT_MBYTE
2094 if (has_mbyte)
2095 {
2096 if (enc_utf8 && maxlen >= 0)
2097 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002098 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002099 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002100 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002101 s = screen_puts_mbyte(s, l, attr);
2102 }
2103 else
2104#endif
2105 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002106 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002107 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002108 else
2109 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002110
2111 if (p_more)
2112 /* store text for scrolling back */
2113 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2114
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002115 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002116 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (must_redraw < VALID)
2118 must_redraw = VALID;
2119 redraw_cmdline = TRUE;
2120 if (cmdline_row > 0 && !exmode_active)
2121 --cmdline_row;
2122
2123 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002124 * If screen is completely filled and 'more' is set then wait
2125 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002127 if (lines_left > 0)
2128 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002129 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 && !msg_no_more && !exmode_active)
2131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002133 if (do_more_prompt(NUL))
2134 s = confirm_msg_tail;
2135#else
2136 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#endif
2138 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002139 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002141
2142 /* When we displayed a char in last column need to check if there
2143 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002144 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002145 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 || msg_col + t_col >= Columns
2150#ifdef FEAT_MBYTE
2151 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2152 && msg_col + t_col >= Columns - 1)
2153#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002154 ;
2155 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2156 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002158 t_puts(&t_col, t_s, s, attr);
2159
2160 if (wrap && p_more && !recurse)
2161 /* store text for scrolling back */
2162 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164 if (*s == '\n') /* go to next line */
2165 {
2166 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002167#ifdef FEAT_RIGHTLEFT
2168 if (cmdmsg_rl)
2169 msg_col = Columns - 1;
2170 else
2171#endif
2172 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (++msg_row >= Rows) /* safety check */
2174 msg_row = Rows - 1;
2175 }
2176 else if (*s == '\r') /* go to column 0 */
2177 {
2178 msg_col = 0;
2179 }
2180 else if (*s == '\b') /* go to previous char */
2181 {
2182 if (msg_col)
2183 --msg_col;
2184 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002185 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 do
2188 msg_screen_putchar(' ', attr);
2189 while (msg_col & 7);
2190 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002191 else if (*s == BELL) /* beep (from ":sh") */
2192 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else
2194 {
2195#ifdef FEAT_MBYTE
2196 if (has_mbyte)
2197 {
2198 cw = (*mb_ptr2cells)(s);
2199 if (enc_utf8 && maxlen >= 0)
2200 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002201 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002203 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 else
2206 {
2207 cw = 1;
2208 l = 1;
2209 }
2210#endif
2211 /* When drawing from right to left or when a double-wide character
2212 * doesn't fit, draw a single character here. Otherwise collect
2213 * characters and draw them all at once later. */
2214#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2215 if (
2216# ifdef FEAT_RIGHTLEFT
2217 cmdmsg_rl
2218# ifdef FEAT_MBYTE
2219 ||
2220# endif
2221# endif
2222# ifdef FEAT_MBYTE
2223 (cw > 1 && msg_col + t_col >= Columns - 1)
2224# endif
2225 )
2226 {
2227# ifdef FEAT_MBYTE
2228 if (l > 1)
2229 s = screen_puts_mbyte(s, l, attr) - 1;
2230 else
2231# endif
2232 msg_screen_putchar(*s, attr);
2233 }
2234 else
2235#endif
2236 {
2237 /* postpone this character until later */
2238 if (t_col == 0)
2239 t_s = s;
2240#ifdef FEAT_MBYTE
2241 t_col += cw;
2242 s += l - 1;
2243#else
2244 ++t_col;
2245#endif
2246 }
2247 }
2248 ++s;
2249 }
2250
2251 /* output any postponed text */
2252 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002253 t_puts(&t_col, t_s, s, attr);
2254 if (p_more && !recurse)
2255 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
2257 msg_check();
2258}
2259
2260/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002261 * Return TRUE when ":filter pattern" was used and "msg" does not match
2262 * "pattern".
2263 */
2264 int
2265message_filtered(char_u *msg)
2266{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002267 int match;
2268
2269 if (cmdmod.filter_regmatch.regprog == NULL)
2270 return FALSE;
2271 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2272 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002273}
2274
2275/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002276 * Scroll the screen up one line for displaying the next message line.
2277 */
2278 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002279msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002280{
2281#ifdef FEAT_GUI
2282 /* Remove the cursor before scrolling, ScreenLines[] is going
2283 * to become invalid. */
2284 if (gui.in_use)
2285 gui_undraw_cursor();
2286#endif
2287 /* scrolling up always works */
2288 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2289
2290 if (!can_clear((char_u *)" "))
2291 {
2292 /* Scrolling up doesn't result in the right background. Set the
2293 * background here. It's not efficient, but avoids that we have to do
2294 * it all over the code. */
2295 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2296
2297 /* Also clear the last char of the last but one line if it was not
2298 * cleared before to avoid a scroll-up. */
2299 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2300 screen_fill((int)Rows - 2, (int)Rows - 1,
2301 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2302 }
2303}
2304
2305/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002306 * Increment "msg_scrolled".
2307 */
2308 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002309inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002310{
2311#ifdef FEAT_EVAL
2312 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2313 {
2314 char_u *p = sourcing_name;
2315 char_u *tofree = NULL;
2316 int len;
2317
2318 /* v:scrollstart is empty, set it to the script/function name and line
2319 * number */
2320 if (p == NULL)
2321 p = (char_u *)_("Unknown");
2322 else
2323 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002324 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002325 tofree = alloc(len);
2326 if (tofree != NULL)
2327 {
2328 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2329 p, (long)sourcing_lnum);
2330 p = tofree;
2331 }
2332 }
2333 set_vim_var_string(VV_SCROLLSTART, p, -1);
2334 vim_free(tofree);
2335 }
2336#endif
2337 ++msg_scrolled;
2338}
2339
2340/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002341 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2342 * store the displayed text and remember where screen lines start.
2343 */
2344typedef struct msgchunk_S msgchunk_T;
2345struct msgchunk_S
2346{
2347 msgchunk_T *sb_next;
2348 msgchunk_T *sb_prev;
2349 char sb_eol; /* TRUE when line ends after this text */
2350 int sb_msg_col; /* column in which text starts */
2351 int sb_attr; /* text attributes */
2352 char_u sb_text[1]; /* text to be displayed, actually longer */
2353};
2354
2355static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2356
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002357static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2358static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002359
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002360static int do_clear_sb_text = FALSE; /* clear text on next msg */
2361
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002362/*
2363 * Store part of a printed message for displaying when scrolling back.
2364 */
2365 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002366store_sb_text(
2367 char_u **sb_str, /* start of string */
2368 char_u *s, /* just after string */
2369 int attr,
2370 int *sb_col,
2371 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002372{
2373 msgchunk_T *mp;
2374
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002375 if (do_clear_sb_text)
2376 {
2377 clear_sb_text();
2378 do_clear_sb_text = FALSE;
2379 }
2380
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002381 if (s > *sb_str)
2382 {
2383 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2384 if (mp != NULL)
2385 {
2386 mp->sb_eol = finish;
2387 mp->sb_msg_col = *sb_col;
2388 mp->sb_attr = attr;
2389 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2390
2391 if (last_msgchunk == NULL)
2392 {
2393 last_msgchunk = mp;
2394 mp->sb_prev = NULL;
2395 }
2396 else
2397 {
2398 mp->sb_prev = last_msgchunk;
2399 last_msgchunk->sb_next = mp;
2400 last_msgchunk = mp;
2401 }
2402 mp->sb_next = NULL;
2403 }
2404 }
2405 else if (finish && last_msgchunk != NULL)
2406 last_msgchunk->sb_eol = TRUE;
2407
2408 *sb_str = s;
2409 *sb_col = 0;
2410}
2411
2412/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002413 * Finished showing messages, clear the scroll-back text on the next message.
2414 */
2415 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002416may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002417{
2418 do_clear_sb_text = TRUE;
2419}
2420
2421/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002422 * Clear any text remembered for scrolling back.
2423 * Called when redrawing the screen.
2424 */
2425 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002426clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002427{
2428 msgchunk_T *mp;
2429
2430 while (last_msgchunk != NULL)
2431 {
2432 mp = last_msgchunk->sb_prev;
2433 vim_free(last_msgchunk);
2434 last_msgchunk = mp;
2435 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436}
2437
2438/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002439 * "g<" command.
2440 */
2441 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002442show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002443{
2444 msgchunk_T *mp;
2445
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002446 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002447 * weird, typing a command without output results in one line. */
2448 mp = msg_sb_start(last_msgchunk);
2449 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002450 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002451 else
2452 {
2453 do_more_prompt('G');
2454 wait_return(FALSE);
2455 }
2456}
2457
2458/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002459 * Move to the start of screen line in already displayed text.
2460 */
2461 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002462msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002463{
2464 msgchunk_T *mp = mps;
2465
2466 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2467 mp = mp->sb_prev;
2468 return mp;
2469}
2470
2471/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002472 * Mark the last message chunk as finishing the line.
2473 */
2474 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002475msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002476{
2477 if (last_msgchunk != NULL)
2478 last_msgchunk->sb_eol = TRUE;
2479}
2480
2481/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002482 * Display a screen line from previously displayed text at row "row".
2483 * Returns a pointer to the text for the next line (can be NULL).
2484 */
2485 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002486disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002487{
2488 msgchunk_T *mp = smp;
2489 char_u *p;
2490
2491 for (;;)
2492 {
2493 msg_row = row;
2494 msg_col = mp->sb_msg_col;
2495 p = mp->sb_text;
2496 if (*p == '\n') /* don't display the line break */
2497 ++p;
2498 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2499 if (mp->sb_eol || mp->sb_next == NULL)
2500 break;
2501 mp = mp->sb_next;
2502 }
2503 return mp->sb_next;
2504}
2505
2506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 * Output any postponed text for msg_puts_attr_len().
2508 */
2509 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002510t_puts(
2511 int *t_col,
2512 char_u *t_s,
2513 char_u *s,
2514 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 /* output postponed text */
2517 msg_didout = TRUE; /* remember that line is not empty */
2518 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 msg_col += *t_col;
2520 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521#ifdef FEAT_MBYTE
2522 /* If the string starts with a composing character don't increment the
2523 * column position for it. */
2524 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2525 --msg_col;
2526#endif
2527 if (msg_col >= Columns)
2528 {
2529 msg_col = 0;
2530 ++msg_row;
2531 }
2532}
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
2535 * Returns TRUE when messages should be printed with mch_errmsg().
2536 * This is used when there is no valid screen, so we can see error messages.
2537 * If termcap is not active, we may be writing in an alternate console
2538 * window, cursor positioning may not work correctly (window size may be
2539 * different, e.g. for Win32 console) or we just don't know where the
2540 * cursor is.
2541 */
2542 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002543msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 return (!msg_check_screen()
2546#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2547 || !termcap_active
2548#endif
2549 || (swapping_screen() && !termcap_active)
2550 );
2551}
2552
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002553/*
2554 * Print a message when there is no valid screen.
2555 */
2556 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002557msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002558{
2559 char_u *s = str;
2560 char_u buf[4];
2561 char_u *p;
2562
2563#ifdef WIN3264
2564 if (!(silent_mode && p_verbose == 0))
2565 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2566#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002567 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002568 {
2569 if (!(silent_mode && p_verbose == 0))
2570 {
2571 /* NL --> CR NL translation (for Unix, not for "--version") */
2572 /* NL --> CR translation (for Mac) */
2573 p = &buf[0];
2574 if (*s == '\n' && !info_message)
2575 *p++ = '\r';
2576#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2577 else
2578#endif
2579 *p++ = *s;
2580 *p = '\0';
2581 if (info_message) /* informative message, not an error */
2582 mch_msg((char *)buf);
2583 else
2584 mch_errmsg((char *)buf);
2585 }
2586
2587 /* primitive way to compute the current column */
2588#ifdef FEAT_RIGHTLEFT
2589 if (cmdmsg_rl)
2590 {
2591 if (*s == '\r' || *s == '\n')
2592 msg_col = Columns - 1;
2593 else
2594 --msg_col;
2595 }
2596 else
2597#endif
2598 {
2599 if (*s == '\r' || *s == '\n')
2600 msg_col = 0;
2601 else
2602 ++msg_col;
2603 }
2604 ++s;
2605 }
2606 msg_didout = TRUE; /* assume that line is not empty */
2607
2608#ifdef WIN3264
2609 if (!(silent_mode && p_verbose == 0))
2610 mch_settmode(TMODE_RAW);
2611#endif
2612}
2613
2614/*
2615 * Show the more-prompt and handle the user response.
2616 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002617 * When at hit-enter prompt "typed_char" is the already typed character,
2618 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002619 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2620 */
2621 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002622do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002623{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002624 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002625 int used_typed_char = typed_char;
2626 int oldState = State;
2627 int c;
2628#ifdef FEAT_CON_DIALOG
2629 int retval = FALSE;
2630#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002631 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002632 msgchunk_T *mp_last = NULL;
2633 msgchunk_T *mp;
2634 int i;
2635
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002636 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002637 * that case don't show another prompt. Also when at the hit-Enter prompt
2638 * and nothing was typed. */
2639 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002640 return FALSE;
2641 entered = TRUE;
2642
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002643 if (typed_char == 'G')
2644 {
2645 /* "g<": Find first line on the last page. */
2646 mp_last = msg_sb_start(last_msgchunk);
2647 for (i = 0; i < Rows - 2 && mp_last != NULL
2648 && mp_last->sb_prev != NULL; ++i)
2649 mp_last = msg_sb_start(mp_last->sb_prev);
2650 }
2651
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002652 State = ASKMORE;
2653#ifdef FEAT_MOUSE
2654 setmouse();
2655#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002656 if (typed_char == NUL)
2657 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002658 for (;;)
2659 {
2660 /*
2661 * Get a typed character directly from the user.
2662 */
2663 if (used_typed_char != NUL)
2664 {
2665 c = used_typed_char; /* was typed at hit-enter prompt */
2666 used_typed_char = NUL;
2667 }
2668 else
2669 c = get_keystroke();
2670
2671#if defined(FEAT_MENU) && defined(FEAT_GUI)
2672 if (c == K_MENU)
2673 {
2674 int idx = get_menu_index(current_menu, ASKMORE);
2675
2676 /* Used a menu. If it starts with CTRL-Y, it must
2677 * be a "Copy" for the clipboard. Otherwise
2678 * assume that we end */
2679 if (idx == MENU_INDEX_INVALID)
2680 continue;
2681 c = *current_menu->strings[idx];
2682 if (c != NUL && current_menu->strings[idx][1] != NUL)
2683 ins_typebuf(current_menu->strings[idx] + 1,
2684 current_menu->noremap[idx], 0, TRUE,
2685 current_menu->silent[idx]);
2686 }
2687#endif
2688
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002689 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690 switch (c)
2691 {
2692 case BS: /* scroll one line back */
2693 case K_BS:
2694 case 'k':
2695 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002696 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002697 break;
2698
2699 case CAR: /* one extra line */
2700 case NL:
2701 case 'j':
2702 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002703 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002704 break;
2705
2706 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002707 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002708 break;
2709
2710 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002711 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002712 break;
2713
2714 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002715 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002716 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002717 break;
2718
2719 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002720 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 case K_PAGEDOWN:
2722 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002723 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002724 break;
2725
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002726 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002727 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002728 break;
2729
2730 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002731 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002732 lines_left = 999999;
2733 break;
2734
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002735 case ':': /* start new command line */
2736#ifdef FEAT_CON_DIALOG
2737 if (!confirm_msg_used)
2738#endif
2739 {
2740 /* Since got_int is set all typeahead will be flushed, but we
2741 * want to keep this ':', remember that in a special way. */
2742 typeahead_noflush(':');
2743 cmdline_row = Rows - 1; /* put ':' on this line */
2744 skip_redraw = TRUE; /* skip redraw once */
2745 need_wait_return = FALSE; /* don't wait in main() */
2746 }
2747 /*FALLTHROUGH*/
2748 case 'q': /* quit */
2749 case Ctrl_C:
2750 case ESC:
2751#ifdef FEAT_CON_DIALOG
2752 if (confirm_msg_used)
2753 {
2754 /* Jump to the choices of the dialog. */
2755 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002756 }
2757 else
2758#endif
2759 {
2760 got_int = TRUE;
2761 quit_more = TRUE;
2762 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002763 /* When there is some more output (wrapping line) display that
2764 * without another prompt. */
2765 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002766 break;
2767
2768#ifdef FEAT_CLIPBOARD
2769 case Ctrl_Y:
2770 /* Strange way to allow copying (yanking) a modeless
2771 * selection at the more prompt. Use CTRL-Y,
2772 * because the same is used in Cmdline-mode and at the
2773 * hit-enter prompt. However, scrolling one line up
2774 * might be expected... */
2775 if (clip_star.state == SELECT_DONE)
2776 clip_copy_modeless_selection(TRUE);
2777 continue;
2778#endif
2779 default: /* no valid response */
2780 msg_moremsg(TRUE);
2781 continue;
2782 }
2783
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002784 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002786 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787 {
2788 /* go to start of last line */
2789 if (mp_last == NULL)
2790 mp = msg_sb_start(last_msgchunk);
2791 else if (mp_last->sb_prev != NULL)
2792 mp = msg_sb_start(mp_last->sb_prev);
2793 else
2794 mp = NULL;
2795
2796 /* go to start of line at top of the screen */
2797 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2798 ++i)
2799 mp = msg_sb_start(mp->sb_prev);
2800
2801 if (mp != NULL && mp->sb_prev != NULL)
2802 {
2803 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002804 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002805 {
2806 if (mp == NULL || mp->sb_prev == NULL)
2807 break;
2808 mp = msg_sb_start(mp->sb_prev);
2809 if (mp_last == NULL)
2810 mp_last = msg_sb_start(last_msgchunk);
2811 else
2812 mp_last = msg_sb_start(mp_last->sb_prev);
2813 }
2814
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002815 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 (int)Rows, NULL) == OK)
2817 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002818 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819 (void)disp_sb_line(0, mp);
2820 }
2821 else
2822 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002823 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002824 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002825 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2826 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002828 ++msg_scrolled;
2829 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002830 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002831 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002832 }
2833 }
2834 else
2835 {
2836 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002837 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002838 {
2839 /* scroll up, display line at bottom */
2840 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002841 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2843 (int)Columns, ' ', ' ', 0);
2844 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002845 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002846 }
2847 }
2848
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002849 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002850 {
2851 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002852 screen_fill((int)Rows - 1, (int)Rows, 0,
2853 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002854 msg_moremsg(FALSE);
2855 continue;
2856 }
2857
2858 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002859 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002860 }
2861
2862 break;
2863 }
2864
2865 /* clear the --more-- message */
2866 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2867 State = oldState;
2868#ifdef FEAT_MOUSE
2869 setmouse();
2870#endif
2871 if (quit_more)
2872 {
2873 msg_row = Rows - 1;
2874 msg_col = 0;
2875 }
2876#ifdef FEAT_RIGHTLEFT
2877 else if (cmdmsg_rl)
2878 msg_col = Columns - 1;
2879#endif
2880
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002881 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882#ifdef FEAT_CON_DIALOG
2883 return retval;
2884#else
2885 return FALSE;
2886#endif
2887}
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2890
2891#ifdef mch_errmsg
2892# undef mch_errmsg
2893#endif
2894#ifdef mch_msg
2895# undef mch_msg
2896#endif
2897
2898/*
2899 * Give an error message. To be used when the screen hasn't been initialized
2900 * yet. When stderr can't be used, collect error messages until the GUI has
2901 * started and they can be displayed in a message box.
2902 */
2903 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002904mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 int len;
2907
2908#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2909 /* On Unix use stderr if it's a tty.
2910 * When not going to start the GUI also use stderr.
2911 * On Mac, when started from Finder, stderr is the console. */
2912 if (
2913# ifdef UNIX
2914# ifdef MACOS_X_UNIX
2915 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2916# else
2917 isatty(2)
2918# endif
2919# ifdef FEAT_GUI
2920 ||
2921# endif
2922# endif
2923# ifdef FEAT_GUI
2924 !(gui.in_use || gui.starting)
2925# endif
2926 )
2927 {
2928 fprintf(stderr, "%s", str);
2929 return;
2930 }
2931#endif
2932
2933 /* avoid a delay for a message that isn't there */
2934 emsg_on_display = FALSE;
2935
2936 len = (int)STRLEN(str) + 1;
2937 if (error_ga.ga_growsize == 0)
2938 {
2939 error_ga.ga_growsize = 80;
2940 error_ga.ga_itemsize = 1;
2941 }
2942 if (ga_grow(&error_ga, len) == OK)
2943 {
2944 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2945 (char_u *)str, len);
2946#ifdef UNIX
2947 /* remove CR characters, they are displayed */
2948 {
2949 char_u *p;
2950
2951 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2952 for (;;)
2953 {
2954 p = vim_strchr(p, '\r');
2955 if (p == NULL)
2956 break;
2957 *p = ' ';
2958 }
2959 }
2960#endif
2961 --len; /* don't count the NUL at the end */
2962 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964}
2965
2966/*
2967 * Give a message. To be used when the screen hasn't been initialized yet.
2968 * When there is no tty, collect messages until the GUI has started and they
2969 * can be displayed in a message box.
2970 */
2971 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002972mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
2974#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2975 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2976 * uses mch_errmsg() when started from the desktop.
2977 * When not going to start the GUI also use stdout.
2978 * On Mac, when started from Finder, stderr is the console. */
2979 if (
2980# ifdef UNIX
2981# ifdef MACOS_X_UNIX
2982 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2983# else
2984 isatty(2)
2985# endif
2986# ifdef FEAT_GUI
2987 ||
2988# endif
2989# endif
2990# ifdef FEAT_GUI
2991 !(gui.in_use || gui.starting)
2992# endif
2993 )
2994 {
2995 printf("%s", str);
2996 return;
2997 }
2998# endif
2999 mch_errmsg(str);
3000}
3001#endif /* USE_MCH_ERRMSG */
3002
3003/*
3004 * Put a character on the screen at the current message position and advance
3005 * to the next position. Only for printable ASCII!
3006 */
3007 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003008msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 msg_didout = TRUE; /* remember that line is not empty */
3011 screen_putchar(c, msg_row, msg_col, attr);
3012#ifdef FEAT_RIGHTLEFT
3013 if (cmdmsg_rl)
3014 {
3015 if (--msg_col == 0)
3016 {
3017 msg_col = Columns;
3018 ++msg_row;
3019 }
3020 }
3021 else
3022#endif
3023 {
3024 if (++msg_col >= Columns)
3025 {
3026 msg_col = 0;
3027 ++msg_row;
3028 }
3029 }
3030}
3031
3032 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003033msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003035 int attr;
3036 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003039 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041 screen_puts((char_u *)
3042 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3043 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044}
3045
3046/*
3047 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3048 * exmode_active.
3049 */
3050 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003051repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 if (State == ASKMORE)
3054 {
3055 msg_moremsg(TRUE); /* display --more-- message again */
3056 msg_row = Rows - 1;
3057 }
3058#ifdef FEAT_CON_DIALOG
3059 else if (State == CONFIRM)
3060 {
3061 display_confirm_msg(); /* display ":confirm" message again */
3062 msg_row = Rows - 1;
3063 }
3064#endif
3065 else if (State == EXTERNCMD)
3066 {
3067 windgoto(msg_row, msg_col); /* put cursor back */
3068 }
3069 else if (State == HITRETURN || State == SETWSIZE)
3070 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003071 if (msg_row == Rows - 1)
3072 {
3073 /* Avoid drawing the "hit-enter" prompt below the previous one,
3074 * overwrite it. Esp. useful when regaining focus and a
3075 * FocusGained autocmd exists but didn't draw anything. */
3076 msg_didout = FALSE;
3077 msg_col = 0;
3078 msg_clr_eos();
3079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 hit_return_msg();
3081 msg_row = Rows - 1;
3082 }
3083}
3084
3085/*
3086 * msg_check_screen - check if the screen is initialized.
3087 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3088 * While starting the GUI the terminal codes will be set for the GUI, but the
3089 * output goes to the terminal. Don't use the terminal codes then.
3090 */
3091 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003092msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 if (!full_screen || !screen_valid(FALSE))
3095 return FALSE;
3096
3097 if (msg_row >= Rows)
3098 msg_row = Rows - 1;
3099 if (msg_col >= Columns)
3100 msg_col = Columns - 1;
3101 return TRUE;
3102}
3103
3104/*
3105 * Clear from current message position to end of screen.
3106 * Skip this when ":silent" was used, no need to clear for redirection.
3107 */
3108 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003109msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 if (msg_silent == 0)
3112 msg_clr_eos_force();
3113}
3114
3115/*
3116 * Clear from current message position to end of screen.
3117 * Note: msg_col is not updated, so we remember the end of the message
3118 * for msg_check().
3119 */
3120 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003121msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 if (msg_use_printf())
3124 {
3125 if (full_screen) /* only when termcap codes are valid */
3126 {
3127 if (*T_CD)
3128 out_str(T_CD); /* clear to end of display */
3129 else if (*T_CE)
3130 out_str(T_CE); /* clear to end of line */
3131 }
3132 }
3133 else
3134 {
3135#ifdef FEAT_RIGHTLEFT
3136 if (cmdmsg_rl)
3137 {
3138 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3139 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3140 }
3141 else
3142#endif
3143 {
3144 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3145 ' ', ' ', 0);
3146 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3147 }
3148 }
3149}
3150
3151/*
3152 * Clear the command line.
3153 */
3154 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003155msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 msg_row = cmdline_row;
3158 msg_col = 0;
3159 msg_clr_eos_force();
3160}
3161
3162/*
3163 * end putting a message on the screen
3164 * call wait_return if the message does not fit in the available space
3165 * return TRUE if wait_return not called.
3166 */
3167 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003168msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003171 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 * or the ruler option is set and we run into it,
3173 * we have to redraw the window.
3174 * Do not do this if we are abandoning the file or editing the command line.
3175 */
3176 if (!exiting && need_wait_return && !(State & CMDLINE))
3177 {
3178 wait_return(FALSE);
3179 return FALSE;
3180 }
3181 out_flush();
3182 return TRUE;
3183}
3184
3185/*
3186 * If the written message runs into the shown command or ruler, we have to
3187 * wait for hit-return and redraw the window later.
3188 */
3189 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003190msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 if (msg_row == Rows - 1 && msg_col >= sc_col)
3193 {
3194 need_wait_return = TRUE;
3195 redraw_cmdline = TRUE;
3196 }
3197}
3198
3199/*
3200 * May write a string to the redirection file.
3201 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3202 */
3203 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003204redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
3206 char_u *s = str;
3207 static int cur_col = 0;
3208
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003209 /* Don't do anything for displaying prompts and the like. */
3210 if (redir_off)
3211 return;
3212
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003213 /* If 'verbosefile' is set prepare for writing in that file. */
3214 if (*p_vfile != NUL && verbose_fd == NULL)
3215 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003216
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003217 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 /* If the string doesn't start with CR or NL, go to msg_col */
3220 if (*s != '\n' && *s != '\r')
3221 {
3222 while (cur_col < msg_col)
3223 {
3224#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003225 if (redir_execute)
3226 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003227 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003229 else if (redir_vname)
3230 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003233 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003235 if (verbose_fd != NULL)
3236 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 ++cur_col;
3238 }
3239 }
3240
3241#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003242 if (redir_execute)
3243 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003244 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003246 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003247 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248#endif
3249
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003250 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3252 {
3253#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003254 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003256 if (redir_fd != NULL)
3257 putc(*s, redir_fd);
3258 if (verbose_fd != NULL)
3259 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (*s == '\r' || *s == '\n')
3261 cur_col = 0;
3262 else if (*s == '\t')
3263 cur_col += (8 - cur_col % 8);
3264 else
3265 ++cur_col;
3266 ++s;
3267 }
3268
3269 if (msg_silent != 0) /* should update msg_col */
3270 msg_col = cur_col;
3271 }
3272}
3273
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003274 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003275redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003276{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003277 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003278#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003279 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003280#endif
3281 ;
3282}
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003285 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003286 * Must always be called paired with verbose_leave()!
3287 */
3288 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003289verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003290{
3291 if (*p_vfile != NUL)
3292 ++msg_silent;
3293}
3294
3295/*
3296 * After giving verbose message.
3297 * Must always be called paired with verbose_enter()!
3298 */
3299 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003300verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003301{
3302 if (*p_vfile != NUL)
3303 if (--msg_silent < 0)
3304 msg_silent = 0;
3305}
3306
3307/*
3308 * Like verbose_enter() and set msg_scroll when displaying the message.
3309 */
3310 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003311verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003312{
3313 if (*p_vfile != NUL)
3314 ++msg_silent;
3315 else
3316 /* always scroll up, don't overwrite */
3317 msg_scroll = TRUE;
3318}
3319
3320/*
3321 * Like verbose_leave() and set cmdline_row when displaying the message.
3322 */
3323 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003324verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003325{
3326 if (*p_vfile != NUL)
3327 {
3328 if (--msg_silent < 0)
3329 msg_silent = 0;
3330 }
3331 else
3332 cmdline_row = msg_row;
3333}
3334
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003335/*
3336 * Called when 'verbosefile' is set: stop writing to the file.
3337 */
3338 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003339verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003340{
3341 if (verbose_fd != NULL)
3342 {
3343 fclose(verbose_fd);
3344 verbose_fd = NULL;
3345 }
3346 verbose_did_open = FALSE;
3347}
3348
3349/*
3350 * Open the file 'verbosefile'.
3351 * Return FAIL or OK.
3352 */
3353 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003354verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003355{
3356 if (verbose_fd == NULL && !verbose_did_open)
3357 {
3358 /* Only give the error message once. */
3359 verbose_did_open = TRUE;
3360
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003361 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003362 if (verbose_fd == NULL)
3363 {
3364 EMSG2(_(e_notopen), p_vfile);
3365 return FAIL;
3366 }
3367 }
3368 return OK;
3369}
3370
3371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 * Give a warning message (for searching).
3373 * Use 'w' highlighting and may repeat the message after redrawing
3374 */
3375 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003376give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 /* Don't do this for ":silent". */
3379 if (msg_silent != 0)
3380 return;
3381
3382 /* Don't want a hit-enter prompt here. */
3383 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#ifdef FEAT_EVAL
3386 set_vim_var_string(VV_WARNINGMSG, message, -1);
3387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 vim_free(keep_msg);
3389 keep_msg = NULL;
3390 if (hl)
3391 keep_msg_attr = hl_attr(HLF_W);
3392 else
3393 keep_msg_attr = 0;
3394 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003395 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 msg_didout = FALSE; /* overwrite this message */
3397 msg_nowait = TRUE; /* don't wait for this message */
3398 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 --no_wait_return;
3401}
3402
3403/*
3404 * Advance msg cursor to column "col".
3405 */
3406 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003407msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 if (msg_silent != 0) /* nothing to advance to */
3410 {
3411 msg_col = col; /* for redirection, may fill it up later */
3412 return;
3413 }
3414 if (col >= Columns) /* not enough room */
3415 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003416#ifdef FEAT_RIGHTLEFT
3417 if (cmdmsg_rl)
3418 while (msg_col > Columns - col)
3419 msg_putchar(' ');
3420 else
3421#endif
3422 while (msg_col < col)
3423 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424}
3425
3426#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3427/*
3428 * Used for "confirm()" function, and the :confirm command prefix.
3429 * Versions which haven't got flexible dialogs yet, and console
3430 * versions, get this generic handler which uses the command line.
3431 *
3432 * type = one of:
3433 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3434 * title = title string (can be NULL for default)
3435 * (neither used in console dialogs at the moment)
3436 *
3437 * Format of the "buttons" string:
3438 * "Button1Name\nButton2Name\nButton3Name"
3439 * The first button should normally be the default/accept
3440 * The second button should be the 'Cancel' button
3441 * Other buttons- use your imagination!
3442 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3443 * different letter.
3444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003446do_dialog(
3447 int type UNUSED,
3448 char_u *title UNUSED,
3449 char_u *message,
3450 char_u *buttons,
3451 int dfltbutton,
3452 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003453 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003454 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003455 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 int oldState;
3458 int retval = 0;
3459 char_u *hotkeys;
3460 int c;
3461 int i;
3462
3463#ifndef NO_CONSOLE
3464 /* Don't output anything in silent mode ("ex -s") */
3465 if (silent_mode)
3466 return dfltbutton; /* return default option */
3467#endif
3468
3469#ifdef FEAT_GUI_DIALOG
3470 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3471 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3472 {
3473 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003474 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003475 /* avoid a hit-enter prompt without clearing the cmdline */
3476 need_wait_return = FALSE;
3477 emsg_on_display = FALSE;
3478 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 /* Flush output to avoid that further messages and redrawing is done
3481 * in the wrong order. */
3482 out_flush();
3483 gui_mch_update();
3484
3485 return c;
3486 }
3487#endif
3488
3489 oldState = State;
3490 State = CONFIRM;
3491#ifdef FEAT_MOUSE
3492 setmouse();
3493#endif
3494
3495 /*
3496 * Since we wait for a keypress, don't make the
3497 * user press RETURN as well afterwards.
3498 */
3499 ++no_wait_return;
3500 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3501
3502 if (hotkeys != NULL)
3503 {
3504 for (;;)
3505 {
3506 /* Get a typed character directly from the user. */
3507 c = get_keystroke();
3508 switch (c)
3509 {
3510 case CAR: /* User accepts default option */
3511 case NL:
3512 retval = dfltbutton;
3513 break;
3514 case Ctrl_C: /* User aborts/cancels */
3515 case ESC:
3516 retval = 0;
3517 break;
3518 default: /* Could be a hotkey? */
3519 if (c < 0) /* special keys are ignored here */
3520 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003521 if (c == ':' && ex_cmd)
3522 {
3523 retval = dfltbutton;
3524 ins_char_typebuf(':');
3525 break;
3526 }
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 /* Make the character lowercase, as chars in "hotkeys" are. */
3529 c = MB_TOLOWER(c);
3530 retval = 1;
3531 for (i = 0; hotkeys[i]; ++i)
3532 {
3533#ifdef FEAT_MBYTE
3534 if (has_mbyte)
3535 {
3536 if ((*mb_ptr2char)(hotkeys + i) == c)
3537 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003538 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540 else
3541#endif
3542 if (hotkeys[i] == c)
3543 break;
3544 ++retval;
3545 }
3546 if (hotkeys[i])
3547 break;
3548 /* No hotkey match, so keep waiting */
3549 continue;
3550 }
3551 break;
3552 }
3553
3554 vim_free(hotkeys);
3555 }
3556
3557 State = oldState;
3558#ifdef FEAT_MOUSE
3559 setmouse();
3560#endif
3561 --no_wait_return;
3562 msg_end_prompt();
3563
3564 return retval;
3565}
3566
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003567static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569/*
3570 * Copy one character from "*from" to "*to", taking care of multi-byte
3571 * characters. Return the length of the character in bytes.
3572 */
3573 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003574copy_char(
3575 char_u *from,
3576 char_u *to,
3577 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579#ifdef FEAT_MBYTE
3580 int len;
3581 int c;
3582
3583 if (has_mbyte)
3584 {
3585 if (lowercase)
3586 {
3587 c = MB_TOLOWER((*mb_ptr2char)(from));
3588 return (*mb_char2bytes)(c, to);
3589 }
3590 else
3591 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003592 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 mch_memmove(to, from, (size_t)len);
3594 return len;
3595 }
3596 }
3597 else
3598#endif
3599 {
3600 if (lowercase)
3601 *to = (char_u)TOLOWER_LOC(*from);
3602 else
3603 *to = *from;
3604 return 1;
3605 }
3606}
3607
3608/*
3609 * Format the dialog string, and display it at the bottom of
3610 * the screen. Return a string of hotkey chars (if defined) for
3611 * each 'button'. If a button has no hotkey defined, the first character of
3612 * the button is used.
3613 * The hotkeys can be multi-byte characters, but without combining chars.
3614 *
3615 * Returns an allocated string with hotkeys, or NULL for error.
3616 */
3617 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003618msg_show_console_dialog(
3619 char_u *message,
3620 char_u *buttons,
3621 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
3623 int len = 0;
3624#ifdef FEAT_MBYTE
3625# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3626#else
3627# define HOTK_LEN 1
3628#endif
3629 int lenhotkey = HOTK_LEN; /* count first button */
3630 char_u *hotk = NULL;
3631 char_u *msgp = NULL;
3632 char_u *hotkp = NULL;
3633 char_u *r;
3634 int copy;
3635#define HAS_HOTKEY_LEN 30
3636 char_u has_hotkey[HAS_HOTKEY_LEN];
3637 int first_hotkey = FALSE; /* first char of button is hotkey */
3638 int idx;
3639
3640 has_hotkey[0] = FALSE;
3641
3642 /*
3643 * First loop: compute the size of memory to allocate.
3644 * Second loop: copy to the allocated memory.
3645 */
3646 for (copy = 0; copy <= 1; ++copy)
3647 {
3648 r = buttons;
3649 idx = 0;
3650 while (*r)
3651 {
3652 if (*r == DLG_BUTTON_SEP)
3653 {
3654 if (copy)
3655 {
3656 *msgp++ = ',';
3657 *msgp++ = ' '; /* '\n' -> ', ' */
3658
3659 /* advance to next hotkey and set default hotkey */
3660#ifdef FEAT_MBYTE
3661 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003662 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 else
3664#endif
3665 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003666 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (dfltbutton)
3668 --dfltbutton;
3669
3670 /* If no hotkey is specified first char is used. */
3671 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3672 first_hotkey = TRUE;
3673 }
3674 else
3675 {
3676 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3677 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3678 if (idx < HAS_HOTKEY_LEN - 1)
3679 has_hotkey[++idx] = FALSE;
3680 }
3681 }
3682 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3683 {
3684 if (*r == DLG_HOTKEY_CHAR)
3685 ++r;
3686 first_hotkey = FALSE;
3687 if (copy)
3688 {
3689 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3690 *msgp++ = *r;
3691 else
3692 {
3693 /* '&a' -> '[a]' */
3694 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3695 msgp += copy_char(r, msgp, FALSE);
3696 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3697
3698 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003699 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 }
3702 else
3703 {
3704 ++len; /* '&a' -> '[a]' */
3705 if (idx < HAS_HOTKEY_LEN - 1)
3706 has_hotkey[idx] = TRUE;
3707 }
3708 }
3709 else
3710 {
3711 /* everything else copy literally */
3712 if (copy)
3713 msgp += copy_char(r, msgp, FALSE);
3714 }
3715
3716 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003717 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
3720 if (copy)
3721 {
3722 *msgp++ = ':';
3723 *msgp++ = ' ';
3724 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 else
3727 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003728 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003729 + 2 /* for the NL's */
3730 + STRLEN(buttons)
3731 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003732 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 /* If no hotkey is specified first char is used. */
3735 if (!has_hotkey[0])
3736 {
3737 first_hotkey = TRUE;
3738 len += 2; /* "x" -> "[x]" */
3739 }
3740
3741 /*
3742 * Now allocate and load the strings
3743 */
3744 vim_free(confirm_msg);
3745 confirm_msg = alloc(len);
3746 if (confirm_msg == NULL)
3747 return NULL;
3748 *confirm_msg = NUL;
3749 hotk = alloc(lenhotkey);
3750 if (hotk == NULL)
3751 return NULL;
3752
3753 *confirm_msg = '\n';
3754 STRCPY(confirm_msg + 1, message);
3755
3756 msgp = confirm_msg + 1 + STRLEN(message);
3757 hotkp = hotk;
3758
Bram Moolenaar6a516062007-07-05 08:11:42 +00003759 /* Define first default hotkey. Keep the hotkey string NUL
3760 * terminated to avoid reading past the end. */
3761 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /* Remember where the choices start, displaying starts here when
3764 * "hotkp" typed at the more prompt. */
3765 confirm_msg_tail = msgp;
3766 *msgp++ = '\n';
3767 }
3768 }
3769
3770 display_confirm_msg();
3771 return hotk;
3772}
3773
3774/*
3775 * Display the ":confirm" message. Also called when screen resized.
3776 */
3777 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003778display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 /* avoid that 'q' at the more prompt truncates the message here */
3781 ++confirm_msg_used;
3782 if (confirm_msg != NULL)
3783 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3784 --confirm_msg_used;
3785}
3786
3787#endif /* FEAT_CON_DIALOG */
3788
3789#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3790
3791 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003792vim_dialog_yesno(
3793 int type,
3794 char_u *title,
3795 char_u *message,
3796 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
3798 if (do_dialog(type,
3799 title == NULL ? (char_u *)_("Question") : title,
3800 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003801 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return VIM_YES;
3803 return VIM_NO;
3804}
3805
3806 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003807vim_dialog_yesnocancel(
3808 int type,
3809 char_u *title,
3810 char_u *message,
3811 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
3813 switch (do_dialog(type,
3814 title == NULL ? (char_u *)_("Question") : title,
3815 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003816 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 {
3818 case 1: return VIM_YES;
3819 case 2: return VIM_NO;
3820 }
3821 return VIM_CANCEL;
3822}
3823
3824 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003825vim_dialog_yesnoallcancel(
3826 int type,
3827 char_u *title,
3828 char_u *message,
3829 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 switch (do_dialog(type,
3832 title == NULL ? (char_u *)"Question" : title,
3833 message,
3834 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003835 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
3837 case 1: return VIM_YES;
3838 case 2: return VIM_NO;
3839 case 3: return VIM_ALL;
3840 case 4: return VIM_DISCARDALL;
3841 }
3842 return VIM_CANCEL;
3843}
3844
3845#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3846
3847#if defined(FEAT_BROWSE) || defined(PROTO)
3848/*
3849 * Generic browse function. Calls gui_mch_browse() when possible.
3850 * Later this may pop-up a non-GUI file selector (external command?).
3851 */
3852 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003853do_browse(
3854 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3855 char_u *title, /* title for the window */
3856 char_u *dflt, /* default file name (may include directory) */
3857 char_u *ext, /* extension added */
3858 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003860 char_u *filter, /* file name filter */
3861 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
3863 char_u *fname;
3864 static char_u *last_dir = NULL; /* last used directory */
3865 char_u *tofree = NULL;
3866 int save_browse = cmdmod.browse;
3867
3868 /* Must turn off browse to avoid that autocommands will get the
3869 * flag too! */
3870 cmdmod.browse = FALSE;
3871
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003872 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003874 if (flags & BROWSE_DIR)
3875 title = (char_u *)_("Select Directory dialog");
3876 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 title = (char_u *)_("Save File dialog");
3878 else
3879 title = (char_u *)_("Open File dialog");
3880 }
3881
3882 /* When no directory specified, use default file name, default dir, buffer
3883 * dir, last dir or current dir */
3884 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3885 {
3886 if (mch_isdir(dflt)) /* default file name is a directory */
3887 {
3888 initdir = dflt;
3889 dflt = NULL;
3890 }
3891 else if (gettail(dflt) != dflt) /* default file name includes a path */
3892 {
3893 tofree = vim_strsave(dflt);
3894 if (tofree != NULL)
3895 {
3896 initdir = tofree;
3897 *gettail(initdir) = NUL;
3898 dflt = gettail(dflt);
3899 }
3900 }
3901 }
3902
3903 if (initdir == NULL || *initdir == NUL)
3904 {
3905 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003906 if (STRCMP(p_bsdir, "last") != 0
3907 && STRCMP(p_bsdir, "buffer") != 0
3908 && STRCMP(p_bsdir, "current") != 0
3909 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 initdir = p_bsdir;
3911 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003912 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 && buf != NULL && buf->b_ffname != NULL)
3914 {
3915 if (dflt == NULL || *dflt == NUL)
3916 dflt = gettail(curbuf->b_ffname);
3917 tofree = vim_strsave(curbuf->b_ffname);
3918 if (tofree != NULL)
3919 {
3920 initdir = tofree;
3921 *gettail(initdir) = NUL;
3922 }
3923 }
3924 /* When 'browsedir' is "last", use dir from last browse */
3925 else if (*p_bsdir == 'l')
3926 initdir = last_dir;
3927 /* When 'browsedir is "current", use current directory. This is the
3928 * default already, leave initdir empty. */
3929 }
3930
3931# ifdef FEAT_GUI
3932 if (gui.in_use) /* when this changes, also adjust f_has()! */
3933 {
3934 if (filter == NULL
3935# ifdef FEAT_EVAL
3936 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3937 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3938# endif
3939 )
3940 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003941 if (flags & BROWSE_DIR)
3942 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003943# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003944 /* For systems that have a directory dialog. */
3945 fname = gui_mch_browsedir(title, initdir);
3946# else
3947 /* Generic solution for selecting a directory: select a file and
3948 * remove the file name. */
3949 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3950# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003951# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003952 /* Win32 adds a dummy file name, others return an arbitrary file
3953 * name. GTK+ 2 returns only the directory, */
3954 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3955 {
3956 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003957 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003958
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003959 if (tail == fname)
3960 *tail++ = '.'; /* use current dir */
3961 *tail = NUL;
3962 }
3963# endif
3964 }
3965 else
3966 fname = gui_mch_browse(flags & BROWSE_SAVE,
3967 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969 /* We hang around in the dialog for a while, the user might do some
3970 * things to our files. The Win32 dialog allows deleting or renaming
3971 * a file, check timestamps. */
3972 need_check_timestamps = TRUE;
3973 did_check_timestamps = FALSE;
3974 }
3975 else
3976# endif
3977 {
3978 /* TODO: non-GUI file selector here */
3979 EMSG(_("E338: Sorry, no file browser in console mode"));
3980 fname = NULL;
3981 }
3982
3983 /* keep the directory for next time */
3984 if (fname != NULL)
3985 {
3986 vim_free(last_dir);
3987 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003988 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 *gettail(last_dir) = NUL;
3991 if (*last_dir == NUL)
3992 {
3993 /* filename only returned, must be in current dir */
3994 vim_free(last_dir);
3995 last_dir = alloc(MAXPATHL);
3996 if (last_dir != NULL)
3997 mch_dirname(last_dir, MAXPATHL);
3998 }
3999 }
4000 }
4001
4002 vim_free(tofree);
4003 cmdmod.browse = save_browse;
4004
4005 return fname;
4006}
4007#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004008
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004009#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004010static char *e_printf = N_("E766: Insufficient arguments for printf()");
4011
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004012static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004013static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004014# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004015static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004016# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004017
4018/*
4019 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4020 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004021 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004022tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004023{
4024 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004025 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004026 int err = FALSE;
4027
4028 if (tvs[idx].v_type == VAR_UNKNOWN)
4029 EMSG(_(e_printf));
4030 else
4031 {
4032 ++*idxp;
4033 n = get_tv_number_chk(&tvs[idx], &err);
4034 if (err)
4035 n = 0;
4036 }
4037 return n;
4038}
4039
4040/*
4041 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004042 * If "tofree" is NULL get_tv_string_chk() is used. Some types (e.g. List)
4043 * are not converted to a string.
4044 * If "tofree" is not NULL echo_string() is used. All types are converted to
4045 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004046 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 */
4048 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004049tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004050{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004051 int idx = *idxp - 1;
4052 char *s = NULL;
4053 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054
4055 if (tvs[idx].v_type == VAR_UNKNOWN)
4056 EMSG(_(e_printf));
4057 else
4058 {
4059 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004060 if (tofree != NULL)
4061 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4062 else
4063 s = (char *)get_tv_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 }
4065 return s;
4066}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004067
4068# ifdef FEAT_FLOAT
4069/*
4070 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4071 */
4072 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004073tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004074{
4075 int idx = *idxp - 1;
4076 double f = 0;
4077
4078 if (tvs[idx].v_type == VAR_UNKNOWN)
4079 EMSG(_(e_printf));
4080 else
4081 {
4082 ++*idxp;
4083 if (tvs[idx].v_type == VAR_FLOAT)
4084 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004085 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004086 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004087 else
4088 EMSG(_("E807: Expected Float argument for printf()"));
4089 }
4090 return f;
4091}
4092# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093#endif
4094
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004095#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004096/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004097 * Return the representation of infinity for printf() function:
4098 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4099 */
4100 static const char *
4101infinity_str(int positive,
4102 char fmt_spec,
4103 int force_sign,
4104 int space_for_positive)
4105{
4106 static const char *table[] =
4107 {
4108 "-inf", "inf", "+inf", " inf",
4109 "-INF", "INF", "+INF", " INF"
4110 };
4111 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4112
4113 if (ASCII_ISUPPER(fmt_spec))
4114 idx += 4;
4115 return table[idx];
4116}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004117#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004118
4119/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004120 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004121 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004122 * consistency.
4123 *
4124 * This code is based on snprintf.c - a portable implementation of snprintf
4125 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004126 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004127 * The original code, including useful comments, can be found here:
4128 * http://www.ijs.si/software/snprintf/
4129 *
4130 * This snprintf() only supports the following conversion specifiers:
4131 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4132 * with flags: '-', '+', ' ', '0' and '#'.
4133 * An asterisk is supported for field width as well as precision.
4134 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004135 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004136 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004137 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4138 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004139 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004140 * The locale is not used, the string is used as a byte string. This is only
4141 * relevant for double-byte encodings where the second byte may be '%'.
4142 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004143 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4144 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004145 *
4146 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004147 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004148 * is greater or equal to "str_m", not all characters from the result
4149 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4150 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004151 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004152 */
4153
4154/*
4155 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156 *
4157 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004158 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004159 */
4160
4161/* When generating prototypes all of this is skipped, cproto doesn't
4162 * understand this. */
4163#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004164
Bram Moolenaara800b422010-06-27 01:15:55 +02004165/* Like vim_vsnprintf() but append to the string. */
4166 int
4167vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4168{
4169 va_list ap;
4170 int str_l;
4171 size_t len = STRLEN(str);
4172 size_t space;
4173
4174 if (str_m <= len)
4175 space = 0;
4176 else
4177 space = str_m - len;
4178 va_start(ap, fmt);
4179 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4180 va_end(ap);
4181 return str_l;
4182}
Bram Moolenaara800b422010-06-27 01:15:55 +02004183
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184 int
4185vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4186{
4187 va_list ap;
4188 int str_l;
4189
4190 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004192 va_end(ap);
4193 return str_l;
4194}
4195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004197vim_vsnprintf(
4198 char *str,
4199 size_t str_m,
4200 char *fmt,
4201 va_list ap,
4202 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004203{
4204 size_t str_l = 0;
4205 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004206 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207
4208 if (p == NULL)
4209 p = "";
4210 while (*p != NUL)
4211 {
4212 if (*p != '%')
4213 {
4214 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004215 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004216
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004217 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004218 if (str_l < str_m)
4219 {
4220 size_t avail = str_m - str_l;
4221
4222 mch_memmove(str + str_l, p, n > avail ? avail : n);
4223 }
4224 p += n;
4225 str_l += n;
4226 }
4227 else
4228 {
4229 size_t min_field_width = 0, precision = 0;
4230 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4231 int alternate_form = 0, force_sign = 0;
4232
4233 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4234 * ignored. */
4235 int space_for_positive = 1;
4236
4237 /* allowed values: \0, h, l, L */
4238 char length_modifier = '\0';
4239
4240 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004241# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004242# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004243 * That sounds reasonable to use as the maximum
4244 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004245# elif defined(FEAT_NUM64)
4246# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004247# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004248# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004249# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004250 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004251
4252 /* string address in case of string argument */
4253 char *str_arg;
4254
4255 /* natural field width of arg without padding and sign */
4256 size_t str_arg_l;
4257
4258 /* unsigned char argument value - only defined for c conversion.
4259 * N.B. standard explicitly states the char argument for the c
4260 * conversion is unsigned */
4261 unsigned char uchar_arg;
4262
4263 /* number of zeros to be inserted for numeric conversions as
4264 * required by the precision or minimal field width */
4265 size_t number_of_zeros_to_pad = 0;
4266
4267 /* index into tmp where zero padding is to be inserted */
4268 size_t zero_padding_insertion_ind = 0;
4269
4270 /* current conversion specifier character */
4271 char fmt_spec = '\0';
4272
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004273 /* buffer for 's' and 'S' specs */
4274 char_u *tofree = NULL;
4275
4276
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004277 str_arg = NULL;
4278 p++; /* skip '%' */
4279
4280 /* parse flags */
4281 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4282 || *p == '#' || *p == '\'')
4283 {
4284 switch (*p)
4285 {
4286 case '0': zero_padding = 1; break;
4287 case '-': justify_left = 1; break;
4288 case '+': force_sign = 1; space_for_positive = 0; break;
4289 case ' ': force_sign = 1;
4290 /* If both the ' ' and '+' flags appear, the ' '
4291 * flag should be ignored */
4292 break;
4293 case '#': alternate_form = 1; break;
4294 case '\'': break;
4295 }
4296 p++;
4297 }
4298 /* If the '0' and '-' flags both appear, the '0' flag should be
4299 * ignored. */
4300
4301 /* parse field width */
4302 if (*p == '*')
4303 {
4304 int j;
4305
4306 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004307 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004309 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310# endif
4311 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004312 if (j >= 0)
4313 min_field_width = j;
4314 else
4315 {
4316 min_field_width = -j;
4317 justify_left = 1;
4318 }
4319 }
4320 else if (VIM_ISDIGIT((int)(*p)))
4321 {
4322 /* size_t could be wider than unsigned int; make sure we treat
4323 * argument like common implementations do */
4324 unsigned int uj = *p++ - '0';
4325
4326 while (VIM_ISDIGIT((int)(*p)))
4327 uj = 10 * uj + (unsigned int)(*p++ - '0');
4328 min_field_width = uj;
4329 }
4330
4331 /* parse precision */
4332 if (*p == '.')
4333 {
4334 p++;
4335 precision_specified = 1;
4336 if (*p == '*')
4337 {
4338 int j;
4339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004342 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004343# endif
4344 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004345 p++;
4346 if (j >= 0)
4347 precision = j;
4348 else
4349 {
4350 precision_specified = 0;
4351 precision = 0;
4352 }
4353 }
4354 else if (VIM_ISDIGIT((int)(*p)))
4355 {
4356 /* size_t could be wider than unsigned int; make sure we
4357 * treat argument like common implementations do */
4358 unsigned int uj = *p++ - '0';
4359
4360 while (VIM_ISDIGIT((int)(*p)))
4361 uj = 10 * uj + (unsigned int)(*p++ - '0');
4362 precision = uj;
4363 }
4364 }
4365
4366 /* parse 'h', 'l' and 'll' length modifiers */
4367 if (*p == 'h' || *p == 'l')
4368 {
4369 length_modifier = *p;
4370 p++;
4371 if (length_modifier == 'l' && *p == 'l')
4372 {
4373 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004374# ifdef FEAT_NUM64
4375 length_modifier = 'L';
4376# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004377 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004378# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004379 p++;
4380 }
4381 }
4382 fmt_spec = *p;
4383
4384 /* common synonyms: */
4385 switch (fmt_spec)
4386 {
4387 case 'i': fmt_spec = 'd'; break;
4388 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4389 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4390 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4391 default: break;
4392 }
4393
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004394# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4395 switch (fmt_spec)
4396 {
4397 case 'd': case 'u': case 'o': case 'x': case 'X':
4398 if (tvs != NULL && length_modifier == '\0')
4399 length_modifier = 'L';
4400 }
4401# endif
4402
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403 /* get parameter value, do initial processing */
4404 switch (fmt_spec)
4405 {
4406 /* '%' and 'c' behave similar to 's' regarding flags and field
4407 * widths */
4408 case '%':
4409 case 'c':
4410 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004411 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004412 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004413 str_arg_l = 1;
4414 switch (fmt_spec)
4415 {
4416 case '%':
4417 str_arg = p;
4418 break;
4419
4420 case 'c':
4421 {
4422 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423
4424 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004426 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427# endif
4428 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004429 /* standard demands unsigned char */
4430 uchar_arg = (unsigned char)j;
4431 str_arg = (char *)&uchar_arg;
4432 break;
4433 }
4434
4435 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004436 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004439 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440# endif
4441 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004442 if (str_arg == NULL)
4443 {
4444 str_arg = "[NULL]";
4445 str_arg_l = 6;
4446 }
4447 /* make sure not to address string beyond the specified
4448 * precision !!! */
4449 else if (!precision_specified)
4450 str_arg_l = strlen(str_arg);
4451 /* truncate string if necessary as requested by precision */
4452 else if (precision == 0)
4453 str_arg_l = 0;
4454 else
4455 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004456 /* Don't put the #if inside memchr(), it can be a
4457 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004458# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004459 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004460# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461 /* memchr on HP does not like n > 2^31 !!! */
4462 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004463 precision <= (size_t)0x7fffffffL ? precision
4464 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004465# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004466 str_arg_l = (q == NULL) ? precision
4467 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004468 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004469# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004470 if (fmt_spec == 'S')
4471 {
4472 if (min_field_width != 0)
4473 min_field_width += STRLEN(str_arg)
4474 - mb_string2cells((char_u *)str_arg, -1);
4475 if (precision)
4476 {
4477 char_u *p1 = (char_u *)str_arg;
4478 size_t i;
4479
4480 for (i = 0; i < precision && *p1; i++)
4481 p1 += mb_ptr2len(p1);
4482
4483 str_arg_l = precision = p1 - (char_u *)str_arg;
4484 }
4485 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004486# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004487 break;
4488
4489 default:
4490 break;
4491 }
4492 break;
4493
Bram Moolenaar91984b92016-08-16 21:58:41 +02004494 case 'd': case 'u':
4495 case 'b': case 'B':
4496 case 'o':
4497 case 'x': case 'X':
4498 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004499 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004500 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004501 * imply the value is unsigned; d implies a signed
4502 * value */
4503
4504 /* 0 if numeric argument is zero (or if pointer is
4505 * NULL for 'p'), +1 if greater than zero (or nonzero
4506 * for unsigned arguments), -1 if negative (unsigned
4507 * argument is never negative) */
4508 int arg_sign = 0;
4509
4510 /* only defined for length modifier h, or for no
4511 * length modifiers */
4512 int int_arg = 0;
4513 unsigned int uint_arg = 0;
4514
4515 /* only defined for length modifier l */
4516 long int long_arg = 0;
4517 unsigned long int ulong_arg = 0;
4518
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004519# ifdef FEAT_NUM64
4520 /* only defined for length modifier ll */
4521 varnumber_T llong_arg = 0;
4522 uvarnumber_T ullong_arg = 0;
4523# endif
4524
Bram Moolenaare9997822016-08-28 16:03:38 +02004525 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004526 uvarnumber_T bin_arg = 0;
4527
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004528 /* pointer argument value -only defined for p
4529 * conversion */
4530 void *ptr_arg = NULL;
4531
4532 if (fmt_spec == 'p')
4533 {
4534 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004537 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4538 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539# endif
4540 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004541 if (ptr_arg != NULL)
4542 arg_sign = 1;
4543 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004544 else if (fmt_spec == 'b' || fmt_spec == 'B')
4545 {
4546 bin_arg =
4547# if defined(FEAT_EVAL)
4548 tvs != NULL ?
4549 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4550# endif
4551 va_arg(ap, uvarnumber_T);
4552 if (bin_arg != 0)
4553 arg_sign = 1;
4554 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004555 else if (fmt_spec == 'd')
4556 {
4557 /* signed */
4558 switch (length_modifier)
4559 {
4560 case '\0':
4561 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 /* char and short arguments are passed as int. */
4563 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004565 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566# endif
4567 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004568 if (int_arg > 0)
4569 arg_sign = 1;
4570 else if (int_arg < 0)
4571 arg_sign = -1;
4572 break;
4573 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004576 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577# endif
4578 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004579 if (long_arg > 0)
4580 arg_sign = 1;
4581 else if (long_arg < 0)
4582 arg_sign = -1;
4583 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004584# ifdef FEAT_NUM64
4585 case 'L':
4586 llong_arg =
4587# if defined(FEAT_EVAL)
4588 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4589# endif
4590 va_arg(ap, varnumber_T);
4591 if (llong_arg > 0)
4592 arg_sign = 1;
4593 else if (llong_arg < 0)
4594 arg_sign = -1;
4595 break;
4596# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004597 }
4598 }
4599 else
4600 {
4601 /* unsigned */
4602 switch (length_modifier)
4603 {
4604 case '\0':
4605 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004608 tvs != NULL ? (unsigned)
4609 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610# endif
4611 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004612 if (uint_arg != 0)
4613 arg_sign = 1;
4614 break;
4615 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004618 tvs != NULL ? (unsigned long)
4619 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620# endif
4621 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004622 if (ulong_arg != 0)
4623 arg_sign = 1;
4624 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004625# ifdef FEAT_NUM64
4626 case 'L':
4627 ullong_arg =
4628# if defined(FEAT_EVAL)
4629 tvs != NULL ? (uvarnumber_T)
4630 tv_nr(tvs, &arg_idx) :
4631# endif
4632 va_arg(ap, uvarnumber_T);
4633 if (ullong_arg != 0)
4634 arg_sign = 1;
4635 break;
4636# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004637 }
4638 }
4639
4640 str_arg = tmp;
4641 str_arg_l = 0;
4642
4643 /* NOTE:
4644 * For d, i, u, o, x, and X conversions, if precision is
4645 * specified, the '0' flag should be ignored. This is so
4646 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4647 * FreeBSD, NetBSD; but not with Perl.
4648 */
4649 if (precision_specified)
4650 zero_padding = 0;
4651 if (fmt_spec == 'd')
4652 {
4653 if (force_sign && arg_sign >= 0)
4654 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4655 /* leave negative numbers for sprintf to handle, to
4656 * avoid handling tricky cases like (short int)-32768 */
4657 }
4658 else if (alternate_form)
4659 {
4660 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004661 && (fmt_spec == 'b' || fmt_spec == 'B'
4662 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004663 {
4664 tmp[str_arg_l++] = '0';
4665 tmp[str_arg_l++] = fmt_spec;
4666 }
4667 /* alternate form should have no effect for p
4668 * conversion, but ... */
4669 }
4670
4671 zero_padding_insertion_ind = str_arg_l;
4672 if (!precision_specified)
4673 precision = 1; /* default precision is 1 */
4674 if (precision == 0 && arg_sign == 0)
4675 {
4676 /* When zero value is formatted with an explicit
4677 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004678 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004679 }
4680 else
4681 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004682 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004683 int f_l = 0;
4684
4685 /* construct a simple format string for sprintf */
4686 f[f_l++] = '%';
4687 if (!length_modifier)
4688 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004689 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004690 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004691# ifdef FEAT_NUM64
4692# ifdef WIN3264
4693 f[f_l++] = 'I';
4694 f[f_l++] = '6';
4695 f[f_l++] = '4';
4696# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004697 f[f_l++] = 'l';
4698 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004699# endif
4700# else
4701 f[f_l++] = 'l';
4702# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004703 }
4704 else
4705 f[f_l++] = length_modifier;
4706 f[f_l++] = fmt_spec;
4707 f[f_l++] = '\0';
4708
4709 if (fmt_spec == 'p')
4710 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004711 else if (fmt_spec == 'b' || fmt_spec == 'B')
4712 {
4713 char b[8 * sizeof(uvarnumber_T)];
4714 size_t b_l = 0;
4715 uvarnumber_T bn = bin_arg;
4716
4717 do
4718 {
4719 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4720 bn >>= 1;
4721 }
4722 while (bn != 0);
4723
4724 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4725 str_arg_l += b_l;
4726 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004727 else if (fmt_spec == 'd')
4728 {
4729 /* signed */
4730 switch (length_modifier)
4731 {
4732 case '\0':
4733 case 'h': str_arg_l += sprintf(
4734 tmp + str_arg_l, f, int_arg);
4735 break;
4736 case 'l': str_arg_l += sprintf(
4737 tmp + str_arg_l, f, long_arg);
4738 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004739# ifdef FEAT_NUM64
4740 case 'L': str_arg_l += sprintf(
4741 tmp + str_arg_l, f, llong_arg);
4742 break;
4743# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004744 }
4745 }
4746 else
4747 {
4748 /* unsigned */
4749 switch (length_modifier)
4750 {
4751 case '\0':
4752 case 'h': str_arg_l += sprintf(
4753 tmp + str_arg_l, f, uint_arg);
4754 break;
4755 case 'l': str_arg_l += sprintf(
4756 tmp + str_arg_l, f, ulong_arg);
4757 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004758# ifdef FEAT_NUM64
4759 case 'L': str_arg_l += sprintf(
4760 tmp + str_arg_l, f, ullong_arg);
4761 break;
4762# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004763 }
4764 }
4765
4766 /* include the optional minus sign and possible
4767 * "0x" in the region before the zero padding
4768 * insertion point */
4769 if (zero_padding_insertion_ind < str_arg_l
4770 && tmp[zero_padding_insertion_ind] == '-')
4771 zero_padding_insertion_ind++;
4772 if (zero_padding_insertion_ind + 1 < str_arg_l
4773 && tmp[zero_padding_insertion_ind] == '0'
4774 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4775 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4776 zero_padding_insertion_ind += 2;
4777 }
4778
4779 {
4780 size_t num_of_digits = str_arg_l
4781 - zero_padding_insertion_ind;
4782
4783 if (alternate_form && fmt_spec == 'o'
4784 /* unless zero is already the first
4785 * character */
4786 && !(zero_padding_insertion_ind < str_arg_l
4787 && tmp[zero_padding_insertion_ind] == '0'))
4788 {
4789 /* assure leading zero for alternate-form
4790 * octal numbers */
4791 if (!precision_specified
4792 || precision < num_of_digits + 1)
4793 {
4794 /* precision is increased to force the
4795 * first character to be zero, except if a
4796 * zero value is formatted with an
4797 * explicit precision of zero */
4798 precision = num_of_digits + 1;
4799 precision_specified = 1;
4800 }
4801 }
4802 /* zero padding to specified precision? */
4803 if (num_of_digits < precision)
4804 number_of_zeros_to_pad = precision - num_of_digits;
4805 }
4806 /* zero padding to specified minimal field width? */
4807 if (!justify_left && zero_padding)
4808 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004809 int n = (int)(min_field_width - (str_arg_l
4810 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004811 if (n > 0)
4812 number_of_zeros_to_pad += n;
4813 }
4814 break;
4815 }
4816
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004817# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004818 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004819 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004820 case 'e':
4821 case 'E':
4822 case 'g':
4823 case 'G':
4824 {
4825 /* Floating point. */
4826 double f;
4827 double abs_f;
4828 char format[40];
4829 int l;
4830 int remove_trailing_zeroes = FALSE;
4831
4832 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004833# if defined(FEAT_EVAL)
4834 tvs != NULL ? tv_float(tvs, &arg_idx) :
4835# endif
4836 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004837 abs_f = f < 0 ? -f : f;
4838
4839 if (fmt_spec == 'g' || fmt_spec == 'G')
4840 {
4841 /* Would be nice to use %g directly, but it prints
4842 * "1.0" as "1", we don't want that. */
4843 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4844 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004845 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004846 else
4847 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4848 remove_trailing_zeroes = TRUE;
4849 }
4850
Bram Moolenaar04186092016-08-29 21:55:35 +02004851 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004852# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004853 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004854# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004855 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004856# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004857 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004858 {
4859 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004860 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4861 force_sign, space_for_positive));
4862 str_arg_l = STRLEN(tmp);
4863 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004864 }
4865 else
4866 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004867 if (isnan(f))
4868 {
4869 /* Not a number: nan or NAN */
4870 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4871 : "nan");
4872 str_arg_l = 3;
4873 zero_padding = 0;
4874 }
4875 else if (isinf(f))
4876 {
4877 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4878 force_sign, space_for_positive));
4879 str_arg_l = STRLEN(tmp);
4880 zero_padding = 0;
4881 }
4882 else
Bram Moolenaar04186092016-08-29 21:55:35 +02004883 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004884 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004885 format[0] = '%';
4886 l = 1;
4887 if (force_sign)
4888 format[l++] = space_for_positive ? ' ' : '+';
4889 if (precision_specified)
4890 {
4891 size_t max_prec = TMP_LEN - 10;
4892
4893 /* Make sure we don't get more digits than we
4894 * have room for. */
4895 if ((fmt_spec == 'f' || fmt_spec == 'F')
4896 && abs_f > 1.0)
4897 max_prec -= (size_t)log10(abs_f);
4898 if (precision > max_prec)
4899 precision = max_prec;
4900 l += sprintf(format + l, ".%d", (int)precision);
4901 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004902 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004903 format[l + 1] = NUL;
4904
Bram Moolenaare9997822016-08-28 16:03:38 +02004905 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar04186092016-08-29 21:55:35 +02004906 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004907
Bram Moolenaara7241f52008-06-24 20:39:31 +00004908 if (remove_trailing_zeroes)
4909 {
4910 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004911 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004912
4913 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02004914 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004915 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004916 else
4917 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004918 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004919 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004920 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004921 {
4922 /* Remove superfluous '+' and leading
4923 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004924 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004925 {
4926 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004927 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004928 --str_arg_l;
4929 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004930 i = (tp[1] == '-') ? 2 : 1;
4931 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932 {
4933 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004934 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004935 --str_arg_l;
4936 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004937 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004938 }
4939 }
4940
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004941 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004942 /* Remove trailing zeroes, but keep the one
4943 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004944 while (tp > tmp + 2 && *tp == '0'
4945 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004946 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004947 STRMOVE(tp, tp + 1);
4948 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004949 --str_arg_l;
4950 }
4951 }
4952 else
4953 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004954 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004955
4956 /* Be consistent: some printf("%e") use 1.0e+12
4957 * and some 1.0e+012. Remove one zero in the last
4958 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004959 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004960 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004961 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4962 && tp[2] == '0'
4963 && vim_isdigit(tp[3])
4964 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004965 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004966 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004967 --str_arg_l;
4968 }
4969 }
4970 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004971 if (zero_padding && min_field_width > str_arg_l
4972 && (tmp[0] == '-' || force_sign))
4973 {
4974 /* padding 0's should be inserted after the sign */
4975 number_of_zeros_to_pad = min_field_width - str_arg_l;
4976 zero_padding_insertion_ind = 1;
4977 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004978 str_arg = tmp;
4979 break;
4980 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004981# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004982
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004983 default:
4984 /* unrecognized conversion specifier, keep format string
4985 * as-is */
4986 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004987 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004988 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004989 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004990
4991 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004992 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004993 str_arg = p;
4994 str_arg_l = 0;
4995 if (*p != NUL)
4996 str_arg_l++; /* include invalid conversion specifier
4997 unchanged if not at end-of-string */
4998 break;
4999 }
5000
5001 if (*p != NUL)
5002 p++; /* step over the just processed conversion specifier */
5003
5004 /* insert padding to the left as requested by min_field_width;
5005 * this does not include the zero padding in case of numerical
5006 * conversions*/
5007 if (!justify_left)
5008 {
5009 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005010 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005011
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005012 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005013 {
5014 if (str_l < str_m)
5015 {
5016 size_t avail = str_m - str_l;
5017
5018 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005019 (size_t)pn > avail ? avail
5020 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005021 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005022 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005023 }
5024 }
5025
5026 /* zero padding as requested by the precision or by the minimal
5027 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005028 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005029 {
5030 /* will not copy first part of numeric right now, *
5031 * force it to be copied later in its entirety */
5032 zero_padding_insertion_ind = 0;
5033 }
5034 else
5035 {
5036 /* insert first part of numerics (sign or '0x') before zero
5037 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005038 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005039
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005040 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005041 {
5042 if (str_l < str_m)
5043 {
5044 size_t avail = str_m - str_l;
5045
5046 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005047 (size_t)zn > avail ? avail
5048 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005049 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005050 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005051 }
5052
5053 /* insert zero padding as requested by the precision or min
5054 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005055 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005056 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005057 {
5058 if (str_l < str_m)
5059 {
5060 size_t avail = str_m-str_l;
5061
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005062 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005063 (size_t)zn > avail ? avail
5064 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005065 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005066 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005067 }
5068 }
5069
5070 /* insert formatted string
5071 * (or as-is conversion specifier for unknown conversions) */
5072 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005073 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005074
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005075 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005076 {
5077 if (str_l < str_m)
5078 {
5079 size_t avail = str_m - str_l;
5080
5081 mch_memmove(str + str_l,
5082 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005083 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005084 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005085 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005086 }
5087 }
5088
5089 /* insert right padding */
5090 if (justify_left)
5091 {
5092 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005093 int pn = (int)(min_field_width
5094 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005095
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005096 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005097 {
5098 if (str_l < str_m)
5099 {
5100 size_t avail = str_m - str_l;
5101
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005102 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005103 (size_t)pn > avail ? avail
5104 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005105 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005106 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005107 }
5108 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005109 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005110 }
5111 }
5112
5113 if (str_m > 0)
5114 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005115 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005116 * overwriting the last character (shouldn't happen, but just in case)
5117 * */
5118 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5119 }
5120
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005121 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005124 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005125 * character), that is, the number of characters that would have been
5126 * written to the buffer if it were large enough. */
5127 return (int)str_l;
5128}
5129
5130#endif /* PROTO */