blob: 6c9dd646cb526b8178cf8dfe4a789095a1babbb9 [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 Moolenaara7241f52008-06-24 20:39:31 +000018#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
19# include <math.h>
20#endif
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int other_sourcing_name(void);
23static char_u *get_emsg_source(void);
24static char_u *get_emsg_lnum(void);
25static void add_msg_hist(char_u *s, int len, int attr);
26static void hit_return_msg(void);
27static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010029static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
32static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
33static void msg_scroll_up(void);
34static void inc_msg_scrolled(void);
35static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
36static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
37static void msg_puts_printf(char_u *str, int maxlen);
38static int do_more_prompt(int typed_char);
39static void msg_screen_putchar(int c, int attr);
40static int msg_check_screen(void);
41static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static int confirm_msg_used = FALSE; /* displaying confirm_msg */
45static char_u *confirm_msg = NULL; /* ":confirm" message */
46static char_u *confirm_msg_tail; /* tail of confirm_msg */
47#endif
48
49struct msg_hist
50{
51 struct msg_hist *next;
52 char_u *msg;
53 int attr;
54};
55
56static struct msg_hist *first_msg_hist = NULL;
57static struct msg_hist *last_msg_hist = NULL;
58static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020060static FILE *verbose_fd = NULL;
61static int verbose_did_open = FALSE;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063/*
64 * When writing messages to the screen, there are many different situations.
65 * A number of variables is used to remember the current state:
66 * msg_didany TRUE when messages were written since the last time the
67 * user reacted to a prompt.
68 * Reset: After hitting a key for the hit-return prompt,
69 * hitting <CR> for the command line or input().
70 * Set: When any message is written to the screen.
71 * msg_didout TRUE when something was written to the current line.
72 * Reset: When advancing to the next line, when the current
73 * text can be overwritten.
74 * Set: When any message is written to the screen.
75 * msg_nowait No extra delay for the last drawn message.
76 * Used in normal_cmd() before the mode message is drawn.
77 * emsg_on_display There was an error message recently. Indicates that there
78 * should be a delay before redrawing.
79 * msg_scroll The next message should not overwrite the current one.
80 * msg_scrolled How many lines the screen has been scrolled (because of
81 * messages). Used in update_screen() to scroll the screen
82 * back. Incremented each time the screen scrolls a line.
83 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
84 * writes something without scrolling should not make
85 * need_wait_return to be set. This is a hack to make ":ts"
86 * work without an extra prompt.
87 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010088 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 * need_wait_return TRUE when the hit-return prompt is needed.
90 * Reset: After giving the hit-return prompt, when the user
91 * has answered some other prompt.
92 * Set: When the ruler or typeahead display is overwritten,
93 * scrolling the screen for some message.
94 * keep_msg Message to be displayed after redrawing the screen, in
95 * main_loop().
96 * This is an allocated string or NULL when not used.
97 */
98
99/*
100 * msg(s) - displays the string 's' on the status line
101 * When terminal not initialized (yet) mch_errmsg(..) is used.
102 * return TRUE if wait_return not called
103 */
104 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100105msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106{
107 return msg_attr_keep(s, 0, FALSE);
108}
109
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000111 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000112/*
113 * Like msg() but keep it silent when 'verbosefile' is set.
114 */
115 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100116verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000117{
118 int n;
119
120 verbose_enter();
121 n = msg_attr_keep(s, 0, FALSE);
122 verbose_leave();
123
124 return n;
125}
126#endif
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
131 return msg_attr_keep(s, attr, FALSE);
132}
133
134 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100135msg_attr_keep(
136 char_u *s,
137 int attr,
138 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 static int entered = 0;
141 int retval;
142 char_u *buf = NULL;
143
144#ifdef FEAT_EVAL
145 if (attr == 0)
146 set_vim_var_string(VV_STATUSMSG, s, -1);
147#endif
148
149 /*
150 * It is possible that displaying a messages causes a problem (e.g.,
151 * when redrawing the window), which causes another message, etc.. To
152 * break this loop, limit the recursiveness to 3 levels.
153 */
154 if (entered >= 3)
155 return TRUE;
156 ++entered;
157
158 /* Add message to history (unless it's a repeated kept message or a
159 * truncated message) */
160 if (s != keep_msg
161 || (*s != '<'
162 && last_msg_hist != NULL
163 && last_msg_hist->msg != NULL
164 && STRCMP(s, last_msg_hist->msg)))
165 add_msg_hist(s, -1, attr);
166
167 /* When displaying keep_msg, don't let msg_start() free it, caller must do
168 * that. */
169 if (s == keep_msg)
170 keep_msg = NULL;
171
172 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000173 msg_start();
174 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 if (buf != NULL)
176 s = buf;
177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 msg_outtrans_attr(s, attr);
179 msg_clr_eos();
180 retval = msg_end();
181
182 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
183 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000184 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 vim_free(buf);
187 --entered;
188 return retval;
189}
190
191/*
192 * Truncate a string such that it can be printed without causing a scroll.
193 * Returns an allocated string or NULL when no truncating is done.
194 */
195 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100196msg_strtrunc(
197 char_u *s,
198 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 char_u *buf = NULL;
201 int len;
202 int room;
203
204 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000205 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
206 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000209 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000210 /* Use all the columns. */
211 room = (int)(Rows - msg_row) * Columns - 1;
212 else
213 /* Use up to 'showcmd' column. */
214 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 if (len > room && room > 0)
216 {
217#ifdef FEAT_MBYTE
218 if (enc_utf8)
219 /* may have up to 18 bytes per cell (6 per char, up to two
220 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100221 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 else if (enc_dbcs == DBCS_JPNU)
223 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 else
226#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 len = room + 2;
228 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100230 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
232 }
233 return buf;
234}
235
236/*
237 * Truncate a string "s" to "buf" with cell width "room".
238 * "s" and "buf" may be equal.
239 */
240 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100241trunc_string(
242 char_u *s,
243 char_u *buf,
244 int room,
245 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 int half;
248 int len;
249 int e;
250 int i;
251 int n;
252
253 room -= 3;
254 half = room / 2;
255 len = 0;
256
257 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100258 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 {
260 if (s[e] == NUL)
261 {
262 /* text fits without truncating! */
263 buf[e] = NUL;
264 return;
265 }
266 n = ptr2cells(s + e);
267 if (len + n >= half)
268 break;
269 len += n;
270 buf[e] = s[e];
271#ifdef FEAT_MBYTE
272 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000273 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100275 if (++e == buflen)
276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 buf[e] = s[e];
278 }
279#endif
280 }
281
282 /* Last part: End of the string. */
283 i = e;
284#ifdef FEAT_MBYTE
285 if (enc_dbcs != 0)
286 {
287 /* For DBCS going backwards in a string is slow, but
288 * computing the cell width isn't too slow: go forward
289 * until the rest fits. */
290 n = vim_strsize(s + i);
291 while (len + n > room)
292 {
293 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000294 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 }
296 }
297 else if (enc_utf8)
298 {
299 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000300 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 for (;;)
302 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000303 do
304 half = half - (*mb_head_off)(s, s + half - 1) - 1;
305 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 n = ptr2cells(s + half);
307 if (len + n > room)
308 break;
309 len += n;
310 i = half;
311 }
312 }
313 else
314#endif
315 {
316 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
317 len += n;
318 }
319
320 /* Set the middle and copy the last part. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100321 if (e + 3 < buflen)
322 {
323 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaar38f5f952012-01-26 13:01:59 +0100324 len = (int)STRLEN(s + i) + 1;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100325 if (len >= buflen - e - 3)
326 len = buflen - e - 3 - 1;
327 mch_memmove(buf + e + 3, s + i, len);
328 buf[e + 3 + len - 1] = NUL;
329 }
330 else
331 {
Bram Moolenaar33c1b192012-01-23 20:48:40 +0100332 buf[e - 1] = NUL; /* make sure it is truncated */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334}
335
336/*
337 * Automatic prototype generation does not understand this function.
338 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
339 * shorter than IOSIZE!!!
340 */
341#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000343int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100346# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349smsg(char_u *s, ...)
350{
351 va_list arglist;
352
353 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000354 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 va_end(arglist);
356 return msg(IObuff);
357}
358
359 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100360# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363smsg_attr(int attr, char_u *s, ...)
364{
365 va_list arglist;
366
367 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000368 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 va_end(arglist);
370 return msg_attr(IObuff, attr);
371}
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#endif
374
375/*
376 * Remember the last sourcing name/lnum used in an error message, so that it
377 * isn't printed each time when it didn't change.
378 */
379static int last_sourcing_lnum = 0;
380static char_u *last_sourcing_name = NULL;
381
382/*
383 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
384 * for the next error message;
385 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000386 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100387reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388{
389 vim_free(last_sourcing_name);
390 last_sourcing_name = NULL;
391 last_sourcing_lnum = 0;
392}
393
394/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000395 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
396 */
397 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100398other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000399{
400 if (sourcing_name != NULL)
401 {
402 if (last_sourcing_name != NULL)
403 return STRCMP(sourcing_name, last_sourcing_name) != 0;
404 return TRUE;
405 }
406 return FALSE;
407}
408
409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 * Get the message about the source, as used for an error message.
411 * Returns an allocated string with room for one more character.
412 * Returns NULL when no message is to be given.
413 */
414 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100415get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
417 char_u *Buf, *p;
418
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
421 p = (char_u *)_("Error detected while processing %s:");
422 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
423 if (Buf != NULL)
424 sprintf((char *)Buf, (char *)p, sourcing_name);
425 return Buf;
426 }
427 return NULL;
428}
429
430/*
431 * Get the message about the source lnum, as used for an error message.
432 * Returns an allocated string with room for one more character.
433 * Returns NULL when no message is to be given.
434 */
435 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100436get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 char_u *Buf, *p;
439
440 /* lnum is 0 when executing a command from the command line
441 * argument, we don't want a line number then */
442 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000443 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 && sourcing_lnum != 0)
445 {
446 p = (char_u *)_("line %4ld:");
447 Buf = alloc((unsigned)(STRLEN(p) + 20));
448 if (Buf != NULL)
449 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
450 return Buf;
451 }
452 return NULL;
453}
454
455/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456 * Display name and line number for the source of an error.
457 * Remember the file name and line number, so that for the next error the info
458 * is only displayed if it changed.
459 */
460 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100461msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000462{
463 char_u *p;
464
465 ++no_wait_return;
466 p = get_emsg_source();
467 if (p != NULL)
468 {
469 msg_attr(p, attr);
470 vim_free(p);
471 }
472 p = get_emsg_lnum();
473 if (p != NULL)
474 {
475 msg_attr(p, hl_attr(HLF_N));
476 vim_free(p);
477 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
478 }
479
480 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000481 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000482 {
483 vim_free(last_sourcing_name);
484 if (sourcing_name == NULL)
485 last_sourcing_name = NULL;
486 else
487 last_sourcing_name = vim_strsave(sourcing_name);
488 }
489 --no_wait_return;
490}
491
492/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000493 * Return TRUE if not giving error messages right now:
494 * If "emsg_off" is set: no error messages at the moment.
495 * If "msg" is in 'debug': do error message but without side effects.
496 * If "emsg_skip" is set: never do error messages.
497 */
498 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100499emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000500{
501 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
502 && vim_strchr(p_debug, 't') == NULL)
503#ifdef FEAT_EVAL
504 || emsg_skip > 0
505#endif
506 )
507 return TRUE;
508 return FALSE;
509}
510
511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * emsg() - display an error message
513 *
514 * Rings the bell, if appropriate, and calls message() to do the real work
515 * When terminal not initialized (yet) mch_errmsg(..) is used.
516 *
517 * return TRUE if wait_return not called
518 */
519 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100520emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
522 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 char_u *p;
524#ifdef FEAT_EVAL
525 int ignore = FALSE;
526 int severe;
527#endif
528
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100529 /* Skip this if not giving error messages at the moment. */
530 if (emsg_not_now())
531 return TRUE;
532
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 called_emsg = TRUE;
Bram Moolenaar8b778d52016-02-18 20:31:34 +0100534 if (emsg_silent == 0)
535 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000538 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
539 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
541#ifdef FEAT_EVAL
542 severe = emsg_severe;
543 emsg_severe = FALSE;
544#endif
545
Bram Moolenaar57657d82006-04-21 22:12:41 +0000546 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {
548#ifdef FEAT_EVAL
549 /*
550 * Cause a throw of an error exception if appropriate. Don't display
551 * the error message in this case. (If no matching catch clause will
552 * be found, the message will be displayed later on.) "ignore" is set
553 * when the message should be ignored completely (used for the
554 * interrupt message).
555 */
556 if (cause_errthrow(s, severe, &ignore) == TRUE)
557 {
558 if (!ignore)
559 did_emsg = TRUE;
560 return TRUE;
561 }
562
563 /* set "v:errmsg", also when using ":silent! cmd" */
564 set_vim_var_string(VV_ERRMSG, s, -1);
565#endif
566
567 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000568 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 * But do write it to the redirection file.
570 */
571 if (emsg_silent != 0)
572 {
573 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000574 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (p != NULL)
576 {
577 STRCAT(p, "\n");
578 redir_write(p, -1);
579 vim_free(p);
580 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000581 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 if (p != NULL)
583 {
584 STRCAT(p, "\n");
585 redir_write(p, -1);
586 vim_free(p);
587 }
588 redir_write(s, -1);
589 return TRUE;
590 }
591
592 /* Reset msg_silent, an error causes messages to be switched back on. */
593 msg_silent = 0;
594 cmd_silent = FALSE;
595
596 if (global_busy) /* break :global command */
597 ++global_busy;
598
599 if (p_eb)
600 beep_flush(); /* also includes flush_buffers() */
601 else
602 flush_buffers(FALSE); /* flush internal buffers */
603 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
605
606 emsg_on_display = TRUE; /* remember there is an error message */
607 ++msg_scroll; /* don't overwrite a previous message */
608 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000609 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 need_wait_return = TRUE; /* needed in case emsg() is called after
611 * wait_return has reset need_wait_return
612 * and a redraw is expected because
613 * msg_scrolled is non-zero */
614
615 /*
616 * Display name and line number for the source of the error.
617 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000618 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 /*
621 * Display the error message itself.
622 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000623 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 return msg_attr(s, attr);
625}
626
627/*
628 * Print an error message with one "%s" and one string argument.
629 */
630 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100631emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 return emsg3(s, a1, NULL);
634}
635
Bram Moolenaarea424162005-06-16 21:51:00 +0000636/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaardf177f62005-02-22 08:39:57 +0000638 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100639emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000640{
641 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
642}
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644/*
645 * Like msg(), but truncate to a single line if p_shm contains 't', or when
646 * "force" is TRUE. This truncates in another way as for normal messages.
647 * Careful: The string may be changed by msg_may_trunc()!
648 * Returns a pointer to the printed message, if wait_return() not called.
649 */
650 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100651msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int n;
654
655 /* Add message to history before truncating */
656 add_msg_hist(s, -1, attr);
657
658 s = msg_may_trunc(force, s);
659
660 msg_hist_off = TRUE;
661 n = msg_attr(s, attr);
662 msg_hist_off = FALSE;
663
664 if (n)
665 return s;
666 return NULL;
667}
668
669/*
670 * Check if message "s" should be truncated at the start (for filenames).
671 * Return a pointer to where the truncated message starts.
672 * Note: May change the message by replacing a character with '<'.
673 */
674 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100675msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
677 int n;
678 int room;
679
680 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
681 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
682 && (n = (int)STRLEN(s) - room) > 0)
683 {
684#ifdef FEAT_MBYTE
685 if (has_mbyte)
686 {
687 int size = vim_strsize(s);
688
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000689 /* There may be room anyway when there are multibyte chars. */
690 if (size <= room)
691 return s;
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 for (n = 0; size >= room; )
694 {
695 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000696 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698 --n;
699 }
700#endif
701 s += n;
702 *s = '<';
703 }
704 return s;
705}
706
707 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100708add_msg_hist(
709 char_u *s,
710 int len, /* -1 for undetermined length */
711 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 struct msg_hist *p;
714
715 if (msg_hist_off || msg_silent != 0)
716 return;
717
718 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000719 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000720 (void)delete_first_msg();
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 /* allocate an entry and add the message at the end of the history */
723 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
724 if (p != NULL)
725 {
726 if (len < 0)
727 len = (int)STRLEN(s);
728 /* remove leading and trailing newlines */
729 while (len > 0 && *s == '\n')
730 {
731 ++s;
732 --len;
733 }
734 while (len > 0 && s[len - 1] == '\n')
735 --len;
736 p->msg = vim_strnsave(s, len);
737 p->next = NULL;
738 p->attr = attr;
739 if (last_msg_hist != NULL)
740 last_msg_hist->next = p;
741 last_msg_hist = p;
742 if (first_msg_hist == NULL)
743 first_msg_hist = last_msg_hist;
744 ++msg_hist_len;
745 }
746}
747
748/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000749 * Delete the first (oldest) message from the history.
750 * Returns FAIL if there are no messages.
751 */
752 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100753delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000754{
755 struct msg_hist *p;
756
757 if (msg_hist_len <= 0)
758 return FAIL;
759 p = first_msg_hist;
760 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000761 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200762 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000763 vim_free(p->msg);
764 vim_free(p);
765 --msg_hist_len;
766 return OK;
767}
768
769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 * ":messages" command.
771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100773ex_messages(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 struct msg_hist *p;
776 char_u *s;
777
778 msg_hist_off = TRUE;
779
780 s = mch_getenv((char_u *)"LANG");
781 if (s != NULL && *s != NUL)
782 msg_attr((char_u *)
783 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
784 hl_attr(HLF_T));
785
Bram Moolenaar5c2e0f22007-09-13 20:05:18 +0000786 for (p = first_msg_hist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (p->msg != NULL)
788 msg_attr(p->msg, p->attr);
789
790 msg_hist_off = FALSE;
791}
792
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000793#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794/*
795 * Call this after prompting the user. This will avoid a hit-return message
796 * and a delay.
797 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000798 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100799msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 need_wait_return = FALSE;
802 emsg_on_display = FALSE;
803 cmdline_row = msg_row;
804 msg_col = 0;
805 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100806 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807}
808#endif
809
810/*
811 * wait for the user to hit a key (normally a return)
812 * if 'redraw' is TRUE, clear and redraw the screen
813 * if 'redraw' is FALSE, just redraw the screen
814 * if 'redraw' is -1, don't redraw at all
815 */
816 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100817wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
819 int c;
820 int oldState;
821 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100823 int save_Recording;
824 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 if (redraw == TRUE)
827 must_redraw = CLEAR;
828
829 /* If using ":silent cmd", don't wait for a return. Also don't set
830 * need_wait_return to do it later. */
831 if (msg_silent != 0)
832 return;
833
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100834 /*
835 * When inside vgetc(), we can't wait for a typed character at all.
836 * With the global command (and some others) we only need one return at
837 * the end. Adjust cmdline_row to avoid the next message overwriting the
838 * last one.
839 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000840 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100842 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (no_wait_return)
844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (!exmode_active)
846 cmdline_row = msg_row;
847 return;
848 }
849
850 redir_off = TRUE; /* don't redirect this message */
851 oldState = State;
852 if (quit_more)
853 {
854 c = CAR; /* just pretend CR was hit */
855 quit_more = FALSE;
856 got_int = FALSE;
857 }
858 else if (exmode_active)
859 {
860 MSG_PUTS(" "); /* make sure the cursor is on the right line */
861 c = CAR; /* no need for a return in ex mode */
862 got_int = FALSE;
863 }
864 else
865 {
866 /* Make sure the hit-return prompt is on screen when 'guioptions' was
867 * just changed. */
868 screenalloc(FALSE);
869
870 State = HITRETURN;
871#ifdef FEAT_MOUSE
872 setmouse();
873#endif
874#ifdef USE_ON_FLY_SCROLL
875 dont_scroll = TRUE; /* disallow scrolling here */
876#endif
Bram Moolenaarec38d692013-04-24 15:12:32 +0200877 /* Avoid the sequence that the user types ":" at the hit-return prompt
878 * to start an Ex command, but the file-changed dialog gets in the
879 * way. */
880 if (need_check_timestamps)
881 check_timestamps(FALSE);
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 hit_return_msg();
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 do
886 {
887 /* Remember "got_int", if it is set vgetc() probably returns a
888 * CTRL-C, but we need to loop then. */
889 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000890
891 /* Don't do mappings here, we put the character back in the
892 * typeahead buffer. */
893 ++no_mapping;
894 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100895
896 /* Temporarily disable Recording. If Recording is active, the
897 * character will be recorded later, since it will be added to the
898 * typebuf after the loop */
899 save_Recording = Recording;
900 save_scriptout = scriptout;
901 Recording = FALSE;
902 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000904 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000906 --no_mapping;
907 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100908 Recording = save_Recording;
909 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#ifdef FEAT_CLIPBOARD
912 /* Strange way to allow copying (yanking) a modeless selection at
913 * the hit-enter prompt. Use CTRL-Y, because the same is used in
914 * Cmdline-mode and it's harmless when there is no selection. */
915 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
916 {
917 clip_copy_modeless_selection(TRUE);
918 c = K_IGNORE;
919 }
920#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000921
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000922 /*
923 * Allow scrolling back in the messages.
924 * Also accept scroll-down commands when messages fill the screen,
925 * to avoid that typing one 'j' too many makes the messages
926 * disappear.
927 */
928 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000929 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100930 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
931 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000932 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100933 if (msg_scrolled > Rows)
934 /* scroll back to show older messages */
935 do_more_prompt(c);
936 else
937 {
938 msg_didout = FALSE;
939 c = K_IGNORE;
940 msg_col =
941#ifdef FEAT_RIGHTLEFT
942 cmdmsg_rl ? Columns - 1 :
943#endif
944 0;
945 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000946 if (quit_more)
947 {
948 c = CAR; /* just pretend CR was hit */
949 quit_more = FALSE;
950 got_int = FALSE;
951 }
Bram Moolenaarb0912962013-08-09 20:38:26 +0200952 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000953 {
954 c = K_IGNORE;
955 hit_return_msg();
956 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000957 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000958 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100959 && (c == 'j' || c == 'd' || c == 'f'
960 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000961 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 } while ((had_got_int && c == Ctrl_C)
964 || c == K_IGNORE
965#ifdef FEAT_GUI
966 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
967#endif
968#ifdef FEAT_MOUSE
969 || c == K_LEFTDRAG || c == K_LEFTRELEASE
970 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
971 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200972 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 || c == K_MOUSEDOWN || c == K_MOUSEUP
974 || (!mouse_has(MOUSE_RETURN)
975 && mouse_row < msg_row
976 && (c == K_LEFTMOUSE
977 || c == K_MIDDLEMOUSE
978 || c == K_RIGHTMOUSE
979 || c == K_X1MOUSE
980 || c == K_X2MOUSE))
981#endif
982 );
983 ui_breakcheck();
984#ifdef FEAT_MOUSE
985 /*
986 * Avoid that the mouse-up event causes visual mode to start.
987 */
988 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
989 || c == K_X1MOUSE || c == K_X2MOUSE)
990 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
991 else
992#endif
993 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
994 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000995 /* Put the character back in the typeahead buffer. Don't use the
996 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000997 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000999 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 redir_off = FALSE;
1003
1004 /*
1005 * If the user hits ':', '?' or '/' we get a command line from the next
1006 * line.
1007 */
1008 if (c == ':' || c == '?' || c == '/')
1009 {
1010 if (!exmode_active)
1011 cmdline_row = msg_row;
1012 skip_redraw = TRUE; /* skip redraw once */
1013 do_redraw = FALSE;
1014 }
1015
1016 /*
1017 * If the window size changed set_shellsize() will redraw the screen.
1018 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1019 * typed.
1020 */
1021 tmpState = State;
1022 State = oldState; /* restore State before set_shellsize */
1023#ifdef FEAT_MOUSE
1024 setmouse();
1025#endif
1026 msg_check();
1027
1028#if defined(UNIX) || defined(VMS)
1029 /*
1030 * When switching screens, we need to output an extra newline on exit.
1031 */
1032 if (swapping_screen() && !termcap_active)
1033 newline_on_exit = TRUE;
1034#endif
1035
1036 need_wait_return = FALSE;
1037 did_wait_return = TRUE;
1038 emsg_on_display = FALSE; /* can delete error message now */
1039 lines_left = -1; /* reset lines_left at next msg_start() */
1040 reset_last_sourcing();
1041 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1042 (Rows - cmdline_row - 1) * Columns + sc_col)
1043 {
1044 vim_free(keep_msg);
1045 keep_msg = NULL; /* don't redisplay message, it's too long */
1046 }
1047
1048 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1049 {
1050 starttermcap(); /* start termcap before redrawing */
1051 shell_resized();
1052 }
1053 else if (!skip_redraw
1054 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1055 {
1056 starttermcap(); /* start termcap before redrawing */
1057 redraw_later(VALID);
1058 }
1059}
1060
1061/*
1062 * Write the hit-return prompt.
1063 */
1064 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001065hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001067 int save_p_more = p_more;
1068
1069 p_more = FALSE; /* don't want see this message when scrolling back */
1070 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 msg_putchar('\n');
1072 if (got_int)
1073 MSG_PUTS(_("Interrupt: "));
1074
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001075 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (!msg_use_printf())
1077 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001078 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079}
1080
1081/*
1082 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1083 */
1084 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001085set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
1087 vim_free(keep_msg);
1088 if (s != NULL && msg_silent == 0)
1089 keep_msg = vim_strsave(s);
1090 else
1091 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001092 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001093 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094}
1095
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001096#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1097/*
1098 * If there currently is a message being displayed, set "keep_msg" to it, so
1099 * that it will be displayed again after redraw.
1100 */
1101 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001102set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001103{
1104 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1105 && (State & NORMAL))
1106 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1107}
1108#endif
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110/*
1111 * Prepare for outputting characters in the command line.
1112 */
1113 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001114msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 int did_return = FALSE;
1117
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001118 if (!msg_silent)
1119 {
1120 vim_free(keep_msg);
1121 keep_msg = NULL; /* don't display old message now */
1122 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001123
1124#ifdef FEAT_EVAL
1125 if (need_clr_eos)
1126 {
1127 /* Halfway an ":echo" command and getting an (error) message: clear
1128 * any text from the command. */
1129 need_clr_eos = FALSE;
1130 msg_clr_eos();
1131 }
1132#endif
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (!msg_scroll && full_screen) /* overwrite last message */
1135 {
1136 msg_row = cmdline_row;
1137 msg_col =
1138#ifdef FEAT_RIGHTLEFT
1139 cmdmsg_rl ? Columns - 1 :
1140#endif
1141 0;
1142 }
1143 else if (msg_didout) /* start message on next line */
1144 {
1145 msg_putchar('\n');
1146 did_return = TRUE;
1147 if (exmode_active != EXMODE_NORMAL)
1148 cmdline_row = msg_row;
1149 }
1150 if (!msg_didany || lines_left < 0)
1151 msg_starthere();
1152 if (msg_silent == 0)
1153 {
1154 msg_didout = FALSE; /* no output on current line yet */
1155 cursor_off();
1156 }
1157
1158 /* when redirecting, may need to start a new line. */
1159 if (!did_return)
1160 redir_write((char_u *)"\n", -1);
1161}
1162
1163/*
1164 * Note that the current msg position is where messages start.
1165 */
1166 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001167msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
1169 lines_left = cmdline_row;
1170 msg_didany = FALSE;
1171}
1172
1173 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001174msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175{
1176 msg_putchar_attr(c, 0);
1177}
1178
1179 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001180msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
1182#ifdef FEAT_MBYTE
1183 char_u buf[MB_MAXBYTES + 1];
1184#else
1185 char_u buf[4];
1186#endif
1187
1188 if (IS_SPECIAL(c))
1189 {
1190 buf[0] = K_SPECIAL;
1191 buf[1] = K_SECOND(c);
1192 buf[2] = K_THIRD(c);
1193 buf[3] = NUL;
1194 }
1195 else
1196 {
1197#ifdef FEAT_MBYTE
1198 buf[(*mb_char2bytes)(c, buf)] = NUL;
1199#else
1200 buf[0] = c;
1201 buf[1] = NUL;
1202#endif
1203 }
1204 msg_puts_attr(buf, attr);
1205}
1206
1207 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001208msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
1210 char_u buf[20];
1211
1212 sprintf((char *)buf, "%ld", n);
1213 msg_puts(buf);
1214}
1215
1216 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001217msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218{
1219 msg_home_replace_attr(fname, 0);
1220}
1221
1222#if defined(FEAT_FIND_ID) || defined(PROTO)
1223 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001224msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225{
1226 msg_home_replace_attr(fname, hl_attr(HLF_D));
1227}
1228#endif
1229
1230 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001231msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232{
1233 char_u *name;
1234
1235 name = home_replace_save(NULL, fname);
1236 if (name != NULL)
1237 msg_outtrans_attr(name, attr);
1238 vim_free(name);
1239}
1240
1241/*
1242 * Output 'len' characters in 'str' (including NULs) with translation
1243 * if 'len' is -1, output upto a NUL character.
1244 * Use attributes 'attr'.
1245 * Return the number of characters it takes on the screen.
1246 */
1247 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001248msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 return msg_outtrans_attr(str, 0);
1251}
1252
1253 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001254msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
1256 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1257}
1258
1259 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001260msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
1262 return msg_outtrans_len_attr(str, len, 0);
1263}
1264
1265/*
1266 * Output one character at "p". Return pointer to the next character.
1267 * Handles multi-byte characters.
1268 */
1269 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001270msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272#ifdef FEAT_MBYTE
1273 int l;
1274
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001275 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 {
1277 msg_outtrans_len_attr(p, l, attr);
1278 return p + l;
1279 }
1280#endif
1281 msg_puts_attr(transchar_byte(*p), attr);
1282 return p + 1;
1283}
1284
1285 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001286msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 int retval = 0;
1289 char_u *str = msgstr;
1290 char_u *plain_start = msgstr;
1291 char_u *s;
1292#ifdef FEAT_MBYTE
1293 int mb_l;
1294 int c;
1295#endif
1296
1297 /* if MSG_HIST flag set, add message to history */
1298 if (attr & MSG_HIST)
1299 {
1300 add_msg_hist(str, len, attr);
1301 attr &= ~MSG_HIST;
1302 }
1303
1304#ifdef FEAT_MBYTE
1305 /* If the string starts with a composing character first draw a space on
1306 * which the composing char can be drawn. */
1307 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1308 msg_puts_attr((char_u *)" ", attr);
1309#endif
1310
1311 /*
1312 * Go over the string. Special characters are translated and printed.
1313 * Normal characters are printed several at a time.
1314 */
1315 while (--len >= 0)
1316 {
1317#ifdef FEAT_MBYTE
1318 if (enc_utf8)
1319 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001320 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001322 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 else
1324 mb_l = 1;
1325 if (has_mbyte && mb_l > 1)
1326 {
1327 c = (*mb_ptr2char)(str);
1328 if (vim_isprintc(c))
1329 /* printable multi-byte char: count the cells. */
1330 retval += (*mb_ptr2cells)(str);
1331 else
1332 {
1333 /* unprintable multi-byte char: print the printable chars so
1334 * far and the translation of the unprintable char. */
1335 if (str > plain_start)
1336 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1337 attr);
1338 plain_start = str + mb_l;
1339 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1340 retval += char2cells(c);
1341 }
1342 len -= mb_l - 1;
1343 str += mb_l;
1344 }
1345 else
1346#endif
1347 {
1348 s = transchar_byte(*str);
1349 if (s[1] != NUL)
1350 {
1351 /* unprintable char: print the printable chars so far and the
1352 * translation of the unprintable char. */
1353 if (str > plain_start)
1354 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1355 attr);
1356 plain_start = str + 1;
1357 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001358 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001360 else
1361 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 ++str;
1363 }
1364 }
1365
1366 if (str > plain_start)
1367 /* print the printable chars at the end */
1368 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1369
1370 return retval;
1371}
1372
1373#if defined(FEAT_QUICKFIX) || defined(PROTO)
1374 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001375msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 int i;
1378 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1379
1380 arg = skipwhite(arg);
1381 for (i = 5; *arg && i >= 0; --i)
1382 if (*arg++ != str[i])
1383 break;
1384 if (i < 0)
1385 {
1386 msg_putchar('\n');
1387 for (i = 0; rs[i]; ++i)
1388 msg_putchar(rs[i] - 3);
1389 }
1390}
1391#endif
1392
1393/*
1394 * Output the string 'str' upto a NUL character.
1395 * Return the number of characters it takes on the screen.
1396 *
1397 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1398 * following character and shown as <F1>, <S-Up> etc. Any other character
1399 * which is not printable shown in <> form.
1400 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1401 * If a character is displayed in one of these special ways, is also
1402 * highlighted (its highlight name is '8' in the p_hl variable).
1403 * Otherwise characters are not highlighted.
1404 * This function is used to show mappings, where we want to see how to type
1405 * the character/string -- webb
1406 */
1407 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001408msg_outtrans_special(
1409 char_u *strstart,
1410 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 char_u *str = strstart;
1413 int retval = 0;
1414 char_u *string;
1415 int attr;
1416 int len;
1417
1418 attr = hl_attr(HLF_8);
1419 while (*str != NUL)
1420 {
1421 /* Leading and trailing spaces need to be displayed in <> form. */
1422 if ((str == strstart || str[1] == NUL) && *str == ' ')
1423 {
1424 string = (char_u *)"<Space>";
1425 ++str;
1426 }
1427 else
1428 string = str2special(&str, from);
1429 len = vim_strsize(string);
1430 /* Highlight special keys */
1431 msg_puts_attr(string, len > 1
1432#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001433 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#endif
1435 ? attr : 0);
1436 retval += len;
1437 }
1438 return retval;
1439}
1440
Bram Moolenaarbd743252010-10-20 21:23:33 +02001441#if defined(FEAT_EVAL) || defined(PROTO)
1442/*
1443 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1444 * strings, in an allocated string.
1445 */
1446 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001447str2special_save(
1448 char_u *str,
1449 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001450{
1451 garray_T ga;
1452 char_u *p = str;
1453
1454 ga_init2(&ga, 1, 40);
1455 while (*p != NUL)
1456 ga_concat(&ga, str2special(&p, is_lhs));
1457 ga_append(&ga, NUL);
1458 return (char_u *)ga.ga_data;
1459}
1460#endif
1461
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462/*
1463 * Return the printable string for the key codes at "*sp".
1464 * Used for translating the lhs or rhs of a mapping to printable chars.
1465 * Advances "sp" to the next code.
1466 */
1467 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001468str2special(
1469 char_u **sp,
1470 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 int c;
1473 static char_u buf[7];
1474 char_u *str = *sp;
1475 int modifiers = 0;
1476 int special = FALSE;
1477
1478#ifdef FEAT_MBYTE
1479 if (has_mbyte)
1480 {
1481 char_u *p;
1482
1483 /* Try to un-escape a multi-byte character. Return the un-escaped
1484 * string if it is a multi-byte character. */
1485 p = mb_unescape(sp);
1486 if (p != NULL)
1487 return p;
1488 }
1489#endif
1490
1491 c = *str;
1492 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1493 {
1494 if (str[1] == KS_MODIFIER)
1495 {
1496 modifiers = str[2];
1497 str += 3;
1498 c = *str;
1499 }
1500 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1501 {
1502 c = TO_SPECIAL(str[1], str[2]);
1503 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001504 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 c = NUL;
1506 }
1507 if (IS_SPECIAL(c) || modifiers) /* special key */
1508 special = TRUE;
1509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
1511#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001512 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001514 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001515
1516 /* For multi-byte characters check for an illegal byte. */
1517 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1518 {
1519 transchar_nonprint(buf, c);
1520 *sp = str + 1;
1521 return buf;
1522 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001523 /* Since 'special' is TRUE the multi-byte character 'c' will be
1524 * processed by get_special_key_name() */
1525 c = (*mb_ptr2char)(str);
1526 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001530 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
1532 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1533 * Use <Space> only for lhs of a mapping. */
1534 if (special || char2cells(c) > 1 || (from && c == ' '))
1535 return get_special_key_name(c, modifiers);
1536 buf[0] = c;
1537 buf[1] = NUL;
1538 return buf;
1539}
1540
1541/*
1542 * Translate a key sequence into special key names.
1543 */
1544 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001545str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 char_u *s;
1548
1549 *buf = NUL;
1550 while (*sp)
1551 {
1552 s = str2special(&sp, FALSE);
1553 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1554 STRCAT(buf, s);
1555 }
1556}
1557
1558/*
1559 * print line for :print or :list command
1560 */
1561 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001562msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 int c;
1565 int col = 0;
1566 int n_extra = 0;
1567 int c_extra = 0;
1568 char_u *p_extra = NULL; /* init to make SASC shut up */
1569 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001570 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 char_u *trail = NULL;
1572#ifdef FEAT_MBYTE
1573 int l;
1574 char_u buf[MB_MAXBYTES + 1];
1575#endif
1576
Bram Moolenaardf177f62005-02-22 08:39:57 +00001577 if (curwin->w_p_list)
1578 list = TRUE;
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001581 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 trail = s + STRLEN(s);
1584 while (trail > s && vim_iswhite(trail[-1]))
1585 --trail;
1586 }
1587
1588 /* output a space for an empty line, otherwise the line will be
1589 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001590 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 msg_putchar(' ');
1592
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001595 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 --n_extra;
1598 if (c_extra)
1599 c = c_extra;
1600 else
1601 c = *p_extra++;
1602 }
1603#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001604 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {
1606 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001607 if (lcs_nbsp != NUL && list
1608 && (mb_ptr2char(s) == 160
1609 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001610 {
1611 mb_char2bytes(lcs_nbsp, buf);
1612 buf[(*mb_ptr2len)(buf)] = NUL;
1613 }
1614 else
1615 {
1616 mch_memmove(buf, s, (size_t)l);
1617 buf[l] = NUL;
1618 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001619 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 s += l;
1621 continue;
1622 }
1623#endif
1624 else
1625 {
1626 attr = 0;
1627 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001628 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 /* tab amount depends on current column */
1631 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001632 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 c = ' ';
1635 c_extra = ' ';
1636 }
1637 else
1638 {
1639 c = lcs_tab1;
1640 c_extra = lcs_tab2;
1641 attr = hl_attr(HLF_8);
1642 }
1643 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001644 else if (c == 160 && list && lcs_nbsp != NUL)
1645 {
1646 c = lcs_nbsp;
1647 attr = hl_attr(HLF_8);
1648 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001649 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 p_extra = (char_u *)"";
1652 c_extra = NUL;
1653 n_extra = 1;
1654 c = lcs_eol;
1655 attr = hl_attr(HLF_AT);
1656 --s;
1657 }
1658 else if (c != NUL && (n = byte2cells(c)) > 1)
1659 {
1660 n_extra = n - 1;
1661 p_extra = transchar_byte(c);
1662 c_extra = NUL;
1663 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001664 /* Use special coloring to be able to distinguish <hex> from
1665 * the same in plain text. */
1666 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 else if (c == ' ' && trail != NULL && s > trail)
1669 {
1670 c = lcs_trail;
1671 attr = hl_attr(HLF_8);
1672 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001673 else if (c == ' ' && list && lcs_space != NUL)
1674 {
1675 c = lcs_space;
1676 attr = hl_attr(HLF_8);
1677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679
1680 if (c == NUL)
1681 break;
1682
1683 msg_putchar_attr(c, attr);
1684 col++;
1685 }
1686 msg_clr_eos();
1687}
1688
1689#ifdef FEAT_MBYTE
1690/*
1691 * Use screen_puts() to output one multi-byte character.
1692 * Return the pointer "s" advanced to the next character.
1693 */
1694 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001695screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 int cw;
1698
1699 msg_didout = TRUE; /* remember that line is not empty */
1700 cw = (*mb_ptr2cells)(s);
1701 if (cw > 1 && (
1702#ifdef FEAT_RIGHTLEFT
1703 cmdmsg_rl ? msg_col <= 1 :
1704#endif
1705 msg_col == Columns - 1))
1706 {
1707 /* Doesn't fit, print a highlighted '>' to fill it up. */
1708 msg_screen_putchar('>', hl_attr(HLF_AT));
1709 return s;
1710 }
1711
1712 screen_puts_len(s, l, msg_row, msg_col, attr);
1713#ifdef FEAT_RIGHTLEFT
1714 if (cmdmsg_rl)
1715 {
1716 msg_col -= cw;
1717 if (msg_col == 0)
1718 {
1719 msg_col = Columns;
1720 ++msg_row;
1721 }
1722 }
1723 else
1724#endif
1725 {
1726 msg_col += cw;
1727 if (msg_col >= Columns)
1728 {
1729 msg_col = 0;
1730 ++msg_row;
1731 }
1732 }
1733 return s + l;
1734}
1735#endif
1736
1737/*
1738 * Output a string to the screen at position msg_row, msg_col.
1739 * Update msg_row and msg_col for the next message.
1740 */
1741 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001742msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001744 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
1747 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001748msg_puts_title(
1749 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 msg_puts_attr(s, hl_attr(HLF_T));
1752}
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754/*
1755 * Show a message in such a way that it always fits in the line. Cut out a
1756 * part in the middle and replace it with "..." when necessary.
1757 * Does not handle multi-byte characters!
1758 */
1759 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001760msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001762 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763}
1764
1765 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001766msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 int slen = len;
1769 int room;
1770
1771 room = Columns - msg_col;
1772 if (len > room && room >= 20)
1773 {
1774 slen = (room - 3) / 2;
1775 msg_outtrans_len_attr(longstr, slen, attr);
1776 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1777 }
1778 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1779}
1780
1781/*
1782 * Basic function for writing a message with highlight attributes.
1783 */
1784 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001785msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 msg_puts_attr_len(s, -1, attr);
1788}
1789
1790/*
1791 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1792 * When "maxlen" is -1 there is no maximum length.
1793 * When "maxlen" is >= 0 the message is not put in the history.
1794 */
1795 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001796msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 /*
1799 * If redirection is on, also write to the redirection file.
1800 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001801 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803 /*
1804 * Don't print anything when using ":silent cmd".
1805 */
1806 if (msg_silent != 0)
1807 return;
1808
1809 /* if MSG_HIST flag set, add message to history */
1810 if ((attr & MSG_HIST) && maxlen < 0)
1811 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001812 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 attr &= ~MSG_HIST;
1814 }
1815
1816 /*
1817 * When writing something to the screen after it has scrolled, requires a
1818 * wait-return prompt later. Needed when scrolling, resetting
1819 * need_wait_return after some prompt, and then outputting something
1820 * without scrolling
1821 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001822 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 need_wait_return = TRUE;
1824 msg_didany = TRUE; /* remember that something was outputted */
1825
1826 /*
1827 * If there is no valid screen, use fprintf so we can see error messages.
1828 * If termcap is not active, we may be writing in an alternate console
1829 * window, cursor positioning may not work correctly (window size may be
1830 * different, e.g. for Win32 console) or we just don't know where the
1831 * cursor is.
1832 */
1833 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001834 msg_puts_printf(str, maxlen);
1835 else
1836 msg_puts_display(str, maxlen, attr, FALSE);
1837}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001839/*
1840 * The display part of msg_puts_attr_len().
1841 * May be called recursively to display scroll-back text.
1842 */
1843 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001844msg_puts_display(
1845 char_u *str,
1846 int maxlen,
1847 int attr,
1848 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001849{
1850 char_u *s = str;
1851 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1852 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1853#ifdef FEAT_MBYTE
1854 int l;
1855 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001857 char_u *sb_str = str;
1858 int sb_col = msg_col;
1859 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001860 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
1862 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001863 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001866 * We are at the end of the screen line when:
1867 * - When outputting a newline.
1868 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001870 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#ifdef FEAT_RIGHTLEFT
1872 cmdmsg_rl
1873 ? (
1874 msg_col <= 1
1875 || (*s == TAB && msg_col <= 7)
1876# ifdef FEAT_MBYTE
1877 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1878# endif
1879 )
1880 :
1881#endif
1882 (msg_col + t_col >= Columns - 1
1883 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1884# ifdef FEAT_MBYTE
1885 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1886 && msg_col + t_col >= Columns - 2)
1887# endif
1888 ))))
1889 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001890 /*
1891 * The screen is scrolled up when at the last row (some terminals
1892 * scroll automatically, some don't. To avoid problems we scroll
1893 * ourselves).
1894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001897 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001899 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (msg_no_more && lines_left == 0)
1901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001903 /* Scroll the screen up one line. */
1904 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 msg_row = Rows - 2;
1907 if (msg_col >= Columns) /* can happen after screen resize */
1908 msg_col = Columns - 1;
1909
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001910 /* Display char in last column before showing more-prompt. */
1911 if (*s >= ' '
1912#ifdef FEAT_RIGHTLEFT
1913 && !cmdmsg_rl
1914#endif
1915 )
1916 {
1917#ifdef FEAT_MBYTE
1918 if (has_mbyte)
1919 {
1920 if (enc_utf8 && maxlen >= 0)
1921 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001922 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001923 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001924 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001925 s = screen_puts_mbyte(s, l, attr);
1926 }
1927 else
1928#endif
1929 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001930 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001931 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001932 else
1933 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001934
1935 if (p_more)
1936 /* store text for scrolling back */
1937 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1938
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001939 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001940 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (must_redraw < VALID)
1942 must_redraw = VALID;
1943 redraw_cmdline = TRUE;
1944 if (cmdline_row > 0 && !exmode_active)
1945 --cmdline_row;
1946
1947 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001948 * If screen is completely filled and 'more' is set then wait
1949 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001951 if (lines_left > 0)
1952 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001953 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 && !msg_no_more && !exmode_active)
1955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001957 if (do_more_prompt(NUL))
1958 s = confirm_msg_tail;
1959#else
1960 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#endif
1962 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001963 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001965
1966 /* When we displayed a char in last column need to check if there
1967 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001968 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001969 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001972 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 || msg_col + t_col >= Columns
1974#ifdef FEAT_MBYTE
1975 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1976 && msg_col + t_col >= Columns - 1)
1977#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001978 ;
1979 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
1980 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001982 t_puts(&t_col, t_s, s, attr);
1983
1984 if (wrap && p_more && !recurse)
1985 /* store text for scrolling back */
1986 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988 if (*s == '\n') /* go to next line */
1989 {
1990 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001991#ifdef FEAT_RIGHTLEFT
1992 if (cmdmsg_rl)
1993 msg_col = Columns - 1;
1994 else
1995#endif
1996 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (++msg_row >= Rows) /* safety check */
1998 msg_row = Rows - 1;
1999 }
2000 else if (*s == '\r') /* go to column 0 */
2001 {
2002 msg_col = 0;
2003 }
2004 else if (*s == '\b') /* go to previous char */
2005 {
2006 if (msg_col)
2007 --msg_col;
2008 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002009 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 do
2012 msg_screen_putchar(' ', attr);
2013 while (msg_col & 7);
2014 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002015 else if (*s == BELL) /* beep (from ":sh") */
2016 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 else
2018 {
2019#ifdef FEAT_MBYTE
2020 if (has_mbyte)
2021 {
2022 cw = (*mb_ptr2cells)(s);
2023 if (enc_utf8 && maxlen >= 0)
2024 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002025 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002027 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 else
2030 {
2031 cw = 1;
2032 l = 1;
2033 }
2034#endif
2035 /* When drawing from right to left or when a double-wide character
2036 * doesn't fit, draw a single character here. Otherwise collect
2037 * characters and draw them all at once later. */
2038#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2039 if (
2040# ifdef FEAT_RIGHTLEFT
2041 cmdmsg_rl
2042# ifdef FEAT_MBYTE
2043 ||
2044# endif
2045# endif
2046# ifdef FEAT_MBYTE
2047 (cw > 1 && msg_col + t_col >= Columns - 1)
2048# endif
2049 )
2050 {
2051# ifdef FEAT_MBYTE
2052 if (l > 1)
2053 s = screen_puts_mbyte(s, l, attr) - 1;
2054 else
2055# endif
2056 msg_screen_putchar(*s, attr);
2057 }
2058 else
2059#endif
2060 {
2061 /* postpone this character until later */
2062 if (t_col == 0)
2063 t_s = s;
2064#ifdef FEAT_MBYTE
2065 t_col += cw;
2066 s += l - 1;
2067#else
2068 ++t_col;
2069#endif
2070 }
2071 }
2072 ++s;
2073 }
2074
2075 /* output any postponed text */
2076 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002077 t_puts(&t_col, t_s, s, attr);
2078 if (p_more && !recurse)
2079 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 msg_check();
2082}
2083
2084/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002085 * Scroll the screen up one line for displaying the next message line.
2086 */
2087 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002088msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002089{
2090#ifdef FEAT_GUI
2091 /* Remove the cursor before scrolling, ScreenLines[] is going
2092 * to become invalid. */
2093 if (gui.in_use)
2094 gui_undraw_cursor();
2095#endif
2096 /* scrolling up always works */
2097 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2098
2099 if (!can_clear((char_u *)" "))
2100 {
2101 /* Scrolling up doesn't result in the right background. Set the
2102 * background here. It's not efficient, but avoids that we have to do
2103 * it all over the code. */
2104 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2105
2106 /* Also clear the last char of the last but one line if it was not
2107 * cleared before to avoid a scroll-up. */
2108 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2109 screen_fill((int)Rows - 2, (int)Rows - 1,
2110 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2111 }
2112}
2113
2114/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002115 * Increment "msg_scrolled".
2116 */
2117 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002118inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002119{
2120#ifdef FEAT_EVAL
2121 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2122 {
2123 char_u *p = sourcing_name;
2124 char_u *tofree = NULL;
2125 int len;
2126
2127 /* v:scrollstart is empty, set it to the script/function name and line
2128 * number */
2129 if (p == NULL)
2130 p = (char_u *)_("Unknown");
2131 else
2132 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002133 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002134 tofree = alloc(len);
2135 if (tofree != NULL)
2136 {
2137 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2138 p, (long)sourcing_lnum);
2139 p = tofree;
2140 }
2141 }
2142 set_vim_var_string(VV_SCROLLSTART, p, -1);
2143 vim_free(tofree);
2144 }
2145#endif
2146 ++msg_scrolled;
2147}
2148
2149/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002150 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2151 * store the displayed text and remember where screen lines start.
2152 */
2153typedef struct msgchunk_S msgchunk_T;
2154struct msgchunk_S
2155{
2156 msgchunk_T *sb_next;
2157 msgchunk_T *sb_prev;
2158 char sb_eol; /* TRUE when line ends after this text */
2159 int sb_msg_col; /* column in which text starts */
2160 int sb_attr; /* text attributes */
2161 char_u sb_text[1]; /* text to be displayed, actually longer */
2162};
2163
2164static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2165
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002166static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2167static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002168
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002169static int do_clear_sb_text = FALSE; /* clear text on next msg */
2170
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002171/*
2172 * Store part of a printed message for displaying when scrolling back.
2173 */
2174 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002175store_sb_text(
2176 char_u **sb_str, /* start of string */
2177 char_u *s, /* just after string */
2178 int attr,
2179 int *sb_col,
2180 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002181{
2182 msgchunk_T *mp;
2183
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002184 if (do_clear_sb_text)
2185 {
2186 clear_sb_text();
2187 do_clear_sb_text = FALSE;
2188 }
2189
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002190 if (s > *sb_str)
2191 {
2192 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2193 if (mp != NULL)
2194 {
2195 mp->sb_eol = finish;
2196 mp->sb_msg_col = *sb_col;
2197 mp->sb_attr = attr;
2198 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2199
2200 if (last_msgchunk == NULL)
2201 {
2202 last_msgchunk = mp;
2203 mp->sb_prev = NULL;
2204 }
2205 else
2206 {
2207 mp->sb_prev = last_msgchunk;
2208 last_msgchunk->sb_next = mp;
2209 last_msgchunk = mp;
2210 }
2211 mp->sb_next = NULL;
2212 }
2213 }
2214 else if (finish && last_msgchunk != NULL)
2215 last_msgchunk->sb_eol = TRUE;
2216
2217 *sb_str = s;
2218 *sb_col = 0;
2219}
2220
2221/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002222 * Finished showing messages, clear the scroll-back text on the next message.
2223 */
2224 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002225may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002226{
2227 do_clear_sb_text = TRUE;
2228}
2229
2230/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002231 * Clear any text remembered for scrolling back.
2232 * Called when redrawing the screen.
2233 */
2234 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002235clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002236{
2237 msgchunk_T *mp;
2238
2239 while (last_msgchunk != NULL)
2240 {
2241 mp = last_msgchunk->sb_prev;
2242 vim_free(last_msgchunk);
2243 last_msgchunk = mp;
2244 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002245}
2246
2247/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002248 * "g<" command.
2249 */
2250 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002251show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002252{
2253 msgchunk_T *mp;
2254
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002255 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002256 * weird, typing a command without output results in one line. */
2257 mp = msg_sb_start(last_msgchunk);
2258 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002259 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002260 else
2261 {
2262 do_more_prompt('G');
2263 wait_return(FALSE);
2264 }
2265}
2266
2267/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002268 * Move to the start of screen line in already displayed text.
2269 */
2270 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002271msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002272{
2273 msgchunk_T *mp = mps;
2274
2275 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2276 mp = mp->sb_prev;
2277 return mp;
2278}
2279
2280/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002281 * Mark the last message chunk as finishing the line.
2282 */
2283 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002284msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002285{
2286 if (last_msgchunk != NULL)
2287 last_msgchunk->sb_eol = TRUE;
2288}
2289
2290/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002291 * Display a screen line from previously displayed text at row "row".
2292 * Returns a pointer to the text for the next line (can be NULL).
2293 */
2294 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002295disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002296{
2297 msgchunk_T *mp = smp;
2298 char_u *p;
2299
2300 for (;;)
2301 {
2302 msg_row = row;
2303 msg_col = mp->sb_msg_col;
2304 p = mp->sb_text;
2305 if (*p == '\n') /* don't display the line break */
2306 ++p;
2307 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2308 if (mp->sb_eol || mp->sb_next == NULL)
2309 break;
2310 mp = mp->sb_next;
2311 }
2312 return mp->sb_next;
2313}
2314
2315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 * Output any postponed text for msg_puts_attr_len().
2317 */
2318 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002319t_puts(
2320 int *t_col,
2321 char_u *t_s,
2322 char_u *s,
2323 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324{
2325 /* output postponed text */
2326 msg_didout = TRUE; /* remember that line is not empty */
2327 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002328 msg_col += *t_col;
2329 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#ifdef FEAT_MBYTE
2331 /* If the string starts with a composing character don't increment the
2332 * column position for it. */
2333 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2334 --msg_col;
2335#endif
2336 if (msg_col >= Columns)
2337 {
2338 msg_col = 0;
2339 ++msg_row;
2340 }
2341}
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343/*
2344 * Returns TRUE when messages should be printed with mch_errmsg().
2345 * This is used when there is no valid screen, so we can see error messages.
2346 * If termcap is not active, we may be writing in an alternate console
2347 * window, cursor positioning may not work correctly (window size may be
2348 * different, e.g. for Win32 console) or we just don't know where the
2349 * cursor is.
2350 */
2351 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002352msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
2354 return (!msg_check_screen()
2355#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2356 || !termcap_active
2357#endif
2358 || (swapping_screen() && !termcap_active)
2359 );
2360}
2361
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002362/*
2363 * Print a message when there is no valid screen.
2364 */
2365 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002366msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002367{
2368 char_u *s = str;
2369 char_u buf[4];
2370 char_u *p;
2371
2372#ifdef WIN3264
2373 if (!(silent_mode && p_verbose == 0))
2374 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2375#endif
2376 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2377 {
2378 if (!(silent_mode && p_verbose == 0))
2379 {
2380 /* NL --> CR NL translation (for Unix, not for "--version") */
2381 /* NL --> CR translation (for Mac) */
2382 p = &buf[0];
2383 if (*s == '\n' && !info_message)
2384 *p++ = '\r';
2385#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2386 else
2387#endif
2388 *p++ = *s;
2389 *p = '\0';
2390 if (info_message) /* informative message, not an error */
2391 mch_msg((char *)buf);
2392 else
2393 mch_errmsg((char *)buf);
2394 }
2395
2396 /* primitive way to compute the current column */
2397#ifdef FEAT_RIGHTLEFT
2398 if (cmdmsg_rl)
2399 {
2400 if (*s == '\r' || *s == '\n')
2401 msg_col = Columns - 1;
2402 else
2403 --msg_col;
2404 }
2405 else
2406#endif
2407 {
2408 if (*s == '\r' || *s == '\n')
2409 msg_col = 0;
2410 else
2411 ++msg_col;
2412 }
2413 ++s;
2414 }
2415 msg_didout = TRUE; /* assume that line is not empty */
2416
2417#ifdef WIN3264
2418 if (!(silent_mode && p_verbose == 0))
2419 mch_settmode(TMODE_RAW);
2420#endif
2421}
2422
2423/*
2424 * Show the more-prompt and handle the user response.
2425 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002426 * When at hit-enter prompt "typed_char" is the already typed character,
2427 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002428 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2429 */
2430 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002431do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002432{
2433 int used_typed_char = typed_char;
2434 int oldState = State;
2435 int c;
2436#ifdef FEAT_CON_DIALOG
2437 int retval = FALSE;
2438#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002439 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002440 msgchunk_T *mp_last = NULL;
2441 msgchunk_T *mp;
2442 int i;
2443
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002444 if (typed_char == 'G')
2445 {
2446 /* "g<": Find first line on the last page. */
2447 mp_last = msg_sb_start(last_msgchunk);
2448 for (i = 0; i < Rows - 2 && mp_last != NULL
2449 && mp_last->sb_prev != NULL; ++i)
2450 mp_last = msg_sb_start(mp_last->sb_prev);
2451 }
2452
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002453 State = ASKMORE;
2454#ifdef FEAT_MOUSE
2455 setmouse();
2456#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002457 if (typed_char == NUL)
2458 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002459 for (;;)
2460 {
2461 /*
2462 * Get a typed character directly from the user.
2463 */
2464 if (used_typed_char != NUL)
2465 {
2466 c = used_typed_char; /* was typed at hit-enter prompt */
2467 used_typed_char = NUL;
2468 }
2469 else
2470 c = get_keystroke();
2471
2472#if defined(FEAT_MENU) && defined(FEAT_GUI)
2473 if (c == K_MENU)
2474 {
2475 int idx = get_menu_index(current_menu, ASKMORE);
2476
2477 /* Used a menu. If it starts with CTRL-Y, it must
2478 * be a "Copy" for the clipboard. Otherwise
2479 * assume that we end */
2480 if (idx == MENU_INDEX_INVALID)
2481 continue;
2482 c = *current_menu->strings[idx];
2483 if (c != NUL && current_menu->strings[idx][1] != NUL)
2484 ins_typebuf(current_menu->strings[idx] + 1,
2485 current_menu->noremap[idx], 0, TRUE,
2486 current_menu->silent[idx]);
2487 }
2488#endif
2489
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002490 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002491 switch (c)
2492 {
2493 case BS: /* scroll one line back */
2494 case K_BS:
2495 case 'k':
2496 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002497 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002498 break;
2499
2500 case CAR: /* one extra line */
2501 case NL:
2502 case 'j':
2503 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002504 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505 break;
2506
2507 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002508 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002509 break;
2510
2511 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002512 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002513 break;
2514
2515 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002516 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002517 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002518 break;
2519
2520 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002521 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002522 case K_PAGEDOWN:
2523 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002524 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002525 break;
2526
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002527 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002528 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002529 break;
2530
2531 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002532 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002533 lines_left = 999999;
2534 break;
2535
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002536 case ':': /* start new command line */
2537#ifdef FEAT_CON_DIALOG
2538 if (!confirm_msg_used)
2539#endif
2540 {
2541 /* Since got_int is set all typeahead will be flushed, but we
2542 * want to keep this ':', remember that in a special way. */
2543 typeahead_noflush(':');
2544 cmdline_row = Rows - 1; /* put ':' on this line */
2545 skip_redraw = TRUE; /* skip redraw once */
2546 need_wait_return = FALSE; /* don't wait in main() */
2547 }
2548 /*FALLTHROUGH*/
2549 case 'q': /* quit */
2550 case Ctrl_C:
2551 case ESC:
2552#ifdef FEAT_CON_DIALOG
2553 if (confirm_msg_used)
2554 {
2555 /* Jump to the choices of the dialog. */
2556 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002557 }
2558 else
2559#endif
2560 {
2561 got_int = TRUE;
2562 quit_more = TRUE;
2563 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002564 /* When there is some more output (wrapping line) display that
2565 * without another prompt. */
2566 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002567 break;
2568
2569#ifdef FEAT_CLIPBOARD
2570 case Ctrl_Y:
2571 /* Strange way to allow copying (yanking) a modeless
2572 * selection at the more prompt. Use CTRL-Y,
2573 * because the same is used in Cmdline-mode and at the
2574 * hit-enter prompt. However, scrolling one line up
2575 * might be expected... */
2576 if (clip_star.state == SELECT_DONE)
2577 clip_copy_modeless_selection(TRUE);
2578 continue;
2579#endif
2580 default: /* no valid response */
2581 msg_moremsg(TRUE);
2582 continue;
2583 }
2584
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002585 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002586 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002587 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002588 {
2589 /* go to start of last line */
2590 if (mp_last == NULL)
2591 mp = msg_sb_start(last_msgchunk);
2592 else if (mp_last->sb_prev != NULL)
2593 mp = msg_sb_start(mp_last->sb_prev);
2594 else
2595 mp = NULL;
2596
2597 /* go to start of line at top of the screen */
2598 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2599 ++i)
2600 mp = msg_sb_start(mp->sb_prev);
2601
2602 if (mp != NULL && mp->sb_prev != NULL)
2603 {
2604 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002605 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 {
2607 if (mp == NULL || mp->sb_prev == NULL)
2608 break;
2609 mp = msg_sb_start(mp->sb_prev);
2610 if (mp_last == NULL)
2611 mp_last = msg_sb_start(last_msgchunk);
2612 else
2613 mp_last = msg_sb_start(mp_last->sb_prev);
2614 }
2615
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002616 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002617 (int)Rows, NULL) == OK)
2618 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002619 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002620 (void)disp_sb_line(0, mp);
2621 }
2622 else
2623 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002624 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002625 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002626 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2627 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002628 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002629 ++msg_scrolled;
2630 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002631 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002632 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002633 }
2634 }
2635 else
2636 {
2637 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002638 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002639 {
2640 /* scroll up, display line at bottom */
2641 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002642 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002643 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2644 (int)Columns, ' ', ' ', 0);
2645 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002646 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002647 }
2648 }
2649
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002650 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002651 {
2652 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002653 screen_fill((int)Rows - 1, (int)Rows, 0,
2654 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002655 msg_moremsg(FALSE);
2656 continue;
2657 }
2658
2659 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002660 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002661 }
2662
2663 break;
2664 }
2665
2666 /* clear the --more-- message */
2667 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2668 State = oldState;
2669#ifdef FEAT_MOUSE
2670 setmouse();
2671#endif
2672 if (quit_more)
2673 {
2674 msg_row = Rows - 1;
2675 msg_col = 0;
2676 }
2677#ifdef FEAT_RIGHTLEFT
2678 else if (cmdmsg_rl)
2679 msg_col = Columns - 1;
2680#endif
2681
2682#ifdef FEAT_CON_DIALOG
2683 return retval;
2684#else
2685 return FALSE;
2686#endif
2687}
2688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2690
2691#ifdef mch_errmsg
2692# undef mch_errmsg
2693#endif
2694#ifdef mch_msg
2695# undef mch_msg
2696#endif
2697
2698/*
2699 * Give an error message. To be used when the screen hasn't been initialized
2700 * yet. When stderr can't be used, collect error messages until the GUI has
2701 * started and they can be displayed in a message box.
2702 */
2703 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002704mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
2706 int len;
2707
2708#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2709 /* On Unix use stderr if it's a tty.
2710 * When not going to start the GUI also use stderr.
2711 * On Mac, when started from Finder, stderr is the console. */
2712 if (
2713# ifdef UNIX
2714# ifdef MACOS_X_UNIX
2715 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2716# else
2717 isatty(2)
2718# endif
2719# ifdef FEAT_GUI
2720 ||
2721# endif
2722# endif
2723# ifdef FEAT_GUI
2724 !(gui.in_use || gui.starting)
2725# endif
2726 )
2727 {
2728 fprintf(stderr, "%s", str);
2729 return;
2730 }
2731#endif
2732
2733 /* avoid a delay for a message that isn't there */
2734 emsg_on_display = FALSE;
2735
2736 len = (int)STRLEN(str) + 1;
2737 if (error_ga.ga_growsize == 0)
2738 {
2739 error_ga.ga_growsize = 80;
2740 error_ga.ga_itemsize = 1;
2741 }
2742 if (ga_grow(&error_ga, len) == OK)
2743 {
2744 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2745 (char_u *)str, len);
2746#ifdef UNIX
2747 /* remove CR characters, they are displayed */
2748 {
2749 char_u *p;
2750
2751 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2752 for (;;)
2753 {
2754 p = vim_strchr(p, '\r');
2755 if (p == NULL)
2756 break;
2757 *p = ' ';
2758 }
2759 }
2760#endif
2761 --len; /* don't count the NUL at the end */
2762 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764}
2765
2766/*
2767 * Give a message. To be used when the screen hasn't been initialized yet.
2768 * When there is no tty, collect messages until the GUI has started and they
2769 * can be displayed in a message box.
2770 */
2771 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002772mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
2774#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2775 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2776 * uses mch_errmsg() when started from the desktop.
2777 * When not going to start the GUI also use stdout.
2778 * On Mac, when started from Finder, stderr is the console. */
2779 if (
2780# ifdef UNIX
2781# ifdef MACOS_X_UNIX
2782 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2783# else
2784 isatty(2)
2785# endif
2786# ifdef FEAT_GUI
2787 ||
2788# endif
2789# endif
2790# ifdef FEAT_GUI
2791 !(gui.in_use || gui.starting)
2792# endif
2793 )
2794 {
2795 printf("%s", str);
2796 return;
2797 }
2798# endif
2799 mch_errmsg(str);
2800}
2801#endif /* USE_MCH_ERRMSG */
2802
2803/*
2804 * Put a character on the screen at the current message position and advance
2805 * to the next position. Only for printable ASCII!
2806 */
2807 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002808msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 msg_didout = TRUE; /* remember that line is not empty */
2811 screen_putchar(c, msg_row, msg_col, attr);
2812#ifdef FEAT_RIGHTLEFT
2813 if (cmdmsg_rl)
2814 {
2815 if (--msg_col == 0)
2816 {
2817 msg_col = Columns;
2818 ++msg_row;
2819 }
2820 }
2821 else
2822#endif
2823 {
2824 if (++msg_col >= Columns)
2825 {
2826 msg_col = 0;
2827 ++msg_row;
2828 }
2829 }
2830}
2831
2832 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002833msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002835 int attr;
2836 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002839 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002841 screen_puts((char_u *)
2842 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2843 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844}
2845
2846/*
2847 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2848 * exmode_active.
2849 */
2850 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002851repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 if (State == ASKMORE)
2854 {
2855 msg_moremsg(TRUE); /* display --more-- message again */
2856 msg_row = Rows - 1;
2857 }
2858#ifdef FEAT_CON_DIALOG
2859 else if (State == CONFIRM)
2860 {
2861 display_confirm_msg(); /* display ":confirm" message again */
2862 msg_row = Rows - 1;
2863 }
2864#endif
2865 else if (State == EXTERNCMD)
2866 {
2867 windgoto(msg_row, msg_col); /* put cursor back */
2868 }
2869 else if (State == HITRETURN || State == SETWSIZE)
2870 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002871 if (msg_row == Rows - 1)
2872 {
2873 /* Avoid drawing the "hit-enter" prompt below the previous one,
2874 * overwrite it. Esp. useful when regaining focus and a
2875 * FocusGained autocmd exists but didn't draw anything. */
2876 msg_didout = FALSE;
2877 msg_col = 0;
2878 msg_clr_eos();
2879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 hit_return_msg();
2881 msg_row = Rows - 1;
2882 }
2883}
2884
2885/*
2886 * msg_check_screen - check if the screen is initialized.
2887 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2888 * While starting the GUI the terminal codes will be set for the GUI, but the
2889 * output goes to the terminal. Don't use the terminal codes then.
2890 */
2891 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002892msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894 if (!full_screen || !screen_valid(FALSE))
2895 return FALSE;
2896
2897 if (msg_row >= Rows)
2898 msg_row = Rows - 1;
2899 if (msg_col >= Columns)
2900 msg_col = Columns - 1;
2901 return TRUE;
2902}
2903
2904/*
2905 * Clear from current message position to end of screen.
2906 * Skip this when ":silent" was used, no need to clear for redirection.
2907 */
2908 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002909msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 if (msg_silent == 0)
2912 msg_clr_eos_force();
2913}
2914
2915/*
2916 * Clear from current message position to end of screen.
2917 * Note: msg_col is not updated, so we remember the end of the message
2918 * for msg_check().
2919 */
2920 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002921msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 if (msg_use_printf())
2924 {
2925 if (full_screen) /* only when termcap codes are valid */
2926 {
2927 if (*T_CD)
2928 out_str(T_CD); /* clear to end of display */
2929 else if (*T_CE)
2930 out_str(T_CE); /* clear to end of line */
2931 }
2932 }
2933 else
2934 {
2935#ifdef FEAT_RIGHTLEFT
2936 if (cmdmsg_rl)
2937 {
2938 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2939 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2940 }
2941 else
2942#endif
2943 {
2944 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2945 ' ', ' ', 0);
2946 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2947 }
2948 }
2949}
2950
2951/*
2952 * Clear the command line.
2953 */
2954 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002955msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 msg_row = cmdline_row;
2958 msg_col = 0;
2959 msg_clr_eos_force();
2960}
2961
2962/*
2963 * end putting a message on the screen
2964 * call wait_return if the message does not fit in the available space
2965 * return TRUE if wait_return not called.
2966 */
2967 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002968msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002971 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 * or the ruler option is set and we run into it,
2973 * we have to redraw the window.
2974 * Do not do this if we are abandoning the file or editing the command line.
2975 */
2976 if (!exiting && need_wait_return && !(State & CMDLINE))
2977 {
2978 wait_return(FALSE);
2979 return FALSE;
2980 }
2981 out_flush();
2982 return TRUE;
2983}
2984
2985/*
2986 * If the written message runs into the shown command or ruler, we have to
2987 * wait for hit-return and redraw the window later.
2988 */
2989 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002990msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 if (msg_row == Rows - 1 && msg_col >= sc_col)
2993 {
2994 need_wait_return = TRUE;
2995 redraw_cmdline = TRUE;
2996 }
2997}
2998
2999/*
3000 * May write a string to the redirection file.
3001 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3002 */
3003 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003004redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 char_u *s = str;
3007 static int cur_col = 0;
3008
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003009 /* Don't do anything for displaying prompts and the like. */
3010 if (redir_off)
3011 return;
3012
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003013 /* If 'verbosefile' is set prepare for writing in that file. */
3014 if (*p_vfile != NUL && verbose_fd == NULL)
3015 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003016
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003017 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 {
3019 /* If the string doesn't start with CR or NL, go to msg_col */
3020 if (*s != '\n' && *s != '\r')
3021 {
3022 while (cur_col < msg_col)
3023 {
3024#ifdef FEAT_EVAL
3025 if (redir_reg)
3026 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003027 else if (redir_vname)
3028 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003031 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003033 if (verbose_fd != NULL)
3034 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 ++cur_col;
3036 }
3037 }
3038
3039#ifdef FEAT_EVAL
3040 if (redir_reg)
3041 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003042 if (redir_vname)
3043 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#endif
3045
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003046 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3048 {
3049#ifdef FEAT_EVAL
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003050 if (!redir_reg && !redir_vname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003052 if (redir_fd != NULL)
3053 putc(*s, redir_fd);
3054 if (verbose_fd != NULL)
3055 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 if (*s == '\r' || *s == '\n')
3057 cur_col = 0;
3058 else if (*s == '\t')
3059 cur_col += (8 - cur_col % 8);
3060 else
3061 ++cur_col;
3062 ++s;
3063 }
3064
3065 if (msg_silent != 0) /* should update msg_col */
3066 msg_col = cur_col;
3067 }
3068}
3069
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003070 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003071redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003072{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003073 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003074#ifdef FEAT_EVAL
3075 || redir_reg || redir_vname
3076#endif
3077 ;
3078}
3079
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003081 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003082 * Must always be called paired with verbose_leave()!
3083 */
3084 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003085verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003086{
3087 if (*p_vfile != NUL)
3088 ++msg_silent;
3089}
3090
3091/*
3092 * After giving verbose message.
3093 * Must always be called paired with verbose_enter()!
3094 */
3095 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003096verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003097{
3098 if (*p_vfile != NUL)
3099 if (--msg_silent < 0)
3100 msg_silent = 0;
3101}
3102
3103/*
3104 * Like verbose_enter() and set msg_scroll when displaying the message.
3105 */
3106 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003107verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003108{
3109 if (*p_vfile != NUL)
3110 ++msg_silent;
3111 else
3112 /* always scroll up, don't overwrite */
3113 msg_scroll = TRUE;
3114}
3115
3116/*
3117 * Like verbose_leave() and set cmdline_row when displaying the message.
3118 */
3119 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003120verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003121{
3122 if (*p_vfile != NUL)
3123 {
3124 if (--msg_silent < 0)
3125 msg_silent = 0;
3126 }
3127 else
3128 cmdline_row = msg_row;
3129}
3130
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003131/*
3132 * Called when 'verbosefile' is set: stop writing to the file.
3133 */
3134 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003135verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003136{
3137 if (verbose_fd != NULL)
3138 {
3139 fclose(verbose_fd);
3140 verbose_fd = NULL;
3141 }
3142 verbose_did_open = FALSE;
3143}
3144
3145/*
3146 * Open the file 'verbosefile'.
3147 * Return FAIL or OK.
3148 */
3149 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003150verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003151{
3152 if (verbose_fd == NULL && !verbose_did_open)
3153 {
3154 /* Only give the error message once. */
3155 verbose_did_open = TRUE;
3156
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003157 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003158 if (verbose_fd == NULL)
3159 {
3160 EMSG2(_(e_notopen), p_vfile);
3161 return FAIL;
3162 }
3163 }
3164 return OK;
3165}
3166
3167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 * Give a warning message (for searching).
3169 * Use 'w' highlighting and may repeat the message after redrawing
3170 */
3171 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003172give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 /* Don't do this for ":silent". */
3175 if (msg_silent != 0)
3176 return;
3177
3178 /* Don't want a hit-enter prompt here. */
3179 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#ifdef FEAT_EVAL
3182 set_vim_var_string(VV_WARNINGMSG, message, -1);
3183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 vim_free(keep_msg);
3185 keep_msg = NULL;
3186 if (hl)
3187 keep_msg_attr = hl_attr(HLF_W);
3188 else
3189 keep_msg_attr = 0;
3190 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003191 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 msg_didout = FALSE; /* overwrite this message */
3193 msg_nowait = TRUE; /* don't wait for this message */
3194 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 --no_wait_return;
3197}
3198
3199/*
3200 * Advance msg cursor to column "col".
3201 */
3202 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003203msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204{
3205 if (msg_silent != 0) /* nothing to advance to */
3206 {
3207 msg_col = col; /* for redirection, may fill it up later */
3208 return;
3209 }
3210 if (col >= Columns) /* not enough room */
3211 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003212#ifdef FEAT_RIGHTLEFT
3213 if (cmdmsg_rl)
3214 while (msg_col > Columns - col)
3215 msg_putchar(' ');
3216 else
3217#endif
3218 while (msg_col < col)
3219 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
3222#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3223/*
3224 * Used for "confirm()" function, and the :confirm command prefix.
3225 * Versions which haven't got flexible dialogs yet, and console
3226 * versions, get this generic handler which uses the command line.
3227 *
3228 * type = one of:
3229 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3230 * title = title string (can be NULL for default)
3231 * (neither used in console dialogs at the moment)
3232 *
3233 * Format of the "buttons" string:
3234 * "Button1Name\nButton2Name\nButton3Name"
3235 * The first button should normally be the default/accept
3236 * The second button should be the 'Cancel' button
3237 * Other buttons- use your imagination!
3238 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3239 * different letter.
3240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003242do_dialog(
3243 int type UNUSED,
3244 char_u *title UNUSED,
3245 char_u *message,
3246 char_u *buttons,
3247 int dfltbutton,
3248 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003249 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003250 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003251 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
3253 int oldState;
3254 int retval = 0;
3255 char_u *hotkeys;
3256 int c;
3257 int i;
3258
3259#ifndef NO_CONSOLE
3260 /* Don't output anything in silent mode ("ex -s") */
3261 if (silent_mode)
3262 return dfltbutton; /* return default option */
3263#endif
3264
3265#ifdef FEAT_GUI_DIALOG
3266 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3267 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3268 {
3269 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003270 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003271 /* avoid a hit-enter prompt without clearing the cmdline */
3272 need_wait_return = FALSE;
3273 emsg_on_display = FALSE;
3274 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 /* Flush output to avoid that further messages and redrawing is done
3277 * in the wrong order. */
3278 out_flush();
3279 gui_mch_update();
3280
3281 return c;
3282 }
3283#endif
3284
3285 oldState = State;
3286 State = CONFIRM;
3287#ifdef FEAT_MOUSE
3288 setmouse();
3289#endif
3290
3291 /*
3292 * Since we wait for a keypress, don't make the
3293 * user press RETURN as well afterwards.
3294 */
3295 ++no_wait_return;
3296 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3297
3298 if (hotkeys != NULL)
3299 {
3300 for (;;)
3301 {
3302 /* Get a typed character directly from the user. */
3303 c = get_keystroke();
3304 switch (c)
3305 {
3306 case CAR: /* User accepts default option */
3307 case NL:
3308 retval = dfltbutton;
3309 break;
3310 case Ctrl_C: /* User aborts/cancels */
3311 case ESC:
3312 retval = 0;
3313 break;
3314 default: /* Could be a hotkey? */
3315 if (c < 0) /* special keys are ignored here */
3316 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003317 if (c == ':' && ex_cmd)
3318 {
3319 retval = dfltbutton;
3320 ins_char_typebuf(':');
3321 break;
3322 }
3323
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 /* Make the character lowercase, as chars in "hotkeys" are. */
3325 c = MB_TOLOWER(c);
3326 retval = 1;
3327 for (i = 0; hotkeys[i]; ++i)
3328 {
3329#ifdef FEAT_MBYTE
3330 if (has_mbyte)
3331 {
3332 if ((*mb_ptr2char)(hotkeys + i) == c)
3333 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003334 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336 else
3337#endif
3338 if (hotkeys[i] == c)
3339 break;
3340 ++retval;
3341 }
3342 if (hotkeys[i])
3343 break;
3344 /* No hotkey match, so keep waiting */
3345 continue;
3346 }
3347 break;
3348 }
3349
3350 vim_free(hotkeys);
3351 }
3352
3353 State = oldState;
3354#ifdef FEAT_MOUSE
3355 setmouse();
3356#endif
3357 --no_wait_return;
3358 msg_end_prompt();
3359
3360 return retval;
3361}
3362
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003363static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365/*
3366 * Copy one character from "*from" to "*to", taking care of multi-byte
3367 * characters. Return the length of the character in bytes.
3368 */
3369 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003370copy_char(
3371 char_u *from,
3372 char_u *to,
3373 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375#ifdef FEAT_MBYTE
3376 int len;
3377 int c;
3378
3379 if (has_mbyte)
3380 {
3381 if (lowercase)
3382 {
3383 c = MB_TOLOWER((*mb_ptr2char)(from));
3384 return (*mb_char2bytes)(c, to);
3385 }
3386 else
3387 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003388 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 mch_memmove(to, from, (size_t)len);
3390 return len;
3391 }
3392 }
3393 else
3394#endif
3395 {
3396 if (lowercase)
3397 *to = (char_u)TOLOWER_LOC(*from);
3398 else
3399 *to = *from;
3400 return 1;
3401 }
3402}
3403
3404/*
3405 * Format the dialog string, and display it at the bottom of
3406 * the screen. Return a string of hotkey chars (if defined) for
3407 * each 'button'. If a button has no hotkey defined, the first character of
3408 * the button is used.
3409 * The hotkeys can be multi-byte characters, but without combining chars.
3410 *
3411 * Returns an allocated string with hotkeys, or NULL for error.
3412 */
3413 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003414msg_show_console_dialog(
3415 char_u *message,
3416 char_u *buttons,
3417 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 int len = 0;
3420#ifdef FEAT_MBYTE
3421# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3422#else
3423# define HOTK_LEN 1
3424#endif
3425 int lenhotkey = HOTK_LEN; /* count first button */
3426 char_u *hotk = NULL;
3427 char_u *msgp = NULL;
3428 char_u *hotkp = NULL;
3429 char_u *r;
3430 int copy;
3431#define HAS_HOTKEY_LEN 30
3432 char_u has_hotkey[HAS_HOTKEY_LEN];
3433 int first_hotkey = FALSE; /* first char of button is hotkey */
3434 int idx;
3435
3436 has_hotkey[0] = FALSE;
3437
3438 /*
3439 * First loop: compute the size of memory to allocate.
3440 * Second loop: copy to the allocated memory.
3441 */
3442 for (copy = 0; copy <= 1; ++copy)
3443 {
3444 r = buttons;
3445 idx = 0;
3446 while (*r)
3447 {
3448 if (*r == DLG_BUTTON_SEP)
3449 {
3450 if (copy)
3451 {
3452 *msgp++ = ',';
3453 *msgp++ = ' '; /* '\n' -> ', ' */
3454
3455 /* advance to next hotkey and set default hotkey */
3456#ifdef FEAT_MBYTE
3457 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003458 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else
3460#endif
3461 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003462 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (dfltbutton)
3464 --dfltbutton;
3465
3466 /* If no hotkey is specified first char is used. */
3467 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3468 first_hotkey = TRUE;
3469 }
3470 else
3471 {
3472 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3473 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3474 if (idx < HAS_HOTKEY_LEN - 1)
3475 has_hotkey[++idx] = FALSE;
3476 }
3477 }
3478 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3479 {
3480 if (*r == DLG_HOTKEY_CHAR)
3481 ++r;
3482 first_hotkey = FALSE;
3483 if (copy)
3484 {
3485 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3486 *msgp++ = *r;
3487 else
3488 {
3489 /* '&a' -> '[a]' */
3490 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3491 msgp += copy_char(r, msgp, FALSE);
3492 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3493
3494 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003495 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 }
3498 else
3499 {
3500 ++len; /* '&a' -> '[a]' */
3501 if (idx < HAS_HOTKEY_LEN - 1)
3502 has_hotkey[idx] = TRUE;
3503 }
3504 }
3505 else
3506 {
3507 /* everything else copy literally */
3508 if (copy)
3509 msgp += copy_char(r, msgp, FALSE);
3510 }
3511
3512 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003513 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515
3516 if (copy)
3517 {
3518 *msgp++ = ':';
3519 *msgp++ = ' ';
3520 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 else
3523 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003525 + 2 /* for the NL's */
3526 + STRLEN(buttons)
3527 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 /* If no hotkey is specified first char is used. */
3531 if (!has_hotkey[0])
3532 {
3533 first_hotkey = TRUE;
3534 len += 2; /* "x" -> "[x]" */
3535 }
3536
3537 /*
3538 * Now allocate and load the strings
3539 */
3540 vim_free(confirm_msg);
3541 confirm_msg = alloc(len);
3542 if (confirm_msg == NULL)
3543 return NULL;
3544 *confirm_msg = NUL;
3545 hotk = alloc(lenhotkey);
3546 if (hotk == NULL)
3547 return NULL;
3548
3549 *confirm_msg = '\n';
3550 STRCPY(confirm_msg + 1, message);
3551
3552 msgp = confirm_msg + 1 + STRLEN(message);
3553 hotkp = hotk;
3554
Bram Moolenaar6a516062007-07-05 08:11:42 +00003555 /* Define first default hotkey. Keep the hotkey string NUL
3556 * terminated to avoid reading past the end. */
3557 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 /* Remember where the choices start, displaying starts here when
3560 * "hotkp" typed at the more prompt. */
3561 confirm_msg_tail = msgp;
3562 *msgp++ = '\n';
3563 }
3564 }
3565
3566 display_confirm_msg();
3567 return hotk;
3568}
3569
3570/*
3571 * Display the ":confirm" message. Also called when screen resized.
3572 */
3573 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003574display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 /* avoid that 'q' at the more prompt truncates the message here */
3577 ++confirm_msg_used;
3578 if (confirm_msg != NULL)
3579 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3580 --confirm_msg_used;
3581}
3582
3583#endif /* FEAT_CON_DIALOG */
3584
3585#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3586
3587 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003588vim_dialog_yesno(
3589 int type,
3590 char_u *title,
3591 char_u *message,
3592 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593{
3594 if (do_dialog(type,
3595 title == NULL ? (char_u *)_("Question") : title,
3596 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003597 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 return VIM_YES;
3599 return VIM_NO;
3600}
3601
3602 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003603vim_dialog_yesnocancel(
3604 int type,
3605 char_u *title,
3606 char_u *message,
3607 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 switch (do_dialog(type,
3610 title == NULL ? (char_u *)_("Question") : title,
3611 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003612 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 case 1: return VIM_YES;
3615 case 2: return VIM_NO;
3616 }
3617 return VIM_CANCEL;
3618}
3619
3620 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003621vim_dialog_yesnoallcancel(
3622 int type,
3623 char_u *title,
3624 char_u *message,
3625 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 switch (do_dialog(type,
3628 title == NULL ? (char_u *)"Question" : title,
3629 message,
3630 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003631 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 case 1: return VIM_YES;
3634 case 2: return VIM_NO;
3635 case 3: return VIM_ALL;
3636 case 4: return VIM_DISCARDALL;
3637 }
3638 return VIM_CANCEL;
3639}
3640
3641#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3642
3643#if defined(FEAT_BROWSE) || defined(PROTO)
3644/*
3645 * Generic browse function. Calls gui_mch_browse() when possible.
3646 * Later this may pop-up a non-GUI file selector (external command?).
3647 */
3648 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003649do_browse(
3650 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3651 char_u *title, /* title for the window */
3652 char_u *dflt, /* default file name (may include directory) */
3653 char_u *ext, /* extension added */
3654 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003656 char_u *filter, /* file name filter */
3657 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 char_u *fname;
3660 static char_u *last_dir = NULL; /* last used directory */
3661 char_u *tofree = NULL;
3662 int save_browse = cmdmod.browse;
3663
3664 /* Must turn off browse to avoid that autocommands will get the
3665 * flag too! */
3666 cmdmod.browse = FALSE;
3667
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003668 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003670 if (flags & BROWSE_DIR)
3671 title = (char_u *)_("Select Directory dialog");
3672 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 title = (char_u *)_("Save File dialog");
3674 else
3675 title = (char_u *)_("Open File dialog");
3676 }
3677
3678 /* When no directory specified, use default file name, default dir, buffer
3679 * dir, last dir or current dir */
3680 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3681 {
3682 if (mch_isdir(dflt)) /* default file name is a directory */
3683 {
3684 initdir = dflt;
3685 dflt = NULL;
3686 }
3687 else if (gettail(dflt) != dflt) /* default file name includes a path */
3688 {
3689 tofree = vim_strsave(dflt);
3690 if (tofree != NULL)
3691 {
3692 initdir = tofree;
3693 *gettail(initdir) = NUL;
3694 dflt = gettail(dflt);
3695 }
3696 }
3697 }
3698
3699 if (initdir == NULL || *initdir == NUL)
3700 {
3701 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003702 if (STRCMP(p_bsdir, "last") != 0
3703 && STRCMP(p_bsdir, "buffer") != 0
3704 && STRCMP(p_bsdir, "current") != 0
3705 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 initdir = p_bsdir;
3707 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003708 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 && buf != NULL && buf->b_ffname != NULL)
3710 {
3711 if (dflt == NULL || *dflt == NUL)
3712 dflt = gettail(curbuf->b_ffname);
3713 tofree = vim_strsave(curbuf->b_ffname);
3714 if (tofree != NULL)
3715 {
3716 initdir = tofree;
3717 *gettail(initdir) = NUL;
3718 }
3719 }
3720 /* When 'browsedir' is "last", use dir from last browse */
3721 else if (*p_bsdir == 'l')
3722 initdir = last_dir;
3723 /* When 'browsedir is "current", use current directory. This is the
3724 * default already, leave initdir empty. */
3725 }
3726
3727# ifdef FEAT_GUI
3728 if (gui.in_use) /* when this changes, also adjust f_has()! */
3729 {
3730 if (filter == NULL
3731# ifdef FEAT_EVAL
3732 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3733 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3734# endif
3735 )
3736 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003737 if (flags & BROWSE_DIR)
3738 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003739# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003740 /* For systems that have a directory dialog. */
3741 fname = gui_mch_browsedir(title, initdir);
3742# else
3743 /* Generic solution for selecting a directory: select a file and
3744 * remove the file name. */
3745 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3746# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003747# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003748 /* Win32 adds a dummy file name, others return an arbitrary file
3749 * name. GTK+ 2 returns only the directory, */
3750 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3751 {
3752 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003753 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003754
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003755 if (tail == fname)
3756 *tail++ = '.'; /* use current dir */
3757 *tail = NUL;
3758 }
3759# endif
3760 }
3761 else
3762 fname = gui_mch_browse(flags & BROWSE_SAVE,
3763 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /* We hang around in the dialog for a while, the user might do some
3766 * things to our files. The Win32 dialog allows deleting or renaming
3767 * a file, check timestamps. */
3768 need_check_timestamps = TRUE;
3769 did_check_timestamps = FALSE;
3770 }
3771 else
3772# endif
3773 {
3774 /* TODO: non-GUI file selector here */
3775 EMSG(_("E338: Sorry, no file browser in console mode"));
3776 fname = NULL;
3777 }
3778
3779 /* keep the directory for next time */
3780 if (fname != NULL)
3781 {
3782 vim_free(last_dir);
3783 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003784 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
3786 *gettail(last_dir) = NUL;
3787 if (*last_dir == NUL)
3788 {
3789 /* filename only returned, must be in current dir */
3790 vim_free(last_dir);
3791 last_dir = alloc(MAXPATHL);
3792 if (last_dir != NULL)
3793 mch_dirname(last_dir, MAXPATHL);
3794 }
3795 }
3796 }
3797
3798 vim_free(tofree);
3799 cmdmod.browse = save_browse;
3800
3801 return fname;
3802}
3803#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003804
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003805#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003806static char *e_printf = N_("E766: Insufficient arguments for printf()");
3807
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003808static long tv_nr(typval_T *tvs, int *idxp);
3809static char *tv_str(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003810# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003811static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003812# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813
3814/*
3815 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3816 */
3817 static long
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003818tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003819{
3820 int idx = *idxp - 1;
3821 long n = 0;
3822 int err = FALSE;
3823
3824 if (tvs[idx].v_type == VAR_UNKNOWN)
3825 EMSG(_(e_printf));
3826 else
3827 {
3828 ++*idxp;
3829 n = get_tv_number_chk(&tvs[idx], &err);
3830 if (err)
3831 n = 0;
3832 }
3833 return n;
3834}
3835
3836/*
3837 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003838 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 */
3840 static char *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003841tv_str(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842{
3843 int idx = *idxp - 1;
3844 char *s = NULL;
3845
3846 if (tvs[idx].v_type == VAR_UNKNOWN)
3847 EMSG(_(e_printf));
3848 else
3849 {
3850 ++*idxp;
3851 s = (char *)get_tv_string_chk(&tvs[idx]);
3852 }
3853 return s;
3854}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003855
3856# ifdef FEAT_FLOAT
3857/*
3858 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3859 */
3860 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003861tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003862{
3863 int idx = *idxp - 1;
3864 double f = 0;
3865
3866 if (tvs[idx].v_type == VAR_UNKNOWN)
3867 EMSG(_(e_printf));
3868 else
3869 {
3870 ++*idxp;
3871 if (tvs[idx].v_type == VAR_FLOAT)
3872 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003873 else if (tvs[idx].v_type == VAR_NUMBER)
3874 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003875 else
3876 EMSG(_("E807: Expected Float argument for printf()"));
3877 }
3878 return f;
3879}
3880# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003881#endif
3882
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003883/*
3884 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003885 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003886 * consistency.
3887 *
3888 * This code is based on snprintf.c - a portable implementation of snprintf
3889 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003890 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003891 * The original code, including useful comments, can be found here:
3892 * http://www.ijs.si/software/snprintf/
3893 *
3894 * This snprintf() only supports the following conversion specifiers:
3895 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3896 * with flags: '-', '+', ' ', '0' and '#'.
3897 * An asterisk is supported for field width as well as precision.
3898 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003899 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3900 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003901 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3902 * 'll' (long long int) is not supported.
3903 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003904 * The locale is not used, the string is used as a byte string. This is only
3905 * relevant for double-byte encodings where the second byte may be '%'.
3906 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003907 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3908 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003909 *
3910 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003911 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003912 * is greater or equal to "str_m", not all characters from the result
3913 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3914 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003915 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003916 */
3917
3918/*
3919 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 *
3921 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003922 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003923 */
3924
3925/* When generating prototypes all of this is skipped, cproto doesn't
3926 * understand this. */
3927#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02003928
Bram Moolenaara800b422010-06-27 01:15:55 +02003929/* Like vim_vsnprintf() but append to the string. */
3930 int
3931vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
3932{
3933 va_list ap;
3934 int str_l;
3935 size_t len = STRLEN(str);
3936 size_t space;
3937
3938 if (str_m <= len)
3939 space = 0;
3940 else
3941 space = str_m - len;
3942 va_start(ap, fmt);
3943 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
3944 va_end(ap);
3945 return str_l;
3946}
Bram Moolenaara800b422010-06-27 01:15:55 +02003947
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003948 int
3949vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3950{
3951 va_list ap;
3952 int str_l;
3953
3954 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003955 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003956 va_end(ap);
3957 return str_l;
3958}
3959
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003960 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003961vim_vsnprintf(
3962 char *str,
3963 size_t str_m,
3964 char *fmt,
3965 va_list ap,
3966 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003967{
3968 size_t str_l = 0;
3969 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003970 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003971
3972 if (p == NULL)
3973 p = "";
3974 while (*p != NUL)
3975 {
3976 if (*p != '%')
3977 {
3978 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003979 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003980
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003981 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003982 if (str_l < str_m)
3983 {
3984 size_t avail = str_m - str_l;
3985
3986 mch_memmove(str + str_l, p, n > avail ? avail : n);
3987 }
3988 p += n;
3989 str_l += n;
3990 }
3991 else
3992 {
3993 size_t min_field_width = 0, precision = 0;
3994 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3995 int alternate_form = 0, force_sign = 0;
3996
3997 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3998 * ignored. */
3999 int space_for_positive = 1;
4000
4001 /* allowed values: \0, h, l, L */
4002 char length_modifier = '\0';
4003
4004 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004005# ifdef FEAT_FLOAT
4006# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004007 * That sounds reasonable to use as the maximum
4008 * printable. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004009# else
4010# define TMP_LEN 32
4011# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004012 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004013
4014 /* string address in case of string argument */
4015 char *str_arg;
4016
4017 /* natural field width of arg without padding and sign */
4018 size_t str_arg_l;
4019
4020 /* unsigned char argument value - only defined for c conversion.
4021 * N.B. standard explicitly states the char argument for the c
4022 * conversion is unsigned */
4023 unsigned char uchar_arg;
4024
4025 /* number of zeros to be inserted for numeric conversions as
4026 * required by the precision or minimal field width */
4027 size_t number_of_zeros_to_pad = 0;
4028
4029 /* index into tmp where zero padding is to be inserted */
4030 size_t zero_padding_insertion_ind = 0;
4031
4032 /* current conversion specifier character */
4033 char fmt_spec = '\0';
4034
4035 str_arg = NULL;
4036 p++; /* skip '%' */
4037
4038 /* parse flags */
4039 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4040 || *p == '#' || *p == '\'')
4041 {
4042 switch (*p)
4043 {
4044 case '0': zero_padding = 1; break;
4045 case '-': justify_left = 1; break;
4046 case '+': force_sign = 1; space_for_positive = 0; break;
4047 case ' ': force_sign = 1;
4048 /* If both the ' ' and '+' flags appear, the ' '
4049 * flag should be ignored */
4050 break;
4051 case '#': alternate_form = 1; break;
4052 case '\'': break;
4053 }
4054 p++;
4055 }
4056 /* If the '0' and '-' flags both appear, the '0' flag should be
4057 * ignored. */
4058
4059 /* parse field width */
4060 if (*p == '*')
4061 {
4062 int j;
4063
4064 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004066# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004067 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004068# endif
4069 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004070 if (j >= 0)
4071 min_field_width = j;
4072 else
4073 {
4074 min_field_width = -j;
4075 justify_left = 1;
4076 }
4077 }
4078 else if (VIM_ISDIGIT((int)(*p)))
4079 {
4080 /* size_t could be wider than unsigned int; make sure we treat
4081 * argument like common implementations do */
4082 unsigned int uj = *p++ - '0';
4083
4084 while (VIM_ISDIGIT((int)(*p)))
4085 uj = 10 * uj + (unsigned int)(*p++ - '0');
4086 min_field_width = uj;
4087 }
4088
4089 /* parse precision */
4090 if (*p == '.')
4091 {
4092 p++;
4093 precision_specified = 1;
4094 if (*p == '*')
4095 {
4096 int j;
4097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004100 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004101# endif
4102 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004103 p++;
4104 if (j >= 0)
4105 precision = j;
4106 else
4107 {
4108 precision_specified = 0;
4109 precision = 0;
4110 }
4111 }
4112 else if (VIM_ISDIGIT((int)(*p)))
4113 {
4114 /* size_t could be wider than unsigned int; make sure we
4115 * treat argument like common implementations do */
4116 unsigned int uj = *p++ - '0';
4117
4118 while (VIM_ISDIGIT((int)(*p)))
4119 uj = 10 * uj + (unsigned int)(*p++ - '0');
4120 precision = uj;
4121 }
4122 }
4123
4124 /* parse 'h', 'l' and 'll' length modifiers */
4125 if (*p == 'h' || *p == 'l')
4126 {
4127 length_modifier = *p;
4128 p++;
4129 if (length_modifier == 'l' && *p == 'l')
4130 {
4131 /* double l = long long */
4132 length_modifier = 'l'; /* treat it as a single 'l' */
4133 p++;
4134 }
4135 }
4136 fmt_spec = *p;
4137
4138 /* common synonyms: */
4139 switch (fmt_spec)
4140 {
4141 case 'i': fmt_spec = 'd'; break;
4142 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4143 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4144 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004145 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004146 default: break;
4147 }
4148
4149 /* get parameter value, do initial processing */
4150 switch (fmt_spec)
4151 {
4152 /* '%' and 'c' behave similar to 's' regarding flags and field
4153 * widths */
4154 case '%':
4155 case 'c':
4156 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004157 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004158 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004159 str_arg_l = 1;
4160 switch (fmt_spec)
4161 {
4162 case '%':
4163 str_arg = p;
4164 break;
4165
4166 case 'c':
4167 {
4168 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004169
4170 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004172 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004173# endif
4174 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004175 /* standard demands unsigned char */
4176 uchar_arg = (unsigned char)j;
4177 str_arg = (char *)&uchar_arg;
4178 break;
4179 }
4180
4181 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004182 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004185 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004186# endif
4187 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004188 if (str_arg == NULL)
4189 {
4190 str_arg = "[NULL]";
4191 str_arg_l = 6;
4192 }
4193 /* make sure not to address string beyond the specified
4194 * precision !!! */
4195 else if (!precision_specified)
4196 str_arg_l = strlen(str_arg);
4197 /* truncate string if necessary as requested by precision */
4198 else if (precision == 0)
4199 str_arg_l = 0;
4200 else
4201 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004202 /* Don't put the #if inside memchr(), it can be a
4203 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004204# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004205 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004206# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207 /* memchr on HP does not like n > 2^31 !!! */
4208 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004209 precision <= (size_t)0x7fffffffL ? precision
4210 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004211# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004212 str_arg_l = (q == NULL) ? precision
4213 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004214 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004215# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004216 if (fmt_spec == 'S')
4217 {
4218 if (min_field_width != 0)
4219 min_field_width += STRLEN(str_arg)
4220 - mb_string2cells((char_u *)str_arg, -1);
4221 if (precision)
4222 {
4223 char_u *p1 = (char_u *)str_arg;
4224 size_t i;
4225
4226 for (i = 0; i < precision && *p1; i++)
4227 p1 += mb_ptr2len(p1);
4228
4229 str_arg_l = precision = p1 - (char_u *)str_arg;
4230 }
4231 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004232# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004233 break;
4234
4235 default:
4236 break;
4237 }
4238 break;
4239
4240 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4241 {
4242 /* NOTE: the u, o, x, X and p conversion specifiers
4243 * imply the value is unsigned; d implies a signed
4244 * value */
4245
4246 /* 0 if numeric argument is zero (or if pointer is
4247 * NULL for 'p'), +1 if greater than zero (or nonzero
4248 * for unsigned arguments), -1 if negative (unsigned
4249 * argument is never negative) */
4250 int arg_sign = 0;
4251
4252 /* only defined for length modifier h, or for no
4253 * length modifiers */
4254 int int_arg = 0;
4255 unsigned int uint_arg = 0;
4256
4257 /* only defined for length modifier l */
4258 long int long_arg = 0;
4259 unsigned long int ulong_arg = 0;
4260
4261 /* pointer argument value -only defined for p
4262 * conversion */
4263 void *ptr_arg = NULL;
4264
4265 if (fmt_spec == 'p')
4266 {
4267 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004269# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004270 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271# endif
4272 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004273 if (ptr_arg != NULL)
4274 arg_sign = 1;
4275 }
4276 else if (fmt_spec == 'd')
4277 {
4278 /* signed */
4279 switch (length_modifier)
4280 {
4281 case '\0':
4282 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 /* char and short arguments are passed as int. */
4284 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004285# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004286 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287# endif
4288 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004289 if (int_arg > 0)
4290 arg_sign = 1;
4291 else if (int_arg < 0)
4292 arg_sign = -1;
4293 break;
4294 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004295 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004297 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004298# endif
4299 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004300 if (long_arg > 0)
4301 arg_sign = 1;
4302 else if (long_arg < 0)
4303 arg_sign = -1;
4304 break;
4305 }
4306 }
4307 else
4308 {
4309 /* unsigned */
4310 switch (length_modifier)
4311 {
4312 case '\0':
4313 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004316 tvs != NULL ? (unsigned)
4317 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318# endif
4319 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004320 if (uint_arg != 0)
4321 arg_sign = 1;
4322 break;
4323 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004324 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004325# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004326 tvs != NULL ? (unsigned long)
4327 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328# endif
4329 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004330 if (ulong_arg != 0)
4331 arg_sign = 1;
4332 break;
4333 }
4334 }
4335
4336 str_arg = tmp;
4337 str_arg_l = 0;
4338
4339 /* NOTE:
4340 * For d, i, u, o, x, and X conversions, if precision is
4341 * specified, the '0' flag should be ignored. This is so
4342 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4343 * FreeBSD, NetBSD; but not with Perl.
4344 */
4345 if (precision_specified)
4346 zero_padding = 0;
4347 if (fmt_spec == 'd')
4348 {
4349 if (force_sign && arg_sign >= 0)
4350 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4351 /* leave negative numbers for sprintf to handle, to
4352 * avoid handling tricky cases like (short int)-32768 */
4353 }
4354 else if (alternate_form)
4355 {
4356 if (arg_sign != 0
4357 && (fmt_spec == 'x' || fmt_spec == 'X') )
4358 {
4359 tmp[str_arg_l++] = '0';
4360 tmp[str_arg_l++] = fmt_spec;
4361 }
4362 /* alternate form should have no effect for p
4363 * conversion, but ... */
4364 }
4365
4366 zero_padding_insertion_ind = str_arg_l;
4367 if (!precision_specified)
4368 precision = 1; /* default precision is 1 */
4369 if (precision == 0 && arg_sign == 0)
4370 {
4371 /* When zero value is formatted with an explicit
4372 * precision 0, the resulting formatted string is
4373 * empty (d, i, u, o, x, X, p). */
4374 }
4375 else
4376 {
4377 char f[5];
4378 int f_l = 0;
4379
4380 /* construct a simple format string for sprintf */
4381 f[f_l++] = '%';
4382 if (!length_modifier)
4383 ;
4384 else if (length_modifier == '2')
4385 {
4386 f[f_l++] = 'l';
4387 f[f_l++] = 'l';
4388 }
4389 else
4390 f[f_l++] = length_modifier;
4391 f[f_l++] = fmt_spec;
4392 f[f_l++] = '\0';
4393
4394 if (fmt_spec == 'p')
4395 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4396 else if (fmt_spec == 'd')
4397 {
4398 /* signed */
4399 switch (length_modifier)
4400 {
4401 case '\0':
4402 case 'h': str_arg_l += sprintf(
4403 tmp + str_arg_l, f, int_arg);
4404 break;
4405 case 'l': str_arg_l += sprintf(
4406 tmp + str_arg_l, f, long_arg);
4407 break;
4408 }
4409 }
4410 else
4411 {
4412 /* unsigned */
4413 switch (length_modifier)
4414 {
4415 case '\0':
4416 case 'h': str_arg_l += sprintf(
4417 tmp + str_arg_l, f, uint_arg);
4418 break;
4419 case 'l': str_arg_l += sprintf(
4420 tmp + str_arg_l, f, ulong_arg);
4421 break;
4422 }
4423 }
4424
4425 /* include the optional minus sign and possible
4426 * "0x" in the region before the zero padding
4427 * insertion point */
4428 if (zero_padding_insertion_ind < str_arg_l
4429 && tmp[zero_padding_insertion_ind] == '-')
4430 zero_padding_insertion_ind++;
4431 if (zero_padding_insertion_ind + 1 < str_arg_l
4432 && tmp[zero_padding_insertion_ind] == '0'
4433 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4434 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4435 zero_padding_insertion_ind += 2;
4436 }
4437
4438 {
4439 size_t num_of_digits = str_arg_l
4440 - zero_padding_insertion_ind;
4441
4442 if (alternate_form && fmt_spec == 'o'
4443 /* unless zero is already the first
4444 * character */
4445 && !(zero_padding_insertion_ind < str_arg_l
4446 && tmp[zero_padding_insertion_ind] == '0'))
4447 {
4448 /* assure leading zero for alternate-form
4449 * octal numbers */
4450 if (!precision_specified
4451 || precision < num_of_digits + 1)
4452 {
4453 /* precision is increased to force the
4454 * first character to be zero, except if a
4455 * zero value is formatted with an
4456 * explicit precision of zero */
4457 precision = num_of_digits + 1;
4458 precision_specified = 1;
4459 }
4460 }
4461 /* zero padding to specified precision? */
4462 if (num_of_digits < precision)
4463 number_of_zeros_to_pad = precision - num_of_digits;
4464 }
4465 /* zero padding to specified minimal field width? */
4466 if (!justify_left && zero_padding)
4467 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004468 int n = (int)(min_field_width - (str_arg_l
4469 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004470 if (n > 0)
4471 number_of_zeros_to_pad += n;
4472 }
4473 break;
4474 }
4475
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004476# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004477 case 'f':
4478 case 'e':
4479 case 'E':
4480 case 'g':
4481 case 'G':
4482 {
4483 /* Floating point. */
4484 double f;
4485 double abs_f;
4486 char format[40];
4487 int l;
4488 int remove_trailing_zeroes = FALSE;
4489
4490 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004491# if defined(FEAT_EVAL)
4492 tvs != NULL ? tv_float(tvs, &arg_idx) :
4493# endif
4494 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004495 abs_f = f < 0 ? -f : f;
4496
4497 if (fmt_spec == 'g' || fmt_spec == 'G')
4498 {
4499 /* Would be nice to use %g directly, but it prints
4500 * "1.0" as "1", we don't want that. */
4501 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4502 || abs_f == 0.0)
4503 fmt_spec = 'f';
4504 else
4505 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4506 remove_trailing_zeroes = TRUE;
4507 }
4508
Bram Moolenaarc9372132009-01-13 15:38:37 +00004509 if (fmt_spec == 'f' &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004510# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004511 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004512# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004513 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004514# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004515 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004516 {
4517 /* Avoid a buffer overflow */
4518 strcpy(tmp, "inf");
4519 str_arg_l = 3;
4520 }
4521 else
4522 {
4523 format[0] = '%';
4524 l = 1;
4525 if (precision_specified)
4526 {
4527 size_t max_prec = TMP_LEN - 10;
4528
4529 /* Make sure we don't get more digits than we
4530 * have room for. */
4531 if (fmt_spec == 'f' && abs_f > 1.0)
4532 max_prec -= (size_t)log10(abs_f);
4533 if (precision > max_prec)
4534 precision = max_prec;
4535 l += sprintf(format + 1, ".%d", (int)precision);
4536 }
4537 format[l] = fmt_spec;
4538 format[l + 1] = NUL;
4539 str_arg_l = sprintf(tmp, format, f);
4540
4541 if (remove_trailing_zeroes)
4542 {
4543 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004544 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004545
4546 /* Using %g or %G: remove superfluous zeroes. */
4547 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004548 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004549 else
4550 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004551 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004552 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004553 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004554 {
4555 /* Remove superfluous '+' and leading
4556 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004557 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004558 {
4559 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004560 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004561 --str_arg_l;
4562 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004563 i = (tp[1] == '-') ? 2 : 1;
4564 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004565 {
4566 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004567 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004568 --str_arg_l;
4569 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004570 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004571 }
4572 }
4573
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004574 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004575 /* Remove trailing zeroes, but keep the one
4576 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004577 while (tp > tmp + 2 && *tp == '0'
4578 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004579 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004580 STRMOVE(tp, tp + 1);
4581 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004582 --str_arg_l;
4583 }
4584 }
4585 else
4586 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004587 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004588
4589 /* Be consistent: some printf("%e") use 1.0e+12
4590 * and some 1.0e+012. Remove one zero in the last
4591 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004592 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004593 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004594 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4595 && tp[2] == '0'
4596 && vim_isdigit(tp[3])
4597 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004598 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004599 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004600 --str_arg_l;
4601 }
4602 }
4603 }
4604 str_arg = tmp;
4605 break;
4606 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004607# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004608
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004609 default:
4610 /* unrecognized conversion specifier, keep format string
4611 * as-is */
4612 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004613 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004614 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004615 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004616
4617 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004618 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004619 str_arg = p;
4620 str_arg_l = 0;
4621 if (*p != NUL)
4622 str_arg_l++; /* include invalid conversion specifier
4623 unchanged if not at end-of-string */
4624 break;
4625 }
4626
4627 if (*p != NUL)
4628 p++; /* step over the just processed conversion specifier */
4629
4630 /* insert padding to the left as requested by min_field_width;
4631 * this does not include the zero padding in case of numerical
4632 * conversions*/
4633 if (!justify_left)
4634 {
4635 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004636 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004637
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004638 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004639 {
4640 if (str_l < str_m)
4641 {
4642 size_t avail = str_m - str_l;
4643
4644 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004645 (size_t)pn > avail ? avail
4646 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004647 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004648 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004649 }
4650 }
4651
4652 /* zero padding as requested by the precision or by the minimal
4653 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004654 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004655 {
4656 /* will not copy first part of numeric right now, *
4657 * force it to be copied later in its entirety */
4658 zero_padding_insertion_ind = 0;
4659 }
4660 else
4661 {
4662 /* insert first part of numerics (sign or '0x') before zero
4663 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004664 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004665
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004666 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004667 {
4668 if (str_l < str_m)
4669 {
4670 size_t avail = str_m - str_l;
4671
4672 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004673 (size_t)zn > avail ? avail
4674 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004675 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004676 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004677 }
4678
4679 /* insert zero padding as requested by the precision or min
4680 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004681 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004682 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004683 {
4684 if (str_l < str_m)
4685 {
4686 size_t avail = str_m-str_l;
4687
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004688 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004689 (size_t)zn > avail ? avail
4690 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004691 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004692 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004693 }
4694 }
4695
4696 /* insert formatted string
4697 * (or as-is conversion specifier for unknown conversions) */
4698 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004699 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004700
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004701 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004702 {
4703 if (str_l < str_m)
4704 {
4705 size_t avail = str_m - str_l;
4706
4707 mch_memmove(str + str_l,
4708 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004709 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004710 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004711 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004712 }
4713 }
4714
4715 /* insert right padding */
4716 if (justify_left)
4717 {
4718 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004719 int pn = (int)(min_field_width
4720 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004721
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004722 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004723 {
4724 if (str_l < str_m)
4725 {
4726 size_t avail = str_m - str_l;
4727
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004728 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004729 (size_t)pn > avail ? avail
4730 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004731 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004732 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004733 }
4734 }
4735 }
4736 }
4737
4738 if (str_m > 0)
4739 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004740 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004741 * overwriting the last character (shouldn't happen, but just in case)
4742 * */
4743 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4744 }
4745
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004746 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004747 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004748
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004749 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004750 * character), that is, the number of characters that would have been
4751 * written to the buffer if it were large enough. */
4752 return (int)str_l;
4753}
4754
4755#endif /* PROTO */