blob: 0d1043137211793e832943b95733fe5274715818 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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() */
15
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static int other_sourcing_name(void);
19static char_u *get_emsg_source(void);
20static char_u *get_emsg_lnum(void);
21static void add_msg_hist(char_u *s, int len, int attr);
22static void hit_return_msg(void);
23static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010025static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010027static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
28static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
29static void msg_scroll_up(void);
30static void inc_msg_scrolled(void);
31static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
32static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
33static void msg_puts_printf(char_u *str, int maxlen);
34static int do_more_prompt(int typed_char);
35static void msg_screen_putchar(int c, int attr);
36static int msg_check_screen(void);
37static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static int confirm_msg_used = FALSE; /* displaying confirm_msg */
41static char_u *confirm_msg = NULL; /* ":confirm" message */
42static char_u *confirm_msg_tail; /* tail of confirm_msg */
43#endif
44
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020056static FILE *verbose_fd = NULL;
57static int verbose_did_open = FALSE;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059/*
60 * When writing messages to the screen, there are many different situations.
61 * A number of variables is used to remember the current state:
62 * msg_didany TRUE when messages were written since the last time the
63 * user reacted to a prompt.
64 * Reset: After hitting a key for the hit-return prompt,
65 * hitting <CR> for the command line or input().
66 * Set: When any message is written to the screen.
67 * msg_didout TRUE when something was written to the current line.
68 * Reset: When advancing to the next line, when the current
69 * text can be overwritten.
70 * Set: When any message is written to the screen.
71 * msg_nowait No extra delay for the last drawn message.
72 * Used in normal_cmd() before the mode message is drawn.
73 * emsg_on_display There was an error message recently. Indicates that there
74 * should be a delay before redrawing.
75 * msg_scroll The next message should not overwrite the current one.
76 * msg_scrolled How many lines the screen has been scrolled (because of
77 * messages). Used in update_screen() to scroll the screen
78 * back. Incremented each time the screen scrolls a line.
79 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
80 * writes something without scrolling should not make
81 * need_wait_return to be set. This is a hack to make ":ts"
82 * work without an extra prompt.
83 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010084 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 * need_wait_return TRUE when the hit-return prompt is needed.
86 * Reset: After giving the hit-return prompt, when the user
87 * has answered some other prompt.
88 * Set: When the ruler or typeahead display is overwritten,
89 * scrolling the screen for some message.
90 * keep_msg Message to be displayed after redrawing the screen, in
91 * main_loop().
92 * This is an allocated string or NULL when not used.
93 */
94
95/*
96 * msg(s) - displays the string 's' on the status line
97 * When terminal not initialized (yet) mch_errmsg(..) is used.
98 * return TRUE if wait_return not called
99 */
100 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100101msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 return msg_attr_keep(s, 0, FALSE);
104}
105
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000107 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000108/*
109 * Like msg() but keep it silent when 'verbosefile' is set.
110 */
111 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100112verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000113{
114 int n;
115
116 verbose_enter();
117 n = msg_attr_keep(s, 0, FALSE);
118 verbose_leave();
119
120 return n;
121}
122#endif
123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100125msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126{
127 return msg_attr_keep(s, attr, FALSE);
128}
129
130 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100131msg_attr_keep(
132 char_u *s,
133 int attr,
134 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135{
136 static int entered = 0;
137 int retval;
138 char_u *buf = NULL;
139
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200140 /* Skip messages not matching ":filter pattern".
141 * Don't filter when there is an error. */
142 if (!emsg_on_display && message_filtered(s))
143 return TRUE;
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#ifdef FEAT_EVAL
146 if (attr == 0)
147 set_vim_var_string(VV_STATUSMSG, s, -1);
148#endif
149
150 /*
151 * It is possible that displaying a messages causes a problem (e.g.,
152 * when redrawing the window), which causes another message, etc.. To
153 * break this loop, limit the recursiveness to 3 levels.
154 */
155 if (entered >= 3)
156 return TRUE;
157 ++entered;
158
159 /* Add message to history (unless it's a repeated kept message or a
160 * truncated message) */
161 if (s != keep_msg
162 || (*s != '<'
163 && last_msg_hist != NULL
164 && last_msg_hist->msg != NULL
165 && STRCMP(s, last_msg_hist->msg)))
166 add_msg_hist(s, -1, attr);
167
168 /* When displaying keep_msg, don't let msg_start() free it, caller must do
169 * that. */
170 if (s == keep_msg)
171 keep_msg = NULL;
172
173 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000174 msg_start();
175 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 if (buf != NULL)
177 s = buf;
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 msg_outtrans_attr(s, attr);
180 msg_clr_eos();
181 retval = msg_end();
182
183 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
184 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000185 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
187 vim_free(buf);
188 --entered;
189 return retval;
190}
191
192/*
193 * Truncate a string such that it can be printed without causing a scroll.
194 * Returns an allocated string or NULL when no truncating is done.
195 */
196 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100197msg_strtrunc(
198 char_u *s,
199 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
201 char_u *buf = NULL;
202 int len;
203 int room;
204
205 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000206 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
207 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 {
209 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000210 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000211 /* Use all the columns. */
212 room = (int)(Rows - msg_row) * Columns - 1;
213 else
214 /* Use up to 'showcmd' column. */
215 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (len > room && room > 0)
217 {
218#ifdef FEAT_MBYTE
219 if (enc_utf8)
220 /* may have up to 18 bytes per cell (6 per char, up to two
221 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100222 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 else if (enc_dbcs == DBCS_JPNU)
224 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100225 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 else
227#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100228 len = room + 2;
229 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100231 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 }
233 }
234 return buf;
235}
236
237/*
238 * Truncate a string "s" to "buf" with cell width "room".
239 * "s" and "buf" may be equal.
240 */
241 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100242trunc_string(
243 char_u *s,
244 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200245 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100246 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247{
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200248 size_t room = room_in - 3; /* "..." takes 3 chars */
249 size_t half;
250 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 int e;
252 int i;
253 int n;
254
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200255 if (room_in < 3)
256 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100260 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
262 if (s[e] == NUL)
263 {
264 /* text fits without truncating! */
265 buf[e] = NUL;
266 return;
267 }
268 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200269 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 break;
271 len += n;
272 buf[e] = s[e];
273#ifdef FEAT_MBYTE
274 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000275 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100277 if (++e == buflen)
278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 buf[e] = s[e];
280 }
281#endif
282 }
283
284 /* Last part: End of the string. */
285 i = e;
286#ifdef FEAT_MBYTE
287 if (enc_dbcs != 0)
288 {
289 /* For DBCS going backwards in a string is slow, but
290 * computing the cell width isn't too slow: go forward
291 * until the rest fits. */
292 n = vim_strsize(s + i);
293 while (len + n > room)
294 {
295 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000296 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 }
298 }
299 else if (enc_utf8)
300 {
301 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000302 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 for (;;)
304 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000305 do
306 half = half - (*mb_head_off)(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200307 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200309 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 break;
311 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200312 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 }
314 }
315 else
316#endif
317 {
318 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
319 len += n;
320 }
321
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200322
323 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100324 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200325 /* text fits without truncating */
326 if (s != buf)
327 {
328 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200329 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200330 len = buflen - 1;
331 len = len - e + 1;
332 if (len < 1)
333 buf[e - 1] = NUL;
334 else
335 mch_memmove(buf + e, s + e, len);
336 }
337 }
338 else if (e + 3 < buflen)
339 {
340 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100341 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200342 len = STRLEN(s + i) + 1;
343 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100344 len = buflen - e - 3 - 1;
345 mch_memmove(buf + e + 3, s + i, len);
346 buf[e + 3 + len - 1] = NUL;
347 }
348 else
349 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200350 /* can't fit in the "...", just truncate it */
351 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353}
354
355/*
356 * Automatic prototype generation does not understand this function.
357 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
358 * shorter than IOSIZE!!!
359 */
360#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000362int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100365# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368smsg(char_u *s, ...)
369{
370 va_list arglist;
371
372 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000373 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 va_end(arglist);
375 return msg(IObuff);
376}
377
378 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100379# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382smsg_attr(int attr, char_u *s, ...)
383{
384 va_list arglist;
385
386 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000387 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 va_end(arglist);
389 return msg_attr(IObuff, attr);
390}
391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393
394/*
395 * Remember the last sourcing name/lnum used in an error message, so that it
396 * isn't printed each time when it didn't change.
397 */
398static int last_sourcing_lnum = 0;
399static char_u *last_sourcing_name = NULL;
400
401/*
402 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
403 * for the next error message;
404 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000405 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100406reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
408 vim_free(last_sourcing_name);
409 last_sourcing_name = NULL;
410 last_sourcing_lnum = 0;
411}
412
413/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000414 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
415 */
416 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100417other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000418{
419 if (sourcing_name != NULL)
420 {
421 if (last_sourcing_name != NULL)
422 return STRCMP(sourcing_name, last_sourcing_name) != 0;
423 return TRUE;
424 }
425 return FALSE;
426}
427
428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 * Get the message about the source, as used for an error message.
430 * Returns an allocated string with room for one more character.
431 * Returns NULL when no message is to be given.
432 */
433 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100434get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
436 char_u *Buf, *p;
437
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000438 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {
440 p = (char_u *)_("Error detected while processing %s:");
441 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
442 if (Buf != NULL)
443 sprintf((char *)Buf, (char *)p, sourcing_name);
444 return Buf;
445 }
446 return NULL;
447}
448
449/*
450 * Get the message about the source lnum, as used for an error message.
451 * Returns an allocated string with room for one more character.
452 * Returns NULL when no message is to be given.
453 */
454 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100455get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457 char_u *Buf, *p;
458
459 /* lnum is 0 when executing a command from the command line
460 * argument, we don't want a line number then */
461 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000462 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 && sourcing_lnum != 0)
464 {
465 p = (char_u *)_("line %4ld:");
466 Buf = alloc((unsigned)(STRLEN(p) + 20));
467 if (Buf != NULL)
468 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
469 return Buf;
470 }
471 return NULL;
472}
473
474/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000475 * Display name and line number for the source of an error.
476 * Remember the file name and line number, so that for the next error the info
477 * is only displayed if it changed.
478 */
479 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100480msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000481{
482 char_u *p;
483
484 ++no_wait_return;
485 p = get_emsg_source();
486 if (p != NULL)
487 {
488 msg_attr(p, attr);
489 vim_free(p);
490 }
491 p = get_emsg_lnum();
492 if (p != NULL)
493 {
494 msg_attr(p, hl_attr(HLF_N));
495 vim_free(p);
496 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
497 }
498
499 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000500 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000501 {
502 vim_free(last_sourcing_name);
503 if (sourcing_name == NULL)
504 last_sourcing_name = NULL;
505 else
506 last_sourcing_name = vim_strsave(sourcing_name);
507 }
508 --no_wait_return;
509}
510
511/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000512 * Return TRUE if not giving error messages right now:
513 * If "emsg_off" is set: no error messages at the moment.
514 * If "msg" is in 'debug': do error message but without side effects.
515 * If "emsg_skip" is set: never do error messages.
516 */
517 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100518emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000519{
520 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
521 && vim_strchr(p_debug, 't') == NULL)
522#ifdef FEAT_EVAL
523 || emsg_skip > 0
524#endif
525 )
526 return TRUE;
527 return FALSE;
528}
529
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200530#if !defined(HAVE_STRERROR) || defined(PROTO)
531/*
532 * Replacement for perror() that behaves more or less like emsg() was called.
533 * v:errmsg will be set and called_emsg will be set.
534 */
535 void
536do_perror(char *msg)
537{
538 perror(msg);
539 ++emsg_silent;
540 emsg((char_u *)msg);
541 --emsg_silent;
542}
543#endif
544
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * emsg() - display an error message
547 *
548 * Rings the bell, if appropriate, and calls message() to do the real work
549 * When terminal not initialized (yet) mch_errmsg(..) is used.
550 *
551 * return TRUE if wait_return not called
552 */
553 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100554emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 char_u *p;
558#ifdef FEAT_EVAL
559 int ignore = FALSE;
560 int severe;
561#endif
562
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100563 /* Skip this if not giving error messages at the moment. */
564 if (emsg_not_now())
565 return TRUE;
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 called_emsg = TRUE;
Bram Moolenaar8b778d52016-02-18 20:31:34 +0100568 if (emsg_silent == 0)
569 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000572 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
573 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 */
575#ifdef FEAT_EVAL
576 severe = emsg_severe;
577 emsg_severe = FALSE;
578#endif
579
Bram Moolenaar57657d82006-04-21 22:12:41 +0000580 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
582#ifdef FEAT_EVAL
583 /*
584 * Cause a throw of an error exception if appropriate. Don't display
585 * the error message in this case. (If no matching catch clause will
586 * be found, the message will be displayed later on.) "ignore" is set
587 * when the message should be ignored completely (used for the
588 * interrupt message).
589 */
590 if (cause_errthrow(s, severe, &ignore) == TRUE)
591 {
592 if (!ignore)
593 did_emsg = TRUE;
594 return TRUE;
595 }
596
597 /* set "v:errmsg", also when using ":silent! cmd" */
598 set_vim_var_string(VV_ERRMSG, s, -1);
599#endif
600
601 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000602 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * But do write it to the redirection file.
604 */
605 if (emsg_silent != 0)
606 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200607 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200609 msg_start();
610 p = get_emsg_source();
611 if (p != NULL)
612 {
613 STRCAT(p, "\n");
614 redir_write(p, -1);
615 vim_free(p);
616 }
617 p = get_emsg_lnum();
618 if (p != NULL)
619 {
620 STRCAT(p, "\n");
621 redir_write(p, -1);
622 vim_free(p);
623 }
624 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 return TRUE;
627 }
628
629 /* Reset msg_silent, an error causes messages to be switched back on. */
630 msg_silent = 0;
631 cmd_silent = FALSE;
632
633 if (global_busy) /* break :global command */
634 ++global_busy;
635
636 if (p_eb)
637 beep_flush(); /* also includes flush_buffers() */
638 else
639 flush_buffers(FALSE); /* flush internal buffers */
640 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
642
643 emsg_on_display = TRUE; /* remember there is an error message */
644 ++msg_scroll; /* don't overwrite a previous message */
645 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000646 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 need_wait_return = TRUE; /* needed in case emsg() is called after
648 * wait_return has reset need_wait_return
649 * and a redraw is expected because
650 * msg_scrolled is non-zero */
651
652 /*
653 * Display name and line number for the source of the error.
654 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000655 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657 /*
658 * Display the error message itself.
659 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000660 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 return msg_attr(s, attr);
662}
663
664/*
665 * Print an error message with one "%s" and one string argument.
666 */
667 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100668emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 return emsg3(s, a1, NULL);
671}
672
Bram Moolenaarea424162005-06-16 21:51:00 +0000673/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100676emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677{
678 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Like msg(), but truncate to a single line if p_shm contains 't', or when
683 * "force" is TRUE. This truncates in another way as for normal messages.
684 * Careful: The string may be changed by msg_may_trunc()!
685 * Returns a pointer to the printed message, if wait_return() not called.
686 */
687 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100688msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689{
690 int n;
691
692 /* Add message to history before truncating */
693 add_msg_hist(s, -1, attr);
694
695 s = msg_may_trunc(force, s);
696
697 msg_hist_off = TRUE;
698 n = msg_attr(s, attr);
699 msg_hist_off = FALSE;
700
701 if (n)
702 return s;
703 return NULL;
704}
705
706/*
707 * Check if message "s" should be truncated at the start (for filenames).
708 * Return a pointer to where the truncated message starts.
709 * Note: May change the message by replacing a character with '<'.
710 */
711 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100712msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
714 int n;
715 int room;
716
717 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
718 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
719 && (n = (int)STRLEN(s) - room) > 0)
720 {
721#ifdef FEAT_MBYTE
722 if (has_mbyte)
723 {
724 int size = vim_strsize(s);
725
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000726 /* There may be room anyway when there are multibyte chars. */
727 if (size <= room)
728 return s;
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 for (n = 0; size >= room; )
731 {
732 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000733 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735 --n;
736 }
737#endif
738 s += n;
739 *s = '<';
740 }
741 return s;
742}
743
744 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100745add_msg_hist(
746 char_u *s,
747 int len, /* -1 for undetermined length */
748 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
750 struct msg_hist *p;
751
752 if (msg_hist_off || msg_silent != 0)
753 return;
754
755 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000756 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000757 (void)delete_first_msg();
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 /* allocate an entry and add the message at the end of the history */
760 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
761 if (p != NULL)
762 {
763 if (len < 0)
764 len = (int)STRLEN(s);
765 /* remove leading and trailing newlines */
766 while (len > 0 && *s == '\n')
767 {
768 ++s;
769 --len;
770 }
771 while (len > 0 && s[len - 1] == '\n')
772 --len;
773 p->msg = vim_strnsave(s, len);
774 p->next = NULL;
775 p->attr = attr;
776 if (last_msg_hist != NULL)
777 last_msg_hist->next = p;
778 last_msg_hist = p;
779 if (first_msg_hist == NULL)
780 first_msg_hist = last_msg_hist;
781 ++msg_hist_len;
782 }
783}
784
785/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000786 * Delete the first (oldest) message from the history.
787 * Returns FAIL if there are no messages.
788 */
789 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100790delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000791{
792 struct msg_hist *p;
793
794 if (msg_hist_len <= 0)
795 return FAIL;
796 p = first_msg_hist;
797 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000798 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200799 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000800 vim_free(p->msg);
801 vim_free(p);
802 --msg_hist_len;
803 return OK;
804}
805
806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 * ":messages" command.
808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200810ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 struct msg_hist *p;
813 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200814 int c = 0;
815
816 if (STRCMP(eap->arg, "clear") == 0)
817 {
818 int keep = eap->addr_count == 0 ? 0 : eap->line2;
819
820 while (msg_hist_len > keep)
821 (void)delete_first_msg();
822 return;
823 }
824
825 if (*eap->arg != NUL)
826 {
827 EMSG(_(e_invarg));
828 return;
829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 msg_hist_off = TRUE;
832
Bram Moolenaar451f8492016-04-14 17:16:22 +0200833 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200834 if (eap->addr_count != 0)
835 {
836 /* Count total messages */
837 for (; p != NULL && !got_int; p = p->next)
838 c++;
839
840 c -= eap->line2;
841
842 /* Skip without number of messages specified */
843 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
844 p = p->next, c--);
845 }
846
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200847 if (p == first_msg_hist)
848 {
849 s = mch_getenv((char_u *)"LANG");
850 if (s != NULL && *s != NUL)
851 msg_attr((char_u *)
852 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
853 hl_attr(HLF_T));
854 }
855
Bram Moolenaar451f8492016-04-14 17:16:22 +0200856 /* Display what was not skipped. */
857 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (p->msg != NULL)
859 msg_attr(p->msg, p->attr);
860
861 msg_hist_off = FALSE;
862}
863
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000864#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865/*
866 * Call this after prompting the user. This will avoid a hit-return message
867 * and a delay.
868 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000869 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100870msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871{
872 need_wait_return = FALSE;
873 emsg_on_display = FALSE;
874 cmdline_row = msg_row;
875 msg_col = 0;
876 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100877 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878}
879#endif
880
881/*
882 * wait for the user to hit a key (normally a return)
883 * if 'redraw' is TRUE, clear and redraw the screen
884 * if 'redraw' is FALSE, just redraw the screen
885 * if 'redraw' is -1, don't redraw at all
886 */
887 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100888wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
890 int c;
891 int oldState;
892 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100894 int save_Recording;
895 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 if (redraw == TRUE)
898 must_redraw = CLEAR;
899
900 /* If using ":silent cmd", don't wait for a return. Also don't set
901 * need_wait_return to do it later. */
902 if (msg_silent != 0)
903 return;
904
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100905 /*
906 * When inside vgetc(), we can't wait for a typed character at all.
907 * With the global command (and some others) we only need one return at
908 * the end. Adjust cmdline_row to avoid the next message overwriting the
909 * last one.
910 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000911 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100913 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (no_wait_return)
915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if (!exmode_active)
917 cmdline_row = msg_row;
918 return;
919 }
920
921 redir_off = TRUE; /* don't redirect this message */
922 oldState = State;
923 if (quit_more)
924 {
925 c = CAR; /* just pretend CR was hit */
926 quit_more = FALSE;
927 got_int = FALSE;
928 }
929 else if (exmode_active)
930 {
931 MSG_PUTS(" "); /* make sure the cursor is on the right line */
932 c = CAR; /* no need for a return in ex mode */
933 got_int = FALSE;
934 }
935 else
936 {
937 /* Make sure the hit-return prompt is on screen when 'guioptions' was
938 * just changed. */
939 screenalloc(FALSE);
940
941 State = HITRETURN;
942#ifdef FEAT_MOUSE
943 setmouse();
944#endif
945#ifdef USE_ON_FLY_SCROLL
946 dont_scroll = TRUE; /* disallow scrolling here */
947#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100948 cmdline_row = msg_row;
949
Bram Moolenaarec38d692013-04-24 15:12:32 +0200950 /* Avoid the sequence that the user types ":" at the hit-return prompt
951 * to start an Ex command, but the file-changed dialog gets in the
952 * way. */
953 if (need_check_timestamps)
954 check_timestamps(FALSE);
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 hit_return_msg();
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 do
959 {
960 /* Remember "got_int", if it is set vgetc() probably returns a
961 * CTRL-C, but we need to loop then. */
962 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000963
964 /* Don't do mappings here, we put the character back in the
965 * typeahead buffer. */
966 ++no_mapping;
967 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100968
969 /* Temporarily disable Recording. If Recording is active, the
970 * character will be recorded later, since it will be added to the
971 * typebuf after the loop */
972 save_Recording = Recording;
973 save_scriptout = scriptout;
974 Recording = FALSE;
975 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000977 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000979 --no_mapping;
980 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100981 Recording = save_Recording;
982 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef FEAT_CLIPBOARD
985 /* Strange way to allow copying (yanking) a modeless selection at
986 * the hit-enter prompt. Use CTRL-Y, because the same is used in
987 * Cmdline-mode and it's harmless when there is no selection. */
988 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
989 {
990 clip_copy_modeless_selection(TRUE);
991 c = K_IGNORE;
992 }
993#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000994
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000995 /*
996 * Allow scrolling back in the messages.
997 * Also accept scroll-down commands when messages fill the screen,
998 * to avoid that typing one 'j' too many makes the messages
999 * disappear.
1000 */
1001 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001002 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001003 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1004 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001005 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001006 if (msg_scrolled > Rows)
1007 /* scroll back to show older messages */
1008 do_more_prompt(c);
1009 else
1010 {
1011 msg_didout = FALSE;
1012 c = K_IGNORE;
1013 msg_col =
1014#ifdef FEAT_RIGHTLEFT
1015 cmdmsg_rl ? Columns - 1 :
1016#endif
1017 0;
1018 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001019 if (quit_more)
1020 {
1021 c = CAR; /* just pretend CR was hit */
1022 quit_more = FALSE;
1023 got_int = FALSE;
1024 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001025 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001026 {
1027 c = K_IGNORE;
1028 hit_return_msg();
1029 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001030 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001031 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001032 && (c == 'j' || c == 'd' || c == 'f'
1033 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001034 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 } while ((had_got_int && c == Ctrl_C)
1037 || c == K_IGNORE
1038#ifdef FEAT_GUI
1039 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1040#endif
1041#ifdef FEAT_MOUSE
1042 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1043 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1044 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001045 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 || c == K_MOUSEDOWN || c == K_MOUSEUP
1047 || (!mouse_has(MOUSE_RETURN)
1048 && mouse_row < msg_row
1049 && (c == K_LEFTMOUSE
1050 || c == K_MIDDLEMOUSE
1051 || c == K_RIGHTMOUSE
1052 || c == K_X1MOUSE
1053 || c == K_X2MOUSE))
1054#endif
1055 );
1056 ui_breakcheck();
1057#ifdef FEAT_MOUSE
1058 /*
1059 * Avoid that the mouse-up event causes visual mode to start.
1060 */
1061 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1062 || c == K_X1MOUSE || c == K_X2MOUSE)
1063 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1064 else
1065#endif
1066 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1067 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001068 /* Put the character back in the typeahead buffer. Don't use the
1069 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001070 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001072 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 redir_off = FALSE;
1076
1077 /*
1078 * If the user hits ':', '?' or '/' we get a command line from the next
1079 * line.
1080 */
1081 if (c == ':' || c == '?' || c == '/')
1082 {
1083 if (!exmode_active)
1084 cmdline_row = msg_row;
1085 skip_redraw = TRUE; /* skip redraw once */
1086 do_redraw = FALSE;
1087 }
1088
1089 /*
1090 * If the window size changed set_shellsize() will redraw the screen.
1091 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1092 * typed.
1093 */
1094 tmpState = State;
1095 State = oldState; /* restore State before set_shellsize */
1096#ifdef FEAT_MOUSE
1097 setmouse();
1098#endif
1099 msg_check();
1100
1101#if defined(UNIX) || defined(VMS)
1102 /*
1103 * When switching screens, we need to output an extra newline on exit.
1104 */
1105 if (swapping_screen() && !termcap_active)
1106 newline_on_exit = TRUE;
1107#endif
1108
1109 need_wait_return = FALSE;
1110 did_wait_return = TRUE;
1111 emsg_on_display = FALSE; /* can delete error message now */
1112 lines_left = -1; /* reset lines_left at next msg_start() */
1113 reset_last_sourcing();
1114 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1115 (Rows - cmdline_row - 1) * Columns + sc_col)
1116 {
1117 vim_free(keep_msg);
1118 keep_msg = NULL; /* don't redisplay message, it's too long */
1119 }
1120
1121 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1122 {
1123 starttermcap(); /* start termcap before redrawing */
1124 shell_resized();
1125 }
1126 else if (!skip_redraw
1127 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1128 {
1129 starttermcap(); /* start termcap before redrawing */
1130 redraw_later(VALID);
1131 }
1132}
1133
1134/*
1135 * Write the hit-return prompt.
1136 */
1137 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001138hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001140 int save_p_more = p_more;
1141
1142 p_more = FALSE; /* don't want see this message when scrolling back */
1143 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 msg_putchar('\n');
1145 if (got_int)
1146 MSG_PUTS(_("Interrupt: "));
1147
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001148 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (!msg_use_printf())
1150 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001151 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152}
1153
1154/*
1155 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1156 */
1157 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001158set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159{
1160 vim_free(keep_msg);
1161 if (s != NULL && msg_silent == 0)
1162 keep_msg = vim_strsave(s);
1163 else
1164 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001165 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001166 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167}
1168
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001169#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1170/*
1171 * If there currently is a message being displayed, set "keep_msg" to it, so
1172 * that it will be displayed again after redraw.
1173 */
1174 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001175set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001176{
1177 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1178 && (State & NORMAL))
1179 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1180}
1181#endif
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183/*
1184 * Prepare for outputting characters in the command line.
1185 */
1186 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001187msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 int did_return = FALSE;
1190
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001191 if (!msg_silent)
1192 {
1193 vim_free(keep_msg);
1194 keep_msg = NULL; /* don't display old message now */
1195 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001196
1197#ifdef FEAT_EVAL
1198 if (need_clr_eos)
1199 {
1200 /* Halfway an ":echo" command and getting an (error) message: clear
1201 * any text from the command. */
1202 need_clr_eos = FALSE;
1203 msg_clr_eos();
1204 }
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (!msg_scroll && full_screen) /* overwrite last message */
1208 {
1209 msg_row = cmdline_row;
1210 msg_col =
1211#ifdef FEAT_RIGHTLEFT
1212 cmdmsg_rl ? Columns - 1 :
1213#endif
1214 0;
1215 }
1216 else if (msg_didout) /* start message on next line */
1217 {
1218 msg_putchar('\n');
1219 did_return = TRUE;
1220 if (exmode_active != EXMODE_NORMAL)
1221 cmdline_row = msg_row;
1222 }
1223 if (!msg_didany || lines_left < 0)
1224 msg_starthere();
1225 if (msg_silent == 0)
1226 {
1227 msg_didout = FALSE; /* no output on current line yet */
1228 cursor_off();
1229 }
1230
1231 /* when redirecting, may need to start a new line. */
1232 if (!did_return)
1233 redir_write((char_u *)"\n", -1);
1234}
1235
1236/*
1237 * Note that the current msg position is where messages start.
1238 */
1239 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001240msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
1242 lines_left = cmdline_row;
1243 msg_didany = FALSE;
1244}
1245
1246 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001247msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 msg_putchar_attr(c, 0);
1250}
1251
1252 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001253msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
1255#ifdef FEAT_MBYTE
1256 char_u buf[MB_MAXBYTES + 1];
1257#else
1258 char_u buf[4];
1259#endif
1260
1261 if (IS_SPECIAL(c))
1262 {
1263 buf[0] = K_SPECIAL;
1264 buf[1] = K_SECOND(c);
1265 buf[2] = K_THIRD(c);
1266 buf[3] = NUL;
1267 }
1268 else
1269 {
1270#ifdef FEAT_MBYTE
1271 buf[(*mb_char2bytes)(c, buf)] = NUL;
1272#else
1273 buf[0] = c;
1274 buf[1] = NUL;
1275#endif
1276 }
1277 msg_puts_attr(buf, attr);
1278}
1279
1280 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001281msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 char_u buf[20];
1284
1285 sprintf((char *)buf, "%ld", n);
1286 msg_puts(buf);
1287}
1288
1289 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001290msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 msg_home_replace_attr(fname, 0);
1293}
1294
1295#if defined(FEAT_FIND_ID) || defined(PROTO)
1296 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001297msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 msg_home_replace_attr(fname, hl_attr(HLF_D));
1300}
1301#endif
1302
1303 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001304msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
1306 char_u *name;
1307
1308 name = home_replace_save(NULL, fname);
1309 if (name != NULL)
1310 msg_outtrans_attr(name, attr);
1311 vim_free(name);
1312}
1313
1314/*
1315 * Output 'len' characters in 'str' (including NULs) with translation
1316 * if 'len' is -1, output upto a NUL character.
1317 * Use attributes 'attr'.
1318 * Return the number of characters it takes on the screen.
1319 */
1320 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001321msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 return msg_outtrans_attr(str, 0);
1324}
1325
1326 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001327msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1330}
1331
1332 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001333msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 return msg_outtrans_len_attr(str, len, 0);
1336}
1337
1338/*
1339 * Output one character at "p". Return pointer to the next character.
1340 * Handles multi-byte characters.
1341 */
1342 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001343msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345#ifdef FEAT_MBYTE
1346 int l;
1347
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001348 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
1350 msg_outtrans_len_attr(p, l, attr);
1351 return p + l;
1352 }
1353#endif
1354 msg_puts_attr(transchar_byte(*p), attr);
1355 return p + 1;
1356}
1357
1358 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001359msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 int retval = 0;
1362 char_u *str = msgstr;
1363 char_u *plain_start = msgstr;
1364 char_u *s;
1365#ifdef FEAT_MBYTE
1366 int mb_l;
1367 int c;
1368#endif
1369
1370 /* if MSG_HIST flag set, add message to history */
1371 if (attr & MSG_HIST)
1372 {
1373 add_msg_hist(str, len, attr);
1374 attr &= ~MSG_HIST;
1375 }
1376
1377#ifdef FEAT_MBYTE
1378 /* If the string starts with a composing character first draw a space on
1379 * which the composing char can be drawn. */
1380 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1381 msg_puts_attr((char_u *)" ", attr);
1382#endif
1383
1384 /*
1385 * Go over the string. Special characters are translated and printed.
1386 * Normal characters are printed several at a time.
1387 */
1388 while (--len >= 0)
1389 {
1390#ifdef FEAT_MBYTE
1391 if (enc_utf8)
1392 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001393 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001395 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 else
1397 mb_l = 1;
1398 if (has_mbyte && mb_l > 1)
1399 {
1400 c = (*mb_ptr2char)(str);
1401 if (vim_isprintc(c))
1402 /* printable multi-byte char: count the cells. */
1403 retval += (*mb_ptr2cells)(str);
1404 else
1405 {
1406 /* unprintable multi-byte char: print the printable chars so
1407 * far and the translation of the unprintable char. */
1408 if (str > plain_start)
1409 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1410 attr);
1411 plain_start = str + mb_l;
1412 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1413 retval += char2cells(c);
1414 }
1415 len -= mb_l - 1;
1416 str += mb_l;
1417 }
1418 else
1419#endif
1420 {
1421 s = transchar_byte(*str);
1422 if (s[1] != NUL)
1423 {
1424 /* unprintable char: print the printable chars so far and the
1425 * translation of the unprintable char. */
1426 if (str > plain_start)
1427 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1428 attr);
1429 plain_start = str + 1;
1430 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001431 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001433 else
1434 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 ++str;
1436 }
1437 }
1438
1439 if (str > plain_start)
1440 /* print the printable chars at the end */
1441 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1442
1443 return retval;
1444}
1445
1446#if defined(FEAT_QUICKFIX) || defined(PROTO)
1447 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001448msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 int i;
1451 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1452
1453 arg = skipwhite(arg);
1454 for (i = 5; *arg && i >= 0; --i)
1455 if (*arg++ != str[i])
1456 break;
1457 if (i < 0)
1458 {
1459 msg_putchar('\n');
1460 for (i = 0; rs[i]; ++i)
1461 msg_putchar(rs[i] - 3);
1462 }
1463}
1464#endif
1465
1466/*
1467 * Output the string 'str' upto a NUL character.
1468 * Return the number of characters it takes on the screen.
1469 *
1470 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1471 * following character and shown as <F1>, <S-Up> etc. Any other character
1472 * which is not printable shown in <> form.
1473 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1474 * If a character is displayed in one of these special ways, is also
1475 * highlighted (its highlight name is '8' in the p_hl variable).
1476 * Otherwise characters are not highlighted.
1477 * This function is used to show mappings, where we want to see how to type
1478 * the character/string -- webb
1479 */
1480 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001481msg_outtrans_special(
1482 char_u *strstart,
1483 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 char_u *str = strstart;
1486 int retval = 0;
1487 char_u *string;
1488 int attr;
1489 int len;
1490
1491 attr = hl_attr(HLF_8);
1492 while (*str != NUL)
1493 {
1494 /* Leading and trailing spaces need to be displayed in <> form. */
1495 if ((str == strstart || str[1] == NUL) && *str == ' ')
1496 {
1497 string = (char_u *)"<Space>";
1498 ++str;
1499 }
1500 else
1501 string = str2special(&str, from);
1502 len = vim_strsize(string);
1503 /* Highlight special keys */
1504 msg_puts_attr(string, len > 1
1505#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001506 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#endif
1508 ? attr : 0);
1509 retval += len;
1510 }
1511 return retval;
1512}
1513
Bram Moolenaarbd743252010-10-20 21:23:33 +02001514#if defined(FEAT_EVAL) || defined(PROTO)
1515/*
1516 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1517 * strings, in an allocated string.
1518 */
1519 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001520str2special_save(
1521 char_u *str,
1522 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001523{
1524 garray_T ga;
1525 char_u *p = str;
1526
1527 ga_init2(&ga, 1, 40);
1528 while (*p != NUL)
1529 ga_concat(&ga, str2special(&p, is_lhs));
1530 ga_append(&ga, NUL);
1531 return (char_u *)ga.ga_data;
1532}
1533#endif
1534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535/*
1536 * Return the printable string for the key codes at "*sp".
1537 * Used for translating the lhs or rhs of a mapping to printable chars.
1538 * Advances "sp" to the next code.
1539 */
1540 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001541str2special(
1542 char_u **sp,
1543 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 int c;
1546 static char_u buf[7];
1547 char_u *str = *sp;
1548 int modifiers = 0;
1549 int special = FALSE;
1550
1551#ifdef FEAT_MBYTE
1552 if (has_mbyte)
1553 {
1554 char_u *p;
1555
1556 /* Try to un-escape a multi-byte character. Return the un-escaped
1557 * string if it is a multi-byte character. */
1558 p = mb_unescape(sp);
1559 if (p != NULL)
1560 return p;
1561 }
1562#endif
1563
1564 c = *str;
1565 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1566 {
1567 if (str[1] == KS_MODIFIER)
1568 {
1569 modifiers = str[2];
1570 str += 3;
1571 c = *str;
1572 }
1573 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1574 {
1575 c = TO_SPECIAL(str[1], str[2]);
1576 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001577 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 c = NUL;
1579 }
1580 if (IS_SPECIAL(c) || modifiers) /* special key */
1581 special = TRUE;
1582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001585 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001587 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001588
1589 /* For multi-byte characters check for an illegal byte. */
1590 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1591 {
1592 transchar_nonprint(buf, c);
1593 *sp = str + 1;
1594 return buf;
1595 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001596 /* Since 'special' is TRUE the multi-byte character 'c' will be
1597 * processed by get_special_key_name() */
1598 c = (*mb_ptr2char)(str);
1599 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001603 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
1605 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1606 * Use <Space> only for lhs of a mapping. */
1607 if (special || char2cells(c) > 1 || (from && c == ' '))
1608 return get_special_key_name(c, modifiers);
1609 buf[0] = c;
1610 buf[1] = NUL;
1611 return buf;
1612}
1613
1614/*
1615 * Translate a key sequence into special key names.
1616 */
1617 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001618str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 char_u *s;
1621
1622 *buf = NUL;
1623 while (*sp)
1624 {
1625 s = str2special(&sp, FALSE);
1626 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1627 STRCAT(buf, s);
1628 }
1629}
1630
1631/*
1632 * print line for :print or :list command
1633 */
1634 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001635msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 int c;
1638 int col = 0;
1639 int n_extra = 0;
1640 int c_extra = 0;
1641 char_u *p_extra = NULL; /* init to make SASC shut up */
1642 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001643 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 char_u *trail = NULL;
1645#ifdef FEAT_MBYTE
1646 int l;
1647 char_u buf[MB_MAXBYTES + 1];
1648#endif
1649
Bram Moolenaardf177f62005-02-22 08:39:57 +00001650 if (curwin->w_p_list)
1651 list = TRUE;
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001654 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 {
1656 trail = s + STRLEN(s);
1657 while (trail > s && vim_iswhite(trail[-1]))
1658 --trail;
1659 }
1660
1661 /* output a space for an empty line, otherwise the line will be
1662 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001663 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 msg_putchar(' ');
1665
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001666 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001668 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
1670 --n_extra;
1671 if (c_extra)
1672 c = c_extra;
1673 else
1674 c = *p_extra++;
1675 }
1676#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001677 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001680 if (lcs_nbsp != NUL && list
1681 && (mb_ptr2char(s) == 160
1682 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001683 {
1684 mb_char2bytes(lcs_nbsp, buf);
1685 buf[(*mb_ptr2len)(buf)] = NUL;
1686 }
1687 else
1688 {
1689 mch_memmove(buf, s, (size_t)l);
1690 buf[l] = NUL;
1691 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001692 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 s += l;
1694 continue;
1695 }
1696#endif
1697 else
1698 {
1699 attr = 0;
1700 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001701 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
1703 /* tab amount depends on current column */
1704 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001705 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 c = ' ';
1708 c_extra = ' ';
1709 }
1710 else
1711 {
1712 c = lcs_tab1;
1713 c_extra = lcs_tab2;
1714 attr = hl_attr(HLF_8);
1715 }
1716 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001717 else if (c == 160 && list && lcs_nbsp != NUL)
1718 {
1719 c = lcs_nbsp;
1720 attr = hl_attr(HLF_8);
1721 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001722 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
1724 p_extra = (char_u *)"";
1725 c_extra = NUL;
1726 n_extra = 1;
1727 c = lcs_eol;
1728 attr = hl_attr(HLF_AT);
1729 --s;
1730 }
1731 else if (c != NUL && (n = byte2cells(c)) > 1)
1732 {
1733 n_extra = n - 1;
1734 p_extra = transchar_byte(c);
1735 c_extra = NUL;
1736 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001737 /* Use special coloring to be able to distinguish <hex> from
1738 * the same in plain text. */
1739 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
1741 else if (c == ' ' && trail != NULL && s > trail)
1742 {
1743 c = lcs_trail;
1744 attr = hl_attr(HLF_8);
1745 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001746 else if (c == ' ' && list && lcs_space != NUL)
1747 {
1748 c = lcs_space;
1749 attr = hl_attr(HLF_8);
1750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752
1753 if (c == NUL)
1754 break;
1755
1756 msg_putchar_attr(c, attr);
1757 col++;
1758 }
1759 msg_clr_eos();
1760}
1761
1762#ifdef FEAT_MBYTE
1763/*
1764 * Use screen_puts() to output one multi-byte character.
1765 * Return the pointer "s" advanced to the next character.
1766 */
1767 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001768screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 int cw;
1771
1772 msg_didout = TRUE; /* remember that line is not empty */
1773 cw = (*mb_ptr2cells)(s);
1774 if (cw > 1 && (
1775#ifdef FEAT_RIGHTLEFT
1776 cmdmsg_rl ? msg_col <= 1 :
1777#endif
1778 msg_col == Columns - 1))
1779 {
1780 /* Doesn't fit, print a highlighted '>' to fill it up. */
1781 msg_screen_putchar('>', hl_attr(HLF_AT));
1782 return s;
1783 }
1784
1785 screen_puts_len(s, l, msg_row, msg_col, attr);
1786#ifdef FEAT_RIGHTLEFT
1787 if (cmdmsg_rl)
1788 {
1789 msg_col -= cw;
1790 if (msg_col == 0)
1791 {
1792 msg_col = Columns;
1793 ++msg_row;
1794 }
1795 }
1796 else
1797#endif
1798 {
1799 msg_col += cw;
1800 if (msg_col >= Columns)
1801 {
1802 msg_col = 0;
1803 ++msg_row;
1804 }
1805 }
1806 return s + l;
1807}
1808#endif
1809
1810/*
1811 * Output a string to the screen at position msg_row, msg_col.
1812 * Update msg_row and msg_col for the next message.
1813 */
1814 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001815msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001817 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818}
1819
1820 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001821msg_puts_title(
1822 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 msg_puts_attr(s, hl_attr(HLF_T));
1825}
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827/*
1828 * Show a message in such a way that it always fits in the line. Cut out a
1829 * part in the middle and replace it with "..." when necessary.
1830 * Does not handle multi-byte characters!
1831 */
1832 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001833msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001835 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836}
1837
1838 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001839msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 int slen = len;
1842 int room;
1843
1844 room = Columns - msg_col;
1845 if (len > room && room >= 20)
1846 {
1847 slen = (room - 3) / 2;
1848 msg_outtrans_len_attr(longstr, slen, attr);
1849 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1850 }
1851 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1852}
1853
1854/*
1855 * Basic function for writing a message with highlight attributes.
1856 */
1857 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001858msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859{
1860 msg_puts_attr_len(s, -1, attr);
1861}
1862
1863/*
1864 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1865 * When "maxlen" is -1 there is no maximum length.
1866 * When "maxlen" is >= 0 the message is not put in the history.
1867 */
1868 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001869msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 /*
1872 * If redirection is on, also write to the redirection file.
1873 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001874 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
1876 /*
1877 * Don't print anything when using ":silent cmd".
1878 */
1879 if (msg_silent != 0)
1880 return;
1881
1882 /* if MSG_HIST flag set, add message to history */
1883 if ((attr & MSG_HIST) && maxlen < 0)
1884 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001885 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 attr &= ~MSG_HIST;
1887 }
1888
1889 /*
1890 * When writing something to the screen after it has scrolled, requires a
1891 * wait-return prompt later. Needed when scrolling, resetting
1892 * need_wait_return after some prompt, and then outputting something
1893 * without scrolling
1894 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001895 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 need_wait_return = TRUE;
1897 msg_didany = TRUE; /* remember that something was outputted */
1898
1899 /*
1900 * If there is no valid screen, use fprintf so we can see error messages.
1901 * If termcap is not active, we may be writing in an alternate console
1902 * window, cursor positioning may not work correctly (window size may be
1903 * different, e.g. for Win32 console) or we just don't know where the
1904 * cursor is.
1905 */
1906 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001907 msg_puts_printf(str, maxlen);
1908 else
1909 msg_puts_display(str, maxlen, attr, FALSE);
1910}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001912/*
1913 * The display part of msg_puts_attr_len().
1914 * May be called recursively to display scroll-back text.
1915 */
1916 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001917msg_puts_display(
1918 char_u *str,
1919 int maxlen,
1920 int attr,
1921 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001922{
1923 char_u *s = str;
1924 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1925 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1926#ifdef FEAT_MBYTE
1927 int l;
1928 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001930 char_u *sb_str = str;
1931 int sb_col = msg_col;
1932 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001933 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
1935 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001936 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
1938 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001939 * We are at the end of the screen line when:
1940 * - When outputting a newline.
1941 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001943 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944#ifdef FEAT_RIGHTLEFT
1945 cmdmsg_rl
1946 ? (
1947 msg_col <= 1
1948 || (*s == TAB && msg_col <= 7)
1949# ifdef FEAT_MBYTE
1950 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1951# endif
1952 )
1953 :
1954#endif
1955 (msg_col + t_col >= Columns - 1
1956 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1957# ifdef FEAT_MBYTE
1958 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1959 && msg_col + t_col >= Columns - 2)
1960# endif
1961 ))))
1962 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001963 /*
1964 * The screen is scrolled up when at the last row (some terminals
1965 * scroll automatically, some don't. To avoid problems we scroll
1966 * ourselves).
1967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001970 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001972 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (msg_no_more && lines_left == 0)
1974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001976 /* Scroll the screen up one line. */
1977 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979 msg_row = Rows - 2;
1980 if (msg_col >= Columns) /* can happen after screen resize */
1981 msg_col = Columns - 1;
1982
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001983 /* Display char in last column before showing more-prompt. */
1984 if (*s >= ' '
1985#ifdef FEAT_RIGHTLEFT
1986 && !cmdmsg_rl
1987#endif
1988 )
1989 {
1990#ifdef FEAT_MBYTE
1991 if (has_mbyte)
1992 {
1993 if (enc_utf8 && maxlen >= 0)
1994 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001995 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001996 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001997 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001998 s = screen_puts_mbyte(s, l, attr);
1999 }
2000 else
2001#endif
2002 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002003 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002004 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002005 else
2006 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002007
2008 if (p_more)
2009 /* store text for scrolling back */
2010 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2011
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002012 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002013 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (must_redraw < VALID)
2015 must_redraw = VALID;
2016 redraw_cmdline = TRUE;
2017 if (cmdline_row > 0 && !exmode_active)
2018 --cmdline_row;
2019
2020 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002021 * If screen is completely filled and 'more' is set then wait
2022 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002024 if (lines_left > 0)
2025 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002026 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 && !msg_no_more && !exmode_active)
2028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002030 if (do_more_prompt(NUL))
2031 s = confirm_msg_tail;
2032#else
2033 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034#endif
2035 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002036 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002038
2039 /* When we displayed a char in last column need to check if there
2040 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002041 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002042 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002045 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 || msg_col + t_col >= Columns
2047#ifdef FEAT_MBYTE
2048 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2049 && msg_col + t_col >= Columns - 1)
2050#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002051 ;
2052 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2053 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002055 t_puts(&t_col, t_s, s, attr);
2056
2057 if (wrap && p_more && !recurse)
2058 /* store text for scrolling back */
2059 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061 if (*s == '\n') /* go to next line */
2062 {
2063 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002064#ifdef FEAT_RIGHTLEFT
2065 if (cmdmsg_rl)
2066 msg_col = Columns - 1;
2067 else
2068#endif
2069 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (++msg_row >= Rows) /* safety check */
2071 msg_row = Rows - 1;
2072 }
2073 else if (*s == '\r') /* go to column 0 */
2074 {
2075 msg_col = 0;
2076 }
2077 else if (*s == '\b') /* go to previous char */
2078 {
2079 if (msg_col)
2080 --msg_col;
2081 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002082 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 do
2085 msg_screen_putchar(' ', attr);
2086 while (msg_col & 7);
2087 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002088 else if (*s == BELL) /* beep (from ":sh") */
2089 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else
2091 {
2092#ifdef FEAT_MBYTE
2093 if (has_mbyte)
2094 {
2095 cw = (*mb_ptr2cells)(s);
2096 if (enc_utf8 && maxlen >= 0)
2097 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002098 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002100 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 else
2103 {
2104 cw = 1;
2105 l = 1;
2106 }
2107#endif
2108 /* When drawing from right to left or when a double-wide character
2109 * doesn't fit, draw a single character here. Otherwise collect
2110 * characters and draw them all at once later. */
2111#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2112 if (
2113# ifdef FEAT_RIGHTLEFT
2114 cmdmsg_rl
2115# ifdef FEAT_MBYTE
2116 ||
2117# endif
2118# endif
2119# ifdef FEAT_MBYTE
2120 (cw > 1 && msg_col + t_col >= Columns - 1)
2121# endif
2122 )
2123 {
2124# ifdef FEAT_MBYTE
2125 if (l > 1)
2126 s = screen_puts_mbyte(s, l, attr) - 1;
2127 else
2128# endif
2129 msg_screen_putchar(*s, attr);
2130 }
2131 else
2132#endif
2133 {
2134 /* postpone this character until later */
2135 if (t_col == 0)
2136 t_s = s;
2137#ifdef FEAT_MBYTE
2138 t_col += cw;
2139 s += l - 1;
2140#else
2141 ++t_col;
2142#endif
2143 }
2144 }
2145 ++s;
2146 }
2147
2148 /* output any postponed text */
2149 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002150 t_puts(&t_col, t_s, s, attr);
2151 if (p_more && !recurse)
2152 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 msg_check();
2155}
2156
2157/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002158 * Return TRUE when ":filter pattern" was used and "msg" does not match
2159 * "pattern".
2160 */
2161 int
2162message_filtered(char_u *msg)
2163{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002164 int match;
2165
2166 if (cmdmod.filter_regmatch.regprog == NULL)
2167 return FALSE;
2168 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2169 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002170}
2171
2172/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002173 * Scroll the screen up one line for displaying the next message line.
2174 */
2175 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002176msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002177{
2178#ifdef FEAT_GUI
2179 /* Remove the cursor before scrolling, ScreenLines[] is going
2180 * to become invalid. */
2181 if (gui.in_use)
2182 gui_undraw_cursor();
2183#endif
2184 /* scrolling up always works */
2185 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2186
2187 if (!can_clear((char_u *)" "))
2188 {
2189 /* Scrolling up doesn't result in the right background. Set the
2190 * background here. It's not efficient, but avoids that we have to do
2191 * it all over the code. */
2192 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2193
2194 /* Also clear the last char of the last but one line if it was not
2195 * cleared before to avoid a scroll-up. */
2196 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2197 screen_fill((int)Rows - 2, (int)Rows - 1,
2198 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2199 }
2200}
2201
2202/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002203 * Increment "msg_scrolled".
2204 */
2205 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002206inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002207{
2208#ifdef FEAT_EVAL
2209 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2210 {
2211 char_u *p = sourcing_name;
2212 char_u *tofree = NULL;
2213 int len;
2214
2215 /* v:scrollstart is empty, set it to the script/function name and line
2216 * number */
2217 if (p == NULL)
2218 p = (char_u *)_("Unknown");
2219 else
2220 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002221 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002222 tofree = alloc(len);
2223 if (tofree != NULL)
2224 {
2225 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2226 p, (long)sourcing_lnum);
2227 p = tofree;
2228 }
2229 }
2230 set_vim_var_string(VV_SCROLLSTART, p, -1);
2231 vim_free(tofree);
2232 }
2233#endif
2234 ++msg_scrolled;
2235}
2236
2237/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002238 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2239 * store the displayed text and remember where screen lines start.
2240 */
2241typedef struct msgchunk_S msgchunk_T;
2242struct msgchunk_S
2243{
2244 msgchunk_T *sb_next;
2245 msgchunk_T *sb_prev;
2246 char sb_eol; /* TRUE when line ends after this text */
2247 int sb_msg_col; /* column in which text starts */
2248 int sb_attr; /* text attributes */
2249 char_u sb_text[1]; /* text to be displayed, actually longer */
2250};
2251
2252static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2253
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002254static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2255static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002256
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002257static int do_clear_sb_text = FALSE; /* clear text on next msg */
2258
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002259/*
2260 * Store part of a printed message for displaying when scrolling back.
2261 */
2262 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002263store_sb_text(
2264 char_u **sb_str, /* start of string */
2265 char_u *s, /* just after string */
2266 int attr,
2267 int *sb_col,
2268 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002269{
2270 msgchunk_T *mp;
2271
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002272 if (do_clear_sb_text)
2273 {
2274 clear_sb_text();
2275 do_clear_sb_text = FALSE;
2276 }
2277
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002278 if (s > *sb_str)
2279 {
2280 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2281 if (mp != NULL)
2282 {
2283 mp->sb_eol = finish;
2284 mp->sb_msg_col = *sb_col;
2285 mp->sb_attr = attr;
2286 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2287
2288 if (last_msgchunk == NULL)
2289 {
2290 last_msgchunk = mp;
2291 mp->sb_prev = NULL;
2292 }
2293 else
2294 {
2295 mp->sb_prev = last_msgchunk;
2296 last_msgchunk->sb_next = mp;
2297 last_msgchunk = mp;
2298 }
2299 mp->sb_next = NULL;
2300 }
2301 }
2302 else if (finish && last_msgchunk != NULL)
2303 last_msgchunk->sb_eol = TRUE;
2304
2305 *sb_str = s;
2306 *sb_col = 0;
2307}
2308
2309/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002310 * Finished showing messages, clear the scroll-back text on the next message.
2311 */
2312 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002313may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002314{
2315 do_clear_sb_text = TRUE;
2316}
2317
2318/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002319 * Clear any text remembered for scrolling back.
2320 * Called when redrawing the screen.
2321 */
2322 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002323clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002324{
2325 msgchunk_T *mp;
2326
2327 while (last_msgchunk != NULL)
2328 {
2329 mp = last_msgchunk->sb_prev;
2330 vim_free(last_msgchunk);
2331 last_msgchunk = mp;
2332 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002333}
2334
2335/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002336 * "g<" command.
2337 */
2338 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002339show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002340{
2341 msgchunk_T *mp;
2342
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002343 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002344 * weird, typing a command without output results in one line. */
2345 mp = msg_sb_start(last_msgchunk);
2346 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002347 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002348 else
2349 {
2350 do_more_prompt('G');
2351 wait_return(FALSE);
2352 }
2353}
2354
2355/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002356 * Move to the start of screen line in already displayed text.
2357 */
2358 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002359msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002360{
2361 msgchunk_T *mp = mps;
2362
2363 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2364 mp = mp->sb_prev;
2365 return mp;
2366}
2367
2368/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002369 * Mark the last message chunk as finishing the line.
2370 */
2371 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002372msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002373{
2374 if (last_msgchunk != NULL)
2375 last_msgchunk->sb_eol = TRUE;
2376}
2377
2378/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002379 * Display a screen line from previously displayed text at row "row".
2380 * Returns a pointer to the text for the next line (can be NULL).
2381 */
2382 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002383disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002384{
2385 msgchunk_T *mp = smp;
2386 char_u *p;
2387
2388 for (;;)
2389 {
2390 msg_row = row;
2391 msg_col = mp->sb_msg_col;
2392 p = mp->sb_text;
2393 if (*p == '\n') /* don't display the line break */
2394 ++p;
2395 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2396 if (mp->sb_eol || mp->sb_next == NULL)
2397 break;
2398 mp = mp->sb_next;
2399 }
2400 return mp->sb_next;
2401}
2402
2403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 * Output any postponed text for msg_puts_attr_len().
2405 */
2406 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002407t_puts(
2408 int *t_col,
2409 char_u *t_s,
2410 char_u *s,
2411 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 /* output postponed text */
2414 msg_didout = TRUE; /* remember that line is not empty */
2415 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002416 msg_col += *t_col;
2417 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#ifdef FEAT_MBYTE
2419 /* If the string starts with a composing character don't increment the
2420 * column position for it. */
2421 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2422 --msg_col;
2423#endif
2424 if (msg_col >= Columns)
2425 {
2426 msg_col = 0;
2427 ++msg_row;
2428 }
2429}
2430
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431/*
2432 * Returns TRUE when messages should be printed with mch_errmsg().
2433 * This is used when there is no valid screen, so we can see error messages.
2434 * If termcap is not active, we may be writing in an alternate console
2435 * window, cursor positioning may not work correctly (window size may be
2436 * different, e.g. for Win32 console) or we just don't know where the
2437 * cursor is.
2438 */
2439 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002440msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 return (!msg_check_screen()
2443#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2444 || !termcap_active
2445#endif
2446 || (swapping_screen() && !termcap_active)
2447 );
2448}
2449
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002450/*
2451 * Print a message when there is no valid screen.
2452 */
2453 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002454msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002455{
2456 char_u *s = str;
2457 char_u buf[4];
2458 char_u *p;
2459
2460#ifdef WIN3264
2461 if (!(silent_mode && p_verbose == 0))
2462 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2463#endif
2464 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2465 {
2466 if (!(silent_mode && p_verbose == 0))
2467 {
2468 /* NL --> CR NL translation (for Unix, not for "--version") */
2469 /* NL --> CR translation (for Mac) */
2470 p = &buf[0];
2471 if (*s == '\n' && !info_message)
2472 *p++ = '\r';
2473#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2474 else
2475#endif
2476 *p++ = *s;
2477 *p = '\0';
2478 if (info_message) /* informative message, not an error */
2479 mch_msg((char *)buf);
2480 else
2481 mch_errmsg((char *)buf);
2482 }
2483
2484 /* primitive way to compute the current column */
2485#ifdef FEAT_RIGHTLEFT
2486 if (cmdmsg_rl)
2487 {
2488 if (*s == '\r' || *s == '\n')
2489 msg_col = Columns - 1;
2490 else
2491 --msg_col;
2492 }
2493 else
2494#endif
2495 {
2496 if (*s == '\r' || *s == '\n')
2497 msg_col = 0;
2498 else
2499 ++msg_col;
2500 }
2501 ++s;
2502 }
2503 msg_didout = TRUE; /* assume that line is not empty */
2504
2505#ifdef WIN3264
2506 if (!(silent_mode && p_verbose == 0))
2507 mch_settmode(TMODE_RAW);
2508#endif
2509}
2510
2511/*
2512 * Show the more-prompt and handle the user response.
2513 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002514 * When at hit-enter prompt "typed_char" is the already typed character,
2515 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002516 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2517 */
2518 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002519do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002520{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002521 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002522 int used_typed_char = typed_char;
2523 int oldState = State;
2524 int c;
2525#ifdef FEAT_CON_DIALOG
2526 int retval = FALSE;
2527#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002528 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002529 msgchunk_T *mp_last = NULL;
2530 msgchunk_T *mp;
2531 int i;
2532
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002533 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002534 * that case don't show another prompt. Also when at the hit-Enter prompt
2535 * and nothing was typed. */
2536 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002537 return FALSE;
2538 entered = TRUE;
2539
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002540 if (typed_char == 'G')
2541 {
2542 /* "g<": Find first line on the last page. */
2543 mp_last = msg_sb_start(last_msgchunk);
2544 for (i = 0; i < Rows - 2 && mp_last != NULL
2545 && mp_last->sb_prev != NULL; ++i)
2546 mp_last = msg_sb_start(mp_last->sb_prev);
2547 }
2548
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002549 State = ASKMORE;
2550#ifdef FEAT_MOUSE
2551 setmouse();
2552#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002553 if (typed_char == NUL)
2554 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002555 for (;;)
2556 {
2557 /*
2558 * Get a typed character directly from the user.
2559 */
2560 if (used_typed_char != NUL)
2561 {
2562 c = used_typed_char; /* was typed at hit-enter prompt */
2563 used_typed_char = NUL;
2564 }
2565 else
2566 c = get_keystroke();
2567
2568#if defined(FEAT_MENU) && defined(FEAT_GUI)
2569 if (c == K_MENU)
2570 {
2571 int idx = get_menu_index(current_menu, ASKMORE);
2572
2573 /* Used a menu. If it starts with CTRL-Y, it must
2574 * be a "Copy" for the clipboard. Otherwise
2575 * assume that we end */
2576 if (idx == MENU_INDEX_INVALID)
2577 continue;
2578 c = *current_menu->strings[idx];
2579 if (c != NUL && current_menu->strings[idx][1] != NUL)
2580 ins_typebuf(current_menu->strings[idx] + 1,
2581 current_menu->noremap[idx], 0, TRUE,
2582 current_menu->silent[idx]);
2583 }
2584#endif
2585
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002586 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587 switch (c)
2588 {
2589 case BS: /* scroll one line back */
2590 case K_BS:
2591 case 'k':
2592 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002593 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002594 break;
2595
2596 case CAR: /* one extra line */
2597 case NL:
2598 case 'j':
2599 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002600 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002601 break;
2602
2603 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002604 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002605 break;
2606
2607 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002608 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002609 break;
2610
2611 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002612 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002613 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002614 break;
2615
2616 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002617 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618 case K_PAGEDOWN:
2619 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002620 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002621 break;
2622
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002623 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002624 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002625 break;
2626
2627 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002628 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002629 lines_left = 999999;
2630 break;
2631
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002632 case ':': /* start new command line */
2633#ifdef FEAT_CON_DIALOG
2634 if (!confirm_msg_used)
2635#endif
2636 {
2637 /* Since got_int is set all typeahead will be flushed, but we
2638 * want to keep this ':', remember that in a special way. */
2639 typeahead_noflush(':');
2640 cmdline_row = Rows - 1; /* put ':' on this line */
2641 skip_redraw = TRUE; /* skip redraw once */
2642 need_wait_return = FALSE; /* don't wait in main() */
2643 }
2644 /*FALLTHROUGH*/
2645 case 'q': /* quit */
2646 case Ctrl_C:
2647 case ESC:
2648#ifdef FEAT_CON_DIALOG
2649 if (confirm_msg_used)
2650 {
2651 /* Jump to the choices of the dialog. */
2652 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002653 }
2654 else
2655#endif
2656 {
2657 got_int = TRUE;
2658 quit_more = TRUE;
2659 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002660 /* When there is some more output (wrapping line) display that
2661 * without another prompt. */
2662 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002663 break;
2664
2665#ifdef FEAT_CLIPBOARD
2666 case Ctrl_Y:
2667 /* Strange way to allow copying (yanking) a modeless
2668 * selection at the more prompt. Use CTRL-Y,
2669 * because the same is used in Cmdline-mode and at the
2670 * hit-enter prompt. However, scrolling one line up
2671 * might be expected... */
2672 if (clip_star.state == SELECT_DONE)
2673 clip_copy_modeless_selection(TRUE);
2674 continue;
2675#endif
2676 default: /* no valid response */
2677 msg_moremsg(TRUE);
2678 continue;
2679 }
2680
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002681 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002682 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002683 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684 {
2685 /* go to start of last line */
2686 if (mp_last == NULL)
2687 mp = msg_sb_start(last_msgchunk);
2688 else if (mp_last->sb_prev != NULL)
2689 mp = msg_sb_start(mp_last->sb_prev);
2690 else
2691 mp = NULL;
2692
2693 /* go to start of line at top of the screen */
2694 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2695 ++i)
2696 mp = msg_sb_start(mp->sb_prev);
2697
2698 if (mp != NULL && mp->sb_prev != NULL)
2699 {
2700 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002701 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002702 {
2703 if (mp == NULL || mp->sb_prev == NULL)
2704 break;
2705 mp = msg_sb_start(mp->sb_prev);
2706 if (mp_last == NULL)
2707 mp_last = msg_sb_start(last_msgchunk);
2708 else
2709 mp_last = msg_sb_start(mp_last->sb_prev);
2710 }
2711
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002712 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002713 (int)Rows, NULL) == OK)
2714 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002715 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002716 (void)disp_sb_line(0, mp);
2717 }
2718 else
2719 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002720 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002722 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2723 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002724 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002725 ++msg_scrolled;
2726 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002728 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729 }
2730 }
2731 else
2732 {
2733 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002734 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002735 {
2736 /* scroll up, display line at bottom */
2737 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002738 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002739 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2740 (int)Columns, ' ', ' ', 0);
2741 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002742 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 }
2744 }
2745
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002746 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002747 {
2748 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002749 screen_fill((int)Rows - 1, (int)Rows, 0,
2750 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002751 msg_moremsg(FALSE);
2752 continue;
2753 }
2754
2755 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002756 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002757 }
2758
2759 break;
2760 }
2761
2762 /* clear the --more-- message */
2763 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2764 State = oldState;
2765#ifdef FEAT_MOUSE
2766 setmouse();
2767#endif
2768 if (quit_more)
2769 {
2770 msg_row = Rows - 1;
2771 msg_col = 0;
2772 }
2773#ifdef FEAT_RIGHTLEFT
2774 else if (cmdmsg_rl)
2775 msg_col = Columns - 1;
2776#endif
2777
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002778 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002779#ifdef FEAT_CON_DIALOG
2780 return retval;
2781#else
2782 return FALSE;
2783#endif
2784}
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2787
2788#ifdef mch_errmsg
2789# undef mch_errmsg
2790#endif
2791#ifdef mch_msg
2792# undef mch_msg
2793#endif
2794
2795/*
2796 * Give an error message. To be used when the screen hasn't been initialized
2797 * yet. When stderr can't be used, collect error messages until the GUI has
2798 * started and they can be displayed in a message box.
2799 */
2800 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002801mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
2803 int len;
2804
2805#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2806 /* On Unix use stderr if it's a tty.
2807 * When not going to start the GUI also use stderr.
2808 * On Mac, when started from Finder, stderr is the console. */
2809 if (
2810# ifdef UNIX
2811# ifdef MACOS_X_UNIX
2812 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2813# else
2814 isatty(2)
2815# endif
2816# ifdef FEAT_GUI
2817 ||
2818# endif
2819# endif
2820# ifdef FEAT_GUI
2821 !(gui.in_use || gui.starting)
2822# endif
2823 )
2824 {
2825 fprintf(stderr, "%s", str);
2826 return;
2827 }
2828#endif
2829
2830 /* avoid a delay for a message that isn't there */
2831 emsg_on_display = FALSE;
2832
2833 len = (int)STRLEN(str) + 1;
2834 if (error_ga.ga_growsize == 0)
2835 {
2836 error_ga.ga_growsize = 80;
2837 error_ga.ga_itemsize = 1;
2838 }
2839 if (ga_grow(&error_ga, len) == OK)
2840 {
2841 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2842 (char_u *)str, len);
2843#ifdef UNIX
2844 /* remove CR characters, they are displayed */
2845 {
2846 char_u *p;
2847
2848 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2849 for (;;)
2850 {
2851 p = vim_strchr(p, '\r');
2852 if (p == NULL)
2853 break;
2854 *p = ' ';
2855 }
2856 }
2857#endif
2858 --len; /* don't count the NUL at the end */
2859 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861}
2862
2863/*
2864 * Give a message. To be used when the screen hasn't been initialized yet.
2865 * When there is no tty, collect messages until the GUI has started and they
2866 * can be displayed in a message box.
2867 */
2868 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002869mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
2871#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2872 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2873 * uses mch_errmsg() when started from the desktop.
2874 * When not going to start the GUI also use stdout.
2875 * On Mac, when started from Finder, stderr is the console. */
2876 if (
2877# ifdef UNIX
2878# ifdef MACOS_X_UNIX
2879 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2880# else
2881 isatty(2)
2882# endif
2883# ifdef FEAT_GUI
2884 ||
2885# endif
2886# endif
2887# ifdef FEAT_GUI
2888 !(gui.in_use || gui.starting)
2889# endif
2890 )
2891 {
2892 printf("%s", str);
2893 return;
2894 }
2895# endif
2896 mch_errmsg(str);
2897}
2898#endif /* USE_MCH_ERRMSG */
2899
2900/*
2901 * Put a character on the screen at the current message position and advance
2902 * to the next position. Only for printable ASCII!
2903 */
2904 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002905msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 msg_didout = TRUE; /* remember that line is not empty */
2908 screen_putchar(c, msg_row, msg_col, attr);
2909#ifdef FEAT_RIGHTLEFT
2910 if (cmdmsg_rl)
2911 {
2912 if (--msg_col == 0)
2913 {
2914 msg_col = Columns;
2915 ++msg_row;
2916 }
2917 }
2918 else
2919#endif
2920 {
2921 if (++msg_col >= Columns)
2922 {
2923 msg_col = 0;
2924 ++msg_row;
2925 }
2926 }
2927}
2928
2929 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002930msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002932 int attr;
2933 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 screen_puts((char_u *)
2939 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2940 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941}
2942
2943/*
2944 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2945 * exmode_active.
2946 */
2947 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002948repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 if (State == ASKMORE)
2951 {
2952 msg_moremsg(TRUE); /* display --more-- message again */
2953 msg_row = Rows - 1;
2954 }
2955#ifdef FEAT_CON_DIALOG
2956 else if (State == CONFIRM)
2957 {
2958 display_confirm_msg(); /* display ":confirm" message again */
2959 msg_row = Rows - 1;
2960 }
2961#endif
2962 else if (State == EXTERNCMD)
2963 {
2964 windgoto(msg_row, msg_col); /* put cursor back */
2965 }
2966 else if (State == HITRETURN || State == SETWSIZE)
2967 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002968 if (msg_row == Rows - 1)
2969 {
2970 /* Avoid drawing the "hit-enter" prompt below the previous one,
2971 * overwrite it. Esp. useful when regaining focus and a
2972 * FocusGained autocmd exists but didn't draw anything. */
2973 msg_didout = FALSE;
2974 msg_col = 0;
2975 msg_clr_eos();
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 hit_return_msg();
2978 msg_row = Rows - 1;
2979 }
2980}
2981
2982/*
2983 * msg_check_screen - check if the screen is initialized.
2984 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2985 * While starting the GUI the terminal codes will be set for the GUI, but the
2986 * output goes to the terminal. Don't use the terminal codes then.
2987 */
2988 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002989msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 if (!full_screen || !screen_valid(FALSE))
2992 return FALSE;
2993
2994 if (msg_row >= Rows)
2995 msg_row = Rows - 1;
2996 if (msg_col >= Columns)
2997 msg_col = Columns - 1;
2998 return TRUE;
2999}
3000
3001/*
3002 * Clear from current message position to end of screen.
3003 * Skip this when ":silent" was used, no need to clear for redirection.
3004 */
3005 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003006msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
3008 if (msg_silent == 0)
3009 msg_clr_eos_force();
3010}
3011
3012/*
3013 * Clear from current message position to end of screen.
3014 * Note: msg_col is not updated, so we remember the end of the message
3015 * for msg_check().
3016 */
3017 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003018msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
3020 if (msg_use_printf())
3021 {
3022 if (full_screen) /* only when termcap codes are valid */
3023 {
3024 if (*T_CD)
3025 out_str(T_CD); /* clear to end of display */
3026 else if (*T_CE)
3027 out_str(T_CE); /* clear to end of line */
3028 }
3029 }
3030 else
3031 {
3032#ifdef FEAT_RIGHTLEFT
3033 if (cmdmsg_rl)
3034 {
3035 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3036 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3037 }
3038 else
3039#endif
3040 {
3041 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3042 ' ', ' ', 0);
3043 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3044 }
3045 }
3046}
3047
3048/*
3049 * Clear the command line.
3050 */
3051 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003052msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 msg_row = cmdline_row;
3055 msg_col = 0;
3056 msg_clr_eos_force();
3057}
3058
3059/*
3060 * end putting a message on the screen
3061 * call wait_return if the message does not fit in the available space
3062 * return TRUE if wait_return not called.
3063 */
3064 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003065msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003068 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 * or the ruler option is set and we run into it,
3070 * we have to redraw the window.
3071 * Do not do this if we are abandoning the file or editing the command line.
3072 */
3073 if (!exiting && need_wait_return && !(State & CMDLINE))
3074 {
3075 wait_return(FALSE);
3076 return FALSE;
3077 }
3078 out_flush();
3079 return TRUE;
3080}
3081
3082/*
3083 * If the written message runs into the shown command or ruler, we have to
3084 * wait for hit-return and redraw the window later.
3085 */
3086 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003087msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 if (msg_row == Rows - 1 && msg_col >= sc_col)
3090 {
3091 need_wait_return = TRUE;
3092 redraw_cmdline = TRUE;
3093 }
3094}
3095
3096/*
3097 * May write a string to the redirection file.
3098 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3099 */
3100 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003101redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102{
3103 char_u *s = str;
3104 static int cur_col = 0;
3105
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003106 /* Don't do anything for displaying prompts and the like. */
3107 if (redir_off)
3108 return;
3109
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003110 /* If 'verbosefile' is set prepare for writing in that file. */
3111 if (*p_vfile != NUL && verbose_fd == NULL)
3112 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003113
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003114 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
3116 /* If the string doesn't start with CR or NL, go to msg_col */
3117 if (*s != '\n' && *s != '\r')
3118 {
3119 while (cur_col < msg_col)
3120 {
3121#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003122 if (redir_execute)
3123 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003124 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003126 else if (redir_vname)
3127 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003128 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003130 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003132 if (verbose_fd != NULL)
3133 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 ++cur_col;
3135 }
3136 }
3137
3138#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003139 if (redir_execute)
3140 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003141 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003143 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003144 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#endif
3146
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003147 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3149 {
3150#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003151 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003153 if (redir_fd != NULL)
3154 putc(*s, redir_fd);
3155 if (verbose_fd != NULL)
3156 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (*s == '\r' || *s == '\n')
3158 cur_col = 0;
3159 else if (*s == '\t')
3160 cur_col += (8 - cur_col % 8);
3161 else
3162 ++cur_col;
3163 ++s;
3164 }
3165
3166 if (msg_silent != 0) /* should update msg_col */
3167 msg_col = cur_col;
3168 }
3169}
3170
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003171 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003172redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003173{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003174 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003175#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003176 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003177#endif
3178 ;
3179}
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003182 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003183 * Must always be called paired with verbose_leave()!
3184 */
3185 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003186verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003187{
3188 if (*p_vfile != NUL)
3189 ++msg_silent;
3190}
3191
3192/*
3193 * After giving verbose message.
3194 * Must always be called paired with verbose_enter()!
3195 */
3196 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003197verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003198{
3199 if (*p_vfile != NUL)
3200 if (--msg_silent < 0)
3201 msg_silent = 0;
3202}
3203
3204/*
3205 * Like verbose_enter() and set msg_scroll when displaying the message.
3206 */
3207 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003208verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003209{
3210 if (*p_vfile != NUL)
3211 ++msg_silent;
3212 else
3213 /* always scroll up, don't overwrite */
3214 msg_scroll = TRUE;
3215}
3216
3217/*
3218 * Like verbose_leave() and set cmdline_row when displaying the message.
3219 */
3220 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003221verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003222{
3223 if (*p_vfile != NUL)
3224 {
3225 if (--msg_silent < 0)
3226 msg_silent = 0;
3227 }
3228 else
3229 cmdline_row = msg_row;
3230}
3231
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003232/*
3233 * Called when 'verbosefile' is set: stop writing to the file.
3234 */
3235 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003236verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003237{
3238 if (verbose_fd != NULL)
3239 {
3240 fclose(verbose_fd);
3241 verbose_fd = NULL;
3242 }
3243 verbose_did_open = FALSE;
3244}
3245
3246/*
3247 * Open the file 'verbosefile'.
3248 * Return FAIL or OK.
3249 */
3250 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003251verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003252{
3253 if (verbose_fd == NULL && !verbose_did_open)
3254 {
3255 /* Only give the error message once. */
3256 verbose_did_open = TRUE;
3257
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003258 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003259 if (verbose_fd == NULL)
3260 {
3261 EMSG2(_(e_notopen), p_vfile);
3262 return FAIL;
3263 }
3264 }
3265 return OK;
3266}
3267
3268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 * Give a warning message (for searching).
3270 * Use 'w' highlighting and may repeat the message after redrawing
3271 */
3272 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003273give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 /* Don't do this for ":silent". */
3276 if (msg_silent != 0)
3277 return;
3278
3279 /* Don't want a hit-enter prompt here. */
3280 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#ifdef FEAT_EVAL
3283 set_vim_var_string(VV_WARNINGMSG, message, -1);
3284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 vim_free(keep_msg);
3286 keep_msg = NULL;
3287 if (hl)
3288 keep_msg_attr = hl_attr(HLF_W);
3289 else
3290 keep_msg_attr = 0;
3291 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003292 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 msg_didout = FALSE; /* overwrite this message */
3294 msg_nowait = TRUE; /* don't wait for this message */
3295 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 --no_wait_return;
3298}
3299
3300/*
3301 * Advance msg cursor to column "col".
3302 */
3303 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003304msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 if (msg_silent != 0) /* nothing to advance to */
3307 {
3308 msg_col = col; /* for redirection, may fill it up later */
3309 return;
3310 }
3311 if (col >= Columns) /* not enough room */
3312 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003313#ifdef FEAT_RIGHTLEFT
3314 if (cmdmsg_rl)
3315 while (msg_col > Columns - col)
3316 msg_putchar(' ');
3317 else
3318#endif
3319 while (msg_col < col)
3320 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321}
3322
3323#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3324/*
3325 * Used for "confirm()" function, and the :confirm command prefix.
3326 * Versions which haven't got flexible dialogs yet, and console
3327 * versions, get this generic handler which uses the command line.
3328 *
3329 * type = one of:
3330 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3331 * title = title string (can be NULL for default)
3332 * (neither used in console dialogs at the moment)
3333 *
3334 * Format of the "buttons" string:
3335 * "Button1Name\nButton2Name\nButton3Name"
3336 * The first button should normally be the default/accept
3337 * The second button should be the 'Cancel' button
3338 * Other buttons- use your imagination!
3339 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3340 * different letter.
3341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003343do_dialog(
3344 int type UNUSED,
3345 char_u *title UNUSED,
3346 char_u *message,
3347 char_u *buttons,
3348 int dfltbutton,
3349 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003350 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003351 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003352 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int oldState;
3355 int retval = 0;
3356 char_u *hotkeys;
3357 int c;
3358 int i;
3359
3360#ifndef NO_CONSOLE
3361 /* Don't output anything in silent mode ("ex -s") */
3362 if (silent_mode)
3363 return dfltbutton; /* return default option */
3364#endif
3365
3366#ifdef FEAT_GUI_DIALOG
3367 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3368 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3369 {
3370 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003371 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003372 /* avoid a hit-enter prompt without clearing the cmdline */
3373 need_wait_return = FALSE;
3374 emsg_on_display = FALSE;
3375 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377 /* Flush output to avoid that further messages and redrawing is done
3378 * in the wrong order. */
3379 out_flush();
3380 gui_mch_update();
3381
3382 return c;
3383 }
3384#endif
3385
3386 oldState = State;
3387 State = CONFIRM;
3388#ifdef FEAT_MOUSE
3389 setmouse();
3390#endif
3391
3392 /*
3393 * Since we wait for a keypress, don't make the
3394 * user press RETURN as well afterwards.
3395 */
3396 ++no_wait_return;
3397 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3398
3399 if (hotkeys != NULL)
3400 {
3401 for (;;)
3402 {
3403 /* Get a typed character directly from the user. */
3404 c = get_keystroke();
3405 switch (c)
3406 {
3407 case CAR: /* User accepts default option */
3408 case NL:
3409 retval = dfltbutton;
3410 break;
3411 case Ctrl_C: /* User aborts/cancels */
3412 case ESC:
3413 retval = 0;
3414 break;
3415 default: /* Could be a hotkey? */
3416 if (c < 0) /* special keys are ignored here */
3417 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003418 if (c == ':' && ex_cmd)
3419 {
3420 retval = dfltbutton;
3421 ins_char_typebuf(':');
3422 break;
3423 }
3424
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 /* Make the character lowercase, as chars in "hotkeys" are. */
3426 c = MB_TOLOWER(c);
3427 retval = 1;
3428 for (i = 0; hotkeys[i]; ++i)
3429 {
3430#ifdef FEAT_MBYTE
3431 if (has_mbyte)
3432 {
3433 if ((*mb_ptr2char)(hotkeys + i) == c)
3434 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003435 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 else
3438#endif
3439 if (hotkeys[i] == c)
3440 break;
3441 ++retval;
3442 }
3443 if (hotkeys[i])
3444 break;
3445 /* No hotkey match, so keep waiting */
3446 continue;
3447 }
3448 break;
3449 }
3450
3451 vim_free(hotkeys);
3452 }
3453
3454 State = oldState;
3455#ifdef FEAT_MOUSE
3456 setmouse();
3457#endif
3458 --no_wait_return;
3459 msg_end_prompt();
3460
3461 return retval;
3462}
3463
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003464static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
3466/*
3467 * Copy one character from "*from" to "*to", taking care of multi-byte
3468 * characters. Return the length of the character in bytes.
3469 */
3470 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003471copy_char(
3472 char_u *from,
3473 char_u *to,
3474 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
3476#ifdef FEAT_MBYTE
3477 int len;
3478 int c;
3479
3480 if (has_mbyte)
3481 {
3482 if (lowercase)
3483 {
3484 c = MB_TOLOWER((*mb_ptr2char)(from));
3485 return (*mb_char2bytes)(c, to);
3486 }
3487 else
3488 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003489 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 mch_memmove(to, from, (size_t)len);
3491 return len;
3492 }
3493 }
3494 else
3495#endif
3496 {
3497 if (lowercase)
3498 *to = (char_u)TOLOWER_LOC(*from);
3499 else
3500 *to = *from;
3501 return 1;
3502 }
3503}
3504
3505/*
3506 * Format the dialog string, and display it at the bottom of
3507 * the screen. Return a string of hotkey chars (if defined) for
3508 * each 'button'. If a button has no hotkey defined, the first character of
3509 * the button is used.
3510 * The hotkeys can be multi-byte characters, but without combining chars.
3511 *
3512 * Returns an allocated string with hotkeys, or NULL for error.
3513 */
3514 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003515msg_show_console_dialog(
3516 char_u *message,
3517 char_u *buttons,
3518 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 int len = 0;
3521#ifdef FEAT_MBYTE
3522# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3523#else
3524# define HOTK_LEN 1
3525#endif
3526 int lenhotkey = HOTK_LEN; /* count first button */
3527 char_u *hotk = NULL;
3528 char_u *msgp = NULL;
3529 char_u *hotkp = NULL;
3530 char_u *r;
3531 int copy;
3532#define HAS_HOTKEY_LEN 30
3533 char_u has_hotkey[HAS_HOTKEY_LEN];
3534 int first_hotkey = FALSE; /* first char of button is hotkey */
3535 int idx;
3536
3537 has_hotkey[0] = FALSE;
3538
3539 /*
3540 * First loop: compute the size of memory to allocate.
3541 * Second loop: copy to the allocated memory.
3542 */
3543 for (copy = 0; copy <= 1; ++copy)
3544 {
3545 r = buttons;
3546 idx = 0;
3547 while (*r)
3548 {
3549 if (*r == DLG_BUTTON_SEP)
3550 {
3551 if (copy)
3552 {
3553 *msgp++ = ',';
3554 *msgp++ = ' '; /* '\n' -> ', ' */
3555
3556 /* advance to next hotkey and set default hotkey */
3557#ifdef FEAT_MBYTE
3558 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003559 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else
3561#endif
3562 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003563 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (dfltbutton)
3565 --dfltbutton;
3566
3567 /* If no hotkey is specified first char is used. */
3568 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3569 first_hotkey = TRUE;
3570 }
3571 else
3572 {
3573 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3574 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3575 if (idx < HAS_HOTKEY_LEN - 1)
3576 has_hotkey[++idx] = FALSE;
3577 }
3578 }
3579 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3580 {
3581 if (*r == DLG_HOTKEY_CHAR)
3582 ++r;
3583 first_hotkey = FALSE;
3584 if (copy)
3585 {
3586 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3587 *msgp++ = *r;
3588 else
3589 {
3590 /* '&a' -> '[a]' */
3591 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3592 msgp += copy_char(r, msgp, FALSE);
3593 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3594
3595 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003596 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 }
3599 else
3600 {
3601 ++len; /* '&a' -> '[a]' */
3602 if (idx < HAS_HOTKEY_LEN - 1)
3603 has_hotkey[idx] = TRUE;
3604 }
3605 }
3606 else
3607 {
3608 /* everything else copy literally */
3609 if (copy)
3610 msgp += copy_char(r, msgp, FALSE);
3611 }
3612
3613 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003614 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616
3617 if (copy)
3618 {
3619 *msgp++ = ':';
3620 *msgp++ = ' ';
3621 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 else
3624 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003625 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003626 + 2 /* for the NL's */
3627 + STRLEN(buttons)
3628 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003629 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 /* If no hotkey is specified first char is used. */
3632 if (!has_hotkey[0])
3633 {
3634 first_hotkey = TRUE;
3635 len += 2; /* "x" -> "[x]" */
3636 }
3637
3638 /*
3639 * Now allocate and load the strings
3640 */
3641 vim_free(confirm_msg);
3642 confirm_msg = alloc(len);
3643 if (confirm_msg == NULL)
3644 return NULL;
3645 *confirm_msg = NUL;
3646 hotk = alloc(lenhotkey);
3647 if (hotk == NULL)
3648 return NULL;
3649
3650 *confirm_msg = '\n';
3651 STRCPY(confirm_msg + 1, message);
3652
3653 msgp = confirm_msg + 1 + STRLEN(message);
3654 hotkp = hotk;
3655
Bram Moolenaar6a516062007-07-05 08:11:42 +00003656 /* Define first default hotkey. Keep the hotkey string NUL
3657 * terminated to avoid reading past the end. */
3658 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660 /* Remember where the choices start, displaying starts here when
3661 * "hotkp" typed at the more prompt. */
3662 confirm_msg_tail = msgp;
3663 *msgp++ = '\n';
3664 }
3665 }
3666
3667 display_confirm_msg();
3668 return hotk;
3669}
3670
3671/*
3672 * Display the ":confirm" message. Also called when screen resized.
3673 */
3674 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003675display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 /* avoid that 'q' at the more prompt truncates the message here */
3678 ++confirm_msg_used;
3679 if (confirm_msg != NULL)
3680 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3681 --confirm_msg_used;
3682}
3683
3684#endif /* FEAT_CON_DIALOG */
3685
3686#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3687
3688 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003689vim_dialog_yesno(
3690 int type,
3691 char_u *title,
3692 char_u *message,
3693 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 if (do_dialog(type,
3696 title == NULL ? (char_u *)_("Question") : title,
3697 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003698 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 return VIM_YES;
3700 return VIM_NO;
3701}
3702
3703 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003704vim_dialog_yesnocancel(
3705 int type,
3706 char_u *title,
3707 char_u *message,
3708 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 switch (do_dialog(type,
3711 title == NULL ? (char_u *)_("Question") : title,
3712 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003713 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
3715 case 1: return VIM_YES;
3716 case 2: return VIM_NO;
3717 }
3718 return VIM_CANCEL;
3719}
3720
3721 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003722vim_dialog_yesnoallcancel(
3723 int type,
3724 char_u *title,
3725 char_u *message,
3726 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
3728 switch (do_dialog(type,
3729 title == NULL ? (char_u *)"Question" : title,
3730 message,
3731 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003732 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
3734 case 1: return VIM_YES;
3735 case 2: return VIM_NO;
3736 case 3: return VIM_ALL;
3737 case 4: return VIM_DISCARDALL;
3738 }
3739 return VIM_CANCEL;
3740}
3741
3742#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3743
3744#if defined(FEAT_BROWSE) || defined(PROTO)
3745/*
3746 * Generic browse function. Calls gui_mch_browse() when possible.
3747 * Later this may pop-up a non-GUI file selector (external command?).
3748 */
3749 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003750do_browse(
3751 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3752 char_u *title, /* title for the window */
3753 char_u *dflt, /* default file name (may include directory) */
3754 char_u *ext, /* extension added */
3755 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003757 char_u *filter, /* file name filter */
3758 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
3760 char_u *fname;
3761 static char_u *last_dir = NULL; /* last used directory */
3762 char_u *tofree = NULL;
3763 int save_browse = cmdmod.browse;
3764
3765 /* Must turn off browse to avoid that autocommands will get the
3766 * flag too! */
3767 cmdmod.browse = FALSE;
3768
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003769 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003771 if (flags & BROWSE_DIR)
3772 title = (char_u *)_("Select Directory dialog");
3773 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 title = (char_u *)_("Save File dialog");
3775 else
3776 title = (char_u *)_("Open File dialog");
3777 }
3778
3779 /* When no directory specified, use default file name, default dir, buffer
3780 * dir, last dir or current dir */
3781 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3782 {
3783 if (mch_isdir(dflt)) /* default file name is a directory */
3784 {
3785 initdir = dflt;
3786 dflt = NULL;
3787 }
3788 else if (gettail(dflt) != dflt) /* default file name includes a path */
3789 {
3790 tofree = vim_strsave(dflt);
3791 if (tofree != NULL)
3792 {
3793 initdir = tofree;
3794 *gettail(initdir) = NUL;
3795 dflt = gettail(dflt);
3796 }
3797 }
3798 }
3799
3800 if (initdir == NULL || *initdir == NUL)
3801 {
3802 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003803 if (STRCMP(p_bsdir, "last") != 0
3804 && STRCMP(p_bsdir, "buffer") != 0
3805 && STRCMP(p_bsdir, "current") != 0
3806 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 initdir = p_bsdir;
3808 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003809 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 && buf != NULL && buf->b_ffname != NULL)
3811 {
3812 if (dflt == NULL || *dflt == NUL)
3813 dflt = gettail(curbuf->b_ffname);
3814 tofree = vim_strsave(curbuf->b_ffname);
3815 if (tofree != NULL)
3816 {
3817 initdir = tofree;
3818 *gettail(initdir) = NUL;
3819 }
3820 }
3821 /* When 'browsedir' is "last", use dir from last browse */
3822 else if (*p_bsdir == 'l')
3823 initdir = last_dir;
3824 /* When 'browsedir is "current", use current directory. This is the
3825 * default already, leave initdir empty. */
3826 }
3827
3828# ifdef FEAT_GUI
3829 if (gui.in_use) /* when this changes, also adjust f_has()! */
3830 {
3831 if (filter == NULL
3832# ifdef FEAT_EVAL
3833 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3834 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3835# endif
3836 )
3837 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003838 if (flags & BROWSE_DIR)
3839 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003840# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003841 /* For systems that have a directory dialog. */
3842 fname = gui_mch_browsedir(title, initdir);
3843# else
3844 /* Generic solution for selecting a directory: select a file and
3845 * remove the file name. */
3846 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3847# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003848# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003849 /* Win32 adds a dummy file name, others return an arbitrary file
3850 * name. GTK+ 2 returns only the directory, */
3851 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3852 {
3853 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003854 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003855
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003856 if (tail == fname)
3857 *tail++ = '.'; /* use current dir */
3858 *tail = NUL;
3859 }
3860# endif
3861 }
3862 else
3863 fname = gui_mch_browse(flags & BROWSE_SAVE,
3864 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 /* We hang around in the dialog for a while, the user might do some
3867 * things to our files. The Win32 dialog allows deleting or renaming
3868 * a file, check timestamps. */
3869 need_check_timestamps = TRUE;
3870 did_check_timestamps = FALSE;
3871 }
3872 else
3873# endif
3874 {
3875 /* TODO: non-GUI file selector here */
3876 EMSG(_("E338: Sorry, no file browser in console mode"));
3877 fname = NULL;
3878 }
3879
3880 /* keep the directory for next time */
3881 if (fname != NULL)
3882 {
3883 vim_free(last_dir);
3884 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003885 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 *gettail(last_dir) = NUL;
3888 if (*last_dir == NUL)
3889 {
3890 /* filename only returned, must be in current dir */
3891 vim_free(last_dir);
3892 last_dir = alloc(MAXPATHL);
3893 if (last_dir != NULL)
3894 mch_dirname(last_dir, MAXPATHL);
3895 }
3896 }
3897 }
3898
3899 vim_free(tofree);
3900 cmdmod.browse = save_browse;
3901
3902 return fname;
3903}
3904#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003905
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003906#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003907static char *e_printf = N_("E766: Insufficient arguments for printf()");
3908
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003909static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003910static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003911# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003912static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003913# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003914
3915/*
3916 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3917 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003918 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003919tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920{
3921 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003922 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003923 int err = FALSE;
3924
3925 if (tvs[idx].v_type == VAR_UNKNOWN)
3926 EMSG(_(e_printf));
3927 else
3928 {
3929 ++*idxp;
3930 n = get_tv_number_chk(&tvs[idx], &err);
3931 if (err)
3932 n = 0;
3933 }
3934 return n;
3935}
3936
3937/*
3938 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003939 * If "tofree" is NULL get_tv_string_chk() is used. Some types (e.g. List)
3940 * are not converted to a string.
3941 * If "tofree" is not NULL echo_string() is used. All types are converted to
3942 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00003943 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003944 */
3945 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003946tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003948 int idx = *idxp - 1;
3949 char *s = NULL;
3950 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951
3952 if (tvs[idx].v_type == VAR_UNKNOWN)
3953 EMSG(_(e_printf));
3954 else
3955 {
3956 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02003957 if (tofree != NULL)
3958 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
3959 else
3960 s = (char *)get_tv_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003961 }
3962 return s;
3963}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003964
3965# ifdef FEAT_FLOAT
3966/*
3967 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3968 */
3969 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003970tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003971{
3972 int idx = *idxp - 1;
3973 double f = 0;
3974
3975 if (tvs[idx].v_type == VAR_UNKNOWN)
3976 EMSG(_(e_printf));
3977 else
3978 {
3979 ++*idxp;
3980 if (tvs[idx].v_type == VAR_FLOAT)
3981 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003982 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003983 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003984 else
3985 EMSG(_("E807: Expected Float argument for printf()"));
3986 }
3987 return f;
3988}
3989# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003990#endif
3991
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003992/*
3993 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003994 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003995 * consistency.
3996 *
3997 * This code is based on snprintf.c - a portable implementation of snprintf
3998 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003999 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004000 * The original code, including useful comments, can be found here:
4001 * http://www.ijs.si/software/snprintf/
4002 *
4003 * This snprintf() only supports the following conversion specifiers:
4004 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4005 * with flags: '-', '+', ' ', '0' and '#'.
4006 * An asterisk is supported for field width as well as precision.
4007 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00004008 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
4009 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004010 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
4011 * 'll' (long long int) is not supported.
4012 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004013 * The locale is not used, the string is used as a byte string. This is only
4014 * relevant for double-byte encodings where the second byte may be '%'.
4015 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004016 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4017 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004018 *
4019 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004020 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004021 * is greater or equal to "str_m", not all characters from the result
4022 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4023 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004024 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004025 */
4026
4027/*
4028 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004029 *
4030 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004031 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004032 */
4033
4034/* When generating prototypes all of this is skipped, cproto doesn't
4035 * understand this. */
4036#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004037
Bram Moolenaara800b422010-06-27 01:15:55 +02004038/* Like vim_vsnprintf() but append to the string. */
4039 int
4040vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4041{
4042 va_list ap;
4043 int str_l;
4044 size_t len = STRLEN(str);
4045 size_t space;
4046
4047 if (str_m <= len)
4048 space = 0;
4049 else
4050 space = str_m - len;
4051 va_start(ap, fmt);
4052 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4053 va_end(ap);
4054 return str_l;
4055}
Bram Moolenaara800b422010-06-27 01:15:55 +02004056
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004057 int
4058vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4059{
4060 va_list ap;
4061 int str_l;
4062
4063 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004065 va_end(ap);
4066 return str_l;
4067}
4068
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004070vim_vsnprintf(
4071 char *str,
4072 size_t str_m,
4073 char *fmt,
4074 va_list ap,
4075 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004076{
4077 size_t str_l = 0;
4078 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004079 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004080
4081 if (p == NULL)
4082 p = "";
4083 while (*p != NUL)
4084 {
4085 if (*p != '%')
4086 {
4087 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004088 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004089
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004090 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004091 if (str_l < str_m)
4092 {
4093 size_t avail = str_m - str_l;
4094
4095 mch_memmove(str + str_l, p, n > avail ? avail : n);
4096 }
4097 p += n;
4098 str_l += n;
4099 }
4100 else
4101 {
4102 size_t min_field_width = 0, precision = 0;
4103 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4104 int alternate_form = 0, force_sign = 0;
4105
4106 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4107 * ignored. */
4108 int space_for_positive = 1;
4109
4110 /* allowed values: \0, h, l, L */
4111 char length_modifier = '\0';
4112
4113 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004114# if defined(FEAT_FLOAT)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004115# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004116 * That sounds reasonable to use as the maximum
4117 * printable. */
Bram Moolenaar91984b92016-08-16 21:58:41 +02004118# elif defined(FEAT_NUM64)
4119# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004120# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004121# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004122# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004123 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004124
4125 /* string address in case of string argument */
4126 char *str_arg;
4127
4128 /* natural field width of arg without padding and sign */
4129 size_t str_arg_l;
4130
4131 /* unsigned char argument value - only defined for c conversion.
4132 * N.B. standard explicitly states the char argument for the c
4133 * conversion is unsigned */
4134 unsigned char uchar_arg;
4135
4136 /* number of zeros to be inserted for numeric conversions as
4137 * required by the precision or minimal field width */
4138 size_t number_of_zeros_to_pad = 0;
4139
4140 /* index into tmp where zero padding is to be inserted */
4141 size_t zero_padding_insertion_ind = 0;
4142
4143 /* current conversion specifier character */
4144 char fmt_spec = '\0';
4145
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004146 /* buffer for 's' and 'S' specs */
4147 char_u *tofree = NULL;
4148
4149
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004150 str_arg = NULL;
4151 p++; /* skip '%' */
4152
4153 /* parse flags */
4154 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4155 || *p == '#' || *p == '\'')
4156 {
4157 switch (*p)
4158 {
4159 case '0': zero_padding = 1; break;
4160 case '-': justify_left = 1; break;
4161 case '+': force_sign = 1; space_for_positive = 0; break;
4162 case ' ': force_sign = 1;
4163 /* If both the ' ' and '+' flags appear, the ' '
4164 * flag should be ignored */
4165 break;
4166 case '#': alternate_form = 1; break;
4167 case '\'': break;
4168 }
4169 p++;
4170 }
4171 /* If the '0' and '-' flags both appear, the '0' flag should be
4172 * ignored. */
4173
4174 /* parse field width */
4175 if (*p == '*')
4176 {
4177 int j;
4178
4179 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004181# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004182 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183# endif
4184 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004185 if (j >= 0)
4186 min_field_width = j;
4187 else
4188 {
4189 min_field_width = -j;
4190 justify_left = 1;
4191 }
4192 }
4193 else if (VIM_ISDIGIT((int)(*p)))
4194 {
4195 /* size_t could be wider than unsigned int; make sure we treat
4196 * argument like common implementations do */
4197 unsigned int uj = *p++ - '0';
4198
4199 while (VIM_ISDIGIT((int)(*p)))
4200 uj = 10 * uj + (unsigned int)(*p++ - '0');
4201 min_field_width = uj;
4202 }
4203
4204 /* parse precision */
4205 if (*p == '.')
4206 {
4207 p++;
4208 precision_specified = 1;
4209 if (*p == '*')
4210 {
4211 int j;
4212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004213 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004214# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004215 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004216# endif
4217 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004218 p++;
4219 if (j >= 0)
4220 precision = j;
4221 else
4222 {
4223 precision_specified = 0;
4224 precision = 0;
4225 }
4226 }
4227 else if (VIM_ISDIGIT((int)(*p)))
4228 {
4229 /* size_t could be wider than unsigned int; make sure we
4230 * treat argument like common implementations do */
4231 unsigned int uj = *p++ - '0';
4232
4233 while (VIM_ISDIGIT((int)(*p)))
4234 uj = 10 * uj + (unsigned int)(*p++ - '0');
4235 precision = uj;
4236 }
4237 }
4238
4239 /* parse 'h', 'l' and 'll' length modifiers */
4240 if (*p == 'h' || *p == 'l')
4241 {
4242 length_modifier = *p;
4243 p++;
4244 if (length_modifier == 'l' && *p == 'l')
4245 {
4246 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004247# ifdef FEAT_NUM64
4248 length_modifier = 'L';
4249# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004250 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004251# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252 p++;
4253 }
4254 }
4255 fmt_spec = *p;
4256
4257 /* common synonyms: */
4258 switch (fmt_spec)
4259 {
4260 case 'i': fmt_spec = 'd'; break;
4261 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4262 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4263 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004264 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004265 default: break;
4266 }
4267
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004268# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4269 switch (fmt_spec)
4270 {
4271 case 'd': case 'u': case 'o': case 'x': case 'X':
4272 if (tvs != NULL && length_modifier == '\0')
4273 length_modifier = 'L';
4274 }
4275# endif
4276
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004277 /* get parameter value, do initial processing */
4278 switch (fmt_spec)
4279 {
4280 /* '%' and 'c' behave similar to 's' regarding flags and field
4281 * widths */
4282 case '%':
4283 case 'c':
4284 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004285 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004286 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004287 str_arg_l = 1;
4288 switch (fmt_spec)
4289 {
4290 case '%':
4291 str_arg = p;
4292 break;
4293
4294 case 'c':
4295 {
4296 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297
4298 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004300 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301# endif
4302 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303 /* standard demands unsigned char */
4304 uchar_arg = (unsigned char)j;
4305 str_arg = (char *)&uchar_arg;
4306 break;
4307 }
4308
4309 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004310 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004311 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004312# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004313 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314# endif
4315 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004316 if (str_arg == NULL)
4317 {
4318 str_arg = "[NULL]";
4319 str_arg_l = 6;
4320 }
4321 /* make sure not to address string beyond the specified
4322 * precision !!! */
4323 else if (!precision_specified)
4324 str_arg_l = strlen(str_arg);
4325 /* truncate string if necessary as requested by precision */
4326 else if (precision == 0)
4327 str_arg_l = 0;
4328 else
4329 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004330 /* Don't put the #if inside memchr(), it can be a
4331 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004332# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004333 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004334# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004335 /* memchr on HP does not like n > 2^31 !!! */
4336 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004337 precision <= (size_t)0x7fffffffL ? precision
4338 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004339# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004340 str_arg_l = (q == NULL) ? precision
4341 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004342 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004343# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004344 if (fmt_spec == 'S')
4345 {
4346 if (min_field_width != 0)
4347 min_field_width += STRLEN(str_arg)
4348 - mb_string2cells((char_u *)str_arg, -1);
4349 if (precision)
4350 {
4351 char_u *p1 = (char_u *)str_arg;
4352 size_t i;
4353
4354 for (i = 0; i < precision && *p1; i++)
4355 p1 += mb_ptr2len(p1);
4356
4357 str_arg_l = precision = p1 - (char_u *)str_arg;
4358 }
4359 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004360# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004361 break;
4362
4363 default:
4364 break;
4365 }
4366 break;
4367
Bram Moolenaar91984b92016-08-16 21:58:41 +02004368 case 'd': case 'u':
4369 case 'b': case 'B':
4370 case 'o':
4371 case 'x': case 'X':
4372 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004373 {
Bram Moolenaar91984b92016-08-16 21:58:41 +02004374 /* NOTE: the u, b, o, x, X and p conversion specifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004375 * imply the value is unsigned; d implies a signed
4376 * value */
4377
4378 /* 0 if numeric argument is zero (or if pointer is
4379 * NULL for 'p'), +1 if greater than zero (or nonzero
4380 * for unsigned arguments), -1 if negative (unsigned
4381 * argument is never negative) */
4382 int arg_sign = 0;
4383
4384 /* only defined for length modifier h, or for no
4385 * length modifiers */
4386 int int_arg = 0;
4387 unsigned int uint_arg = 0;
4388
4389 /* only defined for length modifier l */
4390 long int long_arg = 0;
4391 unsigned long int ulong_arg = 0;
4392
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004393# ifdef FEAT_NUM64
4394 /* only defined for length modifier ll */
4395 varnumber_T llong_arg = 0;
4396 uvarnumber_T ullong_arg = 0;
4397# endif
4398
Bram Moolenaar91984b92016-08-16 21:58:41 +02004399 /* only defined for b convertion */
4400 uvarnumber_T bin_arg = 0;
4401
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004402 /* pointer argument value -only defined for p
4403 * conversion */
4404 void *ptr_arg = NULL;
4405
4406 if (fmt_spec == 'p')
4407 {
4408 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004411 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4412 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413# endif
4414 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004415 if (ptr_arg != NULL)
4416 arg_sign = 1;
4417 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004418 else if (fmt_spec == 'b' || fmt_spec == 'B')
4419 {
4420 bin_arg =
4421# if defined(FEAT_EVAL)
4422 tvs != NULL ?
4423 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4424# endif
4425 va_arg(ap, uvarnumber_T);
4426 if (bin_arg != 0)
4427 arg_sign = 1;
4428 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004429 else if (fmt_spec == 'd')
4430 {
4431 /* signed */
4432 switch (length_modifier)
4433 {
4434 case '\0':
4435 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 /* char and short arguments are passed as int. */
4437 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004439 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440# endif
4441 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004442 if (int_arg > 0)
4443 arg_sign = 1;
4444 else if (int_arg < 0)
4445 arg_sign = -1;
4446 break;
4447 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004450 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451# endif
4452 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004453 if (long_arg > 0)
4454 arg_sign = 1;
4455 else if (long_arg < 0)
4456 arg_sign = -1;
4457 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004458# ifdef FEAT_NUM64
4459 case 'L':
4460 llong_arg =
4461# if defined(FEAT_EVAL)
4462 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4463# endif
4464 va_arg(ap, varnumber_T);
4465 if (llong_arg > 0)
4466 arg_sign = 1;
4467 else if (llong_arg < 0)
4468 arg_sign = -1;
4469 break;
4470# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004471 }
4472 }
4473 else
4474 {
4475 /* unsigned */
4476 switch (length_modifier)
4477 {
4478 case '\0':
4479 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004481# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004482 tvs != NULL ? (unsigned)
4483 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004484# endif
4485 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004486 if (uint_arg != 0)
4487 arg_sign = 1;
4488 break;
4489 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004492 tvs != NULL ? (unsigned long)
4493 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004494# endif
4495 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004496 if (ulong_arg != 0)
4497 arg_sign = 1;
4498 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004499# ifdef FEAT_NUM64
4500 case 'L':
4501 ullong_arg =
4502# if defined(FEAT_EVAL)
4503 tvs != NULL ? (uvarnumber_T)
4504 tv_nr(tvs, &arg_idx) :
4505# endif
4506 va_arg(ap, uvarnumber_T);
4507 if (ullong_arg != 0)
4508 arg_sign = 1;
4509 break;
4510# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004511 }
4512 }
4513
4514 str_arg = tmp;
4515 str_arg_l = 0;
4516
4517 /* NOTE:
4518 * For d, i, u, o, x, and X conversions, if precision is
4519 * specified, the '0' flag should be ignored. This is so
4520 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4521 * FreeBSD, NetBSD; but not with Perl.
4522 */
4523 if (precision_specified)
4524 zero_padding = 0;
4525 if (fmt_spec == 'd')
4526 {
4527 if (force_sign && arg_sign >= 0)
4528 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4529 /* leave negative numbers for sprintf to handle, to
4530 * avoid handling tricky cases like (short int)-32768 */
4531 }
4532 else if (alternate_form)
4533 {
4534 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004535 && (fmt_spec == 'b' || fmt_spec == 'B'
4536 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004537 {
4538 tmp[str_arg_l++] = '0';
4539 tmp[str_arg_l++] = fmt_spec;
4540 }
4541 /* alternate form should have no effect for p
4542 * conversion, but ... */
4543 }
4544
4545 zero_padding_insertion_ind = str_arg_l;
4546 if (!precision_specified)
4547 precision = 1; /* default precision is 1 */
4548 if (precision == 0 && arg_sign == 0)
4549 {
4550 /* When zero value is formatted with an explicit
4551 * precision 0, the resulting formatted string is
Bram Moolenaar91984b92016-08-16 21:58:41 +02004552 * empty (d, i, u, b, B, o, x, X, p). */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004553 }
4554 else
4555 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004556 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004557 int f_l = 0;
4558
4559 /* construct a simple format string for sprintf */
4560 f[f_l++] = '%';
4561 if (!length_modifier)
4562 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004563 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004564 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004565# ifdef FEAT_NUM64
4566# ifdef WIN3264
4567 f[f_l++] = 'I';
4568 f[f_l++] = '6';
4569 f[f_l++] = '4';
4570# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004571 f[f_l++] = 'l';
4572 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004573# endif
4574# else
4575 f[f_l++] = 'l';
4576# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004577 }
4578 else
4579 f[f_l++] = length_modifier;
4580 f[f_l++] = fmt_spec;
4581 f[f_l++] = '\0';
4582
4583 if (fmt_spec == 'p')
4584 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004585 else if (fmt_spec == 'b' || fmt_spec == 'B')
4586 {
4587 char b[8 * sizeof(uvarnumber_T)];
4588 size_t b_l = 0;
4589 uvarnumber_T bn = bin_arg;
4590
4591 do
4592 {
4593 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4594 bn >>= 1;
4595 }
4596 while (bn != 0);
4597
4598 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4599 str_arg_l += b_l;
4600 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004601 else if (fmt_spec == 'd')
4602 {
4603 /* signed */
4604 switch (length_modifier)
4605 {
4606 case '\0':
4607 case 'h': str_arg_l += sprintf(
4608 tmp + str_arg_l, f, int_arg);
4609 break;
4610 case 'l': str_arg_l += sprintf(
4611 tmp + str_arg_l, f, long_arg);
4612 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004613# ifdef FEAT_NUM64
4614 case 'L': str_arg_l += sprintf(
4615 tmp + str_arg_l, f, llong_arg);
4616 break;
4617# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004618 }
4619 }
4620 else
4621 {
4622 /* unsigned */
4623 switch (length_modifier)
4624 {
4625 case '\0':
4626 case 'h': str_arg_l += sprintf(
4627 tmp + str_arg_l, f, uint_arg);
4628 break;
4629 case 'l': str_arg_l += sprintf(
4630 tmp + str_arg_l, f, ulong_arg);
4631 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004632# ifdef FEAT_NUM64
4633 case 'L': str_arg_l += sprintf(
4634 tmp + str_arg_l, f, ullong_arg);
4635 break;
4636# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004637 }
4638 }
4639
4640 /* include the optional minus sign and possible
4641 * "0x" in the region before the zero padding
4642 * insertion point */
4643 if (zero_padding_insertion_ind < str_arg_l
4644 && tmp[zero_padding_insertion_ind] == '-')
4645 zero_padding_insertion_ind++;
4646 if (zero_padding_insertion_ind + 1 < str_arg_l
4647 && tmp[zero_padding_insertion_ind] == '0'
4648 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4649 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4650 zero_padding_insertion_ind += 2;
4651 }
4652
4653 {
4654 size_t num_of_digits = str_arg_l
4655 - zero_padding_insertion_ind;
4656
4657 if (alternate_form && fmt_spec == 'o'
4658 /* unless zero is already the first
4659 * character */
4660 && !(zero_padding_insertion_ind < str_arg_l
4661 && tmp[zero_padding_insertion_ind] == '0'))
4662 {
4663 /* assure leading zero for alternate-form
4664 * octal numbers */
4665 if (!precision_specified
4666 || precision < num_of_digits + 1)
4667 {
4668 /* precision is increased to force the
4669 * first character to be zero, except if a
4670 * zero value is formatted with an
4671 * explicit precision of zero */
4672 precision = num_of_digits + 1;
4673 precision_specified = 1;
4674 }
4675 }
4676 /* zero padding to specified precision? */
4677 if (num_of_digits < precision)
4678 number_of_zeros_to_pad = precision - num_of_digits;
4679 }
4680 /* zero padding to specified minimal field width? */
4681 if (!justify_left && zero_padding)
4682 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004683 int n = (int)(min_field_width - (str_arg_l
4684 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004685 if (n > 0)
4686 number_of_zeros_to_pad += n;
4687 }
4688 break;
4689 }
4690
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004691# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004692 case 'f':
4693 case 'e':
4694 case 'E':
4695 case 'g':
4696 case 'G':
4697 {
4698 /* Floating point. */
4699 double f;
4700 double abs_f;
4701 char format[40];
4702 int l;
4703 int remove_trailing_zeroes = FALSE;
4704
4705 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004706# if defined(FEAT_EVAL)
4707 tvs != NULL ? tv_float(tvs, &arg_idx) :
4708# endif
4709 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004710 abs_f = f < 0 ? -f : f;
4711
4712 if (fmt_spec == 'g' || fmt_spec == 'G')
4713 {
4714 /* Would be nice to use %g directly, but it prints
4715 * "1.0" as "1", we don't want that. */
4716 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4717 || abs_f == 0.0)
4718 fmt_spec = 'f';
4719 else
4720 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4721 remove_trailing_zeroes = TRUE;
4722 }
4723
Bram Moolenaarc9372132009-01-13 15:38:37 +00004724 if (fmt_spec == 'f' &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004725# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004726 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004727# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004728 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004729# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004730 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004731 {
4732 /* Avoid a buffer overflow */
4733 strcpy(tmp, "inf");
4734 str_arg_l = 3;
4735 }
4736 else
4737 {
4738 format[0] = '%';
4739 l = 1;
4740 if (precision_specified)
4741 {
4742 size_t max_prec = TMP_LEN - 10;
4743
4744 /* Make sure we don't get more digits than we
4745 * have room for. */
4746 if (fmt_spec == 'f' && abs_f > 1.0)
4747 max_prec -= (size_t)log10(abs_f);
4748 if (precision > max_prec)
4749 precision = max_prec;
4750 l += sprintf(format + 1, ".%d", (int)precision);
4751 }
4752 format[l] = fmt_spec;
4753 format[l + 1] = NUL;
4754 str_arg_l = sprintf(tmp, format, f);
4755
4756 if (remove_trailing_zeroes)
4757 {
4758 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004759 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004760
4761 /* Using %g or %G: remove superfluous zeroes. */
4762 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004763 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004764 else
4765 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004766 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004767 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004768 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004769 {
4770 /* Remove superfluous '+' and leading
4771 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004772 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004773 {
4774 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004775 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004776 --str_arg_l;
4777 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004778 i = (tp[1] == '-') ? 2 : 1;
4779 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004780 {
4781 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004782 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004783 --str_arg_l;
4784 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004785 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004786 }
4787 }
4788
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004789 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004790 /* Remove trailing zeroes, but keep the one
4791 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004792 while (tp > tmp + 2 && *tp == '0'
4793 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004794 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004795 STRMOVE(tp, tp + 1);
4796 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004797 --str_arg_l;
4798 }
4799 }
4800 else
4801 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004802 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004803
4804 /* Be consistent: some printf("%e") use 1.0e+12
4805 * and some 1.0e+012. Remove one zero in the last
4806 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004807 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004808 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004809 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4810 && tp[2] == '0'
4811 && vim_isdigit(tp[3])
4812 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004813 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004814 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004815 --str_arg_l;
4816 }
4817 }
4818 }
4819 str_arg = tmp;
4820 break;
4821 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004822# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004823
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004824 default:
4825 /* unrecognized conversion specifier, keep format string
4826 * as-is */
4827 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004828 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004829 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004830 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004831
4832 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004833 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004834 str_arg = p;
4835 str_arg_l = 0;
4836 if (*p != NUL)
4837 str_arg_l++; /* include invalid conversion specifier
4838 unchanged if not at end-of-string */
4839 break;
4840 }
4841
4842 if (*p != NUL)
4843 p++; /* step over the just processed conversion specifier */
4844
4845 /* insert padding to the left as requested by min_field_width;
4846 * this does not include the zero padding in case of numerical
4847 * conversions*/
4848 if (!justify_left)
4849 {
4850 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004851 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004852
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004853 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004854 {
4855 if (str_l < str_m)
4856 {
4857 size_t avail = str_m - str_l;
4858
4859 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004860 (size_t)pn > avail ? avail
4861 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004862 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004863 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004864 }
4865 }
4866
4867 /* zero padding as requested by the precision or by the minimal
4868 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004869 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004870 {
4871 /* will not copy first part of numeric right now, *
4872 * force it to be copied later in its entirety */
4873 zero_padding_insertion_ind = 0;
4874 }
4875 else
4876 {
4877 /* insert first part of numerics (sign or '0x') before zero
4878 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004879 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004880
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004881 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004882 {
4883 if (str_l < str_m)
4884 {
4885 size_t avail = str_m - str_l;
4886
4887 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004888 (size_t)zn > avail ? avail
4889 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004890 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004891 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004892 }
4893
4894 /* insert zero padding as requested by the precision or min
4895 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004896 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004897 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004898 {
4899 if (str_l < str_m)
4900 {
4901 size_t avail = str_m-str_l;
4902
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004903 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004904 (size_t)zn > avail ? avail
4905 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004906 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004907 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004908 }
4909 }
4910
4911 /* insert formatted string
4912 * (or as-is conversion specifier for unknown conversions) */
4913 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004914 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004915
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004916 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004917 {
4918 if (str_l < str_m)
4919 {
4920 size_t avail = str_m - str_l;
4921
4922 mch_memmove(str + str_l,
4923 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004924 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004925 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004926 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004927 }
4928 }
4929
4930 /* insert right padding */
4931 if (justify_left)
4932 {
4933 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004934 int pn = (int)(min_field_width
4935 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004936
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004937 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004938 {
4939 if (str_l < str_m)
4940 {
4941 size_t avail = str_m - str_l;
4942
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004943 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004944 (size_t)pn > avail ? avail
4945 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004946 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004947 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004948 }
4949 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004950 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004951 }
4952 }
4953
4954 if (str_m > 0)
4955 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004956 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004957 * overwriting the last character (shouldn't happen, but just in case)
4958 * */
4959 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4960 }
4961
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004962 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004963 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004964
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004965 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004966 * character), that is, the number of characters that would have been
4967 * written to the buffer if it were large enough. */
4968 return (int)str_l;
4969}
4970
4971#endif /* PROTO */