blob: fe72e43a6730566aedd2a0123c466a9ef421229e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
14#define MESSAGE_FILE /* don't include prototype for smsg() */
15
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static int other_sourcing_name(void);
19static char_u *get_emsg_source(void);
20static char_u *get_emsg_lnum(void);
21static void add_msg_hist(char_u *s, int len, int attr);
22static void hit_return_msg(void);
23static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010025static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010027static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
28static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
29static void msg_scroll_up(void);
30static void inc_msg_scrolled(void);
31static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
32static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
33static void msg_puts_printf(char_u *str, int maxlen);
34static int do_more_prompt(int typed_char);
35static void msg_screen_putchar(int c, int attr);
36static int msg_check_screen(void);
37static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static int confirm_msg_used = FALSE; /* displaying confirm_msg */
41static char_u *confirm_msg = NULL; /* ":confirm" message */
42static char_u *confirm_msg_tail; /* tail of confirm_msg */
43#endif
44
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020056static FILE *verbose_fd = NULL;
57static int verbose_did_open = FALSE;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059/*
60 * When writing messages to the screen, there are many different situations.
61 * A number of variables is used to remember the current state:
62 * msg_didany TRUE when messages were written since the last time the
63 * user reacted to a prompt.
64 * Reset: After hitting a key for the hit-return prompt,
65 * hitting <CR> for the command line or input().
66 * Set: When any message is written to the screen.
67 * msg_didout TRUE when something was written to the current line.
68 * Reset: When advancing to the next line, when the current
69 * text can be overwritten.
70 * Set: When any message is written to the screen.
71 * msg_nowait No extra delay for the last drawn message.
72 * Used in normal_cmd() before the mode message is drawn.
73 * emsg_on_display There was an error message recently. Indicates that there
74 * should be a delay before redrawing.
75 * msg_scroll The next message should not overwrite the current one.
76 * msg_scrolled How many lines the screen has been scrolled (because of
77 * messages). Used in update_screen() to scroll the screen
78 * back. Incremented each time the screen scrolls a line.
79 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
80 * writes something without scrolling should not make
81 * need_wait_return to be set. This is a hack to make ":ts"
82 * work without an extra prompt.
83 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010084 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 * need_wait_return TRUE when the hit-return prompt is needed.
86 * Reset: After giving the hit-return prompt, when the user
87 * has answered some other prompt.
88 * Set: When the ruler or typeahead display is overwritten,
89 * scrolling the screen for some message.
90 * keep_msg Message to be displayed after redrawing the screen, in
91 * main_loop().
92 * This is an allocated string or NULL when not used.
93 */
94
95/*
96 * msg(s) - displays the string 's' on the status line
97 * When terminal not initialized (yet) mch_errmsg(..) is used.
98 * return TRUE if wait_return not called
99 */
100 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100101msg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 return msg_attr_keep(s, 0, FALSE);
104}
105
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000107 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000108/*
109 * Like msg() but keep it silent when 'verbosefile' is set.
110 */
111 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100112verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000113{
114 int n;
115
116 verbose_enter();
117 n = msg_attr_keep(s, 0, FALSE);
118 verbose_leave();
119
120 return n;
121}
122#endif
123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100125msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126{
127 return msg_attr_keep(s, attr, FALSE);
128}
129
130 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100131msg_attr_keep(
132 char_u *s,
133 int attr,
134 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135{
136 static int entered = 0;
137 int retval;
138 char_u *buf = NULL;
139
140#ifdef FEAT_EVAL
141 if (attr == 0)
142 set_vim_var_string(VV_STATUSMSG, s, -1);
143#endif
144
145 /*
146 * It is possible that displaying a messages causes a problem (e.g.,
147 * when redrawing the window), which causes another message, etc.. To
148 * break this loop, limit the recursiveness to 3 levels.
149 */
150 if (entered >= 3)
151 return TRUE;
152 ++entered;
153
154 /* Add message to history (unless it's a repeated kept message or a
155 * truncated message) */
156 if (s != keep_msg
157 || (*s != '<'
158 && last_msg_hist != NULL
159 && last_msg_hist->msg != NULL
160 && STRCMP(s, last_msg_hist->msg)))
161 add_msg_hist(s, -1, attr);
162
163 /* When displaying keep_msg, don't let msg_start() free it, caller must do
164 * that. */
165 if (s == keep_msg)
166 keep_msg = NULL;
167
168 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000169 msg_start();
170 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (buf != NULL)
172 s = buf;
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 msg_outtrans_attr(s, attr);
175 msg_clr_eos();
176 retval = msg_end();
177
178 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
179 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000180 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
182 vim_free(buf);
183 --entered;
184 return retval;
185}
186
187/*
188 * Truncate a string such that it can be printed without causing a scroll.
189 * Returns an allocated string or NULL when no truncating is done.
190 */
191 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100192msg_strtrunc(
193 char_u *s,
194 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 char_u *buf = NULL;
197 int len;
198 int room;
199
200 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000201 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
202 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {
204 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000205 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000206 /* Use all the columns. */
207 room = (int)(Rows - msg_row) * Columns - 1;
208 else
209 /* Use up to 'showcmd' column. */
210 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 if (len > room && room > 0)
212 {
213#ifdef FEAT_MBYTE
214 if (enc_utf8)
215 /* may have up to 18 bytes per cell (6 per char, up to two
216 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100217 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 else if (enc_dbcs == DBCS_JPNU)
219 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100220 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 else
222#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = room + 2;
224 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228 }
229 return buf;
230}
231
232/*
233 * Truncate a string "s" to "buf" with cell width "room".
234 * "s" and "buf" may be equal.
235 */
236 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100237trunc_string(
238 char_u *s,
239 char_u *buf,
240 int room,
241 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242{
243 int half;
244 int len;
245 int e;
246 int i;
247 int n;
248
249 room -= 3;
250 half = room / 2;
251 len = 0;
252
253 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100254 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {
256 if (s[e] == NUL)
257 {
258 /* text fits without truncating! */
259 buf[e] = NUL;
260 return;
261 }
262 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200263 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 break;
265 len += n;
266 buf[e] = s[e];
267#ifdef FEAT_MBYTE
268 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000269 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100271 if (++e == buflen)
272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 buf[e] = s[e];
274 }
275#endif
276 }
277
278 /* Last part: End of the string. */
279 i = e;
280#ifdef FEAT_MBYTE
281 if (enc_dbcs != 0)
282 {
283 /* For DBCS going backwards in a string is slow, but
284 * computing the cell width isn't too slow: go forward
285 * until the rest fits. */
286 n = vim_strsize(s + i);
287 while (len + n > room)
288 {
289 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000290 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292 }
293 else if (enc_utf8)
294 {
295 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000296 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 for (;;)
298 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000299 do
300 half = half - (*mb_head_off)(s, s + half - 1) - 1;
301 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 n = ptr2cells(s + half);
303 if (len + n > room)
304 break;
305 len += n;
306 i = half;
307 }
308 }
309 else
310#endif
311 {
312 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
313 len += n;
314 }
315
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200316
317 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100318 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200319 /* text fits without truncating */
320 if (s != buf)
321 {
322 len = STRLEN(s);
323 if (len >= buflen)
324 len = buflen - 1;
325 len = len - e + 1;
326 if (len < 1)
327 buf[e - 1] = NUL;
328 else
329 mch_memmove(buf + e, s + e, len);
330 }
331 }
332 else if (e + 3 < buflen)
333 {
334 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100335 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaar38f5f952012-01-26 13:01:59 +0100336 len = (int)STRLEN(s + i) + 1;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100337 if (len >= buflen - e - 3)
338 len = buflen - e - 3 - 1;
339 mch_memmove(buf + e + 3, s + i, len);
340 buf[e + 3 + len - 1] = NUL;
341 }
342 else
343 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200344 /* can't fit in the "...", just truncate it */
345 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347}
348
349/*
350 * Automatic prototype generation does not understand this function.
351 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
352 * shorter than IOSIZE!!!
353 */
354#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000356int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100359# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362smsg(char_u *s, ...)
363{
364 va_list arglist;
365
366 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000367 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 va_end(arglist);
369 return msg(IObuff);
370}
371
372 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100373# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376smsg_attr(int attr, char_u *s, ...)
377{
378 va_list arglist;
379
380 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000381 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 va_end(arglist);
383 return msg_attr(IObuff, attr);
384}
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386#endif
387
388/*
389 * Remember the last sourcing name/lnum used in an error message, so that it
390 * isn't printed each time when it didn't change.
391 */
392static int last_sourcing_lnum = 0;
393static char_u *last_sourcing_name = NULL;
394
395/*
396 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
397 * for the next error message;
398 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000399 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100400reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
402 vim_free(last_sourcing_name);
403 last_sourcing_name = NULL;
404 last_sourcing_lnum = 0;
405}
406
407/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000408 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
409 */
410 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100411other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000412{
413 if (sourcing_name != NULL)
414 {
415 if (last_sourcing_name != NULL)
416 return STRCMP(sourcing_name, last_sourcing_name) != 0;
417 return TRUE;
418 }
419 return FALSE;
420}
421
422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Get the message about the source, as used for an error message.
424 * Returns an allocated string with room for one more character.
425 * Returns NULL when no message is to be given.
426 */
427 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100428get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 char_u *Buf, *p;
431
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000432 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {
434 p = (char_u *)_("Error detected while processing %s:");
435 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
436 if (Buf != NULL)
437 sprintf((char *)Buf, (char *)p, sourcing_name);
438 return Buf;
439 }
440 return NULL;
441}
442
443/*
444 * Get the message about the source lnum, as used for an error message.
445 * Returns an allocated string with room for one more character.
446 * Returns NULL when no message is to be given.
447 */
448 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100449get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450{
451 char_u *Buf, *p;
452
453 /* lnum is 0 when executing a command from the command line
454 * argument, we don't want a line number then */
455 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 && sourcing_lnum != 0)
458 {
459 p = (char_u *)_("line %4ld:");
460 Buf = alloc((unsigned)(STRLEN(p) + 20));
461 if (Buf != NULL)
462 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
463 return Buf;
464 }
465 return NULL;
466}
467
468/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000469 * Display name and line number for the source of an error.
470 * Remember the file name and line number, so that for the next error the info
471 * is only displayed if it changed.
472 */
473 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100474msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000475{
476 char_u *p;
477
478 ++no_wait_return;
479 p = get_emsg_source();
480 if (p != NULL)
481 {
482 msg_attr(p, attr);
483 vim_free(p);
484 }
485 p = get_emsg_lnum();
486 if (p != NULL)
487 {
488 msg_attr(p, hl_attr(HLF_N));
489 vim_free(p);
490 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
491 }
492
493 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000494 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000495 {
496 vim_free(last_sourcing_name);
497 if (sourcing_name == NULL)
498 last_sourcing_name = NULL;
499 else
500 last_sourcing_name = vim_strsave(sourcing_name);
501 }
502 --no_wait_return;
503}
504
505/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000506 * Return TRUE if not giving error messages right now:
507 * If "emsg_off" is set: no error messages at the moment.
508 * If "msg" is in 'debug': do error message but without side effects.
509 * If "emsg_skip" is set: never do error messages.
510 */
511 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100512emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000513{
514 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
515 && vim_strchr(p_debug, 't') == NULL)
516#ifdef FEAT_EVAL
517 || emsg_skip > 0
518#endif
519 )
520 return TRUE;
521 return FALSE;
522}
523
524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 * emsg() - display an error message
526 *
527 * Rings the bell, if appropriate, and calls message() to do the real work
528 * When terminal not initialized (yet) mch_errmsg(..) is used.
529 *
530 * return TRUE if wait_return not called
531 */
532 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100533emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 char_u *p;
537#ifdef FEAT_EVAL
538 int ignore = FALSE;
539 int severe;
540#endif
541
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100542 /* Skip this if not giving error messages at the moment. */
543 if (emsg_not_now())
544 return TRUE;
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 called_emsg = TRUE;
Bram Moolenaar8b778d52016-02-18 20:31:34 +0100547 if (emsg_silent == 0)
548 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000551 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
552 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
554#ifdef FEAT_EVAL
555 severe = emsg_severe;
556 emsg_severe = FALSE;
557#endif
558
Bram Moolenaar57657d82006-04-21 22:12:41 +0000559 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
561#ifdef FEAT_EVAL
562 /*
563 * Cause a throw of an error exception if appropriate. Don't display
564 * the error message in this case. (If no matching catch clause will
565 * be found, the message will be displayed later on.) "ignore" is set
566 * when the message should be ignored completely (used for the
567 * interrupt message).
568 */
569 if (cause_errthrow(s, severe, &ignore) == TRUE)
570 {
571 if (!ignore)
572 did_emsg = TRUE;
573 return TRUE;
574 }
575
576 /* set "v:errmsg", also when using ":silent! cmd" */
577 set_vim_var_string(VV_ERRMSG, s, -1);
578#endif
579
580 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000581 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 * But do write it to the redirection file.
583 */
584 if (emsg_silent != 0)
585 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200586 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200588 msg_start();
589 p = get_emsg_source();
590 if (p != NULL)
591 {
592 STRCAT(p, "\n");
593 redir_write(p, -1);
594 vim_free(p);
595 }
596 p = get_emsg_lnum();
597 if (p != NULL)
598 {
599 STRCAT(p, "\n");
600 redir_write(p, -1);
601 vim_free(p);
602 }
603 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 return TRUE;
606 }
607
608 /* Reset msg_silent, an error causes messages to be switched back on. */
609 msg_silent = 0;
610 cmd_silent = FALSE;
611
612 if (global_busy) /* break :global command */
613 ++global_busy;
614
615 if (p_eb)
616 beep_flush(); /* also includes flush_buffers() */
617 else
618 flush_buffers(FALSE); /* flush internal buffers */
619 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
621
622 emsg_on_display = TRUE; /* remember there is an error message */
623 ++msg_scroll; /* don't overwrite a previous message */
624 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000625 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 need_wait_return = TRUE; /* needed in case emsg() is called after
627 * wait_return has reset need_wait_return
628 * and a redraw is expected because
629 * msg_scrolled is non-zero */
630
631 /*
632 * Display name and line number for the source of the error.
633 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000634 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 /*
637 * Display the error message itself.
638 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000639 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 return msg_attr(s, attr);
641}
642
643/*
644 * Print an error message with one "%s" and one string argument.
645 */
646 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100647emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 return emsg3(s, a1, NULL);
650}
651
Bram Moolenaarea424162005-06-16 21:51:00 +0000652/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaardf177f62005-02-22 08:39:57 +0000654 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100655emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000656{
657 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
658}
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660/*
661 * Like msg(), but truncate to a single line if p_shm contains 't', or when
662 * "force" is TRUE. This truncates in another way as for normal messages.
663 * Careful: The string may be changed by msg_may_trunc()!
664 * Returns a pointer to the printed message, if wait_return() not called.
665 */
666 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100667msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
669 int n;
670
671 /* Add message to history before truncating */
672 add_msg_hist(s, -1, attr);
673
674 s = msg_may_trunc(force, s);
675
676 msg_hist_off = TRUE;
677 n = msg_attr(s, attr);
678 msg_hist_off = FALSE;
679
680 if (n)
681 return s;
682 return NULL;
683}
684
685/*
686 * Check if message "s" should be truncated at the start (for filenames).
687 * Return a pointer to where the truncated message starts.
688 * Note: May change the message by replacing a character with '<'.
689 */
690 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100691msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 int n;
694 int room;
695
696 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
697 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
698 && (n = (int)STRLEN(s) - room) > 0)
699 {
700#ifdef FEAT_MBYTE
701 if (has_mbyte)
702 {
703 int size = vim_strsize(s);
704
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000705 /* There may be room anyway when there are multibyte chars. */
706 if (size <= room)
707 return s;
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 for (n = 0; size >= room; )
710 {
711 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000712 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 }
714 --n;
715 }
716#endif
717 s += n;
718 *s = '<';
719 }
720 return s;
721}
722
723 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100724add_msg_hist(
725 char_u *s,
726 int len, /* -1 for undetermined length */
727 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
729 struct msg_hist *p;
730
731 if (msg_hist_off || msg_silent != 0)
732 return;
733
734 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000735 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000736 (void)delete_first_msg();
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 /* allocate an entry and add the message at the end of the history */
739 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
740 if (p != NULL)
741 {
742 if (len < 0)
743 len = (int)STRLEN(s);
744 /* remove leading and trailing newlines */
745 while (len > 0 && *s == '\n')
746 {
747 ++s;
748 --len;
749 }
750 while (len > 0 && s[len - 1] == '\n')
751 --len;
752 p->msg = vim_strnsave(s, len);
753 p->next = NULL;
754 p->attr = attr;
755 if (last_msg_hist != NULL)
756 last_msg_hist->next = p;
757 last_msg_hist = p;
758 if (first_msg_hist == NULL)
759 first_msg_hist = last_msg_hist;
760 ++msg_hist_len;
761 }
762}
763
764/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000765 * Delete the first (oldest) message from the history.
766 * Returns FAIL if there are no messages.
767 */
768 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100769delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000770{
771 struct msg_hist *p;
772
773 if (msg_hist_len <= 0)
774 return FAIL;
775 p = first_msg_hist;
776 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000777 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200778 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000779 vim_free(p->msg);
780 vim_free(p);
781 --msg_hist_len;
782 return OK;
783}
784
785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * ":messages" command.
787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200789ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791 struct msg_hist *p;
792 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200793 int c = 0;
794
795 if (STRCMP(eap->arg, "clear") == 0)
796 {
797 int keep = eap->addr_count == 0 ? 0 : eap->line2;
798
799 while (msg_hist_len > keep)
800 (void)delete_first_msg();
801 return;
802 }
803
804 if (*eap->arg != NUL)
805 {
806 EMSG(_(e_invarg));
807 return;
808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 msg_hist_off = TRUE;
811
Bram Moolenaar451f8492016-04-14 17:16:22 +0200812 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200813 if (eap->addr_count != 0)
814 {
815 /* Count total messages */
816 for (; p != NULL && !got_int; p = p->next)
817 c++;
818
819 c -= eap->line2;
820
821 /* Skip without number of messages specified */
822 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
823 p = p->next, c--);
824 }
825
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200826 if (p == first_msg_hist)
827 {
828 s = mch_getenv((char_u *)"LANG");
829 if (s != NULL && *s != NUL)
830 msg_attr((char_u *)
831 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
832 hl_attr(HLF_T));
833 }
834
Bram Moolenaar451f8492016-04-14 17:16:22 +0200835 /* Display what was not skipped. */
836 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (p->msg != NULL)
838 msg_attr(p->msg, p->attr);
839
840 msg_hist_off = FALSE;
841}
842
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000843#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844/*
845 * Call this after prompting the user. This will avoid a hit-return message
846 * and a delay.
847 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000848 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100849msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 need_wait_return = FALSE;
852 emsg_on_display = FALSE;
853 cmdline_row = msg_row;
854 msg_col = 0;
855 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100856 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858#endif
859
860/*
861 * wait for the user to hit a key (normally a return)
862 * if 'redraw' is TRUE, clear and redraw the screen
863 * if 'redraw' is FALSE, just redraw the screen
864 * if 'redraw' is -1, don't redraw at all
865 */
866 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100867wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868{
869 int c;
870 int oldState;
871 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100873 int save_Recording;
874 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
876 if (redraw == TRUE)
877 must_redraw = CLEAR;
878
879 /* If using ":silent cmd", don't wait for a return. Also don't set
880 * need_wait_return to do it later. */
881 if (msg_silent != 0)
882 return;
883
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100884 /*
885 * When inside vgetc(), we can't wait for a typed character at all.
886 * With the global command (and some others) we only need one return at
887 * the end. Adjust cmdline_row to avoid the next message overwriting the
888 * last one.
889 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000890 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100892 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (no_wait_return)
894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (!exmode_active)
896 cmdline_row = msg_row;
897 return;
898 }
899
900 redir_off = TRUE; /* don't redirect this message */
901 oldState = State;
902 if (quit_more)
903 {
904 c = CAR; /* just pretend CR was hit */
905 quit_more = FALSE;
906 got_int = FALSE;
907 }
908 else if (exmode_active)
909 {
910 MSG_PUTS(" "); /* make sure the cursor is on the right line */
911 c = CAR; /* no need for a return in ex mode */
912 got_int = FALSE;
913 }
914 else
915 {
916 /* Make sure the hit-return prompt is on screen when 'guioptions' was
917 * just changed. */
918 screenalloc(FALSE);
919
920 State = HITRETURN;
921#ifdef FEAT_MOUSE
922 setmouse();
923#endif
924#ifdef USE_ON_FLY_SCROLL
925 dont_scroll = TRUE; /* disallow scrolling here */
926#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100927 cmdline_row = msg_row;
928
Bram Moolenaarec38d692013-04-24 15:12:32 +0200929 /* Avoid the sequence that the user types ":" at the hit-return prompt
930 * to start an Ex command, but the file-changed dialog gets in the
931 * way. */
932 if (need_check_timestamps)
933 check_timestamps(FALSE);
934
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 hit_return_msg();
936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 do
938 {
939 /* Remember "got_int", if it is set vgetc() probably returns a
940 * CTRL-C, but we need to loop then. */
941 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000942
943 /* Don't do mappings here, we put the character back in the
944 * typeahead buffer. */
945 ++no_mapping;
946 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100947
948 /* Temporarily disable Recording. If Recording is active, the
949 * character will be recorded later, since it will be added to the
950 * typebuf after the loop */
951 save_Recording = Recording;
952 save_scriptout = scriptout;
953 Recording = FALSE;
954 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000956 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000958 --no_mapping;
959 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100960 Recording = save_Recording;
961 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#ifdef FEAT_CLIPBOARD
964 /* Strange way to allow copying (yanking) a modeless selection at
965 * the hit-enter prompt. Use CTRL-Y, because the same is used in
966 * Cmdline-mode and it's harmless when there is no selection. */
967 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
968 {
969 clip_copy_modeless_selection(TRUE);
970 c = K_IGNORE;
971 }
972#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000973
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000974 /*
975 * Allow scrolling back in the messages.
976 * Also accept scroll-down commands when messages fill the screen,
977 * to avoid that typing one 'j' too many makes the messages
978 * disappear.
979 */
980 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000981 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100982 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
983 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000984 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100985 if (msg_scrolled > Rows)
986 /* scroll back to show older messages */
987 do_more_prompt(c);
988 else
989 {
990 msg_didout = FALSE;
991 c = K_IGNORE;
992 msg_col =
993#ifdef FEAT_RIGHTLEFT
994 cmdmsg_rl ? Columns - 1 :
995#endif
996 0;
997 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000998 if (quit_more)
999 {
1000 c = CAR; /* just pretend CR was hit */
1001 quit_more = FALSE;
1002 got_int = FALSE;
1003 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001004 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001005 {
1006 c = K_IGNORE;
1007 hit_return_msg();
1008 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001009 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001010 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001011 && (c == 'j' || c == 'd' || c == 'f'
1012 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001013 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 } while ((had_got_int && c == Ctrl_C)
1016 || c == K_IGNORE
1017#ifdef FEAT_GUI
1018 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1019#endif
1020#ifdef FEAT_MOUSE
1021 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1022 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1023 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001024 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 || c == K_MOUSEDOWN || c == K_MOUSEUP
1026 || (!mouse_has(MOUSE_RETURN)
1027 && mouse_row < msg_row
1028 && (c == K_LEFTMOUSE
1029 || c == K_MIDDLEMOUSE
1030 || c == K_RIGHTMOUSE
1031 || c == K_X1MOUSE
1032 || c == K_X2MOUSE))
1033#endif
1034 );
1035 ui_breakcheck();
1036#ifdef FEAT_MOUSE
1037 /*
1038 * Avoid that the mouse-up event causes visual mode to start.
1039 */
1040 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1041 || c == K_X1MOUSE || c == K_X2MOUSE)
1042 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1043 else
1044#endif
1045 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1046 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001047 /* Put the character back in the typeahead buffer. Don't use the
1048 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001049 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001051 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
1054 redir_off = FALSE;
1055
1056 /*
1057 * If the user hits ':', '?' or '/' we get a command line from the next
1058 * line.
1059 */
1060 if (c == ':' || c == '?' || c == '/')
1061 {
1062 if (!exmode_active)
1063 cmdline_row = msg_row;
1064 skip_redraw = TRUE; /* skip redraw once */
1065 do_redraw = FALSE;
1066 }
1067
1068 /*
1069 * If the window size changed set_shellsize() will redraw the screen.
1070 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1071 * typed.
1072 */
1073 tmpState = State;
1074 State = oldState; /* restore State before set_shellsize */
1075#ifdef FEAT_MOUSE
1076 setmouse();
1077#endif
1078 msg_check();
1079
1080#if defined(UNIX) || defined(VMS)
1081 /*
1082 * When switching screens, we need to output an extra newline on exit.
1083 */
1084 if (swapping_screen() && !termcap_active)
1085 newline_on_exit = TRUE;
1086#endif
1087
1088 need_wait_return = FALSE;
1089 did_wait_return = TRUE;
1090 emsg_on_display = FALSE; /* can delete error message now */
1091 lines_left = -1; /* reset lines_left at next msg_start() */
1092 reset_last_sourcing();
1093 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1094 (Rows - cmdline_row - 1) * Columns + sc_col)
1095 {
1096 vim_free(keep_msg);
1097 keep_msg = NULL; /* don't redisplay message, it's too long */
1098 }
1099
1100 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1101 {
1102 starttermcap(); /* start termcap before redrawing */
1103 shell_resized();
1104 }
1105 else if (!skip_redraw
1106 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1107 {
1108 starttermcap(); /* start termcap before redrawing */
1109 redraw_later(VALID);
1110 }
1111}
1112
1113/*
1114 * Write the hit-return prompt.
1115 */
1116 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001117hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001119 int save_p_more = p_more;
1120
1121 p_more = FALSE; /* don't want see this message when scrolling back */
1122 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 msg_putchar('\n');
1124 if (got_int)
1125 MSG_PUTS(_("Interrupt: "));
1126
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001127 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (!msg_use_printf())
1129 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001130 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131}
1132
1133/*
1134 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1135 */
1136 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001137set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138{
1139 vim_free(keep_msg);
1140 if (s != NULL && msg_silent == 0)
1141 keep_msg = vim_strsave(s);
1142 else
1143 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001144 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001145 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146}
1147
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001148#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1149/*
1150 * If there currently is a message being displayed, set "keep_msg" to it, so
1151 * that it will be displayed again after redraw.
1152 */
1153 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001154set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001155{
1156 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1157 && (State & NORMAL))
1158 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1159}
1160#endif
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162/*
1163 * Prepare for outputting characters in the command line.
1164 */
1165 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001166msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168 int did_return = FALSE;
1169
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001170 if (!msg_silent)
1171 {
1172 vim_free(keep_msg);
1173 keep_msg = NULL; /* don't display old message now */
1174 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001175
1176#ifdef FEAT_EVAL
1177 if (need_clr_eos)
1178 {
1179 /* Halfway an ":echo" command and getting an (error) message: clear
1180 * any text from the command. */
1181 need_clr_eos = FALSE;
1182 msg_clr_eos();
1183 }
1184#endif
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (!msg_scroll && full_screen) /* overwrite last message */
1187 {
1188 msg_row = cmdline_row;
1189 msg_col =
1190#ifdef FEAT_RIGHTLEFT
1191 cmdmsg_rl ? Columns - 1 :
1192#endif
1193 0;
1194 }
1195 else if (msg_didout) /* start message on next line */
1196 {
1197 msg_putchar('\n');
1198 did_return = TRUE;
1199 if (exmode_active != EXMODE_NORMAL)
1200 cmdline_row = msg_row;
1201 }
1202 if (!msg_didany || lines_left < 0)
1203 msg_starthere();
1204 if (msg_silent == 0)
1205 {
1206 msg_didout = FALSE; /* no output on current line yet */
1207 cursor_off();
1208 }
1209
1210 /* when redirecting, may need to start a new line. */
1211 if (!did_return)
1212 redir_write((char_u *)"\n", -1);
1213}
1214
1215/*
1216 * Note that the current msg position is where messages start.
1217 */
1218 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001219msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 lines_left = cmdline_row;
1222 msg_didany = FALSE;
1223}
1224
1225 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001226msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227{
1228 msg_putchar_attr(c, 0);
1229}
1230
1231 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001232msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233{
1234#ifdef FEAT_MBYTE
1235 char_u buf[MB_MAXBYTES + 1];
1236#else
1237 char_u buf[4];
1238#endif
1239
1240 if (IS_SPECIAL(c))
1241 {
1242 buf[0] = K_SPECIAL;
1243 buf[1] = K_SECOND(c);
1244 buf[2] = K_THIRD(c);
1245 buf[3] = NUL;
1246 }
1247 else
1248 {
1249#ifdef FEAT_MBYTE
1250 buf[(*mb_char2bytes)(c, buf)] = NUL;
1251#else
1252 buf[0] = c;
1253 buf[1] = NUL;
1254#endif
1255 }
1256 msg_puts_attr(buf, attr);
1257}
1258
1259 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001260msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
1262 char_u buf[20];
1263
1264 sprintf((char *)buf, "%ld", n);
1265 msg_puts(buf);
1266}
1267
1268 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001269msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270{
1271 msg_home_replace_attr(fname, 0);
1272}
1273
1274#if defined(FEAT_FIND_ID) || defined(PROTO)
1275 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001276msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 msg_home_replace_attr(fname, hl_attr(HLF_D));
1279}
1280#endif
1281
1282 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001283msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 char_u *name;
1286
1287 name = home_replace_save(NULL, fname);
1288 if (name != NULL)
1289 msg_outtrans_attr(name, attr);
1290 vim_free(name);
1291}
1292
1293/*
1294 * Output 'len' characters in 'str' (including NULs) with translation
1295 * if 'len' is -1, output upto a NUL character.
1296 * Use attributes 'attr'.
1297 * Return the number of characters it takes on the screen.
1298 */
1299 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001300msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 return msg_outtrans_attr(str, 0);
1303}
1304
1305 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001306msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1309}
1310
1311 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001312msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 return msg_outtrans_len_attr(str, len, 0);
1315}
1316
1317/*
1318 * Output one character at "p". Return pointer to the next character.
1319 * Handles multi-byte characters.
1320 */
1321 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001322msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324#ifdef FEAT_MBYTE
1325 int l;
1326
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001327 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 {
1329 msg_outtrans_len_attr(p, l, attr);
1330 return p + l;
1331 }
1332#endif
1333 msg_puts_attr(transchar_byte(*p), attr);
1334 return p + 1;
1335}
1336
1337 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001338msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 int retval = 0;
1341 char_u *str = msgstr;
1342 char_u *plain_start = msgstr;
1343 char_u *s;
1344#ifdef FEAT_MBYTE
1345 int mb_l;
1346 int c;
1347#endif
1348
1349 /* if MSG_HIST flag set, add message to history */
1350 if (attr & MSG_HIST)
1351 {
1352 add_msg_hist(str, len, attr);
1353 attr &= ~MSG_HIST;
1354 }
1355
1356#ifdef FEAT_MBYTE
1357 /* If the string starts with a composing character first draw a space on
1358 * which the composing char can be drawn. */
1359 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1360 msg_puts_attr((char_u *)" ", attr);
1361#endif
1362
1363 /*
1364 * Go over the string. Special characters are translated and printed.
1365 * Normal characters are printed several at a time.
1366 */
1367 while (--len >= 0)
1368 {
1369#ifdef FEAT_MBYTE
1370 if (enc_utf8)
1371 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001372 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001374 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 else
1376 mb_l = 1;
1377 if (has_mbyte && mb_l > 1)
1378 {
1379 c = (*mb_ptr2char)(str);
1380 if (vim_isprintc(c))
1381 /* printable multi-byte char: count the cells. */
1382 retval += (*mb_ptr2cells)(str);
1383 else
1384 {
1385 /* unprintable multi-byte char: print the printable chars so
1386 * far and the translation of the unprintable char. */
1387 if (str > plain_start)
1388 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1389 attr);
1390 plain_start = str + mb_l;
1391 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1392 retval += char2cells(c);
1393 }
1394 len -= mb_l - 1;
1395 str += mb_l;
1396 }
1397 else
1398#endif
1399 {
1400 s = transchar_byte(*str);
1401 if (s[1] != NUL)
1402 {
1403 /* unprintable char: print the printable chars so far and the
1404 * translation of the unprintable char. */
1405 if (str > plain_start)
1406 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1407 attr);
1408 plain_start = str + 1;
1409 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001410 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001412 else
1413 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 ++str;
1415 }
1416 }
1417
1418 if (str > plain_start)
1419 /* print the printable chars at the end */
1420 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1421
1422 return retval;
1423}
1424
1425#if defined(FEAT_QUICKFIX) || defined(PROTO)
1426 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001427msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
1429 int i;
1430 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1431
1432 arg = skipwhite(arg);
1433 for (i = 5; *arg && i >= 0; --i)
1434 if (*arg++ != str[i])
1435 break;
1436 if (i < 0)
1437 {
1438 msg_putchar('\n');
1439 for (i = 0; rs[i]; ++i)
1440 msg_putchar(rs[i] - 3);
1441 }
1442}
1443#endif
1444
1445/*
1446 * Output the string 'str' upto a NUL character.
1447 * Return the number of characters it takes on the screen.
1448 *
1449 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1450 * following character and shown as <F1>, <S-Up> etc. Any other character
1451 * which is not printable shown in <> form.
1452 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1453 * If a character is displayed in one of these special ways, is also
1454 * highlighted (its highlight name is '8' in the p_hl variable).
1455 * Otherwise characters are not highlighted.
1456 * This function is used to show mappings, where we want to see how to type
1457 * the character/string -- webb
1458 */
1459 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001460msg_outtrans_special(
1461 char_u *strstart,
1462 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 char_u *str = strstart;
1465 int retval = 0;
1466 char_u *string;
1467 int attr;
1468 int len;
1469
1470 attr = hl_attr(HLF_8);
1471 while (*str != NUL)
1472 {
1473 /* Leading and trailing spaces need to be displayed in <> form. */
1474 if ((str == strstart || str[1] == NUL) && *str == ' ')
1475 {
1476 string = (char_u *)"<Space>";
1477 ++str;
1478 }
1479 else
1480 string = str2special(&str, from);
1481 len = vim_strsize(string);
1482 /* Highlight special keys */
1483 msg_puts_attr(string, len > 1
1484#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001485 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#endif
1487 ? attr : 0);
1488 retval += len;
1489 }
1490 return retval;
1491}
1492
Bram Moolenaarbd743252010-10-20 21:23:33 +02001493#if defined(FEAT_EVAL) || defined(PROTO)
1494/*
1495 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1496 * strings, in an allocated string.
1497 */
1498 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001499str2special_save(
1500 char_u *str,
1501 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001502{
1503 garray_T ga;
1504 char_u *p = str;
1505
1506 ga_init2(&ga, 1, 40);
1507 while (*p != NUL)
1508 ga_concat(&ga, str2special(&p, is_lhs));
1509 ga_append(&ga, NUL);
1510 return (char_u *)ga.ga_data;
1511}
1512#endif
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514/*
1515 * Return the printable string for the key codes at "*sp".
1516 * Used for translating the lhs or rhs of a mapping to printable chars.
1517 * Advances "sp" to the next code.
1518 */
1519 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001520str2special(
1521 char_u **sp,
1522 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 int c;
1525 static char_u buf[7];
1526 char_u *str = *sp;
1527 int modifiers = 0;
1528 int special = FALSE;
1529
1530#ifdef FEAT_MBYTE
1531 if (has_mbyte)
1532 {
1533 char_u *p;
1534
1535 /* Try to un-escape a multi-byte character. Return the un-escaped
1536 * string if it is a multi-byte character. */
1537 p = mb_unescape(sp);
1538 if (p != NULL)
1539 return p;
1540 }
1541#endif
1542
1543 c = *str;
1544 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1545 {
1546 if (str[1] == KS_MODIFIER)
1547 {
1548 modifiers = str[2];
1549 str += 3;
1550 c = *str;
1551 }
1552 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1553 {
1554 c = TO_SPECIAL(str[1], str[2]);
1555 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001556 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 c = NUL;
1558 }
1559 if (IS_SPECIAL(c) || modifiers) /* special key */
1560 special = TRUE;
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
1563#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001564 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001566 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001567
1568 /* For multi-byte characters check for an illegal byte. */
1569 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1570 {
1571 transchar_nonprint(buf, c);
1572 *sp = str + 1;
1573 return buf;
1574 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001575 /* Since 'special' is TRUE the multi-byte character 'c' will be
1576 * processed by get_special_key_name() */
1577 c = (*mb_ptr2char)(str);
1578 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001582 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1585 * Use <Space> only for lhs of a mapping. */
1586 if (special || char2cells(c) > 1 || (from && c == ' '))
1587 return get_special_key_name(c, modifiers);
1588 buf[0] = c;
1589 buf[1] = NUL;
1590 return buf;
1591}
1592
1593/*
1594 * Translate a key sequence into special key names.
1595 */
1596 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001597str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 char_u *s;
1600
1601 *buf = NUL;
1602 while (*sp)
1603 {
1604 s = str2special(&sp, FALSE);
1605 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1606 STRCAT(buf, s);
1607 }
1608}
1609
1610/*
1611 * print line for :print or :list command
1612 */
1613 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001614msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 int c;
1617 int col = 0;
1618 int n_extra = 0;
1619 int c_extra = 0;
1620 char_u *p_extra = NULL; /* init to make SASC shut up */
1621 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001622 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 char_u *trail = NULL;
1624#ifdef FEAT_MBYTE
1625 int l;
1626 char_u buf[MB_MAXBYTES + 1];
1627#endif
1628
Bram Moolenaardf177f62005-02-22 08:39:57 +00001629 if (curwin->w_p_list)
1630 list = TRUE;
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001633 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 trail = s + STRLEN(s);
1636 while (trail > s && vim_iswhite(trail[-1]))
1637 --trail;
1638 }
1639
1640 /* output a space for an empty line, otherwise the line will be
1641 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001642 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 msg_putchar(' ');
1644
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001645 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001647 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649 --n_extra;
1650 if (c_extra)
1651 c = c_extra;
1652 else
1653 c = *p_extra++;
1654 }
1655#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001656 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001659 if (lcs_nbsp != NUL && list
1660 && (mb_ptr2char(s) == 160
1661 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001662 {
1663 mb_char2bytes(lcs_nbsp, buf);
1664 buf[(*mb_ptr2len)(buf)] = NUL;
1665 }
1666 else
1667 {
1668 mch_memmove(buf, s, (size_t)l);
1669 buf[l] = NUL;
1670 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001671 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 s += l;
1673 continue;
1674 }
1675#endif
1676 else
1677 {
1678 attr = 0;
1679 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001680 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 {
1682 /* tab amount depends on current column */
1683 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001684 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
1686 c = ' ';
1687 c_extra = ' ';
1688 }
1689 else
1690 {
1691 c = lcs_tab1;
1692 c_extra = lcs_tab2;
1693 attr = hl_attr(HLF_8);
1694 }
1695 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001696 else if (c == 160 && list && lcs_nbsp != NUL)
1697 {
1698 c = lcs_nbsp;
1699 attr = hl_attr(HLF_8);
1700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001701 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
1703 p_extra = (char_u *)"";
1704 c_extra = NUL;
1705 n_extra = 1;
1706 c = lcs_eol;
1707 attr = hl_attr(HLF_AT);
1708 --s;
1709 }
1710 else if (c != NUL && (n = byte2cells(c)) > 1)
1711 {
1712 n_extra = n - 1;
1713 p_extra = transchar_byte(c);
1714 c_extra = NUL;
1715 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001716 /* Use special coloring to be able to distinguish <hex> from
1717 * the same in plain text. */
1718 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 else if (c == ' ' && trail != NULL && s > trail)
1721 {
1722 c = lcs_trail;
1723 attr = hl_attr(HLF_8);
1724 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001725 else if (c == ' ' && list && lcs_space != NUL)
1726 {
1727 c = lcs_space;
1728 attr = hl_attr(HLF_8);
1729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
1731
1732 if (c == NUL)
1733 break;
1734
1735 msg_putchar_attr(c, attr);
1736 col++;
1737 }
1738 msg_clr_eos();
1739}
1740
1741#ifdef FEAT_MBYTE
1742/*
1743 * Use screen_puts() to output one multi-byte character.
1744 * Return the pointer "s" advanced to the next character.
1745 */
1746 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001747screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 int cw;
1750
1751 msg_didout = TRUE; /* remember that line is not empty */
1752 cw = (*mb_ptr2cells)(s);
1753 if (cw > 1 && (
1754#ifdef FEAT_RIGHTLEFT
1755 cmdmsg_rl ? msg_col <= 1 :
1756#endif
1757 msg_col == Columns - 1))
1758 {
1759 /* Doesn't fit, print a highlighted '>' to fill it up. */
1760 msg_screen_putchar('>', hl_attr(HLF_AT));
1761 return s;
1762 }
1763
1764 screen_puts_len(s, l, msg_row, msg_col, attr);
1765#ifdef FEAT_RIGHTLEFT
1766 if (cmdmsg_rl)
1767 {
1768 msg_col -= cw;
1769 if (msg_col == 0)
1770 {
1771 msg_col = Columns;
1772 ++msg_row;
1773 }
1774 }
1775 else
1776#endif
1777 {
1778 msg_col += cw;
1779 if (msg_col >= Columns)
1780 {
1781 msg_col = 0;
1782 ++msg_row;
1783 }
1784 }
1785 return s + l;
1786}
1787#endif
1788
1789/*
1790 * Output a string to the screen at position msg_row, msg_col.
1791 * Update msg_row and msg_col for the next message.
1792 */
1793 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001794msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001796 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797}
1798
1799 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001800msg_puts_title(
1801 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
1803 msg_puts_attr(s, hl_attr(HLF_T));
1804}
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806/*
1807 * Show a message in such a way that it always fits in the line. Cut out a
1808 * part in the middle and replace it with "..." when necessary.
1809 * Does not handle multi-byte characters!
1810 */
1811 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001812msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001814 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815}
1816
1817 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001818msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
1820 int slen = len;
1821 int room;
1822
1823 room = Columns - msg_col;
1824 if (len > room && room >= 20)
1825 {
1826 slen = (room - 3) / 2;
1827 msg_outtrans_len_attr(longstr, slen, attr);
1828 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1829 }
1830 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1831}
1832
1833/*
1834 * Basic function for writing a message with highlight attributes.
1835 */
1836 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001837msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
1839 msg_puts_attr_len(s, -1, attr);
1840}
1841
1842/*
1843 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1844 * When "maxlen" is -1 there is no maximum length.
1845 * When "maxlen" is >= 0 the message is not put in the history.
1846 */
1847 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001848msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 /*
1851 * If redirection is on, also write to the redirection file.
1852 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001853 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 /*
1856 * Don't print anything when using ":silent cmd".
1857 */
1858 if (msg_silent != 0)
1859 return;
1860
1861 /* if MSG_HIST flag set, add message to history */
1862 if ((attr & MSG_HIST) && maxlen < 0)
1863 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001864 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 attr &= ~MSG_HIST;
1866 }
1867
1868 /*
1869 * When writing something to the screen after it has scrolled, requires a
1870 * wait-return prompt later. Needed when scrolling, resetting
1871 * need_wait_return after some prompt, and then outputting something
1872 * without scrolling
1873 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001874 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 need_wait_return = TRUE;
1876 msg_didany = TRUE; /* remember that something was outputted */
1877
1878 /*
1879 * If there is no valid screen, use fprintf so we can see error messages.
1880 * If termcap is not active, we may be writing in an alternate console
1881 * window, cursor positioning may not work correctly (window size may be
1882 * different, e.g. for Win32 console) or we just don't know where the
1883 * cursor is.
1884 */
1885 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001886 msg_puts_printf(str, maxlen);
1887 else
1888 msg_puts_display(str, maxlen, attr, FALSE);
1889}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001891/*
1892 * The display part of msg_puts_attr_len().
1893 * May be called recursively to display scroll-back text.
1894 */
1895 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001896msg_puts_display(
1897 char_u *str,
1898 int maxlen,
1899 int attr,
1900 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001901{
1902 char_u *s = str;
1903 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1904 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1905#ifdef FEAT_MBYTE
1906 int l;
1907 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001909 char_u *sb_str = str;
1910 int sb_col = msg_col;
1911 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001912 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001915 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001918 * We are at the end of the screen line when:
1919 * - When outputting a newline.
1920 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001922 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#ifdef FEAT_RIGHTLEFT
1924 cmdmsg_rl
1925 ? (
1926 msg_col <= 1
1927 || (*s == TAB && msg_col <= 7)
1928# ifdef FEAT_MBYTE
1929 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1930# endif
1931 )
1932 :
1933#endif
1934 (msg_col + t_col >= Columns - 1
1935 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1936# ifdef FEAT_MBYTE
1937 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1938 && msg_col + t_col >= Columns - 2)
1939# endif
1940 ))))
1941 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001942 /*
1943 * The screen is scrolled up when at the last row (some terminals
1944 * scroll automatically, some don't. To avoid problems we scroll
1945 * ourselves).
1946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001949 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001951 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (msg_no_more && lines_left == 0)
1953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001955 /* Scroll the screen up one line. */
1956 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 msg_row = Rows - 2;
1959 if (msg_col >= Columns) /* can happen after screen resize */
1960 msg_col = Columns - 1;
1961
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001962 /* Display char in last column before showing more-prompt. */
1963 if (*s >= ' '
1964#ifdef FEAT_RIGHTLEFT
1965 && !cmdmsg_rl
1966#endif
1967 )
1968 {
1969#ifdef FEAT_MBYTE
1970 if (has_mbyte)
1971 {
1972 if (enc_utf8 && maxlen >= 0)
1973 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001974 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001975 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001976 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 s = screen_puts_mbyte(s, l, attr);
1978 }
1979 else
1980#endif
1981 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001982 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001983 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001984 else
1985 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001986
1987 if (p_more)
1988 /* store text for scrolling back */
1989 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1990
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001991 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (must_redraw < VALID)
1994 must_redraw = VALID;
1995 redraw_cmdline = TRUE;
1996 if (cmdline_row > 0 && !exmode_active)
1997 --cmdline_row;
1998
1999 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002000 * If screen is completely filled and 'more' is set then wait
2001 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002003 if (lines_left > 0)
2004 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002005 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 && !msg_no_more && !exmode_active)
2007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002009 if (do_more_prompt(NUL))
2010 s = confirm_msg_tail;
2011#else
2012 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013#endif
2014 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002015 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002017
2018 /* When we displayed a char in last column need to check if there
2019 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002020 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002021 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002024 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 || msg_col + t_col >= Columns
2026#ifdef FEAT_MBYTE
2027 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2028 && msg_col + t_col >= Columns - 1)
2029#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002030 ;
2031 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2032 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002034 t_puts(&t_col, t_s, s, attr);
2035
2036 if (wrap && p_more && !recurse)
2037 /* store text for scrolling back */
2038 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040 if (*s == '\n') /* go to next line */
2041 {
2042 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002043#ifdef FEAT_RIGHTLEFT
2044 if (cmdmsg_rl)
2045 msg_col = Columns - 1;
2046 else
2047#endif
2048 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (++msg_row >= Rows) /* safety check */
2050 msg_row = Rows - 1;
2051 }
2052 else if (*s == '\r') /* go to column 0 */
2053 {
2054 msg_col = 0;
2055 }
2056 else if (*s == '\b') /* go to previous char */
2057 {
2058 if (msg_col)
2059 --msg_col;
2060 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002061 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
2063 do
2064 msg_screen_putchar(' ', attr);
2065 while (msg_col & 7);
2066 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002067 else if (*s == BELL) /* beep (from ":sh") */
2068 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 {
2071#ifdef FEAT_MBYTE
2072 if (has_mbyte)
2073 {
2074 cw = (*mb_ptr2cells)(s);
2075 if (enc_utf8 && maxlen >= 0)
2076 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002077 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002079 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081 else
2082 {
2083 cw = 1;
2084 l = 1;
2085 }
2086#endif
2087 /* When drawing from right to left or when a double-wide character
2088 * doesn't fit, draw a single character here. Otherwise collect
2089 * characters and draw them all at once later. */
2090#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2091 if (
2092# ifdef FEAT_RIGHTLEFT
2093 cmdmsg_rl
2094# ifdef FEAT_MBYTE
2095 ||
2096# endif
2097# endif
2098# ifdef FEAT_MBYTE
2099 (cw > 1 && msg_col + t_col >= Columns - 1)
2100# endif
2101 )
2102 {
2103# ifdef FEAT_MBYTE
2104 if (l > 1)
2105 s = screen_puts_mbyte(s, l, attr) - 1;
2106 else
2107# endif
2108 msg_screen_putchar(*s, attr);
2109 }
2110 else
2111#endif
2112 {
2113 /* postpone this character until later */
2114 if (t_col == 0)
2115 t_s = s;
2116#ifdef FEAT_MBYTE
2117 t_col += cw;
2118 s += l - 1;
2119#else
2120 ++t_col;
2121#endif
2122 }
2123 }
2124 ++s;
2125 }
2126
2127 /* output any postponed text */
2128 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002129 t_puts(&t_col, t_s, s, attr);
2130 if (p_more && !recurse)
2131 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 msg_check();
2134}
2135
2136/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002137 * Scroll the screen up one line for displaying the next message line.
2138 */
2139 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002140msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002141{
2142#ifdef FEAT_GUI
2143 /* Remove the cursor before scrolling, ScreenLines[] is going
2144 * to become invalid. */
2145 if (gui.in_use)
2146 gui_undraw_cursor();
2147#endif
2148 /* scrolling up always works */
2149 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2150
2151 if (!can_clear((char_u *)" "))
2152 {
2153 /* Scrolling up doesn't result in the right background. Set the
2154 * background here. It's not efficient, but avoids that we have to do
2155 * it all over the code. */
2156 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2157
2158 /* Also clear the last char of the last but one line if it was not
2159 * cleared before to avoid a scroll-up. */
2160 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2161 screen_fill((int)Rows - 2, (int)Rows - 1,
2162 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2163 }
2164}
2165
2166/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002167 * Increment "msg_scrolled".
2168 */
2169 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002170inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002171{
2172#ifdef FEAT_EVAL
2173 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2174 {
2175 char_u *p = sourcing_name;
2176 char_u *tofree = NULL;
2177 int len;
2178
2179 /* v:scrollstart is empty, set it to the script/function name and line
2180 * number */
2181 if (p == NULL)
2182 p = (char_u *)_("Unknown");
2183 else
2184 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002185 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002186 tofree = alloc(len);
2187 if (tofree != NULL)
2188 {
2189 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2190 p, (long)sourcing_lnum);
2191 p = tofree;
2192 }
2193 }
2194 set_vim_var_string(VV_SCROLLSTART, p, -1);
2195 vim_free(tofree);
2196 }
2197#endif
2198 ++msg_scrolled;
2199}
2200
2201/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002202 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2203 * store the displayed text and remember where screen lines start.
2204 */
2205typedef struct msgchunk_S msgchunk_T;
2206struct msgchunk_S
2207{
2208 msgchunk_T *sb_next;
2209 msgchunk_T *sb_prev;
2210 char sb_eol; /* TRUE when line ends after this text */
2211 int sb_msg_col; /* column in which text starts */
2212 int sb_attr; /* text attributes */
2213 char_u sb_text[1]; /* text to be displayed, actually longer */
2214};
2215
2216static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2217
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002218static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2219static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002220
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002221static int do_clear_sb_text = FALSE; /* clear text on next msg */
2222
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002223/*
2224 * Store part of a printed message for displaying when scrolling back.
2225 */
2226 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002227store_sb_text(
2228 char_u **sb_str, /* start of string */
2229 char_u *s, /* just after string */
2230 int attr,
2231 int *sb_col,
2232 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002233{
2234 msgchunk_T *mp;
2235
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002236 if (do_clear_sb_text)
2237 {
2238 clear_sb_text();
2239 do_clear_sb_text = FALSE;
2240 }
2241
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002242 if (s > *sb_str)
2243 {
2244 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2245 if (mp != NULL)
2246 {
2247 mp->sb_eol = finish;
2248 mp->sb_msg_col = *sb_col;
2249 mp->sb_attr = attr;
2250 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2251
2252 if (last_msgchunk == NULL)
2253 {
2254 last_msgchunk = mp;
2255 mp->sb_prev = NULL;
2256 }
2257 else
2258 {
2259 mp->sb_prev = last_msgchunk;
2260 last_msgchunk->sb_next = mp;
2261 last_msgchunk = mp;
2262 }
2263 mp->sb_next = NULL;
2264 }
2265 }
2266 else if (finish && last_msgchunk != NULL)
2267 last_msgchunk->sb_eol = TRUE;
2268
2269 *sb_str = s;
2270 *sb_col = 0;
2271}
2272
2273/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002274 * Finished showing messages, clear the scroll-back text on the next message.
2275 */
2276 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002277may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002278{
2279 do_clear_sb_text = TRUE;
2280}
2281
2282/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002283 * Clear any text remembered for scrolling back.
2284 * Called when redrawing the screen.
2285 */
2286 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002287clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002288{
2289 msgchunk_T *mp;
2290
2291 while (last_msgchunk != NULL)
2292 {
2293 mp = last_msgchunk->sb_prev;
2294 vim_free(last_msgchunk);
2295 last_msgchunk = mp;
2296 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002297}
2298
2299/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002300 * "g<" command.
2301 */
2302 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002303show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002304{
2305 msgchunk_T *mp;
2306
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002307 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002308 * weird, typing a command without output results in one line. */
2309 mp = msg_sb_start(last_msgchunk);
2310 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002311 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002312 else
2313 {
2314 do_more_prompt('G');
2315 wait_return(FALSE);
2316 }
2317}
2318
2319/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002320 * Move to the start of screen line in already displayed text.
2321 */
2322 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002323msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002324{
2325 msgchunk_T *mp = mps;
2326
2327 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2328 mp = mp->sb_prev;
2329 return mp;
2330}
2331
2332/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002333 * Mark the last message chunk as finishing the line.
2334 */
2335 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002336msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002337{
2338 if (last_msgchunk != NULL)
2339 last_msgchunk->sb_eol = TRUE;
2340}
2341
2342/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002343 * Display a screen line from previously displayed text at row "row".
2344 * Returns a pointer to the text for the next line (can be NULL).
2345 */
2346 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002347disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002348{
2349 msgchunk_T *mp = smp;
2350 char_u *p;
2351
2352 for (;;)
2353 {
2354 msg_row = row;
2355 msg_col = mp->sb_msg_col;
2356 p = mp->sb_text;
2357 if (*p == '\n') /* don't display the line break */
2358 ++p;
2359 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2360 if (mp->sb_eol || mp->sb_next == NULL)
2361 break;
2362 mp = mp->sb_next;
2363 }
2364 return mp->sb_next;
2365}
2366
2367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 * Output any postponed text for msg_puts_attr_len().
2369 */
2370 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002371t_puts(
2372 int *t_col,
2373 char_u *t_s,
2374 char_u *s,
2375 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
2377 /* output postponed text */
2378 msg_didout = TRUE; /* remember that line is not empty */
2379 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002380 msg_col += *t_col;
2381 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#ifdef FEAT_MBYTE
2383 /* If the string starts with a composing character don't increment the
2384 * column position for it. */
2385 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2386 --msg_col;
2387#endif
2388 if (msg_col >= Columns)
2389 {
2390 msg_col = 0;
2391 ++msg_row;
2392 }
2393}
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395/*
2396 * Returns TRUE when messages should be printed with mch_errmsg().
2397 * This is used when there is no valid screen, so we can see error messages.
2398 * If termcap is not active, we may be writing in an alternate console
2399 * window, cursor positioning may not work correctly (window size may be
2400 * different, e.g. for Win32 console) or we just don't know where the
2401 * cursor is.
2402 */
2403 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002404msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 return (!msg_check_screen()
2407#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2408 || !termcap_active
2409#endif
2410 || (swapping_screen() && !termcap_active)
2411 );
2412}
2413
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002414/*
2415 * Print a message when there is no valid screen.
2416 */
2417 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002418msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002419{
2420 char_u *s = str;
2421 char_u buf[4];
2422 char_u *p;
2423
2424#ifdef WIN3264
2425 if (!(silent_mode && p_verbose == 0))
2426 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2427#endif
2428 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2429 {
2430 if (!(silent_mode && p_verbose == 0))
2431 {
2432 /* NL --> CR NL translation (for Unix, not for "--version") */
2433 /* NL --> CR translation (for Mac) */
2434 p = &buf[0];
2435 if (*s == '\n' && !info_message)
2436 *p++ = '\r';
2437#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2438 else
2439#endif
2440 *p++ = *s;
2441 *p = '\0';
2442 if (info_message) /* informative message, not an error */
2443 mch_msg((char *)buf);
2444 else
2445 mch_errmsg((char *)buf);
2446 }
2447
2448 /* primitive way to compute the current column */
2449#ifdef FEAT_RIGHTLEFT
2450 if (cmdmsg_rl)
2451 {
2452 if (*s == '\r' || *s == '\n')
2453 msg_col = Columns - 1;
2454 else
2455 --msg_col;
2456 }
2457 else
2458#endif
2459 {
2460 if (*s == '\r' || *s == '\n')
2461 msg_col = 0;
2462 else
2463 ++msg_col;
2464 }
2465 ++s;
2466 }
2467 msg_didout = TRUE; /* assume that line is not empty */
2468
2469#ifdef WIN3264
2470 if (!(silent_mode && p_verbose == 0))
2471 mch_settmode(TMODE_RAW);
2472#endif
2473}
2474
2475/*
2476 * Show the more-prompt and handle the user response.
2477 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002478 * When at hit-enter prompt "typed_char" is the already typed character,
2479 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002480 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2481 */
2482 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002483do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002484{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002485 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002486 int used_typed_char = typed_char;
2487 int oldState = State;
2488 int c;
2489#ifdef FEAT_CON_DIALOG
2490 int retval = FALSE;
2491#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002492 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002493 msgchunk_T *mp_last = NULL;
2494 msgchunk_T *mp;
2495 int i;
2496
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002497 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002498 * that case don't show another prompt. Also when at the hit-Enter prompt
2499 * and nothing was typed. */
2500 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002501 return FALSE;
2502 entered = TRUE;
2503
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002504 if (typed_char == 'G')
2505 {
2506 /* "g<": Find first line on the last page. */
2507 mp_last = msg_sb_start(last_msgchunk);
2508 for (i = 0; i < Rows - 2 && mp_last != NULL
2509 && mp_last->sb_prev != NULL; ++i)
2510 mp_last = msg_sb_start(mp_last->sb_prev);
2511 }
2512
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002513 State = ASKMORE;
2514#ifdef FEAT_MOUSE
2515 setmouse();
2516#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002517 if (typed_char == NUL)
2518 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 for (;;)
2520 {
2521 /*
2522 * Get a typed character directly from the user.
2523 */
2524 if (used_typed_char != NUL)
2525 {
2526 c = used_typed_char; /* was typed at hit-enter prompt */
2527 used_typed_char = NUL;
2528 }
2529 else
2530 c = get_keystroke();
2531
2532#if defined(FEAT_MENU) && defined(FEAT_GUI)
2533 if (c == K_MENU)
2534 {
2535 int idx = get_menu_index(current_menu, ASKMORE);
2536
2537 /* Used a menu. If it starts with CTRL-Y, it must
2538 * be a "Copy" for the clipboard. Otherwise
2539 * assume that we end */
2540 if (idx == MENU_INDEX_INVALID)
2541 continue;
2542 c = *current_menu->strings[idx];
2543 if (c != NUL && current_menu->strings[idx][1] != NUL)
2544 ins_typebuf(current_menu->strings[idx] + 1,
2545 current_menu->noremap[idx], 0, TRUE,
2546 current_menu->silent[idx]);
2547 }
2548#endif
2549
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002550 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002551 switch (c)
2552 {
2553 case BS: /* scroll one line back */
2554 case K_BS:
2555 case 'k':
2556 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002557 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002558 break;
2559
2560 case CAR: /* one extra line */
2561 case NL:
2562 case 'j':
2563 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002564 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002565 break;
2566
2567 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002568 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002569 break;
2570
2571 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002572 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002573 break;
2574
2575 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002576 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002577 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002578 break;
2579
2580 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002581 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002582 case K_PAGEDOWN:
2583 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002584 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002585 break;
2586
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002587 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002588 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002589 break;
2590
2591 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002592 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002593 lines_left = 999999;
2594 break;
2595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596 case ':': /* start new command line */
2597#ifdef FEAT_CON_DIALOG
2598 if (!confirm_msg_used)
2599#endif
2600 {
2601 /* Since got_int is set all typeahead will be flushed, but we
2602 * want to keep this ':', remember that in a special way. */
2603 typeahead_noflush(':');
2604 cmdline_row = Rows - 1; /* put ':' on this line */
2605 skip_redraw = TRUE; /* skip redraw once */
2606 need_wait_return = FALSE; /* don't wait in main() */
2607 }
2608 /*FALLTHROUGH*/
2609 case 'q': /* quit */
2610 case Ctrl_C:
2611 case ESC:
2612#ifdef FEAT_CON_DIALOG
2613 if (confirm_msg_used)
2614 {
2615 /* Jump to the choices of the dialog. */
2616 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002617 }
2618 else
2619#endif
2620 {
2621 got_int = TRUE;
2622 quit_more = TRUE;
2623 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002624 /* When there is some more output (wrapping line) display that
2625 * without another prompt. */
2626 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002627 break;
2628
2629#ifdef FEAT_CLIPBOARD
2630 case Ctrl_Y:
2631 /* Strange way to allow copying (yanking) a modeless
2632 * selection at the more prompt. Use CTRL-Y,
2633 * because the same is used in Cmdline-mode and at the
2634 * hit-enter prompt. However, scrolling one line up
2635 * might be expected... */
2636 if (clip_star.state == SELECT_DONE)
2637 clip_copy_modeless_selection(TRUE);
2638 continue;
2639#endif
2640 default: /* no valid response */
2641 msg_moremsg(TRUE);
2642 continue;
2643 }
2644
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002645 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002646 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002647 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002648 {
2649 /* go to start of last line */
2650 if (mp_last == NULL)
2651 mp = msg_sb_start(last_msgchunk);
2652 else if (mp_last->sb_prev != NULL)
2653 mp = msg_sb_start(mp_last->sb_prev);
2654 else
2655 mp = NULL;
2656
2657 /* go to start of line at top of the screen */
2658 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2659 ++i)
2660 mp = msg_sb_start(mp->sb_prev);
2661
2662 if (mp != NULL && mp->sb_prev != NULL)
2663 {
2664 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002665 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002666 {
2667 if (mp == NULL || mp->sb_prev == NULL)
2668 break;
2669 mp = msg_sb_start(mp->sb_prev);
2670 if (mp_last == NULL)
2671 mp_last = msg_sb_start(last_msgchunk);
2672 else
2673 mp_last = msg_sb_start(mp_last->sb_prev);
2674 }
2675
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002676 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002677 (int)Rows, NULL) == OK)
2678 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002679 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002680 (void)disp_sb_line(0, mp);
2681 }
2682 else
2683 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002684 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002685 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002686 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2687 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002688 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002689 ++msg_scrolled;
2690 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002691 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002692 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693 }
2694 }
2695 else
2696 {
2697 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002698 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002699 {
2700 /* scroll up, display line at bottom */
2701 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002702 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002703 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2704 (int)Columns, ' ', ' ', 0);
2705 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002706 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002707 }
2708 }
2709
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002710 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002711 {
2712 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002713 screen_fill((int)Rows - 1, (int)Rows, 0,
2714 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002715 msg_moremsg(FALSE);
2716 continue;
2717 }
2718
2719 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002720 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 }
2722
2723 break;
2724 }
2725
2726 /* clear the --more-- message */
2727 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2728 State = oldState;
2729#ifdef FEAT_MOUSE
2730 setmouse();
2731#endif
2732 if (quit_more)
2733 {
2734 msg_row = Rows - 1;
2735 msg_col = 0;
2736 }
2737#ifdef FEAT_RIGHTLEFT
2738 else if (cmdmsg_rl)
2739 msg_col = Columns - 1;
2740#endif
2741
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002742 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743#ifdef FEAT_CON_DIALOG
2744 return retval;
2745#else
2746 return FALSE;
2747#endif
2748}
2749
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2751
2752#ifdef mch_errmsg
2753# undef mch_errmsg
2754#endif
2755#ifdef mch_msg
2756# undef mch_msg
2757#endif
2758
2759/*
2760 * Give an error message. To be used when the screen hasn't been initialized
2761 * yet. When stderr can't be used, collect error messages until the GUI has
2762 * started and they can be displayed in a message box.
2763 */
2764 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002765mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 int len;
2768
2769#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2770 /* On Unix use stderr if it's a tty.
2771 * When not going to start the GUI also use stderr.
2772 * On Mac, when started from Finder, stderr is the console. */
2773 if (
2774# ifdef UNIX
2775# ifdef MACOS_X_UNIX
2776 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2777# else
2778 isatty(2)
2779# endif
2780# ifdef FEAT_GUI
2781 ||
2782# endif
2783# endif
2784# ifdef FEAT_GUI
2785 !(gui.in_use || gui.starting)
2786# endif
2787 )
2788 {
2789 fprintf(stderr, "%s", str);
2790 return;
2791 }
2792#endif
2793
2794 /* avoid a delay for a message that isn't there */
2795 emsg_on_display = FALSE;
2796
2797 len = (int)STRLEN(str) + 1;
2798 if (error_ga.ga_growsize == 0)
2799 {
2800 error_ga.ga_growsize = 80;
2801 error_ga.ga_itemsize = 1;
2802 }
2803 if (ga_grow(&error_ga, len) == OK)
2804 {
2805 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2806 (char_u *)str, len);
2807#ifdef UNIX
2808 /* remove CR characters, they are displayed */
2809 {
2810 char_u *p;
2811
2812 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2813 for (;;)
2814 {
2815 p = vim_strchr(p, '\r');
2816 if (p == NULL)
2817 break;
2818 *p = ' ';
2819 }
2820 }
2821#endif
2822 --len; /* don't count the NUL at the end */
2823 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825}
2826
2827/*
2828 * Give a message. To be used when the screen hasn't been initialized yet.
2829 * When there is no tty, collect messages until the GUI has started and they
2830 * can be displayed in a message box.
2831 */
2832 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002833mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2836 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2837 * uses mch_errmsg() when started from the desktop.
2838 * When not going to start the GUI also use stdout.
2839 * On Mac, when started from Finder, stderr is the console. */
2840 if (
2841# ifdef UNIX
2842# ifdef MACOS_X_UNIX
2843 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2844# else
2845 isatty(2)
2846# endif
2847# ifdef FEAT_GUI
2848 ||
2849# endif
2850# endif
2851# ifdef FEAT_GUI
2852 !(gui.in_use || gui.starting)
2853# endif
2854 )
2855 {
2856 printf("%s", str);
2857 return;
2858 }
2859# endif
2860 mch_errmsg(str);
2861}
2862#endif /* USE_MCH_ERRMSG */
2863
2864/*
2865 * Put a character on the screen at the current message position and advance
2866 * to the next position. Only for printable ASCII!
2867 */
2868 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002869msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
2871 msg_didout = TRUE; /* remember that line is not empty */
2872 screen_putchar(c, msg_row, msg_col, attr);
2873#ifdef FEAT_RIGHTLEFT
2874 if (cmdmsg_rl)
2875 {
2876 if (--msg_col == 0)
2877 {
2878 msg_col = Columns;
2879 ++msg_row;
2880 }
2881 }
2882 else
2883#endif
2884 {
2885 if (++msg_col >= Columns)
2886 {
2887 msg_col = 0;
2888 ++msg_row;
2889 }
2890 }
2891}
2892
2893 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002894msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 int attr;
2897 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
2899 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002902 screen_puts((char_u *)
2903 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2904 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905}
2906
2907/*
2908 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2909 * exmode_active.
2910 */
2911 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002912repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 if (State == ASKMORE)
2915 {
2916 msg_moremsg(TRUE); /* display --more-- message again */
2917 msg_row = Rows - 1;
2918 }
2919#ifdef FEAT_CON_DIALOG
2920 else if (State == CONFIRM)
2921 {
2922 display_confirm_msg(); /* display ":confirm" message again */
2923 msg_row = Rows - 1;
2924 }
2925#endif
2926 else if (State == EXTERNCMD)
2927 {
2928 windgoto(msg_row, msg_col); /* put cursor back */
2929 }
2930 else if (State == HITRETURN || State == SETWSIZE)
2931 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002932 if (msg_row == Rows - 1)
2933 {
2934 /* Avoid drawing the "hit-enter" prompt below the previous one,
2935 * overwrite it. Esp. useful when regaining focus and a
2936 * FocusGained autocmd exists but didn't draw anything. */
2937 msg_didout = FALSE;
2938 msg_col = 0;
2939 msg_clr_eos();
2940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 hit_return_msg();
2942 msg_row = Rows - 1;
2943 }
2944}
2945
2946/*
2947 * msg_check_screen - check if the screen is initialized.
2948 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2949 * While starting the GUI the terminal codes will be set for the GUI, but the
2950 * output goes to the terminal. Don't use the terminal codes then.
2951 */
2952 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002953msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 if (!full_screen || !screen_valid(FALSE))
2956 return FALSE;
2957
2958 if (msg_row >= Rows)
2959 msg_row = Rows - 1;
2960 if (msg_col >= Columns)
2961 msg_col = Columns - 1;
2962 return TRUE;
2963}
2964
2965/*
2966 * Clear from current message position to end of screen.
2967 * Skip this when ":silent" was used, no need to clear for redirection.
2968 */
2969 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002970msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 if (msg_silent == 0)
2973 msg_clr_eos_force();
2974}
2975
2976/*
2977 * Clear from current message position to end of screen.
2978 * Note: msg_col is not updated, so we remember the end of the message
2979 * for msg_check().
2980 */
2981 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002982msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 if (msg_use_printf())
2985 {
2986 if (full_screen) /* only when termcap codes are valid */
2987 {
2988 if (*T_CD)
2989 out_str(T_CD); /* clear to end of display */
2990 else if (*T_CE)
2991 out_str(T_CE); /* clear to end of line */
2992 }
2993 }
2994 else
2995 {
2996#ifdef FEAT_RIGHTLEFT
2997 if (cmdmsg_rl)
2998 {
2999 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3000 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3001 }
3002 else
3003#endif
3004 {
3005 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3006 ' ', ' ', 0);
3007 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3008 }
3009 }
3010}
3011
3012/*
3013 * Clear the command line.
3014 */
3015 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003016msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 msg_row = cmdline_row;
3019 msg_col = 0;
3020 msg_clr_eos_force();
3021}
3022
3023/*
3024 * end putting a message on the screen
3025 * call wait_return if the message does not fit in the available space
3026 * return TRUE if wait_return not called.
3027 */
3028 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003029msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003032 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 * or the ruler option is set and we run into it,
3034 * we have to redraw the window.
3035 * Do not do this if we are abandoning the file or editing the command line.
3036 */
3037 if (!exiting && need_wait_return && !(State & CMDLINE))
3038 {
3039 wait_return(FALSE);
3040 return FALSE;
3041 }
3042 out_flush();
3043 return TRUE;
3044}
3045
3046/*
3047 * If the written message runs into the shown command or ruler, we have to
3048 * wait for hit-return and redraw the window later.
3049 */
3050 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003051msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 if (msg_row == Rows - 1 && msg_col >= sc_col)
3054 {
3055 need_wait_return = TRUE;
3056 redraw_cmdline = TRUE;
3057 }
3058}
3059
3060/*
3061 * May write a string to the redirection file.
3062 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3063 */
3064 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003065redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 char_u *s = str;
3068 static int cur_col = 0;
3069
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003070 /* Don't do anything for displaying prompts and the like. */
3071 if (redir_off)
3072 return;
3073
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003074 /* If 'verbosefile' is set prepare for writing in that file. */
3075 if (*p_vfile != NUL && verbose_fd == NULL)
3076 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003077
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003078 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 /* If the string doesn't start with CR or NL, go to msg_col */
3081 if (*s != '\n' && *s != '\r')
3082 {
3083 while (cur_col < msg_col)
3084 {
3085#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003086 if (redir_execute)
3087 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003088 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003090 else if (redir_vname)
3091 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003094 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003096 if (verbose_fd != NULL)
3097 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 ++cur_col;
3099 }
3100 }
3101
3102#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003103 if (redir_execute)
3104 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003105 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003107 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003108 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
3110
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003111 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3113 {
3114#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003115 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003117 if (redir_fd != NULL)
3118 putc(*s, redir_fd);
3119 if (verbose_fd != NULL)
3120 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 if (*s == '\r' || *s == '\n')
3122 cur_col = 0;
3123 else if (*s == '\t')
3124 cur_col += (8 - cur_col % 8);
3125 else
3126 ++cur_col;
3127 ++s;
3128 }
3129
3130 if (msg_silent != 0) /* should update msg_col */
3131 msg_col = cur_col;
3132 }
3133}
3134
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003135 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003136redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003137{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003138 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003139#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003140 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003141#endif
3142 ;
3143}
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003146 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003147 * Must always be called paired with verbose_leave()!
3148 */
3149 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003150verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003151{
3152 if (*p_vfile != NUL)
3153 ++msg_silent;
3154}
3155
3156/*
3157 * After giving verbose message.
3158 * Must always be called paired with verbose_enter()!
3159 */
3160 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003161verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003162{
3163 if (*p_vfile != NUL)
3164 if (--msg_silent < 0)
3165 msg_silent = 0;
3166}
3167
3168/*
3169 * Like verbose_enter() and set msg_scroll when displaying the message.
3170 */
3171 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003172verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003173{
3174 if (*p_vfile != NUL)
3175 ++msg_silent;
3176 else
3177 /* always scroll up, don't overwrite */
3178 msg_scroll = TRUE;
3179}
3180
3181/*
3182 * Like verbose_leave() and set cmdline_row when displaying the message.
3183 */
3184 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003185verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003186{
3187 if (*p_vfile != NUL)
3188 {
3189 if (--msg_silent < 0)
3190 msg_silent = 0;
3191 }
3192 else
3193 cmdline_row = msg_row;
3194}
3195
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003196/*
3197 * Called when 'verbosefile' is set: stop writing to the file.
3198 */
3199 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003200verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003201{
3202 if (verbose_fd != NULL)
3203 {
3204 fclose(verbose_fd);
3205 verbose_fd = NULL;
3206 }
3207 verbose_did_open = FALSE;
3208}
3209
3210/*
3211 * Open the file 'verbosefile'.
3212 * Return FAIL or OK.
3213 */
3214 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003215verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003216{
3217 if (verbose_fd == NULL && !verbose_did_open)
3218 {
3219 /* Only give the error message once. */
3220 verbose_did_open = TRUE;
3221
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003222 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003223 if (verbose_fd == NULL)
3224 {
3225 EMSG2(_(e_notopen), p_vfile);
3226 return FAIL;
3227 }
3228 }
3229 return OK;
3230}
3231
3232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 * Give a warning message (for searching).
3234 * Use 'w' highlighting and may repeat the message after redrawing
3235 */
3236 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003237give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 /* Don't do this for ":silent". */
3240 if (msg_silent != 0)
3241 return;
3242
3243 /* Don't want a hit-enter prompt here. */
3244 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#ifdef FEAT_EVAL
3247 set_vim_var_string(VV_WARNINGMSG, message, -1);
3248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 vim_free(keep_msg);
3250 keep_msg = NULL;
3251 if (hl)
3252 keep_msg_attr = hl_attr(HLF_W);
3253 else
3254 keep_msg_attr = 0;
3255 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003256 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 msg_didout = FALSE; /* overwrite this message */
3258 msg_nowait = TRUE; /* don't wait for this message */
3259 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 --no_wait_return;
3262}
3263
3264/*
3265 * Advance msg cursor to column "col".
3266 */
3267 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003268msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 if (msg_silent != 0) /* nothing to advance to */
3271 {
3272 msg_col = col; /* for redirection, may fill it up later */
3273 return;
3274 }
3275 if (col >= Columns) /* not enough room */
3276 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003277#ifdef FEAT_RIGHTLEFT
3278 if (cmdmsg_rl)
3279 while (msg_col > Columns - col)
3280 msg_putchar(' ');
3281 else
3282#endif
3283 while (msg_col < col)
3284 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285}
3286
3287#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3288/*
3289 * Used for "confirm()" function, and the :confirm command prefix.
3290 * Versions which haven't got flexible dialogs yet, and console
3291 * versions, get this generic handler which uses the command line.
3292 *
3293 * type = one of:
3294 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3295 * title = title string (can be NULL for default)
3296 * (neither used in console dialogs at the moment)
3297 *
3298 * Format of the "buttons" string:
3299 * "Button1Name\nButton2Name\nButton3Name"
3300 * The first button should normally be the default/accept
3301 * The second button should be the 'Cancel' button
3302 * Other buttons- use your imagination!
3303 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3304 * different letter.
3305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003307do_dialog(
3308 int type UNUSED,
3309 char_u *title UNUSED,
3310 char_u *message,
3311 char_u *buttons,
3312 int dfltbutton,
3313 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003314 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003315 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003316 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 int oldState;
3319 int retval = 0;
3320 char_u *hotkeys;
3321 int c;
3322 int i;
3323
3324#ifndef NO_CONSOLE
3325 /* Don't output anything in silent mode ("ex -s") */
3326 if (silent_mode)
3327 return dfltbutton; /* return default option */
3328#endif
3329
3330#ifdef FEAT_GUI_DIALOG
3331 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3332 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3333 {
3334 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003335 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003336 /* avoid a hit-enter prompt without clearing the cmdline */
3337 need_wait_return = FALSE;
3338 emsg_on_display = FALSE;
3339 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
3341 /* Flush output to avoid that further messages and redrawing is done
3342 * in the wrong order. */
3343 out_flush();
3344 gui_mch_update();
3345
3346 return c;
3347 }
3348#endif
3349
3350 oldState = State;
3351 State = CONFIRM;
3352#ifdef FEAT_MOUSE
3353 setmouse();
3354#endif
3355
3356 /*
3357 * Since we wait for a keypress, don't make the
3358 * user press RETURN as well afterwards.
3359 */
3360 ++no_wait_return;
3361 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3362
3363 if (hotkeys != NULL)
3364 {
3365 for (;;)
3366 {
3367 /* Get a typed character directly from the user. */
3368 c = get_keystroke();
3369 switch (c)
3370 {
3371 case CAR: /* User accepts default option */
3372 case NL:
3373 retval = dfltbutton;
3374 break;
3375 case Ctrl_C: /* User aborts/cancels */
3376 case ESC:
3377 retval = 0;
3378 break;
3379 default: /* Could be a hotkey? */
3380 if (c < 0) /* special keys are ignored here */
3381 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003382 if (c == ':' && ex_cmd)
3383 {
3384 retval = dfltbutton;
3385 ins_char_typebuf(':');
3386 break;
3387 }
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 /* Make the character lowercase, as chars in "hotkeys" are. */
3390 c = MB_TOLOWER(c);
3391 retval = 1;
3392 for (i = 0; hotkeys[i]; ++i)
3393 {
3394#ifdef FEAT_MBYTE
3395 if (has_mbyte)
3396 {
3397 if ((*mb_ptr2char)(hotkeys + i) == c)
3398 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003399 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401 else
3402#endif
3403 if (hotkeys[i] == c)
3404 break;
3405 ++retval;
3406 }
3407 if (hotkeys[i])
3408 break;
3409 /* No hotkey match, so keep waiting */
3410 continue;
3411 }
3412 break;
3413 }
3414
3415 vim_free(hotkeys);
3416 }
3417
3418 State = oldState;
3419#ifdef FEAT_MOUSE
3420 setmouse();
3421#endif
3422 --no_wait_return;
3423 msg_end_prompt();
3424
3425 return retval;
3426}
3427
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003428static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430/*
3431 * Copy one character from "*from" to "*to", taking care of multi-byte
3432 * characters. Return the length of the character in bytes.
3433 */
3434 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003435copy_char(
3436 char_u *from,
3437 char_u *to,
3438 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440#ifdef FEAT_MBYTE
3441 int len;
3442 int c;
3443
3444 if (has_mbyte)
3445 {
3446 if (lowercase)
3447 {
3448 c = MB_TOLOWER((*mb_ptr2char)(from));
3449 return (*mb_char2bytes)(c, to);
3450 }
3451 else
3452 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003453 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 mch_memmove(to, from, (size_t)len);
3455 return len;
3456 }
3457 }
3458 else
3459#endif
3460 {
3461 if (lowercase)
3462 *to = (char_u)TOLOWER_LOC(*from);
3463 else
3464 *to = *from;
3465 return 1;
3466 }
3467}
3468
3469/*
3470 * Format the dialog string, and display it at the bottom of
3471 * the screen. Return a string of hotkey chars (if defined) for
3472 * each 'button'. If a button has no hotkey defined, the first character of
3473 * the button is used.
3474 * The hotkeys can be multi-byte characters, but without combining chars.
3475 *
3476 * Returns an allocated string with hotkeys, or NULL for error.
3477 */
3478 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003479msg_show_console_dialog(
3480 char_u *message,
3481 char_u *buttons,
3482 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 int len = 0;
3485#ifdef FEAT_MBYTE
3486# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3487#else
3488# define HOTK_LEN 1
3489#endif
3490 int lenhotkey = HOTK_LEN; /* count first button */
3491 char_u *hotk = NULL;
3492 char_u *msgp = NULL;
3493 char_u *hotkp = NULL;
3494 char_u *r;
3495 int copy;
3496#define HAS_HOTKEY_LEN 30
3497 char_u has_hotkey[HAS_HOTKEY_LEN];
3498 int first_hotkey = FALSE; /* first char of button is hotkey */
3499 int idx;
3500
3501 has_hotkey[0] = FALSE;
3502
3503 /*
3504 * First loop: compute the size of memory to allocate.
3505 * Second loop: copy to the allocated memory.
3506 */
3507 for (copy = 0; copy <= 1; ++copy)
3508 {
3509 r = buttons;
3510 idx = 0;
3511 while (*r)
3512 {
3513 if (*r == DLG_BUTTON_SEP)
3514 {
3515 if (copy)
3516 {
3517 *msgp++ = ',';
3518 *msgp++ = ' '; /* '\n' -> ', ' */
3519
3520 /* advance to next hotkey and set default hotkey */
3521#ifdef FEAT_MBYTE
3522 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003523 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 else
3525#endif
3526 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003527 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (dfltbutton)
3529 --dfltbutton;
3530
3531 /* If no hotkey is specified first char is used. */
3532 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3533 first_hotkey = TRUE;
3534 }
3535 else
3536 {
3537 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3538 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3539 if (idx < HAS_HOTKEY_LEN - 1)
3540 has_hotkey[++idx] = FALSE;
3541 }
3542 }
3543 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3544 {
3545 if (*r == DLG_HOTKEY_CHAR)
3546 ++r;
3547 first_hotkey = FALSE;
3548 if (copy)
3549 {
3550 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3551 *msgp++ = *r;
3552 else
3553 {
3554 /* '&a' -> '[a]' */
3555 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3556 msgp += copy_char(r, msgp, FALSE);
3557 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3558
3559 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003560 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562 }
3563 else
3564 {
3565 ++len; /* '&a' -> '[a]' */
3566 if (idx < HAS_HOTKEY_LEN - 1)
3567 has_hotkey[idx] = TRUE;
3568 }
3569 }
3570 else
3571 {
3572 /* everything else copy literally */
3573 if (copy)
3574 msgp += copy_char(r, msgp, FALSE);
3575 }
3576
3577 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003578 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580
3581 if (copy)
3582 {
3583 *msgp++ = ':';
3584 *msgp++ = ' ';
3585 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587 else
3588 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003589 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003590 + 2 /* for the NL's */
3591 + STRLEN(buttons)
3592 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003593 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
3595 /* If no hotkey is specified first char is used. */
3596 if (!has_hotkey[0])
3597 {
3598 first_hotkey = TRUE;
3599 len += 2; /* "x" -> "[x]" */
3600 }
3601
3602 /*
3603 * Now allocate and load the strings
3604 */
3605 vim_free(confirm_msg);
3606 confirm_msg = alloc(len);
3607 if (confirm_msg == NULL)
3608 return NULL;
3609 *confirm_msg = NUL;
3610 hotk = alloc(lenhotkey);
3611 if (hotk == NULL)
3612 return NULL;
3613
3614 *confirm_msg = '\n';
3615 STRCPY(confirm_msg + 1, message);
3616
3617 msgp = confirm_msg + 1 + STRLEN(message);
3618 hotkp = hotk;
3619
Bram Moolenaar6a516062007-07-05 08:11:42 +00003620 /* Define first default hotkey. Keep the hotkey string NUL
3621 * terminated to avoid reading past the end. */
3622 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 /* Remember where the choices start, displaying starts here when
3625 * "hotkp" typed at the more prompt. */
3626 confirm_msg_tail = msgp;
3627 *msgp++ = '\n';
3628 }
3629 }
3630
3631 display_confirm_msg();
3632 return hotk;
3633}
3634
3635/*
3636 * Display the ":confirm" message. Also called when screen resized.
3637 */
3638 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003639display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
3641 /* avoid that 'q' at the more prompt truncates the message here */
3642 ++confirm_msg_used;
3643 if (confirm_msg != NULL)
3644 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3645 --confirm_msg_used;
3646}
3647
3648#endif /* FEAT_CON_DIALOG */
3649
3650#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3651
3652 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003653vim_dialog_yesno(
3654 int type,
3655 char_u *title,
3656 char_u *message,
3657 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 if (do_dialog(type,
3660 title == NULL ? (char_u *)_("Question") : title,
3661 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003662 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return VIM_YES;
3664 return VIM_NO;
3665}
3666
3667 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003668vim_dialog_yesnocancel(
3669 int type,
3670 char_u *title,
3671 char_u *message,
3672 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 switch (do_dialog(type,
3675 title == NULL ? (char_u *)_("Question") : title,
3676 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003677 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
3679 case 1: return VIM_YES;
3680 case 2: return VIM_NO;
3681 }
3682 return VIM_CANCEL;
3683}
3684
3685 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003686vim_dialog_yesnoallcancel(
3687 int type,
3688 char_u *title,
3689 char_u *message,
3690 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
3692 switch (do_dialog(type,
3693 title == NULL ? (char_u *)"Question" : title,
3694 message,
3695 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003696 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 case 1: return VIM_YES;
3699 case 2: return VIM_NO;
3700 case 3: return VIM_ALL;
3701 case 4: return VIM_DISCARDALL;
3702 }
3703 return VIM_CANCEL;
3704}
3705
3706#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3707
3708#if defined(FEAT_BROWSE) || defined(PROTO)
3709/*
3710 * Generic browse function. Calls gui_mch_browse() when possible.
3711 * Later this may pop-up a non-GUI file selector (external command?).
3712 */
3713 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003714do_browse(
3715 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3716 char_u *title, /* title for the window */
3717 char_u *dflt, /* default file name (may include directory) */
3718 char_u *ext, /* extension added */
3719 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003721 char_u *filter, /* file name filter */
3722 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724 char_u *fname;
3725 static char_u *last_dir = NULL; /* last used directory */
3726 char_u *tofree = NULL;
3727 int save_browse = cmdmod.browse;
3728
3729 /* Must turn off browse to avoid that autocommands will get the
3730 * flag too! */
3731 cmdmod.browse = FALSE;
3732
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003733 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003735 if (flags & BROWSE_DIR)
3736 title = (char_u *)_("Select Directory dialog");
3737 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 title = (char_u *)_("Save File dialog");
3739 else
3740 title = (char_u *)_("Open File dialog");
3741 }
3742
3743 /* When no directory specified, use default file name, default dir, buffer
3744 * dir, last dir or current dir */
3745 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3746 {
3747 if (mch_isdir(dflt)) /* default file name is a directory */
3748 {
3749 initdir = dflt;
3750 dflt = NULL;
3751 }
3752 else if (gettail(dflt) != dflt) /* default file name includes a path */
3753 {
3754 tofree = vim_strsave(dflt);
3755 if (tofree != NULL)
3756 {
3757 initdir = tofree;
3758 *gettail(initdir) = NUL;
3759 dflt = gettail(dflt);
3760 }
3761 }
3762 }
3763
3764 if (initdir == NULL || *initdir == NUL)
3765 {
3766 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003767 if (STRCMP(p_bsdir, "last") != 0
3768 && STRCMP(p_bsdir, "buffer") != 0
3769 && STRCMP(p_bsdir, "current") != 0
3770 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 initdir = p_bsdir;
3772 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003773 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 && buf != NULL && buf->b_ffname != NULL)
3775 {
3776 if (dflt == NULL || *dflt == NUL)
3777 dflt = gettail(curbuf->b_ffname);
3778 tofree = vim_strsave(curbuf->b_ffname);
3779 if (tofree != NULL)
3780 {
3781 initdir = tofree;
3782 *gettail(initdir) = NUL;
3783 }
3784 }
3785 /* When 'browsedir' is "last", use dir from last browse */
3786 else if (*p_bsdir == 'l')
3787 initdir = last_dir;
3788 /* When 'browsedir is "current", use current directory. This is the
3789 * default already, leave initdir empty. */
3790 }
3791
3792# ifdef FEAT_GUI
3793 if (gui.in_use) /* when this changes, also adjust f_has()! */
3794 {
3795 if (filter == NULL
3796# ifdef FEAT_EVAL
3797 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3798 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3799# endif
3800 )
3801 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003802 if (flags & BROWSE_DIR)
3803 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003804# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003805 /* For systems that have a directory dialog. */
3806 fname = gui_mch_browsedir(title, initdir);
3807# else
3808 /* Generic solution for selecting a directory: select a file and
3809 * remove the file name. */
3810 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3811# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003812# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003813 /* Win32 adds a dummy file name, others return an arbitrary file
3814 * name. GTK+ 2 returns only the directory, */
3815 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3816 {
3817 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003818 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003819
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003820 if (tail == fname)
3821 *tail++ = '.'; /* use current dir */
3822 *tail = NUL;
3823 }
3824# endif
3825 }
3826 else
3827 fname = gui_mch_browse(flags & BROWSE_SAVE,
3828 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 /* We hang around in the dialog for a while, the user might do some
3831 * things to our files. The Win32 dialog allows deleting or renaming
3832 * a file, check timestamps. */
3833 need_check_timestamps = TRUE;
3834 did_check_timestamps = FALSE;
3835 }
3836 else
3837# endif
3838 {
3839 /* TODO: non-GUI file selector here */
3840 EMSG(_("E338: Sorry, no file browser in console mode"));
3841 fname = NULL;
3842 }
3843
3844 /* keep the directory for next time */
3845 if (fname != NULL)
3846 {
3847 vim_free(last_dir);
3848 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003849 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
3851 *gettail(last_dir) = NUL;
3852 if (*last_dir == NUL)
3853 {
3854 /* filename only returned, must be in current dir */
3855 vim_free(last_dir);
3856 last_dir = alloc(MAXPATHL);
3857 if (last_dir != NULL)
3858 mch_dirname(last_dir, MAXPATHL);
3859 }
3860 }
3861 }
3862
3863 vim_free(tofree);
3864 cmdmod.browse = save_browse;
3865
3866 return fname;
3867}
3868#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003869
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003870#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871static char *e_printf = N_("E766: Insufficient arguments for printf()");
3872
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003873static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003874static char *tv_str(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003875# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003876static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003877# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003878
3879/*
3880 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3881 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003882 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003883tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003884{
3885 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003886 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003887 int err = FALSE;
3888
3889 if (tvs[idx].v_type == VAR_UNKNOWN)
3890 EMSG(_(e_printf));
3891 else
3892 {
3893 ++*idxp;
3894 n = get_tv_number_chk(&tvs[idx], &err);
3895 if (err)
3896 n = 0;
3897 }
3898 return n;
3899}
3900
3901/*
3902 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003903 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 */
3905 static char *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003906tv_str(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003907{
3908 int idx = *idxp - 1;
3909 char *s = NULL;
3910
3911 if (tvs[idx].v_type == VAR_UNKNOWN)
3912 EMSG(_(e_printf));
3913 else
3914 {
3915 ++*idxp;
3916 s = (char *)get_tv_string_chk(&tvs[idx]);
3917 }
3918 return s;
3919}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003920
3921# ifdef FEAT_FLOAT
3922/*
3923 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3924 */
3925 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003926tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003927{
3928 int idx = *idxp - 1;
3929 double f = 0;
3930
3931 if (tvs[idx].v_type == VAR_UNKNOWN)
3932 EMSG(_(e_printf));
3933 else
3934 {
3935 ++*idxp;
3936 if (tvs[idx].v_type == VAR_FLOAT)
3937 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003938 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003939 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003940 else
3941 EMSG(_("E807: Expected Float argument for printf()"));
3942 }
3943 return f;
3944}
3945# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003946#endif
3947
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003948/*
3949 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003950 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003951 * consistency.
3952 *
3953 * This code is based on snprintf.c - a portable implementation of snprintf
3954 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003955 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003956 * The original code, including useful comments, can be found here:
3957 * http://www.ijs.si/software/snprintf/
3958 *
3959 * This snprintf() only supports the following conversion specifiers:
3960 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3961 * with flags: '-', '+', ' ', '0' and '#'.
3962 * An asterisk is supported for field width as well as precision.
3963 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003964 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3965 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003966 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3967 * 'll' (long long int) is not supported.
3968 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003969 * The locale is not used, the string is used as a byte string. This is only
3970 * relevant for double-byte encodings where the second byte may be '%'.
3971 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003972 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3973 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003974 *
3975 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003976 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003977 * is greater or equal to "str_m", not all characters from the result
3978 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3979 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003980 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003981 */
3982
3983/*
3984 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003985 *
3986 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003987 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003988 */
3989
3990/* When generating prototypes all of this is skipped, cproto doesn't
3991 * understand this. */
3992#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02003993
Bram Moolenaara800b422010-06-27 01:15:55 +02003994/* Like vim_vsnprintf() but append to the string. */
3995 int
3996vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
3997{
3998 va_list ap;
3999 int str_l;
4000 size_t len = STRLEN(str);
4001 size_t space;
4002
4003 if (str_m <= len)
4004 space = 0;
4005 else
4006 space = str_m - len;
4007 va_start(ap, fmt);
4008 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4009 va_end(ap);
4010 return str_l;
4011}
Bram Moolenaara800b422010-06-27 01:15:55 +02004012
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004013 int
4014vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4015{
4016 va_list ap;
4017 int str_l;
4018
4019 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004020 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004021 va_end(ap);
4022 return str_l;
4023}
4024
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004025 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004026vim_vsnprintf(
4027 char *str,
4028 size_t str_m,
4029 char *fmt,
4030 va_list ap,
4031 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004032{
4033 size_t str_l = 0;
4034 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004035 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004036
4037 if (p == NULL)
4038 p = "";
4039 while (*p != NUL)
4040 {
4041 if (*p != '%')
4042 {
4043 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004044 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004045
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004046 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004047 if (str_l < str_m)
4048 {
4049 size_t avail = str_m - str_l;
4050
4051 mch_memmove(str + str_l, p, n > avail ? avail : n);
4052 }
4053 p += n;
4054 str_l += n;
4055 }
4056 else
4057 {
4058 size_t min_field_width = 0, precision = 0;
4059 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4060 int alternate_form = 0, force_sign = 0;
4061
4062 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4063 * ignored. */
4064 int space_for_positive = 1;
4065
4066 /* allowed values: \0, h, l, L */
4067 char length_modifier = '\0';
4068
4069 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004070# ifdef FEAT_FLOAT
4071# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004072 * That sounds reasonable to use as the maximum
4073 * printable. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004074# else
4075# define TMP_LEN 32
4076# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004077 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004078
4079 /* string address in case of string argument */
4080 char *str_arg;
4081
4082 /* natural field width of arg without padding and sign */
4083 size_t str_arg_l;
4084
4085 /* unsigned char argument value - only defined for c conversion.
4086 * N.B. standard explicitly states the char argument for the c
4087 * conversion is unsigned */
4088 unsigned char uchar_arg;
4089
4090 /* number of zeros to be inserted for numeric conversions as
4091 * required by the precision or minimal field width */
4092 size_t number_of_zeros_to_pad = 0;
4093
4094 /* index into tmp where zero padding is to be inserted */
4095 size_t zero_padding_insertion_ind = 0;
4096
4097 /* current conversion specifier character */
4098 char fmt_spec = '\0';
4099
4100 str_arg = NULL;
4101 p++; /* skip '%' */
4102
4103 /* parse flags */
4104 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4105 || *p == '#' || *p == '\'')
4106 {
4107 switch (*p)
4108 {
4109 case '0': zero_padding = 1; break;
4110 case '-': justify_left = 1; break;
4111 case '+': force_sign = 1; space_for_positive = 0; break;
4112 case ' ': force_sign = 1;
4113 /* If both the ' ' and '+' flags appear, the ' '
4114 * flag should be ignored */
4115 break;
4116 case '#': alternate_form = 1; break;
4117 case '\'': break;
4118 }
4119 p++;
4120 }
4121 /* If the '0' and '-' flags both appear, the '0' flag should be
4122 * ignored. */
4123
4124 /* parse field width */
4125 if (*p == '*')
4126 {
4127 int j;
4128
4129 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004132 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133# endif
4134 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004135 if (j >= 0)
4136 min_field_width = j;
4137 else
4138 {
4139 min_field_width = -j;
4140 justify_left = 1;
4141 }
4142 }
4143 else if (VIM_ISDIGIT((int)(*p)))
4144 {
4145 /* size_t could be wider than unsigned int; make sure we treat
4146 * argument like common implementations do */
4147 unsigned int uj = *p++ - '0';
4148
4149 while (VIM_ISDIGIT((int)(*p)))
4150 uj = 10 * uj + (unsigned int)(*p++ - '0');
4151 min_field_width = uj;
4152 }
4153
4154 /* parse precision */
4155 if (*p == '.')
4156 {
4157 p++;
4158 precision_specified = 1;
4159 if (*p == '*')
4160 {
4161 int j;
4162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004163 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004164# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004165 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004166# endif
4167 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004168 p++;
4169 if (j >= 0)
4170 precision = j;
4171 else
4172 {
4173 precision_specified = 0;
4174 precision = 0;
4175 }
4176 }
4177 else if (VIM_ISDIGIT((int)(*p)))
4178 {
4179 /* size_t could be wider than unsigned int; make sure we
4180 * treat argument like common implementations do */
4181 unsigned int uj = *p++ - '0';
4182
4183 while (VIM_ISDIGIT((int)(*p)))
4184 uj = 10 * uj + (unsigned int)(*p++ - '0');
4185 precision = uj;
4186 }
4187 }
4188
4189 /* parse 'h', 'l' and 'll' length modifiers */
4190 if (*p == 'h' || *p == 'l')
4191 {
4192 length_modifier = *p;
4193 p++;
4194 if (length_modifier == 'l' && *p == 'l')
4195 {
4196 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004197# ifdef FEAT_NUM64
4198 length_modifier = 'L';
4199# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004200 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004201# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004202 p++;
4203 }
4204 }
4205 fmt_spec = *p;
4206
4207 /* common synonyms: */
4208 switch (fmt_spec)
4209 {
4210 case 'i': fmt_spec = 'd'; break;
4211 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4212 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4213 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004214 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004215 default: break;
4216 }
4217
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004218# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4219 switch (fmt_spec)
4220 {
4221 case 'd': case 'u': case 'o': case 'x': case 'X':
4222 if (tvs != NULL && length_modifier == '\0')
4223 length_modifier = 'L';
4224 }
4225# endif
4226
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004227 /* get parameter value, do initial processing */
4228 switch (fmt_spec)
4229 {
4230 /* '%' and 'c' behave similar to 's' regarding flags and field
4231 * widths */
4232 case '%':
4233 case 'c':
4234 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004235 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004236 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004237 str_arg_l = 1;
4238 switch (fmt_spec)
4239 {
4240 case '%':
4241 str_arg = p;
4242 break;
4243
4244 case 'c':
4245 {
4246 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004247
4248 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004250 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251# endif
4252 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004253 /* standard demands unsigned char */
4254 uchar_arg = (unsigned char)j;
4255 str_arg = (char *)&uchar_arg;
4256 break;
4257 }
4258
4259 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004260 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004263 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264# endif
4265 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004266 if (str_arg == NULL)
4267 {
4268 str_arg = "[NULL]";
4269 str_arg_l = 6;
4270 }
4271 /* make sure not to address string beyond the specified
4272 * precision !!! */
4273 else if (!precision_specified)
4274 str_arg_l = strlen(str_arg);
4275 /* truncate string if necessary as requested by precision */
4276 else if (precision == 0)
4277 str_arg_l = 0;
4278 else
4279 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004280 /* Don't put the #if inside memchr(), it can be a
4281 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004282# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004283 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004284# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004285 /* memchr on HP does not like n > 2^31 !!! */
4286 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004287 precision <= (size_t)0x7fffffffL ? precision
4288 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004289# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004290 str_arg_l = (q == NULL) ? precision
4291 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004292 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004293# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004294 if (fmt_spec == 'S')
4295 {
4296 if (min_field_width != 0)
4297 min_field_width += STRLEN(str_arg)
4298 - mb_string2cells((char_u *)str_arg, -1);
4299 if (precision)
4300 {
4301 char_u *p1 = (char_u *)str_arg;
4302 size_t i;
4303
4304 for (i = 0; i < precision && *p1; i++)
4305 p1 += mb_ptr2len(p1);
4306
4307 str_arg_l = precision = p1 - (char_u *)str_arg;
4308 }
4309 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004310# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004311 break;
4312
4313 default:
4314 break;
4315 }
4316 break;
4317
4318 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4319 {
4320 /* NOTE: the u, o, x, X and p conversion specifiers
4321 * imply the value is unsigned; d implies a signed
4322 * value */
4323
4324 /* 0 if numeric argument is zero (or if pointer is
4325 * NULL for 'p'), +1 if greater than zero (or nonzero
4326 * for unsigned arguments), -1 if negative (unsigned
4327 * argument is never negative) */
4328 int arg_sign = 0;
4329
4330 /* only defined for length modifier h, or for no
4331 * length modifiers */
4332 int int_arg = 0;
4333 unsigned int uint_arg = 0;
4334
4335 /* only defined for length modifier l */
4336 long int long_arg = 0;
4337 unsigned long int ulong_arg = 0;
4338
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004339# ifdef FEAT_NUM64
4340 /* only defined for length modifier ll */
4341 varnumber_T llong_arg = 0;
4342 uvarnumber_T ullong_arg = 0;
4343# endif
4344
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004345 /* pointer argument value -only defined for p
4346 * conversion */
4347 void *ptr_arg = NULL;
4348
4349 if (fmt_spec == 'p')
4350 {
4351 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004352 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004354 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004355# endif
4356 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004357 if (ptr_arg != NULL)
4358 arg_sign = 1;
4359 }
4360 else if (fmt_spec == 'd')
4361 {
4362 /* signed */
4363 switch (length_modifier)
4364 {
4365 case '\0':
4366 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 /* char and short arguments are passed as int. */
4368 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004370 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371# endif
4372 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004373 if (int_arg > 0)
4374 arg_sign = 1;
4375 else if (int_arg < 0)
4376 arg_sign = -1;
4377 break;
4378 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004381 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382# endif
4383 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004384 if (long_arg > 0)
4385 arg_sign = 1;
4386 else if (long_arg < 0)
4387 arg_sign = -1;
4388 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004389# ifdef FEAT_NUM64
4390 case 'L':
4391 llong_arg =
4392# if defined(FEAT_EVAL)
4393 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4394# endif
4395 va_arg(ap, varnumber_T);
4396 if (llong_arg > 0)
4397 arg_sign = 1;
4398 else if (llong_arg < 0)
4399 arg_sign = -1;
4400 break;
4401# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004402 }
4403 }
4404 else
4405 {
4406 /* unsigned */
4407 switch (length_modifier)
4408 {
4409 case '\0':
4410 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004413 tvs != NULL ? (unsigned)
4414 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415# endif
4416 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004417 if (uint_arg != 0)
4418 arg_sign = 1;
4419 break;
4420 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004423 tvs != NULL ? (unsigned long)
4424 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425# endif
4426 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004427 if (ulong_arg != 0)
4428 arg_sign = 1;
4429 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004430# ifdef FEAT_NUM64
4431 case 'L':
4432 ullong_arg =
4433# if defined(FEAT_EVAL)
4434 tvs != NULL ? (uvarnumber_T)
4435 tv_nr(tvs, &arg_idx) :
4436# endif
4437 va_arg(ap, uvarnumber_T);
4438 if (ullong_arg != 0)
4439 arg_sign = 1;
4440 break;
4441# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004442 }
4443 }
4444
4445 str_arg = tmp;
4446 str_arg_l = 0;
4447
4448 /* NOTE:
4449 * For d, i, u, o, x, and X conversions, if precision is
4450 * specified, the '0' flag should be ignored. This is so
4451 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4452 * FreeBSD, NetBSD; but not with Perl.
4453 */
4454 if (precision_specified)
4455 zero_padding = 0;
4456 if (fmt_spec == 'd')
4457 {
4458 if (force_sign && arg_sign >= 0)
4459 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4460 /* leave negative numbers for sprintf to handle, to
4461 * avoid handling tricky cases like (short int)-32768 */
4462 }
4463 else if (alternate_form)
4464 {
4465 if (arg_sign != 0
4466 && (fmt_spec == 'x' || fmt_spec == 'X') )
4467 {
4468 tmp[str_arg_l++] = '0';
4469 tmp[str_arg_l++] = fmt_spec;
4470 }
4471 /* alternate form should have no effect for p
4472 * conversion, but ... */
4473 }
4474
4475 zero_padding_insertion_ind = str_arg_l;
4476 if (!precision_specified)
4477 precision = 1; /* default precision is 1 */
4478 if (precision == 0 && arg_sign == 0)
4479 {
4480 /* When zero value is formatted with an explicit
4481 * precision 0, the resulting formatted string is
4482 * empty (d, i, u, o, x, X, p). */
4483 }
4484 else
4485 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004486 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004487 int f_l = 0;
4488
4489 /* construct a simple format string for sprintf */
4490 f[f_l++] = '%';
4491 if (!length_modifier)
4492 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004493 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004494 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004495# ifdef FEAT_NUM64
4496# ifdef WIN3264
4497 f[f_l++] = 'I';
4498 f[f_l++] = '6';
4499 f[f_l++] = '4';
4500# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004501 f[f_l++] = 'l';
4502 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004503# endif
4504# else
4505 f[f_l++] = 'l';
4506# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004507 }
4508 else
4509 f[f_l++] = length_modifier;
4510 f[f_l++] = fmt_spec;
4511 f[f_l++] = '\0';
4512
4513 if (fmt_spec == 'p')
4514 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4515 else if (fmt_spec == 'd')
4516 {
4517 /* signed */
4518 switch (length_modifier)
4519 {
4520 case '\0':
4521 case 'h': str_arg_l += sprintf(
4522 tmp + str_arg_l, f, int_arg);
4523 break;
4524 case 'l': str_arg_l += sprintf(
4525 tmp + str_arg_l, f, long_arg);
4526 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004527# ifdef FEAT_NUM64
4528 case 'L': str_arg_l += sprintf(
4529 tmp + str_arg_l, f, llong_arg);
4530 break;
4531# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 }
4533 }
4534 else
4535 {
4536 /* unsigned */
4537 switch (length_modifier)
4538 {
4539 case '\0':
4540 case 'h': str_arg_l += sprintf(
4541 tmp + str_arg_l, f, uint_arg);
4542 break;
4543 case 'l': str_arg_l += sprintf(
4544 tmp + str_arg_l, f, ulong_arg);
4545 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004546# ifdef FEAT_NUM64
4547 case 'L': str_arg_l += sprintf(
4548 tmp + str_arg_l, f, ullong_arg);
4549 break;
4550# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004551 }
4552 }
4553
4554 /* include the optional minus sign and possible
4555 * "0x" in the region before the zero padding
4556 * insertion point */
4557 if (zero_padding_insertion_ind < str_arg_l
4558 && tmp[zero_padding_insertion_ind] == '-')
4559 zero_padding_insertion_ind++;
4560 if (zero_padding_insertion_ind + 1 < str_arg_l
4561 && tmp[zero_padding_insertion_ind] == '0'
4562 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4563 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4564 zero_padding_insertion_ind += 2;
4565 }
4566
4567 {
4568 size_t num_of_digits = str_arg_l
4569 - zero_padding_insertion_ind;
4570
4571 if (alternate_form && fmt_spec == 'o'
4572 /* unless zero is already the first
4573 * character */
4574 && !(zero_padding_insertion_ind < str_arg_l
4575 && tmp[zero_padding_insertion_ind] == '0'))
4576 {
4577 /* assure leading zero for alternate-form
4578 * octal numbers */
4579 if (!precision_specified
4580 || precision < num_of_digits + 1)
4581 {
4582 /* precision is increased to force the
4583 * first character to be zero, except if a
4584 * zero value is formatted with an
4585 * explicit precision of zero */
4586 precision = num_of_digits + 1;
4587 precision_specified = 1;
4588 }
4589 }
4590 /* zero padding to specified precision? */
4591 if (num_of_digits < precision)
4592 number_of_zeros_to_pad = precision - num_of_digits;
4593 }
4594 /* zero padding to specified minimal field width? */
4595 if (!justify_left && zero_padding)
4596 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004597 int n = (int)(min_field_width - (str_arg_l
4598 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004599 if (n > 0)
4600 number_of_zeros_to_pad += n;
4601 }
4602 break;
4603 }
4604
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004605# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004606 case 'f':
4607 case 'e':
4608 case 'E':
4609 case 'g':
4610 case 'G':
4611 {
4612 /* Floating point. */
4613 double f;
4614 double abs_f;
4615 char format[40];
4616 int l;
4617 int remove_trailing_zeroes = FALSE;
4618
4619 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004620# if defined(FEAT_EVAL)
4621 tvs != NULL ? tv_float(tvs, &arg_idx) :
4622# endif
4623 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004624 abs_f = f < 0 ? -f : f;
4625
4626 if (fmt_spec == 'g' || fmt_spec == 'G')
4627 {
4628 /* Would be nice to use %g directly, but it prints
4629 * "1.0" as "1", we don't want that. */
4630 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4631 || abs_f == 0.0)
4632 fmt_spec = 'f';
4633 else
4634 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4635 remove_trailing_zeroes = TRUE;
4636 }
4637
Bram Moolenaarc9372132009-01-13 15:38:37 +00004638 if (fmt_spec == 'f' &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004639# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004640 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004641# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004642 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004643# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004644 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004645 {
4646 /* Avoid a buffer overflow */
4647 strcpy(tmp, "inf");
4648 str_arg_l = 3;
4649 }
4650 else
4651 {
4652 format[0] = '%';
4653 l = 1;
4654 if (precision_specified)
4655 {
4656 size_t max_prec = TMP_LEN - 10;
4657
4658 /* Make sure we don't get more digits than we
4659 * have room for. */
4660 if (fmt_spec == 'f' && abs_f > 1.0)
4661 max_prec -= (size_t)log10(abs_f);
4662 if (precision > max_prec)
4663 precision = max_prec;
4664 l += sprintf(format + 1, ".%d", (int)precision);
4665 }
4666 format[l] = fmt_spec;
4667 format[l + 1] = NUL;
4668 str_arg_l = sprintf(tmp, format, f);
4669
4670 if (remove_trailing_zeroes)
4671 {
4672 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004673 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004674
4675 /* Using %g or %G: remove superfluous zeroes. */
4676 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004677 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004678 else
4679 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004680 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004681 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004682 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004683 {
4684 /* Remove superfluous '+' and leading
4685 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004686 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004687 {
4688 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004689 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004690 --str_arg_l;
4691 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004692 i = (tp[1] == '-') ? 2 : 1;
4693 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004694 {
4695 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004696 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004697 --str_arg_l;
4698 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004699 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004700 }
4701 }
4702
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004703 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004704 /* Remove trailing zeroes, but keep the one
4705 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004706 while (tp > tmp + 2 && *tp == '0'
4707 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004708 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004709 STRMOVE(tp, tp + 1);
4710 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004711 --str_arg_l;
4712 }
4713 }
4714 else
4715 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004716 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004717
4718 /* Be consistent: some printf("%e") use 1.0e+12
4719 * and some 1.0e+012. Remove one zero in the last
4720 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004721 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004722 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004723 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4724 && tp[2] == '0'
4725 && vim_isdigit(tp[3])
4726 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004727 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004728 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004729 --str_arg_l;
4730 }
4731 }
4732 }
4733 str_arg = tmp;
4734 break;
4735 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004736# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004737
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004738 default:
4739 /* unrecognized conversion specifier, keep format string
4740 * as-is */
4741 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004742 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004744 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004745
4746 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004747 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004748 str_arg = p;
4749 str_arg_l = 0;
4750 if (*p != NUL)
4751 str_arg_l++; /* include invalid conversion specifier
4752 unchanged if not at end-of-string */
4753 break;
4754 }
4755
4756 if (*p != NUL)
4757 p++; /* step over the just processed conversion specifier */
4758
4759 /* insert padding to the left as requested by min_field_width;
4760 * this does not include the zero padding in case of numerical
4761 * conversions*/
4762 if (!justify_left)
4763 {
4764 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004765 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004766
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004767 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004768 {
4769 if (str_l < str_m)
4770 {
4771 size_t avail = str_m - str_l;
4772
4773 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004774 (size_t)pn > avail ? avail
4775 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004776 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004777 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004778 }
4779 }
4780
4781 /* zero padding as requested by the precision or by the minimal
4782 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004783 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004784 {
4785 /* will not copy first part of numeric right now, *
4786 * force it to be copied later in its entirety */
4787 zero_padding_insertion_ind = 0;
4788 }
4789 else
4790 {
4791 /* insert first part of numerics (sign or '0x') before zero
4792 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004793 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004794
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004795 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004796 {
4797 if (str_l < str_m)
4798 {
4799 size_t avail = str_m - str_l;
4800
4801 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004802 (size_t)zn > avail ? avail
4803 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004804 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004805 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004806 }
4807
4808 /* insert zero padding as requested by the precision or min
4809 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004810 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004811 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004812 {
4813 if (str_l < str_m)
4814 {
4815 size_t avail = str_m-str_l;
4816
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004817 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004818 (size_t)zn > avail ? avail
4819 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004820 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004821 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004822 }
4823 }
4824
4825 /* insert formatted string
4826 * (or as-is conversion specifier for unknown conversions) */
4827 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004828 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004829
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004830 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004831 {
4832 if (str_l < str_m)
4833 {
4834 size_t avail = str_m - str_l;
4835
4836 mch_memmove(str + str_l,
4837 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004838 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004839 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004840 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004841 }
4842 }
4843
4844 /* insert right padding */
4845 if (justify_left)
4846 {
4847 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004848 int pn = (int)(min_field_width
4849 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004850
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004851 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004852 {
4853 if (str_l < str_m)
4854 {
4855 size_t avail = str_m - str_l;
4856
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004857 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004858 (size_t)pn > avail ? avail
4859 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004860 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004861 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004862 }
4863 }
4864 }
4865 }
4866
4867 if (str_m > 0)
4868 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004869 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004870 * overwriting the last character (shouldn't happen, but just in case)
4871 * */
4872 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4873 }
4874
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004875 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004876 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004878 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004879 * character), that is, the number of characters that would have been
4880 * written to the buffer if it were large enough. */
4881 return (int)str_l;
4882}
4883
4884#endif /* PROTO */