blob: 5379b5b2e397f71b76c398b4282263cb56e8cc10 [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 Moolenaar5a66dfb2017-03-01 20:40:39 +0100542#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100543static 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))
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +0100608 /* don't call msg() if it results in a dialog */
609 return msg_use_printf() ? FALSE : msg(s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100610#endif
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 called_emsg = TRUE;
613
614 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000615 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
616 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
618#ifdef FEAT_EVAL
619 severe = emsg_severe;
620 emsg_severe = FALSE;
621#endif
622
Bram Moolenaar57657d82006-04-21 22:12:41 +0000623 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {
625#ifdef FEAT_EVAL
626 /*
627 * Cause a throw of an error exception if appropriate. Don't display
628 * the error message in this case. (If no matching catch clause will
629 * be found, the message will be displayed later on.) "ignore" is set
630 * when the message should be ignored completely (used for the
631 * interrupt message).
632 */
633 if (cause_errthrow(s, severe, &ignore) == TRUE)
634 {
635 if (!ignore)
636 did_emsg = TRUE;
637 return TRUE;
638 }
639
640 /* set "v:errmsg", also when using ":silent! cmd" */
641 set_vim_var_string(VV_ERRMSG, s, -1);
642#endif
643
644 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000645 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 * But do write it to the redirection file.
647 */
648 if (emsg_silent != 0)
649 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200650 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200652 msg_start();
653 p = get_emsg_source();
654 if (p != NULL)
655 {
656 STRCAT(p, "\n");
657 redir_write(p, -1);
658 vim_free(p);
659 }
660 p = get_emsg_lnum();
661 if (p != NULL)
662 {
663 STRCAT(p, "\n");
664 redir_write(p, -1);
665 vim_free(p);
666 }
667 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100669#ifdef FEAT_JOB_CHANNEL
670 ch_logs(NULL, "ERROR: %s", (char *)s);
671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 return TRUE;
673 }
674
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100675 ex_exitval = 1;
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /* Reset msg_silent, an error causes messages to be switched back on. */
678 msg_silent = 0;
679 cmd_silent = FALSE;
680
681 if (global_busy) /* break :global command */
682 ++global_busy;
683
684 if (p_eb)
685 beep_flush(); /* also includes flush_buffers() */
686 else
687 flush_buffers(FALSE); /* flush internal buffers */
688 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690
691 emsg_on_display = TRUE; /* remember there is an error message */
692 ++msg_scroll; /* don't overwrite a previous message */
693 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000694 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 need_wait_return = TRUE; /* needed in case emsg() is called after
696 * wait_return has reset need_wait_return
697 * and a redraw is expected because
698 * msg_scrolled is non-zero */
699
Bram Moolenaar958dc692016-12-01 15:34:12 +0100700#ifdef FEAT_JOB_CHANNEL
701 emsg_to_channel_log = TRUE;
702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 /*
704 * Display name and line number for the source of the error.
705 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000706 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
708 /*
709 * Display the error message itself.
710 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000711 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar958dc692016-12-01 15:34:12 +0100712 r = msg_attr(s, attr);
713
714#ifdef FEAT_JOB_CHANNEL
715 emsg_to_channel_log = FALSE;
716#endif
717 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718}
719
Bram Moolenaar95f09602016-11-10 20:01:45 +0100720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * Print an error message with one "%s" and one string argument.
723 */
724 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100725emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
727 return emsg3(s, a1, NULL);
728}
729
Bram Moolenaar95f09602016-11-10 20:01:45 +0100730/*
731 * Print an error message with one or two "%s" and one or two string arguments.
732 * This is not in message.c to avoid a warning for prototypes.
733 */
734 int
735emsg3(char_u *s, char_u *a1, char_u *a2)
736{
737 if (emsg_not_now())
738 return TRUE; /* no error messages at the moment */
739 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
740 return emsg(IObuff);
741}
742
743/*
744 * Print an error message with one "%ld" and one long int argument.
745 * This is not in message.c to avoid a warning for prototypes.
746 */
747 int
748emsgn(char_u *s, long n)
749{
750 if (emsg_not_now())
751 return TRUE; /* no error messages at the moment */
752 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
753 return emsg(IObuff);
754}
755
756/*
757 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
758 * defined. It is used for internal errors only, so that they can be
759 * detected when fuzzing vim.
760 */
761 void
762iemsg(char_u *s)
763{
764 msg(s);
765#ifdef ABORT_ON_INTERNAL_ERROR
766 abort();
767#endif
768}
769
770
771/*
772 * Same as emsg2(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
773 * defined. It is used for internal errors only, so that they can be
774 * detected when fuzzing vim.
775 */
776 void
777iemsg2(char_u *s, char_u *a1)
778{
779 emsg2(s, a1);
780#ifdef ABORT_ON_INTERNAL_ERROR
781 abort();
782#endif
783}
784
785/*
786 * Same as emsgn(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
787 * defined. It is used for internal errors only, so that they can be
788 * detected when fuzzing vim.
789 */
790 void
791iemsgn(char_u *s, long n)
792{
793 emsgn(s, n);
794#ifdef ABORT_ON_INTERNAL_ERROR
795 abort();
796#endif
797}
798
799/*
800 * Give an "Internal error" message.
801 */
802 void
803internal_error(char *where)
804{
805 IEMSG2(_(e_intern2), where);
806}
807
Bram Moolenaarea424162005-06-16 21:51:00 +0000808/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaardf177f62005-02-22 08:39:57 +0000810 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100811emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000812{
813 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
814}
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Like msg(), but truncate to a single line if p_shm contains 't', or when
818 * "force" is TRUE. This truncates in another way as for normal messages.
819 * Careful: The string may be changed by msg_may_trunc()!
820 * Returns a pointer to the printed message, if wait_return() not called.
821 */
822 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100823msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 int n;
826
827 /* Add message to history before truncating */
828 add_msg_hist(s, -1, attr);
829
830 s = msg_may_trunc(force, s);
831
832 msg_hist_off = TRUE;
833 n = msg_attr(s, attr);
834 msg_hist_off = FALSE;
835
836 if (n)
837 return s;
838 return NULL;
839}
840
841/*
842 * Check if message "s" should be truncated at the start (for filenames).
843 * Return a pointer to where the truncated message starts.
844 * Note: May change the message by replacing a character with '<'.
845 */
846 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100847msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849 int n;
850 int room;
851
852 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
853 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
854 && (n = (int)STRLEN(s) - room) > 0)
855 {
856#ifdef FEAT_MBYTE
857 if (has_mbyte)
858 {
859 int size = vim_strsize(s);
860
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000861 /* There may be room anyway when there are multibyte chars. */
862 if (size <= room)
863 return s;
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 for (n = 0; size >= room; )
866 {
867 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000868 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 --n;
871 }
872#endif
873 s += n;
874 *s = '<';
875 }
876 return s;
877}
878
879 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100880add_msg_hist(
881 char_u *s,
882 int len, /* -1 for undetermined length */
883 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 struct msg_hist *p;
886
887 if (msg_hist_off || msg_silent != 0)
888 return;
889
890 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000891 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000892 (void)delete_first_msg();
893
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 /* allocate an entry and add the message at the end of the history */
895 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
896 if (p != NULL)
897 {
898 if (len < 0)
899 len = (int)STRLEN(s);
900 /* remove leading and trailing newlines */
901 while (len > 0 && *s == '\n')
902 {
903 ++s;
904 --len;
905 }
906 while (len > 0 && s[len - 1] == '\n')
907 --len;
908 p->msg = vim_strnsave(s, len);
909 p->next = NULL;
910 p->attr = attr;
911 if (last_msg_hist != NULL)
912 last_msg_hist->next = p;
913 last_msg_hist = p;
914 if (first_msg_hist == NULL)
915 first_msg_hist = last_msg_hist;
916 ++msg_hist_len;
917 }
918}
919
920/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000921 * Delete the first (oldest) message from the history.
922 * Returns FAIL if there are no messages.
923 */
924 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100925delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000926{
927 struct msg_hist *p;
928
929 if (msg_hist_len <= 0)
930 return FAIL;
931 p = first_msg_hist;
932 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000933 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200934 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000935 vim_free(p->msg);
936 vim_free(p);
937 --msg_hist_len;
938 return OK;
939}
940
941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 * ":messages" command.
943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200945ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 struct msg_hist *p;
948 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200949 int c = 0;
950
951 if (STRCMP(eap->arg, "clear") == 0)
952 {
953 int keep = eap->addr_count == 0 ? 0 : eap->line2;
954
955 while (msg_hist_len > keep)
956 (void)delete_first_msg();
957 return;
958 }
959
960 if (*eap->arg != NUL)
961 {
962 EMSG(_(e_invarg));
963 return;
964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 msg_hist_off = TRUE;
967
Bram Moolenaar451f8492016-04-14 17:16:22 +0200968 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200969 if (eap->addr_count != 0)
970 {
971 /* Count total messages */
972 for (; p != NULL && !got_int; p = p->next)
973 c++;
974
975 c -= eap->line2;
976
977 /* Skip without number of messages specified */
978 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
979 p = p->next, c--);
980 }
981
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200982 if (p == first_msg_hist)
983 {
984 s = mch_getenv((char_u *)"LANG");
985 if (s != NULL && *s != NUL)
986 msg_attr((char_u *)
987 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
988 hl_attr(HLF_T));
989 }
990
Bram Moolenaar451f8492016-04-14 17:16:22 +0200991 /* Display what was not skipped. */
992 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (p->msg != NULL)
994 msg_attr(p->msg, p->attr);
995
996 msg_hist_off = FALSE;
997}
998
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000999#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000/*
1001 * Call this after prompting the user. This will avoid a hit-return message
1002 * and a delay.
1003 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001004 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001005msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 need_wait_return = FALSE;
1008 emsg_on_display = FALSE;
1009 cmdline_row = msg_row;
1010 msg_col = 0;
1011 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001012 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014#endif
1015
1016/*
1017 * wait for the user to hit a key (normally a return)
1018 * if 'redraw' is TRUE, clear and redraw the screen
1019 * if 'redraw' is FALSE, just redraw the screen
1020 * if 'redraw' is -1, don't redraw at all
1021 */
1022 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001023wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 int c;
1026 int oldState;
1027 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001029 int save_Recording;
1030 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 if (redraw == TRUE)
1033 must_redraw = CLEAR;
1034
1035 /* If using ":silent cmd", don't wait for a return. Also don't set
1036 * need_wait_return to do it later. */
1037 if (msg_silent != 0)
1038 return;
1039
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001040 /*
1041 * When inside vgetc(), we can't wait for a typed character at all.
1042 * With the global command (and some others) we only need one return at
1043 * the end. Adjust cmdline_row to avoid the next message overwriting the
1044 * last one.
1045 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001046 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001048 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (no_wait_return)
1050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (!exmode_active)
1052 cmdline_row = msg_row;
1053 return;
1054 }
1055
1056 redir_off = TRUE; /* don't redirect this message */
1057 oldState = State;
1058 if (quit_more)
1059 {
1060 c = CAR; /* just pretend CR was hit */
1061 quit_more = FALSE;
1062 got_int = FALSE;
1063 }
1064 else if (exmode_active)
1065 {
1066 MSG_PUTS(" "); /* make sure the cursor is on the right line */
1067 c = CAR; /* no need for a return in ex mode */
1068 got_int = FALSE;
1069 }
1070 else
1071 {
1072 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1073 * just changed. */
1074 screenalloc(FALSE);
1075
1076 State = HITRETURN;
1077#ifdef FEAT_MOUSE
1078 setmouse();
1079#endif
1080#ifdef USE_ON_FLY_SCROLL
1081 dont_scroll = TRUE; /* disallow scrolling here */
1082#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001083 cmdline_row = msg_row;
1084
Bram Moolenaarec38d692013-04-24 15:12:32 +02001085 /* Avoid the sequence that the user types ":" at the hit-return prompt
1086 * to start an Ex command, but the file-changed dialog gets in the
1087 * way. */
1088 if (need_check_timestamps)
1089 check_timestamps(FALSE);
1090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 hit_return_msg();
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 do
1094 {
1095 /* Remember "got_int", if it is set vgetc() probably returns a
1096 * CTRL-C, but we need to loop then. */
1097 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001098
1099 /* Don't do mappings here, we put the character back in the
1100 * typeahead buffer. */
1101 ++no_mapping;
1102 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001103
1104 /* Temporarily disable Recording. If Recording is active, the
1105 * character will be recorded later, since it will be added to the
1106 * typebuf after the loop */
1107 save_Recording = Recording;
1108 save_scriptout = scriptout;
1109 Recording = FALSE;
1110 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001112 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001114 --no_mapping;
1115 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001116 Recording = save_Recording;
1117 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#ifdef FEAT_CLIPBOARD
1120 /* Strange way to allow copying (yanking) a modeless selection at
1121 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1122 * Cmdline-mode and it's harmless when there is no selection. */
1123 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1124 {
1125 clip_copy_modeless_selection(TRUE);
1126 c = K_IGNORE;
1127 }
1128#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001129
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001130 /*
1131 * Allow scrolling back in the messages.
1132 * Also accept scroll-down commands when messages fill the screen,
1133 * to avoid that typing one 'j' too many makes the messages
1134 * disappear.
1135 */
1136 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001137 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001138 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1139 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001140 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001141 if (msg_scrolled > Rows)
1142 /* scroll back to show older messages */
1143 do_more_prompt(c);
1144 else
1145 {
1146 msg_didout = FALSE;
1147 c = K_IGNORE;
1148 msg_col =
1149#ifdef FEAT_RIGHTLEFT
1150 cmdmsg_rl ? Columns - 1 :
1151#endif
1152 0;
1153 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001154 if (quit_more)
1155 {
1156 c = CAR; /* just pretend CR was hit */
1157 quit_more = FALSE;
1158 got_int = FALSE;
1159 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001160 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001161 {
1162 c = K_IGNORE;
1163 hit_return_msg();
1164 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001165 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001166 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001167 && (c == 'j' || c == 'd' || c == 'f'
1168 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001169 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 } while ((had_got_int && c == Ctrl_C)
1172 || c == K_IGNORE
1173#ifdef FEAT_GUI
1174 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1175#endif
1176#ifdef FEAT_MOUSE
1177 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1178 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1179 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001180 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 || c == K_MOUSEDOWN || c == K_MOUSEUP
1182 || (!mouse_has(MOUSE_RETURN)
1183 && mouse_row < msg_row
1184 && (c == K_LEFTMOUSE
1185 || c == K_MIDDLEMOUSE
1186 || c == K_RIGHTMOUSE
1187 || c == K_X1MOUSE
1188 || c == K_X2MOUSE))
1189#endif
1190 );
1191 ui_breakcheck();
1192#ifdef FEAT_MOUSE
1193 /*
1194 * Avoid that the mouse-up event causes visual mode to start.
1195 */
1196 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1197 || c == K_X1MOUSE || c == K_X2MOUSE)
1198 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1199 else
1200#endif
1201 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1202 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001203 /* Put the character back in the typeahead buffer. Don't use the
1204 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001205 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001207 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210 redir_off = FALSE;
1211
1212 /*
1213 * If the user hits ':', '?' or '/' we get a command line from the next
1214 * line.
1215 */
1216 if (c == ':' || c == '?' || c == '/')
1217 {
1218 if (!exmode_active)
1219 cmdline_row = msg_row;
1220 skip_redraw = TRUE; /* skip redraw once */
1221 do_redraw = FALSE;
1222 }
1223
1224 /*
1225 * If the window size changed set_shellsize() will redraw the screen.
1226 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1227 * typed.
1228 */
1229 tmpState = State;
1230 State = oldState; /* restore State before set_shellsize */
1231#ifdef FEAT_MOUSE
1232 setmouse();
1233#endif
1234 msg_check();
1235
1236#if defined(UNIX) || defined(VMS)
1237 /*
1238 * When switching screens, we need to output an extra newline on exit.
1239 */
1240 if (swapping_screen() && !termcap_active)
1241 newline_on_exit = TRUE;
1242#endif
1243
1244 need_wait_return = FALSE;
1245 did_wait_return = TRUE;
1246 emsg_on_display = FALSE; /* can delete error message now */
1247 lines_left = -1; /* reset lines_left at next msg_start() */
1248 reset_last_sourcing();
1249 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1250 (Rows - cmdline_row - 1) * Columns + sc_col)
1251 {
1252 vim_free(keep_msg);
1253 keep_msg = NULL; /* don't redisplay message, it's too long */
1254 }
1255
1256 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1257 {
1258 starttermcap(); /* start termcap before redrawing */
1259 shell_resized();
1260 }
1261 else if (!skip_redraw
1262 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1263 {
1264 starttermcap(); /* start termcap before redrawing */
1265 redraw_later(VALID);
1266 }
1267}
1268
1269/*
1270 * Write the hit-return prompt.
1271 */
1272 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001273hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001275 int save_p_more = p_more;
1276
1277 p_more = FALSE; /* don't want see this message when scrolling back */
1278 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 msg_putchar('\n');
1280 if (got_int)
1281 MSG_PUTS(_("Interrupt: "));
1282
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001283 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 if (!msg_use_printf())
1285 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001286 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287}
1288
1289/*
1290 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1291 */
1292 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001293set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 vim_free(keep_msg);
1296 if (s != NULL && msg_silent == 0)
1297 keep_msg = vim_strsave(s);
1298 else
1299 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001300 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001301 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302}
1303
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001304#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1305/*
1306 * If there currently is a message being displayed, set "keep_msg" to it, so
1307 * that it will be displayed again after redraw.
1308 */
1309 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001310set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001311{
1312 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1313 && (State & NORMAL))
1314 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1315}
1316#endif
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318/*
1319 * Prepare for outputting characters in the command line.
1320 */
1321 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001322msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 int did_return = FALSE;
1325
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001326 if (!msg_silent)
1327 {
1328 vim_free(keep_msg);
1329 keep_msg = NULL; /* don't display old message now */
1330 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001331
1332#ifdef FEAT_EVAL
1333 if (need_clr_eos)
1334 {
1335 /* Halfway an ":echo" command and getting an (error) message: clear
1336 * any text from the command. */
1337 need_clr_eos = FALSE;
1338 msg_clr_eos();
1339 }
1340#endif
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (!msg_scroll && full_screen) /* overwrite last message */
1343 {
1344 msg_row = cmdline_row;
1345 msg_col =
1346#ifdef FEAT_RIGHTLEFT
1347 cmdmsg_rl ? Columns - 1 :
1348#endif
1349 0;
1350 }
1351 else if (msg_didout) /* start message on next line */
1352 {
1353 msg_putchar('\n');
1354 did_return = TRUE;
1355 if (exmode_active != EXMODE_NORMAL)
1356 cmdline_row = msg_row;
1357 }
1358 if (!msg_didany || lines_left < 0)
1359 msg_starthere();
1360 if (msg_silent == 0)
1361 {
1362 msg_didout = FALSE; /* no output on current line yet */
1363 cursor_off();
1364 }
1365
1366 /* when redirecting, may need to start a new line. */
1367 if (!did_return)
1368 redir_write((char_u *)"\n", -1);
1369}
1370
1371/*
1372 * Note that the current msg position is where messages start.
1373 */
1374 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001375msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 lines_left = cmdline_row;
1378 msg_didany = FALSE;
1379}
1380
1381 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001382msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 msg_putchar_attr(c, 0);
1385}
1386
1387 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001388msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390#ifdef FEAT_MBYTE
1391 char_u buf[MB_MAXBYTES + 1];
1392#else
1393 char_u buf[4];
1394#endif
1395
1396 if (IS_SPECIAL(c))
1397 {
1398 buf[0] = K_SPECIAL;
1399 buf[1] = K_SECOND(c);
1400 buf[2] = K_THIRD(c);
1401 buf[3] = NUL;
1402 }
1403 else
1404 {
1405#ifdef FEAT_MBYTE
1406 buf[(*mb_char2bytes)(c, buf)] = NUL;
1407#else
1408 buf[0] = c;
1409 buf[1] = NUL;
1410#endif
1411 }
1412 msg_puts_attr(buf, attr);
1413}
1414
1415 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001416msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418 char_u buf[20];
1419
1420 sprintf((char *)buf, "%ld", n);
1421 msg_puts(buf);
1422}
1423
1424 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001425msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
1427 msg_home_replace_attr(fname, 0);
1428}
1429
1430#if defined(FEAT_FIND_ID) || defined(PROTO)
1431 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001432msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 msg_home_replace_attr(fname, hl_attr(HLF_D));
1435}
1436#endif
1437
1438 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001439msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 char_u *name;
1442
1443 name = home_replace_save(NULL, fname);
1444 if (name != NULL)
1445 msg_outtrans_attr(name, attr);
1446 vim_free(name);
1447}
1448
1449/*
1450 * Output 'len' characters in 'str' (including NULs) with translation
1451 * if 'len' is -1, output upto a NUL character.
1452 * Use attributes 'attr'.
1453 * Return the number of characters it takes on the screen.
1454 */
1455 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001456msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 return msg_outtrans_attr(str, 0);
1459}
1460
1461 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001462msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1465}
1466
1467 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001468msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 return msg_outtrans_len_attr(str, len, 0);
1471}
1472
1473/*
1474 * Output one character at "p". Return pointer to the next character.
1475 * Handles multi-byte characters.
1476 */
1477 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001478msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
1480#ifdef FEAT_MBYTE
1481 int l;
1482
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001483 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 msg_outtrans_len_attr(p, l, attr);
1486 return p + l;
1487 }
1488#endif
1489 msg_puts_attr(transchar_byte(*p), attr);
1490 return p + 1;
1491}
1492
1493 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001494msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 int retval = 0;
1497 char_u *str = msgstr;
1498 char_u *plain_start = msgstr;
1499 char_u *s;
1500#ifdef FEAT_MBYTE
1501 int mb_l;
1502 int c;
1503#endif
1504
1505 /* if MSG_HIST flag set, add message to history */
1506 if (attr & MSG_HIST)
1507 {
1508 add_msg_hist(str, len, attr);
1509 attr &= ~MSG_HIST;
1510 }
1511
1512#ifdef FEAT_MBYTE
1513 /* If the string starts with a composing character first draw a space on
1514 * which the composing char can be drawn. */
1515 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1516 msg_puts_attr((char_u *)" ", attr);
1517#endif
1518
1519 /*
1520 * Go over the string. Special characters are translated and printed.
1521 * Normal characters are printed several at a time.
1522 */
1523 while (--len >= 0)
1524 {
1525#ifdef FEAT_MBYTE
1526 if (enc_utf8)
1527 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001528 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001530 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 else
1532 mb_l = 1;
1533 if (has_mbyte && mb_l > 1)
1534 {
1535 c = (*mb_ptr2char)(str);
1536 if (vim_isprintc(c))
1537 /* printable multi-byte char: count the cells. */
1538 retval += (*mb_ptr2cells)(str);
1539 else
1540 {
1541 /* unprintable multi-byte char: print the printable chars so
1542 * far and the translation of the unprintable char. */
1543 if (str > plain_start)
1544 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1545 attr);
1546 plain_start = str + mb_l;
1547 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1548 retval += char2cells(c);
1549 }
1550 len -= mb_l - 1;
1551 str += mb_l;
1552 }
1553 else
1554#endif
1555 {
1556 s = transchar_byte(*str);
1557 if (s[1] != NUL)
1558 {
1559 /* unprintable char: print the printable chars so far and the
1560 * translation of the unprintable char. */
1561 if (str > plain_start)
1562 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1563 attr);
1564 plain_start = str + 1;
1565 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001566 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001568 else
1569 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 ++str;
1571 }
1572 }
1573
1574 if (str > plain_start)
1575 /* print the printable chars at the end */
1576 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1577
1578 return retval;
1579}
1580
1581#if defined(FEAT_QUICKFIX) || defined(PROTO)
1582 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001583msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 int i;
1586 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1587
1588 arg = skipwhite(arg);
1589 for (i = 5; *arg && i >= 0; --i)
1590 if (*arg++ != str[i])
1591 break;
1592 if (i < 0)
1593 {
1594 msg_putchar('\n');
1595 for (i = 0; rs[i]; ++i)
1596 msg_putchar(rs[i] - 3);
1597 }
1598}
1599#endif
1600
1601/*
1602 * Output the string 'str' upto a NUL character.
1603 * Return the number of characters it takes on the screen.
1604 *
1605 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1606 * following character and shown as <F1>, <S-Up> etc. Any other character
1607 * which is not printable shown in <> form.
1608 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1609 * If a character is displayed in one of these special ways, is also
1610 * highlighted (its highlight name is '8' in the p_hl variable).
1611 * Otherwise characters are not highlighted.
1612 * This function is used to show mappings, where we want to see how to type
1613 * the character/string -- webb
1614 */
1615 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001616msg_outtrans_special(
1617 char_u *strstart,
1618 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 char_u *str = strstart;
1621 int retval = 0;
1622 char_u *string;
1623 int attr;
1624 int len;
1625
1626 attr = hl_attr(HLF_8);
1627 while (*str != NUL)
1628 {
1629 /* Leading and trailing spaces need to be displayed in <> form. */
1630 if ((str == strstart || str[1] == NUL) && *str == ' ')
1631 {
1632 string = (char_u *)"<Space>";
1633 ++str;
1634 }
1635 else
1636 string = str2special(&str, from);
1637 len = vim_strsize(string);
1638 /* Highlight special keys */
1639 msg_puts_attr(string, len > 1
1640#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001641 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643 ? attr : 0);
1644 retval += len;
1645 }
1646 return retval;
1647}
1648
Bram Moolenaarbd743252010-10-20 21:23:33 +02001649#if defined(FEAT_EVAL) || defined(PROTO)
1650/*
1651 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1652 * strings, in an allocated string.
1653 */
1654 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001655str2special_save(
1656 char_u *str,
1657 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001658{
1659 garray_T ga;
1660 char_u *p = str;
1661
1662 ga_init2(&ga, 1, 40);
1663 while (*p != NUL)
1664 ga_concat(&ga, str2special(&p, is_lhs));
1665 ga_append(&ga, NUL);
1666 return (char_u *)ga.ga_data;
1667}
1668#endif
1669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670/*
1671 * Return the printable string for the key codes at "*sp".
1672 * Used for translating the lhs or rhs of a mapping to printable chars.
1673 * Advances "sp" to the next code.
1674 */
1675 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001676str2special(
1677 char_u **sp,
1678 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 int c;
1681 static char_u buf[7];
1682 char_u *str = *sp;
1683 int modifiers = 0;
1684 int special = FALSE;
1685
1686#ifdef FEAT_MBYTE
1687 if (has_mbyte)
1688 {
1689 char_u *p;
1690
1691 /* Try to un-escape a multi-byte character. Return the un-escaped
1692 * string if it is a multi-byte character. */
1693 p = mb_unescape(sp);
1694 if (p != NULL)
1695 return p;
1696 }
1697#endif
1698
1699 c = *str;
1700 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1701 {
1702 if (str[1] == KS_MODIFIER)
1703 {
1704 modifiers = str[2];
1705 str += 3;
1706 c = *str;
1707 }
1708 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1709 {
1710 c = TO_SPECIAL(str[1], str[2]);
1711 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001712 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 c = NUL;
1714 }
1715 if (IS_SPECIAL(c) || modifiers) /* special key */
1716 special = TRUE;
1717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001720 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001722 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001723
1724 /* For multi-byte characters check for an illegal byte. */
1725 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1726 {
1727 transchar_nonprint(buf, c);
1728 *sp = str + 1;
1729 return buf;
1730 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001731 /* Since 'special' is TRUE the multi-byte character 'c' will be
1732 * processed by get_special_key_name() */
1733 c = (*mb_ptr2char)(str);
1734 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001738 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1741 * Use <Space> only for lhs of a mapping. */
1742 if (special || char2cells(c) > 1 || (from && c == ' '))
1743 return get_special_key_name(c, modifiers);
1744 buf[0] = c;
1745 buf[1] = NUL;
1746 return buf;
1747}
1748
1749/*
1750 * Translate a key sequence into special key names.
1751 */
1752 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001753str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 char_u *s;
1756
1757 *buf = NUL;
1758 while (*sp)
1759 {
1760 s = str2special(&sp, FALSE);
1761 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1762 STRCAT(buf, s);
1763 }
1764}
1765
1766/*
1767 * print line for :print or :list command
1768 */
1769 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001770msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 int c;
1773 int col = 0;
1774 int n_extra = 0;
1775 int c_extra = 0;
1776 char_u *p_extra = NULL; /* init to make SASC shut up */
1777 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001778 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 char_u *trail = NULL;
1780#ifdef FEAT_MBYTE
1781 int l;
1782 char_u buf[MB_MAXBYTES + 1];
1783#endif
1784
Bram Moolenaardf177f62005-02-22 08:39:57 +00001785 if (curwin->w_p_list)
1786 list = TRUE;
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001789 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
1791 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001792 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 --trail;
1794 }
1795
1796 /* output a space for an empty line, otherwise the line will be
1797 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001798 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 msg_putchar(' ');
1800
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001801 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001803 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
1805 --n_extra;
1806 if (c_extra)
1807 c = c_extra;
1808 else
1809 c = *p_extra++;
1810 }
1811#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001812 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
1814 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001815 if (lcs_nbsp != NUL && list
1816 && (mb_ptr2char(s) == 160
1817 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001818 {
1819 mb_char2bytes(lcs_nbsp, buf);
1820 buf[(*mb_ptr2len)(buf)] = NUL;
1821 }
1822 else
1823 {
1824 mch_memmove(buf, s, (size_t)l);
1825 buf[l] = NUL;
1826 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001827 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 s += l;
1829 continue;
1830 }
1831#endif
1832 else
1833 {
1834 attr = 0;
1835 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001836 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
1838 /* tab amount depends on current column */
1839 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001840 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
1842 c = ' ';
1843 c_extra = ' ';
1844 }
1845 else
1846 {
1847 c = lcs_tab1;
1848 c_extra = lcs_tab2;
1849 attr = hl_attr(HLF_8);
1850 }
1851 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001852 else if (c == 160 && list && lcs_nbsp != NUL)
1853 {
1854 c = lcs_nbsp;
1855 attr = hl_attr(HLF_8);
1856 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001857 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
1859 p_extra = (char_u *)"";
1860 c_extra = NUL;
1861 n_extra = 1;
1862 c = lcs_eol;
1863 attr = hl_attr(HLF_AT);
1864 --s;
1865 }
1866 else if (c != NUL && (n = byte2cells(c)) > 1)
1867 {
1868 n_extra = n - 1;
1869 p_extra = transchar_byte(c);
1870 c_extra = NUL;
1871 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001872 /* Use special coloring to be able to distinguish <hex> from
1873 * the same in plain text. */
1874 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else if (c == ' ' && trail != NULL && s > trail)
1877 {
1878 c = lcs_trail;
1879 attr = hl_attr(HLF_8);
1880 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001881 else if (c == ' ' && list && lcs_space != NUL)
1882 {
1883 c = lcs_space;
1884 attr = hl_attr(HLF_8);
1885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887
1888 if (c == NUL)
1889 break;
1890
1891 msg_putchar_attr(c, attr);
1892 col++;
1893 }
1894 msg_clr_eos();
1895}
1896
1897#ifdef FEAT_MBYTE
1898/*
1899 * Use screen_puts() to output one multi-byte character.
1900 * Return the pointer "s" advanced to the next character.
1901 */
1902 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001903screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
1905 int cw;
1906
1907 msg_didout = TRUE; /* remember that line is not empty */
1908 cw = (*mb_ptr2cells)(s);
1909 if (cw > 1 && (
1910#ifdef FEAT_RIGHTLEFT
1911 cmdmsg_rl ? msg_col <= 1 :
1912#endif
1913 msg_col == Columns - 1))
1914 {
1915 /* Doesn't fit, print a highlighted '>' to fill it up. */
1916 msg_screen_putchar('>', hl_attr(HLF_AT));
1917 return s;
1918 }
1919
1920 screen_puts_len(s, l, msg_row, msg_col, attr);
1921#ifdef FEAT_RIGHTLEFT
1922 if (cmdmsg_rl)
1923 {
1924 msg_col -= cw;
1925 if (msg_col == 0)
1926 {
1927 msg_col = Columns;
1928 ++msg_row;
1929 }
1930 }
1931 else
1932#endif
1933 {
1934 msg_col += cw;
1935 if (msg_col >= Columns)
1936 {
1937 msg_col = 0;
1938 ++msg_row;
1939 }
1940 }
1941 return s + l;
1942}
1943#endif
1944
1945/*
1946 * Output a string to the screen at position msg_row, msg_col.
1947 * Update msg_row and msg_col for the next message.
1948 */
1949 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001950msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001952 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953}
1954
1955 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001956msg_puts_title(
1957 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 msg_puts_attr(s, hl_attr(HLF_T));
1960}
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962/*
1963 * Show a message in such a way that it always fits in the line. Cut out a
1964 * part in the middle and replace it with "..." when necessary.
1965 * Does not handle multi-byte characters!
1966 */
1967 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001968msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001970 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971}
1972
1973 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001974msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 int slen = len;
1977 int room;
1978
1979 room = Columns - msg_col;
1980 if (len > room && room >= 20)
1981 {
1982 slen = (room - 3) / 2;
1983 msg_outtrans_len_attr(longstr, slen, attr);
1984 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1985 }
1986 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1987}
1988
1989/*
1990 * Basic function for writing a message with highlight attributes.
1991 */
1992 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001993msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
1995 msg_puts_attr_len(s, -1, attr);
1996}
1997
1998/*
1999 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2000 * When "maxlen" is -1 there is no maximum length.
2001 * When "maxlen" is >= 0 the message is not put in the history.
2002 */
2003 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002004msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 /*
2007 * If redirection is on, also write to the redirection file.
2008 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002009 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
2011 /*
2012 * Don't print anything when using ":silent cmd".
2013 */
2014 if (msg_silent != 0)
2015 return;
2016
2017 /* if MSG_HIST flag set, add message to history */
2018 if ((attr & MSG_HIST) && maxlen < 0)
2019 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002020 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 attr &= ~MSG_HIST;
2022 }
2023
2024 /*
2025 * When writing something to the screen after it has scrolled, requires a
2026 * wait-return prompt later. Needed when scrolling, resetting
2027 * need_wait_return after some prompt, and then outputting something
2028 * without scrolling
2029 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002030 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 need_wait_return = TRUE;
2032 msg_didany = TRUE; /* remember that something was outputted */
2033
2034 /*
2035 * If there is no valid screen, use fprintf so we can see error messages.
2036 * If termcap is not active, we may be writing in an alternate console
2037 * window, cursor positioning may not work correctly (window size may be
2038 * different, e.g. for Win32 console) or we just don't know where the
2039 * cursor is.
2040 */
2041 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002042 msg_puts_printf(str, maxlen);
2043 else
2044 msg_puts_display(str, maxlen, attr, FALSE);
2045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002047/*
2048 * The display part of msg_puts_attr_len().
2049 * May be called recursively to display scroll-back text.
2050 */
2051 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002052msg_puts_display(
2053 char_u *str,
2054 int maxlen,
2055 int attr,
2056 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002057{
2058 char_u *s = str;
2059 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2060 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
2061#ifdef FEAT_MBYTE
2062 int l;
2063 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002065 char_u *sb_str = str;
2066 int sb_col = msg_col;
2067 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002068 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002071 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002074 * We are at the end of the screen line when:
2075 * - When outputting a newline.
2076 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002078 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#ifdef FEAT_RIGHTLEFT
2080 cmdmsg_rl
2081 ? (
2082 msg_col <= 1
2083 || (*s == TAB && msg_col <= 7)
2084# ifdef FEAT_MBYTE
2085 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
2086# endif
2087 )
2088 :
2089#endif
2090 (msg_col + t_col >= Columns - 1
2091 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
2092# ifdef FEAT_MBYTE
2093 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2094 && msg_col + t_col >= Columns - 2)
2095# endif
2096 ))))
2097 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002098 /*
2099 * The screen is scrolled up when at the last row (some terminals
2100 * scroll automatically, some don't. To avoid problems we scroll
2101 * ourselves).
2102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002105 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002107 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (msg_no_more && lines_left == 0)
2109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002111 /* Scroll the screen up one line. */
2112 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114 msg_row = Rows - 2;
2115 if (msg_col >= Columns) /* can happen after screen resize */
2116 msg_col = Columns - 1;
2117
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002118 /* Display char in last column before showing more-prompt. */
2119 if (*s >= ' '
2120#ifdef FEAT_RIGHTLEFT
2121 && !cmdmsg_rl
2122#endif
2123 )
2124 {
2125#ifdef FEAT_MBYTE
2126 if (has_mbyte)
2127 {
2128 if (enc_utf8 && maxlen >= 0)
2129 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002130 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002131 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002132 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002133 s = screen_puts_mbyte(s, l, attr);
2134 }
2135 else
2136#endif
2137 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002138 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002139 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002140 else
2141 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002142
2143 if (p_more)
2144 /* store text for scrolling back */
2145 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2146
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002147 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (must_redraw < VALID)
2150 must_redraw = VALID;
2151 redraw_cmdline = TRUE;
2152 if (cmdline_row > 0 && !exmode_active)
2153 --cmdline_row;
2154
2155 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002156 * If screen is completely filled and 'more' is set then wait
2157 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002159 if (lines_left > 0)
2160 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002161 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 && !msg_no_more && !exmode_active)
2163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002165 if (do_more_prompt(NUL))
2166 s = confirm_msg_tail;
2167#else
2168 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#endif
2170 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002171 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002173
2174 /* When we displayed a char in last column need to check if there
2175 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002176 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002177 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002180 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 || msg_col + t_col >= Columns
2182#ifdef FEAT_MBYTE
2183 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2184 && msg_col + t_col >= Columns - 1)
2185#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002186 ;
2187 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2188 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002190 t_puts(&t_col, t_s, s, attr);
2191
2192 if (wrap && p_more && !recurse)
2193 /* store text for scrolling back */
2194 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 if (*s == '\n') /* go to next line */
2197 {
2198 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002199#ifdef FEAT_RIGHTLEFT
2200 if (cmdmsg_rl)
2201 msg_col = Columns - 1;
2202 else
2203#endif
2204 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (++msg_row >= Rows) /* safety check */
2206 msg_row = Rows - 1;
2207 }
2208 else if (*s == '\r') /* go to column 0 */
2209 {
2210 msg_col = 0;
2211 }
2212 else if (*s == '\b') /* go to previous char */
2213 {
2214 if (msg_col)
2215 --msg_col;
2216 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002217 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 do
2220 msg_screen_putchar(' ', attr);
2221 while (msg_col & 7);
2222 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002223 else if (*s == BELL) /* beep (from ":sh") */
2224 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 else
2226 {
2227#ifdef FEAT_MBYTE
2228 if (has_mbyte)
2229 {
2230 cw = (*mb_ptr2cells)(s);
2231 if (enc_utf8 && maxlen >= 0)
2232 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002233 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002235 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237 else
2238 {
2239 cw = 1;
2240 l = 1;
2241 }
2242#endif
2243 /* When drawing from right to left or when a double-wide character
2244 * doesn't fit, draw a single character here. Otherwise collect
2245 * characters and draw them all at once later. */
2246#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2247 if (
2248# ifdef FEAT_RIGHTLEFT
2249 cmdmsg_rl
2250# ifdef FEAT_MBYTE
2251 ||
2252# endif
2253# endif
2254# ifdef FEAT_MBYTE
2255 (cw > 1 && msg_col + t_col >= Columns - 1)
2256# endif
2257 )
2258 {
2259# ifdef FEAT_MBYTE
2260 if (l > 1)
2261 s = screen_puts_mbyte(s, l, attr) - 1;
2262 else
2263# endif
2264 msg_screen_putchar(*s, attr);
2265 }
2266 else
2267#endif
2268 {
2269 /* postpone this character until later */
2270 if (t_col == 0)
2271 t_s = s;
2272#ifdef FEAT_MBYTE
2273 t_col += cw;
2274 s += l - 1;
2275#else
2276 ++t_col;
2277#endif
2278 }
2279 }
2280 ++s;
2281 }
2282
2283 /* output any postponed text */
2284 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002285 t_puts(&t_col, t_s, s, attr);
2286 if (p_more && !recurse)
2287 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 msg_check();
2290}
2291
2292/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002293 * Return TRUE when ":filter pattern" was used and "msg" does not match
2294 * "pattern".
2295 */
2296 int
2297message_filtered(char_u *msg)
2298{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002299 int match;
2300
2301 if (cmdmod.filter_regmatch.regprog == NULL)
2302 return FALSE;
2303 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2304 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002305}
2306
2307/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002308 * Scroll the screen up one line for displaying the next message line.
2309 */
2310 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002311msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002312{
2313#ifdef FEAT_GUI
2314 /* Remove the cursor before scrolling, ScreenLines[] is going
2315 * to become invalid. */
2316 if (gui.in_use)
2317 gui_undraw_cursor();
2318#endif
2319 /* scrolling up always works */
2320 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2321
2322 if (!can_clear((char_u *)" "))
2323 {
2324 /* Scrolling up doesn't result in the right background. Set the
2325 * background here. It's not efficient, but avoids that we have to do
2326 * it all over the code. */
2327 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2328
2329 /* Also clear the last char of the last but one line if it was not
2330 * cleared before to avoid a scroll-up. */
2331 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2332 screen_fill((int)Rows - 2, (int)Rows - 1,
2333 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2334 }
2335}
2336
2337/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002338 * Increment "msg_scrolled".
2339 */
2340 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002341inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002342{
2343#ifdef FEAT_EVAL
2344 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2345 {
2346 char_u *p = sourcing_name;
2347 char_u *tofree = NULL;
2348 int len;
2349
2350 /* v:scrollstart is empty, set it to the script/function name and line
2351 * number */
2352 if (p == NULL)
2353 p = (char_u *)_("Unknown");
2354 else
2355 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002356 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002357 tofree = alloc(len);
2358 if (tofree != NULL)
2359 {
2360 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2361 p, (long)sourcing_lnum);
2362 p = tofree;
2363 }
2364 }
2365 set_vim_var_string(VV_SCROLLSTART, p, -1);
2366 vim_free(tofree);
2367 }
2368#endif
2369 ++msg_scrolled;
2370}
2371
2372/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002373 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2374 * store the displayed text and remember where screen lines start.
2375 */
2376typedef struct msgchunk_S msgchunk_T;
2377struct msgchunk_S
2378{
2379 msgchunk_T *sb_next;
2380 msgchunk_T *sb_prev;
2381 char sb_eol; /* TRUE when line ends after this text */
2382 int sb_msg_col; /* column in which text starts */
2383 int sb_attr; /* text attributes */
2384 char_u sb_text[1]; /* text to be displayed, actually longer */
2385};
2386
2387static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2388
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002389static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2390static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002391
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002392static int do_clear_sb_text = FALSE; /* clear text on next msg */
2393
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002394/*
2395 * Store part of a printed message for displaying when scrolling back.
2396 */
2397 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002398store_sb_text(
2399 char_u **sb_str, /* start of string */
2400 char_u *s, /* just after string */
2401 int attr,
2402 int *sb_col,
2403 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002404{
2405 msgchunk_T *mp;
2406
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002407 if (do_clear_sb_text)
2408 {
2409 clear_sb_text();
2410 do_clear_sb_text = FALSE;
2411 }
2412
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002413 if (s > *sb_str)
2414 {
2415 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2416 if (mp != NULL)
2417 {
2418 mp->sb_eol = finish;
2419 mp->sb_msg_col = *sb_col;
2420 mp->sb_attr = attr;
2421 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2422
2423 if (last_msgchunk == NULL)
2424 {
2425 last_msgchunk = mp;
2426 mp->sb_prev = NULL;
2427 }
2428 else
2429 {
2430 mp->sb_prev = last_msgchunk;
2431 last_msgchunk->sb_next = mp;
2432 last_msgchunk = mp;
2433 }
2434 mp->sb_next = NULL;
2435 }
2436 }
2437 else if (finish && last_msgchunk != NULL)
2438 last_msgchunk->sb_eol = TRUE;
2439
2440 *sb_str = s;
2441 *sb_col = 0;
2442}
2443
2444/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002445 * Finished showing messages, clear the scroll-back text on the next message.
2446 */
2447 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002448may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002449{
2450 do_clear_sb_text = TRUE;
2451}
2452
2453/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002454 * Clear any text remembered for scrolling back.
2455 * Called when redrawing the screen.
2456 */
2457 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002458clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002459{
2460 msgchunk_T *mp;
2461
2462 while (last_msgchunk != NULL)
2463 {
2464 mp = last_msgchunk->sb_prev;
2465 vim_free(last_msgchunk);
2466 last_msgchunk = mp;
2467 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002468}
2469
2470/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002471 * "g<" command.
2472 */
2473 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002474show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002475{
2476 msgchunk_T *mp;
2477
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002478 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002479 * weird, typing a command without output results in one line. */
2480 mp = msg_sb_start(last_msgchunk);
2481 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002482 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002483 else
2484 {
2485 do_more_prompt('G');
2486 wait_return(FALSE);
2487 }
2488}
2489
2490/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002491 * Move to the start of screen line in already displayed text.
2492 */
2493 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002494msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002495{
2496 msgchunk_T *mp = mps;
2497
2498 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2499 mp = mp->sb_prev;
2500 return mp;
2501}
2502
2503/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002504 * Mark the last message chunk as finishing the line.
2505 */
2506 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002507msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002508{
2509 if (last_msgchunk != NULL)
2510 last_msgchunk->sb_eol = TRUE;
2511}
2512
2513/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002514 * Display a screen line from previously displayed text at row "row".
2515 * Returns a pointer to the text for the next line (can be NULL).
2516 */
2517 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002518disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519{
2520 msgchunk_T *mp = smp;
2521 char_u *p;
2522
2523 for (;;)
2524 {
2525 msg_row = row;
2526 msg_col = mp->sb_msg_col;
2527 p = mp->sb_text;
2528 if (*p == '\n') /* don't display the line break */
2529 ++p;
2530 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2531 if (mp->sb_eol || mp->sb_next == NULL)
2532 break;
2533 mp = mp->sb_next;
2534 }
2535 return mp->sb_next;
2536}
2537
2538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * Output any postponed text for msg_puts_attr_len().
2540 */
2541 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002542t_puts(
2543 int *t_col,
2544 char_u *t_s,
2545 char_u *s,
2546 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 /* output postponed text */
2549 msg_didout = TRUE; /* remember that line is not empty */
2550 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002551 msg_col += *t_col;
2552 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#ifdef FEAT_MBYTE
2554 /* If the string starts with a composing character don't increment the
2555 * column position for it. */
2556 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2557 --msg_col;
2558#endif
2559 if (msg_col >= Columns)
2560 {
2561 msg_col = 0;
2562 ++msg_row;
2563 }
2564}
2565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566/*
2567 * Returns TRUE when messages should be printed with mch_errmsg().
2568 * This is used when there is no valid screen, so we can see error messages.
2569 * If termcap is not active, we may be writing in an alternate console
2570 * window, cursor positioning may not work correctly (window size may be
2571 * different, e.g. for Win32 console) or we just don't know where the
2572 * cursor is.
2573 */
2574 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002575msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
2577 return (!msg_check_screen()
2578#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2579 || !termcap_active
2580#endif
2581 || (swapping_screen() && !termcap_active)
2582 );
2583}
2584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002585/*
2586 * Print a message when there is no valid screen.
2587 */
2588 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002589msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002590{
2591 char_u *s = str;
2592 char_u buf[4];
2593 char_u *p;
2594
2595#ifdef WIN3264
2596 if (!(silent_mode && p_verbose == 0))
2597 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2598#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002599 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002600 {
2601 if (!(silent_mode && p_verbose == 0))
2602 {
2603 /* NL --> CR NL translation (for Unix, not for "--version") */
2604 /* NL --> CR translation (for Mac) */
2605 p = &buf[0];
2606 if (*s == '\n' && !info_message)
2607 *p++ = '\r';
2608#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2609 else
2610#endif
2611 *p++ = *s;
2612 *p = '\0';
2613 if (info_message) /* informative message, not an error */
2614 mch_msg((char *)buf);
2615 else
2616 mch_errmsg((char *)buf);
2617 }
2618
2619 /* primitive way to compute the current column */
2620#ifdef FEAT_RIGHTLEFT
2621 if (cmdmsg_rl)
2622 {
2623 if (*s == '\r' || *s == '\n')
2624 msg_col = Columns - 1;
2625 else
2626 --msg_col;
2627 }
2628 else
2629#endif
2630 {
2631 if (*s == '\r' || *s == '\n')
2632 msg_col = 0;
2633 else
2634 ++msg_col;
2635 }
2636 ++s;
2637 }
2638 msg_didout = TRUE; /* assume that line is not empty */
2639
2640#ifdef WIN3264
2641 if (!(silent_mode && p_verbose == 0))
2642 mch_settmode(TMODE_RAW);
2643#endif
2644}
2645
2646/*
2647 * Show the more-prompt and handle the user response.
2648 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002649 * When at hit-enter prompt "typed_char" is the already typed character,
2650 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002651 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2652 */
2653 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002654do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002655{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002656 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002657 int used_typed_char = typed_char;
2658 int oldState = State;
2659 int c;
2660#ifdef FEAT_CON_DIALOG
2661 int retval = FALSE;
2662#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002663 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002664 msgchunk_T *mp_last = NULL;
2665 msgchunk_T *mp;
2666 int i;
2667
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002668 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002669 * that case don't show another prompt. Also when at the hit-Enter prompt
2670 * and nothing was typed. */
2671 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002672 return FALSE;
2673 entered = TRUE;
2674
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002675 if (typed_char == 'G')
2676 {
2677 /* "g<": Find first line on the last page. */
2678 mp_last = msg_sb_start(last_msgchunk);
2679 for (i = 0; i < Rows - 2 && mp_last != NULL
2680 && mp_last->sb_prev != NULL; ++i)
2681 mp_last = msg_sb_start(mp_last->sb_prev);
2682 }
2683
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684 State = ASKMORE;
2685#ifdef FEAT_MOUSE
2686 setmouse();
2687#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002688 if (typed_char == NUL)
2689 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690 for (;;)
2691 {
2692 /*
2693 * Get a typed character directly from the user.
2694 */
2695 if (used_typed_char != NUL)
2696 {
2697 c = used_typed_char; /* was typed at hit-enter prompt */
2698 used_typed_char = NUL;
2699 }
2700 else
2701 c = get_keystroke();
2702
2703#if defined(FEAT_MENU) && defined(FEAT_GUI)
2704 if (c == K_MENU)
2705 {
2706 int idx = get_menu_index(current_menu, ASKMORE);
2707
2708 /* Used a menu. If it starts with CTRL-Y, it must
2709 * be a "Copy" for the clipboard. Otherwise
2710 * assume that we end */
2711 if (idx == MENU_INDEX_INVALID)
2712 continue;
2713 c = *current_menu->strings[idx];
2714 if (c != NUL && current_menu->strings[idx][1] != NUL)
2715 ins_typebuf(current_menu->strings[idx] + 1,
2716 current_menu->noremap[idx], 0, TRUE,
2717 current_menu->silent[idx]);
2718 }
2719#endif
2720
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002721 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 switch (c)
2723 {
2724 case BS: /* scroll one line back */
2725 case K_BS:
2726 case 'k':
2727 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002728 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729 break;
2730
2731 case CAR: /* one extra line */
2732 case NL:
2733 case 'j':
2734 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002735 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002736 break;
2737
2738 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002739 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 break;
2741
2742 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002743 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002744 break;
2745
2746 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002747 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002748 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002749 break;
2750
2751 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002752 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002753 case K_PAGEDOWN:
2754 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002755 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002756 break;
2757
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002758 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002759 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002760 break;
2761
2762 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002763 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002764 lines_left = 999999;
2765 break;
2766
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002767 case ':': /* start new command line */
2768#ifdef FEAT_CON_DIALOG
2769 if (!confirm_msg_used)
2770#endif
2771 {
2772 /* Since got_int is set all typeahead will be flushed, but we
2773 * want to keep this ':', remember that in a special way. */
2774 typeahead_noflush(':');
2775 cmdline_row = Rows - 1; /* put ':' on this line */
2776 skip_redraw = TRUE; /* skip redraw once */
2777 need_wait_return = FALSE; /* don't wait in main() */
2778 }
2779 /*FALLTHROUGH*/
2780 case 'q': /* quit */
2781 case Ctrl_C:
2782 case ESC:
2783#ifdef FEAT_CON_DIALOG
2784 if (confirm_msg_used)
2785 {
2786 /* Jump to the choices of the dialog. */
2787 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 }
2789 else
2790#endif
2791 {
2792 got_int = TRUE;
2793 quit_more = TRUE;
2794 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002795 /* When there is some more output (wrapping line) display that
2796 * without another prompt. */
2797 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002798 break;
2799
2800#ifdef FEAT_CLIPBOARD
2801 case Ctrl_Y:
2802 /* Strange way to allow copying (yanking) a modeless
2803 * selection at the more prompt. Use CTRL-Y,
2804 * because the same is used in Cmdline-mode and at the
2805 * hit-enter prompt. However, scrolling one line up
2806 * might be expected... */
2807 if (clip_star.state == SELECT_DONE)
2808 clip_copy_modeless_selection(TRUE);
2809 continue;
2810#endif
2811 default: /* no valid response */
2812 msg_moremsg(TRUE);
2813 continue;
2814 }
2815
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002816 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002818 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819 {
2820 /* go to start of last line */
2821 if (mp_last == NULL)
2822 mp = msg_sb_start(last_msgchunk);
2823 else if (mp_last->sb_prev != NULL)
2824 mp = msg_sb_start(mp_last->sb_prev);
2825 else
2826 mp = NULL;
2827
2828 /* go to start of line at top of the screen */
2829 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2830 ++i)
2831 mp = msg_sb_start(mp->sb_prev);
2832
2833 if (mp != NULL && mp->sb_prev != NULL)
2834 {
2835 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002836 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002837 {
2838 if (mp == NULL || mp->sb_prev == NULL)
2839 break;
2840 mp = msg_sb_start(mp->sb_prev);
2841 if (mp_last == NULL)
2842 mp_last = msg_sb_start(last_msgchunk);
2843 else
2844 mp_last = msg_sb_start(mp_last->sb_prev);
2845 }
2846
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002847 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002848 (int)Rows, NULL) == OK)
2849 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002850 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 (void)disp_sb_line(0, mp);
2852 }
2853 else
2854 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002855 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002856 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002857 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2858 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002859 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002860 ++msg_scrolled;
2861 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002862 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002863 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002864 }
2865 }
2866 else
2867 {
2868 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002869 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002870 {
2871 /* scroll up, display line at bottom */
2872 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002873 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002874 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2875 (int)Columns, ' ', ' ', 0);
2876 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002877 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 }
2879 }
2880
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002881 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882 {
2883 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002884 screen_fill((int)Rows - 1, (int)Rows, 0,
2885 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002886 msg_moremsg(FALSE);
2887 continue;
2888 }
2889
2890 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002891 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002892 }
2893
2894 break;
2895 }
2896
2897 /* clear the --more-- message */
2898 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2899 State = oldState;
2900#ifdef FEAT_MOUSE
2901 setmouse();
2902#endif
2903 if (quit_more)
2904 {
2905 msg_row = Rows - 1;
2906 msg_col = 0;
2907 }
2908#ifdef FEAT_RIGHTLEFT
2909 else if (cmdmsg_rl)
2910 msg_col = Columns - 1;
2911#endif
2912
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002913 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002914#ifdef FEAT_CON_DIALOG
2915 return retval;
2916#else
2917 return FALSE;
2918#endif
2919}
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2922
2923#ifdef mch_errmsg
2924# undef mch_errmsg
2925#endif
2926#ifdef mch_msg
2927# undef mch_msg
2928#endif
2929
2930/*
2931 * Give an error message. To be used when the screen hasn't been initialized
2932 * yet. When stderr can't be used, collect error messages until the GUI has
2933 * started and they can be displayed in a message box.
2934 */
2935 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002936mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
2938 int len;
2939
2940#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2941 /* On Unix use stderr if it's a tty.
2942 * When not going to start the GUI also use stderr.
2943 * On Mac, when started from Finder, stderr is the console. */
2944 if (
2945# ifdef UNIX
2946# ifdef MACOS_X_UNIX
2947 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2948# else
2949 isatty(2)
2950# endif
2951# ifdef FEAT_GUI
2952 ||
2953# endif
2954# endif
2955# ifdef FEAT_GUI
2956 !(gui.in_use || gui.starting)
2957# endif
2958 )
2959 {
2960 fprintf(stderr, "%s", str);
2961 return;
2962 }
2963#endif
2964
2965 /* avoid a delay for a message that isn't there */
2966 emsg_on_display = FALSE;
2967
2968 len = (int)STRLEN(str) + 1;
2969 if (error_ga.ga_growsize == 0)
2970 {
2971 error_ga.ga_growsize = 80;
2972 error_ga.ga_itemsize = 1;
2973 }
2974 if (ga_grow(&error_ga, len) == OK)
2975 {
2976 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2977 (char_u *)str, len);
2978#ifdef UNIX
2979 /* remove CR characters, they are displayed */
2980 {
2981 char_u *p;
2982
2983 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2984 for (;;)
2985 {
2986 p = vim_strchr(p, '\r');
2987 if (p == NULL)
2988 break;
2989 *p = ' ';
2990 }
2991 }
2992#endif
2993 --len; /* don't count the NUL at the end */
2994 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996}
2997
2998/*
2999 * Give a message. To be used when the screen hasn't been initialized yet.
3000 * When there is no tty, collect messages until the GUI has started and they
3001 * can be displayed in a message box.
3002 */
3003 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003004mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
3007 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
3008 * uses mch_errmsg() when started from the desktop.
3009 * When not going to start the GUI also use stdout.
3010 * On Mac, when started from Finder, stderr is the console. */
3011 if (
3012# ifdef UNIX
3013# ifdef MACOS_X_UNIX
3014 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
3015# else
3016 isatty(2)
3017# endif
3018# ifdef FEAT_GUI
3019 ||
3020# endif
3021# endif
3022# ifdef FEAT_GUI
3023 !(gui.in_use || gui.starting)
3024# endif
3025 )
3026 {
3027 printf("%s", str);
3028 return;
3029 }
3030# endif
3031 mch_errmsg(str);
3032}
3033#endif /* USE_MCH_ERRMSG */
3034
3035/*
3036 * Put a character on the screen at the current message position and advance
3037 * to the next position. Only for printable ASCII!
3038 */
3039 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003040msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 msg_didout = TRUE; /* remember that line is not empty */
3043 screen_putchar(c, msg_row, msg_col, attr);
3044#ifdef FEAT_RIGHTLEFT
3045 if (cmdmsg_rl)
3046 {
3047 if (--msg_col == 0)
3048 {
3049 msg_col = Columns;
3050 ++msg_row;
3051 }
3052 }
3053 else
3054#endif
3055 {
3056 if (++msg_col >= Columns)
3057 {
3058 msg_col = 0;
3059 ++msg_row;
3060 }
3061 }
3062}
3063
3064 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003065msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003067 int attr;
3068 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003071 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003073 screen_puts((char_u *)
3074 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3075 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077
3078/*
3079 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3080 * exmode_active.
3081 */
3082 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003083repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
3085 if (State == ASKMORE)
3086 {
3087 msg_moremsg(TRUE); /* display --more-- message again */
3088 msg_row = Rows - 1;
3089 }
3090#ifdef FEAT_CON_DIALOG
3091 else if (State == CONFIRM)
3092 {
3093 display_confirm_msg(); /* display ":confirm" message again */
3094 msg_row = Rows - 1;
3095 }
3096#endif
3097 else if (State == EXTERNCMD)
3098 {
3099 windgoto(msg_row, msg_col); /* put cursor back */
3100 }
3101 else if (State == HITRETURN || State == SETWSIZE)
3102 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003103 if (msg_row == Rows - 1)
3104 {
3105 /* Avoid drawing the "hit-enter" prompt below the previous one,
3106 * overwrite it. Esp. useful when regaining focus and a
3107 * FocusGained autocmd exists but didn't draw anything. */
3108 msg_didout = FALSE;
3109 msg_col = 0;
3110 msg_clr_eos();
3111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 hit_return_msg();
3113 msg_row = Rows - 1;
3114 }
3115}
3116
3117/*
3118 * msg_check_screen - check if the screen is initialized.
3119 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3120 * While starting the GUI the terminal codes will be set for the GUI, but the
3121 * output goes to the terminal. Don't use the terminal codes then.
3122 */
3123 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003124msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126 if (!full_screen || !screen_valid(FALSE))
3127 return FALSE;
3128
3129 if (msg_row >= Rows)
3130 msg_row = Rows - 1;
3131 if (msg_col >= Columns)
3132 msg_col = Columns - 1;
3133 return TRUE;
3134}
3135
3136/*
3137 * Clear from current message position to end of screen.
3138 * Skip this when ":silent" was used, no need to clear for redirection.
3139 */
3140 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003141msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 if (msg_silent == 0)
3144 msg_clr_eos_force();
3145}
3146
3147/*
3148 * Clear from current message position to end of screen.
3149 * Note: msg_col is not updated, so we remember the end of the message
3150 * for msg_check().
3151 */
3152 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003153msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 if (msg_use_printf())
3156 {
3157 if (full_screen) /* only when termcap codes are valid */
3158 {
3159 if (*T_CD)
3160 out_str(T_CD); /* clear to end of display */
3161 else if (*T_CE)
3162 out_str(T_CE); /* clear to end of line */
3163 }
3164 }
3165 else
3166 {
3167#ifdef FEAT_RIGHTLEFT
3168 if (cmdmsg_rl)
3169 {
3170 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3171 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3172 }
3173 else
3174#endif
3175 {
3176 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3177 ' ', ' ', 0);
3178 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3179 }
3180 }
3181}
3182
3183/*
3184 * Clear the command line.
3185 */
3186 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003187msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 msg_row = cmdline_row;
3190 msg_col = 0;
3191 msg_clr_eos_force();
3192}
3193
3194/*
3195 * end putting a message on the screen
3196 * call wait_return if the message does not fit in the available space
3197 * return TRUE if wait_return not called.
3198 */
3199 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003200msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003203 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 * or the ruler option is set and we run into it,
3205 * we have to redraw the window.
3206 * Do not do this if we are abandoning the file or editing the command line.
3207 */
3208 if (!exiting && need_wait_return && !(State & CMDLINE))
3209 {
3210 wait_return(FALSE);
3211 return FALSE;
3212 }
3213 out_flush();
3214 return TRUE;
3215}
3216
3217/*
3218 * If the written message runs into the shown command or ruler, we have to
3219 * wait for hit-return and redraw the window later.
3220 */
3221 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 if (msg_row == Rows - 1 && msg_col >= sc_col)
3225 {
3226 need_wait_return = TRUE;
3227 redraw_cmdline = TRUE;
3228 }
3229}
3230
3231/*
3232 * May write a string to the redirection file.
3233 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3234 */
3235 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003236redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 char_u *s = str;
3239 static int cur_col = 0;
3240
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003241 /* Don't do anything for displaying prompts and the like. */
3242 if (redir_off)
3243 return;
3244
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003245 /* If 'verbosefile' is set prepare for writing in that file. */
3246 if (*p_vfile != NUL && verbose_fd == NULL)
3247 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003248
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003249 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
3251 /* If the string doesn't start with CR or NL, go to msg_col */
3252 if (*s != '\n' && *s != '\r')
3253 {
3254 while (cur_col < msg_col)
3255 {
3256#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003257 if (redir_execute)
3258 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003259 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003261 else if (redir_vname)
3262 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003263 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003265 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003267 if (verbose_fd != NULL)
3268 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 ++cur_col;
3270 }
3271 }
3272
3273#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003274 if (redir_execute)
3275 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003276 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003278 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003279 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
3281
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003282 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3284 {
3285#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003286 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003288 if (redir_fd != NULL)
3289 putc(*s, redir_fd);
3290 if (verbose_fd != NULL)
3291 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (*s == '\r' || *s == '\n')
3293 cur_col = 0;
3294 else if (*s == '\t')
3295 cur_col += (8 - cur_col % 8);
3296 else
3297 ++cur_col;
3298 ++s;
3299 }
3300
3301 if (msg_silent != 0) /* should update msg_col */
3302 msg_col = cur_col;
3303 }
3304}
3305
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003306 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003307redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003308{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003309 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003310#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003311 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003312#endif
3313 ;
3314}
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003317 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003318 * Must always be called paired with verbose_leave()!
3319 */
3320 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003321verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003322{
3323 if (*p_vfile != NUL)
3324 ++msg_silent;
3325}
3326
3327/*
3328 * After giving verbose message.
3329 * Must always be called paired with verbose_enter()!
3330 */
3331 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003332verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003333{
3334 if (*p_vfile != NUL)
3335 if (--msg_silent < 0)
3336 msg_silent = 0;
3337}
3338
3339/*
3340 * Like verbose_enter() and set msg_scroll when displaying the message.
3341 */
3342 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003343verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003344{
3345 if (*p_vfile != NUL)
3346 ++msg_silent;
3347 else
3348 /* always scroll up, don't overwrite */
3349 msg_scroll = TRUE;
3350}
3351
3352/*
3353 * Like verbose_leave() and set cmdline_row when displaying the message.
3354 */
3355 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003356verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003357{
3358 if (*p_vfile != NUL)
3359 {
3360 if (--msg_silent < 0)
3361 msg_silent = 0;
3362 }
3363 else
3364 cmdline_row = msg_row;
3365}
3366
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003367/*
3368 * Called when 'verbosefile' is set: stop writing to the file.
3369 */
3370 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003371verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003372{
3373 if (verbose_fd != NULL)
3374 {
3375 fclose(verbose_fd);
3376 verbose_fd = NULL;
3377 }
3378 verbose_did_open = FALSE;
3379}
3380
3381/*
3382 * Open the file 'verbosefile'.
3383 * Return FAIL or OK.
3384 */
3385 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003386verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003387{
3388 if (verbose_fd == NULL && !verbose_did_open)
3389 {
3390 /* Only give the error message once. */
3391 verbose_did_open = TRUE;
3392
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003393 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003394 if (verbose_fd == NULL)
3395 {
3396 EMSG2(_(e_notopen), p_vfile);
3397 return FAIL;
3398 }
3399 }
3400 return OK;
3401}
3402
3403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 * Give a warning message (for searching).
3405 * Use 'w' highlighting and may repeat the message after redrawing
3406 */
3407 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003408give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
3410 /* Don't do this for ":silent". */
3411 if (msg_silent != 0)
3412 return;
3413
3414 /* Don't want a hit-enter prompt here. */
3415 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417#ifdef FEAT_EVAL
3418 set_vim_var_string(VV_WARNINGMSG, message, -1);
3419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 vim_free(keep_msg);
3421 keep_msg = NULL;
3422 if (hl)
3423 keep_msg_attr = hl_attr(HLF_W);
3424 else
3425 keep_msg_attr = 0;
3426 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003427 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 msg_didout = FALSE; /* overwrite this message */
3429 msg_nowait = TRUE; /* don't wait for this message */
3430 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 --no_wait_return;
3433}
3434
3435/*
3436 * Advance msg cursor to column "col".
3437 */
3438 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003439msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 if (msg_silent != 0) /* nothing to advance to */
3442 {
3443 msg_col = col; /* for redirection, may fill it up later */
3444 return;
3445 }
3446 if (col >= Columns) /* not enough room */
3447 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003448#ifdef FEAT_RIGHTLEFT
3449 if (cmdmsg_rl)
3450 while (msg_col > Columns - col)
3451 msg_putchar(' ');
3452 else
3453#endif
3454 while (msg_col < col)
3455 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456}
3457
3458#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3459/*
3460 * Used for "confirm()" function, and the :confirm command prefix.
3461 * Versions which haven't got flexible dialogs yet, and console
3462 * versions, get this generic handler which uses the command line.
3463 *
3464 * type = one of:
3465 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3466 * title = title string (can be NULL for default)
3467 * (neither used in console dialogs at the moment)
3468 *
3469 * Format of the "buttons" string:
3470 * "Button1Name\nButton2Name\nButton3Name"
3471 * The first button should normally be the default/accept
3472 * The second button should be the 'Cancel' button
3473 * Other buttons- use your imagination!
3474 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3475 * different letter.
3476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003478do_dialog(
3479 int type UNUSED,
3480 char_u *title UNUSED,
3481 char_u *message,
3482 char_u *buttons,
3483 int dfltbutton,
3484 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003485 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003486 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003487 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 int oldState;
3490 int retval = 0;
3491 char_u *hotkeys;
3492 int c;
3493 int i;
3494
3495#ifndef NO_CONSOLE
3496 /* Don't output anything in silent mode ("ex -s") */
3497 if (silent_mode)
3498 return dfltbutton; /* return default option */
3499#endif
3500
3501#ifdef FEAT_GUI_DIALOG
3502 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3503 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3504 {
3505 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003506 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003507 /* avoid a hit-enter prompt without clearing the cmdline */
3508 need_wait_return = FALSE;
3509 emsg_on_display = FALSE;
3510 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 /* Flush output to avoid that further messages and redrawing is done
3513 * in the wrong order. */
3514 out_flush();
3515 gui_mch_update();
3516
3517 return c;
3518 }
3519#endif
3520
3521 oldState = State;
3522 State = CONFIRM;
3523#ifdef FEAT_MOUSE
3524 setmouse();
3525#endif
3526
3527 /*
3528 * Since we wait for a keypress, don't make the
3529 * user press RETURN as well afterwards.
3530 */
3531 ++no_wait_return;
3532 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3533
3534 if (hotkeys != NULL)
3535 {
3536 for (;;)
3537 {
3538 /* Get a typed character directly from the user. */
3539 c = get_keystroke();
3540 switch (c)
3541 {
3542 case CAR: /* User accepts default option */
3543 case NL:
3544 retval = dfltbutton;
3545 break;
3546 case Ctrl_C: /* User aborts/cancels */
3547 case ESC:
3548 retval = 0;
3549 break;
3550 default: /* Could be a hotkey? */
3551 if (c < 0) /* special keys are ignored here */
3552 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003553 if (c == ':' && ex_cmd)
3554 {
3555 retval = dfltbutton;
3556 ins_char_typebuf(':');
3557 break;
3558 }
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 /* Make the character lowercase, as chars in "hotkeys" are. */
3561 c = MB_TOLOWER(c);
3562 retval = 1;
3563 for (i = 0; hotkeys[i]; ++i)
3564 {
3565#ifdef FEAT_MBYTE
3566 if (has_mbyte)
3567 {
3568 if ((*mb_ptr2char)(hotkeys + i) == c)
3569 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003570 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 else
3573#endif
3574 if (hotkeys[i] == c)
3575 break;
3576 ++retval;
3577 }
3578 if (hotkeys[i])
3579 break;
3580 /* No hotkey match, so keep waiting */
3581 continue;
3582 }
3583 break;
3584 }
3585
3586 vim_free(hotkeys);
3587 }
3588
3589 State = oldState;
3590#ifdef FEAT_MOUSE
3591 setmouse();
3592#endif
3593 --no_wait_return;
3594 msg_end_prompt();
3595
3596 return retval;
3597}
3598
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003599static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601/*
3602 * Copy one character from "*from" to "*to", taking care of multi-byte
3603 * characters. Return the length of the character in bytes.
3604 */
3605 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003606copy_char(
3607 char_u *from,
3608 char_u *to,
3609 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611#ifdef FEAT_MBYTE
3612 int len;
3613 int c;
3614
3615 if (has_mbyte)
3616 {
3617 if (lowercase)
3618 {
3619 c = MB_TOLOWER((*mb_ptr2char)(from));
3620 return (*mb_char2bytes)(c, to);
3621 }
3622 else
3623 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003624 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 mch_memmove(to, from, (size_t)len);
3626 return len;
3627 }
3628 }
3629 else
3630#endif
3631 {
3632 if (lowercase)
3633 *to = (char_u)TOLOWER_LOC(*from);
3634 else
3635 *to = *from;
3636 return 1;
3637 }
3638}
3639
3640/*
3641 * Format the dialog string, and display it at the bottom of
3642 * the screen. Return a string of hotkey chars (if defined) for
3643 * each 'button'. If a button has no hotkey defined, the first character of
3644 * the button is used.
3645 * The hotkeys can be multi-byte characters, but without combining chars.
3646 *
3647 * Returns an allocated string with hotkeys, or NULL for error.
3648 */
3649 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003650msg_show_console_dialog(
3651 char_u *message,
3652 char_u *buttons,
3653 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654{
3655 int len = 0;
3656#ifdef FEAT_MBYTE
3657# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3658#else
3659# define HOTK_LEN 1
3660#endif
3661 int lenhotkey = HOTK_LEN; /* count first button */
3662 char_u *hotk = NULL;
3663 char_u *msgp = NULL;
3664 char_u *hotkp = NULL;
3665 char_u *r;
3666 int copy;
3667#define HAS_HOTKEY_LEN 30
3668 char_u has_hotkey[HAS_HOTKEY_LEN];
3669 int first_hotkey = FALSE; /* first char of button is hotkey */
3670 int idx;
3671
3672 has_hotkey[0] = FALSE;
3673
3674 /*
3675 * First loop: compute the size of memory to allocate.
3676 * Second loop: copy to the allocated memory.
3677 */
3678 for (copy = 0; copy <= 1; ++copy)
3679 {
3680 r = buttons;
3681 idx = 0;
3682 while (*r)
3683 {
3684 if (*r == DLG_BUTTON_SEP)
3685 {
3686 if (copy)
3687 {
3688 *msgp++ = ',';
3689 *msgp++ = ' '; /* '\n' -> ', ' */
3690
3691 /* advance to next hotkey and set default hotkey */
3692#ifdef FEAT_MBYTE
3693 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003694 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 else
3696#endif
3697 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003698 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 if (dfltbutton)
3700 --dfltbutton;
3701
3702 /* If no hotkey is specified first char is used. */
3703 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3704 first_hotkey = TRUE;
3705 }
3706 else
3707 {
3708 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3709 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3710 if (idx < HAS_HOTKEY_LEN - 1)
3711 has_hotkey[++idx] = FALSE;
3712 }
3713 }
3714 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3715 {
3716 if (*r == DLG_HOTKEY_CHAR)
3717 ++r;
3718 first_hotkey = FALSE;
3719 if (copy)
3720 {
3721 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3722 *msgp++ = *r;
3723 else
3724 {
3725 /* '&a' -> '[a]' */
3726 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3727 msgp += copy_char(r, msgp, FALSE);
3728 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3729
3730 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003731 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733 }
3734 else
3735 {
3736 ++len; /* '&a' -> '[a]' */
3737 if (idx < HAS_HOTKEY_LEN - 1)
3738 has_hotkey[idx] = TRUE;
3739 }
3740 }
3741 else
3742 {
3743 /* everything else copy literally */
3744 if (copy)
3745 msgp += copy_char(r, msgp, FALSE);
3746 }
3747
3748 /* advance to the next character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003749 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751
3752 if (copy)
3753 {
3754 *msgp++ = ':';
3755 *msgp++ = ' ';
3756 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 else
3759 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003760 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003761 + 2 /* for the NL's */
3762 + STRLEN(buttons)
3763 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003764 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
3766 /* If no hotkey is specified first char is used. */
3767 if (!has_hotkey[0])
3768 {
3769 first_hotkey = TRUE;
3770 len += 2; /* "x" -> "[x]" */
3771 }
3772
3773 /*
3774 * Now allocate and load the strings
3775 */
3776 vim_free(confirm_msg);
3777 confirm_msg = alloc(len);
3778 if (confirm_msg == NULL)
3779 return NULL;
3780 *confirm_msg = NUL;
3781 hotk = alloc(lenhotkey);
3782 if (hotk == NULL)
3783 return NULL;
3784
3785 *confirm_msg = '\n';
3786 STRCPY(confirm_msg + 1, message);
3787
3788 msgp = confirm_msg + 1 + STRLEN(message);
3789 hotkp = hotk;
3790
Bram Moolenaar6a516062007-07-05 08:11:42 +00003791 /* Define first default hotkey. Keep the hotkey string NUL
3792 * terminated to avoid reading past the end. */
3793 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 /* Remember where the choices start, displaying starts here when
3796 * "hotkp" typed at the more prompt. */
3797 confirm_msg_tail = msgp;
3798 *msgp++ = '\n';
3799 }
3800 }
3801
3802 display_confirm_msg();
3803 return hotk;
3804}
3805
3806/*
3807 * Display the ":confirm" message. Also called when screen resized.
3808 */
3809 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003810display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 /* avoid that 'q' at the more prompt truncates the message here */
3813 ++confirm_msg_used;
3814 if (confirm_msg != NULL)
3815 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3816 --confirm_msg_used;
3817}
3818
3819#endif /* FEAT_CON_DIALOG */
3820
3821#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3822
3823 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003824vim_dialog_yesno(
3825 int type,
3826 char_u *title,
3827 char_u *message,
3828 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 if (do_dialog(type,
3831 title == NULL ? (char_u *)_("Question") : title,
3832 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003833 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 return VIM_YES;
3835 return VIM_NO;
3836}
3837
3838 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003839vim_dialog_yesnocancel(
3840 int type,
3841 char_u *title,
3842 char_u *message,
3843 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 switch (do_dialog(type,
3846 title == NULL ? (char_u *)_("Question") : title,
3847 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003848 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850 case 1: return VIM_YES;
3851 case 2: return VIM_NO;
3852 }
3853 return VIM_CANCEL;
3854}
3855
3856 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003857vim_dialog_yesnoallcancel(
3858 int type,
3859 char_u *title,
3860 char_u *message,
3861 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
3863 switch (do_dialog(type,
3864 title == NULL ? (char_u *)"Question" : title,
3865 message,
3866 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003867 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 case 1: return VIM_YES;
3870 case 2: return VIM_NO;
3871 case 3: return VIM_ALL;
3872 case 4: return VIM_DISCARDALL;
3873 }
3874 return VIM_CANCEL;
3875}
3876
3877#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3878
3879#if defined(FEAT_BROWSE) || defined(PROTO)
3880/*
3881 * Generic browse function. Calls gui_mch_browse() when possible.
3882 * Later this may pop-up a non-GUI file selector (external command?).
3883 */
3884 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003885do_browse(
3886 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3887 char_u *title, /* title for the window */
3888 char_u *dflt, /* default file name (may include directory) */
3889 char_u *ext, /* extension added */
3890 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003892 char_u *filter, /* file name filter */
3893 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895 char_u *fname;
3896 static char_u *last_dir = NULL; /* last used directory */
3897 char_u *tofree = NULL;
3898 int save_browse = cmdmod.browse;
3899
3900 /* Must turn off browse to avoid that autocommands will get the
3901 * flag too! */
3902 cmdmod.browse = FALSE;
3903
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003904 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003906 if (flags & BROWSE_DIR)
3907 title = (char_u *)_("Select Directory dialog");
3908 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 title = (char_u *)_("Save File dialog");
3910 else
3911 title = (char_u *)_("Open File dialog");
3912 }
3913
3914 /* When no directory specified, use default file name, default dir, buffer
3915 * dir, last dir or current dir */
3916 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3917 {
3918 if (mch_isdir(dflt)) /* default file name is a directory */
3919 {
3920 initdir = dflt;
3921 dflt = NULL;
3922 }
3923 else if (gettail(dflt) != dflt) /* default file name includes a path */
3924 {
3925 tofree = vim_strsave(dflt);
3926 if (tofree != NULL)
3927 {
3928 initdir = tofree;
3929 *gettail(initdir) = NUL;
3930 dflt = gettail(dflt);
3931 }
3932 }
3933 }
3934
3935 if (initdir == NULL || *initdir == NUL)
3936 {
3937 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003938 if (STRCMP(p_bsdir, "last") != 0
3939 && STRCMP(p_bsdir, "buffer") != 0
3940 && STRCMP(p_bsdir, "current") != 0
3941 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 initdir = p_bsdir;
3943 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003944 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 && buf != NULL && buf->b_ffname != NULL)
3946 {
3947 if (dflt == NULL || *dflt == NUL)
3948 dflt = gettail(curbuf->b_ffname);
3949 tofree = vim_strsave(curbuf->b_ffname);
3950 if (tofree != NULL)
3951 {
3952 initdir = tofree;
3953 *gettail(initdir) = NUL;
3954 }
3955 }
3956 /* When 'browsedir' is "last", use dir from last browse */
3957 else if (*p_bsdir == 'l')
3958 initdir = last_dir;
3959 /* When 'browsedir is "current", use current directory. This is the
3960 * default already, leave initdir empty. */
3961 }
3962
3963# ifdef FEAT_GUI
3964 if (gui.in_use) /* when this changes, also adjust f_has()! */
3965 {
3966 if (filter == NULL
3967# ifdef FEAT_EVAL
3968 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3969 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3970# endif
3971 )
3972 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003973 if (flags & BROWSE_DIR)
3974 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003975# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003976 /* For systems that have a directory dialog. */
3977 fname = gui_mch_browsedir(title, initdir);
3978# else
3979 /* Generic solution for selecting a directory: select a file and
3980 * remove the file name. */
3981 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3982# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003983# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003984 /* Win32 adds a dummy file name, others return an arbitrary file
3985 * name. GTK+ 2 returns only the directory, */
3986 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3987 {
3988 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003989 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003990
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003991 if (tail == fname)
3992 *tail++ = '.'; /* use current dir */
3993 *tail = NUL;
3994 }
3995# endif
3996 }
3997 else
3998 fname = gui_mch_browse(flags & BROWSE_SAVE,
3999 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 /* We hang around in the dialog for a while, the user might do some
4002 * things to our files. The Win32 dialog allows deleting or renaming
4003 * a file, check timestamps. */
4004 need_check_timestamps = TRUE;
4005 did_check_timestamps = FALSE;
4006 }
4007 else
4008# endif
4009 {
4010 /* TODO: non-GUI file selector here */
4011 EMSG(_("E338: Sorry, no file browser in console mode"));
4012 fname = NULL;
4013 }
4014
4015 /* keep the directory for next time */
4016 if (fname != NULL)
4017 {
4018 vim_free(last_dir);
4019 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004020 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
4022 *gettail(last_dir) = NUL;
4023 if (*last_dir == NUL)
4024 {
4025 /* filename only returned, must be in current dir */
4026 vim_free(last_dir);
4027 last_dir = alloc(MAXPATHL);
4028 if (last_dir != NULL)
4029 mch_dirname(last_dir, MAXPATHL);
4030 }
4031 }
4032 }
4033
4034 vim_free(tofree);
4035 cmdmod.browse = save_browse;
4036
4037 return fname;
4038}
4039#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004040
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004041#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004042static char *e_printf = N_("E766: Insufficient arguments for printf()");
4043
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004044static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004045static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004046# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004047static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004048# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004049
4050/*
4051 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4052 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004053 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004054tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055{
4056 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004057 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 int err = FALSE;
4059
4060 if (tvs[idx].v_type == VAR_UNKNOWN)
4061 EMSG(_(e_printf));
4062 else
4063 {
4064 ++*idxp;
4065 n = get_tv_number_chk(&tvs[idx], &err);
4066 if (err)
4067 n = 0;
4068 }
4069 return n;
4070}
4071
4072/*
4073 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004074 * If "tofree" is NULL get_tv_string_chk() is used. Some types (e.g. List)
4075 * are not converted to a string.
4076 * If "tofree" is not NULL echo_string() is used. All types are converted to
4077 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004078 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004079 */
4080 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004081tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004083 int idx = *idxp - 1;
4084 char *s = NULL;
4085 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004086
4087 if (tvs[idx].v_type == VAR_UNKNOWN)
4088 EMSG(_(e_printf));
4089 else
4090 {
4091 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004092 if (tofree != NULL)
4093 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4094 else
4095 s = (char *)get_tv_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096 }
4097 return s;
4098}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004099
4100# ifdef FEAT_FLOAT
4101/*
4102 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4103 */
4104 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004105tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004106{
4107 int idx = *idxp - 1;
4108 double f = 0;
4109
4110 if (tvs[idx].v_type == VAR_UNKNOWN)
4111 EMSG(_(e_printf));
4112 else
4113 {
4114 ++*idxp;
4115 if (tvs[idx].v_type == VAR_FLOAT)
4116 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004117 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004118 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004119 else
4120 EMSG(_("E807: Expected Float argument for printf()"));
4121 }
4122 return f;
4123}
4124# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004125#endif
4126
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004127#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004128/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004129 * Return the representation of infinity for printf() function:
4130 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4131 */
4132 static const char *
4133infinity_str(int positive,
4134 char fmt_spec,
4135 int force_sign,
4136 int space_for_positive)
4137{
4138 static const char *table[] =
4139 {
4140 "-inf", "inf", "+inf", " inf",
4141 "-INF", "INF", "+INF", " INF"
4142 };
4143 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4144
4145 if (ASCII_ISUPPER(fmt_spec))
4146 idx += 4;
4147 return table[idx];
4148}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004149#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004150
4151/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004152 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004153 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004154 * consistency.
4155 *
4156 * This code is based on snprintf.c - a portable implementation of snprintf
4157 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004158 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004159 * The original code, including useful comments, can be found here:
4160 * http://www.ijs.si/software/snprintf/
4161 *
4162 * This snprintf() only supports the following conversion specifiers:
4163 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4164 * with flags: '-', '+', ' ', '0' and '#'.
4165 * An asterisk is supported for field width as well as precision.
4166 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004167 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004168 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004169 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4170 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004171 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004172 * The locale is not used, the string is used as a byte string. This is only
4173 * relevant for double-byte encodings where the second byte may be '%'.
4174 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004175 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4176 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004177 *
4178 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004179 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004180 * is greater or equal to "str_m", not all characters from the result
4181 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4182 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004183 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184 */
4185
4186/*
4187 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188 *
4189 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004190 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004191 */
4192
4193/* When generating prototypes all of this is skipped, cproto doesn't
4194 * understand this. */
4195#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004196
Bram Moolenaara800b422010-06-27 01:15:55 +02004197/* Like vim_vsnprintf() but append to the string. */
4198 int
4199vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4200{
4201 va_list ap;
4202 int str_l;
4203 size_t len = STRLEN(str);
4204 size_t space;
4205
4206 if (str_m <= len)
4207 space = 0;
4208 else
4209 space = str_m - len;
4210 va_start(ap, fmt);
4211 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4212 va_end(ap);
4213 return str_l;
4214}
Bram Moolenaara800b422010-06-27 01:15:55 +02004215
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004216 int
4217vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4218{
4219 va_list ap;
4220 int str_l;
4221
4222 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004224 va_end(ap);
4225 return str_l;
4226}
4227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004228 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004229vim_vsnprintf(
4230 char *str,
4231 size_t str_m,
4232 char *fmt,
4233 va_list ap,
4234 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004235{
4236 size_t str_l = 0;
4237 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004238 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004239
4240 if (p == NULL)
4241 p = "";
4242 while (*p != NUL)
4243 {
4244 if (*p != '%')
4245 {
4246 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004247 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004248
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004249 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004250 if (str_l < str_m)
4251 {
4252 size_t avail = str_m - str_l;
4253
4254 mch_memmove(str + str_l, p, n > avail ? avail : n);
4255 }
4256 p += n;
4257 str_l += n;
4258 }
4259 else
4260 {
4261 size_t min_field_width = 0, precision = 0;
4262 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4263 int alternate_form = 0, force_sign = 0;
4264
4265 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4266 * ignored. */
4267 int space_for_positive = 1;
4268
4269 /* allowed values: \0, h, l, L */
4270 char length_modifier = '\0';
4271
4272 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004273# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004274# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004275 * That sounds reasonable to use as the maximum
4276 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004277# elif defined(FEAT_NUM64)
4278# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004279# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004280# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004281# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004282 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283
4284 /* string address in case of string argument */
4285 char *str_arg;
4286
4287 /* natural field width of arg without padding and sign */
4288 size_t str_arg_l;
4289
4290 /* unsigned char argument value - only defined for c conversion.
4291 * N.B. standard explicitly states the char argument for the c
4292 * conversion is unsigned */
4293 unsigned char uchar_arg;
4294
4295 /* number of zeros to be inserted for numeric conversions as
4296 * required by the precision or minimal field width */
4297 size_t number_of_zeros_to_pad = 0;
4298
4299 /* index into tmp where zero padding is to be inserted */
4300 size_t zero_padding_insertion_ind = 0;
4301
4302 /* current conversion specifier character */
4303 char fmt_spec = '\0';
4304
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004305 /* buffer for 's' and 'S' specs */
4306 char_u *tofree = NULL;
4307
4308
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004309 str_arg = NULL;
4310 p++; /* skip '%' */
4311
4312 /* parse flags */
4313 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4314 || *p == '#' || *p == '\'')
4315 {
4316 switch (*p)
4317 {
4318 case '0': zero_padding = 1; break;
4319 case '-': justify_left = 1; break;
4320 case '+': force_sign = 1; space_for_positive = 0; break;
4321 case ' ': force_sign = 1;
4322 /* If both the ' ' and '+' flags appear, the ' '
4323 * flag should be ignored */
4324 break;
4325 case '#': alternate_form = 1; break;
4326 case '\'': break;
4327 }
4328 p++;
4329 }
4330 /* If the '0' and '-' flags both appear, the '0' flag should be
4331 * ignored. */
4332
4333 /* parse field width */
4334 if (*p == '*')
4335 {
4336 int j;
4337
4338 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004339 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004341 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342# endif
4343 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004344 if (j >= 0)
4345 min_field_width = j;
4346 else
4347 {
4348 min_field_width = -j;
4349 justify_left = 1;
4350 }
4351 }
4352 else if (VIM_ISDIGIT((int)(*p)))
4353 {
4354 /* size_t could be wider than unsigned int; make sure we treat
4355 * argument like common implementations do */
4356 unsigned int uj = *p++ - '0';
4357
4358 while (VIM_ISDIGIT((int)(*p)))
4359 uj = 10 * uj + (unsigned int)(*p++ - '0');
4360 min_field_width = uj;
4361 }
4362
4363 /* parse precision */
4364 if (*p == '.')
4365 {
4366 p++;
4367 precision_specified = 1;
4368 if (*p == '*')
4369 {
4370 int j;
4371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004374 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375# endif
4376 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004377 p++;
4378 if (j >= 0)
4379 precision = j;
4380 else
4381 {
4382 precision_specified = 0;
4383 precision = 0;
4384 }
4385 }
4386 else if (VIM_ISDIGIT((int)(*p)))
4387 {
4388 /* size_t could be wider than unsigned int; make sure we
4389 * treat argument like common implementations do */
4390 unsigned int uj = *p++ - '0';
4391
4392 while (VIM_ISDIGIT((int)(*p)))
4393 uj = 10 * uj + (unsigned int)(*p++ - '0');
4394 precision = uj;
4395 }
4396 }
4397
4398 /* parse 'h', 'l' and 'll' length modifiers */
4399 if (*p == 'h' || *p == 'l')
4400 {
4401 length_modifier = *p;
4402 p++;
4403 if (length_modifier == 'l' && *p == 'l')
4404 {
4405 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004406# ifdef FEAT_NUM64
4407 length_modifier = 'L';
4408# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004409 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004410# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004411 p++;
4412 }
4413 }
4414 fmt_spec = *p;
4415
4416 /* common synonyms: */
4417 switch (fmt_spec)
4418 {
4419 case 'i': fmt_spec = 'd'; break;
4420 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4421 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4422 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4423 default: break;
4424 }
4425
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004426# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4427 switch (fmt_spec)
4428 {
4429 case 'd': case 'u': case 'o': case 'x': case 'X':
4430 if (tvs != NULL && length_modifier == '\0')
4431 length_modifier = 'L';
4432 }
4433# endif
4434
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004435 /* get parameter value, do initial processing */
4436 switch (fmt_spec)
4437 {
4438 /* '%' and 'c' behave similar to 's' regarding flags and field
4439 * widths */
4440 case '%':
4441 case 'c':
4442 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004443 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004444 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004445 str_arg_l = 1;
4446 switch (fmt_spec)
4447 {
4448 case '%':
4449 str_arg = p;
4450 break;
4451
4452 case 'c':
4453 {
4454 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455
4456 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004458 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459# endif
4460 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461 /* standard demands unsigned char */
4462 uchar_arg = (unsigned char)j;
4463 str_arg = (char *)&uchar_arg;
4464 break;
4465 }
4466
4467 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004468 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004471 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472# endif
4473 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004474 if (str_arg == NULL)
4475 {
4476 str_arg = "[NULL]";
4477 str_arg_l = 6;
4478 }
4479 /* make sure not to address string beyond the specified
4480 * precision !!! */
4481 else if (!precision_specified)
4482 str_arg_l = strlen(str_arg);
4483 /* truncate string if necessary as requested by precision */
4484 else if (precision == 0)
4485 str_arg_l = 0;
4486 else
4487 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004488 /* Don't put the #if inside memchr(), it can be a
4489 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004490# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004491 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004492# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004493 /* memchr on HP does not like n > 2^31 !!! */
4494 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004495 precision <= (size_t)0x7fffffffL ? precision
4496 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004497# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004498 str_arg_l = (q == NULL) ? precision
4499 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004500 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004501# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004502 if (fmt_spec == 'S')
4503 {
4504 if (min_field_width != 0)
4505 min_field_width += STRLEN(str_arg)
4506 - mb_string2cells((char_u *)str_arg, -1);
4507 if (precision)
4508 {
4509 char_u *p1 = (char_u *)str_arg;
4510 size_t i;
4511
4512 for (i = 0; i < precision && *p1; i++)
4513 p1 += mb_ptr2len(p1);
4514
4515 str_arg_l = precision = p1 - (char_u *)str_arg;
4516 }
4517 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004518# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004519 break;
4520
4521 default:
4522 break;
4523 }
4524 break;
4525
Bram Moolenaar91984b92016-08-16 21:58:41 +02004526 case 'd': case 'u':
4527 case 'b': case 'B':
4528 case 'o':
4529 case 'x': case 'X':
4530 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004531 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004532 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004533 * imply the value is unsigned; d implies a signed
4534 * value */
4535
4536 /* 0 if numeric argument is zero (or if pointer is
4537 * NULL for 'p'), +1 if greater than zero (or nonzero
4538 * for unsigned arguments), -1 if negative (unsigned
4539 * argument is never negative) */
4540 int arg_sign = 0;
4541
4542 /* only defined for length modifier h, or for no
4543 * length modifiers */
4544 int int_arg = 0;
4545 unsigned int uint_arg = 0;
4546
4547 /* only defined for length modifier l */
4548 long int long_arg = 0;
4549 unsigned long int ulong_arg = 0;
4550
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004551# ifdef FEAT_NUM64
4552 /* only defined for length modifier ll */
4553 varnumber_T llong_arg = 0;
4554 uvarnumber_T ullong_arg = 0;
4555# endif
4556
Bram Moolenaare9997822016-08-28 16:03:38 +02004557 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004558 uvarnumber_T bin_arg = 0;
4559
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004560 /* pointer argument value -only defined for p
4561 * conversion */
4562 void *ptr_arg = NULL;
4563
4564 if (fmt_spec == 'p')
4565 {
4566 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004568# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004569 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4570 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571# endif
4572 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004573 if (ptr_arg != NULL)
4574 arg_sign = 1;
4575 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004576 else if (fmt_spec == 'b' || fmt_spec == 'B')
4577 {
4578 bin_arg =
4579# if defined(FEAT_EVAL)
4580 tvs != NULL ?
4581 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4582# endif
4583 va_arg(ap, uvarnumber_T);
4584 if (bin_arg != 0)
4585 arg_sign = 1;
4586 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004587 else if (fmt_spec == 'd')
4588 {
4589 /* signed */
4590 switch (length_modifier)
4591 {
4592 case '\0':
4593 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594 /* char and short arguments are passed as int. */
4595 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004597 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598# endif
4599 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004600 if (int_arg > 0)
4601 arg_sign = 1;
4602 else if (int_arg < 0)
4603 arg_sign = -1;
4604 break;
4605 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004608 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609# endif
4610 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004611 if (long_arg > 0)
4612 arg_sign = 1;
4613 else if (long_arg < 0)
4614 arg_sign = -1;
4615 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004616# ifdef FEAT_NUM64
4617 case 'L':
4618 llong_arg =
4619# if defined(FEAT_EVAL)
4620 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4621# endif
4622 va_arg(ap, varnumber_T);
4623 if (llong_arg > 0)
4624 arg_sign = 1;
4625 else if (llong_arg < 0)
4626 arg_sign = -1;
4627 break;
4628# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004629 }
4630 }
4631 else
4632 {
4633 /* unsigned */
4634 switch (length_modifier)
4635 {
4636 case '\0':
4637 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004640 tvs != NULL ? (unsigned)
4641 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642# endif
4643 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004644 if (uint_arg != 0)
4645 arg_sign = 1;
4646 break;
4647 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004650 tvs != NULL ? (unsigned long)
4651 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652# endif
4653 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004654 if (ulong_arg != 0)
4655 arg_sign = 1;
4656 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004657# ifdef FEAT_NUM64
4658 case 'L':
4659 ullong_arg =
4660# if defined(FEAT_EVAL)
4661 tvs != NULL ? (uvarnumber_T)
4662 tv_nr(tvs, &arg_idx) :
4663# endif
4664 va_arg(ap, uvarnumber_T);
4665 if (ullong_arg != 0)
4666 arg_sign = 1;
4667 break;
4668# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004669 }
4670 }
4671
4672 str_arg = tmp;
4673 str_arg_l = 0;
4674
4675 /* NOTE:
4676 * For d, i, u, o, x, and X conversions, if precision is
4677 * specified, the '0' flag should be ignored. This is so
4678 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4679 * FreeBSD, NetBSD; but not with Perl.
4680 */
4681 if (precision_specified)
4682 zero_padding = 0;
4683 if (fmt_spec == 'd')
4684 {
4685 if (force_sign && arg_sign >= 0)
4686 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4687 /* leave negative numbers for sprintf to handle, to
4688 * avoid handling tricky cases like (short int)-32768 */
4689 }
4690 else if (alternate_form)
4691 {
4692 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004693 && (fmt_spec == 'b' || fmt_spec == 'B'
4694 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004695 {
4696 tmp[str_arg_l++] = '0';
4697 tmp[str_arg_l++] = fmt_spec;
4698 }
4699 /* alternate form should have no effect for p
4700 * conversion, but ... */
4701 }
4702
4703 zero_padding_insertion_ind = str_arg_l;
4704 if (!precision_specified)
4705 precision = 1; /* default precision is 1 */
4706 if (precision == 0 && arg_sign == 0)
4707 {
4708 /* When zero value is formatted with an explicit
4709 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004710 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004711 }
4712 else
4713 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004714 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004715 int f_l = 0;
4716
4717 /* construct a simple format string for sprintf */
4718 f[f_l++] = '%';
4719 if (!length_modifier)
4720 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004721 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004722 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004723# ifdef FEAT_NUM64
4724# ifdef WIN3264
4725 f[f_l++] = 'I';
4726 f[f_l++] = '6';
4727 f[f_l++] = '4';
4728# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004729 f[f_l++] = 'l';
4730 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004731# endif
4732# else
4733 f[f_l++] = 'l';
4734# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004735 }
4736 else
4737 f[f_l++] = length_modifier;
4738 f[f_l++] = fmt_spec;
4739 f[f_l++] = '\0';
4740
4741 if (fmt_spec == 'p')
4742 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004743 else if (fmt_spec == 'b' || fmt_spec == 'B')
4744 {
4745 char b[8 * sizeof(uvarnumber_T)];
4746 size_t b_l = 0;
4747 uvarnumber_T bn = bin_arg;
4748
4749 do
4750 {
4751 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4752 bn >>= 1;
4753 }
4754 while (bn != 0);
4755
4756 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4757 str_arg_l += b_l;
4758 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004759 else if (fmt_spec == 'd')
4760 {
4761 /* signed */
4762 switch (length_modifier)
4763 {
4764 case '\0':
4765 case 'h': str_arg_l += sprintf(
4766 tmp + str_arg_l, f, int_arg);
4767 break;
4768 case 'l': str_arg_l += sprintf(
4769 tmp + str_arg_l, f, long_arg);
4770 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004771# ifdef FEAT_NUM64
4772 case 'L': str_arg_l += sprintf(
4773 tmp + str_arg_l, f, llong_arg);
4774 break;
4775# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004776 }
4777 }
4778 else
4779 {
4780 /* unsigned */
4781 switch (length_modifier)
4782 {
4783 case '\0':
4784 case 'h': str_arg_l += sprintf(
4785 tmp + str_arg_l, f, uint_arg);
4786 break;
4787 case 'l': str_arg_l += sprintf(
4788 tmp + str_arg_l, f, ulong_arg);
4789 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004790# ifdef FEAT_NUM64
4791 case 'L': str_arg_l += sprintf(
4792 tmp + str_arg_l, f, ullong_arg);
4793 break;
4794# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004795 }
4796 }
4797
4798 /* include the optional minus sign and possible
4799 * "0x" in the region before the zero padding
4800 * insertion point */
4801 if (zero_padding_insertion_ind < str_arg_l
4802 && tmp[zero_padding_insertion_ind] == '-')
4803 zero_padding_insertion_ind++;
4804 if (zero_padding_insertion_ind + 1 < str_arg_l
4805 && tmp[zero_padding_insertion_ind] == '0'
4806 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4807 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4808 zero_padding_insertion_ind += 2;
4809 }
4810
4811 {
4812 size_t num_of_digits = str_arg_l
4813 - zero_padding_insertion_ind;
4814
4815 if (alternate_form && fmt_spec == 'o'
4816 /* unless zero is already the first
4817 * character */
4818 && !(zero_padding_insertion_ind < str_arg_l
4819 && tmp[zero_padding_insertion_ind] == '0'))
4820 {
4821 /* assure leading zero for alternate-form
4822 * octal numbers */
4823 if (!precision_specified
4824 || precision < num_of_digits + 1)
4825 {
4826 /* precision is increased to force the
4827 * first character to be zero, except if a
4828 * zero value is formatted with an
4829 * explicit precision of zero */
4830 precision = num_of_digits + 1;
4831 precision_specified = 1;
4832 }
4833 }
4834 /* zero padding to specified precision? */
4835 if (num_of_digits < precision)
4836 number_of_zeros_to_pad = precision - num_of_digits;
4837 }
4838 /* zero padding to specified minimal field width? */
4839 if (!justify_left && zero_padding)
4840 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004841 int n = (int)(min_field_width - (str_arg_l
4842 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004843 if (n > 0)
4844 number_of_zeros_to_pad += n;
4845 }
4846 break;
4847 }
4848
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004849# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004850 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004851 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004852 case 'e':
4853 case 'E':
4854 case 'g':
4855 case 'G':
4856 {
4857 /* Floating point. */
4858 double f;
4859 double abs_f;
4860 char format[40];
4861 int l;
4862 int remove_trailing_zeroes = FALSE;
4863
4864 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004865# if defined(FEAT_EVAL)
4866 tvs != NULL ? tv_float(tvs, &arg_idx) :
4867# endif
4868 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004869 abs_f = f < 0 ? -f : f;
4870
4871 if (fmt_spec == 'g' || fmt_spec == 'G')
4872 {
4873 /* Would be nice to use %g directly, but it prints
4874 * "1.0" as "1", we don't want that. */
4875 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4876 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004877 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004878 else
4879 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4880 remove_trailing_zeroes = TRUE;
4881 }
4882
Bram Moolenaar04186092016-08-29 21:55:35 +02004883 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004884# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004885 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004886# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004887 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004888# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004889 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004890 {
4891 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004892 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4893 force_sign, space_for_positive));
4894 str_arg_l = STRLEN(tmp);
4895 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004896 }
4897 else
4898 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004899 if (isnan(f))
4900 {
4901 /* Not a number: nan or NAN */
4902 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4903 : "nan");
4904 str_arg_l = 3;
4905 zero_padding = 0;
4906 }
4907 else if (isinf(f))
4908 {
4909 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4910 force_sign, space_for_positive));
4911 str_arg_l = STRLEN(tmp);
4912 zero_padding = 0;
4913 }
4914 else
Bram Moolenaar04186092016-08-29 21:55:35 +02004915 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004916 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004917 format[0] = '%';
4918 l = 1;
4919 if (force_sign)
4920 format[l++] = space_for_positive ? ' ' : '+';
4921 if (precision_specified)
4922 {
4923 size_t max_prec = TMP_LEN - 10;
4924
4925 /* Make sure we don't get more digits than we
4926 * have room for. */
4927 if ((fmt_spec == 'f' || fmt_spec == 'F')
4928 && abs_f > 1.0)
4929 max_prec -= (size_t)log10(abs_f);
4930 if (precision > max_prec)
4931 precision = max_prec;
4932 l += sprintf(format + l, ".%d", (int)precision);
4933 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004934 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004935 format[l + 1] = NUL;
4936
Bram Moolenaare9997822016-08-28 16:03:38 +02004937 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar04186092016-08-29 21:55:35 +02004938 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004939
Bram Moolenaara7241f52008-06-24 20:39:31 +00004940 if (remove_trailing_zeroes)
4941 {
4942 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004943 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004944
4945 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02004946 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004947 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004948 else
4949 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004950 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004951 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004952 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004953 {
4954 /* Remove superfluous '+' and leading
4955 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004956 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004957 {
4958 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004959 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004960 --str_arg_l;
4961 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004962 i = (tp[1] == '-') ? 2 : 1;
4963 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004964 {
4965 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004966 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004967 --str_arg_l;
4968 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004969 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004970 }
4971 }
4972
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004973 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004974 /* Remove trailing zeroes, but keep the one
4975 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004976 while (tp > tmp + 2 && *tp == '0'
4977 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004978 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004979 STRMOVE(tp, tp + 1);
4980 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004981 --str_arg_l;
4982 }
4983 }
4984 else
4985 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004986 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004987
4988 /* Be consistent: some printf("%e") use 1.0e+12
4989 * and some 1.0e+012. Remove one zero in the last
4990 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004991 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004992 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004993 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4994 && tp[2] == '0'
4995 && vim_isdigit(tp[3])
4996 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004997 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004998 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004999 --str_arg_l;
5000 }
5001 }
5002 }
Bram Moolenaar04186092016-08-29 21:55:35 +02005003 if (zero_padding && min_field_width > str_arg_l
5004 && (tmp[0] == '-' || force_sign))
5005 {
5006 /* padding 0's should be inserted after the sign */
5007 number_of_zeros_to_pad = min_field_width - str_arg_l;
5008 zero_padding_insertion_ind = 1;
5009 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00005010 str_arg = tmp;
5011 break;
5012 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005013# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00005014
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005015 default:
5016 /* unrecognized conversion specifier, keep format string
5017 * as-is */
5018 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00005019 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005020 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005021 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005022
5023 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005024 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005025 str_arg = p;
5026 str_arg_l = 0;
5027 if (*p != NUL)
5028 str_arg_l++; /* include invalid conversion specifier
5029 unchanged if not at end-of-string */
5030 break;
5031 }
5032
5033 if (*p != NUL)
5034 p++; /* step over the just processed conversion specifier */
5035
5036 /* insert padding to the left as requested by min_field_width;
5037 * this does not include the zero padding in case of numerical
5038 * conversions*/
5039 if (!justify_left)
5040 {
5041 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005042 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005043
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005044 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005045 {
5046 if (str_l < str_m)
5047 {
5048 size_t avail = str_m - str_l;
5049
5050 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005051 (size_t)pn > avail ? avail
5052 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005053 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005054 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005055 }
5056 }
5057
5058 /* zero padding as requested by the precision or by the minimal
5059 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005060 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005061 {
5062 /* will not copy first part of numeric right now, *
5063 * force it to be copied later in its entirety */
5064 zero_padding_insertion_ind = 0;
5065 }
5066 else
5067 {
5068 /* insert first part of numerics (sign or '0x') before zero
5069 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005070 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005071
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005072 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005073 {
5074 if (str_l < str_m)
5075 {
5076 size_t avail = str_m - str_l;
5077
5078 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005079 (size_t)zn > avail ? avail
5080 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005081 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005082 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005083 }
5084
5085 /* insert zero padding as requested by the precision or min
5086 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005087 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005088 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005089 {
5090 if (str_l < str_m)
5091 {
5092 size_t avail = str_m-str_l;
5093
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005094 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005095 (size_t)zn > avail ? avail
5096 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005097 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005098 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005099 }
5100 }
5101
5102 /* insert formatted string
5103 * (or as-is conversion specifier for unknown conversions) */
5104 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005105 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005106
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005107 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005108 {
5109 if (str_l < str_m)
5110 {
5111 size_t avail = str_m - str_l;
5112
5113 mch_memmove(str + str_l,
5114 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005115 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005116 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005117 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005118 }
5119 }
5120
5121 /* insert right padding */
5122 if (justify_left)
5123 {
5124 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005125 int pn = (int)(min_field_width
5126 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005127
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005128 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005129 {
5130 if (str_l < str_m)
5131 {
5132 size_t avail = str_m - str_l;
5133
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005134 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005135 (size_t)pn > avail ? avail
5136 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005137 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005138 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005139 }
5140 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005141 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005142 }
5143 }
5144
5145 if (str_m > 0)
5146 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005147 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005148 * overwriting the last character (shouldn't happen, but just in case)
5149 * */
5150 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5151 }
5152
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005153 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005156 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005157 * character), that is, the number of characters that would have been
5158 * written to the buffer if it were large enough. */
5159 return (int)str_l;
5160}
5161
5162#endif /* PROTO */