blob: 32360a93b65bc384f857063178a21c4a836dfb67 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
14#define MESSAGE_FILE /* don't include prototype for smsg() */
Bram Moolenaar44ca54a2016-08-27 15:41:32 +020015#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17#include "vim.h"
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static int other_sourcing_name(void);
20static char_u *get_emsg_source(void);
21static char_u *get_emsg_lnum(void);
22static void add_msg_hist(char_u *s, int len, int attr);
23static void hit_return_msg(void);
24static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
29static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
30static void msg_scroll_up(void);
31static void inc_msg_scrolled(void);
32static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
33static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
34static void msg_puts_printf(char_u *str, int maxlen);
35static int do_more_prompt(int typed_char);
36static void msg_screen_putchar(int c, int attr);
37static int msg_check_screen(void);
38static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041static int confirm_msg_used = FALSE; /* displaying confirm_msg */
42static char_u *confirm_msg = NULL; /* ":confirm" message */
43static char_u *confirm_msg_tail; /* tail of confirm_msg */
44#endif
45
46struct msg_hist
47{
48 struct msg_hist *next;
49 char_u *msg;
50 int attr;
51};
52
53static struct msg_hist *first_msg_hist = NULL;
54static struct msg_hist *last_msg_hist = NULL;
55static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020057static FILE *verbose_fd = NULL;
58static int verbose_did_open = FALSE;
59
Bram Moolenaar071d4272004-06-13 20:20:40 +000060/*
61 * When writing messages to the screen, there are many different situations.
62 * A number of variables is used to remember the current state:
63 * msg_didany TRUE when messages were written since the last time the
64 * user reacted to a prompt.
65 * Reset: After hitting a key for the hit-return prompt,
66 * hitting <CR> for the command line or input().
67 * Set: When any message is written to the screen.
68 * msg_didout TRUE when something was written to the current line.
69 * Reset: When advancing to the next line, when the current
70 * text can be overwritten.
71 * Set: When any message is written to the screen.
72 * msg_nowait No extra delay for the last drawn message.
73 * Used in normal_cmd() before the mode message is drawn.
74 * emsg_on_display There was an error message recently. Indicates that there
75 * should be a delay before redrawing.
76 * msg_scroll The next message should not overwrite the current one.
77 * msg_scrolled How many lines the screen has been scrolled (because of
78 * messages). Used in update_screen() to scroll the screen
79 * back. Incremented each time the screen scrolls a line.
80 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
81 * writes something without scrolling should not make
82 * need_wait_return to be set. This is a hack to make ":ts"
83 * work without an extra prompt.
84 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010085 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 * need_wait_return TRUE when the hit-return prompt is needed.
87 * Reset: After giving the hit-return prompt, when the user
88 * has answered some other prompt.
89 * Set: When the ruler or typeahead display is overwritten,
90 * scrolling the screen for some message.
91 * keep_msg Message to be displayed after redrawing the screen, in
92 * main_loop().
93 * This is an allocated string or NULL when not used.
94 */
95
96/*
97 * msg(s) - displays the string 's' on the status line
98 * When terminal not initialized (yet) mch_errmsg(..) is used.
99 * return TRUE if wait_return not called
100 */
101 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100102msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103{
104 return msg_attr_keep(s, 0, FALSE);
105}
106
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000107#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000108 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000109/*
110 * Like msg() but keep it silent when 'verbosefile' is set.
111 */
112 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100113verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000114{
115 int n;
116
117 verbose_enter();
118 n = msg_attr_keep(s, 0, FALSE);
119 verbose_leave();
120
121 return n;
122}
123#endif
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100126msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
128 return msg_attr_keep(s, attr, FALSE);
129}
130
131 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100132msg_attr_keep(
133 char_u *s,
134 int attr,
135 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 static int entered = 0;
138 int retval;
139 char_u *buf = NULL;
140
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200141 /* Skip messages not matching ":filter pattern".
142 * Don't filter when there is an error. */
143 if (!emsg_on_display && message_filtered(s))
144 return TRUE;
145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef FEAT_EVAL
147 if (attr == 0)
148 set_vim_var_string(VV_STATUSMSG, s, -1);
149#endif
150
151 /*
152 * It is possible that displaying a messages causes a problem (e.g.,
153 * when redrawing the window), which causes another message, etc.. To
154 * break this loop, limit the recursiveness to 3 levels.
155 */
156 if (entered >= 3)
157 return TRUE;
158 ++entered;
159
160 /* Add message to history (unless it's a repeated kept message or a
161 * truncated message) */
162 if (s != keep_msg
163 || (*s != '<'
164 && last_msg_hist != NULL
165 && last_msg_hist->msg != NULL
166 && STRCMP(s, last_msg_hist->msg)))
167 add_msg_hist(s, -1, attr);
168
169 /* When displaying keep_msg, don't let msg_start() free it, caller must do
170 * that. */
171 if (s == keep_msg)
172 keep_msg = NULL;
173
174 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000175 msg_start();
176 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 if (buf != NULL)
178 s = buf;
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 msg_outtrans_attr(s, attr);
181 msg_clr_eos();
182 retval = msg_end();
183
184 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
185 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000186 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188 vim_free(buf);
189 --entered;
190 return retval;
191}
192
193/*
194 * Truncate a string such that it can be printed without causing a scroll.
195 * Returns an allocated string or NULL when no truncating is done.
196 */
197 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100198msg_strtrunc(
199 char_u *s,
200 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 char_u *buf = NULL;
203 int len;
204 int room;
205
206 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000207 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
208 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000211 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000212 /* Use all the columns. */
213 room = (int)(Rows - msg_row) * Columns - 1;
214 else
215 /* Use up to 'showcmd' column. */
216 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 if (len > room && room > 0)
218 {
219#ifdef FEAT_MBYTE
220 if (enc_utf8)
221 /* may have up to 18 bytes per cell (6 per char, up to two
222 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 else if (enc_dbcs == DBCS_JPNU)
225 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 else
228#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = room + 2;
230 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234 }
235 return buf;
236}
237
238/*
239 * Truncate a string "s" to "buf" with cell width "room".
240 * "s" and "buf" may be equal.
241 */
242 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100243trunc_string(
244 char_u *s,
245 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200246 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100247 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200249 size_t room = room_in - 3; /* "..." takes 3 chars */
250 size_t half;
251 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 int e;
253 int i;
254 int n;
255
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200256 if (room_in < 3)
257 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100261 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 if (s[e] == NUL)
264 {
265 /* text fits without truncating! */
266 buf[e] = NUL;
267 return;
268 }
269 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200270 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 break;
272 len += n;
273 buf[e] = s[e];
274#ifdef FEAT_MBYTE
275 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000276 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100278 if (++e == buflen)
279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 buf[e] = s[e];
281 }
282#endif
283 }
284
285 /* Last part: End of the string. */
286 i = e;
287#ifdef FEAT_MBYTE
288 if (enc_dbcs != 0)
289 {
290 /* For DBCS going backwards in a string is slow, but
291 * computing the cell width isn't too slow: go forward
292 * until the rest fits. */
293 n = vim_strsize(s + i);
294 while (len + n > room)
295 {
296 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000297 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 }
299 }
300 else if (enc_utf8)
301 {
302 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000303 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 for (;;)
305 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000306 do
307 half = half - (*mb_head_off)(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200308 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200310 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 break;
312 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200313 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 }
315 }
316 else
317#endif
318 {
319 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
320 len += n;
321 }
322
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200323
324 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100325 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200326 /* text fits without truncating */
327 if (s != buf)
328 {
329 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200330 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200331 len = buflen - 1;
332 len = len - e + 1;
333 if (len < 1)
334 buf[e - 1] = NUL;
335 else
336 mch_memmove(buf + e, s + e, len);
337 }
338 }
339 else if (e + 3 < buflen)
340 {
341 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100342 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200343 len = STRLEN(s + i) + 1;
344 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 len = buflen - e - 3 - 1;
346 mch_memmove(buf + e + 3, s + i, len);
347 buf[e + 3 + len - 1] = NUL;
348 }
349 else
350 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200351 /* can't fit in the "...", just truncate it */
352 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354}
355
356/*
357 * Automatic prototype generation does not understand this function.
358 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
359 * shorter than IOSIZE!!!
360 */
361#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000363int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100366# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369smsg(char_u *s, ...)
370{
371 va_list arglist;
372
373 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000374 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 va_end(arglist);
376 return msg(IObuff);
377}
378
379 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100380# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383smsg_attr(int attr, char_u *s, ...)
384{
385 va_list arglist;
386
387 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000388 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 va_end(arglist);
390 return msg_attr(IObuff, attr);
391}
392
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394
395/*
396 * Remember the last sourcing name/lnum used in an error message, so that it
397 * isn't printed each time when it didn't change.
398 */
399static int last_sourcing_lnum = 0;
400static char_u *last_sourcing_name = NULL;
401
402/*
403 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
404 * for the next error message;
405 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000406 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100407reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
409 vim_free(last_sourcing_name);
410 last_sourcing_name = NULL;
411 last_sourcing_lnum = 0;
412}
413
414/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000415 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
416 */
417 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100418other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419{
420 if (sourcing_name != NULL)
421 {
422 if (last_sourcing_name != NULL)
423 return STRCMP(sourcing_name, last_sourcing_name) != 0;
424 return TRUE;
425 }
426 return FALSE;
427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Get the message about the source, as used for an error message.
431 * Returns an allocated string with room for one more character.
432 * Returns NULL when no message is to be given.
433 */
434 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100435get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 char_u *Buf, *p;
438
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000439 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
441 p = (char_u *)_("Error detected while processing %s:");
442 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
443 if (Buf != NULL)
444 sprintf((char *)Buf, (char *)p, sourcing_name);
445 return Buf;
446 }
447 return NULL;
448}
449
450/*
451 * Get the message about the source lnum, as used for an error message.
452 * Returns an allocated string with room for one more character.
453 * Returns NULL when no message is to be given.
454 */
455 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100456get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 char_u *Buf, *p;
459
460 /* lnum is 0 when executing a command from the command line
461 * argument, we don't want a line number then */
462 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000463 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 && sourcing_lnum != 0)
465 {
466 p = (char_u *)_("line %4ld:");
467 Buf = alloc((unsigned)(STRLEN(p) + 20));
468 if (Buf != NULL)
469 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
470 return Buf;
471 }
472 return NULL;
473}
474
475/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000476 * Display name and line number for the source of an error.
477 * Remember the file name and line number, so that for the next error the info
478 * is only displayed if it changed.
479 */
480 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100481msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000482{
483 char_u *p;
484
485 ++no_wait_return;
486 p = get_emsg_source();
487 if (p != NULL)
488 {
489 msg_attr(p, attr);
490 vim_free(p);
491 }
492 p = get_emsg_lnum();
493 if (p != NULL)
494 {
495 msg_attr(p, hl_attr(HLF_N));
496 vim_free(p);
497 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
498 }
499
500 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000501 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000502 {
503 vim_free(last_sourcing_name);
504 if (sourcing_name == NULL)
505 last_sourcing_name = NULL;
506 else
507 last_sourcing_name = vim_strsave(sourcing_name);
508 }
509 --no_wait_return;
510}
511
512/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000513 * Return TRUE if not giving error messages right now:
514 * If "emsg_off" is set: no error messages at the moment.
515 * If "msg" is in 'debug': do error message but without side effects.
516 * If "emsg_skip" is set: never do error messages.
517 */
518 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100519emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000520{
521 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
522 && vim_strchr(p_debug, 't') == NULL)
523#ifdef FEAT_EVAL
524 || emsg_skip > 0
525#endif
526 )
527 return TRUE;
528 return FALSE;
529}
530
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200531#if !defined(HAVE_STRERROR) || defined(PROTO)
532/*
533 * Replacement for perror() that behaves more or less like emsg() was called.
534 * v:errmsg will be set and called_emsg will be set.
535 */
536 void
537do_perror(char *msg)
538{
539 perror(msg);
540 ++emsg_silent;
541 emsg((char_u *)msg);
542 --emsg_silent;
543}
544#endif
545
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * emsg() - display an error message
548 *
549 * Rings the bell, if appropriate, and calls message() to do the real work
550 * When terminal not initialized (yet) mch_errmsg(..) is used.
551 *
552 * return TRUE if wait_return not called
553 */
554 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100555emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 char_u *p;
559#ifdef FEAT_EVAL
560 int ignore = FALSE;
561 int severe;
562#endif
563
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100564 /* Skip this if not giving error messages at the moment. */
565 if (emsg_not_now())
566 return TRUE;
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 called_emsg = TRUE;
Bram Moolenaar8b778d52016-02-18 20:31:34 +0100569 if (emsg_silent == 0)
570 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000573 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
574 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 */
576#ifdef FEAT_EVAL
577 severe = emsg_severe;
578 emsg_severe = FALSE;
579#endif
580
Bram Moolenaar57657d82006-04-21 22:12:41 +0000581 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583#ifdef FEAT_EVAL
584 /*
585 * Cause a throw of an error exception if appropriate. Don't display
586 * the error message in this case. (If no matching catch clause will
587 * be found, the message will be displayed later on.) "ignore" is set
588 * when the message should be ignored completely (used for the
589 * interrupt message).
590 */
591 if (cause_errthrow(s, severe, &ignore) == TRUE)
592 {
593 if (!ignore)
594 did_emsg = TRUE;
595 return TRUE;
596 }
597
598 /* set "v:errmsg", also when using ":silent! cmd" */
599 set_vim_var_string(VV_ERRMSG, s, -1);
600#endif
601
602 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000603 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * But do write it to the redirection file.
605 */
606 if (emsg_silent != 0)
607 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200608 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200610 msg_start();
611 p = get_emsg_source();
612 if (p != NULL)
613 {
614 STRCAT(p, "\n");
615 redir_write(p, -1);
616 vim_free(p);
617 }
618 p = get_emsg_lnum();
619 if (p != NULL)
620 {
621 STRCAT(p, "\n");
622 redir_write(p, -1);
623 vim_free(p);
624 }
625 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 return TRUE;
628 }
629
630 /* Reset msg_silent, an error causes messages to be switched back on. */
631 msg_silent = 0;
632 cmd_silent = FALSE;
633
634 if (global_busy) /* break :global command */
635 ++global_busy;
636
637 if (p_eb)
638 beep_flush(); /* also includes flush_buffers() */
639 else
640 flush_buffers(FALSE); /* flush internal buffers */
641 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643
644 emsg_on_display = TRUE; /* remember there is an error message */
645 ++msg_scroll; /* don't overwrite a previous message */
646 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000647 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 need_wait_return = TRUE; /* needed in case emsg() is called after
649 * wait_return has reset need_wait_return
650 * and a redraw is expected because
651 * msg_scrolled is non-zero */
652
653 /*
654 * Display name and line number for the source of the error.
655 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000656 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
658 /*
659 * Display the error message itself.
660 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000661 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 return msg_attr(s, attr);
663}
664
Bram Moolenaar95f09602016-11-10 20:01:45 +0100665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666/*
667 * Print an error message with one "%s" and one string argument.
668 */
669 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100670emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 return emsg3(s, a1, NULL);
673}
674
Bram Moolenaar95f09602016-11-10 20:01:45 +0100675/*
676 * Print an error message with one or two "%s" and one or two string arguments.
677 * This is not in message.c to avoid a warning for prototypes.
678 */
679 int
680emsg3(char_u *s, char_u *a1, char_u *a2)
681{
682 if (emsg_not_now())
683 return TRUE; /* no error messages at the moment */
684 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
685 return emsg(IObuff);
686}
687
688/*
689 * Print an error message with one "%ld" and one long int argument.
690 * This is not in message.c to avoid a warning for prototypes.
691 */
692 int
693emsgn(char_u *s, long n)
694{
695 if (emsg_not_now())
696 return TRUE; /* no error messages at the moment */
697 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
698 return emsg(IObuff);
699}
700
701/*
702 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
703 * defined. It is used for internal errors only, so that they can be
704 * detected when fuzzing vim.
705 */
706 void
707iemsg(char_u *s)
708{
709 msg(s);
710#ifdef ABORT_ON_INTERNAL_ERROR
711 abort();
712#endif
713}
714
715
716/*
717 * Same as emsg2(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
718 * defined. It is used for internal errors only, so that they can be
719 * detected when fuzzing vim.
720 */
721 void
722iemsg2(char_u *s, char_u *a1)
723{
724 emsg2(s, a1);
725#ifdef ABORT_ON_INTERNAL_ERROR
726 abort();
727#endif
728}
729
730/*
731 * Same as emsgn(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
732 * defined. It is used for internal errors only, so that they can be
733 * detected when fuzzing vim.
734 */
735 void
736iemsgn(char_u *s, long n)
737{
738 emsgn(s, n);
739#ifdef ABORT_ON_INTERNAL_ERROR
740 abort();
741#endif
742}
743
744/*
745 * Give an "Internal error" message.
746 */
747 void
748internal_error(char *where)
749{
750 IEMSG2(_(e_intern2), where);
751}
752
Bram Moolenaarea424162005-06-16 21:51:00 +0000753/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaardf177f62005-02-22 08:39:57 +0000755 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100756emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000757{
758 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
759}
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761/*
762 * Like msg(), but truncate to a single line if p_shm contains 't', or when
763 * "force" is TRUE. This truncates in another way as for normal messages.
764 * Careful: The string may be changed by msg_may_trunc()!
765 * Returns a pointer to the printed message, if wait_return() not called.
766 */
767 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100768msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 int n;
771
772 /* Add message to history before truncating */
773 add_msg_hist(s, -1, attr);
774
775 s = msg_may_trunc(force, s);
776
777 msg_hist_off = TRUE;
778 n = msg_attr(s, attr);
779 msg_hist_off = FALSE;
780
781 if (n)
782 return s;
783 return NULL;
784}
785
786/*
787 * Check if message "s" should be truncated at the start (for filenames).
788 * Return a pointer to where the truncated message starts.
789 * Note: May change the message by replacing a character with '<'.
790 */
791 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100792msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 int n;
795 int room;
796
797 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
798 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
799 && (n = (int)STRLEN(s) - room) > 0)
800 {
801#ifdef FEAT_MBYTE
802 if (has_mbyte)
803 {
804 int size = vim_strsize(s);
805
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000806 /* There may be room anyway when there are multibyte chars. */
807 if (size <= room)
808 return s;
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 for (n = 0; size >= room; )
811 {
812 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000813 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
815 --n;
816 }
817#endif
818 s += n;
819 *s = '<';
820 }
821 return s;
822}
823
824 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100825add_msg_hist(
826 char_u *s,
827 int len, /* -1 for undetermined length */
828 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829{
830 struct msg_hist *p;
831
832 if (msg_hist_off || msg_silent != 0)
833 return;
834
835 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000836 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000837 (void)delete_first_msg();
838
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 /* allocate an entry and add the message at the end of the history */
840 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
841 if (p != NULL)
842 {
843 if (len < 0)
844 len = (int)STRLEN(s);
845 /* remove leading and trailing newlines */
846 while (len > 0 && *s == '\n')
847 {
848 ++s;
849 --len;
850 }
851 while (len > 0 && s[len - 1] == '\n')
852 --len;
853 p->msg = vim_strnsave(s, len);
854 p->next = NULL;
855 p->attr = attr;
856 if (last_msg_hist != NULL)
857 last_msg_hist->next = p;
858 last_msg_hist = p;
859 if (first_msg_hist == NULL)
860 first_msg_hist = last_msg_hist;
861 ++msg_hist_len;
862 }
863}
864
865/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000866 * Delete the first (oldest) message from the history.
867 * Returns FAIL if there are no messages.
868 */
869 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100870delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000871{
872 struct msg_hist *p;
873
874 if (msg_hist_len <= 0)
875 return FAIL;
876 p = first_msg_hist;
877 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000878 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200879 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000880 vim_free(p->msg);
881 vim_free(p);
882 --msg_hist_len;
883 return OK;
884}
885
886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 * ":messages" command.
888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200890ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 struct msg_hist *p;
893 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200894 int c = 0;
895
896 if (STRCMP(eap->arg, "clear") == 0)
897 {
898 int keep = eap->addr_count == 0 ? 0 : eap->line2;
899
900 while (msg_hist_len > keep)
901 (void)delete_first_msg();
902 return;
903 }
904
905 if (*eap->arg != NUL)
906 {
907 EMSG(_(e_invarg));
908 return;
909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
911 msg_hist_off = TRUE;
912
Bram Moolenaar451f8492016-04-14 17:16:22 +0200913 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200914 if (eap->addr_count != 0)
915 {
916 /* Count total messages */
917 for (; p != NULL && !got_int; p = p->next)
918 c++;
919
920 c -= eap->line2;
921
922 /* Skip without number of messages specified */
923 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
924 p = p->next, c--);
925 }
926
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200927 if (p == first_msg_hist)
928 {
929 s = mch_getenv((char_u *)"LANG");
930 if (s != NULL && *s != NUL)
931 msg_attr((char_u *)
932 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
933 hl_attr(HLF_T));
934 }
935
Bram Moolenaar451f8492016-04-14 17:16:22 +0200936 /* Display what was not skipped. */
937 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 if (p->msg != NULL)
939 msg_attr(p->msg, p->attr);
940
941 msg_hist_off = FALSE;
942}
943
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000944#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945/*
946 * Call this after prompting the user. This will avoid a hit-return message
947 * and a delay.
948 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000949 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100950msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 need_wait_return = FALSE;
953 emsg_on_display = FALSE;
954 cmdline_row = msg_row;
955 msg_col = 0;
956 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100957 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959#endif
960
961/*
962 * wait for the user to hit a key (normally a return)
963 * if 'redraw' is TRUE, clear and redraw the screen
964 * if 'redraw' is FALSE, just redraw the screen
965 * if 'redraw' is -1, don't redraw at all
966 */
967 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100968wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
970 int c;
971 int oldState;
972 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100974 int save_Recording;
975 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 if (redraw == TRUE)
978 must_redraw = CLEAR;
979
980 /* If using ":silent cmd", don't wait for a return. Also don't set
981 * need_wait_return to do it later. */
982 if (msg_silent != 0)
983 return;
984
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100985 /*
986 * When inside vgetc(), we can't wait for a typed character at all.
987 * With the global command (and some others) we only need one return at
988 * the end. Adjust cmdline_row to avoid the next message overwriting the
989 * last one.
990 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000991 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100993 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (no_wait_return)
995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (!exmode_active)
997 cmdline_row = msg_row;
998 return;
999 }
1000
1001 redir_off = TRUE; /* don't redirect this message */
1002 oldState = State;
1003 if (quit_more)
1004 {
1005 c = CAR; /* just pretend CR was hit */
1006 quit_more = FALSE;
1007 got_int = FALSE;
1008 }
1009 else if (exmode_active)
1010 {
1011 MSG_PUTS(" "); /* make sure the cursor is on the right line */
1012 c = CAR; /* no need for a return in ex mode */
1013 got_int = FALSE;
1014 }
1015 else
1016 {
1017 /* Make sure the hit-return prompt is on screen when 'guioptions' was
1018 * just changed. */
1019 screenalloc(FALSE);
1020
1021 State = HITRETURN;
1022#ifdef FEAT_MOUSE
1023 setmouse();
1024#endif
1025#ifdef USE_ON_FLY_SCROLL
1026 dont_scroll = TRUE; /* disallow scrolling here */
1027#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001028 cmdline_row = msg_row;
1029
Bram Moolenaarec38d692013-04-24 15:12:32 +02001030 /* Avoid the sequence that the user types ":" at the hit-return prompt
1031 * to start an Ex command, but the file-changed dialog gets in the
1032 * way. */
1033 if (need_check_timestamps)
1034 check_timestamps(FALSE);
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 hit_return_msg();
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 do
1039 {
1040 /* Remember "got_int", if it is set vgetc() probably returns a
1041 * CTRL-C, but we need to loop then. */
1042 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001043
1044 /* Don't do mappings here, we put the character back in the
1045 * typeahead buffer. */
1046 ++no_mapping;
1047 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001048
1049 /* Temporarily disable Recording. If Recording is active, the
1050 * character will be recorded later, since it will be added to the
1051 * typebuf after the loop */
1052 save_Recording = Recording;
1053 save_scriptout = scriptout;
1054 Recording = FALSE;
1055 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001057 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001059 --no_mapping;
1060 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001061 Recording = save_Recording;
1062 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#ifdef FEAT_CLIPBOARD
1065 /* Strange way to allow copying (yanking) a modeless selection at
1066 * the hit-enter prompt. Use CTRL-Y, because the same is used in
1067 * Cmdline-mode and it's harmless when there is no selection. */
1068 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1069 {
1070 clip_copy_modeless_selection(TRUE);
1071 c = K_IGNORE;
1072 }
1073#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001074
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001075 /*
1076 * Allow scrolling back in the messages.
1077 * Also accept scroll-down commands when messages fill the screen,
1078 * to avoid that typing one 'j' too many makes the messages
1079 * disappear.
1080 */
1081 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001082 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001083 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1084 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001085 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001086 if (msg_scrolled > Rows)
1087 /* scroll back to show older messages */
1088 do_more_prompt(c);
1089 else
1090 {
1091 msg_didout = FALSE;
1092 c = K_IGNORE;
1093 msg_col =
1094#ifdef FEAT_RIGHTLEFT
1095 cmdmsg_rl ? Columns - 1 :
1096#endif
1097 0;
1098 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001099 if (quit_more)
1100 {
1101 c = CAR; /* just pretend CR was hit */
1102 quit_more = FALSE;
1103 got_int = FALSE;
1104 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001105 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001106 {
1107 c = K_IGNORE;
1108 hit_return_msg();
1109 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001110 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001111 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001112 && (c == 'j' || c == 'd' || c == 'f'
1113 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001114 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 } while ((had_got_int && c == Ctrl_C)
1117 || c == K_IGNORE
1118#ifdef FEAT_GUI
1119 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1120#endif
1121#ifdef FEAT_MOUSE
1122 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1123 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1124 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001125 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 || c == K_MOUSEDOWN || c == K_MOUSEUP
1127 || (!mouse_has(MOUSE_RETURN)
1128 && mouse_row < msg_row
1129 && (c == K_LEFTMOUSE
1130 || c == K_MIDDLEMOUSE
1131 || c == K_RIGHTMOUSE
1132 || c == K_X1MOUSE
1133 || c == K_X2MOUSE))
1134#endif
1135 );
1136 ui_breakcheck();
1137#ifdef FEAT_MOUSE
1138 /*
1139 * Avoid that the mouse-up event causes visual mode to start.
1140 */
1141 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1142 || c == K_X1MOUSE || c == K_X2MOUSE)
1143 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1144 else
1145#endif
1146 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1147 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001148 /* Put the character back in the typeahead buffer. Don't use the
1149 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001150 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001152 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 redir_off = FALSE;
1156
1157 /*
1158 * If the user hits ':', '?' or '/' we get a command line from the next
1159 * line.
1160 */
1161 if (c == ':' || c == '?' || c == '/')
1162 {
1163 if (!exmode_active)
1164 cmdline_row = msg_row;
1165 skip_redraw = TRUE; /* skip redraw once */
1166 do_redraw = FALSE;
1167 }
1168
1169 /*
1170 * If the window size changed set_shellsize() will redraw the screen.
1171 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1172 * typed.
1173 */
1174 tmpState = State;
1175 State = oldState; /* restore State before set_shellsize */
1176#ifdef FEAT_MOUSE
1177 setmouse();
1178#endif
1179 msg_check();
1180
1181#if defined(UNIX) || defined(VMS)
1182 /*
1183 * When switching screens, we need to output an extra newline on exit.
1184 */
1185 if (swapping_screen() && !termcap_active)
1186 newline_on_exit = TRUE;
1187#endif
1188
1189 need_wait_return = FALSE;
1190 did_wait_return = TRUE;
1191 emsg_on_display = FALSE; /* can delete error message now */
1192 lines_left = -1; /* reset lines_left at next msg_start() */
1193 reset_last_sourcing();
1194 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1195 (Rows - cmdline_row - 1) * Columns + sc_col)
1196 {
1197 vim_free(keep_msg);
1198 keep_msg = NULL; /* don't redisplay message, it's too long */
1199 }
1200
1201 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1202 {
1203 starttermcap(); /* start termcap before redrawing */
1204 shell_resized();
1205 }
1206 else if (!skip_redraw
1207 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1208 {
1209 starttermcap(); /* start termcap before redrawing */
1210 redraw_later(VALID);
1211 }
1212}
1213
1214/*
1215 * Write the hit-return prompt.
1216 */
1217 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001218hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001220 int save_p_more = p_more;
1221
1222 p_more = FALSE; /* don't want see this message when scrolling back */
1223 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 msg_putchar('\n');
1225 if (got_int)
1226 MSG_PUTS(_("Interrupt: "));
1227
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001228 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (!msg_use_printf())
1230 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001231 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232}
1233
1234/*
1235 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1236 */
1237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001238set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
1240 vim_free(keep_msg);
1241 if (s != NULL && msg_silent == 0)
1242 keep_msg = vim_strsave(s);
1243 else
1244 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001245 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001246 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247}
1248
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001249#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1250/*
1251 * If there currently is a message being displayed, set "keep_msg" to it, so
1252 * that it will be displayed again after redraw.
1253 */
1254 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001255set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001256{
1257 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1258 && (State & NORMAL))
1259 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1260}
1261#endif
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263/*
1264 * Prepare for outputting characters in the command line.
1265 */
1266 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001267msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 int did_return = FALSE;
1270
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001271 if (!msg_silent)
1272 {
1273 vim_free(keep_msg);
1274 keep_msg = NULL; /* don't display old message now */
1275 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001276
1277#ifdef FEAT_EVAL
1278 if (need_clr_eos)
1279 {
1280 /* Halfway an ":echo" command and getting an (error) message: clear
1281 * any text from the command. */
1282 need_clr_eos = FALSE;
1283 msg_clr_eos();
1284 }
1285#endif
1286
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (!msg_scroll && full_screen) /* overwrite last message */
1288 {
1289 msg_row = cmdline_row;
1290 msg_col =
1291#ifdef FEAT_RIGHTLEFT
1292 cmdmsg_rl ? Columns - 1 :
1293#endif
1294 0;
1295 }
1296 else if (msg_didout) /* start message on next line */
1297 {
1298 msg_putchar('\n');
1299 did_return = TRUE;
1300 if (exmode_active != EXMODE_NORMAL)
1301 cmdline_row = msg_row;
1302 }
1303 if (!msg_didany || lines_left < 0)
1304 msg_starthere();
1305 if (msg_silent == 0)
1306 {
1307 msg_didout = FALSE; /* no output on current line yet */
1308 cursor_off();
1309 }
1310
1311 /* when redirecting, may need to start a new line. */
1312 if (!did_return)
1313 redir_write((char_u *)"\n", -1);
1314}
1315
1316/*
1317 * Note that the current msg position is where messages start.
1318 */
1319 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001320msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 lines_left = cmdline_row;
1323 msg_didany = FALSE;
1324}
1325
1326 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001327msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 msg_putchar_attr(c, 0);
1330}
1331
1332 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001333msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335#ifdef FEAT_MBYTE
1336 char_u buf[MB_MAXBYTES + 1];
1337#else
1338 char_u buf[4];
1339#endif
1340
1341 if (IS_SPECIAL(c))
1342 {
1343 buf[0] = K_SPECIAL;
1344 buf[1] = K_SECOND(c);
1345 buf[2] = K_THIRD(c);
1346 buf[3] = NUL;
1347 }
1348 else
1349 {
1350#ifdef FEAT_MBYTE
1351 buf[(*mb_char2bytes)(c, buf)] = NUL;
1352#else
1353 buf[0] = c;
1354 buf[1] = NUL;
1355#endif
1356 }
1357 msg_puts_attr(buf, attr);
1358}
1359
1360 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001361msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 char_u buf[20];
1364
1365 sprintf((char *)buf, "%ld", n);
1366 msg_puts(buf);
1367}
1368
1369 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001370msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 msg_home_replace_attr(fname, 0);
1373}
1374
1375#if defined(FEAT_FIND_ID) || defined(PROTO)
1376 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001377msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379 msg_home_replace_attr(fname, hl_attr(HLF_D));
1380}
1381#endif
1382
1383 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001384msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 char_u *name;
1387
1388 name = home_replace_save(NULL, fname);
1389 if (name != NULL)
1390 msg_outtrans_attr(name, attr);
1391 vim_free(name);
1392}
1393
1394/*
1395 * Output 'len' characters in 'str' (including NULs) with translation
1396 * if 'len' is -1, output upto a NUL character.
1397 * Use attributes 'attr'.
1398 * Return the number of characters it takes on the screen.
1399 */
1400 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001401msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 return msg_outtrans_attr(str, 0);
1404}
1405
1406 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001407msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1410}
1411
1412 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001413msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 return msg_outtrans_len_attr(str, len, 0);
1416}
1417
1418/*
1419 * Output one character at "p". Return pointer to the next character.
1420 * Handles multi-byte characters.
1421 */
1422 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001423msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425#ifdef FEAT_MBYTE
1426 int l;
1427
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001428 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430 msg_outtrans_len_attr(p, l, attr);
1431 return p + l;
1432 }
1433#endif
1434 msg_puts_attr(transchar_byte(*p), attr);
1435 return p + 1;
1436}
1437
1438 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001439msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 int retval = 0;
1442 char_u *str = msgstr;
1443 char_u *plain_start = msgstr;
1444 char_u *s;
1445#ifdef FEAT_MBYTE
1446 int mb_l;
1447 int c;
1448#endif
1449
1450 /* if MSG_HIST flag set, add message to history */
1451 if (attr & MSG_HIST)
1452 {
1453 add_msg_hist(str, len, attr);
1454 attr &= ~MSG_HIST;
1455 }
1456
1457#ifdef FEAT_MBYTE
1458 /* If the string starts with a composing character first draw a space on
1459 * which the composing char can be drawn. */
1460 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1461 msg_puts_attr((char_u *)" ", attr);
1462#endif
1463
1464 /*
1465 * Go over the string. Special characters are translated and printed.
1466 * Normal characters are printed several at a time.
1467 */
1468 while (--len >= 0)
1469 {
1470#ifdef FEAT_MBYTE
1471 if (enc_utf8)
1472 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001473 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001475 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 else
1477 mb_l = 1;
1478 if (has_mbyte && mb_l > 1)
1479 {
1480 c = (*mb_ptr2char)(str);
1481 if (vim_isprintc(c))
1482 /* printable multi-byte char: count the cells. */
1483 retval += (*mb_ptr2cells)(str);
1484 else
1485 {
1486 /* unprintable multi-byte char: print the printable chars so
1487 * far and the translation of the unprintable char. */
1488 if (str > plain_start)
1489 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1490 attr);
1491 plain_start = str + mb_l;
1492 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1493 retval += char2cells(c);
1494 }
1495 len -= mb_l - 1;
1496 str += mb_l;
1497 }
1498 else
1499#endif
1500 {
1501 s = transchar_byte(*str);
1502 if (s[1] != NUL)
1503 {
1504 /* unprintable char: print the printable chars so far and the
1505 * translation of the unprintable char. */
1506 if (str > plain_start)
1507 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1508 attr);
1509 plain_start = str + 1;
1510 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001511 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001513 else
1514 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 ++str;
1516 }
1517 }
1518
1519 if (str > plain_start)
1520 /* print the printable chars at the end */
1521 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1522
1523 return retval;
1524}
1525
1526#if defined(FEAT_QUICKFIX) || defined(PROTO)
1527 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001528msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 int i;
1531 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1532
1533 arg = skipwhite(arg);
1534 for (i = 5; *arg && i >= 0; --i)
1535 if (*arg++ != str[i])
1536 break;
1537 if (i < 0)
1538 {
1539 msg_putchar('\n');
1540 for (i = 0; rs[i]; ++i)
1541 msg_putchar(rs[i] - 3);
1542 }
1543}
1544#endif
1545
1546/*
1547 * Output the string 'str' upto a NUL character.
1548 * Return the number of characters it takes on the screen.
1549 *
1550 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1551 * following character and shown as <F1>, <S-Up> etc. Any other character
1552 * which is not printable shown in <> form.
1553 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1554 * If a character is displayed in one of these special ways, is also
1555 * highlighted (its highlight name is '8' in the p_hl variable).
1556 * Otherwise characters are not highlighted.
1557 * This function is used to show mappings, where we want to see how to type
1558 * the character/string -- webb
1559 */
1560 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001561msg_outtrans_special(
1562 char_u *strstart,
1563 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 char_u *str = strstart;
1566 int retval = 0;
1567 char_u *string;
1568 int attr;
1569 int len;
1570
1571 attr = hl_attr(HLF_8);
1572 while (*str != NUL)
1573 {
1574 /* Leading and trailing spaces need to be displayed in <> form. */
1575 if ((str == strstart || str[1] == NUL) && *str == ' ')
1576 {
1577 string = (char_u *)"<Space>";
1578 ++str;
1579 }
1580 else
1581 string = str2special(&str, from);
1582 len = vim_strsize(string);
1583 /* Highlight special keys */
1584 msg_puts_attr(string, len > 1
1585#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001586 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
1588 ? attr : 0);
1589 retval += len;
1590 }
1591 return retval;
1592}
1593
Bram Moolenaarbd743252010-10-20 21:23:33 +02001594#if defined(FEAT_EVAL) || defined(PROTO)
1595/*
1596 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1597 * strings, in an allocated string.
1598 */
1599 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001600str2special_save(
1601 char_u *str,
1602 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001603{
1604 garray_T ga;
1605 char_u *p = str;
1606
1607 ga_init2(&ga, 1, 40);
1608 while (*p != NUL)
1609 ga_concat(&ga, str2special(&p, is_lhs));
1610 ga_append(&ga, NUL);
1611 return (char_u *)ga.ga_data;
1612}
1613#endif
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
1616 * Return the printable string for the key codes at "*sp".
1617 * Used for translating the lhs or rhs of a mapping to printable chars.
1618 * Advances "sp" to the next code.
1619 */
1620 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001621str2special(
1622 char_u **sp,
1623 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 int c;
1626 static char_u buf[7];
1627 char_u *str = *sp;
1628 int modifiers = 0;
1629 int special = FALSE;
1630
1631#ifdef FEAT_MBYTE
1632 if (has_mbyte)
1633 {
1634 char_u *p;
1635
1636 /* Try to un-escape a multi-byte character. Return the un-escaped
1637 * string if it is a multi-byte character. */
1638 p = mb_unescape(sp);
1639 if (p != NULL)
1640 return p;
1641 }
1642#endif
1643
1644 c = *str;
1645 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1646 {
1647 if (str[1] == KS_MODIFIER)
1648 {
1649 modifiers = str[2];
1650 str += 3;
1651 c = *str;
1652 }
1653 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1654 {
1655 c = TO_SPECIAL(str[1], str[2]);
1656 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001657 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 c = NUL;
1659 }
1660 if (IS_SPECIAL(c) || modifiers) /* special key */
1661 special = TRUE;
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001665 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001667 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001668
1669 /* For multi-byte characters check for an illegal byte. */
1670 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1671 {
1672 transchar_nonprint(buf, c);
1673 *sp = str + 1;
1674 return buf;
1675 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001676 /* Since 'special' is TRUE the multi-byte character 'c' will be
1677 * processed by get_special_key_name() */
1678 c = (*mb_ptr2char)(str);
1679 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001683 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
1685 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1686 * Use <Space> only for lhs of a mapping. */
1687 if (special || char2cells(c) > 1 || (from && c == ' '))
1688 return get_special_key_name(c, modifiers);
1689 buf[0] = c;
1690 buf[1] = NUL;
1691 return buf;
1692}
1693
1694/*
1695 * Translate a key sequence into special key names.
1696 */
1697 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001698str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 char_u *s;
1701
1702 *buf = NUL;
1703 while (*sp)
1704 {
1705 s = str2special(&sp, FALSE);
1706 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1707 STRCAT(buf, s);
1708 }
1709}
1710
1711/*
1712 * print line for :print or :list command
1713 */
1714 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001715msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 int c;
1718 int col = 0;
1719 int n_extra = 0;
1720 int c_extra = 0;
1721 char_u *p_extra = NULL; /* init to make SASC shut up */
1722 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001723 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 char_u *trail = NULL;
1725#ifdef FEAT_MBYTE
1726 int l;
1727 char_u buf[MB_MAXBYTES + 1];
1728#endif
1729
Bram Moolenaardf177f62005-02-22 08:39:57 +00001730 if (curwin->w_p_list)
1731 list = TRUE;
1732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001734 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
1736 trail = s + STRLEN(s);
1737 while (trail > s && vim_iswhite(trail[-1]))
1738 --trail;
1739 }
1740
1741 /* output a space for an empty line, otherwise the line will be
1742 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001743 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 msg_putchar(' ');
1745
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001746 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001748 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 {
1750 --n_extra;
1751 if (c_extra)
1752 c = c_extra;
1753 else
1754 c = *p_extra++;
1755 }
1756#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001757 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001760 if (lcs_nbsp != NUL && list
1761 && (mb_ptr2char(s) == 160
1762 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001763 {
1764 mb_char2bytes(lcs_nbsp, buf);
1765 buf[(*mb_ptr2len)(buf)] = NUL;
1766 }
1767 else
1768 {
1769 mch_memmove(buf, s, (size_t)l);
1770 buf[l] = NUL;
1771 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001772 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 s += l;
1774 continue;
1775 }
1776#endif
1777 else
1778 {
1779 attr = 0;
1780 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001781 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
1783 /* tab amount depends on current column */
1784 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001785 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
1787 c = ' ';
1788 c_extra = ' ';
1789 }
1790 else
1791 {
1792 c = lcs_tab1;
1793 c_extra = lcs_tab2;
1794 attr = hl_attr(HLF_8);
1795 }
1796 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001797 else if (c == 160 && list && lcs_nbsp != NUL)
1798 {
1799 c = lcs_nbsp;
1800 attr = hl_attr(HLF_8);
1801 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001802 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
1804 p_extra = (char_u *)"";
1805 c_extra = NUL;
1806 n_extra = 1;
1807 c = lcs_eol;
1808 attr = hl_attr(HLF_AT);
1809 --s;
1810 }
1811 else if (c != NUL && (n = byte2cells(c)) > 1)
1812 {
1813 n_extra = n - 1;
1814 p_extra = transchar_byte(c);
1815 c_extra = NUL;
1816 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001817 /* Use special coloring to be able to distinguish <hex> from
1818 * the same in plain text. */
1819 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 else if (c == ' ' && trail != NULL && s > trail)
1822 {
1823 c = lcs_trail;
1824 attr = hl_attr(HLF_8);
1825 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001826 else if (c == ' ' && list && lcs_space != NUL)
1827 {
1828 c = lcs_space;
1829 attr = hl_attr(HLF_8);
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832
1833 if (c == NUL)
1834 break;
1835
1836 msg_putchar_attr(c, attr);
1837 col++;
1838 }
1839 msg_clr_eos();
1840}
1841
1842#ifdef FEAT_MBYTE
1843/*
1844 * Use screen_puts() to output one multi-byte character.
1845 * Return the pointer "s" advanced to the next character.
1846 */
1847 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001848screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 int cw;
1851
1852 msg_didout = TRUE; /* remember that line is not empty */
1853 cw = (*mb_ptr2cells)(s);
1854 if (cw > 1 && (
1855#ifdef FEAT_RIGHTLEFT
1856 cmdmsg_rl ? msg_col <= 1 :
1857#endif
1858 msg_col == Columns - 1))
1859 {
1860 /* Doesn't fit, print a highlighted '>' to fill it up. */
1861 msg_screen_putchar('>', hl_attr(HLF_AT));
1862 return s;
1863 }
1864
1865 screen_puts_len(s, l, msg_row, msg_col, attr);
1866#ifdef FEAT_RIGHTLEFT
1867 if (cmdmsg_rl)
1868 {
1869 msg_col -= cw;
1870 if (msg_col == 0)
1871 {
1872 msg_col = Columns;
1873 ++msg_row;
1874 }
1875 }
1876 else
1877#endif
1878 {
1879 msg_col += cw;
1880 if (msg_col >= Columns)
1881 {
1882 msg_col = 0;
1883 ++msg_row;
1884 }
1885 }
1886 return s + l;
1887}
1888#endif
1889
1890/*
1891 * Output a string to the screen at position msg_row, msg_col.
1892 * Update msg_row and msg_col for the next message.
1893 */
1894 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001895msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001897 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898}
1899
1900 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001901msg_puts_title(
1902 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 msg_puts_attr(s, hl_attr(HLF_T));
1905}
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
1908 * Show a message in such a way that it always fits in the line. Cut out a
1909 * part in the middle and replace it with "..." when necessary.
1910 * Does not handle multi-byte characters!
1911 */
1912 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001913msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001915 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916}
1917
1918 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001919msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
1921 int slen = len;
1922 int room;
1923
1924 room = Columns - msg_col;
1925 if (len > room && room >= 20)
1926 {
1927 slen = (room - 3) / 2;
1928 msg_outtrans_len_attr(longstr, slen, attr);
1929 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1930 }
1931 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1932}
1933
1934/*
1935 * Basic function for writing a message with highlight attributes.
1936 */
1937 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001938msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939{
1940 msg_puts_attr_len(s, -1, attr);
1941}
1942
1943/*
1944 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1945 * When "maxlen" is -1 there is no maximum length.
1946 * When "maxlen" is >= 0 the message is not put in the history.
1947 */
1948 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001949msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 /*
1952 * If redirection is on, also write to the redirection file.
1953 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001954 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956 /*
1957 * Don't print anything when using ":silent cmd".
1958 */
1959 if (msg_silent != 0)
1960 return;
1961
1962 /* if MSG_HIST flag set, add message to history */
1963 if ((attr & MSG_HIST) && maxlen < 0)
1964 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001965 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 attr &= ~MSG_HIST;
1967 }
1968
1969 /*
1970 * When writing something to the screen after it has scrolled, requires a
1971 * wait-return prompt later. Needed when scrolling, resetting
1972 * need_wait_return after some prompt, and then outputting something
1973 * without scrolling
1974 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001975 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 need_wait_return = TRUE;
1977 msg_didany = TRUE; /* remember that something was outputted */
1978
1979 /*
1980 * If there is no valid screen, use fprintf so we can see error messages.
1981 * If termcap is not active, we may be writing in an alternate console
1982 * window, cursor positioning may not work correctly (window size may be
1983 * different, e.g. for Win32 console) or we just don't know where the
1984 * cursor is.
1985 */
1986 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001987 msg_puts_printf(str, maxlen);
1988 else
1989 msg_puts_display(str, maxlen, attr, FALSE);
1990}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992/*
1993 * The display part of msg_puts_attr_len().
1994 * May be called recursively to display scroll-back text.
1995 */
1996 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001997msg_puts_display(
1998 char_u *str,
1999 int maxlen,
2000 int attr,
2001 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002002{
2003 char_u *s = str;
2004 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
2005 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
2006#ifdef FEAT_MBYTE
2007 int l;
2008 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002010 char_u *sb_str = str;
2011 int sb_col = msg_col;
2012 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002013 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
2015 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002016 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019 * We are at the end of the screen line when:
2020 * - When outputting a newline.
2021 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002023 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#ifdef FEAT_RIGHTLEFT
2025 cmdmsg_rl
2026 ? (
2027 msg_col <= 1
2028 || (*s == TAB && msg_col <= 7)
2029# ifdef FEAT_MBYTE
2030 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
2031# endif
2032 )
2033 :
2034#endif
2035 (msg_col + t_col >= Columns - 1
2036 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
2037# ifdef FEAT_MBYTE
2038 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2039 && msg_col + t_col >= Columns - 2)
2040# endif
2041 ))))
2042 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002043 /*
2044 * The screen is scrolled up when at the last row (some terminals
2045 * scroll automatically, some don't. To avoid problems we scroll
2046 * ourselves).
2047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002050 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002052 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if (msg_no_more && lines_left == 0)
2054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002056 /* Scroll the screen up one line. */
2057 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 msg_row = Rows - 2;
2060 if (msg_col >= Columns) /* can happen after screen resize */
2061 msg_col = Columns - 1;
2062
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002063 /* Display char in last column before showing more-prompt. */
2064 if (*s >= ' '
2065#ifdef FEAT_RIGHTLEFT
2066 && !cmdmsg_rl
2067#endif
2068 )
2069 {
2070#ifdef FEAT_MBYTE
2071 if (has_mbyte)
2072 {
2073 if (enc_utf8 && maxlen >= 0)
2074 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002075 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002076 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002077 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002078 s = screen_puts_mbyte(s, l, attr);
2079 }
2080 else
2081#endif
2082 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002083 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002084 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002085 else
2086 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002087
2088 if (p_more)
2089 /* store text for scrolling back */
2090 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2091
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002092 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002093 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 if (must_redraw < VALID)
2095 must_redraw = VALID;
2096 redraw_cmdline = TRUE;
2097 if (cmdline_row > 0 && !exmode_active)
2098 --cmdline_row;
2099
2100 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002101 * If screen is completely filled and 'more' is set then wait
2102 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002104 if (lines_left > 0)
2105 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002106 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 && !msg_no_more && !exmode_active)
2108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002110 if (do_more_prompt(NUL))
2111 s = confirm_msg_tail;
2112#else
2113 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#endif
2115 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002116 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002118
2119 /* When we displayed a char in last column need to check if there
2120 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002121 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002122 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002125 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 || msg_col + t_col >= Columns
2127#ifdef FEAT_MBYTE
2128 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2129 && msg_col + t_col >= Columns - 1)
2130#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002131 ;
2132 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2133 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002135 t_puts(&t_col, t_s, s, attr);
2136
2137 if (wrap && p_more && !recurse)
2138 /* store text for scrolling back */
2139 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 if (*s == '\n') /* go to next line */
2142 {
2143 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002144#ifdef FEAT_RIGHTLEFT
2145 if (cmdmsg_rl)
2146 msg_col = Columns - 1;
2147 else
2148#endif
2149 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if (++msg_row >= Rows) /* safety check */
2151 msg_row = Rows - 1;
2152 }
2153 else if (*s == '\r') /* go to column 0 */
2154 {
2155 msg_col = 0;
2156 }
2157 else if (*s == '\b') /* go to previous char */
2158 {
2159 if (msg_col)
2160 --msg_col;
2161 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002162 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 do
2165 msg_screen_putchar(' ', attr);
2166 while (msg_col & 7);
2167 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002168 else if (*s == BELL) /* beep (from ":sh") */
2169 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 else
2171 {
2172#ifdef FEAT_MBYTE
2173 if (has_mbyte)
2174 {
2175 cw = (*mb_ptr2cells)(s);
2176 if (enc_utf8 && maxlen >= 0)
2177 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002178 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002180 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
2182 else
2183 {
2184 cw = 1;
2185 l = 1;
2186 }
2187#endif
2188 /* When drawing from right to left or when a double-wide character
2189 * doesn't fit, draw a single character here. Otherwise collect
2190 * characters and draw them all at once later. */
2191#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2192 if (
2193# ifdef FEAT_RIGHTLEFT
2194 cmdmsg_rl
2195# ifdef FEAT_MBYTE
2196 ||
2197# endif
2198# endif
2199# ifdef FEAT_MBYTE
2200 (cw > 1 && msg_col + t_col >= Columns - 1)
2201# endif
2202 )
2203 {
2204# ifdef FEAT_MBYTE
2205 if (l > 1)
2206 s = screen_puts_mbyte(s, l, attr) - 1;
2207 else
2208# endif
2209 msg_screen_putchar(*s, attr);
2210 }
2211 else
2212#endif
2213 {
2214 /* postpone this character until later */
2215 if (t_col == 0)
2216 t_s = s;
2217#ifdef FEAT_MBYTE
2218 t_col += cw;
2219 s += l - 1;
2220#else
2221 ++t_col;
2222#endif
2223 }
2224 }
2225 ++s;
2226 }
2227
2228 /* output any postponed text */
2229 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002230 t_puts(&t_col, t_s, s, attr);
2231 if (p_more && !recurse)
2232 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234 msg_check();
2235}
2236
2237/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002238 * Return TRUE when ":filter pattern" was used and "msg" does not match
2239 * "pattern".
2240 */
2241 int
2242message_filtered(char_u *msg)
2243{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002244 int match;
2245
2246 if (cmdmod.filter_regmatch.regprog == NULL)
2247 return FALSE;
2248 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2249 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002250}
2251
2252/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002253 * Scroll the screen up one line for displaying the next message line.
2254 */
2255 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002256msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002257{
2258#ifdef FEAT_GUI
2259 /* Remove the cursor before scrolling, ScreenLines[] is going
2260 * to become invalid. */
2261 if (gui.in_use)
2262 gui_undraw_cursor();
2263#endif
2264 /* scrolling up always works */
2265 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2266
2267 if (!can_clear((char_u *)" "))
2268 {
2269 /* Scrolling up doesn't result in the right background. Set the
2270 * background here. It's not efficient, but avoids that we have to do
2271 * it all over the code. */
2272 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2273
2274 /* Also clear the last char of the last but one line if it was not
2275 * cleared before to avoid a scroll-up. */
2276 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2277 screen_fill((int)Rows - 2, (int)Rows - 1,
2278 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2279 }
2280}
2281
2282/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002283 * Increment "msg_scrolled".
2284 */
2285 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002286inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002287{
2288#ifdef FEAT_EVAL
2289 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2290 {
2291 char_u *p = sourcing_name;
2292 char_u *tofree = NULL;
2293 int len;
2294
2295 /* v:scrollstart is empty, set it to the script/function name and line
2296 * number */
2297 if (p == NULL)
2298 p = (char_u *)_("Unknown");
2299 else
2300 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002301 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002302 tofree = alloc(len);
2303 if (tofree != NULL)
2304 {
2305 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2306 p, (long)sourcing_lnum);
2307 p = tofree;
2308 }
2309 }
2310 set_vim_var_string(VV_SCROLLSTART, p, -1);
2311 vim_free(tofree);
2312 }
2313#endif
2314 ++msg_scrolled;
2315}
2316
2317/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002318 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2319 * store the displayed text and remember where screen lines start.
2320 */
2321typedef struct msgchunk_S msgchunk_T;
2322struct msgchunk_S
2323{
2324 msgchunk_T *sb_next;
2325 msgchunk_T *sb_prev;
2326 char sb_eol; /* TRUE when line ends after this text */
2327 int sb_msg_col; /* column in which text starts */
2328 int sb_attr; /* text attributes */
2329 char_u sb_text[1]; /* text to be displayed, actually longer */
2330};
2331
2332static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2333
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002334static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2335static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002336
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002337static int do_clear_sb_text = FALSE; /* clear text on next msg */
2338
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002339/*
2340 * Store part of a printed message for displaying when scrolling back.
2341 */
2342 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002343store_sb_text(
2344 char_u **sb_str, /* start of string */
2345 char_u *s, /* just after string */
2346 int attr,
2347 int *sb_col,
2348 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002349{
2350 msgchunk_T *mp;
2351
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002352 if (do_clear_sb_text)
2353 {
2354 clear_sb_text();
2355 do_clear_sb_text = FALSE;
2356 }
2357
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002358 if (s > *sb_str)
2359 {
2360 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2361 if (mp != NULL)
2362 {
2363 mp->sb_eol = finish;
2364 mp->sb_msg_col = *sb_col;
2365 mp->sb_attr = attr;
2366 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2367
2368 if (last_msgchunk == NULL)
2369 {
2370 last_msgchunk = mp;
2371 mp->sb_prev = NULL;
2372 }
2373 else
2374 {
2375 mp->sb_prev = last_msgchunk;
2376 last_msgchunk->sb_next = mp;
2377 last_msgchunk = mp;
2378 }
2379 mp->sb_next = NULL;
2380 }
2381 }
2382 else if (finish && last_msgchunk != NULL)
2383 last_msgchunk->sb_eol = TRUE;
2384
2385 *sb_str = s;
2386 *sb_col = 0;
2387}
2388
2389/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002390 * Finished showing messages, clear the scroll-back text on the next message.
2391 */
2392 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002393may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002394{
2395 do_clear_sb_text = TRUE;
2396}
2397
2398/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002399 * Clear any text remembered for scrolling back.
2400 * Called when redrawing the screen.
2401 */
2402 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002403clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002404{
2405 msgchunk_T *mp;
2406
2407 while (last_msgchunk != NULL)
2408 {
2409 mp = last_msgchunk->sb_prev;
2410 vim_free(last_msgchunk);
2411 last_msgchunk = mp;
2412 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002413}
2414
2415/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002416 * "g<" command.
2417 */
2418 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002419show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002420{
2421 msgchunk_T *mp;
2422
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002423 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002424 * weird, typing a command without output results in one line. */
2425 mp = msg_sb_start(last_msgchunk);
2426 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002427 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002428 else
2429 {
2430 do_more_prompt('G');
2431 wait_return(FALSE);
2432 }
2433}
2434
2435/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436 * Move to the start of screen line in already displayed text.
2437 */
2438 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002439msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002440{
2441 msgchunk_T *mp = mps;
2442
2443 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2444 mp = mp->sb_prev;
2445 return mp;
2446}
2447
2448/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002449 * Mark the last message chunk as finishing the line.
2450 */
2451 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002452msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002453{
2454 if (last_msgchunk != NULL)
2455 last_msgchunk->sb_eol = TRUE;
2456}
2457
2458/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002459 * Display a screen line from previously displayed text at row "row".
2460 * Returns a pointer to the text for the next line (can be NULL).
2461 */
2462 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002463disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464{
2465 msgchunk_T *mp = smp;
2466 char_u *p;
2467
2468 for (;;)
2469 {
2470 msg_row = row;
2471 msg_col = mp->sb_msg_col;
2472 p = mp->sb_text;
2473 if (*p == '\n') /* don't display the line break */
2474 ++p;
2475 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2476 if (mp->sb_eol || mp->sb_next == NULL)
2477 break;
2478 mp = mp->sb_next;
2479 }
2480 return mp->sb_next;
2481}
2482
2483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 * Output any postponed text for msg_puts_attr_len().
2485 */
2486 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002487t_puts(
2488 int *t_col,
2489 char_u *t_s,
2490 char_u *s,
2491 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 /* output postponed text */
2494 msg_didout = TRUE; /* remember that line is not empty */
2495 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002496 msg_col += *t_col;
2497 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498#ifdef FEAT_MBYTE
2499 /* If the string starts with a composing character don't increment the
2500 * column position for it. */
2501 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2502 --msg_col;
2503#endif
2504 if (msg_col >= Columns)
2505 {
2506 msg_col = 0;
2507 ++msg_row;
2508 }
2509}
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511/*
2512 * Returns TRUE when messages should be printed with mch_errmsg().
2513 * This is used when there is no valid screen, so we can see error messages.
2514 * If termcap is not active, we may be writing in an alternate console
2515 * window, cursor positioning may not work correctly (window size may be
2516 * different, e.g. for Win32 console) or we just don't know where the
2517 * cursor is.
2518 */
2519 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002520msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
2522 return (!msg_check_screen()
2523#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2524 || !termcap_active
2525#endif
2526 || (swapping_screen() && !termcap_active)
2527 );
2528}
2529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002530/*
2531 * Print a message when there is no valid screen.
2532 */
2533 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002534msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002535{
2536 char_u *s = str;
2537 char_u buf[4];
2538 char_u *p;
2539
2540#ifdef WIN3264
2541 if (!(silent_mode && p_verbose == 0))
2542 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2543#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002544 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002545 {
2546 if (!(silent_mode && p_verbose == 0))
2547 {
2548 /* NL --> CR NL translation (for Unix, not for "--version") */
2549 /* NL --> CR translation (for Mac) */
2550 p = &buf[0];
2551 if (*s == '\n' && !info_message)
2552 *p++ = '\r';
2553#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2554 else
2555#endif
2556 *p++ = *s;
2557 *p = '\0';
2558 if (info_message) /* informative message, not an error */
2559 mch_msg((char *)buf);
2560 else
2561 mch_errmsg((char *)buf);
2562 }
2563
2564 /* primitive way to compute the current column */
2565#ifdef FEAT_RIGHTLEFT
2566 if (cmdmsg_rl)
2567 {
2568 if (*s == '\r' || *s == '\n')
2569 msg_col = Columns - 1;
2570 else
2571 --msg_col;
2572 }
2573 else
2574#endif
2575 {
2576 if (*s == '\r' || *s == '\n')
2577 msg_col = 0;
2578 else
2579 ++msg_col;
2580 }
2581 ++s;
2582 }
2583 msg_didout = TRUE; /* assume that line is not empty */
2584
2585#ifdef WIN3264
2586 if (!(silent_mode && p_verbose == 0))
2587 mch_settmode(TMODE_RAW);
2588#endif
2589}
2590
2591/*
2592 * Show the more-prompt and handle the user response.
2593 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002594 * When at hit-enter prompt "typed_char" is the already typed character,
2595 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2597 */
2598 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002599do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002600{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002601 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002602 int used_typed_char = typed_char;
2603 int oldState = State;
2604 int c;
2605#ifdef FEAT_CON_DIALOG
2606 int retval = FALSE;
2607#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002608 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002609 msgchunk_T *mp_last = NULL;
2610 msgchunk_T *mp;
2611 int i;
2612
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002613 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002614 * that case don't show another prompt. Also when at the hit-Enter prompt
2615 * and nothing was typed. */
2616 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002617 return FALSE;
2618 entered = TRUE;
2619
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002620 if (typed_char == 'G')
2621 {
2622 /* "g<": Find first line on the last page. */
2623 mp_last = msg_sb_start(last_msgchunk);
2624 for (i = 0; i < Rows - 2 && mp_last != NULL
2625 && mp_last->sb_prev != NULL; ++i)
2626 mp_last = msg_sb_start(mp_last->sb_prev);
2627 }
2628
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002629 State = ASKMORE;
2630#ifdef FEAT_MOUSE
2631 setmouse();
2632#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002633 if (typed_char == NUL)
2634 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002635 for (;;)
2636 {
2637 /*
2638 * Get a typed character directly from the user.
2639 */
2640 if (used_typed_char != NUL)
2641 {
2642 c = used_typed_char; /* was typed at hit-enter prompt */
2643 used_typed_char = NUL;
2644 }
2645 else
2646 c = get_keystroke();
2647
2648#if defined(FEAT_MENU) && defined(FEAT_GUI)
2649 if (c == K_MENU)
2650 {
2651 int idx = get_menu_index(current_menu, ASKMORE);
2652
2653 /* Used a menu. If it starts with CTRL-Y, it must
2654 * be a "Copy" for the clipboard. Otherwise
2655 * assume that we end */
2656 if (idx == MENU_INDEX_INVALID)
2657 continue;
2658 c = *current_menu->strings[idx];
2659 if (c != NUL && current_menu->strings[idx][1] != NUL)
2660 ins_typebuf(current_menu->strings[idx] + 1,
2661 current_menu->noremap[idx], 0, TRUE,
2662 current_menu->silent[idx]);
2663 }
2664#endif
2665
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002666 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002667 switch (c)
2668 {
2669 case BS: /* scroll one line back */
2670 case K_BS:
2671 case 'k':
2672 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002673 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002674 break;
2675
2676 case CAR: /* one extra line */
2677 case NL:
2678 case 'j':
2679 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002680 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002681 break;
2682
2683 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002684 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002685 break;
2686
2687 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002688 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002689 break;
2690
2691 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002692 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002693 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002694 break;
2695
2696 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002697 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002698 case K_PAGEDOWN:
2699 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002700 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002701 break;
2702
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002703 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002704 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002705 break;
2706
2707 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002708 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002709 lines_left = 999999;
2710 break;
2711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002712 case ':': /* start new command line */
2713#ifdef FEAT_CON_DIALOG
2714 if (!confirm_msg_used)
2715#endif
2716 {
2717 /* Since got_int is set all typeahead will be flushed, but we
2718 * want to keep this ':', remember that in a special way. */
2719 typeahead_noflush(':');
2720 cmdline_row = Rows - 1; /* put ':' on this line */
2721 skip_redraw = TRUE; /* skip redraw once */
2722 need_wait_return = FALSE; /* don't wait in main() */
2723 }
2724 /*FALLTHROUGH*/
2725 case 'q': /* quit */
2726 case Ctrl_C:
2727 case ESC:
2728#ifdef FEAT_CON_DIALOG
2729 if (confirm_msg_used)
2730 {
2731 /* Jump to the choices of the dialog. */
2732 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733 }
2734 else
2735#endif
2736 {
2737 got_int = TRUE;
2738 quit_more = TRUE;
2739 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002740 /* When there is some more output (wrapping line) display that
2741 * without another prompt. */
2742 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 break;
2744
2745#ifdef FEAT_CLIPBOARD
2746 case Ctrl_Y:
2747 /* Strange way to allow copying (yanking) a modeless
2748 * selection at the more prompt. Use CTRL-Y,
2749 * because the same is used in Cmdline-mode and at the
2750 * hit-enter prompt. However, scrolling one line up
2751 * might be expected... */
2752 if (clip_star.state == SELECT_DONE)
2753 clip_copy_modeless_selection(TRUE);
2754 continue;
2755#endif
2756 default: /* no valid response */
2757 msg_moremsg(TRUE);
2758 continue;
2759 }
2760
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002761 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002762 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002763 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764 {
2765 /* go to start of last line */
2766 if (mp_last == NULL)
2767 mp = msg_sb_start(last_msgchunk);
2768 else if (mp_last->sb_prev != NULL)
2769 mp = msg_sb_start(mp_last->sb_prev);
2770 else
2771 mp = NULL;
2772
2773 /* go to start of line at top of the screen */
2774 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2775 ++i)
2776 mp = msg_sb_start(mp->sb_prev);
2777
2778 if (mp != NULL && mp->sb_prev != NULL)
2779 {
2780 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002781 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782 {
2783 if (mp == NULL || mp->sb_prev == NULL)
2784 break;
2785 mp = msg_sb_start(mp->sb_prev);
2786 if (mp_last == NULL)
2787 mp_last = msg_sb_start(last_msgchunk);
2788 else
2789 mp_last = msg_sb_start(mp_last->sb_prev);
2790 }
2791
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002792 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 (int)Rows, NULL) == OK)
2794 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002795 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 (void)disp_sb_line(0, mp);
2797 }
2798 else
2799 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002800 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002801 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002802 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2803 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002804 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002805 ++msg_scrolled;
2806 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002808 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809 }
2810 }
2811 else
2812 {
2813 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002814 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002815 {
2816 /* scroll up, display line at bottom */
2817 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002818 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2820 (int)Columns, ' ', ' ', 0);
2821 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002822 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002823 }
2824 }
2825
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002826 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 {
2828 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002829 screen_fill((int)Rows - 1, (int)Rows, 0,
2830 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002831 msg_moremsg(FALSE);
2832 continue;
2833 }
2834
2835 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002836 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002837 }
2838
2839 break;
2840 }
2841
2842 /* clear the --more-- message */
2843 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2844 State = oldState;
2845#ifdef FEAT_MOUSE
2846 setmouse();
2847#endif
2848 if (quit_more)
2849 {
2850 msg_row = Rows - 1;
2851 msg_col = 0;
2852 }
2853#ifdef FEAT_RIGHTLEFT
2854 else if (cmdmsg_rl)
2855 msg_col = Columns - 1;
2856#endif
2857
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002858 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002859#ifdef FEAT_CON_DIALOG
2860 return retval;
2861#else
2862 return FALSE;
2863#endif
2864}
2865
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2867
2868#ifdef mch_errmsg
2869# undef mch_errmsg
2870#endif
2871#ifdef mch_msg
2872# undef mch_msg
2873#endif
2874
2875/*
2876 * Give an error message. To be used when the screen hasn't been initialized
2877 * yet. When stderr can't be used, collect error messages until the GUI has
2878 * started and they can be displayed in a message box.
2879 */
2880 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002881mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 int len;
2884
2885#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2886 /* On Unix use stderr if it's a tty.
2887 * When not going to start the GUI also use stderr.
2888 * On Mac, when started from Finder, stderr is the console. */
2889 if (
2890# ifdef UNIX
2891# ifdef MACOS_X_UNIX
2892 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2893# else
2894 isatty(2)
2895# endif
2896# ifdef FEAT_GUI
2897 ||
2898# endif
2899# endif
2900# ifdef FEAT_GUI
2901 !(gui.in_use || gui.starting)
2902# endif
2903 )
2904 {
2905 fprintf(stderr, "%s", str);
2906 return;
2907 }
2908#endif
2909
2910 /* avoid a delay for a message that isn't there */
2911 emsg_on_display = FALSE;
2912
2913 len = (int)STRLEN(str) + 1;
2914 if (error_ga.ga_growsize == 0)
2915 {
2916 error_ga.ga_growsize = 80;
2917 error_ga.ga_itemsize = 1;
2918 }
2919 if (ga_grow(&error_ga, len) == OK)
2920 {
2921 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2922 (char_u *)str, len);
2923#ifdef UNIX
2924 /* remove CR characters, they are displayed */
2925 {
2926 char_u *p;
2927
2928 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2929 for (;;)
2930 {
2931 p = vim_strchr(p, '\r');
2932 if (p == NULL)
2933 break;
2934 *p = ' ';
2935 }
2936 }
2937#endif
2938 --len; /* don't count the NUL at the end */
2939 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941}
2942
2943/*
2944 * Give a message. To be used when the screen hasn't been initialized yet.
2945 * When there is no tty, collect messages until the GUI has started and they
2946 * can be displayed in a message box.
2947 */
2948 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002949mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2952 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2953 * uses mch_errmsg() when started from the desktop.
2954 * When not going to start the GUI also use stdout.
2955 * On Mac, when started from Finder, stderr is the console. */
2956 if (
2957# ifdef UNIX
2958# ifdef MACOS_X_UNIX
2959 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2960# else
2961 isatty(2)
2962# endif
2963# ifdef FEAT_GUI
2964 ||
2965# endif
2966# endif
2967# ifdef FEAT_GUI
2968 !(gui.in_use || gui.starting)
2969# endif
2970 )
2971 {
2972 printf("%s", str);
2973 return;
2974 }
2975# endif
2976 mch_errmsg(str);
2977}
2978#endif /* USE_MCH_ERRMSG */
2979
2980/*
2981 * Put a character on the screen at the current message position and advance
2982 * to the next position. Only for printable ASCII!
2983 */
2984 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002985msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 msg_didout = TRUE; /* remember that line is not empty */
2988 screen_putchar(c, msg_row, msg_col, attr);
2989#ifdef FEAT_RIGHTLEFT
2990 if (cmdmsg_rl)
2991 {
2992 if (--msg_col == 0)
2993 {
2994 msg_col = Columns;
2995 ++msg_row;
2996 }
2997 }
2998 else
2999#endif
3000 {
3001 if (++msg_col >= Columns)
3002 {
3003 msg_col = 0;
3004 ++msg_row;
3005 }
3006 }
3007}
3008
3009 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003010msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003012 int attr;
3013 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003016 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003018 screen_puts((char_u *)
3019 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3020 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021}
3022
3023/*
3024 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3025 * exmode_active.
3026 */
3027 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003028repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 if (State == ASKMORE)
3031 {
3032 msg_moremsg(TRUE); /* display --more-- message again */
3033 msg_row = Rows - 1;
3034 }
3035#ifdef FEAT_CON_DIALOG
3036 else if (State == CONFIRM)
3037 {
3038 display_confirm_msg(); /* display ":confirm" message again */
3039 msg_row = Rows - 1;
3040 }
3041#endif
3042 else if (State == EXTERNCMD)
3043 {
3044 windgoto(msg_row, msg_col); /* put cursor back */
3045 }
3046 else if (State == HITRETURN || State == SETWSIZE)
3047 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003048 if (msg_row == Rows - 1)
3049 {
3050 /* Avoid drawing the "hit-enter" prompt below the previous one,
3051 * overwrite it. Esp. useful when regaining focus and a
3052 * FocusGained autocmd exists but didn't draw anything. */
3053 msg_didout = FALSE;
3054 msg_col = 0;
3055 msg_clr_eos();
3056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 hit_return_msg();
3058 msg_row = Rows - 1;
3059 }
3060}
3061
3062/*
3063 * msg_check_screen - check if the screen is initialized.
3064 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3065 * While starting the GUI the terminal codes will be set for the GUI, but the
3066 * output goes to the terminal. Don't use the terminal codes then.
3067 */
3068 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003069msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 if (!full_screen || !screen_valid(FALSE))
3072 return FALSE;
3073
3074 if (msg_row >= Rows)
3075 msg_row = Rows - 1;
3076 if (msg_col >= Columns)
3077 msg_col = Columns - 1;
3078 return TRUE;
3079}
3080
3081/*
3082 * Clear from current message position to end of screen.
3083 * Skip this when ":silent" was used, no need to clear for redirection.
3084 */
3085 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003086msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 if (msg_silent == 0)
3089 msg_clr_eos_force();
3090}
3091
3092/*
3093 * Clear from current message position to end of screen.
3094 * Note: msg_col is not updated, so we remember the end of the message
3095 * for msg_check().
3096 */
3097 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003098msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 if (msg_use_printf())
3101 {
3102 if (full_screen) /* only when termcap codes are valid */
3103 {
3104 if (*T_CD)
3105 out_str(T_CD); /* clear to end of display */
3106 else if (*T_CE)
3107 out_str(T_CE); /* clear to end of line */
3108 }
3109 }
3110 else
3111 {
3112#ifdef FEAT_RIGHTLEFT
3113 if (cmdmsg_rl)
3114 {
3115 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3116 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3117 }
3118 else
3119#endif
3120 {
3121 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3122 ' ', ' ', 0);
3123 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3124 }
3125 }
3126}
3127
3128/*
3129 * Clear the command line.
3130 */
3131 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003132msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 msg_row = cmdline_row;
3135 msg_col = 0;
3136 msg_clr_eos_force();
3137}
3138
3139/*
3140 * end putting a message on the screen
3141 * call wait_return if the message does not fit in the available space
3142 * return TRUE if wait_return not called.
3143 */
3144 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003145msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003148 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * or the ruler option is set and we run into it,
3150 * we have to redraw the window.
3151 * Do not do this if we are abandoning the file or editing the command line.
3152 */
3153 if (!exiting && need_wait_return && !(State & CMDLINE))
3154 {
3155 wait_return(FALSE);
3156 return FALSE;
3157 }
3158 out_flush();
3159 return TRUE;
3160}
3161
3162/*
3163 * If the written message runs into the shown command or ruler, we have to
3164 * wait for hit-return and redraw the window later.
3165 */
3166 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003167msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 if (msg_row == Rows - 1 && msg_col >= sc_col)
3170 {
3171 need_wait_return = TRUE;
3172 redraw_cmdline = TRUE;
3173 }
3174}
3175
3176/*
3177 * May write a string to the redirection file.
3178 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3179 */
3180 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003181redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 char_u *s = str;
3184 static int cur_col = 0;
3185
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003186 /* Don't do anything for displaying prompts and the like. */
3187 if (redir_off)
3188 return;
3189
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003190 /* If 'verbosefile' is set prepare for writing in that file. */
3191 if (*p_vfile != NUL && verbose_fd == NULL)
3192 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003193
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003194 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
3196 /* If the string doesn't start with CR or NL, go to msg_col */
3197 if (*s != '\n' && *s != '\r')
3198 {
3199 while (cur_col < msg_col)
3200 {
3201#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003202 if (redir_execute)
3203 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003204 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003206 else if (redir_vname)
3207 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003210 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003212 if (verbose_fd != NULL)
3213 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 ++cur_col;
3215 }
3216 }
3217
3218#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003219 if (redir_execute)
3220 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003221 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003223 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003224 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#endif
3226
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003227 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3229 {
3230#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003231 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003233 if (redir_fd != NULL)
3234 putc(*s, redir_fd);
3235 if (verbose_fd != NULL)
3236 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (*s == '\r' || *s == '\n')
3238 cur_col = 0;
3239 else if (*s == '\t')
3240 cur_col += (8 - cur_col % 8);
3241 else
3242 ++cur_col;
3243 ++s;
3244 }
3245
3246 if (msg_silent != 0) /* should update msg_col */
3247 msg_col = cur_col;
3248 }
3249}
3250
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003251 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003252redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003253{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003254 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003255#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003256 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003257#endif
3258 ;
3259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003262 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003263 * Must always be called paired with verbose_leave()!
3264 */
3265 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003266verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003267{
3268 if (*p_vfile != NUL)
3269 ++msg_silent;
3270}
3271
3272/*
3273 * After giving verbose message.
3274 * Must always be called paired with verbose_enter()!
3275 */
3276 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003277verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003278{
3279 if (*p_vfile != NUL)
3280 if (--msg_silent < 0)
3281 msg_silent = 0;
3282}
3283
3284/*
3285 * Like verbose_enter() and set msg_scroll when displaying the message.
3286 */
3287 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003288verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003289{
3290 if (*p_vfile != NUL)
3291 ++msg_silent;
3292 else
3293 /* always scroll up, don't overwrite */
3294 msg_scroll = TRUE;
3295}
3296
3297/*
3298 * Like verbose_leave() and set cmdline_row when displaying the message.
3299 */
3300 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003301verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003302{
3303 if (*p_vfile != NUL)
3304 {
3305 if (--msg_silent < 0)
3306 msg_silent = 0;
3307 }
3308 else
3309 cmdline_row = msg_row;
3310}
3311
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003312/*
3313 * Called when 'verbosefile' is set: stop writing to the file.
3314 */
3315 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003316verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003317{
3318 if (verbose_fd != NULL)
3319 {
3320 fclose(verbose_fd);
3321 verbose_fd = NULL;
3322 }
3323 verbose_did_open = FALSE;
3324}
3325
3326/*
3327 * Open the file 'verbosefile'.
3328 * Return FAIL or OK.
3329 */
3330 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003331verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003332{
3333 if (verbose_fd == NULL && !verbose_did_open)
3334 {
3335 /* Only give the error message once. */
3336 verbose_did_open = TRUE;
3337
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003338 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003339 if (verbose_fd == NULL)
3340 {
3341 EMSG2(_(e_notopen), p_vfile);
3342 return FAIL;
3343 }
3344 }
3345 return OK;
3346}
3347
3348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 * Give a warning message (for searching).
3350 * Use 'w' highlighting and may repeat the message after redrawing
3351 */
3352 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003353give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 /* Don't do this for ":silent". */
3356 if (msg_silent != 0)
3357 return;
3358
3359 /* Don't want a hit-enter prompt here. */
3360 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#ifdef FEAT_EVAL
3363 set_vim_var_string(VV_WARNINGMSG, message, -1);
3364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 vim_free(keep_msg);
3366 keep_msg = NULL;
3367 if (hl)
3368 keep_msg_attr = hl_attr(HLF_W);
3369 else
3370 keep_msg_attr = 0;
3371 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003372 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 msg_didout = FALSE; /* overwrite this message */
3374 msg_nowait = TRUE; /* don't wait for this message */
3375 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 --no_wait_return;
3378}
3379
3380/*
3381 * Advance msg cursor to column "col".
3382 */
3383 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003384msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 if (msg_silent != 0) /* nothing to advance to */
3387 {
3388 msg_col = col; /* for redirection, may fill it up later */
3389 return;
3390 }
3391 if (col >= Columns) /* not enough room */
3392 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003393#ifdef FEAT_RIGHTLEFT
3394 if (cmdmsg_rl)
3395 while (msg_col > Columns - col)
3396 msg_putchar(' ');
3397 else
3398#endif
3399 while (msg_col < col)
3400 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401}
3402
3403#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3404/*
3405 * Used for "confirm()" function, and the :confirm command prefix.
3406 * Versions which haven't got flexible dialogs yet, and console
3407 * versions, get this generic handler which uses the command line.
3408 *
3409 * type = one of:
3410 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3411 * title = title string (can be NULL for default)
3412 * (neither used in console dialogs at the moment)
3413 *
3414 * Format of the "buttons" string:
3415 * "Button1Name\nButton2Name\nButton3Name"
3416 * The first button should normally be the default/accept
3417 * The second button should be the 'Cancel' button
3418 * Other buttons- use your imagination!
3419 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3420 * different letter.
3421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003423do_dialog(
3424 int type UNUSED,
3425 char_u *title UNUSED,
3426 char_u *message,
3427 char_u *buttons,
3428 int dfltbutton,
3429 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003430 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003431 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003432 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 int oldState;
3435 int retval = 0;
3436 char_u *hotkeys;
3437 int c;
3438 int i;
3439
3440#ifndef NO_CONSOLE
3441 /* Don't output anything in silent mode ("ex -s") */
3442 if (silent_mode)
3443 return dfltbutton; /* return default option */
3444#endif
3445
3446#ifdef FEAT_GUI_DIALOG
3447 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3448 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3449 {
3450 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003451 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003452 /* avoid a hit-enter prompt without clearing the cmdline */
3453 need_wait_return = FALSE;
3454 emsg_on_display = FALSE;
3455 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 /* Flush output to avoid that further messages and redrawing is done
3458 * in the wrong order. */
3459 out_flush();
3460 gui_mch_update();
3461
3462 return c;
3463 }
3464#endif
3465
3466 oldState = State;
3467 State = CONFIRM;
3468#ifdef FEAT_MOUSE
3469 setmouse();
3470#endif
3471
3472 /*
3473 * Since we wait for a keypress, don't make the
3474 * user press RETURN as well afterwards.
3475 */
3476 ++no_wait_return;
3477 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3478
3479 if (hotkeys != NULL)
3480 {
3481 for (;;)
3482 {
3483 /* Get a typed character directly from the user. */
3484 c = get_keystroke();
3485 switch (c)
3486 {
3487 case CAR: /* User accepts default option */
3488 case NL:
3489 retval = dfltbutton;
3490 break;
3491 case Ctrl_C: /* User aborts/cancels */
3492 case ESC:
3493 retval = 0;
3494 break;
3495 default: /* Could be a hotkey? */
3496 if (c < 0) /* special keys are ignored here */
3497 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003498 if (c == ':' && ex_cmd)
3499 {
3500 retval = dfltbutton;
3501 ins_char_typebuf(':');
3502 break;
3503 }
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 /* Make the character lowercase, as chars in "hotkeys" are. */
3506 c = MB_TOLOWER(c);
3507 retval = 1;
3508 for (i = 0; hotkeys[i]; ++i)
3509 {
3510#ifdef FEAT_MBYTE
3511 if (has_mbyte)
3512 {
3513 if ((*mb_ptr2char)(hotkeys + i) == c)
3514 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003515 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 else
3518#endif
3519 if (hotkeys[i] == c)
3520 break;
3521 ++retval;
3522 }
3523 if (hotkeys[i])
3524 break;
3525 /* No hotkey match, so keep waiting */
3526 continue;
3527 }
3528 break;
3529 }
3530
3531 vim_free(hotkeys);
3532 }
3533
3534 State = oldState;
3535#ifdef FEAT_MOUSE
3536 setmouse();
3537#endif
3538 --no_wait_return;
3539 msg_end_prompt();
3540
3541 return retval;
3542}
3543
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003544static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546/*
3547 * Copy one character from "*from" to "*to", taking care of multi-byte
3548 * characters. Return the length of the character in bytes.
3549 */
3550 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003551copy_char(
3552 char_u *from,
3553 char_u *to,
3554 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
3556#ifdef FEAT_MBYTE
3557 int len;
3558 int c;
3559
3560 if (has_mbyte)
3561 {
3562 if (lowercase)
3563 {
3564 c = MB_TOLOWER((*mb_ptr2char)(from));
3565 return (*mb_char2bytes)(c, to);
3566 }
3567 else
3568 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003569 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 mch_memmove(to, from, (size_t)len);
3571 return len;
3572 }
3573 }
3574 else
3575#endif
3576 {
3577 if (lowercase)
3578 *to = (char_u)TOLOWER_LOC(*from);
3579 else
3580 *to = *from;
3581 return 1;
3582 }
3583}
3584
3585/*
3586 * Format the dialog string, and display it at the bottom of
3587 * the screen. Return a string of hotkey chars (if defined) for
3588 * each 'button'. If a button has no hotkey defined, the first character of
3589 * the button is used.
3590 * The hotkeys can be multi-byte characters, but without combining chars.
3591 *
3592 * Returns an allocated string with hotkeys, or NULL for error.
3593 */
3594 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003595msg_show_console_dialog(
3596 char_u *message,
3597 char_u *buttons,
3598 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
3600 int len = 0;
3601#ifdef FEAT_MBYTE
3602# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3603#else
3604# define HOTK_LEN 1
3605#endif
3606 int lenhotkey = HOTK_LEN; /* count first button */
3607 char_u *hotk = NULL;
3608 char_u *msgp = NULL;
3609 char_u *hotkp = NULL;
3610 char_u *r;
3611 int copy;
3612#define HAS_HOTKEY_LEN 30
3613 char_u has_hotkey[HAS_HOTKEY_LEN];
3614 int first_hotkey = FALSE; /* first char of button is hotkey */
3615 int idx;
3616
3617 has_hotkey[0] = FALSE;
3618
3619 /*
3620 * First loop: compute the size of memory to allocate.
3621 * Second loop: copy to the allocated memory.
3622 */
3623 for (copy = 0; copy <= 1; ++copy)
3624 {
3625 r = buttons;
3626 idx = 0;
3627 while (*r)
3628 {
3629 if (*r == DLG_BUTTON_SEP)
3630 {
3631 if (copy)
3632 {
3633 *msgp++ = ',';
3634 *msgp++ = ' '; /* '\n' -> ', ' */
3635
3636 /* advance to next hotkey and set default hotkey */
3637#ifdef FEAT_MBYTE
3638 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003639 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 else
3641#endif
3642 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003643 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (dfltbutton)
3645 --dfltbutton;
3646
3647 /* If no hotkey is specified first char is used. */
3648 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3649 first_hotkey = TRUE;
3650 }
3651 else
3652 {
3653 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3654 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3655 if (idx < HAS_HOTKEY_LEN - 1)
3656 has_hotkey[++idx] = FALSE;
3657 }
3658 }
3659 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3660 {
3661 if (*r == DLG_HOTKEY_CHAR)
3662 ++r;
3663 first_hotkey = FALSE;
3664 if (copy)
3665 {
3666 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3667 *msgp++ = *r;
3668 else
3669 {
3670 /* '&a' -> '[a]' */
3671 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3672 msgp += copy_char(r, msgp, FALSE);
3673 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3674
3675 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003676 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 }
3679 else
3680 {
3681 ++len; /* '&a' -> '[a]' */
3682 if (idx < HAS_HOTKEY_LEN - 1)
3683 has_hotkey[idx] = TRUE;
3684 }
3685 }
3686 else
3687 {
3688 /* everything else copy literally */
3689 if (copy)
3690 msgp += copy_char(r, msgp, FALSE);
3691 }
3692
3693 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003694 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697 if (copy)
3698 {
3699 *msgp++ = ':';
3700 *msgp++ = ' ';
3701 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 else
3704 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003705 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003706 + 2 /* for the NL's */
3707 + STRLEN(buttons)
3708 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003709 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
3711 /* If no hotkey is specified first char is used. */
3712 if (!has_hotkey[0])
3713 {
3714 first_hotkey = TRUE;
3715 len += 2; /* "x" -> "[x]" */
3716 }
3717
3718 /*
3719 * Now allocate and load the strings
3720 */
3721 vim_free(confirm_msg);
3722 confirm_msg = alloc(len);
3723 if (confirm_msg == NULL)
3724 return NULL;
3725 *confirm_msg = NUL;
3726 hotk = alloc(lenhotkey);
3727 if (hotk == NULL)
3728 return NULL;
3729
3730 *confirm_msg = '\n';
3731 STRCPY(confirm_msg + 1, message);
3732
3733 msgp = confirm_msg + 1 + STRLEN(message);
3734 hotkp = hotk;
3735
Bram Moolenaar6a516062007-07-05 08:11:42 +00003736 /* Define first default hotkey. Keep the hotkey string NUL
3737 * terminated to avoid reading past the end. */
3738 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
3740 /* Remember where the choices start, displaying starts here when
3741 * "hotkp" typed at the more prompt. */
3742 confirm_msg_tail = msgp;
3743 *msgp++ = '\n';
3744 }
3745 }
3746
3747 display_confirm_msg();
3748 return hotk;
3749}
3750
3751/*
3752 * Display the ":confirm" message. Also called when screen resized.
3753 */
3754 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003755display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 /* avoid that 'q' at the more prompt truncates the message here */
3758 ++confirm_msg_used;
3759 if (confirm_msg != NULL)
3760 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3761 --confirm_msg_used;
3762}
3763
3764#endif /* FEAT_CON_DIALOG */
3765
3766#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3767
3768 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003769vim_dialog_yesno(
3770 int type,
3771 char_u *title,
3772 char_u *message,
3773 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 if (do_dialog(type,
3776 title == NULL ? (char_u *)_("Question") : title,
3777 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003778 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 return VIM_YES;
3780 return VIM_NO;
3781}
3782
3783 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003784vim_dialog_yesnocancel(
3785 int type,
3786 char_u *title,
3787 char_u *message,
3788 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789{
3790 switch (do_dialog(type,
3791 title == NULL ? (char_u *)_("Question") : title,
3792 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003793 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
3795 case 1: return VIM_YES;
3796 case 2: return VIM_NO;
3797 }
3798 return VIM_CANCEL;
3799}
3800
3801 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003802vim_dialog_yesnoallcancel(
3803 int type,
3804 char_u *title,
3805 char_u *message,
3806 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 switch (do_dialog(type,
3809 title == NULL ? (char_u *)"Question" : title,
3810 message,
3811 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003812 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814 case 1: return VIM_YES;
3815 case 2: return VIM_NO;
3816 case 3: return VIM_ALL;
3817 case 4: return VIM_DISCARDALL;
3818 }
3819 return VIM_CANCEL;
3820}
3821
3822#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3823
3824#if defined(FEAT_BROWSE) || defined(PROTO)
3825/*
3826 * Generic browse function. Calls gui_mch_browse() when possible.
3827 * Later this may pop-up a non-GUI file selector (external command?).
3828 */
3829 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003830do_browse(
3831 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3832 char_u *title, /* title for the window */
3833 char_u *dflt, /* default file name (may include directory) */
3834 char_u *ext, /* extension added */
3835 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003837 char_u *filter, /* file name filter */
3838 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
3840 char_u *fname;
3841 static char_u *last_dir = NULL; /* last used directory */
3842 char_u *tofree = NULL;
3843 int save_browse = cmdmod.browse;
3844
3845 /* Must turn off browse to avoid that autocommands will get the
3846 * flag too! */
3847 cmdmod.browse = FALSE;
3848
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003849 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003851 if (flags & BROWSE_DIR)
3852 title = (char_u *)_("Select Directory dialog");
3853 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 title = (char_u *)_("Save File dialog");
3855 else
3856 title = (char_u *)_("Open File dialog");
3857 }
3858
3859 /* When no directory specified, use default file name, default dir, buffer
3860 * dir, last dir or current dir */
3861 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3862 {
3863 if (mch_isdir(dflt)) /* default file name is a directory */
3864 {
3865 initdir = dflt;
3866 dflt = NULL;
3867 }
3868 else if (gettail(dflt) != dflt) /* default file name includes a path */
3869 {
3870 tofree = vim_strsave(dflt);
3871 if (tofree != NULL)
3872 {
3873 initdir = tofree;
3874 *gettail(initdir) = NUL;
3875 dflt = gettail(dflt);
3876 }
3877 }
3878 }
3879
3880 if (initdir == NULL || *initdir == NUL)
3881 {
3882 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003883 if (STRCMP(p_bsdir, "last") != 0
3884 && STRCMP(p_bsdir, "buffer") != 0
3885 && STRCMP(p_bsdir, "current") != 0
3886 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 initdir = p_bsdir;
3888 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003889 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 && buf != NULL && buf->b_ffname != NULL)
3891 {
3892 if (dflt == NULL || *dflt == NUL)
3893 dflt = gettail(curbuf->b_ffname);
3894 tofree = vim_strsave(curbuf->b_ffname);
3895 if (tofree != NULL)
3896 {
3897 initdir = tofree;
3898 *gettail(initdir) = NUL;
3899 }
3900 }
3901 /* When 'browsedir' is "last", use dir from last browse */
3902 else if (*p_bsdir == 'l')
3903 initdir = last_dir;
3904 /* When 'browsedir is "current", use current directory. This is the
3905 * default already, leave initdir empty. */
3906 }
3907
3908# ifdef FEAT_GUI
3909 if (gui.in_use) /* when this changes, also adjust f_has()! */
3910 {
3911 if (filter == NULL
3912# ifdef FEAT_EVAL
3913 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3914 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3915# endif
3916 )
3917 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003918 if (flags & BROWSE_DIR)
3919 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003920# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003921 /* For systems that have a directory dialog. */
3922 fname = gui_mch_browsedir(title, initdir);
3923# else
3924 /* Generic solution for selecting a directory: select a file and
3925 * remove the file name. */
3926 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3927# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003928# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003929 /* Win32 adds a dummy file name, others return an arbitrary file
3930 * name. GTK+ 2 returns only the directory, */
3931 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3932 {
3933 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003934 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003935
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003936 if (tail == fname)
3937 *tail++ = '.'; /* use current dir */
3938 *tail = NUL;
3939 }
3940# endif
3941 }
3942 else
3943 fname = gui_mch_browse(flags & BROWSE_SAVE,
3944 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
3946 /* We hang around in the dialog for a while, the user might do some
3947 * things to our files. The Win32 dialog allows deleting or renaming
3948 * a file, check timestamps. */
3949 need_check_timestamps = TRUE;
3950 did_check_timestamps = FALSE;
3951 }
3952 else
3953# endif
3954 {
3955 /* TODO: non-GUI file selector here */
3956 EMSG(_("E338: Sorry, no file browser in console mode"));
3957 fname = NULL;
3958 }
3959
3960 /* keep the directory for next time */
3961 if (fname != NULL)
3962 {
3963 vim_free(last_dir);
3964 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003965 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
3967 *gettail(last_dir) = NUL;
3968 if (*last_dir == NUL)
3969 {
3970 /* filename only returned, must be in current dir */
3971 vim_free(last_dir);
3972 last_dir = alloc(MAXPATHL);
3973 if (last_dir != NULL)
3974 mch_dirname(last_dir, MAXPATHL);
3975 }
3976 }
3977 }
3978
3979 vim_free(tofree);
3980 cmdmod.browse = save_browse;
3981
3982 return fname;
3983}
3984#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003985
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003986#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003987static char *e_printf = N_("E766: Insufficient arguments for printf()");
3988
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003989static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003990static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003991# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003992static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003993# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003994
3995/*
3996 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3997 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003998 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003999tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004000{
4001 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004002 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004003 int err = FALSE;
4004
4005 if (tvs[idx].v_type == VAR_UNKNOWN)
4006 EMSG(_(e_printf));
4007 else
4008 {
4009 ++*idxp;
4010 n = get_tv_number_chk(&tvs[idx], &err);
4011 if (err)
4012 n = 0;
4013 }
4014 return n;
4015}
4016
4017/*
4018 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004019 * If "tofree" is NULL get_tv_string_chk() is used. Some types (e.g. List)
4020 * are not converted to a string.
4021 * If "tofree" is not NULL echo_string() is used. All types are converted to
4022 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004023 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 */
4025 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004026tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004027{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004028 int idx = *idxp - 1;
4029 char *s = NULL;
4030 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004031
4032 if (tvs[idx].v_type == VAR_UNKNOWN)
4033 EMSG(_(e_printf));
4034 else
4035 {
4036 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004037 if (tofree != NULL)
4038 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4039 else
4040 s = (char *)get_tv_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041 }
4042 return s;
4043}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004044
4045# ifdef FEAT_FLOAT
4046/*
4047 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4048 */
4049 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004050tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004051{
4052 int idx = *idxp - 1;
4053 double f = 0;
4054
4055 if (tvs[idx].v_type == VAR_UNKNOWN)
4056 EMSG(_(e_printf));
4057 else
4058 {
4059 ++*idxp;
4060 if (tvs[idx].v_type == VAR_FLOAT)
4061 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004062 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004063 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004064 else
4065 EMSG(_("E807: Expected Float argument for printf()"));
4066 }
4067 return f;
4068}
4069# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070#endif
4071
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004072#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004073/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004074 * Return the representation of infinity for printf() function:
4075 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4076 */
4077 static const char *
4078infinity_str(int positive,
4079 char fmt_spec,
4080 int force_sign,
4081 int space_for_positive)
4082{
4083 static const char *table[] =
4084 {
4085 "-inf", "inf", "+inf", " inf",
4086 "-INF", "INF", "+INF", " INF"
4087 };
4088 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4089
4090 if (ASCII_ISUPPER(fmt_spec))
4091 idx += 4;
4092 return table[idx];
4093}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004094#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004095
4096/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004097 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004098 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004099 * consistency.
4100 *
4101 * This code is based on snprintf.c - a portable implementation of snprintf
4102 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004103 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004104 * The original code, including useful comments, can be found here:
4105 * http://www.ijs.si/software/snprintf/
4106 *
4107 * This snprintf() only supports the following conversion specifiers:
4108 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4109 * with flags: '-', '+', ' ', '0' and '#'.
4110 * An asterisk is supported for field width as well as precision.
4111 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004112 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004113 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004114 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4115 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004116 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004117 * The locale is not used, the string is used as a byte string. This is only
4118 * relevant for double-byte encodings where the second byte may be '%'.
4119 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004120 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4121 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004122 *
4123 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004124 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004125 * is greater or equal to "str_m", not all characters from the result
4126 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4127 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004128 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004129 */
4130
4131/*
4132 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133 *
4134 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004135 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004136 */
4137
4138/* When generating prototypes all of this is skipped, cproto doesn't
4139 * understand this. */
4140#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004141
Bram Moolenaara800b422010-06-27 01:15:55 +02004142/* Like vim_vsnprintf() but append to the string. */
4143 int
4144vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4145{
4146 va_list ap;
4147 int str_l;
4148 size_t len = STRLEN(str);
4149 size_t space;
4150
4151 if (str_m <= len)
4152 space = 0;
4153 else
4154 space = str_m - len;
4155 va_start(ap, fmt);
4156 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4157 va_end(ap);
4158 return str_l;
4159}
Bram Moolenaara800b422010-06-27 01:15:55 +02004160
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004161 int
4162vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4163{
4164 va_list ap;
4165 int str_l;
4166
4167 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004169 va_end(ap);
4170 return str_l;
4171}
4172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004173 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004174vim_vsnprintf(
4175 char *str,
4176 size_t str_m,
4177 char *fmt,
4178 va_list ap,
4179 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004180{
4181 size_t str_l = 0;
4182 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004183 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184
4185 if (p == NULL)
4186 p = "";
4187 while (*p != NUL)
4188 {
4189 if (*p != '%')
4190 {
4191 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004192 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004193
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004194 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004195 if (str_l < str_m)
4196 {
4197 size_t avail = str_m - str_l;
4198
4199 mch_memmove(str + str_l, p, n > avail ? avail : n);
4200 }
4201 p += n;
4202 str_l += n;
4203 }
4204 else
4205 {
4206 size_t min_field_width = 0, precision = 0;
4207 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4208 int alternate_form = 0, force_sign = 0;
4209
4210 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4211 * ignored. */
4212 int space_for_positive = 1;
4213
4214 /* allowed values: \0, h, l, L */
4215 char length_modifier = '\0';
4216
4217 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004218# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004219# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004220 * That sounds reasonable to use as the maximum
4221 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004222# elif defined(FEAT_NUM64)
4223# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004224# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004225# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004226# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004227 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004228
4229 /* string address in case of string argument */
4230 char *str_arg;
4231
4232 /* natural field width of arg without padding and sign */
4233 size_t str_arg_l;
4234
4235 /* unsigned char argument value - only defined for c conversion.
4236 * N.B. standard explicitly states the char argument for the c
4237 * conversion is unsigned */
4238 unsigned char uchar_arg;
4239
4240 /* number of zeros to be inserted for numeric conversions as
4241 * required by the precision or minimal field width */
4242 size_t number_of_zeros_to_pad = 0;
4243
4244 /* index into tmp where zero padding is to be inserted */
4245 size_t zero_padding_insertion_ind = 0;
4246
4247 /* current conversion specifier character */
4248 char fmt_spec = '\0';
4249
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004250 /* buffer for 's' and 'S' specs */
4251 char_u *tofree = NULL;
4252
4253
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004254 str_arg = NULL;
4255 p++; /* skip '%' */
4256
4257 /* parse flags */
4258 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4259 || *p == '#' || *p == '\'')
4260 {
4261 switch (*p)
4262 {
4263 case '0': zero_padding = 1; break;
4264 case '-': justify_left = 1; break;
4265 case '+': force_sign = 1; space_for_positive = 0; break;
4266 case ' ': force_sign = 1;
4267 /* If both the ' ' and '+' flags appear, the ' '
4268 * flag should be ignored */
4269 break;
4270 case '#': alternate_form = 1; break;
4271 case '\'': break;
4272 }
4273 p++;
4274 }
4275 /* If the '0' and '-' flags both appear, the '0' flag should be
4276 * ignored. */
4277
4278 /* parse field width */
4279 if (*p == '*')
4280 {
4281 int j;
4282
4283 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004285# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004286 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287# endif
4288 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004289 if (j >= 0)
4290 min_field_width = j;
4291 else
4292 {
4293 min_field_width = -j;
4294 justify_left = 1;
4295 }
4296 }
4297 else if (VIM_ISDIGIT((int)(*p)))
4298 {
4299 /* size_t could be wider than unsigned int; make sure we treat
4300 * argument like common implementations do */
4301 unsigned int uj = *p++ - '0';
4302
4303 while (VIM_ISDIGIT((int)(*p)))
4304 uj = 10 * uj + (unsigned int)(*p++ - '0');
4305 min_field_width = uj;
4306 }
4307
4308 /* parse precision */
4309 if (*p == '.')
4310 {
4311 p++;
4312 precision_specified = 1;
4313 if (*p == '*')
4314 {
4315 int j;
4316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004317 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004319 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320# endif
4321 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004322 p++;
4323 if (j >= 0)
4324 precision = j;
4325 else
4326 {
4327 precision_specified = 0;
4328 precision = 0;
4329 }
4330 }
4331 else if (VIM_ISDIGIT((int)(*p)))
4332 {
4333 /* size_t could be wider than unsigned int; make sure we
4334 * treat argument like common implementations do */
4335 unsigned int uj = *p++ - '0';
4336
4337 while (VIM_ISDIGIT((int)(*p)))
4338 uj = 10 * uj + (unsigned int)(*p++ - '0');
4339 precision = uj;
4340 }
4341 }
4342
4343 /* parse 'h', 'l' and 'll' length modifiers */
4344 if (*p == 'h' || *p == 'l')
4345 {
4346 length_modifier = *p;
4347 p++;
4348 if (length_modifier == 'l' && *p == 'l')
4349 {
4350 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004351# ifdef FEAT_NUM64
4352 length_modifier = 'L';
4353# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004354 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004355# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004356 p++;
4357 }
4358 }
4359 fmt_spec = *p;
4360
4361 /* common synonyms: */
4362 switch (fmt_spec)
4363 {
4364 case 'i': fmt_spec = 'd'; break;
4365 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4366 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4367 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4368 default: break;
4369 }
4370
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004371# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4372 switch (fmt_spec)
4373 {
4374 case 'd': case 'u': case 'o': case 'x': case 'X':
4375 if (tvs != NULL && length_modifier == '\0')
4376 length_modifier = 'L';
4377 }
4378# endif
4379
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004380 /* get parameter value, do initial processing */
4381 switch (fmt_spec)
4382 {
4383 /* '%' and 'c' behave similar to 's' regarding flags and field
4384 * widths */
4385 case '%':
4386 case 'c':
4387 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004388 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004389 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004390 str_arg_l = 1;
4391 switch (fmt_spec)
4392 {
4393 case '%':
4394 str_arg = p;
4395 break;
4396
4397 case 'c':
4398 {
4399 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400
4401 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004403 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004404# endif
4405 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004406 /* standard demands unsigned char */
4407 uchar_arg = (unsigned char)j;
4408 str_arg = (char *)&uchar_arg;
4409 break;
4410 }
4411
4412 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004413 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004416 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417# endif
4418 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004419 if (str_arg == NULL)
4420 {
4421 str_arg = "[NULL]";
4422 str_arg_l = 6;
4423 }
4424 /* make sure not to address string beyond the specified
4425 * precision !!! */
4426 else if (!precision_specified)
4427 str_arg_l = strlen(str_arg);
4428 /* truncate string if necessary as requested by precision */
4429 else if (precision == 0)
4430 str_arg_l = 0;
4431 else
4432 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004433 /* Don't put the #if inside memchr(), it can be a
4434 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004435# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004436 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004437# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004438 /* memchr on HP does not like n > 2^31 !!! */
4439 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004440 precision <= (size_t)0x7fffffffL ? precision
4441 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004442# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004443 str_arg_l = (q == NULL) ? precision
4444 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004445 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004446# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004447 if (fmt_spec == 'S')
4448 {
4449 if (min_field_width != 0)
4450 min_field_width += STRLEN(str_arg)
4451 - mb_string2cells((char_u *)str_arg, -1);
4452 if (precision)
4453 {
4454 char_u *p1 = (char_u *)str_arg;
4455 size_t i;
4456
4457 for (i = 0; i < precision && *p1; i++)
4458 p1 += mb_ptr2len(p1);
4459
4460 str_arg_l = precision = p1 - (char_u *)str_arg;
4461 }
4462 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004463# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004464 break;
4465
4466 default:
4467 break;
4468 }
4469 break;
4470
Bram Moolenaar91984b92016-08-16 21:58:41 +02004471 case 'd': case 'u':
4472 case 'b': case 'B':
4473 case 'o':
4474 case 'x': case 'X':
4475 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004476 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004477 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004478 * imply the value is unsigned; d implies a signed
4479 * value */
4480
4481 /* 0 if numeric argument is zero (or if pointer is
4482 * NULL for 'p'), +1 if greater than zero (or nonzero
4483 * for unsigned arguments), -1 if negative (unsigned
4484 * argument is never negative) */
4485 int arg_sign = 0;
4486
4487 /* only defined for length modifier h, or for no
4488 * length modifiers */
4489 int int_arg = 0;
4490 unsigned int uint_arg = 0;
4491
4492 /* only defined for length modifier l */
4493 long int long_arg = 0;
4494 unsigned long int ulong_arg = 0;
4495
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004496# ifdef FEAT_NUM64
4497 /* only defined for length modifier ll */
4498 varnumber_T llong_arg = 0;
4499 uvarnumber_T ullong_arg = 0;
4500# endif
4501
Bram Moolenaare9997822016-08-28 16:03:38 +02004502 /* only defined for b conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004503 uvarnumber_T bin_arg = 0;
4504
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004505 /* pointer argument value -only defined for p
4506 * conversion */
4507 void *ptr_arg = NULL;
4508
4509 if (fmt_spec == 'p')
4510 {
4511 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004514 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4515 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004516# endif
4517 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518 if (ptr_arg != NULL)
4519 arg_sign = 1;
4520 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004521 else if (fmt_spec == 'b' || fmt_spec == 'B')
4522 {
4523 bin_arg =
4524# if defined(FEAT_EVAL)
4525 tvs != NULL ?
4526 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4527# endif
4528 va_arg(ap, uvarnumber_T);
4529 if (bin_arg != 0)
4530 arg_sign = 1;
4531 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 else if (fmt_spec == 'd')
4533 {
4534 /* signed */
4535 switch (length_modifier)
4536 {
4537 case '\0':
4538 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 /* char and short arguments are passed as int. */
4540 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004542 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543# endif
4544 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004545 if (int_arg > 0)
4546 arg_sign = 1;
4547 else if (int_arg < 0)
4548 arg_sign = -1;
4549 break;
4550 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004553 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554# endif
4555 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004556 if (long_arg > 0)
4557 arg_sign = 1;
4558 else if (long_arg < 0)
4559 arg_sign = -1;
4560 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004561# ifdef FEAT_NUM64
4562 case 'L':
4563 llong_arg =
4564# if defined(FEAT_EVAL)
4565 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4566# endif
4567 va_arg(ap, varnumber_T);
4568 if (llong_arg > 0)
4569 arg_sign = 1;
4570 else if (llong_arg < 0)
4571 arg_sign = -1;
4572 break;
4573# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004574 }
4575 }
4576 else
4577 {
4578 /* unsigned */
4579 switch (length_modifier)
4580 {
4581 case '\0':
4582 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004584# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004585 tvs != NULL ? (unsigned)
4586 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587# endif
4588 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004589 if (uint_arg != 0)
4590 arg_sign = 1;
4591 break;
4592 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004595 tvs != NULL ? (unsigned long)
4596 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597# endif
4598 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004599 if (ulong_arg != 0)
4600 arg_sign = 1;
4601 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004602# ifdef FEAT_NUM64
4603 case 'L':
4604 ullong_arg =
4605# if defined(FEAT_EVAL)
4606 tvs != NULL ? (uvarnumber_T)
4607 tv_nr(tvs, &arg_idx) :
4608# endif
4609 va_arg(ap, uvarnumber_T);
4610 if (ullong_arg != 0)
4611 arg_sign = 1;
4612 break;
4613# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004614 }
4615 }
4616
4617 str_arg = tmp;
4618 str_arg_l = 0;
4619
4620 /* NOTE:
4621 * For d, i, u, o, x, and X conversions, if precision is
4622 * specified, the '0' flag should be ignored. This is so
4623 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4624 * FreeBSD, NetBSD; but not with Perl.
4625 */
4626 if (precision_specified)
4627 zero_padding = 0;
4628 if (fmt_spec == 'd')
4629 {
4630 if (force_sign && arg_sign >= 0)
4631 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4632 /* leave negative numbers for sprintf to handle, to
4633 * avoid handling tricky cases like (short int)-32768 */
4634 }
4635 else if (alternate_form)
4636 {
4637 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004638 && (fmt_spec == 'b' || fmt_spec == 'B'
4639 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004640 {
4641 tmp[str_arg_l++] = '0';
4642 tmp[str_arg_l++] = fmt_spec;
4643 }
4644 /* alternate form should have no effect for p
4645 * conversion, but ... */
4646 }
4647
4648 zero_padding_insertion_ind = str_arg_l;
4649 if (!precision_specified)
4650 precision = 1; /* default precision is 1 */
4651 if (precision == 0 && arg_sign == 0)
4652 {
4653 /* When zero value is formatted with an explicit
4654 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004655 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004656 }
4657 else
4658 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004659 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004660 int f_l = 0;
4661
4662 /* construct a simple format string for sprintf */
4663 f[f_l++] = '%';
4664 if (!length_modifier)
4665 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004666 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004667 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004668# ifdef FEAT_NUM64
4669# ifdef WIN3264
4670 f[f_l++] = 'I';
4671 f[f_l++] = '6';
4672 f[f_l++] = '4';
4673# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004674 f[f_l++] = 'l';
4675 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004676# endif
4677# else
4678 f[f_l++] = 'l';
4679# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004680 }
4681 else
4682 f[f_l++] = length_modifier;
4683 f[f_l++] = fmt_spec;
4684 f[f_l++] = '\0';
4685
4686 if (fmt_spec == 'p')
4687 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004688 else if (fmt_spec == 'b' || fmt_spec == 'B')
4689 {
4690 char b[8 * sizeof(uvarnumber_T)];
4691 size_t b_l = 0;
4692 uvarnumber_T bn = bin_arg;
4693
4694 do
4695 {
4696 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4697 bn >>= 1;
4698 }
4699 while (bn != 0);
4700
4701 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4702 str_arg_l += b_l;
4703 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004704 else if (fmt_spec == 'd')
4705 {
4706 /* signed */
4707 switch (length_modifier)
4708 {
4709 case '\0':
4710 case 'h': str_arg_l += sprintf(
4711 tmp + str_arg_l, f, int_arg);
4712 break;
4713 case 'l': str_arg_l += sprintf(
4714 tmp + str_arg_l, f, long_arg);
4715 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004716# ifdef FEAT_NUM64
4717 case 'L': str_arg_l += sprintf(
4718 tmp + str_arg_l, f, llong_arg);
4719 break;
4720# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004721 }
4722 }
4723 else
4724 {
4725 /* unsigned */
4726 switch (length_modifier)
4727 {
4728 case '\0':
4729 case 'h': str_arg_l += sprintf(
4730 tmp + str_arg_l, f, uint_arg);
4731 break;
4732 case 'l': str_arg_l += sprintf(
4733 tmp + str_arg_l, f, ulong_arg);
4734 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004735# ifdef FEAT_NUM64
4736 case 'L': str_arg_l += sprintf(
4737 tmp + str_arg_l, f, ullong_arg);
4738 break;
4739# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004740 }
4741 }
4742
4743 /* include the optional minus sign and possible
4744 * "0x" in the region before the zero padding
4745 * insertion point */
4746 if (zero_padding_insertion_ind < str_arg_l
4747 && tmp[zero_padding_insertion_ind] == '-')
4748 zero_padding_insertion_ind++;
4749 if (zero_padding_insertion_ind + 1 < str_arg_l
4750 && tmp[zero_padding_insertion_ind] == '0'
4751 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4752 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4753 zero_padding_insertion_ind += 2;
4754 }
4755
4756 {
4757 size_t num_of_digits = str_arg_l
4758 - zero_padding_insertion_ind;
4759
4760 if (alternate_form && fmt_spec == 'o'
4761 /* unless zero is already the first
4762 * character */
4763 && !(zero_padding_insertion_ind < str_arg_l
4764 && tmp[zero_padding_insertion_ind] == '0'))
4765 {
4766 /* assure leading zero for alternate-form
4767 * octal numbers */
4768 if (!precision_specified
4769 || precision < num_of_digits + 1)
4770 {
4771 /* precision is increased to force the
4772 * first character to be zero, except if a
4773 * zero value is formatted with an
4774 * explicit precision of zero */
4775 precision = num_of_digits + 1;
4776 precision_specified = 1;
4777 }
4778 }
4779 /* zero padding to specified precision? */
4780 if (num_of_digits < precision)
4781 number_of_zeros_to_pad = precision - num_of_digits;
4782 }
4783 /* zero padding to specified minimal field width? */
4784 if (!justify_left && zero_padding)
4785 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004786 int n = (int)(min_field_width - (str_arg_l
4787 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004788 if (n > 0)
4789 number_of_zeros_to_pad += n;
4790 }
4791 break;
4792 }
4793
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004794# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004795 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004796 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004797 case 'e':
4798 case 'E':
4799 case 'g':
4800 case 'G':
4801 {
4802 /* Floating point. */
4803 double f;
4804 double abs_f;
4805 char format[40];
4806 int l;
4807 int remove_trailing_zeroes = FALSE;
4808
4809 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004810# if defined(FEAT_EVAL)
4811 tvs != NULL ? tv_float(tvs, &arg_idx) :
4812# endif
4813 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004814 abs_f = f < 0 ? -f : f;
4815
4816 if (fmt_spec == 'g' || fmt_spec == 'G')
4817 {
4818 /* Would be nice to use %g directly, but it prints
4819 * "1.0" as "1", we don't want that. */
4820 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4821 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004822 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004823 else
4824 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4825 remove_trailing_zeroes = TRUE;
4826 }
4827
Bram Moolenaar04186092016-08-29 21:55:35 +02004828 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004829# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004830 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004831# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004832 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004833# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004834 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004835 {
4836 /* Avoid a buffer overflow */
Bram Moolenaare9997822016-08-28 16:03:38 +02004837 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4838 force_sign, space_for_positive));
4839 str_arg_l = STRLEN(tmp);
4840 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004841 }
4842 else
4843 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004844 if (isnan(f))
4845 {
4846 /* Not a number: nan or NAN */
4847 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4848 : "nan");
4849 str_arg_l = 3;
4850 zero_padding = 0;
4851 }
4852 else if (isinf(f))
4853 {
4854 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4855 force_sign, space_for_positive));
4856 str_arg_l = STRLEN(tmp);
4857 zero_padding = 0;
4858 }
4859 else
Bram Moolenaar04186092016-08-29 21:55:35 +02004860 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004861 /* Regular float number */
Bram Moolenaar04186092016-08-29 21:55:35 +02004862 format[0] = '%';
4863 l = 1;
4864 if (force_sign)
4865 format[l++] = space_for_positive ? ' ' : '+';
4866 if (precision_specified)
4867 {
4868 size_t max_prec = TMP_LEN - 10;
4869
4870 /* Make sure we don't get more digits than we
4871 * have room for. */
4872 if ((fmt_spec == 'f' || fmt_spec == 'F')
4873 && abs_f > 1.0)
4874 max_prec -= (size_t)log10(abs_f);
4875 if (precision > max_prec)
4876 precision = max_prec;
4877 l += sprintf(format + l, ".%d", (int)precision);
4878 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004879 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004880 format[l + 1] = NUL;
4881
Bram Moolenaare9997822016-08-28 16:03:38 +02004882 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar04186092016-08-29 21:55:35 +02004883 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004884
Bram Moolenaara7241f52008-06-24 20:39:31 +00004885 if (remove_trailing_zeroes)
4886 {
4887 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004888 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004889
4890 /* Using %g or %G: remove superfluous zeroes. */
Bram Moolenaar04186092016-08-29 21:55:35 +02004891 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004892 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004893 else
4894 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004895 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004896 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004897 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004898 {
4899 /* Remove superfluous '+' and leading
4900 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004901 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004902 {
4903 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004904 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004905 --str_arg_l;
4906 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004907 i = (tp[1] == '-') ? 2 : 1;
4908 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004909 {
4910 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004911 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004912 --str_arg_l;
4913 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004914 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004915 }
4916 }
4917
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004918 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004919 /* Remove trailing zeroes, but keep the one
4920 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004921 while (tp > tmp + 2 && *tp == '0'
4922 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004923 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004924 STRMOVE(tp, tp + 1);
4925 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004926 --str_arg_l;
4927 }
4928 }
4929 else
4930 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004931 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932
4933 /* Be consistent: some printf("%e") use 1.0e+12
4934 * and some 1.0e+012. Remove one zero in the last
4935 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004936 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004937 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004938 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4939 && tp[2] == '0'
4940 && vim_isdigit(tp[3])
4941 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004942 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004943 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004944 --str_arg_l;
4945 }
4946 }
4947 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004948 if (zero_padding && min_field_width > str_arg_l
4949 && (tmp[0] == '-' || force_sign))
4950 {
4951 /* padding 0's should be inserted after the sign */
4952 number_of_zeros_to_pad = min_field_width - str_arg_l;
4953 zero_padding_insertion_ind = 1;
4954 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004955 str_arg = tmp;
4956 break;
4957 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004958# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004959
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004960 default:
4961 /* unrecognized conversion specifier, keep format string
4962 * as-is */
4963 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004964 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004965 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004966 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004967
4968 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004969 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004970 str_arg = p;
4971 str_arg_l = 0;
4972 if (*p != NUL)
4973 str_arg_l++; /* include invalid conversion specifier
4974 unchanged if not at end-of-string */
4975 break;
4976 }
4977
4978 if (*p != NUL)
4979 p++; /* step over the just processed conversion specifier */
4980
4981 /* insert padding to the left as requested by min_field_width;
4982 * this does not include the zero padding in case of numerical
4983 * conversions*/
4984 if (!justify_left)
4985 {
4986 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004987 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004988
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004989 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004990 {
4991 if (str_l < str_m)
4992 {
4993 size_t avail = str_m - str_l;
4994
4995 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004996 (size_t)pn > avail ? avail
4997 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004998 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004999 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005000 }
5001 }
5002
5003 /* zero padding as requested by the precision or by the minimal
5004 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00005005 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005006 {
5007 /* will not copy first part of numeric right now, *
5008 * force it to be copied later in its entirety */
5009 zero_padding_insertion_ind = 0;
5010 }
5011 else
5012 {
5013 /* insert first part of numerics (sign or '0x') before zero
5014 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005015 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005016
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005017 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005018 {
5019 if (str_l < str_m)
5020 {
5021 size_t avail = str_m - str_l;
5022
5023 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005024 (size_t)zn > avail ? avail
5025 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005026 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005027 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005028 }
5029
5030 /* insert zero padding as requested by the precision or min
5031 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005032 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005033 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005034 {
5035 if (str_l < str_m)
5036 {
5037 size_t avail = str_m-str_l;
5038
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005039 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005040 (size_t)zn > avail ? avail
5041 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005042 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005043 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005044 }
5045 }
5046
5047 /* insert formatted string
5048 * (or as-is conversion specifier for unknown conversions) */
5049 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005050 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005051
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005052 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005053 {
5054 if (str_l < str_m)
5055 {
5056 size_t avail = str_m - str_l;
5057
5058 mch_memmove(str + str_l,
5059 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005060 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005061 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005062 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 }
5064 }
5065
5066 /* insert right padding */
5067 if (justify_left)
5068 {
5069 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005070 int pn = (int)(min_field_width
5071 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005073 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005074 {
5075 if (str_l < str_m)
5076 {
5077 size_t avail = str_m - str_l;
5078
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005079 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005080 (size_t)pn > avail ? avail
5081 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005082 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005083 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005084 }
5085 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005086 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005087 }
5088 }
5089
5090 if (str_m > 0)
5091 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005092 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005093 * overwriting the last character (shouldn't happen, but just in case)
5094 * */
5095 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5096 }
5097
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005098 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00005101 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005102 * character), that is, the number of characters that would have been
5103 * written to the buffer if it were large enough. */
5104 return (int)str_l;
5105}
5106
5107#endif /* PROTO */