blob: 985e1ebbf2fc24473dea1462d2496a32e9c699f8 [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 Moolenaare0c31f62017-03-01 15:07:05 +0100542#ifdef FEAT_EVAL
543static garray_T ignore_error_list = GA_EMPTY;
544
545 void
546ignore_error_for_testing(char_u *error)
547{
548 if (ignore_error_list.ga_itemsize == 0)
549 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
550
551 ga_add_string(&ignore_error_list, error);
552}
553
554 static int
555ignore_error(char_u *msg)
556{
557 int i;
558
559 for (i = 0; i < ignore_error_list.ga_len; ++i)
560 if (strstr((char *)msg,
561 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
562 return TRUE;
563 return FALSE;
564}
565#endif
566
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200567#if !defined(HAVE_STRERROR) || defined(PROTO)
568/*
569 * Replacement for perror() that behaves more or less like emsg() was called.
570 * v:errmsg will be set and called_emsg will be set.
571 */
572 void
573do_perror(char *msg)
574{
575 perror(msg);
576 ++emsg_silent;
577 emsg((char_u *)msg);
578 --emsg_silent;
579}
580#endif
581
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 * emsg() - display an error message
584 *
585 * Rings the bell, if appropriate, and calls message() to do the real work
586 * When terminal not initialized (yet) mch_errmsg(..) is used.
587 *
588 * return TRUE if wait_return not called
589 */
590 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100591emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100595 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#ifdef FEAT_EVAL
597 int ignore = FALSE;
598 int severe;
599#endif
600
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100601 /* Skip this if not giving error messages at the moment. */
602 if (emsg_not_now())
603 return TRUE;
604
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100605#ifdef FEAT_EVAL
606 /* When testing some errors are turned into a normal message. */
607 if (ignore_error(s))
608 return msg(s);
609#endif
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 called_emsg = TRUE;
612
613 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000614 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
615 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 */
617#ifdef FEAT_EVAL
618 severe = emsg_severe;
619 emsg_severe = FALSE;
620#endif
621
Bram Moolenaar57657d82006-04-21 22:12:41 +0000622 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {
624#ifdef FEAT_EVAL
625 /*
626 * Cause a throw of an error exception if appropriate. Don't display
627 * the error message in this case. (If no matching catch clause will
628 * be found, the message will be displayed later on.) "ignore" is set
629 * when the message should be ignored completely (used for the
630 * interrupt message).
631 */
632 if (cause_errthrow(s, severe, &ignore) == TRUE)
633 {
634 if (!ignore)
635 did_emsg = TRUE;
636 return TRUE;
637 }
638
639 /* set "v:errmsg", also when using ":silent! cmd" */
640 set_vim_var_string(VV_ERRMSG, s, -1);
641#endif
642
643 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000644 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 * But do write it to the redirection file.
646 */
647 if (emsg_silent != 0)
648 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200649 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200651 msg_start();
652 p = get_emsg_source();
653 if (p != NULL)
654 {
655 STRCAT(p, "\n");
656 redir_write(p, -1);
657 vim_free(p);
658 }
659 p = get_emsg_lnum();
660 if (p != NULL)
661 {
662 STRCAT(p, "\n");
663 redir_write(p, -1);
664 vim_free(p);
665 }
666 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100668#ifdef FEAT_JOB_CHANNEL
669 ch_logs(NULL, "ERROR: %s", (char *)s);
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 return TRUE;
672 }
673
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100674 ex_exitval = 1;
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 /* Reset msg_silent, an error causes messages to be switched back on. */
677 msg_silent = 0;
678 cmd_silent = FALSE;
679
680 if (global_busy) /* break :global command */
681 ++global_busy;
682
683 if (p_eb)
684 beep_flush(); /* also includes flush_buffers() */
685 else
686 flush_buffers(FALSE); /* flush internal buffers */
687 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689
690 emsg_on_display = TRUE; /* remember there is an error message */
691 ++msg_scroll; /* don't overwrite a previous message */
692 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000693 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 need_wait_return = TRUE; /* needed in case emsg() is called after
695 * wait_return has reset need_wait_return
696 * and a redraw is expected because
697 * msg_scrolled is non-zero */
698
Bram Moolenaar958dc692016-12-01 15:34:12 +0100699#ifdef FEAT_JOB_CHANNEL
700 emsg_to_channel_log = TRUE;
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 /*
703 * Display name and line number for the source of the error.
704 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000705 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
707 /*
708 * Display the error message itself.
709 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000710 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar958dc692016-12-01 15:34:12 +0100711 r = msg_attr(s, attr);
712
713#ifdef FEAT_JOB_CHANNEL
714 emsg_to_channel_log = FALSE;
715#endif
716 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717}
718
Bram Moolenaar95f09602016-11-10 20:01:45 +0100719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720/*
721 * Print an error message with one "%s" and one string argument.
722 */
723 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100724emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 return emsg3(s, a1, NULL);
727}
728
Bram Moolenaar95f09602016-11-10 20:01:45 +0100729/*
730 * Print an error message with one or two "%s" and one or two string arguments.
731 * This is not in message.c to avoid a warning for prototypes.
732 */
733 int
734emsg3(char_u *s, char_u *a1, char_u *a2)
735{
736 if (emsg_not_now())
737 return TRUE; /* no error messages at the moment */
738 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
739 return emsg(IObuff);
740}
741
742/*
743 * Print an error message with one "%ld" and one long int argument.
744 * This is not in message.c to avoid a warning for prototypes.
745 */
746 int
747emsgn(char_u *s, long n)
748{
749 if (emsg_not_now())
750 return TRUE; /* no error messages at the moment */
751 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
752 return emsg(IObuff);
753}
754
755/*
756 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
757 * defined. It is used for internal errors only, so that they can be
758 * detected when fuzzing vim.
759 */
760 void
761iemsg(char_u *s)
762{
763 msg(s);
764#ifdef ABORT_ON_INTERNAL_ERROR
765 abort();
766#endif
767}
768
769
770/*
771 * Same as emsg2(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
772 * defined. It is used for internal errors only, so that they can be
773 * detected when fuzzing vim.
774 */
775 void
776iemsg2(char_u *s, char_u *a1)
777{
778 emsg2(s, a1);
779#ifdef ABORT_ON_INTERNAL_ERROR
780 abort();
781#endif
782}
783
784/*
785 * Same as emsgn(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
786 * defined. It is used for internal errors only, so that they can be
787 * detected when fuzzing vim.
788 */
789 void
790iemsgn(char_u *s, long n)
791{
792 emsgn(s, n);
793#ifdef ABORT_ON_INTERNAL_ERROR
794 abort();
795#endif
796}
797
798/*
799 * Give an "Internal error" message.
800 */
801 void
802internal_error(char *where)
803{
804 IEMSG2(_(e_intern2), where);
805}
806
Bram Moolenaarea424162005-06-16 21:51:00 +0000807/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
Bram Moolenaardf177f62005-02-22 08:39:57 +0000809 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100810emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000811{
812 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
813}
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Like msg(), but truncate to a single line if p_shm contains 't', or when
817 * "force" is TRUE. This truncates in another way as for normal messages.
818 * Careful: The string may be changed by msg_may_trunc()!
819 * Returns a pointer to the printed message, if wait_return() not called.
820 */
821 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100822msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
824 int n;
825
826 /* Add message to history before truncating */
827 add_msg_hist(s, -1, attr);
828
829 s = msg_may_trunc(force, s);
830
831 msg_hist_off = TRUE;
832 n = msg_attr(s, attr);
833 msg_hist_off = FALSE;
834
835 if (n)
836 return s;
837 return NULL;
838}
839
840/*
841 * Check if message "s" should be truncated at the start (for filenames).
842 * Return a pointer to where the truncated message starts.
843 * Note: May change the message by replacing a character with '<'.
844 */
845 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100846msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848 int n;
849 int room;
850
851 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
852 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
853 && (n = (int)STRLEN(s) - room) > 0)
854 {
855#ifdef FEAT_MBYTE
856 if (has_mbyte)
857 {
858 int size = vim_strsize(s);
859
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000860 /* There may be room anyway when there are multibyte chars. */
861 if (size <= room)
862 return s;
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 for (n = 0; size >= room; )
865 {
866 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000867 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 --n;
870 }
871#endif
872 s += n;
873 *s = '<';
874 }
875 return s;
876}
877
878 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100879add_msg_hist(
880 char_u *s,
881 int len, /* -1 for undetermined length */
882 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
884 struct msg_hist *p;
885
886 if (msg_hist_off || msg_silent != 0)
887 return;
888
889 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000890 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000891 (void)delete_first_msg();
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 /* allocate an entry and add the message at the end of the history */
894 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
895 if (p != NULL)
896 {
897 if (len < 0)
898 len = (int)STRLEN(s);
899 /* remove leading and trailing newlines */
900 while (len > 0 && *s == '\n')
901 {
902 ++s;
903 --len;
904 }
905 while (len > 0 && s[len - 1] == '\n')
906 --len;
907 p->msg = vim_strnsave(s, len);
908 p->next = NULL;
909 p->attr = attr;
910 if (last_msg_hist != NULL)
911 last_msg_hist->next = p;
912 last_msg_hist = p;
913 if (first_msg_hist == NULL)
914 first_msg_hist = last_msg_hist;
915 ++msg_hist_len;
916 }
917}
918
919/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000920 * Delete the first (oldest) message from the history.
921 * Returns FAIL if there are no messages.
922 */
923 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100924delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000925{
926 struct msg_hist *p;
927
928 if (msg_hist_len <= 0)
929 return FAIL;
930 p = first_msg_hist;
931 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000932 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200933 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000934 vim_free(p->msg);
935 vim_free(p);
936 --msg_hist_len;
937 return OK;
938}
939
940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * ":messages" command.
942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200944ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
946 struct msg_hist *p;
947 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200948 int c = 0;
949
950 if (STRCMP(eap->arg, "clear") == 0)
951 {
952 int keep = eap->addr_count == 0 ? 0 : eap->line2;
953
954 while (msg_hist_len > keep)
955 (void)delete_first_msg();
956 return;
957 }
958
959 if (*eap->arg != NUL)
960 {
961 EMSG(_(e_invarg));
962 return;
963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
965 msg_hist_off = TRUE;
966
Bram Moolenaar451f8492016-04-14 17:16:22 +0200967 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200968 if (eap->addr_count != 0)
969 {
970 /* Count total messages */
971 for (; p != NULL && !got_int; p = p->next)
972 c++;
973
974 c -= eap->line2;
975
976 /* Skip without number of messages specified */
977 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
978 p = p->next, c--);
979 }
980
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200981 if (p == first_msg_hist)
982 {
983 s = mch_getenv((char_u *)"LANG");
984 if (s != NULL && *s != NUL)
985 msg_attr((char_u *)
986 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
987 hl_attr(HLF_T));
988 }
989
Bram Moolenaar451f8492016-04-14 17:16:22 +0200990 /* Display what was not skipped. */
991 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (p->msg != NULL)
993 msg_attr(p->msg, p->attr);
994
995 msg_hist_off = FALSE;
996}
997
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000998#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999/*
1000 * Call this after prompting the user. This will avoid a hit-return message
1001 * and a delay.
1002 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001003 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001004msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 need_wait_return = FALSE;
1007 emsg_on_display = FALSE;
1008 cmdline_row = msg_row;
1009 msg_col = 0;
1010 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001011 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012}
1013#endif
1014
1015/*
1016 * wait for the user to hit a key (normally a return)
1017 * if 'redraw' is TRUE, clear and redraw the screen
1018 * if 'redraw' is FALSE, just redraw the screen
1019 * if 'redraw' is -1, don't redraw at all
1020 */
1021 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001022wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
1024 int c;
1025 int oldState;
1026 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001028 int save_Recording;
1029 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
1031 if (redraw == TRUE)
1032 must_redraw = CLEAR;
1033
1034 /* If using ":silent cmd", don't wait for a return. Also don't set
1035 * need_wait_return to do it later. */
1036 if (msg_silent != 0)
1037 return;
1038
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001039 /*
1040 * When inside vgetc(), we can't wait for a typed character at all.
1041 * With the global command (and some others) we only need one return at
1042 * the end. Adjust cmdline_row to avoid the next message overwriting the
1043 * last one.
1044 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001045 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001047 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (no_wait_return)
1049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (!exmode_active)
1051 cmdline_row = msg_row;
1052 return;
1053 }
1054
1055 redir_off = TRUE; /* don't redirect this message */
1056 oldState = State;
1057 if (quit_more)
1058 {
1059 c = CAR; /* just pretend CR was hit */
1060 quit_more = FALSE;
1061 got_int = FALSE;
1062 }
1063 else if (exmode_active)
1064 {
1065 MSG_PUTS(" "); /* make sure the cursor is on the right line */
1066 c = CAR; /* no need for a return in ex mode */
1067 got_int = FALSE;
1068 }
1069 else
1070 {
1071 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1072 * just changed. */
1073 screenalloc(FALSE);
1074
1075 State = HITRETURN;
1076#ifdef FEAT_MOUSE
1077 setmouse();
1078#endif
1079#ifdef USE_ON_FLY_SCROLL
1080 dont_scroll = TRUE; /* disallow scrolling here */
1081#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001082 cmdline_row = msg_row;
1083
Bram Moolenaarec38d692013-04-24 15:12:32 +02001084 /* Avoid the sequence that the user types ":" at the hit-return prompt
1085 * to start an Ex command, but the file-changed dialog gets in the
1086 * way. */
1087 if (need_check_timestamps)
1088 check_timestamps(FALSE);
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 hit_return_msg();
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 do
1093 {
1094 /* Remember "got_int", if it is set vgetc() probably returns a
1095 * CTRL-C, but we need to loop then. */
1096 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001097
1098 /* Don't do mappings here, we put the character back in the
1099 * typeahead buffer. */
1100 ++no_mapping;
1101 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001102
1103 /* Temporarily disable Recording. If Recording is active, the
1104 * character will be recorded later, since it will be added to the
1105 * typebuf after the loop */
1106 save_Recording = Recording;
1107 save_scriptout = scriptout;
1108 Recording = FALSE;
1109 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001111 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001113 --no_mapping;
1114 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001115 Recording = save_Recording;
1116 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#ifdef FEAT_CLIPBOARD
1119 /* Strange way to allow copying (yanking) a modeless selection at
1120 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1121 * Cmdline-mode and it's harmless when there is no selection. */
1122 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1123 {
1124 clip_copy_modeless_selection(TRUE);
1125 c = K_IGNORE;
1126 }
1127#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001128
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001129 /*
1130 * Allow scrolling back in the messages.
1131 * Also accept scroll-down commands when messages fill the screen,
1132 * to avoid that typing one 'j' too many makes the messages
1133 * disappear.
1134 */
1135 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001136 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001137 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1138 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001139 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001140 if (msg_scrolled > Rows)
1141 /* scroll back to show older messages */
1142 do_more_prompt(c);
1143 else
1144 {
1145 msg_didout = FALSE;
1146 c = K_IGNORE;
1147 msg_col =
1148#ifdef FEAT_RIGHTLEFT
1149 cmdmsg_rl ? Columns - 1 :
1150#endif
1151 0;
1152 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001153 if (quit_more)
1154 {
1155 c = CAR; /* just pretend CR was hit */
1156 quit_more = FALSE;
1157 got_int = FALSE;
1158 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001159 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001160 {
1161 c = K_IGNORE;
1162 hit_return_msg();
1163 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001164 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001165 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001166 && (c == 'j' || c == 'd' || c == 'f'
1167 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001168 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 } while ((had_got_int && c == Ctrl_C)
1171 || c == K_IGNORE
1172#ifdef FEAT_GUI
1173 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1174#endif
1175#ifdef FEAT_MOUSE
1176 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1177 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1178 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001179 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 || c == K_MOUSEDOWN || c == K_MOUSEUP
1181 || (!mouse_has(MOUSE_RETURN)
1182 && mouse_row < msg_row
1183 && (c == K_LEFTMOUSE
1184 || c == K_MIDDLEMOUSE
1185 || c == K_RIGHTMOUSE
1186 || c == K_X1MOUSE
1187 || c == K_X2MOUSE))
1188#endif
1189 );
1190 ui_breakcheck();
1191#ifdef FEAT_MOUSE
1192 /*
1193 * Avoid that the mouse-up event causes visual mode to start.
1194 */
1195 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1196 || c == K_X1MOUSE || c == K_X2MOUSE)
1197 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1198 else
1199#endif
1200 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1201 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001202 /* Put the character back in the typeahead buffer. Don't use the
1203 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001204 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001206 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
1209 redir_off = FALSE;
1210
1211 /*
1212 * If the user hits ':', '?' or '/' we get a command line from the next
1213 * line.
1214 */
1215 if (c == ':' || c == '?' || c == '/')
1216 {
1217 if (!exmode_active)
1218 cmdline_row = msg_row;
1219 skip_redraw = TRUE; /* skip redraw once */
1220 do_redraw = FALSE;
1221 }
1222
1223 /*
1224 * If the window size changed set_shellsize() will redraw the screen.
1225 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1226 * typed.
1227 */
1228 tmpState = State;
1229 State = oldState; /* restore State before set_shellsize */
1230#ifdef FEAT_MOUSE
1231 setmouse();
1232#endif
1233 msg_check();
1234
1235#if defined(UNIX) || defined(VMS)
1236 /*
1237 * When switching screens, we need to output an extra newline on exit.
1238 */
1239 if (swapping_screen() && !termcap_active)
1240 newline_on_exit = TRUE;
1241#endif
1242
1243 need_wait_return = FALSE;
1244 did_wait_return = TRUE;
1245 emsg_on_display = FALSE; /* can delete error message now */
1246 lines_left = -1; /* reset lines_left at next msg_start() */
1247 reset_last_sourcing();
1248 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1249 (Rows - cmdline_row - 1) * Columns + sc_col)
1250 {
1251 vim_free(keep_msg);
1252 keep_msg = NULL; /* don't redisplay message, it's too long */
1253 }
1254
1255 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1256 {
1257 starttermcap(); /* start termcap before redrawing */
1258 shell_resized();
1259 }
1260 else if (!skip_redraw
1261 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1262 {
1263 starttermcap(); /* start termcap before redrawing */
1264 redraw_later(VALID);
1265 }
1266}
1267
1268/*
1269 * Write the hit-return prompt.
1270 */
1271 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001272hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001274 int save_p_more = p_more;
1275
1276 p_more = FALSE; /* don't want see this message when scrolling back */
1277 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 msg_putchar('\n');
1279 if (got_int)
1280 MSG_PUTS(_("Interrupt: "));
1281
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001282 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (!msg_use_printf())
1284 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001285 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286}
1287
1288/*
1289 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1290 */
1291 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001292set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 vim_free(keep_msg);
1295 if (s != NULL && msg_silent == 0)
1296 keep_msg = vim_strsave(s);
1297 else
1298 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001299 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001300 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001303#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1304/*
1305 * If there currently is a message being displayed, set "keep_msg" to it, so
1306 * that it will be displayed again after redraw.
1307 */
1308 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001309set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001310{
1311 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1312 && (State & NORMAL))
1313 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1314}
1315#endif
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317/*
1318 * Prepare for outputting characters in the command line.
1319 */
1320 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001321msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 int did_return = FALSE;
1324
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001325 if (!msg_silent)
1326 {
1327 vim_free(keep_msg);
1328 keep_msg = NULL; /* don't display old message now */
1329 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001330
1331#ifdef FEAT_EVAL
1332 if (need_clr_eos)
1333 {
1334 /* Halfway an ":echo" command and getting an (error) message: clear
1335 * any text from the command. */
1336 need_clr_eos = FALSE;
1337 msg_clr_eos();
1338 }
1339#endif
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (!msg_scroll && full_screen) /* overwrite last message */
1342 {
1343 msg_row = cmdline_row;
1344 msg_col =
1345#ifdef FEAT_RIGHTLEFT
1346 cmdmsg_rl ? Columns - 1 :
1347#endif
1348 0;
1349 }
1350 else if (msg_didout) /* start message on next line */
1351 {
1352 msg_putchar('\n');
1353 did_return = TRUE;
1354 if (exmode_active != EXMODE_NORMAL)
1355 cmdline_row = msg_row;
1356 }
1357 if (!msg_didany || lines_left < 0)
1358 msg_starthere();
1359 if (msg_silent == 0)
1360 {
1361 msg_didout = FALSE; /* no output on current line yet */
1362 cursor_off();
1363 }
1364
1365 /* when redirecting, may need to start a new line. */
1366 if (!did_return)
1367 redir_write((char_u *)"\n", -1);
1368}
1369
1370/*
1371 * Note that the current msg position is where messages start.
1372 */
1373 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001374msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 lines_left = cmdline_row;
1377 msg_didany = FALSE;
1378}
1379
1380 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001381msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 msg_putchar_attr(c, 0);
1384}
1385
1386 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001387msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389#ifdef FEAT_MBYTE
1390 char_u buf[MB_MAXBYTES + 1];
1391#else
1392 char_u buf[4];
1393#endif
1394
1395 if (IS_SPECIAL(c))
1396 {
1397 buf[0] = K_SPECIAL;
1398 buf[1] = K_SECOND(c);
1399 buf[2] = K_THIRD(c);
1400 buf[3] = NUL;
1401 }
1402 else
1403 {
1404#ifdef FEAT_MBYTE
1405 buf[(*mb_char2bytes)(c, buf)] = NUL;
1406#else
1407 buf[0] = c;
1408 buf[1] = NUL;
1409#endif
1410 }
1411 msg_puts_attr(buf, attr);
1412}
1413
1414 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001415msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 char_u buf[20];
1418
1419 sprintf((char *)buf, "%ld", n);
1420 msg_puts(buf);
1421}
1422
1423 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001424msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 msg_home_replace_attr(fname, 0);
1427}
1428
1429#if defined(FEAT_FIND_ID) || defined(PROTO)
1430 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001431msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 msg_home_replace_attr(fname, hl_attr(HLF_D));
1434}
1435#endif
1436
1437 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001438msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 char_u *name;
1441
1442 name = home_replace_save(NULL, fname);
1443 if (name != NULL)
1444 msg_outtrans_attr(name, attr);
1445 vim_free(name);
1446}
1447
1448/*
1449 * Output 'len' characters in 'str' (including NULs) with translation
1450 * if 'len' is -1, output upto a NUL character.
1451 * Use attributes 'attr'.
1452 * Return the number of characters it takes on the screen.
1453 */
1454 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001455msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 return msg_outtrans_attr(str, 0);
1458}
1459
1460 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001461msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1464}
1465
1466 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001467msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 return msg_outtrans_len_attr(str, len, 0);
1470}
1471
1472/*
1473 * Output one character at "p". Return pointer to the next character.
1474 * Handles multi-byte characters.
1475 */
1476 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001477msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
1479#ifdef FEAT_MBYTE
1480 int l;
1481
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001482 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 msg_outtrans_len_attr(p, l, attr);
1485 return p + l;
1486 }
1487#endif
1488 msg_puts_attr(transchar_byte(*p), attr);
1489 return p + 1;
1490}
1491
1492 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001493msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 int retval = 0;
1496 char_u *str = msgstr;
1497 char_u *plain_start = msgstr;
1498 char_u *s;
1499#ifdef FEAT_MBYTE
1500 int mb_l;
1501 int c;
1502#endif
1503
1504 /* if MSG_HIST flag set, add message to history */
1505 if (attr & MSG_HIST)
1506 {
1507 add_msg_hist(str, len, attr);
1508 attr &= ~MSG_HIST;
1509 }
1510
1511#ifdef FEAT_MBYTE
1512 /* If the string starts with a composing character first draw a space on
1513 * which the composing char can be drawn. */
1514 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1515 msg_puts_attr((char_u *)" ", attr);
1516#endif
1517
1518 /*
1519 * Go over the string. Special characters are translated and printed.
1520 * Normal characters are printed several at a time.
1521 */
1522 while (--len >= 0)
1523 {
1524#ifdef FEAT_MBYTE
1525 if (enc_utf8)
1526 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001527 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001529 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 else
1531 mb_l = 1;
1532 if (has_mbyte && mb_l > 1)
1533 {
1534 c = (*mb_ptr2char)(str);
1535 if (vim_isprintc(c))
1536 /* printable multi-byte char: count the cells. */
1537 retval += (*mb_ptr2cells)(str);
1538 else
1539 {
1540 /* unprintable multi-byte char: print the printable chars so
1541 * far and the translation of the unprintable char. */
1542 if (str > plain_start)
1543 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1544 attr);
1545 plain_start = str + mb_l;
1546 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1547 retval += char2cells(c);
1548 }
1549 len -= mb_l - 1;
1550 str += mb_l;
1551 }
1552 else
1553#endif
1554 {
1555 s = transchar_byte(*str);
1556 if (s[1] != NUL)
1557 {
1558 /* unprintable char: print the printable chars so far and the
1559 * translation of the unprintable char. */
1560 if (str > plain_start)
1561 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1562 attr);
1563 plain_start = str + 1;
1564 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001565 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001567 else
1568 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 ++str;
1570 }
1571 }
1572
1573 if (str > plain_start)
1574 /* print the printable chars at the end */
1575 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1576
1577 return retval;
1578}
1579
1580#if defined(FEAT_QUICKFIX) || defined(PROTO)
1581 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001582msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 int i;
1585 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1586
1587 arg = skipwhite(arg);
1588 for (i = 5; *arg && i >= 0; --i)
1589 if (*arg++ != str[i])
1590 break;
1591 if (i < 0)
1592 {
1593 msg_putchar('\n');
1594 for (i = 0; rs[i]; ++i)
1595 msg_putchar(rs[i] - 3);
1596 }
1597}
1598#endif
1599
1600/*
1601 * Output the string 'str' upto a NUL character.
1602 * Return the number of characters it takes on the screen.
1603 *
1604 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1605 * following character and shown as <F1>, <S-Up> etc. Any other character
1606 * which is not printable shown in <> form.
1607 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1608 * If a character is displayed in one of these special ways, is also
1609 * highlighted (its highlight name is '8' in the p_hl variable).
1610 * Otherwise characters are not highlighted.
1611 * This function is used to show mappings, where we want to see how to type
1612 * the character/string -- webb
1613 */
1614 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001615msg_outtrans_special(
1616 char_u *strstart,
1617 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 char_u *str = strstart;
1620 int retval = 0;
1621 char_u *string;
1622 int attr;
1623 int len;
1624
1625 attr = hl_attr(HLF_8);
1626 while (*str != NUL)
1627 {
1628 /* Leading and trailing spaces need to be displayed in <> form. */
1629 if ((str == strstart || str[1] == NUL) && *str == ' ')
1630 {
1631 string = (char_u *)"<Space>";
1632 ++str;
1633 }
1634 else
1635 string = str2special(&str, from);
1636 len = vim_strsize(string);
1637 /* Highlight special keys */
1638 msg_puts_attr(string, len > 1
1639#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001640 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641#endif
1642 ? attr : 0);
1643 retval += len;
1644 }
1645 return retval;
1646}
1647
Bram Moolenaarbd743252010-10-20 21:23:33 +02001648#if defined(FEAT_EVAL) || defined(PROTO)
1649/*
1650 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1651 * strings, in an allocated string.
1652 */
1653 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001654str2special_save(
1655 char_u *str,
1656 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001657{
1658 garray_T ga;
1659 char_u *p = str;
1660
1661 ga_init2(&ga, 1, 40);
1662 while (*p != NUL)
1663 ga_concat(&ga, str2special(&p, is_lhs));
1664 ga_append(&ga, NUL);
1665 return (char_u *)ga.ga_data;
1666}
1667#endif
1668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669/*
1670 * Return the printable string for the key codes at "*sp".
1671 * Used for translating the lhs or rhs of a mapping to printable chars.
1672 * Advances "sp" to the next code.
1673 */
1674 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001675str2special(
1676 char_u **sp,
1677 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 int c;
1680 static char_u buf[7];
1681 char_u *str = *sp;
1682 int modifiers = 0;
1683 int special = FALSE;
1684
1685#ifdef FEAT_MBYTE
1686 if (has_mbyte)
1687 {
1688 char_u *p;
1689
1690 /* Try to un-escape a multi-byte character. Return the un-escaped
1691 * string if it is a multi-byte character. */
1692 p = mb_unescape(sp);
1693 if (p != NULL)
1694 return p;
1695 }
1696#endif
1697
1698 c = *str;
1699 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1700 {
1701 if (str[1] == KS_MODIFIER)
1702 {
1703 modifiers = str[2];
1704 str += 3;
1705 c = *str;
1706 }
1707 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1708 {
1709 c = TO_SPECIAL(str[1], str[2]);
1710 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001711 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 c = NUL;
1713 }
1714 if (IS_SPECIAL(c) || modifiers) /* special key */
1715 special = TRUE;
1716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001719 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001721 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001722
1723 /* For multi-byte characters check for an illegal byte. */
1724 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1725 {
1726 transchar_nonprint(buf, c);
1727 *sp = str + 1;
1728 return buf;
1729 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001730 /* Since 'special' is TRUE the multi-byte character 'c' will be
1731 * processed by get_special_key_name() */
1732 c = (*mb_ptr2char)(str);
1733 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001737 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
1739 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1740 * Use <Space> only for lhs of a mapping. */
1741 if (special || char2cells(c) > 1 || (from && c == ' '))
1742 return get_special_key_name(c, modifiers);
1743 buf[0] = c;
1744 buf[1] = NUL;
1745 return buf;
1746}
1747
1748/*
1749 * Translate a key sequence into special key names.
1750 */
1751 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001752str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 char_u *s;
1755
1756 *buf = NUL;
1757 while (*sp)
1758 {
1759 s = str2special(&sp, FALSE);
1760 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1761 STRCAT(buf, s);
1762 }
1763}
1764
1765/*
1766 * print line for :print or :list command
1767 */
1768 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001769msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771 int c;
1772 int col = 0;
1773 int n_extra = 0;
1774 int c_extra = 0;
1775 char_u *p_extra = NULL; /* init to make SASC shut up */
1776 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001777 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 char_u *trail = NULL;
1779#ifdef FEAT_MBYTE
1780 int l;
1781 char_u buf[MB_MAXBYTES + 1];
1782#endif
1783
Bram Moolenaardf177f62005-02-22 08:39:57 +00001784 if (curwin->w_p_list)
1785 list = TRUE;
1786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001788 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 trail = s + STRLEN(s);
1791 while (trail > s && vim_iswhite(trail[-1]))
1792 --trail;
1793 }
1794
1795 /* output a space for an empty line, otherwise the line will be
1796 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001797 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 msg_putchar(' ');
1799
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001800 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001802 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
1804 --n_extra;
1805 if (c_extra)
1806 c = c_extra;
1807 else
1808 c = *p_extra++;
1809 }
1810#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001811 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001814 if (lcs_nbsp != NUL && list
1815 && (mb_ptr2char(s) == 160
1816 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001817 {
1818 mb_char2bytes(lcs_nbsp, buf);
1819 buf[(*mb_ptr2len)(buf)] = NUL;
1820 }
1821 else
1822 {
1823 mch_memmove(buf, s, (size_t)l);
1824 buf[l] = NUL;
1825 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001826 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 s += l;
1828 continue;
1829 }
1830#endif
1831 else
1832 {
1833 attr = 0;
1834 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001835 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 /* tab amount depends on current column */
1838 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001839 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 c = ' ';
1842 c_extra = ' ';
1843 }
1844 else
1845 {
1846 c = lcs_tab1;
1847 c_extra = lcs_tab2;
1848 attr = hl_attr(HLF_8);
1849 }
1850 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001851 else if (c == 160 && list && lcs_nbsp != NUL)
1852 {
1853 c = lcs_nbsp;
1854 attr = hl_attr(HLF_8);
1855 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001856 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 p_extra = (char_u *)"";
1859 c_extra = NUL;
1860 n_extra = 1;
1861 c = lcs_eol;
1862 attr = hl_attr(HLF_AT);
1863 --s;
1864 }
1865 else if (c != NUL && (n = byte2cells(c)) > 1)
1866 {
1867 n_extra = n - 1;
1868 p_extra = transchar_byte(c);
1869 c_extra = NUL;
1870 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001871 /* Use special coloring to be able to distinguish <hex> from
1872 * the same in plain text. */
1873 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875 else if (c == ' ' && trail != NULL && s > trail)
1876 {
1877 c = lcs_trail;
1878 attr = hl_attr(HLF_8);
1879 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001880 else if (c == ' ' && list && lcs_space != NUL)
1881 {
1882 c = lcs_space;
1883 attr = hl_attr(HLF_8);
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
1886
1887 if (c == NUL)
1888 break;
1889
1890 msg_putchar_attr(c, attr);
1891 col++;
1892 }
1893 msg_clr_eos();
1894}
1895
1896#ifdef FEAT_MBYTE
1897/*
1898 * Use screen_puts() to output one multi-byte character.
1899 * Return the pointer "s" advanced to the next character.
1900 */
1901 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001902screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 int cw;
1905
1906 msg_didout = TRUE; /* remember that line is not empty */
1907 cw = (*mb_ptr2cells)(s);
1908 if (cw > 1 && (
1909#ifdef FEAT_RIGHTLEFT
1910 cmdmsg_rl ? msg_col <= 1 :
1911#endif
1912 msg_col == Columns - 1))
1913 {
1914 /* Doesn't fit, print a highlighted '>' to fill it up. */
1915 msg_screen_putchar('>', hl_attr(HLF_AT));
1916 return s;
1917 }
1918
1919 screen_puts_len(s, l, msg_row, msg_col, attr);
1920#ifdef FEAT_RIGHTLEFT
1921 if (cmdmsg_rl)
1922 {
1923 msg_col -= cw;
1924 if (msg_col == 0)
1925 {
1926 msg_col = Columns;
1927 ++msg_row;
1928 }
1929 }
1930 else
1931#endif
1932 {
1933 msg_col += cw;
1934 if (msg_col >= Columns)
1935 {
1936 msg_col = 0;
1937 ++msg_row;
1938 }
1939 }
1940 return s + l;
1941}
1942#endif
1943
1944/*
1945 * Output a string to the screen at position msg_row, msg_col.
1946 * Update msg_row and msg_col for the next message.
1947 */
1948 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001949msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001951 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952}
1953
1954 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001955msg_puts_title(
1956 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958 msg_puts_attr(s, hl_attr(HLF_T));
1959}
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961/*
1962 * Show a message in such a way that it always fits in the line. Cut out a
1963 * part in the middle and replace it with "..." when necessary.
1964 * Does not handle multi-byte characters!
1965 */
1966 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001967msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001969 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970}
1971
1972 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001973msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 int slen = len;
1976 int room;
1977
1978 room = Columns - msg_col;
1979 if (len > room && room >= 20)
1980 {
1981 slen = (room - 3) / 2;
1982 msg_outtrans_len_attr(longstr, slen, attr);
1983 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1984 }
1985 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1986}
1987
1988/*
1989 * Basic function for writing a message with highlight attributes.
1990 */
1991 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001992msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 msg_puts_attr_len(s, -1, attr);
1995}
1996
1997/*
1998 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1999 * When "maxlen" is -1 there is no maximum length.
2000 * When "maxlen" is >= 0 the message is not put in the history.
2001 */
2002 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002003msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 /*
2006 * If redirection is on, also write to the redirection file.
2007 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002008 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 /*
2011 * Don't print anything when using ":silent cmd".
2012 */
2013 if (msg_silent != 0)
2014 return;
2015
2016 /* if MSG_HIST flag set, add message to history */
2017 if ((attr & MSG_HIST) && maxlen < 0)
2018 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 attr &= ~MSG_HIST;
2021 }
2022
2023 /*
2024 * When writing something to the screen after it has scrolled, requires a
2025 * wait-return prompt later. Needed when scrolling, resetting
2026 * need_wait_return after some prompt, and then outputting something
2027 * without scrolling
2028 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002029 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 need_wait_return = TRUE;
2031 msg_didany = TRUE; /* remember that something was outputted */
2032
2033 /*
2034 * If there is no valid screen, use fprintf so we can see error messages.
2035 * If termcap is not active, we may be writing in an alternate console
2036 * window, cursor positioning may not work correctly (window size may be
2037 * different, e.g. for Win32 console) or we just don't know where the
2038 * cursor is.
2039 */
2040 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002041 msg_puts_printf(str, maxlen);
2042 else
2043 msg_puts_display(str, maxlen, attr, FALSE);
2044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002046/*
2047 * The display part of msg_puts_attr_len().
2048 * May be called recursively to display scroll-back text.
2049 */
2050 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002051msg_puts_display(
2052 char_u *str,
2053 int maxlen,
2054 int attr,
2055 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002056{
2057 char_u *s = str;
2058 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2059 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
2060#ifdef FEAT_MBYTE
2061 int l;
2062 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002064 char_u *sb_str = str;
2065 int sb_col = msg_col;
2066 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002067 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
2069 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002070 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002073 * We are at the end of the screen line when:
2074 * - When outputting a newline.
2075 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002077 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#ifdef FEAT_RIGHTLEFT
2079 cmdmsg_rl
2080 ? (
2081 msg_col <= 1
2082 || (*s == TAB && msg_col <= 7)
2083# ifdef FEAT_MBYTE
2084 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
2085# endif
2086 )
2087 :
2088#endif
2089 (msg_col + t_col >= Columns - 1
2090 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
2091# ifdef FEAT_MBYTE
2092 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2093 && msg_col + t_col >= Columns - 2)
2094# endif
2095 ))))
2096 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002097 /*
2098 * The screen is scrolled up when at the last row (some terminals
2099 * scroll automatically, some don't. To avoid problems we scroll
2100 * ourselves).
2101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002104 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002106 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (msg_no_more && lines_left == 0)
2108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002110 /* Scroll the screen up one line. */
2111 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
2113 msg_row = Rows - 2;
2114 if (msg_col >= Columns) /* can happen after screen resize */
2115 msg_col = Columns - 1;
2116
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002117 /* Display char in last column before showing more-prompt. */
2118 if (*s >= ' '
2119#ifdef FEAT_RIGHTLEFT
2120 && !cmdmsg_rl
2121#endif
2122 )
2123 {
2124#ifdef FEAT_MBYTE
2125 if (has_mbyte)
2126 {
2127 if (enc_utf8 && maxlen >= 0)
2128 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002129 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002130 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002131 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002132 s = screen_puts_mbyte(s, l, attr);
2133 }
2134 else
2135#endif
2136 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002137 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002138 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002139 else
2140 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002141
2142 if (p_more)
2143 /* store text for scrolling back */
2144 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2145
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002146 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002147 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (must_redraw < VALID)
2149 must_redraw = VALID;
2150 redraw_cmdline = TRUE;
2151 if (cmdline_row > 0 && !exmode_active)
2152 --cmdline_row;
2153
2154 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002155 * If screen is completely filled and 'more' is set then wait
2156 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002158 if (lines_left > 0)
2159 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002160 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 && !msg_no_more && !exmode_active)
2162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002164 if (do_more_prompt(NUL))
2165 s = confirm_msg_tail;
2166#else
2167 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#endif
2169 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002170 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002172
2173 /* When we displayed a char in last column need to check if there
2174 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002175 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002176 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002179 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 || msg_col + t_col >= Columns
2181#ifdef FEAT_MBYTE
2182 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2183 && msg_col + t_col >= Columns - 1)
2184#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002185 ;
2186 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2187 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002189 t_puts(&t_col, t_s, s, attr);
2190
2191 if (wrap && p_more && !recurse)
2192 /* store text for scrolling back */
2193 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 if (*s == '\n') /* go to next line */
2196 {
2197 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002198#ifdef FEAT_RIGHTLEFT
2199 if (cmdmsg_rl)
2200 msg_col = Columns - 1;
2201 else
2202#endif
2203 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (++msg_row >= Rows) /* safety check */
2205 msg_row = Rows - 1;
2206 }
2207 else if (*s == '\r') /* go to column 0 */
2208 {
2209 msg_col = 0;
2210 }
2211 else if (*s == '\b') /* go to previous char */
2212 {
2213 if (msg_col)
2214 --msg_col;
2215 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002216 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 do
2219 msg_screen_putchar(' ', attr);
2220 while (msg_col & 7);
2221 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002222 else if (*s == BELL) /* beep (from ":sh") */
2223 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 else
2225 {
2226#ifdef FEAT_MBYTE
2227 if (has_mbyte)
2228 {
2229 cw = (*mb_ptr2cells)(s);
2230 if (enc_utf8 && maxlen >= 0)
2231 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002232 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002234 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
2236 else
2237 {
2238 cw = 1;
2239 l = 1;
2240 }
2241#endif
2242 /* When drawing from right to left or when a double-wide character
2243 * doesn't fit, draw a single character here. Otherwise collect
2244 * characters and draw them all at once later. */
2245#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2246 if (
2247# ifdef FEAT_RIGHTLEFT
2248 cmdmsg_rl
2249# ifdef FEAT_MBYTE
2250 ||
2251# endif
2252# endif
2253# ifdef FEAT_MBYTE
2254 (cw > 1 && msg_col + t_col >= Columns - 1)
2255# endif
2256 )
2257 {
2258# ifdef FEAT_MBYTE
2259 if (l > 1)
2260 s = screen_puts_mbyte(s, l, attr) - 1;
2261 else
2262# endif
2263 msg_screen_putchar(*s, attr);
2264 }
2265 else
2266#endif
2267 {
2268 /* postpone this character until later */
2269 if (t_col == 0)
2270 t_s = s;
2271#ifdef FEAT_MBYTE
2272 t_col += cw;
2273 s += l - 1;
2274#else
2275 ++t_col;
2276#endif
2277 }
2278 }
2279 ++s;
2280 }
2281
2282 /* output any postponed text */
2283 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002284 t_puts(&t_col, t_s, s, attr);
2285 if (p_more && !recurse)
2286 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288 msg_check();
2289}
2290
2291/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002292 * Return TRUE when ":filter pattern" was used and "msg" does not match
2293 * "pattern".
2294 */
2295 int
2296message_filtered(char_u *msg)
2297{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002298 int match;
2299
2300 if (cmdmod.filter_regmatch.regprog == NULL)
2301 return FALSE;
2302 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2303 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002304}
2305
2306/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002307 * Scroll the screen up one line for displaying the next message line.
2308 */
2309 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002310msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002311{
2312#ifdef FEAT_GUI
2313 /* Remove the cursor before scrolling, ScreenLines[] is going
2314 * to become invalid. */
2315 if (gui.in_use)
2316 gui_undraw_cursor();
2317#endif
2318 /* scrolling up always works */
2319 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2320
2321 if (!can_clear((char_u *)" "))
2322 {
2323 /* Scrolling up doesn't result in the right background. Set the
2324 * background here. It's not efficient, but avoids that we have to do
2325 * it all over the code. */
2326 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2327
2328 /* Also clear the last char of the last but one line if it was not
2329 * cleared before to avoid a scroll-up. */
2330 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2331 screen_fill((int)Rows - 2, (int)Rows - 1,
2332 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2333 }
2334}
2335
2336/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002337 * Increment "msg_scrolled".
2338 */
2339 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002340inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002341{
2342#ifdef FEAT_EVAL
2343 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2344 {
2345 char_u *p = sourcing_name;
2346 char_u *tofree = NULL;
2347 int len;
2348
2349 /* v:scrollstart is empty, set it to the script/function name and line
2350 * number */
2351 if (p == NULL)
2352 p = (char_u *)_("Unknown");
2353 else
2354 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002355 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002356 tofree = alloc(len);
2357 if (tofree != NULL)
2358 {
2359 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2360 p, (long)sourcing_lnum);
2361 p = tofree;
2362 }
2363 }
2364 set_vim_var_string(VV_SCROLLSTART, p, -1);
2365 vim_free(tofree);
2366 }
2367#endif
2368 ++msg_scrolled;
2369}
2370
2371/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002372 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2373 * store the displayed text and remember where screen lines start.
2374 */
2375typedef struct msgchunk_S msgchunk_T;
2376struct msgchunk_S
2377{
2378 msgchunk_T *sb_next;
2379 msgchunk_T *sb_prev;
2380 char sb_eol; /* TRUE when line ends after this text */
2381 int sb_msg_col; /* column in which text starts */
2382 int sb_attr; /* text attributes */
2383 char_u sb_text[1]; /* text to be displayed, actually longer */
2384};
2385
2386static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2387
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002388static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2389static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002390
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002391static int do_clear_sb_text = FALSE; /* clear text on next msg */
2392
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002393/*
2394 * Store part of a printed message for displaying when scrolling back.
2395 */
2396 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002397store_sb_text(
2398 char_u **sb_str, /* start of string */
2399 char_u *s, /* just after string */
2400 int attr,
2401 int *sb_col,
2402 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002403{
2404 msgchunk_T *mp;
2405
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002406 if (do_clear_sb_text)
2407 {
2408 clear_sb_text();
2409 do_clear_sb_text = FALSE;
2410 }
2411
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002412 if (s > *sb_str)
2413 {
2414 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2415 if (mp != NULL)
2416 {
2417 mp->sb_eol = finish;
2418 mp->sb_msg_col = *sb_col;
2419 mp->sb_attr = attr;
2420 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2421
2422 if (last_msgchunk == NULL)
2423 {
2424 last_msgchunk = mp;
2425 mp->sb_prev = NULL;
2426 }
2427 else
2428 {
2429 mp->sb_prev = last_msgchunk;
2430 last_msgchunk->sb_next = mp;
2431 last_msgchunk = mp;
2432 }
2433 mp->sb_next = NULL;
2434 }
2435 }
2436 else if (finish && last_msgchunk != NULL)
2437 last_msgchunk->sb_eol = TRUE;
2438
2439 *sb_str = s;
2440 *sb_col = 0;
2441}
2442
2443/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002444 * Finished showing messages, clear the scroll-back text on the next message.
2445 */
2446 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002447may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002448{
2449 do_clear_sb_text = TRUE;
2450}
2451
2452/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002453 * Clear any text remembered for scrolling back.
2454 * Called when redrawing the screen.
2455 */
2456 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002457clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002458{
2459 msgchunk_T *mp;
2460
2461 while (last_msgchunk != NULL)
2462 {
2463 mp = last_msgchunk->sb_prev;
2464 vim_free(last_msgchunk);
2465 last_msgchunk = mp;
2466 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002467}
2468
2469/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002470 * "g<" command.
2471 */
2472 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002473show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002474{
2475 msgchunk_T *mp;
2476
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002477 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002478 * weird, typing a command without output results in one line. */
2479 mp = msg_sb_start(last_msgchunk);
2480 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002481 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002482 else
2483 {
2484 do_more_prompt('G');
2485 wait_return(FALSE);
2486 }
2487}
2488
2489/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002490 * Move to the start of screen line in already displayed text.
2491 */
2492 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002493msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002494{
2495 msgchunk_T *mp = mps;
2496
2497 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2498 mp = mp->sb_prev;
2499 return mp;
2500}
2501
2502/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002503 * Mark the last message chunk as finishing the line.
2504 */
2505 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002506msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002507{
2508 if (last_msgchunk != NULL)
2509 last_msgchunk->sb_eol = TRUE;
2510}
2511
2512/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002513 * Display a screen line from previously displayed text at row "row".
2514 * Returns a pointer to the text for the next line (can be NULL).
2515 */
2516 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002517disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002518{
2519 msgchunk_T *mp = smp;
2520 char_u *p;
2521
2522 for (;;)
2523 {
2524 msg_row = row;
2525 msg_col = mp->sb_msg_col;
2526 p = mp->sb_text;
2527 if (*p == '\n') /* don't display the line break */
2528 ++p;
2529 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2530 if (mp->sb_eol || mp->sb_next == NULL)
2531 break;
2532 mp = mp->sb_next;
2533 }
2534 return mp->sb_next;
2535}
2536
2537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 * Output any postponed text for msg_puts_attr_len().
2539 */
2540 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002541t_puts(
2542 int *t_col,
2543 char_u *t_s,
2544 char_u *s,
2545 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 /* output postponed text */
2548 msg_didout = TRUE; /* remember that line is not empty */
2549 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002550 msg_col += *t_col;
2551 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#ifdef FEAT_MBYTE
2553 /* If the string starts with a composing character don't increment the
2554 * column position for it. */
2555 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2556 --msg_col;
2557#endif
2558 if (msg_col >= Columns)
2559 {
2560 msg_col = 0;
2561 ++msg_row;
2562 }
2563}
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565/*
2566 * Returns TRUE when messages should be printed with mch_errmsg().
2567 * This is used when there is no valid screen, so we can see error messages.
2568 * If termcap is not active, we may be writing in an alternate console
2569 * window, cursor positioning may not work correctly (window size may be
2570 * different, e.g. for Win32 console) or we just don't know where the
2571 * cursor is.
2572 */
2573 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002574msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 return (!msg_check_screen()
2577#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2578 || !termcap_active
2579#endif
2580 || (swapping_screen() && !termcap_active)
2581 );
2582}
2583
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002584/*
2585 * Print a message when there is no valid screen.
2586 */
2587 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002588msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002589{
2590 char_u *s = str;
2591 char_u buf[4];
2592 char_u *p;
2593
2594#ifdef WIN3264
2595 if (!(silent_mode && p_verbose == 0))
2596 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2597#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002598 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002599 {
2600 if (!(silent_mode && p_verbose == 0))
2601 {
2602 /* NL --> CR NL translation (for Unix, not for "--version") */
2603 /* NL --> CR translation (for Mac) */
2604 p = &buf[0];
2605 if (*s == '\n' && !info_message)
2606 *p++ = '\r';
2607#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2608 else
2609#endif
2610 *p++ = *s;
2611 *p = '\0';
2612 if (info_message) /* informative message, not an error */
2613 mch_msg((char *)buf);
2614 else
2615 mch_errmsg((char *)buf);
2616 }
2617
2618 /* primitive way to compute the current column */
2619#ifdef FEAT_RIGHTLEFT
2620 if (cmdmsg_rl)
2621 {
2622 if (*s == '\r' || *s == '\n')
2623 msg_col = Columns - 1;
2624 else
2625 --msg_col;
2626 }
2627 else
2628#endif
2629 {
2630 if (*s == '\r' || *s == '\n')
2631 msg_col = 0;
2632 else
2633 ++msg_col;
2634 }
2635 ++s;
2636 }
2637 msg_didout = TRUE; /* assume that line is not empty */
2638
2639#ifdef WIN3264
2640 if (!(silent_mode && p_verbose == 0))
2641 mch_settmode(TMODE_RAW);
2642#endif
2643}
2644
2645/*
2646 * Show the more-prompt and handle the user response.
2647 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002648 * When at hit-enter prompt "typed_char" is the already typed character,
2649 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002650 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2651 */
2652 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002653do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002655 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002656 int used_typed_char = typed_char;
2657 int oldState = State;
2658 int c;
2659#ifdef FEAT_CON_DIALOG
2660 int retval = FALSE;
2661#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002662 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002663 msgchunk_T *mp_last = NULL;
2664 msgchunk_T *mp;
2665 int i;
2666
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002667 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002668 * that case don't show another prompt. Also when at the hit-Enter prompt
2669 * and nothing was typed. */
2670 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002671 return FALSE;
2672 entered = TRUE;
2673
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002674 if (typed_char == 'G')
2675 {
2676 /* "g<": Find first line on the last page. */
2677 mp_last = msg_sb_start(last_msgchunk);
2678 for (i = 0; i < Rows - 2 && mp_last != NULL
2679 && mp_last->sb_prev != NULL; ++i)
2680 mp_last = msg_sb_start(mp_last->sb_prev);
2681 }
2682
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002683 State = ASKMORE;
2684#ifdef FEAT_MOUSE
2685 setmouse();
2686#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002687 if (typed_char == NUL)
2688 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002689 for (;;)
2690 {
2691 /*
2692 * Get a typed character directly from the user.
2693 */
2694 if (used_typed_char != NUL)
2695 {
2696 c = used_typed_char; /* was typed at hit-enter prompt */
2697 used_typed_char = NUL;
2698 }
2699 else
2700 c = get_keystroke();
2701
2702#if defined(FEAT_MENU) && defined(FEAT_GUI)
2703 if (c == K_MENU)
2704 {
2705 int idx = get_menu_index(current_menu, ASKMORE);
2706
2707 /* Used a menu. If it starts with CTRL-Y, it must
2708 * be a "Copy" for the clipboard. Otherwise
2709 * assume that we end */
2710 if (idx == MENU_INDEX_INVALID)
2711 continue;
2712 c = *current_menu->strings[idx];
2713 if (c != NUL && current_menu->strings[idx][1] != NUL)
2714 ins_typebuf(current_menu->strings[idx] + 1,
2715 current_menu->noremap[idx], 0, TRUE,
2716 current_menu->silent[idx]);
2717 }
2718#endif
2719
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002720 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 switch (c)
2722 {
2723 case BS: /* scroll one line back */
2724 case K_BS:
2725 case 'k':
2726 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002727 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728 break;
2729
2730 case CAR: /* one extra line */
2731 case NL:
2732 case 'j':
2733 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002734 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002735 break;
2736
2737 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002738 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002739 break;
2740
2741 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002742 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 break;
2744
2745 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002746 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002747 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002748 break;
2749
2750 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002751 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002752 case K_PAGEDOWN:
2753 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002754 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755 break;
2756
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002757 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002758 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002759 break;
2760
2761 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002762 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002763 lines_left = 999999;
2764 break;
2765
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002766 case ':': /* start new command line */
2767#ifdef FEAT_CON_DIALOG
2768 if (!confirm_msg_used)
2769#endif
2770 {
2771 /* Since got_int is set all typeahead will be flushed, but we
2772 * want to keep this ':', remember that in a special way. */
2773 typeahead_noflush(':');
2774 cmdline_row = Rows - 1; /* put ':' on this line */
2775 skip_redraw = TRUE; /* skip redraw once */
2776 need_wait_return = FALSE; /* don't wait in main() */
2777 }
2778 /*FALLTHROUGH*/
2779 case 'q': /* quit */
2780 case Ctrl_C:
2781 case ESC:
2782#ifdef FEAT_CON_DIALOG
2783 if (confirm_msg_used)
2784 {
2785 /* Jump to the choices of the dialog. */
2786 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787 }
2788 else
2789#endif
2790 {
2791 got_int = TRUE;
2792 quit_more = TRUE;
2793 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002794 /* When there is some more output (wrapping line) display that
2795 * without another prompt. */
2796 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 break;
2798
2799#ifdef FEAT_CLIPBOARD
2800 case Ctrl_Y:
2801 /* Strange way to allow copying (yanking) a modeless
2802 * selection at the more prompt. Use CTRL-Y,
2803 * because the same is used in Cmdline-mode and at the
2804 * hit-enter prompt. However, scrolling one line up
2805 * might be expected... */
2806 if (clip_star.state == SELECT_DONE)
2807 clip_copy_modeless_selection(TRUE);
2808 continue;
2809#endif
2810 default: /* no valid response */
2811 msg_moremsg(TRUE);
2812 continue;
2813 }
2814
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002815 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002817 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002818 {
2819 /* go to start of last line */
2820 if (mp_last == NULL)
2821 mp = msg_sb_start(last_msgchunk);
2822 else if (mp_last->sb_prev != NULL)
2823 mp = msg_sb_start(mp_last->sb_prev);
2824 else
2825 mp = NULL;
2826
2827 /* go to start of line at top of the screen */
2828 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2829 ++i)
2830 mp = msg_sb_start(mp->sb_prev);
2831
2832 if (mp != NULL && mp->sb_prev != NULL)
2833 {
2834 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002835 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002836 {
2837 if (mp == NULL || mp->sb_prev == NULL)
2838 break;
2839 mp = msg_sb_start(mp->sb_prev);
2840 if (mp_last == NULL)
2841 mp_last = msg_sb_start(last_msgchunk);
2842 else
2843 mp_last = msg_sb_start(mp_last->sb_prev);
2844 }
2845
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002846 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002847 (int)Rows, NULL) == OK)
2848 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002849 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002850 (void)disp_sb_line(0, mp);
2851 }
2852 else
2853 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002854 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002855 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002856 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2857 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002859 ++msg_scrolled;
2860 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002862 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002863 }
2864 }
2865 else
2866 {
2867 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002868 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002869 {
2870 /* scroll up, display line at bottom */
2871 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002872 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2874 (int)Columns, ' ', ' ', 0);
2875 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002876 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002877 }
2878 }
2879
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002880 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002881 {
2882 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002883 screen_fill((int)Rows - 1, (int)Rows, 0,
2884 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 msg_moremsg(FALSE);
2886 continue;
2887 }
2888
2889 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002890 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002891 }
2892
2893 break;
2894 }
2895
2896 /* clear the --more-- message */
2897 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2898 State = oldState;
2899#ifdef FEAT_MOUSE
2900 setmouse();
2901#endif
2902 if (quit_more)
2903 {
2904 msg_row = Rows - 1;
2905 msg_col = 0;
2906 }
2907#ifdef FEAT_RIGHTLEFT
2908 else if (cmdmsg_rl)
2909 msg_col = Columns - 1;
2910#endif
2911
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002912 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913#ifdef FEAT_CON_DIALOG
2914 return retval;
2915#else
2916 return FALSE;
2917#endif
2918}
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2921
2922#ifdef mch_errmsg
2923# undef mch_errmsg
2924#endif
2925#ifdef mch_msg
2926# undef mch_msg
2927#endif
2928
2929/*
2930 * Give an error message. To be used when the screen hasn't been initialized
2931 * yet. When stderr can't be used, collect error messages until the GUI has
2932 * started and they can be displayed in a message box.
2933 */
2934 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002935mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 int len;
2938
2939#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2940 /* On Unix use stderr if it's a tty.
2941 * When not going to start the GUI also use stderr.
2942 * On Mac, when started from Finder, stderr is the console. */
2943 if (
2944# ifdef UNIX
2945# ifdef MACOS_X_UNIX
2946 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2947# else
2948 isatty(2)
2949# endif
2950# ifdef FEAT_GUI
2951 ||
2952# endif
2953# endif
2954# ifdef FEAT_GUI
2955 !(gui.in_use || gui.starting)
2956# endif
2957 )
2958 {
2959 fprintf(stderr, "%s", str);
2960 return;
2961 }
2962#endif
2963
2964 /* avoid a delay for a message that isn't there */
2965 emsg_on_display = FALSE;
2966
2967 len = (int)STRLEN(str) + 1;
2968 if (error_ga.ga_growsize == 0)
2969 {
2970 error_ga.ga_growsize = 80;
2971 error_ga.ga_itemsize = 1;
2972 }
2973 if (ga_grow(&error_ga, len) == OK)
2974 {
2975 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2976 (char_u *)str, len);
2977#ifdef UNIX
2978 /* remove CR characters, they are displayed */
2979 {
2980 char_u *p;
2981
2982 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2983 for (;;)
2984 {
2985 p = vim_strchr(p, '\r');
2986 if (p == NULL)
2987 break;
2988 *p = ' ';
2989 }
2990 }
2991#endif
2992 --len; /* don't count the NUL at the end */
2993 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995}
2996
2997/*
2998 * Give a message. To be used when the screen hasn't been initialized yet.
2999 * When there is no tty, collect messages until the GUI has started and they
3000 * can be displayed in a message box.
3001 */
3002 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003003mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3006 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3007 * uses mch_errmsg() when started from the desktop.
3008 * When not going to start the GUI also use stdout.
3009 * On Mac, when started from Finder, stderr is the console. */
3010 if (
3011# ifdef UNIX
3012# ifdef MACOS_X_UNIX
3013 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3014# else
3015 isatty(2)
3016# endif
3017# ifdef FEAT_GUI
3018 ||
3019# endif
3020# endif
3021# ifdef FEAT_GUI
3022 !(gui.in_use || gui.starting)
3023# endif
3024 )
3025 {
3026 printf("%s", str);
3027 return;
3028 }
3029# endif
3030 mch_errmsg(str);
3031}
3032#endif /* USE_MCH_ERRMSG */
3033
3034/*
3035 * Put a character on the screen at the current message position and advance
3036 * to the next position. Only for printable ASCII!
3037 */
3038 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003039msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 msg_didout = TRUE; /* remember that line is not empty */
3042 screen_putchar(c, msg_row, msg_col, attr);
3043#ifdef FEAT_RIGHTLEFT
3044 if (cmdmsg_rl)
3045 {
3046 if (--msg_col == 0)
3047 {
3048 msg_col = Columns;
3049 ++msg_row;
3050 }
3051 }
3052 else
3053#endif
3054 {
3055 if (++msg_col >= Columns)
3056 {
3057 msg_col = 0;
3058 ++msg_row;
3059 }
3060 }
3061}
3062
3063 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003064msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003066 int attr;
3067 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003070 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003072 screen_puts((char_u *)
3073 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3074 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075}
3076
3077/*
3078 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3079 * exmode_active.
3080 */
3081 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003082repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 if (State == ASKMORE)
3085 {
3086 msg_moremsg(TRUE); /* display --more-- message again */
3087 msg_row = Rows - 1;
3088 }
3089#ifdef FEAT_CON_DIALOG
3090 else if (State == CONFIRM)
3091 {
3092 display_confirm_msg(); /* display ":confirm" message again */
3093 msg_row = Rows - 1;
3094 }
3095#endif
3096 else if (State == EXTERNCMD)
3097 {
3098 windgoto(msg_row, msg_col); /* put cursor back */
3099 }
3100 else if (State == HITRETURN || State == SETWSIZE)
3101 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003102 if (msg_row == Rows - 1)
3103 {
3104 /* Avoid drawing the "hit-enter" prompt below the previous one,
3105 * overwrite it. Esp. useful when regaining focus and a
3106 * FocusGained autocmd exists but didn't draw anything. */
3107 msg_didout = FALSE;
3108 msg_col = 0;
3109 msg_clr_eos();
3110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 hit_return_msg();
3112 msg_row = Rows - 1;
3113 }
3114}
3115
3116/*
3117 * msg_check_screen - check if the screen is initialized.
3118 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3119 * While starting the GUI the terminal codes will be set for the GUI, but the
3120 * output goes to the terminal. Don't use the terminal codes then.
3121 */
3122 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003123msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 if (!full_screen || !screen_valid(FALSE))
3126 return FALSE;
3127
3128 if (msg_row >= Rows)
3129 msg_row = Rows - 1;
3130 if (msg_col >= Columns)
3131 msg_col = Columns - 1;
3132 return TRUE;
3133}
3134
3135/*
3136 * Clear from current message position to end of screen.
3137 * Skip this when ":silent" was used, no need to clear for redirection.
3138 */
3139 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003140msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 if (msg_silent == 0)
3143 msg_clr_eos_force();
3144}
3145
3146/*
3147 * Clear from current message position to end of screen.
3148 * Note: msg_col is not updated, so we remember the end of the message
3149 * for msg_check().
3150 */
3151 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003152msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
3154 if (msg_use_printf())
3155 {
3156 if (full_screen) /* only when termcap codes are valid */
3157 {
3158 if (*T_CD)
3159 out_str(T_CD); /* clear to end of display */
3160 else if (*T_CE)
3161 out_str(T_CE); /* clear to end of line */
3162 }
3163 }
3164 else
3165 {
3166#ifdef FEAT_RIGHTLEFT
3167 if (cmdmsg_rl)
3168 {
3169 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3170 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3171 }
3172 else
3173#endif
3174 {
3175 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3176 ' ', ' ', 0);
3177 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3178 }
3179 }
3180}
3181
3182/*
3183 * Clear the command line.
3184 */
3185 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003186msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 msg_row = cmdline_row;
3189 msg_col = 0;
3190 msg_clr_eos_force();
3191}
3192
3193/*
3194 * end putting a message on the screen
3195 * call wait_return if the message does not fit in the available space
3196 * return TRUE if wait_return not called.
3197 */
3198 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003199msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003202 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * or the ruler option is set and we run into it,
3204 * we have to redraw the window.
3205 * Do not do this if we are abandoning the file or editing the command line.
3206 */
3207 if (!exiting && need_wait_return && !(State & CMDLINE))
3208 {
3209 wait_return(FALSE);
3210 return FALSE;
3211 }
3212 out_flush();
3213 return TRUE;
3214}
3215
3216/*
3217 * If the written message runs into the shown command or ruler, we have to
3218 * wait for hit-return and redraw the window later.
3219 */
3220 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003221msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 if (msg_row == Rows - 1 && msg_col >= sc_col)
3224 {
3225 need_wait_return = TRUE;
3226 redraw_cmdline = TRUE;
3227 }
3228}
3229
3230/*
3231 * May write a string to the redirection file.
3232 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3233 */
3234 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003235redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 char_u *s = str;
3238 static int cur_col = 0;
3239
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003240 /* Don't do anything for displaying prompts and the like. */
3241 if (redir_off)
3242 return;
3243
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003244 /* If 'verbosefile' is set prepare for writing in that file. */
3245 if (*p_vfile != NUL && verbose_fd == NULL)
3246 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003247
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003248 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 /* If the string doesn't start with CR or NL, go to msg_col */
3251 if (*s != '\n' && *s != '\r')
3252 {
3253 while (cur_col < msg_col)
3254 {
3255#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003256 if (redir_execute)
3257 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003258 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003260 else if (redir_vname)
3261 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003264 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003266 if (verbose_fd != NULL)
3267 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 ++cur_col;
3269 }
3270 }
3271
3272#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003273 if (redir_execute)
3274 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003275 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003277 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003278 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
3280
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003281 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3283 {
3284#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003285 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003287 if (redir_fd != NULL)
3288 putc(*s, redir_fd);
3289 if (verbose_fd != NULL)
3290 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (*s == '\r' || *s == '\n')
3292 cur_col = 0;
3293 else if (*s == '\t')
3294 cur_col += (8 - cur_col % 8);
3295 else
3296 ++cur_col;
3297 ++s;
3298 }
3299
3300 if (msg_silent != 0) /* should update msg_col */
3301 msg_col = cur_col;
3302 }
3303}
3304
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003305 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003306redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003307{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003308 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003309#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003310 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003311#endif
3312 ;
3313}
3314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003316 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003317 * Must always be called paired with verbose_leave()!
3318 */
3319 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003320verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003321{
3322 if (*p_vfile != NUL)
3323 ++msg_silent;
3324}
3325
3326/*
3327 * After giving verbose message.
3328 * Must always be called paired with verbose_enter()!
3329 */
3330 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003331verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003332{
3333 if (*p_vfile != NUL)
3334 if (--msg_silent < 0)
3335 msg_silent = 0;
3336}
3337
3338/*
3339 * Like verbose_enter() and set msg_scroll when displaying the message.
3340 */
3341 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003342verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003343{
3344 if (*p_vfile != NUL)
3345 ++msg_silent;
3346 else
3347 /* always scroll up, don't overwrite */
3348 msg_scroll = TRUE;
3349}
3350
3351/*
3352 * Like verbose_leave() and set cmdline_row when displaying the message.
3353 */
3354 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003355verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003356{
3357 if (*p_vfile != NUL)
3358 {
3359 if (--msg_silent < 0)
3360 msg_silent = 0;
3361 }
3362 else
3363 cmdline_row = msg_row;
3364}
3365
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003366/*
3367 * Called when 'verbosefile' is set: stop writing to the file.
3368 */
3369 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003370verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003371{
3372 if (verbose_fd != NULL)
3373 {
3374 fclose(verbose_fd);
3375 verbose_fd = NULL;
3376 }
3377 verbose_did_open = FALSE;
3378}
3379
3380/*
3381 * Open the file 'verbosefile'.
3382 * Return FAIL or OK.
3383 */
3384 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003385verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003386{
3387 if (verbose_fd == NULL && !verbose_did_open)
3388 {
3389 /* Only give the error message once. */
3390 verbose_did_open = TRUE;
3391
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003392 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003393 if (verbose_fd == NULL)
3394 {
3395 EMSG2(_(e_notopen), p_vfile);
3396 return FAIL;
3397 }
3398 }
3399 return OK;
3400}
3401
3402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * Give a warning message (for searching).
3404 * Use 'w' highlighting and may repeat the message after redrawing
3405 */
3406 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003407give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 /* Don't do this for ":silent". */
3410 if (msg_silent != 0)
3411 return;
3412
3413 /* Don't want a hit-enter prompt here. */
3414 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#ifdef FEAT_EVAL
3417 set_vim_var_string(VV_WARNINGMSG, message, -1);
3418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 vim_free(keep_msg);
3420 keep_msg = NULL;
3421 if (hl)
3422 keep_msg_attr = hl_attr(HLF_W);
3423 else
3424 keep_msg_attr = 0;
3425 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003426 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 msg_didout = FALSE; /* overwrite this message */
3428 msg_nowait = TRUE; /* don't wait for this message */
3429 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 --no_wait_return;
3432}
3433
3434/*
3435 * Advance msg cursor to column "col".
3436 */
3437 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003438msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 if (msg_silent != 0) /* nothing to advance to */
3441 {
3442 msg_col = col; /* for redirection, may fill it up later */
3443 return;
3444 }
3445 if (col >= Columns) /* not enough room */
3446 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003447#ifdef FEAT_RIGHTLEFT
3448 if (cmdmsg_rl)
3449 while (msg_col > Columns - col)
3450 msg_putchar(' ');
3451 else
3452#endif
3453 while (msg_col < col)
3454 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455}
3456
3457#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3458/*
3459 * Used for "confirm()" function, and the :confirm command prefix.
3460 * Versions which haven't got flexible dialogs yet, and console
3461 * versions, get this generic handler which uses the command line.
3462 *
3463 * type = one of:
3464 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3465 * title = title string (can be NULL for default)
3466 * (neither used in console dialogs at the moment)
3467 *
3468 * Format of the "buttons" string:
3469 * "Button1Name\nButton2Name\nButton3Name"
3470 * The first button should normally be the default/accept
3471 * The second button should be the 'Cancel' button
3472 * Other buttons- use your imagination!
3473 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3474 * different letter.
3475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003477do_dialog(
3478 int type UNUSED,
3479 char_u *title UNUSED,
3480 char_u *message,
3481 char_u *buttons,
3482 int dfltbutton,
3483 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003484 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003485 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003486 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 int oldState;
3489 int retval = 0;
3490 char_u *hotkeys;
3491 int c;
3492 int i;
3493
3494#ifndef NO_CONSOLE
3495 /* Don't output anything in silent mode ("ex -s") */
3496 if (silent_mode)
3497 return dfltbutton; /* return default option */
3498#endif
3499
3500#ifdef FEAT_GUI_DIALOG
3501 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3502 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3503 {
3504 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003505 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003506 /* avoid a hit-enter prompt without clearing the cmdline */
3507 need_wait_return = FALSE;
3508 emsg_on_display = FALSE;
3509 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 /* Flush output to avoid that further messages and redrawing is done
3512 * in the wrong order. */
3513 out_flush();
3514 gui_mch_update();
3515
3516 return c;
3517 }
3518#endif
3519
3520 oldState = State;
3521 State = CONFIRM;
3522#ifdef FEAT_MOUSE
3523 setmouse();
3524#endif
3525
3526 /*
3527 * Since we wait for a keypress, don't make the
3528 * user press RETURN as well afterwards.
3529 */
3530 ++no_wait_return;
3531 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3532
3533 if (hotkeys != NULL)
3534 {
3535 for (;;)
3536 {
3537 /* Get a typed character directly from the user. */
3538 c = get_keystroke();
3539 switch (c)
3540 {
3541 case CAR: /* User accepts default option */
3542 case NL:
3543 retval = dfltbutton;
3544 break;
3545 case Ctrl_C: /* User aborts/cancels */
3546 case ESC:
3547 retval = 0;
3548 break;
3549 default: /* Could be a hotkey? */
3550 if (c < 0) /* special keys are ignored here */
3551 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003552 if (c == ':' && ex_cmd)
3553 {
3554 retval = dfltbutton;
3555 ins_char_typebuf(':');
3556 break;
3557 }
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /* Make the character lowercase, as chars in "hotkeys" are. */
3560 c = MB_TOLOWER(c);
3561 retval = 1;
3562 for (i = 0; hotkeys[i]; ++i)
3563 {
3564#ifdef FEAT_MBYTE
3565 if (has_mbyte)
3566 {
3567 if ((*mb_ptr2char)(hotkeys + i) == c)
3568 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003569 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571 else
3572#endif
3573 if (hotkeys[i] == c)
3574 break;
3575 ++retval;
3576 }
3577 if (hotkeys[i])
3578 break;
3579 /* No hotkey match, so keep waiting */
3580 continue;
3581 }
3582 break;
3583 }
3584
3585 vim_free(hotkeys);
3586 }
3587
3588 State = oldState;
3589#ifdef FEAT_MOUSE
3590 setmouse();
3591#endif
3592 --no_wait_return;
3593 msg_end_prompt();
3594
3595 return retval;
3596}
3597
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003598static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600/*
3601 * Copy one character from "*from" to "*to", taking care of multi-byte
3602 * characters. Return the length of the character in bytes.
3603 */
3604 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003605copy_char(
3606 char_u *from,
3607 char_u *to,
3608 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610#ifdef FEAT_MBYTE
3611 int len;
3612 int c;
3613
3614 if (has_mbyte)
3615 {
3616 if (lowercase)
3617 {
3618 c = MB_TOLOWER((*mb_ptr2char)(from));
3619 return (*mb_char2bytes)(c, to);
3620 }
3621 else
3622 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003623 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 mch_memmove(to, from, (size_t)len);
3625 return len;
3626 }
3627 }
3628 else
3629#endif
3630 {
3631 if (lowercase)
3632 *to = (char_u)TOLOWER_LOC(*from);
3633 else
3634 *to = *from;
3635 return 1;
3636 }
3637}
3638
3639/*
3640 * Format the dialog string, and display it at the bottom of
3641 * the screen. Return a string of hotkey chars (if defined) for
3642 * each 'button'. If a button has no hotkey defined, the first character of
3643 * the button is used.
3644 * The hotkeys can be multi-byte characters, but without combining chars.
3645 *
3646 * Returns an allocated string with hotkeys, or NULL for error.
3647 */
3648 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003649msg_show_console_dialog(
3650 char_u *message,
3651 char_u *buttons,
3652 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 int len = 0;
3655#ifdef FEAT_MBYTE
3656# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3657#else
3658# define HOTK_LEN 1
3659#endif
3660 int lenhotkey = HOTK_LEN; /* count first button */
3661 char_u *hotk = NULL;
3662 char_u *msgp = NULL;
3663 char_u *hotkp = NULL;
3664 char_u *r;
3665 int copy;
3666#define HAS_HOTKEY_LEN 30
3667 char_u has_hotkey[HAS_HOTKEY_LEN];
3668 int first_hotkey = FALSE; /* first char of button is hotkey */
3669 int idx;
3670
3671 has_hotkey[0] = FALSE;
3672
3673 /*
3674 * First loop: compute the size of memory to allocate.
3675 * Second loop: copy to the allocated memory.
3676 */
3677 for (copy = 0; copy <= 1; ++copy)
3678 {
3679 r = buttons;
3680 idx = 0;
3681 while (*r)
3682 {
3683 if (*r == DLG_BUTTON_SEP)
3684 {
3685 if (copy)
3686 {
3687 *msgp++ = ',';
3688 *msgp++ = ' '; /* '\n' -> ', ' */
3689
3690 /* advance to next hotkey and set default hotkey */
3691#ifdef FEAT_MBYTE
3692 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003693 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 else
3695#endif
3696 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003697 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (dfltbutton)
3699 --dfltbutton;
3700
3701 /* If no hotkey is specified first char is used. */
3702 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3703 first_hotkey = TRUE;
3704 }
3705 else
3706 {
3707 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3708 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3709 if (idx < HAS_HOTKEY_LEN - 1)
3710 has_hotkey[++idx] = FALSE;
3711 }
3712 }
3713 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3714 {
3715 if (*r == DLG_HOTKEY_CHAR)
3716 ++r;
3717 first_hotkey = FALSE;
3718 if (copy)
3719 {
3720 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3721 *msgp++ = *r;
3722 else
3723 {
3724 /* '&a' -> '[a]' */
3725 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3726 msgp += copy_char(r, msgp, FALSE);
3727 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3728
3729 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003730 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 }
3733 else
3734 {
3735 ++len; /* '&a' -> '[a]' */
3736 if (idx < HAS_HOTKEY_LEN - 1)
3737 has_hotkey[idx] = TRUE;
3738 }
3739 }
3740 else
3741 {
3742 /* everything else copy literally */
3743 if (copy)
3744 msgp += copy_char(r, msgp, FALSE);
3745 }
3746
3747 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003748 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750
3751 if (copy)
3752 {
3753 *msgp++ = ':';
3754 *msgp++ = ' ';
3755 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 else
3758 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003759 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003760 + 2 /* for the NL's */
3761 + STRLEN(buttons)
3762 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003763 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /* If no hotkey is specified first char is used. */
3766 if (!has_hotkey[0])
3767 {
3768 first_hotkey = TRUE;
3769 len += 2; /* "x" -> "[x]" */
3770 }
3771
3772 /*
3773 * Now allocate and load the strings
3774 */
3775 vim_free(confirm_msg);
3776 confirm_msg = alloc(len);
3777 if (confirm_msg == NULL)
3778 return NULL;
3779 *confirm_msg = NUL;
3780 hotk = alloc(lenhotkey);
3781 if (hotk == NULL)
3782 return NULL;
3783
3784 *confirm_msg = '\n';
3785 STRCPY(confirm_msg + 1, message);
3786
3787 msgp = confirm_msg + 1 + STRLEN(message);
3788 hotkp = hotk;
3789
Bram Moolenaar6a516062007-07-05 08:11:42 +00003790 /* Define first default hotkey. Keep the hotkey string NUL
3791 * terminated to avoid reading past the end. */
3792 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /* Remember where the choices start, displaying starts here when
3795 * "hotkp" typed at the more prompt. */
3796 confirm_msg_tail = msgp;
3797 *msgp++ = '\n';
3798 }
3799 }
3800
3801 display_confirm_msg();
3802 return hotk;
3803}
3804
3805/*
3806 * Display the ":confirm" message. Also called when screen resized.
3807 */
3808 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003809display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
3811 /* avoid that 'q' at the more prompt truncates the message here */
3812 ++confirm_msg_used;
3813 if (confirm_msg != NULL)
3814 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3815 --confirm_msg_used;
3816}
3817
3818#endif /* FEAT_CON_DIALOG */
3819
3820#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3821
3822 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003823vim_dialog_yesno(
3824 int type,
3825 char_u *title,
3826 char_u *message,
3827 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 if (do_dialog(type,
3830 title == NULL ? (char_u *)_("Question") : title,
3831 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003832 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 return VIM_YES;
3834 return VIM_NO;
3835}
3836
3837 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003838vim_dialog_yesnocancel(
3839 int type,
3840 char_u *title,
3841 char_u *message,
3842 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
3844 switch (do_dialog(type,
3845 title == NULL ? (char_u *)_("Question") : title,
3846 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003847 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
3849 case 1: return VIM_YES;
3850 case 2: return VIM_NO;
3851 }
3852 return VIM_CANCEL;
3853}
3854
3855 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003856vim_dialog_yesnoallcancel(
3857 int type,
3858 char_u *title,
3859 char_u *message,
3860 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 switch (do_dialog(type,
3863 title == NULL ? (char_u *)"Question" : title,
3864 message,
3865 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003866 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868 case 1: return VIM_YES;
3869 case 2: return VIM_NO;
3870 case 3: return VIM_ALL;
3871 case 4: return VIM_DISCARDALL;
3872 }
3873 return VIM_CANCEL;
3874}
3875
3876#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3877
3878#if defined(FEAT_BROWSE) || defined(PROTO)
3879/*
3880 * Generic browse function. Calls gui_mch_browse() when possible.
3881 * Later this may pop-up a non-GUI file selector (external command?).
3882 */
3883 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003884do_browse(
3885 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3886 char_u *title, /* title for the window */
3887 char_u *dflt, /* default file name (may include directory) */
3888 char_u *ext, /* extension added */
3889 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003891 char_u *filter, /* file name filter */
3892 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 char_u *fname;
3895 static char_u *last_dir = NULL; /* last used directory */
3896 char_u *tofree = NULL;
3897 int save_browse = cmdmod.browse;
3898
3899 /* Must turn off browse to avoid that autocommands will get the
3900 * flag too! */
3901 cmdmod.browse = FALSE;
3902
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003903 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003905 if (flags & BROWSE_DIR)
3906 title = (char_u *)_("Select Directory dialog");
3907 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 title = (char_u *)_("Save File dialog");
3909 else
3910 title = (char_u *)_("Open File dialog");
3911 }
3912
3913 /* When no directory specified, use default file name, default dir, buffer
3914 * dir, last dir or current dir */
3915 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3916 {
3917 if (mch_isdir(dflt)) /* default file name is a directory */
3918 {
3919 initdir = dflt;
3920 dflt = NULL;
3921 }
3922 else if (gettail(dflt) != dflt) /* default file name includes a path */
3923 {
3924 tofree = vim_strsave(dflt);
3925 if (tofree != NULL)
3926 {
3927 initdir = tofree;
3928 *gettail(initdir) = NUL;
3929 dflt = gettail(dflt);
3930 }
3931 }
3932 }
3933
3934 if (initdir == NULL || *initdir == NUL)
3935 {
3936 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003937 if (STRCMP(p_bsdir, "last") != 0
3938 && STRCMP(p_bsdir, "buffer") != 0
3939 && STRCMP(p_bsdir, "current") != 0
3940 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 initdir = p_bsdir;
3942 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003943 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 && buf != NULL && buf->b_ffname != NULL)
3945 {
3946 if (dflt == NULL || *dflt == NUL)
3947 dflt = gettail(curbuf->b_ffname);
3948 tofree = vim_strsave(curbuf->b_ffname);
3949 if (tofree != NULL)
3950 {
3951 initdir = tofree;
3952 *gettail(initdir) = NUL;
3953 }
3954 }
3955 /* When 'browsedir' is "last", use dir from last browse */
3956 else if (*p_bsdir == 'l')
3957 initdir = last_dir;
3958 /* When 'browsedir is "current", use current directory. This is the
3959 * default already, leave initdir empty. */
3960 }
3961
3962# ifdef FEAT_GUI
3963 if (gui.in_use) /* when this changes, also adjust f_has()! */
3964 {
3965 if (filter == NULL
3966# ifdef FEAT_EVAL
3967 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3968 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3969# endif
3970 )
3971 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003972 if (flags & BROWSE_DIR)
3973 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003974# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003975 /* For systems that have a directory dialog. */
3976 fname = gui_mch_browsedir(title, initdir);
3977# else
3978 /* Generic solution for selecting a directory: select a file and
3979 * remove the file name. */
3980 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3981# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003982# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003983 /* Win32 adds a dummy file name, others return an arbitrary file
3984 * name. GTK+ 2 returns only the directory, */
3985 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3986 {
3987 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003988 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003989
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003990 if (tail == fname)
3991 *tail++ = '.'; /* use current dir */
3992 *tail = NUL;
3993 }
3994# endif
3995 }
3996 else
3997 fname = gui_mch_browse(flags & BROWSE_SAVE,
3998 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 /* We hang around in the dialog for a while, the user might do some
4001 * things to our files. The Win32 dialog allows deleting or renaming
4002 * a file, check timestamps. */
4003 need_check_timestamps = TRUE;
4004 did_check_timestamps = FALSE;
4005 }
4006 else
4007# endif
4008 {
4009 /* TODO: non-GUI file selector here */
4010 EMSG(_("E338: Sorry, no file browser in console mode"));
4011 fname = NULL;
4012 }
4013
4014 /* keep the directory for next time */
4015 if (fname != NULL)
4016 {
4017 vim_free(last_dir);
4018 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004019 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
4021 *gettail(last_dir) = NUL;
4022 if (*last_dir == NUL)
4023 {
4024 /* filename only returned, must be in current dir */
4025 vim_free(last_dir);
4026 last_dir = alloc(MAXPATHL);
4027 if (last_dir != NULL)
4028 mch_dirname(last_dir, MAXPATHL);
4029 }
4030 }
4031 }
4032
4033 vim_free(tofree);
4034 cmdmod.browse = save_browse;
4035
4036 return fname;
4037}
4038#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004039
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004040#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041static char *e_printf = N_("E766: Insufficient arguments for printf()");
4042
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004043static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004044static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004045# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004046static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004047# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004048
4049/*
4050 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4051 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004052 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004053tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054{
4055 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004056 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004057 int err = FALSE;
4058
4059 if (tvs[idx].v_type == VAR_UNKNOWN)
4060 EMSG(_(e_printf));
4061 else
4062 {
4063 ++*idxp;
4064 n = get_tv_number_chk(&tvs[idx], &err);
4065 if (err)
4066 n = 0;
4067 }
4068 return n;
4069}
4070
4071/*
4072 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004073 * If "tofree" is NULL get_tv_string_chk() is used. Some types (e.g. List)
4074 * are not converted to a string.
4075 * If "tofree" is not NULL echo_string() is used. All types are converted to
4076 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004077 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 */
4079 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004080tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004081{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004082 int idx = *idxp - 1;
4083 char *s = NULL;
4084 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085
4086 if (tvs[idx].v_type == VAR_UNKNOWN)
4087 EMSG(_(e_printf));
4088 else
4089 {
4090 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004091 if (tofree != NULL)
4092 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4093 else
4094 s = (char *)get_tv_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 }
4096 return s;
4097}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004098
4099# ifdef FEAT_FLOAT
4100/*
4101 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4102 */
4103 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004104tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004105{
4106 int idx = *idxp - 1;
4107 double f = 0;
4108
4109 if (tvs[idx].v_type == VAR_UNKNOWN)
4110 EMSG(_(e_printf));
4111 else
4112 {
4113 ++*idxp;
4114 if (tvs[idx].v_type == VAR_FLOAT)
4115 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004116 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004117 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004118 else
4119 EMSG(_("E807: Expected Float argument for printf()"));
4120 }
4121 return f;
4122}
4123# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124#endif
4125
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004126#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004127/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004128 * Return the representation of infinity for printf() function:
4129 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4130 */
4131 static const char *
4132infinity_str(int positive,
4133 char fmt_spec,
4134 int force_sign,
4135 int space_for_positive)
4136{
4137 static const char *table[] =
4138 {
4139 "-inf", "inf", "+inf", " inf",
4140 "-INF", "INF", "+INF", " INF"
4141 };
4142 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4143
4144 if (ASCII_ISUPPER(fmt_spec))
4145 idx += 4;
4146 return table[idx];
4147}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004148#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004149
4150/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004151 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004152 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004153 * consistency.
4154 *
4155 * This code is based on snprintf.c - a portable implementation of snprintf
4156 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004157 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004158 * The original code, including useful comments, can be found here:
4159 * http://www.ijs.si/software/snprintf/
4160 *
4161 * This snprintf() only supports the following conversion specifiers:
4162 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4163 * with flags: '-', '+', ' ', '0' and '#'.
4164 * An asterisk is supported for field width as well as precision.
4165 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004166 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004167 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004168 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4169 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004170 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004171 * The locale is not used, the string is used as a byte string. This is only
4172 * relevant for double-byte encodings where the second byte may be '%'.
4173 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004174 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4175 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004176 *
4177 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004178 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004179 * is greater or equal to "str_m", not all characters from the result
4180 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4181 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004182 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004183 */
4184
4185/*
4186 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004187 *
4188 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004189 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004190 */
4191
4192/* When generating prototypes all of this is skipped, cproto doesn't
4193 * understand this. */
4194#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004195
Bram Moolenaara800b422010-06-27 01:15:55 +02004196/* Like vim_vsnprintf() but append to the string. */
4197 int
4198vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4199{
4200 va_list ap;
4201 int str_l;
4202 size_t len = STRLEN(str);
4203 size_t space;
4204
4205 if (str_m <= len)
4206 space = 0;
4207 else
4208 space = str_m - len;
4209 va_start(ap, fmt);
4210 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4211 va_end(ap);
4212 return str_l;
4213}
Bram Moolenaara800b422010-06-27 01:15:55 +02004214
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004215 int
4216vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4217{
4218 va_list ap;
4219 int str_l;
4220
4221 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004222 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223 va_end(ap);
4224 return str_l;
4225}
4226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004228vim_vsnprintf(
4229 char *str,
4230 size_t str_m,
4231 char *fmt,
4232 va_list ap,
4233 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004234{
4235 size_t str_l = 0;
4236 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004237 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004238
4239 if (p == NULL)
4240 p = "";
4241 while (*p != NUL)
4242 {
4243 if (*p != '%')
4244 {
4245 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004246 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004247
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004248 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004249 if (str_l < str_m)
4250 {
4251 size_t avail = str_m - str_l;
4252
4253 mch_memmove(str + str_l, p, n > avail ? avail : n);
4254 }
4255 p += n;
4256 str_l += n;
4257 }
4258 else
4259 {
4260 size_t min_field_width = 0, precision = 0;
4261 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4262 int alternate_form = 0, force_sign = 0;
4263
4264 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4265 * ignored. */
4266 int space_for_positive = 1;
4267
4268 /* allowed values: \0, h, l, L */
4269 char length_modifier = '\0';
4270
4271 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004272# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004273# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004274 * That sounds reasonable to use as the maximum
4275 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004276# elif defined(FEAT_NUM64)
4277# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004278# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004279# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004280# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004281 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004282
4283 /* string address in case of string argument */
4284 char *str_arg;
4285
4286 /* natural field width of arg without padding and sign */
4287 size_t str_arg_l;
4288
4289 /* unsigned char argument value - only defined for c conversion.
4290 * N.B. standard explicitly states the char argument for the c
4291 * conversion is unsigned */
4292 unsigned char uchar_arg;
4293
4294 /* number of zeros to be inserted for numeric conversions as
4295 * required by the precision or minimal field width */
4296 size_t number_of_zeros_to_pad = 0;
4297
4298 /* index into tmp where zero padding is to be inserted */
4299 size_t zero_padding_insertion_ind = 0;
4300
4301 /* current conversion specifier character */
4302 char fmt_spec = '\0';
4303
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004304 /* buffer for 's' and 'S' specs */
4305 char_u *tofree = NULL;
4306
4307
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004308 str_arg = NULL;
4309 p++; /* skip '%' */
4310
4311 /* parse flags */
4312 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4313 || *p == '#' || *p == '\'')
4314 {
4315 switch (*p)
4316 {
4317 case '0': zero_padding = 1; break;
4318 case '-': justify_left = 1; break;
4319 case '+': force_sign = 1; space_for_positive = 0; break;
4320 case ' ': force_sign = 1;
4321 /* If both the ' ' and '+' flags appear, the ' '
4322 * flag should be ignored */
4323 break;
4324 case '#': alternate_form = 1; break;
4325 case '\'': break;
4326 }
4327 p++;
4328 }
4329 /* If the '0' and '-' flags both appear, the '0' flag should be
4330 * ignored. */
4331
4332 /* parse field width */
4333 if (*p == '*')
4334 {
4335 int j;
4336
4337 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004339# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004340 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341# endif
4342 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004343 if (j >= 0)
4344 min_field_width = j;
4345 else
4346 {
4347 min_field_width = -j;
4348 justify_left = 1;
4349 }
4350 }
4351 else if (VIM_ISDIGIT((int)(*p)))
4352 {
4353 /* size_t could be wider than unsigned int; make sure we treat
4354 * argument like common implementations do */
4355 unsigned int uj = *p++ - '0';
4356
4357 while (VIM_ISDIGIT((int)(*p)))
4358 uj = 10 * uj + (unsigned int)(*p++ - '0');
4359 min_field_width = uj;
4360 }
4361
4362 /* parse precision */
4363 if (*p == '.')
4364 {
4365 p++;
4366 precision_specified = 1;
4367 if (*p == '*')
4368 {
4369 int j;
4370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004373 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374# endif
4375 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004376 p++;
4377 if (j >= 0)
4378 precision = j;
4379 else
4380 {
4381 precision_specified = 0;
4382 precision = 0;
4383 }
4384 }
4385 else if (VIM_ISDIGIT((int)(*p)))
4386 {
4387 /* size_t could be wider than unsigned int; make sure we
4388 * treat argument like common implementations do */
4389 unsigned int uj = *p++ - '0';
4390
4391 while (VIM_ISDIGIT((int)(*p)))
4392 uj = 10 * uj + (unsigned int)(*p++ - '0');
4393 precision = uj;
4394 }
4395 }
4396
4397 /* parse 'h', 'l' and 'll' length modifiers */
4398 if (*p == 'h' || *p == 'l')
4399 {
4400 length_modifier = *p;
4401 p++;
4402 if (length_modifier == 'l' && *p == 'l')
4403 {
4404 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004405# ifdef FEAT_NUM64
4406 length_modifier = 'L';
4407# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004408 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004409# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004410 p++;
4411 }
4412 }
4413 fmt_spec = *p;
4414
4415 /* common synonyms: */
4416 switch (fmt_spec)
4417 {
4418 case 'i': fmt_spec = 'd'; break;
4419 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4420 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4421 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4422 default: break;
4423 }
4424
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004425# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4426 switch (fmt_spec)
4427 {
4428 case 'd': case 'u': case 'o': case 'x': case 'X':
4429 if (tvs != NULL && length_modifier == '\0')
4430 length_modifier = 'L';
4431 }
4432# endif
4433
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004434 /* get parameter value, do initial processing */
4435 switch (fmt_spec)
4436 {
4437 /* '%' and 'c' behave similar to 's' regarding flags and field
4438 * widths */
4439 case '%':
4440 case 'c':
4441 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004442 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004443 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004444 str_arg_l = 1;
4445 switch (fmt_spec)
4446 {
4447 case '%':
4448 str_arg = p;
4449 break;
4450
4451 case 'c':
4452 {
4453 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454
4455 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004457 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458# endif
4459 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004460 /* standard demands unsigned char */
4461 uchar_arg = (unsigned char)j;
4462 str_arg = (char *)&uchar_arg;
4463 break;
4464 }
4465
4466 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004467 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004468 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004470 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471# endif
4472 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004473 if (str_arg == NULL)
4474 {
4475 str_arg = "[NULL]";
4476 str_arg_l = 6;
4477 }
4478 /* make sure not to address string beyond the specified
4479 * precision !!! */
4480 else if (!precision_specified)
4481 str_arg_l = strlen(str_arg);
4482 /* truncate string if necessary as requested by precision */
4483 else if (precision == 0)
4484 str_arg_l = 0;
4485 else
4486 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004487 /* Don't put the #if inside memchr(), it can be a
4488 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004489# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004490 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004491# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492 /* memchr on HP does not like n > 2^31 !!! */
4493 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004494 precision <= (size_t)0x7fffffffL ? precision
4495 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004496# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004497 str_arg_l = (q == NULL) ? precision
4498 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004499 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004500# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004501 if (fmt_spec == 'S')
4502 {
4503 if (min_field_width != 0)
4504 min_field_width += STRLEN(str_arg)
4505 - mb_string2cells((char_u *)str_arg, -1);
4506 if (precision)
4507 {
4508 char_u *p1 = (char_u *)str_arg;
4509 size_t i;
4510
4511 for (i = 0; i < precision && *p1; i++)
4512 p1 += mb_ptr2len(p1);
4513
4514 str_arg_l = precision = p1 - (char_u *)str_arg;
4515 }
4516 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004517# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518 break;
4519
4520 default:
4521 break;
4522 }
4523 break;
4524
Bram Moolenaar91984b92016-08-16 21:58:41 +02004525 case 'd': case 'u':
4526 case 'b': case 'B':
4527 case 'o':
4528 case 'x': case 'X':
4529 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004530 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004531 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 * imply the value is unsigned; d implies a signed
4533 * value */
4534
4535 /* 0 if numeric argument is zero (or if pointer is
4536 * NULL for 'p'), +1 if greater than zero (or nonzero
4537 * for unsigned arguments), -1 if negative (unsigned
4538 * argument is never negative) */
4539 int arg_sign = 0;
4540
4541 /* only defined for length modifier h, or for no
4542 * length modifiers */
4543 int int_arg = 0;
4544 unsigned int uint_arg = 0;
4545
4546 /* only defined for length modifier l */
4547 long int long_arg = 0;
4548 unsigned long int ulong_arg = 0;
4549
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004550# ifdef FEAT_NUM64
4551 /* only defined for length modifier ll */
4552 varnumber_T llong_arg = 0;
4553 uvarnumber_T ullong_arg = 0;
4554# endif
4555
Bram Moolenaare9997822016-08-28 16:03:38 +02004556 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004557 uvarnumber_T bin_arg = 0;
4558
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004559 /* pointer argument value -only defined for p
4560 * conversion */
4561 void *ptr_arg = NULL;
4562
4563 if (fmt_spec == 'p')
4564 {
4565 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004568 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4569 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570# endif
4571 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004572 if (ptr_arg != NULL)
4573 arg_sign = 1;
4574 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004575 else if (fmt_spec == 'b' || fmt_spec == 'B')
4576 {
4577 bin_arg =
4578# if defined(FEAT_EVAL)
4579 tvs != NULL ?
4580 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4581# endif
4582 va_arg(ap, uvarnumber_T);
4583 if (bin_arg != 0)
4584 arg_sign = 1;
4585 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004586 else if (fmt_spec == 'd')
4587 {
4588 /* signed */
4589 switch (length_modifier)
4590 {
4591 case '\0':
4592 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 /* char and short arguments are passed as int. */
4594 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004596 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597# endif
4598 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004599 if (int_arg > 0)
4600 arg_sign = 1;
4601 else if (int_arg < 0)
4602 arg_sign = -1;
4603 break;
4604 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004607 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608# endif
4609 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004610 if (long_arg > 0)
4611 arg_sign = 1;
4612 else if (long_arg < 0)
4613 arg_sign = -1;
4614 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004615# ifdef FEAT_NUM64
4616 case 'L':
4617 llong_arg =
4618# if defined(FEAT_EVAL)
4619 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4620# endif
4621 va_arg(ap, varnumber_T);
4622 if (llong_arg > 0)
4623 arg_sign = 1;
4624 else if (llong_arg < 0)
4625 arg_sign = -1;
4626 break;
4627# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004628 }
4629 }
4630 else
4631 {
4632 /* unsigned */
4633 switch (length_modifier)
4634 {
4635 case '\0':
4636 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004639 tvs != NULL ? (unsigned)
4640 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641# endif
4642 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004643 if (uint_arg != 0)
4644 arg_sign = 1;
4645 break;
4646 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004649 tvs != NULL ? (unsigned long)
4650 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651# endif
4652 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004653 if (ulong_arg != 0)
4654 arg_sign = 1;
4655 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004656# ifdef FEAT_NUM64
4657 case 'L':
4658 ullong_arg =
4659# if defined(FEAT_EVAL)
4660 tvs != NULL ? (uvarnumber_T)
4661 tv_nr(tvs, &arg_idx) :
4662# endif
4663 va_arg(ap, uvarnumber_T);
4664 if (ullong_arg != 0)
4665 arg_sign = 1;
4666 break;
4667# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004668 }
4669 }
4670
4671 str_arg = tmp;
4672 str_arg_l = 0;
4673
4674 /* NOTE:
4675 * For d, i, u, o, x, and X conversions, if precision is
4676 * specified, the '0' flag should be ignored. This is so
4677 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4678 * FreeBSD, NetBSD; but not with Perl.
4679 */
4680 if (precision_specified)
4681 zero_padding = 0;
4682 if (fmt_spec == 'd')
4683 {
4684 if (force_sign && arg_sign >= 0)
4685 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4686 /* leave negative numbers for sprintf to handle, to
4687 * avoid handling tricky cases like (short int)-32768 */
4688 }
4689 else if (alternate_form)
4690 {
4691 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004692 && (fmt_spec == 'b' || fmt_spec == 'B'
4693 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004694 {
4695 tmp[str_arg_l++] = '0';
4696 tmp[str_arg_l++] = fmt_spec;
4697 }
4698 /* alternate form should have no effect for p
4699 * conversion, but ... */
4700 }
4701
4702 zero_padding_insertion_ind = str_arg_l;
4703 if (!precision_specified)
4704 precision = 1; /* default precision is 1 */
4705 if (precision == 0 && arg_sign == 0)
4706 {
4707 /* When zero value is formatted with an explicit
4708 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004709 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004710 }
4711 else
4712 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004713 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004714 int f_l = 0;
4715
4716 /* construct a simple format string for sprintf */
4717 f[f_l++] = '%';
4718 if (!length_modifier)
4719 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004720 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004721 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004722# ifdef FEAT_NUM64
4723# ifdef WIN3264
4724 f[f_l++] = 'I';
4725 f[f_l++] = '6';
4726 f[f_l++] = '4';
4727# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004728 f[f_l++] = 'l';
4729 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004730# endif
4731# else
4732 f[f_l++] = 'l';
4733# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004734 }
4735 else
4736 f[f_l++] = length_modifier;
4737 f[f_l++] = fmt_spec;
4738 f[f_l++] = '\0';
4739
4740 if (fmt_spec == 'p')
4741 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004742 else if (fmt_spec == 'b' || fmt_spec == 'B')
4743 {
4744 char b[8 * sizeof(uvarnumber_T)];
4745 size_t b_l = 0;
4746 uvarnumber_T bn = bin_arg;
4747
4748 do
4749 {
4750 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4751 bn >>= 1;
4752 }
4753 while (bn != 0);
4754
4755 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4756 str_arg_l += b_l;
4757 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004758 else if (fmt_spec == 'd')
4759 {
4760 /* signed */
4761 switch (length_modifier)
4762 {
4763 case '\0':
4764 case 'h': str_arg_l += sprintf(
4765 tmp + str_arg_l, f, int_arg);
4766 break;
4767 case 'l': str_arg_l += sprintf(
4768 tmp + str_arg_l, f, long_arg);
4769 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004770# ifdef FEAT_NUM64
4771 case 'L': str_arg_l += sprintf(
4772 tmp + str_arg_l, f, llong_arg);
4773 break;
4774# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004775 }
4776 }
4777 else
4778 {
4779 /* unsigned */
4780 switch (length_modifier)
4781 {
4782 case '\0':
4783 case 'h': str_arg_l += sprintf(
4784 tmp + str_arg_l, f, uint_arg);
4785 break;
4786 case 'l': str_arg_l += sprintf(
4787 tmp + str_arg_l, f, ulong_arg);
4788 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004789# ifdef FEAT_NUM64
4790 case 'L': str_arg_l += sprintf(
4791 tmp + str_arg_l, f, ullong_arg);
4792 break;
4793# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004794 }
4795 }
4796
4797 /* include the optional minus sign and possible
4798 * "0x" in the region before the zero padding
4799 * insertion point */
4800 if (zero_padding_insertion_ind < str_arg_l
4801 && tmp[zero_padding_insertion_ind] == '-')
4802 zero_padding_insertion_ind++;
4803 if (zero_padding_insertion_ind + 1 < str_arg_l
4804 && tmp[zero_padding_insertion_ind] == '0'
4805 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4806 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4807 zero_padding_insertion_ind += 2;
4808 }
4809
4810 {
4811 size_t num_of_digits = str_arg_l
4812 - zero_padding_insertion_ind;
4813
4814 if (alternate_form && fmt_spec == 'o'
4815 /* unless zero is already the first
4816 * character */
4817 && !(zero_padding_insertion_ind < str_arg_l
4818 && tmp[zero_padding_insertion_ind] == '0'))
4819 {
4820 /* assure leading zero for alternate-form
4821 * octal numbers */
4822 if (!precision_specified
4823 || precision < num_of_digits + 1)
4824 {
4825 /* precision is increased to force the
4826 * first character to be zero, except if a
4827 * zero value is formatted with an
4828 * explicit precision of zero */
4829 precision = num_of_digits + 1;
4830 precision_specified = 1;
4831 }
4832 }
4833 /* zero padding to specified precision? */
4834 if (num_of_digits < precision)
4835 number_of_zeros_to_pad = precision - num_of_digits;
4836 }
4837 /* zero padding to specified minimal field width? */
4838 if (!justify_left && zero_padding)
4839 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004840 int n = (int)(min_field_width - (str_arg_l
4841 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004842 if (n > 0)
4843 number_of_zeros_to_pad += n;
4844 }
4845 break;
4846 }
4847
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004848# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004849 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004850 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004851 case 'e':
4852 case 'E':
4853 case 'g':
4854 case 'G':
4855 {
4856 /* Floating point. */
4857 double f;
4858 double abs_f;
4859 char format[40];
4860 int l;
4861 int remove_trailing_zeroes = FALSE;
4862
4863 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004864# if defined(FEAT_EVAL)
4865 tvs != NULL ? tv_float(tvs, &arg_idx) :
4866# endif
4867 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004868 abs_f = f < 0 ? -f : f;
4869
4870 if (fmt_spec == 'g' || fmt_spec == 'G')
4871 {
4872 /* Would be nice to use %g directly, but it prints
4873 * "1.0" as "1", we don't want that. */
4874 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4875 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004876 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004877 else
4878 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4879 remove_trailing_zeroes = TRUE;
4880 }
4881
Bram Moolenaar04186092016-08-29 21:55:35 +02004882 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004883# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004884 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004885# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004886 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004887# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004888 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004889 {
4890 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004891 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4892 force_sign, space_for_positive));
4893 str_arg_l = STRLEN(tmp);
4894 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004895 }
4896 else
4897 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004898 if (isnan(f))
4899 {
4900 /* Not a number: nan or NAN */
4901 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4902 : "nan");
4903 str_arg_l = 3;
4904 zero_padding = 0;
4905 }
4906 else if (isinf(f))
4907 {
4908 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4909 force_sign, space_for_positive));
4910 str_arg_l = STRLEN(tmp);
4911 zero_padding = 0;
4912 }
4913 else
Bram Moolenaar04186092016-08-29 21:55:35 +02004914 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004915 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004916 format[0] = '%';
4917 l = 1;
4918 if (force_sign)
4919 format[l++] = space_for_positive ? ' ' : '+';
4920 if (precision_specified)
4921 {
4922 size_t max_prec = TMP_LEN - 10;
4923
4924 /* Make sure we don't get more digits than we
4925 * have room for. */
4926 if ((fmt_spec == 'f' || fmt_spec == 'F')
4927 && abs_f > 1.0)
4928 max_prec -= (size_t)log10(abs_f);
4929 if (precision > max_prec)
4930 precision = max_prec;
4931 l += sprintf(format + l, ".%d", (int)precision);
4932 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004933 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004934 format[l + 1] = NUL;
4935
Bram Moolenaare9997822016-08-28 16:03:38 +02004936 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar04186092016-08-29 21:55:35 +02004937 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004938
Bram Moolenaara7241f52008-06-24 20:39:31 +00004939 if (remove_trailing_zeroes)
4940 {
4941 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004942 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004943
4944 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02004945 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004946 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004947 else
4948 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004949 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004950 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004951 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004952 {
4953 /* Remove superfluous '+' and leading
4954 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004955 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004956 {
4957 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004958 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004959 --str_arg_l;
4960 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004961 i = (tp[1] == '-') ? 2 : 1;
4962 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004963 {
4964 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004965 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004966 --str_arg_l;
4967 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004968 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004969 }
4970 }
4971
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004972 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004973 /* Remove trailing zeroes, but keep the one
4974 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004975 while (tp > tmp + 2 && *tp == '0'
4976 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004977 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004978 STRMOVE(tp, tp + 1);
4979 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004980 --str_arg_l;
4981 }
4982 }
4983 else
4984 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004985 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004986
4987 /* Be consistent: some printf("%e") use 1.0e+12
4988 * and some 1.0e+012. Remove one zero in the last
4989 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004990 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004991 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004992 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4993 && tp[2] == '0'
4994 && vim_isdigit(tp[3])
4995 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004996 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004997 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004998 --str_arg_l;
4999 }
5000 }
5001 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005002 if (zero_padding && min_field_width > str_arg_l
5003 && (tmp[0] == '-' || force_sign))
5004 {
5005 /* padding 0's should be inserted after the sign */
5006 number_of_zeros_to_pad = min_field_width - str_arg_l;
5007 zero_padding_insertion_ind = 1;
5008 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005009 str_arg = tmp;
5010 break;
5011 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005012# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005013
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005014 default:
5015 /* unrecognized conversion specifier, keep format string
5016 * as-is */
5017 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005018 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005019 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005020 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005021
5022 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005023 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005024 str_arg = p;
5025 str_arg_l = 0;
5026 if (*p != NUL)
5027 str_arg_l++; /* include invalid conversion specifier
5028 unchanged if not at end-of-string */
5029 break;
5030 }
5031
5032 if (*p != NUL)
5033 p++; /* step over the just processed conversion specifier */
5034
5035 /* insert padding to the left as requested by min_field_width;
5036 * this does not include the zero padding in case of numerical
5037 * conversions*/
5038 if (!justify_left)
5039 {
5040 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005041 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005042
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005043 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005044 {
5045 if (str_l < str_m)
5046 {
5047 size_t avail = str_m - str_l;
5048
5049 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005050 (size_t)pn > avail ? avail
5051 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005052 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005053 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005054 }
5055 }
5056
5057 /* zero padding as requested by the precision or by the minimal
5058 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005059 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005060 {
5061 /* will not copy first part of numeric right now, *
5062 * force it to be copied later in its entirety */
5063 zero_padding_insertion_ind = 0;
5064 }
5065 else
5066 {
5067 /* insert first part of numerics (sign or '0x') before zero
5068 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005069 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005070
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005071 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072 {
5073 if (str_l < str_m)
5074 {
5075 size_t avail = str_m - str_l;
5076
5077 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005078 (size_t)zn > avail ? avail
5079 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005080 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005081 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005082 }
5083
5084 /* insert zero padding as requested by the precision or min
5085 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005086 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005087 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005088 {
5089 if (str_l < str_m)
5090 {
5091 size_t avail = str_m-str_l;
5092
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005093 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005094 (size_t)zn > avail ? avail
5095 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005096 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005097 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005098 }
5099 }
5100
5101 /* insert formatted string
5102 * (or as-is conversion specifier for unknown conversions) */
5103 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005104 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005105
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005106 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005107 {
5108 if (str_l < str_m)
5109 {
5110 size_t avail = str_m - str_l;
5111
5112 mch_memmove(str + str_l,
5113 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005114 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005115 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005116 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005117 }
5118 }
5119
5120 /* insert right padding */
5121 if (justify_left)
5122 {
5123 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005124 int pn = (int)(min_field_width
5125 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005126
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005127 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005128 {
5129 if (str_l < str_m)
5130 {
5131 size_t avail = str_m - str_l;
5132
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005133 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005134 (size_t)pn > avail ? avail
5135 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005136 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005137 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005138 }
5139 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005140 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005141 }
5142 }
5143
5144 if (str_m > 0)
5145 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005146 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005147 * overwriting the last character (shouldn't happen, but just in case)
5148 * */
5149 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5150 }
5151
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005152 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005155 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005156 * character), that is, the number of characters that would have been
5157 * written to the buffer if it were large enough. */
5158 return (int)str_l;
5159}
5160
5161#endif /* PROTO */